Repository navigation
fix(python): keep the facts a partly failed batch did store (#753) - #754
Merged
Merged
Conversation
When one group of a remember_batch call was stored and another failed for a reason other than the write quota, the call raised a plain RuntimeError that did not say which facts landed. Auto-extraction then logged stored facts as not stored and skipped the tombstone of a stored UPDATE (both versions stayed in the vault); the #747 retry-queue drain re-queued stored facts, so the next drain could write them twice; and recrystallize lost the ids of a session's landed facts, so a resume re-wrote them. - client: remember_batch raises BatchStoreError (a RuntimeError) that carries batch_results, one id or None per input fact, like BatchIds and RelayWriteQuotaExceeded. New per_fact_results(outcome, n) reads that outcome from a return value or an error; lifecycle's #751 helper folds into it. - lifecycle: auto-extraction records stored facts, tombstones a stored UPDATE's old fact, reports only the failed facts, and counts a partial success as a write (so queued facts get their retry). The drain re-queues only the facts that failed. - recrystallize: SessionCheckpoint.new_fact_ids_by_old_id records landed facts after every batch, including a batch that then raised, so a resume writes only the missing facts, even after a hard kill between batches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #753.
Problem
In a
remember_batchcall, one group can be stored while another fails for a reason other than the write quota. The call then raised a plainRuntimeErrorthat did not say which facts landed. #747 had already fixed this for quota refusals, but not for any other error. Each caller treated the whole batch as failed:UPDATEwhose replacement was stored did not tombstone its old fact, so both versions stayed in the vault.attempts + 1. If the subgraph had not indexed them yet, the next drain could write them a second time.facts_writtenis only set once all its batches land. A failure inside one batch or in a later batch lost the ids of the facts that had landed, so a resume wrote them again. A quota pause in the middle of a session had the same gap.The import engine is not affected. It calls
store_fact_batchdirectly.Changes
client.pyremember_batchnow raisesBatchStoreError, aRuntimeErrorsubclass, so existingexcept RuntimeError/except Exceptionhandlers keep working. It carriesbatch_results, the same per-fact shape asBatchIds.batch_resultsandRelayWriteQuotaExceeded.batch_results.per_fact_results(outcome, n). It reads the per-fact outcome from a return value or from a raised error, in one place.lifecycle._per_fact_idsfrom Python: a skipped batch group shifts fact ids onto the wrong facts (auto-extraction can tombstone a fact whose replacement was not stored) #751 is folded into it.agent/lifecycle.pyUPDATEis tombstoned;wrote_ok, so facts queued by an earlier refusal get their retry.crystals/recrystallize.py: newSessionCheckpoint.new_fact_ids_by_old_id(old fact id → new fact id)._rewrite_session_factsskips facts already in it.loadalready ignores unknown keys and fills defaults.client-consistency.md: the "per-fact outcomes on a batch" rule now also covers non-quota partial failures and skipped groups.Testing
tests/test_batch_id_alignment_751.py, run against the realremember_batchover a patchedstore_fact_batch: the per-fact results on a partial and on a total failure, auto-extraction, and the drain.tests/test_recrystallize.py:BatchStoreError;forgetnever awaited; drainsaved == 0; nothing on disk before the second batch.wrote_okon a partial success, and the drain's use of the per-fact results.api-staging.totalreclaw.xyz. It answers a 2-fact op with-32500, which the UserOp layer retries 3 times before the batch writer halves the group. The first single op goes to staging; the second gets a-32603error, which is not retried.UPDATE old → newplus anADD:ADDis not on staging and is the only fact logged as not stored.attempts == 1, and the stored one is indexed.🤖 Generated with Claude Code