package thintest import ( "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // Needs-you triage, thin v1 (spec 2026-11-11-equipo-needs-you-triage.md): // one contract per case Paul's staging showed, and the guards. // started records a task and starts it, returning its id and the // Engineering step's dispatch. func (r *toolRig) started(key, title string) (string, *wire.DispatchCommand) { r.t.Helper() 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) } return id, r.nextDispatch() } func (r *toolRig) nextDispatch() *wire.DispatchCommand { r.t.Helper() _, body := r.expect(wire.FrameDispatch) return body.(*wire.DispatchCommand) } // stop reports the dispatched attempt running, then failed with reason // and its termination proven. func (r *toolRig) stop(key string, d *wire.DispatchCommand, reason string) { r.t.Helper() r.step(key+"-run", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: d.BaseSHA}) 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}) } // triageActions are the person's triage actions of detector. func (r *toolRig) triageActions(detector string) []wire.HousekeepingAction { r.t.Helper() var out []wire.HousekeepingAction for _, a := range r.housekeeping().Actions { if a.Detector == detector { out = append(out, a) } } return out } // commands counts the commands of kind the instance wrote for runners, // sent or not: what a tick did, before any frame is read. func (r *toolRig) commands(kind string) int { r.t.Helper() var n int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ?", kind).Scan(&n); err != nil { r.t.Fatal(err) } return n } func (r *toolRig) task(id string) wire.Task { r.t.Helper() k, err := r.client.Task(r.ctx, id) if err != nil { r.t.Fatal(err) } return k } // TestTriageRetriesPassingStop (staging items 4 and 5): Engineering // stopped because the runner restarted, before any push. Equipo retries // it once on its own (a receipt on the Housekeeping view, an honest line // on the task, nothing in Needs you); the same step stopping again stays // with the person, with Equipo's recommendation. func TestTriageRetriesPassingStop(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) id, d := r.started("p", "Show project context on thinking indicators") r.stop("p1", d, "runner_restarted") again := r.nextDispatch() if again.StepID != d.StepID || again.Attempt != 2 || again.Specialist != wire.SpecialistEngineering { t.Fatalf("not retried on its own: %+v", again) } k := r.task(id) if k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRetrying || k.Triage.Reason != "runner_restarted" { t.Fatalf("while retrying %+v %+v", k.NeedsYou, k.Triage) } if acts := r.triageActions("triage_retry"); len(acts) != 1 || acts[0].State != wire.ActionDone || acts[0].Subject.ID != id || acts[0].Undoable { t.Fatalf("receipt %+v", acts) } if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); !strings.Contains(page, "Equipo is trying the step again because the runner restarted.") { t.Fatalf("task page: %s", page) } if needs, _ := r.client.NeedsYou(r.ctx); len(needs.Items) != 0 { t.Fatalf("in Needs you while retrying: %+v", needs.Items) } // The retried attempt stops the same way: once per step, so it waits. r.stop("p2", again, "runner_restarted") k = r.task(id) 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 { t.Fatalf("second stop %+v %+v", k.Steps[0], k.Triage) } if len(r.triageActions("triage_retry")) != 1 || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("retried twice") } 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.") { t.Fatalf("recommendation: %s", page) } // A stop that is not a passing one is never retried on its own. id2, d2 := r.started("q", "Add Done to recheck granted access") r.stop("q1", d2, "provider_failed") 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 != "" { t.Fatalf("non-passing stop %+v %+v", k.Steps[0], k.Triage) } } // TestTriageNeverRetriesMerge: a merge step is never retried on its own, // whatever its reason, even when Needs you reads retry. func TestTriageNeverRetriesMerge(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id := r.judgedTask("m", "Fix the README typo", base, cand, wire.ChecksPassed, "") m := r.dispatched(wire.SpecialistMerge, base, cand, 0) r.step("m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) r.step("m-stop", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepFailed, Reason: "interrupted", BaseSHA: base, CandidateSHA: cand, Termination: true}) // Even with the task where Needs you reads retry (a stage the merge // left behind), and a sync to look again: no merge attempt. if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET stage = ? WHERE id = ?", wire.StageInReview, id); err != nil { t.Fatal(err) } r.syncNow("m-sync") if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } k := r.task(id) last := k.Steps[len(k.Steps)-1] if last.Specialist != wire.SpecialistMerge || last.Attempt != 1 || last.State != wire.StepFailed || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatalf("merge retried: %+v", last) } if acts := r.triageActions("triage_retry"); len(acts) != 0 { t.Fatalf("a triage row for a merge: %+v", acts) } } // conflictAgain takes a rebase round to its new candidate on tip, checks // and Review pass it, and the merge conflicts again. func (r *toolRig) conflictAgain(key string, rb *wire.DispatchCommand, tip, cand string) { r.t.Helper() r.step(key+"-rb-run", wire.StepReport{TaskID: rb.TaskID, StepID: rb.StepID, Attempt: rb.Attempt, State: wire.StepRunning, BaseSHA: tip}) 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}) r.expect(wire.FrameWatchMerge) c := r.dispatched(wire.SpecialistChecks, tip, cand, 0) r.checks(key+"-c", c, wire.ChecksPassed, exit(0), "", "ok\n") rv := r.dispatched(wire.SpecialistReview, tip, cand, 0) r.verdict(key+"-r", rv, wire.VerdictApprove) m := r.dispatched(wire.SpecialistMerge, tip, cand, 0) r.step(key+"-m-run", wire.StepReport{TaskID: rb.TaskID, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: tip, CandidateSHA: cand}) 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}) } // TestTriageRebasesConflict (staging item 2): the first conflict's rebase // round is the merge's own; after it, Equipo rebases each conflicting // candidate once more on its own, at most twice a task, with the gate // deciding every merge as usual. Then the person decides: Try another // round rebases again. func TestTriageRebasesConflict(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id := r.judgedTask("c", "Centre the star on the task timeline", base, cand, wire.ChecksPassed, "") m := r.dispatched(wire.SpecialistMerge, base, cand, 0) r.step("c-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) 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}) rb := r.nextDispatch() if !rb.Rebase || rb.CandidateSHA != cand { t.Fatalf("first rebase %+v", rb) } prev := cand for i, sha := range []string{"3", "4"} { tip, next := strings.Repeat("a"+sha, 20), strings.Repeat(sha, 40) r.conflictAgain("c"+sha, rb, tip, next) rb = r.nextDispatch() if rb.Specialist != wire.SpecialistEngineering || !rb.Rebase || rb.CandidateSHA != next || rb.CandidateSHA == prev { t.Fatalf("automatic rebase %d: %+v", i+1, rb) } if k := r.task(id); k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRebasing { t.Fatalf("while rebasing %+v %+v", k.NeedsYou, k.Triage) } prev = next } // A restart's re-triage gives no fresh allowance. if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } r.conflictAgain("c5", rb, strings.Repeat("a5", 20), strings.Repeat("5", 40)) r.wait("needs you", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsMergeConflict }) if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } k := r.task(id) if k.Triage == nil || k.Triage.Why != wire.TriageWhyTried || k.Triage.Recommend != wire.RecommendRebase || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatalf("after two automatic rebases %+v", k.Triage) } if acts := r.triageActions("triage_rebase"); len(acts) != 2 { t.Fatalf("rebase rows %+v", acts) } 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`) { t.Fatalf("decision: %s", page) } if _, err := r.client.RoundTask(r.ctx, id); err != nil { t.Fatal(err) } if again := r.nextDispatch(); !again.Rebase || again.CandidateSHA != strings.Repeat("5", 40) { t.Fatalf("person's rebase %+v", again) } } // TestTriageRechecksStaleChecks (staging item 6): checks that could not // run (needs_network) before the project's first hosted CI verdict run // again on the exact candidate, offered hosted CI. The SHA the card asked // about stays asked; the hosted pass rewords the card; nothing merges. func TestTriageRechecksStaleChecks(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id := r.judgedTask("s", "Plan private repository storage", base, cand, wire.ChecksCannotRun, "needs_network") card := r.mergeCardsWait(id) // No hosted verdict in the project yet: not a hosted-CI project. r.syncNow("s-sync0") if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } if k := r.task(id); len(k.Steps) != 3 || k.NeedsYou != wire.NeedsMerge { t.Fatalf("re-checked before hosted CI existed: %+v", k.Steps) } // Another task of the project gets hosted CI's verdict. time.Sleep(10 * time.Millisecond) other, _ := r.pushed("o", "Tidy the footer", base, strings.Repeat("3", 40)) ot := r.task(other) r.expect(wire.FrameWatchMerge) oc := r.dispatched(wire.SpecialistChecks, base, strings.Repeat("3", 40), 0) r.step("o-c", hostedReport(oc, ot.Branch, wire.HostedSuccess)) orv := r.dispatched(wire.SpecialistReview, base, strings.Repeat("3", 40), 0) r.verdict("o-r", orv, wire.VerdictChanges) r.dispatched(wire.SpecialistEngineering, strings.Repeat("3", 40), "", 1) if _, err := r.client.CancelTask(r.ctx, other); err != nil { t.Fatal(err) } r.syncNow("s-sync1") c := r.nextDispatch() if c.TaskID != id || c.Specialist != wire.SpecialistChecks || c.CandidateSHA != cand || !c.HostedChecks { t.Fatalf("re-check %+v", c) } if k := r.task(id); k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRechecking { t.Fatalf("while re-checking %+v %+v", k.NeedsYou, k.Triage) } task := r.task(id) r.step("s-c2", hostedReport(c, task.Branch, wire.HostedSuccess)) r.wait("card reworded", func(cv *Conversation) bool { for _, k := range cv.Cards { 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") { return true } } return false }) k := r.task(id) if k.Checks != wire.ChecksPassed || k.Stage != wire.StageReadyToMerge || k.NeedsYou != wire.NeedsMerge { t.Fatalf("after the hosted pass %+v", k) } for _, h := range r.held { if d, ok := h.body.(*wire.DispatchCommand); ok && d.Specialist == wire.SpecialistMerge && d.TaskID == id { t.Fatal("merged on its own") } } if len(r.triageActions("triage_recheck")) != 1 { t.Fatal("re-check receipt") } // Once per SHA, whatever happens next. r.syncNow("s-sync2") if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } if len(r.triageActions("triage_recheck")) != 1 || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("re-checked twice") } } // TestTriageAsksWithRecommendation (staging items 1 and 3): rounds used up // stay in Needs you as a plain question, with hosted CI's failing steps // or Review's latest finding and Equipo's recommendation; Try another // round sends Engineering once more with the last feedback. func TestTriageAsksWithRecommendation(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) base := strings.Repeat("1", 40) shas := []string{strings.Repeat("2", 40), strings.Repeat("3", 40), strings.Repeat("4", 40)} id, _ := r.pushed("f", "Add project rename control", base, shas[0]) task := r.task(id) r.expect(wire.FrameWatchMerge) for round, sha := range shas { c := r.dispatched(wire.SpecialistChecks, base, sha, round) r.step("f-c"+itoa(round), hostedReport(c, task.Branch, wire.HostedFailure, "compile", "check shard 5 of 23")) if round == len(shas)-1 { break } fix := r.dispatched(wire.SpecialistEngineering, sha, "", round+1) 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}) r.expect(wire.FrameWatchMerge) } r.wait("needs you", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsFixRounds }) k := r.task(id) 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" { t.Fatalf("recommendation %+v", k.Triage) } page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) for _, want := range []string{"Hosted checks failed: compile; check shard 5 of 23.", "Equipo recommends another round.", "data-task-round", "data-ask-coordinator"} { if !strings.Contains(page, want) { t.Fatalf("task page lacks %q: %s", want, page) } } if needs, _ := r.client.NeedsYou(r.ctx); len(needs.Items) != 1 || needs.Items[0].ID != id { t.Fatalf("Needs you %+v", needs.Items) } // Try another round: round 3, the person's, with the last feedback. if _, err := r.client.RoundTask(r.ctx, id); err != nil { t.Fatal(err) } fix := r.dispatched(wire.SpecialistEngineering, shas[2], "", 3) 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"`) { t.Fatalf("round 3 feedback %q", fix.Feedback) } // It passes the checks, and Review still asks for changes: its latest // finding is the word on the task. cand := strings.Repeat("5", 40) 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}) r.expect(wire.FrameWatchMerge) c := r.dispatched(wire.SpecialistChecks, base, cand, 3) r.step("f-c3", hostedReport(c, task.Branch, wire.HostedSuccess)) rv := r.dispatched(wire.SpecialistReview, base, cand, 3) 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}) r.wait("needs you again", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsFixRounds }) if k := r.task(id); k.Triage == nil || k.Triage.Said != "The rename field loses focus after saving." || len(k.Triage.Failed) != 0 { t.Fatalf("Review's word %+v", k.Triage) } if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); !strings.Contains(page, "Review's latest finding: The rename field loses focus after saving.") { t.Fatalf("task page: %s", page) } } // TestTriageBudget: a person's fourth automatic retry of a day waits in // Needs you, saying today's tries are used, with the recommendation; the // next day it is claimed again while the task is still there; a person's // Retry or Cancel meanwhile makes a held one stale for good. func TestTriageBudget(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) now := time.Now().UTC().Truncate(24 * time.Hour).Add(12 * time.Hour) r.clock(now) for i := range 3 { _, d := r.started("b"+itoa(i), "Task number "+itoa(i)) r.stop("b"+itoa(i), d, "runner_restarted") if again := r.nextDispatch(); again.Attempt != 2 { t.Fatalf("retry %d: %+v", i, again) } } held, d := r.started("h", "The held one") r.stop("h", d, "runner_restarted") k := r.task(held) if k.NeedsYou != wire.NeedsRetry || k.Steps[0].Attempt != 1 || k.Triage == nil || k.Triage.Why != wire.TriageWhyBudget || k.Triage.Recommend != wire.RecommendRetry { t.Fatalf("over budget %+v %+v", k.Steps[0], k.Triage) } 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.") { t.Fatalf("task page: %s", page) } // Two more held: the person retries one and cancels the other. mine, dm := r.started("y", "The one the person retries") r.stop("y", dm, "runner_restarted") gone, dg := r.started("g", "The one the person cancels") r.stop("g", dg, "runner_restarted") if _, err := r.client.RetryTask(r.ctx, mine); err != nil { t.Fatal(err) } mineAgain := r.nextDispatch() if _, err := r.client.CancelTask(r.ctx, gone); err != nil { t.Fatal(err) } // Still today: the tick claims nothing. r.tick() if k := r.task(held); k.Steps[0].Attempt != 1 { t.Fatalf("claimed over budget %+v", k.Steps[0]) } // Tomorrow: the held row is claimed; the stale ones never act. r.clock(now.Add(24 * time.Hour)) r.tick() again := r.nextDispatch() if again.TaskID != held || again.Attempt != 2 { t.Fatalf("next day %+v", again) } if k := r.task(mine); k.Steps[0].Attempt != mineAgain.Attempt || mineAgain.Attempt != 2 { t.Fatalf("person's retry retried again %+v", k.Steps[0]) } if k := r.task(gone); k.Stage != wire.StageCancelled || k.Steps[0].Attempt != 1 { t.Fatalf("cancelled task touched %+v", k) } if r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("a stale row acted") } } // TestTriageRetriageIdempotent: start-up re-triage acts once per // situation, however often it and syncs run. A stop left from before // triage (the person had paused housekeeping) is retried then, once. func TestTriageRetriageIdempotent(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) if _, err := r.client.SetHousekeeping(r.ctx, true, r.housekeeping().Revision); err != nil { t.Fatal(err) } id, d := r.started("i", "Left from before") r.stop("i", d, "runner_restarted") if k := r.task(id); k.Steps[0].Attempt != 1 || k.NeedsYou != wire.NeedsRetry { t.Fatalf("acted while paused %+v", k.Steps[0]) } if _, err := r.client.SetHousekeeping(r.ctx, false, r.housekeeping().Revision); err != nil { t.Fatal(err) } for range 2 { if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } } if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { t.Fatalf("re-triage %+v", again) } r.syncNow("i-sync") if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } if k := r.task(id); k.Steps[0].Attempt != 2 || len(r.triageActions("triage_retry")) != 1 || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatalf("acted twice %+v", k.Steps[0]) } } // TestTriageCancelledUntouched: a cancelled task is never touched. func TestTriageCancelledUntouched(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) cancelled, d := r.started("x", "Cancelled after it stopped") r.stop("x", d, "provider_failed") if _, err := r.client.CancelTask(r.ctx, cancelled); err != nil { t.Fatal(err) } // Its stop reads as a passing one now; it is still cancelled. if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET reason = 'runner_restarted' WHERE task_id = ?", cancelled); err != nil { t.Fatal(err) } for range 2 { if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } } r.syncNow("x-sync") if k := r.task(cancelled); k.Stage != wire.StageCancelled || k.Steps[0].Attempt != 1 { t.Fatalf("cancelled task touched %+v", k) } if len(r.triageActions("triage_retry")) != 0 || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("acted") } } // TestTriageHeldStillRetries (stopped-jobs audit, example 2): the // coordinator held the merge because the person wants to try the change // first, then Engineering ran out of time. The hold stops a merge, not // the retry: Equipo tries the step again once, and the hold stays. func TestTriageHeldStillRetries(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) held, dh := r.started("h", "Held by the coordinator") if res := r.call("h-hold", wire.ToolHoldMerge, map[string]string{"task": held, "reason": "Paul wants to look first."}); res.IsError { t.Fatalf("hold %+v", res) } r.stop("h", dh, "step_timeout") again := r.nextDispatch() if again.TaskID != held || again.StepID != dh.StepID || again.Attempt != 2 { t.Fatalf("held task not retried: %+v", again) } if k := r.task(held); k.NeedsYou != "" || k.Triage == nil || k.Triage.Kind != wire.TriageRetrying || k.Triage.Reason != "step_timeout" { t.Fatalf("while retrying %+v %+v", k.NeedsYou, k.Triage) } if hold := r.task(held).MergeHold; hold != "Paul wants to look first." { t.Fatalf("hold lost: %q", hold) } if len(r.triageActions("triage_retry")) != 1 { t.Fatal("not one retry") } } // TestTriageStaleRebase: a rebase held for today's budget is bound to the // merge attempt that raised it. The person's own merge Retry on the same // SHA conflicting again is a new situation: the held row never acts. func TestTriageStaleRebase(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) now := time.Now().UTC().Truncate(24 * time.Hour).Add(12 * time.Hour) r.clock(now) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id := r.judgedTask("z", "Centre the star", base, cand, wire.ChecksPassed, "") m := r.dispatched(wire.SpecialistMerge, base, cand, 0) r.step("z-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) 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}) rb := r.nextDispatch() // Today's automatic rounds are used. db := r.inst.DB.Writer() 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 { t.Fatal(err) } tip, cand2 := strings.Repeat("a3", 20), strings.Repeat("3", 40) r.conflictAgain("z2", rb, tip, cand2) r.wait("needs you", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsMergeConflict }) if k := r.task(id); k.Triage == nil || k.Triage.Why != wire.TriageWhyBudget || k.Triage.Recommend != wire.RecommendRebase { t.Fatalf("held %+v", k.Triage) } // The person merges the same SHA again; it conflicts again. if _, err := r.client.RetryTask(r.ctx, id); err != nil { t.Fatal(err) } m2 := r.nextDispatch() if m2.Specialist != wire.SpecialistMerge || m2.Attempt != 2 { t.Fatalf("person's merge %+v", m2) } r.step("z-m2-run", wire.StepReport{TaskID: id, StepID: m2.StepID, Attempt: 2, State: wire.StepRunning, BaseSHA: tip, CandidateSHA: cand2}) 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}) // Tomorrow there is room, but the held row's merge attempt is gone. r.clock(now.Add(24 * time.Hour)) r.tick() if _, err := r.inst.Store.Chat().Retriage(r.ctx, false); err != nil { t.Fatal(err) } if k := r.task(id); k.NeedsYou != wire.NeedsMergeConflict || r.heldKinds(wire.FrameDispatch) != 0 || len(r.triageActions("triage_rebase")) != 1 { t.Fatalf("a stale rebase acted: %+v", k.Steps[len(k.Steps)-1]) } } // TestTriageSweepsStalled (Paul: "does it need a loop ... to make sure it // actually carries through tasks to completion?"): housekeeping's minute // finds tasks with no progress and carries them on, once per finding, // within the budgets, never touching merges or held tasks. func TestTriageSweepsStalled(t *testing.T) { later := func(r *toolRig, d time.Duration) { r.clock(time.Now().UTC().Add(d)) } t.Run("running far past its time", func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) id, d := r.started("r", "Stuck running") r.step("r-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: d.BaseSHA}) later(r, time.Hour) r.tick() if r.commands(wire.FrameCancelStep) != 0 { t.Fatal("taken back before its time") } later(r, 2*time.Hour) r.tick() _, body := r.expect(wire.FrameCancelStep) if c := body.(*wire.CancelStepCommand); c.StepID != d.StepID || c.Attempt != 1 { t.Fatalf("cancel %+v", c) } r.step("r-gone", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Termination: true}) if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { t.Fatalf("next attempt %+v", again) } if k := r.task(id); k.Stage == wire.StageCancelled || k.Triage == nil || k.Triage.Kind != wire.TriageRetrying || k.Triage.Reason != "step_timeout" { t.Fatalf("after the take-back %+v %+v", k.Stage, k.Triage) } r.tick() if acts := r.triageActions("triage_stall"); len(acts) != 1 || r.commands(wire.FrameCancelStep) != 1 { t.Fatalf("acted twice %+v", acts) } }) t.Run("queued and never started", func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) id, d := r.started("q", "Stuck queued") later(r, time.Hour) r.tick() if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { t.Fatalf("placed again %+v", again) } dispatches := r.commands(wire.FrameDispatch) r.tick() if k := r.task(id); k.Steps[0].Attempt != 2 || len(r.triageActions("triage_stall")) != 1 || r.commands(wire.FrameDispatch) != dispatches { t.Fatalf("after the sweep %+v", k.Steps[0]) } }) t.Run("stopped with nothing scheduled", func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) if _, err := r.client.SetHousekeeping(r.ctx, true, r.housekeeping().Revision); err != nil { t.Fatal(err) } id, d := r.started("s", "Stopped and forgotten") r.stop("s", d, "runner_restarted") if _, err := r.client.SetHousekeeping(r.ctx, false, r.housekeeping().Revision); err != nil { t.Fatal(err) } later(r, time.Hour) r.tick() if again := r.nextDispatch(); again.StepID != d.StepID || again.Attempt != 2 { t.Fatalf("retried %+v", again) } if k := r.task(id); k.NeedsYou != "" || len(r.triageActions("triage_retry")) != 1 { t.Fatalf("after the sweep %+v", k.NeedsYou) } }) t.Run("done with its next step lost", func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.pushed("l", "Next step lost", base, cand) r.expect(wire.FrameWatchMerge) c := r.dispatched(wire.SpecialistChecks, base, cand, 0) if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_steps WHERE id = ?", c.StepID); err != nil { t.Fatal(err) } later(r, time.Hour) r.tick() if again := r.dispatched(wire.SpecialistChecks, base, cand, 0); again.TaskID != id || again.StepID == c.StepID { t.Fatalf("checks again %+v", again) } // A minute on, at the real time: the checks it sent are fresh. dispatches := r.commands(wire.FrameDispatch) later(r, time.Minute) r.tick() if len(r.triageActions("triage_kick")) != 1 || r.commands(wire.FrameDispatch) != dispatches { t.Fatal("kicked twice") } }) t.Run("never a merge", func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolInjectionDefences) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id := r.judgedTask("n", "Merging for ever", base, cand, wire.ChecksPassed, "") m := r.dispatched(wire.SpecialistMerge, base, cand, 0) r.step("n-m-run", wire.StepReport{TaskID: id, StepID: m.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) dispatches := r.commands(wire.FrameDispatch) later(r, 3*time.Hour) r.tick() r.tick() if r.commands(wire.FrameCancelStep) != 0 || r.commands(wire.FrameDispatch) != dispatches || len(r.triageActions("triage_stall")) != 0 { t.Fatal("touched a merge") } }) // A merge hold stops a merge, not the take-back of a stuck step. t.Run("a held task's stuck step", func(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) held, dh := r.started("h", "Held and stuck") if res := r.call("h-hold", wire.ToolHoldMerge, map[string]string{"task": held, "reason": "Paul wants to look first."}); res.IsError { t.Fatalf("hold %+v", res) } r.step("h-run", wire.StepReport{TaskID: held, StepID: dh.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: dh.BaseSHA}) later(r, 2*time.Hour) r.tick() if _, body := r.expect(wire.FrameCancelStep); body.(*wire.CancelStepCommand).StepID != dh.StepID { t.Fatalf("cancel %+v", body) } if k := r.task(held); k.MergeHold != "Paul wants to look first." || len(r.triageActions("triage_stall")) != 1 { t.Fatalf("after the take-back %+v", k.MergeHold) } }) }