| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Settled but not done (Paul's live check, 2026-11-16): a job whose work |
| 14 | // went as far as it will go on its own, with nothing open in front of |
| 15 | // the person, is never left working. A failed go-live the person is not |
| 16 | // asked about again (or declined when asked again) is stopped, for a |
| 17 | // legacy hand-off and a job thread alike; a merged website whose Publish |
| 18 | // was declined is done. |
| 19 | |
| 20 | // paulsWebsite is Paul's live sequence: "Can you make me a website?" |
| 21 | // handed over (a legacy hand-off, or a new one with a thread); the team |
| 22 | // asks "Which website?" and the person answers; that turn ends with no |
| 23 | // work, so the Assistant nudges it through from its team_update turn; |
| 24 | // the nudge's turn builds the site, its preview comes up and the person |
| 25 | // gives the go-ahead. It returns the request, the task and the merge |
| 26 | // step's dispatch, every team_update turn so far ended. |
| 27 | func (o *teamRig) paulsWebsite(legacy bool) (request, id string, merge *wire.DispatchCommand) { |
| 28 | o.t.Helper() |
| 29 | t := o.t |
| 30 | o.sitesOn(wire.ProtocolCurrent, true) |
| 31 | o.shipsNo(o.project()) |
| 32 | words := "Can you make me a website?" |
| 33 | o.ask(words) |
| 34 | var turn, lane string |
| 35 | if legacy { |
| 36 | request, turn = o.legacyHandoff("pw", words) |
| 37 | lane = wire.LaneCoordinator |
| 38 | } else { |
| 39 | var cmd *wire.MessageCommand |
| 40 | request, cmd = o.handoff("pw1", words, "") |
| 41 | turn, lane = cmd.TurnID, wire.JobLane(request) |
| 42 | } |
| 43 | o.in(turn, func() { |
| 44 | 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" { |
| 45 | t.Fatalf("ask %+v", res) |
| 46 | } |
| 47 | }) |
| 48 | o.reply(turn) |
| 49 | var card string |
| 50 | var revision int64 |
| 51 | o.until("the question", func() bool { |
| 52 | 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 |
| 53 | }) |
| 54 | ans, err := o.client.Answer(o.ctx, card, "A new one", revision) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if next := o.message(lane); next.TurnID != ans.Turn.ID { |
| 59 | t.Fatalf("the answer's frame %+v", next) |
| 60 | } |
| 61 | o.reply(ans.Turn.ID) |
| 62 | o.noteFor("the no-work note", noWork(request, 2)) |
| 63 | o.reply(o.turn) |
| 64 | u := o.message(wire.LaneAssistant) |
| 65 | if u.Kind != wire.TurnTeamUpdate { |
| 66 | t.Fatalf("update frame %+v", u) |
| 67 | } |
| 68 | o.in(u.TurnID, func() { nudged(t, o.teamNudge("pw-nudge", request, "make me a website"), request, 1) }) |
| 69 | next := o.message(lane) |
| 70 | o.reply(u.TurnID) |
| 71 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 72 | o.in(next.TurnID, func() { id, _ = o.websiteReady("pw", "Disability benefits website", base, cand, true) }) |
| 73 | o.reply(next.TurnID) |
| 74 | pv := o.previewDispatch(cand) |
| 75 | o.built("pw-pv-built", pv, digestA) |
| 76 | 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}) |
| 77 | ahead := o.assistantCard("the go-ahead", func(c wire.AssistantCard) bool { |
| 78 | return isMergeOutcome(c) && c.State == wire.CardOpen && len(c.Links) == 1 |
| 79 | }) |
| 80 | if _, err := o.a.Answer(o.ctx, ahead.ID, wire.ChoiceMerge, ahead.Revision); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | merge = o.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 84 | o.endUpdates() |
| 85 | return request, id, merge |
| 86 | } |
| 87 | |
| 88 | // paulsPublish takes paulsWebsite's merge through and gives the Publish: |
| 89 | // the publish step's dispatch. |
| 90 | func (o *teamRig) paulsPublish(id string, merge *wire.DispatchCommand) *wire.DispatchCommand { |
| 91 | o.t.Helper() |
| 92 | o.mergedStep("pw-m", merge, strings.Repeat("e", 40)) |
| 93 | o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 94 | publish := o.assistantCard("the Publish card", func(c wire.AssistantCard) bool { return isPublishOutcome(c) && c.State == wire.CardOpen }) |
| 95 | if _, err := o.a.Answer(o.ctx, publish.ID, wire.ChoicePublish, publish.Revision); err != nil { |
| 96 | o.t.Fatal(err) |
| 97 | } |
| 98 | return o.publishDispatch() |
| 99 | } |
| 100 | |
| 101 | // endUpdates ends the rig's Assistant turn and then every team_update |
| 102 | // turn once it is dispatched, by its id (its frame may have gone by), |
| 103 | // until none is queued or running. |
| 104 | func (o *teamRig) endUpdates() { |
| 105 | o.t.Helper() |
| 106 | inFlight := "SELECT count(*) FROM chat_turns WHERE id = ? AND state IN (?, ?, ?)" |
| 107 | if o.rows(inFlight, o.turn, wire.TurnQueued, wire.TurnRunning, wire.TurnWaiting) == 1 { |
| 108 | o.reply(o.turn) |
| 109 | } |
| 110 | for range 50 { |
| 111 | time.Sleep(20 * time.Millisecond) |
| 112 | var turn string |
| 113 | var dispatched bool |
| 114 | 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) |
| 115 | switch { |
| 116 | case err == sql.ErrNoRows: |
| 117 | return |
| 118 | case err != nil: |
| 119 | o.t.Fatal(err) |
| 120 | case dispatched: |
| 121 | o.reply(turn) |
| 122 | } |
| 123 | } |
| 124 | o.t.Fatal("team_update turns keep coming") |
| 125 | } |
| 126 | |
| 127 | // openOutcomes are the go-ahead and Publish cards open in the Assistant |
| 128 | // lane. |
| 129 | func (o *teamRig) openOutcomes() int { |
| 130 | o.t.Helper() |
| 131 | return o.rows("SELECT count(*) FROM chat_cards WHERE lane = ? AND kind = ? AND state = ?", wire.LaneAssistant, wire.CardOutcome, wire.CardOpen) |
| 132 | } |
| 133 | |
| 134 | // settledStopped fails unless the request's job card ends stopped, in its |
| 135 | // words, with nothing open in front of the person. |
| 136 | func (o *teamRig) settledStopped(request string) { |
| 137 | o.t.Helper() |
| 138 | o.endUpdates() |
| 139 | card := o.jobOf(request, "stopped", func(c wire.AssistantCard) bool { return c.State != wire.JobWorking }) |
| 140 | if status, detail := jobWords(wire.JobStopped); card.State != wire.JobStopped || card.Result != status || card.Detail != detail { |
| 141 | o.t.Fatalf("the job card %+v, want stopped", card) |
| 142 | } |
| 143 | if n := o.openOutcomes(); n != 0 { |
| 144 | o.t.Fatalf("%d go-ahead or Publish cards open", n) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // TestContractTeamJobSettledStates: Paul's sequence, for a legacy |
| 149 | // hand-off and a job thread, through each way its going live can end: |
| 150 | // |
| 151 | // - the publish failed and was not asked again (the app name was |
| 152 | // taken): stopped; |
| 153 | // - the publish failed, was asked again, and the person said Not now: |
| 154 | // stopped (ruling R19); |
| 155 | // - the publish ended cancelled with nothing asked again: stopped, a |
| 156 | // site that did not go live is not done; |
| 157 | // - the merge the person gave the go-ahead for failed, leaving the task |
| 158 | // ready to merge with nothing open: stopped, not "Almost ready"; |
| 159 | // - the Publish declined: done. |
| 160 | func TestContractTeamJobSettledStates(t *testing.T) { |
| 161 | cases := []struct { |
| 162 | name string |
| 163 | run func(o *teamRig, request, id string, merge *wire.DispatchCommand) |
| 164 | }{ |
| 165 | {"publish_failed", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| 166 | d := o.paulsPublish(id, merge) |
| 167 | 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{}}) |
| 168 | o.settledStopped(request) |
| 169 | }}, |
| 170 | {"publish_failed_declined", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| 171 | d := o.paulsPublish(id, merge) |
| 172 | o.publishReport("pw-failed", d, wire.StepFailed, nil) |
| 173 | again := o.assistantCard("the Publish asked again", func(c wire.AssistantCard) bool { return isPublishOutcome(c) && c.State == wire.CardOpen }) |
| 174 | o.endUpdates() |
| 175 | if ready := o.jobOf(request, "ready", nil); ready.State != wire.JobReady { |
| 176 | o.t.Fatalf("with the Publish asked again %+v", ready) |
| 177 | } |
| 178 | if _, err := o.a.Answer(o.ctx, again.ID, wire.ChoiceNotNow, again.Revision); err != nil { |
| 179 | o.t.Fatal(err) |
| 180 | } |
| 181 | o.settledStopped(request) |
| 182 | }}, |
| 183 | {"publish_cancelled", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| 184 | d := o.paulsPublish(id, merge) |
| 185 | o.exec("UPDATE chat_steps SET state = ?, reason = 'withdrawn', terminated = 1 WHERE id = ?", wire.StepCancelled, d.StepID) |
| 186 | if _, err := o.client.PostThread(o.ctx, id, "For the record.", "pw-note"); err != nil { |
| 187 | o.t.Fatal(err) |
| 188 | } |
| 189 | o.settledStopped(request) |
| 190 | }}, |
| 191 | {"merge_failed", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| 192 | 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}) |
| 193 | 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}) |
| 194 | o.wait("ready to merge again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 195 | o.settledStopped(request) |
| 196 | o.noteFor("the failed merge's note", blockedOn("going live failed, and they need to try it again")) |
| 197 | }}, |
| 198 | {"merge_credential", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| 199 | // The runner refused the merge itself: no access to publish to |
| 200 | // the repository. Stopped, saying what is missing, and the |
| 201 | // Assistant is not told to have the person try again. |
| 202 | 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}) |
| 203 | o.step("pw-m-fail", wire.StepReport{TaskID: merge.TaskID, StepID: merge.StepID, Attempt: merge.Attempt, State: wire.StepFailed, Reason: wire.ReasonMergeCredential, BaseSHA: merge.BaseSHA, CandidateSHA: merge.CandidateSHA, Termination: true}) |
| 204 | o.wait("ready to merge again", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageReadyToMerge }) |
| 205 | o.endUpdates() |
| 206 | card := o.jobOf(request, "stopped", func(c wire.AssistantCard) bool { return c.State != wire.JobWorking }) |
| 207 | if card.State != wire.JobStopped || card.Result != equipo.RunnerCopy("assistant.job.stopped") || card.Detail != equipo.RunnerCopy("assistant.job.detail.no_access") { |
| 208 | o.t.Fatalf("the job card %+v, want stopped for want of access", card) |
| 209 | } |
| 210 | if n := o.openOutcomes(); n != 0 { |
| 211 | o.t.Fatalf("%d go-ahead or Publish cards open", n) |
| 212 | } |
| 213 | note := o.noteFor("the refused merge's note", blockedOn(noAccessLine)) |
| 214 | for _, retry := range []string{"try it again", "go-ahead", "team_nudge", equipo.RunnerCopy("assistant.team.stage.ready_to_merge")} { |
| 215 | if strings.Contains(note, retry) { |
| 216 | o.t.Fatalf("the note invites %q: %q", retry, note) |
| 217 | } |
| 218 | } |
| 219 | if !strings.Contains(note, equipo.RunnerCopy("assistant.team.no_access")) { |
| 220 | o.t.Fatalf("the note does not say what is missing: %q", note) |
| 221 | } |
| 222 | }}, |
| 223 | {"publish_declined", func(o *teamRig, request, id string, merge *wire.DispatchCommand) { |
| 224 | o.mergedStep("pw-m", merge, strings.Repeat("e", 40)) |
| 225 | o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 226 | publish := o.assistantCard("the Publish card", func(c wire.AssistantCard) bool { return isPublishOutcome(c) && c.State == wire.CardOpen }) |
| 227 | if _, err := o.a.Answer(o.ctx, publish.ID, wire.ChoiceNotNow, publish.Revision); err != nil { |
| 228 | o.t.Fatal(err) |
| 229 | } |
| 230 | o.endUpdates() |
| 231 | card := o.jobOf(request, "done", func(c wire.AssistantCard) bool { return c.State != wire.JobWorking && c.State != wire.JobReady }) |
| 232 | if status, detail := jobWords(wire.JobDone); card.State != wire.JobDone || card.Result != status || card.Detail != detail { |
| 233 | o.t.Fatalf("the job card %+v, want done", card) |
| 234 | } |
| 235 | }}, |
| 236 | } |
| 237 | for _, legacy := range []bool{true, false} { |
| 238 | for _, c := range cases { |
| 239 | name := "thread/" + c.name |
| 240 | if legacy { |
| 241 | name = "legacy/" + c.name |
| 242 | } |
| 243 | t.Run(name, func(t *testing.T) { |
| 244 | o := newTeamRigAt(t, wire.ProtocolCurrent) |
| 245 | request, id, merge := o.paulsWebsite(legacy) |
| 246 | c.run(o, request, id, merge) |
| 247 | }) |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |