Skip to content
PidocsCLI & tooling

Understand Pi's /compact context compaction logic

Session from pi-mono-sessions on Hugging Face by thomasmustier · MIT · condensed by Coders Talk

33msession
0interventions
GOAL

Understand how Pi's /compact command generates its compaction summary and what gets exposed to the agent afterward, including cut-point and split-turn behavior.

  1. Prompt · +0
    How does Pi currently compact? Explain in simple terms how /compact generates the summary and what's exposed to the agent post-compaction.
  2. Agent did · +0m
    Searched the codebase for compaction logic, read compaction.ts, session-manager.ts, messages.ts, and interactive-mode.ts, and summarized how /compact picks a cut point, keeps a recent token tail, and generates a checkpoint summary.
  3. Prompt · +14m
    Can you illustrate with an example of what the assistant sees post-compaction? Does the recent tail include tool calls and responses? How long is the tail?
  4. Prompt · +15m
    What do you mean by 'if the cut splits a turn'?
  5. Agent did · +15m
    Traced findTurnStartIndex and findCutPoint to explain that a turn runs from a user message through assistant/tool activity, and a split turn occurs when the cut lands mid-turn, requiring a separate turn-prefix summary appended to the main summary.
  6. Prompt · +28m
    Explain the turn-prefix behavior in plain English. Isn't every compaction landing on a split turn? When would /compact not do this?
  7. Agent did · +28m
    Read findValidCutPoints and findCutPoint in detail, clarifying that cut points never land on tool results, and explaining when a cut can land exactly on a user message (no split) versus mid-turn.
  8. Prompt · +31m
    In what scenario does the cut point fall exactly on a user message? And is a split turn always the oldest turn in the kept tail, with a turn summary substituted for it?
  9. Outcome · +33m
    Confirmed via code (compaction.ts) that a split turn is always the oldest turn in the kept tail, and pinned down exactly which messages form the 'prefix' summarized before the cut point.
VERDICT

I'd start by asking the agent to trace findCutPoint and findTurnStartIndex directly instead of doing broad rg searches first, since that's where the real logic lives. Repeatedly pushing back with 'what do you mean by X' forced it to ground its explanations in exact line numbers rather than hand-wavy summaries, which is what actually clarified the split-turn behavior for me.

I tried this · 0

Discussion · 0

How Builds work