# S4 progress: the Usage page and advice

Branch `connections-s4-usage` from main 75ba4863 (S1, S2 and S3 merged).
Spec: `docs/superpowers/specs/2026-11-12-equipo-connectors-and-token-scheduling.md`,
now on this branch as **revision 3.2**, §11 row S4: §9 (advice), §2.3's
spend line, §10's advice API, and the Usage page of the Connections rev 3
spec §6 (availability now, spend this month, token use by connection,
role, project and task; the sidebar dot). Also the controller's rulings
on S3's review: #7 (reading freshness) built, #8 (a kept metered
session moving) a spec amendment only. No real key, sign-in or
provider: fakes only.

## Done

1. **Readings and their age (ruling #7).** Protocol 15
   (`ProtocolReadings`): `Headroom.checked_at`, the time the runner took
   a reading. `Conns.Observe` and `Conns.Limit` stamp every reading
   (a turn's, a notification's, a probe's) with a copy, so the caller's
   value is untouched; a reading that says the same again moves only its
   time and wakes no held step. The scheduler's staleness check reads the
   reading's own time (the row's write time only for a row from before).
   Below 15, `Encode` drops the time from hello, sync and connection
   reports (`downgradeReadings`), and the runner's first hello never
   carries it (`WithoutReadingTimes`, a prompt hello after a 15 welcome).
2. **The Codex reading probe (ruling #7).** `provider.ReadCodexLimits`:
   the login probe's unconfined app-server in the source home, its
   environment and egress, `initialize`, `account/read` then
   `account/rateLimits/read`, no thread and no turn. `thin.CodexReading`
   runs it through `provider.OpenRuntimeCopy` (the hourly refresh's lock
   and identity check) and is now `Conns.Reading` in production, so a
   Codex reading older than 15 minutes is refreshed before `Decide`, at
   most once per 15 minutes a connection. Claude, token and metered
   connections return nothing: Claude stays as last reported.
3. **`internal/advice`**, pure: `Compute(facts, now)` gives every piece
   of advice whose facts hold, in §9's order (kind; a connection's id, a
   role's fixed place, a task set's earliest task then id; the key);
   `Choose` applies the rules (none while one is open, one new card a
   day in the person's zone counted on the day it shows, Not now silences
   a key 14 days and 60 after a second); `Holds` says whether open
   advice still holds. The four kinds with their triggers as §9 says.
4. **The instance** (`chat/advice.go`, migration 0074): the forward-only
   `chat_advice` table (open, then acted, dismissed or withdrawn);
   `RefreshAdvice` on the housekeeping loop's minute (so on fact changes
   within a minute, and at 08:00): open advice whose facts stopped
   holding is withdrawn silently (its card resolved, nothing said), open
   advice whose facts moved but hold follows them at a new revision, and
   with none open the first eligible key not silenced becomes the day's
   card, held through quiet hours with housekeeping's `VisibleAt`. One
   quiet `advice` card in the conversation, never Needs you, never a
   push; the coordinator has no tool for it. Actions are the ordinary
   calls, in the advice's transaction, at the revision the advice read,
   after checking the facts still hold: `SetBudget`, `SetPreference`
   (with `until`), `SetPriority` per task (each through a `txn` form of
   the store call). Each is audited as the call and as `advice.act` (or
   `advice.dismiss`) with `source=advice`, the advice id and its key.
   `chat_steps.held_at` keeps when a hold began. API: `GET
   /api/v1/advice`, `POST /api/v1/advice/{id}/act`, `POST
   /api/v1/advice/{id}/dismiss` (409 `revision_conflict` when it moved,
   was answered, or its facts no longer hold), and `GET
   /api/v1/usage?period=&by=`. Fixtures `api/advice.json`,
   `api/advice-action.json`, `api/card-advice.json`,
   `api/usage-month.json`, `frames/hello-readings.json`.
5. **The Usage page** (`/usage`, `usage_page.go`, `usage.html`): advice
   at the top with its action and Not now; available now (each
   connection's state, its provider's meters and the reading's age, "as
   of 2 h ago" for Codex, "as last reported 2 h ago" for Claude, none
   from an older runner; "Headroom not reported" otherwise); spend this
   month (S1's spend line per metered connection, reported by its
   harness, where prices come from read from the price table's version:
   "TensorX prices taken from the public page on 12 Nov 2026.";
   subscriptions as plan and tokens, never money); token use with tabs
   (today, this week, this month) by connection, role, project or task,
   columns input, cached input, output and the estimate where priced.
   The sidebar has Usage with a dot when a connection is limited or needs
   a sign-in, or a metered one is paused. A step held for headroom says
   its reading's age on its line ("… · as last reported 2 h ago").
