Skip to content

Conversation

@tonyfettes
Copy link
Contributor

This pull request introduces automatic context compaction to the agent system, allowing conversations to be summarized and condensed when they exceed token limits. This helps keep the conversation within model context constraints and improves long-running interaction reliability. The changes add a new Compactor utility, integrate it into the agent lifecycle, and update event handling to support compacted context summaries.

Agent context compaction integration

  • Added a Compactor instance and token usage tracking (total_usage_tokens) to the Agent struct, enabling monitoring and management of context length.
  • Implemented run_auto_compact and get_compact_summary methods in Agent, which trigger compaction, summarize conversation history via LLM, reset token counters, and emit a new ContextCompacted event.
  • Integrated compaction checks and execution in the agent's main loop (Agent::start), automatically summarizing when token thresholds are exceeded and the conversation continues.
  • Instantiated the Compactor with configuration based on model token limits during agent initialization.

Compactor utility and summarization prompts

  • Added the new internal/compact/compact.mbt module, which provides the Compactor struct, compaction threshold logic, and summarization prompt construction.
  • Defined standard summarization and summary prefix prompts in internal/compact/prompts.mbt for consistent LLM handoff and context continuity.
  • Added a package manifest for the new compact module and updated dependencies in the agent package. [1] [2]

Event handling and serialization

  • Added a new ContextCompacted(summary : String) variant to the EventDesc enum, including serialization and deserialization support for compacted context summaries. [1] [2] [3]
  • Updated the conversation message rendering logic to insert the summary as a user message and discard prior messages at compaction points.

Testing

  • Added comprehensive tests for compactor configuration, threshold logic, and prompt formatting in internal/compact/compact_test.mbt.

@tonyfettes tonyfettes force-pushed the haoxiang/compact branch 3 times, most recently from 9395fe4 to 304e81b Compare January 9, 2026 05:11
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