Dream — Nightly Memory Distillation & Self-Evolution
现状
Phase 1(✅ 已实现): OpenClaw Hook hippocampus 监听每条消息,实时写入 memory/logs/
Phase 2(✅ 已实现): dream.py v3.3 定时蒸馏,新增 Distillation Agent 做真正的推理
完整链路
用户对话
↓
OpenClaw Hook: message:preprocessed
↓
丘脑过滤(Thalamus)→ 杏仁核标记(Amygdala)→ 海马体存储(memory/logs/)
↓
cron 触发(早7点/晚10点)
dream.py v3.3
↓
读取 hippocampus 日志 + recall store
↓
杏仁核标记融合 → Auditor审计 → 分析皮层模式识别 → 前额叶蒸馏规划 → 归档区 → 真相文件写回 → 梦境报告
脑区协同架构
① 丘脑(Thalamus)— 注意力门控
过滤纯问候/简单确认,只记录有意义的事件 标记类型:event / decision / correction / completed / insight / error
② 杏仁核(Amygdala)— 情绪标记
correction/error/decision/completed/insight 携带 HIGH 权重,优先蒸馏
③ 海马体(Hippocampus)— 原始存储
Phase 1:OpenClaw hook 实时追加到
memory/logs/YYYY/MM/YYYY-MM-DD.mdPhase 2:dream.py读取日志,融合 recall store,一并蒸馏
④ 前额叶(Prefrontal Cortex)— 蒸馏规划 + 执行控制
人脑中前额叶负责计划、推理、抑制冲动。核心功能是「在适当的时候做适当的事」。 Dream 中:
- 规划:cron 触发时决定今天沉淀什么、遗忘什么
- 抑制:断路器规则——3轮内无法完成则终止,防止死循环
- 调度:协调分析皮层和归档区的工作顺序
⑤ 蓝斑核(Locus Coeruleus)— 警觉与新鲜度信号
人脑中蓝斑核释放去甲肾上腺素,调节警觉和注意力,对新奇事物敏感。 Dream 中:health metrics 里的 freshness 分数——最近被提及的记忆权重更高。 公式:freshness = entries_referenced_last_30_days / total_entries
Phase 1 — 丘脑 + 杏仁核 + 海马体(始终活跃)
对话进行时,三个脑区协同工作:
- 丘脑过滤:「这条要记吗?」→ 重要事件才写日志
- 杏仁核标记:correction / error 携带 [HIGH] 权重
- 海马体存储:追加到
memory/logs/YYYY/MM/YYYY-MM-DD.md
Phase 2 — 前额叶 + 分析皮层 + 蓝斑核(cron 或手动触发)
cron 触发时:
- 前额叶规划:读取今日日志 +
.learnings/+ 现有 topic 文件,制定蒸馏计划- 分析皮层扫描:失败模式 / 重复错误 / 用户纠正规律 → 提炼 Pattern
- 蓝斑核评分:计算 freshness / coverage / coherence → 更新 health metrics
- 归档区执行:90天未引用且低价值的条目归档
- 前额叶输出:梦境报告(生长指标 + 洞察 + 明日重点)
Integration with self-improving-agent
Dream reads and promotes entries from the self-improving-agent skill:
.learnings/
├── LEARNINGS.md # Corrections, insights, best practices
├── ERRORS.md # Command failures, integration errors
└── FEATURE_REQUESTS.md # User-requested capabilities
Distillation logic:
- Corrections (category:
correction) → promote tofeedbacktopic - Recurring errors (3+ occurrences) → extract to Pattern Library
- Errors with fixes → promote to relevant topic
- Feature requests → create
projecttopic if validated - Best practices → promote to relevant topic
- Mark promoted entries as
status: promoted
Pattern Library (from eo-ability-dream)
Patterns are reusable response templates extracted from recurring learnings:
memory/patterns/
└── p-xxx.md # Pattern files with trigger + response
Pattern format:
---
name: pattern名称
trigger: 什么情况下触发
response: 如何响应
examples: [案例1, 案例2]
created: YYYY-MM-DD
updated: YYYY-MM-DD
---
Extraction criteria:
- Same error occurs ≥3 times across logs
- User-corrected mistake ≥2 times
- Non-obvious fix discovered through debugging
Memory Taxonomy
Four memory types:
- user — User preferences, role, goals, communication style
- feedback — User corrections and confirmations
- project — Project context, decisions, deadlines, tools in use
- reference — External systems, links, credentials locations
Directory Structure
memory/
├── logs/
│ └── YYYY/MM/YYYY-MM-DD.md # Daily append-only logs
├── topics/ # Distilled topic memories
│ ├── user_xxx.md
│ ├── feedback_xxx.md
│ ├── project_xxx.md
│ └── reference_xxx.md
├── patterns/ # Pattern Library (from eo-ability-dream)
│ └── p-xxx.md # Reusable patterns
├── episodes/ # Project narratives (from openclaw-auto-dream)
├── procedures.md # Workflow preferences
├── archive.md # Compressed old entries
├── dream-log.md # Dream cycle reports
├── index.json # Entry metadata (v3.0 schema)
└── MEMORY.md # INDEX only — points to topics
.learnings/ # self-improving-agent
├── LEARNINGS.md
├── ERRORS.md
└── FEATURE_REQUESTS.md
Health Score (from openclaw-auto-dream)
Five metrics measured each dream cycle:
| Metric | Weight | Formula |
|---|---|---|
| Freshness | 0.25 | entries_referenced_last_30_days / total |
| Coverage | 0.25 | categories_updated_last_14_days / 10 |
| Coherence | 0.20 | entries_with_relations / total |
| Efficiency | 0.15 | max(0, 1 - line_count/500) |
| Reachability | 0.15 | graph connectivity (connected components) |
Health Score = weighted sum × 100 (0-100)
| Score | Rating |
|---|---|
| 80-100 | Excellent |
| 60-79 | Good |
| 40-59 | Fair |
| 20-39 | Poor |
| 0-19 | Critical |
Importance Scoring (from openclaw-auto-dream)
importance = base_weight × recency_factor × reference_boost
- base_weight: default 1.0, 🔥HIGH = 2.0, ⚠️PERMANENT = 1.0 (skip formula)
- recency_factor:
max(0.1, 1.0 - days/180) - reference_boost:
max(1.0, log2(refs + 1))
Archive conditions (all must be true):
- days_since_referenced > 90
- importance < 0.3
- NOT ⚠️PERMANENT, NOT 📌PIN, NOT in episodes/
Optional: SwarmRecall Cloud Tier
Only active if SWARMRECALL_API_KEY environment variable is set.
SwarmRecall provides advanced cloud-side operations for large memory stores:
- Tier 1 (server-side): deduplication clustering, importance decay, stale pruning
- Tier 2 (agent-driven): contradiction resolution, session summarization, knowledge graph enrichment
Setup
- Get an API key:
POST /api/v1/registerwith{ "name": "<agent-name>" } - Set environment variable:
SWARMRECALL_API_KEY=<your-key> - SwarmRecall is now active — Tier 1 runs automatically on configured interval
SwarmRecall Endpoints Used
POST /api/v1/dream— start dream cyclePOST /api/v1/dream/execute— run server-side ops (decay, prune)GET /api/v1/dream/candidates/duplicates— find duplicate clustersGET /api/v1/dream/candidates/contradictions— find conflicting memoriesGET /api/v1/dream/candidates/stale— find prunable memoriesPATCH /api/v1/dream/:id— report dream results
Privacy
- Data transmitted over HTTPS to
swarmrecall-api.onrender.com - Only your agent's existing memory data is sent — no new external data collected
- Archived memories are soft-deleted (recoverable)
- Tenant-isolated by owner ID and agent ID
MEMORY.md Index Format
MEMORY.md is an INDEX ONLY — one line per topic:
# MEMORY.md — Long-Term Memory Index
_Last updated: YYYY-MM-DD_
## Topics
- [Title](topics/file.md) — one-line description
- [Title](topics/file.md) — one-line description
## Patterns
- [pattern-name](patterns/p-xxx.md) — trigger summary
## Stats
- Total entries: N | Health: N/100 | Streak: N dreams
Daily Log Format
Append to memory/logs/YYYY/MM/YYYY-MM-DD.md after each significant event:
## HH:MM
- [type] content description
- completed: task name
- decision: what was decided
- correction: user feedback received
- insight: what was learned
Types: event | decision | correction | completed | insight | error | question
Cron Configuration
Two cron jobs:
- dream-morning: 7:00 AM
- dream-night: 10:00 PM
--no-deliver(writes to file only)
Cron message is updated daily via update-cron-date.py script which injects current date.
Dream Distillation Steps (v3.3)
When cron triggers:
- Read
memory/logs/{date}.md - Read
.learnings/LEARNINGS.md,.learnings/ERRORS.md,.learnings/FEATURE_REQUESTS.md - Read
MEMORY.md, topic files,index.json,procedures.mdfor context - Snapshot BEFORE: count entries, decisions, lessons, procedures
- Analyze failures:
- Count occurrences of each error in ERRORS.md
- Identify recurring patterns (≥3 occurrences)
- Extract root cause
- Distillation Agent(新增): Run sub-agent on raw entries + learnings + health metrics → produce:
- 3-5 genuine insights ("I learned that...")
- 1-3 tomorrow action items
- 0-3 topic files to write to
memory/topics/ - Health metric interpretation
- Write topic files (from Distillation Agent output)
- Update truth files (
user_state.md,pending.md) - Update
index.jsonentry metadata (id, importance, tags, related) - Compute health metrics → update
index.jsonstats - Archive eligible entries → append to
archive.md - Update
MEMORY.mdindex (max 200 lines) - Snapshot AFTER: calculate deltas
- Write dream report to
memory/dreams/{date}.mdanddream-log.md - Regenerate
dashboard.htmlif exists - [Optional SwarmRecall]: If
SWARMRECALL_API_KEYis set:- Start dream cycle:
POST /api/v1/dream - Run server-side ops:
POST /api/v1/dream/execute - Fetch duplicate clusters → merge redundant memories
- Fetch contradictions → resolve conflicting entries
- Update dream cycle with results
- Start dream cycle:
Dream Report Format (v3.3)
# 🌙 Dream Report — {date}
## System Status
- Raw entries: N | recall store: N | Health: {score}/100
## Health Insights
- {insight based on coherence/freshness metrics}
## Insights ("I Learned")
- {genuine insight 1}
- {genuine insight 2}
## Tomorrow's Focus
- {actionable item 1}
- {actionable item 2}
## Topic Files Written
- {filename}: {title}
## Analysis
- Recurring errors found: {list}
- Root causes identified: {analysis}
## Patterns Updated
- {pattern_name}: {change}
First Dream (post-install)
After initial setup, run a full scan of ALL existing daily logs (not just last 3 days). Compare before/after metrics and send user a First Dream Report showing:
- How many logs scanned, entries extracted
- Before → After table
- Personalized reflection based on actual log content
User Prompts
- "dream report" / "梦境报告" → read and display latest dream report
- "dream" / "做梦" → run distillation now
- "/dream status" → show health score, pattern count, streak, stale threads
Scripts
dream.py— Phase 2 蒸馏脚本(v3.3,Distillation Agent)update-cron-date.py— 每日 cron 日期注入
Phase 1 启用(hippocampus hook)
Hook 目录: ~/.openclaw/hooks/hippocampus/
已配置: openclaw.json 中 hooks.internal.entries.hippocampus: enabled: true
功能: 监听 message:preprocessed 事件,自动记录对话到 memory/logs/YYYY/MM/YYYY-MM-DD.md
丘脑过滤规则:
- 纯问候 / 简单确认(<20字)不记录
- 高权重标记:correction / error / decision / completed / insight
重启 gateway 后生效:
schtasks /run /tn "OpenClaw Gateway"
技能整合说明
Phase 1 + Phase 2 完整闭环(已实现)
- Phase 1:
~/.openclaw/hooks/hippocampus/— OpenClaw hook 实时记录 - Phase 2:
dream.py v3.3— Distillation Agent 做真正的推理,取代"444个候选topic"的列表
已整合
- self-improving-agent → dream 的日志层(
.learnings/),无需单独使用
其他AI自我进化技能定位
- recursive-self-improvement:代码级自修复系统(错误检测→根因分析→修复→测试→验证),与 dream 是不同维度,可共存
- dream = 记忆蒸馏 + Pattern提取 + 健康评分
- recursive-self-improvement = 代码错误自动修复
