| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | _ "embed" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "path/filepath" |
| 14 | "time" |
| 15 | |
| 16 | "al.essio.dev/pkg/shellescape" |
| 17 | "github.com/sylvinus/agent-vm/internal/cage" |
| 18 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | var nativeComponentProbe string |
| 23 | |
| 24 | type nativeComponentSnapshot struct { |
| 25 | Manifest *cage.GuestManifest `json:"manifest"` |
| 26 | Tools map[string]string `json:"tools"` |
| 27 | Installed bool `json:"installed"` |
| 28 | Missing []string `json:"missing"` |
| 29 | Error string `json:"error"` |
| 30 | } |
| 31 | |
| 32 | func (s *Server) nativeComponentKit() (string, string) { |
| 33 | system := guestos.Windows11 |
| 34 | if s.macosGuest != nil { |
| 35 | system = guestos.MacOS |
| 36 | } |
| 37 | return filepath.Join(filepath.Dir(s.exe), "guest-os"), system |
| 38 | } |
| 39 | |
| 40 | func (s *Server) readNativeComponents(ctx context.Context) (nativeComponentSnapshot, error) { |
| 41 | var snapshot nativeComponentSnapshot |
| 42 | if s.macosGuest != nil { |
| 43 | command := shellescape.QuoteCommand([]string{"/usr/local/libexec/cage-cli/bin/python3", "-I", "-c", nativeComponentProbe}) |
| 44 | data, err := s.guest(ctx, "exec "+command) |
| 45 | if err != nil { |
| 46 | return snapshot, err |
| 47 | } |
| 48 | return snapshot, json.Unmarshal(data, &snapshot) |
| 49 | } |
| 50 | cmd, err := guestos.WindowsRelayCommand(ctx, s.profile, "versions") |
| 51 | if err != nil { |
| 52 | return snapshot, err |
| 53 | } |
| 54 | var data bytes.Buffer |
| 55 | cmd.Stdout = &boundedWindowsStatusWriter{buffer: &data, remaining: 64 << 10} |
| 56 | cmd.Stderr = io.Discard |
| 57 | if err = cmd.Run(); err != nil { |
| 58 | return snapshot, err |
| 59 | } |
| 60 | return snapshot, json.Unmarshal(data.Bytes(), &snapshot) |
| 61 | } |
| 62 | |
| 63 | func (s *Server) nativeEnvironmentVersions(w http.ResponseWriter, r *http.Request) { |
| 64 | kit, system := s.nativeComponentKit() |
| 65 | required, kitError := guestos.RequiredNativeComponents(kit, system) |
| 66 | components := componentVersions{Status: "stopped", Required: required.GuestManifest} |
| 67 | tools := map[string]toolVersion{} |
| 68 | for name, version := range required.Tools { |
| 69 | tools[name] = toolVersion{Required: version} |
| 70 | } |
| 71 | detail := "" |
| 72 | running := s.vmRunning() |
| 73 | installed := false |
| 74 | if running { |
| 75 | ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second) |
| 76 | snapshot, err := s.readNativeComponents(ctx) |
| 77 | cancel() |
| 78 | components.Current = snapshot.Manifest |
| 79 | components.UpdateRequired = true |
| 80 | components.Status = "update_required" |
| 81 | installed = snapshot.Installed |
| 82 | if err != nil { |
| 83 | detail = "客机组件版本暂时无法核验。旧环境可以同步环境组件;请先登录客机,再重试。" |
| 84 | } else { |
| 85 | detail = snapshot.Error |
| 86 | if snapshot.Manifest != nil && kitError == nil && snapshot.Installed && snapshot.Manifest.Protocol == required.Protocol && snapshot.Manifest.Revision == required.Revision { |
| 87 | components.Status = "current" |
| 88 | components.UpdateRequired = false |
| 89 | } |
| 90 | for name, current := range snapshot.Tools { |
| 91 | item := tools[name] |
| 92 | item.Current = current |
| 93 | tools[name] = item |
| 94 | } |
| 95 | if len(snapshot.Missing) > 0 { |
| 96 | detail = "部分客机组件缺失或内容已变化,请同步环境组件。" |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | if kitError != nil { |
| 101 | components.Status = "unknown" |
| 102 | components.UpdateRequired = false |
| 103 | detail = "此应用的原生组件包无法核验,请重新安装完整的 Cage 应用。" |
| 104 | } |
| 105 | s.mu.Lock() |
| 106 | busy := s.jobs.snapshot().Running |
| 107 | identity := s.identity |
| 108 | s.mu.Unlock() |
| 109 | jsonReply(w, map[string]any{"vm_running": running, "busy": busy, "components": components, "controller": identity, "tools": tools, "installed": installed, |
| 110 | "tools_update_allowed": running && kitError == nil && !busy, "error": detail, |
| 111 | "maintenance": map[string]any{"native": true, "guest_system": system, "requires_closed_terminals": true, "requires_system_authorization": true}}) |
| 112 | } |
| 113 | |
| 114 | func (s *Server) nativeMaintenancePreflight() error { |
| 115 | if s.macosGuest == nil && s.windowsGuest == nil { |
| 116 | return nil |
| 117 | } |
| 118 | if s.terminals.hasLive() || len(s.mobileCLI.activeAccounts()) != 0 { |
| 119 | return errors.New("请先保存工作,结束正在运行的终端和 Claude Code 轮次,再重试更新。更新会重启客机连接组件;聊天历史、项目和登录资料会保留。") |
| 120 | } |
| 121 | if !s.vmRunning() { |
| 122 | return errors.New("请先启动并登录客机,再更新环境组件。") |
| 123 | } |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | func (s *Server) maintainNativeComponents(tools bool) error { |
| 128 | if err := s.nativeMaintenancePreflight(); err != nil { |
| 129 | return err |
| 130 | } |
| 131 | kit, system := s.nativeComponentKit() |
| 132 | manifest, err := guestos.RequiredNativeComponents(kit, system) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | file, err := os.CreateTemp(s.profile, "native-components-*.json") |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | defer os.Remove(file.Name()) |
| 141 | data, err := json.Marshal(manifest) |
| 142 | if err == nil { |
| 143 | _, err = file.Write(data) |
| 144 | } |
| 145 | err = errors.Join(err, file.Close()) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | if err = s.closeProxyGate("正在更新原生客机组件,外网等待重新检查"); err != nil { |
| 150 | return err |
| 151 | } |
| 152 | s.mu.Lock() |
| 153 | s.ready = false |
| 154 | s.guestUpdateRequired = true |
| 155 | s.guestUpdateError = "原生组件更新尚未完成,可以保留当前环境重试。" |
| 156 | s.mu.Unlock() |
| 157 | s.closeWindowsBrowser() |
| 158 | s.closeWindowsNetwork() |
| 159 | s.closeProjectShares() |
| 160 | s.setMessage("正在更新原生客机组件,保留系统磁盘、账户和项目数据…") |
| 161 | var command *exec.Cmd |
| 162 | if system == guestos.MacOS { |
| 163 | command = exec.CommandContext(s.operationContext(), s.exe, "guest-os", "macos", "update-components", "--manifest", file.Name()) |
| 164 | if tools { |
| 165 | command.Args = append(command.Args, "--tools") |
| 166 | } |
| 167 | command.Env = append(os.Environ(), cage.ConfigEnv+"="+s.configPath) |
| 168 | } else { |
| 169 | command, err = guestos.WindowsMaintenanceCommand(s.operationContext(), s.profile, kit, file.Name(), tools) |
| 170 | if err != nil { |
| 171 | return err |
| 172 | } |
| 173 | } |
| 174 | command.Stdout, command.Stderr = s.jobs.execution(), s.jobs.execution() |
| 175 | if err = command.Run(); err != nil { |
| 176 | return executionFailure("更新原生客机组件", err, s.jobs.snapshot().Log) |
| 177 | } |
| 178 | ctx, cancel := context.WithTimeout(s.operationContext(), 25*time.Second) |
| 179 | snapshot, err := s.readNativeComponents(ctx) |
| 180 | cancel() |
| 181 | if err != nil || snapshot.Manifest == nil || !snapshot.Installed || snapshot.Manifest.Revision != manifest.Revision || snapshot.Manifest.Protocol != manifest.Protocol { |
| 182 | return errors.New("更新后的客机组件未通过实际文件核验,请保留环境并重试同步组件。") |
| 183 | } |
| 184 | if err = s.attach(); err != nil { |
| 185 | return err |
| 186 | } |
| 187 | s.mu.Lock() |
| 188 | s.guestUpdateRequired = false |
| 189 | s.guestUpdateError = "" |
| 190 | s.mu.Unlock() |
| 191 | return s.checkNetworkIfWanted() |
| 192 | } |