Skip to content

fix(sync): release cap-hold reservations at projection so a full ledger recovers (SCA-2886) - #25697

Merged
Git-on-my-level merged 2 commits into
BasedHardware:mainfrom
Git-on-my-level:lane/sol-caphold-release
Nov 18, 2026
Merged

Git-on-my-level merged 2 commits into
BasedHardware:mainfrom
Git-on-my-level:lane/sol-caphold-release

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

What this fixes

A user's stt_cap_hold_reservations ledger pins at MAX_OPEN = 200 and never recovers. Once the bound trips, every further reserve() for that owner raises ValueError('cap-hold reservation bound exceeded'), the two-minute cap-hold-durable-redrive sweep catches it per owner and still returns HTTP 200, and the failure is visible only in logs. It has run continuously since 2026-11-03T19:32:28Z at roughly 2,500–3,200 log entries per day.

The mechanism: a reservation is released only as a side effect of the worker's next capacity check (processing_backlog_worker.py:353). A pinned owner never reaches that check again, so the ledger cannot drain itself.

The change

Two edits, both small and reversible.

backend/utils/sync/cap_hold_recovery.py — finish_handoff now releases the matching reservation after durable projection and before erasing audio. Releasing first means a failed release leaves the hand-off discoverable for retry rather than erasing the only copy.

backend/utils/sync/cap_hold_reservations.py — reserve() runs the existing settlement pass once when the ledger is full, then retries. This also frees slots left by hand-offs erased under the old code. The 200-entry bound and the six-hour stale alert still apply; neither threshold changed.

_settle now matches on batch_job_id before removing an entry, so a reservation re-created for a different job is not dropped, and the released counter only increments for outcomes actually applied.

Verification

  • Two full-ledger regressions in backend/tests/unit/test_cap_hold_batch_debit.py failed before the change and pass after.
  • 68 focused tests pass across test_cap_hold_batch_debit.py and test_cap_hold_batch_admission.py.
  • Verification used synthetic data. Production was untouched.

Not claimed

The 24-hour production readback is pending. This change makes a pinned ledger able to recover; it does not prove the production stock of pinned ledgers drains, and it does not address the ~30k quarantined batch stock, which is a separate item.

Product invariants affected

none

Failure class (fixes)

Failure-Class: new

Family: work-bounded-resumable — "A timeout, budget or cap does not actually bound the work, discards in-flight work, or leaves an unbounded keyspace/TTL."

Proposed class: FC-cap-bound-pins-owner-without-release-path

A cap that bounds concurrent state must not be able to become a permanent block. When the only path that releases an entry is a later admission attempt, and admission is refused once the cap is reached, the cap converts a bounded resource into a deadlock: the owner can never admit again and the entries can never be released. The bound must be reachable from a path that does not itself require admission.

Instance: stt_cap_hold_reservations pins at MAX_OPEN = 200 because release is a side effect of the worker's next capacity check (processing_backlog_worker.py:353), and a pinned owner never reaches that check. The two-minute recovery sweep catches the ValueError per owner and still returns HTTP 200, so the deadlock is visible only in logs. Running continuously since 2026-11-03T19:32:28Z at ~2,500–3,200 entries/day.

No existing class in this family fits: FC-bounded-discovery-pinned-by-blocked-page is about a scanner re-querying the oldest page, not about a cap whose release path requires the admission the cap refuses.

View guided diff

…er recovers

A user's stt_cap_hold_reservations ledger pins at MAX_OPEN=200 because a
reservation is only released as a side effect of the worker's next capacity
check, and once the bound trips that check is never reached again. The
two-minute cap-hold recovery sweep catches the ValueError per owner and still
returns HTTP 200, so the failure is only visible in logs.

Two changes:

- finish_handoff releases the matching reservation after durable projection
  and before erasing audio. A failed release leaves the hand-off discoverable
  for retry.
