AutoDream & Compaction
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- internal/engine/workflow/plan.go
- internal/engine/workflow/prompts/base.md
- internal/engine/workflow/prompts/autonomous.md
- pkg/autodream/autodream.go (Referenced via README.md architecture and package summary)
- pkg/compaction/compaction.go (Referenced via README.md architecture and package summary)
AutoDream and Compaction are critical context management systems within the M31 Autonomous (M31A) engine. They ensure that long-running coding sessions remain within the Large Language Model's (LLM) context window constraints by consolidating history and summarizing previous interactions. While standard coding assistants often lose context as conversations grow, M31A uses these systems to "own the loop" and maintain a coherent mental model of the task across hundreds of tool calls.
Sources: README.md:27-40, internal/engine/workflow/prompts/base.md:12-25
AutoDream Context Consolidation
AutoDream is a specialized system designed to compress long conversations automatically when the context window nears its limit. It prevents context overflow by merging redundant information while preserving essential data required for task execution.
Protection Logic
AutoDream does not compress all messages equally. To ensure the agent does not lose its "identity" or current progress, specific message types are protected:
- The initial user goal.
- Active tool calls and their results.
- The implementation plan.
- The last 5 messages in the current buffer.
Sources: README.md:173-177, internal/engine/workflow/prompts/autonomous.md:35-45
Workflow Integration
The consolidation process is triggered automatically on context window overflow. It can also be manually invoked via the /compress command in the terminal UI.
The diagram above illustrates how AutoDream transitions from a trigger event to a condensed state without losing critical task data.
Sources: README.md:173-177, README.md:210-220
Session Compaction
Compaction operates at the package level (pkg/compaction) to provide automatic session summarization. Unlike the immediate consolidation of AutoDream, Compaction focuses on replacing detailed historical logs with LLM-generated summaries.
Key Functionalities
- Summarization: Collapses multiple turns of conversation into a concise narrative.
- Replacement: Swaps out original verbose messages for summarized versions in the session persistence layer.
- Token Efficiency: Reduces the total token count of the session history while maintaining the logical flow of decisions made.
Sources: README.md:315-325, internal/engine/workflow/plan.go:50-65
Comparison of Management Systems
M31A employs multiple layers of memory management to balance cost, performance, and accuracy.
| System | Purpose | Persistence | Trigger |
|---|---|---|---|
| AutoDream | Real-time context compression | Volatile/Session-based | Context window overflow (>95%) |
| Compaction | Historical summarization | Saved to STATE.md/Session JSON | Post-phase transitions |
| Learning Ledger | Cross-session knowledge | Permanent Markdown-backed | Session completion (Ship phase) |
Sources: README.md:144-160, README.md:315-330, internal/engine/workflow/plan.go:150-165
Technical Implementation and Safety
The implementation of these systems includes reentrancy guards and safety heuristics to prevent the agent from entering a loop of continuous compression.
Context Awareness Rules
The engine enforces specific "Read-Before-Write" rules that interact with consolidated context. Even after a compaction event, the agent is instructed to:
- Map the dependency graph using
CodeMaporGrep. - Read the full content of relevant files (target, imports, callers).
- Verify project conventions before applying summarized knowledge.
Sources: internal/engine/workflow/prompts/base.md:33-50, internal/engine/workflow/prompts/context-awareness.md:10-30
Token Calibration
M31A uses tiktoken for estimation combined with Exponential Moving Average (EMA) self-calibration. This allows the AutoDream system to predict exactly when a compaction is required.
- Warning Banner: Triggers at 80% context usage.
- Hard Reject/Auto-Compress: Triggers at 95% context usage.
Sources: README.md:178-182, internal/engine/workflow/plan.go:80-95
Conclusion
AutoDream and Compaction are foundational to M31 Autonomous's ability to handle complex, multi-file software engineering tasks. By intelligently summarizing history and protecting critical goal-oriented data, these systems allow the agent to operate efficiently within LLM token limits while maintaining high code quality and architectural consistency across extended sessions.
Sources: README.md:12-25, internal/engine/workflow/prompts/autonomous.md:10-25