| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Paused work for Base (spec 2026-11-24-paused-work-design.md, decision |
| 14 | // 5): a job whose only task is held for the Claude allowance, with Codex |
| 15 | // free, is paused until the reset, in the person's zone; Carry on now |
| 16 | // runs the held step now (Run now's release) once per key; a client that |
| 17 | // does not ask for paused job cards reads it as working with the same |
| 18 | // words; and the person's zone moves the words. |
| 19 | |
| 20 | // pausedConnections gives the owner's runner a Claude connection at its |
| 21 | // limit until reset and a Codex one with room. |
| 22 | func (o *teamRig) pausedConnections(reset time.Time) { |
| 23 | o.t.Helper() |
| 24 | var runner string |
| 25 | if err := o.inst.DB.Writer().QueryRow("SELECT runner_id FROM runner_sessions LIMIT 1").Scan(&runner); err != nil { |
| 26 | o.t.Fatal(err) |
| 27 | } |
| 28 | room := reset.Add(72 * time.Hour) |
| 29 | at := o.inst.Store.Chat().Now() |
| 30 | for _, c := range []struct{ id, local, provider, state string }{{"cn_claude", "claude-1", "claude", wire.ConnLimited}, {"cn_codex", "codex-1", "codex", wire.ConnHealthy}} { |
| 31 | used, resets := 100.0, reset |
| 32 | if c.provider == "codex" { |
| 33 | used, resets = 12, room |
| 34 | } |
| 35 | h, _ := json.Marshal(wire.Headroom{ResetsAt: &resets, Windows: []wire.Window{{Name: "five_hour", UsedPercent: used, Minutes: 300, ResetsAt: &resets}}}) |
| 36 | o.exec("INSERT INTO chat_connections(id, person, runner_id, local_id, provider, kind, label, state, headroom, revision, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,1,?,?)", |
| 37 | c.id, Owner, runner, c.local, c.provider, wire.KindToken, "", c.state, string(h), at, at) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // jobKnowing is the request's job card as a client that knows paused |
| 42 | // reads it (the snapshot with job_states=paused). |
| 43 | func (o *teamRig) jobKnowing(request, what string, match func(wire.AssistantCard) bool) wire.AssistantCard { |
| 44 | o.t.Helper() |
| 45 | var out wire.AssistantCard |
| 46 | o.until(what, func() bool { |
| 47 | id := o.jobCardID(request) |
| 48 | snap, err := o.a.SnapshotKnowing(o.ctx, wire.JobPaused) |
| 49 | if err != nil { |
| 50 | o.t.Fatal(err) |
| 51 | } |
| 52 | for _, c := range snap.Cards { |
| 53 | if c.ID == id && match(c) { |
| 54 | out = c |
| 55 | return true |
| 56 | } |
| 57 | } |
| 58 | return false |
| 59 | }) |
| 60 | return out |
| 61 | } |
| 62 | |
| 63 | func TestContractPausedJob(t *testing.T) { |
| 64 | for _, hour := range []int{2, 12, 23} { |
| 65 | t.Run(itoa(hour), func(t *testing.T) { |
| 66 | testContractPausedJobAt(t, time.Date(2026, time.November, 24, hour, 30, 0, 0, time.UTC)) |
| 67 | }) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func testContractPausedJobAt(t *testing.T, now time.Time) { |
| 72 | o := newPausedTeamRig(t, now) |
| 73 | reset := now.Add(9 * time.Hour) |
| 74 | o.pausedConnections(reset) |
| 75 | request, id, d := o.startedInThread("pz") |
| 76 | o.reply(o.handoffTurn(request)) |
| 77 | o.endUpdates() |
| 78 | o.step("pz-hold", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, Findings: []wire.Finding{}, Hold: &wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-1", Provider: "claude", ResetsAt: &reset, WakeAt: &reset, StepRevision: 1}}) |
| 79 | |
| 80 | dublin, _ := time.LoadLocation("Europe/Dublin") |
| 81 | until := func(loc *time.Location) string { |
| 82 | return strings.Replace(equipo.RunnerCopy("assistant.job.paused"), "%s", reset.In(loc).Format("15:04"), 1) |
| 83 | } |
| 84 | paused := o.jobKnowing(request, "paused", func(c wire.AssistantCard) bool { return c.State == wire.JobPaused }) |
| 85 | if paused.Result != until(dublin) || !paused.CarryOn || paused.StartedAt == nil || !paused.StartedAt.Equal(now) { |
| 86 | t.Fatalf("paused %+v", paused) |
| 87 | } |
| 88 | if !strings.Contains(paused.Detail, reset.In(dublin).Format("15:04")) || !strings.Contains(paused.Detail, "(in 9 hours)") { |
| 89 | t.Fatalf("detail %q", paused.Detail) |
| 90 | } |
| 91 | for _, word := range []string{"Claude", "Codex", "runner", "Equipo", "allowance", "computer", "UTC"} { |
| 92 | if strings.Contains(paused.Result+" "+paused.Detail, word) { |
| 93 | t.Fatalf("Base's words name %q: %q %q", word, paused.Result, paused.Detail) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // A client that does not know paused: working, the same words, no |
| 98 | // Carry on now; on the stream too. |
| 99 | older := o.jobOf(request, "as working", func(c wire.AssistantCard) bool { return c.ID == paused.ID }) |
| 100 | if older.State != wire.JobWorking || older.Result != paused.Result || older.Detail != paused.Detail || older.CarryOn { |
| 101 | t.Fatalf("older client %+v", older) |
| 102 | } |
| 103 | for _, knows := range []bool{false, true} { |
| 104 | var s *Stream |
| 105 | var err error |
| 106 | if knows { |
| 107 | s, err = o.a.OpenKnowing(o.ctx, wire.JobPaused, 0) |
| 108 | } else { |
| 109 | s, err = o.a.Open(o.ctx, 0) |
| 110 | } |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | o.tick() |
| 115 | want := wire.JobWorking |
| 116 | if knows { |
| 117 | want = wire.JobPaused |
| 118 | } |
| 119 | // The card's newest event on the stream, once it is quiet. |
| 120 | got := "" |
| 121 | quiet := time.NewTimer(5 * time.Second) |
| 122 | read: |
| 123 | for { |
| 124 | select { |
| 125 | case e := <-s.Events: |
| 126 | var c wire.AssistantCard |
| 127 | if e.Type == wire.EventCard && json.Unmarshal(e.Data, &c) == nil && c.ID == paused.ID { |
| 128 | got = c.State |
| 129 | quiet.Reset(300 * time.Millisecond) |
| 130 | } |
| 131 | case <-quiet.C: |
| 132 | break read |
| 133 | } |
| 134 | } |
| 135 | s.Close() |
| 136 | if got != want { |
| 137 | t.Fatalf("stream (knows %v): %s, want %s", knows, got, want) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // The person's zone moves the words. |
| 142 | if z, err := o.a.PutTimeZone(o.ctx, "Asia/Tokyo"); err != nil || z.TimeZone != "Asia/Tokyo" { |
| 143 | t.Fatalf("zone %+v %v", z, err) |
| 144 | } |
| 145 | if _, err := o.a.PutTimeZone(o.ctx, "Mars/Olympus"); Status(err) != 422 { |
| 146 | t.Fatalf("an unknown zone: %v", err) |
| 147 | } |
| 148 | o.tick() |
| 149 | tokyo, _ := time.LoadLocation("Asia/Tokyo") |
| 150 | paused = o.jobKnowing(request, "in Tokyo", func(c wire.AssistantCard) bool { return c.State == wire.JobPaused && c.Result == until(tokyo) }) |
| 151 | if _, err := o.a.PutTimeZone(o.ctx, "Europe/Dublin"); err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | // Base's team zone never overrides the person's own device's: the |
| 155 | // answer is the zone in effect. |
| 156 | var device wire.TimeZone |
| 157 | if err := o.client.do(o.ctx, "PUT", "/api/v1/time-zone", wire.TimeZone{TimeZone: "Europe/Dublin"}, &device); err != nil || device.TimeZone != "Europe/Dublin" { |
| 158 | t.Fatalf("the device's zone: %+v %v", device, err) |
| 159 | } |
| 160 | if z, err := o.a.PutTimeZoneIfUnset(o.ctx, "Asia/Tokyo"); err != nil || z.TimeZone != "Europe/Dublin" { |
| 161 | t.Fatalf("only_if_unset over the device's zone: %+v %v", z, err) |
| 162 | } |
| 163 | o.tick() |
| 164 | paused = o.jobKnowing(request, "back in Dublin", func(c wire.AssistantCard) bool { return c.State == wire.JobPaused && c.Result == until(dublin) }) |
| 165 | |
| 166 | // Carry on now: a stale revision is refused with the card; then the |
| 167 | // release for the held step, once per key. |
| 168 | if _, err := o.a.CarryOn(o.ctx, paused.ID, paused.Revision-1, "k1"); Status(err) != 409 || !strings.Contains(err.Error(), "card_stale") { |
| 169 | t.Fatalf("stale: %v", err) |
| 170 | } |
| 171 | if n := o.rows("SELECT count(*) FROM chat_releases"); n != 0 { |
| 172 | t.Fatalf("a refused carry on wrote %d releases", n) |
| 173 | } |
| 174 | out, err := o.a.CarryOn(o.ctx, paused.ID, paused.Revision, "k1") |
| 175 | if err != nil || out.Card.ID != paused.ID || out.Card.State != wire.JobWorking || out.Card.CarryOn { |
| 176 | t.Fatalf("carry on %+v %v", out, err) |
| 177 | } |
| 178 | _, body := o.expect(wire.FrameRelease) |
| 179 | if rel := body.(*wire.ReleaseCommand); rel.TaskID != id || rel.StepID != d.StepID || rel.StepRevision != 1 { |
| 180 | t.Fatalf("release %+v", rel) |
| 181 | } |
| 182 | again, err := o.a.CarryOn(o.ctx, paused.ID, paused.Revision, "k1") |
| 183 | if err != nil || again.Card.ID != paused.ID || o.rows("SELECT count(*) FROM chat_releases") != 1 { |
| 184 | t.Fatalf("the same key again %+v %v", again, err) |
| 185 | } |
| 186 | // Working now: another key is not_paused, with the card. |
| 187 | if _, err := o.a.CarryOn(o.ctx, paused.ID, out.Card.Revision, "k2"); Status(err) != 409 || !strings.Contains(err.Error(), "not_paused") { |
| 188 | t.Fatalf("not paused: %v", err) |
| 189 | } |
| 190 | // Not a job card, or not one at all: not found. |
| 191 | if _, err := o.a.CarryOn(o.ctx, "c_nothing", 1, "k3"); Status(err) != 404 { |
| 192 | t.Fatalf("no card: %v", err) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // With nothing else to run on, the job is paused without Carry on now, |
| 197 | // and Carry on now is refused as unavailable. |
| 198 | func TestContractPausedJobNoCarryOn(t *testing.T) { |
| 199 | now := time.Date(2026, time.November, 24, 2, 30, 0, 0, time.UTC) |
| 200 | o := newPausedTeamRig(t, now) |
| 201 | reset := now.Add(3 * time.Hour) |
| 202 | o.pausedConnections(reset) |
| 203 | // Every other connection the rig has is at its limit too. |
| 204 | o.exec("UPDATE chat_connections SET state = ? WHERE id <> 'cn_claude'", wire.ConnLimited) |
| 205 | request, id, d := o.startedInThread("pn") |
| 206 | o.reply(o.handoffTurn(request)) |
| 207 | o.endUpdates() |
| 208 | o.step("pn-hold", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, Findings: []wire.Finding{}, Hold: &wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-1", Provider: "claude", ResetsAt: &reset, WakeAt: &reset, StepRevision: 1}}) |
| 209 | paused := o.jobKnowing(request, "paused", func(c wire.AssistantCard) bool { return c.State == wire.JobPaused }) |
| 210 | if paused.CarryOn { |
| 211 | t.Fatalf("carry on with nothing to run on: %+v", paused) |
| 212 | } |
| 213 | if _, err := o.a.CarryOn(o.ctx, paused.ID, paused.Revision, "k1"); Status(err) != 409 || !strings.Contains(err.Error(), "carry_on_unavailable") { |
| 214 | t.Fatalf("unavailable: %v", err) |
| 215 | } |
| 216 | if n := o.rows("SELECT count(*) FROM chat_releases"); n != 0 { |
| 217 | t.Fatalf("%d releases", n) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // newPausedTeamRig keeps the allowance, projection and housekeeping on |
| 222 | // one clock. The generic team rig moves housekeeping to noon, which can |
| 223 | // otherwise sweep work created hours earlier by the wall clock. |
| 224 | func newPausedTeamRig(t *testing.T, now time.Time) *teamRig { |
| 225 | t.Helper() |
| 226 | o := newTeamRigAt(t, wire.ProtocolCurrent) |
| 227 | o.inst.Store.Chat().SetClock(func() time.Time { return now }) |
| 228 | o.inst.Store.Chat().SetHousekeepingClock(nil) |
| 229 | return o |
| 230 | } |
| 231 | |