opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: resolve broken forked sessions with compactions due to missing parent-child message references

Open ryanwyler opened this issue 1 month ago • 1 comments

Summary

Forking a compacted session fails with:

prompt is too long: 203573 tokens > 200000 maximum

The bug was introduced in commit 9a0735de7 on October 6, 2025 with the commit message "Add session forking functionality and simplify remove logic".

This was a bug from day one of the fork feature - it just wasn't noticed until compaction logic (which relies on parent-child relationships) was added later.

The Bug

When forking was originally implemented, each message gets a new id via Identifier.ascending("message"), but the code never updated the parentID field. Assistant messages have a parentID that points to their corresponding user message. After forking, those parentID values still reference the old message IDs from the source session, which don't exist in the forked session.

Fix

Maintain an ID mapping (old -> new) when cloning messages, updating parentID references to point to the new IDs. This restores the parent-child relationships that filterCompacted() relies on to detect compaction breakpoints.

ryanwyler avatar Dec 30 '25 14:12 ryanwyler

After forking a session that has been compacted the user many times will get this error. This PR resolves this so it fixes this by maintaining the proper breakpoints during a fork.

image

ryanwyler avatar Jan 06 '26 16:01 ryanwyler

/review

rekram1-node avatar Jan 09 '26 17:01 rekram1-node

lgtm

github-actions[bot] avatar Jan 09 '26 17:01 github-actions[bot]