跳到主要内容

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

浏览文件

lifecycle.go

166 行 · 4.2 KB历史下载
1package workbench
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "time"
9
10 "github.com/sylvinus/agent-vm/internal/diagnostics"
11 "github.com/sylvinus/agent-vm/internal/managedproxy"
12)
13
14func (s *Server) lifetimeContextLocked() context.Context {
15 if s.lifeContext == nil {
16 s.lifeContext, s.lifeCancel = context.WithCancel(context.Background())
17 if s.closing {
18 s.lifeCancel()
19 }
20 }
21 return s.lifeContext
22}
23
24func (s *Server) lifetimeContext() context.Context {
25 s.mu.Lock()
26 defer s.mu.Unlock()
27 return s.lifetimeContextLocked()
28}
29
30func (s *Server) startMonitors() {
31 s.mu.Lock()
32 if s.closing {
33 s.mu.Unlock()
34 return
35 }
36 ctx := s.lifetimeContextLocked()
37 s.monitors.Add(4)
38 s.mu.Unlock()
39 go func() {
40 defer s.monitors.Done()
41 ticker := time.NewTicker(time.Hour)
42 defer ticker.Stop()
43 for {
44 select {
45 case <-ctx.Done():
46 return
47 case <-ticker.C:
48 if err := diagnostics.New(s.profile).Clean(); err != nil {
49 diagnostics.Log("error", "diagnostics", "log cleanup failed: "+err.Error())
50 }
51 }
52 }
53 }()
54 go func() {
55 defer s.monitors.Done()
56 s.monitor(ctx)
57 }()
58 go func() {
59 defer s.monitors.Done()
60 ticker := time.NewTicker(5 * time.Second)
61 defer ticker.Stop()
62 for ctx.Err() == nil {
63 // Long-running maintenance jobs own their own monitor and stop it
64 // before changing guest components. Other jobs cancel observation.
65 if !s.busy() {
66 s.monitorSafety()
67 }
68 select {
69 case <-ctx.Done():
70 return
71 case <-ticker.C:
72 }
73 }
74 }()
75 go func() {
76 defer s.monitors.Done()
77 // macOS performs both probes from the same tick. Its periodic SSH
78 // attestation otherwise races this loop for the lifecycle lock.
79 if s.macosGuest == nil && (s.windowsGuest == nil || s.windowsGuest.NetworkPairingFile != "") {
80 s.monitorNetwork(ctx)
81 }
82 }()
83}
84
85// beginClose seals workbench admission and cancels service work without waiting.
86func (s *Server) beginClose() {
87 s.mu.Lock()
88 s.closing = true
89 s.lifetimeContextLocked()
90 s.lifeCancel()
91 if s.checkCancel != nil {
92 s.checkCancel()
93 }
94 if s.monitorCancel != nil {
95 s.monitorCancel()
96 }
97 s.mu.Unlock()
98}
99
100// Wait outside the operation lock: a cancelled job may still need that lock
101// before it can finish and release its resources.
102func (s *Server) closeBackground() {
103 s.beginClose()
104 s.jobs.close()
105 s.monitors.Wait()
106}
107
108// An activity lasts through the handler's stream and child goroutine cleanup.
109// Add and the closing decision share mu, so shutdown can safely wait outside it.
110func (s *Server) beginActivity(parent context.Context) (context.Context, func(), error) {
111 s.mu.Lock()
112 if s.closing {
113 s.mu.Unlock()
114 return nil, nil, errJobsClosed
115 }
116 lifetime := s.lifetimeContextLocked()
117 s.activities.Add(1)
118 s.mu.Unlock()
119 ctx, cancel := context.WithCancel(parent)
120 stop := context.AfterFunc(lifetime, cancel)
121 return ctx, func() { stop(); cancel(); s.activities.Done() }, nil
122}
123
124// closeService releases host connections while retaining the guest VM and its
125// disk. It runs outside request handlers and without either workbench lock.
126func (s *Server) closeService() error {
127 s.closeOnce.Do(func() {
128 s.beginClose()
129 // Closing PTYs first releases handlers blocked writing terminal input.
130 // The manager also rejects a terminal created by an already-admitted call.
131 s.terminals.close()
132 s.closeBackground()
133 s.activities.Wait()
134 s.op.Lock()
135 defer s.op.Unlock()
136 s.closeProjectShares()
137 s.closeWindowsBrowser()
138 if s.windowsGuest != nil {
139 s.closeErr = s.closeProxyGate("控制服务正在退出,外网保持关闭")
140 }
141 s.closeWindowsNetwork()
142 s.mu.Lock()
143 browser := s.browserControl
144 s.ready = false
145 s.credentials = guestCredentials{}
146 s.mu.Unlock()
147 if browser != nil {
148 browser.close()
149 }
150 s.controlSession.close()
151 if s.windowsGuest == nil {
152 s.closeErr = s.closeProxyGate("控制服务正在退出,外网保持关闭")
153 }
154 s.closeErr = errors.Join(s.closeErr, managedproxy.Stop(s.profile))
155 if s.controlPublished {
156 if err := os.Remove(filepath.Join(s.profile, "control.json")); err != nil && !os.IsNotExist(err) {
157 s.closeErr = errors.Join(s.closeErr, err)
158 }
159 }
160 })
161 return s.closeErr
162}
163
164func (s *Server) controlContext() (context.Context, context.CancelFunc) {
165 return context.WithTimeout(s.lifetimeContext(), 5*time.Second)
166}