| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "the-valley/flow/internal/run" |
| 11 | ) |
| 12 | |
| 13 | // useWorker makes the fake worker of a backend, claude, codex or agy, print |
| 14 | // that backend's stream for a run whose conversation is session, with no PR |
| 15 | // URL, so finalize records the pushed branch. Every fake worker is |
| 16 | // claudeStubScript, so resetWorker, WaitForClaudeStart, ClaudeArgv and |
| 17 | // ReleaseClaude serve all three. The claude stub is left as it is, which may |
| 18 | // be recordConversation's. |
| 19 | func (h *Harness) useWorker(kind, session string) { |
| 20 | h.t.Helper() |
| 21 | switch kind { |
| 22 | case "claude": |
| 23 | h.WriteDefaultClaudeOutput(`{"type":"result","subtype":"success","total_cost_usd":0.5,"duration_ms":1000,"session_id":"` + session + `"}` + "\n") |
| 24 | case "codex": |
| 25 | h.WriteStub("codex", h.claudeStubScript()) |
| 26 | h.WriteDefaultClaudeOutput(`{"type":"thread.started","thread_id":"` + session + `"} |
| 27 | {"type":"item.completed","item":{"type":"agent_message","text":"Pushed the branch."}} |
| 28 | {"type":"turn.completed","usage":{"input_tokens":7,"output_tokens":3}}` + "\n") |
| 29 | case "agy": |
| 30 | h.WriteStub("agy", h.claudeStubScript()) |
| 31 | h.WriteDefaultClaudeOutput(`{"event":"init","conversation_id":"` + session + `","init":{"model":"test"}} |
| 32 | {"event":"result","result":{"conversation_id":"` + session + `","status":"SUCCESS","response":"Pushed the branch.","duration_seconds":1}}` + "\n") |
| 33 | default: |
| 34 | h.t.Fatalf("no fake worker for backend %q", kind) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // startRun launches a run, waits for its fake worker to start, and has it |
| 39 | // commit and push file; it returns the run and the launch's output. |
| 40 | func (h *Harness) startRun(file string, args ...string) (*runAndOutput, string) { |
| 41 | h.t.Helper() |
| 42 | st, res := h.launchRun(args...) |
| 43 | h.WaitForClaudeStart(30 * time.Second) |
| 44 | head := h.pushWork(st, file, "Work on "+file) |
| 45 | return &runAndOutput{st.ID, st.Branch, st.ChangeID, st.Worktree, res.Stdout}, head |
| 46 | } |
| 47 | |
| 48 | type runAndOutput struct{ ID, Branch, ChangeID, Worktree, Stdout string } |
| 49 | |
| 50 | // waitNextSecond sleeps into the next second. Runs record when they were |
| 51 | // created to the second, so a run meant to be a change's newest must start |
| 52 | // in a later second than the run before it. |
| 53 | func waitNextSecond() { |
| 54 | time.Sleep(time.Until(time.Now().Truncate(time.Second).Add(time.Second))) |
| 55 | } |
| 56 | |
| 57 | // TestChangeContinuesOnEveryBackend continues a change a Claude run made |
| 58 | // with a Codex worker and with an agy worker. Each works on the change's |
| 59 | // branch with its change ID, stamps it on its commit, and finalize marks |
| 60 | // the pushed head ready as a version of the same change. Neither can resume |
| 61 | // a Claude conversation, and the replay: line says so. |
| 62 | func TestChangeContinuesOnEveryBackend(t *testing.T) { |
| 63 | for kind, why := range map[string]string{ |
| 64 | "codex": "a codex worker can't resume its conversation", |
| 65 | "agy": "agy workers can't resume a conversation", |
| 66 | } { |
| 67 | t.Run(kind, func(t *testing.T) { |
| 68 | t.Parallel() |
| 69 | h := NewHarness(t) |
| 70 | h.RegisterProject("acme/widget", h.RepoDir) |
| 71 | h.installValleyStub() |
| 72 | h.joinValley() |
| 73 | |
| 74 | h.useWorker("claude", changeSession) |
| 75 | first, _ := h.startRun("health.go", "add a health check endpoint") |
| 76 | h.finishRun(first.ID) |
| 77 | |
| 78 | h.resetWorker() |
| 79 | h.useWorker(kind, kind+"-session") |
| 80 | follow, head := h.startRun("health_test.go", "--repo", "widget", "--backend", kind, "--change", first.ID, "test the health check") |
| 81 | want := "replay: fresh agent (" + why + ")" |
| 82 | if kind == "codex" { |
| 83 | want = "replay: fresh agent (the latest run, " + first.ID + " (claude), ran on another backend; " + why + ")" |
| 84 | } |
| 85 | if !strings.Contains(follow.Stdout, want) { |
| 86 | t.Errorf("launch output missing %q:\n%s", want, follow.Stdout) |
| 87 | } |
| 88 | if follow.Branch != first.Branch || follow.ChangeID != first.ChangeID { |
| 89 | t.Errorf("%s run on branch %q, change %q; want %q, %q", kind, follow.Branch, follow.ChangeID, first.Branch, first.ChangeID) |
| 90 | } |
| 91 | // The stubs record their arguments, not their name. |
| 92 | if argv := h.ClaudeArgv(); argv == "" || strings.HasPrefix(argv, "exec\nfork\n") { |
| 93 | t.Errorf("%s worker should start fresh:\n%s", kind, argv) |
| 94 | } |
| 95 | if got := h.GitOutput(follow.Worktree, "log", "-1", "--format=%(trailers:key=Change-Id,valueonly)"); got != first.ChangeID { |
| 96 | t.Errorf("%s run's commit Change-Id = %q, want %q", kind, got, first.ChangeID) |
| 97 | } |
| 98 | st := h.finishRun(follow.ID) |
| 99 | if st.Backend != kind { |
| 100 | t.Errorf("run's backend = %q, want %q", st.Backend, kind) |
| 101 | } |
| 102 | ready := follow.Worktree + ": ready --change " + first.ChangeID + " --head " + head + " " + first.Branch |
| 103 | if calls := h.valleyCalls("ready"); len(calls) != 2 || calls[1] != ready { |
| 104 | t.Errorf("valley ready calls = %q, want the second to be %q", calls, ready) |
| 105 | } |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // TestChangeReplaysOnlyTheLatestRun: a change worked on by a Claude run, then |
| 111 | // a Codex run, continued on Claude, starts fresh. The Claude conversation on |
| 112 | // the data ref never saw the Codex run's work. |
| 113 | func TestChangeReplaysOnlyTheLatestRun(t *testing.T) { |
| 114 | t.Parallel() |
| 115 | h := NewHarness(t) |
| 116 | h.recordConversation() |
| 117 | |
| 118 | h.useWorker("claude", changeSession) |
| 119 | a, _ := h.startRun("health.go", "add a health check endpoint") |
| 120 | h.finishRun(a.ID) |
| 121 | |
| 122 | h.resetWorker() |
| 123 | h.useWorker("codex", "codex-thread") |
| 124 | waitNextSecond() |
| 125 | b, _ := h.startRun("health_test.go", "--backend", "codex", "--change", a.ID, "test the health check") |
| 126 | h.finishRun(b.ID) |
| 127 | |
| 128 | h.resetWorker() |
| 129 | h.useWorker("claude", changeSession) |
| 130 | c, res := h.launchRun("--change", a.ID, "rename the endpoint") |
| 131 | h.WaitForClaudeStart(30 * time.Second) |
| 132 | want := "replay: fresh agent (the latest run, " + b.ID + " (codex), ran on another backend; a claude worker can't resume its conversation)" |
| 133 | if !strings.Contains(res.Stdout, want) { |
| 134 | t.Errorf("launch output missing %q:\n%s", want, res.Stdout) |
| 135 | } |
| 136 | if argv := h.ClaudeArgv(); strings.Contains(argv, "--resume") { |
| 137 | t.Errorf("claude should not resume %s's conversation, which predates %s:\n%s", a.ID, b.ID, argv) |
| 138 | } |
| 139 | h.finishRun(c.ID) |
| 140 | } |
| 141 | |
| 142 | // TestChangeForksCodexSession: a Codex run continuing a change a Codex run |
| 143 | // made forks that run's session, as Claude replays; --no-replay starts fresh. |
| 144 | func TestChangeForksCodexSession(t *testing.T) { |
| 145 | t.Parallel() |
| 146 | h := NewHarness(t) |
| 147 | h.useWorker("codex", "codex-thread") |
| 148 | a, _ := h.startRun("health.go", "--backend", "codex", "add a health check endpoint") |
| 149 | h.finishRun(a.ID) |
| 150 | |
| 151 | h.resetWorker() |
| 152 | b, res := h.launchRun("--backend", "codex", "--change", a.ID, "test the health check") |
| 153 | h.WaitForClaudeStart(30 * time.Second) |
| 154 | if want := "replay: forking Codex session codex-thread from run " + a.ID; !strings.Contains(res.Stdout, want) { |
| 155 | t.Errorf("launch output missing %q:\n%s", want, res.Stdout) |
| 156 | } |
| 157 | if argv := h.ClaudeArgv(); !strings.HasPrefix(argv, "exec\nfork\ncodex-thread\n") { |
| 158 | t.Errorf("codex should fork the change's session:\n%s", argv) |
| 159 | } |
| 160 | if b.OriginalRunID == nil || *b.OriginalRunID != a.ID { |
| 161 | t.Errorf("run should record the run it continues, %s: %v", a.ID, b.OriginalRunID) |
| 162 | } |
| 163 | h.finishRun(b.ID) |
| 164 | |
| 165 | h.resetWorker() |
| 166 | c, res := h.launchRun("--backend", "codex", "--change", a.ID, "--no-replay", "start the tests over") |
| 167 | h.WaitForClaudeStart(30 * time.Second) |
| 168 | if !strings.Contains(res.Stdout, "replay: fresh agent (--no-replay)") { |
| 169 | t.Errorf("launch output should say --no-replay starts fresh:\n%s", res.Stdout) |
| 170 | } |
| 171 | if argv := h.ClaudeArgv(); !strings.HasPrefix(argv, "exec\n--json\n") { |
| 172 | t.Errorf("--no-replay should start a fresh codex worker:\n%s", argv) |
| 173 | } |
| 174 | h.finishRun(c.ID) |
| 175 | } |
| 176 | |
| 177 | // TestChangeReplayIgnoresTrackRecords: `klaus track` writes a record naming |
| 178 | // the change, newer than the codex run that made it. That record holds no |
| 179 | // conversation, so a codex run continuing the change still forks the codex |
| 180 | // run's session rather than starting fresh as if the latest run were claude's. |
| 181 | func TestChangeReplayIgnoresTrackRecords(t *testing.T) { |
| 182 | t.Parallel() |
| 183 | h := NewHarness(t) |
| 184 | h.useWorker("codex", "codex-thread") |
| 185 | a, _ := h.startRun("health.go", "--backend", "codex", "add a health check endpoint") |
| 186 | first := h.finishRun(a.ID) |
| 187 | |
| 188 | waitNextSecond() |
| 189 | h.SeedState(&run.State{ |
| 190 | ID: time.Now().Format("20060102-1504") + "-ffffffff", |
| 191 | Type: "track", |
| 192 | TargetRepo: first.TargetRepo, |
| 193 | ChangeID: first.ChangeID, |
| 194 | CreatedAt: time.Now().Format(time.RFC3339), |
| 195 | }) |
| 196 | |
| 197 | h.resetWorker() |
| 198 | b, res := h.launchRun("--backend", "codex", "--change", a.ID, "test the health check") |
| 199 | h.WaitForClaudeStart(30 * time.Second) |
| 200 | if want := "replay: forking Codex session codex-thread from run " + a.ID; !strings.Contains(res.Stdout, want) { |
| 201 | t.Errorf("launch output missing %q:\n%s", want, res.Stdout) |
| 202 | } |
| 203 | if b.OriginalRunID == nil || *b.OriginalRunID != a.ID { |
| 204 | t.Errorf("run should continue %s, not the track record: %v", a.ID, b.OriginalRunID) |
| 205 | } |
| 206 | h.finishRun(b.ID) |
| 207 | } |