Repository navigation
Compose preserved reservation adapters with current accounting source - #1257
Conversation
Paid AI work needs a before-call hold and an exactly-once settlement on the existing wallet (app#2105 milestone 1, reused by Context accounting app#2123). The current preflight only reads the balance, so concurrent requests can each pass it. reserveCreditsOnce, settleCreditReservation and releaseCreditReservation wrap the new database functions. Inputs are validated before the wallet is touched; insufficient capacity is a definite receipt so a caller can return a machine-readable 402. Lost replies and invalid receipts raise reconciliation errors that keep the operation key; settlement reuses the charge receipt identity and its CreditChargeNeedsReconciliation. No caller, price, ceiling, auto-top-up or provider call is added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-up for the credit reservation adapters. A returned database error with SQLSTATE 22023 is raised inside the RPC transaction, which rolls back, so the call wrote nothing. The adapters now throw CreditReservationRejected with the fixed database reason in that case, so a future caller can return a definite 402/409 instead of replaying an identical call forever. Every other error, lost reply or invalid receipt still raises the reconciliation error. The README now says a held receipt is not an execution lease (duplicate deliveries must be deduplicated by the caller's attempt identity) and that connecting any caller requires database#101, api#1009 and database#88. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Pre-merge checks:
|
Codex Review Summary: ✅ CompletedThis 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.
All reported issues were addressed across 12 files
Architecture diagram
sequenceDiagram
participant Caller as Trusted Server Caller
participant Adapter as Reservation Adapter
participant Validator as Zod Validator
participant Supabase as Supabase RPC Client
participant DB as Database (Postgres RPC)
Note over Caller,DB: Opt-in credit reservation adapters (no production callers yet)
Caller->>Adapter: reserveCreditsOnce(accountId, operationKey, creditsToReserve)
Adapter->>Validator: validateCreditReservation()
Validator-->>Adapter: Validated input
Adapter->>Supabase: rpc("reserve_credits_once", params)
Supabase->>DB: reserve_credits_once RPC
alt Hold created
DB-->>Supabase: { state: "held", reservationId, creditsHeld, spendableCredits }
Supabase-->>Adapter: Receipt data
Adapter->>Validator: Parse & validate receipt
Validator-->>Adapter: Valid receipt
Adapter-->>Caller: CreditReservationReceipt (held)
else Duplicate delivery
DB-->>Supabase: { state: "reused", status: "held"|"settled"|"released" }
Supabase-->>Adapter: Receipt data
Adapter-->>Caller: CreditReservationReceipt (reused)
else Insufficient credits
DB-->>Supabase: { state: "insufficient", creditsRequested, spendableCredits }
Supabase-->>Adapter: Receipt data
Adapter-->>Caller: CreditReservationReceipt (insufficient)
else SQLSTATE 22023 rejection
DB-->>Supabase: { code: "22023", message }
Supabase-->>Adapter: Error object
Adapter->>Adapter: toCreditReservationRejection(error)
Adapter-->>Caller: throw CreditReservationRejected (definite, nothing written)
else Transport/invalid receipt failure
DB-->>Supabase: Other error, lost response, or invalid receipt
Supabase-->>Adapter: Error/null data
Adapter-->>Caller: throw CreditReservationNeedsReconciliation (retain same key)
end
Note over Caller,DB: After work completes - settlement
Caller->>Adapter: settleCreditReservation(accountId, operationKey, creditsToDeduct, event)
Adapter->>Validator: validateCreditCharge()
Validator-->>Adapter: Validated input
Adapter->>Supabase: rpc("settle_credit_reservation", params)
Supabase->>DB: settle_credit_reservation RPC
alt Settlement successful
DB-->>Supabase: { state: "settled", eventId, creditsCharged, creditsReleased }
Supabase-->>Adapter: Receipt data
Adapter->>Validator: Parse & validate receipt & invariant check
Adapter-->>Caller: CreditSettlementReceipt
else Duplicate settlement
DB-->>Supabase: { state: "reused", ... }
Supabase-->>Adapter: Receipt data
Adapter-->>Caller: CreditSettlementReceipt (reused)
else SQLSTATE 22023 rejection
DB-->>Supabase: { code: "22023", message }
Supabase-->>Adapter: Error object
Adapter->>Adapter: toCreditReservationRejection(error)
Adapter-->>Caller: throw CreditReservationRejected (nothing written)
else Uncertain outcome (may have committed)
DB-->>Supabase: Other error/lost response/invalid receipt
Supabase-->>Adapter: Error/null data
Adapter-->>Caller: throw CreditChargeNeedsReconciliation (keep same key)
end
Note over Caller,DB: Cancellation - release hold without charge
Caller->>Adapter: releaseCreditReservation(accountId, operationKey)
Adapter->>Validator: validateCreditReservationRelease()
Validator-->>Adapter: Validated input
Adapter->>Supabase: rpc("release_credit_reservation", params)
Supabase->>DB: release_credit_reservation RPC
alt Release successful
DB-->>Supabase: { state: "released", creditsHeld, creditsReleased }
Supabase-->>Adapter: Receipt data
Adapter->>Validator: Parse & validate receipt & equality check
Adapter-->>Caller: CreditReleaseReceipt
else SQLSTATE 22023 rejection
DB-->>Supabase: { code: "22023", message }
Supabase-->>Adapter: Error object
Adapter->>Adapter: toCreditReservationRejection(error)
Adapter-->>Caller: throw CreditReservationRejected
else Uncertain outcome
DB-->>Supabase: Other error/lost response/invalid receipt
Supabase-->>Adapter: Error/null data
Adapter-->>Caller: throw CreditReservationNeedsReconciliation
end
Note over Caller,DB: All adapters: no retry, no fresh key, no provider calls, no auto-top-up. Operation key stable across replays.
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
There was a problem hiding this comment.
0 issues found across 1 file (changes from recent commits).
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Auto-approved: Adds opt-in credit reservation adapters, validators, error types, tests, and README as new inert modules; no existing production code or callers change, so the change is bounded and adds no runtime behavior until explicitly wired.
View guided diff | Turn on auto-fix | Re-trigger cubic
Result
Preserve the reviewed opt-in reservation adapters from #1249 while normally composing current main
aca47f23e38c924e4d5e2fa8b1cc7d417dc6507f. Normal merge4fda79203a1052537704fd9ab1f3fca616b0494bretains both histories; final headc8828d93c0058b30f4f13aebacef1c00d06b2e39adds the reviewed README correction. All adapter/validator/test files remain byte-identical to #1249; the README now corrects duplicate receipts to report the hold’s current status and explicitly requires independent dispatch ownership/reconciliation; original #1026 and #1249 remain untouched.The adapters remain unused by production callers. Actual Context accounting ports accept their real function signatures and receipt variants in a compile-only fixture. Incoming Context strict CI, Packs lifecycle/replay, package scripts and workflows match main exactly. No wallet or behavior changes.
Qualification
Independent exact-head composition review: PASS, no actionable findings. 73 focused reservation/receipt tests, production/contract types and real-function port compatibility passed. Independent README/contract correction review passed; runtime bytes remain unchanged. Final head c8828d9 passes all reported checks: full suite 9,713 passed / 53 skipped, Context strict types, offline Packs replay, production/contract types, format/lint and compatibility. Cubic correction review reports zero issues; CodeRabbit is a review skip. Preview success is not production acceptance. Final qualification is recorded in the coordinator handoff.
Release boundary
Ready for source review only; coordinator owns final merge/release decision after Sites repairs. Database #134/#135 remain draft with no apply. No production wiring, paid execution, provider calls or migration authorization. Source compatibility does not establish durable Context service wiring or deployed settlement.