fix(memory): 修图记忆召回的两处能力缺失(关系重复 + 原句不回显)

针对 memory_recall / 自动注入这条图记忆召回链路的实测复核:

- Recall(depth>1) 跨层不去重:每层都用已累积的 entityIDs 查邻接,
  上一层刚产出的关系会在下一层被反复查回并再次 append。实测
  小明→小红 在 depth=2 出现两次,memory_recall 的 10 条关系预算被
  同一句话刷屏、真正的新关系(小红→小刚)被截断。改为按 relation ID
  跨层去重(实体本就已去重)。
- memory_recall 从不回显 sentence_text:工具 schema 明写「填了才能日后
  从图谱回到原文」、Recall 也已 JOIN 出句子,但输出只给实体名与关系类型,
  该字段形同虚设。抽出 formatRecallRelations,对非空原句截断 60 字附在
  关系行后;超过 10 条仍截断并提示。

测试:TestRecallWithDepth 增补去重与精确条数断言;
新增 TestFormatRecallRelations_SurfacesSentence / _Truncates。
go build/vet 干净,go test -race ./internal/memory/ ./internal/agent/core/... 全绿。
This commit is contained in:
JianFeeeee
2026-09-15 06:48:36 +08:00
parent 6afe361804
commit 94995eaa64
5 changed files with 87 additions and 8 deletions

View File

@ -554,6 +554,14 @@ func (g *GraphDB) Recall(keywords []string, seedEntities []string, depth int, se
return result, nil
}
// seenRel 跨层去重。
//
// 每层都用**已累积的** entityIDs 查邻接关系,因此上一层刚产出、以及
// 两个已访问实体之间的关系会在下一层被重复查回并再次 append。
// 深度 2、稠密图上重复会淹没 memory_recall 的 10 条关系预算——
// 模型看到的是同一句话刷屏,真正的新关系被截断。
seenRel := make(map[int64]bool)
for depthLevel := 0; depthLevel < depth; depthLevel++ {
ids := make([]interface{}, 0, len(entityIDs))
for id := range entityIDs {
@ -600,7 +608,10 @@ func (g *GraphDB) Recall(keywords []string, seedEntities []string, depth int, se
relRows.Close()
return nil, err
}
result.Relations = append(result.Relations, rel)
if !seenRel[rel.ID] {
seenRel[rel.ID] = true
result.Relations = append(result.Relations, rel)
}
if !entityIDs[rel.SourceID] {
newIDs[rel.SourceID] = true