Architecture - Context Lifecycle
Context quality determines agent quality. MicroClaw uses layered context management to keep long conversations useful and bounded.
Context layers
- Live turn context: latest user input + immediate tool outputs.
- Session history: persisted conversation blocks (including tool_use/tool_result).
- Compacted summary context: when session grows beyond threshold.
- File memory:
AGENTS.mdglobal + bot + per-chat memory (written bywrite_memorytool). - Structured memories: rows extracted from the
memoriestable by the background Reflector.
Lifecycle
- Load session from
sessionstable when available. - Append new user messages since
updated_at. - Inject memory context: file memory (
AGENTS.md) + structured memories from DB. - If message count exceeds
max_session_messages, compact older blocks. - Run tool loop, then persist updated session state.
Structured memory injection is budgeted by memory_token_budget (default 1500, estimated as content.len()/4 + 10 per memory). When budget is hit, remaining rows are omitted and a summary suffix is appended.
Background Reflector
Runs independently of the main chat loop every reflector_interval_mins (default 15 min). For each chat it tracks a cursor in memory_reflector_state, processes only unseen messages, calls the LLM to extract structured facts, validates categories, deduplicates, and persists to the memories table. This decouples memory extraction from model attention while avoiding repeated scans.
Dedup mode:
- semantic mode:
sqlite-vecfeature + runtime embedding config - fallback mode: Jaccard similarity
See Memory System for details.
Compaction strategy
- Keep recent messages verbatim (
compact_keep_recent). - Summarize older conversation into a compact summary block.
- Fallback to truncation if summarization fails.
- Strip large image payloads before session persistence.
Structured-memory retrieval modes
At prompt build time, structured memories are ordered by:
- semantic KNN (
sqlite-vec+ configured embedding provider) - fallback keyword relevance scoring (CJK-aware tokenizer) with recency tie-break
- MCP-backed query ordering when a memory MCP backend is active (
memory_query+memory_upsert)
When MCP memory backend is active, local sqlite-vec KNN ranking is not applied to MCP-backed rows; query ordering comes from MCP results, with automatic SQLite fallback on MCP failures.
Sub-agent inheritance and isolation
Current model:
- Sub-agent gets restricted tool registry.
- No recursive sub-agent calls.
- No side-effect scheduling/memory-write/send-message tools.
Recommended next step:
- Explicit inheritance policy by field (
history,memory,tool results,working_dir). - Context provenance markers in debug output.
Debugging context behavior
Use:
RUST_LOG=debug cargo run -- start- Web stream/tool events for per-iteration tool traces.
microclaw doctorfor environment/dependency issues that often masquerade as context problems.