A failed preview can run again after a merge step exists
Stuck tasks (d), the tkbstr6cjl7h shape: the preview (step 4) failed while the merge (step 5) was queued, and Retry only ever meant the newest step, so nobody could run the preview again. Retry now names a step. The task page shows Retry (accepted copy work.retry) on a failed preview's own entry for its creator; it posts the step, and Store.RetryStep makes the person's Retry checks (owner, creator, task not ended) for that step. A preview, newest or behind a merge, runs again only through retryPreview: the task's newest preview, ended, of the candidate the task stands on (approved, branch at it), on a primary whose preview host the project allows now. It is recomputed: the recorded export is cleared, so the runner builds and scans afresh and every publish check is decided for the new export by authorizePreview (public previews only under the project's setting). The task's stage, the merge and every other step are untouched. Retry with no step still means the newest step; a named step other than a preview must be the newest. It reruns from the current candidate (the commit the merge takes), the one the preview guards accept. Tests: thintest TestContractRetryFailedPreviewAfterMerge (plain Retry still 409 with the merge queued; the creator's Retry on the preview is attempt 2 of that step; stage Merging and the merge untouched; nothing publishes before the new export is built; a second tap adds nothing; no Retry for a teammate), TestContractRetryPreviewOnlyForTheCandidate. Mutation checks (each observed failing, then restored): - the page's step ignored: TestContractRetryFailedPreviewAfterMerge (409) - the old export kept: same test (old export on the new attempt) - candidate check removed: TestContractRetryPreviewOnlyForTheCandidate - a queued preview retried again: same first test (attempt 3) - Retry shown to a teammate: same first test - the "named non-preview step must be the newest" guard survives alone: a step behind the newest is never failed-and-ended in these flows, so retryStep's own state check refuses it too (kept as a second guard)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+261
−17
internal/equipo/chat/stuck.go+59 −0internal/equipo/chat/tasks.go+57 −15internal/equipo/templates/work_task.html+1 −0internal/equipo/work_pages.go+6 −2internal/equipo/work_views.go+22 −0internal/thintest/stuck_contract_test.go+116 −0
diff --git a/internal/equipo/chat/stuck.go b/internal/equipo/chat/stuck.go| index 178093a7..48396e3c 100644 |
| --- a/internal/equipo/chat/stuck.go |
| +++ b/internal/equipo/chat/stuck.go |
| @@ -93,3 +93,62 @@ func (t *txn) offerRetry(r turnRecord, args json.RawMessage) (wire.ToolResult, e |
| } |
| return wire.ToolResult{Code: "offered", Text: toolRetryOffered}, nil |
| } |
| + |
| +// retryPreview runs a failed preview again as its next attempt, also when a |
| +// merge step came after it (the person's Retry on it, or retry_step): only |
| +// the task's newest preview, ended, of the candidate the task stands on |
| +// (approved, the branch at it), on a primary whose preview host the |
| +// project allows now. It is recomputed: the export is built and scanned |
| +// afresh and every publish is decided for that export (authorizePreview), |
| +// a public one only under the project's own setting. A preview is |
| +// ancillary, so the task's stage and its other steps are untouched. |
| +func (t *txn) retryPreview(k *taskRecord, st *stepRecord, person string) (int64, error) { |
| + if st.State != wire.StepFailed || !st.Terminated { |
| + return 0, ErrNotRetryable |
| + } |
| + if st.Person != person { |
| + return 0, ErrForbidden |
| + } |
| + steps, err := t.steps(k.ID) |
| + if err != nil { |
| + return 0, err |
| + } |
| + for _, s := range steps { |
| + if s.Specialist == wire.SpecialistPreview && s.Idx > st.Idx { |
| + return 0, ErrNotRetryable |
| + } |
| + } |
| + p := progressFor(*k, steps) |
| + if p.moved || p.candidate == "" || p.candidate != st.CandidateSHA || p.review == nil || p.review.State != wire.StepDone || p.review.Verdict != wire.VerdictApprove { |
| + return 0, ErrNotRetryable |
| + } |
| + primary, err := t.s.authority.PrimaryRunner(t.db, st.Person) |
| + if err != nil { |
| + return 0, err |
| + } |
| + rs, err := t.runners(st.Person) |
| + if err != nil { |
| + return 0, err |
| + } |
| + allowed := false |
| + for _, r := range rs { |
| + if r.ID == primary && primary != "" { |
| + reason, err := t.previewHostAuthority(k.ProjectID, r) |
| + if err != nil { |
| + return 0, err |
| + } |
| + allowed = reason == "" |
| + } |
| + } |
| + if !allowed { |
| + return 0, ErrNotRetryable |
| + } |
| + st.Attempt++ |
| + st.State, st.Reason, st.Terminated, st.Summary, st.LogTail, st.StartedAt, st.FinishedAt, st.Headline, st.Activity = wire.StepQueued, "", false, "", "", nil, nil, "", "" |
| + // Recomputed: no export recorded until this attempt reports one. |
| + st.Preview, st.Withdrawn, st.RefusedBy = "", false, "" |
| + if err := t.dispatchStep(k, st); err != nil { |
| + return 0, err |
| + } |
| + return st.Attempt, t.saveStep(st, k) |
| +} |
diff --git a/internal/equipo/chat/tasks.go b/internal/equipo/chat/tasks.go| index fdb8b935..fee99237 100644 |
| --- a/internal/equipo/chat/tasks.go |
| +++ b/internal/equipo/chat/tasks.go |
| @@ -1932,39 +1932,81 @@ func (s *Store) CancelTask(ctx context.Context, person, id string) (attempt int6 |
| // RetryTask is POST /tasks/{id}/retry: a new attempt of the last step, |
| // only once the runner confirmed termination of the current one. |
| func (s *Store) RetryTask(ctx context.Context, person, id string) (attempt int64, err error) { |
| + return s.retryTask(ctx, person, id, "") |
| +} |
| + |
| +// RetryStep is the person's Retry of one step of a task (the task page's |
| +// Retry on a failed preview behind a newer step): the checks and the new |
| +// attempt RetryTask makes, for that step. |
| +func (s *Store) RetryStep(ctx context.Context, person, id, step string) (attempt int64, err error) { |
| + if step == "" { |
| + return 0, ErrInvalid |
| + } |
| + return s.retryTask(ctx, person, id, step) |
| +} |
| + |
| +func (s *Store) retryTask(ctx context.Context, person, id, step string) (attempt int64, err error) { |
| err = s.tx(ctx, func(t *txn) error { |
| if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| return err |
| } |
| - if err := t.isOwner(person); err != nil { |
| - return err |
| - } |
| - k, err := t.loadTask(id) |
| - if err != nil { |
| - return err |
| - } |
| - // Only the task's creator retries it, onto their own runner. |
| - if k.Creator != person { |
| - return ErrForbidden |
| - } |
| - if wire.FinalStage(k.Stage) || k.CancelRequested { |
| - return ErrTaskEnded |
| - } |
| - attempt, err = t.retryStep(&k, person) |
| + attempt, err = t.personRetry(person, id, step) |
| return err |
| }) |
| return attempt, err |
| } |
| +// personRetry is the person's Retry of the task's step ("" for its newest |
| +// step): an owner, the task's creator, a task that has not ended. |
| +func (t *txn) personRetry(person, id, step string) (int64, error) { |
| + if err := t.isOwner(person); err != nil { |
| + return 0, err |
| + } |
| + k, err := t.loadTask(id) |
| + if err != nil { |
| + return 0, err |
| + } |
| + // Only the task's creator retries it, onto their own runner. |
| + if k.Creator != person { |
| + return 0, ErrForbidden |
| + } |
| + if wire.FinalStage(k.Stage) || k.CancelRequested { |
| + return 0, ErrTaskEnded |
| + } |
| + return t.retryStepOf(&k, person, step) |
| +} |
| + |
| // retryStep runs the task's newest step again as its next attempt, once |
| // the runner confirmed termination of the current one: the person's Retry |
| // (RetryTask) and needs-you triage's automatic retry of a passing stop, |
| // under the housekeeping principal, which refuses a merge step. |
| func (t *txn) retryStep(k *taskRecord, person string) (attempt int64, err error) { |
| + return t.retryStepOf(k, person, "") |
| +} |
| + |
| +// retryStepOf is retryStep for a named step: the newest step, or a failed |
| +// preview behind it (retryPreview). "" is the newest step. A preview, the |
| +// newest or not, runs again only as retryPreview says. |
| +func (t *txn) retryStepOf(k *taskRecord, person, step string) (attempt int64, err error) { |
| st, found, err := t.latestStep(k.ID) |
| if err != nil { |
| return 0, err |
| } |
| + if step != "" { |
| + var named stepRecord |
| + if err := t.db.Where("id = ? AND task_id = ? AND coalesce(retired, 0) = 0", step, k.ID).First(&named).Error; errors.Is(err, gorm.ErrRecordNotFound) { |
| + return 0, ErrNotRetryable |
| + } else if err != nil { |
| + return 0, err |
| + } |
| + if named.Specialist != wire.SpecialistPreview && (!found || named.ID != st.ID) { |
| + return 0, ErrNotRetryable |
| + } |
| + st, found = named, true |
| + } |
| + if found && st.Specialist == wire.SpecialistPreview { |
| + return t.retryPreview(k, &st, person) |
| + } |
| if found && st.Specialist == wire.SpecialistEngineering && st.State == wire.StepDone && st.Publication == wire.PublicationFailed { |
| // A worker's commit the primary could not push: publish the |
| // recorded commit again, without running Engineering. |
diff --git a/internal/equipo/templates/work_task.html b/internal/equipo/templates/work_task.html| index eb03da12..1c90f06c 100644 |
| --- a/internal/equipo/templates/work_task.html |
| +++ b/internal/equipo/templates/work_task.html |
| @@ -38,6 +38,7 @@ |
| {{with .Findings}}<ul equipo-findings>{{range .}}<li>{{with .Headline}}<b>{{.}}</b> {{end}}{{.Text}}{{if .Path}}<small><code>{{.Path}}{{if .Line}}:{{.Line}}{{end}}</code></small>{{end}}</li>{{end}}</ul>{{end}} |
| {{range .Text}}<p equipo-tl-note>{{.}}</p>{{end}} |
| </div></details>{{end}} |
| +{{if .RetryStep}}<form method="post" action="{{.RetryPost}}" data-card-form><input type="hidden" name="from" value="task"><input type="hidden" name="step" value="{{.RetryStep}}"><button equipo-link type="submit" data-step-retry>{{copy "work.retry"}}</button></form>{{end}} |
| {{if .Decision}}{{template "decision" (dict "Task" $.Thin.Task "Head" false "From" "task")}}{{end}} |
| </div></li>{{end}}</ol> |
| <details equipo-details id="task-details"><summary>{{icon "chevron-down"}}{{copy "work.details"}}</summary> |
diff --git a/internal/equipo/work_pages.go b/internal/equipo/work_pages.go| index 6229c44b..fcd85de5 100644 |
| --- a/internal/equipo/work_pages.go |
| +++ b/internal/equipo/work_pages.go |
| @@ -939,9 +939,13 @@ func (a *application) workTaskAction(retry bool) http.HandlerFunc { |
| id := chi.URLParam(r, "id") |
| a.chatActionTo(w, r, workBack(r, id), func(ctx context.Context, address string) error { |
| var err error |
| - if retry { |
| + switch step := r.PostFormValue("step"); { |
| + case retry && step != "": |
| + // One step's Retry (a failed preview behind the merge). |
| + _, err = a.store.Chat().RetryStep(ctx, address, id, step) |
| + case retry: |
| _, err = a.store.Chat().RetryTask(ctx, address, id) |
| - } else { |
| + default: |
| _, err = a.store.Chat().CancelTask(ctx, address, id) |
| } |
| return err |
diff --git a/internal/equipo/work_views.go b/internal/equipo/work_views.go| index 803dfd64..9b23b6b0 100644 |
| --- a/internal/equipo/work_views.go |
| +++ b/internal/equipo/work_views.go |
| @@ -954,6 +954,9 @@ type timelineItem struct { |
| // once done (research_views.go). |
| Specialist string |
| Research *researchView |
| + // RetryPost and RetryStep are a stopped step's own Retry (a failed |
| + // preview behind the merge), for the task's creator. |
| + RetryPost, RetryStep string |
| } |
| // HasDetails reports whether the entry has anything behind Details. |
| @@ -1129,6 +1132,9 @@ func timelineOf(t wire.Task, v taskLine) []timelineItem { |
| if s.LogTail != "" { |
| it.Markdown = "```\n" + s.LogTail + "\n```" |
| } |
| + if previewRetry(t, s, v.Mine) { |
| + it.RetryPost, it.RetryStep = "/conversation/tasks/"+t.ID+"/retry", s.ID |
| + } |
| out = append(out, it) |
| continue |
| case wire.StepCancelled: |
| @@ -1248,6 +1254,22 @@ func timelineOf(t wire.Task, v taskLine) []timelineItem { |
| return out |
| } |
| +// previewRetry reports whether the person may run a failed preview again |
| +// from its own entry: theirs, ended, the task's newest preview, of the |
| +// candidate the task stands on, the task not ended. The store decides |
| +// again (retryPreview). |
| +func previewRetry(t wire.Task, s wire.Step, mine bool) bool { |
| + if !mine || s.Specialist != wire.SpecialistPreview || s.State != wire.StepFailed || !s.Terminated || wire.FinalStage(t.Stage) || t.CandidateSHA == "" || s.CandidateSHA != t.CandidateSHA { |
| + return false |
| + } |
| + for _, o := range t.Steps { |
| + if o.Specialist == wire.SpecialistPreview && o.Index > s.Index { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| // sentenceCase capitalises a reason fragment for a line of its own. |
| func sentenceCase(s string) string { |
| if s == "" { |
diff --git a/internal/thintest/stuck_contract_test.go b/internal/thintest/stuck_contract_test.go| index a7077ac2..dd1a5a97 100644 |
| --- a/internal/thintest/stuck_contract_test.go |
| +++ b/internal/thintest/stuck_contract_test.go |
| @@ -2,6 +2,8 @@ package thintest |
| import ( |
| "encoding/json" |
| + "net/http" |
| + "net/url" |
| "slices" |
| "strings" |
| "testing" |
| @@ -153,3 +155,117 @@ func TestContractOfferRetryOnlyWhatTheTapCanMerge(t *testing.T) { |
| t.Fatal("a card was raised") |
| } |
| } |
| + |
| +// previewedAndMerging is an approved candidate with a preview config on a |
| +// project that asks before merging: the preview is dispatched (step 4), |
| +// the person taps Merge (step 5, queued), and the preview fails: the |
| +// incident on tkbstr6cjl7h. |
| +func (r *toolRig) previewedAndMerging(key, base, cand string) (string, *wire.DispatchCommand, *wire.DispatchCommand) { |
| + r.t.Helper() |
| + r.asksBeforeMerging() |
| + id, _ := r.pushed(key, "Barber mockups", base, cand) |
| + r.expect(wire.FrameWatchMerge) |
| + c := r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| + r.checks(key+"-c", c, wire.ChecksPassed, exit(0), "", "ok\n") |
| + rv := r.dispatched(wire.SpecialistReview, base, cand, 0) |
| + 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)}) |
| + pv := r.previewDispatch(cand) |
| + if pv == nil { |
| + r.t.Fatal("no preview dispatched") |
| + } |
| + var card wire.Card |
| + r.wait("merge card", func(c *Conversation) bool { |
| + for _, k := range c.Cards { |
| + if k.Kind == wire.CardMerge && k.TaskID == id && k.State == wire.CardOpen { |
| + card = k |
| + } |
| + } |
| + return card.ID != "" |
| + }) |
| + if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| + r.t.Fatal(err) |
| + } |
| + m := r.dispatched(wire.SpecialistMerge, base, cand, 0) |
| + // Its export was built, then publishing it failed. |
| + r.built(key+"-pv-built", pv, digestA) |
| + 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{}}) |
| + return id, pv, m |
| +} |
| + |
| +// TestContractRetryFailedPreviewAfterMerge (stuck tasks d): a preview that |
| +// failed while the merge went ahead can be run again by its person from |
| +// the task page: a new attempt of that preview for the current candidate, |
| +// recomputed (its export reported afresh) and published only through the |
| +// preview's own guards, the merge and the task's stage untouched. Retry |
| +// with no step still means the newest step, which is the merge. |
| +func TestContractRetryFailedPreviewAfterMerge(t *testing.T) { |
| + r := publicPreviewRig(t) |
| + base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| + id, pv, m := r.previewedAndMerging("v", base, cand) |
| + if _, err := r.client.RetryTask(r.ctx, id); Status(err) != 409 { |
| + t.Fatalf("Retry with the merge queued: %v", err) |
| + } |
| + form := `<input type="hidden" name="step" value="` + pv.StepID + `">` |
| + page := r.get(r.inst.Cookie(), "/tasks/"+id, 200) |
| + if !strings.Contains(page, form) || !strings.Contains(page, "data-step-retry") { |
| + t.Fatal("the failed preview offers no Retry") |
| + } |
| + if p := r.get(r.memberCookie(), "/tasks/"+id, 200); strings.Contains(p, "data-step-retry") { |
| + t.Fatal("a teammate is offered the preview's Retry") |
| + } |
| + before, _ := r.client.Task(r.ctx, id) |
| + 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 { |
| + t.Fatalf("Retry the preview: %d %s", status, at) |
| + } |
| + again := r.previewDispatch(cand) |
| + if again == nil || again.StepID != pv.StepID || again.Attempt != 2 || again.CandidateSHA != cand { |
| + t.Fatalf("the preview again %+v", again) |
| + } |
| + k, _ := r.client.Task(r.ctx, id) |
| + if k.Stage != before.Stage || k.Stage != wire.StageMerging { |
| + t.Fatalf("the preview moved the task: %s to %s", before.Stage, k.Stage) |
| + } |
| + for _, s := range k.Steps { |
| + if s.ID == m.StepID && (s.Attempt != 1 || s.State != wire.StepQueued && s.State != wire.StepRunning) { |
| + t.Fatalf("the merge was touched %+v", s) |
| + } |
| + if s.ID == pv.StepID && (s.State != wire.StepQueued || s.Preview != nil) { |
| + t.Fatalf("the preview kept its old export %+v %+v", s, s.Preview) |
| + } |
| + } |
| + // Recomputed: nothing publishes until this attempt's export is built. |
| + if refused := r.lease(r.runner, "v-start", wire.LeaseCheck{StepID: again.StepID, Attempt: 2}); refused != "" { |
| + t.Fatalf("start refused %q", refused) |
| + } |
| + if refused := r.lease(r.runner, "v-early", wire.LeaseCheck{StepID: again.StepID, Attempt: 2, Purpose: wire.LeasePurposePreview, Digest: digestA}); refused != wire.RefusedPreviewNotBuilt { |
| + t.Fatalf("publish before built: %q", refused) |
| + } |
| + r.built("v-built", again, digestB) |
| + if refused := r.lease(r.runner, "v-pub", wire.LeaseCheck{StepID: again.StepID, Attempt: 2, Purpose: wire.LeasePurposePreview, Digest: digestB}); refused != "" { |
| + t.Fatalf("publish refused %q", refused) |
| + } |
| + // Asked again while it runs: nothing more. |
| + r.post(r.inst.Cookie(), "/conversation/tasks/"+id+"/retry", url.Values{"from": {"task"}, "step": {pv.StepID}}) |
| + if extra := r.previewDispatch(cand); extra != nil { |
| + t.Fatalf("a second preview attempt %+v", extra) |
| + } |
| + if page := r.get(r.inst.Cookie(), "/tasks/"+id, 200); strings.Contains(page, "data-step-retry") { |
| + t.Fatal("Retry still offered on a running preview") |
| + } |
| +} |
| + |
| +// TestContractRetryPreviewOnlyForTheCandidate: a failed preview of a |
| +// candidate the branch moved away from is not run again. |
| +func TestContractRetryPreviewOnlyForTheCandidate(t *testing.T) { |
| + r := publicPreviewRig(t) |
| + base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| + id, pv, _ := r.previewedAndMerging("w", base, cand) |
| + if err := r.runner.Send(r.ctx, "w-away", wire.FrameBranch, wire.BranchReport{TaskID: id, CandidateSHA: cand, Head: strings.Repeat("8", 40)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + r.wait("moved", func(c *Conversation) bool { return c.Tasks[id].NeedsYou == wire.NeedsBranchMoved }) |
| + r.post(r.inst.Cookie(), "/conversation/tasks/"+id+"/retry", url.Values{"from": {"task"}, "step": {pv.StepID}}) |
| + if again := r.previewDispatch(cand); again != nil { |
| + t.Fatalf("a preview of a candidate the branch left %+v", again) |
| + } |
| +} |