| new file mode 100644 |
| index 00000000..6da003f7 |
| --- /dev/null |
| +++ b/docs/superpowers/plans/S3-SCHEDULER-PROGRESS.md |
| @@ -0,0 +1,260 @@ |
| +# S3 progress: the scheduler |
| + |
| +Branch `connections-s3-scheduler` from main 379fe0d2 (S1 and S2 merged). |
| +Spec: `docs/superpowers/specs/2026-11-12-equipo-connectors-and-token-scheduling.md` |
| +revision 3.1 (on `connections-slice2-design`), §11 row S3: §0 decisions |
| +5 and 6, §6, §7, §8, §10 (S3's wire, API and fixtures) and §15 round 3 |
| +rows 4, 6 and 7. No real key, sign-in or provider: fakes only. |
| + |
| +## Done |
| + |
| +1. `internal/schedule`: the pure `Decide(Input) Decision` (§7). Bars |
| + (usable, not avoided, capable; metered: urgent or a release, slot |
| + free, spend known, under both budgets), thresholds 0.60/0.80/1.0 on |
| + the effective priority (a release is urgent), windows past their |
| + reset not reported, the fixed room precedence for subscriptions and |
| + for metered connections, the rules in order (kept session, preference, |
| + subscription, metered, hold), the hold naming the subscription whose |
| + reset comes first or the most actionable spending bar, wake-ups |
| + strictly after now (every capable connection's resets, the next budget |
| + day or month when the step has leave, the preference's end, `by` |
| + minus 24 h unless `deadline_notified`), and Needs you (deadline, no |
| + reset in 72 h, no_connection). Review's family rule is applied as |
| + tiers: another family, then another account, then Engineering's own. |
| +2. Wire, protocol 14 (`ProtocolScheduler`): dispatch `priority`, `by`, |
| + `release`; a step report's `hold` (`StepHold`: reason, connection, |
| + provider, resets_at, wake_at, needs_person, step_revision); frames |
| + `release` and `priority` (durable); `Preferences.until` and |
| + `PreferenceCommand.until`; record_task and start_task arguments |
| + `priority` and `by`. Below 14 Encode refuses the two frames, a held |
| + report and a preference with an end, and drops the dispatch fields and |
| + the tool arguments. Fixtures `frames/release.json`, |
| + `frames/priority.json`, `frames/step-held.json`, |
| + `frames/preference-until.json`, `frames/dispatch-urgent.json`, |
| + `api/task-held.json`, `api/card-held.json`, `api/priority-request.json`, |
| + `api/run-now-request.json`, `api/run-now-accepted.json`; the secret |
| + guards cover every new type and fixture. |
| +3. The runner (`thin/schedule.go`): every Engineering, Review and |
| + Research attempt, failover included, is admitted through Decide. |
| + Readings are refreshed before the call (an unchecked connection is |
| + checked; a reading over 15 minutes old goes through `Conns.Reading`, |
| + at most once per 15 minutes). The admission is one thin.db transaction |
| + that reads the facts again and decides again if they moved; a pending |
| + release is consumed there and kept as a grant for (step, |
| + step_revision); a metered admission commits in S1's guard transaction |
| + (`Meter.RunAdmitted`). Holds are kept with `wake_at` (or until a |
| + change), reported once per hold, woken at `wake_at` and on any reading, |
| + step end, budget, Resume, preference, priority or release. The release |
| + order (`nextStep`): urgent, earliest `by`, normal, low, oldest, step |
| + id. Questions stay off Decide and keep S1's guard. A kept metered |
| + session under a spending bar is held there; without leave it starts |
| + afresh on a subscription (S2's rule, kept). |
| +4. Leave to spend comes only from `urgent` or a grant. The test-build |
| + seam (`testLeaveToSpend`, `EQUIPO_TEST_LEAVE_TO_SPEND`) is gone: the Pi |
| + tests mark their tasks urgent; the release check no longer looks for a |
| + marker nothing can contain. |
| +5. The instance: `chat_tasks.priority`, `due_by`, `priority_set_by`; |
| + `chat_steps.hold`; `chat_releases` (one per step revision); migration |
| + 0073. record_task (and start_task, only on a task recorded in that |
| + turn with nothing set) take `low`/`normal` and `by`; `urgent` is |
| + refused. `PUT /api/v1/tasks/{id}/priority` is the creator's tap |
| + (actor from the session, roster in the transaction, revision, audit |
| + `task.priority`) and sends a `priority` frame to the runner holding the |
| + step. A held step stays queued with its hold; where the creator's input |
| + changes the outcome it is Needs you `held` with a `held` card ("I'd |
| + wait: Claude resets ..."), Wait or Run it now. `POST |
| + /api/v1/tasks/{id}/run-now {step, step_revision}`: the creator's, |
| + bound to the hold's revision, one release per revision (a second tap |
| + answers the first), audited `task.run_now`, refused on a budget or |
| + spend-unknown hold (409 `hold_bar`) and on a runner before 14 (409 |
| + `runner_too_old`). `PUT /connections/preference` takes `until` (a |
| + runner before 14 is refused one). Dispatches carry the priority, |
| + deadline and an open release; housekeeping's go as `low` with none. |
| +6. Pages: the task card and page show the hold line with Run it now |
| + anyway (headroom, busy, no_connection) or Raise the budget / Resume |
| + (links to Connections), the priority line ("Low priority · By Fri 20 |
| + Nov · set from your message") with Mark urgent / Not urgent, and the |
| + Needs you decision from the held card. |
| + |
| +## Tests |
| + |
| +- `internal/schedule`: `TestDecideRules` (a row per rule and bar), |
| + `TestDecideReviewFamily`, `TestDecideWakeUps`, `TestDecideNeedsYou`, |
| + `TestDecideInvariants` ((a) to (f) and wake-ups after now over 20,000 |
| + random inputs), `TestDecideOrderFree`. |
| +- `internal/wire`: `TestEncodeBelowProtocol14DropsScheduler`, |
| + `TestEncodeBelowProtocol14DropsToolPriority`, |
| + `TestSchedulerFramesStrict`, fixture round trips, secret guards. |
| +- `internal/runner/thin`: the finish line |
| + `TestScheduleLowHoldsThenStartsAfterReset` (a low step holds at 0.61, |
| + said once with a future wake-up, a normal step runs meanwhile, and the |
| + low one starts by itself after the reset), |
| + `TestScheduleReleaseOnceAndStaleRefused` (a stale, another person's |
| + and a replayed release start nothing; the consumed one is a grant), |
| + `TestScheduleFailoverInsideAGrant`, `TestSchedulePriorityChangeGivesLeave`, |
| + `TestScheduleReleaseOrder`, `TestScheduleAdmissionRechecksFacts`, |
| + `TestScheduleRefreshesStaleReadings`, `TestPreferenceUntilEnds`; the |
| + Pi tests now give leave by an urgent task (`TestPiReviewStep` also |
| + checks the admission row `metered_urgent`; |
| + `TestPiReviewWithoutLeaveToSpend` the headroom hold). |
| +- `internal/thintest`: `TestContractScheduler`, |
| + `TestContractSchedulerOlderRunner`; `internal/equipo/chat`: |
| + `TestSchedulerWritesCheckCreatorAndRoster`. |
| +- Local runs were targeted (`-run`) on the changed packages, plus |
| + gofmt, go vet, staticcheck on the changed packages, the darwin runner |
| + build, `make budget` (budget lines updated; `internal/wire` gets an |
| + exemption at 5032 lines) and the dead-reference scan. |
| + |
| +## Mutations, each observed failing |
| + |
| +### schedule mutations (all caught) |
| +- SM1 low threshold 0.70 -> TestDecideRules/low_holds_at_0.61 |
| +- SM2 windows past reset still counted -> TestDecideRules/a_window_past_its_reset_is_not_reported |
| +- SM3 leave always granted -> TestDecideRules/kept_metered_without_leave_moves; TestDecideInvariants (b) #34 metered without leave |
| +- SM4 budget bar off -> TestDecideRules/kept_metered_over_budget_holds_there; TestDecideInvariants (d) |
| +- SM5 busy-slot bar off -> TestDecideRules/kept_metered_busy_holds_there; TestDecideInvariants (c) |
| +- SM5b spend-unknown bar off -> TestDecideInvariants (d) |
| +- SM6 kept metered under a spending bar moved instead of held -> TestDecideRules/kept_metered_busy_holds_there (ran on b) |
| +- SM7 wake-up allowed at now -> TestDecideWakeUps, TestDecideInvariants (#76 a wake-up not after now) |
| +- SM8 rule 4 removed -> TestDecideRules/metered_only_with_urgent; TestDecideInvariants (e) held though a candidate admits |
| +- SM9 reported-before-unreported key dropped -> TestDecideRules/reported_before_unreported |
| +- SM10 preference until ignored -> TestDecideRules/an_expired_preference_is_absent |
| +- SM11 deadline check fires again after deadline_notified -> TestDecideNeedsYou |
| +- SM12 Review family tiers removed -> TestDecideReviewFamily |
| +- SM13 kept subscription not sticky -> TestDecideRules/kept_stays_whatever_the_threshold |
| +- SM14 limited always usable -> TestDecideRules/limited_is_not_usable |
| +- SM15 no 72 h bound on reset -> TestDecideNeedsYou (a reset 73 h away) |
| +- SM16 threshold compare > instead of >= -> TestDecideInvariants (a) admitted at 0.60 |
| +### wire mutations (TestEncodeBelowProtocol14DropsScheduler) |
| +- WM1 release frame not refused below 14 -> caught (release encoded at 13) |
| +- WM2 held step report not refused below 14 -> caught |
| +- WM3 dispatch priority kept below 14 -> caught (dispatch at 13) |
| +- WM4 preference with until not refused below 14 -> caught |
| +### runner mutations |
| +- RM1 release not consumed / no grant at admission -> TestScheduleReleaseOnceAndStaleRefused (release "pending", grants 0) |
| +- RM2 release accepted for a stale step revision -> TestScheduleReleaseOnceAndStaleRefused (a stale release was kept) |
| +- RM3 release not bound to the person -> TestScheduleReleaseOnceAndStaleRefused (another person's release was kept) |
| +- RM4 release kept for a revision that already holds a grant -> first MISSED (the replay arrived after the step ended, so the step check alone refused it); a case with the step still held and its revision granted was added, then caught |
| +- RM5 admission without the facts recheck -> TestScheduleAdmissionRechecksFacts (admitted on moved facts) |
| +- RM6 fact changes not waking held steps -> TestScheduleReleaseOnceAndStaleRefused, TestScheduleFailoverInsideAGrant (timed out) |
| +- RM7 hold said at every look -> first MISSED (nothing re-decided before the reset); the test now wakes the step three times, then caught (said 4 times) |
| +- RM8 stale readings never probed -> TestScheduleRefreshesStaleReadings (0 probes) |
| +- RM9 grant not read for later attempts -> TestScheduleFailoverInsideAGrant (the failover attempt held) |
| +- RM10 priority frame ignores its revision -> TestSchedulePriorityChangeGivesLeave (an older priority applied) |
| +- RM11 release order back to oldest first -> TestScheduleReleaseOrder |
| +- RM12 preference until ignored -> TestPreferenceUntilEnds |
| +- RM13 dispatch's scheduler facts not recorded -> TestScheduleLowHoldsThenStartsAfterReset (ran at 0.61 as normal) |
| +- RM14 runPi not writing the scheduler's admission in the guard's transaction -> TestPiReviewStep (no admission row) |
| +### instance mutations |
| +- IM1 coordinator allowed urgent -> TestContractScheduler (the coordinator set urgent) |
| +- IM2 coordinator priority changeable after creation (start_task) -> TestContractScheduler (dispatch / start_task changed it) |
| +- IM3 priority revision not checked -> TestContractScheduler (a stale revision accepted) |
| +- IM4 priority/Run now creator check removed -> first MISSED in thintest (the rig cannot make a second owner, and isOwner refused the teammate first); TestSchedulerWritesCheckCreatorAndRoster (chat store, two owners) added, then caught for both |
| +- IM5 roster check left out of SetPriority -> TestSchedulerWritesCheckCreatorAndRoster (a removed creator's priority) |
| +- IM6 priority audit left out -> TestSchedulerWritesCheckCreatorAndRoster (audits []) |
| +- IM7 priority frame not sent -> TestContractScheduler (no priority frame; timed out) |
| +- IM8 release not bound to the held step revision -> TestContractScheduler (a stale step revision accepted) |
| +- IM9 second tap writes a second release -> TestContractScheduler (a second tap) |
| +- IM10 Run now passes a spending bar -> TestContractScheduler (Run now past spend unknown) |
| +- IM11 Run now to a protocol 13 runner -> TestContractSchedulerOlderRunner |
| +- IM12 preference with an end to a protocol 13 runner -> TestContractSchedulerOlderRunner |
| +- IM13 dispatch without priority -> TestContractScheduler (dispatch) |
| +- IM14 a held step marked running -> TestContractScheduler (held: state running) |
| +- IM15 Needs you held dropped -> TestContractScheduler (needs "") |
| +- IM16 held card left open after Run now -> TestContractScheduler |
| +- IM17 page offers Run it now on a spending bar -> TestContractScheduler (spend unknown offers Run now) |
| + |
| +## Decisions where the spec was open |
| + |
| +- The step revision is the attempt the instance dispatched (runner |
| + column `steps.revision`); the runner's failover attempts keep it, a |
| + Retry is a new revision. The hold carries it, Run now names it. |
| +- The held report is state `running` with `hold` (the instance keeps the |
| + step queued until a report without it), not a new `deferred` state an |
| + older instance would refuse; below 14 it is not sent. |
| +- `busy` is a hold reason too: a kept metered session whose slot is |
| + taken (§7 rule 1) and an admission race. |
| +- The admission rechecks by reading the facts again inside its |
| + transaction and comparing, not by per-row revision columns; the metered |
| + slot, spend and budget are rechecked by S1's guard in that same |
| + transaction. |
| +- A `priority` frame (not in §10) carries a priority change to a step |
| + the runner already holds, so marking a held task urgent takes effect at |
| + once. |
| +- Research steps go through Decide too (coordinator's family first, as |
| + before; never metered). Coordinator turns do not (person turns are |
| + never held). Housekeeping turns are not dispatched today |
| + (`TurnHousekeeping` is never created); housekeeping's step dispatches |
| + go as `low` with no release. |
| +- The preference stays one provider for every role (as built); `until` |
| + is kept beside it, and an expired preference reads as Automatic on |
| + both ends rather than being cleared by a write. |
| +- A deadline given as a date is the end of that day, UTC; at most a year |
| + ahead. start_task's priority applies only to a task recorded in the |
| + same turn with nothing set. |
| +- Run now is refused on a budget or spend-unknown hold (it never passes |
| + a bar); those holds link to the Connections page for Raise the budget |
| + and Resume, and their card offers Wait only. |
| +- The budget day and month boundaries are wake-ups only for a step with |
| + leave and a capable metered connection. Spend unknown has no reset, so |
| + it is Needs you. |
| + |
| +## Open, or not as the spec says |
| + |
| +- No reading probe exists that is not a model turn: Claude reports |
| + headroom only on a turn, and Codex's `account/rateLimits/read` runs |
| + only at sign-in. The refresh is built and tested with a fake |
| + (`Conns.Reading`), but is nil in production, so a reading older than |
| + 15 minutes is used as last reported (a window past its reset counts as |
| + not reported). A Codex probe over the app-server is the follow-up. |
| +- No browser test of the new forms (the pages are checked server-side). |
| +- The staging witness waits for a release (below). |
| + |
| +## New copy keys (pending, `COPY REVIEW REQUIRED`, in the Plan 3 inventory) |
| + |
| +- `work.hold.headroom`: Waiting for headroom on %s |
| +- `work.hold.headroom_reset`: Waiting for headroom on %s, resets %s |
| +- `work.hold.budget`: Waiting: %s has reached today’s budget |
| +- `work.hold.spend_unknown`: Waiting: %s spend unknown, check it, then Resume |
| +- `work.hold.busy`: Waiting: %s is running another step |
| +- `work.hold.no_connection`: Waiting: nothing you connected can run it without spending |
| +- `work.hold.run_now`: Run it now anyway |
| +- `work.hold.raise_budget`: Raise the budget |
| +- `work.hold.resume`: Resume |
| +- `work.priority.urgent`: Urgent |
| +- `work.priority.normal`: Normal priority |
| +- `work.priority.low`: Low priority |
| +- `work.priority.by`: By %s |
| +- `work.priority.from_message`: set from your message |
| +- `work.priority.mark_urgent`: Mark urgent |
| +- `work.priority.make_normal`: Not urgent |
| +- `chat.held.wait.before`: I’d wait: %s resets %s, before %s. |
| +- `chat.held.wait.reset`: I’d wait: %s resets %s. |
| +- `chat.held.run.after_by`: I’d run it now: %s resets %s, after %s. |
| +- `chat.held.run.no_reset`: I’d run it now: %s has no reset in the next three days. |
| +- `chat.held.run.no_connection`: I’d run it now: only a metered connection can run it. |
| +- `chat.held.spend_unknown`: %s spend is unknown. Check it, then Resume. |
| +- `chat.held.budget`: %s has reached its budget. Raise it, or wait for tomorrow. |
| +- `chat.card.wait_reset`: Wait |
| +- `chat.card.run_now`: Run it now |
| + |
| +## How Paul tries it |
| + |
| +On staging, once this is released (Pi is already installed on the |
| +owner runner): |
| + |
| +1. Settings, Connections, Add a connection, TensorX: tick "This key has |
| + a spending limit at TensorX" (a small prepaid balance, no automatic |
| + top-up), keep the $1 a day and $10 a month budget, paste the key and |
| + pick `z-ai/glm-5.3`. The one-token probe makes it Ready and shows on |
| + its spend line. |
| +2. Ask the coordinator for a small change on a test repository. On the |
| + task page tap Mark urgent (or, when a step shows "Waiting for headroom |
| + on Claude", tap Run it now anyway). With Claude doing Engineering, |
| + Review goes to another family first, so it runs on TensorX. |
| +3. What to see: the Review step on TensorX, family glm, label |
| + other_family; the Connections spend line moved by that step's cost; |
| + once today's spend reaches $1 the next step waits with "TensorX has |
| + reached today's budget", Raise the budget, and the row says "Paused |
| + until tomorrow". Ask the Review a question: it answers on TensorX, |
| + charged. Mark the task Not urgent and a normal step never spends. |