Repository navigation
feat(db): Store money as integer micro-USD - #3621
Merged
Merged
Conversation
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]>
Co-Authored-By: BYK <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
BYK
approved these changes
Nov 17, 2026
BYK
enabled auto-merge (squash)
November 17, 2026 22:12
24 tasks done
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:cost_usd(a USD double) becomescost_micro_usdin the samevaluecolumn. 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.cost_micro_usd bigintcolumn.cost_usdstays for one release so the old runtime can still write it. A later release drops it.UNITS_PER_USD = 10_000) in anint4, which at micro-USD caps a counter at about $2,147 a day.junior_statsgets abigintvaluecolumn (add and backfill, no table rewrite). The four dollar counters get*_micro_usdnames. The oldcountcolumn stays for one release. The savings re-fill job key moves tov2, 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. TheaddUsdfloat-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-unitsstep in lint rejects:bigintnamed*_micro_usd,packages/*/srcwith a money word that does not end in_micro_usd,*_PER_USD, or multiplication by1e6,1_000_000,1e-6) outside the helper.The allowlist names the legacy Brief
cost_usdcolumn, with a comment that says why.AGENTS.mdstates the rule in one line.Rollout. Migrations
0119and0120convert stored data.0120updates every Run cost row in one statement, as0092did. Only Runs in progress wait for its row locks. Until the rollout finishes, old instances still write the old units:cost_usdrows, Briefcost_usd, and counters incount. They also sum a Conversation's usage cost from their own rows only. New instances do not read those writes. A repair migration onbyk/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):
mainThe People list difference comes from dead rows that the
0120update leaves behind. AfterVACUUM FULLon both, the People list took 2,300 ms onmainand 2,235 ms on this PR.0119took 21 ms and0120took 3.5–3.7 s for 200,000 cost rows.via @BYK.
--
View Session [Sentry]