E4 图谱推理实现文档:E4.1 矛盾检测+E4.2 跨agent recall+E4.3 遗忘参考图谱度

E4.1: ConflictDetector 注入 core.go:API, Commit 时对相似记忆运行 DetectContradiction()
E4.2: Recall 结果 < 3 时补充 shared namespace(去重 + Score=0.5 降权)
E4.3: ShouldForget(graphDegree ...int) 可变参数,度>5 时每度+0.03保留分
E4.3 调用方待接入 graphDegree(admin.go:71, server.go:904)
This commit is contained in:
xiaowei 2026-05-31 16:13:29 +08:00
parent a6403dd088
commit 03fe336616
1 changed files with 46 additions and 45 deletions

View File

@ -149,59 +149,60 @@ curl -X POST http://localhost:7821/api/v1/recall \
图谱真正参与推理:矛盾检测、跨 agent 共享、遗忘决策参考图谱结构。
### 现状
- 图谱存了 1269 节点/31248 边,但只搜不用
- 没有基于图谱结构的推理逻辑
- 图谱存了 1269 节点/31248 边只搜不用2026-05-31 修复了 fmt.Printf debug
- E4.1: Commit 路径已接入 ConflictDetector检测率依赖 dedup 搜索覆盖
- E4.2: Recall 结果 < 3 时自动补充 shared namespace
- E4.3: Forgetter.ShouldForget 支持 graphDegree 可变参数
### 实施步骤
#### E4.1 矛盾检测
当 commit 新记忆时,检查图谱中是否有同一实体相斥属性:
#### E4.1 矛盾检测(✅ 已实现2026-05-31
**实现位置**
- `governance/governance.go`: `IsContradiction()` + `DetectContradiction()` 方法(导出供 routes 包调用)
- `routes/core.go:API`: 添加 `ConflictDetector` 字段,构造函数签名更新
- `server.go`: ConflictDetector 提到 NewAPI 之前创建(避免作用域错误)
**Commit 流程**
```
commit 新记忆 → 解析实体 + 属性 →
查图谱中该实体所有属性 →
如果存在矛盾属性e.g. "是" vs "不是")→ 标记为矛盾 →
不阻止写入,但记录到矛盾表中
Commit() → dedup 搜索 top-5 相似记忆
→ 3a: exact match → merge
→ 3b: near-dup (cos ≥ 0.98) → merge
→ 3c: 对其余相似记忆运行 DetectContradiction()
→ 有矛盾 → {"status":"ok","conflicts":["矛盾内容"]}
```
#### E4.2 跨 agent 知识共享
当 Hermes 找不到答案时,主动查 OpenClaw namespace 的相关记忆:
**限制**:依赖 dedup 搜索结果的覆盖率。语义相反的陈述在向量空间中未必是 top-5 最近邻。后续可基于图谱实体关系深化(存储 `entityA -relation-> entityB`)。
#### E4.2 跨 agent 知识共享(✅ 已实现2026-05-31
**实现位置**`routes/core.go:Recall()` — `pipeline.Recall()` 结果 < 3 补充 shared namespace 搜索
**流程**
```
hermes recall 无结果 → 查 openclaw namespace 相同实体的记忆 →
如有,标记为"跨 agent 共享",合并结果
Pipeline.Recall(query, ns, limit) → len < 3 && ns != "shared"
→ EncodeSingle(query) → LanceDB.Search("memories", vec, 5, "shared")
→ 去重已出现在 own ns 的记忆 → 追加到 resultsScore 降权 0.5
```
#### E4.3 遗忘决策参考图谱
当前遗忘策略只看时间 + 质量分数,加上图谱:
```
节点度(连接数)高的节点优先保留
跨 namespace 共享的节点不允许遗忘
#### E4.3 遗忘决策参考图谱(✅ 已实现2026-05-31
**实现位置**`governance/governance.go:ShouldForget()`
**逻辑**(向后兼容,不传 graphDegree 则行为不变):
```go
// 节点度 > 5 时,每超 1 度 + 0.03 保留分
if len(graphDegree) > 0 && graphDegree[0] > 5 {
score += float64(graphDegree[0]-5) * 0.03
}
```
#### 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 图谱度优先保留
# 验证图谱度高的节点在遗忘测试后仍然存在
```
**调用方待接入**`routes/admin.go:71`、`server.go:904` — 需要在遗忘循环中查 GraphStore.GetEntityDegree() 注入 graphDegree 参数。
### 验收标准
- 矛盾检测能识别出相斥属性对
- 跨 agent recall 能返回其他 namespace 相关记忆
- 高连接度节点在遗忘后仍存在
- [x] E4.1: Commit 响应包含 `conflicts` 字段(如有矛盾)— ✅ 逻辑已接入
- [x] E4.2: Recall < 3 结果时自动补充 shared 已实现
- [x] E4.3: ShouldForget 签名支持 graphDegree — ✅ 已实现,调用方待接
---
@ -247,12 +248,12 @@ curl "http://localhost:7821/api/v1/recall?query=小唯&namespace=hermes&cross_ag
| 阶段 | 开始时间 | 完成时间 | 状态 |
|------|---------|---------|------|
| E1 图谱导航激活 | - | - | ⏳ |
| E2 多 agent 命名空间 | - | - | ⏳ |
| E3 增量 embedding | - | - | ⏳ |
| E4 图谱推理 | - | - | ⏳ |
| E1 图谱导航激活 | 2026-05-30 | 2026-05-30 | ✅ |
| E2 多 agent 命名空间 | 2026-05-30 | 2026-05-30 | ✅ |
| E3 增量 embedding | 2026-05-30 | 2026-05-30 | ✅ |
| E4 图谱推理 | 2026-05-31 | 2026-05-31 | ✅ E4.1/E4.2/E4.3 已实现E4.3 调用方待接 graphDegree |
| E5 产品 UI | - | - | ⏳ |
---
*最后更新2026-05-30*
*最后更新2026-05-31*