跳到主要内容

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

浏览文件

resources.go

94 行 · 3.0 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package hostagent
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "io"
11 "os/exec"
12 "runtime"
13 "strconv"
14 "sync"
15 "time"
16
17 "github.com/lima-vm/lima/v2/pkg/driver"
18 "github.com/lima-vm/lima/v2/pkg/limatype"
19 "github.com/lima-vm/lima/v2/pkg/sshutil"
20)
21
22// ManageResources is an optional embedding hook. It starts after the driver and
23// stops before it. Guest observations use the existing private SSH transport.
24var ManageResources func(context.Context, string, *limatype.LimaYAML, driver.MemoryBalloon, func(context.Context, string) ([]byte, error)) func()
25
26func (a *HostAgent) startResourceManager(ctx context.Context) func() {
27 if ManageResources == nil {
28 return func() {}
29 }
30 d := a.driver
31 if configured, ok := d.(*driver.ConfiguredDriver); ok {
32 d = configured.Driver
33 }
34 balloon, ok := d.(driver.MemoryBalloon)
35 if !ok {
36 return func() {}
37 }
38 read := func(ctx context.Context, script string) ([]byte, error) {
39 return runResourceProbe(ctx, a.resourceProbeCommand(ctx, script))
40 }
41 stop := ManageResources(ctx, a.instDir, a.instConfig, balloon, read)
42 var once sync.Once
43 return func() { once.Do(stop) }
44}
45
46// Retain a bounded prefix while draining all stderr, so a noisy SSH process
47// cannot block. The embedding application's diagnostic store redacts the error.
48type resourceProbeStderr struct{ data []byte }
49
50func (b *resourceProbeStderr) Write(p []byte) (int, error) {
51 n := len(p)
52 if remaining := 4096 - len(b.data); remaining > 0 {
53 b.data = append(b.data, p[:min(remaining, n)]...)
54 }
55 return n, nil
56}
57
58func runResourceProbe(ctx context.Context, cmd *exec.Cmd) ([]byte, error) {
59 var stderr resourceProbeStderr
60 cmd.Stderr = &stderr
61 pipe, err := cmd.StdoutPipe()
62 if err != nil {
63 return nil, fmt.Errorf("resource probe stdout: %w", err)
64 }
65 if err = cmd.Start(); err != nil {
66 return nil, fmt.Errorf("resource probe start: %w", err)
67 }
68 out, readErr := io.ReadAll(io.LimitReader(pipe, 16385))
69 if readErr != nil || len(out) > 16384 {
70 _ = cmd.Process.Kill()
71 _ = cmd.Wait()
72 if len(out) > 16384 {
73 return nil, errors.New("resource observation exceeded its limit")
74 }
75 return nil, fmt.Errorf("resource probe read: %w; stderr=%s", errors.Join(ctx.Err(), readErr), stderr.data)
76 }
77 if err = cmd.Wait(); err != nil {
78 return nil, fmt.Errorf("resource probe execution: %w; stderr=%s", errors.Join(ctx.Err(), err), stderr.data)
79 }
80 return out, nil
81}
82
83func (a *HostAgent) resourceProbeCommand(ctx context.Context, script string) *exec.Cmd {
84 args := a.sshConfig.Args()
85 if runtime.GOOS == "windows" {
86 // The bundled Windows SSH client cannot run the probe over Lima's mux socket.
87 args = sshutil.DisableControlMasterOptsFromSSHArgs(args)
88 }
89 args = append(args, "-o", "ConnectTimeout=2", "-o", "ClearAllForwardings=yes", "-T", "-p", strconv.Itoa(a.sshLocalPort), a.instSSHAddress, "--", script)
90 cmd := exec.CommandContext(ctx, a.sshConfig.Binary(), args...)
91 hideResourceProbe(cmd)
92 cmd.WaitDelay = time.Second
93 return cmd
94}