diff --git a/go/internal/governance/graph_sqlite.go b/go/internal/governance/graph_sqlite.go index 2f8a895..af33640 100644 --- a/go/internal/governance/graph_sqlite.go +++ b/go/internal/governance/graph_sqlite.go @@ -805,18 +805,20 @@ func (gs *SQLiteGraphStore) ExpandFromResults(results []models.RecallResult, nam seenEntities[entity] = true // SQLite 图谱节点 ID 格式: n_{entity_name},需 normalizeEntityID 转换 + // Navigate 的 from/to 都是**节点 ID**(n_织忆),不能用裸名 entity 比对, + // 否则恒不相等 ⇒ 路径全被 continue ⇒ 结构性 0 条附加(t_be4d070a 诊断)。 nodeID := normalizeEntityID(entity) paths, _ := gs.Navigate(nodeID, maxHops, namespace, nil) for _, p := range paths { - // Navigate 返回字段: from, to, relation, weight, hop + // Navigate 返回字段: from, to, relation, weight, hop(均为节点 ID) var neighbor, rel string from, _ := p["from"].(string) to, _ := p["to"].(string) relVal, _ := p["relation"].(string) - if from == entity && to != "" { + if from == nodeID && to != "" { neighbor = to rel = relVal - } else if to == entity && from != "" { + } else if to == nodeID && from != "" { neighbor = from rel = "↩ " + relVal } @@ -828,7 +830,11 @@ func (gs *SQLiteGraphStore) ExpandFromResults(results []models.RecallResult, nam } seenEntities[neighbor] = true expanded = append(expanded, models.RecallResult{ - Content: fmt.Sprintf("[graph] %s --[%s]--> %s", entity, rel, neighbor), + // ID 用邻居节点 ID(n_<实体> 或 n_mem_<记忆id>),非空才不破坏 recall.go 的 + // ID 去重(Step 5 按 r.ID==e.ID 判已存在);若邻居是记忆节点,ID 即真实记忆指针。 + ID: neighbor, + Category: "graph_expanded", + Content: fmt.Sprintf("[graph] %s --[%s]--> %s", entity, rel, displayNodeName(neighbor)), Score: r.Score * 0.5, }) } @@ -837,6 +843,18 @@ func (gs *SQLiteGraphStore) ExpandFromResults(results []models.RecallResult, nam return expanded } +// displayNodeName 把节点 ID 转成可读显示名(去 n_ / n_mem_ / mem_ 前缀)。 +// 图节点 ID 形态:实体 n_织忆 / 概念 n_mem_<记忆id>(name 列才是 mem_)。 +// 仅用于格式化 Content 的可读展示,不做任何记忆正文编造。 +func displayNodeName(nodeID string) string { + for _, p := range []string{"n_mem_", "n_", "mem_", "ep_"} { + if strings.HasPrefix(nodeID, p) { + return strings.TrimPrefix(nodeID, p) + } + } + return nodeID +} + // ExpandWithSummary BFS 扩展 + 生成汇总语句 — E1 图谱导航增强 // 从 recall 结果提取实体,进行多跳扩展,返回扩展关系列表和一句话汇总 func (gs *SQLiteGraphStore) ExpandWithSummary(results []models.RecallResult, namespace string, maxHops int) models.GraphBFSResult { diff --git a/go/internal/governance/graph_sqlite_expand_test.go b/go/internal/governance/graph_sqlite_expand_test.go new file mode 100644 index 0000000..1e5624e --- /dev/null +++ b/go/internal/governance/graph_sqlite_expand_test.go @@ -0,0 +1,95 @@ +// 织忆 MemoryWeave — SQLiteGraphStore.ExpandFromResults 修复锁死测试(t_724f31ad) +// +// 背景:SQLiteGraphStore.ExpandFromResults(graph_sqlite.go:787)此前拿裸实体名 entity +// 与 Navigate 返回的节点 ID(n_织忆)做 == 比对 ⇒ 恒不相等 ⇒ 路径全被 continue ⇒ 结构性 0 条附加。 +// 且附加结果 Category 空、ID 空(审计 graph_expanded 计数恒 0 + recall.go ID 去重失效)。 +// +// 本文件两类断言: +// 1) 正向:构造 n_<实体> 节点 + 一条 recall 结果 ⇒ ExpandFromResults 返回 >len(results), +// 且新增项 Category=="graph_expanded" 且 ID!=""(ID 用邻居节点 ID)。 +// 2) 反向:实体在图里不存在 ⇒ 结果条数不变(不得凭空加)。 +package governance + +import ( + "path/filepath" + "testing" + + "github.com/xiaoxue/memoryweave/internal/models" +) + +// testNS724 SQLite 图的 namespace 须用 `-main` 全形态:SQLiteGraphStore.Navigate 经 +// deriveNamespaceForGraph 会把裸名 ns 变 ns-main 再过滤(与生产 recall.go 传的 namespace 一致)。 +const testNS724 = "t-ns-expand-sqlite-main" + +// newTestSQLiteGraph 建临时 SQLiteGraphStore 并灌入一条链:n_织忆 -LINKS_TO-> n_图谱。 +func newTestSQLiteGraph(t *testing.T) *SQLiteGraphStore { + t.Helper() + path := filepath.Join(t.TempDir(), "graph-test.db") + gs, err := NewSQLiteGraphStore(path) + if err != nil { + t.Fatalf("NewSQLiteGraphStore: %v", err) + } + t.Cleanup(func() { _ = gs.Close() }) + if err := gs.AddNode("n_织忆", "织忆", "entity", testNS724); err != nil { + t.Fatalf("AddNode n_织忆: %v", err) + } + if err := gs.AddNode("n_图谱", "图谱", "entity", testNS724); err != nil { + t.Fatalf("AddNode n_图谱: %v", err) + } + if err := gs.AddEdge("e-ab", "n_织忆", "n_图谱", "LINKS_TO", testNS724, 1.0); err != nil { + t.Fatalf("AddEdge: %v", err) + } + return gs +} + +// TestSQLiteGraphStore_ExpandFromResults_Inserts 正向:实体在图中 ⇒ 补入 graph_expanded 且 ID 非空。 +func TestSQLiteGraphStore_ExpandFromResults_Inserts(t *testing.T) { + gs := newTestSQLiteGraph(t) + + results := []models.RecallResult{{ + ID: "mem_expand_1", + Content: "织忆 与 图谱 的扩展机制", + Category: "distilled", + Score: 0.9, + Namespace: testNS724, + }} + + got := gs.ExpandFromResults(results, testNS724, 1) + if len(got) <= len(results) { + t.Fatalf("期望补入 >len(results)=%d,实际 %d 条(结构性 0 产出未修?)", len(results), len(got)) + } + var added int + for _, e := range got[len(results):] { + if e.Category != "graph_expanded" { + t.Fatalf("补入结果 category=%q 期望 graph_expanded", e.Category) + } + if e.ID == "" { + t.Fatalf("补入结果 ID 为空(ID 去重会失效),Content=%q", e.Content) + } + added++ + } + if added == 0 { + t.Fatal("没有任何 graph_expanded 补入") + } + t.Logf("SQLiteGraphStore: 1-recall 补入 %d 条 graph_expanded(ID 均非空)", added) +} + +// TestSQLiteGraphStore_ExpandFromResults_NoFabrication 反向:实体不在图中 ⇒ 条数不变,不得凭空加。 +func TestSQLiteGraphStore_ExpandFromResults_NoFabrication(t *testing.T) { + gs := newTestSQLiteGraph(t) + + // content 只含一个图中不存在的实体「完全不存在的实体」(切分成 2~8 字中文串均无图节点)。 + results := []models.RecallResult{{ + ID: "mem_expand_2", + Content: "完全不存在的实体", + Category: "distilled", + Score: 0.9, + Namespace: testNS724, + }} + + got := gs.ExpandFromResults(results, testNS724, 1) + if len(got) != len(results) { + t.Fatalf("实体不在图中却补入 %d 条(期望 0 补入,len=%d)", len(got)-len(results), len(results)) + } + t.Logf("SQLiteGraphStore: 图外实体 ⇒ 0 补入(不凭空加)") +} \ No newline at end of file