跳到主要内容

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

浏览文件

build.go

155 行 · 5.0 KB历史下载
1package mounts
2
3import (
4 "fmt"
5 "io"
6 "os"
7 "path/filepath"
8 "strconv"
9 "strings"
10
11 vm "github.com/sylvinus/agent-vm/internal/mounts/spec"
12 "github.com/sylvinus/agent-vm/internal/paths"
13)
14
15// FileMount is a single file of ~/.agent-vm/volumes: hardlinked into a
16// staging folder of its own, which is shared, then bound in place in the VM
17// (BindSrc onto BindDst), so the VM never sees the file's folder.
18type FileMount struct {
19 Src, Staging, BindSrc, BindDst string
20}
21
22// Line is how the state folder records it, as 0.2 did.
23func (f FileMount) Line() string {
24 return strings.Join([]string{f.Src, f.Staging, f.BindSrc, f.BindDst}, "|")
25}
26
27// CacheOn is sshfs's cache mode asked from the shell: on with
28// AGENT_VM_SSHFS_CACHE=1, never from the project. With it, a file the host
29// adds, deletes or extends shows as it was for up to 20 seconds, and an
30// agent writing back what it read would undo the host's change; it is off
31// on the writable shares otherwise.
32func CacheOn() bool { return os.Getenv("AGENT_VM_SSHFS_CACHE") == "1" }
33
34// Shares is what Build is given.
35type Shares struct {
36 VM string
37 Dir string // the project
38 Writable bool // false for --readonly, every share
39 // Names are read-only on every share (none with .git left writable).
40 // Every share is served by the builtin SFTP server: the one confined to
41 // its folder (vm.Confined).
42 Names []string
43 Entries []Entry // see Entries
44 StateDir string // where single files are staged
45 Warn io.Writer
46}
47
48// Build is the shares of the project's VM: the project first, then the
49// entries, read-only unless rw (and never writable under --readonly: a
50// writable volume holding the project would be a second way to write it).
51func Build(s Shares) ([]vm.Mount, []FileMount) {
52 w := func(format string, a ...any) { fmt.Fprintf(s.Warn, format+"\n", a...) }
53 opts := func(writable bool) *vm.SSHFS {
54 o := &vm.SSHFS{SFTPDriver: "builtin", ReadonlyNames: append([]string(nil), s.Names...)}
55 if writable && !CacheOn() {
56 off := false
57 o.Cache = &off
58 }
59 return o
60 }
61 out := []vm.Mount{{Location: s.Dir, MountPoint: paths.Guest(s.Dir), Writable: s.Writable, SSHFS: opts(s.Writable)}}
62 var files []FileMount
63 idx := 0
64 for _, e := range s.Entries {
65 dst := e.Dst
66 if dst != "" && !strings.HasPrefix(dst, "/") {
67 p, ok := ProjectMountpoint(s.Dir, dst, e.Src, s.Warn)
68 if !ok {
69 continue
70 }
71 dst = paths.Guest(p)
72 }
73 if isFile(e.Src) {
74 if e.Mode == "rw" {
75 w("Warning: Mount entry '%s' (from ~/.agent-vm/volumes) requests rw on a file; only directories support rw. Mount the parent directory instead. Skipping.", e.Line)
76 continue
77 }
78 dir := paths.Host(filepath.Join(s.StateDir, "file-mounts", s.VM, strconv.Itoa(idx)))
79 staged := dir + "/" + filepath.Base(e.Src)
80 if !Stage(e.Src, staged, s.Warn) {
81 w("Warning: Failed to stage '%s', skipping.", e.Src)
82 os.RemoveAll(dir)
83 continue
84 }
85 mp := "/tmp/.agent-vm-file-mounts/" + strconv.Itoa(idx)
86 bind := dst
87 if bind == "" {
88 bind = paths.Guest(e.Src)
89 }
90 files = append(files, FileMount{Src: e.Src, Staging: staged, BindSrc: mp + "/" + filepath.Base(e.Src), BindDst: bind})
91 out = append(out, vm.Mount{Location: dir, MountPoint: mp, SSHFS: opts(false)})
92 idx++
93 continue
94 }
95 if !isDir(e.Src) {
96 w("Warning: Mount path '%s' (from ~/.agent-vm/volumes) is not a regular file or directory, skipping.", e.Src)
97 continue
98 }
99 writable := false
100 if e.Mode == "rw" {
101 if s.Writable {
102 writable = true
103 } else {
104 w("Note: --readonly: '%s' (rw in ~/.agent-vm/volumes) is mounted read-only too.", e.Src)
105 }
106 }
107 mp := dst
108 if mp == "" {
109 mp = paths.Guest(e.Src)
110 }
111 out = append(out, vm.Mount{Location: e.Src, MountPoint: mp, Writable: writable, SSHFS: opts(writable)})
112 }
113 return out, files
114}
115
116// Stage puts src at dst by a hardlink, which keeps it in step with the host
117// (same inode) without sharing its folder; by a copy when they are on two
118// file systems, with a warning: then changes reach the VM on its next start.
119func Stage(src, dst string, warn io.Writer) bool {
120 if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
121 return false
122 }
123 os.Remove(dst)
124 // The file itself: link(2) links a symlink as one (stow's dotfiles), which
125 // would leave dst pointing out of the folder the VM is given. The checks
126 // made on src were on that file too (UnsafeLocation).
127 physical, err := paths.Resolve(src)
128 if err != nil {
129 return false
130 }
131 src = physical
132 if os.Link(src, dst) == nil {
133 return true
134 }
135 in, err := os.Open(src)
136 if err != nil {
137 return false
138 }
139 defer in.Close()
140 fi, err := in.Stat()
141 if err != nil {
142 return false
143 }
144 out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode().Perm())
145 if err != nil {
146 return false
147 }
148 _, cerr := io.Copy(out, in)
149 if err := out.Close(); cerr != nil || err != nil {
150 return false
151 }
152 os.Chtimes(dst, fi.ModTime(), fi.ModTime())
153 fmt.Fprintf(warn, "Warning: Staged '%s' via copy (cross-filesystem hardlink failed); live host changes will not propagate until VM (re)start.\n", src)
154 return true
155}