跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

project.go

139 行 · 4.2 KB历史下载
1package env
2
3import (
4 "os"
5 "strings"
6
7 "github.com/sylvinus/agent-vm/internal/paths"
8)
9
10// ProjectPath is a project file's path: p relative to the project dir
11// unless absolute. An integrator moves the env file and the runtime script
12// into a folder of its own (".mytool/env") with AGENT_VM_PROJECT_ENV and
13// AGENT_VM_PROJECT_RUNTIME.
14func ProjectPath(dir, p string) string {
15 return paths.Abs(dir, p)
16}
17
18// ProjectEnvFile is the project dir's env file: in the project, so it
19// follows a clone, a move and a delete.
20func ProjectEnvFile(dir string) string {
21 p := os.Getenv("AGENT_VM_PROJECT_ENV")
22 if p == "" {
23 p = ".agent-vm.env"
24 }
25 return ProjectPath(dir, p)
26}
27
28// ProjectRel is target relative to the project dir, when it is inside it,
29// where the VM can write. The host must not read such a file by its path:
30// the VM can make it a symlink, and the host would follow it to any file of
31// the user's and hand the content over. It is read in the VM, or with
32// NoFollow.
33//
34// Inside by its spelling, or by where it leads: the project reached through
35// a link of the user's, the other spelling of a linked folder (/tmp and
36// /private/tmp on macOS), another case where the file system ignores it. So
37// target is resolved one component at a time, links followed one hop at a
38// time, and the first step that reaches the project decides, before
39// anything in it is followed. Before that point, every link is outside the
40// project, where the VM cannot change it.
41func ProjectRel(dir, target string) (string, bool) {
42 dir = strings.TrimSuffix(dir, "/")
43 if rest, ok := strings.CutPrefix(target, dir+"/"); ok {
44 return rest, true
45 }
46 pdir, err := paths.Real(dir)
47 if err != nil || pdir == "" {
48 return "", false
49 }
50 // From the root: "", or the drive on Windows ("C:").
51 root := paths.Root(target)
52 cur, hops := strings.TrimSuffix(root, "/"), 0
53 rest := strings.TrimPrefix(target, root) + "/"
54 for rest != "" {
55 var comp string
56 comp, rest, _ = strings.Cut(rest, "/")
57 switch comp {
58 case "", ".":
59 continue
60 case "..":
61 if i := strings.LastIndexByte(cur, '/'); i >= 0 {
62 cur = cur[:i]
63 }
64 continue
65 }
66 next := cur + "/" + comp
67 if inside, ok := paths.In(next, pdir); ok {
68 r := strings.TrimSuffix(rest, "/")
69 if inside != "." {
70 r = inside + "/" + r
71 }
72 return strings.TrimSuffix(strings.TrimPrefix(r, "/"), "/"), true
73 }
74 if fi, err := os.Lstat(next); err == nil && fi.Mode()&os.ModeSymlink != 0 {
75 if hops++; hops > 40 {
76 return "", false
77 }
78 t, err := os.Readlink(next)
79 if err != nil {
80 return "", false
81 }
82 if t = paths.Host(t); paths.IsAbs(t) {
83 r := paths.Root(t)
84 cur, t = strings.TrimSuffix(r, "/"), t[len(r):]
85 }
86 rest = strings.TrimPrefix(t, "/") + "/" + rest
87 continue
88 }
89 cur = next
90 }
91 return "", false
92}
93
94// InProject reports whether target is inside the project dir (see
95// ProjectRel).
96func InProject(dir, target string) bool {
97 _, ok := ProjectRel(dir, target)
98 return ok
99}
100
101// StripCR is content with a CR ending a line removed, each line ending with
102// a newline: a CRLF file (Windows) would put a CR at the end of every value
103// in the guest.
104func StripCR(content string) string {
105 var b strings.Builder
106 for _, l := range records(content) {
107 b.WriteString(strings.TrimSuffix(l, "\r") + "\n")
108 }
109 return b.String()
110}
111
112// Payload is what is pushed into the VM of the project dir: the shared file
113// first, the project's next, so the project's value wins (the guest sources
114// it). The project's file is only in here when AGENT_VM_PROJECT_ENV puts it
115// outside the project; inside, the VM reads it itself (see ProjectRel).
116func Payload(shared, dir string) string {
117 var b strings.Builder
118 if s, ok := readRegular(shared); ok {
119 // The newline keeps a shared file without one from gluing its last
120 // line to the project's first.
121 b.WriteString(StripCR(s) + "\n")
122 }
123 if pf := ProjectEnvFile(dir); !InProject(dir, pf) {
124 if p, ok := readRegular(pf); ok {
125 b.WriteString(StripCR(p))
126 }
127 }
128 return b.String()
129}
130
131// readRegular is the content of p, a regular file once links are followed:
132// one of the user's own, outside the project.
133func readRegular(p string) (string, bool) {
134 if fi, err := os.Stat(p); err != nil || !fi.Mode().IsRegular() {
135 return "", false
136 }
137 b, err := os.ReadFile(p)
138 return string(b), err == nil
139}