refactor(storage): t_5028c14d 抽出召回融合纯函数 + 回归测试锁死「频率/时间衰减参与排序」 — Search()内联算法逐位等价抽到 quality_fusion.go(computeRecencyFactor/computeImportance/fuseQuality), 行为零变化不改数值; 新增单测锁定频率↑/越近→融合分↑; go build 0; 除既有flaky TestCommitConflictGate_E2E(干净HEAD同败)外全绿

This commit is contained in:
小唯
2026-10-14 02:05:00 +08:00
parent aab877a13c
commit ef4f379736
3 changed files with 156 additions and 37 deletions
+6 -37
View File
@@ -7,7 +7,6 @@ import (
"fmt"
"io"
"log"
"math"
"net"
"os"
"path/filepath"
@@ -257,62 +256,32 @@ func (rc *RustLanceDBClient) Search(table string, vector []float32, topK int, na
if namespaceFilter != "" && r.Namespace != namespaceFilter {
continue
}
// 重要性计算:recency_factor × (1 + log(1 + recall_count))
// 重要性计算 + 质量融合 —— 纯函数在 quality_fusion.go(t_5028c14d)
daysSinceRecall := 30.0 // default for never-recalled
if r.LastRecalledAt != "" {
if lastT, err := time.Parse(time.RFC3339, r.LastRecalledAt); err == nil {
daysSinceRecall = time.Since(lastT).Hours() / 24.0
}
}
recencyFactor := 1.0
if daysSinceRecall > 7 {
recencyFactor = 0.5 // 7天内高权重,之后折半
}
if daysSinceRecall > 30 {
recencyFactor = 0.2 // 30天以上权重很低
}
// GA-P0-2 (t_c6d41ac6):排序去偏用 recall_seen_count(观测/选择信号);
// recall_verified_count 只参与衰减/遗忘决策,不参与排序。
recallCount := float64(r.RecallSeenCount)
computedImportance := recencyFactor * (1.0 + math.Log(1.0+recallCount))
// VolatileFlag 惩罚
if r.VolatileFlag {
computedImportance *= 0.5
}
computedImportance := computeImportance(daysSinceRecall, r.RecallSeenCount, r.VolatileFlag)
// QualityScore 融合(归一化到 0-1 范围)
// t_631ab13d:先存原始 DB 值(r.QualityScore 是 LanceDB 列直接值),
// 融合后 finalQuality 写进 QualityScore(供排序),RawQualityScore 保留原值(供 recall 响应展示)
dbRawQS := r.QualityScore
finalQuality := math.Min(computedImportance, 1.0)
// 2026-10-12:存量 quality_score 有 54.2% 不可信(0=未打分 / 0.755=历史常量 bug)。
// 修入口治不了存量 —— 消费端必须自己判定,不可信时把权重让给可信信号。
qsTrusted := models.IsReliableQuality(r.QualityScore)
if qsTrusted {
// QualityScore from LLM (0-1) + computed importance, capped at 1.0
finalQuality = math.Min(0.4*computedImportance+0.6*r.QualityScore, 1.0)
} else if qstrustVerbose {
if !models.IsReliableQuality(r.QualityScore) && qstrustVerbose {
log.Printf("[qstrust-ipc] 不可信质量分已剔除 id=%s rawQS=%.6f → 仅用 importance %.4f",
r.ID, r.QualityScore, computedImportance)
}
// V 值融合:用户决策信号(correct/useful/not-useful)
var vVal float64
if VValueProvider != nil {
vVal := VValueProvider(r.ID)
if vVal > 0 {
if qsTrusted {
// 30% V值 + 30% importance + 40% LLM quality
finalQuality = 0.3*vVal + 0.3*computedImportance + 0.4*r.QualityScore
} else {
// 质量分不可信 → 去掉它的 40%,剩余 30:30 重新归一为 50:50。
// 这样既不让假分数进来,也不因为"少了一项"而整体压沉。
finalQuality = 0.5*vVal + 0.5*computedImportance
}
if finalQuality > 1.0 {
finalQuality = 1.0
}
}
vVal = VValueProvider(r.ID)
}
finalQuality := fuseQuality(computedImportance, r.QualityScore, vVal)
out = append(out, models.MemoryRecord{
ID: r.ID, Content: r.Content, Category: r.Category, Namespace: r.Namespace,
+63
View File
@@ -0,0 +1,63 @@
package storage
import (
"math"
"github.com/xiaoxue/memoryweave/internal/models"
)
// 召回排序「频率 + 时间衰减」融合公式 —— 从 lancedb_ipc.go Search() 内联代码抽出的纯函数。
// 行为逐位等价于原内联实现(t_5028c14d),不改任何数值常量。
// 调用链:recall.go Recall() → Search() → 本文件三函数 → QualityScore 供排序。
// computeRecencyFactor 时间衰减因子:>30d→0.2 / >7d→0.5 / else 1.0(原 :267-273)。
func computeRecencyFactor(days float64) float64 {
if days > 30 {
return 0.2 // 30天以上权重很低
}
if days > 7 {
return 0.5 // 7天内高权重,之后折半
}
return 1.0
}
// computeImportance 重要度 = recencyFactor × (1 + log(1 + recall_seen_count));
// volatile 减半(原 :260-282)。recall_seen_count 是观测/选择信号(GA-P0-2),
// recall_verified_count 只参与衰减/遗忘决策,不参与排序。
func computeImportance(days float64, recallSeenCount int64, volatile bool) float64 {
rf := computeRecencyFactor(days)
imp := rf * (1.0 + math.Log(1.0+float64(recallSeenCount)))
if volatile {
imp *= 0.5
}
return imp
}
// fuseQuality 融合 QualityScore(归一化 0-1):computedImportance 与 LLM 质量分、
// 用户决策 V 值按可信度分支加权(原 :284-315)。
// vValue ≤ 0 表示无用户决策信号(VValueProvider nil 或返回非正)→ 不进 V 分支。
// rawQuality 经 models.IsReliableQuality 判定可信度(0=未打分/历史常量 bug 视为不可信)。
func fuseQuality(computedImportance, rawQuality, vValue float64) float64 {
qsTrusted := models.IsReliableQuality(rawQuality)
var finalQuality float64
if qsTrusted {
// QualityScore from LLM (0-1) + computed importance, capped at 1.0
finalQuality = math.Min(0.4*computedImportance+0.6*rawQuality, 1.0)
} else {
// 质量分不可信 → 仅用 importance([qstrust-ipc] 日志留在调用处)
finalQuality = math.Min(computedImportance, 1.0)
}
if vValue > 0 {
if qsTrusted {
// 30% V值 + 30% importance + 40% LLM quality
finalQuality = 0.3*vValue + 0.3*computedImportance + 0.4*rawQuality
} else {
// 质量分不可信 → 去掉它的 40%,剩余 30:30 重新归一为 50:50。
finalQuality = 0.5*vValue + 0.5*computedImportance
}
if finalQuality > 1.0 {
finalQuality = 1.0
}
}
return finalQuality
}
@@ -0,0 +1,87 @@
package storage
import (
"math"
"testing"
)
// 锁死「频率/时间衰减参与召回排序」:MOA 三件套 ③ 测试覆盖。
// 行为逐位等价于原 lancedb_ipc.go Search() 内联公式(AC3 手工期望值)。
func TestComputeRecencyFactor(t *testing.T) {
cases := []struct {
days float64
want float64
}{
{1.0, 1.0}, // ≤7d 高权重
{7.0, 1.0}, // 边界:>7 才折半
{10.0, 0.5}, // 7~30d 折半
{30.0, 0.5}, // 边界:>30 才再降
{31.0, 0.2}, // >30d 很低
{100.0, 0.2},
}
for _, c := range cases {
if got := computeRecencyFactor(c.days); got != c.want {
t.Errorf("computeRecencyFactor(%v)=%v, want %v", c.days, got, c.want)
}
}
}
func TestComputeImportance(t *testing.T) {
// seen=0,days=1 → rf=1, imp=1*(1+log(1))=1.0
if got := computeImportance(1.0, 0, false); got != 1.0 {
t.Errorf("imp(seen=0,days=1)=%v, want 1.0", got)
}
// seen=7,days=1 → 1*(1+ln8)=3.0794…
if got := computeImportance(1.0, 7, false); math.Abs(got-(1+math.Log(8))) > 1e-9 {
t.Errorf("imp(seen=7,days=1)=%v, want %.4f", got, 1+math.Log(8))
}
// seen=0,days=10 → rf=0.5, imp=0.5
if got := computeImportance(10.0, 0, false); got != 0.5 {
t.Errorf("imp(seen=0,days=10)=%v, want 0.5", got)
}
// 同样频次+同样时间,volatile 减半:seen=7,days=1,volatile → 0.5*3.0794
if got := computeImportance(1.0, 7, true); math.Abs(got-0.5*(1+math.Log(8))) > 1e-9 {
t.Errorf("imp(volatile)=%v, want %.4f", got, 0.5*(1+math.Log(8)))
}
// 频率单调性:同 rf 下 seen 越大 imp 越大
a := computeImportance(1.0, 0, false)
b := computeImportance(1.0, 3, false)
c := computeImportance(1.0, 7, false)
if !(a < b && b < c) {
t.Errorf("imp 应随频率单调增,got a=%v b=%v c=%v", a, b, c)
}
}
func TestFuseQuality(t *testing.T) {
// raw 可信 + 无 V 值 → 0.4*imp+0.6*raw, cap 1.0
// raw=0.605, imp=1.0 → 0.4*1+0.6*0.605=0.763
if got := fuseQuality(1.0, 0.605, 0); math.Abs(got-0.763) > 1e-9 {
t.Errorf("fuse(imp=1,raw=0.605,可信)=%v, want 0.763", got)
}
// 频率越高 → 融合分越高(可信+无V 分支)
lo := fuseQuality(computeImportance(1.0, 0, false), 0.605, 0)
hi := fuseQuality(computeImportance(1.0, 7, false), 0.605, 0)
if !(lo < hi) {
t.Errorf("fuse 应随 frequency 增,got lo=%v hi=%v", lo, hi)
}
// 越近 → 融合分越高
old := fuseQuality(computeImportance(31.0, 7, false), 0.605, 0)
near := fuseQuality(computeImportance(1.0, 7, false), 0.605, 0)
if !(old < near) {
t.Errorf("fuse 应随 recency 增,got old=%v near=%v", old, near)
}
// V 值分支:可信 → 0.3*v + 0.3*imp + 0.4*raw
if got := fuseQuality(1.0, 0.605, 0.9); math.Abs(got-(0.3*0.9+0.3*1.0+0.4*0.605)) > 1e-9 {
t.Errorf("fuse(V=0.9,可信)=%v, want %v", got, 0.3*0.9+0.3*1.0+0.4*0.605)
}
// 不可信 + 有 V → 0.5*v+0.5*imp(raw 被剔除)
// raw=0(不可信), imp=1.0, v=0.9 → 0.5*0.9+0.5*1.0=0.95
if got := fuseQuality(1.0, 0, 0.9); math.Abs(got-0.95) > 1e-9 {
t.Errorf("fuse(V=0.9,不可信)=%v, want 0.95", got)
}
// 融合结果有界 [0,1]
if got := fuseQuality(10.0, 1.0, 1.0); got > 1.0 {
t.Errorf("fuse 应 cap 到 1.0,got %v", got)
}
}