Files
memoryweave/go/internal/storage/specificity.go
T
xiaoyi 5b2587d159 feat(recall): 具体性加权补两类降权 —— 任务流水 + 系统流水
事故:关系类查询 top1 被「牧尘与小怡讨论了任务t_a50e04f0」霸占。根因不是相似度,
是 reSpecCode 把 task/t_a50e04f0 当代码锚点 ⇒ anchors=2 ⇒ ×1.2 被**加权**。
修:
- 数锚点前先抹掉 t_[0-9a-f]{8,}(流水号不是知识)
- 「讨论了/完成任务t_xxx」句 ⇒ ×0.4
- cron 通知/任务回执/提醒条目(收到了一条 / 触发:cron / 任务(ID: / 运行于 N)
  ⇒ ×0.4;有效知识(cron 投递要写 feishu:oc_xxx)不误伤(单测锁)

单测 3 个(specificity_test.go):任务流水降权 / 短句人主语降权 / 含锚点加权 /
系统流水降权 / 有效知识不误伤 / gate off 逐位等价。
实测:『牧尘讨厌我做什么』top1 从「讨论了任务t_a50e04f0」变为真实事件句。
2026-11-16 16:42:11 +08:00

125 lines
5.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package storage
import (
"os"
"regexp"
"strings"
"github.com/xiaoxue/memoryweave/internal/models"
)
// ── t_0e02017b 具体性加权(2026-11-16)────────────────────────────────────
// 病根:「短句霸榜」—— 8 字零信息句「牧尘是待拍板人。」对 20 道「关于我们」题
// 的 bge-m3 余弦 mean=0.7791(短句胜 18/20 题),且 quality_score=0.655(可信、非 0),
// 被 fuseQuality 抬到高位 ⇒ 在向量 rerank 漏斗里霸 top1(基线 5 题 top1 + 2 题 top2)。
// ① 余弦证伪已确认:是向量本身偏好零实体短句(叠加质量分 + FTS/RRF 推动),
// 不是「排序层 bug」也不是「向量退化」。
//
// 修法(卡 §②):在召回打分里加一项**具体性因子**——纯函数,env 门控,默认关(逐位等价现状):
// · 含日期 \d{4}-\d{2}-\d{2} / 原话「」/ 具体数字 / 代码标识(_、/、.、#)⇒ 加权
// · len<16 且 零锚点 且 含人主语(牧尘/用户/小怡)⇒ 降权(×0.5 起)
// · 不动 top-k 条数、不硬删数据、不按关键词黑名单
//
// 与既有 bias(confidenceLambda / sourceTierBias / feedbackBias / timeDecayBias)同款:
// 加在 MMRSelect 之前 ⇒ 选择与展示共用同一套分数(recall.go Step 3.7 教训)。
// gate 关(默认)⇒ 恒 1.0 ⇒ 逐位等价现状,回滚 = 置 0 env 重启。
// specificityGate 每次调用读 env(测试可 t.Setenv 翻转,同 lowScoreDwGate 模式)。
func specificityGate() bool {
switch strings.ToLower(os.Getenv("ZHIYI_SPECIFICITY")) {
case "1", "true", "on":
return true
}
return false
}
// 锚点正则(包级编译一次)。
var (
reSpecDate = regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) // 日期
reSpecQuote = regexp.MustCompile(`[「」「]`) // 原话引号
reSpecNumber = regexp.MustCompile(`\d`) // 数字
reSpecCode = regexp.MustCompile(`[a-zA-Z][a-zA-Z0-9_./#-]*[a-zA-Z0-9]`) // 代码标识符
// 2026-11-16 二次修订:任务编号不是知识锚点。
// 事故:「牧尘与小怡讨论了任务t_a50e04f0」(27 字、零实体)因 task/t_a50e04f0
// 命中 reSpecCode ⇒ anchors=2 ⇒ ×1.2 被**加权**,在关系类查询里霸 top1。
reSpecTaskID = regexp.MustCompile(`t_[0-9a-f]{8,}`)
// 「讨论了任务X」这类纯任务流水,不论多长都不该代表「关于我们」。
reSpecTaskTalk = regexp.MustCompile(`(讨论|完成|处理|收尾)了?(kanban)?任务\s?t_[0-9a-f]{8}|讨论了任务`)
// 系统流水(cron 通知/任务回执/提醒条目):对「关于某个人」类查询零信息,
// 却因为句首就是「牧尘收到了一条…」而天然高相似 —— 实测霸占多个 top1。
reSpecSysFlow = regexp.MustCompile(`cron failed|cron 通知|收到了一条|触发:cron|任务(ID:|运行于 \d|提醒-待|状态 o(k|k\b)|job_id|执行状态`)
)
// personSubjects 人主语词(「len<16 且 零锚点 且 含人主语 ⇒ 降权」的判据之一)。
var personSubjects = []string{"牧尘", "用户", "小怡"}
// hasPersonSubject 内容里是否含人主语。
func hasPersonSubject(content string) bool {
for _, p := range personSubjects {
if strings.Contains(content, p) {
return true
}
}
return false
}
// specificityAnchors 一条内容的锚点数(日期/原话/数字/代码 各自有则 +1)。
func specificityAnchors(content string) int {
// 任务编号先抹掉再数锚点:它只是流水号,不是知识
content = reSpecTaskID.ReplaceAllString(content, " ")
a := 0
if reSpecDate.MatchString(content) {
a++
}
if reSpecQuote.MatchString(content) {
a++
}
if reSpecNumber.MatchString(content) {
a++
}
if reSpecCode.MatchString(content) {
a++
}
return a
}
// specificityFactor 返回具体性缩放因子 ∈ (0, 1.5]。
// gate 关(默认)⇒ 恒 1.0(逐位等价现状)。
// · 零锚点 且 len<16 且 含人主语 ⇒ 降权 ×floor(默认 0.5)
// · 每多 1 个锚点 ⇒ 加权 ×(1 + boost*锚点数),封顶 cap(默认 1.5)
// · 零锚点但不满足「短 + 人主语」⇒ 1.0(不误伤「用户必须关闭 NAT」这类有效短规则,
// 它含具体标识 NAT;纯人主语短规则仍被降权——这是卡的明确意图)
func specificityFactor(content string, c *models.MemoryRecord) float64 {
_ = c // 预留(后续可按 source 微调;本期只按 content 判)
if !specificityGate() {
return 1.0
}
runes := []rune(content)
n := len(runes)
// 任务流水句(「讨论了任务t_xxx」)无论长短一律降权:它对「关于我们」零信息
if n < 300 && (reSpecTaskTalk.MatchString(content) || reSpecSysFlow.MatchString(content)) {
if f := envFloat("ZHIYI_SPECIFICITY_JUNK_FLOOR", 0.4); f > 0 {
return f
}
return 1.0
}
anchors := specificityAnchors(content)
if anchors == 0 && n < 16 && hasPersonSubject(content) {
// 短 + 零锚点 + 人主语 ⇒ 降权
if f := envFloat("ZHIYI_SPECIFICITY_FLOOR", 0.5); f > 0 {
return f
}
return 1.0
}
if anchors > 0 {
boost := envFloat("ZHIYI_SPECIFICITY_BOOST", 0.1)
capV := envFloat("ZHIYI_SPECIFICITY_CAP", 1.5)
f := 1.0 + boost*float64(anchors)
if f > capV {
f = capV
}
return f
}
return 1.0
}