fix(storage): prune redundant intermediate message_update frames - #1279
Merged
Conversation
Co-Authored-By: Claude Code <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 #1278.
What
After a
message_endis durably appended, prune the intermediatemessage_updaterows of that message's contiguous update run, keeping only the first and last. This materializes at write-time exactly what the read path already discards.Why
pi streams an assistant message as many
message_updateevents and every frame is persisted carrying the full accumulated message. The read path (readEventsAfterForReplay/ clientcompactReplayEvents) already keeps only the first+last of each contiguous run,message_endcarries the full text, and crash/resume never readsmessage_update(it reconstructs from the entry tree + pi transcript). So intermediate frames are persisted but never consumed — pure write amplification (measured ~74% of rows / ~89% of bytes in one deployment).How
PgAgentSessionStore.pruneMessageUpdates(sessionId, messageEndSeq): walks backward from the persistedmessage_endvia the(session_id, seq)primary-key index, bounds the contiguousmessage_updaterun immediately preceding it, and deletes only rows strictly between the run's first and last frames. O(run length), not O(session); never touches rows at/aftermessageEndSeq(in-flight next message).message_endis durably appended, ordered in the write chain after that append, off the live-stream path; prune failures are logged and do not fail the run.Safety
message_update).Tests
message_endseq and correct ordering.Validation
tsc --noEmit, storage schema suites (66 passed / 37 env-gated skipped), runner suites (9 passed), ESLint, repo-widepnpm check.