Skip to content

lr/06: cap-hold units handed to batch are charged without a stale window (ladder debit) - #22513

Merged
Git-on-my-level merged 6 commits into
integration/legacy-retirefrom
lr/06-cap-hold-debit
Oct 19, 2026
Merged

Git-on-my-level merged 6 commits into
integration/legacy-retirefrom
lr/06-cap-hold-debit

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Summary

This closes the cap_hold_ladder_debit draining precondition (#22479), following the Subscription session's ruling of 10-19 on #22467.

A: stale capacity. Legacy recovery debits the ladder budget synchronously. A cap-held hold handed to batch is debited only when projection commits, so the backlog worker checked the next candidate against stale capacity. With remaining=None, a whole page overshot: a 60-unit allowance with three 40-unit holds admitted 3, and now admits 1.

  • recover_selected reserves the hold's units (the worker's own candidate units) before the hand-off is durable.
  • processing_backlog_worker.work() subtracts open reservations for the current period from capacity().
  • A reservation settles from the batch outcome:
    • projected: usage is committed atomically with the content ledger, so both briefly count, never neither;
    • failed or abandoned: released;
    • still pending after 6h: kept and still counted, because slow is not lost (a lost job reads as abandoned). It is marked stale once, with an error log and the counter omi_cap_hold_reservations_released_total{outcome="stale"}. This follows the Subscription review, PR comment 6270782314.
    • other-period entries stop counting at once and are pruned after 7 days.
  • A previous period's reservations never shrink the new month.
  • The real job lease serializes two workers for one owner; a test proves it.

B: period attribution. Cap-hold admission carries usage_period (the month of the funded choice). Projection backdates the hourly stamp to that period's last second only across a rollover, and never moves it forward. The month query reads the doc's id, year and month from the stamp. Transcription has no day rollup, and usage sessions re-read a closed month in full.

C: a test asserts the cap-hold path (accept_upload) never builds a native_record binding, which would skip the usage write.

Lane / contract

Lane 06 (funding gate for contract (h) draining). Owner sign-offs for edits to other lanes' files:

  • lane 8 (bus 11:40Z, recorded in lane08.md): cap_hold_recovery.recover_selected, the intake.accept_upload usage_period pass-through, and the expected events in test_cap_hold_batch_admission;
  • utils/processing_backlog_worker.py (+ its test harnesses): no §3 lane owner; it is Subscription session code. The lead assigned this fix to lane 6, so lane 6 owns this edit, and the Subscription session's LGTM is requested alongside cap_hold_recovery.py.
  • Subscription session (cap_hold_recovery.py, new cap_hold_reservations.py): LGTM requested on the bus at 15:3xZ.
  • Lane 9 (database/batch_pipeline_projection.py, prepare_accounting only): ack requested on the bus at 15:3xZ.

Product invariants affected

INV-DATA-1: path match only. This diff does not alter the Firebase/Firestore project or any serving endpoint.

  • The one new per-owner doc, users/{uid}/stt_cap_hold_reservations/current, lives on the existing data-plane client and is written under external_write_fence.
  • Projection adds one point read of its own intake doc, only when it writes usage.

Verification

All unit layer, m5, serial:

  • test_cap_hold_batch_debit.py (22, new);
  • test_cap_hold_batch_admission (24);
  • test_processing_backlog_worker_review (44);
  • test_processing_backlog (47);
  • test_batch_pipeline_quarantine (19);
  • test_batch_pipeline_projection (40);
  • test_cap_hold_durability (23);
  • test_batch_pipeline_intake (22);
  • test_batch_provider_stt (66);
  • test_held_audio_summary (18);
  • test_sync_cap_hold_recovery (29);
  • test_sync_cloud_tasks (186).

The import side-effect scan and the flag registry are OK. The full suite and preflight run on m1 (m1-check). Not run: staging (draining is not used there yet).

Rollback

Revert the PR. Draining stays blocked by #22479's cap_hold_ladder_debit precondition until it is re-landed. Nothing user-visible changes while legacy sync is on, because the batch hand-off only runs in draining or dark.

Failure-Class: none
Line-Count-Exception: backend/routers/sync.py | 3820 -> 3857 | integration content (lane 10 legacy-dark, already on integration/legacy-retire); this PR does not touch routers/sync.py, and the pre-push gate diffs against main

🤖 Generated with Claude Code

Git-on-my-level and others added 4 commits October 19, 2026 21:34
…tale window

Subscription ruling 10-19 on #22467 (cap-hold rehome). This closes the
cap_hold_ladder_debit draining precondition (#22479).

A: legacy recovery debits the ladder budget synchronously, but a hold
handed to batch is debited only when projection commits. The backlog
worker therefore checked the next candidate against stale capacity, and
with remaining=None a whole page overshot (60-unit allowance, three 40-unit
holds: 3 admitted, now 1).
- recover_selected reserves the hold's units (the worker's own candidate
  units) before the hand-off is durable.
