| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "syscall" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "the-valley/flow/internal/config" |
| 17 | ) |
| 18 | |
| 19 | // TestWorkerPromptCarriesRunContract checks that every backend's worker is told |
| 20 | // its run ends with its turn, even when a custom .klaus/prompt.md replaces the |
| 21 | // default template. |
| 22 | func TestWorkerPromptCarriesRunContract(t *testing.T) { |
| 23 | for _, kind := range []string{"claude", "codex", "agy"} { |
| 24 | t.Run(kind, func(t *testing.T) { |
| 25 | t.Parallel() |
| 26 | h := NewHarness(t) |
| 27 | if err := os.WriteFile(filepath.Join(h.RepoDir, ".klaus", "prompt.md"), []byte("House rules for {{.RunID}}.\n"), 0o644); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | h.WriteStub(kind, h.claudeStubScript()) |
| 31 | res := h.RunKlaus("launch", "--backend", kind, "small task") |
| 32 | if res.ExitCode != 0 { |
| 33 | t.Fatalf("launch: %s%s", res.Stdout, res.Stderr) |
| 34 | } |
| 35 | h.WaitForClaudeStart(30 * time.Second) |
| 36 | |
| 37 | argv := strings.Split(h.ClaudeArgv(), "\n") |
| 38 | var system string |
| 39 | if kind == "agy" { |
| 40 | // agy has no system prompt flag: it reads it on stdin, ahead of the brief. |
| 41 | system = strings.TrimSuffix(h.ClaudeStdin(), "\n\nsmall task") |
| 42 | } |
| 43 | for i, a := range argv { |
| 44 | switch { |
| 45 | case kind == "claude" && a == "--append-system-prompt-file" && i+1 < len(argv): |
| 46 | data, err := os.ReadFile(argv[i+1]) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | system = string(data) |
| 51 | case kind == "codex" && strings.HasPrefix(a, "developer_instructions="): |
| 52 | if err := json.Unmarshal([]byte(strings.TrimPrefix(a, "developer_instructions=")), &system); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | if !strings.Contains(system, "House rules for ") || !strings.Contains(system, config.WorkerRules) { |
| 58 | t.Fatalf("%s worker system prompt lacks the custom template or the run contract:\n%s", kind, system) |
| 59 | } |
| 60 | releaseBackendWorkers(t, h) |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // TestFinalizeNamesLeftoverBackgroundWork launches a worker that leaves |
| 66 | // unfinished work and exits without a PR, once with a background process still |
| 67 | // running in its worktree. The needs-attention note must name that process, and |
| 68 | // must not blame the pane's own shell, which also sits in the worktree. |
| 69 | func TestFinalizeNamesLeftoverBackgroundWork(t *testing.T) { |
| 70 | if _, err := os.Stat("/proc/self/cwd"); err != nil { |
| 71 | t.Skip("leftover-process detection reads /proc") |
| 72 | } |
| 73 | for _, background := range []bool{true, false} { |
| 74 | t.Run(fmt.Sprintf("background=%t", background), func(t *testing.T) { |
| 75 | t.Parallel() |
| 76 | h := NewHarness(t) |
| 77 | pidFile := filepath.Join(h.E2EDir, "bg.pid") |
| 78 | bg := "" |
| 79 | if background { |
| 80 | bg = fmt.Sprintf("sleep 120 </dev/null >/dev/null 2>&1 &\necho $! > %q\n", pidFile) |
| 81 | t.Cleanup(func() { |
| 82 | if b, err := os.ReadFile(pidFile); err == nil { |
| 83 | if pid, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil { |
| 84 | syscall.Kill(pid, syscall.SIGKILL) |
| 85 | } |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | // Block until released like the default stub: a worker that exits |
| 90 | // before launch saves its state finalizes nothing. |
| 91 | h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.01,"duration_ms":10,"session_id":"11111111-2222-4333-8444-555555555555"}` + "\n") |
| 92 | h.WriteStub("claude", h.claudeStubScript()+"echo wip > unfinished.txt\n"+bg) |
| 93 | |
| 94 | res := h.RunKlaus("launch", "build it") |
| 95 | if res.ExitCode != 0 { |
| 96 | t.Fatalf("launch: %s%s", res.Stdout, res.Stderr) |
| 97 | } |
| 98 | ids := h.RunIDs() |
| 99 | if len(ids) != 1 { |
| 100 | t.Fatalf("runs: %v", ids) |
| 101 | } |
| 102 | h.WaitForClaudeStart(30 * time.Second) |
| 103 | releaseBackendWorkers(t, h) |
| 104 | st, err := h.ReadState(ids[0]) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if st.NeedsAttention == nil { |
| 109 | t.Fatalf("salvaged run not marked needs-attention: %+v", st) |
| 110 | } |
| 111 | if background { |
| 112 | const want = "background work still running (sleep)" |
| 113 | if st.FailureReason == nil || *st.FailureReason != want { |
| 114 | t.Fatalf("failure reason = %v, want %q", st.FailureReason, want) |
| 115 | } |
| 116 | if !strings.HasPrefix(*st.NeedsAttention, want+"; ") { |
| 117 | t.Fatalf("needs-attention = %q, want it to start with %q", *st.NeedsAttention, want) |
| 118 | } |
| 119 | } else { |
| 120 | if st.FailureReason != nil { |
| 121 | t.Fatalf("failure reason = %q, want none", *st.FailureReason) |
| 122 | } |
| 123 | if !strings.HasPrefix(*st.NeedsAttention, "no_pr; ") { |
| 124 | t.Fatalf("needs-attention = %q, want no_pr", *st.NeedsAttention) |
| 125 | } |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | } |