diff --git a/go/internal/api/server.go b/go/internal/api/server.go index f0fa38a..51636cf 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -287,6 +287,10 @@ func NewServer() http.Handler { // 蒸馏产出的每条候选事实,对照 top-5 相似现有记忆判 ADD/UPDATE/NOOP/CONFLICT: // shadow:只记影子日志(本文件 write_ops.go),不真改库;apply:按判定执行。 // 复用上面已算出的 qvec/sim(不重复编码),hash 去重兜底在 JudgeWriteOp 前。 + // t_a0831457 P0 解耦:门控**只决定标记动作**(supersede/conflict);基础 + // InsertMemory 是 pre-feature 基线行为,必须无条件执行(shadow/off 下 + // ApplyWriteOp 恒返回 shouldInsert=true ⇒ 下方不再 continue ⇒ 基础插入照跑)。 + // 唯一丢弃路径 = apply + NOOP(显式记 warning,含候选内容,禁静默)。 if distill.WriteOpsEnabled() { if dupID, dup := distill.WriteOpHashDedup(fact, sim); dup { log.Printf("%s", distill.DedupNote(dupID, fact)) @@ -294,16 +298,22 @@ func NewServer() http.Handler { } sims := distill.FromMemoryRecords(sim, qvec) verdict := distill.JudgeWriteOp(fact, sims.TopN(5), nil, nil) - if verdict == nil { - continue // off 模式 - } - shouldInsert, err := distill.ApplyWriteOp(*verdict, &ldbWriteOpsStore{ldb: ldb}, memIDFor(fact, input)) - if err != nil { - log.Printf("[write-ops] apply failed: %v", err) - } - if !shouldInsert { - continue // NOOP:丢弃 + if verdict != nil { + shouldInsert, err := distill.ApplyWriteOp(*verdict, &ldbWriteOpsStore{ldb: ldb}, memIDFor(fact, input)) + if err != nil { + // apply 标记失败(MarkSuperseded/MarkConflict 落库失败): + // 丢弃本条(不做半截标记),显式记 warning(禁静默失败)。 + log.Printf("[write-ops] apply 标记失败,本条事实丢弃: %v candidate=%.40s", err, fact) + continue + } + if !shouldInsert { + // 唯一丢弃路径:apply + NOOP(影子/off 不会到这里,见上) + log.Printf("[write-ops] apply NOOP 丢弃事实(判定=NOOP,候选=%.40s)", fact) + continue + } } + // verdict==nil(off 未跑判定;理论死分支——off 不进 WriteOpsEnabled 块) + // 或 shadow(标记零调用)⇒ 基础插入无条件执行。 } mem := models.MemoryRecord{ ID: fmt.Sprintf("mem_%d", time.Now().UnixNano()), diff --git a/go/internal/distill/write_ops.go b/go/internal/distill/write_ops.go index cb95d9e..cfa8b7c 100644 --- a/go/internal/distill/write_ops.go +++ b/go/internal/distill/write_ops.go @@ -17,9 +17,19 @@ // CONFLICT-MARK 矛盾 → 双方标记 conflict,交人工(DELETE 保守:永不自动删除) // // ── 门控(默认 shadow ⇒ 零行为变化,硬要求)────────────────────────── -// ZHIYI_WRITE_OPS=shadow(默认) 只记判定 log,不改库 -// ZHIYI_WRITE_OPS=apply 真改库(supersede/conflict 标记 + 新条 active) -// ZHIYI_WRITE_OPS=off 完全关(连判定都不跑,零开销) +// 门控**只决定标记动作**(supersede/conflict),**不得拦基础 InsertMemory** +// (基础插入是 pre-feature 基线行为,必须无条件执行;2026-10-15 t_a0831457 P0 +// 教训:旧实现让 shadow/off 恒不插入 ⇒ 10-14 起蒸馏事实全丢 2120 条)。 +// 三模式语义表: +// · shadow(默认) ⇒ 插入新条 ✓ + 不做任何标记 ✓ + 记 shadow log ✓ +// (= 注释里"零行为变化"的**字面**含义:等价于特性不存在) +// · apply ⇒ 插入新条 ✓ + 做标记 ✓(NOOP 丢弃;UPDATE 标 supersede; +// CONFLICT-MARK 标 conflict) +// · off ⇒ 插入新条 ✓ + 不跑判定 ✓(零开销) +// +// env(t_a0831457 AC10 改名,旧名兼容一档 + 弃用日志): +// ZHIYI_WRITE_SEMANTIC_OPS=shadow(默认)/apply/off +// 旧名 ZHIYI_WRITE_OPS 仍认(新名未设时兜底),日志提示弃用。 // // 影子观察 48h → 出四操作判定分布 → 转 apply 走生产变更纪律 // (停→备份→改→启→验,db 备份走门禁)。 @@ -126,13 +136,32 @@ func FromMemoryRecords(recs []models.MemoryRecord, queryVec []float32) SimilarMe // JudgeFunc 判定函数签名。生产走 LLM(JudgementJudge);单测注入确定性假实现。 type JudgeFunc func(candidate string, similar []SimilarMemory) WriteVerdict -// writeOpsMode 读取写时四操作模式(shadow 默认 / apply / off) +// writeOpsMode 读取写时四操作模式(shadow 默认 / apply / off)。 +// t_a0831457 AC10:env 改名 ZHIYI_WRITE_OPS → ZHIYI_WRITE_SEMANTIC_OPS(明确只控 +// 语义增量操作,防基础写功能被误挂进此门控)。旧名保留兼容一档:新名未设时 +// 回退旧名,且首次读到旧名时打一条弃用日志(只打一次,不刷)。 +var writeOpsDeprecationOnce sync.Once + func writeOpsMode() string { - v := os.Getenv("ZHIYI_WRITE_OPS") - switch v { + switch v := os.Getenv("ZHIYI_WRITE_SEMANTIC_OPS"); v { case "apply", "off": return v + case "": + // 新名未设 → 旧名兜底(兼容一档) + old := os.Getenv("ZHIYI_WRITE_OPS") + if old != "" { + writeOpsDeprecationOnce.Do(func() { + log.Printf("[write-ops] DEPRECATED: env ZHIYI_WRITE_OPS 已弃用,请改用 ZHIYI_WRITE_SEMANTIC_OPS(当前值=%s,语义不变,下版本移除旧名)", old) + }) + } + switch old { + case "apply", "off": + return old + default: + return "shadow" + } default: + // 非 apply/off 的值(含拼写错误)→ 默认 shadow(安全默认,零行为变化) return "shadow" } } @@ -348,18 +377,32 @@ type WriteOpsStore interface { MarkConflict(id string) error } -// ApplyWriteOp 应用四操作判定到存储(仅 apply 模式)。返回是否应插入新记忆 + 错误。 -// ⚠️ 影子/off 模式:本函数是 no-op(不改库、不插入),确保零行为变化—— -// 调用方即使误调也不产生副作用(影子纪律:先观察,不真拦)。 -// 语义: -// · ADD → 插入新记忆(返回 true) -// · UPDATE → 旧条标 superseded_by(历史保留)+ 插入新记忆(新条 active,supersedes=旧ID) -// · NOOP → 不插入(丢弃) -// · CONFLICT-MARK→ 旧条标 conflict + 新记忆标 conflict(交人工),仍插入(不静默丢,可人工复核) -// 返回 shouldInsert:true = 调用方应继续 InsertMemory;false = 丢弃。 +// ApplyWriteOp 应用四操作判定到存储。返回是否应插入新记忆 + 错误。 +// +// t_a0831457 P0 解耦修复:门控**只决定标记动作**(supersede/conflict), +// **不得拦基础 InsertMemory** —— 基础插入是 pre-feature 基线行为("特性不存在 +// 之前"插入总是发生),"零行为变化"的硬要求 ⇒ 默认态(shadow)必须等价于 +// 特性不存在。旧实现在 shadow/off 恒返回 shouldInsert=false,调用点据此 +// `continue` 跳过基础插入 ⇒ 2026-10-14 起每日蒸馏事实 2053/1094/1559 → 384 → 0 +// 全丢(2120 条判定被弃),属实现违背自身注释声明的 bug。 +// +// 三模式语义表(与文件头部门控节一致): +// · shadow(默认)⇒ 本函数对所有判定返回 shouldInsert=true:插入新条 ✓ +// + 不做任何标记(MarkSuperseded/MarkConflict 零调用)✓ + 判定已由 +// JudgeWriteOp 记入 shadow log ✓ +// · off ⇒ 不跑判定(JudgeWriteOp 返回 nil,调用点不进本函数); +// 即使误调本函数也返回 true(基础插入照跑,零开销) +// · apply ⇒ 按判定执行标记 + 插入: +// · ADD → 插入新记忆(返回 true) +// · UPDATE → 旧条标 superseded_by(历史保留)+ 插入新记忆(新条 active) +// · NOOP → 不插入(丢弃,特性的预期去重行为) +// · CONFLICT-MARK→ 旧条标 conflict + 新记忆供人工复核,仍插入(不静默丢) +// 返回 shouldInsert:true = 调用方应继续 InsertMemory;false = 丢弃 +// (**只有 apply + NOOP / 标记失败** 才返回 false)。 func ApplyWriteOp(v WriteVerdict, store WriteOpsStore, newID string) (shouldInsert bool, err error) { if !writeOpsApply() { - return false, nil // 影子/off 模式:零行为变化 + // shadow/off:门控不参与插入决策、不做任何标记 —— 基础插入无条件执行。 + return true, nil } if v.Op == OpNoop { return false, nil diff --git a/go/internal/distill/write_ops_regression_test.go b/go/internal/distill/write_ops_regression_test.go new file mode 100644 index 0000000..21c481f --- /dev/null +++ b/go/internal/distill/write_ops_regression_test.go @@ -0,0 +1,150 @@ +// 织忆 MemoryWeave — 实验门控不得劫持基础插入(t_a0831457 P0 解耦修复的回归测试) +// +// 背景:写时四操作门控默认 shadow,旧实现让 ApplyWriteOp 在 shadow/off 恒返回 +// shouldInsert=false ⇒ 调用点 server.go 的 `if !shouldInsert { continue }` 把基础 +// InsertMemory 整个跳过 ⇒ 2026-10-14 起每日蒸馏事实 2053/1094/1559 → 384 → 0, +// 2120 条判定该落库的事实被丢(原始层 episodes 完整,可重蒸)。 +// +// 修复语义(三模式表,与 write_ops.go 头部注释一致): +// · shadow(默认)⇒ 插入新条 ✓ + 不做任何标记 ✓ + 记 shadow log ✓(= 特性不存在之前的基线) +// · apply ⇒ 插入新条 ✓ + 做标记 ✓(NOOP 丢弃) +// · off ⇒ 插入新条 ✓ + 不跑判定 ✓(零开销) +// +// ⚠️ 本文件只用旧代码就存在的符号(ApplyWriteOp/JudgeWriteOp/withMode/…), +// 可直接在旧代码上运行且**必须失败**(先红);修复后全绿(后绿)。 +// 运行:go test ./internal/distill/ -run 'TestRegression' -v +package distill + +import ( + "testing" +) + +// 测试环境兜底:新 env 名(ZHIYI_WRITE_SEMANTIC_OPS,AC10 改名)置空 = 未设, +// 避免测试机残留的新名干扰旧名/默认路径的断言。 +func clearSemanticOpsEnv(t *testing.T) { + t.Setenv("ZHIYI_WRITE_SEMANTIC_OPS", "") +} + +// 四操作各造一个确定性 verdict(不依赖 LLM,与 write_ops_test.go 同型夹具)。 +func mkVerdicts() []WriteVerdict { + return []WriteVerdict{ + {Op: OpAdd, Candidate: "一个独立新事实A", Similarity: 0.2, Reason: "无语义等价 → 新建"}, + {Op: OpUpdate, Candidate: "牧尘在招商局工作 负责算法部门", TargetID: "mem_1", Similarity: 0.92, Reason: "互补/更新"}, + {Op: OpNoop, Candidate: "牧尘在招商局工作", TargetID: "mem_1", Similarity: 0.99, Reason: "语义等价/完全重复"}, + {Op: OpConflictMark, Candidate: "牧尘不在招商局工作了", TargetID: "mem_1", Similarity: 0.85, Reason: "矛盾"}, + } +} + +// P0 主断言(AC2 + 增补):shadow 下任何判定都**不拦**基础插入, +// 且 **MarkSuperseded / MarkConflict 零调用**(门控只决定标记,不决定插入)。 +func TestRegression_ShadowMode_InsertAlwaysProceeds(t *testing.T) { + clearSemanticOpsEnv(t) + defer withMode("shadow")() + defer withLogPath(t)() + for _, v := range mkVerdicts() { + t.Run(string(v.Op), func(t *testing.T) { + store := newFakeStore() + shouldInsert, err := ApplyWriteOp(v, store, "mem_new") + if err != nil { + t.Fatalf("shadow %s 不应有错:err=%v", v.Op, err) + } + if !shouldInsert { + t.Fatalf("P0:shadow %s 不得拦基础插入(旧实现此处恒 false ⇒ 蒸馏事实全丢):shouldInsert=%v", v.Op, shouldInsert) + } + // AC2 增补:shadow 下标记动作必须零调用 + if len(store.superseded) != 0 || len(store.conflicted) != 0 { + t.Fatalf("P0:shadow 不得做任何标记:superseded=%v conflicted=%v", store.superseded, store.conflicted) + } + }) + } +} + +// 默认值安全断言(门控设计铁律 #3:默认态必须等价于「特性不存在」)。 +func TestRegression_DefaultMode_InsertAlwaysProceeds(t *testing.T) { + clearSemanticOpsEnv(t) + defer withMode("")() // 空值模拟未设(writeOpsMode 回退默认 shadow) + defer withLogPath(t)() + v := JudgeWriteOp("默认态测试事实", []SimilarMemory{{ID: "mem_1", Content: "无关事实", Similarity: 0.2}}, nil, nil) + if v == nil || v.Op != OpAdd { + t.Fatalf("默认(shadow)态应跑判定且判 ADD,实际 %v", v) + } + shouldInsert, err := ApplyWriteOp(*v, newFakeStore(), "mem_new") + if err != nil || !shouldInsert { + t.Fatalf("P0:默认态不得拦基础插入:shouldInsert=%v err=%v", shouldInsert, err) + } +} + +// off:不跑判定(JudgeWriteOp 恒 nil)⇒ 调用点跳过整个门控块,基础插入照常; +// 且即使 off 态误调 ApplyWriteOp,也不得拦基础插入(零开销语义)。 +func TestRegression_OffMode_NoVerdict_InsertProceeds(t *testing.T) { + clearSemanticOpsEnv(t) + defer withMode("off")() + if v := JudgeWriteOp("任意事实", []SimilarMemory{{ID: "mem_1", Content: "无关", Similarity: 0.2}}, nil, nil); v != nil { + t.Fatalf("off 不得跑判定(JudgeWriteOp 应返回 nil),实际 %+v", v) + } + shouldInsert, err := ApplyWriteOp(WriteVerdict{Op: OpNoop}, newFakeStore(), "mem_new") + if err != nil || !shouldInsert { + t.Fatalf("P0:off 不得拦基础插入:shouldInsert=%v err=%v", shouldInsert, err) + } +} + +// apply 语义保持(解耦修复不得顺手改掉 apply 的真行为): +// NOOP 仍丢弃(这是特性的预期去重行为,不是基础功能)。 +func TestRegression_ApplyMode_NoopStillDrops(t *testing.T) { + clearSemanticOpsEnv(t) + defer withMode("apply")() + shouldInsert, err := ApplyWriteOp(WriteVerdict{Op: OpNoop, TargetID: "mem_1"}, newFakeStore(), "mem_new") + if err != nil || shouldInsert { + t.Fatalf("apply+NOOP 应丢弃:shouldInsert=%v err=%v", shouldInsert, err) + } +} + +// apply + UPDATE:标记(supersede)+ 插入 —— 特性行为保持。 +func TestRegression_ApplyMode_UpdateMarksAndInserts(t *testing.T) { + clearSemanticOpsEnv(t) + defer withMode("apply")() + store := newFakeStore() + shouldInsert, err := ApplyWriteOp(WriteVerdict{Op: OpUpdate, TargetID: "mem_1"}, store, "mem_new") + if err != nil || !shouldInsert { + t.Fatalf("apply+UPDATE 应插入:shouldInsert=%v err=%v", shouldInsert, err) + } + if store.superseded["mem_1"] != "mem_new" { + t.Fatalf("apply+UPDATE 应标 superseded_by=mem_new:%v", store.superseded) + } +} + +// apply + CONFLICT-MARK:标记(conflict)+ 插入 —— 特性行为保持。 +func TestRegression_ApplyMode_ConflictMarksAndInserts(t *testing.T) { + clearSemanticOpsEnv(t) + defer withMode("apply")() + store := newFakeStore() + shouldInsert, err := ApplyWriteOp(WriteVerdict{Op: OpConflictMark, TargetID: "mem_1"}, store, "mem_new") + if err != nil || !shouldInsert { + t.Fatalf("apply+CONFLICT-MARK 应插入(供人工复核):shouldInsert=%v err=%v", shouldInsert, err) + } + if !store.conflicted["mem_1"] { + t.Fatalf("apply+CONFLICT-MARK 应标 conflict:%v", store.conflicted) + } +} + +// AC10:env 改名 —— 新名 ZHIYI_WRITE_SEMANTIC_OPS 优先;旧名兼容一档; +// 都未设 = shadow 默认(默认值不许漂移)。 +func TestRegression_EnvRename_CompatMatrix(t *testing.T) { + // ① 新名生效(旧名相反值也不许干扰) + t.Setenv("ZHIYI_WRITE_SEMANTIC_OPS", "apply") + t.Setenv("ZHIYI_WRITE_OPS", "off") + if WriteOpsMode() != "apply" { + t.Fatalf("新名应优先:mode=%s", WriteOpsMode()) + } + // ② 新名未设 → 旧名兜底(兼容一档) + t.Setenv("ZHIYI_WRITE_SEMANTIC_OPS", "") + t.Setenv("ZHIYI_WRITE_OPS", "off") + if WriteOpsMode() != "off" { + t.Fatalf("旧名兼容失效:mode=%s", WriteOpsMode()) + } + // ③ 双名都未设 → 默认 shadow + t.Setenv("ZHIYI_WRITE_OPS", "") + if WriteOpsMode() != "shadow" { + t.Fatalf("默认值漂移(必须 shadow):mode=%s", WriteOpsMode()) + } +} diff --git a/go/internal/distill/write_ops_test.go b/go/internal/distill/write_ops_test.go index 76bbbc0..a28f268 100644 --- a/go/internal/distill/write_ops_test.go +++ b/go/internal/distill/write_ops_test.go @@ -148,24 +148,42 @@ func TestWriteOps_Update(t *testing.T) { } } -// 操作3:NOOP —— 无新增价值 → 丢弃(不插入) +// 操作3:NOOP —— 无新增价值。 +// t_a0831457 解耦修复后语义:apply 模式丢弃(特性预期去重行为); +// shadow/默认模式**不再拦基础插入**(门控只决定标记,不决定插入)—— +// 旧断言「shadow+NOOP 不插入」正是 10-14 起蒸馏事实全丢的 bug 语义,已改写。 func TestWriteOps_Noop(t *testing.T) { - defer withMode("shadow")() candidate := "牧尘在招商局工作" similar := []SimilarMemory{ {ID: "mem_1", Content: "牧尘在招商局工作", Similarity: 0.99}, // 语义等价 } - v := JudgeWriteOp(candidate, similar, nil, nil) - if v == nil { - t.Fatalf("nil verdict") - } - if v.Op != OpNoop { - t.Fatalf("期望 NOOP,实际 %s(%s)", v.Op, v.Reason) - } - shouldInsert, err := ApplyWriteOp(*v, newFakeStore(), "mem_new") - if err != nil || shouldInsert { - t.Fatalf("NOOP 不应插入:shouldInsert=%v err=%v", shouldInsert, err) - } + t.Run("apply+NOOP_丢弃", func(t *testing.T) { + defer withMode("apply")() + v := JudgeWriteOp(candidate, similar, nil, nil) + if v == nil || v.Op != OpNoop { + t.Fatalf("期望 NOOP,实际 %v", v) + } + shouldInsert, err := ApplyWriteOp(*v, newFakeStore(), "mem_new") + if err != nil || shouldInsert { + t.Fatalf("apply+NOOP 应丢弃:shouldInsert=%v err=%v", shouldInsert, err) + } + }) + t.Run("shadow+NOOP_基础插入照跑", func(t *testing.T) { + defer withMode("shadow")() + defer withLogPath(t)() + v := JudgeWriteOp(candidate, similar, nil, nil) + if v == nil || v.Op != OpNoop { + t.Fatalf("期望 NOOP,实际 %v", v) + } + store := newFakeStore() + shouldInsert, err := ApplyWriteOp(*v, store, "mem_new") + if err != nil || !shouldInsert { + t.Fatalf("P0:shadow+NOOP 不得拦基础插入:shouldInsert=%v err=%v", shouldInsert, err) + } + if len(store.superseded) != 0 || len(store.conflicted) != 0 { + t.Fatalf("shadow 不得做标记:%v %v", store.superseded, store.conflicted) + } + }) } // 操作4:CONFLICT-MARK —— 矛盾 → 双方标记 conflict(不删),交人工