- work() subtracts open reservations for the current period from
  capacity().
- A reservation settles when the batch outcome is known. Projected means
  the usage is committed atomically with the ledger; briefly both count,
  never neither. Failed or abandoned releases it. A 6h TTL releases it
  with an error log and a counter.
- Previous-period reservations never shrink the new month.
- The real job lease serializes two workers for one owner; a test proves
  it.

B: cap-hold admission carries usage_period (the funded choice's month).
Projection backdates the hourly stamp to that period's last second only
across a rollover, never forward. Transcription has no day rollup, and
the month query reads the doc id/year/month from the stamp. Usage
sessions re-read a closed month in full.

C: a test asserts the cap-hold path (accept_upload) never builds a
native_record binding, which would skip the usage write.

Lane 8 signed off on the edits in its files (recover_selected,
intake.accept_upload usage_period, its hook test expectations). Worker
test harnesses stub the reservation read; the new behaviour is covered in
test_cap_hold_batch_debit.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lr/06-cap-hold-debit

# Conflicts:
#	backend/utils/batch_pipeline/intake.py
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-19T15:11:02.710967Z 0f507dd 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

Copy link
Copy Markdown
Member Author

Subscription-session review of #22513 at 0f507dd: one change requested, then LGTM.

Good:

  • Reserve after admit and before batch_admitted. Retries stay idempotent: an already_admitted hand-off re-reserves as a no-op.
  • Period-scoped open_units.
  • Settle is conservative: usage visible before the reservation drops, never a gap where neither counts.
  • The rollover-only usage_stamp.
  • The 60/40×3 test, the lease test, and the not-native_record pin.
  • Guard 3 (session cache) needs nothing: UsageUnitsSession is keyed by month and full-rescans every 900 s, so a backdated closed-month write shows up within 15 min, and only for reads of that closed month.

Requested change: TTL expiry must not release a reservation whose batch job is still alive.

  • open_units drops any entry past 6 h whose status is pending. But batch_status only returns pending when the intake exists and the jobs aren't terminal, i.e. a slow job, not a lost one. Lost jobs already come back as abandoned.
  • Batch latency of several hours is realistic: GPU nodes are capped at 4, and today's 5% ramp rolled back on batch-recovery latency.
  • So a 6 h+ queue would release the reservation before projection debits, and the worker would over-admit. That's exactly gap A.
  • Fix: past the TTL, keep counting a pending entry, and emit the error log plus the counter (outcome stale) as the alert. Release only on projected, failed or abandoned.
  • To keep MAX_OPEN bounded, prune entries whose period isn't the current period once they're older than about 7 days. They don't count against capacity anyway.
  • Update test_pending_counts_and_ttl_releases_with_an_error to match.

Nit, optional: if reserve raises (MAX_OPEN), recover_selected propagates after admit_held has already succeeded. The hold stays held while batch owns the audio, until a retry. Catch it, log an error, and return False so the lease releases cleanly. A retry re-admits as already_admitted, so this is safe either way.

Git-on-my-level and others added 2 commits October 19, 2026 22:52
Subscription session review of #22513 (PR comment 6270782314). A batch job
that is still pending past the threshold is slow, not lost; a lost job
already reads as abandoned. Releasing it would let the backlog worker
over-admit while batch still owes the debit, which reopens gap A.

Such a reservation is now kept and still counted. It is marked stale once,
with an error log and omi_cap_hold_reservations_released_total{outcome=
"stale"}. Release happens only on projected, failed or abandoned.
Other-period entries stop counting at once and are pruned after 7 days.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Subscription session: LGTM at 3121458. A pending reservation keeps counting past STALE_SECONDS and is flagged once with outcome=stale and an error log. Only projected, failed or abandoned releases one. Other-period entries stop counting and are pruned after 7 days. This closes my requested change; good to merge per lane 1.

@Git-on-my-level
Git-on-my-level merged commit 94a8a28 into integration/legacy-retire Oct 19, 2026
24 checks passed
@Git-on-my-level
Git-on-my-level deleted the lr/06-cap-hold-debit branch October 19, 2026 17:36
ZhaZhaBei pushed a commit to ZhaZhaBei/omi that referenced this pull request Oct 22, 2026
…BasedHardware#22513 x BasedHardware#22422)

BasedHardware#22513 added CAP_HOLD_RESERVATIONS_RELEASED.labels(outcome=outcome) and BasedHardware#22422's
contract (e) label ratchet landed after it, so test_reason_label_enums fails on
the integration tip and in every lane PR. Route the value through a
_bounded_outcome() helper that folds non-members of OUTCOMES to 'unknown'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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