我们已经有一段时间没有看到 radishzz 了 — 他/她上次发帖是 6 个月前。
春晚太难看了,我一边看春晚,一边和 antigravity、cursor 合作,花了四五个小时,设计出了这一套 OpenClaw 终极记忆系统。看着还不错,分享给大家,帮助大家解决 OpenClaw 的失忆问题,也顺便祝大家新年快乐!
方案介绍
参考了哪些方案?
在设计这套记忆系统之前,我们调研了以下方案:
| 方案 | 来源 | 类型 |
|---|---|---|
| calicastle 三层架构 | X ( @calicastle ) | 社区方案 |
| openclaw-memory | GitHub ( geq1fan ) | 官方 Skill |
| openclaw-engram | GitHub | 社区插件 |
| Supermemory / Mem0 | 官方插件 | 云服务方案 |
1.2 各方案的优缺点
| 方案 | 优点 | 缺点 |
|---|---|---|
| calicastle | 架构清晰,三层设计简单易懂 | 无去重机制,仅每天捕获一次,无验证逻辑 |
| openclaw-memory | 支持自动验证(过时/重复/冲突检测) | 两个独立 Job 较复杂,Python 依赖 |
| engram | 功能最全(10种记忆分类、信心分数、实体档案) | 复杂度高,需要 API Key |
| Supermemory / Mem0 | 零配置,即装即用 | 云端依赖,需要付费 |
1.3 我们的方案特点
经过三轮对比分析,我们设计出这套「终极记忆系统」:
核心优势:
| 特点 | 说明 |
|---|---|
| Session ID 去重,避免重复记录 | |
| MEMORY.md 上限 80 行 / 5KB,防止膨胀 | |
| 跳过不足 2 条消息的会话,减少噪音 | |
| 进入长期记忆需满足四个条件 | |
| 修改前自动备份,安全性 | |
| 仅 2 个 Cron Job,易于维护 |
2. 如何配置
2.1 前置要求
- OpenClaw 已安装并运行
- 已配置 qmd 作为记忆后端
- 工作目录
~/.openclaw/workspace/
2.2 配置步骤
步骤 1:创建目录结构
mkdir -p ~/.openclaw/workspace/memory/weekly
mkdir -p ~/.openclaw/workspace/memory/archive
步骤 2:创建 MEMORY.md 模板
# Long-Term Memory
> Only write info here that you'd make mistakes without. Event logs stay in daily files.
> Hard limit: 80 lines / 5KB. Must compress before adding when over limit.
## User Preferences
## Active Projects
## Key Decisions
## Important Contacts
步骤 3:更新 AGENTS.md
在 ## Memory 部分添加:
### 🔍 Memory Retrieval (MANDATORY)
When you need to recall past events, **search first — never read all files**:
1. `qmd query "<question>"` — hybrid search with reranking
2. `qmd get <file>:<line> -l 20` — pull only the snippet you need
3. Only fall back to reading files directly if qmd returns nothing
### ✍️ Memory Writing - Don't Wait for Cron
When you make a decision, the user says "I prefer X", or a key task completes → **immediately append to `memory/YYYY-MM-DD.md`**.
Cron captures sessions automatically every few hours, but it's just the safety net. Write it down in the moment.
步骤 4:添加 Cron Job
Job 1: memory-sync(记忆同步)
openclaw cron add \
--name "memory-sync" \
--cron "0 10,14,18,22 * * *" \
--tz "Asia/Shanghai" \
--session isolated \
--wake now \
--delivery none \
--message "MEMORY SYNC — You are the memory capture agent. Run silently, no notifications.
1. Use sessions_list to get sessions with activity in the last 4 hours
2. Skip isolated sessions
3. For each session, use sessions_history to read conversation content
4. Skip sessions with user_message_count < 2
5. If no valid sessions remain, do nothing — reply ANNOUNCE_SKIP and stop here.
6. Read today's memory/YYYY-MM-DD.md (create if it doesn't exist)
7. Idempotency check: if a session_id's first 8 characters already appear in the file, skip that session
8. For unrecorded sessions, extract: user's key requests, assistant's conclusions/decisions, important action results. Compress each session to 3-10 summary items.
9. Append to the daily file in this format: ## HH:MM session:FIRST8 | N messages
10. Run qmd update && qmd embed
11. Reply ANNOUNCE_SKIP when done."
Job 2: memory-tidy(记忆整理)
openclaw cron add \
--name "memory-tidy" \
--cron "0 3 * * *" \
--tz "Asia/Shanghai" \
--session isolated \
--wake now \
--deliver \
--message "MEMORY TIDY — You are the memory maintenance agent. You are explicitly authorized to read and modify MEMORY.md in this isolated session.
[Phase 1: Compress]
1. List all date-named files (YYYY-MM-DD.md) in memory/
2. Identify files older than 7 days. If none, skip this phase.
3. Group by natural week, generate memory/weekly/YYYY-MM-DD.md (named after Monday)
4. Extract [Decisions] [Discoveries] [Preferences] [Tasks], tag each with (src: YYYY-MM-DD)
5. Idempotent: if ### YYYY-MM-DD section already exists in weekly file, skip it
[Phase 2: Distill]
6. Read daily files from the last 7 days + current MEMORY.md
7. Identify info worth keeping long-term. All four criteria must be met: (a) agent would make a concrete mistake without it (b) applies to many future conversations (c) self-contained and understandable (d) not duplicated in existing MEMORY.md
8. Reverse check: before writing, ask yourself — what specific error would occur without this? If you can't answer, don't write it.
9. Backup: mkdir -p memory/archive && cp MEMORY.md memory/archive/MEMORY.md.bak-\$(date +%F)
10. Update MEMORY.md. Hard limit: 80 lines. If over, compress/merge existing entries first.
[Phase 3: Archive]
11. Move daily files that have been compressed into weekly summaries to memory/archive/YYYY/
[Wrap-up]
12. Run qmd update && qmd embed
13. If changes were made, send a brief summary. If no changes: reply 🧠 memory-tidy done, no changes."
2.3 文件结构
~/.openclaw/workspace/
├── MEMORY.md # 长期记忆(80 行上限)
├── AGENTS.md # 已更新记忆规则
└── memory/
├── YYYY-MM-DD.md # 每日日志
├── weekly/ # 周度压缩摘要
└── archive/ # 归档旧日志
3. 工作流程
┌─────────────────────────────────────┐
│ memory-sync (10/14/18/22) │
│ • 扫描过去 4 小时的会话 │
│ • 跳过 <2 条消息的会话 │
│ • Session ID 去重 │
│ • 写入 memory/YYYY-MM-DD.md │
│ • qmd update && qmd embed │
└─────────────────────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ memory-tidy (每日 3 AM) │
│ Phase 1: 压缩 → 周度摘要 │
│ Phase 2: 蒸馏 → 更新 MEMORY.md │
│ Phase 3: 归档 → 移动旧文件 │
│ qmd update && qmd embed │
└─────────────────────────────────────┘
4. 验证
配置完成后:
- 运行
openclaw cron list确认 Job 已注册 - 发起几轮对话,等待下一个 sync 窗口(10/14/18/22 点)
- 检查
memory/2026-02-16.md是否生成 - 测试
qmd query "关键词"搜索功能 - 一周后检查
memory/weekly/是否有压缩摘要