memoryweave/IMPLEMENTATION-FIVE.md

288 lines
8.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 织忆五步实施计划
> 创建时间2026-05-30
> 状态:规划阶段,未开始
> 禁止:偷懒、随意更改变动设计语言
---
## 总览
| 阶段 | 内容 | 优先级 | 预计工期 | 状态 |
|------|------|--------|---------|------|
| **E1** | 图谱导航激活 | 🔴 高 | 1-2 天 | ⏳ |
| **E2** | 多 agent 命名空间激活 | 🔴 高 | 1 天 | ⏳ |
| **E3** | 增量 embedding | 🟡 中 | 1 天 | ⏳ |
| **E4** | 图谱推理 | 🟡 中 | 2-3 天 | ⏳ |
| **E5** | 产品 UI | 🔵 低 | 长期 | ⏳ |
---
## E1图谱导航激活
### 目标
改造 `/recall` 链路,向量搜索后接图谱 BFS 扩展,让搜索结果融合结构化知识。
### 现状
- 图谱数据1269 节点 / 31248 边(`graph/stats` 确认)
- `navigate.go` BFS 导航代码已写好,但 `/recall` 没调用
- `/recall` 当前纯语义搜索,不走图谱
### 实施步骤
#### E1.1 确认 navigate.go 接口
- 找到 `navigate.go` 的函数签名和参数
- 确认 BFS 扩展是 1 跳还是 2 跳
- 确认输出格式(节点列表还是边列表)
#### E1.2 改造 recall 链路
修改 `internal/api/routes/recall.go`
```
语义搜索 → 向量 top-K →
图谱 BFS 扩展navigate 1跳邻居
合并去重 → bge-reranker 重排 →
返回最终结果
```
#### E1.3 测试验证
```bash
# 验证图谱有数据
curl http://localhost:7821/api/v1/graph/stats
# 验证 recall 走图谱(找有图谱关联的记忆)
curl "http://localhost:7821/api/v1/recall?query=织忆图谱导航&top_k=5"
# 对比:图谱激活前 vs 激活后的召回结果差异
# 激活后应该出现更多 1 跳邻居相关结果
```
### 验收标准
- `graph/stats` node_count > 1000已有确认不变
- recall 结果中包含图谱扩展内容(通过日志或响应标记确认)
- 延迟 < 500ms可接受范围
---
## E2多 agent 命名空间激活
### 目标
Hermes OpenClaw 使用独立 namespace数据物理隔离互不串味
### 现状
- 所有记忆 `agent_id=default`混在一起
- Go 代码已有 namespace 隔离逻辑配置未激活
### 实施步骤
#### E2.1 确认当前 namespace 配置
- `config.go` 或环境变量看当前默认 namespace
- Hermes 插件 `__init__.py` `agent_id` 写法
#### E2.2 配置 Hermes namespace
`~/.hermes/plugins/zhiyi/__init__.py` config
```python
# 设为 "hermes",所有 Hermes 发起的记忆走这个 namespace
self.namespace = "hermes"
```
#### E2.3 配置 OpenClaw namespace
OpenClaw zhiyi 集成配置中
```yaml
zhiyi:
namespace: "openclaw"
```
#### E2.4 迁移历史数据(可选,先做新数据隔离)
```bash
# 备份
cp /var/lib/memoryweave/memories.lance /var/lib/memoryweave/memories.lance.bak
# 将 default namespace 的老数据标记为 hermes如果确认都是 Hermes 的)
# 或保持 default 不动,等自然过期
```
#### E2.5 测试验证
```bash
# Hermes 写入记忆,验证 namespace=hermes
curl -X POST http://localhost:7821/api/v1/commit \
-H "Content-Type: application/json" \
-d '{"content":"E2测试记忆 hermes namespace","namespace":"hermes"}'
# OpenClaw 写入记忆,验证 namespace=openclaw
curl -X POST http://localhost:7821/api/v1/commit \
-H "Content-Type: application/json" \
-d '{"content":"E2测试记忆 openclaw namespace","namespace":"openclaw"}'
# 各自查询,只看到自己的
curl "http://localhost:7821/api/v1/recall?query=E2测试记忆&namespace=hermes"
# → 应只有 hermes 那条
curl "http://localhost:7821/api/v1/recall?query=E2测试记忆&namespace=openclaw"
# → 应只有 openclaw 那条
```
### 验收标准
- `namespace=hermes` 查询不到 `namespace=openclaw` 的记忆
- `namespace=openclaw` 查询不到 `namespace=hermes` 的记忆
- 各自 recall 结果只包含同 namespace 内容
---
## E3增量 embedding
### 目标
commit 时同步调用 vLLM embedding不等 Rust sidecar batch延迟从分钟级降到毫秒级
### 现状
- commit 只写原始文本embedding 要等 Rust sidecar 批处理
- vLLM BGE-M3 已在 `localhost:8000` 运行17ms/
### 实施步骤
#### E3.1 确认当前 embedding 流程
- `commit` API Go 层的处理逻辑
- 确认 vLLM embedding 调用在哪里Go 还是 Rust
#### E3.2 Go 层直接调用 vLLM
`internal/api/routes/commit.go` commit 处理中写入文本后同步调用
```
commit 文本 → 同步 POST vLLM localhost:8000 → 获取 1024dim 向量 →
写入 LanceDBtext + vector 同时落盘)
```
#### E3.3 保留 Rust sidecar 作为 fallback
如果 vLLM 不可用fallback Rust sidecar batch embedding
#### E3.4 测试验证
```bash
# 测试 embedding 延迟
time curl -X POST http://localhost:7821/api/v1/commit \
-H "Content-Type: application/json" \
-d '{"content":"E3增量embedding测试验证同步embedding延迟","namespace":"hermes"}'
# 验证:写入后立即 recall 能搜到(不等 batch
sleep 1
curl "http://localhost:7821/api/v1/recall?query=同步embedding延迟测试&top_k=3"
# 验证 LanceDB 中该条记忆有向量(查不到具体值,但 recall 能用说明有)
```
### 验收标准
- commit 响应时间 < 100ms包含 embedding 调用
- commit 5 秒内 recall 能搜到无需等待 Rust sidecar
- vLLM 不可用时 fallback 正常不报错
---
## E4图谱推理
### 目标
图谱真正参与推理矛盾检测 agent 共享遗忘决策参考图谱结构
### 现状
- 图谱存了 1269 节点/31248 但只搜不用
- 没有基于图谱结构的推理逻辑
### 实施步骤
#### E4.1 矛盾检测
commit 新记忆时检查图谱中是否有同一实体相斥属性
```
commit 新记忆 → 解析实体 + 属性 →
查图谱中该实体所有属性 →
如果存在矛盾属性e.g. "是" vs "不是")→ 标记为矛盾 →
不阻止写入,但记录到矛盾表中
```
#### E4.2 跨 agent 知识共享
Hermes 找不到答案时主动查 OpenClaw namespace 的相关记忆
```
hermes recall 无结果 → 查 openclaw namespace 相同实体的记忆 →
如有,标记为"跨 agent 共享",合并结果
```
#### E4.3 遗忘决策参考图谱
当前遗忘策略只看时间 + 质量分数加上图谱
```
节点度(连接数)高的节点优先保留
跨 namespace 共享的节点不允许遗忘
```
#### E4.4 测试验证
```bash
# E4.1 矛盾检测
curl -X POST http://localhost:7821/api/v1/commit \
-H "Content-Type: application/json" \
-d '{"content":"小唯是牧尘的女朋友","namespace":"hermes"}'
curl -X POST http://localhost:7821/api/v1/commit \
-H "Content-Type: application/json" \
-d '{"content":"小唯不是牧尘的女朋友","namespace":"hermes"}'
# 验证矛盾标记
curl "http://localhost:7821/api/v1/graph/conflicts?entity=小唯"
# E4.2 跨 agent 共享
curl "http://localhost:7821/api/v1/recall?query=小唯&namespace=hermes&cross_agent=true"
# 应能看到 openclaw 相关的记忆(如果有)
# E4.3 图谱度优先保留
# 验证图谱度高的节点在遗忘测试后仍然存在
```
### 验收标准
- 矛盾检测能识别出相斥属性对
- agent recall 能返回其他 namespace 相关记忆
- 高连接度节点在遗忘后仍存在
---
## E5产品 UI长期
### 目标
给织忆做一个简单的可视化界面用于查看记忆图谱搜索结果
### 现状
- 只能 API 没有界面
- 个人用足够但不方便查看图谱结构
### 实施步骤
待定优先级最低 4 个阶段完成后再规划
### 可能的方案
- 简单 Web UIReact + Go API
- Obsidian 插件直接可视化
- CLI 增强tree/graph 可视化
---
## 阶段推进规则
1. **必须按顺序完成**E1 E2 E3 E4 E5
2. **每个阶段必须测试验证后才能进入下一阶段**
3. **禁止跳过测试验证步骤**
4. **禁止偷懒:实施步骤必须逐条执行**
5. **禁止随意更改变动设计语言**阶段目标和验收标准锁定
6. **如有阻塞,记录到 BLOCKED 章节,继续下一个阶段**
---
## BLOCKED阻塞记录
| 时间 | 阶段 | 阻塞原因 | 尝试方案 |
|------|------|---------|---------|
| - | - | 无阻塞 | - |
---
## 进度追踪
| 阶段 | 开始时间 | 完成时间 | 状态 |
|------|---------|---------|------|
| E1 图谱导航激活 | - | - | |
| E2 agent 命名空间 | - | - | |
| E3 增量 embedding | - | - | |
| E4 图谱推理 | - | - | |
| E5 产品 UI | - | - | |
---
*最后更新2026-05-30*