Skip to content

Conversation

@ryanwyler
Copy link
Contributor

@ryanwyler ryanwyler commented Dec 30, 2025

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.

…arent-child message references

When forking a session, message IDs are regenerated but parentID references in assistant messages were not updated to point to the new IDs. This broke features that rely on parent-child relationships, such as compaction breakpoints.

## Problem

Forking a compacted session would fail with:
```
prompt is too long: 203573 tokens > 200000 maximum
```

The forked session didn't honor the compaction breakpoint because the parent-child message relationships were broken.

## Fix

Added an ID mapping that tracks old ID -> new ID, then updates parentID references when cloning assistant messages.

## Changes

- packages/opencode/src/session/index.ts - Add ID mapping in fork function to preserve parent-child relationships
@ryanwyler ryanwyler force-pushed the fix/fork-parent-id-mapping branch from a0121b1 to 88060ae Compare December 31, 2025 10:57
ryanwyler added a commit to gignit/opencode that referenced this pull request Jan 2, 2026
…older messages

Adds a new "collapse" compaction mode that preserves recent context while
summarizing older messages, with intelligent merging of historical summaries
across multiple compaction cycles.

The problem: Standard compaction summarizes the entire conversation, reducing
visible context to just 1 message - a brief summary. The agent loses access to
working code, user preferences, design decisions - essentially starting blind
after each compaction. When multiple compactions occur, each new summary only
knows about messages since the last compaction, causing progressive amnesia.

The solution: Collapse compaction keeps the agent intelligent:

1. Selective compression - Only summarizes OLD messages while keeping recent
   work fully visible:
   - Extracts oldest 65% of tokens and creates a rich summary at a breakpoint
   - Uses newest 15% as reference context for the summarizing LLM
   - Everything after the breakpoint (35%) remains as full messages
   - Result: 168 visible messages vs 1 with standard compaction

2. Rich context restoration document - The summary preserves:
   - Current task state, next steps, blockers
   - Working code blocks verbatim
   - User directives and preferences
   - Design decisions and lessons learned
   - Technical facts (paths, function names, config values)

3. Rolling knowledge accumulation - When creating a new summary, includes up to
   N previous summaries in the prompt with instructions to MERGE all historical
   context. Each compaction consolidates everything that came before - nothing
   is ever lost. A session can run indefinitely with unlimited compactions and
   the agent retains access to critical knowledge from day one.

4. Chain protection - Ensures compaction breakpoints don't split message chains
   (user message + assistant responses linked by parentID).

Configuration:
  compaction.method: "collapse" (default: "standard")
  compaction.trigger: 0.85
  compaction.extractRatio: 0.65
  compaction.recentRatio: 0.15
  compaction.summaryMaxTokens: 10000
  compaction.previousSummaries: 3

Changes:
- src/session/compaction.ts - Main collapse logic, processCollapse(), helpers
- src/session/index.ts - Fork fix: preserve parentID references when forking
- src/id/id.ts - Add insert() for positioning messages between existing IDs
- src/session/message-v2.ts - filterCompacted() breakpoint detection
- src/session/prompt.ts - Integrate collapse breakpoint into prompt building
- src/config/config.ts - Compaction config schema
- src/cli/cmd/tui/routes/session/index.tsx - TUI toggle
- src/cli/cmd/tui/routes/session/sidebar.tsx - Sidebar display
- src/server/server.ts - Unified compaction flow
- test/id-chain-fix.test.ts - Tests for ID insertion
- SDK types regenerated

Includes fork fix from anomalyco#6445: preserve parentID references when forking
sessions to prevent broken compaction detection.
@ryanwyler
Copy link
Contributor Author

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

@rekram1-node
Copy link
Collaborator

/review

@github-actions
Copy link
Contributor

github-actions bot commented Jan 9, 2026

lgtm

@rekram1-node rekram1-node merged commit a5edf3a into anomalyco:dev Jan 9, 2026
2 checks passed
shuv1337 added a commit to Latitudes-Dev/shuvcode that referenced this pull request Jan 9, 2026
Cherry-picked from upstream a5edf3a.
Fixes missing parent-child message references when cloning sessions.
aryasaatvik pushed a commit to aryasaatvik/opencode that referenced this pull request Jan 10, 2026
fwang pushed a commit to lifefloating/opencode that referenced this pull request Jan 10, 2026
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.

2 participants