Skip to content

fix(session): remap compaction tail_start_id when forking#24898

Merged
rekram1-node merged 3 commits intoanomalyco:devfrom
spark4862:fix/session-fork-remap-compaction-tail
Apr 29, 2026
Merged

fix(session): remap compaction tail_start_id when forking#24898
rekram1-node merged 3 commits intoanomalyco:devfrom
spark4862:fix/session-fork-remap-compaction-tail

Conversation

@spark4862
Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #24700

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Fixes Session.fork not remapping CompactionPart.tail_start_id, which caused forked sessions to include all pre-compaction messages in context and inflate session length.

Problem

Session.fork copies messages and remaps each message’s id and assistant parentID via idMap, but tail_start_id on compaction parts was left as the parent session’s message id.

filterCompacted (message-v2.ts) uses tail_start_id as the retention boundary: it walks older messages until it finds one whose id equals tail_start_id. In a fork, every message id is new, so the old tail_start_id never matches; the loop does not stop where compaction intended, and pre-compaction history leaks into the prompt (observed as much larger context / context_length_exceeded on the next request).

Regression

Introduced when tail_start_id and the corresponding filterCompacted behavior were added in commit 6f5a3d30f (“keep recent turns during session compaction”, 2026-04-10). Session.fork was not updated to remap that field alongside parentID.

Fix

When cloning parts, after ...part, override tail_start_id for compaction parts using the same idMap:

...(part.type === "compaction" && part.tail_start_id
  ? { tail_start_id: idMap.get(part.tail_start_id) }
  : {}),

If the tail message was not copied (e.g. partial fork), idMap.get returns undefined, clearing the stale reference instead of pointing at a non-existent id.

How did you verify your code works?

Screenshots / recordings

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@rekram1-node rekram1-node force-pushed the fix/session-fork-remap-compaction-tail branch from b54f298 to a40622f Compare April 29, 2026 05:10
@rekram1-node
Copy link
Copy Markdown
Collaborator

/review

@github-actions
Copy link
Copy Markdown
Contributor

lgtm

@rekram1-node rekram1-node merged commit d71b827 into anomalyco:dev Apr 29, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session.fork does not remap CompactionPart.tail_start_id, breaking filterCompacted and inflating forked context

2 participants