From 1b6920e8e15902b00505e85247a3073647e8680f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=94=AF?= Date: Tue, 13 Oct 2026 07:26:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(storage):=20t=5Fbe3e958a=20=E5=8F=AC?= =?UTF-8?q?=E5=9B=9E=E6=8E=92=E5=BA=8F=E5=8A=A0=E6=9D=A5=E6=BA=90=E5=88=86?= =?UTF-8?q?=E5=B1=82=E5=81=8F=E7=BD=AE=E2=80=94=E2=80=94=E5=86=B3=E7=AD=96?= =?UTF-8?q?=E7=B1=BB(wiki:decision)=E5=9C=A8=20MMR=20=E5=89=8D=E5=8A=A0?= =?UTF-8?q?=E6=80=A7=E5=8A=A0=E5=88=86=EF=BC=8C=E6=B5=81=E6=B0=B4(llm=5Fdi?= =?UTF-8?q?still/episode)=E4=B8=8D=E5=8A=A8=EF=BC=9Benv=20ZHIYI=5FSOURCE?= =?UTF-8?q?=5FTIER=5FBIAS=20=E6=8E=A7=E5=88=B6(0=3D=E5=85=B3)=EF=BC=8B?= =?UTF-8?q?=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/internal/storage/recall.go | 25 ++++ .../storage/recall_source_tier_test.go | 120 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 go/internal/storage/recall_source_tier_test.go diff --git a/go/internal/storage/recall.go b/go/internal/storage/recall.go index 8cfcdb2..7d9ace4 100644 --- a/go/internal/storage/recall.go +++ b/go/internal/storage/recall.go @@ -30,6 +30,10 @@ import ( var ( confidenceLambda = envFloat("ZHIYI_CONFIDENCE_LAMBDA", 0) // 0 = 关闭 confidenceMean = envFloat("ZHIYI_CONFIDENCE_MEAN", 0.7) // 去中心基准 + // t_be3e958a 来源分层偏置:决策类来源(wiki:decision 等)在 MMR 之前加性加分, + // 让「我们定了什么/阈值怎么定的」这类问题召回出决定本体而非流水。0 = 关闭。 + // 流水类(llm_distill/episode)不加分也不降权——偏置只抬决策,不动流水的相对位次。 + sourceTierBias = envFloat("ZHIYI_SOURCE_TIER_BIAS", 0) // 0 = 关闭 ) func envFloat(key string, def float64) float64 { @@ -52,6 +56,14 @@ func coalesceConfidence(c *float64) float64 { return *c } +// isDecisionSource t_be3e958a:来源分层偏置的决策类判定。 +// source 归一后含 "decision"(wiki:decision / decision / wiki:decision:vault 等)即算决策来源。 +// 归一步骤:TrimSpace → TrimPrefix("wiki:") → ToLower,与 graph_auto.go 的 source 归一保持一致。 +func isDecisionSource(source string) bool { + s := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(source), "wiki:")) + return strings.Contains(s, "decision") +} + // MissRecorder 缺口记录接口(由 routes 层注入 gapDetector.RecordMiss) type MissRecorder func(query, namespace string) @@ -327,6 +339,19 @@ func (p *RecallPipeline) recallUncached(query, namespace string, topK int, diver } } + // t_be3e958a 来源分层偏置(紧邻 confidenceLambda,同样在 MMR 之前): + // 决策类来源(source 含 "decision",如 wiki:decision / decision)加性加分 + // sourceTierBias。只抬决策、不动流水/其它来源 —— 避免偏置挤压本应排前的流水。 + // sourceTierBias=0 时本循环不执行 ⇒ 与旧实现逐位等价,回滚只需把环境变量置 0。 + if sourceTierBias > 0 { + for i := range reranked { + idx := reranked[i].Index + if idx >= 0 && idx < len(candidates) && isDecisionSource(candidates[idx].Source) { + reranked[i].Score += sourceTierBias + } + } + } + // Step 4: MMR diversity finalIndices := MMRSelect(reranked, candidates, topK*2, diversity) diff --git a/go/internal/storage/recall_source_tier_test.go b/go/internal/storage/recall_source_tier_test.go new file mode 100644 index 0000000..a1cc962 --- /dev/null +++ b/go/internal/storage/recall_source_tier_test.go @@ -0,0 +1,120 @@ +package storage + +import ( + "testing" + + "github.com/xiaoxue/memoryweave/internal/models" +) + +// srcTierLance 复用 nsRecordingLance 的 Search/Update 语义,但行由外部注入。 +// 这里直接嵌入 nsRecordingLance 继承其 Search/Update 桩。 +type srcTierLance struct { + nsRecordingLance +} + +// TestIsDecisionSource t_be3e958a:决策类来源判定(归一后含 "decision")。 +func TestIsDecisionSource(t *testing.T) { + cases := []struct { + src string + want bool + }{ + {"wiki:decision", true}, + {"decision", true}, + {"wiki:decision:vault", true}, + {"wiki:/home/muc/mc/xxx", false}, // 普通 wiki 笔记不算决策 + {"llm_distill", false}, + {"episode", false}, + {"wiki:worklog-note", false}, + {"", false}, + } + for _, c := range cases { + if got := isDecisionSource(c.src); got != c.want { + t.Errorf("isDecisionSource(%q)=%v, want %v", c.src, got, c.want) + } + } +} + +// vecDim helper:构造同维度向量,保证第一个分量不同以区分候选。 +func srcVec(v0 float32) []float32 { + v := make([]float32, 1024) + v[0] = v0 + return v +} + +// newSrcTierPipeline 与 newNSTestPipeline 同构:隔离搜索缓存 + 预置 query 向量缓存, +// 但注入可自定义向量的 LanceDB 桩 + 自定义 query 向量。 +func newSrcTierPipeline(t *testing.T, rows []models.MemoryRecord, queryVec []float32) (*RecallPipeline, *srcTierLance) { + 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("srcTierQuery", queryVec) // 预置 → EncodeSingle 不走网络 + fake := &srcTierLance{nsRecordingLance{rows: rows}} + p := NewRecallPipeline(emb, fake, &Reranker{}) + return p, fake +} + +// TestSourceTierBiasLiftsDecision 核心行为:bias>0 时决策来源被抬进 top-3; +// bias=0 时流水类保持领先(决策不越位)。用向量直接算 cosine,确定性可复算。 +// +// 构造:5 个候选,全部 category=distilled(过原始对话过滤),query 向量 v_q=[1,0,0...]。 +// idx0 流水 vec=[1.0,0,..] cos=1.00 → 最高 +// idx1 流水 vec=[0.9,0,..] cos=0.90 +// idx2 流水 vec=[0.8,0,..] cos=0.80 +// idx3 决策 vec=[0.7,0,..] cos=0.70 → bias=0 时第4名;bias=0.2 后 0.90 与 idx1 并列 +// idx4 流水 vec=[0.6,0,..] cos=0.60 +// 期望:bias=0 → top-3 = [0,1,2] 全流水,决策 idx3 不在 top-3(before FAIL 语义); +// bias=0.2 → idx3 抬到 0.90,与 idx1 并列,进入 top-3(after PASS 语义)。 +func TestSourceTierBiasLiftsDecision(t *testing.T) { + rows := []models.MemoryRecord{ + {ID: "s0", Content: "流水记忆A 完全无关内容一", Category: "distilled", Source: "llm_distill", Vector: srcVec(1.0)}, + {ID: "s1", Content: "流水记忆B 完全无关内容二", Category: "distilled", Source: "llm_distill", Vector: srcVec(0.9)}, + {ID: "s2", Content: "流水记忆C 完全无关内容三", Category: "distilled", Source: "llm_distill", Vector: srcVec(0.8)}, + {ID: "d0", Content: "决策记忆:今晚我们定下了哪些决策 平台决策条目", Category: "distilled", Source: "wiki:decision", Vector: srcVec(0.7)}, + {ID: "s3", Content: "流水记忆D 完全无关内容四", Category: "distilled", Source: "llm_distill", Vector: srcVec(0.6)}, + } + q := srcVec(1.0) // query 向量与第一个分量对齐 + + // --- bias=0(默认,= 开关关闭)--- + oldBias := sourceTierBias + sourceTierBias = 0 + defer func() { sourceTierBias = oldBias }() + p, _ := newSrcTierPipeline(t, rows, q) + res, _, err := p.RecallWithMeta("srcTierQuery", "hermes-main", 5, 0.0) + if err != nil { + t.Fatalf("bias=0 RecallWithMeta 出错: %v", err) + } + posDecision := findDecisionPos(res) + if posDecision < 3 { + t.Fatalf("bias=0 决策竟进 top-3(pos=%d),与 before-FAIL 语义冲突", posDecision) + } + t.Logf("bias=0: 决策位次=%d(top-3 之外 ✓)", posDecision) + + // --- bias=0.2(开关开启,决策抬进 top-3)--- + sourceTierBias = 0.2 + p2, _ := newSrcTierPipeline(t, rows, q) + res2, _, err := p2.RecallWithMeta("srcTierQuery", "hermes-main", 5, 0.0) + if err != nil { + t.Fatalf("bias=0.2 RecallWithMeta 出错: %v", err) + } + posDecision2 := findDecisionPos(res2) + if posDecision2 < 0 { + t.Fatal("bias=0.2 决策未出现在结果里") + } + if posDecision2 >= 3 { + t.Fatalf("bias=0.2 决策仍在 top-3 之外(pos=%d),偏置未生效", posDecision2) + } + t.Logf("bias=0.2: 决策位次=%d(top-3 内 ✓)", posDecision2) +} + +// findDecisionPos 返回首个 source 含 decision 的结果位次,无则 -1。 +func findDecisionPos(res []models.RecallResult) int { + for i, r := range res { + if isDecisionSource(r.Source) { + return i + } + } + return -1 +} \ No newline at end of file