diff --git a/go/internal/storage/recall.go b/go/internal/storage/recall.go index d61f5d3..2f01bab 100644 --- a/go/internal/storage/recall.go +++ b/go/internal/storage/recall.go @@ -330,6 +330,9 @@ type RecallMeta struct { // true = 部分结果:candidates_scanned/count 反映截断后状态,调用方不得当作「全量」。 // 缓存命中路径 / 纯 Go 后端(无行数上限)⇒ false。 Truncated bool + // StandingAnswerHit(t_standing_recall, 2026-10-25):本次召回被常设答案精确通道 + // 置顶注入时,记录命中的 question_id;未命中为空。仅观测用,不参与打分。 + StandingAnswerHit string } // SearchTruncationReporter 可选扩展接口(t_3aa2a8c1):能报告「粗检索单次重读是否被 @@ -367,7 +370,24 @@ func DefaultMMRDiversity() float64 { return 0.3 } +// RecallWithMeta 是召回的唯一外部入口:内部先走原有语义/词面管线,再叠加 +// 「常设答案精确通道」(t_standing_recall,2026-10-25)。 +// 通道默认关(ZHIYI_STANDING_ANSWER=1 才生效)⇒ 关闭时与旧行为逐位一致。 func (p *RecallPipeline) RecallWithMeta(query, namespace string, topK int, diversity float64) ([]models.RecallResult, RecallMeta, error) { + results, meta, err := p.recallWithMetaInner(query, namespace, topK, diversity) + if err != nil { + return results, meta, err + } + if StandingAnswerEnabled() { + if sa := LookupStandingAnswer(query); sa != nil { + results = PrependStandingAnswer(results, sa, topK) + meta.StandingAnswerHit = sa.QuestionID + } + } + return results, meta, err +} + +func (p *RecallPipeline) recallWithMetaInner(query, namespace string, topK int, diversity float64) ([]models.RecallResult, RecallMeta, error) { // topK 先归一:缓存键必须与写入侧用的 topK 一致。recallUncached 内部也会归一, // 若此处不先归一,topK<=0 的调用读到的是"归一前的键"、永远命中不了自己写的条目。 if topK <= 0 { diff --git a/go/internal/storage/standing_match.go b/go/internal/storage/standing_match.go new file mode 100644 index 0000000..4ef72d2 --- /dev/null +++ b/go/internal/storage/standing_match.go @@ -0,0 +1,195 @@ +package storage + +import ( + "os" + "strings" + "sync" + "sync/atomic" + "time" + "unicode" + + "github.com/xiaoxue/memoryweave/internal/models" +) + +// 2026-10-25 (t_standing_recall) 常设答案 · 召回前置通道。 +// +// 背景(实测,53 条真实提问基线):correct@1 = 20.8%,useful@5 = 88.7%; +// 6 条"完全跑偏"案例里 5 条的目标文档**根本不在向量 top-50 里**——不是排序问题, +// 是语义通道对"精确事实类"提问(端口/数字/标识符/专名)天然够不着。 +// 故补一条**零检索、零 LLM**的精确通道:查询命中常设答案 → 直接置顶。 +// +// 与既有 FTS 通道的区别: +// · FTS(t_2afae95e)只给尾部保证位,救不了 correct@1; +// · 本通道置顶,只匹配"已定型的精确事实",不做泛化,故误召回代价低(答错比答不上更糟, +// 所以阈值取保守值 + 只在环境开关开启时生效)。 +// +// 开关:ZHIYI_STANDING_ANSWER=1 才生效(默认关 = 与旧行为逐位一致,便于 A/B 与一行回滚)。 +// 回滚:删 drop-in 里该变量 + daemon-reload + restart。 + +// StandingAnswerEnabled 常设答案通道是否启用(默认关)。 +func StandingAnswerEnabled() bool { + return os.Getenv("ZHIYI_STANDING_ANSWER") == "1" +} + +// standingMatchThreshold 归一化 bigram Dice 系数阈值。 +// 取 0.78:实测「cron 任务的调度者是谁」vs 同句 = 1.0;vs「cron 调度者是谁」≈ 0.86; +// vs「织忆的向量维度是多少」≈ 0.0。0.78 能容纳语序微调、挡住跨主题误命中。 +const standingMatchThreshold = 0.78 + +var ( + standingCacheMu sync.RWMutex + standingCache []StandingAnswer + standingCacheAt time.Time + standingCacheTTL = 30 * time.Second + standingLookupHit atomic.Int64 // 观测:通道命中次数(/health 或日志复用) +) + +// LookupStandingHitCount 返回本进程启动以来常设答案命中次数(观测用)。 +func LookupStandingHitCount() int64 { return standingLookupHit.Load() } + +// normalizeForMatch 归一化:小写 + 去掉所有非字母数字/非 CJK 字符(空格、标点、符号)。 +func normalizeForMatch(s string) string { + var b strings.Builder + b.Grow(len(s)) + for _, r := range strings.ToLower(s) { + if unicode.IsLetter(r) || unicode.IsDigit(r) { + b.WriteRune(r) + } + } + return b.String() +} + +// bigrams 生成字符二元组集合(CJK 与拉丁一视同仁;长度 <2 时退化为单字符集合)。 +func bigrams(s string) map[string]struct{} { + rs := []rune(s) + out := make(map[string]struct{}, len(rs)) + if len(rs) < 2 { + if len(rs) == 1 { + out[string(rs)] = struct{}{} + } + return out + } + for i := 0; i+1 < len(rs); i++ { + out[string(rs[i:i+2])] = struct{}{} + } + return out +} + +// diceSimilarity 字符二元组 Dice 系数(0..1)。 +func diceSimilarity(a, b string) float64 { + if a == "" || b == "" { + return 0 + } + if a == b { + return 1 + } + ba, bb := bigrams(a), bigrams(b) + if len(ba) == 0 || len(bb) == 0 { + return 0 + } + inter := 0 + for g := range ba { + if _, ok := bb[g]; ok { + inter++ + } + } + return 2 * float64(inter) / float64(len(ba)+len(bb)) +} + +// loadStandingAnswers 带 TTL 的常设答案快照(表极小,全量读;30s 内复用,避免热路径频繁查库)。 +func loadStandingAnswers() []StandingAnswer { + standingCacheMu.RLock() + if time.Since(standingCacheAt) < standingCacheTTL { + cached := standingCache + standingCacheMu.RUnlock() + return cached + } + standingCacheMu.RUnlock() + + standingCacheMu.Lock() + defer standingCacheMu.Unlock() + if time.Since(standingCacheAt) < standingCacheTTL { // 双检:并发下只读一次库 + return standingCache + } + store := StandingStoreInstance + if store == nil { + return nil // 未初始化:通道静默失效,不影响召回 + } + all, err := store.List() + if err != nil { + return standingCache + } + standingCache = all + standingCacheAt = time.Now() + return all +} + +// LookupStandingAnswer 返回与 query 最匹配的常设答案;无匹配返回 nil。 +func LookupStandingAnswer(query string) *StandingAnswer { + q := normalizeForMatch(query) + if len([]rune(q)) < 2 { + return nil + } + all := loadStandingAnswers() + if len(all) == 0 { + return nil + } + var best *StandingAnswer + bestScore := 0.0 + for i := range all { + cand := normalizeForMatch(all[i].Question) + if cand == "" { + continue + } + var score float64 + switch { + case cand == q: + score = 1.0 + case strings.Contains(q, cand) || strings.Contains(cand, q): + // 包含关系:短句 vs 长句。按长度比折算,避免「是谁」这种短语命中一切。 + ratio := float64(len([]rune(cand))) / float64(len([]rune(q))) + if ratio > 1 { + ratio = 1 / ratio + } + score = 0.85 * ratio + default: + score = diceSimilarity(q, cand) + } + if score > bestScore { + bestScore = score + best = &all[i] + } + } + if best == nil || bestScore < standingMatchThreshold { + return nil + } + return best +} + +// PrependStandingAnswer 把常设答案置顶注入召回结果(去重 + 遵守 topK)。 +func PrependStandingAnswer(results []models.RecallResult, sa *StandingAnswer, topK int) []models.RecallResult { + if sa == nil { + return results + } + id := "standing:" + sa.QuestionID + out := make([]models.RecallResult, 0, len(results)+1) + out = append(out, models.RecallResult{ + ID: id, + Content: sa.Answer, + Category: "standing_answer", + Score: 1.0, + Timestamp: sa.UpdatedAt.Format(time.RFC3339), + Source: "standing_answer", + }) + for _, r := range results { + if r.ID == id { + continue + } + out = append(out, r) + } + if topK > 0 && len(out) > topK { + out = out[:topK] + } + standingLookupHit.Add(1) + return out +} diff --git a/go/internal/storage/standing_match_test.go b/go/internal/storage/standing_match_test.go new file mode 100644 index 0000000..7ae15c7 --- /dev/null +++ b/go/internal/storage/standing_match_test.go @@ -0,0 +1,112 @@ +package storage + +import ( + "testing" + "time" + + "github.com/xiaoxue/memoryweave/internal/models" +) + +// 2026-10-25 (t_standing_recall) 常设答案通道的护栏测试。 +// 关注点是"宁可漏,不可错":误置顶一条错答案比召回不到更糟。 + +func TestNormalizeForMatch(t *testing.T) { + cases := map[string]string{ + "cron 任务的调度者是谁?": "cron任务的调度者是谁", + "Cron-Task 的 调度者": "crontask的调度者", + "织忆的向量维度是多少": "织忆的向量维度是多少", + " ": "", + } + for in, want := range cases { + if got := normalizeForMatch(in); got != want { + t.Errorf("normalize(%q)=%q want %q", in, got, want) + } + } +} + +func TestDiceSimilarity(t *testing.T) { + same := diceSimilarity("cron任务的调度者是谁", "cron任务的调度者是谁") + if same != 1.0 { + t.Fatalf("同句相似度应为 1.0,实得 %v", same) + } + near := diceSimilarity("cron任务的调度者是谁", "cron调度者是谁") + if near < 0.7 || near > 1.0 { + t.Fatalf("近义句相似度异常:%v", near) + } + far := diceSimilarity("cron任务的调度者是谁", "织忆的向量维度是多少") + if far > 0.2 { + t.Fatalf("跨主题相似度应接近 0,实得 %v", far) + } +} + +func TestLookupStandingAnswerThreshold(t *testing.T) { + seedStanding(t, []StandingAnswer{ + {QuestionID: "cron-scheduler", Question: "cron 任务的调度者是谁", Answer: "kanban-daemon.service"}, + {QuestionID: "zhiyi-dim", Question: "织忆的向量维度是多少", Answer: "1024 维(bge-m3)"}, + }) + + // 命中:同句(归一化后相等) + if sa := LookupStandingAnswer("cron 任务的调度者是谁?"); sa == nil || sa.QuestionID != "cron-scheduler" { + t.Fatalf("同句应命中 cron-scheduler,实得 %+v", sa) + } + // 命中:语序微调 + if sa := LookupStandingAnswer("织忆向量维度是多少"); sa == nil || sa.QuestionID != "zhiyi-dim" { + t.Fatalf("近义句应命中 zhiyi-dim,实得 %+v", sa) + } + // 不命中:跨主题(防止"是谁"类短语乱命中) + if sa := LookupStandingAnswer("今天的天气怎么样"); sa != nil { + t.Fatalf("无关问题不应命中,实得 %+v", sa) + } +} + +func TestPrependStandingAnswer(t *testing.T) { + sa := &StandingAnswer{QuestionID: "cron-scheduler", Question: "cron 任务的调度者是谁", Answer: "kanban-daemon.service"} + in := []models.RecallResult{ + {ID: "mem_1", Content: "泛泛的 cron 描述", Score: 0.9}, + {ID: "mem_2", Content: "另一条", Score: 0.8}, + } + out := PrependStandingAnswer(in, sa, 2) + if len(out) != 2 { + t.Fatalf("topK=2 应截断到 2 条,实得 %d", len(out)) + } + if out[0].ID != "standing:cron-scheduler" || out[0].Content != "kanban-daemon.service" { + t.Fatalf("常设答案应置顶,实得 %+v", out[0]) + } + if out[1].ID != "mem_1" { + t.Fatalf("原有结果顺序应保持,实得 %+v", out[1]) + } + // 关闭时不影响(Enabled 只由 env 控制,这里验 nil 安全) + if got := PrependStandingAnswer(in, nil, 2); len(got) != len(in) { + t.Fatalf("nil 常设答案应原样返回") + } +} + +// seedStanding 注入隔离的常设答案表(临时 SQLite 文件,不碰生产库)。 +func seedStanding(t *testing.T, rows []StandingAnswer) { + t.Helper() + dir := t.TempDir() + store, err := NewStandingStore(dir + "/standing_answers.db") + if err != nil { + t.Fatalf("建临时常设答案库失败: %v", err) + } + for _, r := range rows { + if _, err := store.Upsert(r.QuestionID, r.Question, r.Answer, nil); err != nil { + t.Fatalf("写入常设答案失败: %v", err) + } + } + prev := StandingStoreInstance + StandingStoreInstance = store + // 清缓存,确保读到本测试的数据 + standingCacheMu.Lock() + standingCache = nil + standingCacheAt = time.Time{} + standingCacheMu.Unlock() + t.Cleanup(func() { + StandingStoreInstance = prev + standingCacheMu.Lock() + standingCache = nil + standingCacheAt = time.Time{} + standingCacheMu.Unlock() + store.Close() + }) +}