Finish coderouter billing operations and telemetry

Merged
LA
2mo ago
+1373-52

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.


View with [code]smith Autofix with [code]smith
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 (Bearer CRON_SECRET) runs under a global advisory lock to serialize cron/operator runs, uses per-subscription cursor last_reconciled_at, bounded concurrency, and CLI bun coderouter:reconcile-billing with --dry-run; exits nonzero on failures/truncation; returns 503 with Retry-After; scheduled at minute 23 in vercel.json.
    • Webhook always re-fetches the current Stripe subscription; handles charge.refunded by 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:benchmark reports status counts, p50/p95/p99, and Server-Timing, and refuses to send route tokens to unapproved origins; runbook docs/coderouter-operations.md covers replay, reconciliation, latency evidence, and observability.
  • Bug Fixes

    • Prevents stale Stripe payloads from overwriting newer entitlements; Stripe is authoritative.
    • Stripe catalog provisioning uses /products/search with next_page and curl timeouts/retries; dev stack listens for charge.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.