Skip to content

feat(db): Store money as integer micro-USD - #3621

Merged
BYK merged 2 commits into
mainfrom
byk/money-micro-usd
Nov 17, 2026
Merged

BYK merged 2 commits into
mainfrom
byk/money-micro-usd

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Every money value in Postgres is now whole micro-USD, and a lint rule keeps it that way. Run cost already did this (junior_automation_runs.cost_micro_usd, #3572). This applies the same rule to the remaining money values:

  • Run cost metric: cost_usd (a USD double) becomes cost_micro_usd in the same value column. A double holds every integer up to 2^53 exactly, so the EAV table keeps one value type. People spend, conversation stats, and the usage cost copied to a Conversation and its Automation run all sum integers now.
  • Brief cost: new cost_micro_usd bigint column. cost_usd stays for one release so the old runtime can still write it. A later release drops it.
  • Cache warming savings: the dollar counters stored their own unit (UNITS_PER_USD = 10_000) in an int4, which at micro-USD caps a counter at about $2,147 a day. junior_stats gets a bigint value column (add and backfill, no table rewrite). The four dollar counters get *_micro_usd names. The old count column stays for one release. The savings re-fill job key moves to v2, because a job in progress keeps its sums in the old unit.

Amounts round once where they are stored and convert to USD once in the API, through one helper (src/db/micro-usd.ts). API responses and the dashboard do not change. The addUsd float-rounding helpers that integer sums replace are gone. Usage JSON (usage_json, execution_usage_json) and event JSON keep the provider's USD floats, as before.

The rule. A new money-units step in lint rejects:

  • a column in the latest Drizzle snapshot with a money word in its name (cost, usd, price, spend, savings, amount, and similar) that is not a bigint named *_micro_usd,
  • a snake_case name, such as a metric or counter, in packages/*/src with a money word that does not end in _micro_usd,
  • a money scale (*_PER_USD, or multiplication by 1e6, 1_000_000, 1e-6) outside the helper.

The allowlist names the legacy Brief cost_usd column, with a comment that says why. AGENTS.md states the rule in one line.

Rollout. Migrations 0119 and 0120 convert stored data. 0120 updates every Run cost row in one statement, as 0092 did. Only Runs in progress wait for its row locks. Until the rollout finishes, old instances still write the old units: cost_usd rows, Brief cost_usd, and counters in count. They also sum a Conversation's usage cost from their own rows only. New instances do not read those writes. A repair migration on byk/money-micro-usd-repair (stacked, 0121) converts what the old runtime wrote, and sums the usage cost of the affected Conversations again. It is idempotent. Merge it after this PR is fully deployed. If #3392 merges first, this PR renumbers its migrations.

Parity. Each stored amount rounds once, so a total can differ from the old float total by at most half a micro-USD per Run. For 60 provider-shaped costs, the total is $7.134042, against a float total of $7.134039 (3 µ$; the bound is 30 µ$). On synthetic data with 200,000 Run costs over 40 people and 30 days, migrated from main, spend, People windows, and stats days differ from the old reads by at most 15 µ$ per person-window and 360 µ$ per day. Each difference is less than half a micro-USD per row. Cache warming totals are equal.

Bench (local Postgres, same synthetic data, 400,400 metric rows, medians of 15 reads, two runs each):

Read main this PR
Personal spend 10.3–11.2 ms 10.7 ms
People list 2,097–2,121 ms 2,269–2,319 ms
Conversation stats 163–165 ms 155–161 ms
Cache warming savings 8.9–12.8 ms 8.7–10.9 ms

The People list difference comes from dead rows that the 0120 update leaves behind. After VACUUM FULL on both, the People list took 2,300 ms on main and 2,235 ms on this PR. 0119 took 21 ms and 0120 took 3.5–3.7 s for 200,000 cost rows.

via @BYK.

--

View Session [Sentry]

sentry-junior Bot and others added 2 commits November 17, 2026 21:47
Every money value in Postgres is now whole micro-USD: the Run cost
metric (`cost_micro_usd`), Brief cost, and the cache warming dollar
counters, which move to a `bigint` junior_stats column. Amounts round
once where they are stored and convert to USD once in the API, so API
responses do not change. One helper owns the conversion.

`pnpm money-units:check` fails on a money column or name in another
unit, and on a money scale outside the helper.

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot requested a review from BYK November 17, 2026 22:03
@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 17, 2026 10:03pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 17, 2026
@BYK
BYK enabled auto-merge (squash) November 17, 2026 22:12
@BYK
BYK merged commit 380dd33 into main Nov 17, 2026
48 checks passed
@BYK
BYK deleted the byk/money-micro-usd branch November 17, 2026 22:19
BYK added a commit that referenced this pull request Nov 22, 2026
…out (#4041)

#3621 moved money to integer micro-USD. While its
migrations (0119, 0120) rolled out, the previous runtime still wrote
money in the old units, and nothing converted those rows afterwards:

- `cost_usd` metric rows, which every cost read now skips. The usage
cost of those Conversations, and the cost of their Automation runs, came
from one runtime and missed the other runtime's rows.
- Brief versions with only `cost_usd` set.
- Stats counters added to the legacy `count` column, including cache
warming dollar counters under their old names and units.

Migration 0160 converts the old cost rows to `cost_micro_usd`. When both
runtimes wrote a cost row for the same Run, the later row wins. It then
sums the usage cost of each affected Conversation again and copies it to
their Automation runs. It also fills `cost_micro_usd` on those Brief
versions and moves `count` amounts into `value`, using the micro-USD
names for cache warming counters. Each statement touches only old-unit
rows, so running it a second time changes nothing.

The metrics table is large and has no index on `cost_usd`. So the cost
repair is one statement with data-modifying CTEs: it scans the table
once, and finds the other rows of each affected Conversation by primary
key.

#3615 is a different repair: it fixed Automation run
cost and last run after the 0117 rollout.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3Af3d48a21456a0e73f7ba562c44119dca -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3Af3d48a21456a0e73f7ba562c44119dca)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3Af3d48a21456a0e73f7ba562c44119dca/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — cc22d4dd Deployed Nov 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant