跳到主要内容

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

浏览文件

0024-resource-probe-errors.patch

159 行 · 4.9 KB历史下载
1Subject: [PATCH] Retain bounded resource probe errors for embedding diagnostics
2
3diff --git a/pkg/hostagent/resources.go b/pkg/hostagent/resources.go
4--- a/pkg/hostagent/resources.go
5+++ b/pkg/hostagent/resources.go
6@@ -6,6 +6,7 @@
7 import (
8 "context"
9 "errors"
10+ "fmt"
11 "io"
12 "os/exec"
13 "runtime"
14@@ -35,25 +36,48 @@
15 return func() {}
16 }
17 read := func(ctx context.Context, script string) ([]byte, error) {
18- cmd := a.resourceProbeCommand(ctx, script)
19- pipe, err := cmd.StdoutPipe()
20- if err != nil {
21- return nil, err
22- }
23- if err = cmd.Start(); err != nil {
24- return nil, err
25- }
26- out, readErr := io.ReadAll(io.LimitReader(pipe, 16385))
27- if readErr != nil || len(out) > 16384 {
28- _ = cmd.Process.Kill()
29- _ = cmd.Wait()
30- return nil, errors.New("resource observation exceeded its limit")
31- }
32- return out, cmd.Wait()
33+ return runResourceProbe(ctx, a.resourceProbeCommand(ctx, script))
34 }
35 stop := ManageResources(ctx, a.instDir, a.instConfig, balloon, read)
36 var once sync.Once
37 return func() { once.Do(stop) }
38+}
39+
40+// Retain a bounded prefix while draining all stderr, so a noisy SSH process
41+// cannot block. The embedding application's diagnostic store redacts the error.
42+type resourceProbeStderr struct{ data []byte }
43+
44+func (b *resourceProbeStderr) Write(p []byte) (int, error) {
45+ n := len(p)
46+ if remaining := 4096 - len(b.data); remaining > 0 {
47+ b.data = append(b.data, p[:min(remaining, n)]...)
48+ }
49+ return n, nil
50+}
51+
52+func runResourceProbe(ctx context.Context, cmd *exec.Cmd) ([]byte, error) {
53+ var stderr resourceProbeStderr
54+ cmd.Stderr = &stderr
55+ pipe, err := cmd.StdoutPipe()
56+ if err != nil {
57+ return nil, fmt.Errorf("resource probe stdout: %w", err)
58+ }
59+ if err = cmd.Start(); err != nil {
60+ return nil, fmt.Errorf("resource probe start: %w", err)
61+ }
62+ out, readErr := io.ReadAll(io.LimitReader(pipe, 16385))
63+ if readErr != nil || len(out) > 16384 {
64+ _ = cmd.Process.Kill()
65+ _ = cmd.Wait()
66+ if len(out) > 16384 {
67+ return nil, errors.New("resource observation exceeded its limit")
68+ }
69+ return nil, fmt.Errorf("resource probe read: %w; stderr=%s", errors.Join(ctx.Err(), readErr), stderr.data)
70+ }
71+ if err = cmd.Wait(); err != nil {
72+ return nil, fmt.Errorf("resource probe execution: %w; stderr=%s", errors.Join(ctx.Err(), err), stderr.data)
73+ }
74+ return out, nil
75 }
76
77 func (a *HostAgent) resourceProbeCommand(ctx context.Context, script string) *exec.Cmd {
78diff --git a/pkg/hostagent/resources_process_test.go b/pkg/hostagent/resources_process_test.go
79new file mode 100644
80--- /dev/null
81+++ b/pkg/hostagent/resources_process_test.go
82@@ -0,0 +1,77 @@
83+// SPDX-FileCopyrightText: Copyright The Lima Authors
84+// SPDX-License-Identifier: Apache-2.0
85+
86+package hostagent
87+
88+import (
89+ "context"
90+ "errors"
91+ "fmt"
92+ "os"
93+ "os/exec"
94+ "strings"
95+ "testing"
96+ "time"
97+)
98+
99+func TestResourceProbeProcess(t *testing.T) {
100+ if mode := os.Getenv("CAGE_RESOURCE_PROBE_TEST"); mode != "" {
101+ switch mode {
102+ case "failure":
103+ fmt.Fprint(os.Stdout, "private-stdout")
104+ fmt.Fprint(os.Stderr, "getsockname failed: Not a socket\n"+strings.Repeat("x", 1<<20))
105+ os.Exit(7)
106+ case "timeout":
107+ time.Sleep(30 * time.Second)
108+ case "oversize":
109+ fmt.Fprint(os.Stdout, strings.Repeat("x", 16385))
110+ case "success":
111+ fmt.Fprint(os.Stdout, "metrics")
112+ }
113+ os.Exit(0)
114+ }
115+ exe, err := os.Executable()
116+ if err != nil {
117+ t.Fatal(err)
118+ }
119+ for _, mode := range []string{"failure", "timeout", "oversize", "success", "start"} {
120+ t.Run(mode, func(t *testing.T) {
121+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
122+ defer cancel()
123+ if mode == "timeout" {
124+ ctx, cancel = context.WithTimeout(ctx, 100*time.Millisecond)
125+ defer cancel()
126+ }
127+ cmd := exec.CommandContext(ctx, exe, "-test.run=^TestResourceProbeProcess$")
128+ cmd.Env = append(os.Environ(), "CAGE_RESOURCE_PROBE_TEST="+mode)
129+ cmd.WaitDelay = time.Second
130+ hideResourceProbe(cmd)
131+ if mode == "start" {
132+ cmd.Path = "missing-resource-probe-executable"
133+ }
134+ out, err := runResourceProbe(ctx, cmd)
135+ switch mode {
136+ case "success":
137+ if err != nil || string(out) != "metrics" {
138+ t.Fatalf("valid observation lost: %q %v", out, err)
139+ }
140+ case "failure":
141+ if err == nil || !strings.Contains(err.Error(), "Not a socket") || !strings.Contains(err.Error(), "exit status 7") || len(err.Error()) > 4300 || strings.Contains(err.Error(), "private-stdout") {
142+ t.Fatalf("missing or unbounded execution diagnostic: %v", err)
143+ }
144+ case "timeout":
145+ if !errors.Is(err, context.DeadlineExceeded) {
146+ t.Fatalf("timeout cause lost: %v", err)
147+ }
148+ case "oversize":
149+ if err == nil || !strings.Contains(err.Error(), "exceeded") {
150+ t.Fatalf("oversized stdout accepted: %v", err)
151+ }
152+ case "start":
153+ if err == nil || !strings.Contains(err.Error(), "probe start") {
154+ t.Fatalf("start failure lost: %v", err)
155+ }
156+ }
157+ })
158+ }
159+}