Files
memoryweave/go/internal/distill/engine_echo_test.go
T
小唯 d472996f80 fix(distill): SCORE line root causes - truncation guard drops last line; placeholder echo corrupts 5D scores
AC3 evidence for t_23f6bce3: two production bugs that made 5D scores all-zero.
(1) ParseLineFormat truncation detection dropped the last line when no END marker was present, but the production prompt never requires END, so a complete final SCORE line was always stripped. Now only drop the last line when it is a FACT/ENTITY (half-record), keep SCORE.
(2) The 2B model copies the example placeholder names is|su|pa|vd|ru into its SCORE line, producing a 7-field line that SplitN limit-6 mis-parses into the placeholder names, giving non-numeric all-zero scores. Now detect the echoed placeholder prefix, take the real trailing 5 fields, and validate each with isValidScoreField (0.0-1.0 float) - invalid fields discard the whole line instead of storing junk.

engine.go: prompt format string reorganized to outer 3-slot + inner content slot (text byte-identical, removes the one-arg-three-slots hazard). Test anchors in engine_echo_test.go and rawscore_liveprobe_test.go updated to the inner format string. New parser tests: echoed placeholder, no-END SCORE preserved, junk field discarded. New liveprobe test TestLiveScoreNotEcho (6 distinct contents, >=3 distinct vectors, no echo vector).
2026-10-08 16:13:31 +08:00

107 lines
4.4 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 (
"os"
"strings"
"testing"
)
// TestIsEchoScore_Positive 阳性对照:照抄旧 prompt 示例 → 必须报 true。
// 量具铁律:新量具先用正例证明它会报阳性,否则"没报警"毫无意义。
func TestIsEchoScore_Positive(t *testing.T) {
echo := FiveDScore{IS: 0.8, SU: 0.7, PA: 0.6, VD: 0.9, RU: 0.7}
if !IsEchoScore(echo) {
t.Fatalf("阳性对照失败:照抄示例 %+v 未被识别为 echo(量具失灵)", echo)
}
}
// TestIsEchoScore_Negative 阴性对照:不得滥报。
func TestIsEchoScore_Negative(t *testing.T) {
cases := map[string]FiveDScore{
"仅一位不同": {IS: 0.8, SU: 0.7, PA: 0.6, VD: 0.9, RU: 0.8},
"全零": {},
"真实差异评分": {IS: 0.95, SU: 0.4, PA: 0.2, VD: 0.85, RU: 0.6},
"全一分": {IS: 1, SU: 1, PA: 1, VD: 1, RU: 1},
}
for name, s := range cases {
if IsEchoScore(s) {
t.Errorf("阴性对照失败[%s]:%+v 被误判为 echo(滥报)", name, s)
}
}
}
// TestEchoVectorMatchesWeightedOverall 证明 echo 向量的加权分恰好是观测到的恒定值 0.755。
// 这条是"因果链"的证据固化:0.755 ≥ 0.7 阈值 ⇒ 闸门恒不触发。
func TestEchoVectorMatchesWeightedOverall(t *testing.T) {
echo := FiveDScore{IS: 0.8, SU: 0.7, PA: 0.6, VD: 0.9, RU: 0.7}
overall := echo.IS*Weights.IS + echo.SU*Weights.SU + echo.PA*Weights.PA +
echo.VD*Weights.VD + echo.RU*Weights.RU
// 0.80*0.20 + 0.70*0.20 + 0.60*0.15 + 0.90*0.25 + 0.70*0.20 = 0.755
if overall < 0.7549 || overall > 0.7551 {
t.Fatalf("加权分应为 0.755,实得 %.4f(权重被改动?)", overall)
}
if overall < 0.7 {
t.Fatalf("echo 加权分 %.4f < 0.7 阈值 —— 与「闸门恒不触发」的观测矛盾", overall)
}
}
// TestIsDegenerateScore 退化向量检测(阳性 + 阴性对照)。
//
// ⚠️ 本函数只做**观测标记**,不得单独用作丢弃判据 —— 见 engine.go 中 IsDegenerateScore 注释。
func TestIsDegenerateScore(t *testing.T) {
// 阳性:五维相同(含全零)
for name, s := range map[string]FiveDScore{
"全零(可能字段缺失)": {IS: 0, SU: 0, PA: 0, VD: 0, RU: 0},
"全0.5": {IS: 0.5, SU: 0.5, PA: 0.5, VD: 0.5, RU: 0.5},
"全0.8": {IS: 0.8, SU: 0.8, PA: 0.8, VD: 0.8, RU: 0.8},
"全一": {IS: 1, SU: 1, PA: 1, VD: 1, RU: 1},
} {
if !IsDegenerateScore(s) {
t.Errorf("阳性对照失败[%s]:五维相同的 %+v 未被识别为退化评分", name, s)
}
}
// 阴性:至少一维不同 → 不算退化
for name, s := range map[string]FiveDScore{
"真实分维度(实测样本)": {IS: 0.8, SU: 0.8, PA: 0.7, VD: 0.6, RU: 0.5},
"仅一位不同": {IS: 0.8, SU: 0.8, PA: 0.8, VD: 0.8, RU: 0.7},
"历史echo向量": {IS: 0.8, SU: 0.7, PA: 0.6, VD: 0.9, RU: 0.7},
} {
if IsDegenerateScore(s) {
t.Errorf("阴性对照失败[%s]:%+v 被误判为退化评分(滥报)", name, s)
}
}
}
// TestPromptHasNoEchoLiterals 回归守卫:**prompt 评分段**不得再出现具体示例小数。
//
// 起因:示例写成具体数值 ⇒ 模型照抄 ⇒ 每条 overall 恒为 0.755 ⇒ 评分闸门形同虚设。
// 二次踩坑:改成"禁止沿用(例如 0.8/0.7/0.6/0.9/0.7)"后,模型转而锚定 0.6 ⇒ 恒定 0.600。
// 因此守卫必须**只扫 prompt 模板段**(文件别处的 echoScoreVectors 注释本就需要记录历史值)。
func TestPromptHasNoEchoLiterals(t *testing.T) {
src, err := os.ReadFile("engine.go")
if err != nil {
t.Fatalf("读 engine.go 失败: %v", err)
}
s := string(src)
start := strings.Index(s, "输出格式(严格JSON)")
end := strings.Index(s, `fmt.Sprintf("内容:\n%s"`)
if start < 0 || end < 0 || end <= start {
t.Fatal("无法定位 prompt 评分段(模板被重构?请更新本测试的锚点)")
}
prompt := s[start:end]
if !strings.Contains(prompt, `"is": <小数>`) {
t.Fatal("回归:prompt 里已找不到 <小数> 占位符 —— 示例可能又被写成具体数字")
}
if !strings.Contains(prompt, "禁止给出固定不变的分数组合") {
t.Error("prompt 缺少「禁止固定数值组合」的约束句")
}
// 评分段不得出现任何具体小数(0.0/1.0 范围端点除外)
for _, bad := range []string{"0.8", "0.9", "0.7", "0.6", "0.5"} {
if strings.Contains(prompt, bad) {
t.Fatalf("回归:prompt 评分段出现具体示例小数 %q —— 会变成锚点(实测会把评分钉成常数)", bad)
}
}
}