| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "the-valley/flow/internal/run" |
| 12 | ) |
| 13 | |
| 14 | // TestCleanupRemovesPaneAndWorktree launches an agent and then cleans it up |
| 15 | // while it is still live, asserting that `klaus cleanup` kills the real pane, |
| 16 | // removes the real worktree, and deletes the state file. The fake claude is |
| 17 | // left blocked so the run is genuinely active (exercising --force). |
| 18 | func TestCleanupRemovesPaneAndWorktree(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | h := NewHarness(t) |
| 21 | |
| 22 | res := h.RunKlaus("launch", "do some work") |
| 23 | if res.ExitCode != 0 { |
| 24 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 25 | } |
| 26 | ids := h.RunIDs() |
| 27 | if len(ids) != 1 { |
| 28 | t.Fatalf("expected 1 run, got %d: %v", len(ids), ids) |
| 29 | } |
| 30 | runID := ids[0] |
| 31 | |
| 32 | st, err := h.ReadState(runID) |
| 33 | if err != nil { |
| 34 | t.Fatalf("reading state: %v", err) |
| 35 | } |
| 36 | if st.TmuxPane == nil { |
| 37 | t.Fatal("launch wrote no TmuxPane") |
| 38 | } |
| 39 | pane := *st.TmuxPane |
| 40 | worktree := st.Worktree |
| 41 | |
| 42 | // Make sure the agent is genuinely running in its pane before cleanup. |
| 43 | h.WaitForClaudeStart(30 * time.Second) |
| 44 | if !h.PaneExists(pane) { |
| 45 | t.Fatalf("pane %s should exist before cleanup", pane) |
| 46 | } |
| 47 | if _, err := os.Stat(worktree); err != nil { |
| 48 | t.Fatalf("worktree should exist before cleanup: %v", err) |
| 49 | } |
| 50 | |
| 51 | // --force because the blocked stub keeps the run "active". |
| 52 | res = h.RunKlaus("cleanup", "--force", runID) |
| 53 | if res.ExitCode != 0 { |
| 54 | t.Fatalf("cleanup exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 55 | } |
| 56 | |
| 57 | // Pane killed. |
| 58 | waitPaneGone(t, h, pane, 30*time.Second) |
| 59 | |
| 60 | // Worktree removed. |
| 61 | if _, err := os.Stat(worktree); !os.IsNotExist(err) { |
| 62 | t.Errorf("worktree should be removed, stat err: %v", err) |
| 63 | } |
| 64 | |
| 65 | // State file deleted. |
| 66 | for _, id := range h.RunIDs() { |
| 67 | if id == runID { |
| 68 | t.Errorf("state for %s should be deleted after cleanup", runID) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // TestCleanupDoneKeepsOpenWork runs the real `klaus cleanup --done` over a |
| 74 | // session with a ready change the restacker has made newer versions of, an |
| 75 | // integrated change, a merged PR and a running agent: it removes the |
| 76 | // finished changes' and PR's runs and the ready change's older run, keeps |
| 77 | // the ready change's latest run and the running one, and the session's cost |
| 78 | // is the same after as before. |
| 79 | func TestCleanupDoneKeepsOpenWork(t *testing.T) { |
| 80 | t.Parallel() |
| 81 | h := NewHarness(t) |
| 82 | h.installValleyStub() |
| 83 | h.WriteStub("gh", `#!/usr/bin/env bash |
| 84 | case "$*" in |
| 85 | *"--json state "*) echo MERGED ;; |
| 86 | esac |
| 87 | exit 0 |
| 88 | `) |
| 89 | sha := func(c byte) string { return strings.Repeat(string(c), 40) } |
| 90 | ago := func(m int) time.Time { return time.Now().Add(-time.Duration(m) * time.Minute) } |
| 91 | |
| 92 | // The ready change's first run pushed v1, its follow-up v2, and the |
| 93 | // restacker made v3, the active one. |
| 94 | older := h.valleyRun("20261013-0900-0001", "first try", "rstk", "Add a health check", 1, sha('1'), ago(30)) |
| 95 | latest := h.valleyRun("20261013-0900-0002", "follow-up", "rstk", "Add a health check", 2, sha('2'), ago(20)) |
| 96 | landed := h.valleyRun("20261013-0900-0003", "fix the build", "lndd", "Fix the build", 1, sha('4'), ago(15)) |
| 97 | landedV := version(1, sha('4'), "Fix the build", "landed") |
| 98 | landedV["landed_commit"] = sha('4') |
| 99 | h.writeValleyChanges(map[string]any{ |
| 100 | "schema": "valley-changes/v1", |
| 101 | "host": "valley-host", |
| 102 | "repos": []any{map[string]any{ |
| 103 | "repo": "widget", "url": "git@valley-host:widget.git", "error": nil, |
| 104 | "changes": []any{ |
| 105 | change(latest, "ready", 3, version(1, sha('1'), "Add a health check", "not-landed"), |
| 106 | version(2, sha('2'), "Add a health check", "not-landed"), |
| 107 | version(3, sha('3'), "Add a health check", "not-landed")), |
| 108 | change(landed, "ready", 1, landedV), |
| 109 | }, |
| 110 | "branches": []any{}, |
| 111 | }}, |
| 112 | }) |
| 113 | prCost := 2.5 |
| 114 | merged := &run.State{ |
| 115 | ID: "20261013-0900-0004", Type: "launch", Prompt: "ship the docs", TargetRepo: strPtrE2E("acme/widget"), |
| 116 | Branch: "agent/docs", PRURL: strPtrE2E("https://github.com/acme/widget/pull/41"), CostUSD: &prCost, |
| 117 | CreatedAt: ago(10).Format(time.RFC3339), |
| 118 | } |
| 119 | h.SeedState(merged) |
| 120 | |
| 121 | if res := h.RunKlaus("launch", "do some work"); res.ExitCode != 0 { |
| 122 | t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 123 | } |
| 124 | h.WaitForClaudeStart(30 * time.Second) |
| 125 | var running string |
| 126 | for _, id := range h.RunIDs() { |
| 127 | if !strings.HasPrefix(id, "20261013-") { |
| 128 | running = id |
| 129 | } |
| 130 | } |
| 131 | if running == "" { |
| 132 | t.Fatalf("no launched run among %v", h.RunIDs()) |
| 133 | } |
| 134 | |
| 135 | sessionCost := func() string { |
| 136 | t.Helper() |
| 137 | res := h.RunKlaus("status") |
| 138 | if res.ExitCode != 0 { |
| 139 | t.Fatalf("status exited %d; stderr: %s", res.ExitCode, res.Stderr) |
| 140 | } |
| 141 | for _, line := range strings.Split(res.Stdout, "\n") { |
| 142 | if strings.HasPrefix(line, "Session cost: ") { |
| 143 | return strings.Fields(line)[2] |
| 144 | } |
| 145 | } |
| 146 | t.Fatalf("status shows no session cost:\n%s", res.Stdout) |
| 147 | return "" |
| 148 | } |
| 149 | before := sessionCost() |
| 150 | |
| 151 | res := h.RunKlaus("cleanup", "--done") |
| 152 | if res.ExitCode != 0 { |
| 153 | t.Fatalf("cleanup --done exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 154 | } |
| 155 | left := make(map[string]bool) |
| 156 | for _, id := range h.RunIDs() { |
| 157 | left[id] = true |
| 158 | } |
| 159 | for id, keep := range map[string]bool{older.ID: false, latest.ID: true, landed.ID: false, merged.ID: false, running: true} { |
| 160 | if left[id] != keep { |
| 161 | t.Errorf("%s: kept %v, want %v\n%s", id, left[id], keep, res.Stdout) |
| 162 | } |
| 163 | verb := "removed " |
| 164 | if keep { |
| 165 | verb = "kept " |
| 166 | } |
| 167 | if !strings.Contains(res.Stdout, verb+id+" ") { |
| 168 | t.Errorf("cleanup --done should say it %s%s:\n%s", verb, id, res.Stdout) |
| 169 | } |
| 170 | } |
| 171 | if after := sessionCost(); after != before { |
| 172 | t.Errorf("session cost %s after cleanup --done, want %s as before", after, before) |
| 173 | } |
| 174 | } |