- experience_gate.go 新增:env 门控 ZHIYI_DISTILL_EXPERIENCE(启动读一次,同 density_gate 写法)、
ExperienceRecord{Task,Solution,Boundary}(无 confidence 自评,质量复用五维 overall/VD/RU)、
独立 prompt 附加常量(1157B,拼在 outFmt 之后)、ParseExperienceLine(4 段硬约束 + 反回显)、
ExperienceGrounded(反张冠李戴:task 对象名必须在 episode 原文有影子,确定性零 LLM)
- engine/parser/server 接线:关态条件短路不可达 ⇒ 关态 prompt 逐字节不变
(sha256 三臂一致:head-off==tree-off=49295e70…,开态=关态+1157B)
- score_cache 门控开时加 \x00experience-v1 后缀,防旧 prompt 缓存短路灰度
- 落库:接地闸 → 近重复跳过(cosine≥0.98)→ 写现成 category=experience(不造新表/新列)
实测(40 条真实工作笔记,本地 2B):EXP 行出现 32/40=80%,但过格式闸仅 2/40=5%
⇒ 2B 会写 EXP 内容但用自然语言衔接、不拆竖线三段,属能力边界。故**默认关,未部署未 push**。
go test ./internal/distill/ ./internal/api/routes/ -count=1 全绿
198 lines
8.0 KiB
Go
198 lines
8.0 KiB
Go
// 织忆 MemoryWeave — 行协议解析器测试(t_180c0179 AC1/AC2/AC4)
|
||
//
|
||
// AC1:11 个反例全过(覆盖核心反例·内层双引号、原文含竖线、非法 UTF-8、截断、兜底等)
|
||
// AC2:撤掉 ParseLineFormat 实现后,这 11 个用例必须 FAIL(否则测试无效)
|
||
// AC4:OutputFormat="json" 时走旧 JSON 路径,行为与改动前一致
|
||
|
||
package distill
|
||
|
||
import (
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// AC1 · 单测矩阵:11 个反例(facts 数断言)。
|
||
func TestParseLineFormat_AC1Matrix(t *testing.T) {
|
||
cases := []struct {
|
||
name string
|
||
input string
|
||
wantFacts int
|
||
}{
|
||
{"正常多行", "FACT|甲\nENTITY|乙|person\nEND", 1},
|
||
{"核心反例·内层双引号", `FACT|他说"你好"就这样`, 1},
|
||
{"原文含竖线", "FACT|连接条件是a|b", 1},
|
||
{"原文含等号", "FACT|公式a=b", 1},
|
||
{"截断在字段中", "FACT|内容\nFACT|半截", 1},
|
||
{"乱行夹杂", "垃圾行\nFACT|真内容", 1},
|
||
{"空输出", "", 0},
|
||
{"无END", "FACT|内容", 1},
|
||
{"非法UTF-8", "FACT|" + "\xff\xfe", 1},
|
||
{"LLM仍产JSON", `{"facts":["test"]}`, 0},
|
||
{"只有前言废话", "以下是我提取的事实:", 0},
|
||
}
|
||
for _, tc := range cases {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
facts, _, _, _, ok := ParseLineFormat(tc.input)
|
||
if got := len(facts); got != tc.wantFacts {
|
||
t.Fatalf("facts 数 = %d,期望 %d | input=%q ok=%v", got, tc.wantFacts, tc.input, ok)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// AC1 补充:FACT 行内容整段保留(含引号、竖线),SplitN(...,2) 保护。
|
||
func TestParseLineFormat_FACTContentPreserved(t *testing.T) {
|
||
facts, _, _, _, ok := ParseLineFormat(`FACT|他说"你好"就这样|补充` + "\nEND")
|
||
if !ok {
|
||
t.Fatalf("应识别到 FACT 行")
|
||
}
|
||
if len(facts) != 1 {
|
||
t.Fatalf("facts = %d,期望 1", len(facts))
|
||
}
|
||
if facts[0] != `他说"你好"就这样|补充` {
|
||
t.Fatalf("FACT 内容被截断/改写: %q(期望整段含引号+竖线保留)", facts[0])
|
||
}
|
||
}
|
||
|
||
// AC1 补充:ENTITY|name|type 三元组正确解析(SplitN(...,3))。
|
||
func TestParseLineFormat_EntityTriplet(t *testing.T) {
|
||
_, entities, _, _, ok := ParseLineFormat("ENTITY|小唯|person\nENTITY|ffmpeg|tool\nEND")
|
||
if !ok {
|
||
t.Fatalf("应识别到 ENTITY 行")
|
||
}
|
||
if len(entities) != 2 {
|
||
t.Fatalf("entities = %d,期望 2", len(entities))
|
||
}
|
||
if entities[0].Name != "小唯" || entities[0].Type != "person" {
|
||
t.Fatalf("实体0 = %+v,期望 {小唯, person}", entities[0])
|
||
}
|
||
if entities[1].Name != "ffmpeg" || entities[1].Type != "tool" {
|
||
t.Fatalf("实体1 = %+v,期望 {ffmpeg, tool}", entities[1])
|
||
}
|
||
}
|
||
|
||
// AC1 补充:SCORE 行解析出 5 维评分。
|
||
func TestParseLineFormat_ScoreParsed(t *testing.T) {
|
||
_, _, scores, _, ok := ParseLineFormat("FACT|x\nSCORE|0.8|0.7|0.6|0.9|0.7\nEND")
|
||
if !ok {
|
||
t.Fatalf("应识别到有效行")
|
||
}
|
||
if scores.IS != 0.8 || scores.SU != 0.7 || scores.PA != 0.6 || scores.VD != 0.9 || scores.RU != 0.7 {
|
||
t.Fatalf("评分解析错误: %+v", scores)
|
||
}
|
||
}
|
||
|
||
// 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|乙")
|
||
if len(facts) != 1 {
|
||
t.Fatalf("facts = %d,期望 1(END 后的 FACT 应忽略)", len(facts))
|
||
}
|
||
if facts[0] != "甲" {
|
||
t.Fatalf("facts[0] = %q,期望 甲", facts[0])
|
||
}
|
||
}
|
||
|
||
// AC1 补充:空输出 → ok=false(调用方落 JSON 兜底)。
|
||
func TestParseLineFormat_EmptyReturnsNotOK(t *testing.T) {
|
||
_, _, _, _, ok := ParseLineFormat("")
|
||
if ok {
|
||
t.Fatalf("空输出 ok 应为 false(应落兜底)")
|
||
}
|
||
}
|
||
|
||
// AC4 · OutputFormat="json" 回退旧 JSON 路径:行为与改动前一致。
|
||
// 用 httptest 桩返回 JSON body,断言 json 模式能解析出 facts/entities/评分。
|
||
func TestParseLineFormat_JSONModeRollback(t *testing.T) {
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.Header().Set("Content-Type", "application/json")
|
||
_, _ = w.Write([]byte(`{"choices":[{"message":{"content":"{\"facts\":[\"用户今天买了咖啡\"],\"entities\":[\"咖啡\"],\"is\":0.9,\"su\":0.8,\"pa\":0.7,\"vd\":0.9,\"ru\":0.8}"}}]}`))
|
||
}))
|
||
defer srv.Close()
|
||
|
||
e := NewEngine(srv.URL, "test-model", "test-key")
|
||
e.OutputFormat = "json" // 一行配置回退旧 JSON 路径
|
||
resp, err := e.callLLM5D("用户今天买了咖啡")
|
||
if err != nil {
|
||
t.Fatalf("json 模式 callLLM5D 报错: %v", err)
|
||
}
|
||
if len(resp.Facts) != 1 || resp.Facts[0] != "用户今天买了咖啡" {
|
||
t.Fatalf("json 模式 facts = %v,期望 [用户今天买了咖啡]", resp.Facts)
|
||
}
|
||
if resp.IS != 0.9 || resp.RU != 0.8 {
|
||
t.Fatalf("json 模式评分 = IS=%v RU=%v,期望 0.9/0.8", resp.IS, resp.RU)
|
||
}
|
||
t.Logf("✅ AC4: OutputFormat=json 回退旧 JSON 路径成功,facts=%v is=%.1f ru=%.1f", resp.Facts, resp.IS, resp.RU)
|
||
}
|
||
|
||
// 行协议整链路:callLLM5D 拿到行协议 body → 解析出 facts/entities/评分。
|
||
// 用内层双引号的 FACT 验证「换格式根治未转义引号崩溃」的端到端效果。
|
||
func TestCallLLM5D_LineProtocolParse(t *testing.T) {
|
||
lineBody := `{"choices":[{"message":{"content":"FACT|他说\"你好\"就这样\nENTITY|小唯|person\nSCORE|0.7|0.6|0.5|0.8|0.6\nEND"}}]}`
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
w.Header().Set("Content-Type", "application/json")
|
||
_, _ = w.Write([]byte(lineBody))
|
||
}))
|
||
defer srv.Close()
|
||
|
||
e := NewEngine(srv.URL, "test-model", "test-key") // 默认 lines 模式
|
||
resp, err := e.callLLM5D("用户今天买了咖啡")
|
||
if err != nil {
|
||
t.Fatalf("lines 模式 callLLM5D 报错: %v", err)
|
||
}
|
||
if len(resp.Facts) != 1 || resp.Facts[0] != `他说"你好"就这样` {
|
||
t.Fatalf("lines facts = %v,期望 [他说\"你好\"就这样](内层引号必须整段保留,不再崩溃)", resp.Facts)
|
||
}
|
||
if len(resp.Entities) != 1 || resp.Entities[0] != "小唯" {
|
||
t.Fatalf("lines entities = %v,期望 [小唯]", resp.Entities)
|
||
}
|
||
if resp.VD != 0.8 {
|
||
t.Fatalf("lines VD = %v,期望 0.8(SCORE 行应解析)", resp.VD)
|
||
}
|
||
t.Logf("✅ lines 全链路:facts=%v entities=%v vd=%.1f", resp.Facts, resp.Entities, resp.VD)
|
||
}
|
||
|
||
// AC1 补充:默认 OutputFormat 为 "lines"(ZHIYI_DISTILL_FORMAT=json 时环境显式覆盖则跳过)。
|
||
func TestDistillOutputFormat_DefaultLines(t *testing.T) {
|
||
if got := distillOutputFormat(); strings.ToLower(got) != "lines" {
|
||
t.Fatalf("默认 OutputFormat 应为 lines,实际=%s", got)
|
||
}
|
||
}
|