跳到主要内容

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

浏览文件

probe.go

79 行 · 2.8 KB历史下载
1package cli
2
3import (
4 "fmt"
5 "io"
6 "math/rand/v2"
7 "os"
8 "path/filepath"
9 "strings"
10
11 "github.com/sylvinus/agent-vm/internal/paths"
12 "github.com/sylvinus/agent-vm/internal/vm"
13)
14
15// probeResult is what the guest answered.
16type probeResult struct {
17 writable string // "true", "false", or "lost": the share is not mounted
18 runtimeFound bool
19 envOK bool
20}
21
22// The guest's side of the probe, its arguments the project's path, the
23// project's env file and runtime script (by their paths in the VM), and the
24// token the host put in the project.
25const probeScript = `
26(umask 077 && rm -f "$HOME/.agent-vm.env" && { cat; [ -z "$2" ] || [ ! -f "$2" ] || awk "{ sub(/\r\$/, \"\"); print }" "$2"; } > "$HOME/.agent-vm.env") && echo env-ok
27[ -z "$3" ] || [ ! -f "$3" ] || echo runtime-found
28[ -z "$4" ] || [ ! -f "$1/$4" ] || echo share-ok
29p="$1/.agent-vm-write-probe.$$"; touch "$p" 2>/dev/null || exit 1; rm -f "$p"`
30
31// probe pushes the env and checks the project share, in one round trip:
32// - writes ~/.agent-vm.env in the VM (mode 600, sourced by its ~/.zshenv):
33// payload, then the project's env, read by the VM, CRs dropped; empty
34// too, so env removed on the host goes;
35// - says whether the project's runtime script is there;
36// - writes into the project, a real write removed at once, not `test -w`:
37// after a mode change the guest's fstab still shows the old one;
38// - "lost" when the share is not mounted (sshfs died, or never mounted),
39// where the VM writes on its own disk: the guest does not see the file
40// put in the project just before. Not for a scratch VM, which shares
41// nothing.
42func (s *start) probe(payload, guestEnv, guestRuntime string) probeResult {
43 token := ""
44 if !s.opts.Scratch {
45 t := fmt.Sprintf(".agent-vm-probe.%d.%d", os.Getpid(), rand.IntN(1e9))
46 // Never through something already there.
47 if f, err := os.OpenFile(filepath.Join(s.dir, t), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644); err == nil {
48 f.Close()
49 token = t
50 defer os.Remove(filepath.Join(s.dir, t))
51 }
52 }
53 in := ""
54 if payload != "" {
55 in = payload + "\n"
56 }
57 var out strings.Builder
58 b, err := s.backend()
59 code := 1
60 if err == nil {
61 code, err = b.Shell(s.ctx, s.name, vm.ShellOpts{
62 Args: []string{"sh", "-c", probeScript, "sh", paths.Guest(s.dir), guestEnv, guestRuntime, token},
63 Stdin: strings.NewReader(in), Stdout: &out, Stderr: io.Discard,
64 })
65 }
66 r := probeResult{
67 envOK: strings.Contains(out.String(), "env-ok"),
68 runtimeFound: strings.Contains(out.String(), "runtime-found"),
69 writable: fmt.Sprint(err == nil && code == 0),
70 }
71 // The guest answered, but without the share.
72 if token != "" && r.envOK && !strings.Contains(out.String(), "share-ok") {
73 r.writable = "lost"
74 }
75 if !r.envOK {
76 s.warnf("Warning: failed to push the env files into VM '%s'.", s.name)
77 }
78 return r
79}