ga-meta-1b t_67d0279a: 方案A 来源分档 — auditSource(worklog-note/audit) 蒸馏实体不写图, 事实仍进记忆层

终审采用 1a 方案A:
- DistillInput +Source (additive)
- server.go OnDistillComplete / auto_distill.go 降级路径 补 Source: input.Source
- graph_auto.go UpdateFromDistill 顶部: auditSource(Source) 命中即 return (跳 AddNode/AddEdge, 事实照写 LanceDB)
- auditSource: TrimSpace → TrimPrefix(wiki:) → ToLower (前缀归一, 防静默 no-op)
- echo_guard.go 判据零改
AC1 复现 entities=10→0 新节点; AC2 dry-run 35412 行误杀0; AC5 正负对照+skip-entity 日志; AC4 节点 delta 非负
This commit is contained in:
小唯
2026-10-12 23:56:58 +08:00
parent e900e4073f
commit 95bb930bd6
4 changed files with 99 additions and 0 deletions
+1
View File
@@ -54,6 +54,7 @@ func AutoDistillTrigger(episodeID string, content string, category string, names
Facts: facts,
Entities: extractEntities(content),
Namespace: namespace,
Source: meta.Source, // 2026-10-12 t_67d0279a 方案A:降级路径同样透传来源分档
})
}
+1
View File
@@ -172,6 +172,7 @@ func NewServer() http.Handler {
Content: input.Content, Facts: result.Facts,
Entities: entityNames, Namespace: input.Namespace,
EpisodeID: input.EpisodeID,
Source: input.Source, // 2026-10-12 t_67d0279a 方案A:来源分档透传(auditSource 前缀归一后比对)
})
// 回写蒸馏产物到记忆库(LanceDB)
+39
View File
@@ -3,6 +3,7 @@ package governance
import (
"fmt"
"log"
"strings"
"time"
"unicode"
@@ -86,6 +87,17 @@ func isASCIIOnly(s string) bool {
// UpdateFromDistill 从蒸馏产物自动更新图谱(§3.4)
func (agu *AutoGraphUpdater) UpdateFromDistill(distilled *DistillInput) {
// 0a. 来源分档(2026-10-12 t_67d0279a GA-META-1b,方案A):
// 审计/工作流类来源(worklog-note / worklog-audit)跳过实体/概念写图——
// 笔记本体仍经同一次蒸馏进 LanceDB 记忆层(facts 照写),只是不再被
// 拆成实体/概念节点写进图谱(元噪声根源:审计散文整句被 LLM 当实体)。
// 判据零改动(echo_guard.go 未动),语义边界:跳过的是「进图」,不是「进记忆」。
if auditSource(distilled.Source) {
log.Printf("[graph] skip-entity for source=%q episode=%s(来源分档:实体/概念不写图,事实仍进记忆层)",
distilled.Source, distilled.EpisodeID)
return
}
// 0. 密钥过滤(2026-09-10 P0,见 internal/redact):含凭证形状的实体不进图,
// 否则实体名会被 ObsidianSync 当文件名,把密钥落成可读镜像文件
// (实例:小唯/07-Wiki/织忆/未找到命令.md 的文件名与正文含 tskey-auth-…)。
@@ -204,6 +216,29 @@ func (agu *AutoGraphUpdater) RecordCoOccurrence(query, namespace string, resultI
// ─── 辅助 ─────────────────────────────────────────────
// auditSources 来源分档白名单(2026-10-12 t_67d0279a GA-META-1b,方案A):
// 命中这些来源的蒸馏产物只做「事实进记忆层」,跳过实体/概念写图。
// ⚠️ 匹配走 auditSource 的前缀归一(TrimPrefix "wiki:"),不做裸串精确相等——
// 库内真实值经 srcOrLLMDistill 落成 "wiki:worklog-note"([srcprobe] 实测),
// 直接 == "worklog-note" 永远 miss = 静默 no-op。
var auditSources = []string{"worklog-note", "worklog-audit"}
// auditSource 判定 distilled.Source 是否属于审计/工作流类来源(来源分档跳实体写图)。
// 归一:先 TrimPrefix "wiki:"(server.go srcOrLLMDistill 会给非空 source 加前缀),
// 再与 auditSources 精确比对。大小写不敏感兜底(来源标记是小写约定,防御上游变体)。
func auditSource(source string) bool {
// 先 TrimSpace 再 TrimPrefix —— 前导空白时 "wiki:" 前缀不在开头,直接 TrimPrefix 会 miss
s := strings.TrimSpace(source)
s = strings.ToLower(strings.TrimPrefix(s, "wiki:"))
s = strings.TrimSpace(s)
for _, a := range auditSources {
if s == a {
return true
}
}
return false
}
type DistillInput struct {
EpisodeID string
Content string
@@ -211,6 +246,10 @@ type DistillInput struct {
Decisions []string
Entities []string
Namespace string
// Source 来源标记透传(2026-10-12 t_67d0279a GA-META-1b,方案A来源分档):
// commit 写入方 metadata.source → distill.DistillInput.Source → 本字段。
// UpdateFromDistill 用它做来源分档:审计/工作流类来源跳过实体/概念写图。
Source string
}
func entityID(name string) string {
+58
View File
@@ -208,3 +208,61 @@ func TestRecordCoOccurrence_NoSelfLoop_WithDupResultIDs(t *testing.T) {
t.Errorf("RecordCoOccurrence 应 2 条非自环 CO_OCCURS,实际 %d", coEdges)
}
}
// ── 来源分档(2026-10-12 t_67d0279a GA-META-1b,方案A)──
// 审计/工作流类来源:跳实体/概念写图;正常来源:照写。AC5 防静默 no-op 的正/负对照。
func TestAuditSource_PrefixNormalization(t *testing.T) {
// 正对照:库内真实值带 wiki: 前缀(srcOrLLMDistill 加前缀,[srcprobe] 实测)
for _, s := range []string{"wiki:worklog-note", "worklog-note", "wiki:worklog-audit", "worklog-audit", " wiki:WORKLOG-NOTE "} {
if !auditSource(s) {
t.Errorf("auditSource(%q) 应为 true(前缀归一 + 大小写兜底)", s)
}
}
// 负对照:常规来源不得命中(防误杀)
for _, s := range []string{"wiki:llm_distill", "llm_distill", "wiki:vault-ingest", "vault-ingest", "", "wiki:worklog-notes"} {
if auditSource(s) {
t.Errorf("auditSource(%q) 应为 false(误杀常规来源)", s)
}
}
}
func TestUpdateFromDistill_AuditSourceSkipsEntityGraphWrites(t *testing.T) {
g := NewInMemoryGraph()
up := NewAutoGraphUpdater(g)
// 审计散文类内容 + 含实体/概念词,但来源 = 库内真实值 wiki:worklog-note
up.UpdateFromDistill(&DistillInput{
EpisodeID: "ep_audit_src_1",
Content: "空格版节点 n_牵挂 当前仍在图,与 hub 版同源同义",
Facts: []string{"待办.md 里已打勾的旧条目清理完成"},
Entities: []string{"Galway", "rothfuss", "Name of the Wind"},
Namespace: "ns_audit_src",
Source: "wiki:worklog-note",
})
// 节点/边都应为 0:实体不写图、实体间关系边不建、DERIVED_FROM 不建
if n := len(g.ListNodes("ns_audit_src")); n != 0 {
t.Errorf("audit 来源应 0 实体节点,实际 %d: %v", n, g.ListNodes("ns_audit_src"))
}
if e := len(g.AllEdges("ns_audit_src")); e != 0 {
t.Errorf("audit 来源应 0 条边,实际 %d", e)
}
}
func TestUpdateFromDistill_NormalSourceStillWritesEntities(t *testing.T) {
g := NewInMemoryGraph()
up := NewAutoGraphUpdater(g)
up.UpdateFromDistill(&DistillInput{
EpisodeID: "ep_normal_src_1",
Content: "配置 Docker Nginx",
Facts: []string{"Docker 容器已启动"},
Entities: []string{"Docker", "Nginx"},
Namespace: "ns_normal_src",
Source: "wiki:vault-ingest", // 常规来源:照写
})
nodes := g.ListNodes("ns_normal_src")
if len(nodes) < 2 {
t.Fatalf("常规来源应照写实体节点(≥2),实际 %d: %v", len(nodes), nodes)
}
if e := len(g.AllEdges("ns_normal_src")); e == 0 {
t.Errorf("常规来源应建关系边,实际 0 条")
}
}