| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo" |
| 9 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // held sends a hold for the task's step and returns the task as read. |
| 14 | func (r *toolRig) hold(key string, d *wire.DispatchCommand, h wire.StepHold) wire.Task { |
| 15 | r.t.Helper() |
| 16 | r.step(key, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, Findings: []wire.Finding{}, Hold: &h}) |
| 17 | k, err := r.client.Task(r.ctx, d.TaskID) |
| 18 | if err != nil { |
| 19 | r.t.Fatal(err) |
| 20 | } |
| 21 | return k |
| 22 | } |
| 23 | |
| 24 | // frames counts the commands of kind the instance wrote for runners. |
| 25 | func (r *toolRig) frames(kind string) int { |
| 26 | r.t.Helper() |
| 27 | var n int |
| 28 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind=?", kind).Scan(&n); err != nil { |
| 29 | r.t.Fatal(err) |
| 30 | } |
| 31 | return n |
| 32 | } |
| 33 | |
| 34 | // TestContractScheduler (connectors spec S3, §6, §8, §10): the |
| 35 | // coordinator sets low or normal and a deadline only at a task's |
| 36 | // creation, never urgent; the dispatch carries them; a held step is |
| 37 | // shown queued with its hold and, where the creator's input changes the |
| 38 | // outcome, Needs you with Equipo's recommendation; the creator's |
| 39 | // priority change is revision-checked and reaches the runner; Run now is |
| 40 | // the creator's alone, bound to the held step revision, one release per |
| 41 | // revision, and never passes a spending bar. |
| 42 | func TestContractScheduler(t *testing.T) { |
| 43 | r := newToolRigAt(t, wire.ProtocolScheduler) |
| 44 | ctx := r.ctx |
| 45 | by := time.Now().Add(5 * 24 * time.Hour).UTC().Format("2006-01-02") |
| 46 | if res := r.call("p0", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Urgent from the model", "brief": "b", "priority": "urgent"}); !res.IsError { |
| 47 | t.Fatalf("the coordinator set urgent: %+v", res) |
| 48 | } |
| 49 | if res := r.call("p0b", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "A past deadline", "brief": "b", "by": "2020-01-01"}); !res.IsError { |
| 50 | t.Fatalf("a past deadline: %+v", res) |
| 51 | } |
| 52 | id := taskID(t, r.call("p1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Tidy the README", "brief": "When you get to it.", "priority": "low", "by": by})) |
| 53 | k, err := r.client.Task(ctx, id) |
| 54 | if err != nil || k.Priority == nil || k.Priority.Priority != wire.PriorityLow || k.Priority.By == nil || k.Priority.SetBy != wire.PrioritySetByCoordinator { |
| 55 | t.Fatalf("priority %+v %v", k.Priority, err) |
| 56 | } |
| 57 | // Security round #9: a task recorded and started with nothing set, |
| 58 | // then start_task again in the same turn with fields: refused, and |
| 59 | // nothing set. |
| 60 | plain := taskID(t, r.call("n1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Plain one", "brief": "b"})) |
| 61 | if res := r.call("n2", wire.ToolStartTask, map[string]string{"task": plain, "brief": "go"}); res.IsError { |
| 62 | t.Fatalf("start: %+v", res) |
| 63 | } |
| 64 | r.expect(wire.FrameDispatch) |
| 65 | if res := r.call("n3", wire.ToolStartTask, map[string]string{"task": plain, "brief": "again", "priority": "low", "by": by}); !res.IsError { |
| 66 | t.Fatalf("a second start: %+v", res) |
| 67 | } |
| 68 | if k, _ := r.client.Task(ctx, plain); k.Priority != nil { |
| 69 | t.Fatalf("a second start_task set the priority: %+v", k.Priority) |
| 70 | } |
| 71 | // start_task cannot change what is set. |
| 72 | if res := r.call("p2", wire.ToolStartTask, map[string]string{"task": id, "brief": "go", "priority": "normal"}); res.IsError { |
| 73 | t.Fatalf("start: %+v", res) |
| 74 | } |
| 75 | _, body := r.expect(wire.FrameDispatch) |
| 76 | d := body.(*wire.DispatchCommand) |
| 77 | if d.Priority != wire.PriorityLow || d.By == nil || !d.By.Equal(*k.Priority.By) || d.Release != "" || d.PriorityRevision != 1 || d.Housekeeping { |
| 78 | t.Fatalf("dispatch %+v", d) |
| 79 | } |
| 80 | if k, _ = r.client.Task(ctx, id); k.Priority.Priority != wire.PriorityLow { |
| 81 | t.Fatalf("start_task changed the priority: %+v", k.Priority) |
| 82 | } |
| 83 | |
| 84 | // Held for headroom with a reset in time: queued, the hold shown, not |
| 85 | // Needs you. |
| 86 | reset := time.Now().Add(3 * time.Hour).UTC().Truncate(time.Second) |
| 87 | k = r.hold("h1", d, wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-a", Provider: "claude", ResetsAt: &reset, WakeAt: &reset, StepRevision: 1}) |
| 88 | if s := k.Steps[len(k.Steps)-1]; s.State != wire.StepQueued || s.Hold == nil || s.Hold.Reason != wire.HoldHeadroom || k.NeedsYou != "" { |
| 89 | t.Fatalf("held: %+v %+v needs %q", s, s.Hold, k.NeedsYou) |
| 90 | } |
| 91 | pages := &toolRig{rig: r.rig} |
| 92 | owner := r.inst.Cookie() |
| 93 | // Paused, with plain Run now: this runner (protocol 14) cannot be held |
| 94 | // to one connection, so nothing is promised (paused work review B1). |
| 95 | if page := pages.get(owner, "/tasks/"+id, 200); !strings.Contains(page, "<strong>Paused until ") || !strings.Contains(page, "your Claude allowance has run out.") || !strings.Contains(page, `data-pause-act="run-now"`) || !strings.Contains(page, "Run it now anyway") || strings.Contains(page, "has room now") || !strings.Contains(page, "Low priority") || !strings.Contains(page, "set from your message") || !strings.Contains(page, "data-task-urgent") { |
| 96 | t.Fatal("the task page lacks the pause, Run now or the priority") |
| 97 | } |
| 98 | // No reset in sight: Needs you, with Equipo's recommendation. |
| 99 | far := time.Now().Add(80 * time.Hour).UTC().Truncate(time.Second) |
| 100 | k = r.hold("h2", d, wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-a", Provider: "claude", ResetsAt: &far, WakeAt: &far, NeedsPerson: wire.HoldNeedsNoReset, StepRevision: 1}) |
| 101 | if k.NeedsYou != wire.NeedsHeld { |
| 102 | t.Fatalf("needs %q", k.NeedsYou) |
| 103 | } |
| 104 | shape(t, "api/task-held.json", k) |
| 105 | var card wire.Card |
| 106 | snap, err := r.client.Snapshot(ctx) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | for _, c := range snap.Cards { |
| 111 | if c.Kind == wire.CardHeld && c.TaskID == id && c.State == wire.CardOpen { |
| 112 | card = c |
| 113 | } |
| 114 | } |
| 115 | if card.ID == "" || !strings.Contains(card.Question, "Claude") || len(card.Options) != 2 || card.Options[1] != chat.ChoiceRunNow { |
| 116 | t.Fatalf("held card %+v", card) |
| 117 | } |
| 118 | shape(t, "api/card-held.json", card) |
| 119 | |
| 120 | // The creator's priority: revision-checked, the creator's alone, sent |
| 121 | // to the runner holding the step. |
| 122 | pages.memberCookie() |
| 123 | const Member = "member@example.com" |
| 124 | if _, err := r.client.SetPriority(ctx, id, wire.SetPriority{Priority: wire.PriorityUrgent, Revision: k.Revision - 1}); Status(err) != 409 { |
| 125 | t.Fatalf("a stale revision: %v", err) |
| 126 | } |
| 127 | if _, err := r.client.SetPriority(ctx, id, wire.SetPriority{Priority: "high", Revision: k.Revision}); Status(err) != 422 && Status(err) != 400 { |
| 128 | t.Fatalf("an unknown priority: %v", err) |
| 129 | } |
| 130 | if _, err := r.inst.Store.Chat().SetPriority(ctx, Member, id, wire.SetPriority{Priority: wire.PriorityUrgent, Revision: k.Revision}); err == nil { |
| 131 | t.Fatalf("a teammate's priority: %v", err) |
| 132 | } |
| 133 | // Another owner is not the task's creator. |
| 134 | if err := r.inst.Store.SetMember(ctx, equipo.Actor{Address: Owner}, "second@example.com", equipo.RoleOwner, true, 0); err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | if _, err := r.inst.Store.Chat().SetPriority(ctx, "second@example.com", id, wire.SetPriority{Priority: wire.PriorityUrgent, Revision: k.Revision}); err == nil { |
| 138 | t.Fatal("another owner set the creator's priority") |
| 139 | } |
| 140 | if _, err := r.inst.Store.Chat().RunNow(ctx, "second@example.com", id, wire.RunNow{Step: d.StepID, StepRevision: 1}); err == nil { |
| 141 | t.Fatal("another owner's Run now") |
| 142 | } |
| 143 | k, err = r.client.SetPriority(ctx, id, wire.SetPriority{Priority: wire.PriorityUrgent, Revision: k.Revision}) |
| 144 | if err != nil || k.Priority.Priority != wire.PriorityUrgent || k.Priority.SetBy != wire.PrioritySetByPerson || k.Priority.By != nil { |
| 145 | t.Fatalf("set: %+v %v", k.Priority, err) |
| 146 | } |
| 147 | _, body = r.expect(wire.FramePriority) |
| 148 | shape(t, "frames/priority.json", r.runner.Received[len(r.runner.Received)-1]) |
| 149 | if p := body.(*wire.PriorityCommand); p.TaskID != id || p.Priority != wire.PriorityUrgent || p.Revision != 2 || p.Person != Owner { |
| 150 | t.Fatalf("priority frame %+v", p) |
| 151 | } |
| 152 | |
| 153 | // Run now: the creator's alone, bound to the held revision, one |
| 154 | // release per revision. |
| 155 | if _, err := r.inst.Store.Chat().RunNow(ctx, Member, id, wire.RunNow{Step: d.StepID, StepRevision: 1}); err == nil { |
| 156 | t.Fatalf("a teammate's Run now: %v", err) |
| 157 | } |
| 158 | if _, err := r.client.RunNow(ctx, id, wire.RunNow{Step: d.StepID, StepRevision: 2}); Status(err) != 409 { |
| 159 | t.Fatalf("a stale step revision: %v", err) |
| 160 | } |
| 161 | // This runner (protocol 14) was offered plain Run now, so the tap says |
| 162 | // so; naming a connection it was never offered is refused. |
| 163 | if _, err := r.client.RunNow(ctx, id, wire.RunNow{Step: d.StepID, StepRevision: 1, Offer: "cn_somewhere"}); Status(err) != 409 || !strings.Contains(err.Error(), "offer_changed") { |
| 164 | t.Fatalf("a tap naming another offer: %v", err) |
| 165 | } |
| 166 | rel, err := r.client.RunNow(ctx, id, wire.RunNow{Step: d.StepID, StepRevision: 1, Offer: wire.RunNowPlain}) |
| 167 | if err != nil || rel.Release == "" { |
| 168 | t.Fatalf("run now %+v %v", rel, err) |
| 169 | } |
| 170 | _, body = r.expect(wire.FrameRelease) |
| 171 | shape(t, "frames/release.json", r.runner.Received[len(r.runner.Received)-1]) |
| 172 | if c := body.(*wire.ReleaseCommand); c.ID != rel.Release || c.StepID != d.StepID || c.StepRevision != 1 || c.TaskID != id || c.Person != Owner { |
| 173 | t.Fatalf("release %+v", c) |
| 174 | } |
| 175 | // A second tap is told it is on its way, not answered as new (review S2). |
| 176 | if _, err := r.client.RunNow(ctx, id, wire.RunNow{Step: d.StepID, StepRevision: 1, Offer: wire.RunNowPlain}); Status(err) != 409 || !strings.Contains(err.Error(), "already_running") { |
| 177 | t.Fatalf("a second tap: %v", err) |
| 178 | } |
| 179 | if n := r.frames(wire.FrameRelease); n != 1 { |
| 180 | t.Fatalf("%d releases written", n) |
| 181 | } |
| 182 | // The card was answered by the tap. |
| 183 | snap, _ = r.client.Snapshot(ctx) |
| 184 | for _, c := range snap.Cards { |
| 185 | if c.ID == card.ID && c.State == wire.CardOpen { |
| 186 | t.Fatal("the held card stayed open after Run now") |
| 187 | } |
| 188 | } |
| 189 | // Admitted: a report without the hold starts the step and ends Needs |
| 190 | // you. |
| 191 | r.step("s1", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}}) |
| 192 | if k, _ = r.client.Task(ctx, id); k.Steps[len(k.Steps)-1].State != wire.StepRunning || k.Steps[len(k.Steps)-1].Hold != nil || k.NeedsYou != "" { |
| 193 | t.Fatalf("started: %+v needs %q", k.Steps[len(k.Steps)-1], k.NeedsYou) |
| 194 | } |
| 195 | |
| 196 | // A spending bar: Run now never passes it; the card offers Wait only. |
| 197 | other := taskID(t, r.call("q1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Another one", "brief": "b"})) |
| 198 | r.call("q2", wire.ToolStartTask, map[string]string{"task": other, "brief": "go"}) |
| 199 | _, body = r.expect(wire.FrameDispatch) |
| 200 | d2 := body.(*wire.DispatchCommand) |
| 201 | r.hold("h3", d2, wire.StepHold{Reason: wire.HoldSpendUnknown, Connection: "tensorx-1", Provider: "tensorx", NeedsPerson: wire.HoldNeedsNoReset, StepRevision: 1}) |
| 202 | if _, err := r.client.RunNow(ctx, other, wire.RunNow{Step: d2.StepID, StepRevision: 1}); Status(err) != 409 { |
| 203 | t.Fatalf("Run now past spend unknown: %v", err) |
| 204 | } |
| 205 | if page := pages.get(owner, "/tasks/"+other, 200); !strings.Contains(page, `data-pause-act="resume"`) || strings.Contains(page, `data-pause-act="run-now"`) { |
| 206 | t.Fatal("spend unknown offers Run now, or no Resume") |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // An older runner (before the scheduler's protocol) is refused Run now |
| 211 | // and a preference with an end, never handed one; a newer one gets the |
| 212 | // preference with its end, and once it passes it reads Automatic. |
| 213 | func TestContractSchedulerOlderRunner(t *testing.T) { |
| 214 | old := newToolRigAt(t, wire.ProtocolResearch) |
| 215 | id := taskID(t, old.call("o1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Old runner", "brief": "b"})) |
| 216 | old.call("o2", wire.ToolStartTask, map[string]string{"task": id, "brief": "go"}) |
| 217 | _, body := old.expect(wire.FrameDispatch) |
| 218 | d := body.(*wire.DispatchCommand) |
| 219 | old.hold("o3", d, wire.StepHold{Reason: wire.HoldNoConnection, NeedsPerson: wire.HoldNeedsNoConnection, StepRevision: 1}) |
| 220 | if _, err := old.client.RunNow(old.ctx, id, wire.RunNow{Step: d.StepID, StepRevision: 1}); Status(err) != 409 { |
| 221 | t.Fatalf("Run now on a protocol 13 runner: %v", err) |
| 222 | } |
| 223 | if n := old.frames(wire.FrameRelease); n != 0 { |
| 224 | t.Fatalf("%d releases for a protocol 13 runner", n) |
| 225 | } |
| 226 | until := time.Now().Add(time.Hour) |
| 227 | if _, err := old.client.SetPreferences(old.ctx, wire.Preferences{Provider: "codex", Until: &until}); Status(err) != 409 { |
| 228 | t.Fatalf("a preference with an end on a protocol 13 runner: %v", err) |
| 229 | } |
| 230 | |
| 231 | r := newToolRigAt(t, wire.ProtocolScheduler) |
| 232 | p, err := r.client.SetPreferences(r.ctx, wire.Preferences{Provider: "codex", Until: &until}) |
| 233 | if err != nil || p.Until == nil || p.Provider != "codex" { |
| 234 | t.Fatalf("preference %+v %v", p, err) |
| 235 | } |
| 236 | _, body = r.expect(wire.FramePreference) |
| 237 | shape(t, "frames/preference-until.json", r.runner.Received[len(r.runner.Received)-1]) |
| 238 | if c := body.(*wire.PreferenceCommand); c.Until == nil || c.Provider != "codex" { |
| 239 | t.Fatalf("preference frame %+v", c) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Security round #2: housekeeping's retry is dispatched as housekeeping |
| 244 | // (low, no release, whatever the task's priority) and it stays so on the |
| 245 | // record, for a placement after the housekeeping pass; Run now on it is |
| 246 | // refused, and its held card offers Wait only. |
| 247 | func TestContractSchedulerHousekeeping(t *testing.T) { |
| 248 | r := newToolRigAt(t, wire.ProtocolScheduler) |
| 249 | id, d := r.started("hk", "Retry me by housekeeping") |
| 250 | k := r.task(id) |
| 251 | if _, err := r.client.SetPriority(r.ctx, id, wire.SetPriority{Priority: wire.PriorityUrgent, Revision: k.Revision}); err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | r.expect(wire.FramePriority) |
| 255 | r.stop("hk1", d, "runner_restarted") |
| 256 | again := r.nextDispatch() |
| 257 | if again.Attempt != 2 || !again.Housekeeping || again.Priority != wire.PriorityLow || again.Release != "" { |
| 258 | t.Fatalf("housekeeping's retry %+v", again) |
| 259 | } |
| 260 | var attempt int64 |
| 261 | if err := r.inst.DB.Writer().QueryRow("SELECT housekeeping_attempt FROM chat_steps WHERE id = ?", again.StepID).Scan(&attempt); err != nil || attempt != 2 { |
| 262 | t.Fatalf("housekeeping provenance %d %v", attempt, err) |
| 263 | } |
| 264 | r.hold("hk2", again, wire.StepHold{Reason: wire.HoldNoConnection, NeedsPerson: wire.HoldNeedsNoConnection, StepRevision: 2}) |
| 265 | if _, err := r.client.RunNow(r.ctx, id, wire.RunNow{Step: again.StepID, StepRevision: 2}); Status(err) != 409 { |
| 266 | t.Fatalf("Run now on housekeeping's attempt: %v", err) |
| 267 | } |
| 268 | snap, _ := r.client.Snapshot(r.ctx) |
| 269 | for _, c := range snap.Cards { |
| 270 | if c.Kind == wire.CardHeld && c.TaskID == id && c.State == wire.CardOpen && len(c.Options) != 1 { |
| 271 | t.Fatalf("housekeeping's held card %+v", c) |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Security round #5: Research takes the task's priority and deadline on |
| 277 | // its dispatch, and its hold is kept (queued, not running) with Needs you |
| 278 | // and Run now, as for any step. |
| 279 | func TestContractSchedulerResearch(t *testing.T) { |
| 280 | r := newToolRigSaying(t, wire.ProtocolScheduler, personWords) |
| 281 | r.researchOn() |
| 282 | r.inventoryOn() |
| 283 | id := r.shopTask("a") |
| 284 | k := r.task(id) |
| 285 | if _, err := r.client.SetPriority(r.ctx, id, wire.SetPriority{Priority: wire.PriorityUrgent, Revision: k.Revision}); err != nil { |
| 286 | t.Fatal(err) |
| 287 | } |
| 288 | r.research("a-r", id) |
| 289 | r.begin("a-start", id) |
| 290 | d := r.nextDispatch() |
| 291 | if d.Specialist != wire.SpecialistResearch || d.Priority != wire.PriorityUrgent || d.PriorityRevision == 0 { |
| 292 | t.Fatalf("research dispatch %+v", d) |
| 293 | } |
| 294 | k = r.hold("a-h", d, wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-a", Provider: "claude", NeedsPerson: wire.HoldNeedsNoReset, StepRevision: 1}) |
| 295 | var research wire.Step |
| 296 | for _, s := range k.Steps { |
| 297 | if s.ID == d.StepID { |
| 298 | research = s |
| 299 | } |
| 300 | } |
| 301 | if research.State != wire.StepQueued || research.Hold == nil || k.NeedsYou != wire.NeedsHeld { |
| 302 | t.Fatalf("held Research %+v needs %q", research, k.NeedsYou) |
| 303 | } |
| 304 | // The tap carries what the task offered (review R1). |
| 305 | if research.Paused == nil || research.Paused.RunNow == nil { |
| 306 | t.Fatalf("no Run now offered on Research: %+v", research.Paused) |
| 307 | } |
| 308 | offer := research.Paused.RunNow.Connection |
| 309 | if research.Paused.RunNow.Plain { |
| 310 | offer = wire.RunNowPlain |
| 311 | } |
| 312 | if rel, err := r.client.RunNow(r.ctx, id, wire.RunNow{Step: d.StepID, StepRevision: 1, Offer: offer}); err != nil || rel.Release == "" { |
| 313 | t.Fatalf("Run now on Research: %+v %v", rel, err) |
| 314 | } |
| 315 | } |
| 316 | |