fix(claude-code): segment retains from PreCompact#2355
Open
Sanderhoff-alt wants to merge 1 commit into
Open
Conversation
Claude Code transcripts are append-only across compaction. Add the PreCompact hook so full-session retain writes the pre-compact transcript before recording a boundary for the next compact segment. Remove shrink-based compaction detection from retention state. The next full-session retain now writes appended compact summary, retained tail, and new turns into a session_id-cN document after the checkpoint. Make run_retain return a success flag and only mark the PreCompact checkpoint after the retain API call succeeds. This avoids losing the pre-compact window when the daemon or retain request fails.
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.
Why
The Claude Code integration retains from the on-disk transcript JSONL file provided by the hook input as transcript_path. On current Claude Code builds we verified, that file is append-only across compaction: compact does not truncate the file or delete old transcript entries. Instead, Claude Code appends compact metadata, a compact summary, retained tail entries, and later conversation turns to the same JSONL transcript.
The previous full-session retain logic inferred compaction from a transcript size decrease. That does not match the observed Claude Code transcript behavior, so it is not a reliable way to segment retained documents. With an append-only transcript, shrink-based detection never represents the real compact boundary and can leave pre- and post-compact material mixed under the same document lifecycle.
This change wires the Claude Code PreCompact hook into the integration. Before Claude Code compacts, the hook forces one retain of the current pre-compact transcript. If that retain succeeds, it records the current transcript message count as a checkpoint. The next full-session retain reads the same append-only transcript and writes only the appended compact summary, retained tail, and new turns to a fresh session_id-cN document, preserving the pre-compact document instead of overwriting it.
The checkpoint is only marked after the retain API call succeeds. If the daemon, API URL resolution, or retain request fails, the hook does not advance the boundary. The next normal retain will continue using the original session document and full transcript, which may cost more extraction work but avoids dropping the pre-compact window.
Chunked retain mode continues to use its existing timestamped document behavior and does not create full-session compact checkpoints.
What changed