Claude Memory Optimizer
Structured memory system for OpenClaw with 4-type classification and automated migration.
When to Use
- - Setting up memory for the first time in OpenClaw
- Migrating from unstructured
memory/*.md to organized categories - Improving memory recall with semantic frontmatter
- Implementing Claude Code-style memory architecture
Features
- - 4-Type Classification: user, feedback, project, reference
- Frontmatter Metadata: structured name/description/type for semantic search
- Auto-Migration: one-command refactor of existing memory files
- Log Mode: optional append-only daily logs (KAIROS style)
Quick Start
Install
CODEBLOCK0
Run Migration
CODEBLOCK1
Verify
CODEBLOCK2
Memory Types
| Type | Purpose | Example |
|---|
| user | User role, preferences, skills | "Data scientist, prefers concise replies" |
| feedback |
Behavior corrections/confirmations | "No trailing summaries — user can read diffs" |
|
project | Project context, decisions, deadlines | "Thesis deadline: 2026-06-01" |
|
reference | External system pointers | "Kaggle: https://kaggle.com/chenziong" |
Directory Structure
CODEBLOCK3
Memory File Format
Each memory file uses frontmatter metadata:
CODEBLOCK4
What NOT to Save
- - Code patterns, architecture, file paths (derivable from codebase)
- Git history, recent changes (use
git log) - Debugging solutions (fix is in the code)
- Content already in CLAUDE.md
- Ephemeral task details (only useful in current session)
Configuration
OpenClaw Config
CODEBLOCK5
Usage Examples
Save User Preference
User: "Remember, I prefer concise replies without trailing summaries."
AI: Saves to memory/feedback/reply-style.md:
CODEBLOCK6
Retrieve Memory
User: "What did I say about database testing?"
AI: Runs memory_search query="database testing" → returns INLINECODE4
Verify Memory
User: "Is the experiment design in memory/project/dong-thesis.md still current?"
AI: Runs grep to verify → detects outdated info → updates memory file.
Migration Guide
Before
CODEBLOCK7
After
CODEBLOCK8
Advanced Features
Semantic Retrieval (Future)
CODEBLOCK9
Verification on Recall (Future)
Before recommending from memory:
- 1. If memory names a file →
ls to verify existence - If memory names a function →
grep to confirm - If memory conflicts with current state → trust current observation, update memory
"Memory says X exists" ≠ "X exists now"
Maintenance
Daily (Heartbeat)
- - Append to INLINECODE8
- Record decisions, conversations, learnings
Weekly (Review)
- - Read daily notes
- Distill important info to INLINECODE9
- Remove outdated entries
Monthly (Audit)
- - Review project progress
- Update long-term goals
- Check
.learnings/ records
Troubleshooting
Memory Not Loaded
- - Ensure
MEMORY.md exists in workspace root - Check INLINECODE12
- Restart OpenClaw gateway
Poor Recall Quality
- - Add specific
description in frontmatter - Use consistent keywords
- Adjust
minScore (lower = broader matches)
Migration Fails
- - Backup
memory/ directory first - Run script with
--dry-run (if available) - Check file permissions
References
- - Claude Code:
src/memdir/ (memdir.ts, memoryTypes.ts, findRelevantMemories.ts) - OpenClaw Docs: INLINECODE18
- Related Skills:
memory-setup-openclaw, INLINECODE20
License
MIT-0
License
MIT-0
Claude Memory Optimizer
针对OpenClaw的结构化记忆系统,支持4类分类与自动迁移。
使用时机
- - 在OpenClaw中首次设置记忆系统
- 从非结构化的memory/*.md迁移至分类组织
- 通过语义化前置元数据提升记忆召回效果
- 实现Claude Code风格记忆架构
功能特性
- - 4类分类:用户、反馈、项目、参考
- 前置元数据:结构化的名称/描述/类型,支持语义搜索
- 自动迁移:一键重构现有记忆文件
- 日志模式:可选的仅追加每日日志(KAIROS风格)
快速开始
安装
bash
clawhub install claude-memory-optimizer
运行迁移
bash
自动检测工作区
node ~/.openclaw/skills/claude-memory-optimizer/scripts/refactor-memory.js
或显式指定
node ~/.openclaw/skills/claude-memory-optimizer/scripts/refactor-memory.js ~/.openclaw/workspace
验证
bash
ls -la ~/.openclaw/workspace/memory/
cat ~/.openclaw/workspace/MEMORY.md
记忆类型
| 类型 | 用途 | 示例 |
|---|
| 用户 | 用户角色、偏好、技能 | 数据科学家,偏好简洁回复 |
| 反馈 |
行为纠正/确认 | 不要结尾总结——用户能自己看差异 |
|
项目 | 项目上下文、决策、截止日期 | 论文截止日期:2026-06-01 |
|
参考 | 外部系统指针 | Kaggle:https://kaggle.com/chenziong |
目录结构
memory/
├── user/ # 用户信息
├── feedback/ # 行为指导
├── project/ # 项目上下文
├── reference/ # 外部参考
└── logs/ # 仅追加日志(可选)
└── YYYY/
└── MM/
└── YYYY-MM-DD.md
记忆文件格式
每个记忆文件使用前置元数据:
markdown
name: 数据科学背景
description: 用户是专注于可观测性和LLM的数据科学家
type: user
用户就读于北京工业大学和都柏林大学学院,GPA 3.95/4.2。
研究方向:LLM、AI Agent、MCP。
技能: Python、PyTorch、Transformers、NLP
应用方式: 使用数据科学术语,假设具备机器学习背景。
不应保存的内容
- - 代码模式、架构、文件路径(可从代码库推导)
- Git历史、近期变更(使用git log)
- 调试解决方案(修复已在代码中)
- CLAUDE.md中已有的内容
- 临时任务细节(仅当前会话有用)
配置
OpenClaw配置
json
{
agents: {
defaults: {
memorySearch: {
enabled: true,
provider: local,
maxResults: 20,
minScore: 0.3
},
compaction: {
memoryFlush: {
enabled: true,
softThresholdTokens: 4000
}
}
}
}
}
使用示例
保存用户偏好
用户: 记住,我偏好简洁回复,不要结尾总结。
AI: 保存至memory/feedback/reply-style.md:
markdown
name: 回复风格偏好
description: 用户希望简洁回复,不要结尾总结
type: feedback
规则: 保持回复简洁,不要结尾总结。
原因: 用户表示我自己能看差异。
应用方式: 完成工作后直接结束回复。
检索记忆
用户: 我之前关于数据库测试说了什么?
AI: 运行memory_search query=数据库测试 → 返回memory/feedback/db-testing.md
验证记忆
用户: memory/project/dong-thesis.md中的实验设计是否仍然有效?
AI: 运行grep验证 → 检测到过时信息 → 更新记忆文件。
迁移指南
迁移前
memory/
├── 2026-03-21.md
├── 2026-03-28.md
├── research-memory.md
└── video-memory.md
迁移后
memory/
├── project/
│ ├── 2026-03-21-.md
│ ├── 2026-03-28-.md
│ └── research-memory.md
├── reference/
│ └── video-memory.md
└── logs/2026/04/2026-04-02.md
高级功能
语义检索(未来)
typescript
async function findRelevantMemories(query: string, memoryDir: string) {
const memories = await scanMemoryFiles(memoryDir);
const selected = await selectRelevantMemories(query, memories);
return selected.slice(0, 5); // 返回前5条相关记忆
}
召回时验证(未来)
在从记忆中推荐前:
- 1. 如果记忆提及文件 → 使用ls验证文件是否存在
- 如果记忆提及函数 → 使用grep确认
- 如果记忆与当前状态冲突 → 信任当前观察,更新记忆
记忆说X存在 ≠ X现在存在
维护
每日(心跳)
- - 追加至memory/YYYY-MM-DD.md
- 记录决策、对话、学习内容
每周(回顾)
- - 阅读每日笔记
- 将重要信息提炼至MEMORY.md
- 移除过时条目
每月(审计)
- - 审查项目进度
- 更新长期目标
- 检查.learnings/记录
故障排除
记忆未加载
- - 确保工作区根目录存在MEMORY.md
- 检查agents.defaults.memorySearch.enabled = true
- 重启OpenClaw网关
召回质量差
- - 在前置元数据中添加具体的description
- 使用一致的关键词
- 调整minScore(值越低匹配范围越广)
迁移失败
- - 先备份memory/目录
- 使用--dry-run运行脚本(如可用)
- 检查文件权限
参考
- - Claude Code:src/memdir/(memdir.ts、memoryTypes.ts、findRelevantMemories.ts)
- OpenClaw文档:docs/concepts/memory.md
- 相关技能:memory-setup-openclaw、elite-longterm-memory
许可证
MIT-0
许可证
MIT-0