From 3f4e00dac74b6683ed881b17edf404c08004539d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Mon, 9 Nov 2026 19:46:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(recall):=20debug=20=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=E8=A1=A5=20ghost-ns=20=E9=97=B8=20=E2=80=94=20=E4=B8=8E?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E5=90=8C=E5=88=A4=E6=8D=AE=EF=BC=8C=E8=AF=8A?= =?UTF-8?q?=E6=96=AD=E4=B8=8D=E5=86=8D=E6=92=92=E8=B0=8E=20(t=5Fnsdebug-20?= =?UTF-8?q?261109)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 生产 /api/v1/recall 已于 t_15d82af7 + t_a243aefa(11-07)修好:ghost ns ⇒ 0 条。 但 /api/v1/recall/debug 自带一套 nsList,仍无条件并 shared ⇒ 实测 ghost ns 报 "5 candidates from 2 namespaces"、返回 5 条 —— 诊断端点与生产不同判据。 改:nsList 并 shared 前过 storage.NamespaceHasRowsFor(与 recall.go 同一判据 + 同一 60s 缓存); 0 候选分支的 detail 补 "from N namespaces"(否则看不出搜了几个 ns)。 测试:internal/api/routes/nsdebug_ghost_test.go 2 例(ghost ⇒ 0;真实 ns 仍 2 namespaces)。 RED 已验:修前 ghost ns count=2(sh_1/sh_2)。 --- go/internal/api/routes/core.go | 9 +- go/internal/api/routes/nsdebug_ghost_test.go | 89 ++++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 go/internal/api/routes/nsdebug_ghost_test.go diff --git a/go/internal/api/routes/core.go b/go/internal/api/routes/core.go index 2c9f019..f60f2eb 100644 --- a/go/internal/api/routes/core.go +++ b/go/internal/api/routes/core.go @@ -1115,11 +1115,14 @@ func (a *API) RecallDebug(w http.ResponseWriter, r *http.Request) { t2 := time.Now() // 2026-09-21 五修:与 RecallPipeline 完全一致 —— ns=="" 走全库单次检索, // 否则 [ns, shared]。诊断端点必须如实反映生产召回行为,不能自带另一套 ns 逻辑。 + // 2026-11-09 t_nsdebug:补上 ghost-ns 闸(与 recall.go 的 namespaceHasRows 同判据、同缓存)。 + // 背景:生产 /recall 已于 11-07 修好(ghost ns ⇒ 0 条),debug 端点仍无条件并 shared, + // 实测 ghost ns 报 "5 candidates from 2 namespaces" ⇒ 诊断撒的谎比没诊断更贵。 nsList := []string{req.Namespace} if req.Namespace == "" { nsList = []string{""} // 全库 - } else if req.Namespace != "shared" { - nsList = append(nsList, "shared") + } else if req.Namespace != "shared" && storage.NamespaceHasRowsFor(a.LanceDB, req.Namespace) { + nsList = append(nsList, "shared") // ghost ns(全量表无行)⇒ 不并 shared,与生产同口径 } var candidates []models.MemoryRecord for _, ns := range nsList { @@ -1134,7 +1137,7 @@ func (a *API) RecallDebug(w http.ResponseWriter, r *http.Request) { } annTime := time.Since(t2).Milliseconds() if len(candidates) == 0 { - steps = append(steps, stepInfo{"ann_search", "ok", annTime, "0 candidates"}) + steps = append(steps, stepInfo{"ann_search", "ok", annTime, fmt.Sprintf("0 candidates from %d namespaces", len(nsList))}) respond(w, 200, map[string]interface{}{"steps": steps, "results": []interface{}{}, "total_ms": time.Since(totalStart).Milliseconds()}) return } diff --git a/go/internal/api/routes/nsdebug_ghost_test.go b/go/internal/api/routes/nsdebug_ghost_test.go new file mode 100644 index 0000000..0a6a73c --- /dev/null +++ b/go/internal/api/routes/nsdebug_ghost_test.go @@ -0,0 +1,89 @@ +package routes + +// 2026-11-09 t_nsdebug:/api/v1/recall/debug 的 ghost-ns 判据回归。 +// 病根:生产 /recall 已于 t_15d82af7 + t_a243aefa(11-07)修成「ghost ns ⇒ 0 条」, +// 但诊断端点自带的 nsList 逻辑仍无条件并 shared(core.go「五修」段), +// 实测 ghost ns 报 "5 candidates from 2 namespaces" —— 诊断端点撒谎比没有诊断更危险。 +// 判据:diagnose 与生产同判据(storage.NamespaceHasRowsFor),真实 ns 的 [ns, shared] 语义不变。 + +import ( + "encoding/json" + "net/http/httptest" + "strings" + "testing" + + "github.com/xiaoxue/memoryweave/internal/governance" + "github.com/xiaoxue/memoryweave/internal/models" + "github.com/xiaoxue/memoryweave/internal/storage" +) + +func runRecallDebugHTTP(t *testing.T, db storage.LanceDB, body string) (int, map[string]interface{}) { + t.Helper() + saved := storage.SearchCacheInstance + storage.SearchCacheInstance = nil + t.Cleanup(func() { storage.SearchCacheInstance = saved }) + + srv := fakeEmbedServer(t) + emb := storage.NewEmbedder(srv.URL) + rerank := storage.NewReranker("", emb) + api := NewAPI(db, emb, rerank, nil, governance.NewInMemoryGraph()) + req := httptest.NewRequest("POST", "/api/v1/recall/debug", strings.NewReader(body)) + w := httptest.NewRecorder() + api.RecallDebug(w, req) + var m map[string]interface{} + _ = json.Unmarshal(w.Body.Bytes(), &m) + return w.Code, m +} + +func annDetail(m map[string]interface{}) string { + steps, _ := m["steps"].([]interface{}) + for _, x := range steps { + s, _ := x.(map[string]interface{}) + if s["step"] == "ann_search" { + d, _ := s["detail"].(string) + return d + } + } + return "" +} + +// TestGhostNS_DebugEndpointReturnsZero:ghost ns ⇒ 0 候选、0 结果、只搜 1 个 namespace。 +func TestGhostNS_DebugEndpointReturnsZero(t *testing.T) { + db := &nsAwareRecallLance{rows: []models.MemoryRecord{ + {ID: "sh_1", Namespace: "shared", Category: "distilled", Content: "共享事实一"}, + {ID: "sh_2", Namespace: "shared", Category: "distilled", Content: "共享事实二"}, + {ID: "wiki_1", Namespace: "wiki-curator-main", Category: "distilled", Content: "笔记知识一"}, + }} + code, m := runRecallDebugHTTP(t, db, `{"query":"ghost ns 诊断端点探针","limit":5,"namespace":"ghost-ns-debug-2026-11"}`) + if code != 200 { + t.Fatalf("HTTP=%d body=%v", code, m) + } + if n, _ := m["count"].(float64); n != 0 { + t.Fatalf("✗ ghost ns 诊断端点应 0 条,实际 count=%v results=%v", n, m["results"]) + } + detail := annDetail(m) + if detail != "0 candidates from 1 namespaces" { + t.Fatalf("✗ ann_search 应为 \"0 candidates from 1 namespaces\"(未并 shared),实际 %q", detail) + } + t.Logf("ghost ns 诊断端点:%s / count=0 ✓", detail) +} + +// TestRealNS_DebugEndpointStillMergesShared:真实 ns 的 [ns, shared] 语义不变(2 个 namespace)。 +func TestRealNS_DebugEndpointStillMergesShared(t *testing.T) { + db := &nsAwareRecallLance{rows: []models.MemoryRecord{ + {ID: "own_1", Namespace: "hermes-main", Category: "distilled", Content: "本域事实一"}, + {ID: "sh_1", Namespace: "shared", Category: "distilled", Content: "共享事实一"}, + }} + code, m := runRecallDebugHTTP(t, db, `{"query":"真实 ns 诊断端点对照","limit":5,"namespace":"hermes-main"}`) + if code != 200 { + t.Fatalf("HTTP=%d body=%v", code, m) + } + detail := annDetail(m) + if detail != "2 candidates from 2 namespaces" { + t.Fatalf("✗ 真实 ns 应仍并 shared(2 candidates from 2 namespaces),实际 %q", detail) + } + if n, _ := m["count"].(float64); n == 0 { + t.Fatalf("✗ 真实 ns 诊断端点不应 0 条:%v", m) + } + t.Logf("真实 ns 诊断端点:%s / count=%v ✓", detail, m["count"]) +}