6. **Spec revision 3.2** on this branch, with its Astra findings files
   and the dead-reference allowlist entries: §6, §7, §12 and the status
   line for ruling #7; §7 rule 1 adds spending permission withdrawn as a
   switch reason for a kept session (ruling #8, no code change); §10's
   `Headroom.checked_at`.

## Tests

- `internal/advice`: `TestEachKindFiresOnce` (each kind from its fixture,
  only it), `TestEachKindsLine` (each trigger just short of its line),
  `TestTiesPickTheSameCard` (all kinds at once, 500 shuffles, the same
  order and the same card), `TestNotNowSilences` (14 days, then 60),
  `TestNoDayShowsTwoCards` (open, acted, withdrawn or dismissed today;
  quiet hours; the person's zone).
- `internal/equipo/chat`: `TestAdviceStoreActsThroughTheOrdinaryCall`,
  `TestAdviceStoreNotNowAndWithdrawal`, `TestAdviceStoreQuietHours`,
  `TestAdviceStoreTaskActions`, `TestAdviceStorePreferSwitch`,
  `TestAcceptHoldKeepsHeldSince`.
- `internal/equipo`: `TestUsagePage`, `TestPriceSource`,
  `TestUsageAttention`, `TestHoldLineSaysTheReadingsAge`.
- `internal/wire`: `TestEncodeBelowProtocol15DropsReadingTimes` (frozen
  protocol 14 headroom), `TestWithoutReadingTimes`,
  `TestAdviceTypesCarryNoSecrets`, fixture round trips.
- `internal/runner`: `TestReadCodexLimitsWithoutTurn` (provider: a fake
  app-server that fails on anything but its four requests),
  `TestCodexReadingInProduction`, `TestScheduleRefreshUsesReadingTime`,
  `TestHandshakeHelloWithholdsReadingTimes` (thin).
- `internal/thintest`: `TestContractUsage` (page, advice in both places,
  API, act from the page reaching the runner, the hold line's age, the
  dot), `TestContractUsageOlderRunner` (protocol 14).
- `internal/headless` (`-tags browser`): `TestBrowserUsageAndAdvice` at
  1280 and 390: the sidebar, the card at the top, the action in reach,
  the reading age, the spend line, the tabs, no sideways scroll; Raise
  today's budget reaches the runner at twice the figure.
- `internal/plan3test`: the four person API routes registered.
- Local runs were targeted (`-run`) on the changed packages, plus gofmt,
  go vet, staticcheck on the changed packages, `make budget` (lines
  updated), `scripts/ci-test.py check` and the dead-reference scan.

## Mutations, each observed failing

### wire and runner
- WM1 readings downgrade removed -> TestEncodeBelowProtocol15DropsReadingTimes (hello carries checked_at)
- PM1 reading probe asks turn/start -> TestReadCodexLimitsWithoutTurn
- PM2 reading probe skips rateLimits/read -> TestReadCodexLimitsWithoutTurn
- RM1 Observe/Limit not stamping the reading time -> TestCodexReadingInProduction (CheckedAt nil), TestScheduleRefreshUsesReadingTime
- RM2 staleness read from the row's updated_at, not the reading's time -> TestScheduleRefreshUsesReadingTime (0 probes)
- RM3 a reading that says the same again counted as new (time compared) -> TestCodexReadingInProduction (woke 1)
- RM4 CodexReading reads any provider (Claude too) -> TestCodexReadingInProduction (claude-1 read)
- RM5 first hello carries the reading times -> TestHandshakeHelloWithholdsReadingTimes (handshake hello)
- RM6 no prompt hello after a protocol 15 welcome -> TestHandshakeHelloWithholdsReadingTimes
### advice (internal/advice)
- AM1 near-budget line moved above 80% -> TestEachKindFiresOnce (budget_near: []), TestEachKindsLine
- AM2 first Not now silences 7 days -> TestNotNowSilences (day 13 shown)
- AM3 second Not now silences 14 days, not 60 -> TestNotNowSilences (day 29 shown)
- AM4 a second card on the same day allowed -> TestNoDayShowsTwoCards
- AM5 a card while one is open -> TestNoDayShowsTwoCards
- AM6 budget_near's connection order dropped -> first MISSED: equivalent (the key holds the id and breaks the tie the same way); reversed instead -> TestTiesPickTheSameCard
- AM7 no sort (facts' order decides) -> TestTiesPickTheSameCard
- AM8 prefer_switch ignores the 24 h reset bound -> TestEachKindsLine (reset within a day)
- AM9 prefer_switch ignores the target's 0.50 -> TestEachKindsLine (target at 0.50)
- AM10 held_long ignores the week -> TestEachKindFiresOnce (tk5,tk6)
- AM11 defer_tasks takes any hold, not headroom -> TestEachKindsLine (held for the budget)
- AM12 the day read in UTC, not the person's zone -> first MISSED (the zone case only crossed one way); a case shown just after the person's midnight added, then caught
- AM13 prefer_switch ignores the runner rule -> TestEachKindsLine (another runner)
- AM14 role order reversed -> TestTiesPickTheSameCard
### instance store (internal/equipo/chat)
- IM1 act's revision not checked -> TestAdviceStoreActsThroughTheOrdinaryCall (a stale revision)
- IM2 act does not recheck that the facts hold -> first MISSED; a case acting on facts that stopped holding before the next look added, then caught
- IM3 the advice audit (source and key) left out -> TestAdviceStoreActsThroughTheOrdinaryCall, TestAdviceStoreTaskActions
- IM4 the day of a quiet-hours card counted as tonight, not the morning -> first MISSED; a night card after the day's card added, then caught
- IM5 quiet-hours card not held -> TestAdviceStoreQuietHours
- IM6 advice whose facts stopped holding not withdrawn -> TestAdviceStoreNotNowAndWithdrawal (first tried as a deletion, a compile error; then as a dead branch, caught)
- IM7 Not now not recorded with its time -> TestAdviceStoreNotNowAndWithdrawal (shown again within 14 days)
- IM8 open advice not following facts that moved -> TestAdviceStoreTaskActions (revision 1)
- IM9 a card in quiet hours answerable -> TestAdviceStoreQuietHours
- IM10 advice looked up without its person -> TestAdviceStoreActsThroughTheOrdinaryCall (another person's act)
- IM11 a hold's start reset by every report -> first MISSED; TestAcceptHoldKeepsHeldSince added, then caught
- IM12 a hold's start kept after the hold -> TestAcceptHoldKeepsHeldSince
### pages and wiring (internal/equipo, internal/thintest)
- PG1 Claude's age said as refreshed ("as of") -> TestUsagePage, TestHoldLineSaysTheReadingsAge
- PG2 the price source written as a fixed sentence -> first MISSED (the fixture's version gave the same words); a second price table version added, then caught
- PG3 a version without its dash accepted -> TestPriceSource
- PG4 the dot ignores the month's budget -> TestUsageAttention
- PG5 the task pages not adding the reading's age to a hold -> TestContractUsage (first tried as a deletion, a compile error; then as a dead read, caught)
- PG6 a reading with no time said "as of just now" -> TestUsagePage, TestHoldLineSaysTheReadingsAge
- PG7 the sidebar dot never set -> TestContractUsage (no dot for a limited connection)
- PG8 the loop's minute not looking at advice -> TestContractUsage (advice [])
- PG10 the conversation's advice card never open -> TestContractUsage (no quiet advice card)
- BM1 the advice action posting to Not now -> TestBrowserUsageAndAdvice (no budget frame)

## Decisions where the spec was open

- A reading's time rides the wire (`Headroom.checked_at`) rather than
  being the instance's receipt time: a fresh reading that says the same
  again would otherwise look old.
- The routing line is the task's hold line for headroom; its age is the
  named connection's current reading on the instance, not the reading the
  hold was decided on (a newer reading may have come without a new hold
  report).
- The Codex probe asks `account/read` without a token refresh, then the
  rate limits, under the same lock and runtime copy as the hourly check.
- `budget_near` names today's budget first, this month's when today's
  is not near or cannot be raised; its action doubles that budget (a
  day's never above the month's, both within S1's bounds). Leave it is
  its Not now and silences the same way.
- `prefer_switch` fires only with a preference set (Automatic already
  picks the most room). The preference is one provider for every role
  (as built), so the card names no role; the key carries the first
  waiting role in the fixed order. The preferred provider is near its
  limit when each of its connections has a window at or above 0.80
  resetting more than 24 h away; until is the earliest of those resets.
  The other subscription must be healthy, report at least one window,
  all below 0.50, and sit on a runner holding a waiting step of the role
  (the primary for one not placed yet). Its act is refused by a runner
  below protocol 14, as `SetPreference` with `until` is.
- `defer_tasks` per role: the normal tasks without a deadline whose step
  for that role is queued or held, the person's own, housekeeping's
  attempts never counted; the day it names is the earliest reset among
  the held urgent steps (no day when none was reported).
- `held_long` counts a hold without a break (`held_at`, set when a hold
  begins, kept while held, cleared with it).
- The day is the person's zone (UTC until one is known); a card made in
  quiet hours counts for the day it shows; a withdrawn card still counts.
- Quiet hours are the person's housekeeping quiet hours (22:00 to 08:00
  by default), released by housekeeping's minute; a person who paused
  housekeeping sees a night card after Resume or their first message, as
  housekeeping's own receipts.
- Usage periods are calendar ones in the person's zone (today, since
  Monday, since the 1st), the person's own rows only; rows that
  overcount are left out with the existing note; the estimate sums priced
  rows only.
- Where prices come from is the price table each metered connection was
  last charged by, from its usage rows.
- `GET /api/v1/usage` is built (Connections rev 3 §6) because the page
  reads the same totals.

## Open, or not as the spec says

- Connections rev 3 §6's routing sum lines and a subscription's typed
  plan price are not built (not in the S4 row).
- The Usage page does not follow the event stream; it is current on each
  load and after each form.
- The staging witness waits for a release.

## Copy keys (in the Plan 3 inventory)

The 51 strings below, as first written, went through Astra as the
copy-review gate (Paul's delegation of 2026-10-25): 39 kept, 12 changed,
applied verbatim, each now `COPY ACCEPTED BY DELEGATION (Astra,
2026-11-12)` in `copy.go` and in the inventory. The changes drop
"headroom" and "cached" ("Remaining usage not reported", "Reused
input"), name the estimate ("Estimated cost"), say a Claude reading was
"last reported", say what the budget figures are ("$0.84 of today’s $1
budget used."), make Leave it "Not now" like every other card, say the
reset's day and that the preference covers every role ("…, which resets
on Thu 19 Nov. Codex has room. Prefer Codex for all roles until then?"),
say subscriptions bring no separate token charges, and say "Set them to
normal priority". The tests and fixtures that quote these follow.

- `nav.usage`: Usage
- `nav.usage_attention`: Something on Usage needs a look
- `usage.title`: Usage
- `usage.lede`: What can run now, what it costs, and where the tokens went.
- `usage.advice`: Advice
- `usage.now`: Available now
- `usage.reading.as_of`: as of %s
- `usage.reading.last_reported`: as last reported %s
- `usage.reading.none`: Headroom not reported
- `usage.no_connections`: Nothing is connected yet.
- `usage.connect`: Add a connection
- `usage.spend`: Spend this month
- `usage.price_source`: %s prices taken from the public page on %s.
- `usage.plan_tokens`: %s tokens this month
- `usage.plan`: %s plan
- `usage.spend_none`: Nothing here spends money: every connection is a subscription.
- `usage.tokens`: Token use
- `usage.period.day`: Today
- `usage.period.week`: This week
- `usage.period.month`: This month
- `usage.by.connection`: Connection
- `usage.by.role`: Role
- `usage.by.project`: Project
- `usage.by.task`: Task
- `usage.col.input`: Input
- `usage.col.cached`: Cached input
- `usage.col.output`: Output
- `usage.col.estimate`: Estimate
- `usage.tokens_empty`: No tokens recorded for this period.
- `usage.no_task`: The conversation
- `usage.role.probe`: Connection checks
- `usage.at_least`: at least %s
- `work.hold.reading`: %s · %s
- `advice.not_now`: Not now
- `advice.leave_it`: Leave it
- `advice.budget_near.day`: %s: %s of today’s %s.
- `advice.budget_near.month`: %s: %s of this month’s %s.
- `advice.budget_near.raise_day`: Raise today’s budget to %s
- `advice.budget_near.raise_month`: Raise this month’s budget to %s
- `advice.prefer_switch`: %s is near its limit until %s. %s has room. Prefer %s until then?
- `advice.prefer_switch.act`: Prefer %s until %s
- `advice.defer_tasks.one`: One task could wait until %s so the urgent work goes first. Let it wait?
- `advice.defer_tasks.many`: %d tasks could wait until %s so the urgent work goes first. Let them wait?
- `advice.defer_tasks.one_no_reset`: One task could wait so the urgent work goes first. Let it wait?
- `advice.defer_tasks.many_no_reset`: %d tasks could wait so the urgent work goes first. Let them wait?
- `advice.defer_tasks.act_one`: Make it low priority
- `advice.defer_tasks.act_many`: Make them low priority
- `advice.held_long.one`: A low-priority task has waited a week.
- `advice.held_long.many`: %d low-priority tasks have waited a week.
- `advice.held_long.act_one`: Make it normal
- `advice.held_long.act_many`: Make them normal
