| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Stuck tasks (Paul, 2026-11-25: "empower Equipo to be more able to do |
| 15 | // work"): the coordinator can see why a preview could not be scanned, |
| 16 | // offer the person a one-tap retry of a failed merge, and run a failed |
| 17 | // preview, checks or review again; it never merges or publishes. |
| 18 | |
| 19 | // toolsOf is status's runner tools. |
| 20 | func toolsOf(t *testing.T, res wire.ToolResult) wire.ToolHealth { |
| 21 | t.Helper() |
| 22 | var out struct { |
| 23 | Runner struct { |
| 24 | Tools *wire.ToolHealth `json:"tools"` |
| 25 | } `json:"runner"` |
| 26 | } |
| 27 | if res.IsError || json.Unmarshal([]byte(res.Text), &out) != nil || out.Runner.Tools == nil { |
| 28 | t.Fatalf("status %+v", res) |
| 29 | } |
| 30 | return *out.Runner.Tools |
| 31 | } |
| 32 | |
| 33 | // TestContractStatusToolHealth: status says whether the primary's pinned |
| 34 | // preview scanner is installed and matches its pin, from its newest |
| 35 | // hello; unknown when the runner does not say (an older runner). Never a |
| 36 | // path. |
| 37 | func TestContractStatusToolHealth(t *testing.T) { |
| 38 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 39 | if got := toolsOf(t, r.call("s1", wire.ToolStatus, map[string]string{})); got != (wire.ToolHealth{Scanner: wire.ToolUnknown}) { |
| 40 | t.Fatalf("an unsaid health %+v", got) |
| 41 | } |
| 42 | h := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolCurrent) |
| 43 | h.Capabilities.Tools = &wire.ToolHealth{Scanner: wire.ToolMismatch} |
| 44 | if err := r.runner.Send(r.ctx, "h-tools", wire.FrameHello, h); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | r.client.Snapshot(r.ctx) |
| 48 | res := r.call("s2", wire.ToolStatus, map[string]string{}) |
| 49 | if got := toolsOf(t, res); got != (wire.ToolHealth{Scanner: wire.ToolMismatch}) { |
| 50 | t.Fatalf("reported health %+v", got) |
| 51 | } |
| 52 | if strings.Contains(res.Text, "/opt/") || strings.Contains(res.Text, "/home/") || strings.Contains(res.Text, "gitleaks") { |
| 53 | t.Fatalf("status names a path: %s", res.Text) |
| 54 | } |
| 55 | // The page renderer's health (Linux amd64 runners) is shown as said. |
| 56 | h.Capabilities.Tools = &wire.ToolHealth{Scanner: wire.ToolHealthy, Renderer: wire.ToolMismatch} |
| 57 | if err := r.runner.Send(r.ctx, "h-renderer", wire.FrameHello, h); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | r.client.Snapshot(r.ctx) |
| 61 | if got := toolsOf(t, r.call("s2r", wire.ToolStatus, map[string]string{})); got != *h.Capabilities.Tools { |
| 62 | t.Fatalf("reported renderer health %+v", got) |
| 63 | } |
| 64 | // A report out of bounds is not believed: unknown again. |
| 65 | h.Capabilities.Tools = &wire.ToolHealth{Scanner: "fine"} |
| 66 | if err := r.runner.Send(r.ctx, "h-bad", wire.FrameHello, h); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | r.client.Snapshot(r.ctx) |
| 70 | if got := toolsOf(t, r.call("s3", wire.ToolStatus, map[string]string{})); got.Scanner != wire.ToolUnknown { |
| 71 | t.Fatalf("an invalid report believed %+v", got) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // TestContractOfferRetryMerge (stuck tasks c): the coordinator cannot |
| 76 | // merge. For a merge that failed and ended, offer_retry puts the merge |
| 77 | // card in front of the person, asking whether to try it again; nothing is |
| 78 | // dispatched until they tap Merge, and then the merge is theirs, decided |
| 79 | // by the gate for them. Asked again it is the one card; nothing to offer |
| 80 | // while a merge runs or once the branch moved. |
| 81 | func TestContractOfferRetryMerge(t *testing.T) { |
| 82 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 83 | ctx := r.ctx |
| 84 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 85 | id := r.judgedTask("o", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 86 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 87 | if res := r.call("o1", wire.ToolOfferRetry, map[string]string{"task": id}); !res.IsError || res.Code != "no_failed_merge" { |
| 88 | t.Fatalf("offered while merging: %+v", res) |
| 89 | } |
| 90 | r.step("o-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 91 | r.step("o-m-fail", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_failed", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 92 | r.wait("ready again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 93 | res := r.call("o2", wire.ToolOfferRetry, map[string]string{"task": id}) |
| 94 | if res.IsError || res.Code != "offered" || !strings.Contains(res.Text, "tap") { |
| 95 | t.Fatalf("offer %+v", res) |
| 96 | } |
| 97 | if refused := r.lease(r.runner, "o-ping", wire.LeaseCheck{}); refused != "" || r.heldKinds(wire.FrameDispatch) != 0 { |
| 98 | t.Fatalf("the offer dispatched something (%q)", refused) |
| 99 | } |
| 100 | k, _ := r.client.Task(ctx, id) |
| 101 | if last := k.Steps[len(k.Steps)-1]; k.Stage != wire.StageReadyToMerge || last.Specialist != wire.SpecialistMerge || last.Attempt != 1 || last.State != wire.StepFailed { |
| 102 | t.Fatalf("after the offer %s %+v", k.Stage, last) |
| 103 | } |
| 104 | var card wire.Card |
| 105 | r.wait("retry card", func(c *Conversation) bool { |
| 106 | for _, k := range c.Cards { |
| 107 | if k.Kind == wire.CardMerge && k.TaskID == id && k.State == wire.CardOpen { |
| 108 | card = k |
| 109 | } |
| 110 | } |
| 111 | return card.ID != "" |
| 112 | }) |
| 113 | if card.Question != "Merging Fix the README typo didn’t go through. Try the merge again?" || !slices.Equal(card.Options, []string{wire.ChoiceMerge, wire.ChoiceNotNow}) { |
| 114 | t.Fatalf("card %+v", card) |
| 115 | } |
| 116 | if page := r.get(r.inst.Cookie(), "/conversation", 200); !strings.Contains(page, "didn’t go through. Try the merge again?") { |
| 117 | t.Fatal("the card is not on the page") |
| 118 | } |
| 119 | // Asked again in the turn: the first offer's result, still the one |
| 120 | // card; the same call, its stored result. |
| 121 | if again := r.call("o3", wire.ToolOfferRetry, map[string]string{"task": id}); again.IsError || again.Code != "offered" || !again.Replayed { |
| 122 | t.Fatalf("second offer %+v", again) |
| 123 | } |
| 124 | if same := r.call("o2", wire.ToolOfferRetry, map[string]string{"task": id}); same != res { |
| 125 | t.Fatalf("replay %+v", same) |
| 126 | } |
| 127 | if n := len(r.mergeCards(id)); n != 1 { |
| 128 | t.Fatalf("%d merge cards", n) |
| 129 | } |
| 130 | // The person's tap merges, as theirs. |
| 131 | if _, err := r.client.Answer(ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | _, body := r.expect(wire.FrameDispatch) |
| 135 | if d := body.(*wire.DispatchCommand); d.Specialist != wire.SpecialistMerge || d.CandidateSHA != cand || d.Attempt != 2 { |
| 136 | t.Fatalf("dispatch after the tap %+v", d) |
| 137 | } |
| 138 | if k, _ := r.client.Task(ctx, id); k.MergeDecision == nil || k.MergeDecision.DecidedBy != "person:"+Owner { |
| 139 | t.Fatalf("decision %+v", k.MergeDecision) |
| 140 | } |
| 141 | m.Attempt = 2 |
| 142 | r.mergedStep("o-m2", m, strings.Repeat("e", 40)) |
| 143 | r.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 144 | // Merged: asked again in the same turn, only the ledger's first answer; |
| 145 | // no card is raised. |
| 146 | if res := r.call("o4", wire.ToolOfferRetry, map[string]string{"task": id}); !res.Replayed || r.count("SELECT count(*) FROM chat_cards WHERE task_id = ? AND kind = ? AND state = ?", id, wire.CardMerge, wire.CardOpen) != 0 { |
| 147 | t.Fatalf("offer on a merged task %+v", res) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // TestContractOfferRetryOnlyWhatTheTapCanMerge: a failed merge whose |
| 152 | // branch moved since is not offered (the tap could not merge it). |
| 153 | func TestContractOfferRetryOnlyWhatTheTapCanMerge(t *testing.T) { |
| 154 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 155 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 156 | id := r.judgedTask("n", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 157 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 158 | r.step("n-m-fail", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_failed", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 159 | if err := r.runner.Send(r.ctx, "n-away", wire.FrameBranch, wire.BranchReport{TaskID: id, CandidateSHA: cand, Head: strings.Repeat("8", 40)}); err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | r.wait("moved", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsBranchMoved }) |
| 163 | if res := r.call("n1", wire.ToolOfferRetry, map[string]string{"task": id}); !res.IsError || res.Code != "not_mergeable" { |
| 164 | t.Fatalf("offered with the branch away %+v", res) |
| 165 | } |
| 166 | if len(r.mergeCards(id)) != 0 { |
| 167 | t.Fatal("a card was raised") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // previewedAndMerging is an approved candidate with a preview config on a |
| 172 | // project that asks before merging: the preview is dispatched (step 4), |
| 173 | // the person taps Merge (step 5, queued), and the preview fails: the |
| 174 | // incident on tkbstr6cjl7h. |
| 175 | func (r *toolRig) previewedAndMerging(key, base, cand string) (string, *wire.DispatchCommand, *wire.DispatchCommand) { |
| 176 | r.t.Helper() |
| 177 | r.asksBeforeMerging() |
| 178 | id, _ := r.pushed(key, "Barber mockups", base, cand) |
| 179 | r.expect(wire.FrameWatchMerge) |
| 180 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 181 | r.checks(key+"-c", c, wire.ChecksPassed, exit(0), "", "ok\n") |
| 182 | rv := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| 183 | r.step(key+"-r", wire.StepReport{TaskID: rv.TaskID, StepID: rv.StepID, Attempt: rv.Attempt, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Termination: true, Family: "codex", Account: "codex-a", Verdict: wire.VerdictApprove, ReviewLabel: wire.ReviewOtherFamily, Findings: []wire.Finding{}, Summary: "Read it.", HasPreview: true, Change: facts(rv)}) |
| 184 | pv := r.previewDispatch(cand) |
| 185 | if pv == nil { |
| 186 | r.t.Fatal("no preview dispatched") |
| 187 | } |
| 188 | var card wire.Card |
| 189 | r.wait("merge card", func(c *Conversation) bool { |
| 190 | for _, k := range c.Cards { |
| 191 | if k.Kind == wire.CardMerge && k.TaskID == id && k.State == wire.CardOpen { |
| 192 | card = k |
| 193 | } |
| 194 | } |
| 195 | return card.ID != "" |
| 196 | }) |
| 197 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 198 | r.t.Fatal(err) |
| 199 | } |
| 200 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 201 | // Its export was built, then publishing it failed. |
| 202 | r.built(key+"-pv-built", pv, digestA) |
| 203 | r.step(key+"-pv-fail", wire.StepReport{TaskID: id, StepID: pv.StepID, Attempt: 1, State: wire.StepFailed, Reason: "preview_failed", BaseSHA: base, CandidateSHA: cand, Termination: true, Findings: []wire.Finding{}}) |
| 204 | return id, pv, m |
| 205 | } |
| 206 | |
| 207 | // TestContractRetryFailedPreviewAfterMerge (stuck tasks d): a preview that |
| 208 | // failed while the merge went ahead can be run again by its person from |
| 209 | // the task page: a new attempt of that preview for the current candidate, |
| 210 | // recomputed (its export reported afresh) and published only through the |
| 211 | // preview's own guards, the merge and the task's stage untouched. Retry |
| 212 | // with no step still means the newest step, which is the merge. |
| 213 | func TestContractRetryFailedPreviewAfterMerge(t *testing.T) { |
| 214 | r := publicPreviewRig(t) |
| 215 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 216 | id, pv, m := r.previewedAndMerging("v", base, cand) |
| 217 | if _, err := r.client.RetryTask(r.ctx, id); Status(err) != 409 { |
| 218 | t.Fatalf("Retry with the merge queued: %v", err) |
| 219 | } |
| 220 | form := `<input type="hidden" name="step" value="` + pv.StepID + `">` |
| 221 | page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| 222 | if !strings.Contains(page, form) || !strings.Contains(page, "data-step-retry") { |
| 223 | t.Fatal("the failed preview offers no Retry") |
| 224 | } |
| 225 | if p := r.get(r.memberCookie(), "/tasks/"+id, 200); strings.Contains(p, "data-step-retry") { |
| 226 | t.Fatal("a teammate is offered the preview's Retry") |
| 227 | } |
| 228 | before, _ := r.client.Task(r.ctx, id) |
| 229 | if status, at := r.post(r.inst.Cookie(), "/conversation/tasks/"+id+"/retry", url.Values{"from": {"task"}, "step": {pv.StepID}}); status != http.StatusSeeOther || at != "/tasks/"+id { |
| 230 | t.Fatalf("Retry the preview: %d %s", status, at) |
| 231 | } |
| 232 | again := r.previewDispatch(cand) |
| 233 | if again == nil || again.StepID != pv.StepID || again.Attempt != 2 || again.CandidateSHA != cand { |
| 234 | t.Fatalf("the preview again %+v", again) |
| 235 | } |
| 236 | k, _ := r.client.Task(r.ctx, id) |
| 237 | if k.Stage != before.Stage || k.Stage != wire.StageMerging { |
| 238 | t.Fatalf("the preview moved the task: %s to %s", before.Stage, k.Stage) |
| 239 | } |
| 240 | for _, s := range k.Steps { |
| 241 | if s.ID == m.StepID && (s.Attempt != 1 || s.State != wire.StepQueued && s.State != wire.StepRunning) { |
| 242 | t.Fatalf("the merge was touched %+v", s) |
| 243 | } |
| 244 | if s.ID == pv.StepID && (s.State != wire.StepQueued || s.Preview != nil) { |
| 245 | t.Fatalf("the preview kept its old export %+v %+v", s, s.Preview) |
| 246 | } |
| 247 | } |
| 248 | // Recomputed: nothing publishes until this attempt's export is built. |
| 249 | if refused := r.lease(r.runner, "v-start", wire.LeaseCheck{StepID: again.StepID, Attempt: 2}); refused != "" { |
| 250 | t.Fatalf("start refused %q", refused) |
| 251 | } |
| 252 | if refused := r.lease(r.runner, "v-early", wire.LeaseCheck{StepID: again.StepID, Attempt: 2, Purpose: wire.LeasePurposePreview, Digest: digestA}); refused != wire.RefusedPreviewNotBuilt { |
| 253 | t.Fatalf("publish before built: %q", refused) |
| 254 | } |
| 255 | r.built("v-built", again, digestB) |
| 256 | if refused := r.lease(r.runner, "v-pub", wire.LeaseCheck{StepID: again.StepID, Attempt: 2, Purpose: wire.LeasePurposePreview, Digest: digestB}); refused != "" { |
| 257 | t.Fatalf("publish refused %q", refused) |
| 258 | } |
| 259 | // Asked again while it runs: nothing more. |
| 260 | r.post(r.inst.Cookie(), "/conversation/tasks/"+id+"/retry", url.Values{"from": {"task"}, "step": {pv.StepID}}) |
| 261 | if extra := r.previewDispatch(cand); extra != nil { |
| 262 | t.Fatalf("a second preview attempt %+v", extra) |
| 263 | } |
| 264 | if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); strings.Contains(page, "data-step-retry") { |
| 265 | t.Fatal("Retry still offered on a running preview") |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // TestContractRetryPreviewOnlyForTheCandidate: a failed preview of a |
| 270 | // candidate the branch moved away from is not run again. |
| 271 | func TestContractRetryPreviewOnlyForTheCandidate(t *testing.T) { |
| 272 | r := publicPreviewRig(t) |
| 273 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 274 | id, pv, _ := r.previewedAndMerging("w", base, cand) |
| 275 | if err := r.runner.Send(r.ctx, "w-away", wire.FrameBranch, wire.BranchReport{TaskID: id, CandidateSHA: cand, Head: strings.Repeat("8", 40)}); err != nil { |
| 276 | t.Fatal(err) |
| 277 | } |
| 278 | r.wait("moved", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsBranchMoved }) |
| 279 | r.post(r.inst.Cookie(), "/conversation/tasks/"+id+"/retry", url.Values{"from": {"task"}, "step": {pv.StepID}}) |
| 280 | if again := r.previewDispatch(cand); again != nil { |
| 281 | t.Fatalf("a preview of a candidate the branch left %+v", again) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // TestContractCoordinatorRetryStep (stuck tasks b): retry_step runs a |
| 286 | // failed preview, checks or review again with exactly the person's Retry |
| 287 | // (the creator's task, an owner, ended and failed, the newest step), as |
| 288 | // the next attempt, and the task page says the coordinator started it. |
| 289 | // Never a merge, a publish or the work itself: those are refused and |
| 290 | // nothing is dispatched. |
| 291 | func TestContractCoordinatorRetryStep(t *testing.T) { |
| 292 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 293 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 294 | id, _ := r.pushed("b", "Fix the README typo", base, cand) |
| 295 | r.expect(wire.FrameWatchMerge) |
| 296 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 297 | if res := r.call("b0", wire.ToolRetryStep, map[string]string{"task": id, "step": wire.SpecialistChecks}); !res.IsError || res.Code != "not_retryable" { |
| 298 | t.Fatalf("retry of queued checks %+v", res) |
| 299 | } |
| 300 | r.step("b-c-fail", wire.StepReport{TaskID: id, StepID: c.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", BaseSHA: base, CandidateSHA: cand, Termination: true, Findings: []wire.Finding{}}) |
| 301 | for _, kind := range []string{wire.SpecialistMerge, wire.SpecialistPublish, wire.SpecialistEngineering, "anything"} { |
| 302 | if res := r.call("b-"+kind, wire.ToolRetryStep, map[string]string{"task": id, "step": kind}); !res.IsError || res.Code != "invalid" { |
| 303 | t.Fatalf("retry_step %s: %+v", kind, res) |
| 304 | } |
| 305 | } |
| 306 | if refused := r.lease(r.runner, "b-ping", wire.LeaseCheck{}); refused != "" || r.heldKinds(wire.FrameDispatch) != 0 { |
| 307 | t.Fatal("a refused retry dispatched something") |
| 308 | } |
| 309 | res := r.call("b1", wire.ToolRetryStep, map[string]string{"task": id, "step": wire.SpecialistChecks}) |
| 310 | if res.IsError || res.Code != "retrying" { |
| 311 | t.Fatalf("retry_step %+v", res) |
| 312 | } |
| 313 | _, body := r.expect(wire.FrameDispatch) |
| 314 | if d := body.(*wire.DispatchCommand); d.Specialist != wire.SpecialistChecks || d.StepID != c.StepID || d.Attempt != 2 || d.CandidateSHA != cand { |
| 315 | t.Fatalf("dispatch %+v", d) |
| 316 | } |
| 317 | k, _ := r.client.Task(r.ctx, id) |
| 318 | if s := k.Steps[len(k.Steps)-1]; s.ID != c.StepID || s.Attempt != 2 || s.RetriedBy != wire.RetriedByCoordinator { |
| 319 | t.Fatalf("the step %+v", s) |
| 320 | } |
| 321 | if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); !strings.Contains(page, "The coordinator started Checks again.") { |
| 322 | t.Fatal("the timeline does not say the coordinator started it") |
| 323 | } |
| 324 | if r.audits("task.retry") != 1 { |
| 325 | t.Fatalf("audits %d", r.audits("task.retry")) |
| 326 | } |
| 327 | // Once per turn: asked again with a fresh call while it runs, and |
| 328 | // again once that attempt failed and ended, the ledger answers with |
| 329 | // the first result and nothing more runs. |
| 330 | if again := r.call("b2", wire.ToolRetryStep, map[string]string{"task": id, "step": wire.SpecialistChecks}); again.IsError || again.Code != "retrying" || !again.Replayed { |
| 331 | t.Fatalf("second retry_step %+v", again) |
| 332 | } |
| 333 | if same := r.call("b1", wire.ToolRetryStep, map[string]string{"task": id, "step": wire.SpecialistChecks}); same != res { |
| 334 | t.Fatalf("replay %+v", same) |
| 335 | } |
| 336 | if refused := r.lease(r.runner, "b-ping2", wire.LeaseCheck{}); refused != "" || r.heldKinds(wire.FrameDispatch) != 0 { |
| 337 | t.Fatal("a second dispatch") |
| 338 | } |
| 339 | r.step("b-c2-fail", wire.StepReport{TaskID: id, StepID: c.StepID, Attempt: 2, State: wire.StepFailed, Reason: "provider_failed", BaseSHA: base, CandidateSHA: cand, Termination: true, Findings: []wire.Finding{}}) |
| 340 | if again := r.call("b3", wire.ToolRetryStep, map[string]string{"task": id, "step": wire.SpecialistChecks}); again.IsError || again.Code != "retrying" || !again.Replayed { |
| 341 | t.Fatalf("retry_step after the retry failed %+v", again) |
| 342 | } |
| 343 | if refused := r.lease(r.runner, "b-ping3", wire.LeaseCheck{}); refused != "" || r.heldKinds(wire.FrameDispatch) != 0 { |
| 344 | t.Fatal("a third attempt in the same turn") |
| 345 | } |
| 346 | if k, _ := r.client.Task(r.ctx, id); k.Steps[len(k.Steps)-1].Attempt != 2 || k.Steps[len(k.Steps)-1].State != wire.StepFailed || r.audits("task.retry") != 1 { |
| 347 | t.Fatalf("after the fresh call %+v, %d audits", k.Steps[len(k.Steps)-1], r.audits("task.retry")) |
| 348 | } |
| 349 | // The person's own Retry later is theirs: the page no longer credits |
| 350 | // the coordinator. |
| 351 | if acc, err := r.client.RetryTask(r.ctx, id); err != nil || acc.Attempt != 3 { |
| 352 | t.Fatalf("the person's Retry %+v %v", acc, err) |
| 353 | } |
| 354 | if k, _ := r.client.Task(r.ctx, id); k.Steps[len(k.Steps)-1].RetriedBy != "" { |
| 355 | t.Fatal("the person's attempt credited to the coordinator") |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // TestContractCoordinatorRetryNeverMerges: a failed merge is not the |
| 360 | // coordinator's to retry (offer_retry is); a failed preview behind the |
| 361 | // queued merge is, as the person's Retry would run it. |
| 362 | func TestContractCoordinatorRetryNeverMerges(t *testing.T) { |
| 363 | r := publicPreviewRig(t) |
| 364 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 365 | id, pv, m := r.previewedAndMerging("x", base, cand) |
| 366 | res := r.call("x1", wire.ToolRetryStep, map[string]string{"task": id, "step": wire.SpecialistPreview}) |
| 367 | if res.IsError || res.Code != "retrying" { |
| 368 | t.Fatalf("retry_step preview %+v", res) |
| 369 | } |
| 370 | if again := r.previewDispatch(cand); again == nil || again.StepID != pv.StepID || again.Attempt != 2 { |
| 371 | t.Fatalf("the preview again %+v", again) |
| 372 | } |
| 373 | k, _ := r.client.Task(r.ctx, id) |
| 374 | for _, s := range k.Steps { |
| 375 | if s.ID == pv.StepID && s.RetriedBy != wire.RetriedByCoordinator { |
| 376 | t.Fatalf("the preview's attempt is not the coordinator's %+v", s) |
| 377 | } |
| 378 | } |
| 379 | r.step("x-m-fail", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_failed", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 380 | r.wait("ready again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 381 | if res := r.call("x2", wire.ToolRetryStep, map[string]string{"task": id, "step": wire.SpecialistMerge}); !res.IsError || res.Code != "invalid" { |
| 382 | t.Fatalf("retry_step merge %+v", res) |
| 383 | } |
| 384 | if refused := r.lease(r.runner, "x-ping", wire.LeaseCheck{}); refused != "" || r.heldKinds(wire.FrameDispatch) != 0 { |
| 385 | t.Fatal("the coordinator's call dispatched a merge") |
| 386 | } |
| 387 | if k, _ := r.client.Task(r.ctx, id); k.Stage != wire.StageReadyToMerge || k.Merged != nil { |
| 388 | t.Fatalf("after retry_step merge %+v", k) |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // TestContractOfferRetryOncePerTurn: after the person taps Not now, a |
| 393 | // fresh offer_retry call in the same turn reopens nothing: the ledger |
| 394 | // answers with the first offer, and no card, merge or audit is added. |
| 395 | func TestContractOfferRetryOncePerTurn(t *testing.T) { |
| 396 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 397 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 398 | id := r.judgedTask("q", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 399 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 400 | r.step("q-m-fail", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_failed", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 401 | r.wait("ready again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 402 | if res := r.call("q1", wire.ToolOfferRetry, map[string]string{"task": id}); res.IsError || res.Code != "offered" { |
| 403 | t.Fatalf("offer %+v", res) |
| 404 | } |
| 405 | var card wire.Card |
| 406 | r.wait("retry card", func(c *Conversation) bool { |
| 407 | for _, k := range c.Cards { |
| 408 | if k.Kind == wire.CardMerge && k.TaskID == id && k.State == wire.CardOpen { |
| 409 | card = k |
| 410 | } |
| 411 | } |
| 412 | return card.ID != "" |
| 413 | }) |
| 414 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceNotNow, card.Revision); err != nil { |
| 415 | t.Fatal(err) |
| 416 | } |
| 417 | cards, audits := r.count("SELECT count(*) FROM chat_cards WHERE task_id = ? AND kind = ?", id, wire.CardMerge), r.audits("task.merge_retry_offered") |
| 418 | again := r.call("q2", wire.ToolOfferRetry, map[string]string{"task": id}) |
| 419 | if again.IsError || again.Code != "offered" || !again.Replayed { |
| 420 | t.Fatalf("offer after Not now %+v", again) |
| 421 | } |
| 422 | if refused := r.lease(r.runner, "q-ping", wire.LeaseCheck{}); refused != "" || r.heldKinds(wire.FrameDispatch) != 0 { |
| 423 | t.Fatal("the second offer dispatched something") |
| 424 | } |
| 425 | if n := r.count("SELECT count(*) FROM chat_cards WHERE task_id = ? AND kind = ? AND state = ?", id, wire.CardMerge, wire.CardOpen); n != 0 { |
| 426 | t.Fatalf("%d open merge cards after Not now", n) |
| 427 | } |
| 428 | if n := r.count("SELECT count(*) FROM chat_cards WHERE task_id = ? AND kind = ?", id, wire.CardMerge); n != cards || r.audits("task.merge_retry_offered") != audits { |
| 429 | t.Fatalf("cards %d (was %d), audits %d (was %d)", n, cards, r.audits("task.merge_retry_offered"), audits) |
| 430 | } |
| 431 | } |
| 432 | |