返回 DeepSeek-Reasonix
shell_regression_test.go
根目录 / internal / tool / builtin / shell_regression_test.go
1 package builtin
2
3 import (
4 "context"
5 "encoding/json"
6 "io"
7 "runtime"
8 "strings"
9 "testing"
10 "time"
11
12 "reasonix/internal/event"
13 "reasonix/internal/jobs"
14 "reasonix/internal/sandbox"
15 "reasonix/internal/tool"
16 )
17
18 func TestShellTimeoutClampsBeforeDurationConversion(t *testing.T) {
19 for _, cap := range []time.Duration{time.Second, 500 * time.Microsecond, 0} {
20 b := bash{timeout: cap}
21 for _, ms := range []int{1, 1500, 9223372036855, int(^uint(0) >> 1)} {
22 got := b.foregroundTimeoutFor(bashParams{TimeoutMS: ms})
23 if got <= 0 || (cap > 0 && got > cap) {
24 t.Fatalf("cap=%v ms=%d got=%v", cap, ms, got)
25 }
26 }
27 }
28 if got := cappedMilliseconds(9223372036855, jobOutputMaxWait); got != jobOutputMaxWait {
29 t.Fatalf("job wait overflow: %v", got)
30 }
31 }
32
33 func TestInvalidJobFilterPreservesUnreadOutput(t *testing.T) {
34 for _, reader := range []tool.Tool{jobOutput{}, bashOutput{}} {
35 t.Run(reader.Name(), func(t *testing.T) {
36 m := jobs.NewManager(event.Discard)
37 defer m.Close()
38 ctx := jobs.WithManager(t.Context(), m)
39 j := m.Start("pwsh", "failure", func(_ context.Context, w io.Writer) (string, error) {
40 _, _ = io.WriteString(w, "startup diagnostic to preserve\n")
41 return "", nil
42 })
43 m.Wait(ctx, []string{j.ID}, 5)
44 args, _ := json.Marshal(map[string]any{"job_id": j.ID, "filter": "["})
45 if _, err := reader.Execute(ctx, args); err == nil {
46 t.Fatal("invalid filter accepted")
47 }
48 args, _ = json.Marshal(map[string]any{"job_id": j.ID})
49 got, err := reader.Execute(ctx, args)
50 if err != nil || !strings.Contains(got, "startup diagnostic to preserve") {
51 t.Fatalf("lost log: %q, %v", got, err)
52 }
53 })
54 }
55 }
56
57 func TestBackgroundPermissionFailureOffersExactRetry(t *testing.T) {
58 if !sandbox.OSSandboxSupported() {
59 t.Skip("exact denial retries require an OS shell sandbox")
60 }
61 previous := bashSandboxCommand
62 bashSandboxCommand = func(_ sandbox.Spec, _ sandbox.Shell, _ string) ([]string, bool) {
63 if runtime.GOOS == "windows" {
64 return []string{"cmd", "/c", "echo Error: spawn EPERM: operation not permitted & exit /b 1"}, true
65 }
66 return []string{"sh", "-c", "printf 'Error: spawn EPERM: operation not permitted\\n'; exit 1"}, true
67 }
68 t.Cleanup(func() { bashSandboxCommand = previous })
69 m := jobs.NewManager(event.Discard)
70 defer m.Close()
71 ctx := sandbox.WithPermissionPreset(jobs.WithManager(t.Context(), m), "workspace-write")
72 b := bash{name: "pwsh", shell: sandbox.Shell{Kind: sandbox.ShellPowerShell, Path: "pwsh"}, workDir: t.TempDir(), sb: sandbox.Spec{Mode: "enforce", Network: true}}
73 _, err := b.Execute(ctx, json.RawMessage(`{"command":"npm run start","description":"Start service","run_in_background":true}`))
74 if err != nil {
75 t.Fatal(err)
76 }
77 got, err := (jobOutput{}).ExecuteDetailed(ctx, json.RawMessage(`{"job_id":"pwsh-1","wait":true,"timeout_ms":5000}`))
78 if err != nil || !strings.Contains(got.Output, "denial_id ") {
79 t.Fatalf("no authorized retry path: %q, %v", got.Output, err)
80 }
81 if got.Execution.FailurePhase != tool.ShellPhaseExecution || got.Execution.MutationRisk != tool.ShellMutationMayBePartial {
82 t.Fatalf("runtime error reclassified: %+v", got.Execution)
83 }
84 id := strings.TrimSuffix(strings.Fields(strings.SplitN(got.Output, "denial_id ", 2)[1])[0], ".")
85 if !sandbox.ConsumeDenial(id, "npm run start") || sandbox.ConsumeDenial(id, "npm run start") {
86 t.Fatal("denial must authorize one exact retry")
87 }
88 }
89
90 func TestBackgroundDiagnosticCaptureKeepsFailureTail(t *testing.T) {
91 w := &boundedDiagnosticWriter{limit: 64 << 10}
92 _, _ = io.WriteString(w, strings.Repeat("ordinary output\n", 10_000))
93 _, _ = io.WriteString(w, "Error: spawn EPERM: operation not permitted\n")
94 if !strings.Contains(w.String(), "spawn EPERM") || len(w.String()) > 64<<10 {
95 t.Fatalf("bounded diagnostic lost failure tail: bytes=%d", len(w.String()))
96 }
97 }
98
98 lines GO