summaryrefslogtreecommitdiff
path: root/internal/executor/claude_test.go
blob: 1f95b4a5637925eb93b7db66958253af50f1d102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package executor

import (
	"context"
	"io"
	"log/slog"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
	"time"

	"github.com/thepeterstone/claudomator/internal/storage"
	"github.com/thepeterstone/claudomator/internal/task"
)

func TestClaudeRunner_BuildArgs_BasicTask(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "claude",
			Instructions: "fix the bug",
			Model:        "sonnet",
			SkipPlanning: true,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	argMap := make(map[string]bool)
	for _, a := range args {
		argMap[a] = true
	}
	for _, want := range []string{"-p", "fix the bug", "--output-format", "stream-json", "--verbose", "--model", "sonnet"} {
		if !argMap[want] {
			t.Errorf("missing arg %q in %v", want, args)
		}
	}
}

func TestClaudeRunner_BuildArgs_FullConfig(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:               "claude",
			Instructions:       "implement feature",
			Model:              "opus",
			MaxBudgetUSD:       5.0,
			PermissionMode:     "bypassPermissions",
			SystemPromptAppend: "Follow TDD",
			AllowedTools:       []string{"Bash", "Edit"},
			DisallowedTools:    []string{"Write"},
			ContextFiles:       []string{"/src"},
			AdditionalArgs:     []string{"--verbose"},
			SkipPlanning:       true,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	// Check key args are present.
	argMap := make(map[string]bool)
	for _, a := range args {
		argMap[a] = true
	}

	requiredArgs := []string{
		"-p", "implement feature", "--output-format", "stream-json",
		"--model", "opus", "--max-budget-usd", "5.00",
		"--permission-mode", "bypassPermissions",
		"--append-system-prompt", "Follow TDD",
		"--allowedTools", "Bash", "Edit",
		"--disallowedTools", "Write",
		"--add-dir", "/src",
		"--verbose",
	}
	for _, req := range requiredArgs {
		if !argMap[req] {
			t.Errorf("missing arg %q in %v", req, args)
		}
	}
}

func TestClaudeRunner_BuildArgs_DefaultsToBypassPermissions(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "claude",
			Instructions: "do work",
			SkipPlanning: true,
			// PermissionMode intentionally not set
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	found := false
	for i, a := range args {
		if a == "--permission-mode" && i+1 < len(args) && args[i+1] == "bypassPermissions" {
			found = true
		}
	}
	if !found {
		t.Errorf("expected --permission-mode bypassPermissions when PermissionMode is empty, args: %v", args)
	}
}

func TestClaudeRunner_BuildArgs_RespectsExplicitPermissionMode(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:           "claude",
			Instructions:   "do work",
			PermissionMode: "default",
			SkipPlanning:   true,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	for i, a := range args {
		if a == "--permission-mode" && i+1 < len(args) {
			if args[i+1] != "default" {
				t.Errorf("expected --permission-mode default, got %q", args[i+1])
			}
			return
		}
	}
	t.Errorf("--permission-mode flag not found in args: %v", args)
}

func TestClaudeRunner_BuildArgs_AlwaysIncludesVerbose(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "claude",
			Instructions: "do something",
			SkipPlanning: true,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	found := false
	for _, a := range args {
		if a == "--verbose" {
			found = true
			break
		}
	}
	if !found {
		t.Errorf("--verbose missing from args: %v", args)
	}
}

func TestClaudeRunner_BuildArgs_PreamblePrepended(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "claude",
			Instructions: "fix the bug",
			SkipPlanning: false,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	// The -p value should start with the preamble and end with the original instructions.
	if len(args) < 2 || args[0] != "-p" {
		t.Fatalf("expected -p as first arg, got: %v", args)
	}
	if !strings.HasPrefix(args[1], planningPreamble) {
		t.Errorf("instructions should start with planning preamble")
	}
	if !strings.HasSuffix(args[1], "fix the bug") {
		t.Errorf("instructions should end with original instructions")
	}
}

