gunk-dev/the-valley

backend_test.go

source60ad80d5a5a5main's published tip history
9.6 KiB
1package backend
2
3import (
4 "encoding/json"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "reflect"
9 "slices"
10 "strings"
11 "testing"
12)
13
14// Exercise the shell boundary with quotes, newlines and substitutions. The CLI
15// stub receives the exact argv and must not execute any prompt content.
16func TestWorkerShellBoundary(t *testing.T) {
17 for _, k := range []Kind{Claude, Codex, Agy} {
18 t.Run(string(k), func(t *testing.T) {
19 dir := t.TempDir()
20 stub := filepath.Join(dir, string(k))
21 if err := os.WriteFile(stub, []byte("#!/bin/sh\nprintf '%s\\000' \"$@\"\n"), 0700); err != nil {
22 t.Fatal(err)
23 }
24 o := Options{SystemPrompt: "instructions\nwith 'quotes'", Prompt: "do not execute $(exit 8) `exit 9`\n--help", Model: "model'quoted", Effort: "high", Budget: "5", RunID: "run"}
25 args := k.Worker(o)
26 args[0] = stub
27 out, err := exec.Command("sh", "-c", ShellCommand(args)).Output()
28 if err != nil {
29 t.Fatal(err)
30 }
31 actual := strings.Split(strings.TrimSuffix(string(out), "\x00"), "\x00")
32 if !reflect.DeepEqual(actual, args[1:]) {
33 t.Fatalf("argv changed across shell: %#v != %#v", actual, args[1:])
34 }
35 if k != Claude && strings.Contains(string(out), "--max-budget-usd") {
36 t.Fatal("unsupported budget flag")
37 }
38 if k == Codex && !strings.Contains(string(out), "developer_instructions=") {
39 t.Fatal("missing developer instructions")
40 }
41 })
42 }
43}
44
45// Every backend takes the prompt on stdin so a brief of any length stays out
46// of the tmux command. agy has no system prompt flag, so it reads that there too.
47func TestWorkerPromptTransport(t *testing.T) {
48 const prompt, system, sysFile = "the whole brief", "system text", "/prompts/run.system.md"
49 for _, k := range []Kind{Claude, Codex, Agy} {
50 for _, resume := range []string{"", "prior-session"} {
51 t.Run(string(k)+"/resume="+resume, func(t *testing.T) {
52 args := k.Worker(Options{Prompt: prompt, SystemPrompt: system, SystemPromptFile: sysFile, ResumeID: resume, RunID: "run", Budget: "5"})
53 joined := strings.Join(args, "\n")
54 if strings.Contains(joined, prompt) {
55 t.Fatalf("prompt in argv: %q", args)
56 }
57 stdin, want := k.Stdin(system, prompt), prompt
58 if k == Agy {
59 want = system + "\n\n" + prompt
60 }
61 if stdin != want {
62 t.Fatalf("stdin = %q, want %q", stdin, want)
63 }
64 switch k {
65 case Claude:
66 if !strings.Contains(joined, "--append-system-prompt-file\n"+sysFile) || strings.Contains(joined, system) {
67 t.Fatalf("system prompt not read from its file: %q", args)
68 }
69 case Codex:
70 if args[len(args)-1] != "-" || !strings.Contains(joined, "developer_instructions=") {
71 t.Fatalf("codex must read stdin and keep inline instructions: %q", args)
72 }
73 case Agy:
74 // --print would take the next argument as the prompt.
75 if slices.Contains(args, "--print") || strings.Contains(joined, system) {
76 t.Fatalf("agy must read both prompts on stdin: %q", args)
77 }
78 }
79 })
80 }
81 }
82 args := Claude.Worker(Options{SystemPrompt: system})
83 if !strings.Contains(strings.Join(args, "\n"), "--append-system-prompt\n"+system) {
84 t.Fatalf("inline system prompt dropped without a file: %q", args)
85 }
86}
87
88// Claude sessions that can commit or open PRs must run with attribution off,
89// whether --settings loads the inline settings or the coordinator's status
90// line file: Claude takes one --settings, so the file must carry attribution
91// too. A worker always gets the inline settings. The setting is Claude-only,
92// so other backends' argv must not carry it.
93func TestClaudeAttributionDisabled(t *testing.T) {
94 for _, s := range []string{ClaudeSettings, ClaudeSettingsWithStatusLine("widget")} {
95 var settings struct {
96 Attribution struct {
97 Commit, PR *string
98 SessionURL *bool `json:"sessionUrl"`
99 }
100 }
101 if err := json.Unmarshal([]byte(s), &settings); err != nil {
102 t.Fatal(err)
103 }
104 if a := settings.Attribution; a.Commit == nil || *a.Commit != "" || a.PR == nil || *a.PR != "" || a.SessionURL == nil || *a.SessionURL {
105 t.Fatalf("attribution not disabled: %s", s)
106 }
107 }
108 for _, k := range []Kind{Claude, Codex, Agy} {
109 for _, file := range []string{"", "/state/run.settings.json"} {
110 for _, resume := range []string{"", "prior-session"} {
111 o := Options{SystemPrompt: "system", Prompt: "prompt", RunID: "run", Budget: "5", ResumeID: resume, Continue: resume != "", SettingsFile: file}
112 coordinator := ClaudeSettings
113 if file != "" {
114 coordinator = file
115 }
116 worker := k.Worker(o)
117 for name, argv := range map[string][]string{"worker": worker, "coordinator": k.Coordinator(o)} {
118 want := map[string]string{"worker": ClaudeSettings, "coordinator": coordinator}[name]
119 joined := strings.Join(argv, "\n")
120 has := strings.Contains(joined, "--settings\n"+want+"\n")
121 if has != (k == Claude) || strings.Count(joined, "--settings") != map[bool]int{true: 1}[k == Claude] {
122 t.Errorf("%s %s file=%q resume=%q: --settings %s present = %v: %q", k, name, file, resume, want, has, argv)
123 }
124 }
125 }
126 }
127 }
128}
129
130func TestAgyWorkspaceResume(t *testing.T) {
131 home := t.TempDir()
132 t.Setenv("HOME", home)
133 dir := filepath.Join(home, ".gemini", "antigravity-cli", "cache")
134 if err := os.MkdirAll(dir, 0700); err != nil {
135 t.Fatal(err)
136 }
137 data, _ := json.Marshal(map[string]string{"/work/one": "session-one", "/work/two": "session-two"})
138 if err := os.WriteFile(filepath.Join(dir, "last_conversations.json"), data, 0600); err != nil {
139 t.Fatal(err)
140 }
141 if got := AgyConversationID("/work/one"); got != "session-one" {
142 t.Fatal(got)
143 }
144 if got := AgyConversationID("/work/missing"); got != "" {
145 t.Fatalf("resumed unrelated conversation %s", got)
146 }
147 args := Agy.Coordinator(Options{Continue: true, ResumeID: AgyConversationID("/work/one")})
148 if !strings.Contains(strings.Join(args, "\n"), "--conversation\nsession-one") {
149 t.Fatal(args)
150 }
151}
152
153func TestCodexPromptControlCharacters(t *testing.T) {
154 prompt := "bell\a vertical\v nul\x00 delete\x7f tab\t line\n unicode café"
155 argv := Codex.Coordinator(Options{SystemPrompt: prompt})
156 for _, arg := range argv {
157 if encoded, ok := strings.CutPrefix(arg, "developer_instructions="); ok {
158 if strings.Contains(encoded, "\x7f") || strings.Contains(encoded, `\x`) || strings.Contains(encoded, `\a`) || strings.Contains(encoded, `\v`) {
159 t.Fatalf("Go-only TOML escapes: %s", encoded)
160 }
161 var decoded string
162 if err := json.Unmarshal([]byte(encoded), &decoded); err != nil || decoded != prompt {
163 t.Fatalf("prompt changed: %q, %v", decoded, err)
164 }
165 return
166 }
167 }
168 t.Fatal("instructions missing")
169}
170
171func TestAgyResumeDoesNotRepeatInstructions(t *testing.T) {
172 argv := Agy.Coordinator(Options{SystemPrompt: "long original instructions", Continue: true, ResumeID: "conversation"})
173 joined := strings.Join(argv, "\n")
174 if strings.Contains(joined, "long original instructions") || !strings.Contains(joined, "session resumed") || !strings.Contains(joined, "--conversation\nconversation") {
175 t.Fatal(argv)
176 }
177}
178
179func TestOneShot(t *testing.T) {
180 for _, kind := range []Kind{Claude, Codex, Agy} {
181 for _, mode := range []string{"one-shot", "new-thread", "resume"} {
182 t.Run(string(kind)+"/"+mode, func(t *testing.T) {
183 opts := OneShotOptions{AgyAgent: "read-only-agent", SystemPrompt: "system", Model: "model", Effort: "high", Threaded: mode != "one-shot"}
184 if mode == "new-thread" {
185 opts.SessionID = "new-id"
186 }
187 if mode == "resume" {
188 opts.ResumeID = "prior-id"
189 }
190 argv, err := OneShot(kind, opts)
191 if err != nil {
192 t.Fatal(err)
193 }
194 joined := strings.Join(argv, "\n")
195 require := func(parts ...string) {
196 t.Helper()
197 for _, p := range parts {
198 if !strings.Contains(joined, p) {
199 t.Fatalf("missing %q in %q", p, argv)
200 }
201 }
202 }
203 if strings.Contains(joined, "dangerously") || strings.Contains(joined, "stream-json") {
204 t.Fatalf("unsafe argv: %q", argv)
205 }
206 require("--model\nmodel")
207 switch kind {
208 case Claude:
209 require("-p", "--safe-mode", "--output-format\ntext", "--tools\nRead,Grep,Glob", "--permission-mode\ndontAsk", "--strict-mcp-config", "--system-prompt\nsystem", "--effort\nhigh")
210 if mode == "one-shot" {
211 require("--no-session-persistence")
212 } else if strings.Contains(joined, "--no-session-persistence") {
213 t.Fatal("thread persistence disabled")
214 }
215 if mode == "new-thread" {
216 require("--session-id\nnew-id")
217 }
218 if mode == "resume" {
219 require("--resume\nprior-id")
220 }
221 case Codex:
222 if strings.Contains(joined, "mcp_servers={}") {
223 t.Fatalf("ineffective MCP override: %q", argv)
224 }
225 require("--ignore-user-config", "features.apps=false", "features.plugins=false", "features.hooks=false", "features.multi_agent=false", "--sandbox\nread-only", `approval_policy="never"`, `developer_instructions="system"`, `model_reasoning_effort="high"`)
226 if mode == "one-shot" {
227 require("--ephemeral")
228 } else if strings.Contains(joined, "--ephemeral") {
229 t.Fatal("thread persistence disabled")
230 }
231 if mode == "resume" {
232 require("resume\nprior-id")
233 }
234 if argv[len(argv)-1] != "-" {
235 t.Fatal("prompt must come from stdin")
236 }
237 case Agy:
238 require("--agent\nread-only-agent", "--mode\nplan", "--sandbox", "--output-format\ntext", "--effort\nhigh")
239 if slices.Contains(argv, "--print") || strings.Contains(joined, "system") {
240 t.Fatalf("agy must read both prompts on stdin: %q", argv)
241 }
242 if mode == "resume" {
243 require("--conversation\nprior-id")
244 }
245 }
246 })
247 }
248 }
249}
250
251func TestOneShotMisconfiguration(t *testing.T) {
252 for _, kind := range []Kind{Agy, Kind("unknown")} {
253 if argv, err := OneShot(kind, OneShotOptions{}); err == nil || argv != nil {
254 t.Fatalf("%s: argv=%v, err=%v", kind, argv, err)
255 }
256 }
257}