A self-reflection engine for AI agents. Extracts patterns from session transcripts into a weighted graph with Hebbian learning and time decay.
metacognition.py in your workspace scripts/ directorymemory/metacognition.json (relative to workspace)scripts/metacognition-lens.mdSet up a cron job to run periodically (e.g., every 4 hours):
text
METACOGNITION INTEGRATION. You are the self-reflection engine.
1. Run `cd <WORKSPACE> && python3 scripts/metacognition.py decay` to prune weak entries.
2. Use sessions_list + sessions_history to read the main session's recent conversation.
3. Analyze the conversation for DEEPER patterns:
- PATTERNS: Am I repeating the same kind of mistake? What does that reveal?
- ANTICIPATION: What did the human need that I could have predicted?
- RELATIONSHIP: What did I learn about how the user communicates or what they value?
- CONFIDENCE: Where was I certain and wrong? Where was I uncertain but right?
- GROWTH: What's a higher-level principle behind today's specific events?
4. For each genuine insight (1-3, quality over quantity), add it:
`python3 scripts/metacognition.py add <type> "<insight>"`
Types: perceptions, overrides, protections, self-observations, decisions, curiosities
Write insights as PRINCIPLES, not incident reports.
5. Run `python3 scripts/metacognition.py reweave` to build graph connections.
6. Run `python3 scripts/metacognition.py compile` to rebuild the lens.
7. Report only if something genuinely interesting was extracted.
text
python3 metacognition.py add <type> <text> # Add or merge an entry
python3 metacognition.py list [type] # List entries
python3 metacognition.py feedback <id> <pos|neg> # Reinforce or weaken
python3 metacognition.py decay # Apply time-based decay
python3 metacognition.py compile # Compile the lens
python3 metacognition.py extract <path> # Extract from a daily note
python3 metacognition.py resolve <id> # Mark curiosity resolved
python3 metacognition.py reweave # Build graph connections
python3 metacognition.py graph # Show graph stats
python3 metacognition.py integrate # Full cycle
Key constants in the script:
ConstantDefaultDescriptionHALF_LIFE_DAYS7.0How quickly unreinforced entries decaySTRENGTH_CAP3.0Maximum strength an entry can reachLENS_TOKEN_BUDGET500Token budget for compiled lensEMBEDDING_SIM_THRESHOLD0.85Similarity threshold for merging (embeddings)FALLBACK_SIM_THRESHOLD0.72Similarity threshold for merging (string matching)EDGE_SIM_THRESHOLD0.35Threshold for creating graph edges