| 1 | # S4 progress: the Usage page and advice |
| 2 | |
| 3 | Branch `connections-s4-usage` from main 75ba4863 (S1, S2 and S3 merged). |
| 4 | Spec: `docs/superpowers/specs/2026-11-12-equipo-connectors-and-token-scheduling.md`, |
| 5 | now on this branch as **revision 3.2**, §11 row S4: §9 (advice), §2.3's |
| 6 | spend line, §10's advice API, and the Usage page of the Connections rev 3 |
| 7 | spec §6 (availability now, spend this month, token use by connection, |
| 8 | role, project and task; the sidebar dot). Also the controller's rulings |
| 9 | on S3's review: #7 (reading freshness) built, #8 (a kept metered |
| 10 | session moving) a spec amendment only. No real key, sign-in or |
| 11 | provider: fakes only. |
| 12 | |
| 13 | ## Done |
| 14 | |
| 15 | 1. **Readings and their age (ruling #7).** Protocol 15 |
| 16 | (`ProtocolReadings`): `Headroom.checked_at`, the time the runner took |
| 17 | a reading. `Conns.Observe` and `Conns.Limit` stamp every reading |
| 18 | (a turn's, a notification's, a probe's) with a copy, so the caller's |
| 19 | value is untouched; a reading that says the same again moves only its |
| 20 | time and wakes no held step. The scheduler's staleness check reads the |
| 21 | reading's own time (the row's write time only for a row from before). |
| 22 | Below 15, `Encode` drops the time from hello, sync and connection |
| 23 | reports (`downgradeReadings`), and the runner's first hello never |
| 24 | carries it (`WithoutReadingTimes`, a prompt hello after a 15 welcome). |
| 25 | 2. **The Codex reading probe (ruling #7).** `provider.ReadCodexLimits`: |
| 26 | the login probe's unconfined app-server in the source home, its |
| 27 | environment and egress, `initialize`, `account/read` then |
| 28 | `account/rateLimits/read`, no thread and no turn. `thin.CodexReading` |
| 29 | runs it through `provider.OpenRuntimeCopy` (the hourly refresh's lock |
| 30 | and identity check) and is now `Conns.Reading` in production, so a |
| 31 | Codex reading older than 15 minutes is refreshed before `Decide`, at |
| 32 | most once per 15 minutes a connection. Claude, token and metered |
| 33 | connections return nothing: Claude stays as last reported. |
| 34 | 3. **`internal/advice`**, pure: `Compute(facts, now)` gives every piece |
| 35 | of advice whose facts hold, in §9's order (kind; a connection's id, a |
| 36 | role's fixed place, a task set's earliest task then id; the key); |
| 37 | `Choose` applies the rules (none while one is open, one new card a |
| 38 | day in the person's zone counted on the day it shows, Not now silences |
| 39 | a key 14 days and 60 after a second); `Holds` says whether open |
| 40 | advice still holds. The four kinds with their triggers as §9 says. |
| 41 | 4. **The instance** (`chat/advice.go`, migration 0074): the forward-only |
| 42 | `chat_advice` table (open, then acted, dismissed or withdrawn); |
| 43 | `RefreshAdvice` on the housekeeping loop's minute (so on fact changes |
| 44 | within a minute, and at 08:00): open advice whose facts stopped |
| 45 | holding is withdrawn silently (its card resolved, nothing said), open |
| 46 | advice whose facts moved but hold follows them at a new revision, and |
| 47 | with none open the first eligible key not silenced becomes the day's |
| 48 | card, held through quiet hours with housekeeping's `VisibleAt`. One |
| 49 | quiet `advice` card in the conversation, never Needs you, never a |
| 50 | push; the coordinator has no tool for it. Actions are the ordinary |
| 51 | calls, in the advice's transaction, at the revision the advice read, |
| 52 | after checking the facts still hold: `SetBudget`, `SetPreference` |
| 53 | (with `until`), `SetPriority` per task (each through a `txn` form of |
| 54 | the store call). Each is audited as the call and as `advice.act` (or |
| 55 | `advice.dismiss`) with `source=advice`, the advice id and its key. |
| 56 | `chat_steps.held_at` keeps when a hold began. API: `GET |
| 57 | /api/v1/advice`, `POST /api/v1/advice/{id}/act`, `POST |
| 58 | /api/v1/advice/{id}/dismiss` (409 `revision_conflict` when it moved, |
| 59 | was answered, or its facts no longer hold), and `GET |
| 60 | /api/v1/usage?period=&by=`. Fixtures `api/advice.json`, |
| 61 | `api/advice-action.json`, `api/card-advice.json`, |
| 62 | `api/usage-month.json`, `frames/hello-readings.json`. |
| 63 | 5. **The Usage page** (`/usage`, `usage_page.go`, `usage.html`): advice |
| 64 | at the top with its action and Not now; available now (each |
| 65 | connection's state, its provider's meters and the reading's age, "as |
| 66 | of 2 h ago" for Codex, "as last reported 2 h ago" for Claude, none |
| 67 | from an older runner; "Headroom not reported" otherwise); spend this |
| 68 | month (S1's spend line per metered connection, reported by its |
| 69 | harness, where prices come from read from the price table's version: |
| 70 | "TensorX prices taken from the public page on 12 Nov 2026."; |
| 71 | subscriptions as plan and tokens, never money); token use with tabs |
| 72 | (today, this week, this month) by connection, role, project or task, |
| 73 | columns input, cached input, output and the estimate where priced. |
| 74 | The sidebar has Usage with a dot when a connection is limited or needs |
| 75 | a sign-in, or a metered one is paused. A step held for headroom says |
| 76 | its reading's age on its line ("… · as last reported 2 h ago"). |
| 77 | 6. **Spec revision 3.2** on this branch, with its Astra findings files |
| 78 | and the dead-reference allowlist entries: §6, §7, §12 and the status |
| 79 | line for ruling #7; §7 rule 1 adds spending permission withdrawn as a |
| 80 | switch reason for a kept session (ruling #8, no code change); §10's |
| 81 | `Headroom.checked_at`. |
| 82 | |
| 83 | ## Tests |
| 84 | |
| 85 | - `internal/advice`: `TestEachKindFiresOnce` (each kind from its fixture, |
| 86 | only it), `TestEachKindsLine` (each trigger just short of its line), |
| 87 | `TestTiesPickTheSameCard` (all kinds at once, 500 shuffles, the same |
| 88 | order and the same card), `TestNotNowSilences` (14 days, then 60), |
| 89 | `TestNoDayShowsTwoCards` (open, acted, withdrawn or dismissed today; |
| 90 | quiet hours; the person's zone). |
| 91 | - `internal/equipo/chat`: `TestAdviceStoreActsThroughTheOrdinaryCall`, |
| 92 | `TestAdviceStoreNotNowAndWithdrawal`, `TestAdviceStoreQuietHours`, |
| 93 | `TestAdviceStoreTaskActions`, `TestAdviceStorePreferSwitch`, |
| 94 | `TestAcceptHoldKeepsHeldSince`. |
| 95 | - `internal/equipo`: `TestUsagePage`, `TestPriceSource`, |
| 96 | `TestUsageAttention`, `TestHoldLineSaysTheReadingsAge`. |
| 97 | - `internal/wire`: `TestEncodeBelowProtocol15DropsReadingTimes` (frozen |
| 98 | protocol 14 headroom), `TestWithoutReadingTimes`, |
| 99 | `TestAdviceTypesCarryNoSecrets`, fixture round trips. |
| 100 | - `internal/runner`: `TestReadCodexLimitsWithoutTurn` (provider: a fake |
| 101 | app-server that fails on anything but its four requests), |
| 102 | `TestCodexReadingInProduction`, `TestScheduleRefreshUsesReadingTime`, |
| 103 | `TestHandshakeHelloWithholdsReadingTimes` (thin). |
| 104 | - `internal/thintest`: `TestContractUsage` (page, advice in both places, |
| 105 | API, act from the page reaching the runner, the hold line's age, the |
| 106 | dot), `TestContractUsageOlderRunner` (protocol 14). |
| 107 | - `internal/headless` (`-tags browser`): `TestBrowserUsageAndAdvice` at |
| 108 | 1280 and 390: the sidebar, the card at the top, the action in reach, |
| 109 | the reading age, the spend line, the tabs, no sideways scroll; Raise |
| 110 | today's budget reaches the runner at twice the figure. |
| 111 | - `internal/plan3test`: the four person API routes registered. |
| 112 | - Local runs were targeted (`-run`) on the changed packages, plus gofmt, |
| 113 | go vet, staticcheck on the changed packages, `make budget` (lines |
| 114 | updated), `scripts/ci-test.py check` and the dead-reference scan. |
| 115 | |
| 116 | ## Mutations, each observed failing |
| 117 | |
| 118 | ### wire and runner |
| 119 | - WM1 readings downgrade removed -> TestEncodeBelowProtocol15DropsReadingTimes (hello carries checked_at) |
| 120 | - PM1 reading probe asks turn/start -> TestReadCodexLimitsWithoutTurn |
| 121 | - PM2 reading probe skips rateLimits/read -> TestReadCodexLimitsWithoutTurn |
| 122 | - RM1 Observe/Limit not stamping the reading time -> TestCodexReadingInProduction (CheckedAt nil), TestScheduleRefreshUsesReadingTime |
| 123 | - RM2 staleness read from the row's updated_at, not the reading's time -> TestScheduleRefreshUsesReadingTime (0 probes) |
| 124 | - RM3 a reading that says the same again counted as new (time compared) -> TestCodexReadingInProduction (woke 1) |
| 125 | - RM4 CodexReading reads any provider (Claude too) -> TestCodexReadingInProduction (claude-1 read) |
| 126 | - RM5 first hello carries the reading times -> TestHandshakeHelloWithholdsReadingTimes (handshake hello) |
| 127 | - RM6 no prompt hello after a protocol 15 welcome -> TestHandshakeHelloWithholdsReadingTimes |
| 128 | ### advice (internal/advice) |
| 129 | - AM1 near-budget line moved above 80% -> TestEachKindFiresOnce (budget_near: []), TestEachKindsLine |
| 130 | - AM2 first Not now silences 7 days -> TestNotNowSilences (day 13 shown) |
| 131 | - AM3 second Not now silences 14 days, not 60 -> TestNotNowSilences (day 29 shown) |
| 132 | - AM4 a second card on the same day allowed -> TestNoDayShowsTwoCards |
| 133 | - AM5 a card while one is open -> TestNoDayShowsTwoCards |
| 134 | - 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 |
| 135 | - AM7 no sort (facts' order decides) -> TestTiesPickTheSameCard |
| 136 | - AM8 prefer_switch ignores the 24 h reset bound -> TestEachKindsLine (reset within a day) |
| 137 | - AM9 prefer_switch ignores the target's 0.50 -> TestEachKindsLine (target at 0.50) |
| 138 | - AM10 held_long ignores the week -> TestEachKindFiresOnce (tk5,tk6) |
| 139 | - AM11 defer_tasks takes any hold, not headroom -> TestEachKindsLine (held for the budget) |
| 140 | - 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 |
| 141 | - AM13 prefer_switch ignores the runner rule -> TestEachKindsLine (another runner) |
| 142 | - AM14 role order reversed -> TestTiesPickTheSameCard |
| 143 | ### instance store (internal/equipo/chat) |
| 144 | - IM1 act's revision not checked -> TestAdviceStoreActsThroughTheOrdinaryCall (a stale revision) |
| 145 | - 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 |
| 146 | - IM3 the advice audit (source and key) left out -> TestAdviceStoreActsThroughTheOrdinaryCall, TestAdviceStoreTaskActions |
| 147 | - 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 |
| 148 | - IM5 quiet-hours card not held -> TestAdviceStoreQuietHours |
| 149 | - IM6 advice whose facts stopped holding not withdrawn -> TestAdviceStoreNotNowAndWithdrawal (first tried as a deletion, a compile error; then as a dead branch, caught) |
| 150 | - IM7 Not now not recorded with its time -> TestAdviceStoreNotNowAndWithdrawal (shown again within 14 days) |
| 151 | - IM8 open advice not following facts that moved -> TestAdviceStoreTaskActions (revision 1) |
| 152 | - IM9 a card in quiet hours answerable -> TestAdviceStoreQuietHours |
| 153 | - IM10 advice looked up without its person -> TestAdviceStoreActsThroughTheOrdinaryCall (another person's act) |
| 154 | - IM11 a hold's start reset by every report -> first MISSED; TestAcceptHoldKeepsHeldSince added, then caught |
| 155 | - IM12 a hold's start kept after the hold -> TestAcceptHoldKeepsHeldSince |
| 156 | ### pages and wiring (internal/equipo, internal/thintest) |
| 157 | - PG1 Claude's age said as refreshed ("as of") -> TestUsagePage, TestHoldLineSaysTheReadingsAge |
| 158 | - 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 |
| 159 | - PG3 a version without its dash accepted -> TestPriceSource |
| 160 | - PG4 the dot ignores the month's budget -> TestUsageAttention |
| 161 | - 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) |
| 162 | - PG6 a reading with no time said "as of just now" -> TestUsagePage, TestHoldLineSaysTheReadingsAge |
| 163 | - PG7 the sidebar dot never set -> TestContractUsage (no dot for a limited connection) |
| 164 | - PG8 the loop's minute not looking at advice -> TestContractUsage (advice []) |
| 165 | - PG10 the conversation's advice card never open -> TestContractUsage (no quiet advice card) |
| 166 | - BM1 the advice action posting to Not now -> TestBrowserUsageAndAdvice (no budget frame) |
| 167 | |
| 168 | ## Decisions where the spec was open |
| 169 | |
| 170 | - A reading's time rides the wire (`Headroom.checked_at`) rather than |
| 171 | being the instance's receipt time: a fresh reading that says the same |
| 172 | again would otherwise look old. |
| 173 | - The routing line is the task's hold line for headroom; its age is the |
| 174 | named connection's current reading on the instance, not the reading the |
| 175 | hold was decided on (a newer reading may have come without a new hold |
| 176 | report). |
| 177 | - The Codex probe asks `account/read` without a token refresh, then the |
| 178 | rate limits, under the same lock and runtime copy as the hourly check. |
| 179 | - `budget_near` names today's budget first, this month's when today's |
| 180 | is not near or cannot be raised; its action doubles that budget (a |
| 181 | day's never above the month's, both within S1's bounds). Leave it is |
| 182 | its Not now and silences the same way. |
| 183 | - `prefer_switch` fires only with a preference set (Automatic already |
| 184 | picks the most room). The preference is one provider for every role |
| 185 | (as built), so the card names no role; the key carries the first |
| 186 | waiting role in the fixed order. The preferred provider is near its |
| 187 | limit when each of its connections has a window at or above 0.80 |
| 188 | resetting more than 24 h away; until is the earliest of those resets. |
| 189 | The other subscription must be healthy, report at least one window, |
| 190 | all below 0.50, and sit on a runner holding a waiting step of the role |
| 191 | (the primary for one not placed yet). Its act is refused by a runner |
| 192 | below protocol 14, as `SetPreference` with `until` is. |
| 193 | - `defer_tasks` per role: the normal tasks without a deadline whose step |
| 194 | for that role is queued or held, the person's own, housekeeping's |
| 195 | attempts never counted; the day it names is the earliest reset among |
| 196 | the held urgent steps (no day when none was reported). |
| 197 | - `held_long` counts a hold without a break (`held_at`, set when a hold |
| 198 | begins, kept while held, cleared with it). |
| 199 | - The day is the person's zone (UTC until one is known); a card made in |
| 200 | quiet hours counts for the day it shows; a withdrawn card still counts. |
| 201 | - Quiet hours are the person's housekeeping quiet hours (22:00 to 08:00 |
| 202 | by default), released by housekeeping's minute; a person who paused |
| 203 | housekeeping sees a night card after Resume or their first message, as |
| 204 | housekeeping's own receipts. |
| 205 | - Usage periods are calendar ones in the person's zone (today, since |
| 206 | Monday, since the 1st), the person's own rows only; rows that |
| 207 | overcount are left out with the existing note; the estimate sums priced |
| 208 | rows only. |
| 209 | - Where prices come from is the price table each metered connection was |
| 210 | last charged by, from its usage rows. |
| 211 | - `GET /api/v1/usage` is built (Connections rev 3 §6) because the page |
| 212 | reads the same totals. |
| 213 | |
| 214 | ## Open, or not as the spec says |
| 215 | |
| 216 | - Connections rev 3 §6's routing sum lines and a subscription's typed |
| 217 | plan price are not built (not in the S4 row). |
| 218 | - The Usage page does not follow the event stream; it is current on each |
| 219 | load and after each form. |
| 220 | - The staging witness waits for a release. |
| 221 | |
| 222 | ## Copy keys (in the Plan 3 inventory) |
| 223 | |
| 224 | The 51 strings below, as first written, went through Astra as the |
| 225 | copy-review gate (Paul's delegation of 2026-10-25): 39 kept, 12 changed, |
| 226 | applied verbatim, each now `COPY ACCEPTED BY DELEGATION (Astra, |
| 227 | 2026-11-12)` in `copy.go` and in the inventory. The changes drop |
| 228 | "headroom" and "cached" ("Remaining usage not reported", "Reused |
| 229 | input"), name the estimate ("Estimated cost"), say a Claude reading was |
| 230 | "last reported", say what the budget figures are ("$0.84 of today’s $1 |
| 231 | budget used."), make Leave it "Not now" like every other card, say the |
| 232 | reset's day and that the preference covers every role ("…, which resets |
| 233 | on Thu 19 Nov. Codex has room. Prefer Codex for all roles until then?"), |
| 234 | say subscriptions bring no separate token charges, and say "Set them to |
| 235 | normal priority". The tests and fixtures that quote these follow. |
| 236 | |
| 237 | - `nav.usage`: Usage |
| 238 | - `nav.usage_attention`: Something on Usage needs a look |
| 239 | - `usage.title`: Usage |
| 240 | - `usage.lede`: What can run now, what it costs, and where the tokens went. |
| 241 | - `usage.advice`: Advice |
| 242 | - `usage.now`: Available now |
| 243 | - `usage.reading.as_of`: as of %s |
| 244 | - `usage.reading.last_reported`: as last reported %s |
| 245 | - `usage.reading.none`: Headroom not reported |
| 246 | - `usage.no_connections`: Nothing is connected yet. |
| 247 | - `usage.connect`: Add a connection |
| 248 | - `usage.spend`: Spend this month |
| 249 | - `usage.price_source`: %s prices taken from the public page on %s. |
| 250 | - `usage.plan_tokens`: %s tokens this month |
| 251 | - `usage.plan`: %s plan |
| 252 | - `usage.spend_none`: Nothing here spends money: every connection is a subscription. |
| 253 | - `usage.tokens`: Token use |
| 254 | - `usage.period.day`: Today |
| 255 | - `usage.period.week`: This week |
| 256 | - `usage.period.month`: This month |
| 257 | - `usage.by.connection`: Connection |
| 258 | - `usage.by.role`: Role |
| 259 | - `usage.by.project`: Project |
| 260 | - `usage.by.task`: Task |
| 261 | - `usage.col.input`: Input |
| 262 | - `usage.col.cached`: Cached input |
| 263 | - `usage.col.output`: Output |
| 264 | - `usage.col.estimate`: Estimate |
| 265 | - `usage.tokens_empty`: No tokens recorded for this period. |
| 266 | - `usage.no_task`: The conversation |
| 267 | - `usage.role.probe`: Connection checks |
| 268 | - `usage.at_least`: at least %s |
| 269 | - `work.hold.reading`: %s · %s |
| 270 | - `advice.not_now`: Not now |
| 271 | - `advice.leave_it`: Leave it |
| 272 | - `advice.budget_near.day`: %s: %s of today’s %s. |
| 273 | - `advice.budget_near.month`: %s: %s of this month’s %s. |
| 274 | - `advice.budget_near.raise_day`: Raise today’s budget to %s |
| 275 | - `advice.budget_near.raise_month`: Raise this month’s budget to %s |
| 276 | - `advice.prefer_switch`: %s is near its limit until %s. %s has room. Prefer %s until then? |
| 277 | - `advice.prefer_switch.act`: Prefer %s until %s |
| 278 | - `advice.defer_tasks.one`: One task could wait until %s so the urgent work goes first. Let it wait? |
| 279 | - `advice.defer_tasks.many`: %d tasks could wait until %s so the urgent work goes first. Let them wait? |
| 280 | - `advice.defer_tasks.one_no_reset`: One task could wait so the urgent work goes first. Let it wait? |
| 281 | - `advice.defer_tasks.many_no_reset`: %d tasks could wait so the urgent work goes first. Let them wait? |
| 282 | - `advice.defer_tasks.act_one`: Make it low priority |
| 283 | - `advice.defer_tasks.act_many`: Make them low priority |
| 284 | - `advice.held_long.one`: A low-priority task has waited a week. |
| 285 | - `advice.held_long.many`: %d low-priority tasks have waited a week. |
| 286 | - `advice.held_long.act_one`: Make it normal |
| 287 | - `advice.held_long.act_many`: Make them normal |
| 288 | |