feat(billing): settle container usage charges - #5017
Conversation
2614e4d to
4b22435
Compare
| "CONTAINER_BILLING_USER_IDS": "daef8451-f3f3-490e-93f1-21fafc2b005e", | ||
| "CONTAINER_BILLING_ORG_IDS": "", |
There was a problem hiding this comment.
fyi - Just turning this on for me in prod for now. Will extend to our org after.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryIncremental review of Files Reviewed (2 files)
Previous Review Summary (commit 6f41b15)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 6f41b15)Status: 1 Issue Found | Recommendation: Address before merge Executive SummaryThe paid Overview
Issue Details (click to expand)WARNING
Files Reviewed (28 files)
Generated migration snapshot/journal files were excluded from review per repo rules. Settlement math (cumulative floor watermark), idempotent retry handling, fail-closed billing config, partition provisioning, and balance recompute integration all verified correct. Reviewed by kimi-k3 · Input: 76.2K · Output: 15.4K · Cached: 877.6K Review guidance: REVIEW.md from base branch |
Summary
continue,warn, orstopbased on the wallet balance after the debit.compute_usage_charge, an immutable financial ledger for metered infrastructure. A charge records the source that produced it, the payer, SKU, quantity, applied rate, final microdollar amount, and the source timestamp. It is intentionally generic enough for future Worker billing without making container settlement state generic.created_atmonth. The migration creates the initial partitions, and the existing partition-maintenance cron keeps the current and next two months ready. This avoids an ever-growing single ledger table while ensuring a retry is routed to the same monthly partition as the original source event.settled_billable_secondswatermark because cumulative settlement is container-specific. Container ledger rows usecontainer_usage_segmentplus the segment idempotency key as their source identity; future compute producers can use their own stable source identities and quantities.stopverdict blocks new work, allows a 120-second save/drain period, then force-stops the container and suppresses automatic restart.sequenceDiagram participant G as Gastown container participant M as Usage meter participant I as Container interval participant L as compute_usage_charge participant W as Kilo-credit wallet Note over G,W: Paid session runs for 7 minutes G->>M: recordStart at 00:00 M->>I: Create paid interval and snapshot payer plus SKU rate M-->>G: Accepted G->>M: Heartbeat at 05:00 for 300 accepted seconds M->>I: Lock interval and persist segment M->>I: Advance settled watermark to 300 seconds M->>L: Insert charge 1 with heartbeat source key and rate snapshot M->>W: Atomically debit charge 1 M-->>G: Return continue warn or stop from post-debit balance G->>M: recordStop at 07:00 for 120 final seconds M->>I: Lock interval and persist final segment M->>I: Advance watermark to 420 seconds and close interval M->>L: Insert charge 2 with final segment source key M->>W: Atomically debit charge 2 M-->>G: Return final post-debit verdictVerification
Visual Changes
N/A
Reviewer Notes
gastownand userdaef8451-f3f3-490e-93f1-21fafc2b005e; organization billing is disabled by an empty organization allowlist.