根因:ApplyWriteOp 在 shadow(默认)/off 恒返回 shouldInsert=false ⇒ api/server.go 蒸馏写回路径
`if !shouldInsert { continue }` 把基础 InsertMemory 整个跳过 ⇒ 2026-10-14 起每日蒸馏事实
2053/1094/1559 → 384 → 0 全丢(shadow log 2189 条判定,2120 条该落库的被弃;episodes 原始层完整可重蒸)。
违背了门控自身注释声明的「默认 shadow ⇒ 零行为变化」硬要求(零变化=等价特性不存在,之前基础插入总是发生)。
改动(三模式语义表):
- shadow(默认) ⇒ 插入新条 ✓ + 不做任何标记 ✓ + 记 shadow log ✓
- apply ⇒ 插入新条 ✓ + 做标记 ✓(NOOP 丢弃;apply 语义不变,回归测试守住)
- off ⇒ 插入新条 ✓ + 不跑判定 ✓(零开销)
1) write_ops.go ApplyWriteOp:!writeOpsApply() 分支 false→true(门控不参与插入决策)
2) api/server.go 调用点:verdict!=nil 包一层;err/!shouldInsert 丢弃路径仅剩 apply+NOOP 与 apply 标记失败,均显式 warning(禁静默)
3) AC10:ZHIYI_WRITE_OPS → ZHIYI_WRITE_SEMANTIC_OPS(防「所有写操作开关」误读);旧名兼容一档+弃用日志(sync.Once 只打一次);默认值仍是 shadow
4) 回归测试 write_ops_regression_test.go(旧代码上先红已实证:shadow 4 判定/默认/off 5 项 FAIL):
断言 shadow 下四操作均 shouldInsert=true 且 MarkSuperseded/MarkConflict 零调用(AC2 增补)
5) 旧测试 TestWriteOps_Noop 改写:其「shadow+NOOP 不插入」断言即 bug 语义本身(量具自证:该测试随修复必须改,否则回归门失效)
边界(AC6):不动 episodes 层、不删任何 memories、不改默认值(仍 shadow,改的是 shadow 的语义);
与 284584c(lancedb_ipc.go/usage_track.go)文件不重叠,合并部署=一次观察协议+一个回滚点。
未部署:按 AC3/AC9 交证据包(diff+测试红/绿+md5+回滚方案+观察协议)本体转呈牧尘拍板。
151 lines
6.9 KiB
Go
151 lines
6.9 KiB
Go
// 织忆 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())
|
||
}
|
||
}
|