Skip to content

Context compaction loses assistant messages and merges user follow-ups #43066

@KiruyaMomochi

Description

@KiruyaMomochi

Summary

After context compaction (auto or /compress), two defects appear in the new child session:

  1. Assistant messages lost — replies the agent delivered between the last compaction boundary and the compaction event are absent from the child session DB. The user sees "compaction summary → their next message" with no agent replies in between.

  2. User follow-ups merged into one turn — separate Telegram messages sent while the agent was busy are newline-joined into a single user message, destroying message boundaries visible to the user.

Root Cause

Issue 1: Stale offset after session split

compress_context() rotates to a child session and resets _last_flushed_db_idx = 0. However, _flush_messages_to_session_db computes start_idx = len(conversation_history) using the pre-compression history passed by the gateway/CLI caller. Since the old history is much longer than the compressed child transcript, messages[start_idx:] is empty and nothing gets written to the child session DB.

Issue 2: merge_pending_message_event with merge_text=True

The busy-mode interrupt path called merge_pending_message_event(..., merge_text=True) which joins consecutive text events with \n. The queue-mode path already used FIFO semantics (each message gets its own turn). The interrupt/steer-fallback paths did not.

Impact

  • After compaction, the agent has no memory of its own recent replies
  • Multiple user messages appear as one block — context and intent are lost
  • Particularly visible in Telegram DMs where users send rapid sequential messages

Environment

  • Platform: Telegram DM (no topic threads)
  • Triggered by both auto-compaction and manual /compress
  • Current HEAD: 02f878e

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High — major feature broken, no workaroundcomp/agentCore agent loop, run_agent.py, prompt buildercomp/gatewayGateway runner, session dispatch, deliverytype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions