Files
memoryweave/go/internal/storage/searchcache_test.go
T
xiaoyi fe8126dd85 fix(recall): 缓存键补 topK/diversity + 失效改前缀寻址,修 main 上 3 个长期红用例
一、3 个 recall 命名空间测试长期红灯(真因:测试对全局缓存不设防)
RecallWithMeta 命中缓存即提前返回,而 SearchCacheInstance 是带 Redis L2 的包级单例
(跨 test 运行持久)⇒ 一旦缓存里有同 (query,namespace) 条目,Search 永不被调用。
用例断言「Search 被调用几次」,于是恒红。修:用例内隔离该全局(存值→置 nil→Cleanup 还原),
并补一条反证用例 TestRecallCachedPathSkipsSearch 固证「命中缓存就不查库」这一机制。
(三次反证均确认有鉴别力:撤掉隔离后两次都假红。)

二、缓存键缺 topK/diversity(真实缺陷,非测试问题)
键原为 (query,namespace),但召回结果随 topK 变化(rerank/MMR 取 topK*2、最终按 topK 截断),
而调用方 topK 各异(eval 用 3 / 5,HTTP 用客户端 Limit)⇒ 不同调用方串用同一条缓存、
拿到条数不对的结果(金标集评分会被静默少算;客户端要 50 条可能只拿到 5 条)。
修:键布局改为 <namespace>\x00<queryHash>\x00<topK>\x00<diversity>,可前缀寻址;
RecallWithMeta 在查缓存前先归一 topK(否则 topK<=0 的调用永远命中不了自己写的条目)。

三、Invalidate(namespace) 是静默空操作(真实缺陷)
旧实现把 entry.Results 反序列化成 map 再读 ["namespace"],而 Set 存的是 []RecallResult
(JSON 数组)⇒ 反序列化恒失败 ⇒ 一个键都删不掉。后果:记忆写入后调用方以为缓存已刷新,
实际最长 1h TTL 内持续返回陈旧召回。修:改为按键前缀匹配 + L2 DelPrefix(RedisCache.DelPrefix
用 SCAN 游标 + 批量 DEL,不用会阻塞 Redis 的 KEYS)。

验证:main 上 `go test ./... -count=1` 失败包 2→0、失败测试 5→0、通过包 7→9;
新增 8 条回归用例;反证 A(键退回不含 topK)、B(失效退回 payload 解析)、C(撤掉缓存隔离)
均确认会让对应用例变红。
2026-10-09 12:59:37 +08:00

