Files
memoryweave/go/internal/distill/write_gate_test.go
T

119 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 distill
import "testing"
// t_286d1310: 写库闸门(server.go OnDistillComplete)此前独立跑 IsQualityFact
// 长度启发式——lane=keep 的决策类内容,其 <20 字碎片在 insert 前被第三次独立
// 丢弃(t_2ae69bb2 只穿透到 fact 级,没穿透到写库级),DB 里 derived_from=<episode>
// 0 行。server.go 已改为复用 engine.go 同一函数 FilterQualityFactsWithLane,并在
// 调用前用 ClassifyAdmission 重算 lane(确定性分诊,与 engine.go:537 同一 lane)。
//
// 本文件把 server.go 的写库判据【原样复现】成测试:ClassifyAdmission(input.Content)
// → FilterQualityFactsWithLane(result.Facts, lane),证明决策碎片经写库判据后保留、
// 非 keep lane 行为与旧判据逐字一致。
//
// 刻意不碰:admission_lane.go 判据、isSoulfulShortFact 白名单、IsQualityFact 阈值。
// 与 quality_lane_test.go 的区别:那组直接把 lane 喂给 FilterQualityFactsWithLane;
// 本组是「从 content 重算 lane 再过滤」的完整写库路径复现(同 server.go)。
// TestWriteGate_KeepDecisionFragmentsSurvive AC1:
// LaneKeep 的 <20 字决定碎片经写库判据后保留(≥3 例,每例含对照锚证明旧判据确实全拒)。
func TestWriteGate_KeepDecisionFragmentsSurvive(t *testing.T) {
cases := []struct {
name string
content string
fragments []string
}{
{
"决定录入格式规范(原文复现)",
"【决定】决定录入格式规范:一条只写一个决定,路径与日期不混进正文,避免蒸馏碎片化后宽泛问法召回不到。",
[]string{"路径与日期不混进正文", "一条只写一个决定"},
},
{
"决策承诺(测试词表同款)",
"牧尘决定以后都用分身C来处理采集类任务。",
[]string{"采集任务交给分身C"},
},
{
"自动化边界决定(同批原文复现)",
"【决定】决定类记忆回流不挂 cron:由小怡在每次拍板后主动调用脚本,先跑顺一两周。",
[]string{"回流不挂cron", "拍板后主动调用"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// server.go 写库判据的精确复现:先重算 lane
lane, why := ClassifyAdmission(tc.content)
if lane != LaneKeep {
t.Fatalf("前提失败: ClassifyAdmission 未判 keep (lane=%s, %s), content=%.60s", lane, why, tc.content)
}
// 再走 FilterQualityFactsWithLane(server.go 现在用的同一函数)
got := FilterQualityFactsWithLane(tc.fragments, lane)
if len(got) != len(tc.fragments) {
t.Fatalf("写库判据丢失决策碎片: got %d/%d %v", len(got), len(tc.fragments), got)
}
// 对照锚:旧写库判据(IsQualityFact)确实全拒这批 <20 字碎片 —— 证明本测试
// 测的就是行为差(server.go 改前的 write-gate 会丢掉全部这些碎片)
for _, f := range tc.fragments {
if IsQualityFact(f) {
t.Fatalf("对照锚失效: 旧判据 IsQualityFact 放行了 <20 字碎片 %q,本测试无区分度", f)
}
}
})
}
}
// TestWriteGate_KeepStillRejectsNegatives AC1 边界:
// LaneKeep 下两条硬否定(纯疑问 / 状态汇报型)仍被写库判据拒绝(任何长度)。
func TestWriteGate_KeepStillRejectsNegatives(t *testing.T) {
content := "牧尘决定以后都用分身C来处理采集类任务。"
lane, why := ClassifyAdmission(content)
if lane != LaneKeep {
t.Fatalf("前提失败: lane=%s (%s)", lane, why)
}
negatives := []string{
"为什么采集类任务要统一交给分身C来执行处理?", // 纯疑问
"健康检查完成,所有服务运行正常,无异常", // 状态汇报
}
for _, f := range negatives {
if got := FilterQualityFactsWithLane([]string{f}, lane); len(got) != 0 {
t.Fatalf("LaneKeep 下写库判据不应放行 %q: %v", f, got)
}
}
}
// TestWriteGate_NonKeepDelegates AC2:
// LaneDrop / LaneObserve 的写库判据与旧判据 FilterQualityFacts 逐字一致(负例 ≥2 条)。
func TestWriteGate_NonKeepDelegates(t *testing.T) {
dropLane, _ := ClassifyAdmission("好的") // <8 字 → 确定性 drop
if dropLane != LaneDrop {
t.Fatalf("前提失败: 期望 drop, got %s", dropLane)
}
obsLane, _ := ClassifyAdmission("服务已重启完成,当前版本 v0.21.6。") // 不确定带 → observe
if obsLane != LaneObserve {
t.Fatalf("前提失败: 期望 observe, got %s", obsLane)
}
// 负例:两种 lane 下都应被旧判据拒绝的碎片/噪声(≥2 条,且与 quality_lane_test.go
// 已有用例不重复,作为独立新增负例)
in := []string{
"重启系统", // <20 字无前缀碎片
"为什么系统会重启", // 纯疑问(无问号但疑问前缀+短句)
}
for lane, name := range map[AdmissionLane]string{dropLane: "LaneDrop", obsLane: "LaneObserve"} {
t.Run(name, func(t *testing.T) {
want := FilterQualityFacts(in)
got := FilterQualityFactsWithLane(in, lane)
if len(want) != len(got) {
t.Fatalf("非 keep lane 行为漂移: got %d want %d (%v vs %v)", len(got), len(want), got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("非 keep lane 行为漂移: got[%d]=%q want=%q", i, got[i], want[i])
}
}
if len(want) != 0 {
t.Fatalf("对照锚异常: 负例本应全拒, FilterQualityFacts 结果 = %v", want)
}
})
}
}