Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
feat(credits): cut the api over to six-decimal (micro-dollar) credits#855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a6f462b5833e8155480eabefd90e77df1657db8e82File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,15 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { describe, it, expect } from "vitest"; | ||
| import { DEFAULT_CREDITS, DEFAULT_CREDITS_USD, PRO_CREDITS, PRO_CREDITS_USD } from "../const"; | ||
| import { usdToCredits } from "../usdToCredits"; | ||
| import { DEFAULT_CREDITS, PRO_CREDITS } from "@/lib/credits/const"; | ||
| describe("credit plan constants", () => { | ||
| it("keeps the free-tier allotment at 333 (matches chat/lib/consts.ts)", () => { | ||
| expect(DEFAULT_CREDITS).toBe(333); | ||
| describe("credit grant totals", () => { | ||
| it("derive from their dollar values through the shared conversion", () => { | ||
| expect(DEFAULT_CREDITS).toBe(usdToCredits(DEFAULT_CREDITS_USD)); | ||
| expect(PRO_CREDITS).toBe(usdToCredits(PRO_CREDITS_USD)); | ||
| }); | ||
| it("gives pro accounts 9999 credits per month (matches chat/lib/consts.ts)", () => { | ||
| expect(PRO_CREDITS).toBe(9999); | ||
| it("are $3.33 and $99.99 in micro-dollars", () => { | ||
| expect(DEFAULT_CREDITS).toBe(3_330_000); | ||
| expect(PRO_CREDITS).toBe(99_990_000); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { creditsToUsd } from "../creditsToUsd"; | ||
| import { usdToCredits } from "../usdToCredits"; | ||
| describe("creditsToUsd", () => { | ||
| it("keeps the two directions consistent at the unit boundary", () => { | ||
| expect(creditsToUsd(1_000_000)).toBe(1); | ||
| expect(creditsToUsd(92_440_000)).toBe(92.44); | ||
| expect(creditsToUsd(usdToCredits(4.12))).toBe(4.12); | ||
| }); | ||
| it("carries sub-cent amounts", () => { | ||
| expect(creditsToUsd(51_740)).toBe(0.05174); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { usdToCredits } from "../usdToCredits"; | ||
| import { CREDIT_DECIMALS } from "../creditDecimals"; | ||
| describe("usdToCredits", () => { | ||
| it("is the USDC unit: six decimals, a million per dollar", () => { | ||
| expect(CREDIT_DECIMALS).toBe(6); | ||
| expect(usdToCredits(1)).toBe(1_000_000); | ||
| expect(usdToCredits(92.44)).toBe(92_440_000); | ||
| }); | ||
| it("converts fractional dollars exactly, without floating-point drift", () => { | ||
| // 0.12 * 1e6 is 119999.99999999999 in IEEE-754; the ledger must see 120000. | ||
| expect(usdToCredits(0.12)).toBe(120_000); | ||
| expect(usdToCredits(0.002 * 25.87)).toBe(51_740); | ||
| }); | ||
| it("never rounds up: a fraction of the ledger unit is absorbed, not charged", () => { | ||
| // recoupable/app#2000, owner decision 2026-08-27: pass provider prices | ||
| // through; the unit is the precision and any residue below it is ours. | ||
| expect(usdToCredits(0.0000019)).toBe(1); | ||
| expect(usdToCredits(0.1234567)).toBe(123_456); | ||
| }); | ||
| it("charges at least one ledger unit, even for a zero or sub-unit cost", () => { | ||
| // A request that reached a model is never free: the minimum is one | ||
| // micro-dollar. | ||
| expect(usdToCredits(0)).toBe(1); | ||
| expect(usdToCredits(0.0000001)).toBe(1); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * Decimal places in a credit: the single definition of what a credit is worth. | ||
| * | ||
| * Six, the same unit as USDC: 1,000,000 credits = $1.00, so a credit is a | ||
| * micro-dollar and provider prices pass through exactly. Every conversion | ||
| * between credits and dollars goes through `usdToCredits` or `creditsToUsd`, | ||
| * which are viem's `parseUnits` / `formatUnits` at this precision. | ||
| * | ||
| * Must match `chat/lib/credits/creditDecimals.ts` and the values stored in | ||
| * `credits_usage.remaining_credits`; the database rescale | ||
| * (recoupable/app#2000, database#62) and the deploy of this constant have to | ||
| * land in the same window or every charge is off by the ratio between them. | ||
| */ | ||
| export const CREDIT_DECIMALS = 6; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis🏁 Script executed: #!/bin/bashset -euo pipefail
rg -n -C 4 'CREDIT_DECIMALS|remaining_credits|creditDecimals|rescale'.Repository: recoupable/api Length of output: 50370 🏁 Script executed: #!/bin/bashset -euo pipefail
printf'%s\n''--- applicable conventions ---'
find /tmp/coderabbit-repo-knowledge/recoupable-api-763f6dcf -type f -name '*.md' -print \
| sort \
| xargs -r grep -H -E -m1 '^(#|##|###)'||trueprintf'%s\n''--- changed files ---'
git status --short
git diff --stat
git diff -- lib/credits/creditDecimals.ts
printf'%s\n''--- credit definitions and direct imports ---'
find . -type f \( -name 'creditDecimals.ts' -o -name '*Credit*.ts' -o -name '*credit*.sql'\) -print | sort
rg -n --glob '!pnpm-lock.yaml' --glob '!**/__tests__/**' \
'CREDIT_DECIMALS|DEFAULT_CREDITS|PRO_CREDITS|usdToCredits|credits_usage|remaining_credits' \
lib app supabase migrations chat 2>/dev/null | head -300Repository: recoupable/api Length of output: 20517 🏁 Script executed: #!/bin/bashset -euo pipefail
printf'%s\n''--- scoped conventions ---'
cat /tmp/coderabbit-repo-knowledge/recoupable-api-763f6dcf/conventions/lib.md
cat /tmp/coderabbit-repo-knowledge/recoupable-api-763f6dcf/conventions/lib-supabase.md
cat /tmp/coderabbit-repo-knowledge/recoupable-api-763f6dcf/conventions/repo-wide.md
printf'%s\n''--- credit unit definitions ---'
cat -n lib/credits/creditDecimals.ts
cat -n lib/credits/usdToCredits.ts
cat -n lib/credits/creditsToUsd.ts
cat -n lib/credits/const.ts
cat -n lib/credits/creditsToStripeCents.ts
printf'%s\n''--- schema and migration candidates ---'
find . -type f \( -path '*/migrations/*' -o -iname '*migration*' -o -iname '*.sql'\) -print | sort
rg -n -g '*.sql' -g '*.ts' -g '*.tsx' -g '*.js' -g '*.jsx' \
'credits_usage|credit_grants|remaining_credits|deduct_credits_with_audit|grant_credits_with_audit|CREDIT_DECIMALS|usdToCredits'. \
| grep -v '/__tests__/'| head -400
printf'%s\n''--- all creditDecimals paths ---'
find . -type f -iname '*credit*decimal*' -print | sortRepository: recoupable/api Length of output: 25311 🏁 Script executed: #!/bin/bashset -euo pipefail
printf'%s\n''--- repository layout and database references ---'
find . -maxdepth 3 -type d -print | sort | head -200
rg -n --glob '!pnpm-lock.yaml' --glob '!**/__tests__/**' \
'database/supabase|supabase/migrations|migration|deploy|rescale|app#2000|database#62|chat/lib/credits' \
README.md CONTRIBUTING.md .github app lib package.json supabase database 2>/dev/null ||trueprintf'%s\n''--- current and parent definition ---'
git show HEAD:lib/credits/creditDecimals.ts | cat -n
git show HEAD^:lib/credits/creditDecimals.ts 2>/dev/null | cat -n ||true
git diff HEAD^ HEAD -- lib/credits/creditDecimals.ts 2>/dev/null ||trueprintf'%s\n''--- direct persistence paths ---'
cat -n lib/supabase/credits_usage/insertCreditsUsage.ts
cat -n lib/supabase/credits_usage/incrementRemainingCredits.ts
cat -n lib/supabase/credits_usage/deductCreditsWithAudit.ts
cat -n lib/supabase/credit_grants/grantCreditsWithAudit.tsRepository: recoupable/api Length of output: 16375 Coordinate this constant with the database rescale.
🤖 Prompt for AI Agents | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Custom agent: Enforce Clear Code Style and Maintainability Practices
The digest fixture values are now micro-dollars (4_120_000 = $4.12, confirmed by the "Jane — $4.12" assertion routed through formatCentsAsUsd which now divides by 1e6), but the fields
total_cents,main_cents, andby_modelstill use the "cents" naming (and the source interface in lib/supabase/usage_events/getCreditSpendDigest.ts still documents them as "Credits (cents)"). A reader doing arithmetic on these fields would assume cents and get wrong results by a factor of 10,000. Per the precise-naming requirement in the code-style rule, rename these digest fields (and formatCentsAsUsd) to reflect the micro-dollar unit in concert with the rescale.Prompt for AI agents