feat(gateway)!: reserve capped API key spending before execution - #49
Merged
Conversation
tangletools
approved these changes
Sep 8, 2026
tangletools
left a comment
There was a problem hiding this comment.
Reviewed the prepared execution boundary and verified fixes for malformed preparation falling back to unbounded execution and abort listener cleanup. Independent SQL review covered reservation admission, begin CAS, settlement replay, expiry, and retained uncertain execution. No remaining blockers in this scope. Real distributed D1 and upstream provider enforcement remain explicitly unproven; remote adapters must reject capped execution until they implement enforcement.
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.
Finite API-key caps previously checked settled charges while concurrent requests could execute beyond the remaining budget. The gateway now reserves the full quoted customer charge before compute, reconciles matching usage once, and releases only pre-execution holds. Outstanding executions retain their reservations across workers and rate-counter pruning.
Capped execution requires a prepared stream that enforces the supplied provider, token, and tool limits. Missing or malformed preparation fails closed without falling back to unbounded execution. Current remote agent-app adapters do not implement this enforcement contract and remain unsupported for capped keys; explicitly uncapped keys retain their existing behavior.
Adoption requires the additive reservation table from sqlApiKeyStoreSchemaStatements and apiKeyReservationLifecycle wiring. Version 0.10.0 reflects this capped-host contract change.
Validation: 480 tests passed, typecheck and build passed on Node 24.18.0. Tests cover competing SQL stores, migration preservation, cap/expiry/revocation changes, duplicate execution, settlement replay, uncertain abort retention, malformed preparation, and abort listener cleanup. Independent SQL and prepared-stream reviews found no remaining blockers. Concurrency tests use two store instances against one real SQLite database; distributed D1 and real upstream provider enforcement are not claimed.