package thintest import ( "encoding/json" "fmt" "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // pushed starts a recorded task and reports its Engineering step done on // cand, returning the task id and the step's dispatch. func (r *toolRig) pushed(key, title, base, cand string) (string, *wire.DispatchCommand) { r.t.Helper() // separate: these rigs record many tasks with one brief on purpose. 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})) if res := r.call(key+"-start", wire.ToolStartTask, map[string]string{"task": id, "brief": "Fix it."}); res.IsError { r.t.Fatalf("start: %+v", res) } _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) r.step(key+"-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, Family: "claude", Account: "claude-a"}) 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"}) return id, d } // dispatched reads the next dispatch and checks what it judges or builds on. func (r *toolRig) dispatched(specialist, base, cand string, round int) *wire.DispatchCommand { r.t.Helper() _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if d.Specialist != specialist || d.BaseSHA != base || d.CandidateSHA != cand || d.Round != round || d.Attempt != 1 { r.t.Fatalf("dispatch for %s on %s from %s round %d: %+v", specialist, cand, base, round, d) } return d } func exit(n int) *int { return &n } func (r *toolRig) checks(key string, d *wire.DispatchCommand, outcome string, code *int, reason, output string) { r.t.Helper() gate := "make ci" if outcome == wire.ChecksNone || outcome == wire.ChecksCannotRun { gate = "" } 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}) } func (r *toolRig) verdict(key string, d *wire.DispatchCommand, verdict string, findings ...wire.Finding) { r.t.Helper() if findings == nil { findings = []wire.Finding{} } 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."}) } // TestContractReviewAndChecks is scenario 2 to the end on the contract: // a pushed candidate gets the repository's checks, then a review; changes // requested send one automatic Engineering fix round on top of it; the // new candidate leaves the first judgements behind and is judged afresh; // approval on that one SHA makes the task ready to merge, the person's // decision; the runner watches the branch and a merge of that candidate // makes the task done. func TestContractReviewAndChecks(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) ctx := r.ctx base, cand, cand2 := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("3", 40) id, eng := r.pushed("a", "Fix the README typo", base, cand) task, _ := r.client.Task(ctx, id) // The runner watches the pushed candidate; checks run on it first. _, body := r.expect(wire.FrameWatchMerge) shape(t, "frames/watch_merge.json", r.runner.Received[len(r.runner.Received)-1]) if w := body.(*wire.WatchMergeCommand); w.TaskID != id || w.CandidateSHA != cand || w.Branch != task.Branch || w.Repo != "amadan.net/oficina/sample" || w.Person != Owner { t.Fatalf("watch %+v", w) } c1 := r.dispatched(wire.SpecialistChecks, base, cand, 0) r.wait("in review", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageInReview }) // A judgement of another SHA, or without a whole result, counts for nothing. 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)}) 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)}) if k, _ := r.client.Task(ctx, id); k.Checks != "" || k.Steps[1].State == wire.StepDone { t.Fatalf("a wrong judgement counted: %+v", k.Steps[1]) } r.checks("a-c1", c1, wire.ChecksPassed, exit(0), "", "ok \tsample\t0.01s\n") rv1 := r.dispatched(wire.SpecialistReview, base, cand, 0) if rv1.AvoidAccount != "claude-a" || !strings.Contains(rv1.Brief, "make ci passed.") { t.Fatalf("review dispatch %+v", rv1) } 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}) // One automatic fix round, on top of the candidate, with what review found. fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) 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") { t.Fatalf("fix round %+v", fix) } // This runner speaks wire.ProtocolMergeFacts, past ProtocolSpend: the // fix round's feedback reaches it, and only the fix round's // (TestContractFixRoundFeedbackByProtocol covers the older runners). if fix.Feedback == "" || eng.Feedback != "" || rv1.Feedback != "" { t.Fatalf("fix round feedback at ProtocolMergeFacts %q", fix.Feedback) } k, _ := r.client.Task(ctx, id) if k.Stage != wire.StageInProgress || k.CandidateSHA != cand || k.Verdict != wire.VerdictChanges || k.Checks != wire.ChecksPassed || k.FixRounds != 1 || k.NeedsYou != "" { t.Fatalf("during the fix round %+v", k) } 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 { t.Fatalf("review findings %+v", f) } r.step("a-f-run", wire.StepReport{TaskID: id, StepID: fix.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: cand}) 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}) // The new candidate invalidates both judgements: nothing is judged yet. _, body = r.expect(wire.FrameWatchMerge) if w := body.(*wire.WatchMergeCommand); w.CandidateSHA != cand2 { t.Fatalf("watch %+v", w) } c2 := r.dispatched(wire.SpecialistChecks, base, cand2, 1) k, _ = r.client.Task(ctx, id) if k.CandidateSHA != cand2 || k.Checks != "" || k.Verdict != "" || k.ReviewLabel != "" || k.Stage != wire.StageInReview { t.Fatalf("judgements of the old candidate kept: %+v", k) } // A delayed checks result for the older SHA, arriving on the new // candidate's checks step (a runner that judged the old one), is // dropped by the judged-SHA rule. 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{}}) if k, _ = r.client.Task(ctx, id); k.Checks != "" || k.Steps[len(k.Steps)-1].State == wire.StepDone { t.Fatalf("an older SHA's checks counted: %+v", k) } r.checks("a-c2", c2, wire.ChecksPassed, exit(0), "", "ok\n") rv2 := r.dispatched(wire.SpecialistReview, base, cand2, 1) // Likewise a delayed review verdict of the older SHA on the new review // step: it cannot make the newer candidate ready. 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{}}) if k, _ = r.client.Task(ctx, id); k.Verdict != "" || k.Stage == wire.StageReadyToMerge { t.Fatalf("an older SHA's verdict counted: %+v", k) } // A review of the old candidate arriving late changes nothing. r.verdict("a-r-late", rv1, wire.VerdictApprove) r.verdict("a-r2", rv2, wire.VerdictApprove) // Approved with checks passed, both on the branch's head: ready to // merge, and Equipo merges that SHA on its own. m := r.dispatched(wire.SpecialistMerge, base, cand2, 1) r.wait("merging", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageMerging }) k, _ = r.client.Task(ctx, id) shape(t, "api/task-ready.json", k) last := k.Steps[len(k.Steps)-1] 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) { t.Fatalf("merging %+v", k) } if needs, err := r.client.NeedsYou(ctx); err != nil || len(needs.Items) != 0 { t.Fatalf("a task Equipo is merging needs you: %+v %v", needs, err) } // A person may still merge it on Amadan (slice 4's detection stays): // a merge of an older candidate is not this one. if err := r.runner.Send(ctx, "m-old", wire.FrameMerged, wire.MergedReport{TaskID: id, CandidateSHA: cand}); err != nil { t.Fatal(err) } if err := r.runner.Send(ctx, "m-new", wire.FrameMerged, wire.MergedReport{TaskID: id, CandidateSHA: cand2}); err != nil { t.Fatal(err) } shape(t, "frames/merged.json", r.runner.Sent[len(r.runner.Sent)-1]) r.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) var stages []string r.conv.Locked(func(c *Conversation) { stages = c.Stages[id] }) for _, want := range []string{wire.StageNoted, wire.StageInProgress, wire.StageInReview, wire.StageReadyToMerge, wire.StageMerging, wire.StageDone} { if !slices.Contains(stages, want) { t.Fatalf("stages %v lack %s", stages, want) } } if needs, _ := r.client.NeedsYou(ctx); len(needs.Items) != 0 { t.Fatalf("done still needs you: %+v", needs) } } // TestContractFixRoundsCap: checks that fail send a fix round with their // output; review that still objects after two automatic rounds leaves the // task in review, waiting for its person, with nothing more dispatched. func TestContractFixRoundsCap(t *testing.T) { r := newToolRig(t) ctx := r.ctx base := strings.Repeat("1", 40) cands := []string{strings.Repeat("2", 40), strings.Repeat("3", 40), strings.Repeat("4", 40)} id, _ := r.pushed("b", "Fix the README typo", base, cands[0]) c := r.dispatched(wire.SpecialistChecks, base, cands[0], 0) r.checks("b-c0", c, wire.ChecksFailed, exit(2), "", "--- FAIL: TestReadme\nREADME.md:5: teh\nFAIL\n") fix := r.dispatched(wire.SpecialistEngineering, cands[0], "", 1) if !strings.Contains(fix.Brief, `checks ("make ci") failed`) || !strings.Contains(fix.Brief, "README.md:5: teh") { t.Fatalf("fix round brief %q", fix.Brief) } log, err := r.client.StepLog(ctx, c.StepID) if err != nil || !strings.Contains(log.Tail, "--- FAIL: TestReadme") { t.Fatalf("checks output %+v %v", log, err) } for round := 1; round <= 2; round++ { key := "b" + itoa(round) 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}) c = r.dispatched(wire.SpecialistChecks, base, cands[round], round) r.checks(key+"-c", c, wire.ChecksNone, nil, "", "") rv := r.dispatched(wire.SpecialistReview, base, cands[round], round) if !strings.Contains(rv.Brief, "checks on this commit: none found.") { t.Fatalf("review brief %q", rv.Brief) } r.verdict(key+"-r", rv, wire.VerdictChanges, wire.Finding{Text: "Still wrong.", Path: "README.md", Line: 5}) if round == 1 { fix = r.dispatched(wire.SpecialistEngineering, cands[1], "", 2) } } r.wait("needs you after the rounds", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsFixRounds }) k, _ := r.client.Task(ctx, id) if k.Stage != wire.StageInReview || k.FixRounds != 2 || k.CandidateSHA != cands[2] || k.Checks != wire.ChecksNone || k.Verdict != wire.VerdictChanges { t.Fatalf("after the cap %+v", k) } for _, h := range r.held { if h.f.Type == wire.FrameDispatch { t.Fatalf("dispatched after the cap: %+v", h.body) } } if needs, _ := r.client.NeedsYou(ctx); len(needs.Items) != 1 || needs.Items[0].NeedsYou != wire.NeedsFixRounds { t.Fatalf("needs you %+v", needs) } } // TestContractReviewBlockedAndRetry: checks that cannot run here are // honest and still lead to review; a review that ends without a verdict // is blocked for its person, and Retry reviews the same SHA again. A // failed push retries by publishing the recorded commit, not by running // Engineering anew. func TestContractReviewBlockedAndRetry(t *testing.T) { r := newToolRig(t) ctx := r.ctx base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.pushed("c", "Swipe back on iOS", base, cand) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) r.checks("c-c", c, wire.ChecksCannotRun, nil, "needs_macos", "") rv := r.dispatched(wire.SpecialistReview, base, cand, 0) if !strings.Contains(rv.Brief, "not run here (needs_macos)") { t.Fatalf("review brief %q", rv.Brief) } r.step("c-r-fail", wire.StepReport{TaskID: id, StepID: rv.StepID, Attempt: 1, State: wire.StepFailed, Reason: "no_verdict", CandidateSHA: cand, Termination: true}) r.wait("review blocked", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsReviewBlocked }) acc, err := r.client.RetryTask(ctx, id) if err != nil || acc.Attempt != 2 { t.Fatalf("retry review %+v %v", acc, err) } _, body := r.expect(wire.FrameDispatch) if d := body.(*wire.DispatchCommand); d.StepID != rv.StepID || d.Attempt != 2 || d.CandidateSHA != cand || d.Specialist != wire.SpecialistReview || d.BaseSHA != base { t.Fatalf("review retry %+v", d) } r.verdict("c-r2", &wire.DispatchCommand{TaskID: id, StepID: rv.StepID, Attempt: 2, BaseSHA: base, CandidateSHA: cand}, wire.VerdictApprove) r.wait("ready", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) if k, _ := r.client.Task(ctx, id); k.Checks != wire.ChecksCannotRun || k.ChecksReason != "needs_macos" || k.NeedsYou != wire.NeedsMerge { t.Fatalf("ready without checks %+v", k) } // Cancelling a ready task stops the merge watch. if _, err := r.client.CancelTask(ctx, id); err != nil { t.Fatal(err) } for { _, body := r.expect(wire.FrameWatchMerge) if w := body.(*wire.WatchMergeCommand); w.CandidateSHA == "" && w.TaskID == id { break } } // A failed push: Retry publishes the recorded commit again. other := taskID(t, r.call("d-rec", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Another", "brief": "b"})) r.call("d-start", wire.ToolStartTask, map[string]string{"task": other, "brief": "go"}) _, body = r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) r.step("d-run", wire.StepReport{TaskID: other, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) 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}) if _, err := r.client.RetryTask(ctx, other); err != nil { t.Fatal(err) } _, body = r.expect(wire.FrameDispatch) if d2 := body.(*wire.DispatchCommand); !d2.Republish || d2.FromAttempt != 1 || d2.CandidateSHA != cand || d2.Attempt != 2 || d2.BaseSHA != base { t.Fatalf("republish %+v", d2) } 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}) if _, err := r.client.RetryTask(ctx, other); err != nil { t.Fatal(err) } _, body = r.expect(wire.FrameDispatch) if d3 := body.(*wire.DispatchCommand); !d3.Republish || d3.FromAttempt != 1 || d3.Attempt != 3 { t.Fatalf("second republish keeps the attempt that made the commit: %+v", d3) } } // TestContractSyncJudgesOldCandidates: a task pushed before this instance // judged candidates (or whose follow-up was lost) gets its checks after // the runner's next sync. func TestContractSyncJudgesOldCandidates(t *testing.T) { r := newToolRig(t) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.pushed("e", "Old task", base, cand) r.dispatched(wire.SpecialistChecks, base, cand, 0) // As if the checks step and the watch had never been sent. if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_steps WHERE task_id = ? AND specialist = ?", id, wire.SpecialistChecks); err != nil { t.Fatal(err) } if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET watching = '' WHERE id = ?", id); err != nil { t.Fatal(err) } if err := r.runner.Send(r.ctx, "sync-e", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}, Steps: []wire.StepRun{}}); err != nil { t.Fatal(err) } if _, body := r.expect(wire.FrameWatchMerge); body.(*wire.WatchMergeCommand).CandidateSHA != cand { t.Fatal("no watch after sync") } r.dispatched(wire.SpecialistChecks, base, cand, 0) } // Astra round 1, finding 1: readiness holds only while the task branch is // at the judged SHA. A push of B over approved A takes the task out of // Ready to merge and drops A's checks and verdict from the task (the link // opens B); the branch back at A restores it. A report about an older // candidate changes nothing. func TestContractBranchMovedInvalidatesReadiness(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) ctx := r.ctx base, cand, other := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("5", 40) id, _ := r.pushed("g", "Fix the README typo", base, cand) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) r.checks("g-c", c, wire.ChecksPassed, exit(0), "", "ok\n") rv := r.dispatched(wire.SpecialistReview, base, cand, 0) r.verdict("g-r", rv, wire.VerdictApprove) m := r.dispatched(wire.SpecialistMerge, base, cand, 0) r.wait("merging", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageMerging }) branch := func(frame, head string) { if err := r.runner.Send(ctx, frame, wire.FrameBranch, wire.BranchReport{TaskID: id, CandidateSHA: cand, Head: head}); err != nil { t.Fatal(err) } } branch("b-moved", other) shape(t, "frames/branch.json", r.runner.Sent[len(r.runner.Sent)-1]) // The runner checks the head right before merging: moved, not merged. r.step("g-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) 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}) r.wait("moved", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsBranchMoved }) k, _ := r.client.Task(ctx, id) if k.Stage != wire.StageInReview || k.Checks != "" || k.Verdict != "" || k.ReviewLabel != "" || k.BranchHead != other || k.CandidateSHA != cand { t.Fatalf("readiness kept after the branch moved: %+v", k) } 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") { t.Fatalf("card after the branch moved: %s", page) } if needs, _ := r.client.NeedsYou(ctx); len(needs.Items) != 1 || needs.Items[0].NeedsYou != wire.NeedsBranchMoved { t.Fatalf("needs you %+v", needs) } // Deleted: still not ready. branch("b-gone", "") r.wait("gone", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsBranchMoved && c.Tasks[id].BranchHead == "" }) // Retry of the merge is refused while the branch is elsewhere: never a // SHA that is not the judged one as it stands. if _, err := r.client.RetryTask(ctx, id); err == nil { t.Fatal("retried a merge while the branch is not at the judged SHA") } // Back at the judged SHA: ready again, and the person's Retry merges // it (no automatic merge after a flap). branch("b-back", cand) r.wait("ready again", func(c *Conversation) bool { k := c.Tasks[id] return k.Stage == wire.StageReadyToMerge && k.Verdict == wire.VerdictApprove && k.NeedsYou == wire.NeedsMerge }) if r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("merged again on its own after the branch came back") } if acc, err := r.client.RetryTask(ctx, id); err != nil || acc.Attempt != 2 { t.Fatalf("retry the merge: %+v %v", acc, err) } m2, body := r.expect(wire.FrameDispatch) if d := body.(*wire.DispatchCommand); d.Specialist != wire.SpecialistMerge || d.Attempt != 2 || d.CandidateSHA != cand || d.StepID != m.StepID { t.Fatalf("merge retry %s %+v", m2.ID, d) } r.wait("merging again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageMerging }) // A report about a candidate that is not the newest is ignored. if err := r.runner.Send(ctx, "b-old", wire.FrameBranch, wire.BranchReport{TaskID: id, CandidateSHA: other, Head: base}); err != nil { t.Fatal(err) } r.client.Task(ctx, id) if k, _ := r.client.Task(ctx, id); k.Stage != wire.StageMerging { t.Fatalf("an old candidate's branch report moved the task: %+v", k) } } // Astra round 1, finding 4: a checks result the runner can emit // (needs_network) is acked and recorded, and review follows; a reason // the instance does not know is recorded as unknown rather than refused // (which closed the connection and replayed the report forever). func TestContractCannotRunReasonsAreDelivered(t *testing.T) { r := newToolRig(t) base := strings.Repeat("1", 40) for i, reason := range []string{"needs_network", "needs_quantum_computer"} { cand := strings.Repeat(string(rune('6'+i)), 40) key := "h" + itoa(i) id, _ := r.pushed(key, "Task "+key, base, cand) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) r.checks(key+"-c", c, wire.ChecksCannotRun, nil, reason, "") r.dispatched(wire.SpecialistReview, base, cand, 0) want := reason if !wire.StepReasons[reason] { want = wire.ReasonUnknown } if k, _ := r.client.Task(r.ctx, id); k.Checks != wire.ChecksCannotRun || k.ChecksReason != want { t.Fatalf("%s recorded as %+v", reason, k) } } } // A fix round's feedback goes only to a runner that speaks protocol 3: // an older runner (protocol 1 or 2, whose strict decoder knows no such // field) gets the same dispatch as before and runs the round fresh. func TestContractFixRoundFeedbackByProtocol(t *testing.T) { for _, proto := range []int{wire.Protocol1, wire.Protocol2, wire.ProtocolSpend} { t.Run(fmt.Sprint(proto), func(t *testing.T) { r := newToolRig(t) if proto > wire.Protocol1 { gen := r.runner.Welcome.Generation r.runner.Close() h := primaryHello(gen) h.Protocol = proto p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h) if err != nil { t.Fatal(err) } t.Cleanup(p.Close) if p.Welcome.Protocol != proto { t.Fatalf("welcome %+v", p.Welcome) } r.runner = p } base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) r.pushed("f", "Fix the README typo", base, cand) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) r.checks("f-c", c, wire.ChecksFailed, exit(2), "", "README.md:5: teh\n") fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) raw := string(r.runner.Received[len(r.runner.Received)-1]) if !strings.Contains(fix.Brief, "Fix round 1 of 2") { t.Fatalf("brief %q", fix.Brief) } 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")) { t.Fatalf("protocol %d: feedback sent %v: %s", proto, got, raw) } }) } } func TestContractBaseFailureStartsFixRound(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) base, cand := strings.Repeat("a", 40), strings.Repeat("b", 40) id, _ := r.pushed("base-failure", "Update budgets", base, cand) d := r.dispatched(wire.SpecialistChecks, base, cand, 0) 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."} // Simulate a contradictory report: candidate success cannot mask the base result. if err := json.Unmarshal([]byte(`{"base_checks":"failed"}`), &report); err != nil { t.Fatal(err) } r.step("base-result", report) fix := r.dispatched(wire.SpecialistEngineering, cand, "", 1) if !strings.Contains(fix.Feedback, "Base gate: failed") { t.Fatalf("base diagnostic absent: %s", fix.Feedback) } var outcome, reason, baseOutcome string 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 { t.Fatal(err) } if outcome != wire.ChecksFailed || baseOutcome != wire.ChecksFailed || reason != "base_checks_failed" { t.Fatalf("lost base failure: %s %s %s", outcome, reason, baseOutcome) } }