Skip to content

Protect shared reservations across pack and legacy wallet writes - #134

Draft
sidneyswift wants to merge 4 commits into
mainfrom
codex/shared-reservation-composition
Draft

sidneyswift wants to merge 4 commits into
mainfrom
codex/shared-reservation-composition

Conversation

@sidneyswift

@sidneyswift sidneyswift commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Shared reservations currently leave money in the wallet that legacy debits and pack approvals can spend. This successor integrates Database #115 unchanged onto current main and adds a wallet-write guard so those operations cannot consume reserved funds. Settlement closes only its own hold within the charge transaction; failures restore the hold. A wallet row-version update on reserve also closes a reproduced repeatable-read race.

Released pack holds already deduct funds, so the existing pack-aware refill remains unchanged. API #1026 adapters retain their inputs and receipts; companion recoupable/api#1249 documents this composition. Original #115 remains untouched. This is the common primitive work for recoupable/app#2105 milestone 1 and #2123, separate from Context request/pilot ceilings and Sites hosting policy.

Validation: initial composition regressions failed in six cases; 36 reservation/composition tests now pass on PostgreSQL15 and17, and 11 existing pack tests pass on17. Independent review passed and independently reran all17 new composition tests. Fixtures use real owning migration functions on disposable local PostgreSQL with synthetic balances and TCP disabled. CI adds the composition suite on15/16.

No caller is activated, no price changes, and no production schema/grant or financial operation was performed. One-wallet enforcement remains a separate prerequisite; legacy callers can incur provider expense before a guarded debit fails, so this is not pre-dispatch spend authorization. Release requires the original reservation migration, released pack migration, then the additive composition migration and verified grants. Do not remove the guard or retained receipts while live holds exist; stop new reservations and reconcile holds before any rollback. Root retains review/merge/release authority.

sidneyswift and others added 4 commits October 10, 2026 16:41
AI work needs to authorize capacity before paid calls start and settle
the actual charge exactly once afterwards (app#2105 milestone 1, which
Context accounting app#2123 reuses). The existing preflight only reads
the balance, so concurrent requests can each pass it and overspend.

Holds live in a new credit_reservations table; they are claims, never
temporary debits, so refill, top-up and auto-top-up never see them as
consumption. Reserve, settle and release lock the owner's single wallet
row first and compute spendable = remaining_credits - outstanding holds.
Settlement charges through record_credit_charge_once with the same
operation key, so reservation and charge share one retry-stable
identity. Service role only; no price, ceiling, expiry, top-up or
caller is added, and legacy debit writers still ignore holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-up for credit reservations. The service role's UPDATE grant
now covers only the closing columns (state, credits_charged,
charge_event_id, closed_at), so a hold's owner, key and amount cannot be
rewritten outside the functions; a new test checks this and that the
service role can still read open holds to reconcile a lost key.

The docs now say that connecting any caller requires database#101 and
api#1009 (main's API top-up and refill still write an absolute balance
read earlier, which can erase a settlement) and database#88. The top-up
and refill tests are renamed to say they model atomic writers. The docs
also note that every raised rejection is SQLSTATE 22023 and rolled back,
and that a held receipt is not an execution lease for duplicate deliveries.

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

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@supabase

supabase Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Updates to Preview Branch (codex/shared-reservation-composition) ↗︎

Deployments Status Updated
Database ✅ Tue, 17 Nov 2026 19:15:43 UTC
Services ✅ Tue, 17 Nov 2026 19:15:43 UTC
APIs ✅ Tue, 17 Nov 2026 19:15:43 UTC

Tasks are run on every commit but only new migration files are pushed.
Close and reopen this PR if you want to apply changes from existing seed or migration files.

Tasks Status Updated
Configurations ✅ Tue, 17 Nov 2026 19:15:44 UTC
Migrations ✅ Tue, 17 Nov 2026 19:15:49 UTC
Seeding ✅ Tue, 17 Nov 2026 19:15:53 UTC
Edge Functions ✅ Tue, 17 Nov 2026 19:15:53 UTC

View logs for this Workflow Run ↗︎.
Learn more about Supabase for Git ↗︎.

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.

1 participant