From 0bdcae2b91a998be41e93867cdf47c174480a0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Tue, 13 Oct 2026 22:19:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(storage):=20t=5F8f1f6e5e=20=E5=8F=8D?= =?UTF-8?q?=E9=A6=88=E5=9B=9E=E8=B7=AF=E6=8E=A5=E9=80=9A=E2=80=94=E2=80=94?= =?UTF-8?q?FeedbackScore()(env=E9=97=A8=E6=8E=A7=E5=8A=A0=E6=80=A7?= =?UTF-8?q?=E5=81=8F=E7=BD=AE,=E9=BB=98=E8=AE=A40)+=E5=8D=95=E6=B5=8B5/5;?= =?UTF-8?q?=20ep=5F*=E4=BB=8EVValueProvider=E6=8E=92=E9=99=A4(=E5=8F=AA?= =?UTF-8?q?=E6=94=B9=E8=AF=BB=E4=BE=A7);=20ZHIYI=5FFEEDBACK=5FBIAS=3D0?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=E9=9B=B6=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/internal/api/server.go | 6 ++++ go/internal/storage/feedback_score.go | 34 ++++++++++++++++++++++ go/internal/storage/feedback_score_test.go | 30 +++++++++++++++++++ go/internal/storage/recall.go | 17 +++++++++++ 4 files changed, 87 insertions(+) create mode 100644 go/internal/storage/feedback_score.go create mode 100644 go/internal/storage/feedback_score_test.go diff --git a/go/internal/api/server.go b/go/internal/api/server.go index 9937d8b..8cfcdfe 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -328,6 +328,12 @@ func NewServer() http.Handler { // 注入 V 值查询函数到存储层(召回排序融合) storage.VValueProvider = func(memoryID string) float64 { + // 2026-10-13:ep_* 是蒸馏自证(恒 1.0),零区分度且淹没真实 mem_* 信号 + // (实测 ep_* 4304 个 vs mem_* 24 个)。召回目标全是 mem_*,ep_* 的 V 值 + // 不参与排序即可,无需删 Redis 数据(保留可回溯)。 + if strings.HasPrefix(memoryID, "ep_") { + return 0 + } return vPropagator.GetMemVValue(memoryID) } // 在蒸馏完成回调中记录 V 值 diff --git a/go/internal/storage/feedback_score.go b/go/internal/storage/feedback_score.go new file mode 100644 index 0000000..d924fee --- /dev/null +++ b/go/internal/storage/feedback_score.go @@ -0,0 +1,34 @@ +package storage + +import "github.com/xiaoxue/memoryweave/internal/models" + +// feedbackSaturation 是饱和常数 K:sat(x)=x/(x+K)。 +// 计数无上界(可被反复标记),必须压进 [0,1) 才能当加性偏置用。 +var feedbackSaturation = envFloat("ZHIYI_FEEDBACK_K", 3) + +// FeedbackScore 把 per-memory 反馈计数压成有界双向分 ∈ (-1, 1)。 +// +// 设计(2026-10-13):排序此前只读 V 值(VValueProvider),而 V 值轴被 ep_* +// 蒸馏自证(4304 个恒 1.0)污染、mem_* 覆盖仅 0.067% ⇒ 反馈等于没接上。 +// 本函数改读 DB 计数(干净、per-memory、双向),由 recall.go 以 env 门控 +// 加性偏置的形式接入排序(ZHIYI_FEEDBACK_BIAS,默认 0 = 关闭)。 +// +// 正向 = 被真实使用/认可:useful_count(人工点有用)+ recall_verified_count +// (有正反馈或下游引用,models/memory.go:69 定义的唯一正信号)。 +// 负向 = 被真实否定/取代:human_not_useful_count(人工点没用)+ +// dedup_downweight_count(去重降权)。 +// 注意 not_useful_count 是 deprecated 混写列(GA-P0-1 已拆),本函数不读它。 +func FeedbackScore(m *models.MemoryRecord) float64 { + if m == nil { + return 0 + } + pos := float64(m.UsefulCount + m.RecallVerifiedCount) + neg := float64(m.HumanNotUsefulCount + m.DedupDownweightCount) + sat := func(x float64) float64 { + if x <= 0 { + return 0 + } + return x / (x + feedbackSaturation) + } + return sat(pos) - sat(neg) +} \ No newline at end of file diff --git a/go/internal/storage/feedback_score_test.go b/go/internal/storage/feedback_score_test.go new file mode 100644 index 0000000..fb737b8 --- /dev/null +++ b/go/internal/storage/feedback_score_test.go @@ -0,0 +1,30 @@ +package storage + +import ( + "testing" + + "github.com/xiaoxue/memoryweave/internal/models" +) + +func TestFeedbackScore_Bounds(t *testing.T) { + cases := []struct { + name string + m models.MemoryRecord + want float64 + }{ + {"零反馈=0", models.MemoryRecord{}, 0}, + {"正反馈饱和→趋近1", models.MemoryRecord{UsefulCount: 999}, 0.9966}, + {"负反馈→负", models.MemoryRecord{HumanNotUsefulCount: 999}, -0.9966}, + {"正负相抵=0", models.MemoryRecord{UsefulCount: 3, HumanNotUsefulCount: 3}, 0}, + {"上限=1(不含)", models.MemoryRecord{UsefulCount: 1 << 30}, 0.9999}, + } + for _, c := range cases { + got := FeedbackScore(&c.m) + if got < -1 || got > 1 { + t.Fatalf("%s: 越界 %v", c.name, got) + } + if diff := got - c.want; diff > 0.01 || diff < -0.01 { + t.Errorf("%s: got %v want ~%v", c.name, got, c.want) + } + } +} \ No newline at end of file diff --git a/go/internal/storage/recall.go b/go/internal/storage/recall.go index 7d9ace4..280f8b3 100644 --- a/go/internal/storage/recall.go +++ b/go/internal/storage/recall.go @@ -34,6 +34,9 @@ var ( // 让「我们定了什么/阈值怎么定的」这类问题召回出决定本体而非流水。0 = 关闭。 // 流水类(llm_distill/episode)不加分也不降权——偏置只抬决策,不动流水的相对位次。 sourceTierBias = envFloat("ZHIYI_SOURCE_TIER_BIAS", 0) // 0 = 关闭 + // 2026-10-13 反馈回路接通(t_8f1f6e5e):per-memory 反馈计数加性偏置。 + // 0 = 关闭(默认)⇒ 与旧实现逐位等价,回滚只需把环境变量置 0。 + feedbackBias = envFloat("ZHIYI_FEEDBACK_BIAS", 0) ) func envFloat(key string, def float64) float64 { @@ -352,6 +355,20 @@ func (p *RecallPipeline) recallUncached(query, namespace string, topK int, diver } } + // 2026-10-13 反馈回路接通(t_8f1f6e5e):per-memory 反馈计数加性偏置。 + // 与 confidenceLambda / sourceTierBias 同款:默认 0 ⇒ 本循环不执行 ⇒ + // 与旧实现逐位等价,回滚只需把 ZHIYI_FEEDBACK_BIAS 置 0。 + // 放在 MMR 之前 ⇒ 选择与展示共用同一套分数(recall.go:325-331 的既有教训: + // 偏置加在 MMR 之后就只改数字、不改结果集合)。 + if feedbackBias > 0 { + for i := range reranked { + idx := reranked[i].Index + if idx >= 0 && idx < len(candidates) { + reranked[i].Score += feedbackBias * FeedbackScore(&candidates[idx]) + } + } + } + // Step 4: MMR diversity finalIndices := MMRSelect(reranked, candidates, topK*2, diversity)