背景(2026-09-10 mc P0 事故):某轮蒸馏把含真 Tailscale 预授权 key 的原始对话
当事实入库 → 知识图谱按其文本建实体 → ObsidianSyncer 用实体标题当文件名/正文,
把 tskey-auth-… 落成可读镜像文件,又被 git 跟踪并推送 Gitea。
本提交在三个写入边界统一加脱敏。
新增 internal/redact(唯一真源):
- SecretPattern 覆盖 sk- / tskey- / github_pat_ / ghp_ / nvapi- / AKIA
- Go RE2 不支持 lookbehind,故"前一字符是否属于更长标识符"手工判定(isTokenByte)
- sk- 段只吃 [A-Za-z0-9],避免误伤 URL 里的 task-driven 片段(事故中的假阳性来源)
- RedactSecrets 幂等;ContainsSecret 供"直接丢弃"场景用
四处接入:
- distill/quality.go IsQualityFact 判定前脱敏 + FilterQualityFacts 返回值脱敏
- governance/graph_auto.go cleanEntityName 命中→空串;UpdateFromDistill 只对有效实体
建节点/建边;extractEntitiesFromText 过滤;含凭证的 fact 不建节点
- api/routes/obsidian.go sanitizeFilename 入参脱敏 + PushToObsidian 镜像正文脱敏
- api/server.go 蒸馏直写记忆库路径(绕过 FilterQualityFacts 的那条)同步脱敏
测试:13 条新用例(redact 5 / distill 3 / governance 4 / routes 1),
含 URL 假阳性回归与幂等性断言。全部使用构造的假密钥,不含任何真实凭证。
证据:go build ./... OK;
go test -count=1 ./internal/redact/ ./internal/distill/ ./internal/governance/ ./internal/api/routes/ 全绿。
顺带修既有 test 腐化(否则 governance 包根本无法编译测试,非本任务引入):
governance_test.go isContradiction→IsContradiction、Navigate 补 relFilter 参数;
bench_test.go 同。仅测试文件,无生产行为改动。
164 lines
4.1 KiB
Go
164 lines
4.1 KiB
Go
// 织忆 MemoryWeave — 治理层性能基准
|
|
package governance
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// ─── 冲突检测 ────────────────────────────────────────────
|
|
|
|
func BenchmarkConflictDetector_Scan100(b *testing.B) {
|
|
cd := NewConflictDetector()
|
|
// 预填充 100 条已有记忆
|
|
existing := make([]map[string]interface{}, 100)
|
|
for i := range existing {
|
|
existing[i] = map[string]interface{}{
|
|
"content": fmt.Sprintf("entity-%d has property value-%d", i, i),
|
|
"entities": []string{fmt.Sprintf("entity-%d", i)},
|
|
}
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
cd.Scan(
|
|
fmt.Sprintf("entity-%d has new value", b.N%100),
|
|
[]string{fmt.Sprintf("entity-%d", b.N%100)},
|
|
existing,
|
|
)
|
|
}
|
|
}
|
|
|
|
func BenchmarkConflictDetector_IsContradiction(b *testing.B) {
|
|
tests := [][2]string{
|
|
{"A is used by B", "A is not used by B"},
|
|
{"system runs on Linux", "system runs on Windows"},
|
|
{"config set to true", "config set to not true"},
|
|
}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
pair := tests[i%len(tests)]
|
|
IsContradiction(pair[0], pair[1])
|
|
}
|
|
}
|
|
|
|
// ─── 遗忘策略 ────────────────────────────────────────────
|
|
|
|
func BenchmarkForgetter_DecayScore(b *testing.B) {
|
|
f := NewForgetter()
|
|
times := []time.Time{
|
|
time.Now().AddDate(0, 0, -1), // 1 天前
|
|
time.Now().AddDate(0, 0, -30), // 30 天前
|
|
time.Now().AddDate(0, 0, -90), // 90 天前
|
|
time.Now().AddDate(0, 0, -365), // 1 年前
|
|
}
|
|
recalls := []int{0, 5, 20, 100}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
f.DecayScore(times[i%len(times)], recalls[i%len(recalls)])
|
|
}
|
|
}
|
|
|
|
func BenchmarkForgetter_ShouldForget(b *testing.B) {
|
|
f := NewForgetter()
|
|
times := []time.Time{
|
|
time.Now(),
|
|
time.Now().AddDate(0, -1, 0),
|
|
time.Now().AddDate(0, -6, 0),
|
|
}
|
|
tiers := []string{"normal", "core", "normal", "normal"}
|
|
recalls := []int{100, 5, 0, 0}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
f.ShouldForget(times[i%len(times)], recalls[i%len(recalls)], tiers[i%len(tiers)])
|
|
}
|
|
}
|
|
|
|
// ─── 图谱操作 ────────────────────────────────────────────
|
|
|
|
func BenchmarkGraph_AddNode(b *testing.B) {
|
|
g := NewInMemoryGraph()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
g.AddNode(fmt.Sprintf("node-%d", i), fmt.Sprintf("Entity-%d", i), "entity", "shared")
|
|
}
|
|
}
|
|
|
|
func BenchmarkGraph_AddEdge(b *testing.B) {
|
|
g := NewInMemoryGraph()
|
|
// 预填充 1000 节点
|
|
for i := 0; i < 1000; i++ {
|
|
g.AddNode(fmt.Sprintf("n-%d", i), fmt.Sprintf("E-%d", i), "entity", "shared")
|
|
}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
g.AddEdge(
|
|
fmt.Sprintf("e-%d", i),
|
|
fmt.Sprintf("n-%d", i%1000),
|
|
fmt.Sprintf("n-%d", (i+1)%1000),
|
|
"related_to",
|
|
"shared",
|
|
0.8,
|
|
)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGraph_Navigate_5Hops(b *testing.B) {
|
|
g := buildScaleGraph(200, 3)
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
g.Navigate(fmt.Sprintf("n-%d", i%200), 5, "shared", nil)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGraph_Navigate_Deep(b *testing.B) {
|
|
g := buildScaleGraph(500, 2)
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
g.Navigate("n-0", 10, "shared", nil)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGraph_Navigate_LargeScale(b *testing.B) {
|
|
g := buildScaleGraph(2000, 3)
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
g.Navigate(fmt.Sprintf("n-%d", i%2000), 3, "shared", nil)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGraph_Prune(b *testing.B) {
|
|
g := buildScaleGraph(500, 3)
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
g.Prune(0.3)
|
|
}
|
|
}
|
|
|
|
// buildScaleGraph 构建大规模测试图谱
|
|
func buildScaleGraph(nodes, edgesPerNode int) *InMemoryGraph {
|
|
g := NewInMemoryGraph()
|
|
for i := 0; i < nodes; i++ {
|
|
g.AddNode(fmt.Sprintf("n-%d", i), fmt.Sprintf("E-%d", i), "entity", "shared")
|
|
}
|
|
edgeCount := 0
|
|
for i := 0; i < nodes; i++ {
|
|
for j := 0; j < edgesPerNode; j++ {
|
|
target := (i + j + 1) % nodes
|
|
g.AddEdge(
|
|
fmt.Sprintf("e-%d", edgeCount),
|
|
fmt.Sprintf("n-%d", i),
|
|
fmt.Sprintf("n-%d", target),
|
|
"related",
|
|
"shared",
|
|
0.5+float64(j)*0.1,
|
|
)
|
|
edgeCount++
|
|
}
|
|
}
|
|
return g
|
|
}
|