diff --git a/go/internal/api/routes/core.go b/go/internal/api/routes/core.go index 1ace913..d5ee7e6 100644 --- a/go/internal/api/routes/core.go +++ b/go/internal/api/routes/core.go @@ -1512,32 +1512,41 @@ func capRecallResults(results []models.RecallResult, limit int) (prim, expanded return prim, []models.RecallResult{} } exp := make([]models.RecallResult, 0, len(results)) + // 2026-10-26 (t_e98cb1d6):pattern / peer 是「附带注入」的召回条目(recall.go Step 6.2/6.3), + // 追加在 results 末尾 ⇒ 若并入 prim 一起截断,distilled 占满 limit 时它们恒被切光 + // (16:28 topkcap 引入的 pattern 截断回归,limit=50 时 pattern=0 / limit=200 才现形 3 条)。 + // 正确语义:主检索结果(distilled 等)照常收口到 limit;pattern/peer 作为附带条目 + // 恒保留(不挤占主结果配额),避免「命中即注入」被 cap 吞掉。graph_expanded 仍走独立字段。 + var carry []models.RecallResult for _, r := range results { - if r.Category == "graph_expanded" { + switch r.Category { + case "graph_expanded": exp = append(exp, r) - } else { + case "pattern", "peer": + carry = append(carry, r) + default: prim = append(prim, r) } } - if len(exp) == 0 { - if len(prim) > limit { - prim = prim[:limit] - } - return prim, []models.RecallResult{} - } + // 先收口主结果 if len(prim) > limit { prim = prim[:limit] } - if room := limit - len(prim); room > 0 { - if room > len(exp) { - room = len(exp) + // 主结果不足 limit 时用扩展结果按原顺序补齐(稀疏查询仍有扩展收益) + if len(exp) > 0 { + if room := limit - len(prim); room > 0 { + if room > len(exp) { + room = len(exp) + } + prim = append(prim, exp[:room]...) + exp = exp[room:] + } + if len(exp) > limit { + exp = exp[:limit] } - prim = append(prim, exp[:room]...) - exp = exp[room:] - } - if len(exp) > limit { - exp = exp[:limit] } + // pattern / peer 附带条目恒保留在 results 末尾(独立于主结果配额) + prim = append(prim, carry...) if expanded == nil { expanded = exp } diff --git a/go/internal/api/routes/core_topk_test.go b/go/internal/api/routes/core_topk_test.go index c79f976..bcd5e01 100644 --- a/go/internal/api/routes/core_topk_test.go +++ b/go/internal/api/routes/core_topk_test.go @@ -74,3 +74,80 @@ func TestCapRecallResultsDefaultLimit(t *testing.T) { t.Fatalf("limit=0 应回落 10,得到 %d", len(prim)) } } + +// ── t_e98cb1d6 回归:pattern / peer 被 cap 截断 ──────────────────────────── +// 病灶:capRecallResults 把非 graph_expanded 全部并入 prim 再截断到 limit; +// pattern/peer 是 recall.go Step 6.2/6.3 追加到 results 末尾的附带条目, +// distilled 占满 limit 时它们被整段切掉(16:28 topkcap 引入,limit=50 时 pattern=0)。 +// 修后:pattern/peer 恒保留在 results 末尾(独立于主结果配额),主结果仍照常收口。 +func TestCapRecallResults_PatternAndPeerSurviveCap(t *testing.T) { + mk := func(n int, cat string) []models.RecallResult { + out := make([]models.RecallResult, n) + for i := 0; i < n; i++ { + out[i] = models.RecallResult{ID: cat + string(rune('a'+i%26)), Category: cat, Content: cat} + } + return out + } + count := func(rs []models.RecallResult, cat string) int { + n := 0 + for _, r := range rs { + if r.Category == cat { + n++ + } + } + return n + } + + cases := []struct { + name string + d, p, pe, e int // distilled / pattern / peer / graph_expanded + limit int + wantD, wantP, wantPe int + }{ + // ① 金标场景:主结果占满 limit,pattern/peer 追加在末尾 ⇒ 修前恒为 0 + {"limit=50_主结果占满_pattern 恒保留", 50, 3, 1, 0, 50, 50, 3, 1}, + {"limit=200_主结果占满_pattern 仍保留", 100, 3, 1, 97, 200, 100, 3, 1}, + // ② peer 注入同样不被截断 + {"limit=50_peer 注入不被截断", 50, 0, 2, 0, 50, 50, 0, 2}, + // ③ 主结果不足 limit:扩展先补齐主结果,carry 仍保留在末尾 + {"limit=50_主结果不足_扩展补齐+carry 保留", 30, 2, 1, 10, 50, 30, 2, 1}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + all := make([]models.RecallResult, 0, c.d+c.p+c.pe+c.e) + all = append(all, mk(c.d, "distilled")...) + all = append(all, mk(c.p, "pattern")...) + all = append(all, mk(c.pe, "peer")...) + all = append(all, mk(c.e, "graph_expanded")...) + prim, exp := capRecallResults(all, c.limit) + d := count(prim, "distilled") + p := count(prim, "pattern") + pe := count(prim, "peer") + e := count(prim, "graph_expanded") + if d != c.wantD { + t.Errorf("distilled=%d want %d", d, c.wantD) + } + // AC:limit=50 时 pattern 必须 ≥1(金标 query 复现终审现象) + if c.p > 0 && p < 1 { + t.Errorf("pattern=%d,应 ≥1(截断回归未修)", p) + } + if p != c.wantP { + t.Errorf("pattern=%d want %d", p, c.wantP) + } + if pe != c.wantPe { + t.Errorf("peer=%d want %d", pe, c.wantPe) + } + // cap 仍然生效:主结果(distilled + 扩展补齐)不得超过 limit + if d+e > c.limit { + t.Fatalf("主结果超 cap: distilled=%d graph_expanded=%d limit=%d", d, e, c.limit) + } + if len(exp) > c.limit { + t.Fatalf("graph_expanded 字段超 cap: %d > %d", len(exp), c.limit) + } + // 附带条目不得被塞进 graph_expanded 字段 + if count(exp, "pattern") > 0 || count(exp, "peer") > 0 { + t.Fatalf("pattern/peer 被误放入 graph_expanded 字段") + } + }) + } +}