Memory Optimization Skill
Quickly implement a comprehensive memory management system for AI agents based on Moltbook community best practices.
When to Use This Skill
- - Context compression causes memory loss between sessions
- Need fast context recovery (currently 5-10 minutes, target <30 seconds)
- Want structured project tracking with clear separation of concerns
- Need automated daily memory maintenance
- Building knowledge graph for entity relationships
- Migrating from simple file-based memory to advanced system
What This Skill Provides
- 1. TL;DR Summary System - 30-second context recovery
- Three-File Pattern - Structured project tracking
- Fixed Tags System - Quick grep search capability
- Daily Cleanup Script - 3-minute automated maintenance
- HEARTBEAT Integration - Mandatory memory checklist
- Rolling Summary Template - Concise daily summaries
- Testing Framework - 6 automated tests
- Knowledge Graph - 18 entities, 15 relationships
- Skill Usage Tracker - Track and analyze skill usage patterns
Quick Start
TL;DR Summary System
Add to each daily log (memory/YYYY-MM-DD.md):
CODEBLOCK0
Three-File Pattern
For complex projects, create:
- -
memory/task_plan.md - What to do (goals, phases, decisions) - INLINECODE1 - What discovered (research, key info)
- INLINECODE2 - What done (timeline, errors)
Fixed Tags
Use consistent tags across files:
- -
#memory - Memory-related content - INLINECODE4 - Important decisions
- INLINECODE5 - Optimization work
- INLINECODE6 - Daily log entries
Daily Cleanup
Run automated cleanup:
CODEBLOCK1
HEARTBEAT Integration
Add to HEARTBEAT.md:
CODEBLOCK2
Scripts
See scripts/README.md for detailed usage:
- -
daily-cleanup.sh - 3-minute daily memory maintenance - INLINECODE8 - Verify all improvements working
- INLINECODE9 - Knowledge Graph management tool (with Phase 6 retrieve command)
- INLINECODE10 - Fix entities missing type field by inferring from ID prefix
- INLINECODE11 - KG extraction from agent sessions (LLM-driven)
- INLINECODE12 - Phase 8: Write-time gating evaluation
- INLINECODE13 - Cold storage management (Phase 8)
- INLINECODE14 - Phase 2: User preference inference from conversation history
- INLINECODE15 - Phase 3: consolidate similar episodes into SkillCards
- INLINECODE16 - Batch decay engine for memory strength management
- INLINECODE17 - Phase 2: Embedding-based entity deduplication and merging
- INLINECODE18 - Phase 5: Context Window layered compression (3 levels)
- INLINECODE19 - Phase 6: Proactive memory recovery (3-stage staged loading)
- INLINECODE20 - Phase 7: Memory health dashboard (Health Score, decay forecast)
Phase 6: Value-Aware Retrieval
KG entities can be retrieved by value score (weighted combination of reliability, strength, significance, preference match, recency):
CODEBLOCK3
References
See reference files for detailed guidance:
Key Metrics
| Metric | Before | After | Improvement |
|---|
| Context Recovery | 5-10 min | 30 sec | -98% |
| File Size |
2000+ tokens | 1.3KB | -99% |
| Automation | Manual | 3-min script | +100% |
| Tests | None | 6/6 pass | +100% |
Key Insights from Moltbook
"Forget is a survival mechanism" - Compression forces distillation of experience into most resilient forms
"Knowledge graph is an index for your brain" - Query efficiency 10x better than grep
"Record immediately, not wait" - Details fade quickly
"Focus on why, not what" - Rationale is more important than the fact
File Structure
CODEBLOCK4
Skill Usage Tracker
Track and analyze skill usage patterns via Knowledge Graph:
CODEBLOCK5
Features:
- - Record skill invocations with status, duration, and error info
- Automatic skill categorization (feishu/apple/coding/memory/api/system/utility)
- Session history scanning for automatic usage detection
- KG-backed storage with fallback to file
Usage Examples
Create New Daily Log with TL;DR
CODEBLOCK6
Use Knowledge Graph
CODEBLOCK7
Environment Variables
CODEBLOCK8
KG Sharing Across Agents
Multi-agent setups can share a single Knowledge Graph for collaborative memory.
Setup
CODEBLOCK9
Usage by Agents
Each agent should reference the shared KG in their TOOLS.md:
CODEBLOCK10
Script Usage with Shared KG
CODEBLOCK11
Benefits:
- - All agents access the same entity pool
- Decisions and lessons are shared across agents
- No duplicate entity creation
OpenClaw Skill Invocation
When the user types /xmo, OpenClaw automatically invokes this memory-optimization skill.
Add the following to OpenClaw's settings.json or skills.json:
CODEBLOCK12
After configuration, the user can activate this skill by typing /xmo.
Next Steps
- 1. Run test script: INLINECODE26
- Verify TL;DR exists in today's log
- Start using KG for important decisions
- Run daily cleanup each day
For complete implementation details, see references/implementation.md.
内存优化技能
基于Moltbook社区最佳实践,快速为AI代理实现全面的内存管理系统。
何时使用此技能
- - 上下文压缩导致会话间内存丢失
- 需要快速上下文恢复(当前5-10分钟,目标<30秒)
- 希望以清晰的关注点分离进行结构化项目跟踪
- 需要自动化的每日内存维护
- 构建知识图谱以管理实体关系
- 从简单的基于文件的内存迁移到高级系统
此技能提供的内容
- 1. TL;DR摘要系统 - 30秒上下文恢复
- 三文件模式 - 结构化项目跟踪
- 固定标签系统 - 快速grep搜索能力
- 每日清理脚本 - 3分钟自动化维护
- HEARTBEAT集成 - 强制内存检查清单
- 滚动摘要模板 - 简洁的每日摘要
- 测试框架 - 6个自动化测试
- 知识图谱 - 18个实体,15个关系
- 技能使用追踪器 - 追踪和分析技能使用模式
快速开始
TL;DR摘要系统
添加到每个每日日志(memory/YYYY-MM-DD.md):
markdown
⚡ TL;DR摘要
核心成就:
今日要点:
决策:今日做出的重要决策
三文件模式
对于复杂项目,创建:
- - memory/task_plan.md - 待办事项(目标、阶段、决策)
- memory/findings.md - 发现内容(研究、关键信息)
- memory/progress.md - 完成内容(时间线、错误)
固定标签
在文件中使用一致的标签:
- - #memory - 内存相关内容
- #decision - 重要决策
- #improvement - 优化工作
- #daily-log - 每日日志条目
每日清理
运行自动化清理:
bash
./memory/daily-cleanup.sh
HEARTBEAT集成
添加到HEARTBEAT.md:
markdown
🧠 内存管理检查清单
每次会话开始:
- - [ ] 读取SOUL.md(代理身份)
- [ ] 读取USER.md(用户偏好)
- [ ] 读取memory/YYYY-MM-DD.md(今天+昨天)
- [ ] 读取MEMORY.md(长期内存)
脚本
详见scripts/README.md:
- - daily-cleanup.sh - 3分钟每日内存维护
- test-memory-system.sh - 验证所有改进是否正常工作
- memoryontology.py - 知识图谱管理工具(含Phase 6检索命令)
- kgtypefixer.py - 通过ID前缀推断修复缺失类型字段的实体
- kgextractor.py - 从代理会话中提取知识图谱(LLM驱动)
- writetimegating.py - Phase 8:写入时间门控评估
- archivedmemorystore.py - 冷存储管理(Phase 8)
- preferenceengine.py - Phase 2:从对话历史推断用户偏好
- consolidationengine.py - Phase 3:将相似片段合并为SkillCard
- decayengine.py - 内存强度管理的批量衰减引擎
- entitydedup.py - Phase 2:基于嵌入的实体去重与合并
- workingmemory.py - Phase 5:上下文窗口分层压缩(3级)
- memoryloader.py - Phase 6:主动内存恢复(3阶段分步加载)
- memory_dashboard.py - Phase 7:内存健康仪表盘(健康评分、衰减预测)
Phase 6:价值感知检索
知识图谱实体可按价值评分检索(可靠性、强度、重要性、偏好匹配、时效性的加权组合):
bash
按价值评分排序检索实体
python3 scripts/memory_ontology.py retrieve --types Decision Finding --min-score 0.4
按文本查询并按评分过滤
python3 scripts/memory_ontology.py retrieve --query 架构 --show-scores
CLI帮助
python3 scripts/memory_ontology.py retrieve --help
参考资料
详见参考文件:
关键指标
| 指标 | 之前 | 之后 | 改进 |
|---|
| 上下文恢复 | 5-10分钟 | 30秒 | -98% |
| 文件大小 |
2000+ tokens | 1.3KB | -99% |
| 自动化 | 手动 | 3分钟脚本 | +100% |
| 测试 | 无 | 6/6通过 | +100% |
Moltbook关键见解
遗忘是一种生存机制 - 压缩迫使经验蒸馏为最具韧性的形式
知识图谱是你大脑的索引 - 查询效率比grep高10倍
立即记录,不要等待 - 细节会迅速消失
关注为什么,而不是什么 - 理由比事实更重要
文件结构
memory/
├── YYYY-MM-DD.md # 带TL;DR的每日日志
├── task_plan.md # 任务规划
├── findings.md # 研究发现
├── progress.md # 进度跟踪
├── rolling-summary-template.md
├── daily-cleanup.sh
├── test-memory-system.sh
└── ontology/
├── memory-schema.yaml
├── entity-templates.md
├── INTEGRATION.md
└── graph.jsonl
scripts/
└── memory_ontology.py
技能使用追踪器
通过知识图谱追踪和分析技能使用模式:
bash
记录技能使用
python3 scripts/skill_tracker.py record --skill coding-agent --status success --duration 2.5
查看使用统计
python3 scripts/skill_tracker.py stats
扫描会话历史并保存到知识图谱
python3 scripts/skill_tracker.py scan --save
功能:
- - 记录技能调用,包含状态、时长和错误信息
- 自动技能分类(飞书/苹果/编码/内存/API/系统/工具)
- 会话历史扫描,自动检测使用情况
- 基于知识图谱的存储,支持文件回退
使用示例
创建带TL;DR的新每日日志
markdown
每日内存 - 2026-03-13
⚡ TL;DR摘要
核心成就:
今日要点:
决策:选择了方案Z
使用知识图谱
bash
创建决策实体
python3 scripts/memory
ontology.py create --type Decision --props {title:...,rationale:...,madeat:...,confidence:0.9,tags:[#decision]}
按标签查询
python3 scripts/memory_ontology.py query --tags #memory #decision
获取相关实体
python3 scripts/memory
ontology.py related --id decxxx
环境变量
bash
GLM API配置(由kg_extractor.py使用)
export OPENAI
APIKEY=your-glm-token
export OPENAI
BASEURL=https://open.bigmodel.cn/api/paas/v4
export OPENAI_MODEL=glm-5
全局知识图谱路径(可选,默认为~/.openclaw/workspace/memory/ontology)
通过KG_DIR环境变量配置
跨代理知识图谱共享
多代理设置可共享单个知识图谱以实现协作内存。
设置
bash
1. 创建共享目录
mkdir -p ~/.openclaw/shared-kg
2. 创建指向主知识图谱的符号链接
ln -sf ~/.openclaw/workspace/memory/ontology/graph.jsonl ~/.openclaw/shared-kg/main-kg.jsonl
代理使用
每个代理应在各自的TOOLS.md中引用共享知识图谱:
markdown
知识图谱(KG)
- - 共享KG: ~/.openclaw/shared-kg/main-kg.jsonl
- 包含所有重要的长期记忆、决策、经验教训
使用共享知识图谱的脚本
bash
直接指定KG路径
KG
DIR=~/.openclaw/shared-kg python3 scripts/memoryontology.py query --tags #decision
或在.env中设置
KG_DIR=~/.openclaw/shared-kg