Memory Stack Core
Transforms your agent's memory from fragile to antifragile. Implements proven patterns from the Claude Code leak and ClawHub's compaction-survival + session-persistence.
The Problem
LLM context windows fill up. When compaction happens, older messages get summarized. Summaries lose precision:
- - Exact file paths → "some file"
- Specific numbers → "approximately 42"
- Decisions → "we decided to do something"
- Preferences → forgotten
Your agent wakes up after compaction dumber. Every session restarts from scratch.
The Solution: Three-Layer Memory Stack
CODEBLOCK0
WAL (Write-Ahead Log)
When: Immediately upon receiving a human message that contains any:
- - Corrections ("Actually it's X not Y")
- Proper nouns (names, places, products)
- Preferences ("I prefer...")
- Decisions ("Let's do X")
- Draft changes (edits to active work)
- Specific values (numbers, dates, IDs, URLs, paths)
What: Write a structured JSON line to memory/wal.jsonl with:
CODEBLOCK1
Why: WAL entries are tiny, numerous, and survive forever. They're the source of truth for specifics.
Working Buffer
When: Token utilization reaches 60% (tracked via session_status).
What: Append every human + assistant exchange (full text) to memory/working-buffer.md:
CODEBLOCK2
Why: The buffer is a file, so it doesn't count against context. It's your safety net for the danger zone. When compaction inevitably happens, you can recover from the buffer.
Daily Logs & Long-term
Already in OpenClaw. We integrate by:
- - At 80% context, suggest
/wrap_up to flush to daily log - Periodic (weekly) review to promote daily log entries to INLINECODE6
Usage
Automatic Mode (recommended)
The skill hooks into your agent's message processing:
- 1. Install skill
- Enable WAL and buffer in agent config (or use defaults)
- Nothing else — the skill automatically:
- Scans human messages for specifics → WAL
- Monitors token usage → activates buffer at 60%
- Provides
/memory_health command to view status
Manual Commands
- -
tool("memory-stack-core", "wal_write", {...}) — manually add WAL entry - INLINECODE9 — view recent WAL
- INLINECODE10 — view buffer tail
- INLINECODE11 — get health report
Recovery Protocol
When context is lost (e.g., after compaction or new session):
- 1. Read
memory/working-buffer.md last entries - Read recent WAL entries (last 50)
- Read yesterday's + today's INLINECODE13
- Reconstruct missing specifics
The skill provides a recover() helper (used automatically by agent if configured).
Configuration
Create memory-stack-config.json in workspace root (optional):
CODEBLOCK3
Performance
- - WAL write: <1ms (append to file)
- Buffer append: <1ms
- Memory overhead: ~100B per WAL entry; ~1KB per buffer turn
- Disk: WAL grows ~1-2KB per conversation; buffer ~5-10KB per session
Negligible impact.
Compatibility
- - Works with any OpenClaw agent (uses standard
tool interface) - No external dependencies
- Compatible with
compaction-survival patterns (this is an implementation) - Enhances
session-persistence by providing WAL + buffer layers
FAQ
Q: Do I need to change my agent?
A: Only to optionally call memory_health or recover if you want explicit control. Otherwise install and go.
Q: What if I already use session-persistence?
A: This skill implements the WAL + buffer layers that session-persistence mentions. They're complementary.
Q: Will WAL fill my disk?
A: WAL is capped at max_entries (default 10k). Old entries can be archived to memory/wal-archive.jsonl monthly.
Q: Can I use without ToolRegistry?
A: Yes, the skill provides standalone scripts too (scripts/wal.py, scripts/buffer.py).
License
Commercial. One-time purchase includes lifetime updates. Team licenses allow unlimited agents.
Built with insights from the Claude Code leak and ClawHub community.
记忆栈核心
将您的智能体记忆从脆弱转变为反脆弱。实现了来自Claude Code泄露和ClawHub的compaction-survival + session-persistence的成熟模式。
问题所在
大语言模型上下文窗口会填满。当压缩发生时,较旧的消息会被总结。总结会丢失精确性:
- - 确切文件路径 → 某个文件
- 具体数字 → 大约42
- 决策 → 我们决定做某事
- 偏好 → 被遗忘
您的智能体在压缩后醒来变得更笨。每个会话都从头开始。
解决方案:三层记忆栈
┌────────────────────────────────────────┐
│ 长期记忆 (MEMORY.md) │ ← 精心整理的知识,永不手动编辑
├────────────────────────────────────────┤
│ 每日日志 (memory/YYYY-MM-DD.md) │ ← 对话摘要
├────────────────────────────────────────┤
│ 工作缓冲区 (memory/working-buffer.md) │ ← 危险区域捕获(60%+上下文)
├────────────────────────────────────────┤
│ WAL (memory/wal.jsonl) │ ← 预写日志:具体信息即时记录
└────────────────────────────────────────┘
WAL(预写日志)
时机: 收到包含以下任何内容的人类消息时立即执行:
- - 更正(实际上是X不是Y)
- 专有名词(姓名、地点、产品)
- 偏好(我更喜欢...)
- 决策(我们做X吧)
- 草稿变更(对活跃工作的编辑)
- 具体数值(数字、日期、ID、URL、路径)
内容: 将结构化JSON行写入memory/wal.jsonl:
json
{
timestamp: 2026-04-01T16:20:00Z,
category: decision|preference|path|value|correction|draft,
content: 具体细节,
context: 周围消息片段
}
原因: WAL条目体积小、数量多、永久保存。它们是具体信息的真实来源。
工作缓冲区
时机: Token利用率达到60%(通过session_status跟踪)。
内容: 将每次人类+智能体的交流(完整文本)追加到memory/working-buffer.md:
markdown
2026-04-01 16:25:00 (第47轮)
用户:
<消息>
智能体:
<响应>
原因: 缓冲区是一个文件,因此不计入上下文。它是危险区域的安全网。当压缩不可避免发生时,您可以从缓冲区恢复。
每日日志与长期记忆
已在OpenClaw中实现。我们通过以下方式集成:
- - 在上下文达到80%时,建议使用/wrap_up刷新到每日日志
- 定期(每周)审查,将每日日志条目提升到MEMORY.md
使用方式
自动模式(推荐)
该技能会挂接到智能体的消息处理流程:
- 1. 安装技能
- 在智能体配置中启用WAL和缓冲区(或使用默认值)
- 无需其他操作——技能会自动:
- 扫描人类消息中的具体信息 → WAL
- 监控Token使用情况 → 在60%时激活缓冲区
- 提供/memory_health命令查看状态
手动命令
- - tool(memory-stack-core, walwrite, {...}) — 手动添加WAL条目
- tool(memory-stack-core, walread, {limit: 50}) — 查看最近的WAL
- tool(memory-stack-core, bufferread, {tail: 1000}) — 查看缓冲区尾部
- tool(memory-stack-core, memoryhealth, {}) — 获取健康报告
恢复协议
当上下文丢失时(例如压缩后或新会话):
- 1. 读取memory/working-buffer.md的最后条目
- 读取最近的WAL条目(最近50条)
- 读取昨天和今天的memory/YYYY-MM-DD.md
- 重建丢失的具体信息
该技能提供一个recover()辅助函数(如果配置,智能体会自动使用)。
配置
在工作区根目录创建memory-stack-config.json(可选):
json
{
wal: {
enabled: true,
auto_capture: true,
max_entries: 10000
},
buffer: {
enabled: true,
thresholdtokenpercent: 60,
maxsizemb: 10
},
integration: {
autowrapupattoken_percent: 80,
includebufferinwrapup: true
}
}
性能
- - WAL写入:<1ms(追加到文件)
- 缓冲区追加:<1ms
- 内存开销:每条WAL条目约100B;每轮缓冲区约1KB
- 磁盘:每次对话WAL增长约1-2KB;每次会话缓冲区约5-10KB
影响可忽略不计。
兼容性
- - 适用于任何OpenClaw智能体(使用标准tool接口)
- 无外部依赖
- 兼容compaction-survival模式(这是其实现)
- 通过提供WAL+缓冲区层增强session-persistence
常见问题
问:我需要修改智能体吗?
答:仅当您想要显式控制时才需要调用memory_health或recover。否则安装即可使用。
问:如果我已经在使用session-persistence呢?
答:该技能实现了session-persistence提到的WAL+缓冲区层。它们是互补的。
问:WAL会填满我的磁盘吗?
答:WAL有max_entries上限(默认1万条)。旧条目可以每月归档到memory/wal-archive.jsonl。
问:没有ToolRegistry可以使用吗?
答:可以,该技能也提供独立脚本(scripts/wal.py、scripts/buffer.py)。
许可证
商业许可。一次性购买包含终身更新。团队许可允许无限智能体。
基于Claude Code泄露和ClawHub社区的见解构建。