oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "encoding/json"
5 "slices"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/equipo/assist"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// team_reply (jobs design §4; D17): the Assistant answers the open
14// question on one of the person's jobs, as the person, through the
15// question card's own path: an option when the question has options,
16// else its own words, with no words check. One answer per key, audited
17// without payload. Only a question card, and in a team_update turn only
18// the job the update is about.
19
20const loginQuestion = "Which login page?"
21
22// askedJob hands "Login is broken" to the team from a new Assistant turn,
23// has the job's thread ask question (with options, none when nil), ends
24// the thread's turn and the Assistant's, and returns the request, the
25// thread's turn and the team_update turn that tells the Assistant,
26// dispatched.
27func (o *teamRig) askedJob(key, question string, options []string) (request, asked, update string) {
28 o.t.Helper()
29 o.ask("Can we fix a bug in Tito? Login is broken")
30 request, cmd := o.handoff(key+"-h", "Login is broken", "")
31 args := map[string]any{"question": question}
32 if options != nil {
33 args["options"] = options
34 }
35 o.in(cmd.TurnID, func() {
36 if res := o.call(key+"-ask", wire.ToolAskPerson, args); res.Code != "asked" {
37 o.t.Fatalf("ask %+v", res)
38 }
39 })
40 o.reply(cmd.TurnID)
41 o.reply(o.turn)
42 u := o.message(wire.LaneAssistant)
43 if u.Kind != wire.TurnTeamUpdate {
44 o.t.Fatalf("update frame %+v", u)
45 }
46 return request, cmd.TurnID, u.TurnID
47}
48
49// teamReply is team_reply for request in the rig's turn.
50func (o *teamRig) teamReply(id, request, answer string) wire.ToolResult {
51 o.t.Helper()
52 return o.use(id, "team_reply", map[string]any{"request": request, "answer": answer})
53}
54
55// replied fails unless res is team_reply's answer for request.
56func replied(t *testing.T, res wire.ToolResult, request string) {
57 t.Helper()
58 want, _ := json.Marshal(map[string]string{"request": request, "status": "answered"})
59 if res.IsError || res.Code != "used" || res.Text != string(want) {
60 t.Fatalf("team_reply %+v, want %s", res, want)
61 }
62}
63
64// questionCard is the state, choice and revision of the request's newest
65// question card.
66func (o *teamRig) questionCard(request string) (state, choice string, revision int64) {
67 o.t.Helper()
68 q := "SELECT state, coalesce(choice, ''), revision FROM chat_cards c WHERE kind = ? AND (lane = ? OR turn_id IN (SELECT turn_id FROM chat_handoff_turns WHERE handoff_id = ?)) ORDER BY created_at DESC, rowid DESC LIMIT 1"
69 if err := o.inst.DB.Writer().QueryRow(q, wire.CardQuestion, wire.JobLane(request), request).Scan(&state, &choice, &revision); err != nil {
70 o.t.Fatal(err)
71 }
72 return state, choice, revision
73}
74
75// lastAudit is the newest audit row's id.
76func (o *teamRig) lastAudit() int {
77 return o.rows("SELECT coalesce(max(id), 0) FROM audit_events")
78}
79
80// TestContractTeamReply: in the team_update turn that tells it of the
81// job's question, the Assistant answers it with team_reply, in any case:
82// the question card is answered with the option's own text, which the
83// job's thread gets as the person's message, dispatched with the
84// question it answers. One audit row, with neither the answer nor the
85// question. The job card never shows asking after it, and the same
86// question asked again is told again.
87func TestContractTeamReply(t *testing.T) {
88 o := newTeamRig(t)
89 request, asked, update := o.askedJob("rp", loginQuestion, []string{"Web", "App"})
90 card := o.jobCardID(request)
91 events := len(o.jobEvents(card))
92 audit := o.lastAudit()
93 var res wire.ToolResult
94 o.in(update, func() { res = o.teamReply("rp-reply", request, " web ") })
95 replied(t, res, request)
96 if state, choice, _ := o.questionCard(request); state != wire.CardAnswered || choice != "Web" {
97 t.Fatalf("the question card %s %q", state, choice)
98 }
99 thread := o.thread(request)
100 if last := thread[len(thread)-1]; last.Role != wire.RolePerson || last.Text != "Web" || last.TurnID == asked {
101 t.Fatalf("the thread's last message %+v", last)
102 }
103 cmd := o.message(wire.JobLane(request))
104 if withoutProjectContext(t, cmd.Text) != `(My answer to your question "Which login page?") Web` || len(cmd.PersonText) != 0 {
105 t.Fatalf("the answer's frame %+v", cmd)
106 }
107 if n := o.rows("SELECT count(*) FROM audit_events WHERE id > ? AND kind = 'assistant.team_reply' AND actor = ? AND record_id = ?", audit, Owner, request); n != 1 {
108 t.Fatalf("%d team_reply audits", n)
109 }
110 if n := o.rows("SELECT count(*) FROM audit_events WHERE id > ? AND (record_id LIKE '%Web%' OR record_id LIKE '%login page%')", audit); n != 0 {
111 t.Fatalf("%d audit rows carry the answer or the question", n)
112 }
113 o.reply(update)
114 o.jobOf(request, "working", func(c wire.AssistantCard) bool { return c.State == wire.JobWorking })
115 for _, c := range o.jobEvents(card)[events:] {
116 if c.State != wire.JobWorking {
117 t.Fatalf("the job card after the answer %+v", c)
118 }
119 }
120 // The same question asked again is news again.
121 o.in(cmd.TurnID, func() {
122 if res := o.call("rp-again", wire.ToolAskPerson, map[string]any{"question": loginQuestion, "options": []string{"Web", "App"}}); res.Code != "asked" {
123 t.Fatalf("ask again %+v", res)
124 }
125 })
126 o.reply(cmd.TurnID)
127 o.until("the second update", func() bool { ids, _, _ := o.updates(); return len(ids) == 2 })
128}
129
130// TestContractTeamReplyOptions: an answer that is none of the options is
131// not_an_option, naming them, and the card stays open; a question with
132// no options takes any words of 1 to 2000 characters (2001 and blank are
133// invalid_input); with no question open, no_question.
134func TestContractTeamReplyOptions(t *testing.T) {
135 t.Run("options", func(t *testing.T) {
136 o := newTeamRig(t)
137 request, _, update := o.askedJob("ro", loginQuestion, []string{"Web", "App"})
138 _, _, revision := o.questionCard(request)
139 o.in(update, func() {
140 res := o.teamReply("ro-desktop", request, "Desktop")
141 if !res.IsError || res.Code != "not_an_option" || res.Text != `The answer must be one of the question's options: "Web", "App".` {
142 t.Fatalf("not an option %+v", res)
143 }
144 })
145 if state, _, rev := o.questionCard(request); state != wire.CardOpen || rev != revision {
146 t.Fatalf("the card after a refusal %s %d", state, rev)
147 }
148 })
149 // Malformed UTF-8 in the raw input is refused before it is decoded
150 // (the decoder would make it U+FFFD), for team_request too.
151 t.Run("malformed bytes", func(t *testing.T) {
152 o := newTeamRig(t)
153 request, _, update := o.askedJob("rm", "What should the button say?", nil)
154 _, _, revision := o.questionCard(request)
155 raw := func(op, input string) json.RawMessage {
156 return json.RawMessage(`{"operation":"` + op + `","input":` + input + `}`)
157 }
158 o.in(update, func() {
159 if res := o.tool("rm-reply", wire.ToolUse, raw("team_reply", `{"request":"`+request+`","answer":"Sign in`+"\xff"+`"}`)); !res.IsError || res.Code != "invalid_input" {
160 t.Fatalf("team_reply with a malformed byte %+v", res)
161 }
162 })
163 if state, _, rev := o.questionCard(request); state != wire.CardOpen || rev != revision {
164 t.Fatalf("the card after malformed bytes %s %d", state, rev)
165 }
166 o.reply(update)
167 o.ask("Can we fix a bug in Tito? Login is broken")
168 before := o.rows("SELECT count(*) FROM chat_handoffs")
169 if res := o.tool("rm-request", wire.ToolUse, raw("team_request", `{"words":"Login is broken","context":"Tito`+"\xff"+`"}`)); !res.IsError || res.Code != "invalid_input" {
170 t.Fatalf("team_request with a malformed byte %+v", res)
171 }
172 if n := o.rows("SELECT count(*) FROM chat_handoffs"); n != before {
173 t.Fatalf("%d hand-offs, %d before", n, before)
174 }
175 })
176 t.Run("free text", func(t *testing.T) {
177 o := newTeamRig(t)
178 request, _, update := o.askedJob("rf", "What should the button say?", nil)
179 o.in(update, func() {
180 for key, answer := range map[string]string{"rf-long": strings.Repeat("é", 2001), "rf-blank": " \n "} {
181 if res := o.teamReply(key, request, answer); !res.IsError || res.Code != "invalid_input" {
182 t.Fatalf("%s %+v", key, res)
183 }
184 }
185 if state, _, _ := o.questionCard(request); state != wire.CardOpen {
186 t.Fatalf("the card after refusals %s", state)
187 }
188 answer := strings.Repeat("é", 2000)
189 replied(t, o.teamReply("rf-ok", request, answer), request)
190 if state, choice, _ := o.questionCard(request); state != wire.CardAnswered || choice != answer {
191 t.Fatalf("the card %s, %d runes", state, len([]rune(choice)))
192 }
193 res := o.teamReply("rf-none", request, "Sign in")
194 if !res.IsError || res.Code != "no_question" || res.Text != "That job has no open question. Use team_status to see where it stands." {
195 t.Fatalf("no question %+v", res)
196 }
197 })
198 })
199}
200
201// TestContractTeamReplyFromContext: once the Assistant was told and the
202// job shows asking, it answers from the conversation in a turn the
203// person started, in words found in no message of theirs; the job card
204// is working again at once.
205func TestContractTeamReplyFromContext(t *testing.T) {
206 o := newTeamRig(t)
207 request, _, update := o.askedJob("rc", "What should the button say?", nil)
208 o.reply(update)
209 card := o.jobOf(request, "asking", func(c wire.AssistantCard) bool { return c.State == wire.JobAsking })
210 o.ask("Use the email one, like we said")
211 events := len(o.jobEvents(card.ID))
212 replied(t, o.teamReply("rc-reply", request, "Sign in with email"), request)
213 if state, choice, _ := o.questionCard(request); state != wire.CardAnswered || choice != "Sign in with email" {
214 t.Fatalf("the question card %s %q", state, choice)
215 }
216 after := o.jobEvents(card.ID)[events:]
217 if len(after) == 0 || after[0].State != wire.JobWorking {
218 t.Fatalf("the job card's events after the answer %+v", after)
219 }
220}
221
222// TestContractTeamReplyOnce: the same call under a new call id, then a
223// result lost before it was stored, in attempt 2: one answer, one
224// message in the thread, one action, one audit, equal results.
225func TestContractTeamReplyOnce(t *testing.T) {
226 o := newTeamRig(t)
227 request, _, update := o.askedJob("ron", loginQuestion, []string{"Web", "App"})
228 var first, again, lost wire.ToolResult
229 o.in(update, func() {
230 first = o.teamReply("ron-1", request, "Web")
231 again = o.teamReply("ron-2", request, "Web")
232 })
233 replied(t, first, request)
234 if again.IsError || !again.Replayed || again.Text != first.Text {
235 t.Fatalf("again %+v, first %+v", again, first)
236 }
237 o.runner.Send(o.ctx, "r-ron-wait", wire.FrameTurn, wire.TurnReport{TurnID: update, Attempt: 1, State: wire.TurnWaiting, Reason: "limited"})
238 o.runner.Send(o.ctx, "r-ron-run2", wire.FrameTurn, wire.TurnReport{TurnID: update, Attempt: 2, State: wire.TurnRunning})
239 o.until("attempt 2", func() bool {
240 return o.rows("SELECT count(*) FROM chat_turns WHERE id = ? AND attempt = 2 AND state = ?", update, wire.TurnRunning) == 1
241 })
242 // The ledger lost the result: the provider gets the same key again.
243 o.exec("UPDATE chat_tool_calls SET ledger_key = '' WHERE turn_id = ?", update)
244 o.in(update, func() {
245 o.attempt = 2
246 lost = o.teamReply("ron-3", request, "Web")
247 })
248 if lost.IsError || lost.Text != first.Text {
249 t.Fatalf("after a lost result %+v, first %+v", lost, first)
250 }
251 if n := o.rows("SELECT count(*) FROM chat_messages WHERE lane = ? AND role = ? AND text = 'Web'", wire.JobLane(request), wire.RolePerson); n != 1 {
252 t.Fatalf("%d answers in the thread", n)
253 }
254 if n := o.rows("SELECT count(*) FROM chat_handoff_actions WHERE handoff_id = ? AND kind = 'reply'", request); n != 1 {
255 t.Fatalf("%d reply actions", n)
256 }
257 if n := o.rows("SELECT count(*) FROM audit_events WHERE kind = 'assistant.team_reply'"); n != 1 {
258 t.Fatalf("%d team_reply audits", n)
259 }
260}
261
262// TestContractTeamReplyOnlyOwnJob: a team_update turn answers only the
263// job it is about (another job's is not_asked, and its card stays open);
264// another person's request is not_found; a turn the person started
265// answers any job of theirs.
266func TestContractTeamReplyOnlyOwnJob(t *testing.T) {
267 o := newTeamRig(t)
268 o.ask("Can we fix a bug in Tito? Login is broken, and the logo is blurry")
269 requests := map[string]string{}
270 for key, words := range map[string]string{"a": "Login is broken", "b": "the logo is blurry"} {
271 request, cmd := o.handoff("oj-"+key, words, "")
272 requests[key] = request
273 o.in(cmd.TurnID, func() {
274 if res := o.call("oj-ask-"+key, wire.ToolAskPerson, map[string]any{"question": "Which one?", "options": []string{"This", "That"}}); res.Code != "asked" {
275 t.Fatalf("ask %+v", res)
276 }
277 })
278 o.reply(cmd.TurnID)
279 }
280 o.reply(o.turn)
281 first := o.message(wire.LaneAssistant)
282 var mine, other string
283 for _, request := range requests {
284 if o.rows("SELECT count(*) FROM chat_handoff_turns WHERE handoff_id = ? AND turn_id = ?", request, first.TurnID) == 1 {
285 mine = request
286 } else {
287 other = request
288 }
289 }
290 if first.Kind != wire.TurnTeamUpdate || mine == "" || other == "" {
291 t.Fatalf("the first update %+v (%v)", first, requests)
292 }
293 _, _, revision := o.questionCard(other)
294 o.in(first.TurnID, func() {
295 res := o.teamReply("oj-other", other, "This")
296 if !res.IsError || res.Code != "not_asked" || res.Text != "This update is about another job. Answer or nudge only the job it names." {
297 t.Fatalf("another job's question %+v", res)
298 }
299 })
300 if state, _, rev := o.questionCard(other); state != wire.CardOpen || rev != revision {
301 t.Fatalf("the other job's card %s %d", state, rev)
302 }
303 o.reply(first.TurnID)
304 second := o.message(wire.LaneAssistant)
305 o.reply(second.TurnID)
306
307 mate := &teamRig{o.other("mate@example.com")}
308 mate.connect(o.base.Mint())
309 mate.ask("Pick This for that one")
310 if res := mate.teamReply("oj-mate", mine, "This"); !res.IsError || res.Code != "not_found" {
311 t.Fatalf("another person's request %+v", res)
312 }
313
314 o.ask("This for both")
315 for _, request := range []string{other, mine} {
316 replied(t, o.teamReply("oj-own-"+request, request, "This"), request)
317 }
318}
319
320// TestContractTeamReplyOnlyQuestions: team_reply answers only a
321// question: with only a go-ahead open for the job it is no_question, and
322// the go-ahead stays open, unmerged.
323func TestContractTeamReplyOnlyQuestions(t *testing.T) {
324 o := newTeamRig(t)
325 o.ask("Can we fix a bug in Tito? Login is broken")
326 request, cmd := o.handoff("oq-h", "Login is broken", "")
327 base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40)
328 var id string
329 o.in(cmd.TurnID, func() { id, _ = o.websiteReady("oqr", "Quick website", base, cand, false) })
330 outcome := o.assistantCard("the merge outcome", isMergeOutcome)
331 team := o.cardOf(id)
332 for i, answer := range []string{wire.ChoiceMerge, "Yes"} {
333 res := o.teamReply("oq-reply-"+itoa(i), request, answer)
334 if !res.IsError || res.Code != "no_question" {
335 t.Fatalf("team_reply %q on a go-ahead %+v", answer, res)
336 }
337 }
338 if got := o.cardOf(id); got.State != wire.CardOpen || got.Revision != team.Revision {
339 t.Fatalf("the go-ahead %+v", got)
340 }
341 if got := o.assistantCard("still open", func(c wire.AssistantCard) bool { return c.ID == outcome.ID }); got.State != wire.CardOpen || got.Revision != outcome.Revision {
342 t.Fatalf("the outcome %+v", got)
343 }
344 if n := o.rows("SELECT count(*) FROM chat_steps WHERE task_id = ? AND specialist = ?", id, wire.SpecialistMerge); n != 0 {
345 t.Fatal("merged on a team_reply")
346 }
347}
348
349// TestContractTeamReplyLegacy: a legacy hand-off's question, in the
350// person's own conversation, answered with team_reply: the answer goes
351// to lane "" with the question it answers, as slice 1's card answer did,
352// and its turn is the hand-off's.
353func TestContractTeamReplyLegacy(t *testing.T) {
354 o := newTeamRig(t)
355 o.ask("Can we fix a bug in Tito? Login is broken")
356 request, turn := o.legacyHandoff("lr", "Login is broken")
357 o.in(turn, func() {
358 if res := o.call("lr-ask", wire.ToolAskPerson, map[string]any{"question": loginQuestion, "options": []string{"Web", "App"}}); res.Code != "asked" {
359 t.Fatalf("ask %+v", res)
360 }
361 })
362 o.reply(turn)
363 replied(t, o.teamReply("lr-reply", request, "App"), request)
364 next := o.message(wire.LaneCoordinator)
365 if withoutProjectContext(t, next.Text) != `(My answer to your question "Which login page?") App` || o.laneOf(next.TurnID) != wire.LaneCoordinator {
366 t.Fatalf("the answer's frame %+v", next)
367 }
368 if n := o.rows("SELECT count(*) FROM chat_handoff_turns WHERE handoff_id = ? AND turn_id = ?", request, next.TurnID); n != 1 {
369 t.Fatal("the answer's turn is not the legacy hand-off's")
370 }
371 if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane LIKE 'job:%'"); n != 0 {
372 t.Fatalf("%d turns in a job thread", n)
373 }
374}
375
376// team_nudge (jobs design §4; D6, D7; rulings P1j, P2j, P4j): the
377// Assistant pushes one of the person's stopped jobs through: a job that
378// stopped before any work started gets the person's own words again, in
379// its thread; a failed step the instance can retry is tried again. Any
380// other stop is the person's to move on. At most two nudges per job
381// until the person says something newer about it.
382
383// nudgeReference is the marker a nudge's message is dispatched with when
384// the Assistant gave no context.
385const nudgeReference = "[Reference from the Assistant, not the person's words: this stopped before any work started; the person still wants it.]"
386
387// teamNudge is team_nudge for request in the rig's turn.
388func (o *teamRig) teamNudge(id, request, words string) wire.ToolResult {
389 o.t.Helper()
390 return o.use(id, "team_nudge", map[string]any{"request": request, "words": words})
391}
392
393// nudged fails unless res is team_nudge's answer for request with left
394// nudges.
395func nudged(t *testing.T, res wire.ToolResult, request string, left int) {
396 t.Helper()
397 want, _ := json.Marshal(map[string]any{"request": request, "status": "nudged", "nudges_left": left})
398 if res.IsError || res.Code != "used" || res.Text != string(want) {
399 t.Fatalf("team_nudge %+v, want %s", res, want)
400 }
401}
402
403// refusedWith fails unless res is the refusal code with text.
404func refusedWith(t *testing.T, res wire.ToolResult, code, text string) {
405 t.Helper()
406 if !res.IsError || res.Code != code || res.Text != text {
407 t.Fatalf("got %+v, want %s %q", res, code, text)
408 }
409}
410
411// nudges is the request's nudge count.
412func (o *teamRig) nudges(request string) int {
413 o.t.Helper()
414 return o.rows("SELECT nudges FROM chat_handoffs WHERE id = ?", request)
415}
416
417// nudgeKeys are the idempotency keys of the request's nudge messages,
418// oldest first.
419func (o *teamRig) nudgeKeys(request string) []string {
420 o.t.Helper()
421 rows, err := o.inst.DB.Writer().Query("SELECT idempotency_key FROM chat_messages WHERE idempotency_key LIKE ? ORDER BY seq", "handoff:"+request+":nudge:%")
422 if err != nil {
423 o.t.Fatal(err)
424 }
425 defer rows.Close()
426 var out []string
427 for rows.Next() {
428 var key string
429 if err := rows.Scan(&key); err != nil {
430 o.t.Fatal(err)
431 }
432 out = append(out, key)
433 }
434 if err := rows.Err(); err != nil {
435 o.t.Fatal(err)
436 }
437 return out
438}
439
440// stoppedJob hands "Build me a quick website" to the team from a new
441// Assistant turn; the thread's turn ends with no work and the Assistant's
442// turn ends. It returns the request and the team_update turn, dispatched,
443// that tells the Assistant to push it through.
444func (o *teamRig) stoppedJob(key string) (request, update string) {
445 o.t.Helper()
446 o.ask("Build me a quick website")
447 request, cmd := o.handoff(key+"-h", "Build me a quick website", "")
448 o.reply(cmd.TurnID)
449 o.noteFor("the no-work note", noWork(request, 2))
450 o.reply(o.turn)
451 u := o.message(wire.LaneAssistant)
452 if u.Kind != wire.TurnTeamUpdate || !strings.Contains(u.Text, noWork(request, 2)) {
453 o.t.Fatalf("update frame %+v", u)
454 }
455 return request, u.TurnID
456}
457
458// nudgeThrough nudges request with words in update, which leaves left
459// nudges; the update ends, and the thread's new turn ends with no work
460// again. It returns the next team_update, dispatched.
461func (o *teamRig) nudgeThrough(key, request, update, words string, left int) *wire.MessageCommand {
462 o.t.Helper()
463 var res wire.ToolResult
464 o.in(update, func() { res = o.teamNudge(key, request, words) })
465 nudged(o.t, res, request, left)
466 cmd := o.message(wire.JobLane(request))
467 o.reply(update)
468 o.reply(cmd.TurnID)
469 u := o.message(wire.LaneAssistant)
470 if u.Kind != wire.TurnTeamUpdate {
471 o.t.Fatalf("update frame %+v", u)
472 }
473 return u
474}
475
476// TestContractTeamNudgeNoWork: in the team_update about a job that
477// stopped before any work started, team_nudge quoting the person's
478// earlier words posts those words, as they wrote them, as the person's
479// message in the job's thread, keyed by the hand-off and the key the
480// call was invoked with, and dispatched with the default reference after
481// them; its turn is the hand-off's. One nudge left. The job card shows
482// working, never stopped, once the update ends. One audit row, without
483// the words.
484func TestContractTeamNudgeNoWork(t *testing.T) {
485 o := newTeamRig(t)
486 request, update := o.stoppedJob("nw")
487 card := o.jobCardID(request)
488 events := len(o.jobEvents(card))
489 audit := o.lastAudit()
490 var res wire.ToolResult
491 o.in(update, func() { res = o.teamNudge("nw-nudge", request, "quick website") })
492 nudged(t, res, request, 1)
493 var ledger string
494 if err := o.inst.DB.Writer().QueryRow("SELECT ledger_key FROM chat_tool_calls WHERE call_id = ?", update+"/1/nw-nudge").Scan(&ledger); err != nil || ledger == "" {
495 t.Fatalf("the call's ledger key %q %v", ledger, err)
496 }
497 cmd := o.message(wire.JobLane(request))
498 if withoutProjectContext(t, cmd.Text) != "quick website\n\n"+nudgeReference || len(cmd.PersonText) != 0 {
499 t.Fatalf("the nudge's frame %+v", cmd)
500 }
501 thread := o.thread(request)
502 if last := thread[len(thread)-1]; len(thread) != 3 || last.Role != wire.RolePerson || last.Text != "quick website" || last.TurnID != cmd.TurnID || last.TurnID == thread[0].TurnID {
503 t.Fatalf("the thread %+v", thread)
504 }
505 if keys := o.nudgeKeys(request); !slices.Equal(keys, []string{"handoff:" + request + ":nudge:" + assist.Key(ledger)}) {
506 t.Fatalf("the nudge's keys %q", keys)
507 }
508 if n := o.rows("SELECT count(*) FROM chat_handoff_turns WHERE handoff_id = ? AND turn_id = ?", request, cmd.TurnID); n != 1 {
509 t.Fatal("the nudge's turn is not the hand-off's")
510 }
511 if n := o.nudges(request); n != 1 {
512 t.Fatalf("%d nudges", n)
513 }
514 if n := o.rows("SELECT count(*) FROM audit_events WHERE id > ? AND kind = 'assistant.team_nudge' AND actor = ? AND record_id = ?", audit, Owner, request); n != 1 {
515 t.Fatalf("%d team_nudge audits", n)
516 }
517 if n := o.rows("SELECT count(*) FROM audit_events WHERE id > ? AND record_id LIKE '%website%'", audit); n != 0 {
518 t.Fatalf("%d audit rows carry the words", n)
519 }
520 o.reply(update)
521 o.jobOf(request, "working", func(c wire.AssistantCard) bool { return c.State == wire.JobWorking })
522 for _, c := range o.jobEvents(card)[events:] {
523 if c.State != wire.JobWorking {
524 t.Fatalf("the job card after the nudge %+v", c)
525 }
526 }
527}
528
529// TestContractTeamNudgeRetry: a failed Engineering step the instance can
530// retry gets a new attempt, as the person's Retry would place it, and
531// nothing is posted to the thread.
532func TestContractTeamNudgeRetry(t *testing.T) {
533 o := newTeamRig(t)
534 request, id, d := o.startedInThread("nr")
535 o.step("nr-fail", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true})
536 o.noteFor("the failed note", failedStep(request, 2))
537 thread := len(o.thread(request))
538 o.reply(o.turn)
539 u := o.message(wire.LaneAssistant)
540 // Only the person's own task is tried again.
541 o.exec("UPDATE chat_tasks SET creator = 'mate@example.com' WHERE id = ?", id)
542 var res wire.ToolResult
543 o.in(u.TurnID, func() { res = o.teamNudge("nr-theirs", request, "Login is broken") })
544 refusedWith(t, res, "needs_person", "Only the person can move that job on. Tell them plainly what is needed.")
545 if n := o.rows("SELECT count(*) FROM chat_steps WHERE id = ? AND attempt = 1", d.StepID); n != 1 || o.nudges(request) != 0 {
546 t.Fatalf("someone else's task was tried again (%d nudges)", o.nudges(request))
547 }
548 o.exec("UPDATE chat_tasks SET creator = ? WHERE id = ?", Owner, id)
549 o.in(u.TurnID, func() { res = o.teamNudge("nr-nudge", request, "Login is broken") })
550 nudged(t, res, request, 1)
551 _, body := o.expect(wire.FrameDispatch)
552 if again := body.(*wire.DispatchCommand); again.StepID != d.StepID || again.TaskID != id || again.Attempt != 2 || again.Specialist != wire.SpecialistEngineering {
553 t.Fatalf("the retry's dispatch %+v", again)
554 }
555 if n := o.rows("SELECT count(*) FROM chat_steps WHERE task_id = ?", id); n != 1 {
556 t.Fatalf("%d steps", n)
557 }
558 if n := len(o.thread(request)); n != thread || len(o.nudgeKeys(request)) != 0 {
559 t.Fatalf("%d thread messages, %d before", n, thread)
560 }
561 if n := o.rows("SELECT count(*) FROM chat_handoff_actions WHERE handoff_id = ? AND kind = 'nudge' AND coalesce(message_id, '') = ''", request); n != 1 || o.nudges(request) != 1 {
562 t.Fatalf("%d nudge actions, %d nudges", n, o.nudges(request))
563 }
564 o.reply(u.TurnID)
565 o.jobOf(request, "working", func(c wire.AssistantCard) bool { return c.State == wire.JobWorking })
566}
567
568// refusesNudge: in the team_update the rig's turn ending dispatches,
569// team_nudge on request is needs_person and changes nothing: no step
570// placed or tried again, no message, no nudge counted. Once the update
571// ends the job is stopped.
572func (o *teamRig) refusesNudge(request, words string) {
573 o.t.Helper()
574 o.reply(o.turn)
575 u := o.message(wire.LaneAssistant)
576 if u.Kind != wire.TurnTeamUpdate {
577 o.t.Fatalf("update frame %+v", u)
578 }
579 steps := o.rows("SELECT count(*) + coalesce(sum(attempt), 0) FROM chat_steps")
580 messages := o.rows("SELECT count(*) FROM chat_messages WHERE role = ?", wire.RolePerson)
581 var res wire.ToolResult
582 o.in(u.TurnID, func() { res = o.teamNudge("pn-nudge", request, words) })
583 refusedWith(o.t, res, "needs_person", "Only the person can move that job on. Tell them plainly what is needed.")
584 if n := o.rows("SELECT count(*) + coalesce(sum(attempt), 0) FROM chat_steps"); n != steps {
585 o.t.Fatal("a step was placed")
586 }
587 if n := o.rows("SELECT count(*) FROM chat_messages WHERE role = ?", wire.RolePerson); n != messages {
588 o.t.Fatalf("%d person messages, %d before", n, messages)
589 }
590 if n := o.nudges(request); n != 0 || o.rows("SELECT count(*) FROM chat_handoff_actions") != 0 {
591 o.t.Fatalf("%d nudges", n)
592 }
593 o.reply(u.TurnID)
594 o.jobOf(request, "stopped", func(c wire.AssistantCard) bool { return c.State == wire.JobStopped })
595}
596
597// TestContractTeamNudgePersonNeeds (D6, ruling P4j): a job stopped on a
598// need only the person can meet is needs_person, with no fallback nudge:
599// each need a task reports (a failed merge step among them), a failed
600// publish step, and an open access card. A job still working is
601// not_stopped.
602func TestContractTeamNudgePersonNeeds(t *testing.T) {
603 for _, c := range personNeeds() {
604 t.Run(c.name, func(t *testing.T) {
605 o := newTeamRigAt(t, wire.ProtocolCurrent)
606 request, id, d := o.startedInThread("pn")
607 c.set(o, id, d.StepID)
608 if _, err := o.client.PostThread(o.ctx, id, "For the record.", "pn-note"); err != nil {
609 t.Fatal(err)
610 }
611 o.noteFor("the "+c.name+" note", blockedOn(c.line))
612 o.refusesNudge(request, "Login is broken")
613 })
614 }
615 // runner_offline is a need no task of the instance's reports now
616 // (needsYou never returns it); like every need that is neither
617 // no_work nor failed, stopReason makes it blocked, and blocked is
618 // needs_person.
619 t.Run("publish_failed", func(t *testing.T) {
620 o := newTeamRigAt(t, wire.ProtocolCurrent)
621 id, _, _, _ := o.mergedWebsite("pf", true)
622 request := o.requestOf(id)
623 var ships string
624 var revision int64
625 if err := o.inst.DB.Writer().QueryRow("SELECT id, revision FROM chat_cards WHERE kind = ? AND state = ?", wire.CardMergeShips, wire.CardOpen).Scan(&ships, &revision); err != nil {
626 t.Fatal(err)
627 }
628 if _, err := o.client.Answer(o.ctx, ships, wire.ChoiceNotShips, revision); err != nil {
629 t.Fatal(err)
630 }
631 card := o.assistantCard("the publish outcome", isPublishOutcome)
632 if _, err := o.a.Answer(o.ctx, card.ID, wire.ChoicePublish, card.Revision); err != nil {
633 t.Fatal(err)
634 }
635 o.publishReport("pf-failed", o.publishDispatch(), wire.StepFailed, nil)
636 o.noteFor("the publish failure's note", blockedOn("going live failed, and they need to try it again"))
637 // The Publish asked again is ready while it is open; Not now
638 // leaves the failed go-live.
639 again := o.assistantCard("the Publish asked again", func(c wire.AssistantCard) bool {
640 return isPublishOutcome(c) && c.ID != card.ID && c.State == wire.CardOpen
641 })
642 if _, err := o.a.Answer(o.ctx, again.ID, wire.ChoiceNotNow, again.Revision); err != nil {
643 t.Fatal(err)
644 }
645 o.refusesNudge(request, "quick website")
646 })
647 t.Run("access", func(t *testing.T) {
648 o := newTeamRigAt(t, wire.ProtocolCurrent)
649 o.ask("Can we fix a bug in Tito? Login is broken")
650 request, cmd := o.handoff("pa1", "Login is broken", "")
651 o.in(cmd.TurnID, func() {
652 if res := o.call("pa-find", wire.ToolFind, map[string]string{"query": "moonbeam/app"}); res.Code != "none" {
653 t.Fatalf("find %+v", res)
654 }
655 })
656 o.reply(cmd.TurnID)
657 o.noteFor("the access card's note", blockedOn("they need to give access to where the work lives"))
658 o.refusesNudge(request, "Login is broken")
659 })
660 t.Run("working", func(t *testing.T) {
661 o := newTeamRigAt(t, wire.ProtocolCurrent)
662 request, _, _ := o.startedInThread("pw")
663 refusedWith(t, o.teamNudge("pw-nudge", request, "Login is broken"), "not_stopped", "That job is not stopped. Nothing needs pushing.")
664 if n := o.nudges(request); n != 0 {
665 t.Fatalf("%d nudges", n)
666 }
667 })
668}
669
670// TestContractTeamNudgeCap (D7): two nudges from team_update turns pass;
671// the update after the second tells the Assistant no nudges are left,
672// and a third nudge there is nudge_limit, posting nothing.
673func TestContractTeamNudgeCap(t *testing.T) {
674 o := newTeamRig(t)
675 request, update := o.stoppedJob("nc")
676 u := o.nudgeThrough("nc-1", request, update, "quick website", 1)
677 if !strings.Contains(u.Text, noWork(request, 1)) {
678 t.Fatalf("the second update %q", u.Text)
679 }
680 u = o.nudgeThrough("nc-2", request, u.TurnID, "quick website", 0)
681 if !strings.Contains(u.Text, "It stopped again and no nudges are left.") || strings.Contains(u.Text, "team_nudge") {
682 t.Fatalf("the third update %q", u.Text)
683 }
684 messages := len(o.thread(request))
685 o.in(u.TurnID, func() {
686 refusedWith(t, o.teamNudge("nc-3", request, "quick website"), "nudge_limit", "That job has been nudged twice already. Tell the person in one line that it stopped.")
687 })
688 if n := len(o.thread(request)); n != messages || o.nudges(request) != 2 {
689 t.Fatalf("%d thread messages, %d before; %d nudges", n, messages, o.nudges(request))
690 }
691}
692
693// TestContractTeamNudgeCapResetsOnlyOnNewerWords (ruling P1j): after two
694// nudges, an unrelated new message from the person resets nothing: a
695// nudge in its turn quoting their older words is still nudge_limit. A
696// nudge quoting a message sent after the last nudge passes, with one
697// nudge left.
698func TestContractTeamNudgeCapResetsOnlyOnNewerWords(t *testing.T) {
699 o := newTeamRig(t)
700 request, update := o.stoppedJob("cr")
701 u := o.nudgeThrough("cr-1", request, update, "quick website", 1)
702 u = o.nudgeThrough("cr-2", request, u.TurnID, "quick website", 0)
703 o.reply(u.TurnID)
704 o.ask("What is on my calendar today?")
705 refusedWith(t, o.teamNudge("cr-old", request, "Build me a quick website"), "nudge_limit", "That job has been nudged twice already. Tell the person in one line that it stopped.")
706 o.reply(o.turn)
707 o.ask("I still want that quick website, please")
708 nudged(t, o.teamNudge("cr-new", request, "still want that QUICK website"), request, 1)
709 if thread := o.thread(request); thread[len(thread)-1].Text != "still want that quick website" {
710 t.Fatalf("the thread %+v", thread)
711 }
712}
713
714// TestContractTeamNudgeKeyAfterReset (ruling P2j): two nudges, a reset by
715// newer words, then a third and a fourth nudge: each posts its own
716// message, under four distinct keys, none of them a counter.
717func TestContractTeamNudgeKeyAfterReset(t *testing.T) {
718 o := newTeamRig(t)
719 request, update := o.stoppedJob("ka")
720 u := o.nudgeThrough("ka-1", request, update, "quick website", 1)
721 u = o.nudgeThrough("ka-2", request, u.TurnID, "quick website", 0)
722 o.reply(u.TurnID)
723 o.ask("I still want that quick website, please")
724 nudged(t, o.teamNudge("ka-3", request, "still want that quick website"), request, 1)
725 cmd := o.message(wire.JobLane(request))
726 o.reply(o.turn)
727 o.reply(cmd.TurnID)
728 u = o.message(wire.LaneAssistant)
729 if u.Kind != wire.TurnTeamUpdate || !strings.Contains(u.Text, noWork(request, 1)) {
730 t.Fatalf("the update after the reset %+v", u)
731 }
732 o.in(u.TurnID, func() { nudged(t, o.teamNudge("ka-4", request, "still want that quick website"), request, 0) })
733 o.message(wire.JobLane(request))
734 keys := o.nudgeKeys(request)
735 seen := map[string]bool{}
736 for _, key := range keys {
737 if !uuidV5.MatchString(strings.TrimPrefix(key, "handoff:"+request+":nudge:")) {
738 t.Fatalf("key %q", key)
739 }
740 seen[key] = true
741 }
742 if len(keys) != 4 || len(seen) != 4 {
743 t.Fatalf("nudge keys %q", keys)
744 }
745 if n := o.rows("SELECT count(*) FROM chat_messages WHERE lane = ? AND role = ? AND idempotency_key LIKE 'handoff:%:nudge:%'", wire.JobLane(request), wire.RolePerson); n != 4 {
746 t.Fatalf("%d nudge messages", n)
747 }
748}
749
750// TestContractTeamNudgeWords: words found in no message the person sent
751// the Assistant (one in their own Equipo conversation among them), or
752// too short to quote, are not_their_words, and nothing is nudged.
753func TestContractTeamNudgeWords(t *testing.T) {
754 o := newTeamRig(t)
755 request, update := o.stoppedJob("tw")
756 o.own("Make it purple")
757 messages := len(o.thread(request))
758 o.in(update, func() {
759 for i, words := range []string{"make it purple", "make it blue", "qu"} {
760 refusedWith(t, o.teamNudge("tw-"+itoa(i), request, words), "not_their_words", "Not nudged: words must quote something the person said to you in this conversation, word for word, and be more than a couple of letters. Quote them; put anything of yours in context.")
761 }
762 })
763 if n := len(o.thread(request)); n != messages || o.nudges(request) != 0 {
764 t.Fatalf("%d thread messages, %d before; %d nudges", n, messages, o.nudges(request))
765 }
766}
767
768// TestContractTeamNudgeOnce: the same nudge under a new call id, then a
769// result lost before it was stored, in attempt 2: one message, one
770// nudge counted, one action, one audit, equal results. The Assistant's
771// context rides after the words as reference, as kept.
772func TestContractTeamNudgeOnce(t *testing.T) {
773 o := newTeamRig(t)
774 request, update := o.stoppedJob("on")
775 in := map[string]any{"request": request, "words": "quick website", "context": "A portfolio [with photos]"}
776 var first, again, lost wire.ToolResult
777 o.in(update, func() {
778 first = o.use("on-1", "team_nudge", in)
779 again = o.use("on-2", "team_nudge", in)
780 })
781 nudged(t, first, request, 1)
782 if again.IsError || !again.Replayed || again.Text != first.Text {
783 t.Fatalf("again %+v, first %+v", again, first)
784 }
785 if cmd := o.message(wire.JobLane(request)); withoutProjectContext(t, cmd.Text) != "quick website\n\n[Reference from the Assistant, not the person's words: A portfolio (with photos)]" {
786 t.Fatalf("the nudge's frame %+v", cmd)
787 }
788 o.runner.Send(o.ctx, "r-on-wait", wire.FrameTurn, wire.TurnReport{TurnID: update, Attempt: 1, State: wire.TurnWaiting, Reason: "limited"})
789 o.runner.Send(o.ctx, "r-on-run2", wire.FrameTurn, wire.TurnReport{TurnID: update, Attempt: 2, State: wire.TurnRunning})
790 o.until("attempt 2", func() bool {
791 return o.rows("SELECT count(*) FROM chat_turns WHERE id = ? AND attempt = 2 AND state = ?", update, wire.TurnRunning) == 1
792 })
793 // The ledger lost the result: the provider gets the same key again.
794 o.exec("UPDATE chat_tool_calls SET ledger_key = '' WHERE turn_id = ?", update)
795 o.in(update, func() {
796 o.attempt = 2
797 lost = o.use("on-3", "team_nudge", in)
798 })
799 if lost.IsError || lost.Text != first.Text {
800 t.Fatalf("after a lost result %+v, first %+v", lost, first)
801 }
802 if keys := o.nudgeKeys(request); len(keys) != 1 || o.nudges(request) != 1 {
803 t.Fatalf("nudge messages %q, %d nudges", keys, o.nudges(request))
804 }
805 if n := o.rows("SELECT count(*) FROM chat_handoff_actions WHERE handoff_id = ? AND kind = 'nudge' AND context = 'A portfolio (with photos)'", request); n != 1 {
806 t.Fatalf("%d nudge actions", n)
807 }
808 if n := o.rows("SELECT count(*) FROM audit_events WHERE kind = 'assistant.team_nudge'"); n != 1 {
809 t.Fatalf("%d team_nudge audits", n)
810 }
811}
812
813// TestContractTeamNudgeLegacy (ruling P3j): a legacy hand-off that
814// stopped with no work is nudged in the person's own conversation (lane
815// ""), as slice 1's hand-off message was, its turn linked to it; no job
816// thread is made.
817func TestContractTeamNudgeLegacy(t *testing.T) {
818 o := newTeamRig(t)
819 o.ask("Build me a quick website")
820 request, turn := o.legacyHandoff("ln", "Build me a quick website")
821 o.reply(turn)
822 o.noteFor("the legacy no-work note", noWork(request, 2))
823 o.reply(o.turn)
824 u := o.message(wire.LaneAssistant)
825 if u.Kind != wire.TurnTeamUpdate {
826 t.Fatalf("update frame %+v", u)
827 }
828 o.in(u.TurnID, func() { nudged(t, o.teamNudge("ln-nudge", request, "quick website"), request, 1) })
829 next := o.message(wire.LaneCoordinator)
830 if withoutProjectContext(t, next.Text) != "quick website\n\n"+nudgeReference || o.laneOf(next.TurnID) != wire.LaneCoordinator {
831 t.Fatalf("the nudge's frame %+v", next)
832 }
833 if n := o.rows("SELECT count(*) FROM chat_handoff_turns WHERE handoff_id = ? AND turn_id = ?", request, next.TurnID); n != 1 {
834 t.Fatal("the nudge's turn is not the legacy hand-off's")
835 }
836 if keys := o.nudgeKeys(request); len(keys) != 1 || o.rows("SELECT count(*) FROM chat_messages WHERE idempotency_key = ? AND coalesce(lane, '') = ''", keys[0]) != 1 {
837 t.Fatalf("nudge messages %q", keys)
838 }
839 if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane LIKE 'job:%'"); n != 0 {
840 t.Fatalf("%d turns in a job thread", n)
841 }
842}
843
844// TestContractTeamNudgeOnlyOwnJob: a team_update turn nudges only the job
845// it is about (another job's is not_asked); an unknown request is
846// not_found; a request the person cancelled is turn_ended; a job thread
847// the person's runner is too old to carry is runner_too_old. None of
848// them posts anything or counts a nudge.
849func TestContractTeamNudgeOnlyOwnJob(t *testing.T) {
850 o := newTeamRig(t)
851 o.ask("Build me a quick website, and the logo is blurry")
852 requests := map[string]bool{}
853 for key, words := range map[string]string{"a": "Build me a quick website", "b": "the logo is blurry"} {
854 request, cmd := o.handoff("oj-"+key, words, "")
855 requests[request] = true
856 o.reply(cmd.TurnID)
857 }
858 o.reply(o.turn)
859 u := o.message(wire.LaneAssistant)
860 var mine, other string
861 for request := range requests {
862 if o.rows("SELECT count(*) FROM chat_handoff_turns WHERE handoff_id = ? AND turn_id = ?", request, u.TurnID) == 1 {
863 mine = request
864 } else {
865 other = request
866 }
867 }
868 if u.Kind != wire.TurnTeamUpdate || mine == "" || other == "" {
869 t.Fatalf("the first update %+v (%v)", u, requests)
870 }
871 messages := o.rows("SELECT count(*) FROM chat_messages WHERE role = ?", wire.RolePerson)
872 o.in(u.TurnID, func() {
873 refusedWith(t, o.teamNudge("oj-other", other, "the logo is blurry"), "not_asked", "This update is about another job. Answer or nudge only the job it names.")
874 if res := o.teamNudge("oj-none", "h_nothere", "the logo is blurry"); !res.IsError || res.Code != "not_found" {
875 t.Fatalf("an unknown request %+v", res)
876 }
877 o.exec("UPDATE runner_sessions SET protocol = ?", wire.Protocol21)
878 if res := o.teamNudge("oj-old", mine, "quick website"); !res.IsError || res.Code != "runner_too_old" {
879 t.Fatalf("a runner too old for job threads %+v", res)
880 }
881 o.exec("UPDATE runner_sessions SET protocol = ?", wire.ProtocolJobs)
882 o.exec("UPDATE chat_handoffs SET state = 'cancelled' WHERE id = ?", mine)
883 if res := o.teamNudge("oj-cancelled", mine, "quick website"); !res.IsError || res.Code != "turn_ended" {
884 t.Fatalf("a cancelled request %+v", res)
885 }
886 })
887 if n := o.rows("SELECT count(*) FROM chat_messages WHERE role = ?", wire.RolePerson); n != messages || o.rows("SELECT sum(nudges) FROM chat_handoffs") != 0 {
888 t.Fatalf("%d person messages, %d before", n, messages)
889 }
890}
891
892// TestContractTeamReplyQueued: an answer given while the job's thread
893// still has a turn running waits behind it, and is dispatched with the
894// question it answers and nothing of a nudge's.
895func TestContractTeamReplyQueued(t *testing.T) {
896 o := newTeamRig(t)
897 o.ask("Can we fix a bug in Tito? Login is broken")
898 request, cmd := o.handoff("rq-h", "Login is broken", "")
899 o.in(cmd.TurnID, func() {
900 if res := o.call("rq-ask", wire.ToolAskPerson, map[string]any{"question": loginQuestion, "options": []string{"Web", "App"}}); res.Code != "asked" {
901 t.Fatalf("ask %+v", res)
902 }
903 })
904 replied(t, o.teamReply("rq-reply", request, "Web"), request)
905 o.reply(cmd.TurnID)
906 if next := o.message(wire.JobLane(request)); withoutProjectContext(t, next.Text) != `(My answer to your question "Which login page?") Web` {
907 t.Fatalf("the answer's frame %+v", next)
908 }
909}
910
911// TestContractTeamNudgeToldAgain: a nudge clears what the Assistant was
912// last told of the job: after a reset by newer words, the same note (one
913// nudge left) is told again when the job stops again.
914func TestContractTeamNudgeToldAgain(t *testing.T) {
915 o := newTeamRig(t)
916 request, update := o.stoppedJob("ta")
917 u := o.nudgeThrough("ta-1", request, update, "quick website", 1)
918 if !strings.Contains(u.Text, noWork(request, 1)) {
919 t.Fatalf("the second update %q", u.Text)
920 }
921 o.reply(u.TurnID)
922 o.ask("I still want that quick website, please")
923 nudged(t, o.teamNudge("ta-2", request, "still want that quick website"), request, 1)
924 cmd := o.message(wire.JobLane(request))
925 o.reply(o.turn)
926 o.reply(cmd.TurnID)
927 if again := o.message(wire.LaneAssistant); again.Kind != wire.TurnTeamUpdate || again.Text != u.Text {
928 t.Fatalf("told again %+v", again)
929 }
930}
931
932// TestContractTeamNudgeRetryOne (ruling R22): one nudge tries again
933// exactly one task: the hand-off's earliest one a failed step the
934// instance can retry holds. With two failed, the first nudge retries
935// only the first; once the first is done, a second nudge retries the
936// second.
937func TestContractTeamNudgeRetryOne(t *testing.T) {
938 o := newTeamRig(t)
939 request, first, d1 := o.startedInThread("n1")
940 var second string
941 var d2 *wire.DispatchCommand
942 o.in(o.handoffTurn(request), func() {
943 second = o.record("n1-rec2", "Fix the logo")
944 if res := o.call("n1-start2", wire.ToolStartTask, map[string]string{"task": second, "brief": "Fix it."}); res.IsError {
945 t.Fatalf("start %+v", res)
946 }
947 _, body := o.expect(wire.FrameDispatch)
948 d2 = body.(*wire.DispatchCommand)
949 })
950 o.step("n1-fail1", wire.StepReport{TaskID: first, StepID: d1.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true})
951 o.step("n1-fail2", wire.StepReport{TaskID: second, StepID: d2.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true})
952 o.noteFor("the failed note", failedStep(request, 2))
953 o.reply(o.turn)
954 u := o.message(wire.LaneAssistant)
955 attempts := func(want1, want2 int) {
956 t.Helper()
957 if o.rows("SELECT attempt FROM chat_steps WHERE id = ?", d1.StepID) != want1 || o.rows("SELECT attempt FROM chat_steps WHERE id = ?", d2.StepID) != want2 {
958 t.Fatalf("attempts %d and %d, want %d and %d", o.rows("SELECT attempt FROM chat_steps WHERE id = ?", d1.StepID), o.rows("SELECT attempt FROM chat_steps WHERE id = ?", d2.StepID), want1, want2)
959 }
960 }
961 o.in(u.TurnID, func() { nudged(t, o.teamNudge("n1-nudge1", request, "Login is broken"), request, 1) })
962 attempts(2, 1)
963 o.reply(u.TurnID)
964 // The first is done; the second still waits on its failed step.
965 o.exec("UPDATE chat_steps SET state = ?, terminated = 1 WHERE id = ?", wire.StepDone, d1.StepID)
966 o.exec("UPDATE chat_tasks SET stage = ? WHERE id = ?", wire.StageDone, first)
967 o.ask("Try the logo again please")
968 nudged(t, o.teamNudge("n1-nudge2", request, "Login is broken"), request, 0)
969 attempts(2, 2)
970}
971