136 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package storage
import (
"encoding/json"
"testing"
"time"
"github.com/xiaoxue/memoryweave/internal/models"
)
func mustResults(n int) []models.RecallResult {
out := make([]models.RecallResult, n)
for i := range out {
out[i] = models.RecallResult{ID: string(rune('a' + i)), Content: "c"}
}
return out
}
func getLen(t *testing.T, c *SearchCache, q, ns string, topK int, div float64) (int, bool) {
t.Helper()
raw, ok := c.Get(q, ns, topK, div)
if !ok {
return 0, false
}
var got []models.RecallResult
if err := json.Unmarshal(raw, &got); err != nil {
t.Fatalf("缓存内容不是 RecallResult 数组: %v", err)
}
return len(got), true
}
// 回归(核心):键必须区分 topK。
// 旧键 cacheKey(query,namespace) 不含 topK ⇒ eval(topK=3) 与 eval(topK=5)/HTTP(req.Limit)
// 会串用同一条缓存,调用方拿到条数不对的结果。
func TestSearchCacheKeyDistinguishesTopK(t *testing.T) {
c := NewSearchCache(100, time.Hour)
c.Set("同一句话", "hermes-main", 5, 0.3, mustResults(5))
c.Set("同一句话", "hermes-main", 3, 0.3, mustResults(3))
n5, ok := getLen(t, c, "同一句话", "hermes-main", 5, 0.3)
if !ok {
t.Fatal("topK=5 应命中")
}
if n5 != 5 {
t.Errorf("topK=5 应取回 5 条,实际 %d 条 ⇒ topK 未进键", n5)
}
n3, ok := getLen(t, c, "同一句话", "hermes-main", 3, 0.3)
if !ok {
t.Fatal("topK=3 应命中")
}
if n3 != 3 {
t.Errorf("topK=3 应取回 3 条,实际 %d 条 ⇒ topK 未进键", n3)
}
}
// 回归:键必须区分 diversity(MMR 选择随其变化)。
func TestSearchCacheKeyDistinguishesDiversity(t *testing.T) {
c := NewSearchCache(100, time.Hour)
c.Set("q", "ns", 5, 0.3, mustResults(3))
if _, ok := c.Get("q", "ns", 5, 0.5); ok {
t.Error("diversity=0.5 命中了 divergence=0.3 的条目 ⇒ diversity 未进键")
}
if _, ok := c.Get("q", "ns", 5, 0.3); !ok {
t.Error("相同 diversity 应命中")
}
}
// 回归:namespace 与 query 之间必须无键碰撞(旧键用 '|' 拼接后整体哈希,
// 新键用 \x00 分隔且 namespace 在前,需确认前缀关系不误伤)。
func TestSearchCacheKeyNoCrossNamespaceCollision(t *testing.T) {
c := NewSearchCache(100, time.Hour)
c.Set("q", "ns-a", 5, 0.3, mustResults(1))
if _, ok := c.Get("q", "ns-ab", 5, 0.3); ok {
t.Error("namespace 前缀相近时串了缓存(ns-a vs ns-ab)")
}
if _, ok := c.Get("q", "ns-a", 5, 0.3); !ok {
t.Error("同 namespace 应命中")
}
}
// 回归(真实 bug):Invalidate(namespace) 曾对数组型 payload 反序列化失败 ⇒ 静默空操作。
// 现为前缀匹配,必须真的删掉,且不得误删其他 namespace。
func TestSearchCacheInvalidateByNamespace(t *testing.T) {
c := NewSearchCache(100, time.Hour)
c.Set("q1", "hermes-main", 5, 0.3, mustResults(2))
c.Set("q2", "hermes-main", 3, 0.5, mustResults(2))
c.Set("q1", "wiki", 5, 0.3, mustResults(2))
c.Invalidate("hermes-main")
if _, ok := c.Get("q1", "hermes-main", 5, 0.3); ok {
t.Error("Invalidate(hermes-main) 后 q1 仍命中 ⇒ 失效是空操作")
}
if _, ok := c.Get("q2", "hermes-main", 3, 0.5); ok {
t.Error("Invalidate(hermes-main) 后 q2 仍命中 ⇒ 未按 namespace 全量失效")
}
if _, ok := c.Get("q1", "wiki", 5, 0.3); !ok {
t.Error("Invalidate(hermes-main) 误删了 wiki 的条目")
}
}
// 回归:InvalidateKey(query, namespace) 必须清掉该 query 的全部 topK/diversity 变体。
func TestSearchCacheInvalidateKeyAllVariants(t *testing.T) {
c := NewSearchCache(100, time.Hour)
c.Set("q", "ns", 5, 0.3, mustResults(1))
c.Set("q", "ns", 3, 0.5, mustResults(1))
c.Set("q-别的", "ns", 5, 0.3, mustResults(1))
c.InvalidateKey("q", "ns")
if _, ok := c.Get("q", "ns", 5, 0.3); ok {
t.Error("InvalidateKey 未清掉 (topK=5,div=0.3) 变体")
}
if _, ok := c.Get("q", "ns", 3, 0.5); ok {
t.Error("InvalidateKey 未清掉 (topK=3,div=0.5) 变体")
}
if _, ok := c.Get("q-别的", "ns", 5, 0.3); !ok {
t.Error("InvalidateKey 误删了别的 query")
}
}
// 反证:单一 query 下 namespace 是键的第一段,前缀匹配不会因 query 哈希前缀相同而误删。
func TestSearchCacheInvalidatePrefixIsExact(t *testing.T) {
c := NewSearchCache(100, time.Hour)
// "a" 与 "ab" 做 namespace:Invalidate("a") 不得删掉 "ab"
c.Set("q", "a", 5, 0.3, mustResults(1))
c.Set("q", "ab", 5, 0.3, mustResults(1))
c.Invalidate("a")
if _, ok := c.Get("q", "a", 5, 0.3); ok {
t.Error("Invalidate(a) 未生效")
}
if _, ok := c.Get("q", "ab", 5, 0.3); !ok {
t.Error("Invalidate(a) 前缀误伤 'ab'(separator 未生效)")
}
}