fix(recall): debug 端点补 ghost-ns 闸 — 与生产同判据,诊断不再撒谎 (t_nsdebug-20261109)

生产 /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)。
This commit is contained in:
小唯
2026-11-09 19:46:49 +08:00
parent 15a224f1c6
commit 3f4e00dac7
2 changed files with 95 additions and 3 deletions
+6 -3
View File
@@ -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
}
@@ -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"])
}