pi-web icon indicating copy to clipboard operation
pi-web copied to clipboard

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

Open zhudatou630 opened this issue 4 months ago • 0 comments

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).

zhudatou630 avatar Jul 22 '26 13:07 zhudatou630