Repository navigation
Description
Context
Inbox budget reservations are settled by two separate write paths, and the tested one is not the one production runs.
-
The store path, which only tests call.
InboxStore.commit([{ kind: "settle", … }])(packages/ui-server/src/inbox/store.ts:937-975, mutation type at:125-131) enforces three invariants:- the reservation must be
active; - a
subscriptionreservation settles at $0; - the charge never drops below observed spend.
Its only callers are
packages/ui-server/tests/inbox-store.test.ts:786-809.git grep -n '"settle"' -- packages/ui-serverfinds the type, the switch case and that test, nothing else. - the reservation must be
-
The production path.
settleInboxBudgetRun(packages/ui-server/src/inbox/budget.ts:281-347) computes the charge from activity receipts and writes it with its ownUPDATE … WHERE id = ? AND status = 'active'(budget.ts:344). It is reached throughbudget.settle(budget.ts:261, called frominbox/triage.ts:210), fromactivity/store.ts:619when a run's root span ends, and from recovery (budget.ts:356,inbox/snapshot.ts:372). Recovery of a reservation with no run releases it with yet another rawUPDATE(budget.ts:358,inbox/snapshot.ts:373).
The two paths write different columns. The store path does not write observed_billing_mode; the production path does (budget.ts:344). A future caller that picks the store's settle because it is the typed, tested API would silently drop the observed billing mode.
The money invariants the store test proves are therefore not proven for the code that charges real runs. They hold in settleInboxBudgetRun only by reading its arithmetic (budget.ts:318-343).
Reproduce:
git grep -n '"settle"' -- packages/ui-server
grep -rnE '(UPDATE|INSERT INTO) inbox_budget_reservations' packages/ui-server/srcScope
- Delete the store's
settlemutation kind and its switch case.settleInboxBudgetRunstays the single settlement writer. It already owns the receipt-derived arithmetic, which the store mutation cannot express. - Move the three invariant tests from
tests/inbox-store.test.tstosettleInboxBudgetRun. Drive it with real activity rollups from a fictional run, not hand-built charge values. - Have the two no-run release
UPDATEs (budget.ts:358,inbox/snapshot.ts:373) call one helper.
Out of scope
- Changing settlement arithmetic, conservative allowances or the unpriced-model notice.
- The
reservemutation, and thesqlite_masterprobe inbudget.ts:282. - ui-server: an escalated Claude turn whose continuation was already sent settles with zero in-flight allowance #1590 (escalated continuation allowance) and production enablement of dispatch (ui-server: enable autonomous dispatch in production behind a supported switch #1595).
Acceptance criteria
-
git grep -n '"settle"' -- packages/ui-serverreturns nothing. -
Tests against
settleInboxBudgetRunprove three things:- a second settlement of the same reservation writes nothing;
- a subscription-billed run charges $0;
- the charge is never below observed spend.
Mutating the
status = 'active'guard atbudget.ts:344makes the first test fail (recorded in the PR). -
A test asserts that
observed_billing_modeis written on settlement. -
grep -rnE "UPDATE inbox_budget_reservations SET status = 'released'" packages/ui-server/srcmatches a single helper. -
bun run test packages/ui-serverpasses.
Verification
bun run test packages/ui-server
bun run typecheckContract impact
None. InboxStore mutation kinds are internal, and stored data is unchanged.
Found by a repository review on 2026-10-22.
Activity