Install
openclaw skills install memory-tree-universalMemory Tree 记忆树架构 — 通用版,适用于任何 AI Agent 的长期记忆系统。包含三棵树设计、热冷路径管道、14 条打分规则、数据库 Schema、检索 API 和坑点总结。当用户需要记忆系统、长期记忆、AI Agent 记忆架构时加载此技能。
openclaw skills install memory-tree-universal源自 OpenHuman Memory Tree 设计理念,经 Hermes Agent 实战验证后抽象为通用架构。 适用于任何需要长期记忆、多轮对话、信息压缩的 AI Agent 系统。
问题:传统 Agent 记忆是"碎片袋"——所有对话塞进 context,越聊越慢,重要信息被淹没。
答案:Memory Tree 把记忆变成"大脑"——有层次、有分类、有热度、有压缩。
| 树 | 作用域 | 功能 |
|---|---|---|
| Source Tree | 每个来源(QQ/飞书/文档) | 滚动缓冲区,L0→L1→L2 级联压缩 |
| Topic Tree | 每个实体(人/项目/概念) | 热度驱动,越频繁越深入 |
| Global Tree | 全局 | 每日摘要,跨来源聚合 |
向量库只能回答"什么与查询相似"。树能回答:
结构让记忆可导航,嵌入保证语义搜索。
输入源(聊天/文档/邮件)
↓
Canonicalize → 标准化 Markdown + 来源元数据
↓
Chunker → 确定性 ID, ≤3k token/块
↓
Fast Score → 14 条规则快速打分(无 LLM)
↓
Persist → SQLite 持久化(chunks + FTS5 索引)
↓
┌─────────────────────────────────────┐
│ 冷路径(异步后台) │
├─────────────────────────────────────┤
│ Deep Score → LLM 判断 admitted/dropped │
│ Entity Extract → 提取实体 │
│ Topic Route → 自动分类 │
│ Hotness Engine → 热度追踪 │
│ Sealer → L0→L1 摘要压缩 │
└─────────────────────────────────────┘
↓
检索 → 中文 LIKE / 英文 FTS5 MATCH + 热度排序
每条消息转为统一 Markdown 格式:
[role: user] [source: qqbot] [time: 2026-05-19 01:16]
记住:项目预算 5 万,9 月 15 日前完成。
元数据字段:source, role, timestamp, session_id, message_id
{session_id[:12]}-{role[:3]}-{index:03d}-{hash_suffix}INSERT OR IGNORE 自动去重| 规则 | 分值 | 示例 |
|---|---|---|
| 用户明确要求记忆 | +0.25 | "记住"、"保存" |
| 用户偏好信息 | +0.25 | "喜欢"、"讨厌" |
| 金额信息 | +0.20 | "5 万"、"100 元" |
| 重要标记 | +0.20 | "重要"、"必须" |
| 情感/需求 | +0.15 | "需要"、"想要" |
| 错误/异常 | +0.15 | "bug"、"报错" |
| 版本信息 | +0.10 | "v0.14.0" |
| 项目相关 | +0.10 | "yinmei 项目" |
| 配置信息 | +0.10 | "config" |
| 首次事件 | +0.10 | 第一次提到 |
| 成功/完成 | +0.10 | "完成"、"成功" |
| 凭证相关 | +0.05 | "API key" |
| 时间信息 | +0.05 | "2026-05-19" |
| 短文本惩罚 | ×0.5 | <20 字 |
-- 记忆块
CREATE TABLE memory_chunks (
id TEXT PRIMARY KEY,
session_id TEXT,
message_id INTEGER,
content TEXT,
content_hash TEXT,
token_count INTEGER,
source TEXT,
role TEXT,
timestamp REAL,
score REAL,
is_admitted INTEGER, -- 0=dropped, 1=admitted, 2=sealed
score_reasons TEXT -- JSON 数组
);
-- 主题/实体
CREATE TABLE memory_topics (
id INTEGER PRIMARY KEY,
name TEXT,
entity_type TEXT,
hotness REAL,
summary TEXT,
last_updated REAL
);
-- Chunk-Topic 关联
CREATE TABLE memory_chunk_topics (
chunk_id TEXT,
topic_id INTEGER,
relevance REAL
);
-- 摘要(L1/L2)
CREATE TABLE memory_summaries (
id INTEGER PRIMARY KEY,
topic_id INTEGER,
level INTEGER, -- 1=L1, 2=L2
content TEXT,
child_chunk_ids TEXT,
sealed_at REAL
);
-- FTS5 全文索引(外部内容表)
CREATE VIRTUAL TABLE memory_chunks_fts USING FTS5(
content='memory_chunks',
content_rowid='rowid'
);
-- Trigram 索引(中文搜索)
CREATE VIRTUAL TABLE memory_chunks_fts_trigram USING fts5tokenize=trigram(...);
CREATE TABLE memory_jobs (
id INTEGER PRIMARY KEY,
job_type TEXT, -- deep_score / entity_extract / digest
payload TEXT, -- JSON
dedupe_key TEXT,
status TEXT, -- pending / running / completed / failed
worker_id TEXT,
lease_expires REAL,
retry_count INTEGER
);
兜底规则(不调用 LLM 时):
| 实体类型 | 正则模式 |
|---|---|
| 仓库 | github.com/[^/\s]+/[^/\s]+ |
| 网站 | https?://[^\s]+ |
| 版本 | v?\d+.\d+.\d+ |
| 日期 | \d{4}-\d{2}-\d{2} |
| 金额 | \d+[万块元]+ |
| 项目名 | [a-zA-Z][a-zA-Z0-9_-]+ |
| 人名 | [一-龯]{2,4} |
实体 → 主题路由规则:
github.com/... → repo:<org>/<name>v1.2.3 → version:<name>5 万 → budget:<project>每次提及: hotness += 0.05
每日衰减: hotness *= 0.95
活跃阈值: 0.5(高于此值优先检索)
归档阈值: 0.1(低于此值压缩为摘要)
L0 chunk 累积到阈值(如 10 个)→ 触发 seal
↓
LLM 生成 L1 摘要
↓
标记子 chunk is_admitted=2(已密封)
↓
L1 摘要存入 memory_summaries
def search(query, limit=10, min_score=0.1, topic=None, days=None):
has_cjk = bool(re.search(r'[\u4e00-\u9fff]', query))
if has_cjk:
# 中文:LIKE(准确但慢)
sql = """
SELECT c.*, t.name as topic_name, t.hotness
FROM memory_chunks c
LEFT JOIN memory_chunk_topics ct ON c.id = ct.chunk_id
LEFT JOIN memory_topics t ON ct.topic_id = t.id
WHERE c.content LIKE ? AND c.is_admitted >= 1
AND c.score >= ?
ORDER BY c.score DESC, t.hotness DESC
LIMIT ?
"""
params = [f'%{query}%', min_score, limit]
else:
# 英文:FTS5 MATCH(快)
sql = """
SELECT c.*, t.name as topic_name, t.hotness
FROM memory_chunks c
JOIN memory_chunks_fts f ON c.rowid = f.rowid
LEFT JOIN memory_chunk_topics ct ON c.id = ct.chunk_id
LEFT JOIN memory_topics t ON ct.topic_id = t.id
WHERE f MATCH ? AND c.is_admitted >= 1
AND c.score >= ?
ORDER BY c.score DESC, t.hotness DESC
LIMIT ?
"""
params = [query, min_score, limit]
| 方法 | 说明 |
|---|---|
search(query) | 全文搜索 |
get_topic_summary(topic_id) | 主题详情(hotness、chunk 数、摘要) |
get_hot_topics(limit) | 热门主题排行 |
search_by_session(session_id) | 按会话检索 |
get_global_stats() | 全局统计 |
# config.yaml
agent:
memory_tree_enabled: true
bridge.ingest_turn()/memory-tree stats # 统计信息
/memory-tree search <q> # 搜索记忆
/memory-tree topics # 主题列表
/memory-tree clear # 清除数据(需确认)
错:is_admitted = score
对:is_admitted = 1(单独存 score 列)
每增加一列,VALUES 必须增加一个参数。否则 INSERT OR IGNORE 静默失败。
-- 错:所有行被过滤
WHERE id NOT IN (SELECT chunk_id FROM memory_jobs)
-- 对:显式排除 NULL
WHERE payload->>'chunk_id' IS NOT NULL
# 先清空 FTS5 索引
c.execute("INSERT INTO memory_chunks_fts(memory_chunks) VALUES('delete')")
# 再删除父表
c.execute("DELETE FROM memory_chunks WHERE ...")
SQLite FTS5 对中文逐字分词,"预算"分成"预"和"算",MATCH 返回 0 结果。
唯一可靠方案:LIKE %预算%
清理数据用独立进程,清理前关闭所有 Python 连接。
canonicalize() 方法,适配你的输入源search() 注入上下文只需实现:
memory_chunks 表 + FTS5 索引ingest(messages) → 分块 + 打分 + 入库search(query) → LIKE/MATCH 检索冷路径(LLM 深度处理)可选。
Hermes Agent 完整实现(~1944 行):
agent/memory_tree.py — 热路径管道agent/memory_tree_cold.py — 冷路径管道agent/memory_tree_retrieval.py — 检索 APIagent/memory_tree_clean.py — 清理脚本agent/memory_tree_integration.py — Hermes 桥接器测试脚本:scripts/quick_test.py
调试记录:references/debug-log.md
快速上手:references/quickstart.md