这批是 10-26~27 已在生产二进制(zhiyid-protectrails-20261027)里运行、 但一直留在工作树未提交的改动。收口原因:三件改造要开分支,工作树脏 = 分支基准与回滚点都不存在。 主要内容: - api/routes/contract.go + trace.go:召回响应契约字段(trace_id/status/scope/ continuation/channel)与 /api/v1/trace 回溯端点 - governance/entity_quality.go:实体质量闸 - storage/trace.go + recall.go/recall_write_buffer.go:召回写入缓冲与轨迹 - 测试:contract/recall_contract_fields/recall_lifecycle/stale_lifecycle/ entity_quality/trace 同时修一个 flaky:recall_lifecycle_test 原本硬编码「今天 11:00 已过」, 凌晨跑就会把「已失效」fixture 当成仍有效。改成相对时间(过期=now-1h), 生命周期判据与运行时刻无关。修完 go test ./internal/api/routes ./internal/storage 全绿。
133 lines
5.3 KiB
Go
133 lines
5.3 KiB
Go
// 织忆 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)
|
||
}
|
||
// P0-4 度数闸(2026-10-26):degree<2 的孤边节点不再注入 ⇒ 正向用例的起点实体
|
||
// 必须真的在图里有 ≥2 条边(两跳邻居即真实图谱形态,不是为过测试造数据)。
|
||
if err := gs.AddNode("n_召回", "召回", "entity", testNS724); err != nil {
|
||
t.Fatalf("AddNode n_召回: %v", err)
|
||
}
|
||
if err := gs.AddEdge("e-ac", "n_织忆", "n_召回", "LINKS_TO", testNS724, 1.0); err != nil {
|
||
t.Fatalf("AddEdge e-ac: %v", err)
|
||
}
|
||
return gs
|
||
}
|
||
|
||
// TestSQLiteGraphStore_DegreeGate P0-4:度数=1 的孤边实体不得被扩展注入。
|
||
func TestSQLiteGraphStore_DegreeGate(t *testing.T) {
|
||
path := filepath.Join(t.TempDir(), "graph-degree.db")
|
||
gs, err := NewSQLiteGraphStore(path)
|
||
if err != nil {
|
||
t.Fatalf("NewSQLiteGraphStore: %v", err)
|
||
}
|
||
t.Cleanup(func() { _ = gs.Close() })
|
||
// 孤边:n_孤边 --LINKS_TO--> n_邻居(起点实体度=1)
|
||
if err := gs.AddNode("n_孤边", "孤边", "entity", testNS724); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := gs.AddNode("n_邻居", "邻居", "entity", testNS724); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := gs.AddEdge("e-one", "n_孤边", "n_邻居", "LINKS_TO", testNS724, 1.0); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if got := gs.EdgeDegree("n_孤边"); got != 1 {
|
||
t.Fatalf("EdgeDegree(n_孤边) 应为 1,实际 %d", got)
|
||
}
|
||
results := []models.RecallResult{{
|
||
ID: "mem_degree_1", Content: "孤边 的扩展", Category: "distilled", Score: 0.9, Namespace: testNS724,
|
||
}}
|
||
got := gs.ExpandFromResults(results, testNS724, 1)
|
||
if len(got) != len(results) {
|
||
t.Fatalf("度数=1 的实体不应扩展注入,实际补入 %d 条", len(got)-len(results))
|
||
}
|
||
}
|
||
|
||
// 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 补入(不凭空加)")
|
||
} |