package thintest import ( "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // Words a failed turn carries into the next (dispatch's unanswered): // only a plain message the coordinator never saw, and in the turn that // carries them they are the person's words for team_request too. // failTurn reports id failed at attempt 1 and waits for it to end. func (r *rig) failTurn(runner *FakeRunner, id string) { r.t.Helper() if err := runner.Send(r.ctx, "e-"+id, wire.FrameTurn, wire.TurnReport{TurnID: id, Attempt: 1, State: wire.TurnFailed, Reason: "policy_rejected"}); err != nil { r.t.Fatal(err) } for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) { var state string if err := r.inst.DB.Writer().QueryRow("SELECT state FROM chat_turns WHERE id = ?", id).Scan(&state); err == nil && state == wire.TurnFailed { return } } r.t.Fatalf("turn %s did not fail", id) } // A turn that reached the model (a delta or a thinking or tool activity // was recorded for it) is not carried: its words may already be in a // resumed session's transcript. One that failed before is. func TestContractCarriedOnlyBeforeTheModel(t *testing.T) { r := newRig(t) runner := r.dial(0) turn := func(text, key string) (wire.Sent, *wire.MessageCommand) { t.Helper() sent, err := r.client.Send(r.ctx, text, key) if err != nil { t.Fatal(err) } f, body, err := runner.Expect(r.ctx, wire.FrameMessage) if err != nil { t.Fatal(err) } runner.Send(r.ctx, "a-"+key, wire.FrameAck, wire.Ack{ID: f.ID}) r.wait("running", func(c *Conversation) bool { return c.Turns[sent.Turn.ID].State == wire.TurnRunning }) return sent, body.(*wire.MessageCommand) } // Frames apply in order: each report lands before the failure after it. streamed, _ := turn("streamed words", "k1") runner.Send(r.ctx, "d1", wire.FrameDelta, wire.DeltaReport{TurnID: streamed.Turn.ID, Attempt: 1, Text: "Looking"}) r.failTurn(runner, streamed.Turn.ID) thought, cmd := turn("thought words", "k2") if slices.Contains(cmd.PersonText, "streamed words") { t.Fatalf("a turn that streamed was carried %q", cmd.PersonText) } runner.Send(r.ctx, "t2", wire.FrameActivity, wire.ActivityReport{TurnID: thought.Turn.ID, Attempt: 1, Kind: wire.ActivityThink}) r.failTurn(runner, thought.Turn.ID) early, cmd := turn("early words", "k3") if len(cmd.PersonText) != 1 || strings.Contains(cmd.Text, "Not yet answered") { t.Fatalf("a turn that reached the model was carried %q", cmd.PersonText) } r.failTurn(runner, early.Turn.ID) last, cmd := turn("last", "k4") if strings.Join(cmd.PersonText, "|") != "early words|last" { t.Fatalf("a turn that failed before the model was not carried %q", cmd.PersonText) } // The turn that carried them reached the model: they are in its // transcript now, and ride no further. runner.Send(r.ctx, "d4", wire.FrameDelta, wire.DeltaReport{TurnID: last.Turn.ID, Attempt: 1, Text: "On it"}) r.failTurn(runner, last.Turn.ID) if _, cmd = turn("final", "k5"); len(cmd.PersonText) != 1 { t.Fatalf("carried past a turn that reached the model %q", cmd.PersonText) } } // A typed answer to a card is not carried as a plain message: bare, // "the second one" means nothing without the question it answers. func TestContractCarriedSkipsCardAnswers(t *testing.T) { r := newToolRig(t) if res := r.call("q1", wire.ToolAskPerson, map[string]any{"question": "Which one do you mean?"}); res.Code != "asked" { t.Fatalf("ask_person %+v", res) } r.runner.Send(r.ctx, "rep1", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Which day suits?"}) var card wire.Card r.wait("question card", func(c *Conversation) bool { for _, k := range c.Cards { if k.Kind == wire.CardQuestion && k.State == wire.CardOpen { card = k return true } } return false }) answered, err := r.client.Answer(r.ctx, card.ID, "the second one", card.Revision) if err != nil { t.Fatal(err) } f, _, err := r.runner.Expect(r.ctx, wire.FrameMessage) if err != nil { t.Fatal(err) } r.runner.Send(r.ctx, "a-ans", wire.FrameAck, wire.Ack{ID: f.ID}) r.failTurn(r.runner, answered.Turn.ID) if _, err = r.client.Send(r.ctx, "next", "k-next"); err != nil { t.Fatal(err) } _, body, err := r.runner.Expect(r.ctx, wire.FrameMessage) if err != nil { t.Fatal(err) } if cmd := body.(*wire.MessageCommand); len(cmd.PersonText) != 1 || strings.Contains(cmd.Text, "Not yet answered") { t.Fatalf("a card answer was carried bare %q %q", cmd.PersonText, cmd.Text) } } // Words a failed Assistant turn left unanswered ride in the next turn, // and there team_request takes them as the person's: the hand-off is // their message, as written. Words from no message of theirs still are // not theirs. func TestContractCarriedWordsHandOver(t *testing.T) { o := newTeamRig(t) lost := o.ask("Can we fix a bug in Tito? Login is broken") o.failTurn(o.runner, lost) o.ask("Please continue") if res := o.use("tr-not", "team_request", map[string]any{"words": "The checkout page is broken"}); !res.IsError || res.Code != "not_their_words" { t.Fatalf("words from no message %+v", res) } request, cmd := o.handoff("tr1", "Login is broken", "") if !strings.HasPrefix(cmd.Text, "Login is broken") { t.Fatalf("thread frame %q", cmd.Text) } if thread := o.thread(request); len(thread) != 1 || thread[0].Text != "Login is broken" || thread[0].Role != wire.RolePerson { t.Fatalf("thread %+v", thread) } }