| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // pushed starts a recorded task and reports its Engineering step done on |
| 14 | // cand, returning the task id and the step's dispatch. |
| 15 | func (r *toolRig) pushed(key, title, base, cand string) (string, *wire.DispatchCommand) { |
| 16 | r.t.Helper() |
| 17 | // separate: these rigs record many tasks with one brief on purpose. |
| 18 | id := taskID(r.t, r.call(key+"-rec", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": title, "brief": "The README says teh.", "separate": true})) |
| 19 | if res := r.call(key+"-start", wire.ToolStartTask, map[string]string{"task": id, "brief": "Fix it."}); res.IsError { |
| 20 | r.t.Fatalf("start: %+v", res) |
| 21 | } |
| 22 | _, body := r.expect(wire.FrameDispatch) |
| 23 | d := body.(*wire.DispatchCommand) |
| 24 | r.step(key+"-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, Family: "claude", Account: "claude-a"}) |
| 25 | r.step(key+"-done", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Pushed: true, Termination: true, Family: "claude", Account: "claude-a"}) |
| 26 | return id, d |
| 27 | } |
| 28 | |
| 29 | // dispatched reads the next dispatch and checks what it judges or builds on. |
| 30 | func (r *toolRig) dispatched(specialist, base, cand string, round int) *wire.DispatchCommand { |
| 31 | r.t.Helper() |
| 32 | _, body := r.expect(wire.FrameDispatch) |
| 33 | d := body.(*wire.DispatchCommand) |
| 34 | if d.Specialist != specialist || d.BaseSHA != base || d.CandidateSHA != cand || d.Round != round || d.Attempt != 1 { |
| 35 | r.t.Fatalf("dispatch for %s on %s from %s round %d: %+v", specialist, cand, base, round, d) |
| 36 | } |
| 37 | return d |
| 38 | } |
| 39 | |
| 40 | func exit(n int) *int { return &n } |
| 41 | |
| 42 | func (r *toolRig) checks(key string, d *wire.DispatchCommand, outcome string, code *int, reason, output string) { |
| 43 | r.t.Helper() |
| 44 | gate := "make ci" |
| 45 | if outcome == wire.ChecksNone || outcome == wire.ChecksCannotRun { |
| 46 | gate = "" |
| 47 | } |
| 48 | r.step(key, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, Reason: reason, BaseSHA: d.BaseSHA, CandidateSHA: d.CandidateSHA, Termination: true, Checks: outcome, BaseChecksSHA: d.BaseSHA, Gate: gate, ExitCode: code, Output: output, LogTail: output}) |
| 49 | } |
| 50 | |
| 51 | func (r *toolRig) verdict(key string, d *wire.DispatchCommand, verdict string, findings ...wire.Finding) { |
| 52 | r.t.Helper() |
| 53 | if findings == nil { |
| 54 | findings = []wire.Finding{} |
| 55 | } |
| 56 | r.step(key, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, BaseSHA: d.BaseSHA, CandidateSHA: d.CandidateSHA, Termination: true, Family: "claude", Account: "claude-b", Verdict: verdict, Change: facts(d), ReviewLabel: wire.ReviewSameFamily, Findings: findings, Summary: "Read the change."}) |
| 57 | } |
| 58 | |
| 59 | // TestContractReviewAndChecks is scenario 2 to the end on the contract: |
| 60 | // a pushed candidate gets the repository's checks, then a review; changes |
| 61 | // requested send one automatic Engineering fix round on top of it; the |
| 62 | // new candidate leaves the first judgements behind and is judged afresh; |
| 63 | // approval on that one SHA makes the task ready to merge, the person's |
| 64 | // decision; the runner watches the branch and a merge of that candidate |
| 65 | // makes the task done. |
| 66 | func TestContractReviewAndChecks(t *testing.T) { |
| 67 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 68 | ctx := r.ctx |
| 69 | base, cand, cand2 := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("3", 40) |
| 70 | id, eng := r.pushed("a", "Fix the README typo", base, cand) |
| 71 | task, _ := r.client.Task(ctx, id) |
| 72 | |
| 73 | // The runner watches the pushed candidate; checks run on it first. |
| 74 | _, body := r.expect(wire.FrameWatchMerge) |
| 75 | shape(t, "frames/watch_merge.json", r.runner.Received[len(r.runner.Received)-1]) |
| 76 | if w := body.(*wire.WatchMergeCommand); w.TaskID != id || w.CandidateSHA != cand || w.Branch != task.Branch || w.Repo != "amadan.net/oficina/sample" || w.Person != Owner { |
| 77 | t.Fatalf("watch %+v", w) |
| 78 | } |
| 79 | c1 := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 80 | r.wait("in review", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageInReview }) |
| 81 | // A judgement of another SHA, or without a whole result, counts for nothing. |
| 82 | r.step("a-c-wrong", wire.StepReport{TaskID: id, StepID: c1.StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: cand2, Termination: true, Checks: wire.ChecksPassed, Gate: "make ci", ExitCode: exit(0)}) |
| 83 | r.step("a-c-half", wire.StepReport{TaskID: id, StepID: c1.StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: cand, Termination: true, Checks: wire.ChecksPassed, Gate: "make ci", ExitCode: exit(2)}) |
| 84 | if k, _ := r.client.Task(ctx, id); k.Checks != "" || k.Steps[1].State == wire.StepDone { |
| 85 | t.Fatalf("a wrong judgement counted: %+v", k.Steps[1]) |
| 86 | } |
| 87 | r.checks("a-c1", c1, wire.ChecksPassed, exit(0), "", "ok \tsample\t0.01s\n") |
| 88 | rv1 := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 89 | if rv1.AvoidAccount != "claude-a" || !strings.Contains(rv1.Brief, "make ci passed.") { |
| 90 | t.Fatalf("review dispatch %+v", rv1) |
| 91 | } |
| 92 | r.verdict("a-r1", rv1, wire.VerdictChanges, wire.Finding{Text: "The second typo is still there.", Path: "README.md", Line: 7}, wire.Finding{Text: "Bad path", Path: "../x", Line: 3}) |
| 93 | // One automatic fix round, on top of the candidate, with what review found. |
| 94 | fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) |
| 95 | if strings.Contains(fix.Brief, "Review finding:") || fix.StepID == eng.StepID || !strings.Contains(fix.Feedback, `Review finding: "The second typo is still there."`) || !strings.Contains(fix.Feedback, `Review path: "README.md"`) || !strings.Contains(fix.Feedback, "Review line: 7") || !strings.Contains(fix.Feedback, "Fix round 1 of 2") { |
| 96 | t.Fatalf("fix round %+v", fix) |
| 97 | } |
| 98 | // This runner speaks wire.ProtocolMergeFacts, past ProtocolSpend: the |
| 99 | // fix round's feedback reaches it, and only the fix round's |
| 100 | // (TestContractFixRoundFeedbackByProtocol covers the older runners). |
| 101 | if fix.Feedback == "" || eng.Feedback != "" || rv1.Feedback != "" { |
| 102 | t.Fatalf("fix round feedback at ProtocolMergeFacts %q", fix.Feedback) |
| 103 | } |
| 104 | k, _ := r.client.Task(ctx, id) |
| 105 | if k.Stage != wire.StageInProgress || k.CandidateSHA != cand || k.Verdict != wire.VerdictChanges || k.Checks != wire.ChecksPassed || k.FixRounds != 1 || k.NeedsYou != "" { |
| 106 | t.Fatalf("during the fix round %+v", k) |
| 107 | } |
| 108 | if f := k.Steps[2].Findings; len(f) != 2 || f[0].Path != "README.md" || f[0].Line != 7 || f[0].Repo != "amadan.net/oficina/sample" || f[1].Path != "" || f[1].Line != 0 { |
| 109 | t.Fatalf("review findings %+v", f) |
| 110 | } |
| 111 | r.step("a-f-run", wire.StepReport{TaskID: id, StepID: fix.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: cand}) |
| 112 | r.step("a-f-done", wire.StepReport{TaskID: id, StepID: fix.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: cand, CandidateSHA: cand2, Pushed: true, Termination: true}) |
| 113 | // The new candidate invalidates both judgements: nothing is judged yet. |
| 114 | _, body = r.expect(wire.FrameWatchMerge) |
| 115 | if w := body.(*wire.WatchMergeCommand); w.CandidateSHA != cand2 { |
| 116 | t.Fatalf("watch %+v", w) |
| 117 | } |
| 118 | c2 := r.dispatched(wire.SpecialistChecks, base, cand2, 1) |
| 119 | k, _ = r.client.Task(ctx, id) |
| 120 | if k.CandidateSHA != cand2 || k.Checks != "" || k.Verdict != "" || k.ReviewLabel != "" || k.Stage != wire.StageInReview { |
| 121 | t.Fatalf("judgements of the old candidate kept: %+v", k) |
| 122 | } |
| 123 | // A delayed checks result for the older SHA, arriving on the new |
| 124 | // candidate's checks step (a runner that judged the old one), is |
| 125 | // dropped by the judged-SHA rule. |
| 126 | r.step("a-c2-old", wire.StepReport{TaskID: id, StepID: c2.StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: cand, Termination: true, Checks: wire.ChecksPassed, Gate: "make ci", ExitCode: exit(0), Findings: []wire.Finding{}}) |
| 127 | if k, _ = r.client.Task(ctx, id); k.Checks != "" || k.Steps[len(k.Steps)-1].State == wire.StepDone { |
| 128 | t.Fatalf("an older SHA's checks counted: %+v", k) |
| 129 | } |
| 130 | r.checks("a-c2", c2, wire.ChecksPassed, exit(0), "", "ok\n") |
| 131 | rv2 := r.dispatched(wire.SpecialistReview, base, cand2, 1) |
| 132 | // Likewise a delayed review verdict of the older SHA on the new review |
| 133 | // step: it cannot make the newer candidate ready. |
| 134 | r.step("a-r2-old", wire.StepReport{TaskID: id, StepID: rv2.StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: cand, Termination: true, Verdict: wire.VerdictApprove, ReviewLabel: wire.ReviewSameFamily, Findings: []wire.Finding{}}) |
| 135 | if k, _ = r.client.Task(ctx, id); k.Verdict != "" || k.Stage == wire.StageReadyToMerge { |
| 136 | t.Fatalf("an older SHA's verdict counted: %+v", k) |
| 137 | } |
| 138 | // A review of the old candidate arriving late changes nothing. |
| 139 | r.verdict("a-r-late", rv1, wire.VerdictApprove) |
| 140 | r.verdict("a-r2", rv2, wire.VerdictApprove) |
| 141 | // Approved with checks passed, both on the branch's head: ready to |
| 142 | // merge, and Equipo merges that SHA on its own. |
| 143 | m := r.dispatched(wire.SpecialistMerge, base, cand2, 1) |
| 144 | r.wait("merging", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageMerging }) |
| 145 | k, _ = r.client.Task(ctx, id) |
| 146 | shape(t, "api/task-ready.json", k) |
| 147 | last := k.Steps[len(k.Steps)-1] |
| 148 | if k.CandidateSHA != cand2 || k.Checks != wire.ChecksPassed || k.Verdict != wire.VerdictApprove || k.ReviewLabel != wire.ReviewSameFamily || k.NeedsYou != "" || last.Specialist != wire.SpecialistMerge || last.CandidateSHA != cand2 || last.ID != m.StepID || !strings.Contains(k.URL, k.Branch) { |
| 149 | t.Fatalf("merging %+v", k) |
| 150 | } |
| 151 | if needs, err := r.client.NeedsYou(ctx); err != nil || len(needs.Items) != 0 { |
| 152 | t.Fatalf("a task Equipo is merging needs you: %+v %v", needs, err) |
| 153 | } |
| 154 | // A person may still merge it on Amadan (slice 4's detection stays): |
| 155 | // a merge of an older candidate is not this one. |
| 156 | if err := r.runner.Send(ctx, "m-old", wire.FrameMerged, wire.MergedReport{TaskID: id, CandidateSHA: cand}); err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | if err := r.runner.Send(ctx, "m-new", wire.FrameMerged, wire.MergedReport{TaskID: id, CandidateSHA: cand2}); err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | shape(t, "frames/merged.json", r.runner.Sent[len(r.runner.Sent)-1]) |
| 163 | r.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 164 | var stages []string |
| 165 | r.conv.Locked(func(c *Conversation) { stages = c.Stages[id] }) |
| 166 | for _, want := range []string{wire.StageNoted, wire.StageInProgress, wire.StageInReview, wire.StageReadyToMerge, wire.StageMerging, wire.StageDone} { |
| 167 | if !slices.Contains(stages, want) { |
| 168 | t.Fatalf("stages %v lack %s", stages, want) |
| 169 | } |
| 170 | } |
| 171 | if needs, _ := r.client.NeedsYou(ctx); len(needs.Items) != 0 { |
| 172 | t.Fatalf("done still needs you: %+v", needs) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // TestContractFixRoundsCap: checks that fail send a fix round with their |
| 177 | // output; review that still objects after two automatic rounds leaves the |
| 178 | // task in review, waiting for its person, with nothing more dispatched. |
| 179 | func TestContractFixRoundsCap(t *testing.T) { |
| 180 | r := newToolRig(t) |
| 181 | ctx := r.ctx |
| 182 | base := strings.Repeat("1", 40) |
| 183 | cands := []string{strings.Repeat("2", 40), strings.Repeat("3", 40), strings.Repeat("4", 40)} |
| 184 | id, _ := r.pushed("b", "Fix the README typo", base, cands[0]) |
| 185 | c := r.dispatched(wire.SpecialistChecks, base, cands[0], 0) |
| 186 | r.checks("b-c0", c, wire.ChecksFailed, exit(2), "", "--- FAIL: TestReadme\nREADME.md:5: teh\nFAIL\n") |
| 187 | fix := r.dispatched(wire.SpecialistEngineering, cands[0], "", 1) |
| 188 | if !strings.Contains(fix.Brief, `checks ("make ci") failed`) || !strings.Contains(fix.Brief, "README.md:5: teh") { |
| 189 | t.Fatalf("fix round brief %q", fix.Brief) |
| 190 | } |
| 191 | log, err := r.client.StepLog(ctx, c.StepID) |
| 192 | if err != nil || !strings.Contains(log.Tail, "--- FAIL: TestReadme") { |
| 193 | t.Fatalf("checks output %+v %v", log, err) |
| 194 | } |
| 195 | for round := 1; round <= 2; round++ { |
| 196 | key := "b" + itoa(round) |
| 197 | r.step(key+"-done", wire.StepReport{TaskID: id, StepID: fix.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: cands[round-1], CandidateSHA: cands[round], Pushed: true, Termination: true}) |
| 198 | c = r.dispatched(wire.SpecialistChecks, base, cands[round], round) |
| 199 | r.checks(key+"-c", c, wire.ChecksNone, nil, "", "") |
| 200 | rv := r.dispatched(wire.SpecialistReview, base, cands[round], round) |
| 201 | if !strings.Contains(rv.Brief, "checks on this commit: none found.") { |
| 202 | t.Fatalf("review brief %q", rv.Brief) |
| 203 | } |
| 204 | r.verdict(key+"-r", rv, wire.VerdictChanges, wire.Finding{Text: "Still wrong.", Path: "README.md", Line: 5}) |
| 205 | if round == 1 { |
| 206 | fix = r.dispatched(wire.SpecialistEngineering, cands[1], "", 2) |
| 207 | } |
| 208 | } |
| 209 | r.wait("needs you after the rounds", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsFixRounds }) |
| 210 | k, _ := r.client.Task(ctx, id) |
| 211 | if k.Stage != wire.StageInReview || k.FixRounds != 2 || k.CandidateSHA != cands[2] || k.Checks != wire.ChecksNone || k.Verdict != wire.VerdictChanges { |
| 212 | t.Fatalf("after the cap %+v", k) |
| 213 | } |
| 214 | for _, h := range r.held { |
| 215 | if h.f.Type == wire.FrameDispatch { |
| 216 | t.Fatalf("dispatched after the cap: %+v", h.body) |
| 217 | } |
| 218 | } |
| 219 | if needs, _ := r.client.NeedsYou(ctx); len(needs.Items) != 1 || needs.Items[0].NeedsYou != wire.NeedsFixRounds { |
| 220 | t.Fatalf("needs you %+v", needs) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // TestContractReviewBlockedAndRetry: checks that cannot run here are |
| 225 | // honest and still lead to review; a review that ends without a verdict |
| 226 | // is blocked for its person, and Retry reviews the same SHA again. A |
| 227 | // failed push retries by publishing the recorded commit, not by running |
| 228 | // Engineering anew. |
| 229 | func TestContractReviewBlockedAndRetry(t *testing.T) { |
| 230 | r := newToolRig(t) |
| 231 | ctx := r.ctx |
| 232 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 233 | id, _ := r.pushed("c", "Swipe back on iOS", base, cand) |
| 234 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 235 | r.checks("c-c", c, wire.ChecksCannotRun, nil, "needs_macos", "") |
| 236 | rv := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 237 | if !strings.Contains(rv.Brief, "not run here (needs_macos)") { |
| 238 | t.Fatalf("review brief %q", rv.Brief) |
| 239 | } |
| 240 | r.step("c-r-fail", wire.StepReport{TaskID: id, StepID: rv.StepID, Attempt: 1, State: wire.StepFailed, Reason: "no_verdict", CandidateSHA: cand, Termination: true}) |
| 241 | r.wait("review blocked", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsReviewBlocked }) |
| 242 | acc, err := r.client.RetryTask(ctx, id) |
| 243 | if err != nil || acc.Attempt != 2 { |
| 244 | t.Fatalf("retry review %+v %v", acc, err) |
| 245 | } |
| 246 | _, body := r.expect(wire.FrameDispatch) |
| 247 | if d := body.(*wire.DispatchCommand); d.StepID != rv.StepID || d.Attempt != 2 || d.CandidateSHA != cand || d.Specialist != wire.SpecialistReview || d.BaseSHA != base { |
| 248 | t.Fatalf("review retry %+v", d) |
| 249 | } |
| 250 | r.verdict("c-r2", &wire.DispatchCommand{TaskID: id, StepID: rv.StepID, Attempt: 2, BaseSHA: base, CandidateSHA: cand}, wire.VerdictApprove) |
| 251 | r.wait("ready", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 252 | if k, _ := r.client.Task(ctx, id); k.Checks != wire.ChecksCannotRun || k.ChecksReason != "needs_macos" || k.NeedsYou != wire.NeedsMerge { |
| 253 | t.Fatalf("ready without checks %+v", k) |
| 254 | } |
| 255 | // Cancelling a ready task stops the merge watch. |
| 256 | if _, err := r.client.CancelTask(ctx, id); err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | for { |
| 260 | _, body := r.expect(wire.FrameWatchMerge) |
| 261 | if w := body.(*wire.WatchMergeCommand); w.CandidateSHA == "" && w.TaskID == id { |
| 262 | break |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // A failed push: Retry publishes the recorded commit again. |
| 267 | other := taskID(t, r.call("d-rec", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Another", "brief": "b"})) |
| 268 | r.call("d-start", wire.ToolStartTask, map[string]string{"task": other, "brief": "go"}) |
| 269 | _, body = r.expect(wire.FrameDispatch) |
| 270 | d := body.(*wire.DispatchCommand) |
| 271 | r.step("d-run", wire.StepReport{TaskID: other, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 272 | r.step("d-fail", wire.StepReport{TaskID: other, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "push_failed", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 273 | if _, err := r.client.RetryTask(ctx, other); err != nil { |
| 274 | t.Fatal(err) |
| 275 | } |
| 276 | _, body = r.expect(wire.FrameDispatch) |
| 277 | if d2 := body.(*wire.DispatchCommand); !d2.Republish || d2.FromAttempt != 1 || d2.CandidateSHA != cand || d2.Attempt != 2 || d2.BaseSHA != base { |
| 278 | t.Fatalf("republish %+v", d2) |
| 279 | } |
| 280 | r.step("d-fail2", wire.StepReport{TaskID: other, StepID: d.StepID, Attempt: 2, State: wire.StepFailed, Reason: "push_failed", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 281 | if _, err := r.client.RetryTask(ctx, other); err != nil { |
| 282 | t.Fatal(err) |
| 283 | } |
| 284 | _, body = r.expect(wire.FrameDispatch) |
| 285 | if d3 := body.(*wire.DispatchCommand); !d3.Republish || d3.FromAttempt != 1 || d3.Attempt != 3 { |
| 286 | t.Fatalf("second republish keeps the attempt that made the commit: %+v", d3) |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // TestContractSyncJudgesOldCandidates: a task pushed before this instance |
| 291 | // judged candidates (or whose follow-up was lost) gets its checks after |
| 292 | // the runner's next sync. |
| 293 | func TestContractSyncJudgesOldCandidates(t *testing.T) { |
| 294 | r := newToolRig(t) |
| 295 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 296 | id, _ := r.pushed("e", "Old task", base, cand) |
| 297 | r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 298 | // As if the checks step and the watch had never been sent. |
| 299 | if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_steps WHERE task_id = ? AND specialist = ?", id, wire.SpecialistChecks); err != nil { |
| 300 | t.Fatal(err) |
| 301 | } |
| 302 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET watching = '' WHERE id = ?", id); err != nil { |
| 303 | t.Fatal(err) |
| 304 | } |
| 305 | if err := r.runner.Send(r.ctx, "sync-e", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}, Steps: []wire.StepRun{}}); err != nil { |
| 306 | t.Fatal(err) |
| 307 | } |
| 308 | if _, body := r.expect(wire.FrameWatchMerge); body.(*wire.WatchMergeCommand).CandidateSHA != cand { |
| 309 | t.Fatal("no watch after sync") |
| 310 | } |
| 311 | r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 312 | } |
| 313 | |
| 314 | // Astra round 1, finding 1: readiness holds only while the task branch is |
| 315 | // at the judged SHA. A push of B over approved A takes the task out of |
| 316 | // Ready to merge and drops A's checks and verdict from the task (the link |
| 317 | // opens B); the branch back at A restores it. A report about an older |
| 318 | // candidate changes nothing. |
| 319 | func TestContractBranchMovedInvalidatesReadiness(t *testing.T) { |
| 320 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 321 | ctx := r.ctx |
| 322 | base, cand, other := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("5", 40) |
| 323 | id, _ := r.pushed("g", "Fix the README typo", base, cand) |
| 324 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 325 | r.checks("g-c", c, wire.ChecksPassed, exit(0), "", "ok\n") |
| 326 | rv := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 327 | r.verdict("g-r", rv, wire.VerdictApprove) |
| 328 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 329 | r.wait("merging", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageMerging }) |
| 330 | branch := func(frame, head string) { |
| 331 | if err := r.runner.Send(ctx, frame, wire.FrameBranch, wire.BranchReport{TaskID: id, CandidateSHA: cand, Head: head}); err != nil { |
| 332 | t.Fatal(err) |
| 333 | } |
| 334 | } |
| 335 | branch("b-moved", other) |
| 336 | shape(t, "frames/branch.json", r.runner.Sent[len(r.runner.Sent)-1]) |
| 337 | // The runner checks the head right before merging: moved, not merged. |
| 338 | r.step("g-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 339 | r.step("g-m-moved", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "branch_moved", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 340 | r.wait("moved", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsBranchMoved }) |
| 341 | k, _ := r.client.Task(ctx, id) |
| 342 | if k.Stage != wire.StageInReview || k.Checks != "" || k.Verdict != "" || k.ReviewLabel != "" || k.BranchHead != other || k.CandidateSHA != cand { |
| 343 | t.Fatalf("readiness kept after the branch moved: %+v", k) |
| 344 | } |
| 345 | if page := r.get(r.inst.Cookie(), "/conversation", 200); strings.Contains(page, "data-merged") || strings.Contains(page, "Approved (") || !strings.Contains(page, "Needs you: the branch moved on Amadan after it was checked") || strings.Contains(page, "Checks passed") { |
| 346 | t.Fatalf("card after the branch moved: %s", page) |
| 347 | } |
| 348 | if needs, _ := r.client.NeedsYou(ctx); len(needs.Items) != 1 || needs.Items[0].NeedsYou != wire.NeedsBranchMoved { |
| 349 | t.Fatalf("needs you %+v", needs) |
| 350 | } |
| 351 | // Deleted: still not ready. |
| 352 | branch("b-gone", "") |
| 353 | r.wait("gone", func(c *Conversation) bool { |
| 354 | return c.Tasks[id].NeedsYou == wire.NeedsBranchMoved && c.Tasks[id].BranchHead == "" |
| 355 | }) |
| 356 | // Retry of the merge is refused while the branch is elsewhere: never a |
| 357 | // SHA that is not the judged one as it stands. |
| 358 | if _, err := r.client.RetryTask(ctx, id); err == nil { |
| 359 | t.Fatal("retried a merge while the branch is not at the judged SHA") |
| 360 | } |
| 361 | // Back at the judged SHA: ready again, and the person's Retry merges |
| 362 | // it (no automatic merge after a flap). |
| 363 | branch("b-back", cand) |
| 364 | r.wait("ready again", func(c *Conversation) bool { |
| 365 | k := c.Tasks[id] |
| 366 | return k.Stage == wire.StageReadyToMerge && k.Verdict == wire.VerdictApprove && k.NeedsYou == wire.NeedsMerge |
| 367 | }) |
| 368 | if r.heldKinds(wire.FrameDispatch) != 0 { |
| 369 | t.Fatal("merged again on its own after the branch came back") |
| 370 | } |
| 371 | if acc, err := r.client.RetryTask(ctx, id); err != nil || acc.Attempt != 2 { |
| 372 | t.Fatalf("retry the merge: %+v %v", acc, err) |
| 373 | } |
| 374 | m2, body := r.expect(wire.FrameDispatch) |
| 375 | if d := body.(*wire.DispatchCommand); d.Specialist != wire.SpecialistMerge || d.Attempt != 2 || d.CandidateSHA != cand || d.StepID != m.StepID { |
| 376 | t.Fatalf("merge retry %s %+v", m2.ID, d) |
| 377 | } |
| 378 | r.wait("merging again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageMerging }) |
| 379 | // A report about a candidate that is not the newest is ignored. |
| 380 | if err := r.runner.Send(ctx, "b-old", wire.FrameBranch, wire.BranchReport{TaskID: id, CandidateSHA: other, Head: base}); err != nil { |
| 381 | t.Fatal(err) |
| 382 | } |
| 383 | r.client.Task(ctx, id) |
| 384 | if k, _ := r.client.Task(ctx, id); k.Stage != wire.StageMerging { |
| 385 | t.Fatalf("an old candidate's branch report moved the task: %+v", k) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // Astra round 1, finding 4: a checks result the runner can emit |
| 390 | // (needs_network) is acked and recorded, and review follows; a reason |
| 391 | // the instance does not know is recorded as unknown rather than refused |
| 392 | // (which closed the connection and replayed the report forever). |
| 393 | func TestContractCannotRunReasonsAreDelivered(t *testing.T) { |
| 394 | r := newToolRig(t) |
| 395 | base := strings.Repeat("1", 40) |
| 396 | for i, reason := range []string{"needs_network", "needs_quantum_computer"} { |
| 397 | cand := strings.Repeat(string(rune('6'+i)), 40) |
| 398 | key := "h" + itoa(i) |
| 399 | id, _ := r.pushed(key, "Task "+key, base, cand) |
| 400 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 401 | r.checks(key+"-c", c, wire.ChecksCannotRun, nil, reason, "") |
| 402 | r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 403 | want := reason |
| 404 | if !wire.StepReasons[reason] { |
| 405 | want = wire.ReasonUnknown |
| 406 | } |
| 407 | if k, _ := r.client.Task(r.ctx, id); k.Checks != wire.ChecksCannotRun || k.ChecksReason != want { |
| 408 | t.Fatalf("%s recorded as %+v", reason, k) |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // A fix round's feedback goes only to a runner that speaks protocol 3: |
| 414 | // an older runner (protocol 1 or 2, whose strict decoder knows no such |
| 415 | // field) gets the same dispatch as before and runs the round fresh. |
| 416 | func TestContractFixRoundFeedbackByProtocol(t *testing.T) { |
| 417 | for _, proto := range []int{wire.Protocol1, wire.Protocol2, wire.ProtocolSpend} { |
| 418 | t.Run(fmt.Sprint(proto), func(t *testing.T) { |
| 419 | r := newToolRig(t) |
| 420 | if proto > wire.Protocol1 { |
| 421 | gen := r.runner.Welcome.Generation |
| 422 | r.runner.Close() |
| 423 | h := primaryHello(gen) |
| 424 | h.Protocol = proto |
| 425 | p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h) |
| 426 | if err != nil { |
| 427 | t.Fatal(err) |
| 428 | } |
| 429 | t.Cleanup(p.Close) |
| 430 | if p.Welcome.Protocol != proto { |
| 431 | t.Fatalf("welcome %+v", p.Welcome) |
| 432 | } |
| 433 | r.runner = p |
| 434 | } |
| 435 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 436 | r.pushed("f", "Fix the README typo", base, cand) |
| 437 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 438 | r.checks("f-c", c, wire.ChecksFailed, exit(2), "", "README.md:5: teh\n") |
| 439 | fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) |
| 440 | raw := string(r.runner.Received[len(r.runner.Received)-1]) |
| 441 | if !strings.Contains(fix.Brief, "Fix round 1 of 2") { |
| 442 | t.Fatalf("brief %q", fix.Brief) |
| 443 | } |
| 444 | if got := strings.Contains(raw, `"feedback"`); got != (proto >= wire.ProtocolSpend) || got && (!strings.HasPrefix(fix.Feedback, "Fix round 1 of 2") || !strings.HasSuffix(fix.Brief, fix.Feedback) || strings.Contains(fix.Feedback, "Fix the README typo")) { |
| 445 | t.Fatalf("protocol %d: feedback sent %v: %s", proto, got, raw) |
| 446 | } |
| 447 | }) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | func TestContractBaseFailureStartsFixRound(t *testing.T) { |
| 452 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 453 | base, cand := strings.Repeat("a", 40), strings.Repeat("b", 40) |
| 454 | id, _ := r.pushed("base-failure", "Update budgets", base, cand) |
| 455 | d := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 456 | report := wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Termination: true, Checks: wire.ChecksPassed, Gate: "make ci", ExitCode: exit(0), Output: "Candidate gate diagnostic: pass. Base gate: failed."} |
| 457 | // Simulate a contradictory report: candidate success cannot mask the base result. |
| 458 | if err := json.Unmarshal([]byte(`{"base_checks":"failed"}`), &report); err != nil { |
| 459 | t.Fatal(err) |
| 460 | } |
| 461 | r.step("base-result", report) |
| 462 | fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) |
| 463 | if !strings.Contains(fix.Feedback, "Base gate: failed") { |
| 464 | t.Fatalf("base diagnostic absent: %s", fix.Feedback) |
| 465 | } |
| 466 | var outcome, reason, baseOutcome string |
| 467 | if err := r.inst.DB.Writer().QueryRow("SELECT checks,reason,base_checks FROM chat_steps WHERE id=?", d.StepID).Scan(&outcome, &reason, &baseOutcome); err != nil { |
| 468 | t.Fatal(err) |
| 469 | } |
| 470 | if outcome != wire.ChecksFailed || baseOutcome != wire.ChecksFailed || reason != "base_checks_failed" { |
| 471 | t.Fatalf("lost base failure: %s %s %s", outcome, reason, baseOutcome) |
| 472 | } |
| 473 | } |
| 474 | |