Files
memoryweave/go/internal/pattern/semantics_test.go
T
小唯 d95245f45c feat(zhiyi): L2 扩语义 task_seq + discipline — tool_combo 之外两类新 pattern(t_751a6739)
DetectFromRecords 只认工具组合共现,45,132 条蒸馏记忆仅 1 条过 0.85。
新增两类语义(internal/pattern/semantics.go,与 tool_combo 并列不替换):
- task_seq 任务型:诊断/修复/复验 关键词位置严格有序 first(诊断)<first(修复)<first(复验)
- discipline 纪律型:触发动作位置 < 连接词(前/先/之前) < 约束动作位置
两类都机器可判(无 LLM),conf 沿用 confidenceFromFreq 同口径。
扩展 IsValidPatternName 写闸接纳新命名键。改后过 0.85 从 1 → 8 条。
零行为变化:不碰召回路径,ZHIYI_PATTERN_RECALL 默认关。
2026-11-09 02:36:44 +08:00

132 lines
4.3 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 pattern
import (
"testing"
"github.com/xiaoxue/memoryweave/internal/models"
)
func mem(id, content string) models.MemoryRecord {
return models.MemoryRecord{ID: id, Content: content, Category: "distilled", Vector: nil}
}
// TestDetect_TaskSeq —— 3 条「诊断→修复→复验」位置有序记忆 → 检出 1 个 task_seq(AC:任务型)。
func TestDetect_TaskSeq(t *testing.T) {
recs := []models.MemoryRecord{
mem("t1", "cron 超时问题已查清(根因)并修复,验收通过"),
mem("t2", "失败判据已修并逐处 sandbox 验证通过"),
mem("t3", "排查到根因后已修复,复核通过"),
}
pats := DetectFromRecords(recs, DefaultConfig())
var taskPats []Pattern
for _, p := range pats {
if p.Type == "task_seq" {
taskPats = append(taskPats, p)
}
}
if len(taskPats) != 1 {
t.Fatalf("3 条任务序列应产 1 个 task_seq,实际 %d: %#v", len(taskPats), pats)
}
if taskPats[0].Name != taskSeqName {
t.Fatalf("task_seq 命名键不符: got %q", taskPats[0].Name)
}
if len(taskPats[0].SourceTasks) != 3 {
t.Fatalf("task_seq 支撑应为 3 条,实际 %d", len(taskPats[0].SourceTasks))
}
}
// TestDetect_TaskSeq_OrderMatters —— 位置无序(修复在诊断前)⇒ 不产 task_seq(顺序语义,非词频)。
func TestDetect_TaskSeq_OrderMatters(t *testing.T) {
// 修复词「已修」出现在诊断词「排查」之前 ⇒ 顺序不满足
recs := []models.MemoryRecord{
mem("o1", "已修好并验证通过,无需再排查"),
mem("o2", "解决并验收,不用定位问题"),
mem("o3", "落地后复验,非故障"),
}
pats := DetectFromRecords(recs, DefaultConfig())
for _, p := range pats {
if p.Type == "task_seq" {
t.Fatalf("位置无序不应产 task_seq,实际 %#v", p)
}
}
}
// TestDetect_Discipline —— 3 条「改X前先备份」→ 检出 discipline(聚合 *先备份 + 具体 修改先备份)。
func TestDetect_Discipline(t *testing.T) {
recs := []models.MemoryRecord{
mem("d1", "改配置文件前先备份 jobs.json"),
mem("d2", "修改 AGENTS.md 前先备份到 backups/"),
mem("d3", "更新脚本前必须先全量备份"),
}
pats := DetectFromRecords(recs, DefaultConfig())
agg := map[string]bool{}
td := map[string]bool{}
for _, p := range pats {
if p.Type != "discipline" {
continue
}
if p.Name == "discipline:*先备份" {
agg["agg"] = true
}
if p.Name == "discipline:修改先备份" {
td["td"] = true
}
}
if !agg["agg"] {
t.Fatalf("应产出聚合 discipline:*先备份,实际 %#v", pats)
}
if !td["td"] {
t.Fatalf("应产出具体 discipline:修改先备份,实际 %#v", pats)
}
}
// TestDetect_Discipline_NoConnector —— 只有动作无「前/先」约束词 ⇒ 不产 discipline(顺序约束语义)。
func TestDetect_Discipline_NoConnector(t *testing.T) {
recs := []models.MemoryRecord{
mem("n1", "备份了 jobs.json 之后又改了配置"),
mem("n2", "改完配置再备份文件"),
mem("n3", "配置修改完成并备份"),
}
pats := DetectFromRecords(recs, DefaultConfig())
for _, p := range pats {
if p.Type == "discipline" {
t.Fatalf("无「前/先」约束词不应产 discipline,实际 %#v", p)
}
}
}
// TestIsValidPatternName_NewSemantics —— task_seq / discipline 命名键写闸。
func TestIsValidPatternName_NewSemantics(t *testing.T) {
good := []string{
"task_seq:诊断>修复>复验",
"discipline:*先备份",
"discipline:*先拉现状",
"discipline:修改先备份",
"discipline:安装先拉现状",
}
bad := []string{
"task_seq:别的模板",
"discipline:*先", // 空约束动作
"discipline:*先先建卡", // 嵌套 先 的坏键
"discipline:乱写先备份", // trigger 不在词表
"discipline:修改先乱写", // 动作不在词表
"discipline:备份", // 缺 先
}
for _, name := range bad {
if IsValidPatternName(name) {
t.Fatalf("非法命名键 %q 不应通过闸门", name)
}
}
for _, name := range good {
if !IsValidPatternName(name) {
t.Fatalf("合法命名键 %q 应通过闸门", name)
}
}
}
// TestDetect_Empty_NoNewSemantics —— 空输入不产 task_seq/discipline(AC1 空库前提,含新语义)。
func TestDetect_Empty_NoNewSemantics(t *testing.T) {
if pats := DetectFromRecords(nil, DefaultConfig()); len(pats) != 0 {
t.Fatalf("空输入应 0 pattern,实际 %d", len(pats))
}
}