跳到主要内容

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

浏览文件

environment.go

124 行 · 4.3 KB历史下载
1package cage
2
3import (
4 "encoding/json"
5 "errors"
6 "os"
7 "path/filepath"
8 "strings"
9
10 "github.com/sylvinus/agent-vm/internal/paths"
11 "github.com/sylvinus/agent-vm/internal/vmname"
12)
13
14type EnvironmentRecord struct {
15 Directory string `json:"directory"`
16 IdentityDirectory string `json:"identity_directory,omitempty"`
17}
18
19func LoadEnvironmentRecord(profile string) (EnvironmentRecord, error) {
20 var saved EnvironmentRecord
21 data, err := os.ReadFile(filepath.Join(profile, "environment.json"))
22 if errors.Is(err, os.ErrNotExist) {
23 return saved, nil
24 }
25 if err != nil {
26 return saved, err
27 }
28 if json.Unmarshal(data, &saved) != nil || !filepath.IsAbs(saved.Directory) ||
29 saved.IdentityDirectory != "" && !filepath.IsAbs(saved.IdentityDirectory) {
30 return saved, errors.New("环境工作目录记录无效,请保留原配置并导出诊断记录")
31 }
32 return saved, nil
33}
34
35// DefaultEnvironmentDir keeps generated workspaces outside the default profile,
36// including independent profiles stored below that profile's directory.
37func DefaultEnvironmentDir(profile string) string {
38 path := filepath.Clean(profile) + "-workspace"
39 if home, err := os.UserHomeDir(); err == nil {
40 defaultProfile := filepath.Join(home, ".claude-cage")
41 if !paths.Equal(profile, defaultProfile) && StorageContains(defaultProfile, profile) {
42 if relative, err := filepath.Rel(defaultProfile, profile); err == nil {
43 path = filepath.Join(defaultProfile+"-workspaces", relative)
44 }
45 }
46 }
47 if real, err := filepath.EvalSymlinks(path); err == nil {
48 return real
49 }
50 return path
51}
52
53// EnvironmentDir is the default shared workspace, independent of the currently
54// selected project. IdentityDirectory retains a legacy VM's naming input.
55func EnvironmentDir(profile string) string {
56 if saved, err := LoadEnvironmentRecord(profile); err == nil && saved.Directory != "" {
57 return saved.Directory
58 }
59 // CLI and desktop upgrades must retain the same existing virtual disk.
60 var legacy struct {
61 Workspace string `json:"workspace"`
62 }
63 if b, err := os.ReadFile(filepath.Join(profile, "workbench.json")); err == nil && json.Unmarshal(b, &legacy) == nil && filepath.IsAbs(legacy.Workspace) {
64 if _, err := os.Stat(filepath.Join(profile, "state", "lima", InstanceName(profile, legacy.Workspace), "lima.yaml")); err == nil {
65 return legacy.Workspace
66 }
67 }
68 previous := filepath.Clean(profile) + "-workspace"
69 if _, err := os.Stat(filepath.Join(profile, "state", "lima", InstanceName(profile, previous), "lima.yaml")); err == nil {
70 return previous
71 }
72 return DefaultEnvironmentDir(profile)
73}
74
75func EnvironmentIdentityDir(profile string) string {
76 if saved, err := LoadEnvironmentRecord(profile); err == nil && saved.IdentityDirectory != "" {
77 return saved.IdentityDirectory
78 }
79 return EnvironmentDir(profile)
80}
81
82// InstanceName is shared by the controller and each Cage CLI subprocess.
83// It uses the same spelling as the CLI's AbsDir, including Windows drives.
84func InstanceName(profile, workspace string) string {
85 if saved, err := LoadEnvironmentRecord(profile); err == nil && saved.IdentityDirectory != "" &&
86 paths.Equal(paths.Host(filepath.Clean(workspace)), paths.Host(filepath.Clean(saved.Directory))) {
87 workspace = saved.IdentityDirectory
88 }
89 home := filepath.Join(profile, "state", "lima")
90 _, err := os.Stat(filepath.Join(profile, locationFile))
91 logical := err == nil
92 if logical {
93 if real, e := filepath.EvalSymlinks(profile); e == nil {
94 home = filepath.Join(real, "state", "lima")
95 }
96 }
97 dir := paths.Host(filepath.Clean(workspace))
98 expected := vmname.HomeLimit(home, logical).Name(dir)
99 if _, err := os.Stat(filepath.Join(home, expected)); err == nil {
100 return expected
101 }
102 // A profile restored from a different path can contain an instance made
103 // under a previous socket limit. Only accept a unique, valid truncation
104 // of this exact workspace's name, confirmed by a persistent Lima config.
105 entries, _ := os.ReadDir(home)
106 var old string
107 for _, entry := range entries {
108 name := entry.Name()
109 if !entry.IsDir() || !strings.HasSuffix(name, expected[len(expected)-9:]) || vmname.Limit(len(name)).Name(dir) != name {
110 continue
111 }
112 if _, err := os.Stat(filepath.Join(home, name, "lima.yaml")); err != nil {
113 continue
114 }
115 if old != "" {
116 return expected
117 }
118 old = name
119 }
120 if old != "" {
121 return old
122 }
123 return expected
124}