diff --git a/go/internal/api/routes/core.go b/go/internal/api/routes/core.go index 36ded6b..0dc80b3 100644 --- a/go/internal/api/routes/core.go +++ b/go/internal/api/routes/core.go @@ -918,9 +918,12 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) { // dbg 插桩实证注入发生 4087→4088 但响应无 peer)。语义改走纯函数 // paginateRecallWithCarry(可单测):carry 摘出→offset/去重/截断→回尾。 totalFetched := len(results) - results = paginateRecallWithCarry(results, offset, prevIDs, req.Limit) + results, mainLen := paginateRecallWithCarry(results, offset, prevIDs, req.Limit) nextCont := "" - if totalFetched >= fetchLimit && len(results) == req.Limit && offset+req.Limit < maxContinuationOffset { + // ⚠ 2026-11-02 (t_2891d689):判据看主路段 mainLen,不看 len(results)—— + // carry 会把 results 撑过 limit,旧判据 len(results)==req.Limit 在有注入时 + // 恒假 ⇒ nextCont 恒空、翻页链断裂。收口到 shouldEmitContinuation(可单测)。 + if shouldEmitContinuation(totalFetched, fetchLimit, mainLen, req.Limit, offset) { returned := make([]string, 0, len(prevIDs)+len(results)) returned = append(returned, prevIDs...) for _, r := range results { @@ -1692,11 +1695,19 @@ func capRecallResults(results []models.RecallResult, limit int) (prim, expanded // ——t_e98cb1d6 的 carry 修复被作废,peer/pattern 注入在主召回响应里结构性失明。 // // 语义(纯函数,可单测):先把 carry 摘出,对剩余主路段照旧做 -// offset 切片 → prevIDs 去重 → [:limit] 截断,最后把 carry 回尾。 +// offset 切片 → prevIDs 去重 → [:limit] 截断,第一页把 carry 回尾。 // 保证:① 主路段逐位不变(t_9025b0eb AC 不动:主路段 ≤ limit); -// ② carry 恒存活且恒在响应末尾;③ 无 carry(门关态)逐位与旧内联逻辑 delta=0; +// ② carry 在第一页存活且恒在响应末尾;③ 无 carry(门关态)逐位与旧内联逻辑 delta=0; // ④ 主路段切空(offset 越界 / 去重全掉)时返回空段,不返回孤儿 carry。 -func paginateRecallWithCarry(results []models.RecallResult, offset int, prevIDs []string, limit int) []models.RecallResult { +// +// 2026-11-02(t_2891d689)两处修订: +// ① 返回值加 mainLen(主路段截断后的长度)。4ffc546 后 carry 使 len(results) 可以 +// 超过 limit,翻页判据若看 len(results) 会恒假 ⇒ nextCont 恒空、带注入的 query +// 翻页链断裂(本卡病灶)。判据改看主路段(见 shouldEmitContinuation)。 +// ② carry 只在第一页出现:carry 是固定钉住的注入条目,第一页其 ID 已随 returned +// 记进令牌 prev;续页再追加必然逐页重复,等于把去重成本转嫁给客户端。 +// ⇒ offset > 0 时只回主路段,不追加 carry。 +func paginateRecallWithCarry(results []models.RecallResult, offset int, prevIDs []string, limit int) ([]models.RecallResult, int) { var carry []models.RecallResult main := make([]models.RecallResult, 0, len(results)) for _, r := range results { @@ -1729,9 +1740,25 @@ func paginateRecallWithCarry(results []models.RecallResult, offset int, prevIDs if limit >= 0 && len(main) > limit { main = main[:limit] } - if len(main) == 0 { + mainLen := len(main) + if mainLen == 0 { // 与旧语义一致:主路段空 ⇒ 整页空(页面上只有注入条目没有意义) - return []models.RecallResult{} + return []models.RecallResult{}, 0 } - return append(main, carry...) + if offset > 0 { + // t_2891d689:carry 只在第一页出现,续页不回尾(见函数头注释 ②) + return main, mainLen + } + return append(main, carry...), mainLen +} + +// shouldEmitContinuation —— P0-3 是否发放下一页 continuation 令牌(2026-11-02 t_2891d689)。 +// +// 病灶:4ffc546 后 carry 追加在主路段之后 ⇒ len(results) 可以 > limit, +// 判据 `len(results) == req.Limit` 只要 carry 命中就恒假 ⇒ nextCont 恒空, +// 带 pattern/peer 注入的 query 翻页链断裂(第二页永远取不到)。 +// 修法:判据看**主路段长度** mainLen —— carry 不参与「是否还有下一页」的判断。 +// 必须用 == 而不用 >=:主路段未满一页(offset 越界 / 尾页不满)时不得错发令牌。 +func shouldEmitContinuation(totalFetched, fetchLimit, mainLen, limit, offset int) bool { + return totalFetched >= fetchLimit && mainLen == limit && offset+limit < maxContinuationOffset } diff --git a/go/internal/api/routes/core_topk_test.go b/go/internal/api/routes/core_topk_test.go index 428aa45..8efd21a 100644 --- a/go/internal/api/routes/core_topk_test.go +++ b/go/internal/api/routes/core_topk_test.go @@ -173,10 +173,13 @@ func TestPaginateRecallWithCarry(t *testing.T) { // 且主路段逐位 = 原前 5 位(t_9025b0eb AC:主路 ≤ limit 不变)。 t.Run("limit=5_末位peer存活_主路段逐位不变", func(t *testing.T) { in := append(mkMain(5), models.RecallResult{ID: "peer:muc", Category: "peer", Content: "p"}) - got := paginateRecallWithCarry(in, 0, nil, 5) + got, mainLen := paginateRecallWithCarry(in, 0, nil, 5) if len(got) != 6 { t.Fatalf("len=%d want 6(主 5 + carry 1)", len(got)) } + if mainLen != 5 { + t.Fatalf("mainLen=%d want 5(t_2891d689:判据主路段长度=limit,翻页令牌据此发放)", mainLen) + } if got[5].ID != "peer:muc" || got[5].Category != "peer" { t.Fatalf("末位=%s/%s,want peer:muc/peer(截断回归未修)", got[5].ID, got[5].Category) } @@ -195,7 +198,7 @@ func TestPaginateRecallWithCarry(t *testing.T) { models.RecallResult{ID: "pattern:c", Category: "pattern"}, models.RecallResult{ID: "peer:muc", Category: "peer"}, )...) - got := paginateRecallWithCarry(in, 0, nil, 50) + got, _ := paginateRecallWithCarry(in, 0, nil, 50) if len(got) != 54 { t.Fatalf("len=%d want 54", len(got)) } @@ -252,7 +255,7 @@ func TestPaginateRecallWithCarry(t *testing.T) { for _, offset := range []int{0, 1, 7, 29, 30, 45} { for _, prev := range [][]string{nil, {"mem_008"}, {"mem_008", "mem_009", "mem_020"}} { for _, limit := range []int{5, 10, 50} { - got := paginateRecallWithCarry(in, offset, prev, limit) + got, _ := paginateRecallWithCarry(in, offset, prev, limit) want := legacy(in, offset, prev, limit) if len(got) != len(want) { t.Fatalf("offset=%d prev=%v limit=%d: len got=%d want=%d", offset, prev, limit, len(got), len(want)) @@ -270,17 +273,23 @@ func TestPaginateRecallWithCarry(t *testing.T) { // ④ 主路段切空 ⇒ 整页空(不返回孤儿 carry,与旧语义一致)。 t.Run("offset越界_整页空不带孤儿carry", func(t *testing.T) { in := append(mkMain(5), models.RecallResult{ID: "peer:muc", Category: "peer"}) - if got := paginateRecallWithCarry(in, 5, nil, 5); len(got) != 0 { + if got, _ := paginateRecallWithCarry(in, 5, nil, 5); len(got) != 0 { t.Fatalf("len=%d want 0", len(got)) } }) - // ⑤ 续页(offset>0 + prevIDs 去重)carry 仍回尾,主路段从 offset 起且去掉 prevIDs。 - t.Run("续页carry回尾_主路段offset加去重", func(t *testing.T) { + // ⑤ 续页(offset>0 + prevIDs 去重)主路段从 offset 起且去掉 prevIDs。 + // ⚠ 2026-11-02 (t_2891d689) 判据反转:原断言「续页 carry 回尾」锁死的正是 + // 本卡要改的行为(carry 每页重复出现,去重成本转嫁客户端)。现语义: + // carry 只在第一页出现,续页只回主路段。 + t.Run("续页不回carry_主路段offset加去重", func(t *testing.T) { in := append(mkMain(20), models.RecallResult{ID: "peer:muc", Category: "peer"}) - got := paginateRecallWithCarry(in, 3, []string{"mem_005"}, 5) - if len(got) != 6 { - t.Fatalf("len=%d want 6", len(got)) + got, mainLen := paginateRecallWithCarry(in, 3, []string{"mem_005"}, 5) + if len(got) != 5 { + t.Fatalf("len=%d want 5(续页不含 carry)", len(got)) + } + if mainLen != 5 { + t.Fatalf("mainLen=%d want 5", mainLen) } wantMain := []string{"mem_003", "mem_004", "mem_006", "mem_007", "mem_008"} for i, id := range wantMain { @@ -288,8 +297,10 @@ func TestPaginateRecallWithCarry(t *testing.T) { t.Fatalf("主路段第 %d 位 got=%s want=%s", i, got[i].ID, id) } } - if got[5].ID != "peer:muc" { - t.Fatalf("末位=%s want peer:muc", got[5].ID) + for _, r := range got { + if r.Category == "peer" || r.Category == "pattern" { + t.Fatalf("续页混入 carry: %s/%s", r.ID, r.Category) + } } }) @@ -308,7 +319,7 @@ func TestPaginateRecallWithCarry(t *testing.T) { if prim[len(prim)-1].Category != "peer" { t.Fatalf("cap 后末位=%s,capRecallResults 语义变了", prim[len(prim)-1].ID) } - got := paginateRecallWithCarry(prim, 0, nil, 50) + got, _ := paginateRecallWithCarry(prim, 0, nil, 50) // 修前此处恒被切(results[:50] 丢掉末尾 carry) if got[len(got)-1].ID != "peer:muc" { t.Fatalf("翻页后末位=%s want peer:muc(P0-3 截断回归未修)", got[len(got)-1].ID) @@ -324,3 +335,157 @@ func TestPaginateRecallWithCarry(t *testing.T) { } }) } + +// ── t_2891d689 回归:carry 撑大 len(results) ⇒ nextCont 恒空、翻页链断 ────── +// 病灶:4ffc546 后 paginateRecallWithCarry 回 main+carry,len(results) 可 > limit, +// handler 判据 `len(results) == req.Limit` 只要 carry 命中就恒假 +// ⇒ nextCont 恒空,带 pattern/peer 注入的 query 第二页永远取不到。 +// 修后:判据收口 shouldEmitContinuation,看主路段 mainLen;carry 只在第一页回尾。 +// 判据链与 handler(core.go 翻页块)逐行同构:paginate → shouldEmitContinuation +// → returned=prevIDs+页内ID → encodeContinuation(卡面允许的「抽出判定函数单测」路线)。 +func TestContinuationIssuedWithCarry(t *testing.T) { + mkMain := func(n int) []models.RecallResult { + out := make([]models.RecallResult, n) + for i := 0; i < n; i++ { + out[i] = models.RecallResult{ID: fmt.Sprintf("mem_%03d", i), Category: "distilled", Content: "m"} + } + return out + } + idsOf := func(rs []models.RecallResult) []string { + out := make([]string, len(rs)) + for i, r := range rs { + out[i] = r.ID + } + return out + } + // 生产形态(t_58f9d476 实证):cap 后 prim = 主路段填满 limit + 末尾 carry。 + limit := 5 + prim := append(mkMain(5), models.RecallResult{ID: "peer:muc", Category: "peer"}) + totalFetched := len(prim) // 6 ≥ fetchLimit(=limit+offset+len(prev)=5) + page, mainLen := paginateRecallWithCarry(prim, 0, nil, limit) + if len(page) != 6 || mainLen != 5 { + t.Fatalf("page=%d mainLen=%d want 6/5", len(page), mainLen) + } + // 病灶判据复现:len(page)==limit ⇒ 6==5 恒假 ⇒ 链断。本例页形必须仍是 len>limit, + // 否则该用例失去对旧病灶的覆盖。 + if len(page) == limit { + t.Fatal("页形不再是 len>limit,本用例失去对旧病灶的覆盖") + } + // 新判据:主路段满页 ⇒ 必须发令牌 + if !shouldEmitContinuation(totalFetched, limit, mainLen, limit, 0) { + t.Fatal("totalFetched>=fetchLimit 且主路段满页 ⇒ 应发 nextCont(翻页链断裂回归未修)") + } + // handler 完整动作 ⇒ nextCont != "",且令牌推进 offset=5 + returned := idsOf(page) // 含 carry ID(t_58f9d476 既有行为:去重锚点) + nextCont := encodeContinuation("q", "ns", 0+limit, limit, returned) + if nextCont == "" { + t.Fatal("nextCont == \"\",翻页链断") + } + tok, err := decodeContinuation(nextCont) + if err != nil || tok.Off != 5 || tok.Lim != 5 { + t.Fatalf("令牌非法: off=%d lim=%d err=%v(want 5/5/nil)", tok.Off, tok.Lim, err) + } +} + +// AC2:offset>0 的续页 ⇒ 不含前页已返回的 carry,主路段继续推进;尾页不满 ⇒ 链自然终止。 +func TestCarryOnlyOnFirstPageAcrossPages(t *testing.T) { + mkMain := func(n int) []models.RecallResult { + out := make([]models.RecallResult, n) + for i := 0; i < n; i++ { + out[i] = models.RecallResult{ID: fmt.Sprintf("mem_%03d", i), Category: "distilled", Content: "m"} + } + return out + } + limit := 5 + // 18 主(4 页:5+5+5+3)+ 2 carry + prim := append(mkMain(18), + models.RecallResult{ID: "pattern:a", Category: "pattern"}, + models.RecallResult{ID: "peer:muc", Category: "peer"}) + + // 第 1 页:主 5 + carry 2 回尾;令牌 returned 含 carry ID + page1, ml1 := paginateRecallWithCarry(prim, 0, nil, limit) + if len(page1) != 7 || ml1 != 5 { + t.Fatalf("page1=%d mainLen=%d want 7/5", len(page1), ml1) + } + if page1[6].Category != "peer" || page1[5].Category != "pattern" { + t.Fatalf("第一页 carry 未回尾: %v", page1[5:]) + } + if !shouldEmitContinuation(len(prim), limit, ml1, limit, 0) { + t.Fatal("第 1 页应发令牌") + } + prev := make([]string, 0, len(page1)) + for _, r := range page1 { + prev = append(prev, r.ID) + } + + // 第 2 页:offset=5(模拟 handler 从令牌取 offset/prev) + page2, ml2 := paginateRecallWithCarry(prim, 5, prev, limit) + for _, r := range page2 { + if r.Category == "pattern" || r.Category == "peer" { + t.Fatalf("续页混入前页 carry: %s/%s", r.ID, r.Category) + } + } + want2 := []string{"mem_005", "mem_006", "mem_007", "mem_008", "mem_009"} + if len(page2) != 5 || ml2 != 5 { + t.Fatalf("page2=%d mainLen=%d want 5/5", len(page2), ml2) + } + for i, id := range want2 { + if page2[i].ID != id { + t.Fatalf("主路段未推进: 第%d位 got=%s want=%s", i, page2[i].ID, id) + } + } + // 链未断:第 2 页满页且取数满足 ⇒ 继续发令牌(生产页 2 的 fetchLimit=5+5+len(prev)) + if !shouldEmitContinuation(limit+5+len(prev), limit+5+len(prev), ml2, limit, 5) { + t.Fatal("满页续页应继续发令牌(链不得在第 2 页断)") + } + for _, r := range page2 { + prev = append(prev, r.ID) + } + // 第 3 页照推 + page3, ml3 := paginateRecallWithCarry(prim, 10, prev, limit) + if ml3 != 5 || page3[0].ID != "mem_010" { + t.Fatalf("page3 mainLen=%d first=%s want 5/mem_010", ml3, page3[0].ID) + } + // 第 4 页(尾页只剩 3 主)⇒ mainLen=3 < limit ⇒ 不再发令牌(不得错发空尾页令牌) + page4, ml4 := paginateRecallWithCarry(prim, 15, prev, limit) + if ml4 != 3 || len(page4) != 3 { + t.Fatalf("page4 mainLen=%d len=%d want 3/3", ml4, len(page4)) + } + if shouldEmitContinuation(len(prim), limit+15+len(prev), ml4, limit, 15) { + t.Fatal("尾页主路段不满 limit ⇒ 不应发令牌") + } +} + +// AC3:offset 越界 ⇒ 空页且 nextCont == "";并钉死「判据看 mainLen 而非 len(results)」: +// 主路段 4 + carry 1(len(page)==limit 但 mainLen=limit,offset 越界/尾页会错发 token」的镜像。 +func TestNoContinuationOnEmptyOrShortMainPage(t *testing.T) { + mkMain := func(n int) []models.RecallResult { + out := make([]models.RecallResult, n) + for i := 0; i < n; i++ { + out[i] = models.RecallResult{ID: fmt.Sprintf("mem_%03d", i), Category: "distilled", Content: "m"} + } + return out + } + limit := 5 + + // A. offset 越界(=主路段长度)⇒ 空页 + 不发令牌 + prim := append(mkMain(5), models.RecallResult{ID: "peer:muc", Category: "peer"}) + page, ml := paginateRecallWithCarry(prim, 5, []string{"mem_000"}, limit) + if len(page) != 0 || ml != 0 { + t.Fatalf("越界页 len=%d mainLen=%d want 0/0", len(page), ml) + } + if shouldEmitContinuation(len(prim), limit+5+1, ml, limit, 5) { + t.Fatal("越界空页不得发令牌") + } + + // B. 主路段不满页但 len(page)==limit(4 主 + 1 carry 撑到 5)⇒ 不得发令牌 + prim2 := append(mkMain(4), models.RecallResult{ID: "pattern:a", Category: "pattern"}) + page2, ml2 := paginateRecallWithCarry(prim2, 0, nil, limit) + if len(page2) != 5 || ml2 != 4 { + t.Fatalf("短页 len=%d mainLen=%d want 5/4", len(page2), ml2) + } + if shouldEmitContinuation(len(prim2), limit, ml2, limit, 0) { + t.Fatal("主路段未满页 ⇒ 不得发令牌(判据必须看 mainLen,不是 len(results))") + } +}