fix(compress): count only episodes actually deleted, not candidates - #52
Merged
Merged
Conversation
Engram.compress() incremented episodes_removed once per candidate in each batch regardless of whether store.delete_episode() succeeded, so CompressionRun.episodes_removed overstated when a candidate was gone by the time compress() tried to delete it (for example a forget() racing compress() between candidate selection and deletion, where delete_episode returns False because the row is already gone). The memory_forgotten events, built from the ids actually deleted, stayed correct; the count a caller or a downstream governance service reads did not. Move the increment inside the existing "if delete_episode(...)" branch so removed == len(deleted_ids) always. Red first: tests/test_compress.py::test_compress_removed_count_excludes_already_deleted_candidates simulates the race by wrapping store.delete_episode so it deletes the row and reports False for one candidate, then asserts episodes_removed equals the number of memory_forgotten events. Against the unfixed code this failed with episodes_removed=5 against 4 emitted events; it is green after the fix. Adds invariant 10 to CLAUDE.md with its test marker. README's gated test-coverage heading moves 519/526 to 520/527 for the new test (scripts/readme-numbers.sh, invariant 2). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…the events Co-Authored-By: Claude Opus 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.
What
Engram.compress()incrementedCompressionRun.episodes_removedonce percandidate episode in a batch regardless of whether
store.delete_episode()actually deleted it. When a candidate was already gone by the time
compress()tried to delete it (for example aforget()racingcompress()between candidate selection and the delete call, where
delete_episodereturns
False), the count still went up. Thememory_forgottenevents,built from the ids actually deleted, stayed correct; the number a caller or a
downstream governance service reads did not.
Fix: move
removed += 1inside the existingif self._store.delete_episode(ep.id):branch, so
removed == len(deleted_ids)always.Report
🤖 Generated with Claude Code