Skip to content

[Bug] Forking a compacted session re-injects discarded history and overflows the context window #334

Description

@rankaiyx

Title: Forking a compacted session re-injects discarded history and overflows the context window

Steps to reproduce

  1. Run a long session until auto-compaction fires (history is moved to a closed context window).
  2. Fork the session from any message.
  3. Send a new message in the forked session.

Actual behavior

The first LLM request in the fork contains the full pre-compaction history. Compaction is silently undone, and the payload can exceed the model's context window.

Expected behavior

The fork should contain only the current context window (the compacted summary and messages after it), like the parent session sees.

Root cause

forkSession (src/server/session/manager.ts) copies messages by index and rewrites every message's contextWindowId to the fork's new window. Compaction keeps old messages in state and isolates them only by window id, so this remap turns discarded history into "current window" messages. Context building trusts the window id, so everything is re-injected. The fork also resets contextState (tokens = 0, canCompact = false) and drops context.compacted events, so no protective compaction fires and compaction history is lost.

Why it went unnoticed

For non-compacted sessions all messages share one window, so the remap is a no-op and forks work fine. The bug only triggers after a compaction.

Introduced in: v2.0.89 (PR #166, 1f54d6aa) · Still present in: v2.0.143

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions