feat(storage): t_5d28ad74 两路融合门一次接通 — ZHIYI_SORT_FUSION(默认关) score×(1+W_IMP×imp)+B_FB×(sat(useful)−sat(human_not_useful)); FeedbackScore.pos 移除 recall_verified_count(有害热度信号); 3 Go 测试(门关 delta=0 / 门开方向 / B_FB=0退化)

This commit is contained in:
小唯
2026-10-19 00:36:03 +08:00
parent 772a597a53
commit 37890e3095
3 changed files with 198 additions and 3 deletions
+7 -3
View File
@@ -13,8 +13,12 @@ var feedbackSaturation = envFloat("ZHIYI_FEEDBACK_K", 3)
// 本函数改读 DB 计数(干净、per-memory、双向),由 recall.go 以 env 门控
// 加性偏置的形式接入排序(ZHIYI_FEEDBACK_BIAS,默认 0 = 关闭)。
//
// 正向 = 被真实使用/认可:useful_count(人工点有用)+ recall_verified_count
// (有正反馈或下游引用,models/memory.go:69 定义的唯一正信号)。
// 正向 = 只信人工正反馈:useful_count(人工点有用)。
// ⚠️ 2026-10-19 (t_5d28ad74) 移除 recall_verified_count:它是热度信号
// (7413 条/总和 45527,会让 7433 条拿近满分偏置),已被离线重排(119 条金标
// 真实候选集)证实有害:含它 B=1.0 → MRR 0.7115→0.4535(线上 0.694→0.540 同向)。
// 只信 useful_count 后反馈偏置从「有害」变「有益」:B=0.2 MRR 0.7254。
// 详见卡 t_5d28ad74 结论 1/2。
// 负向 = 被真实否定/取代:human_not_useful_count(人工点没用)+
// dedup_downweight_count(去重降权)。
// 注意 not_useful_count 是 deprecated 混写列(GA-P0-1 已拆),本函数不读它。
@@ -22,7 +26,7 @@ func FeedbackScore(m *models.MemoryRecord) float64 {
if m == nil {
return 0
}
pos := float64(m.UsefulCount + m.RecallVerifiedCount)
pos := float64(m.UsefulCount)
neg := float64(m.HumanNotUsefulCount + m.DedupDownweightCount)
sat := func(x float64) float64 {
if x <= 0 {
+38
View File
@@ -39,6 +39,17 @@ var (
// 2026-10-13 反馈回路接通(t_8f1f6e5e):per-memory 反馈计数加性偏置。
// 0 = 关闭(默认)⇒ 与旧实现逐位等价,回滚只需把环境变量置 0。
feedbackBias = envFloat("ZHIYI_FEEDBACK_BIAS", 0)
// 2026-10-19 (t_5d28ad74) 两路融合门:反馈→排序 × importance→排序 一次接通。
// score' = score × (1 + W_IMP × importance) + B_FB × ( sat(useful,K) − sat(human_not_useful,K) )
// 参数全走 env:ZHIYI_SORT_W_IMP / ZHIYI_SORT_B_FB / ZHIYI_SORT_K。
// 默认关(ZHIYI_SORT_FUSION=0)⇒ 本门不执行 ⇒ 与旧实现逐位等价,回滚只需置 0。
// importance 只用「轻加权」(1+W_IMP×imp),乘性全权重会压坍相关性(离线已证,
// score×imp → MRR 0.5641 / score×(1+0.1imp) → 0.7244,见卡 t_5d28ad74 结论 3)。
// 反馈偏置的 pos 只信 useful_count(feedback_score.go 已移除 recall_verified_count)。
sortFusionOn = os.Getenv("ZHIYI_SORT_FUSION") == "1"
sortWImp = envFloat("ZHIYI_SORT_W_IMP", 0.15)
sortBFB = envFloat("ZHIYI_SORT_B_FB", 0.2)
sortK = envFloat("ZHIYI_SORT_K", 3)
// 2026-10-13 时间维检索(t_e9a4d18f P0):recency 加性偏置。
// 0 = 关闭(默认)⇒ 行为零变化,回滚只需置 0。
// 形式:+timeDecayBias * recency(UpdatedAt),recency ∈ [0,1],
@@ -720,6 +731,33 @@ func (p *RecallPipeline) recallUncached(query, namespace string, topK int, diver
}
}
// 2026-10-19 (t_5d28ad74) 两路融合门:反馈×importance 一次接通。
// 门默认关(ZHIYI_SORT_FUSION≠1 ⇒ 本循环不执行 ⇒ 与旧实现逐位等价)。
// score' = score×(1+W_IMP×importance) + B_FB×(sat(useful)−sat(human_not_useful))
// 放在 MMR 之前 ⇒ 选择与展示共用同一套分数(recall.go:325-331 的既有教训)。
// importance 用 storage/importance_rule.go 写入的档位值(core/wiki=0.7, legacy=0.4...)。
if sortFusionOn {
for i := range reranked {
idx := reranked[i].Index
if idx >= 0 && idx < len(candidates) {
m := &candidates[idx]
imp := m.Importance
if imp <= 0 {
imp = 1.0 // 未声明(存量 NULL)不惩罚
}
// 反馈项:pos=useful / neg=human_not_useful(与离线验证公式逐位一致)
fsat := func(x float64) float64 {
if x <= 0 {
return 0
}
return x / (x + sortK)
}
fb := fsat(float64(m.UsefulCount)) - fsat(float64(m.HumanNotUsefulCount))
reranked[i].Score = reranked[i].Score*(1+sortWImp*imp) + sortBFB*fb
}
}
}
// 2026-10-13 时间维检索(t_e9a4d18f P0):recency 加性偏置。
// 与 confidenceLambda / sourceTierBias / feedbackBias 同款:默认 0 ⇒ 本循环
// 不执行 ⇒ 与旧实现逐位等价,回滚只需把 ZHIYI_TIME_DECAY 置 0。
@@ -0,0 +1,153 @@
package storage
import (
"fmt"
"math"
"testing"
"github.com/xiaoxue/memoryweave/internal/models"
)
// t_5d28ad74 AC2 —— 两路融合门(ZHIYI_SORT_FUSION)3 用例:
// ① 门关逐位相同(默认 ⇒ 行为零变化)
// ② 门开方向正确(构造样本 rank 变化符合公式)
// ③ 反向对照(B_FB=0 退化为主只加 importance)
//
// 复用 fbBiasLance 桩(Search/Update/IncrementHumanNotUseful 同生产 storage 接口)。
// 注意:融合门直接写 reranked[i].Score,不走 MMR 前其它偏置(feedbackBias 等默认 0)。
// fusionRun 跑一次召回并返回 [id → (位次, 分数)];位次 0-based。
func fusionRun(t *testing.T, rows []models.MemoryRecord, queryVec []float32) map[string][2]float64 {
t.Helper()
p := newFbBiasPipeline(t, rows, queryVec)
res, _, err := p.RecallWithMeta("fbBiasQuery", "hermes-main", 5, 0.0)
if err != nil {
t.Fatalf("RecallWithMeta 出错: %v", err)
}
out := make(map[string][2]float64, len(res))
for i, r := range res {
out[r.ID] = [2]float64{float64(i), r.Score}
}
return out
}
// TestSortFusionZeroDiffWhenOff — AC2①门关判据:ZHIYI_SORT_FUSION 默认关(≠1)
// ⇒ 融合循环不执行 ⇒ 标记 useful/not_useful 前后召回**逐位相同**(id 序列 + 分数全同,delta=0)。
func TestSortFusionZeroDiffWhenOff(t *testing.T) {
oldFusion := sortFusionOn
sortFusionOn = false // 生产现状默认(ZHIYI_SORT_FUSION 未置 1)
defer func() { sortFusionOn = oldFusion }()
rows := []models.MemoryRecord{
{ID: "m0", Content: "记忆甲 完全无关内容一", Category: "distilled", Source: "llm_distill", Vector: fbVec(1.0), Importance: 0.7},
{ID: "m1", Content: "记忆乙 完全无关内容二", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.9), Importance: 0.7},
{ID: "m2", Content: "记忆丙 完全无关内容三", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.8), Importance: 0.7},
{ID: "m3", Content: "记忆丁 完全无关内容四", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.7), Importance: 0.7},
{ID: "m4", Content: "记忆戊 完全无关内容五", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.6), Importance: 0.7},
}
before := fusionRun(t, rows, fbVec(1.0))
// 同时标 useful=3 和 human_not_useful=1,制造强信号(门关时应被完全忽略)
for i := range rows {
if rows[i].ID == "m2" {
rows[i].UsefulCount = 3
rows[i].HumanNotUsefulCount = 1
}
}
after := fusionRun(t, rows, fbVec(1.0))
for id, b := range before {
a, ok := after[id]
if !ok {
t.Fatalf("门关时结果集合变了:标记后 %s 消失", id)
}
if a[0] != b[0] || a[1] != b[1] {
t.Fatalf("ZHIYI_SORT_FUSION 关时标记前后位次/分数必须逐位相同:%s before=(pos %.0f score %.4f) after=(pos %.0f score %.4f)",
id, b[0], b[1], a[0], a[1])
}
}
t.Logf("fusion 门关(默认): 标记前后 5 条逐位相同 ✓ m2 pos=%.0f score=%.4f(delta=0)", before["m2"][0], before["m2"][1])
}
// TestSortFusionDirectionOn — AC2②门开判据:ZHIYI_SORT_FUSION=1,
// W_IMP=0.15 / B_FB=0.2 / K=3(默认推荐值)。
// 构造:m2 与 m0 向量相近(score 接近),但 m2 UsefulCount=3(sat(3)=3/6=0.5)
// 且 Importance=0.7 ⇒ m2 应获 +B_FB*0.5 的反馈加成 + (1+0.15*0.7)=1.105 的 importance 加权,
// 相对基线位次上移。反向 m3 标 HumanNotUseful=3 ⇒ 应下滑。
func TestSortFusionDirectionOn(t *testing.T) {
oldFusion, oldW, oldB, oldK := sortFusionOn, sortWImp, sortBFB, sortK
sortFusionOn = true
sortWImp = 0.15
sortBFB = 0.2
sortK = 3
defer func() { sortFusionOn, sortWImp, sortBFB, sortK = oldFusion, oldW, oldB, oldK }()
rows := []models.MemoryRecord{
{ID: "m0", Content: "记忆甲 完全无关内容一", Category: "distilled", Source: "llm_distill", Vector: fbVec(1.0), Importance: 0.4},
{ID: "m1", Content: "记忆乙 完全无关内容二", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.99), Importance: 0.4},
{ID: "m2", Content: "记忆丙 完全无关内容三", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.98), Importance: 0.7, UsefulCount: 4},
{ID: "m3", Content: "记忆丁 完全无关内容四", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.97), Importance: 0.7, HumanNotUsefulCount: 4},
{ID: "m4", Content: "记忆戊 完全无关内容五", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.96), Importance: 0.4},
}
res := fusionRun(t, rows, fbVec(1.0))
// m2: 高有用 + 高 importance ⇒ 应上移到前 2(baseline 向量 0.98 本来第 3)
if res["m2"][0] > 1 {
t.Fatalf("门开后 useful 高分 m2 应上移前 2,实际 pos=%.0f(结果:%v)", res["m2"][0], rankMapStr(res))
}
// m3: 高负反馈 ⇒ 应下滑到后段(baseline 向量 0.97 第 4)
if res["m3"][0] < 2 {
t.Fatalf("门开后 not-useful 高分 m3 应下滑到后段,实际 pos=%.0f(结果:%v)", res["m3"][0], rankMapStr(res))
}
t.Logf("fusion 门开: m2(pos useful) pos=%.0f(上移✓), m3(pos not-useful) pos=%.0f(下滑✓)", res["m2"][0], res["m3"][0])
}
func rankMapStr(m map[string][2]float64) string {
order := []string{"m0", "m1", "m2", "m3", "m4"}
s := ""
for _, id := range order {
if v, ok := m[id]; ok {
s += fmt.Sprintf("%s=%.0f(%.4f) ", id, v[0], v[1])
}
}
return s
}
// TestSortFusionBfbZeroImportanceOnly — AC2③反向对照:B_FB=0 时融合退化为只加 importance。
// 即 feedback 项全消失(useful/not_useful 不再影响排序),只剩 score×(1+W_IMP×imp)。
// 判据:标 useful 前后位次/分数逐位相同(B_FB=0 ⇒ 反馈项贡献恒 0)。
func TestSortFusionBfbZeroImportanceOnly(t *testing.T) {
oldFusion, oldW, oldB, oldK := sortFusionOn, sortWImp, sortBFB, sortK
sortFusionOn = true
sortWImp = 0.15
sortBFB = 0.0 // 反向对照
sortK = 3
defer func() { sortFusionOn, sortWImp, sortBFB, sortK = oldFusion, oldW, oldB, oldK }()
rows := []models.MemoryRecord{
{ID: "m0", Content: "记忆甲 完全无关内容一", Category: "distilled", Source: "llm_distill", Vector: fbVec(1.0), Importance: 0.7},
{ID: "m1", Content: "记忆乙 完全无关内容二", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.9), Importance: 0.7},
{ID: "m2", Content: "记忆丙 完全无关内容三", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.8), Importance: 0.7},
}
before := fusionRun(t, rows, fbVec(1.0))
for i := range rows {
if rows[i].ID == "m2" {
rows[i].UsefulCount = 10 // 强 useful,但 B_FB=0 ⇒ 应被忽略
}
}
after := fusionRun(t, rows, fbVec(1.0))
for id, b := range before {
a, ok := after[id]
if !ok {
t.Fatalf("B_FB=0 时结果集合变了:%s 消失", id)
}
if math.Abs(a[1]-b[1]) > 1e-12 {
t.Fatalf("B_FB=0 时 useful 标记不得影响分数:%s before=%.6f after=%.6f", id, b[1], a[1])
}
}
t.Logf("B_FB=0(反向对照): useful 标记前后分数逐位相同 ✓ ⇒ 退化为主只加 importance ✓")
}