Memory Oracle
A structured, self-maintaining memory system for OpenClaw agents.
Why this exists
OpenClaw's built-in memory is markdown files + LLM discretion. This means:
- - The agent decides whether to save (often doesn't)
- The agent decides whether to search (often doesn't)
- Compaction silently destroys chat-only instructions
- MEMORY.md becomes a dumping ground with no cleanup
- No structured search, no decay, no dedup
Memory Oracle fixes all of these with zero external dependencies (Python stdlib + SQLite).
Architecture
Two processes, mirroring the HEAVY/LIGHT pattern:
LIGHT process (every turn, zero tokens, zero API calls)
- 1. capture.py — Rule-based extraction runs AFTER each agent response.
Parses the conversation turn for facts, decisions, preferences, tasks
using bilingual (RU+EN) pattern matching. Writes to SQLite.
If a fact's content hash already exists → bumps
access_count instead.
- 2. recall.py — Three-slot retrieval runs BEFORE each agent response.
Injects relevant context from SQLite using a budget of ~2000 tokens:
- Slot 1 (10%): Guardrails — always present, immune to decay
- Slot 2 (30%): Fresh facts from last 24h by importance
- Slot 3 (60%): FTS5 search ranked by INLINECODE1
- 3. checkpoint.py — Emergency save triggered by OpenClaw's pre-compaction hook.
Dumps all hot-tier context to SQLite with
source = "checkpoint" tag.
HEAVY process (cron, uses Claude API)
Runs nightly at 03:00 agent-local time:
- 1. consolidate.py — Sends today's daily log to Claude API (~2K tokens).
Extracts structured facts that rule-based capture missed.
- 2. reflect.py — Analyzes the day's facts against MEMORY.md and prior reflections.
- Light mode (daily): contradictions, new topics, priorities, stale candidates
- Deep mode (weekly, Mondays): 7-day trend analysis, pattern detection, strategic rebalancing
Outputs
reflection.json with score modifications +
YYYY-MM-DD-reflection.md digest.
- 3. maintenance.py — Applies decay, prunes dead facts, archives cold tier,
re-renders MEMORY.md from top SQLite facts, vacuums DB.
Agent protocol
These rules go into your AGENTS.md or equivalent config:
CODEBLOCK0
Installation
CODEBLOCK1
The installer will:
- 1. Initialize SQLite database with FTS5 index
- Import existing MEMORY.md and daily logs (if present)
- Prompt you to optionally set up cron jobs for the HEAVY process
- Print a snippet to paste into your AGENTS.md
- Print a snippet to paste into your OpenClaw compaction config
Note: The installer does NOT auto-edit your AGENTS.md or OpenClaw config.
You review and paste the snippets yourself.
Configuration
All thresholds live in config/settings.json. Key tunables:
- -
recall_budget_tokens: Total injection budget (default: 2000) - INLINECODE7 : Daily score decay multiplier (default: 0.05)
- INLINECODE8 : Score below which facts move to cold (default: 0.2)
- INLINECODE9 : Score below which cold facts are purged (default: 0.05)
- INLINECODE10 : Minimum age before deletion (default: 90)
- INLINECODE11 : Day of week for deep reflection, 0=Mon (default: 0)
- INLINECODE12 : Model for HEAVY process (default: claude-sonnet-4-20250514)
- INLINECODE13 : Max response tokens for LLM calls (default: 1000)
Uninstall / rollback
CODEBLOCK2
The uninstaller will:
- 1. Export full memory state to JSON (for re-import if you reinstall)
- Remove cron jobs (with confirmation)
- Restore original MEMORY.md from the backup created during install
- Optionally delete the SQLite database (asks with fact count shown)
- Clean up reflection files and pending queue
- Print remaining manual steps (AGENTS.md cleanup, compaction config)
Use --force to skip confirmations, --keep-db to keep the database while removing everything else.
Known issues & limitations
FTS5 not available on some systems.
Minimal containers (Alpine) and old Debian/Ubuntu may lack FTS5. install.sh checks
for this and prints fix instructions. Most systems with Python 3.8+ include FTS5.
Conflict with other memory skills.
If you have memory-complete, continuity, or agent-brain installed, they may
compete for MEMORY.md writes. Disable other memory plugins before installing:
plugins.slots.memory = "none" in your OpenClaw config, or remove conflicting skills.
Cron unavailable in containers.
Docker, sandboxed VPS, and some hosting environments block crontab.
install.sh handles this gracefully — just run the HEAVY pipeline manually or
via your own scheduler (systemd timer, supervisor, etc.):
CODEBLOCK3
Large existing MEMORY.md (>50KB).
Import during init_db.py works but may create noisy low-confidence facts.
After install, run python3 scripts/maintenance.py --stats and check the
fact count. If too high, run python3 scripts/maintenance.py to let decay
and pruning clean up naturally over a few days.
Rule-based capture misses implicit facts.
Capture.py uses pattern matching — it catches ~70% of facts. The remaining 30%
are caught by consolidate.py (HEAVY process) with LLM extraction. Without
ANTHROPICAPIKEY, only rule-based capture works.
File structure
CODEBLOCK4
Graceful degradation
Every component is designed to fail safely:
- - SQLite corrupted → recall falls back to grep over MEMORY.md
- API unreachable → consolidate/reflect queue to
pending_queue.json, LIGHT continues - Cron missed → next run processes all accumulated data
- Bad LLM output → stored in
failed_reflections/, scores unchanged - Low confidence reflection → stored but not applied, re-evaluated next cycle
Privacy & data flow
What stays local (always):
- - SQLite database with all facts, scores, and access logs
- MEMORY.md, daily logs, SESSION-STATE.md
- Rule-based capture and recall (LIGHT process) — zero network calls
What is sent to Anthropic API (HEAVY process only):
- -
consolidate.py sends the text of today's daily log to Claude API for fact extraction - INLINECODE22 sends extracted facts (not raw logs) and current memory state for analysis
- Both require
ANTHROPIC_API_KEY env var to be set - If the API key is not set, HEAVY process is skipped entirely — LIGHT works alone
If you are not comfortable sending daily logs to an external LLM:
- - Set
api.model to a local model endpoint in INLINECODE25 - Or simply don't set
ANTHROPIC_API_KEY — the skill runs in LIGHT-only mode
with rule-based capture and full recall, just without LLM-powered consolidation
and reflection
Other privacy rules:
- - MEMORY.md and recall output are NEVER loaded in group contexts
- Guardrails table is private-session only
- INLINECODE27 dumps full state to JSON for backup/migration
Author
记忆神谕
一个为OpenClaw智能体设计的结构化、自维护记忆系统。
为何存在
OpenClaw内置的记忆系统是markdown文件+LLM自由裁量。这意味着:
- - 智能体决定是否保存(通常不保存)
- 智能体决定是否搜索(通常不搜索)
- 压缩操作会静默销毁仅存在于对话中的指令
- MEMORY.md成为无人清理的垃圾场
- 没有结构化搜索、没有衰减、没有去重
记忆神谕以零外部依赖(Python标准库+SQLite)解决了所有这些问题。
架构
两个进程,镜像HEAVY/LIGHT模式:
LIGHT进程(每轮执行,零token消耗,零API调用)
- 1. capture.py — 基于规则的提取,在每次智能体响应后执行。
解析对话轮次中的事实、决策、偏好、任务,使用双语(RU+EN)模式匹配。写入SQLite。
如果某个事实的内容哈希已存在 → 增加access_count而非重复写入。
- 2. recall.py — 三槽位检索,在每次智能体响应前执行。
使用约2000 token的预算从SQLite注入相关上下文:
- 槽位1(10%):护栏规则 — 始终存在,不受衰减影响
- 槽位2(30%):过去24小时内的新事实,按重要性排序
- 槽位3(60%):FTS5搜索,按分数 = 重要性 × 新近度 × 访问增益排序
- 3. checkpoint.py — 由OpenClaw的预压缩钩子触发的紧急保存。
将所有热层级上下文转储到SQLite,标记source = checkpoint。
HEAVY进程(定时任务,使用Claude API)
在智能体本地时间每天凌晨03:00运行:
- 1. consolidate.py — 将今天的每日日志发送给Claude API(约2K token)。
提取基于规则捕获遗漏的结构化事实。
- 2. reflect.py — 对照MEMORY.md和之前的反思分析当天的事实。
- 轻量模式(每日):矛盾、新主题、优先级、待淘汰候选
- 深度模式(每周一):7天趋势分析、模式检测、战略再平衡
输出reflection.json(含分数修改)+ YYYY-MM-DD-reflection.md摘要。
- 3. maintenance.py — 应用衰减、修剪已死亡的事实、归档冷层级、
从顶级SQLite事实重新渲染MEMORY.md、真空压缩数据库。
智能体协议
这些规则放入你的AGENTS.md或等效配置中:
记忆神谕协议
在响应用户之前:
运行:python3 ~/.openclaw/skills/memory-oracle/scripts/recall.py --query <用户消息>
将输出作为相关背景注入到你的上下文中。
在响应用户之后:
运行:python3 ~/.openclaw/skills/memory-oracle/scripts/capture.py --turn <完整对话轮次文本>
当用户说记住这个、永远不要忘记、这很关键时:
运行:python3 ~/.openclaw/skills/memory-oracle/scripts/capture.py --turn <文本> --guardrail
当用户问你对X了解多少时:
运行:python3 ~/.openclaw/skills/memory-oracle/scripts/recall.py --query X --verbose
显示事实及其分数、来源和年龄。
每10轮(心跳):
运行:python3 ~/.openclaw/skills/memory-oracle/scripts/capture.py --flush
这会强制提取任何缓冲的上下文。
安装
bash
cd ~/.openclaw/skills/
git clone <仓库地址> memory-oracle
cd memory-oracle
bash install.sh
安装程序将:
- 1. 初始化带FTS5索引的SQLite数据库
- 导入现有的MEMORY.md和每日日志(如果存在)
- 提示你选择性地为HEAVY进程设置cron任务
- 打印一段代码片段供粘贴到你的AGENTS.md中
- 打印一段代码片段供粘贴到你的OpenClaw压缩配置中
注意: 安装程序不会自动编辑你的AGENTS.md或OpenClaw配置。
你需要自行审查并粘贴这些代码片段。
配置
所有阈值位于config/settings.json中。关键可调参数:
- - recallbudgettokens:总注入预算(默认:2000)
- decayrate:每日分数衰减乘数(默认:0.05)
- archivethreshold:低于此分数的事实移至冷层级(默认:0.2)
- deletethreshold:低于此分数的冷层级事实被清除(默认:0.05)
- deleteminagedays:删除前的最小存在天数(默认:90)
- reflectdeepday:深度反思的星期几,0=周一(默认:0)
- apimodel:HEAVY进程使用的模型(默认:claude-sonnet-4-20250514)
- apimax_tokens:LLM调用的最大响应token数(默认:1000)
卸载/回滚
bash
cd ~/.openclaw/skills/memory-oracle
bash uninstall.sh
卸载程序将:
- 1. 将完整记忆状态导出为JSON(供重新安装时重新导入)
- 移除cron任务(需确认)
- 从安装时创建的备份恢复原始MEMORY.md
- 可选删除SQLite数据库(显示事实数量后询问)
- 清理反思文件和待处理队列
- 打印剩余的手动步骤(AGENTS.md清理、压缩配置)
使用--force跳过确认,使用--keep-db在移除其他所有内容时保留数据库。
已知问题与限制
某些系统上FTS5不可用。
最小化容器(Alpine)和旧版Debian/Ubuntu可能缺少FTS5。install.sh会检查
此情况并打印修复说明。大多数Python 3.8+系统包含FTS5。
与其他记忆技能冲突。
如果你安装了memory-complete、continuity或agent-brain,它们可能
会竞争MEMORY.md的写入。安装前禁用其他记忆插件:
在OpenClaw配置中设置plugins.slots.memory = none,或移除冲突的技能。
容器中cron不可用。
Docker、沙盒VPS和一些托管环境会阻止crontab。
install.sh会优雅处理此情况——只需手动运行HEAVY流水线或
通过你自己的调度器(systemd定时器、supervisor等)运行:
bash
python3 scripts/consolidate.py && python3 scripts/reflect.py --auto && python3 scripts/maintenance.py
现有MEMORY.md过大(>50KB)。
init_db.py中的导入可以工作,但可能会创建大量低置信度的事实。
安装后,运行python3 scripts/maintenance.py --stats并检查
事实数量。如果过高,运行python3 scripts/maintenance.py让衰减
和修剪在几天内自然清理。
基于规则的捕获会遗漏隐含事实。
Capture.py使用模式匹配——它能捕获约70%的事实。剩余30%
由consolidate.py(HEAVY进程)通过LLM提取捕获。如果没有
设置ANTHROPICAPIKEY,则只有基于规则的捕获能工作。
文件结构
memory-oracle/
├── SKILL.md ← 你在这里
├── README.md ← GitHub友好文档
├── LICENSE ← MIT
├── install.sh ← 引导安装 + cron设置
├── uninstall.sh ← 安全回滚 + 导出
├── scripts/
│ ├── init_db.py ← 模式 + 从现有.md迁移
│ ├── capture.py ← LIGHT:基于规则的提取
│ ├── recall.py ← LIGHT:三槽位混合搜索
│ ├── checkpoint.py ← 预压缩紧急保存
│ ├── consolidate.py ← HEAVY:LLM事实提取
│ ├── reflect.py ← HEAVY:自适应反思
│ ├── maintenance.py ← HEAVY:衰减、修剪、重新渲染
│ └── migrate.py ← 模式版本管理
├── config/
│ ├── settings.json ← 所有可调参数
│ └── patterns.json ← 双语提取规则
├── prompts/
│ ├── consolidate.txt ← LLM提取提示词
│ ├── reflect_light.txt ← 每日反思提示词
│ └── reflect_deep.txt ← 每周反思提示词
└── tests/
├── test_capture.py ← 模式匹配测试
└── test_recall.py ← 排名+预算测试
优雅降级
每个组件都设计为安全失败:
- - SQLite损坏 → recall回退到对MEMORY.md的grep搜索
- API