Skip to content

Budget-release sweep takes the 100 oldest candidates before it checks which can be released, so stuck attempts can keep a newer reservation from being released #439

In progress

Description

@PetrefiedThunder

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.py line 253 (waits 300 seconds before each pass) and lines 322 to 325 (reconcile(idle_seconds=..., terminal_idle_seconds=300), no limit). It runs only when DATABASE_URL is set (line 300).
    • Batch size: reconcile defaults to limit: int = 100 (gateway/app/services/mcp_dispatch_reconciliation.py line 256) and passes it to the query (lines 354 to 357).
    • Query: list_unreleased_budget_attempts (gateway/app/services/mcp_dispatch_attempts.py lines 2224 to 2288). It selects state == "returned_error", budget_released_at IS NULL and updated_at older than the cutoff, orders by updated_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_at is NULL and its debit exists (mcp_dispatch_reconciliation.py lines 376 to 378).
    • Operator retry: _retry_locked in gateway/app/services/refund_reconciliation.py (line 624). It refunds and decrements spent_credits. It writes nothing on the dispatch attempt row.
  • Actual:
    1. Each upstream call whose live refund fails, and that is recorded as an operator work item, leaves an attempt with state returned_error, budget_released_at NULL, debit_refunded_at NULL and a debit. The sweep skips it on every pass. In the tests, 3 sweeps changed the updated_at of none of these attempts.
    2. The operator retry of its pending refund gives back the money and the permit budget, but leaves debit_refunded_at, budget_released_at and updated_at as they were. Tested: after 100 operator retries, 0 attempt rows had changed. So a resolved item stays a skipped candidate at the front.
    3. With 100 such attempts older than a releasable attempt, the sweep does not reach the releasable one.
  • Measured, same on both commits: 100 stuck attempts on wallet A, each made through POST /mcp/messages with 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_credits 2) stayed at spent_credits 2 and refused the next call with permit_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:
  • 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_budgets repairs only permits that are not active or are past expiry. For a live permit it only logs a permit_budget_live_drift warning with the permit id (gateway/app/services/permits.py lines 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.py lines 809 to 846). Or page past skipped attempts with a keyset on updated_at and the attempt id. Keep the check at lines 376 to 378.
  • Do not make the operator retry set debit_refunded_at alone. The retry already decrements spent_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

  1. added
    bug
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions