Files
memoryweave/go/internal/storage/specificity_test.go
T

104 lines
4.0 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"
"testing"
)
// t_0e02017b 具体性加权 + 2026-11-16 任务流水降权(回归锁)
func TestSpecificityFactor(t *testing.T) {
t.Setenv("ZHIYI_SPECIFICITY", "1")
cases := []struct {
name string
content string
wantMin float64
wantMax float64
}{
// 闸门开:任务流水句必须被降权(事故原句,修前 ×1.2 霸 top1)
{"任务流水-短", "牧尘与小怡讨论了任务t_a50e04f0", 0.39, 0.41},
{"任务流水-完成", "完成了kanban任务t_23f6bce3", 0.39, 0.41},
// 短 + 零锚点 + 人主语 ⇒ 降权 0.5
{"短句人主语", "牧尘是待拍板人。", 0.49, 0.51},
// 有锚点(日期+引号)⇒ 加权 ≥1.2
{"画像带日期原话", "画像: 牧尘 2026-11-16 说过「你的定位不是资产,是家人」", 1.15, 1.55},
// 含代码标识 NAT ⇒ 算 1 个锚点(有意为之:具体规则该加分)
{"含代码标识", "用户必须关闭 NAT 再重启服务否则端口不通", 1.05, 1.15},
// 纯长句零锚点 ⇒ 不奖不罚
{"长句零锚点", "这种情况之下我们需要认真反思一下整个流程哪里出了问题", 1.0, 1.01},
}
for _, c := range cases {
got := specificityFactor(c.content, nil, "")
if got < c.wantMin || got > c.wantMax {
t.Errorf("%s: factor=%.3f want ∈[%.2f,%.2f] content=%q",
c.name, got, c.wantMin, c.wantMax, c.content)
}
}
}
// 闸门关 ⇒ 逐位等价现状(回滚安全性)
func TestSpecificityFactorGateOff(t *testing.T) {
t.Setenv("ZHIYI_SPECIFICITY", "0")
for _, content := range []string{
"牧尘与小怡讨论了任务t_a50e04f0",
"牧尘是待拍板人。",
"画像: 牧尘 2026-11-16 说过「你的定位不是资产,是家人」",
} {
if got := specificityFactor(content, nil, ""); got != 1.0 {
t.Errorf("gate off: factor=%.3f want 1.0 content=%q", got, content)
}
}
}
// 系统流水句降权(2026-11-16 三次修订)
func TestSpecificitySystemFlow(t *testing.T) {
t.Setenv("ZHIYI_SPECIFICITY", "1")
for _, c := range []string{
"牧尘收到了一条cron failed通知。",
"触发:cron `a0336d1b9eff`(画像体检)报 🔴 recent_frustrations[107]",
"提醒-待牧尘回复项任务(ID: 966dc3433480)运行于 20:45:23,状态 ok",
} {
if got := specificityFactor(c, nil, ""); got > 0.41 {
t.Errorf("系统流水未降权: factor=%.3f content=%q", got, c)
}
}
// 有效知识(讲 cron 机制本身)不许误伤
if got := specificityFactor("cron 投递必须写 feishu:oc_4520d148ac68f0a802480f36fcea7e59 才是情报频道", nil, ""); got < 1.0 {
t.Errorf("误伤有效知识: factor=%.3f", got)
}
}
// TestSpecificityRelatedGate 验「与查询共有」口径(2026-11-25 夜15)。
// 正控①:零锚点查询(「然后呢」)⇒ 拿不到条目自身锚点的加成
// 正控②:查询与条目同日期的 ⇒ 仍加成
// 负控:开关关 ⇒ 与老口径逐位等价
func TestSpecificityRelatedGate(t *testing.T) {
os.Setenv("ZHIYI_SPECIFICITY", "1")
defer os.Unsetenv("ZHIYI_SPECIFICITY")
content := "2026-11-25 牧尘拍板:台账 6,502 收盘,收口三件"
zeroQuery := "然后呢"
// 负控:开关关 ⇒ 条目自身锚点口径(有日期+数字 ⇒ anchors≥2 ⇒ ≥1.2)
os.Unsetenv("ZHIYI_SPECIFICITY_RELATED")
off := specificityFactor(content, nil, zeroQuery)
if off < 1.2 {
t.Errorf("负控失败:开关关时应按条目自身锚点(≥1.2),got %.3f", off)
}
// 正控①:开关开 + 零锚点查询 ⇒ 共有锚点 0 ⇒ 1.0(无加成)
os.Setenv("ZHIYI_SPECIFICITY_RELATED", "1")
on := specificityFactor(content, nil, zeroQuery)
if on != 1.0 {
t.Errorf("正控①失败:零锚点查询不该拿到加成,got %.3f(want 1.0)", on)
}
// 正控②:查询含日期 ⇒ 共有锚点 ≥1 ⇒ >1.0
hit := specificityFactor(content, nil, "2026-11-25 那天台账多少行")
if hit <= 1.0 {
t.Errorf("正控②失败:查询与条目同类型锚点应加成,got %.3f", hit)
}
t.Logf("开关关=%.3f(条目锚点口径) 零锚点查询=%.3f 同锚点查询=%.3f", off, on, hit)
}