Skip to content

fix(backend): release a daily-sweep model fence whose claim provably never dispatched - #14338

Merged
Git-on-my-level merged 3 commits into
mainfrom
fix/sweep-predispatch-fence-release
Sep 17, 2026
Merged

Git-on-my-level merged 3 commits into
mainfrom
fix/sweep-predispatch-fence-release

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Summary

The daily memory sweep claims its at-most-once model-invocation fence before it builds the request. Any failure after the claim tombstoned the fence (pending / indeterminate, "closed forever unless an operator repair receipt reopens one attempt"), including failures that happened before any provider request could have been sent. Measured on the isolated JIT QA plane on 2026-09-17:

  1. A sweep claimed the fence, then the summary agent raised its pre-dispatch input-budget check. No llm_gateway_attempts row exists for that claim and no request reached the QA gateway.
  2. After the budget fix (fix(backend): make daily sweep input-budget failures diagnosable and degrade instead of stalling #14320) deployed, the next run reported source_incomplete:2026-09-10 without the agent ever running: the claim transaction returned blocked on the old tombstone, and every later day queues behind it.
  3. The sanctioned repair refused: QA sweep repair could not join the tombstoned claim to a sweep run id in gateway accounting — it required an accounting row carrying a jit_run_id, which a claim that never dispatched cannot have.

So the failures that provably spent nothing were the only ones that could never be repaired. In production the same shape means any failure between claim and first provider request (prompt build, input budget, request serialization, client construction) permanently stops that user's sweep, with no operator path at all, since the repair tool is QA-only.

Design

Failure Before After
Certified before the first dispatch, first three times tombstoned pre_dispatch_released; the next run may claim again, counter and content-free reason recorded
Fourth certified pre-dispatch failure tombstoned pre_dispatch_exhausted, blocked_reason=pre_dispatch_release_limit (visible, repairable)
At or after dispatch, parse failure, timeout, unknown origin indeterminate unchanged: indeterminate, blocked
  • Certification cannot be forged by accident. models/daily_sweep_dispatch.py opens a claim-local scope around the builder. The summary agent sets an irreversible latch immediately before it records request evidence and enters the provider call. A failure is pre-dispatch only if it is the exact exception object the instrumented agent issued inside the current claim's scope while the latch is still clear. Constructing the exception type, clearing the mutable QA evidence list, or replaying an earlier certified exception after dispatch all stay indeterminate. If the scope is not visible (another thread, another caller), nothing is certified — the default is today's behaviour.
  • Release is transactional and claim-bound. Every claim carries a claim_id. Release, finalize, and indeterminate writes require state pending, matching identity, matching claim_id, and open deletion/generation fences, with all reads before writes. A deleted or foreign user payload is never recreated. Overlapping runners still dispatch at most once.
  • Repair never infers absence. The first version of this PR let the operator repair a claim when the accounting read found no memories attempt. An independent review showed that is unsafe: llm_gateway_attempts is a best-effort, asynchronous ledger written after the provider call (queue overflow, a dead process, or a failed write all leave no row), so a request the provider consumed could be "proven" absent and dispatched again. The gateway's durable JIT reservation is written before dispatch, but only for requests carrying the QA JIT contract headers, production sweeps do not send them, and legacy fences record no run or request binding to check it against. So absence — of accounting rows or of reservations — is never repair authority. The narrower shape is operator_attested_no_dispatch: the exact assertion ATTEST_NO_PROVIDER_DISPATCH_AND_WORKER_TERMINATED, the operator's identity, a content-free reference to independently reviewed evidence, the full claim identity, claim token, claim time and attestation time, an expired lease plus margin, transactional issuance, and single-use consumption. It validates attribution and claim binding, not the truth of the human assertion, and nothing is ever labelled "no spend". Receipts of the withdrawn no_recorded_attempt shape are rejected. The tool remains QA-plane-only.
  • Accounting read fixed. The evidence read took the first 50 rows of the whole day across all users and features and filtered client-side, so on a busy day it could miss a recorded attempt. It now pages to exhaustion and refuses when the read may be incomplete. This strengthens the recorded-attempt repair; it is not used to prove absence.

Review

Designed and implemented with an independent model (Codex), reviewed line by line, then attacked by a second independent read-only review. That review found the release half sound (no path where a request could have been sent and the failure still certifies; the scope is visible on the production call path; reads precede writes; the release budget cannot be reset) and rejected the original absence-based repair, which is why it was redesigned as above. It also asked for two missing regressions — a claim whose fence token changed before finalize, and the same for the generation-close path — both added, and both verified to fail when the corresponding check is removed. Line-by-line points: the latch placement (before evidence recording and model.invoke, after every local preparation step), the claim-bound transactions, legacy tombstones without a claim_id (repairable: the receipt's prior_claim_id and the fence's absent claim_id compare equal), and the in-thread call path from the completed-day builder to the agent.

Failure-Class: FC-daily-memory-sweep-fence

Product invariants affected

  • INV-MEM-1
  • INV-MEM-4

Cited because the change edits files those invariants lock. Candidate admission, authority ranking, memory writes, and the cursor are untouched; the change is confined to the model-invocation fence's failure transitions and the QA repair operator.

Line-Count-Exception: backend/utils/memory/daily_memory_sweep.py | 5814 -> 5984 | claim-bound release/exhaust transitions and validated operator-attestation repair on the existing fence

Evidence

Hermetic local runs (Google credentials blanked), Python 3.11:

test_daily_memory_sweep.py, test_daily_sweep_summary_agent.py, test_jit_qa_sweep_repair.py,
test_jit_qa_sweep_operator.py, test_daily_memory_sweep_inventory.py, test_daily_memory_sweep_job.py,
test_jit_qa_manual_operator.py, test_jit_qa_manual_operator_workflow.py        243 passed
backend/scripts/typecheck.sh                                                   0 errors
Firestore emulator proof (daily_memory_sweep_emulator_test.py)                 PASS
  crash / deletion / generation / paid-wipe / pre-dispatch-release-contention / accounting-pagination / lost-accounting-refusal

New tests cover: release with reason and re-claim on the next run; the fourth failure exhausting the budget; release refused when the fence changed (state, identity, claim id); a forged pre-dispatch exception staying indeterminate; a post-dispatch failure staying blocked; an operator-attested repair reopening exactly once and consuming its receipt; refusal while the lease is unexpired; refusal of absence-based receipts, including the dropped-accounting and late-insertion cases; a stale claim unable to finalize or close over a newer claim; the accounting read pinned with more than 50 foreign rows ahead of the matching row.

Layer measured: unit tests, typecheck, and the Firestore emulator. Not yet observed: a successful sweep-verify on the QA plane. The two wedged QA claims are legacy fences; one of them did reach the provider and is only repairable through the recorded-attempt path, the other needs an operator attestation backed by independent evidence (the QA gateway's Cloud Run request log for the claim window and the terminated job execution).

🤖 Generated with Claude Code

Git-on-my-level and others added 2 commits September 17, 2026 17:30
…never dispatched

The sweep claims its at-most-once fence before building the request, and any
later failure tombstoned it forever, including failures before a provider
request could have been sent. Those spend-free failures were also the only ones
the repair operator could not repair, because it required a gateway accounting
row. Certified pre-dispatch failures now release the exact pending claim (three
times, then a named exhausted state); anything at or after dispatch stays
indeterminate. Repair accepts an explicit, lease-expired, fully-read
no_recorded_attempt proof, and the accounting read pages to exhaustion.

Failure-Class: FC-daily-memory-sweep-fence

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ery states live in the runbook

Failure-Class: FC-daily-memory-sweep-fence

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mintlify

mintlify Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
omi 🟢 Ready View Preview Sep 17, 2026, 10:56 AM

💡 Tip: Enable Automations to automatically generate PRs for you.

…claim did not dispatch

Review follow-up. llm_gateway_attempts is a best-effort ledger written after
the provider call, and the durable JIT reservation exists only on the QA
contract path, so neither absence can authorize reopening an invocation. Replace
the no_recorded_attempt repair with an explicit, attributed, claim-bound,
single-use operator attestation; reject the withdrawn receipt shape; add the
missing stale-claim regressions for finalize and generation close.

Failure-Class: FC-daily-memory-sweep-fence

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@Git-on-my-level
Git-on-my-level marked this pull request as ready for review September 17, 2026 14:51
@Git-on-my-level
Git-on-my-level merged commit 8ea5779 into main Sep 17, 2026
59 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/sweep-predispatch-fence-release branch September 17, 2026 14:51

This branch was successfully deployed

1 active deployment
staging - docs — 1b97ed2d Deployed Sep 17, 2026 by mintlify[bot]
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