Skip to content

fix(store): refund rejected Stripe withdrawals when the coordinator clock is ahead - #1352

Merged
Gajesh2007 merged 5 commits into
masterfrom
fix/stripe-refund-recovery-tests
Oct 5, 2026
Merged

Gajesh2007 merged 5 commits into
masterfrom
fix/stripe-refund-recovery-tests

Conversation

@davidtai

@davidtai davidtai commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fix rejected Stripe withdrawal recovery when the coordinator clock is ahead of PostgreSQL. The previous ledger timestamp bound could hide the original debit and permanently return ErrPayoutConflict.

  • Match debit/refund evidence by account and stripe_withdraw:<id> reference, independent of clocks.
  • Build the partial, non-unique idx_ledger_stripe_refund(account_id, reference) index concurrently for refund and stripe_payout rows. This addresses the review finding that removing the time bound alone would scan a busy provider's lifetime ledger.
  • Preserve exact amount checks, withdrawal/advisory locks, and atomic balance/ledger/refunded-flag writes.
  • Add clock-skew, legacy-credit concurrency, invalid-evidence, rollback, and real PostgreSQL query-plan regression coverage.
  • Merge current master and preserve both sides of the changelog conflict. Update canonical billing/storage documentation.

Before

flowchart TD
  A["Recovery or payout-audit"] --> B["RefundRejectedStripeWithdrawal: lock withdrawal and refund reference"]
  B --> C["Ledger SUM: account + reference + coordinator timestamp bound"]
  D["Database-clock debit predates withdrawal timestamp"] --> C
  C --> E["Missing debit: ErrPayoutConflict"]
  E --> F["Recovery retries; balance stays debited"]
  C -. "Removing only the time bound" .-> G["Account-history scan risks timeout"]
  classDef process fill:#dbeafe,stroke:#2563eb,color:#172554
  classDef failure fill:#fee2e2,stroke:#dc2626,color:#450a0a
  class A,B,C,D process
  class E,F,G failure
Loading

After

flowchart TD
  M["migrate: ensureConcurrentIndex"] --> I["Valid partial account/reference index before readiness"]
  A["Recovery or payout-audit"] --> B["RefundRejectedStripeWithdrawal: same row and advisory locks"]
  I --> C["Indexed ledger SUM: account + reference; no clock bound"]
  B --> C
  C --> D{"Exact debit and valid refund evidence?"}
  D -- "No" --> E["ErrPayoutConflict; no balance mutation"]
  D -- "No prior refund" --> F["Commit credit + ledger row + refunded flag atomically"]
  D -- "Already refunded in ledger" --> G["Repair flag without another credit"]
  F --> H["Subsequent recovery is a no-op"]
  G --> H
  classDef process fill:#dbeafe,stroke:#2563eb,color:#172554
  classDef success fill:#dcfce7,stroke:#16a34a,color:#052e16
  classDef failure fill:#fee2e2,stroke:#dc2626,color:#450a0a
  class M,I,A,B,C,D process
  class F,G,H success
  class E failure
Loading

Validation

Validated locally against isolated PostgreSQL, never production:

  • go test -count=1 ./coordinator/tests/store/... ./coordinator/tests/cmd/...: all 10 packages pass with local DATABASE_URL.
  • make coordinator-test: complete suite and runner guards pass with local DATABASE_URL.
  • make coordinator-build: passes.
  • golangci-lint run ./coordinator/...: 0 issues.
  • make docs-impact-check BASE=origin/master and make docs-check: pass.
  • Go formatting and pre-push checks pass.
  • Query-plan regression loads 100,000 payout ledger rows and requires an indexed account/reference lookup.

The original PR's failed coordinator run was e2e/testbed/profile.TestProfilerDiff (negative timing delta), unrelated to refund correctness. The full local suite passes; latest remote checks are tracked separately.

Refactor Pass

A dedicated, fresh-context, read-only refactor reviewer assessed the final scoped code, tests, and docs after focused validation. No cleanup was warranted; the existing transaction structure and shared concurrent-index helper remain intact. Full tests/build/lint and documentation checks were run on the final version.

Migration And Operations

No API or protocol changes. Startup builds the new index concurrently, preserving live writes; an invalid/interrupted index build fails readiness and requires operator repair. Initial construction still consumes database IO and may extend startup.

After a separately approved deployment, existing confirmed-rejected withdrawals are retried by the recovery loop. A read-only payout-audit can identify affected withdrawals first. This PR review did not deploy, mutate production, or execute refunds.

