Repository navigation
fix(hook): start generation history at the last compact boundary - #111
Open
lautaro-iridia wants to merge 1 commit into
Open
lautaro-iridia wants to merge 1 commit into
lautaro-iridia wants to merge 1 commit into
Conversation
After a /compact (manual or automatic) the model continues from the compact summary plus the preserved segment; earlier messages are no longer in its context window. build_session_history rebuilt every generation input from the whole transcript, so turns after a compaction showed messages the model never saw and the input kept growing with the session. Split the transcript at each compact_boundary row. A turn's prefix now starts at its segment: the summary, the messages in compactMetadata.preservedSegment, then the turns of that segment. Transcripts without compactions are unchanged. 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.
Problem
After a
/compact(manual or automatic), Claude Code continues from the compact summary plus the preserved segment. Messages before thecompact_boundaryare no longer in the model's context window.build_session_historyrebuilds every generation input from the whole transcript, starting at byte 0. Two things go wrong:Fix
Split the transcript at each
compact_boundaryrow (type: system,subtype: compact_boundary). Each segment is one context window. A turn's prefix starts at its segment and contains:isCompactSummaryuser row);compactMetadata.preservedSegment(headUuidtotailUuid), when present;When work resumed right after an automatic compaction, the summary row opens the first turn of the segment. That case is handled, so the summary is not dropped.
SessionHistorygets asegment_start_index_by_user_row_uuidmap, andprefix_for_turnslices from there. The default is 0, so transcripts without compactions produce exactly the same inputs as before.Testing
tests/integration/test_compacted_session_history.py, with 4 cases: a manual compact between turns, the preserved segment, work resumed after an auto compact, and two compactions in one session. All 4 fail onmainand pass with this change.test_state_lock_contention.py::test_entry_of_another_session_survives_the_emit, also fails onmain(expired fixed timestamp, as noted in fix: ignore internal API request snapshots in turn assembly #107 and fix(hook): ignore typed non-chat rows that carry a chat role #108). Same result on Python 3.10.ruff check --select F,E9is clean, and the empty-payload smoke run exits 0.Related: #110 / #108 (
api-request-blobrows). That is independent of this change; for the real-transcript comparison those rows were filtered on both sides.