func TestClaudeRunner_BuildArgs_PreambleAddsBash(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "claude",
			Instructions: "do work",
			AllowedTools: []string{"Read"},
			SkipPlanning: false,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	// Bash should be appended to allowed tools.
	foundBash := false
	for i, a := range args {
		if a == "--allowedTools" && i+1 < len(args) && args[i+1] == "Bash" {
			foundBash = true
		}
	}
	if !foundBash {
		t.Errorf("Bash should be added to --allowedTools when preamble is active: %v", args)
	}
}

func TestClaudeRunner_BuildArgs_PreambleBashNotDuplicated(t *testing.T) {
	r := &ClaudeRunner{}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "claude",
			Instructions: "do work",
			AllowedTools: []string{"Bash", "Read"},
			SkipPlanning: false,
		},
	}

	args := r.buildArgs(tk, &storage.Execution{ID: "test-exec"}, "/tmp/q.json")

	// Count Bash occurrences in --allowedTools values.
	bashCount := 0
	for i, a := range args {
		if a == "--allowedTools" && i+1 < len(args) && args[i+1] == "Bash" {
			bashCount++
		}
	}
	if bashCount != 1 {
		t.Errorf("Bash should appear exactly once in --allowedTools, got %d: %v", bashCount, args)
	}
}

func TestClaudeRunner_Run_InaccessibleWorkingDir_ReturnsError(t *testing.T) {
	r := &ClaudeRunner{
		BinaryPath: "true", // would succeed if it ran
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     t.TempDir(),
	}
	tk := &task.Task{
		Agent: task.AgentConfig{
			Type:         "claude",
			ProjectDir:   "/nonexistent/path/does/not/exist",
			SkipPlanning: true,
		},
	}
	exec := &storage.Execution{ID: "test-exec"}

	err := r.Run(context.Background(), tk, exec)

	if err == nil {
		t.Fatal("expected error for inaccessible working_dir, got nil")
	}
	if !strings.Contains(err.Error(), "project_dir") {
		t.Errorf("expected 'project_dir' in error, got: %v", err)
	}
}

func TestClaudeRunner_BinaryPath_Default(t *testing.T) {
	r := &ClaudeRunner{}
	if r.binaryPath() != "claude" {
		t.Errorf("want 'claude', got %q", r.binaryPath())
	}
}

func TestClaudeRunner_BinaryPath_Custom(t *testing.T) {
	r := &ClaudeRunner{BinaryPath: "/usr/local/bin/claude"}
	if r.binaryPath() != "/usr/local/bin/claude" {
		t.Errorf("want custom path, got %q", r.binaryPath())
	}
}

// TestExecOnce_NoGoroutineLeak_OnNaturalExit verifies that execOnce does not
// leave behind any goroutines when the subprocess exits normally (no context
// cancellation). Both the pgid-kill goroutine and the parseStream goroutine
// must have exited before execOnce returns.
func TestExecOnce_NoGoroutineLeak_OnNaturalExit(t *testing.T) {
	logDir := t.TempDir()
	r := &ClaudeRunner{
		BinaryPath: "true", // exits immediately with status 0, produces no output
		Logger:     slog.New(slog.NewTextHandler(io.Discard, nil)),
		LogDir:     logDir,
	}
	e := &storage.Execution{
		ID:          "goroutine-leak-test",
		TaskID:      "task-id",
		StdoutPath:  filepath.Join(logDir, "stdout.log"),
		StderrPath:  filepath.Join(logDir, "stderr.log"),
		ArtifactDir: logDir,
	}

	// Let any goroutines from test infrastructure settle before sampling.
	runtime.Gosched()
	baseline := runtime.NumGoroutine()

	if err := r.execOnce(context.Background(), []string{}, "", e); err != nil {
		t.Fatalf("execOnce failed: %v", err)
	}

	// Give the scheduler a moment to let any leaked goroutines actually exit.
	// In correct code the goroutines exit before execOnce returns, so this is
	// just a safety buffer for the scheduler.
	time.Sleep(10 * time.Millisecond)
	runtime.Gosched()

	after := runtime.NumGoroutine()
	if after > baseline {
		t.Errorf("goroutine leak: %d goroutines before execOnce, %d after (leaked %d)",
			baseline, after, after-baseline)
	}
}