| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // A pinned Run now (protocol 29, paused work B1): the person was told it |
| 11 | // would run on one connection. Both Claude accounts are over the normal |
| 12 | // threshold (the step holds), and with leave both admit; unpinned leave |
| 13 | // takes the roomier claude-a. Pinned to claude-b it runs on claude-b, so |
| 14 | // a runner that ignored the pin fails here (re-review nit). |
| 15 | func TestSchedulePinnedReleaseRunsOnlyThere(t *testing.T) { |
| 16 | r := newSchedRig(t, "claude-a", "claude-b") |
| 17 | r.tensorx(bigBudget) |
| 18 | r.reading("claude-a", 0.82, time.Now().Add(9*time.Hour)) |
| 19 | r.reading("claude-b", 0.9, time.Now().Add(9*time.Hour)) |
| 20 | r.start() |
| 21 | r.dispatch("p1", wire.PriorityNormal) |
| 22 | r.until(5*time.Second, "the hold", func() bool { return len(r.reports("p1")) > 0 }) |
| 23 | if kept, err := r.state.Release(r.ctx, "fr-p1", wire.ReleaseCommand{ID: "rl_p1", Person: connPerson, TaskID: "tk-p1", StepID: "p1", StepRevision: 1, Connection: "claude-b"}); err != nil || !kept { |
| 24 | t.Fatalf("release %v %v", kept, err) |
| 25 | } |
| 26 | r.steps.MeteredChanged() |
| 27 | r.until(5*time.Second, "the released step", func() bool { _, ok := r.final("p1"); return ok }) |
| 28 | if on := r.ranOn("p1", 1); on != "claude-b" { |
| 29 | t.Fatalf("a release pinned to claude-b ran on %q", on) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Only a metered connection is free: both subscriptions are full and |
| 34 | // TensorX has budget. A release pinned to a subscription keeps holding; |
| 35 | // unpinned leave would bill TensorX. |
| 36 | func TestSchedulePinnedReleaseNeverBillsMetered(t *testing.T) { |
| 37 | r := newSchedRig(t, "claude-a", "claude-b") |
| 38 | r.tensorx(bigBudget) |
| 39 | r.reading("claude-a", 1.0, time.Now().Add(9*time.Hour)) |
| 40 | r.reading("claude-b", 1.0, time.Now().Add(9*time.Hour)) |
| 41 | r.start() |
| 42 | r.dispatch("p4", wire.PriorityNormal) |
| 43 | r.until(5*time.Second, "the hold", func() bool { return len(r.reports("p4")) > 0 }) |
| 44 | if _, err := r.state.Release(r.ctx, "fr-p4", wire.ReleaseCommand{ID: "rl_p4", Person: connPerson, TaskID: "tk-p4", StepID: "p4", StepRevision: 1, Connection: "claude-b"}); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | r.steps.MeteredChanged() |
| 48 | time.Sleep(300 * time.Millisecond) |
| 49 | if _, done := r.final("p4"); done || r.ranOn("p4", 1) != "" { |
| 50 | t.Fatalf("a release pinned to a full subscription ran on %q", r.ranOn("p4", 1)) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // The reading was stale: the pinned connection filled before the runner |
| 55 | // decided. The step keeps holding, on its own connection's reset, and |
| 56 | // TensorX is never billed in its place; a failover inside a pinned grant |
| 57 | // holds too rather than moving to a metered connection. |
| 58 | func TestSchedulePinnedReleaseNeverFallsBack(t *testing.T) { |
| 59 | r := newSchedRig(t, "claude-a", "claude-b") |
| 60 | r.tensorx(bigBudget) |
| 61 | r.reading("claude-a", 0.85, time.Now().Add(9*time.Hour)) |
| 62 | r.reading("claude-b", 1.0, time.Now().Add(9*time.Hour)) |
| 63 | r.start() |
| 64 | r.dispatch("p2", wire.PriorityNormal) |
| 65 | r.until(5*time.Second, "the hold", func() bool { return len(r.reports("p2")) > 0 }) |
| 66 | if _, err := r.state.Release(r.ctx, "fr-p2", wire.ReleaseCommand{ID: "rl_p2", Person: connPerson, TaskID: "tk-p2", StepID: "p2", StepRevision: 1, Connection: "claude-b"}); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | r.steps.MeteredChanged() |
| 70 | time.Sleep(300 * time.Millisecond) |
| 71 | if _, done := r.final("p2"); done || r.ranOn("p2", 1) != "" { |
| 72 | t.Fatalf("a release pinned to a full Claude account ran on %q", r.ranOn("p2", 1)) |
| 73 | } |
| 74 | var state string |
| 75 | r.state.db.QueryRow("SELECT state FROM releases WHERE id='rl_p2'").Scan(&state) |
| 76 | if state != releasePending { |
| 77 | t.Fatalf("the pinned release is %q, not still pending", state) |
| 78 | } |
| 79 | |
| 80 | // A failover inside a pinned grant: Claude refuses on quota; the next |
| 81 | // attempt may not go to TensorX. |
| 82 | r.fail["claude-a"] = true |
| 83 | r.dispatch("p3", wire.PriorityNormal) |
| 84 | r.until(5*time.Second, "the third hold", func() bool { return len(r.reports("p3")) > 0 }) |
| 85 | if _, err := r.state.Release(r.ctx, "fr-p3", wire.ReleaseCommand{ID: "rl_p3", Person: connPerson, TaskID: "tk-p3", StepID: "p3", StepRevision: 1, Connection: "claude-a"}); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | r.steps.MeteredChanged() |
| 89 | r.until(5*time.Second, "the first attempt", func() bool { return r.ranOn("p3", 1) == "claude-a" }) |
| 90 | time.Sleep(300 * time.Millisecond) |
| 91 | if on := r.ranOn("p3", 2); on == piLocal { |
| 92 | t.Fatal("a failover inside a pinned grant billed TensorX") |
| 93 | } |
| 94 | } |
| 95 | |