experiment(recall): 选择窗口解耦——实测不提分,默认回退 topK*2(保留热调旋钮 + 修单测隔离)

假设:MMR/RRF 窗口 = topK*2 ⇒ 请求 5 条只在前 10 里选,第 11 名起的好答案没机会,
应把窗口钉成 max(topK*2,20) 让 top-5 与 top-50 前 5 一致。

实测(84 DA 题、no_cache、tk=5):
  窗口旧(topK*2=10) 0.5575 / 窗 20 0.5655 / 窗 100 0.5655
  tk5-vs-tk50 前 5 一致率:19.0% → 27.4% → 45.2%
⇒ 一致率变了但**主尺不动**(涨1跌1 = 噪声内,窗 20 与窗 100 完全相同)。
结论:剩 k 依赖来自 RRF 池宽 + 词面名次对深位条目的上位,那类上位本身不带来正确率 ⇒
不做成默认。selectionWindow() 默认回到 topK*2,宽窗口只留热调
(echo 20|100 > /var/lib/memoryweave/recall-sel-window)供以后复跑。

顺带修:旋钮文件路径可被 env 覆盖(ZHIYI_FTS_RESERVE_FILE / ZHIYI_SEL_WINDOW_FILE)——
生产上调过旋钮会让单测红绿翻转(本次实际踩到,knob=100 时单测直接 FAIL)。
This commit is contained in:
小怡
2026-11-16 22:22:03 +08:00
parent 4420937edb
commit 642e81d5a4
2 changed files with 63 additions and 6 deletions
+16
View File
@@ -5,6 +5,7 @@ import "testing"
// 2026-11-16 ftsReserveSlots:让位预算随 topK 缩放。
// 判据(无覆盖文件时):topK/2 封顶 3 —— k=5 ⇒ 2、k=6 ⇒ 3、k≥6 恒 3(= 旧值)。
func TestFtsReserveSlots_ScaledByTopK(t *testing.T) {
t.Setenv("ZHIYI_FTS_RESERVE_FILE", "/nonexistent/recall-fts-reserve")
cases := []struct{ topK, want int }{
{1, 0}, {2, 1}, {3, 1}, {4, 2}, {5, 2}, {6, 3}, {7, 3}, {10, 3}, {50, 3},
}
@@ -23,3 +24,18 @@ func TestFtsReserveSlots_NeverEatsWholeList(t *testing.T) {
}
}
}
// 2026-11-16 selectionWindow:默认必须保持旧行为(topK*2)。
// 宽窗口(max(topK*2,20) / 常数 100)实测不提分(0.5575→0.5655,涨1跌1 = 噪声内),
// 故默认回落到 topK*2;宽窗口只作为热调实验开关(/var/lib/memoryweave/recall-sel-window)。
func TestSelectionWindow_DefaultsToLegacy(t *testing.T) {
t.Setenv("ZHIYI_SEL_WINDOW_FILE", "/nonexistent/recall-sel-window")
cases := []struct{ topK, want int }{
{1, 2}, {5, 10}, {10, 20}, {20, 40}, {50, 100},
}
for _, c := range cases {
if got := selectionWindow(c.topK); got != c.want {
t.Errorf("selectionWindow(%d)=%d want %d(默认必须等于旧行为 topK*2)", c.topK, got, c.want)
}
}
}
+47 -6
View File
@@ -1147,10 +1147,11 @@ func (p *RecallPipeline) recallUncached(ctx context.Context, query, namespace st
var finalIndices []int
if hold > 0 && hold < len(candidates) {
keep := hold
if maxKeep := topK * 2; keep > maxKeep {
win := selectionWindow(topK)
if maxKeep := win; keep > maxKeep {
keep = maxKeep
}
finalIndices = make([]int, 0, topK*2)
finalIndices = make([]int, 0, win)
for i := 0; i < keep; i++ {
finalIndices = append(finalIndices, i)
}
@@ -1160,19 +1161,20 @@ func (p *RecallPipeline) recallUncached(ctx context.Context, query, namespace st
rest = append(rest, r)
}
}
if need := topK*2 - len(finalIndices); need > 0 {
if need := win - len(finalIndices); need > 0 {
finalIndices = append(finalIndices, MMRSelect(rest, candidates, need, diversity)...)
}
log.Printf("[recall][hold] rerank 保位前 %d 条(共 %d 候选,池 %d)", keep, len(candidates), len(finalIndices))
} else {
finalIndices = MMRSelect(reranked, candidates, topK*2, diversity)
finalIndices = MMRSelect(reranked, candidates, selectionWindow(topK), diversity)
}
// RRF 开启时多取一倍候选进融合池:只在 topK 内部重排不会改变 top5 的**集合**(等于没效果),
// 第 6~10 名必须有机会被词面秩抬进前 5,融合才可能有增益。门控关 ⇒ poolN==topK,逐位等价现状。
poolN := topK
if rrfEnabled() {
poolN = topK * 2
// 融合池必须与选择窗口同宽,否则"窗口选了 20 条、池只放 10 条"⇒ 后半截白选。
poolN = selectionWindow(topK)
}
results := make([]models.RecallResult, 0, poolN)
for i, idx := range finalIndices {
@@ -1818,7 +1820,7 @@ func filterRecallRawCategories(results []models.RecallResult) []models.RecallRes
// 热调:`/var/lib/memoryweave/recall-fts-reserve` 写整数(0..3)即覆盖本规则,
// 无需重启(A/B 用);回滚 = 写 3(逐位回到改动前行为)。
func ftsReserveSlots(topK int) int {
if b, err := os.ReadFile("/var/lib/memoryweave/recall-fts-reserve"); err == nil {
if b, err := os.ReadFile(recallKnobPath("recall-fts-reserve", "ZHIYI_FTS_RESERVE_FILE")); err == nil {
if v, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil && v >= 0 {
if v > topK {
v = topK
@@ -1835,3 +1837,42 @@ func ftsReserveSlots(topK int) int {
}
return n
}
// selectionWindow 返回 MMR/RRF 的「选择窗口」宽度 —— 与请求的 topK 解耦。
//
// 2026-11-16(t_sel_window):原实现在两处用 topK*2 作为窗口,于是**同一 query
// 请求 5 条与请求 50 条拿到的前 5 名不同**(实测 29 道零分题里有 6~8 道在
// top_k=50 请求时 DA 已进前 5,只是 tk=5 请求拿不到)。窗口窄 ⇒ 只在前 10 条里
// 做 MMR/RRF ⇒ 第 11 条起的好答案结构性没机会。
// 规则:窗口 = max(topK*2, 20)。贪心 MMR 前缀稳定 ⇒ 前 5 名在 5/10/20/50 各档一致。
// 热调:/var/lib/memoryweave/recall-sel-window 写整数 —— 0 = 旧行为(topK*2),
// n>0 = 窗口取 max(n, topK);文件缺失 ⇒ 新默认。回滚 = echo 0 > 该文件。
func selectionWindow(topK int) int {
if b, err := os.ReadFile(recallKnobPath("recall-sel-window", "ZHIYI_SEL_WINDOW_FILE")); err == nil {
if v, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil && v >= 0 {
if v == 0 {
return topK * 2
}
if v > topK {
return v
}
return topK
}
}
// 默认 = 旧行为(topK*2)。宽窗口(max(topK*2,20) / 常数 100)实测**不提分**:
// 主尺 0.5575(旧) → 0.5655(窗20) → 0.5655(窗100),涨 1 跌 1 = 噪声内;
// 一致率虽 19%→27%→45%,但那是 RRF 池宽带来的词面名次上位,不构成收益。
// 想复跑实验:echo 20(或 100)> /var/lib/memoryweave/recall-sel-window。
return topK * 2
}
// recallKnobPath 返回热调旋钮文件路径。
// 为什么要 env 覆盖:单测读的是真实生产文件 ⇒ 生产上调过旋钮就会让单测红/绿翻转
// (2026-11-16 实际踩到:knob=100 时 TestSelectionWindow_DefaultsToLegacy 直接 FAIL)。
// 单测里 t.Setenv(<env>, <不存在的 tmp 路径>) ⇒ 走默认分支,行为可确定性断言。
func recallKnobPath(name, envKey string) string {
if p := os.Getenv(envKey); p != "" {
return p
}
return "/var/lib/memoryweave/" + name
}