| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "database/sql" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | func i64(v int64) *int64 { return &v } |
| 14 | |
| 15 | func held(conns ...wire.HeldConnection) wire.Hello { |
| 16 | return wire.Hello{Repos: []string{}, CreateIn: []string{}, Catalog: []wire.RepoInfo{}, Connections: conns, Versions: map[string]string{"runner": "test"}} |
| 17 | } |
| 18 | |
| 19 | // TestContractConnections: the runner's hello records the person's own |
| 20 | // connections (never a teammate's); an attempt started in the app goes to |
| 21 | // the runner as a durable connect frame, shows the provider's device |
| 22 | // code, turns healthy with the runner's name for it and is not |
| 23 | // duplicated by the next hello; cancel goes to the runner; a connection |
| 24 | // the runner no longer holds is signed out. Every document matches the |
| 25 | // shared fixtures, and no secret-shaped field is ever stored. |
| 26 | func TestContractConnections(t *testing.T) { |
| 27 | r := newRig(t) |
| 28 | runner := r.dial(0) |
| 29 | r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) |
| 30 | reset := time.Date(2026, 11, 10, 10, 0, 0, 0, time.UTC) |
| 31 | one := 1.0 |
| 32 | hello := held(wire.HeldConnection{LocalID: "claude-a", Person: Owner, Provider: "claude", Kind: "subscription", Label: Owner, State: wire.ConnLimited, Headroom: &wire.Headroom{Utilization: &one, ResetsAt: &reset, Windows: []wire.Window{{Name: "seven_day", UsedPercent: 100, Minutes: 10080, ResetsAt: &reset}}}}, |
| 33 | wire.HeldConnection{LocalID: "claude-b", Person: "teammate@example.com", Provider: "claude", Kind: "subscription", Label: "teammate@example.com", State: wire.ConnHealthy}) |
| 34 | hello.Generation = runner.Welcome.Generation |
| 35 | if err := runner.Send(r.ctx, "h1", wire.FrameHello, hello); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | r.wait("claude-a recorded", func(c *Conversation) bool { return len(c.Connections) == 1 }) |
| 39 | list, err := r.client.Connections(r.ctx) |
| 40 | if err != nil || len(list.Items) != 1 || list.Items[0].LocalID != "claude-a" || list.Items[0].State != wire.ConnLimited { |
| 41 | t.Fatalf("connections %+v %v", list, err) |
| 42 | } |
| 43 | |
| 44 | attempt, err := r.client.Connect(r.ctx, wire.Connect{Provider: "codex", Kind: "subscription"}) |
| 45 | if err != nil || attempt.State != wire.ConnConnecting || attempt.LocalID != "" { |
| 46 | t.Fatalf("connect %+v %v", attempt, err) |
| 47 | } |
| 48 | f, body, err := runner.Expect(r.ctx, wire.FrameConnect) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | shape(t, "frames/connect.json", runner.Received[len(runner.Received)-1]) |
| 53 | cmd := body.(*wire.ConnectCommand) |
| 54 | if cmd.Connection != attempt.ID || cmd.Person != Owner || cmd.Provider != "codex" || cmd.SameMachine { |
| 55 | t.Fatalf("connect frame %+v", cmd) |
| 56 | } |
| 57 | if err = runner.Send(r.ctx, "r-ack-c", wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | // A second Add while one is connecting is the same attempt. |
| 61 | again, err := r.client.Connect(r.ctx, wire.Connect{Provider: "codex"}) |
| 62 | if err != nil || again.ID != attempt.ID { |
| 63 | t.Fatalf("second attempt %+v %v", again, err) |
| 64 | } |
| 65 | report := wire.ConnectionReport{Connection: attempt.ID, Person: Owner, Provider: "codex", State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptDeviceCode, URL: "https://auth.openai.com/codex/device", Code: "ABCD-1234"}} |
| 66 | if err = runner.Send(r.ctx, "r-conn-1", wire.FrameConnection, report); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | r.wait("device code shown", func(c *Conversation) bool { |
| 70 | p := c.Connections[attempt.ID].Prompt |
| 71 | return p != nil && p.Code == "ABCD-1234" |
| 72 | }) |
| 73 | got, err := r.client.Connection(r.ctx, attempt.ID) |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | shape(t, "api/connection-connecting-codex.json", got) |
| 78 | |
| 79 | done := wire.ConnectionReport{Connection: attempt.ID, Person: Owner, LocalID: "codex-1", Provider: "codex", State: wire.ConnHealthy, Label: Owner, Plan: "pro", Headroom: &wire.Headroom{Windows: []wire.Window{}}} |
| 80 | // The hello can arrive before the attempt's report: it is not a second |
| 81 | // connection. |
| 82 | hello.Connections = append(hello.Connections, wire.HeldConnection{LocalID: "codex-1", Person: Owner, Provider: "codex", Kind: "subscription", Label: Owner, State: wire.ConnHealthy, Plan: "pro"}) |
| 83 | if err = runner.Send(r.ctx, "h2", wire.FrameHello, hello); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if err = runner.Send(r.ctx, "r-conn-2", wire.FrameConnection, done); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | shape(t, "frames/connection.json", runner.Sent[len(runner.Sent)-1]) |
| 90 | r.wait("codex healthy", func(c *Conversation) bool { |
| 91 | v := c.Connections[attempt.ID] |
| 92 | return v.State == wire.ConnHealthy && v.LocalID == "codex-1" && v.Prompt == nil |
| 93 | }) |
| 94 | if err = runner.Send(r.ctx, "h3", wire.FrameHello, hello); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | time.Sleep(200 * time.Millisecond) |
| 98 | list, _ = r.client.Connections(r.ctx) |
| 99 | if len(list.Items) != 2 { |
| 100 | t.Fatalf("after the hello: %+v", list.Items) |
| 101 | } |
| 102 | shape(t, "api/connections.json", list) |
| 103 | // A late frame for an ended attempt changes nothing. |
| 104 | if err = runner.Send(r.ctx, "r-conn-3", wire.FrameConnection, report); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | if v, _ := r.client.Connection(r.ctx, attempt.ID); v.State != wire.ConnHealthy { |
| 108 | t.Fatalf("late frame reopened the attempt: %+v", v) |
| 109 | } |
| 110 | |
| 111 | // Claude, browser elsewhere: the sign-in by a link, starting on the |
| 112 | // runner (no link yet). |
| 113 | claude, err := r.client.Connect(r.ctx, wire.Connect{Provider: "claude"}) |
| 114 | if err != nil || claude.Prompt == nil || claude.Prompt.Kind != wire.PromptAuthorize || claude.Prompt.URL != "" || claude.Prompt.Attempt != 0 || claude.Kind != wire.KindSubscription { |
| 115 | t.Fatalf("claude attempt %+v %v", claude, err) |
| 116 | } |
| 117 | if _, body, err = runner.Expect(r.ctx, wire.FrameConnect); err != nil || body.(*wire.ConnectCommand).SameMachine || body.(*wire.ConnectCommand).Kind != wire.KindSubscription { |
| 118 | t.Fatalf("claude connect %+v %v", body, err) |
| 119 | } |
| 120 | cancelled, err := r.client.CancelConnection(r.ctx, claude.ID) |
| 121 | if err != nil || cancelled.State != wire.ConnError || cancelled.Reason != "cancelled" { |
| 122 | t.Fatalf("cancel %+v %v", cancelled, err) |
| 123 | } |
| 124 | if _, body, err = runner.Expect(r.ctx, wire.FrameCancelConnect); err != nil || body.(*wire.CancelConnectCommand).Connection != claude.ID { |
| 125 | t.Fatalf("cancel frame %+v %v", body, err) |
| 126 | } |
| 127 | |
| 128 | // A connection the runner no longer holds is signed out. |
| 129 | hello.Connections = hello.Connections[:1] |
| 130 | if err = runner.Send(r.ctx, "h4", wire.FrameHello, hello); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | r.wait("codex signed out", func(c *Conversation) bool { return c.Connections[attempt.ID].State == wire.ConnSignedOut }) |
| 134 | snap, err := r.client.Snapshot(r.ctx) |
| 135 | if err != nil || len(snap.Connections) != 3 { |
| 136 | t.Fatalf("snapshot connections %+v %v", snap.Connections, err) |
| 137 | } |
| 138 | // Nothing secret-shaped was stored. |
| 139 | var dump string |
| 140 | _ = r.inst.DB.Writer().QueryRow("SELECT group_concat(coalesce(headroom,'') || coalesce(prompt,'') || label || plan, '|') FROM chat_connections").Scan(&dump) |
| 141 | for _, bad := range []string{"token", "secret", "password", "/home/"} { |
| 142 | if strings.Contains(strings.ToLower(dump), bad) { |
| 143 | t.Fatalf("stored %q: %s", bad, dump) |
| 144 | } |
| 145 | } |
| 146 | var _ = sql.ErrNoRows |
| 147 | } |
| 148 | |
| 149 | // TestContractHeldTurnAndSwitch: a turn held for a connection carries the |
| 150 | // reason no_connection while it waits and drops it when it goes on; a |
| 151 | // failover's new attempt drops the first attempt's draft; the reply |
| 152 | // carries the switched line and its usage is recorded. |
| 153 | func TestContractHeldTurnAndSwitch(t *testing.T) { |
| 154 | r := newRig(t) |
| 155 | runner := r.dial(0) |
| 156 | r.wait("runner online", func(c *Conversation) bool { return c.Runner.Online }) |
| 157 | sent, err := r.client.Send(r.ctx, "hello", "k-held") |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | f, _, err := runner.Expect(r.ctx, wire.FrameMessage) |
| 162 | if err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | runner.Send(r.ctx, "a-1", wire.FrameAck, wire.Ack{ID: f.ID}) |
| 166 | turn := sent.Turn.ID |
| 167 | runner.Send(r.ctx, "r-hold", wire.FrameTurn, wire.TurnReport{TurnID: turn, Attempt: 1, State: wire.TurnWaiting, Reason: wire.ReasonNoConnection}) |
| 168 | r.wait("held", func(c *Conversation) bool { |
| 169 | v := c.Turns[turn] |
| 170 | return v.State == wire.TurnWaiting && v.Reason == wire.ReasonNoConnection |
| 171 | }) |
| 172 | // A message sent while it waits joins the queue behind it. |
| 173 | later, err := r.client.Send(r.ctx, "still there?", "k-held-2") |
| 174 | if err != nil || later.Turn.State != wire.TurnQueued || later.Turn.ID == turn { |
| 175 | t.Fatalf("second message %+v %v", later.Turn, err) |
| 176 | } |
| 177 | page := r.inst.Get(t, "/conversation") |
| 178 | if !strings.Contains(page, "data-no-connection") || !strings.Contains(page, "Nothing can answer right now.") || strings.Contains(page, "Send your message again") { |
| 179 | t.Fatalf("held card missing from the page") |
| 180 | } |
| 181 | runner.Send(r.ctx, "r-go", wire.FrameTurn, wire.TurnReport{TurnID: turn, Attempt: 1, State: wire.TurnRunning}) |
| 182 | r.wait("going again", func(c *Conversation) bool { v := c.Turns[turn]; return v.State == wire.TurnRunning && v.Reason == "" }) |
| 183 | runner.Send(r.ctx, "r-d1", wire.FrameDelta, wire.DeltaReport{TurnID: turn, Attempt: 1, Offset: 0, Text: "Partial"}) |
| 184 | r.wait("draft", func(c *Conversation) bool { return c.Drafts[turn] == "Partial" }) |
| 185 | // Claude refused on quota: attempt 2 on Codex. |
| 186 | runner.Send(r.ctx, "r-retry", wire.FrameTurn, wire.TurnReport{TurnID: turn, Attempt: 2, State: wire.TurnRunning}) |
| 187 | r.wait("attempt 2, no draft", func(c *Conversation) bool { return c.Turns[turn].Attempt == 2 && c.Drafts[turn] == "" }) |
| 188 | runner.Send(r.ctx, "r-stale", wire.FrameDelta, wire.DeltaReport{TurnID: turn, Attempt: 1, Offset: 7, Text: " old"}) |
| 189 | runner.Send(r.ctx, "r-d2", wire.FrameDelta, wire.DeltaReport{TurnID: turn, Attempt: 2, Offset: 0, Text: "Done."}) |
| 190 | r.wait("new draft", func(c *Conversation) bool { return c.Drafts[turn] == "Done." }) |
| 191 | reset := time.Date(2026, 11, 10, 10, 0, 0, 0, time.UTC) |
| 192 | reply := wire.Reply{TurnID: turn, Attempt: 2, Text: "Done.", Usage: []wire.Usage{{Provider: "codex", Connection: "codex-1", Model: "gpt-6-astra", Attempt: 2, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: i64(5120), CachedInputTokens: i64(4096), CacheWriteTokens: i64(0), OutputTokens: i64(88), ReasoningOutputTokens: i64(40)}}, Switched: &wire.Switched{From: "claude", To: "codex", ResetsAt: &reset}} |
| 193 | runner.Send(r.ctx, "r-reply", wire.FrameReply, reply) |
| 194 | r.wait("replied", func(c *Conversation) bool { return c.Turns[turn].State == wire.TurnReplied && len(c.Replies()) == 1 }) |
| 195 | m := r.conv.Replies()[0] |
| 196 | if m.Switched == nil || m.Switched.To != "codex" || !m.Switched.ResetsAt.Equal(reset) { |
| 197 | t.Fatalf("switched %+v", m.Switched) |
| 198 | } |
| 199 | if v, _ := r.client.Turn(r.ctx, turn); v.Connection != "codex-1" || v.Attempt != 2 { |
| 200 | t.Fatalf("turn %+v", v) |
| 201 | } |
| 202 | page = r.inst.Get(t, "/conversation") |
| 203 | // In the person's zone (Europe/Dublin, UTC in November), no UTC |
| 204 | // suffix (paused work, decision 4). |
| 205 | if !strings.Contains(page, "Switched to Codex; Claude resets Tue 10:00<") || strings.Contains(page, "10:00 UTC") || strings.Contains(page, "data-no-connection") { |
| 206 | t.Fatalf("switched line missing from the page") |
| 207 | } |
| 208 | var rows, input, cached, reasoning int64 |
| 209 | var provider, model, conn, role string |
| 210 | err = r.inst.DB.Writer().QueryRow("SELECT count(*), sum(input_tokens), sum(cached_input_tokens), sum(reasoning_output_tokens), max(provider), max(model), max(connection), max(role) FROM chat_usage WHERE turn_id = ?", turn).Scan(&rows, &input, &cached, &reasoning, &provider, &model, &conn, &role) |
| 211 | if err != nil || rows != 1 || input != 5120 || cached != 4096 || reasoning != 40 || provider != "codex" || model != "gpt-6-astra" || conn != "codex-1" || role != "coordinator" { |
| 212 | t.Fatalf("usage %d %d %d %d %s %s %s %s %v", rows, input, cached, reasoning, provider, model, conn, role, err) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // TestContractWritesReplayAcrossAttempts: after a turn moves to a new |
| 217 | // attempt, the instance's mutation ledger holds it to what the earlier |
| 218 | // attempt did, whatever the new wording (spec §3 step 4): a reworded |
| 219 | // record_task attaches to the task already recorded, a paraphrased |
| 220 | // question returns the card already asked, a reworded start of the same |
| 221 | // task starts nothing; each answers with the stored result, replayed. A |
| 222 | // further record_task records anew, a finding with other words is made, |
| 223 | // and reads run fresh: status after add_project sees the project. |
| 224 | func TestContractWritesReplayAcrossAttempts(t *testing.T) { |
| 225 | r := newToolRig(t) |
| 226 | before := r.call("s1", wire.ToolStatus, map[string]string{}) |
| 227 | added := r.call("a1", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) |
| 228 | recorded := r.call("r1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "The README has teh"}) |
| 229 | var rec struct{ Task string } |
| 230 | json.Unmarshal([]byte(recorded.Text), &rec) |
| 231 | asked := r.call("q1", wire.ToolAskPerson, map[string]any{"question": "Which colour?", "options": []string{"Red", "Blue"}}) |
| 232 | started := r.call("t1", wire.ToolStartTask, map[string]string{"task": rec.Task, "brief": "Fix teh."}) |
| 233 | for _, res := range []wire.ToolResult{added, recorded, asked, started} { |
| 234 | if res.IsError || res.Replayed { |
| 235 | t.Fatalf("first attempt %+v", res) |
| 236 | } |
| 237 | } |
| 238 | projects := func(text string) int { |
| 239 | var v struct{ Projects []any } |
| 240 | json.Unmarshal([]byte(text), &v) |
| 241 | return len(v.Projects) |
| 242 | } |
| 243 | if projects(before.Text) != 0 { |
| 244 | t.Fatalf("status before add_project already has it: %s", before.Text) |
| 245 | } |
| 246 | r.runner.Send(r.ctx, "r-wait", wire.FrameTurn, wire.TurnReport{TurnID: r.turn, Attempt: 1, State: wire.TurnWaiting, Reason: "limited"}) |
| 247 | r.runner.Send(r.ctx, "r-run2", wire.FrameTurn, wire.TurnReport{TurnID: r.turn, Attempt: 2, State: wire.TurnRunning}) |
| 248 | r.wait("attempt 2", func(c *Conversation) bool { |
| 249 | return c.Turns[r.turn].Attempt == 2 && c.Turns[r.turn].State == wire.TurnRunning |
| 250 | }) |
| 251 | r.attempt = 2 |
| 252 | // Reads run fresh: status sees the project added in attempt 1. |
| 253 | if after := r.call("s2", wire.ToolStatus, map[string]string{}); after.Replayed || projects(after.Text) != 1 { |
| 254 | t.Fatalf("status was not fresh: %+v", after) |
| 255 | } |
| 256 | for name, call := range map[string]func() wire.ToolResult{ |
| 257 | "record_task reworded": func() wire.ToolResult { |
| 258 | return r.call("r2", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "The README says teh."}) |
| 259 | }, |
| 260 | "ask_person paraphrased": func() wire.ToolResult { |
| 261 | return r.call("q2", wire.ToolAskPerson, map[string]any{"question": "What colour would you like?", "options": []string{"Blue", "Red", "Green"}}) |
| 262 | }, |
| 263 | "start_task reworded": func() wire.ToolResult { |
| 264 | return r.call("t2", wire.ToolStartTask, map[string]string{"task": rec.Task, "brief": "Please correct the spelling."}) |
| 265 | }, |
| 266 | "add_project again": func() wire.ToolResult { |
| 267 | return r.call("a2", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) |
| 268 | }, |
| 269 | } { |
| 270 | if res := call(); !res.Replayed { |
| 271 | t.Fatalf("%s ran again: %+v", name, res) |
| 272 | } |
| 273 | } |
| 274 | var cards int |
| 275 | snap, _ := r.client.Snapshot(r.ctx) |
| 276 | for _, c := range snap.Cards { |
| 277 | if c.Kind == wire.CardQuestion { |
| 278 | cards++ |
| 279 | } |
| 280 | } |
| 281 | tasks, err := r.client.Tasks(r.ctx) |
| 282 | if err != nil { |
| 283 | t.Fatal(err) |
| 284 | } |
| 285 | n, steps := 0, 0 |
| 286 | for _, k := range tasks.Items { |
| 287 | if strings.Contains(k.Title, "typo") || strings.Contains(k.Title, "misspelling") { |
| 288 | n++ |
| 289 | steps += len(k.Steps) |
| 290 | } |
| 291 | } |
| 292 | if n != 1 || cards != 1 || steps != 1 { |
| 293 | t.Fatalf("%d tasks, %d question cards, %d steps", n, cards, steps) |
| 294 | } |
| 295 | // Beyond what the earlier attempt did, a write is made. |
| 296 | other := r.call("r3", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Another problem entirely", "brief": "something else", "separate": true}) |
| 297 | if other.Replayed || other.IsError { |
| 298 | t.Fatalf("a new write was folded: %+v", other) |
| 299 | } |
| 300 | r.attempt = 1 |
| 301 | stale := r.call("r4", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Third", "brief": "x"}) |
| 302 | if !stale.IsError || stale.Code != "turn_ended" { |
| 303 | t.Fatalf("a call from the old attempt ran: %+v", stale) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Astra round 1, finding 1: the per-kind rules hold within one attempt |
| 308 | // too. A second question in other words is the same card; the home and a |
| 309 | // finding are made once per target; record_task makes two tasks for two |
| 310 | // different things and one for the same thing reworded. |
| 311 | func TestContractWritesOncePerTurnWithinAnAttempt(t *testing.T) { |
| 312 | r := newToolRig(t) |
| 313 | r.call("a1", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) |
| 314 | asked := r.call("q1", wire.ToolAskPerson, map[string]any{"question": "Which colour?", "options": []string{"Red", "Blue"}}) |
| 315 | again := r.call("q2", wire.ToolAskPerson, map[string]any{"question": "And which size?", "options": []string{"Small", "Large"}}) |
| 316 | if asked.IsError || asked.Replayed || !again.Replayed { |
| 317 | t.Fatalf("questions %+v %+v", asked, again) |
| 318 | } |
| 319 | a := r.call("r1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "The README says teh panes."}) |
| 320 | b := r.call("r2", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Add a dark theme toggle", "brief": "Settings needs a switch for the dark theme."}) |
| 321 | a2 := r.call("r3", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "The README says teh panes."}) |
| 322 | if a.IsError || b.IsError || a.Replayed || b.Replayed || !a2.Replayed || a2.Text != a.Text { |
| 323 | t.Fatalf("tasks %+v %+v %+v", a, b, a2) |
| 324 | } |
| 325 | var task struct{ Task string } |
| 326 | json.Unmarshal([]byte(a.Text), &task) |
| 327 | f1 := r.call("f1", wire.ToolAddFinding, map[string]string{"task": task.Task, "text": "The docs say teh too."}) |
| 328 | f2 := r.call("f2", wire.ToolAddFinding, map[string]string{"task": task.Task, "text": "the docs say TEH too."}) |
| 329 | if f1.IsError || !f2.Replayed { |
| 330 | t.Fatalf("findings %+v %+v", f1, f2) |
| 331 | } |
| 332 | var cards int |
| 333 | snap, _ := r.client.Snapshot(r.ctx) |
| 334 | for _, c := range snap.Cards { |
| 335 | if c.Kind == wire.CardQuestion { |
| 336 | cards++ |
| 337 | } |
| 338 | } |
| 339 | tasks, _ := r.client.Tasks(r.ctx) |
| 340 | if cards != 1 || len(tasks.Items) != 2 { |
| 341 | t.Fatalf("%d question cards, %d tasks", cards, len(tasks.Items)) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Finding 2, one ledger: attempt 1 records A and B; attempt 2 calls B in |
| 346 | // other words first, then A exactly. Each maps back to its own task. |
| 347 | func TestContractRecordTaskMapsBackAcrossAttempts(t *testing.T) { |
| 348 | r := newToolRig(t) |
| 349 | r.call("a1", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) |
| 350 | aArgs := map[string]string{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "The README says teh panes."} |
| 351 | a := r.call("r1", wire.ToolRecordTask, aArgs) |
| 352 | b := r.call("r2", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Add a dark theme toggle", "brief": "Settings needs a switch for the dark theme."}) |
| 353 | r.runner.Send(r.ctx, "r-wait", wire.FrameTurn, wire.TurnReport{TurnID: r.turn, Attempt: 1, State: wire.TurnWaiting, Reason: "limited"}) |
| 354 | r.runner.Send(r.ctx, "r-run2", wire.FrameTurn, wire.TurnReport{TurnID: r.turn, Attempt: 2, State: wire.TurnRunning}) |
| 355 | r.wait("attempt 2", func(c *Conversation) bool { return c.Turns[r.turn].Attempt == 2 }) |
| 356 | r.attempt = 2 |
| 357 | b2 := r.call("r3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Dark theme: add the toggle (as the second account puts it)", "brief": "A switch in Settings for the dark theme (as the second account puts it)."}) |
| 358 | a2 := r.call("r4", wire.ToolRecordTask, aArgs) |
| 359 | if !b2.Replayed || b2.Text != b.Text || !a2.Replayed || a2.Text != a.Text { |
| 360 | t.Fatalf("attempt 2 mapped B to %s and A to %s (want %s, %s)", b2.Text, a2.Text, b.Text, a.Text) |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // Astra round 2: never by order alone. Attempt 1 records A; attempt 2 |
| 365 | // asks for an unrelated B before repeating A: B is made, A's repeat maps |
| 366 | // to A. A call that reads like two earlier tasks is refused with both, |
| 367 | // so the model names the one it means. |
| 368 | func TestContractRecordTaskNeverByOrder(t *testing.T) { |
| 369 | r := newToolRig(t) |
| 370 | r.call("a1", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) |
| 371 | aArgs := map[string]string{"repo": "oficina/sample", "title": "Fix the README typo", "brief": "The README says teh panes."} |
| 372 | a := r.call("r1", wire.ToolRecordTask, aArgs) |
| 373 | a2 := r.call("r2", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Fix the README typo in the install section", "brief": "The install section of the README says teh too.", "separate": true}) |
| 374 | if a.IsError || a2.IsError || a2.Replayed { |
| 375 | t.Fatalf("attempt 1 %+v %+v", a, a2) |
| 376 | } |
| 377 | r.runner.Send(r.ctx, "r-wait", wire.FrameTurn, wire.TurnReport{TurnID: r.turn, Attempt: 1, State: wire.TurnWaiting, Reason: "limited"}) |
| 378 | r.runner.Send(r.ctx, "r-run2", wire.FrameTurn, wire.TurnReport{TurnID: r.turn, Attempt: 2, State: wire.TurnRunning}) |
| 379 | r.wait("attempt 2", func(c *Conversation) bool { return c.Turns[r.turn].Attempt == 2 }) |
| 380 | r.attempt = 2 |
| 381 | b := r.call("r3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Add a dark theme toggle", "brief": "Settings needs a switch for the dark theme."}) |
| 382 | if b.IsError || b.Replayed || b.Code != "recorded" { |
| 383 | t.Fatalf("the unrelated task was not made: %+v", b) |
| 384 | } |
| 385 | again := r.call("r4", wire.ToolRecordTask, aArgs) |
| 386 | if !again.Replayed || again.Text != a.Text { |
| 387 | t.Fatalf("A's repeat mapped to %s, want %s", again.Text, a.Text) |
| 388 | } |
| 389 | both := r.call("r5", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Fix the README typo (teh)", "brief": "The README says teh in two places."}) |
| 390 | var listed struct { |
| 391 | Earlier []struct{ Task string } |
| 392 | } |
| 393 | json.Unmarshal([]byte(both.Text), &listed) |
| 394 | if !both.IsError || both.Code != "ambiguous" || len(listed.Earlier) != 2 { |
| 395 | t.Fatalf("an ambiguous call was not refused with both: %+v", both) |
| 396 | } |
| 397 | tasks, _ := r.client.Tasks(r.ctx) |
| 398 | if len(tasks.Items) != 3 { |
| 399 | t.Fatalf("%d tasks, want A, A2 and B", len(tasks.Items)) |
| 400 | } |
| 401 | } |
| 402 | |