| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // Needs-you triage, thin v1 (spec 2026-11-11-equipo-needs-you-triage.md): |
| 12 | // one contract per case Paul's staging showed, and the guards. |
| 13 | |
| 14 | // started records a task and starts it, returning its id and the |
| 15 | // Engineering step's dispatch. |
| 16 | func (r *toolRig) started(key, title string) (string, *wire.DispatchCommand) { |
| 17 | r.t.Helper() |
| 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 | return id, r.nextDispatch() |
| 23 | } |
| 24 | |
| 25 | func (r *toolRig) nextDispatch() *wire.DispatchCommand { |
| 26 | r.t.Helper() |
| 27 | _, body := r.expect(wire.FrameDispatch) |
| 28 | return body.(*wire.DispatchCommand) |
| 29 | } |
| 30 | |
| 31 | // stop reports the dispatched attempt running, then failed with reason |
| 32 | // and its termination proven. |
| 33 | func (r *toolRig) stop(key string, d *wire.DispatchCommand, reason string) { |
| 34 | r.t.Helper() |
| 35 | r.step(key+"-run", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: d.BaseSHA}) |
| 36 | r.step(key+"-stop", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepFailed, Reason: reason, BaseSHA: d.BaseSHA, Termination: true}) |
| 37 | } |
| 38 | |
| 39 | // triageActions are the person's triage actions of detector. |
| 40 | func (r *toolRig) triageActions(detector string) []wire.HousekeepingAction { |
| 41 | r.t.Helper() |
| 42 | var out []wire.HousekeepingAction |
| 43 | for _, a := range r.housekeeping().Actions { |
| 44 | if a.Detector == detector { |
| 45 | out = append(out, a) |
| 46 | } |
| 47 | } |
| 48 | return out |
| 49 | } |
| 50 | |
| 51 | // commands counts the commands of kind the instance wrote for runners, |
| 52 | // sent or not: what a tick did, before any frame is read. |
| 53 | func (r *toolRig) commands(kind string) int { |
| 54 | r.t.Helper() |
| 55 | var n int |
| 56 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ?", kind).Scan(&n); err != nil { |
| 57 | r.t.Fatal(err) |
| 58 | } |
| 59 | return n |
| 60 | } |
| 61 | |
| 62 | func (r *toolRig) task(id string) wire.Task { |
| 63 | r.t.Helper() |
| 64 | k, err := r.client.Task(r.ctx, id) |
| 65 | if err != nil { |
| 66 | r.t.Fatal(err) |
| 67 | } |
| 68 | return k |
| 69 | } |
| 70 | |
| 71 | // TestTriageRetriesPassingStop (staging items 4 and 5): Engineering |
| 72 | // stopped because the runner restarted, before any push. Equipo retries |
| 73 | // it once on its own (a receipt on the Housekeeping view, an honest line |
| 74 | // on the task, nothing in Needs you); the same step stopping again stays |
| 75 | // with the person, with Equipo's recommendation. |
| 76 | func TestTriageRetriesPassingStop(t *testing.T) { |
| 77 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 78 | id, d := r.started("p", "Show project context on thinking indicators") |
| 79 | r.stop("p1", d, "runner_restarted") |
| 80 | again := r.nextDispatch() |
| 81 | if again.StepID != d.StepID || again.Attempt != 2 || again.Specialist != wire.SpecialistEngineering { |
| 82 | t.Fatalf("not retried on its own: %+v", again) |
| 83 | } |
| 84 | k := r.task(id) |
| 85 | if k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRetrying || k.Triage.Reason != "runner_restarted" { |
| 86 | t.Fatalf("while retrying %+v %+v", k.NeedsYou, k.Triage) |
| 87 | } |
| 88 | if acts := r.triageActions("triage_retry"); len(acts) != 1 || acts[0].State != wire.ActionDone || acts[0].Subject.ID != id || acts[0].Undoable { |
| 89 | t.Fatalf("receipt %+v", acts) |
| 90 | } |
| 91 | if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); !strings.Contains(page, "Equipo is trying the step again because the runner restarted.") { |
| 92 | t.Fatalf("task page: %s", page) |
| 93 | } |
| 94 | if needs, _ := r.client.NeedsYou(r.ctx); len(needs.Items) != 0 { |
| 95 | t.Fatalf("in Needs you while retrying: %+v", needs.Items) |
| 96 | } |
| 97 | // The retried attempt stops the same way: once per step, so it waits. |
| 98 | r.stop("p2", again, "runner_restarted") |
| 99 | k = r.task(id) |
| 100 | if k.NeedsYou != wire.NeedsRetry || k.Steps[0].Attempt != 2 || k.Triage == nil || k.Triage.Kind != wire.TriageAsking || k.Triage.Why != wire.TriageWhyTried || k.Triage.Recommend != wire.RecommendRetry { |
| 101 | t.Fatalf("second stop %+v %+v", k.Steps[0], k.Triage) |
| 102 | } |
| 103 | if len(r.triageActions("triage_retry")) != 1 || r.heldKinds(wire.FrameDispatch) != 0 { |
| 104 | t.Fatal("retried twice") |
| 105 | } |
| 106 | if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); !strings.Contains(page, "Equipo already tried this once on its own. Equipo recommends trying this step again.") { |
| 107 | t.Fatalf("recommendation: %s", page) |
| 108 | } |
| 109 | // A stop that is not a passing one is never retried on its own. |
| 110 | id2, d2 := r.started("q", "Add Done to recheck granted access") |
| 111 | r.stop("q1", d2, "provider_failed") |
| 112 | if k := r.task(id2); k.NeedsYou != wire.NeedsRetry || k.Steps[0].Attempt != 1 || k.Triage == nil || k.Triage.Recommend != wire.RecommendRetry || k.Triage.Why != "" { |
| 113 | t.Fatalf("non-passing stop %+v %+v", k.Steps[0], k.Triage) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // TestTriageNeverRetriesMerge: a merge step is never retried on its own, |
| 118 | // whatever its reason, even when Needs you reads retry. |
| 119 | func TestTriageNeverRetriesMerge(t *testing.T) { |
| 120 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 121 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 122 | id := r.judgedTask("m", "Fix the README typo", base, cand, wire.ChecksPassed, "") |
| 123 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 124 | r.step("m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 125 | r.step("m-stop", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "interrupted", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 126 | // Even with the task where Needs you reads retry (a stage the merge |
| 127 | // left behind), and a sync to look again: no merge attempt. |
| 128 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET stage = ? WHERE id = ?", wire.StageInReview, id); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | r.syncNow("m-sync") |
| 132 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | k := r.task(id) |
| 136 | last := k.Steps[len(k.Steps)-1] |
| 137 | if last.Specialist != wire.SpecialistMerge || last.Attempt != 1 || last.State != wire.StepFailed || r.heldKinds(wire.FrameDispatch) != 0 { |
| 138 | t.Fatalf("merge retried: %+v", last) |
| 139 | } |
| 140 | if acts := r.triageActions("triage_retry"); len(acts) != 0 { |
| 141 | t.Fatalf("a triage row for a merge: %+v", acts) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // conflictAgain takes a rebase round to its new candidate on tip, checks |
| 146 | // and Review pass it, and the merge conflicts again. |
| 147 | func (r *toolRig) conflictAgain(key string, rb *wire.DispatchCommand, tip, cand string) { |
| 148 | r.t.Helper() |
| 149 | r.step(key+"-rb-run", wire.StepReport{TaskID: rb.TaskID, StepID: rb.StepID, Attempt: rb.Attempt, State: wire.StepRunning, BaseSHA: tip}) |
| 150 | r.step(key+"-rb-done", wire.StepReport{TaskID: rb.TaskID, StepID: rb.StepID, Attempt: rb.Attempt, State: wire.StepDone, BaseSHA: tip, CandidateSHA: cand, Pushed: true, Termination: true}) |
| 151 | r.expect(wire.FrameWatchMerge) |
| 152 | c := r.dispatched(wire.SpecialistChecks, tip, cand, 0) |
| 153 | r.checks(key+"-c", c, wire.ChecksPassed, exit(0), "", "ok\n") |
| 154 | rv := r.dispatched(wire.SpecialistReview, tip, cand, 0) |
| 155 | r.verdict(key+"-r", rv, wire.VerdictApprove) |
| 156 | m := r.dispatched(wire.SpecialistMerge, tip, cand, 0) |
| 157 | r.step(key+"-m-run", wire.StepReport{TaskID: rb.TaskID, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: tip, CandidateSHA: cand}) |
| 158 | r.step(key+"-m-conflict", wire.StepReport{TaskID: rb.TaskID, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_conflict", BaseSHA: tip, CandidateSHA: cand, Termination: true}) |
| 159 | } |
| 160 | |
| 161 | // TestTriageRebasesConflict (staging item 2): the first conflict's rebase |
| 162 | // round is the merge's own; after it, Equipo rebases each conflicting |
| 163 | // candidate once more on its own, at most twice a task, with the gate |
| 164 | // deciding every merge as usual. Then the person decides: Try another |
| 165 | // round rebases again. |
| 166 | func TestTriageRebasesConflict(t *testing.T) { |
| 167 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 168 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 169 | id := r.judgedTask("c", "Centre the star on the task timeline", base, cand, wire.ChecksPassed, "") |
| 170 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 171 | r.step("c-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 172 | r.step("c-m-conflict", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_conflict", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 173 | rb := r.nextDispatch() |
| 174 | if !rb.Rebase || rb.CandidateSHA != cand { |
| 175 | t.Fatalf("first rebase %+v", rb) |
| 176 | } |
| 177 | prev := cand |
| 178 | for i, sha := range []string{"3", "4"} { |
| 179 | tip, next := strings.Repeat("a"+sha, 20), strings.Repeat(sha, 40) |
| 180 | r.conflictAgain("c"+sha, rb, tip, next) |
| 181 | rb = r.nextDispatch() |
| 182 | if rb.Specialist != wire.SpecialistEngineering || !rb.Rebase || rb.CandidateSHA != next || rb.CandidateSHA == prev { |
| 183 | t.Fatalf("automatic rebase %d: %+v", i+1, rb) |
| 184 | } |
| 185 | if k := r.task(id); k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRebasing { |
| 186 | t.Fatalf("while rebasing %+v %+v", k.NeedsYou, k.Triage) |
| 187 | } |
| 188 | prev = next |
| 189 | } |
| 190 | // A restart's re-triage gives no fresh allowance. |
| 191 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | r.conflictAgain("c5", rb, strings.Repeat("a5", 20), strings.Repeat("5", 40)) |
| 195 | r.wait("needs you", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsMergeConflict }) |
| 196 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | k := r.task(id) |
| 200 | if k.Triage == nil || k.Triage.Why != wire.TriageWhyTried || k.Triage.Recommend != wire.RecommendRebase || r.heldKinds(wire.FrameDispatch) != 0 { |
| 201 | t.Fatalf("after two automatic rebases %+v", k.Triage) |
| 202 | } |
| 203 | if acts := r.triageActions("triage_rebase"); len(acts) != 2 { |
| 204 | t.Fatalf("rebase rows %+v", acts) |
| 205 | } |
| 206 | if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); !strings.Contains(page, "data-task-round") || !strings.Contains(page, `href="/conversation?about=`+id+`" data-ask-coordinator`) { |
| 207 | t.Fatalf("decision: %s", page) |
| 208 | } |
| 209 | if _, err := r.client.RoundTask(r.ctx, id); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | if again := r.nextDispatch(); !again.Rebase || again.CandidateSHA != strings.Repeat("5", 40) { |
| 213 | t.Fatalf("person's rebase %+v", again) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // TestTriageRechecksStaleChecks (staging item 6): checks that could not |
| 218 | // run (needs_network) before the project's first hosted CI verdict run |
| 219 | // again on the exact candidate, offered hosted CI. The SHA the card asked |
| 220 | // about stays asked; the hosted pass rewords the card; nothing merges. |
| 221 | func TestTriageRechecksStaleChecks(t *testing.T) { |
| 222 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 223 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 224 | id := r.judgedTask("s", "Plan private repository storage", base, cand, wire.ChecksCannotRun, "needs_network") |
| 225 | card := r.mergeCardsWait(id) |
| 226 | // No hosted verdict in the project yet: not a hosted-CI project. |
| 227 | r.syncNow("s-sync0") |
| 228 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 229 | t.Fatal(err) |
| 230 | } |
| 231 | if k := r.task(id); len(k.Steps) != 3 || k.NeedsYou != wire.NeedsMerge { |
| 232 | t.Fatalf("re-checked before hosted CI existed: %+v", k.Steps) |
| 233 | } |
| 234 | // Another task of the project gets hosted CI's verdict. |
| 235 | time.Sleep(10 * time.Millisecond) |
| 236 | other, _ := r.pushed("o", "Tidy the footer", base, strings.Repeat("3", 40)) |
| 237 | ot := r.task(other) |
| 238 | r.expect(wire.FrameWatchMerge) |
| 239 | oc := r.dispatched(wire.SpecialistChecks, base, strings.Repeat("3", 40), 0) |
| 240 | r.step("o-c", hostedReport(oc, ot.Branch, wire.HostedSuccess)) |
| 241 | orv := r.dispatched(wire.SpecialistReview, base, strings.Repeat("3", 40), 0) |
| 242 | r.verdict("o-r", orv, wire.VerdictChanges) |
| 243 | r.dispatched(wire.SpecialistEngineering, strings.Repeat("3", 40), "", 1) |
| 244 | if _, err := r.client.CancelTask(r.ctx, other); err != nil { |
| 245 | t.Fatal(err) |
| 246 | } |
| 247 | r.syncNow("s-sync1") |
| 248 | c := r.nextDispatch() |
| 249 | if c.TaskID != id || c.Specialist != wire.SpecialistChecks || c.CandidateSHA != cand || !c.HostedChecks { |
| 250 | t.Fatalf("re-check %+v", c) |
| 251 | } |
| 252 | if k := r.task(id); k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRechecking { |
| 253 | t.Fatalf("while re-checking %+v %+v", k.NeedsYou, k.Triage) |
| 254 | } |
| 255 | task := r.task(id) |
| 256 | r.step("s-c2", hostedReport(c, task.Branch, wire.HostedSuccess)) |
| 257 | r.wait("card reworded", func(cv *Conversation) bool { |
| 258 | for _, k := range cv.Cards { |
| 259 | if k.ID == card.ID && strings.Contains(k.Question, "its hosted checks passed, but I need to verify those runs before I can merge it on my own") { |
| 260 | return true |
| 261 | } |
| 262 | } |
| 263 | return false |
| 264 | }) |
| 265 | k := r.task(id) |
| 266 | if k.Checks != wire.ChecksPassed || k.Stage != wire.StageReadyToMerge || k.NeedsYou != wire.NeedsMerge { |
| 267 | t.Fatalf("after the hosted pass %+v", k) |
| 268 | } |
| 269 | for _, h := range r.held { |
| 270 | if d, ok := h.body.(*wire.DispatchCommand); ok && d.Specialist == wire.SpecialistMerge && d.TaskID == id { |
| 271 | t.Fatal("merged on its own") |
| 272 | } |
| 273 | } |
| 274 | if len(r.triageActions("triage_recheck")) != 1 { |
| 275 | t.Fatal("re-check receipt") |
| 276 | } |
| 277 | // Once per SHA, whatever happens next. |
| 278 | r.syncNow("s-sync2") |
| 279 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 280 | t.Fatal(err) |
| 281 | } |
| 282 | if len(r.triageActions("triage_recheck")) != 1 || r.heldKinds(wire.FrameDispatch) != 0 { |
| 283 | t.Fatal("re-checked twice") |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // TestTriageAsksWithRecommendation (staging items 1 and 3): rounds used up |
| 288 | // stay in Needs you as a plain question, with hosted CI's failing steps |
| 289 | // or Review's latest finding and Equipo's recommendation; Try another |
| 290 | // round sends Engineering once more with the last feedback. |
| 291 | func TestTriageAsksWithRecommendation(t *testing.T) { |
| 292 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 293 | base := strings.Repeat("1", 40) |
| 294 | shas := []string{strings.Repeat("2", 40), strings.Repeat("3", 40), strings.Repeat("4", 40)} |
| 295 | id, _ := r.pushed("f", "Add project rename control", base, shas[0]) |
| 296 | task := r.task(id) |
| 297 | r.expect(wire.FrameWatchMerge) |
| 298 | for round, sha := range shas { |
| 299 | c := r.dispatched(wire.SpecialistChecks, base, sha, round) |
| 300 | r.step("f-c"+itoa(round), hostedReport(c, task.Branch, wire.HostedFailure, "compile", "check shard 5 of 23")) |
| 301 | if round == len(shas)-1 { |
| 302 | break |
| 303 | } |
| 304 | fix := r.dispatched(wire.SpecialistEngineering, sha, "", round+1) |
| 305 | r.step("f-e"+itoa(round), wire.StepReport{TaskID: id, StepID: fix.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: sha, CandidateSHA: shas[round+1], Pushed: true, Termination: true}) |
| 306 | r.expect(wire.FrameWatchMerge) |
| 307 | } |
| 308 | r.wait("needs you", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsFixRounds }) |
| 309 | k := r.task(id) |
| 310 | if k.Triage == nil || k.Triage.Kind != wire.TriageAsking || k.Triage.Recommend != wire.RecommendRound || strings.Join(k.Triage.Failed, "; ") != "compile; check shard 5 of 23" { |
| 311 | t.Fatalf("recommendation %+v", k.Triage) |
| 312 | } |
| 313 | page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| 314 | for _, want := range []string{"Hosted checks failed: compile; check shard 5 of 23.", "Equipo recommends another round.", "data-task-round", "data-ask-coordinator"} { |
| 315 | if !strings.Contains(page, want) { |
| 316 | t.Fatalf("task page lacks %q: %s", want, page) |
| 317 | } |
| 318 | } |
| 319 | if needs, _ := r.client.NeedsYou(r.ctx); len(needs.Items) != 1 || needs.Items[0].ID != id { |
| 320 | t.Fatalf("Needs you %+v", needs.Items) |
| 321 | } |
| 322 | // Try another round: round 3, the person's, with the last feedback. |
| 323 | if _, err := r.client.RoundTask(r.ctx, id); err != nil { |
| 324 | t.Fatal(err) |
| 325 | } |
| 326 | fix := r.dispatched(wire.SpecialistEngineering, shas[2], "", 3) |
| 327 | if !strings.Contains(fix.Feedback, "Round 3, which the person asked for") || !strings.Contains(fix.Feedback, "\n=== compile\n") || !strings.Contains(fix.Feedback, `CI failed job: "check shard 5 of 23"`) { |
| 328 | t.Fatalf("round 3 feedback %q", fix.Feedback) |
| 329 | } |
| 330 | // It passes the checks, and Review still asks for changes: its latest |
| 331 | // finding is the word on the task. |
| 332 | cand := strings.Repeat("5", 40) |
| 333 | r.step("f-e3", wire.StepReport{TaskID: id, StepID: fix.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: shas[2], CandidateSHA: cand, Pushed: true, Termination: true}) |
| 334 | r.expect(wire.FrameWatchMerge) |
| 335 | c := r.dispatched(wire.SpecialistChecks, base, cand, 3) |
| 336 | r.step("f-c3", hostedReport(c, task.Branch, wire.HostedSuccess)) |
| 337 | rv := r.dispatched(wire.SpecialistReview, base, cand, 3) |
| 338 | r.verdict("f-r3", rv, wire.VerdictChanges, wire.Finding{Headline: "The rename field loses focus after saving.", Text: "Focus is lost.", Path: "web/rename.js", Line: 4}) |
| 339 | r.wait("needs you again", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsFixRounds }) |
| 340 | if k := r.task(id); k.Triage == nil || k.Triage.Said != "The rename field loses focus after saving." || len(k.Triage.Failed) != 0 { |
| 341 | t.Fatalf("Review's word %+v", k.Triage) |
| 342 | } |
| 343 | if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); !strings.Contains(page, "Review's latest finding: The rename field loses focus after saving.") { |
| 344 | t.Fatalf("task page: %s", page) |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // TestTriageBudget: a person's fourth automatic retry of a day waits in |
| 349 | // Needs you, saying today's tries are used, with the recommendation; the |
| 350 | // next day it is claimed again while the task is still there; a person's |
| 351 | // Retry or Cancel meanwhile makes a held one stale for good. |
| 352 | func TestTriageBudget(t *testing.T) { |
| 353 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 354 | now := time.Now().UTC().Truncate(24 * time.Hour).Add(12 * time.Hour) |
| 355 | r.clock(now) |
| 356 | for i := range 3 { |
| 357 | _, d := r.started("b"+itoa(i), "Task number "+itoa(i)) |
| 358 | r.stop("b"+itoa(i), d, "runner_restarted") |
| 359 | if again := r.nextDispatch(); again.Attempt != 2 { |
| 360 | t.Fatalf("retry %d: %+v", i, again) |
| 361 | } |
| 362 | } |
| 363 | held, d := r.started("h", "The held one") |
| 364 | r.stop("h", d, "runner_restarted") |
| 365 | k := r.task(held) |
| 366 | if k.NeedsYou != wire.NeedsRetry || k.Steps[0].Attempt != 1 || k.Triage == nil || k.Triage.Why != wire.TriageWhyBudget || k.Triage.Recommend != wire.RecommendRetry { |
| 367 | t.Fatalf("over budget %+v %+v", k.Steps[0], k.Triage) |
| 368 | } |
| 369 | if page := r.get(r.inst.Cookie(), "/tasks/"+held, 200); !strings.Contains(page, "Equipo has used all its automatic tries for today. Equipo recommends trying this step again.") { |
| 370 | t.Fatalf("task page: %s", page) |
| 371 | } |
| 372 | // Two more held: the person retries one and cancels the other. |
| 373 | mine, dm := r.started("y", "The one the person retries") |
| 374 | r.stop("y", dm, "runner_restarted") |
| 375 | gone, dg := r.started("g", "The one the person cancels") |
| 376 | r.stop("g", dg, "runner_restarted") |
| 377 | if _, err := r.client.RetryTask(r.ctx, mine); err != nil { |
| 378 | t.Fatal(err) |
| 379 | } |
| 380 | mineAgain := r.nextDispatch() |
| 381 | if _, err := r.client.CancelTask(r.ctx, gone); err != nil { |
| 382 | t.Fatal(err) |
| 383 | } |
| 384 | // Still today: the tick claims nothing. |
| 385 | r.tick() |
| 386 | if k := r.task(held); k.Steps[0].Attempt != 1 { |
| 387 | t.Fatalf("claimed over budget %+v", k.Steps[0]) |
| 388 | } |
| 389 | // Tomorrow: the held row is claimed; the stale ones never act. |
| 390 | r.clock(now.Add(24 * time.Hour)) |
| 391 | r.tick() |
| 392 | again := r.nextDispatch() |
| 393 | if again.TaskID != held || again.Attempt != 2 { |
| 394 | t.Fatalf("next day %+v", again) |
| 395 | } |
| 396 | if k := r.task(mine); k.Steps[0].Attempt != mineAgain.Attempt || mineAgain.Attempt != 2 { |
| 397 | t.Fatalf("person's retry retried again %+v", k.Steps[0]) |
| 398 | } |
| 399 | if k := r.task(gone); k.Stage != wire.StageCancelled || k.Steps[0].Attempt != 1 { |
| 400 | t.Fatalf("cancelled task touched %+v", k) |
| 401 | } |
| 402 | if r.heldKinds(wire.FrameDispatch) != 0 { |
| 403 | t.Fatal("a stale row acted") |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // TestTriageRetriageIdempotent: start-up re-triage acts once per |
| 408 | // situation, however often it and syncs run. A stop left from before |
| 409 | // triage (the person had paused housekeeping) is retried then, once. |
| 410 | func TestTriageRetriageIdempotent(t *testing.T) { |
| 411 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 412 | if _, err := r.client.SetHousekeeping(r.ctx, true, r.housekeeping().Revision); err != nil { |
| 413 | t.Fatal(err) |
| 414 | } |
| 415 | id, d := r.started("i", "Left from before") |
| 416 | r.stop("i", d, "runner_restarted") |
| 417 | if k := r.task(id); k.Steps[0].Attempt != 1 || k.NeedsYou != wire.NeedsRetry { |
| 418 | t.Fatalf("acted while paused %+v", k.Steps[0]) |
| 419 | } |
| 420 | if _, err := r.client.SetHousekeeping(r.ctx, false, r.housekeeping().Revision); err != nil { |
| 421 | t.Fatal(err) |
| 422 | } |
| 423 | for range 2 { |
| 424 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 425 | t.Fatal(err) |
| 426 | } |
| 427 | } |
| 428 | if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { |
| 429 | t.Fatalf("re-triage %+v", again) |
| 430 | } |
| 431 | r.syncNow("i-sync") |
| 432 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 433 | t.Fatal(err) |
| 434 | } |
| 435 | if k := r.task(id); k.Steps[0].Attempt != 2 || len(r.triageActions("triage_retry")) != 1 || r.heldKinds(wire.FrameDispatch) != 0 { |
| 436 | t.Fatalf("acted twice %+v", k.Steps[0]) |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // TestTriageCancelledUntouched: a cancelled task is never touched. |
| 441 | func TestTriageCancelledUntouched(t *testing.T) { |
| 442 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 443 | cancelled, d := r.started("x", "Cancelled after it stopped") |
| 444 | r.stop("x", d, "provider_failed") |
| 445 | if _, err := r.client.CancelTask(r.ctx, cancelled); err != nil { |
| 446 | t.Fatal(err) |
| 447 | } |
| 448 | // Its stop reads as a passing one now; it is still cancelled. |
| 449 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET reason = 'runner_restarted' WHERE task_id = ?", cancelled); err != nil { |
| 450 | t.Fatal(err) |
| 451 | } |
| 452 | for range 2 { |
| 453 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 454 | t.Fatal(err) |
| 455 | } |
| 456 | } |
| 457 | r.syncNow("x-sync") |
| 458 | if k := r.task(cancelled); k.Stage != wire.StageCancelled || k.Steps[0].Attempt != 1 { |
| 459 | t.Fatalf("cancelled task touched %+v", k) |
| 460 | } |
| 461 | if len(r.triageActions("triage_retry")) != 0 || r.heldKinds(wire.FrameDispatch) != 0 { |
| 462 | t.Fatal("acted") |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // TestTriageHeldStillRetries (stopped-jobs audit, example 2): the |
| 467 | // coordinator held the merge because the person wants to try the change |
| 468 | // first, then Engineering ran out of time. The hold stops a merge, not |
| 469 | // the retry: Equipo tries the step again once, and the hold stays. |
| 470 | func TestTriageHeldStillRetries(t *testing.T) { |
| 471 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 472 | held, dh := r.started("h", "Held by the coordinator") |
| 473 | if res := r.call("h-hold", wire.ToolHoldMerge, map[string]string{"task": held, "reason": "Paul wants to look first."}); res.IsError { |
| 474 | t.Fatalf("hold %+v", res) |
| 475 | } |
| 476 | r.stop("h", dh, "step_timeout") |
| 477 | again := r.nextDispatch() |
| 478 | if again.TaskID != held || again.StepID != dh.StepID || again.Attempt != 2 { |
| 479 | t.Fatalf("held task not retried: %+v", again) |
| 480 | } |
| 481 | if k := r.task(held); k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRetrying || k.Triage.Reason != "step_timeout" { |
| 482 | t.Fatalf("while retrying %+v %+v", k.NeedsYou, k.Triage) |
| 483 | } |
| 484 | if hold := r.task(held).MergeHold; hold != "Paul wants to look first." { |
| 485 | t.Fatalf("hold lost: %q", hold) |
| 486 | } |
| 487 | if len(r.triageActions("triage_retry")) != 1 { |
| 488 | t.Fatal("not one retry") |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // TestTriageStaleRebase: a rebase held for today's budget is bound to the |
| 493 | // merge attempt that raised it. The person's own merge Retry on the same |
| 494 | // SHA conflicting again is a new situation: the held row never acts. |
| 495 | func TestTriageStaleRebase(t *testing.T) { |
| 496 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 497 | now := time.Now().UTC().Truncate(24 * time.Hour).Add(12 * time.Hour) |
| 498 | r.clock(now) |
| 499 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 500 | id := r.judgedTask("z", "Centre the star", base, cand, wire.ChecksPassed, "") |
| 501 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 502 | r.step("z-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 503 | r.step("z-m-conflict", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "merge_conflict", BaseSHA: base, CandidateSHA: cand, Termination: true}) |
| 504 | rb := r.nextDispatch() |
| 505 | // Today's automatic rounds are used. |
| 506 | db := r.inst.DB.Writer() |
| 507 | if _, err := db.Exec(`INSERT INTO chat_housekeeping (id, team, person, detector, subject_kind, subject_id, finding_key, class, state, claimed_at, reserved_rounds, charged_rounds, at, revision) SELECT 'hk_spent', id, ?, 'triage_rebase', 'task', 'tkelsewhere', 'rebase:tkelsewhere:x', 'triage', 'done', ?, 3, 3, ?, 1 FROM teams LIMIT 1`, Owner, now, now); err != nil { |
| 508 | t.Fatal(err) |
| 509 | } |
| 510 | tip, cand2 := strings.Repeat("a3", 20), strings.Repeat("3", 40) |
| 511 | r.conflictAgain("z2", rb, tip, cand2) |
| 512 | r.wait("needs you", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsMergeConflict }) |
| 513 | if k := r.task(id); k.Triage == nil || k.Triage.Why != wire.TriageWhyBudget || k.Triage.Recommend != wire.RecommendRebase { |
| 514 | t.Fatalf("held %+v", k.Triage) |
| 515 | } |
| 516 | // The person merges the same SHA again; it conflicts again. |
| 517 | if _, err := r.client.RetryTask(r.ctx, id); err != nil { |
| 518 | t.Fatal(err) |
| 519 | } |
| 520 | m2 := r.nextDispatch() |
| 521 | if m2.Specialist != wire.SpecialistMerge || m2.Attempt != 2 { |
| 522 | t.Fatalf("person's merge %+v", m2) |
| 523 | } |
| 524 | r.step("z-m2-run", wire.StepReport{TaskID: id, StepID: m2.StepID, Attempt: 2, State: wire.StepRunning, BaseSHA: tip, CandidateSHA: cand2}) |
| 525 | r.step("z-m2-conflict", wire.StepReport{TaskID: id, StepID: m2.StepID, Attempt: 2, State: wire.StepFailed, Reason: "merge_conflict", BaseSHA: tip, CandidateSHA: cand2, Termination: true}) |
| 526 | // Tomorrow there is room, but the held row's merge attempt is gone. |
| 527 | r.clock(now.Add(24 * time.Hour)) |
| 528 | r.tick() |
| 529 | if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { |
| 530 | t.Fatal(err) |
| 531 | } |
| 532 | if k := r.task(id); k.NeedsYou != wire.NeedsMergeConflict || r.heldKinds(wire.FrameDispatch) != 0 || len(r.triageActions("triage_rebase")) != 1 { |
| 533 | t.Fatalf("a stale rebase acted: %+v", k.Steps[len(k.Steps)-1]) |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // TestTriageSweepsStalled (Paul: "does it need a loop ... to make sure it |
| 538 | // actually carries through tasks to completion?"): housekeeping's minute |
| 539 | // finds tasks with no progress and carries them on, once per finding, |
| 540 | // within the budgets, never touching merges or held tasks. |
| 541 | func TestTriageSweepsStalled(t *testing.T) { |
| 542 | later := func(r *toolRig, d time.Duration) { r.clock(time.Now().UTC().Add(d)) } |
| 543 | t.Run("running far past its time", func(t *testing.T) { |
| 544 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 545 | id, d := r.started("r", "Stuck running") |
| 546 | r.step("r-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: d.BaseSHA}) |
| 547 | later(r, time.Hour) |
| 548 | r.tick() |
| 549 | if r.commands(wire.FrameCancelStep) != 0 { |
| 550 | t.Fatal("taken back before its time") |
| 551 | } |
| 552 | later(r, 2*time.Hour) |
| 553 | r.tick() |
| 554 | _, body := r.expect(wire.FrameCancelStep) |
| 555 | if c := body.(*wire.CancelStepCommand); c.StepID != d.StepID || c.Attempt != 1 { |
| 556 | t.Fatalf("cancel %+v", c) |
| 557 | } |
| 558 | r.step("r-gone", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Termination: true}) |
| 559 | if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { |
| 560 | t.Fatalf("next attempt %+v", again) |
| 561 | } |
| 562 | if k := r.task(id); k.Stage == wire.StageCancelled || k.Triage == nil || k.Triage.Kind != wire.TriageRetrying || k.Triage.Reason != "step_timeout" { |
| 563 | t.Fatalf("after the take-back %+v %+v", k.Stage, k.Triage) |
| 564 | } |
| 565 | r.tick() |
| 566 | if acts := r.triageActions("triage_stall"); len(acts) != 1 || r.commands(wire.FrameCancelStep) != 1 { |
| 567 | t.Fatalf("acted twice %+v", acts) |
| 568 | } |
| 569 | }) |
| 570 | t.Run("queued and never started", func(t *testing.T) { |
| 571 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 572 | id, d := r.started("q", "Stuck queued") |
| 573 | later(r, time.Hour) |
| 574 | r.tick() |
| 575 | if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { |
| 576 | t.Fatalf("placed again %+v", again) |
| 577 | } |
| 578 | dispatches := r.commands(wire.FrameDispatch) |
| 579 | r.tick() |
| 580 | if k := r.task(id); k.Steps[0].Attempt != 2 || len(r.triageActions("triage_stall")) != 1 || r.commands(wire.FrameDispatch) != dispatches { |
| 581 | t.Fatalf("after the sweep %+v", k.Steps[0]) |
| 582 | } |
| 583 | }) |
| 584 | t.Run("stopped with nothing scheduled", func(t *testing.T) { |
| 585 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 586 | if _, err := r.client.SetHousekeeping(r.ctx, true, r.housekeeping().Revision); err != nil { |
| 587 | t.Fatal(err) |
| 588 | } |
| 589 | id, d := r.started("s", "Stopped and forgotten") |
| 590 | r.stop("s", d, "runner_restarted") |
| 591 | if _, err := r.client.SetHousekeeping(r.ctx, false, r.housekeeping().Revision); err != nil { |
| 592 | t.Fatal(err) |
| 593 | } |
| 594 | later(r, time.Hour) |
| 595 | r.tick() |
| 596 | if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { |
| 597 | t.Fatalf("retried %+v", again) |
| 598 | } |
| 599 | if k := r.task(id); k.NeedsYou != "" || len(r.triageActions("triage_retry")) != 1 { |
| 600 | t.Fatalf("after the sweep %+v", k.NeedsYou) |
| 601 | } |
| 602 | }) |
| 603 | t.Run("done with its next step lost", func(t *testing.T) { |
| 604 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 605 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 606 | id, _ := r.pushed("l", "Next step lost", base, cand) |
| 607 | r.expect(wire.FrameWatchMerge) |
| 608 | c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 609 | if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_steps WHERE id = ?", c.StepID); err != nil { |
| 610 | t.Fatal(err) |
| 611 | } |
| 612 | later(r, time.Hour) |
| 613 | r.tick() |
| 614 | if again := r.dispatched(wire.SpecialistChecks, base, cand, 0); again.TaskID != id || again.StepID == c.StepID { |
| 615 | t.Fatalf("checks again %+v", again) |
| 616 | } |
| 617 | // A minute on, at the real time: the checks it sent are fresh. |
| 618 | dispatches := r.commands(wire.FrameDispatch) |
| 619 | later(r, time.Minute) |
| 620 | r.tick() |
| 621 | if len(r.triageActions("triage_kick")) != 1 || r.commands(wire.FrameDispatch) != dispatches { |
| 622 | t.Fatal("kicked twice") |
| 623 | } |
| 624 | }) |
| 625 | t.Run("never a merge", func(t *testing.T) { |
| 626 | r := newToolRigAt(t, wire.ProtocolInjectionDefences) |
| 627 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 628 | id := r.judgedTask("n", "Merging for ever", base, cand, wire.ChecksPassed, "") |
| 629 | m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 630 | r.step("n-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 631 | dispatches := r.commands(wire.FrameDispatch) |
| 632 | later(r, 3*time.Hour) |
| 633 | r.tick() |
| 634 | r.tick() |
| 635 | if r.commands(wire.FrameCancelStep) != 0 || r.commands(wire.FrameDispatch) != dispatches || len(r.triageActions("triage_stall")) != 0 { |
| 636 | t.Fatal("touched a merge") |
| 637 | } |
| 638 | }) |
| 639 | // A merge hold stops a merge, not the take-back of a stuck step. |
| 640 | t.Run("a held task's stuck step", func(t *testing.T) { |
| 641 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 642 | held, dh := r.started("h", "Held and stuck") |
| 643 | if res := r.call("h-hold", wire.ToolHoldMerge, map[string]string{"task": held, "reason": "Paul wants to look first."}); res.IsError { |
| 644 | t.Fatalf("hold %+v", res) |
| 645 | } |
| 646 | r.step("h-run", wire.StepReport{TaskID: held, StepID: dh.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: dh.BaseSHA}) |
| 647 | later(r, 2*time.Hour) |
| 648 | r.tick() |
| 649 | if _, body := r.expect(wire.FrameCancelStep); body.(*wire.CancelStepCommand).StepID != dh.StepID { |
| 650 | t.Fatalf("cancel %+v", body) |
| 651 | } |
| 652 | if k := r.task(held); k.MergeHold != "Paul wants to look first." || len(r.triageActions("triage_stall")) != 1 { |
| 653 | t.Fatalf("after the take-back %+v", k.MergeHold) |
| 654 | } |
| 655 | }) |
| 656 | } |
| 657 | |