🪲 BUG-#19: Persist only new messages instead of rewriting the whole session - #54
Merged
Conversation
Storage backends need to tell an append-only save from one where existing history was replaced or reordered. replace_messages(), trim() (when it actually truncates), and _repair_dangling_tool_calls() (when it actually inserts a repair message) now set session.dirty, giving SqliteSessions a signal to fall back to a full rewrite instead of assuming everything before the last known count is unchanged.
…ession post() deleted and reinserted every MessageRecord on every call, even though Agent's on_message fires after each message including every tool result — a 20-tool-call turn on a 200-message session did 20 × (200 DELETE + 200 INSERT) writes. Now inserts only the messages appended since the last save (tracked via record.message_count), falling back to the previous full delete-and-reinsert when the session is new, a different Session object was posted for the same key (checked via identity — a count watermark alone can't tell an overwrite from an append), session.dirty is set, or the message count shrank.
FernandoCelmer
commented
Aug 15, 2026
FernandoCelmer
left a comment
Member
Author
There was a problem hiding this comment.
🔍 Code Review
Code issues found: 1
| # | Severity | Comment |
|---|---|---|
| 1 | [Suggestion] | session.dirty read without _lock in post() |
post() read session.dirty and session.messages unlocked while _repair_dangling_tool_calls() sets dirty under session._lock, letting a concurrent repair's synthetic messages be dropped silently. Snapshot both fields together under the lock before touching the DB.
This was referenced Aug 16, 2026
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.
Description
pycodeloop/core/session.py— Adds adirty: boolfield (defaultFalse) toSession.replace_messages(),trim()(when it actually truncates), and_repair_dangling_tool_calls()(when it inserts synthetic results) now setdirty = Trueto signal that history was mutated rather than appended.pycodeloop/store/sqlite_sessions.py—post()now appends only the messages added since the last save (tracked byrecord.message_count), falling back to a full delete-and-reinsert when the session is new, a different object was posted for the same key,session.dirtyis set, or the message count shrank. Adds_session_identitydict to detect object identity changes across calls. Clearssession.dirtyand records the identity after a successful commit.tests/core/test_session.py— NewTestSessionDirtyFlagsuite covering all dirty-marking paths and their no-op counterparts.tests/store/test_sqlite_sessions.py— New integration tests spy onQuery.deleteto prove the incremental path skips the rewrite, and that dirty/different-object/new-session cases fall back correctly.Motivation and Context
SqliteSessions.post()deleted and reinserted everyMessageRecordon every call, even thoughAgent.on_messagefires after each message including every tool result. For a 20-tool-call turn on a 200-message session that's 20 × (200 DELETE + 200 INSERT) = 8,000 row writes. Closes #19.Types of changes
Checklist