| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo" |
| 9 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 10 | ) |
| 11 | |
| 12 | func TestCoordinatorBlockedPollWritesOnlyOnChange(t *testing.T) { |
| 13 | for _, reason := range []string{"budget_needed", "waiting_connection", "waiting_task", "context_limit"} { |
| 14 | t.Run(reason, func(t *testing.T) { |
| 15 | f, x, p, _ := coordinatorFixture(t) |
| 16 | if reason == "waiting_task" { |
| 17 | first := queueCoordinator(t, f, "first", "first") |
| 18 | c, err := x.ClaimCoordinator(ctx, p, first.ID, first.Revision) |
| 19 | must(t, err) |
| 20 | f.exec(t, "UPDATE coordinator_turns SET state='delivered',attempt_id=0 WHERE id=?", first.ID) |
| 21 | f.exec(t, `INSERT INTO runner_assignments(id,task_id,person,state,attempt_id,selection) VALUES('blocking','blocking-task',?,'claimed',?,?)`, owner.Address, c.Handle.Attempt, `{"ConnectionID":"claude"}`) |
| 22 | } |
| 23 | body := "request" |
| 24 | if reason == "context_limit" { |
| 25 | body = strings.Repeat("\x00", 16000) |
| 26 | } |
| 27 | turn := queueCoordinator(t, f, body, "request") |
| 28 | switch reason { |
| 29 | case "budget_needed": |
| 30 | f.exec(t, "DELETE FROM coordinator_limits") |
| 31 | case "waiting_connection": |
| 32 | f.exec(t, "DELETE FROM connections WHERE id='claude'") |
| 33 | case "context_limit": |
| 34 | if _, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision); err == nil { |
| 35 | t.Fatal("oversized context accepted") |
| 36 | } |
| 37 | } |
| 38 | turns, err := x.PollCoordinator(ctx, p) |
| 39 | must(t, err) |
| 40 | if len(turns) != 0 { |
| 41 | t.Fatal("blocked turn offered", turns) |
| 42 | } |
| 43 | if f.count(t, "SELECT count(*) FROM coordinator_turns WHERE id=? AND state=?", turn.ID, reason) != 1 { |
| 44 | t.Fatal("waiting state missing", reason) |
| 45 | } |
| 46 | assertCoordinatorPollReadOnly(t, f, func() { |
| 47 | turns, err := x.PollCoordinator(ctx, p) |
| 48 | must(t, err) |
| 49 | if len(turns) != 0 { |
| 50 | t.Fatal("blocked turn re-offered", reason) |
| 51 | } |
| 52 | }) |
| 53 | if reason == "budget_needed" { |
| 54 | must(t, f.Store.SetCoordinatorLimits(ctx, owner, owner.Address, team.Limits{Calls: 24, InputTokens: 200000, OutputTokens: 40000, Seconds: 10000}, equipo.Mutation{RequestID: "restore-limits"})) |
| 55 | turns, err = x.PollCoordinator(ctx, p) |
| 56 | must(t, err) |
| 57 | if len(turns) != 1 || turns[0].ID != turn.ID { |
| 58 | t.Fatal("restored budget lost turn", turns) |
| 59 | } |
| 60 | if f.count(t, "SELECT count(*) FROM conversation_requests WHERE operation='coordinator.waiting'") != 0 { |
| 61 | t.Fatal("restored budget retained waiting record") |
| 62 | } |
| 63 | assertCoordinatorPollReadOnly(t, f, func() { |
| 64 | turns, err := x.PollCoordinator(ctx, p) |
| 65 | must(t, err) |
| 66 | if len(turns) != 1 { |
| 67 | t.Fatal("available turn lost") |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func assertCoordinatorPollReadOnly(t *testing.T, f *fixture, poll func()) { |
| 76 | t.Helper() |
| 77 | revision := f.count(t, "SELECT revision FROM conversations WHERE person=?", owner.Address) |
| 78 | audit := f.count(t, "SELECT count(*) FROM audit_events") |
| 79 | for _, table := range []string{"conversation_requests", "coordinator_turns", "conversations", "audit_events"} { |
| 80 | for _, op := range []string{"INSERT", "UPDATE", "DELETE"} { |
| 81 | name := "forbid_poll_" + table + "_" + op |
| 82 | f.exec(t, fmt.Sprintf("CREATE TRIGGER %s BEFORE %s ON %s BEGIN SELECT RAISE(ABORT,'unchanged poll wrote'); END", name, op, table)) |
| 83 | defer f.exec(t, "DROP TRIGGER "+name) |
| 84 | } |
| 85 | } |
| 86 | for i := 0; i < 3; i++ { |
| 87 | poll() |
| 88 | } |
| 89 | if got := f.count(t, "SELECT revision FROM conversations WHERE person=?", owner.Address); got != revision { |
| 90 | t.Fatal("unchanged poll bumped revision", revision, got) |
| 91 | } |
| 92 | if got := f.count(t, "SELECT count(*) FROM audit_events"); got != audit { |
| 93 | t.Fatal("unchanged poll appended audit", audit, got) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestCoordinatorFilteredRequestParked(t *testing.T) { |
| 98 | for _, action := range []string{"retry", "reset"} { |
| 99 | t.Run(action, func(t *testing.T) { |
| 100 | f, x, p, b := coordinatorFixture(t) |
| 101 | turn := queueCoordinator(t, f, "request with revoked source", "request") |
| 102 | var task string |
| 103 | must(t, f.DB.Writer().QueryRow("SELECT id FROM tasks LIMIT 1").Scan(&task)) |
| 104 | // A routed source is checked during context assembly, after Current succeeds. |
| 105 | f.exec(t, "INSERT INTO conversation_routes(source_id,notice_id,task_id) VALUES(?,?,?)", turn.MessageID, turn.MessageID, task) |
| 106 | f.exec(t, `UPDATE connection_choices SET connection_ids_json='["codex"]',pinned_id='codex'`) |
| 107 | if _, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision); err == nil { |
| 108 | t.Fatal("filtered request reached provider") |
| 109 | } |
| 110 | if f.count(t, "SELECT count(*) FROM coordinator_turns WHERE id=? AND state='context_unavailable'", turn.ID) != 1 { |
| 111 | t.Fatal("filtered request not parked") |
| 112 | } |
| 113 | if f.count(t, "SELECT count(*) FROM admission_tickets") != 0 { |
| 114 | t.Fatal("filtered request admitted") |
| 115 | } |
| 116 | page, err := f.Store.Conversation(ctx, owner, "", 50) |
| 117 | must(t, err) |
| 118 | found := false |
| 119 | for _, card := range page.Cards { |
| 120 | if card.ResourceID == turn.ID && card.CopyKey == "coordinator.context_unavailable" && strings.Join(card.ActionKeys, ",") == "coordinator.reset,coordinator.retry" { |
| 121 | found = true |
| 122 | } |
| 123 | } |
| 124 | if !found { |
| 125 | t.Fatal("filtered request missing person-visible card") |
| 126 | } |
| 127 | assertCoordinatorPollReadOnly(t, f, func() { |
| 128 | turns, err := x.PollCoordinator(ctx, p) |
| 129 | must(t, err) |
| 130 | if len(turns) != 0 { |
| 131 | t.Fatal("filtered request re-offered") |
| 132 | } |
| 133 | if _, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision); err == nil { |
| 134 | t.Fatal("parked request directly claimed") |
| 135 | } |
| 136 | }) |
| 137 | f.exec(t, `UPDATE connection_choices SET connection_ids_json='["claude","codex"]',pinned_id='claude'`) |
| 138 | var next equipo.CoordinatorTurn |
| 139 | if action == "retry" { |
| 140 | next, err = f.Store.RetryCoordinatorTurn(ctx, owner, turn.ID, equipo.Mutation{RequestID: "retry", Revision: turn.Revision}) |
| 141 | must(t, err) |
| 142 | } else { |
| 143 | _, err = f.Store.ResetCoordinator(ctx, owner, equipo.Mutation{RequestID: "reset", Revision: b.Revision}) |
| 144 | must(t, err) |
| 145 | turns, err := x.PollCoordinator(ctx, p) |
| 146 | must(t, err) |
| 147 | if len(turns) != 1 { |
| 148 | t.Fatal("reset did not release filtered request", turns) |
| 149 | } |
| 150 | next = turns[0] |
| 151 | } |
| 152 | c, err := x.ClaimCoordinator(ctx, p, next.ID, next.Revision) |
| 153 | must(t, err) |
| 154 | if c.Context.Recent[len(c.Context.Recent)-1].ID != turn.MessageID { |
| 155 | t.Fatal("retry lost request") |
| 156 | } |
| 157 | page, err = f.Store.Conversation(ctx, owner, "", 50) |
| 158 | must(t, err) |
| 159 | for _, card := range page.Cards { |
| 160 | if card.CopyKey == "coordinator.context_unavailable" { |
| 161 | t.Fatal("resolved card survived") |
| 162 | } |
| 163 | } |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | |