| new file mode 100644 |
| index 00000000..35208814 |
| --- /dev/null |
| +++ b/internal/thintest/assistant_job_state_contract_test.go |
| @@ -0,0 +1,224 @@ |
| +package thintest |
| + |
| +import ( |
| + "database/sql" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// Settled but not done (Paul's live check, 2026-11-16): a job whose work |
| +// went as far as it will go on its own, with nothing open in front of |
| +// the person, is never left working. A failed go-live the person is not |
| +// asked about again (or declined when asked again) is stopped, for a |
| +// legacy hand-off and a job thread alike; a merged website whose Publish |
| +// was declined is done. |
| + |
| +// paulsWebsite is Paul's live sequence: "Can you make me a website?" |
| +// handed over (a legacy hand-off, or a new one with a thread); the team |
| +// asks "Which website?" and the person answers; that turn ends with no |
| +// work, so the Assistant nudges it through from its team_update turn; |
| +// the nudge's turn builds the site, its preview comes up and the person |
| +// gives the go-ahead. It returns the request, the task and the merge |
| +// step's dispatch, every team_update turn so far ended. |
| +func (o *teamRig) paulsWebsite(legacy bool) (request, id string, merge *wire.DispatchCommand) { |
| + o.t.Helper() |
| + t := o.t |
| + o.sitesOn(wire.ProtocolAssistant, true) |
| + o.shipsNo(o.project()) |
| + words := "Can you make me a website?" |
| + o.ask(words) |
| + var turn, lane string |
| + if legacy { |
| + request, turn = o.legacyHandoff("pw", words) |
| + lane = wire.LaneCoordinator |
| + } else { |
| + var cmd *wire.MessageCommand |
| + request, cmd = o.handoff("pw1", words, "") |
| + turn, lane = cmd.TurnID, wire.JobLane(request) |
| + } |
| + o.in(turn, func() { |
| + if res := o.call("pw-ask", wire.ToolAskPerson, map[string]any{"question": "Which website?", "options": []string{"A new one", "One I have"}}); res.Code != "asked" { |
| + t.Fatalf("ask %+v", res) |
| + } |
| + }) |
| + o.reply(turn) |
| + var card string |
| + var revision int64 |
| + o.until("the question", func() bool { |
| + return o.inst.DB.Writer().QueryRow("SELECT id, revision FROM chat_cards WHERE turn_id = ? AND kind = ? AND state = ?", turn, wire.CardQuestion, wire.CardOpen).Scan(&card, &revision) == nil |
| + }) |
| + ans, err := o.client.Answer(o.ctx, card, "A new one", revision) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if next := o.message(lane); next.TurnID != ans.Turn.ID { |
| + t.Fatalf("the answer's frame %+v", next) |
| + } |
| + o.reply(ans.Turn.ID) |
| + o.noteFor("the no-work note", noWork(request, 2)) |
| + o.reply(o.turn) |
| + u := o.message(wire.LaneAssistant) |
| + if u.Kind != wire.TurnTeamUpdate { |
| + t.Fatalf("update frame %+v", u) |
| + } |
| + o.in(u.TurnID, func() { nudged(t, o.teamNudge("pw-nudge", request, "make me a website"), request, 1) }) |
| + next := o.message(lane) |
| + o.reply(u.TurnID) |
| + base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| + o.in(next.TurnID, func() { id, _ = o.websiteReady("pw", "Disability benefits website", base, cand, true) }) |
| + o.reply(next.TurnID) |
| + pv := o.previewDispatch(cand) |
| + o.built("pw-pv-built", pv, digestA) |
| + o.previewDone("pw-pv-done", pv, wire.PreviewResult{Phase: wire.PreviewDone, App: "eq-" + id, URL: "https://eq-" + id + ".docs-t1.example.ts.net/", Digest: digestA, Outcome: wire.PreviewDeployed}) |
| + ahead := o.assistantCard("the go-ahead", func(c wire.AssistantCard) bool { |
| + return isMergeOutcome(c) && c.State == wire.CardOpen && len(c.Links) == 1 |
| + }) |
| + if _, err := o.a.Answer(o.ctx, ahead.ID, wire.ChoiceMerge, ahead.Revision); err != nil { |
| + t.Fatal(err) |
| + } |
| + merge = o.dispatched(wire.SpecialistMerge, base, cand, 0) |
| + o.endUpdates() |
| + return request, id, merge |
| +} |
| + |
| +// paulsPublish takes paulsWebsite's merge through and gives the Publish: |
| +// the publish step's dispatch. |
| +func (o *teamRig) paulsPublish(id string, merge *wire.DispatchCommand) *wire.DispatchCommand { |
| + o.t.Helper() |
| + o.mergedStep("pw-m", merge, strings.Repeat("e", 40)) |
| + o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| + publish := o.assistantCard("the Publish card", func(c wire.AssistantCard) bool { return isPublishOutcome(c) && c.State == wire.CardOpen }) |
| + if _, err := o.a.Answer(o.ctx, publish.ID, wire.ChoicePublish, publish.Revision); err != nil { |
| + o.t.Fatal(err) |
| + } |
| + return o.publishDispatch() |
| +} |
| + |
| +// endUpdates ends the rig's Assistant turn and then every team_update |
| +// turn once it is dispatched, by its id (its frame may have gone by), |
| +// until none is queued or running. |
| +func (o *teamRig) endUpdates() { |
| + o.t.Helper() |
| + inFlight := "SELECT count(*) FROM chat_turns WHERE id = ? AND state IN (?, ?, ?)" |
| + if o.rows(inFlight, o.turn, wire.TurnQueued, wire.TurnRunning, wire.TurnWaiting) == 1 { |
| + o.reply(o.turn) |
| + } |
| + for range 50 { |
| + time.Sleep(20 * time.Millisecond) |
| + var turn string |
| + var dispatched bool |
| + err := o.inst.DB.Writer().QueryRow("SELECT id, dispatched FROM chat_turns WHERE kind = ? AND lane = ? AND state IN (?, ?, ?) ORDER BY created_at, id LIMIT 1", wire.TurnTeamUpdate, wire.LaneAssistant, wire.TurnQueued, wire.TurnRunning, wire.TurnWaiting).Scan(&turn, &dispatched) |
| + switch { |
| + case err == sql.ErrNoRows: |
| + return |
| + case err != nil: |
| + o.t.Fatal(err) |
| + case dispatched: |
| + o.reply(turn) |
| + } |
| + } |
| + o.t.Fatal("team_update turns keep coming") |
| +} |
| + |
| +// openOutcomes are the go-ahead and Publish cards open in the Assistant |
| +// lane. |
| +func (o *teamRig) openOutcomes() int { |
| + o.t.Helper() |
| + return o.rows("SELECT count(*) FROM chat_cards WHERE lane = ? AND kind = ? AND state = ?", wire.LaneAssistant, wire.CardOutcome, wire.CardOpen) |
| +} |
| + |
| +// settledStopped fails unless the request's job card ends stopped, in its |
| +// words, with nothing open in front of the person. |
| +func (o *teamRig) settledStopped(request string) { |
| + o.t.Helper() |
| + o.endUpdates() |
| + card := o.jobOf(request, "stopped", func(c wire.AssistantCard) bool { return c.State != wire.JobWorking }) |
| + if status, detail := jobWords(wire.JobStopped); card.State != wire.JobStopped || card.Result != status || card.Detail != detail { |
| + o.t.Fatalf("the job card %+v, want stopped", card) |
| + } |
| + if n := o.openOutcomes(); n != 0 { |
| + o.t.Fatalf("%d go-ahead or Publish cards open", n) |
| + } |
| +} |
| + |
| +// TestContractTeamJobSettledStates: Paul's sequence, for a legacy |
| +// hand-off and a job thread, through each way its going live can end: |
| +// |
| +// - the publish failed and was not asked again (the app name was |
| +// taken): stopped; |
| +// - the publish failed, was asked again, and the person said Not now: |
| +// stopped (ruling R19); |
| +// - the publish ended cancelled with nothing asked again: stopped, a |
| +// site that did not go live is not done; |
| +// - the merge the person gave the go-ahead for failed, leaving the task |
| +// ready to merge with nothing open: stopped, not "Almost ready"; |
| +// - the Publish declined: done. |
| +func TestContractTeamJobSettledStates(t *testing.T) { |
| + cases := []struct { |
| + name string |
| + run func(o *teamRig, request, id string, merge *wire.DispatchCommand) |
| + }{ |
| + {"publish_failed", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| + d := o.paulsPublish(id, merge) |
| + o.step("pw-failed", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepFailed, Reason: "publish_app_taken", CandidateSHA: d.CandidateSHA, Termination: true, Findings: []wire.Finding{}}) |
| + o.settledStopped(request) |
| + }}, |
| + {"publish_failed_declined", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| + d := o.paulsPublish(id, merge) |
| + o.publishReport("pw-failed", d, wire.StepFailed, nil) |
| + again := o.assistantCard("the Publish asked again", func(c wire.AssistantCard) bool { return isPublishOutcome(c) && c.State == wire.CardOpen }) |
| + o.endUpdates() |
| + if ready := o.jobOf(request, "ready", nil); ready.State != wire.JobReady { |
| + o.t.Fatalf("with the Publish asked again %+v", ready) |
| + } |
| + if _, err := o.a.Answer(o.ctx, again.ID, wire.ChoiceNotNow, again.Revision); err != nil { |
| + o.t.Fatal(err) |
| + } |
| + o.settledStopped(request) |
| + }}, |
| + {"publish_cancelled", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| + d := o.paulsPublish(id, merge) |
| + o.exec("UPDATE chat_steps SET state = ?, reason = 'withdrawn', terminated = 1 WHERE id = ?", wire.StepCancelled, d.StepID) |
| + if _, err := o.client.PostThread(o.ctx, id, "For the record.", "pw-note"); err != nil { |
| + o.t.Fatal(err) |
| + } |
| + o.settledStopped(request) |
| + }}, |
| + {"merge_failed", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| + o.step("pw-m-run", wire.StepReport{TaskID: merge.TaskID, StepID: merge.StepID, Attempt: merge.Attempt, State: wire.StepRunning, BaseSHA: merge.BaseSHA, CandidateSHA: merge.CandidateSHA}) |
| + o.step("pw-m-fail", wire.StepReport{TaskID: merge.TaskID, StepID: merge.StepID, Attempt: merge.Attempt, State: wire.StepFailed, Reason: "merge_failed", BaseSHA: merge.BaseSHA, CandidateSHA: merge.CandidateSHA, Termination: true}) |
| + o.wait("ready to merge again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| + o.settledStopped(request) |
| + o.noteFor("the failed merge's note", blockedOn("going live failed, and they need to try it again")) |
| + }}, |
| + {"publish_declined", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| + o.mergedStep("pw-m", merge, strings.Repeat("e", 40)) |
| + o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| + publish := o.assistantCard("the Publish card", func(c wire.AssistantCard) bool { return isPublishOutcome(c) && c.State == wire.CardOpen }) |
| + if _, err := o.a.Answer(o.ctx, publish.ID, wire.ChoiceNotNow, publish.Revision); err != nil { |
| + o.t.Fatal(err) |
| + } |
| + o.endUpdates() |
| + card := o.jobOf(request, "done", func(c wire.AssistantCard) bool { return c.State != wire.JobWorking && c.State != wire.JobReady }) |
| + if status, detail := jobWords(wire.JobDone); card.State != wire.JobDone || card.Result != status || card.Detail != detail { |
| + o.t.Fatalf("the job card %+v, want done", card) |
| + } |
| + }}, |
| + } |
| + for _, legacy := range []bool{true, false} { |
| + for _, c := range cases { |
| + name := "thread/" + c.name |
| + if legacy { |
| + name = "legacy/" + c.name |
| + } |
| + t.Run(name, func(t *testing.T) { |
| + o := newTeamRig(t) |
| + request, id, merge := o.paulsWebsite(legacy) |
| + c.run(o, request, id, merge) |
| + }) |
| + } |
| + } |
| +} |