Repository navigation
Protect shared reservations across pack and legacy wallet writes - #134
Draft
sidneyswift wants to merge 4 commits into
Draft
sidneyswift wants to merge 4 commits into
sidneyswift wants to merge 4 commits into
Conversation
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>
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
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. |
|
Updates to Preview Branch (codex/shared-reservation-composition) ↗︎
Tasks are run on every commit but only new migration files are pushed.
View logs for this Workflow Run ↗︎. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.