davidtai and others added 3 commits October 5, 2026 04:01
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Postgres refund check summed ledger rows with created_at >= the
withdrawal created_at. The withdrawal uses the coordinator clock and the
ledger rows use the database clock. When the coordinator clock is ahead,
the check misses the debit and refuses every refund with
ErrPayoutConflict, so rejected Stripe transfers are never refunded.

Remove the time bound. The memory store already has no bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 5, 2026

Copy link
Copy Markdown

Deployment failed for project d-inference with the following error:

You don't have permission to create a Preview Deployment for this Vercel project: d-inference.

View Documentation: https://vercel.com/docs/accounts/team-members-and-roles

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Threat model review — advisory

Reviewed head e452938af14c against base 6dde0beeebb5.

Review not completed for this head. Previous findings are superseded, not confirmed resolved.
Ten-PR pilot complete; maintainer evaluation required

Models: budgeted Sonnet / selective Opus / selective Sol 6.1. Findings require human validation; this review never requests changes or blocks merging.

Coverage: 0/0 source units; cross-file integration pending. 0 selected depth batch(es) pending. All threat definitions indexed; relevant full definitions selected. Unchanged callers are not scanned.

Cost this run: $0.0000 reported, $0.0000 reserved with unknown cost; 0 paid request(s), 0 reused batch(es), 0 provider cache-hit tokens.

Saved findings and earlier report · Historical findings are not confirmed resolved.

@Gajesh2007

Copy link
Copy Markdown
Member

@codex review this

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-05T23:03:01.609880Z e452938 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca51fbe08f

ℹ️ 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".

@vercel

vercel Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
d-inference Ready Ready Preview Oct 5, 2026 11:00pm UTC
d-inference-console-ui-dev Ready Ready Preview Oct 5, 2026 11:00pm UTC
d-inference-landing Ready Ready Preview Oct 5, 2026 11:00pm UTC
eigen-homepages-darkbloom Ready Ready Preview Oct 5, 2026 11:00pm UTC

Request Review

@vercel

vercel Bot commented Oct 5, 2026

Copy link
Copy Markdown

Deployment failed for project darkbloom-status with the following error:

You don't have permission to create a Preview Deployment for this Vercel project: darkbloom-status.

View Documentation: https://vercel.com/docs/accounts/team-members-and-roles

@Gajesh2007

Copy link
Copy Markdown
Member

@codex review the latest head e452938. The account-history scan finding is addressed with a concurrent partial account/reference index and a 100,000-row PostgreSQL plan regression. Full local coordinator tests, build, lint, and docs checks pass.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: e452938af1

ℹ️ 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".

@Gajesh2007
Gajesh2007 merged commit cc13623 into master Oct 5, 2026
38 of 40 checks passed
jonathan308 added a commit that referenced this pull request Oct 6, 2026
Master is at dccc9ee (#1328 goose migrations, #1352, #1351, #1355, #1344). No conflicts. The merge brings master's submodule pin libs/mlx-swift-lm e407e899 (a formatting-only commit on top of 1ee8675a).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
davidtai added a commit that referenced this pull request Oct 6, 2026
Brings in master through #1328 (goose migrations, store in
store/postgres/), #1344, #1351, #1352 and others.

Conflicts:
- ci.yml (Report coverage): master inlined the empty-profile,
  coordinator-row and statement-format checks (#1252). This PR moved
  the same checks into scripts/coordinator-statement-coverage.sh,
  which ci.yml and integration.yml both call. Keep the script call.
- docs/developer/test.md (coverage command): keep the script call.
- docs/developer/test.md (CI workflow map): keep master's ci.yml row
  (adds the provider coverage table) and this PR's integration.yml row
  (adds the e2e coverage row and artifact).

The #1284 store contract tests and coverage tests build and vet
against master's store layout with no change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was successfully deployed

4 active deployments
Preview – d-inference — e452938a Deployed Oct 5, 2026 by vercel[bot]
Preview – d-inference-console-ui-dev — e452938a Deployed Oct 5, 2026 by vercel[bot]
Preview – d-inference-landing — e452938a Deployed Oct 5, 2026 by vercel[bot]
Preview – eigen-homepages-darkbloom — e452938a Deployed Oct 5, 2026 by vercel[bot]
benchmarks — e452938a Deployed Oct 5, 2026 by Gajesh2007 via E2E Benchmarks #2301
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants