Cognitive Brain
Unified memory system modeled on human brain architecture. One CLI (brain) for all memory operations.
Architecture
| System | Brain Region | What it does |
|---|
| Episodic | Hippocampus | Time-stamped experiences with emotional tags |
| Semantic |
Neocortex | Structured facts (entity/key/value with FTS5) |
|
Procedural | Cerebellum | Versioned workflows that evolve from failures |
|
Attention | Thalamus | Score incoming info → store/summarize/discard |
|
Consolidation | Sleep replay | Batch-process episodes → extract facts |
|
Health | Soul erosion | Detect memory drift, conflicts, flatness |
Installation
CODEBLOCK0
Environment Variables
| Variable | Default | Purpose |
|---|
| INLINECODE1 | INLINECODE2 | Path to brain database |
| INLINECODE3 |
margot | Agent identity for scoping |
|
BRAIN_FACTS_DB |
memory/facts.db | Legacy facts database path |
|
BRAIN_LLM_URL | Google Gemini endpoint | OpenAI-compatible chat completions URL |
|
BRAIN_LLM_KEY |
(none — must be set) | API key for LLM provider (required for
proc evolve) |
|
BRAIN_LLM_MODEL |
gemini-2.5-flash | Model name for evolution reasoning |
Credentials & Scope
Required for brain proc evolve only:
- -
BRAIN_LLM_KEY — Your API key for the LLM provider. Set via env var or brain config set key <value>. - No credentials are auto-discovered or read from platform stores.
- Without a key,
proc evolve falls back to local pattern-based evolution (no LLM needed).
Data scope:
- - All data stays in your
brain.db file (local SQLite). - INLINECODE17 reads/writes
BRAIN_FACTS_DB (default: facts.db in skill directory). - INLINECODE20 reads/writes
SESSION_STATE (default: SESSION-STATE.md in workspace root). - No data is sent externally except LLM API calls during
proc evolve.
Quick Reference
Store & Recall
CODEBLOCK1
Facts (Semantic Memory)
CODEBLOCK2
Procedures (Cerebellum)
CODEBLOCK3
Attention Filter
CODEBLOCK4
Consolidation
CODEBLOCK5
Health (Soul Erosion Detection)
CODEBLOCK6
Configuration
CODEBLOCK7
Multi-Agent
CODEBLOCK8
Procedure Evolution Flow
The core innovation — procedures that rewrite themselves from failure patterns:
- 1. Record failures with step-level granularity: INLINECODE24
- At 3+ failures, brain suggests evolution
brain proc evolve <slug> analyzes patterns:
- Repeat offender steps (same step failing multiple times)
- Brittle chains (consecutive step failures)
- Error keyword clustering (timeout, auth, permission, etc.)
- 4. LLM synthesizes and rewrites steps — adds pre-checks, reorders, annotates with INLINECODE26
- Local fallback if LLM unavailable — pattern-matching inserts defensive steps
- Full version history preserved: INLINECODE27
Health Metrics
Seven metrics, each scored 1-10:
| Metric | What it detects |
|---|
| Memory Freshness | Time since last recorded episode |
| Consolidation Debt |
Backlog of unprocessed episodes |
| Importance Calibration | Everything rated 8+? Nothing is important |
| Emotional Diversity | Flatlined to one emotion = loss of range |
| Fact Consistency | Contradictory facts = identity fragmentation |
| Procedure Health | Success rates dropping on learned behaviors |
| Recording Cadence | Silent days creating memory gaps |
Schema
Database: SQLite with WAL mode, FTS5 full-text search, foreign keys.
Tables: episodes, episodes_fts, facts, facts_fts, procedures, procedure_history, working_memory, consolidation_log, brain_meta.
Initialize with: INLINECODE37
Files
| File | Purpose |
|---|
| INLINECODE38 | Main CLI dispatcher |
| INLINECODE39 |
Database schema |
|
scripts/attention.py | Thalamic attention filter (rule-based scoring) |
|
scripts/consolidate.py | Sleep replay consolidation pipeline |
|
scripts/erosion.py | Soul erosion health metrics |
|
scripts/evolve.py | Procedure evolution engine (LLM + local fallback) |
|
scripts/facts.py | Semantic fact storage wrapper |
|
scripts/migrate-daily-logs.py | Import existing daily markdown logs |
认知大脑
基于人脑架构的统一记忆系统。所有记忆操作通过单一CLI(brain)完成。
架构
| 系统 | 脑区 | 功能 |
|---|
| 情景记忆 | 海马体 | 带情感标签的时间戳经历 |
| 语义记忆 |
新皮层 | 结构化事实(实体/键/值,支持FTS5) |
|
程序记忆 | 小脑 | 从失败中进化的版本化工作流 |
|
注意力 | 丘脑 | 对输入信息评分 → 存储/总结/丢弃 |
|
巩固 | 睡眠回放 | 批量处理情景 → 提取事实 |
|
健康 | 灵魂侵蚀 | 检测记忆漂移、冲突、扁平化 |
安装
bash
1. 初始化数据库
sqlite3 brain.db < scripts/schema.sql
2. 链接CLI
ln -sf $(pwd)/scripts/brain.sh ~/.local/bin/brain
chmod +x scripts/brain.sh
3. (可选)迁移现有每日日志
python3 scripts/migrate-daily-logs.py --dir /path/to/memory/ --db brain.db
环境变量
| 变量 | 默认值 | 用途 |
|---|
| BRAINDB | <技能目录>/brain.db | 大脑数据库路径 |
| BRAINAGENT |
margot | 用于作用域划分的代理身份 |
| BRAIN
FACTSDB | memory/facts.db | 旧版事实数据库路径 |
| BRAIN
LLMURL | Google Gemini端点 | 兼容OpenAI的聊天补全URL |
| BRAIN
LLMKEY |
(无 — 必须设置) | LLM提供商的API密钥(proc evolve必需) |
| BRAIN
LLMMODEL | gemini-2.5-flash | 进化推理的模型名称 |
凭证与作用域
仅brain proc evolve需要:
- - BRAINLLMKEY — 您的LLM提供商API密钥。通过环境变量或brain config set key 设置。
- 不会自动发现或从平台存储读取凭证。
- 没有密钥时,proc evolve回退到基于本地模式的进化(无需LLM)。
数据作用域:
- - 所有数据保留在您的brain.db文件中(本地SQLite)。
- brain facts读取/写入BRAINFACTSDB(默认:技能目录中的facts.db)。
- brain wm读取/写入SESSION_STATE(默认:工作区根目录中的SESSION-STATE.md)。
- 除proc evolve期间的LLM API调用外,不会向外部发送任何数据。
快速参考
存储与回忆
bash
brain store 修复了部署流水线 --title 部署修复 --emotion 宽慰 --importance 8
brain ingest 凌晨3点Docker内存溢出 --title OOM事件 --source mqtt # 注意力门控
brain recall 部署流水线 --type all --limit 5
brain episodes 2026-03-15
brain emotions 7
brain important 8 14
事实(语义记忆)
bash
brain facts get Darian favorite_movie
brain facts set Mae birthday 9月12日 --category date --permanent
brain facts search SSH --limit 5
brain facts list --entity Darian --limit 10
brain facts stats
程序(小脑)
bash
brain proc create deploy-api --title 部署API --steps [拉取最新代码,运行测试,部署]
brain proc success deploy-api
brain proc fail deploy-api --step 2 --error 测试超时 --fix 将超时时间延长至60秒
brain proc evolve deploy-api # LLM根据失败模式重写步骤
brain proc evolve deploy-api --dry-run # 预览但不应用
brain proc history deploy-api # 完整进化时间线
brain proc list
注意力过滤器
bash
brain filter GPU温度72°C --source mqtt # → 丢弃(常规)
brain filter 来自新IP的SSH暴力破解 --source security # → 存储(新型威胁)
巩固
bash
brain consolidate --dry-run # 预览将处理的内容
brain consolidate # 运行睡眠回放
健康(灵魂侵蚀检测)
bash
brain health # 7项指标评分报告
brain health -v # 详细模式,包含所有细节
brain health --json # 机器可读格式,适用于定时任务
配置
bash
brain config show # 当前LLM配置
brain config set model gpt-4o # 更改模型
brain config set url http://localhost:11434/v1/chat/completions # 切换到Ollama
多代理
bash
brain --agent bud store 巡逻完成 --title Bud巡逻 --importance 3
brain --agent bud proc list # 查看自身及共享的程序
brain who # 显示系统中所有代理
程序进化流程
核心创新——程序根据失败模式自我重写:
- 1. 记录失败,精确到步骤级别:brain proc fail --step N --error 描述
- 累计3次以上失败时,大脑建议进化
- brain proc evolve 分析模式:
- 重复出错的步骤(同一步骤多次失败)
- 脆弱链条(连续步骤失败)
- 错误关键词聚类(超时、认证、权限等)
- 4. LLM综合并重写步骤——添加前置检查、重新排序、标注[vN: 原因]
- LLM不可用时本地回退——模式匹配插入防御性步骤
- 完整版本历史保留:brain proc history
健康指标
七项指标,每项评分1-10分:
| 指标 | 检测内容 |
|---|
| 记忆新鲜度 | 自上次记录情景以来的时间 |
| 巩固债务 |
未处理情景的积压量 |
| 重要性校准 | 所有内容都评8分以上?那就不存在重要之事 |
| 情感多样性 | 扁平化为单一情感 = 情感范围丧失 |
| 事实一致性 | 矛盾的事实 = 身份碎片化 |
| 程序健康度 | 已学习行为成功率下降 |
| 记录节奏 | 沉默天数造成记忆空白 |
架构
数据库:SQLite,启用WAL模式、FTS5全文搜索、外键。
表:episodes、episodesfts、facts、factsfts、procedures、procedurehistory、workingmemory、consolidationlog、brainmeta。
初始化:sqlite3 brain.db < scripts/schema.sql
文件
| 文件 | 用途 |
|---|
| scripts/brain.sh | 主CLI调度器 |
| scripts/schema.sql |
数据库架构 |
| scripts/attention.py | 丘脑注意力过滤器(基于规则的评分) |
| scripts/consolidate.py | 睡眠回放巩固流水线 |
| scripts/erosion.py | 灵魂侵蚀健康指标 |
| scripts/evolve.py | 程序进化引擎(LLM + 本地回退) |
| scripts/facts.py | 语义事实存储封装 |
| scripts/migrate-daily-logs.py | 导入现有每日Markdown日志 |