Big Memory
Structured task snapshots that survive context compaction. Three-phase protocol:
- 1. CAPTURE -- Save a structured task snapshot before compaction wipes your context
- DETECT -- Recognize when compaction has occurred and context is missing
- RECOVER -- Search memory for the latest snapshot and resume exactly where you left off
Uses only OpenClaw built-in tools (memory_search, memory_get, Read, Edit). No scripts, no external databases, no API keys.
Task Snapshot Schema
Every snapshot MUST follow this exact structure. Do not omit fields -- write "none" if a field is empty. For the full template with field-by-field guidelines, read {baseDir}/references/TASK-SNAPSHOT.md.
CODEBLOCK0
The HTML comment markers (<!-- BIG-MEMORY-SNAPSHOT v1 --> and <!-- /BIG-MEMORY-SNAPSHOT -->) are critical. They act as machine-parseable delimiters that memory_search matches via BM25 exact-term matching, enabling precise retrieval.
When to Capture
Trigger 1: Pre-Compaction Flush (Automatic)
When you receive a system message related to compaction (containing "compact", "memory flush", "store durable memories", or "nearing compaction"), execute the CAPTURE protocol instead of writing generic notes. Create a full structured snapshot following the schema above.
Trigger 2: Milestone Capture (Agent-Initiated)
After completing a significant unit of work -- implementing a feature, fixing a bug, making an architecture decision -- self-assess: "If compaction happened right now, would I lose critical context?" If yes, create a snapshot.
Good times to snapshot:
- - After a design decision that affects multiple files
- After writing code the user will refer back to
- After debugging a complex issue (capture the root cause and fix)
- When switching between subtasks within a larger task
Trigger 3: User Command (User-Initiated)
When the user says /big-memory save, "save snapshot", "checkpoint", or "big-memory save", create a snapshot immediately.
How to Store Snapshots
- 1. Read the current daily log
memory/{YYYY-MM-DD}.md using Read. If it does not exist, you will create it. - APPEND the snapshot. Never overwrite existing content. Read the file first, then use
Edit to append at the end. Alternatively, use Write with the full existing content plus the new snapshot. - Precede the snapshot with a horizontal rule and heading:
CODEBLOCK1
- 4. Multiple snapshots in the same file is expected. Each captures a point-in-time state. The most recent snapshot is the source of truth.
Post-Compaction Recovery Protocol
Step 1: Detect Compaction
Suspect compaction when ANY of these are true:
- - The conversation begins with a summary/compaction block rather than the original exchange
- You cannot recall specific details (file paths, variable names, exact code) that should be known
- The user says "you forgot", "we were working on", "remember when", "what was I doing", or similar
- A system message contains "Auto-compaction complete" or similar
Step 2: Search for Latest Snapshot
Execute this search:
CODEBLOCK2
This triggers hybrid vector + BM25 search across all memory files. The HTML comment markers ensure high BM25 relevance scoring.
If results are found, use memory_get or Read to retrieve the full file content at the matched path and line range.
If no results from the primary search, try broader queries:
CODEBLOCK3
Step 3: Inject and Orient
After retrieving a snapshot:
- 1. Parse the content between
<!-- BIG-MEMORY-SNAPSHOT v1 --> and <!-- /BIG-MEMORY-SNAPSHOT --> markers - Inform the user: "I detected context was compacted. Restoring task state from snapshot taken at {timestamp}..."
- Present the recovered state concisely:
-
Goal: {from Active Goal}
-
Phase: {from Current State}
-
Working on: {from Files In Play}
-
Next up: {from Next Steps}
- 4. Ask: "Does this match where we left off? Anything to update before I continue?"
- Resume work from the Next Steps section
If No Snapshot Found
If no snapshot exists in memory:
- 1. Search more broadly:
memory_search("{project name}"), memory_search("decided"), INLINECODE19 - Check
MEMORY.md for any relevant long-term notes - Tell the user: "I couldn't find a structured snapshot. Can you briefly remind me what we were working on? I'll save a snapshot this time so it won't happen again."
Multiple Snapshots
If multiple snapshots are found:
- - Use the one with the most recent timestamp (check the
<!-- timestamp: --> comment) - If the user indicates the latest is stale, check the next one back
/big-memory Command
/big-memory save
Create a snapshot now. Execute the full CAPTURE protocol:
- 1. Read
{baseDir}/references/TASK-SNAPSHOT.md for the template (first time only) - Assess the current task state
- Fill in all 8 schema sections
- Append to today's daily log (
memory/YYYY-MM-DD.md) - Confirm: "Snapshot saved to memory/{date}.md"
/big-memory recall
Search for and display the most recent snapshot:
- 1. Execute INLINECODE26
- Retrieve the full snapshot via
memory_get or INLINECODE28 - Display the recovered state (goal, phase, files, next steps)
- Ask if the user wants to resume from this state
/big-memory status
Show current snapshot information:
- 1. Search for snapshots in today's daily log
- Report: number of snapshots today, timestamp of most recent, whether the current task state has diverged from the last snapshot
- Suggest
/big-memory save if no recent snapshot exists, or /big-memory recall if context seems incomplete
/big-memory (no arguments)
Default to status. Show the current state and suggest the most useful action.
Recommended Configuration
For optimal automatic capture, add these settings to your project's openclaw.json. See {baseDir}/references/openclaw-config.md for the full configuration.
The most impactful change is replacing the default generic flush prompt:
CODEBLOCK4
This replaces the default "store durable memories now" with instructions that trigger our structured capture protocol.
Best Practices
- - Keep code snippets short. Only include lines that matter: function signatures, error-producing code, regex patterns, config values. If the code is in a committed file, reference the file path instead.
- Don't snapshot trivially. If the conversation is simple Q&A with no state to preserve, skip it. Snapshots are for complex, multi-step tasks.
- Latest snapshot wins. When multiple snapshots exist, the most recent one is the source of truth. Earlier snapshots provide history but should not override later decisions.
- Update after corrections. If the user corrects something after recovery, create a new snapshot reflecting the correction.
- Pair with MEMORY.md. Use
MEMORY.md for long-term knowledge (project architecture, conventions, preferences). Use snapshots only for transient task state that needs to survive the next compaction. - Snapshot before switching tasks. If you're about to pivot to a different part of the codebase, snapshot the current task so you can return to it.
大内存
在上下文压缩后仍能存留的结构化任务快照。三阶段协议:
- 1. 捕获 -- 在压缩清除上下文之前保存结构化任务快照
- 检测 -- 识别何时发生压缩以及上下文丢失
- 恢复 -- 在内存中搜索最新快照并精确从上次中断处继续
仅使用 OpenClaw 内置工具(memorysearch、memoryget、Read、Edit)。无需脚本、外部数据库或 API 密钥。
任务快照模式
每个快照必须遵循此精确结构。不要省略字段——如果字段为空则填写无。如需包含逐字段指南的完整模板,请阅读 {baseDir}/references/TASK-SNAPSHOT.md。
markdown
[快照] 当前目标
一句话:用户当前想要完成什么?包括为什么。
[快照] 当前状态
- - 阶段:{规划|实施|调试|测试|审查|部署}
- 分支:{git 分支名称 或 n/a}
- 受阻:{是|否} -- {如果是,描述阻碍因素}
- 进度:{大致百分比或里程碑}
[快照] 涉及文件
- - /path/to/file.ts -- 此文件中正在进行的操作
- /path/to/other.ts -- 此文件在当前任务中的用途
[快照] 已做决策
- 1. 决策描述 -- 选择此方案的理由
- 另一个决策 -- 其理由
[快照] 代码上下文
必须在压缩中存留的关键代码。仅包含无法通过读取文件重建的内容(正在设计的函数签名、正在调试的错误消息、正在复制的精确模式)。总行数控制在 50 行以内。
[快照] 关键名称与值
容易遗忘的精确标识符:
- - API 端点:POST /api/v1/users
- 表名:usersessions
- 环境变量:DATABASEURL
- 错误:ERRDUPLICATEKEY
[快照] 阻碍因素与未解决问题
- - 阻碍:带上下文的描述
- 问题:未解决的决策及考虑的选项
[快照] 后续步骤
有序、具体、可执行:
- 1. 在 /path/to/file 中完成 X 的实现,覆盖边界情况 A、B、C
- 为 Y 端点编写测试:成功(201)、重复(409)、缺少字段(400)
- 更新配置以包含 Z
HTML 注释标记( 和 )至关重要。它们充当机器可解析的分隔符,memory_search 通过 BM25 精确术语匹配进行匹配,从而实现精确检索。
何时捕获
触发条件 1:压缩前刷新(自动)
当收到与压缩相关的系统消息(包含compact、memory flush、store durable memories或nearing compaction)时,执行捕获协议而非编写通用笔记。按照上述模式创建完整的结构化快照。
触发条件 2:里程碑捕获(代理发起)
在完成重要工作单元后——实现功能、修复错误、做出架构决策——自我评估:如果现在发生压缩,我会丢失关键上下文吗?如果是,则创建快照。
适合创建快照的时机:
- - 做出影响多个文件的设计决策后
- 编写用户将来会参考的代码后
- 调试复杂问题后(捕获根本原因和修复方案)
- 在较大任务中切换子任务时
触发条件 3:用户命令(用户发起)
当用户说 /big-memory save、save snapshot、checkpoint或big-memory save时,立即创建快照。
如何存储快照
- 1. 使用 Read 读取当前每日日志 memory/{YYYY-MM-DD}.md。如果不存在,则创建它。
- 追加快照。切勿覆盖现有内容。先读取文件,然后使用 Edit 在末尾追加。或者,使用 Write 写入完整现有内容加上新快照。
- 在快照前添加水平线和标题:
markdown
任务快照 -- HH:MM
...
- 4. 同一文件中可以有多个快照。每个快照捕获一个时间点的状态。最新的快照是事实来源。
压缩后恢复协议
步骤 1:检测压缩
当以下任一情况成立时,怀疑发生压缩:
- - 对话以摘要/压缩块开始而非原始交流
- 无法回忆起本应知道的特定细节(文件路径、变量名、确切代码)
- 用户说你忘了、我们正在做、还记得吗、我刚才在做什么或类似话语
- 系统消息包含Auto-compaction complete或类似内容
步骤 2:搜索最新快照
执行此搜索:
memory_search(BIG-MEMORY-SNAPSHOT)
这会触发对所有内存文件的混合向量 + BM25 搜索。HTML 注释标记确保高 BM25 相关性评分。
如果找到结果,使用 memory_get 或 Read 检索匹配路径和行范围的完整文件内容。
如果主要搜索无结果,尝试更广泛的查询:
memory_search(SNAPSHOT Active Goal Next Steps)
memory_search({今天的日期} task snapshot)
步骤 3:注入并定位
检索到快照后:
- 1. 解析 和 标记之间的内容
- 告知用户:我检测到上下文已被压缩。正在从 {时间戳} 的快照恢复任务状态...
- 简洁呈现恢复的状态:
-
目标: {来自当前目标}
-
阶段: {来自当前状态}
-
正在处理: {来自涉及文件}
-
下一步: {来自后续步骤}
- 4. 询问:这与我们上次中断的状态一致吗?在我继续之前有什么需要更新的吗?
- 从后续步骤部分继续工作
如果未找到快照
如果内存中不存在快照:
- 1. 更广泛搜索:memorysearch({项目名称})、memorysearch(decided)、memory_search(working on)
- 检查 MEMORY.md 中是否有相关长期笔记
- 告诉用户:我找不到结构化快照。你能简要提醒我我们正在做什么吗?这次我会保存一个快照,这样就不会再发生了。
多个快照
如果找到多个快照:
- - 使用时间戳最新的那个(检查 注释)
- 如果用户表示最新的已过时,检查前一个
/big-memory 命令
/big-memory save
立即创建快照。执行完整的捕获协议:
- 1. 读取 {baseDir}/references/TASK-SNAPSHOT.md 获取模板(仅首次)
- 评估当前任务状态
- 填写所有 8 个模式部分
- 追加到今天的每日日志(memory/YYYY-MM-DD.md)
- 确认:快照已保存到 memory/{date}.md
/big-memory recall
搜索并显示最近的快照:
- 1. 执行 memorysearch(BIG-MEMORY-SNAPSHOT)
- 通过 memoryget 或 Read 检索完整快照
- 显示恢复的状态(目标、阶段、文件、后续步骤)
- 询问用户是否要从该状态继续
/big-memory status
显示当前快照信息:
- 1. 搜索今天每日日志中的快照
- 报告:今天的快照数量、最近快照的时间戳、当前任务状态是否与上次快照不同
- 如果没有最近的快照,建议 /big-memory save;如果上下文似乎不完整,建议 /big-memory recall
/big-memory(无参数)
默认为 status。显示当前状态并建议最有用的操作。
推荐配置
为获得最佳自动捕获效果,将这些设置添加到项目的 openclaw.json 中。完整配置请参见 {baseDir}/references/openclaw-config.md。
最有影响力的更改是替换默认的通用刷新提示:
json
{
agents: {
defaults: {
compaction: {
memoryFlush: {
enabled: true,
softThresholdTokens: 40000,
systemPrompt: 您正在捕获结构化任务状态以供压缩后恢复。请严格遵循 BIG-MEM