跳到主要内容

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

浏览文件

state.go

169 行 · 5.2 KB历史下载
1// Package state is agent-vm's folder, ~/.agent-vm (AGENT_VM_STATE_DIR): the
2// user's files (env, volumes, runtime.sh, setup.sh) and agent-vm's markers,
3// one file per VM and fact, named as 0.2 named them.
4package state
5
6import (
7 "errors"
8 "os"
9 "path/filepath"
10 "regexp"
11 "strconv"
12 "strings"
13 "time"
14
15 "github.com/sylvinus/agent-vm/internal/paths"
16)
17
18// Dir is the state folder.
19type Dir string
20
21// Default is $AGENT_VM_STATE_DIR, made absolute so that the checks that keep
22// shares away from it compare real locations, or ~/.agent-vm, spelled as
23// paths.Host does.
24func Default() (Dir, error) {
25 if d := os.Getenv("AGENT_VM_STATE_DIR"); d != "" {
26 abs, err := filepath.Abs(d)
27 if err != nil {
28 return "", err
29 }
30 return Dir(paths.Host(abs)), nil
31 }
32 home, err := os.UserHomeDir()
33 if err != nil {
34 return "", err
35 }
36 return Dir(paths.Host(filepath.Join(home, ".agent-vm"))), nil
37}
38
39// Path is name in the folder.
40func (d Dir) Path(name string) string {
41 return paths.Host(filepath.Join(string(d), name))
42}
43
44// The markers of a VM.
45const (
46 VersionOf = ".agent-vm-version-" // the base's build time it was cloned from
47 BuiltByOf = ".agent-vm-built-by-" // the agent-vm version that built that base
48 SSHFSOf = ".agent-vm-sshfs-" // 0.1 migration done
49 TermOf = ".agent-vm-term-" // the $TERM whose terminfo it has
50 FileMountsOf = ".agent-vm-file-mounts-" // single files of ~/.agent-vm/volumes
51 MountsOf = ".agent-vm-mounts-" // 0.2's record of its shares: only removed now
52 CacheOf = ".agent-vm-sshfs-cache-" // 0.2's record of sshfs's cache: only removed now
53 ScratchOf = ".agent-vm-scratch-" // a --scratch run, with its pid
54 PolicyOf = ".agent-vm-policy-" // fingerprint of the network and guarded policies it booted with
55)
56
57// The base template's markers. BaseVersion, written last by setup, says it
58// can be cloned.
59const (
60 BaseVersion = ".agent-vm-base-version"
61 BaseBuiltBy = ".agent-vm-base-built-by"
62)
63
64// Marker is the path of marker kind (one of the ...Of) for vm.
65func (d Dir) Marker(kind, vm string) string {
66 return d.Path(kind + vm)
67}
68
69// Read is the content of name in the folder, without its trailing newline,
70// "" when there is none.
71func (d Dir) Read(name string) string {
72 b, err := os.ReadFile(d.Path(name))
73 if err != nil {
74 return ""
75 }
76 return strings.TrimSuffix(string(b), "\n")
77}
78
79// Write replaces name in the folder, made if need be, through a temporary
80// file renamed over it: a reader sees the old content or the new, never part
81// of it. The temporary name does not start like a marker's.
82func (d Dir) Write(name string, data []byte) error {
83 if err := os.MkdirAll(string(d), 0o755); err != nil {
84 return err
85 }
86 tmp, err := os.CreateTemp(string(d), ".tmp-"+name+"-*")
87 if err != nil {
88 return err
89 }
90 _, werr := tmp.Write(data)
91 if err := errors.Join(werr, tmp.Chmod(0o644), tmp.Close()); err != nil {
92 os.Remove(tmp.Name())
93 return err
94 }
95 if err := os.Rename(tmp.Name(), d.Path(name)); err != nil {
96 os.Remove(tmp.Name())
97 return err
98 }
99 return nil
100}
101
102// ReadMarker is Read for a marker of vm.
103func (d Dir) ReadMarker(kind, vm string) string {
104 return d.Read(kind + vm)
105}
106
107var markers = []string{VersionOf, BuiltByOf, SSHFSOf, TermOf, FileMountsOf, MountsOf, CacheOf, ScratchOf, PolicyOf}
108
109// Rename moves what is known of the VM from to its new name to: its
110// markers, and its staged single files (its shares are set again before it
111// next boots, from where they now are).
112func (d Dir) Rename(from, to string) error {
113 var last error
114 for _, kind := range markers {
115 if err := os.Rename(d.Marker(kind, from), d.Marker(kind, to)); err != nil && !os.IsNotExist(err) {
116 last = err
117 }
118 }
119 if err := os.Rename(d.Path(filepath.Join("file-mounts", from)), d.Path(filepath.Join("file-mounts", to))); err != nil && !os.IsNotExist(err) {
120 last = err
121 }
122 return last
123}
124
125// Forget removes what is known of vm, once deleted. Deleting the base
126// template retires the marker that says it can be cloned.
127func (d Dir) Forget(vm, template string) error {
128 var last error
129 for _, kind := range markers {
130 if err := os.Remove(d.Marker(kind, vm)); err != nil && !os.IsNotExist(err) {
131 last = err
132 }
133 }
134 if err := os.RemoveAll(d.Path(filepath.Join("file-mounts", vm))); err != nil {
135 last = err
136 }
137 if vm == template {
138 for _, f := range []string{BaseVersion, BaseBuiltBy} {
139 if err := os.Remove(d.Path(f)); err != nil && !os.IsNotExist(err) {
140 last = err
141 }
142 }
143 }
144 return last
145}
146
147var builtByRe = regexp.MustCompile(`^[0-9A-Za-z.+-]+$`)
148
149// BaseLabel is the base vm was cloned from, for `list`: the agent-vm version
150// that built it, and the day, "-" when not known. The base template
151// describes itself. 0.1.0 recorded no version: a base without one was built
152// by it.
153func (d Dir) BaseLabel(vm, template string) string {
154 verFile, byFile := VersionOf+vm, BuiltByOf+vm
155 if vm == template {
156 verFile, byFile = BaseVersion, BaseBuiltBy
157 }
158 by := d.Read(byFile)
159 if !builtByRe.MatchString(by) {
160 by = "0.1.0"
161 }
162 day := "-"
163 if built := d.Read(verFile); built != "" {
164 if sec, err := strconv.ParseInt(built, 10, 64); err == nil && sec >= 0 && strings.Trim(built, "0123456789") == "" {
165 day = time.Unix(sec, 0).Format("2006-01-02")
166 }
167 }
168 return by + " " + day
169}