Skip to content

ui-server: the tested budget-settlement path is never called in production and writes fewer columns than the real one #2526

Open

Description

@schlessera

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 subscription reservation 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-server finds the type, the switch case and that test, nothing else.

  • The production path. settleInboxBudgetRun (packages/ui-server/src/inbox/budget.ts:281-347) computes the charge from activity receipts and writes it with its own UPDATE … WHERE id = ? AND status = 'active' (budget.ts:344). It is reached through budget.settle (budget.ts:261, called from inbox/triage.ts:210), from activity/store.ts:619 when 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 raw UPDATE (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/src

Scope

  • Delete the store's settle mutation kind and its switch case. settleInboxBudgetRun stays 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.ts to settleInboxBudgetRun. 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

Acceptance criteria

  • git grep -n '"settle"' -- packages/ui-server returns nothing.

  • Tests against settleInboxBudgetRun prove 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 at budget.ts:344 makes the first test fail (recorded in the PR).

  • A test asserts that observed_billing_mode is written on settlement.

  • grep -rnE "UPDATE inbox_budget_reservations SET status = 'released'" packages/ui-server/src matches a single helper.

  • bun run test packages/ui-server passes.

Verification

bun run test packages/ui-server
bun run typecheck

Contract impact

None. InboxStore mutation kinds are internal, and stored data is unchanged.

Found by a repository review on 2026-10-22.

Activity

  1. added
    type: refactor
    area: ui-server
    priority: p3
    agent-ready
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-serverpriority: p3type: refactor

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions