Skip to content

fix: collapse post-compaction messages when compaction drops the user prompt#205

Open
zhudatou630 wants to merge 1 commit into
agegr:mainfrom
zhudatou630:fix/compaction-post-messages-collapse
Open

fix: collapse post-compaction messages when compaction drops the user prompt#205
zhudatou630 wants to merge 1 commit into
agegr:mainfrom
zhudatou630:fix/compaction-post-messages-collapse

Conversation

@zhudatou630

Copy link
Copy Markdown
Contributor

Problem

During a long agentic turn, auto-compaction can fire mid-turn. When it does, pi replaces the compacted history — including the original user prompt — with a compaction summary and the agent keeps producing tool calls and a final answer. There is no user message left to anchor the rest of the turn.

ChatWindow's message grouping only treated role === \"user\" as a turn anchor. After a mid-turn compaction, every subsequent tool call and assistant reply rendered standalone and never folded into a collapsed ProcessDetailsGroup. The whole post-compaction tail stayed expanded even after the run ended — the auto-collapse that normally happens when a turn completes was lost.

Repro

Real session data confirms it: a compaction entry whose firstKeptEntryId points at an assistant message, followed by assistant/toolResult entries with no user message after it. The loaded message list is [compaction(custom), assistant, toolResult, …], so the grouping loop never enters the user-anchored branch and renders everything standalone.

Fix

Treat a compaction summary (role: \"custom\", customType: \"compaction\") as a group anchor too:

  • It renders as the compaction block (unchanged).
  • The process messages after it fold into a ProcessDetailsGroup (collapsed by default).
  • The final assistant renders as the answer.

Live-tail detection now uses the last anchor (user or compaction) instead of only the last user message, so a compaction summary that sits after the last user message still anchors the currently-streaming segment.

Verification

  • tsc --noEmit — no new errors in the touched file.
  • Simulated the grouping loop on the real post-compaction message sequence:
    • Before: standalone × N (nothing collapses).
    • After: compaction anchors the segment → process messages grouped, final assistant rendered as answer.

Scope

Single-file, ~26-line change in components/ChatWindow.tsx. No behavior change for sessions without mid-turn compaction (lastAnchorIdx === lastUserIdx in the normal case).

… prompt

When compaction fires mid-turn, pi replaces the compacted history (including
the original user prompt) with a compaction summary and the agent keeps
producing tool calls and a final answer with no user message left to anchor
the turn.

ChatWindow's message grouping only treated role==="user" as a turn anchor,
so after such a compaction every subsequent tool call and assistant reply
rendered standalone and never folded into a collapsed ProcessDetailsGroup —
the whole post-compaction tail stayed expanded even after the run ended.

Treat a compaction summary (role "custom", customType "compaction") as a
group anchor too: it renders as the compaction block, the process messages
after it fold into ProcessDetailsGroup, and the final assistant renders as
the answer. Live-tail detection now uses the last anchor (user or
compaction) instead of only the last user message, so a compaction summary
that sits after the last user message still anchors the streaming segment.
Copilot AI review requested due to automatic review settings July 22, 2026 13:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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