Repository navigation
Description
Summary
On main, the sweep that gives back a permit reservation after a failed upstream call takes the 100 oldest candidates and only then checks which of them it may release. An attempt whose live refund failed, and that was recorded as an operator work item, is never released by this sweep. In the tests, neither the sweeps nor an operator retry that resolves the item took it out of the front of that order. Once 100 such attempts are older than an attempt that could be released, the sweep does not reach that attempt. Its permit then keeps the reservation and can refuse further calls with permit_budget_exceeded.
This moves no wallet money wrongly and lets no permit admit spend past max_credits. The error goes the other way: a permit refuses spend it should allow. It is hard to reach in a production-like deployment (see Preconditions).
CodeRabbit raised this in its review of #330 at head 707ba293ade3b2de06ff1ac7e5646a4d0411b25c (review body, no thread: #330 (review)). It is not new with #330. The query and the sweep are the same on main.
Affected area
- spec
- verifier
- Python SDK
- TypeScript SDK
- integration
- example
- gateway
- build or tooling
- Other:
Version or commit
main at 37618e049f589318d30112f4e9cc5c656c57cdbd. Line numbers are at that commit. Also tested at the #330 head 707ba293ade3b2de06ff1ac7e5646a4d0411b25c, with the same result. Tests ran on SQLite.
The finding (low)
- Where:
- Periodic job:
gateway/app/main.pyline 253 (waits 300 seconds before each pass) and lines 322 to 325 (reconcile(idle_seconds=..., terminal_idle_seconds=300), nolimit). It runs only whenDATABASE_URLis set (line 300). - Batch size:
reconciledefaults tolimit: int = 100(gateway/app/services/mcp_dispatch_reconciliation.pyline 256) and passes it to the query (lines 354 to 357). - Query:
list_unreleased_budget_attempts(gateway/app/services/mcp_dispatch_attempts.pylines 2224 to 2288). It selectsstate == "returned_error",budget_released_at IS NULLandupdated_atolder than the cutoff, orders byupdated_at, and applies the limit. It has no eligibility condition and no wallet filter. - Eligibility, after the query: the loop skips an attempt when
debit_refunded_atis NULL and its debit exists (mcp_dispatch_reconciliation.pylines 376 to 378). - Operator retry:
_retry_lockedingateway/app/services/refund_reconciliation.py(line 624). It refunds and decrementsspent_credits. It writes nothing on the dispatch attempt row.
- Periodic job:
- Actual:
- Each upstream call whose live refund fails, and that is recorded as an operator work item, leaves an attempt with
statereturned_error,budget_released_atNULL,debit_refunded_atNULL and a debit. The sweep skips it on every pass. In the tests, 3 sweeps changed theupdated_atof none of these attempts. - The operator retry of its pending refund gives back the money and the permit budget, but leaves
debit_refunded_at,budget_released_atandupdated_atas they were. Tested: after 100 operator retries, 0 attempt rows had changed. So a resolved item stays a skipped candidate at the front. - With 100 such attempts older than a releasable attempt, the sweep does not reach the releasable one.
- Each upstream call whose live refund fails, and that is recorded as an operator work item, leaves an attempt with
- Measured, same on both commits: 100 stuck attempts on wallet A, each made through
POST /mcp/messageswith a failing refund. One newer attempt on a permit of wallet B, whose refund succeeded and whose release failed. In 3 sweeps it was not released. Wallet B's permit (max_credits2) stayed atspent_credits2 and refused the next call withpermit_budget_exceeded. With 99 stuck attempts, or with the releasable attempt older than the stuck ones, the first sweep released it and the next call was admitted. The sweeps did not change wallet A's balance, and the release did not change wallet B's balance. - Expected: the sweep reaches every attempt it may release, however many older attempts it has to skip.
- Preconditions:
- 100 or more refund-failed attempts that are older, by
updated_at, than the releasable attempt. They can belong to any wallet. In the tests, neither the sweeps nor the operator retry removed them. - An attempt that the live path left releasable but not released. In the tests this came from a contended release (
PermitWriteContendedError). By reading, on this path that error comes only from SQLite write conflicts: the release retries only on errors thatis_retryable_write_conflictmatches (gateway/app/core/resilience.pylines 209 to 219), and the compare-and-set that raises it directly applies only to an attempt that was never dispatched (gateway/app/services/permits.pylines 1751 to 1755 and 1815 to 1823). Production-like environments refuse a SQLiteDATABASE_URLat boot (gateway/app/core/trust_mode.pylines 270 to 291 and 303 to 304). On PostgreSQL it needs another fault, for example, by reading, an exception during the release after the refund, or the lost acknowledgement described in this comment on Permit reservation can be released twice for one refunded upstream call, so a permit admits spend past max_credits; and the replay contract docs misstate revoked-permit replay #434: Permit reservation can be released twice for one refunded upstream call, so a permit admits spend past max_credits; and the replay contract docs misstate revoked-permit replay #434 (comment)
- 100 or more refund-failed attempts that are older, by
- Cost: the permit keeps the reservation while it is live. The sweep does not count or report the attempts it skips. By reading, not tested:
PermitService.reconcile_budgetsrepairs only permits that are not active or are past expiry. For a live permit it only logs apermit_budget_live_driftwarning with the permit id (gateway/app/services/permits.pylines 2037 to 2047 and 2224 to 2239). That warning names the permit, not the stuck attempts. - What fix(refunds): record a pending refund even when its receipt cannot be signed #330 adds: one more source of such attempts, a call whose refund failed while its receipt could not be signed. On main the terminal sweep healed that call once refunds worked again. fix(refunds): record a pending refund even when its receipt cannot be signed #330 records it as an operator work item.
- Related: Permit reservation can be released twice for one refunded upstream call, so a permit admits spend past max_credits; and the replay contract docs misstate revoked-permit replay #434. The operator retry does not mark the attempt there either. If the retry marks the attempt released, as the corrected fix in that comment needs, resolved items also leave this queue.
Suggested next step
- Put the eligibility test into the query, before the limit, with the same two branches as
_find_operation_debit(mcp_dispatch_reconciliation.pylines 809 to 846). Or page past skipped attempts with a keyset onupdated_atand the attempt id. Keep the check at lines 376 to 378. - Do not make the operator retry set
debit_refunded_atalone. The retry already decrementsspent_credits, so the sweep would then release the reservation a second time. - Add a regression test with more stuck attempts than the limit.
Nothing here was run on PostgreSQL.
Generated by Claude Code
Activity