Context Compression & MoA
源码版本v2026.7.20
Responsibility
Two things: (1) when history messages approach the provider's context limit, summarize/trim old messages to free up budget (context_compressor); (2) optionally use a Mixture-of-Agents path to aggregate outputs from multiple models (moa_loop). Both run inside the main loop — one keeps long conversations from overflowing the context window, the other aggregates multiple models for better answers.
Key Files
context_compressor module— compression main entry (~3700 lines)budget estimate:190-242—_estimate_msg_budget_tokens/_serialized_length_for_budgetmessage sanitization:136-155—_fresh_compaction_message_copy/_strip_persistence_markerstool call & path mention extraction:337-360—_extract_tool_call_name_and_args/_collect_path_mentionscontent trimming:472-515—_append_text_to_content/_strip_image_parts_from_partsmoa_loop— Mixture-of-Agents aggregationIterationBudget— compression refund and budget coupling
Data Flow
- Each turn the main loop evaluates whether compression is needed (see
_should_run_preflight_estimate:213). - When the threshold is hit, context_compressor is called:
- estimate each message's budget cost (
agent/context_compressor.py:419) - make sanitized copies of old messages (
agent/context_compressor.py:136), stripping persistence markers (agent/context_compressor.py:155) - preserve tool-call names/arguments and path mentions so summarization doesn't drop key operations (
agent/context_compressor.py:337) - trim image parts and overlong content (
agent/context_compressor.py:472)
- estimate each message's budget cost (
- The "new space" freed by compression is partially refunded via
iteration_budget.refund()(agent/iteration_budget.py), letting the main loop run a few more turns. - If MoA is enabled,
moa_loopcalls multiple providers in parallel this turn and aggregates them as an enhancement or arbitration of the main answer.
Summary
The compressor is what keeps long conversations going; the core tradeoff is "what to drop, what to keep" — it prioritizes tool calls and path mentions, because losing them makes subsequent turns "amnesic". MoA is an orthogonal enhancement that can be toggled. Neither changes the main-loop structure; they only cut in at the right moment.