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"]) +}