Skip to content

feat: /compact summarizes earlier messages so long conversations keep fitting the context - #144

Merged
bencode merged 1 commit into
mainfrom
feat/conversation-compaction
Oct 13, 2026
Merged

bencode merged 1 commit into
mainfrom
feat/conversation-compaction

Conversation

@bencode

@bencode bencode commented Oct 13, 2026

Copy link
Copy Markdown
Owner

What

A long conversation used to send every message to the model on every request; nothing ever made it smaller. /compact now summarizes the earlier part of a conversation so it keeps fitting the model's context. The reader still sees every message, with a divider that opens the summary.

The algorithm is ported from pi's coding agent (packages/coding-agent/src/core/compaction):

  • Append-only. A compaction message is appended to the conversation (a CustomAgentMessages entry), recording the summary and firstKept, the stored position of the first message kept as is. Nothing is rewritten, and the IndexedDB schema is unchanged.
  • Projection. transformContext sends [system messages…, <summary>, messages from firstKept…]. Every system message is kept, because pi-agent-core declares tool changes with system messages in the middle of the transcript. Memory's transform runs after the projection.
  • Cut point. Walking back from the newest message until 20k tokens are kept, the cut never lands on a tool result. When trailing tool results alone exceed the budget, the cut keeps the call they answer (pi #9740). A cut inside a turn summarizes the start of that turn separately (split turn).
  • Summary. Uses the conversation's model with thinking off. The format is for reading: goal, materials and position (names, fileIds, ranges read), findings, preferences, open questions, critical quotations. A second compaction updates the previous summary through <previous-summary>. A reply that stops for length, an error, or a tool call fails the compaction and records nothing.
  • Token usage. The status bar estimates the projected context. The summary message's timestamp invalidates usage reported before it. The summary calls count towards "used".

Automatic compaction and overflow recovery come next, as a separate change.

Files

  • core/agent/compaction.ts: message type, projection, cut point, serialization. All pure functions, with tests.
  • features/assistant/compaction.ts: prompts, plus compactConversation, which calls the model.
  • create-reader-agent.ts, use-conversation.ts, conversation-panel.tsx, conversation-messages.tsx, token-usage.ts, styles.css: wiring, the /compact command, the compacting phase and the divider.

Testing

  • pnpm check passes.
  • End to end on a local server with agent-browser, using a generated 99 KB, 8-chapter brennock-light.md with one key fact per chapter:
    1. Reading chapters 1–4 and then 5–8 brought the context to 31.2K. /compact brought it to 19.6K. The cut fell inside the first turn (firstKept=16), so only a turn-prefix summary was made, with the fileId, chapter line ranges, ranges read and key records preserved.
    2. After another turn (23.9K), a second /compact updated the previous summary (firstKept=18), giving 19.9K.
    3. Asked without tools whether any earlier read had failed, the assistant quoted the exact error ("Cursor does not match this request. Restart without a cursor.") from a message that was by then only in the summary.
    4. After a reload, the conversation still showed both dividers, with the context at 22.9K.
    5. /compact on a two-message conversation shows "Nothing to compact yet".

…ps fitting the model's context; the reader still sees every message, with a divider that opens the summary
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.

1 participant