跳到主要内容

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

浏览文件

info.go

167 行 · 5.1 KB历史下载
1package cli
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "github.com/sylvinus/agent-vm/internal/env"
13 "github.com/sylvinus/agent-vm/internal/gitguard"
14 "github.com/sylvinus/agent-vm/internal/mounts"
15 "github.com/sylvinus/agent-vm/internal/paths"
16 "github.com/sylvinus/agent-vm/internal/state"
17 "github.com/sylvinus/agent-vm/internal/version"
18 "github.com/sylvinus/agent-vm/internal/vm"
19 "github.com/sylvinus/agent-vm/internal/vmname"
20)
21
22// refs are the locations no share may reach (see mounts.Refs).
23func (e *app) refs() []mounts.Ref {
24 self := ""
25 if exe, err := os.Executable(); err == nil {
26 if p, err := paths.Resolve(exe); err == nil {
27 self = paths.Host(filepath.Dir(p))
28 }
29 }
30 private := []string{LimaHome(e.state), oldLimaHome()}
31 private = append(private, e.privateStorage...)
32 return mounts.Refs(paths.Home(), self, string(e.state), private...)
33}
34
35// entries are the usable entries of ~/.agent-vm/volumes for the project
36// dir, their warnings on warn.
37func (e *app) entries(dir string, warn io.Writer) []mounts.Entry {
38 return mounts.Entries(e.state.Path("volumes"), dir, paths.Home(), e.refs(), warn)
39}
40
41// scan is the git scan of the project dir's writable shares (see
42// gitguard.Scan), the volumes read quietly.
43func (e *app) scan(ctx context.Context, dir string) *gitguard.Result {
44 shares := gitguard.Shares(dir, mounts.RWDirs(e.entries(dir, io.Discard)))
45 return gitguard.Scan(ctx, shares, paths.Home())
46}
47
48// Cage projects support Git writes by default. Standalone agent-vm requires
49// an explicit host-side opt-out.
50func writableGitOptOut(flag bool) bool {
51 return cageWritableGit() || flag || os.Getenv("AGENT_VM_UNSAFE_WRITABLE_GIT") == "1"
52}
53
54func cageWritableGit() bool { return os.Getenv("CLAUDE_CAGE_CONFIG") != "" }
55
56// tristate is a key's value: 1, 0, or unknown when the backend could not be
57// asked, never a confident 0.
58func tristate(ok bool, err error) string {
59 switch {
60 case err != nil:
61 return "unknown"
62 case ok:
63 return "1"
64 }
65 return "0"
66}
67
68// `info [dir]`: machine-readable state, one key=value per line, the
69// supported way for another tool to ask what agent-vm knows. Booleans are
70// 1/0; what cannot be told is "unknown".
71//
72// security_questions: what a start with writable shares of a stopped VM
73// would stop on unless answered yes, comma-separated, or "none": hooks (a
74// hooks folder that cannot be protected), git-config (git config or commands
75// in a share), bare-repo (safe.bareRepository).
76func (e *app) infoCmd(ctx context.Context, args []string) int {
77 d := ""
78 if len(args) > 0 {
79 d = args[0]
80 }
81 dir, err := vmname.AbsDir(d)
82 if err != nil {
83 fmt.Fprintf(e.io.Stderr, "Error: %v\n", err)
84 return 1
85 }
86 name := e.instanceName(dir)
87 w := e.io.Stdout
88 fmt.Fprintf(w, "version=%s\n", version.Version)
89 fmt.Fprintf(w, "template=%s\n", vmname.Template)
90 fmt.Fprintf(w, "state_dir=%s\n", e.state)
91 fmt.Fprintf(w, "project_env=%s\n", env.ProjectEnvFile(dir))
92 fmt.Fprintf(w, "dir=%s\n", dir)
93 fmt.Fprintf(w, "vm_name=%s\n", name)
94
95 b, berr := e.readOnlyBackend()
96 get := func(n string) (*vm.Instance, error) {
97 if berr != nil {
98 return nil, berr
99 }
100 inst, err := b.Get(ctx, n)
101 if errors.Is(err, vm.ErrNotFound) {
102 return nil, nil
103 }
104 return inst, err
105 }
106 base, baseErr := get(vmname.Template)
107 _, markErr := os.Stat(e.state.Path(state.BaseVersion))
108 fmt.Fprintf(w, "base_exists=%s\n", tristate(base != nil && markErr == nil, baseErr))
109 inst, instErr := get(name)
110 fmt.Fprintf(w, "vm_exists=%s\n", tristate(inst != nil, instErr))
111 fmt.Fprintf(w, "vm_running=%s\n", tristate(inst != nil && inst.Status == vm.Running, instErr))
112 stale := "unknown"
113 if inst != nil {
114 stale = e.staleState(name)
115 }
116 fmt.Fprintf(w, "vm_stale=%s\n", stale)
117 // Lima names the alias after the instance.
118 fmt.Fprintf(w, "ssh_host=lima-%s\n", name)
119 cfg := "unknown"
120 if inst != nil && inst.SSHConfig != "" {
121 cfg = inst.SSHConfig
122 }
123 fmt.Fprintf(w, "ssh_config=%s\n", cfg)
124
125 var questions []string
126 protected := "1"
127 if writableGitOptOut(false) {
128 protected = "0"
129 } else {
130 scan := e.scan(ctx, dir)
131 for _, h := range scan.Hooks {
132 rel, inRepo, _ := strings.Cut(h, "\t")
133 if _, ok := gitguard.HooksName(inRepo); rel != "" && !ok {
134 questions = append(questions, "hooks")
135 break
136 }
137 }
138 if len(scan.Risks(scan.Names())) > 0 {
139 questions = append(questions, "git-config")
140 }
141 }
142 if st := gitguard.BareState(ctx); st == gitguard.BareUnset || st == gitguard.BareOld {
143 questions = append(questions, "bare-repo")
144 }
145 fmt.Fprintf(w, "git_protected=%s\n", protected)
146 q := strings.Join(questions, ",")
147 if q == "" {
148 q = "none"
149 }
150 fmt.Fprintf(w, "security_questions=%s\n", q)
151 return 0
152}
153
154// staleState says whether vm was cloned from an older base than the
155// current one: 1, 0, or unknown with nothing to compare against. A VM with
156// no record but a known base predates the record, which makes it stale.
157func (e *app) staleState(name string) string {
158 base, err := os.ReadFile(e.state.Path(state.BaseVersion))
159 if err != nil {
160 return "unknown"
161 }
162 mine, err := os.ReadFile(e.state.Marker(state.VersionOf, name))
163 if err != nil || strings.TrimRight(string(mine), "\n") != strings.TrimRight(string(base), "\n") {
164 return "1"
165 }
166 return "0"
167}