Commercial Ledger & Dual-Currency Economics
Integer minor-unit USD/CNY pricing, append-only dual-pool credit_ledger, fail-closed webhooks, and on-demand IMAP caching.
Exact Integer Accounting & Dual-Pool Credits
- Zero Floating-Point Money: Currency minor units (
USDcents,CNYfen) and AI credits useint64across PostgreSQL, Go, and JSON, paired withshopspring/decimalceiling rounding andmin_charge_creditsfor token-to-credit conversion. - Append-Only
credit_ledger& Concurrency Slots:- Historical ledger rows are never updated or deleted; refunds write compensating entries guarded by unique
idempotency_keyvalues (ai:<requestID>,order:<orderNo>,redeem:<code>:<userID>). - AI streams pre-check
available >= min_charge_credits(402 INSUFFICIENT_CREDITS), acquire a bounded Valkey slot (lock:ai:slot:<uid>, max 2 concurrent streams), and settle inside a singleSELECT ... FOR UPDATEtransaction deducting expiringcredit_buckets(expires_at ASC) before permanentpaid_credits.
- Historical ledger rows are never updated or deleted; refunds write compensating entries guarded by unique
Fail-Closed Webhooks & Storage Economics
- Signed Asynchronous Fulfillment Only: Browser
return_urlredirects never grant entitlements. Only verified webhooks (/api/v1/webhooks/stripe,/api/v1/payments/callback/{provider}) or Step-Up audited admin fulfillment transitionpending → paidunderFOR UPDATE, asserting exact amount, currency, and proration preconditions (409 PRORATION_STATE_CHANGED). - On-Demand Large Attachments (
cloud): Attachments aboveSTORAGE_ONDEMAND_THRESHOLD_BYTES(2 MiB) record metadata +imap_uid/imap_part_spec(stored_remotely = true) and stream from upstream IMAP on demand (<= 35 MiB) into a 7-day ephemeral cache.