Repository navigation
fix(billing): make failed schedule entitlement updates retryable behind the gate - #21373
Conversation
…gestion gate Failure-Class: FC-billing-webhook-acknowledges-failed-entitlement
…dger Co-authored-by: Cursor <[email protected]> # Conflicts: # backend/docs/runbooks/billing-reconciliation.md # backend/routers/payment.py # config/feature-flags.yaml
…age init The webhook ledger loads billing_ledger at import time, and the promotion-code stub only defined record_fallback, so package init failed on bucket_outcome. Co-authored-by: Cursor <[email protected]>
…-retry Co-authored-by: Cursor <[email protected]> # Conflicts: # backend/docs/runbooks/billing-reconciliation.md # backend/routers/payment.py # backend/tests/unit/test_stripe_webhook_none_guard.py # backend/utils/subscription_state_machine.py # config/feature-flags.yaml
…etry Co-authored-by: Cursor <[email protected]> # Conflicts: # backend/routers/payment.py # config/feature-flags.yaml
…-retry Co-authored-by: Cursor <[email protected]> # Conflicts: # config/feature-flags.yaml
…ion. Co-authored-by: Cursor <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 625d06c53e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| async def dispatch_with_ledger(event: dict, handler: Callable[[], Awaitable[dict]], run_db: Callable) -> dict: | ||
| if not webhook_ledger_enabled(): | ||
| return await handler() | ||
| from database import stripe_event_ledger as ledger |
There was a problem hiding this comment.
Move the ledger imports to module scope
Move this import to the module header, along with the database._client import inside stripe_event_ledger._client. These new runtime modules introduce function-local imports even though the backend explicitly requires every import at module scope so the database → utils → routers dependency graph remains statically traceable; neither import constructs a Firestore client, so call-time client creation can still be preserved after moving them.
AGENTS.md reference: backend/AGENTS.md:L161-L169
Useful? React with 👍 / 👎.
| return get_firestore_client() | ||
|
|
||
|
|
||
| def claim_event(event: dict[str, Any], token: str, *, now: datetime | None = None, client=None) -> Claim: |
There was a problem hiding this comment.
Name the Firestore injection seam
firestore_client
Expose the injected client as the keyword-only firestore_client parameter on claim_event, finish_event, and observe_event rather than introducing a parallel client convention. Repository test and database tooling use the standardized keyword, so callers following the documented seam currently receive TypeError: unexpected keyword argument 'firestore_client' and need ledger-specific adapters.
AGENTS.md reference: backend/AGENTS.md:L173-L175
Useful? React with 👍 / 👎.
What changed and why
Named bug: failed subscription-schedule reads/writes were logged and acknowledged with HTTP 200, preventing Stripe retry after an entitlement update was lost. Behind the existing new default-off billing-ingestion gate, both completed and canceled schedule handlers now return 503 on transient failure. The ledger can then safely protect schedule event ids. Missing/deleted-user NotFound stays acknowledged. Creator-app metadata is explicitly excluded from suppression so this Omi plan fix does not redefine creator billing.
Review after audit → state machine → read-only reconciler → durable ledger. This final dependent draft targets main and contains prerequisite commits until they merge. Off preserves the legacy characterization; on changes failure delivery only, never plan/status/limits/price projection. INF files only, with minimal required shared metadata: flag registry/generated doc and one new failure-class definition. No new workflow, deploy, production write, Stripe request, secret access or flag activation.
Product invariants affected
none. Legacy entitlement payloads remain identical; failed delivery becomes retryable only under the new gate.
Verification and review hardening
Schedule tests now exercise failed first delivery → one successful write → suppressed third delivery for both completion and cancellation. Deleted-owner tests assert acknowledged receipt and second-delivery suppression. Confirmed Stripe
resource_missingis permanently acknowledged; other InvalidRequest errors still produce gated retries. Unknown-price schedules preserve HTTP 200 with a reclaimable failed receipt. The inactive-to-Basic builder comment is restored. Schedule event projection is shared, keeping payment.py below the existing line-count limit; the static None guard follows that helper.Validation:
BACKEND_UNIT_TEST_FILE_LIST=/tmp/inf-r2-final-tests bash backend/test.sh: 411 tests passed across eight files (22 Neo, 39 state machine, 30 reconciler, 34 ledger, 232 characterization, 23 behavioral, 18 schedule, 13 None guards). Gate-off/on, provider/write failures, receipt replay, unknown-price recovery and permanent ignores are covered. No live provider/account validation or Firestore emulator contention was run.Tests
backend/tests/unit/test_billing_schedule_retry.py is the reusable behavioral contract guard; backend/tests/unit/test_stripe_event_ledger.py covers error/replay integration. No entitlement or Stripe money mutation is invented.
UI evidence
UI-Evidence: none -- default-off operational billing bug fix.
Failure class (fixes)
Failure-Class: new
Failure-class transition narrative
FC-billing-webhook-acknowledges-failed-entitlement: a transient schedule fetch/persist failure must remain retryable, not become successful delivery. The canonical boundary is the gated schedule adapter plus ledger success receipt; INF owns it. The behavioral guard exercises both statuses, both gate modes, both failure layers, successful duplicate suppression and deleted-owner acknowledgement. Evidence is the reproduced current origin/main HTTP-200/no-write path, captured before refactoring; evidence_prs stays empty until this first fix merges.
Risks for reviewer
Review order and handoff
#21368 → #21370 → #21371 → #21372 → #21373
All four remain drafts against main. #21368 is merged as 764a0ac. Current origin/main was merged into the bottom branch, and review fixes were propagated upward with merge commits. No rebase, force push or skip hatch was used. No runtime acceptance, live reconciliation, secrets, flag activation, deployment or TTL policy creation was performed.