| 1 | package cage |
| 2 | |
| 3 | import ( |
| 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 | |
| 14 | type EnvironmentRecord struct { |
| 15 | Directory string `json:"directory"` |
| 16 | IdentityDirectory string `json:"identity_directory,omitempty"` |
| 17 | } |
| 18 | |
| 19 | func 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 | |
| 36 | |
| 37 | func 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 | |
| 54 | |
| 55 | func EnvironmentDir(profile string) string { |
| 56 | if saved, err := LoadEnvironmentRecord(profile); err == nil && saved.Directory != "" { |
| 57 | return saved.Directory |
| 58 | } |
| 59 | |
| 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 | |
| 75 | func 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 | |
| 83 | |
| 84 | func 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 | |
| 103 | |
| 104 | |
| 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 | } |