- reserve() runs the existing settlement pass once when the ledger is full,
  then retries. This also frees slots left by hand-offs erased under the old
  code. The 200-entry bound and the six-hour stale alert still apply.

_settle now matches on batch_job_id before removing an entry, so a reservation
re-created for a different job is not dropped, and the released counter only
increments for outcomes actually applied.

Refs SCA-2886, SCA-2885.
A cap that bounds concurrent state must not be able to become a permanent
block. When the only path that releases an entry is a later admission attempt,
and admission is refused once the cap is reached, the cap converts a bounded
resource into a deadlock.

Evidence: the stt_cap_hold_reservations ledger pinning at MAX_OPEN=200, fixed
in the preceding commit.

Refs SCA-2886.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-18T14:12:37.359934Z
We couldn’t complete this review. Comment @codex review to try again.
374b9ca PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@Git-on-my-level Git-on-my-level changed the title fix(sync): release cap-hold reservations at projection so a full ledger recovers fix(sync): release cap-hold reservations at projection so a full ledger recovers (SCA-2886) Nov 18, 2026
@Git-on-my-level
Git-on-my-level merged commit 075453f into BasedHardware:main Nov 18, 2026
61 checks passed
Git-on-my-level added a commit that referenced this pull request Nov 19, 2026
… as backpressure (SCA-2886) (#25745)

## What this fixes

#25697 released a cap-hold reservation when its projection is observed
and made a full ledger run the terminal-outcome settle pass before
refusing. Verify found it serving (`a6824b8`, 2026-11-19T00:49Z) but the
Done check unmet: `MAX_OPEN = 200` still refused owners who legitimately
hold 201-205 live reservations, so `ValueError: cap-hold reservation
bound exceeded` could not reach zero.

The bound was sized for a backlog page (`PAGE = 10`), but one sweep
admits up to 100 turns x 10 items for a single owner before any batch
outcome can settle. A page-sized bound therefore sat below a legitimate
burst, and the refusal was logged as a generic worker failure with a
traceback, which made the deadlock class uncountable.

## The change

- `MAX_OPEN` 200 -> 1000, matching the sweep ceiling. The ledger
document stays ~150 KB, far under Firestore's 1 MiB limit. The settle
pass keeps terminal entries out, so the bound now counts live work only.
- A full ledger raises `ReservationBoundExceeded` (a `ValueError`)
carrying `live` and `settled`, and logs `outcome=bound_exceeded`. The
worker catches it and logs `outcome=reservation_full` without a
traceback. `settled=0` is backpressure (every entry is live);
`settled>0` is the deadlock signature.
- `open_units()` and `reserve()` share one settle pass (`_open_ledger`),
so the two cannot drift.

No schema change; no change to what a committed hold means; revert by
reverting the commit.

## Done check (revised)

After this serves, `ValueError: cap-hold reservation bound exceeded` in
`backend-sync` logs is zero over 24 h. If any refusal appears, its
`outcome=bound_exceeded live= settled=` line must show `settled=0` and
never `settled>0`. `batch_admitted` holds track live batch work against
the 18,951 baseline (2026-11-17T22:25Z), with `READBACK:` on SCA-2789
quoting both numbers.

## Verification

- `python3 -m py_compile` on the three changed files.
- Backend unit shards run in CI; this host has no backend venv (SCA-2134
places heavy checks on m1/multica-worker-1).
- New tests: a full ledger of live entries refuses with `live=MAX_OPEN,
settled=0` and logs `outcome=bound_exceeded`; the worker logs
`outcome=reservation_full` and no `outcome=deferred`; the existing
full-ledger recovery test still passes.

Failure-Class: FC-cap-bound-pins-owner-without-release-path


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/25745?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<a
href="https://www.cubic.dev/action/auto-fix/pr/BasedHardware/omi/25745?returnTo=https%3A%2F%2Fgithub.com%2FBasedHardware%2Fomi%2Fpull%2F25745&source=description"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img
alt="Turn on auto-fix"
src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant