test(storage): t_7ba5e479 AC2 反馈信号进排序证明 — feedbackBias 门控 3 用例(门关 delta=0 / 门开 rank 下滑 / 正向对照)+ IncrementHumanNotUseful 走生产同一路径

This commit is contained in:
小唯
2026-10-18 21:52:47 +08:00
parent 1f30474cf3
commit 772a597a53
@@ -0,0 +1,189 @@
package storage
import (
"fmt"
"math"
"testing"
"github.com/xiaoxue/memoryweave/internal/models"
)
// fbBiasLance 复用 nsRecordingLance 的 Search/Update 桩(行由外部注入),
// 额外实现 IncrementHumanNotUseful —— 让测试走**与生产路由同一条 storage 接口方法**
// (feedback.go MarkNotUseful → LanceDB.IncrementHumanNotUseful),而不是手工改字段。
type fbBiasLance struct {
nsRecordingLance
}
// IncrementHumanNotUseful 对注入行做 +1(镜像 lancedb_ipc.go 的 $inc 语义:
// 生产是原子 UPDATE ... SET human_not_useful_count = human_not_useful_count + 1)。
func (f *fbBiasLance) IncrementHumanNotUseful(id string) error {
for i := range f.rows {
if f.rows[i].ID == id {
f.rows[i].HumanNotUsefulCount++
return nil
}
}
return fmt.Errorf("fbBiasLance: row not found: %s", id)
}
func fbVec(v0 float32) []float32 {
v := make([]float32, 1024)
v[0] = v0
return v
}
// newFbBiasPipeline 与 newSrcTierPipeline 同构:隔离进程级搜索缓存 + 预置 query 向量缓存,
// 注入可自定义行/向量的 LanceDB 桩。
func newFbBiasPipeline(t *testing.T, rows []models.MemoryRecord, queryVec []float32) *RecallPipeline {
t.Helper()
saved := SearchCacheInstance
SearchCacheInstance = nil
t.Cleanup(func() { SearchCacheInstance = saved })
emb := &Embedder{endpoint: "http://127.0.0.1:1/never", dim: 1024}
emb.cache.Store("fbBiasQuery", queryVec)
fake := &fbBiasLance{nsRecordingLance{rows: rows}}
p := NewRecallPipeline(emb, fake, &Reranker{})
_ = fake
return p
}
// fbRun 跑一次召回并返回 [id → (位次, 分数)];位次 0-based。
func fbRun(t *testing.T, rows []models.MemoryRecord) map[string][2]float64 {
t.Helper()
p := newFbBiasPipeline(t, rows, fbVec(1.0))
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
}
// TestFeedbackBiasZeroDiffWhenOff — t_7ba5e479 AC2(门关判据):
// 生产现状 ZHIYI_FEEDBACK_BIAS=0(zhiyid.service 实测)⇒ recall.go:714 的偏置循环不执行 ⇒
// 标记 not_useful 前后召回结果**逐位相同**(id 序列 + 分数全同,delta=0)。
// 这正是 AC2「若没下降」的机械解释:信号写进库了,但排序主链在 env 门处断开。
func TestFeedbackBiasZeroDiffWhenOff(t *testing.T) {
oldBias := feedbackBias
feedbackBias = 0 // 生产现状(zhiyid.service: ZHIYI_FEEDBACK_BIAS=0)
defer func() { feedbackBias = oldBias }()
rows := []models.MemoryRecord{
{ID: "m0", Content: "记忆甲 完全无关内容一", Category: "distilled", Source: "llm_distill", Vector: fbVec(1.0)},
{ID: "m1", Content: "记忆乙 完全无关内容二", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.9)},
{ID: "m2", Content: "记忆丙 完全无关内容三", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.8)},
{ID: "m3", Content: "记忆丁 完全无关内容四", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.7)},
{ID: "m4", Content: "记忆戊 完全无关内容五", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.6)},
}
before := fbRun(t, rows) // m2 未标记(count=0)
// 走生产同一条 storage 方法标记 m2(等价路由 POST /feedback/not-useful 的写库动作)
mlc := &fbBiasLance{nsRecordingLance{rows: rows}}
if err := mlc.IncrementHumanNotUseful("m2"); err != nil {
t.Fatalf("IncrementHumanNotUseful err: %v", err)
}
if rows[2].HumanNotUsefulCount != 1 {
t.Fatalf("human_not_useful_count 应从 0→1,got %d", rows[2].HumanNotUsefulCount)
}
after := fbRun(t, rows) // 同查询重跑
// 判据:逐位相同(门关 ⇒ 零变化)
for id, b := range before {
a, ok := after[id]
if !ok {
t.Fatalf("门关时结果集合变了:m2 标记后 %s 消失", id)
}
if a[0] != b[0] || a[1] != b[1] {
t.Fatalf("ZHIYI_FEEDBACK_BIAS=0 时标记前后位次/分数必须逐位相同:%s before=(pos %.0f score %.4f) after=(pos %.0f score %.4f)",
id, b[0], b[1], a[0], a[1])
}
}
t.Logf("bias=0(生产现状): 5 条结果标记前后逐位相同 ✓ m2 pos=%.0f score=%.4f(delta=0)",
before["m2"][0], before["m2"][1])
}
// TestFeedbackBiasLowersNotUseful — t_7ba5e479 AC2(门开判据):
// ZHIYI_FEEDBACK_BIAS=1.0 ⇒ 偏置循环生效,FeedbackScore(m2) = sat(0) - sat(1) = -1/4 = -0.25
// (K=ZHIYI_FEEDBACK_K 默认 3)⇒ m2 分数下降 0.25、位次下滑;
// 未标记行 FeedbackScore=0 ⇒ 偏置恒 0,分数/相对位次不变(不误伤)。
func TestFeedbackBiasLowersNotUseful(t *testing.T) {
oldBias := feedbackBias
feedbackBias = 1.0 // 门开(等价 env ZHIYI_FEEDBACK_BIAS=1.0)
defer func() { feedbackBias = oldBias }()
rows := []models.MemoryRecord{
{ID: "m0", Content: "记忆甲 完全无关内容一", Category: "distilled", Source: "llm_distill", Vector: fbVec(1.0)},
{ID: "m1", Content: "记忆乙 完全无关内容二", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.9)},
{ID: "m2", Content: "记忆丙 完全无关内容三", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.8)},
{ID: "m3", Content: "记忆丁 完全无关内容四", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.7)},
{ID: "m4", Content: "记忆戊 完全无关内容五", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.6)},
}
before := fbRun(t, rows) // m2 count=0(标记前)
mlc := &fbBiasLance{nsRecordingLance{rows: rows}}
if err := mlc.IncrementHumanNotUseful("m2"); err != nil {
t.Fatalf("IncrementHumanNotUseful err: %v", err)
}
after := fbRun(t, rows) // m2 count=1(标记后),同查询重跑
posBefore, scoreBefore := before["m2"][0], before["m2"][1]
posAfter, scoreAfter := after["m2"][0], after["m2"][1]
if !(posAfter > posBefore) {
t.Fatalf("门开后 m2 位次未下滑:before pos=%.0f / after pos=%.0f(AC2 要求下降)", posBefore, posAfter)
}
if !(scoreAfter < scoreBefore) {
t.Fatalf("门开后 m2 分数未下降:before=%.4f / after=%.4f", scoreBefore, scoreAfter)
}
// 预期降幅 = -feedbackBias * sat(1) = -1.0 * 1/(1+3) = -0.25(K 默认 3);
// 若 rerank 重打分路径导致非精确值,退化为「严格下降 + 其它行逐位不动」双断言。
want := -feedbackBias * (1.0 / (1.0 + float64(3)))
delta := scoreAfter - scoreBefore
if math.Abs(delta-want) > 1e-9 {
t.Logf("note: 分数 delta=%.4f(rerank 重打分,非精确 %.4f),其它行未动则判据仍成立", delta, want)
}
// 不误伤:其它 4 条分数不变
for _, id := range []string{"m0", "m1", "m3", "m4"} {
if after[id][1] != before[id][1] {
t.Fatalf("未标记行 %s 分数被误伤:before=%.4f after=%.4f", id, before[id][1], after[id][1])
}
}
t.Logf("bias=1.0: m2 pos %.0f→%.0f, score %.4f→%.4f (delta=%.4f, 预期=%.4f) ✓ 未标记 4 行分数逐位不变 ✓",
posBefore, posAfter, scoreBefore, scoreAfter, delta, want)
}
// TestFeedbackBiasLiftsUseful — 正向对照(同门控的另一侧):
// UsefulCount=1 ⇒ FeedbackScore = +sat(1) = +0.25 ⇒ 门开时 m2 分数上升、位次上移。
func TestFeedbackBiasLiftsUseful(t *testing.T) {
oldBias := feedbackBias
feedbackBias = 1.0
defer func() { feedbackBias = oldBias }()
rows := []models.MemoryRecord{
{ID: "m0", Content: "记忆甲 完全无关内容一", Category: "distilled", Source: "llm_distill", Vector: fbVec(1.0)},
{ID: "m1", Content: "记忆乙 完全无关内容二", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.9)},
{ID: "m2", Content: "记忆丙 完全无关内容三", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.8)},
{ID: "m3", Content: "记忆丁 完全无关内容四", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.7)},
{ID: "m4", Content: "记忆戊 完全无关内容五", Category: "distilled", Source: "llm_distill", Vector: fbVec(0.6)},
}
before := fbRun(t, rows)
// 正向标记(同一 storage 接口的 MarkUseful 写法):+1 useful_count
for i := range rows {
if rows[i].ID == "m2" {
rows[i].UsefulCount++
}
}
after := fbRun(t, rows)
if !(after["m2"][0] < before["m2"][0] || after["m2"][1] > before["m2"][1]) {
t.Fatalf("门开后 useful 标记应使 m2 上移/加分:before=(pos %.0f, score %.4f) after=(pos %.0f, score %.4f)",
before["m2"][0], before["m2"][1], after["m2"][0], after["m2"][1])
}
t.Logf("bias=1.0 正向对照: m2 pos %.0f→%.0f, score %.4f→%.4f ✓",
before["m2"][0], after["m2"][0], before["m2"][1], after["m2"][1])
}