Finish coderouter billing operations and telemetry
Merged
LA
2mo ago+1373+1373+0123456789012345678901234567890123456789-52-52-01234567890123456789
Adds hourly Stripe/RDS reconciliation, delayed-event repair, refund and checkout-start analytics, internal runbooks and benchmark tooling, bounded retries/concurrency, privacy-safe Sentry breadcrumbs, and deterministic coverage.
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Completes coderouter billing operations and telemetry for the private beta. Adds serialized hourly Stripe↔RDS reconciliation, hardened webhooks (including refunds), privacy‑safe checkout/refund analytics, and lightweight ops tooling.
-
New Features
- Hourly reconciliation cron
/api/cron/billing-reconcile(BearerCRON_SECRET) runs under a global advisory lock to serialize cron/operator runs, uses per-subscription cursorlast_reconciled_at, bounded concurrency, and CLIbun coderouter:reconcile-billingwith--dry-run; exits nonzero on failures/truncation; returns 503 withRetry-After; scheduled at minute 23 invercel.json. - Webhook always re-fetches the current Stripe subscription; handles
charge.refundedby joining invoice→subscription, records analytics, and does not revoke active access. - Checkout-start analytics from checkout routes are deferred via Next.js
after()with a safe fallback; unified capture adds stable$insert_id, team grouping via$groups, and a single retry on 5xx only. - Benchmark harness
bun coderouter:benchmarkreports status counts, p50/p95/p99, andServer-Timing, and refuses to send route tokens to unapproved origins; runbookdocs/coderouter-operations.mdcovers replay, reconciliation, latency evidence, and observability.
- Hourly reconciliation cron
-
Bug Fixes
- Prevents stale Stripe payloads from overwriting newer entitlements; Stripe is authoritative.
- Stripe catalog provisioning uses
/products/searchwithnext_pageandcurltimeouts/retries; dev stack listens forcharge.refunded. - Adds privacy-safe Sentry breadcrumb for reconciliation runs; analytics failures never affect billing paths.
Summary by CodeRabbit
-
Billing
- Added automated Stripe subscription reconciliation with drift detection, repair, dry-run support, and failure reporting.
- Scheduled recurring reconciliation with secure access controls.
- Improved handling of refunded charges and stale subscription events to keep entitlements accurate.
- Improved Stripe catalog provisioning and webhook configuration.
-
Observability
- Added checkout-start and refund analytics with retries, deduplication, and privacy-safe event data.
-
Operations
- Added benchmarking and billing-reconciliation command-line tools.
- Added an operations runbook covering billing recovery, performance checks, and observability practices.