summaryrefslogtreecommitdiff
path: root/internal/executor/claude_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/executor/claude_test.go')
-rw-r--r--internal/executor/claude_test.go100
1 files changed, 100 insertions, 0 deletions
diff --git a/internal/executor/claude_test.go b/internal/executor/claude_test.go
index 1f6e5be..b5f7962 100644
--- a/internal/executor/claude_test.go
+++ b/internal/executor/claude_test.go
@@ -2,6 +2,7 @@ package executor
import (
"context"
+ "errors"
"io"
"log/slog"
"os"
@@ -478,3 +479,102 @@ func TestTeardownSandbox_CleanSandboxWithNoNewCommits_RemovesSandbox(t *testing.
os.RemoveAll(sandbox)
}
}
+
+// TestBlockedError_IncludesSandboxDir verifies that when a task is blocked in a
+// sandbox, the BlockedError carries the sandbox path so the resume execution can
+// run in the same directory (where Claude's session files are stored).
+func TestBlockedError_IncludesSandboxDir(t *testing.T) {
+ src := t.TempDir()
+ initGitRepo(t, src)
+
+ logDir := t.TempDir()
+
+ // Use a script that writes question.json to the env-var path and exits 0
+ // (simulating a blocked agent that asks a question before exiting).
+ scriptPath := filepath.Join(t.TempDir(), "fake-claude.sh")
+ if err := os.WriteFile(scriptPath, []byte(`#!/bin/sh
+if [ -n "$CLAUDOMATOR_QUESTION_FILE" ]; then
+ printf '{"question":"continue?"}' > "$CLAUDOMATOR_QUESTION_FILE"
+fi
+`), 0755); err != nil {
+ t.Fatalf("write script: %v", err)
+ }
+
+ r := &ClaudeRunner{
+ BinaryPath: scriptPath,
+ Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
+ LogDir: logDir,
+ }
+ tk := &task.Task{
+ Agent: task.AgentConfig{
+ Type: "claude",
+ Instructions: "do something",
+ ProjectDir: src,
+ SkipPlanning: true,
+ },
+ }
+ exec := &storage.Execution{ID: "blocked-exec-uuid", TaskID: "task-1"}
+
+ err := r.Run(context.Background(), tk, exec)
+
+ var blocked *BlockedError
+ if !errors.As(err, &blocked) {
+ t.Fatalf("expected BlockedError, got: %v", err)
+ }
+ if blocked.SandboxDir == "" {
+ t.Error("BlockedError.SandboxDir should be set when task runs in a sandbox")
+ }
+ // Sandbox should still exist (preserved for resume).
+ if _, statErr := os.Stat(blocked.SandboxDir); os.IsNotExist(statErr) {
+ t.Error("sandbox directory should be preserved when blocked")
+ } else {
+ os.RemoveAll(blocked.SandboxDir) // cleanup
+ }
+}
+
+// TestClaudeRunner_Run_ResumeUsesStoredSandboxDir verifies that when a resume
+// execution has SandboxDir set, the runner uses that directory (not project_dir)
+// as the working directory, so Claude finds its session files there.
+func TestClaudeRunner_Run_ResumeUsesStoredSandboxDir(t *testing.T) {
+ logDir := t.TempDir()
+ sandboxDir := t.TempDir()
+ cwdFile := filepath.Join(logDir, "cwd.txt")
+
+ // Use a script that writes its working directory to a file in logDir (stable path).
+ scriptPath := filepath.Join(t.TempDir(), "fake-claude.sh")
+ script := "#!/bin/sh\nprintf '%s' \"$PWD\" > " + cwdFile + "\n"
+ if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil {
+ t.Fatalf("write script: %v", err)
+ }
+
+ r := &ClaudeRunner{
+ BinaryPath: scriptPath,
+ Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
+ LogDir: logDir,
+ }
+ tk := &task.Task{
+ Agent: task.AgentConfig{
+ Type: "claude",
+ ProjectDir: sandboxDir, // must exist; resume overrides it with SandboxDir anyway
+ SkipPlanning: true,
+ },
+ }
+ exec := &storage.Execution{
+ ID: "resume-exec-uuid",
+ TaskID: "task-1",
+ ResumeSessionID: "original-session",
+ ResumeAnswer: "yes",
+ SandboxDir: sandboxDir,
+ }
+
+ _ = r.Run(context.Background(), tk, exec)
+
+ got, err := os.ReadFile(cwdFile)
+ if err != nil {
+ t.Fatalf("cwd file not written: %v", err)
+ }
+ // The runner should have executed claude in sandboxDir, not in project_dir.
+ if string(got) != sandboxDir {
+ t.Errorf("resume working dir: want %q, got %q", sandboxDir, string(got))
+ }
+}