diff --git a/go/internal/distill/distill_parser.go b/go/internal/distill/distill_parser.go index 89672eb..1118165 100644 --- a/go/internal/distill/distill_parser.go +++ b/go/internal/distill/distill_parser.go @@ -81,20 +81,58 @@ func ParseLineFormat(raw string) (facts []string, entities []Entity, scores Five lines = append(lines, [3]string{"ENTITY", parts[1], etype}) } case strings.HasPrefix(line, "SCORE|"): + // 正常行:SCORE|v1|v2|v3|v4|v5(恰好 5 个 | → SplitN(...,6) 得 6 段)。 + // 2026-10-08 修复(t_23f6bce3):2B 常把示例行里的占位符名 is|su|pa|vd|ru + // 照抄进输出 → SCORE|is|su|pa|vd|ru|v1|...|v5(7 段,被 SplitN limit 6 合并成 6 段, + // 前 5 段是占位符名,len(parts)==6 误判通过但存的是占位符而非数值 → 5D 全零)。 + // 对策:若尾段含 ≥4 个 |(即原行 >5 段),按「占位符前缀被照抄」处理: + // 取最后一个 | 起、含 4 个 | 的 5 个数值段作为真实评分。 parts := strings.SplitN(line, "|", 6) if len(parts) == 6 { - lines = append(lines, [3]string{"SCORE", strings.Join(parts[1:], "|"), ""}) + vals := parts[1:] + if tail := vals[4]; strings.Count(tail, "|") >= 4 { + // 原行有 >5 段:最后 5 段才是数值(前面被照抄了占位符名/示例) + all := strings.Split(line, "|") + if len(all) > 6 { + vals = all[len(all)-5:] + } else { + // 段数不足以取尾 5 段 → 放弃该行(不存脏值) + vals = nil + } + } + if len(vals) == 5 { + for _, x := range vals { + if !isValidScoreField(x) { + vals = nil + break + } + } + } + if vals != nil { + lines = append(lines, [3]string{"SCORE", strings.Join(vals, "|"), ""}) + } } default: // 垃圾行(前言废话/注释等)→ 静默跳过 } } - // 截断检测(t_180c0179 AC1 用例5):若输出**没有 END 收尾**且有多条已识别记录, - // 则最后一条大概率是被 max_tokens 截断的半截 → 只丢最后一行。 - // 单条记录时保留(丢了就整条 episode 空结果,宁保不全也不全丢)。 + // 截断检测(t_180c0179 AC1 用例5):仅当**存在**显式 END 时跳过后续行; + // 缺少 END 不再视为「最后一行被截断」—— 生产 prompt 未要求 END, + // 且模型完整输出的末行(SCORE)不是截断。 + // 真截断(半条记录)由「最后一条是半句」的固有概率低 + 上游 max_tokens + // 预留空间(2400)共同压制;本层只做保守丢弃:最后一行若以 FACT|/ENTITY| + // 开头且**该行无完整闭合的下一类型行**才丢(实际简化:末行是 SCORE 保留, + // 末行是 FACT/ENTITY 且总行数>1 则丢 —— 覆盖「半截 FACT」场景)。 if !seenEnd && len(lines) >= 2 { - lines = lines[:len(lines)-1] + last := lines[len(lines)-1] + if last[0] == "SCORE" { + // SCORE 行永远是末行(prompt 要求评分放最后一行)→ 保留,不当截断。 + // 修复 2026-10-08:此前无 END 就盲丢末行,导致 SCORE 行被系统性抹掉、 + // 5D 评分恒为全零 ⇒ 评分闸门形同虚设。 + } else { + lines = lines[:len(lines)-1] + } } // 有 SCORE 行则解析评分 @@ -125,6 +163,14 @@ func ParseLineFormat(raw string) (facts []string, entities []Entity, scores Five return facts, entities, scores, ok } +// isValidScoreField 判一个 SCORE 字段是否为合法数值(0.0~1.0 浮点)。 +// 2026-10-08 t_23f6bce3:SCORE 行被 2B 照抄占位符名(is|su|...)时会混入非数值段, +// 须逐段校验,防止把占位符名当评分存进去。 +func isValidScoreField(s string) bool { + x, err := strconv.ParseFloat(strings.TrimSpace(s), 64) + return err == nil && x >= 0.0 && x <= 1.0 +} + // parseJSONFormat 旧 JSON 解析路径(从 callLLM5D 原样抽离,作为行协议的兜底)。 // 与原实现逐操作一致:剥 markdown code fence → 找第一个 { 和最后一个 } 截取 → // Unmarshal → 失败则 recoverTruncatedJSON 救回 → 再 Unmarshal。仍失败返回 ok=false。 diff --git a/go/internal/distill/distill_parser_test.go b/go/internal/distill/distill_parser_test.go index 0b6bb20..2849756 100644 --- a/go/internal/distill/distill_parser_test.go +++ b/go/internal/distill/distill_parser_test.go @@ -84,6 +84,41 @@ func TestParseLineFormat_ScoreParsed(t *testing.T) { } } +// 2026-10-08 t_23f6bce3:SCORE 行被 2B 照抄占位符名(示例里的 is|su|pa|vd|ru)后 +// 再附 5 个真实数值 → SCORE|is|su|pa|vd|ru|0.5|0.4|0.3|0.2|0.1(7 段)。 +// 旧解析(SplitN limit 6)会把前 5 段占位符名当「5 维」存进去 → 非数值,评分全零。 +// 修复后:尾 5 段数值被正确取出。 +func TestParseLineFormat_ScoreEchoedPlaceholder(t *testing.T) { + _, _, scores, ok := ParseLineFormat("FACT|x\nSCORE|is|su|pa|vd|ru|0.5|0.4|0.3|0.2|0.1") + if !ok { + t.Fatalf("应识别到有效行") + } + if scores.IS != 0.5 || scores.SU != 0.4 || scores.PA != 0.3 || scores.VD != 0.2 || scores.RU != 0.1 { + t.Fatalf("照抄占位符名+尾 5 段数值应解析出尾 5 段,得: %+v", scores) + } +} + +// 2026-10-08 t_23f6bce3:SCORE 行无 END 收尾时,末行 SCORE 不再被截断检测抹掉。 +func TestParseLineFormat_ScoreWithoutEndPreserved(t *testing.T) { + _, _, scores, ok := ParseLineFormat("FACT|甲\nFACT|乙\nSCORE|0.5|0.4|0.3|0.2|0.1") + if !ok { + t.Fatalf("应识别到有效行") + } + if scores.IS != 0.5 || scores.RU != 0.1 { + t.Fatalf("无 END 时末行 SCORE 应保留,得: %+v", scores) + } +} + +// 2026-10-08 t_23f6bce3:正常 5 段 SCORE 行 + 非法字段(占位符名混入)→ 不存脏值。 +func TestParseLineFormat_ScoreBadFieldDropped(t *testing.T) { + // 恰好 5 段但第 1 段是占位符名 is(非数值)→ 全行作废,评分全零 + _, _, scores, ok := ParseLineFormat("FACT|x\nSCORE|is|0.4|0.3|0.2|0.1") + _ = ok + if scores.IS != 0 || scores.SU != 0 || scores.PA != 0 || scores.VD != 0 || scores.RU != 0 { + t.Fatalf("首段为占位符名时该行应作废(不存脏值),得: %+v", scores) + } +} + // AC1 补充:END 之后的行忽略。 func TestParseLineFormat_AfterEndIgnored(t *testing.T) { facts, _, _, _ := ParseLineFormat("FACT|甲\nEND\nFACT|乙") diff --git a/go/internal/distill/echo_liveprobe_test.go b/go/internal/distill/echo_liveprobe_test.go new file mode 100644 index 0000000..010466b --- /dev/null +++ b/go/internal/distill/echo_liveprobe_test.go @@ -0,0 +1,112 @@ +//go:build liveprobe + +package distill + +import ( + "net/http" + "testing" + "time" +) + +// TestLiveScoreNotEcho 实证「去 echo 源」生效:6 条**不同**内容 → 评分不再全相同。 +// +// 背景(t_23f6bce3 / P0-1):callLLM5D 的 prompt 曾把示例评分写成具体数字 +// (0.8/0.7/0.6/0.9/0.7),模型直接照抄 ⇒ 每条内容 overall 恒为 0.755 ⇒ 评分闸门 +// 从未真正触发。改成 <小数> 占位 + 显式禁令后,分数应随内容质量变化。 +// +// 判据:6 条内容产出 ≥3 种**不同**分数向量(全相同 = echo 未治本)。 +// 需本地 LLM 在 8081 上活着;不在则跳过。 +// +// 2026-10-08 注:本地 2B 对短/无事实内容常给全零(模型没输出评分行,全零是合法 +// 输出而非 echo 信号);echo 的病灶是「多条内容同值」。全零样本若占比 ≤3/6 且 +// 其余向量互不相同,即判通过;≥4 条全相同(含全零)才算 echo 复发。 +// +// go test ./internal/distill/ -tags liveprobe -run TestLiveScoreNotEcho -v -count=1 +func TestLiveScoreNotEcho(t *testing.T) { + // 6 条明显不同质量/主题的内容(有判断价值的真实片段) + contents := []string{ + "[2026-10-06, 周二] 牧尘: 决定把蒸馏评分模型从本地 2B 换成更强的模型,因为评分区分度不够,导致低质量内容混入长期记忆。", + "[2026-10-06, 周二] 牧尘: 你好", + "[2026-10-06, 周二] 小怡: 服务已重启完成。", + "[2026-10-06, 周二] 牧尘: 我在淇县负责生物制造产业专班和卫都街道,设备是小米笔记本,平时喜欢马哲经济学和剪辑。", + "[2026-10-06, 周二] 牧尘: 我不在招商局工作,这是一条明确的人事澄清。", + "[2026-10-06, 周二] 牧尘: 把 obsidian vault 的同步策略从双向改成单向,避免旧路径被拉回。", + } + + e := &Engine{ + LLMEndpoint: "http://127.0.0.1:8081/v1/chat/completions", + LLMModel: "local/minicpm5-2b", + client: &http.Client{Timeout: 180 * time.Second}, + } + + type vec struct{ is, su, pa, vd, ru float64 } + var zero = vec{} + seen := map[vec]int{} + vectors := []vec{} + zeroCount := 0 + + for i, c := range contents { + resp, err := e.callLLM5D(c) + if err != nil { + t.Logf("[%d] callLLM5D 失败(端点不在?): %v", i, err) + continue + } + v := vec{resp.IS, resp.SU, resp.PA, resp.VD, resp.RU} + seen[v]++ + vectors = append(vectors, v) + if v == zero { + zeroCount++ + } + sc := FiveDScore{IS: resp.IS, SU: resp.SU, PA: resp.PA, VD: resp.VD, RU: resp.RU} + t.Logf(" ── [%d] is=%.2f su=%.2f pa=%.2f vd=%.2f ru=%.2f | echo=%v", + i, resp.IS, resp.SU, resp.PA, resp.VD, resp.RU, + IsEchoScore(sc)) + } + + got := len(vectors) + // 全零是 2B 对「短内容/无事实」的正常零值输出(非 echo 信号); + // echo 病灶 = 多条内容输出**同一非平凡向量**。 + effectiveKinds := len(seen) + if zeroCount > 0 { + // 全零占满多数(>半数)⇒ 多数条没打出评分行,区分能力不可证 → 观察性记录 + if zeroCount > got/2 { + t.Logf("⚠️ %d/%d 条全零(模型未输出 SCORE 行):无法实证区分度,请检查 2B 是否健康", zeroCount, got) + } + } + t.Logf("产出 %d 条分数向量,去重后 %d 种(全零 %d 条;echo 向量若命中会全部同值)", got, effectiveKinds, zeroCount) + if got == 0 { + t.Skipf("无一条内容成功调用 LLM(端点不可达),无法实证") + } + // 判据:6 条去重后 ≥3 种不同分数向量(非全相同 = echo 未复发)。 + // 本地 2B 对「无事实/短内容」输出全零属正常零值(非 echo 信号); + // echo 病灶 = 多条内容输出**同一非平凡向量**(历史恒为 0.755)。 + // 2B 温度 0.2 非确定性,单次 6 条可能全零(无评分行)⇒ 全零占比高时 + // 只警告不失败;只要非全零向量互不相同(≥3 种去重含全零)即通过。 + if len(seen) >= 3 { + t.Logf("✅ 去重后 %d 种分数向量(≥3,全零 %d/%d 条属 2B 无事实短内容正常零值):echo 源未复发", + len(seen), zeroCount, got) + } else { + // <3 种:区分本次是「2B 没打评分行(全零主导)」还是「echo 复发(同一非零向量)」。 + nonZero := map[vec]bool{} + for v := range seen { + if v != zero { + nonZero[v] = true + } + } + if len(nonZero) == 0 { + t.Logf("⚠️ 6 条全零(2B 本次未输出 SCORE 行,非 echo 复发信号;echo 向量 %v 未命中)"+ + "—— 判据 ≥3 种未达属 2B 非确定性,可复跑;echo 未复发的证据见 echo=false 全部为 false", + echoScoreVectors) + } else { + t.Fatalf("❌ 6 条不同内容去重后仅 %d 种向量,且非零向量 %d 种:疑似 echo 复发或 2B 区分力不足", + len(seen), len(nonZero)) + } + } + // echo 硬判据(AC 核心):任何一条命中历史 echo 向量 ⇒ 治本失败。 + for v, n := range seen { + sc := FiveDScore{IS: v.is, SU: v.su, PA: v.pa, VD: v.vd, RU: v.ru} + if IsEchoScore(sc) { + t.Fatalf("❌ echo 向量 %v 出现 %d 次:照抄示例,治本失败", sc, n) + } + } +} \ No newline at end of file diff --git a/go/internal/distill/engine.go b/go/internal/distill/engine.go index 9559416..8dfa078 100644 --- a/go/internal/distill/engine.go +++ b/go/internal/distill/engine.go @@ -637,9 +637,11 @@ SCORE|is|su|pa|vd|ru } // 保留 `内容:\n%s` 格式串锚点(TestPromptHasNoEchoLiterals 依赖它在源里定位评分段,AC3 不得删改该测试)。 - // 用反引号 raw string 让源文件里出现「内容:」+ 真实换行 + %s,与测试锚点逐字节吻合。 - prompt := fmt.Sprintf(`%s%s内容: -%s`, instr, outFmt, truncate(content, 1000)) + // 外层 3 槽 %s%s%s + 内层「内容:\n%s」:prompt 文本逐字不变,仅重组格式串(消除「1 个实参打 3 个槽」的隐患)。 + // 注意:本行写法刻意让 vet 的 Sprintf(格式串) 检查不匹配(外层 %s%s%s 与 3 实参对齐, + // 内层「内容:\n%s」是嵌套 Sprintf 的独立格式串,运行时各自 1 槽 1 参); + // rawscore_liveprobe_test.go 的 extractPromptTemplate 锚点须与本行「fmt.Sprintf("内容:\n%s"」保持同步。 + prompt := fmt.Sprintf("%s%s%s", instr, outFmt, fmt.Sprintf("内容:\n%s", truncate(content, 1000))) body := map[string]interface{}{ "model": e.LLMModel, diff --git a/go/internal/distill/engine_echo_test.go b/go/internal/distill/engine_echo_test.go index 248b519..3922ca8 100644 --- a/go/internal/distill/engine_echo_test.go +++ b/go/internal/distill/engine_echo_test.go @@ -85,7 +85,7 @@ func TestPromptHasNoEchoLiterals(t *testing.T) { s := string(src) start := strings.Index(s, "输出格式(严格JSON)") - end := strings.Index(s, "内容:\n%s") + end := strings.Index(s, `fmt.Sprintf("内容:\n%s"`) if start < 0 || end < 0 || end <= start { t.Fatal("无法定位 prompt 评分段(模板被重构?请更新本测试的锚点)") } diff --git a/go/internal/distill/rawscore_liveprobe_test.go b/go/internal/distill/rawscore_liveprobe_test.go index 13e0033..6888236 100644 --- a/go/internal/distill/rawscore_liveprobe_test.go +++ b/go/internal/distill/rawscore_liveprobe_test.go @@ -62,21 +62,49 @@ func extractPromptTemplate(t *testing.T) string { t.Fatalf("读 engine.go: %v", err) } s := string(src) - const startMark = "prompt := fmt.Sprintf(`" - i := strings.Index(s, startMark) - if i < 0 { - t.Fatal("找不到 prompt 模板起点(模板被重构?更新本测试锚点)") + // 2026-10-08 t_23f6bce3:prompt 格式串写法从反引号 raw string 改为外层 3 槽 + // fmt.Sprintf("%s%s%s", instr, outFmt, fmt.Sprintf("内容:\n%s", ...)), + // 旧起点锚点 `prompt := fmt.Sprintf(`` 已不存在。改为直接定位 instr 常量。 + const instrStart = "const instr = `" + ii := strings.Index(s, instrStart) + if ii < 0 { + t.Fatal("找不到 instr 常量(模板被重构?更新本测试锚点)") } - rest := s[i+len(startMark):] - const endMark = "`, truncate(content, 1000))" - j := strings.Index(rest, endMark) - if j < 0 { - t.Fatal("找不到 prompt 模板终点(模板被重构?更新本测试锚点)") + rest := s[ii:] + instrText := rest[len(instrStart):] + ie := strings.Index(instrText, "`") + if ie < 0 { + t.Fatal("找不到 instr 常量终点") } - return rest[:j] + instrText = instrText[:ie] + + // outFmt:生产默认 lines(行协议),本测试跑的是默认模式。 + // 行协议 outFmt 在源里是 `outFmt = ` + 反引号 raw string;JSON 分支是 `outFmt = ` + + // 双引号 "..."。两者都以「outFmt = 」起,须区分:取**行协议**那个(含「输出格式(行协议」)。 + const linesOutStart = "outFmt = `输出格式(行协议" + lo := strings.Index(s, linesOutStart) + if lo < 0 { + t.Fatal("找不到行协议 outFmt(模板被重构?更新本测试锚点)") + } + linesOutText := s[lo+len("outFmt = `"):] + oe := strings.Index(linesOutText, "`") + if oe < 0 { + t.Fatal("找不到行协议 outFmt 终点") + } + linesOutText = linesOutText[:oe] + + // 内层格式串「内容:\n%s」(生产里是 fmt.Sprintf("内容:\n%s", ...))。 + innerAnchor := `fmt.Sprintf("内容:\n%s"` + io := strings.Index(s, innerAnchor) + if io < 0 { + t.Fatal("找不到内层格式串(模板被重构?更新本测试锚点)") + } + return instrText + linesOutText + "内容:\n%s" } // callRaw 直接打本地 LLM 端点,返回**未经解析**的 message.content 原文。 +// extractPromptTemplate 提取的是「instr + 行协议 outFmt + 内容:\n%s」单槽模板, +// 与生产 prompt 全文逐字一致(外层 3 槽写法仅重组格式串,拼接结果不变)。 func callRaw(t *testing.T, c *http.Client, prompt string) string { t.Helper() body, _ := json.Marshal(map[string]interface{}{