608 lines
19 KiB
Go
608 lines
19 KiB
Go
// 织忆 MemoryWeave — L2 模式检测引擎(t_d32e5f8a)
|
||
//
|
||
// 背景:MemOS 血统的 L2「跨任务重复模式」检测。原实现活在 Python daemon 的
|
||
// main_loop(30s tick),2026-09-24 为 state-db 写安全整体退役后 L2 pattern 蒸馏
|
||
// 停摆(graph_nodes type='pattern' 停为 0 行;旧 Python daemon 残留的 85 行是
|
||
// type='pattern_stat',id 全 NULL、语义为 L1 命名空间统计快照,非 L2 pattern / 2026-09-18)。本包把 L1→L2 蒸馏
|
||
// 复活成**短命单发**(modernc sqlite 直连 graph.db,开/关不占长连接,不碰 state.db),
|
||
// 并按设计 §4.1 叠加两条检测规则:
|
||
// - 工具组合频率:同一 tool 组合出现 ≥3 次 → tool_combo pattern
|
||
// (confidence = min(freq*0.1, 1.0))
|
||
// - 相似解决路径聚类:cosine ≥0.85 成簇且簇内 ≥3 条 → workflow pattern
|
||
//
|
||
// 存储:复用现有 graph_nodes(type='pattern'),**不另开新表**(避免与现存图谱查询割裂;
|
||
// 表内另有 85 行 type='pattern_stat' 属旧 daemon 统计快照,与 L2 pattern 无关)。
|
||
// namespace='daemon-distill' 沿用旧 daemon 约定。
|
||
package pattern
|
||
|
||
import (
|
||
"fmt"
|
||
"math"
|
||
"sort"
|
||
"strings"
|
||
|
||
"github.com/xiaoxue/memoryweave/internal/models"
|
||
)
|
||
|
||
// Pattern 一个被检测出的 L2 模式。
|
||
type Pattern struct {
|
||
ID string
|
||
Type string // "tool_combo" | "workflow"
|
||
Name string // 稳定命名键(同组合去重)
|
||
Data string // tool_combo=排序后工具串;workflow=共同步骤/代表路径摘要
|
||
Confidence float64
|
||
SourceTasks []string // 支撑该 pattern 的记忆 ID(设计 source_tasks)
|
||
Tools []string // tool_combo 专用
|
||
}
|
||
|
||
// Config 检测参数(设计 §4.1)。
|
||
type Config struct {
|
||
MinComboFreq int // 工具组合需 ≥ N 条**独立来源记忆**才产 pattern(默认 2,t_639c5a03 门槛)
|
||
ClusterThresh float64 // 相似路径聚类 cosine 阈值(默认 0.85)
|
||
MinClusterSize int // 簇内 ≥ N 条才产 workflow pattern(默认 3)
|
||
MaxRecords int // 单次扫描参与检测的记忆上限(防 O(n²) 爆炸,默认 500)
|
||
TopKPatternList int // 召回/列出取前 N(默认 10)
|
||
}
|
||
|
||
// DefaultConfig 设计参数。
|
||
func DefaultConfig() Config {
|
||
return Config{
|
||
MinComboFreq: 2,
|
||
ClusterThresh: 0.85,
|
||
MinClusterSize: 3,
|
||
MaxRecords: 500,
|
||
TopKPatternList: 10,
|
||
}
|
||
}
|
||
|
||
// knownTools 常见工具/CLI/命令名(小写)白名单。t_639c5a03:工具组合的成员**必须**
|
||
// 来自本白名单或"命令式标识"(含 - 或数字,如 systemd-run / sqlite3),普通英文词不再入组合键。
|
||
// 剔除记录:"ts"(会命中时间戳片段 ts)、"jq2"(非真实命令)。
|
||
var knownTools = map[string]bool{
|
||
"jq": true, "yq": true, "git": true, "grep": true, "sed": true, "awk": true,
|
||
"curl": true, "wget": true, "tar": true, "zip": true, "unzip": true,
|
||
"docker": true, "kubectl": true, "helm": true, "npm": true, "pip": true,
|
||
"go": true, "python": true, "python3": true, "node": true, "pnpm": true,
|
||
"make": true, "cmake": true, "gcc": true, "gcc-12": true, "clang": true,
|
||
"sqlite3": true, "psql": true, "mysql": true, "redis-cli": true,
|
||
"ssh": true, "scp": true, "rsync": true, "systemctl": true, "journalctl": true,
|
||
"systemd-run": true, "crontab": true, "find": true, "rg": true, "ripgrep": true,
|
||
"fd": true, "fzf": true, "gh": true, "gcloud": true, "aws": true,
|
||
"hermes": true, "zhiyi": true, "obsidian": true, "esbuild": true,
|
||
"tsc": true, "cargo": true, "rustc": true, "swift": true, "brew": true,
|
||
"bash": true, "sh": true, "zsh": true, "cat": true, "ls": true, "cp": true,
|
||
"mv": true, "rm": true, "mkdir": true, "chmod": true, "head": true, "tail": true,
|
||
"sort": true, "uniq": true, "wc": true, "xargs": true, "kill": true, "ps": true,
|
||
"lsof": true, "flock": true, "ping": true, "nc": true, "openssl": true,
|
||
"java": true, "javac": true, "mvn": true, "gradle": true, "dotnet": true,
|
||
"remotion": true, "uv": true, "pytest": true, "gofmt": true, "golangci-lint": true,
|
||
}
|
||
|
||
// commonExts 常见文件扩展名(小写,含点)。token 以此结尾 ⇒ 文件名/扩展名,不是工具。
|
||
var commonExts = map[string]bool{
|
||
".md": true, ".go": true, ".json": true, ".py": true, ".js": true, ".ts": true,
|
||
".tsx": true, ".jsx": true, ".yaml": true, ".yml": true, ".toml": true, ".sh": true,
|
||
".db": true, ".sql": true, ".csv": true, ".log": true, ".txt": true, ".html": true,
|
||
".css": true, ".rs": true, ".c": true, ".h": true, ".cpp": true, ".conf": true,
|
||
".xml": true, ".ini": true, ".env": true, ".pdf": true, ".png": true, ".jpg": true,
|
||
".jpeg": true, ".svg": true, ".gif": true, ".ipynb": true, ".lock": true, ".sum": true,
|
||
".bak": true, ".tmp": true, ".gz": true, ".mp4": true, ".mp3": true, ".docx": true,
|
||
".xlsx": true, ".pptx": true, ".service": true, ".sock": true, ".socket": true,
|
||
}
|
||
|
||
// toolStopwords 常见停用词(小写):高频非工具词,绝不作为工具名(卡要求第 1 条)。
|
||
var toolStopwords = map[string]bool{
|
||
"the": true, "and": true, "for": true, "with": true, "that": true, "this": true,
|
||
"from": true, "into": true, "then": true, "when": true, "what": true, "they": true,
|
||
"have": true, "will": true, "your": true, "not": true, "but": true, "all": true,
|
||
"can": true, "use": true, "via": true, "per": true, "are": true, "was": true,
|
||
"one": true, "two": true, "ok": true, "yes": true, "true": true, "false": true,
|
||
"null": true, "none": true, "etc": true, "vs": true, "id": true, "ids": true,
|
||
"ns": true, "log": true, "logs": true, "file": true, "files": true, "path": true,
|
||
"line": true, "lines": true, "name": true, "type": true, "data": true, "time": true,
|
||
"date": true, "test": true, "note": true, "step": true, "steps": true, "first": true,
|
||
"last": true, "next": true, "top": true, "api": true, "http": true, "https": true,
|
||
"url": true, "uri": true, "json": true, "yaml": true, "yml": true, "md": true,
|
||
"txt": true, "tmp": true, "dev": true, "prod": true, "src": true, "doc": true,
|
||
"docs": true, "ref": true, "config": true, "default": true, "error": true,
|
||
"errors": true, "warn": true, "info": true, "debug": true, "fail": true,
|
||
"failed": true, "success": true, "start": true, "stop": true, "check": true,
|
||
"list": true, "get": true, "set": true, "new": true, "old": true, "fix": true,
|
||
"pr": true, "mr": true, "db": true,
|
||
}
|
||
|
||
// DetectFromRecords 纯检测核心:从 L1 记忆列表发现重复模式。
|
||
// 不读 LanceDB / 不写库,只做「记录 → pattern 候选」,便于确定性单测。
|
||
// 返回合并去重后的 pattern 列表(同 Name 合并 source_tasks、取更高 confidence)。
|
||
func DetectFromRecords(records []models.MemoryRecord, cfg Config) []Pattern {
|
||
if cfg.MinComboFreq <= 0 {
|
||
cfg.MinComboFreq = 2
|
||
}
|
||
if cfg.MinClusterSize <= 0 {
|
||
cfg.MinClusterSize = 3
|
||
}
|
||
if cfg.ClusterThresh <= 0 {
|
||
cfg.ClusterThresh = 0.85
|
||
}
|
||
lim := cfg.MaxRecords
|
||
if lim <= 0 {
|
||
lim = 500
|
||
}
|
||
if len(records) > lim {
|
||
records = records[:lim]
|
||
}
|
||
|
||
combined := make(map[string]*Pattern) // Name → merged pattern
|
||
order := []string{}
|
||
|
||
merge := func(p Pattern) {
|
||
if ex, ok := combined[p.Name]; ok {
|
||
ex.SourceTasks = append(ex.SourceTasks, p.SourceTasks...)
|
||
if p.Confidence > ex.Confidence {
|
||
ex.Confidence = p.Confidence
|
||
}
|
||
return
|
||
}
|
||
combined[p.Name] = &p
|
||
order = append(order, p.Name)
|
||
}
|
||
|
||
// 1. 工具组合频率分析
|
||
comboCount := map[string]*struct {
|
||
tools []string
|
||
tasks []string
|
||
}{}
|
||
for _, rec := range records {
|
||
tools := ExtractTools(rec.Content)
|
||
if len(tools) == 0 {
|
||
continue
|
||
}
|
||
tools = dedupeSorted(tools)
|
||
key := "tool_combo:" + strings.Join(tools, "+")
|
||
c := comboCount[key]
|
||
if c == nil {
|
||
c = &struct {
|
||
tools []string
|
||
tasks []string
|
||
}{tools: tools}
|
||
comboCount[key] = c
|
||
}
|
||
c.tasks = append(c.tasks, rec.ID)
|
||
}
|
||
for name, c := range comboCount {
|
||
// t_639c5a03 门槛:支撑数按**独立来源记忆**(去重且非空 ID)计,< MinComboFreq 直接丢。
|
||
seen := map[string]bool{}
|
||
var sources []string
|
||
for _, id := range c.tasks {
|
||
if id != "" && !seen[id] {
|
||
seen[id] = true
|
||
sources = append(sources, id)
|
||
}
|
||
}
|
||
if len(sources) < cfg.MinComboFreq {
|
||
continue
|
||
}
|
||
merge(Pattern{
|
||
Type: "tool_combo",
|
||
Name: name,
|
||
Data: strings.Join(c.tools, "+"),
|
||
Tools: c.tools,
|
||
Confidence: confidenceFromFreq(len(sources)),
|
||
SourceTasks: sources,
|
||
})
|
||
}
|
||
|
||
// 2. 相似解决路径聚类(cosine ≥ 阈值 成簇,簇内 ≥ N 条)
|
||
paths := clusterPaths(records, cfg.ClusterThresh, cfg.MinClusterSize)
|
||
for _, cluster := range paths {
|
||
// 代表路径 = 簇内最长 content(共同步骤近似)
|
||
rep := ""
|
||
var ids []string
|
||
for _, r := range cluster {
|
||
ids = append(ids, r.ID)
|
||
if len(r.Content) > len(rep) {
|
||
rep = r.Content
|
||
}
|
||
}
|
||
conf := clusterConfidence(cluster) // 平均两两 cosine,封顶 1.0
|
||
if conf > 1.0 {
|
||
conf = 1.0
|
||
}
|
||
// 簇名带代表内容哈希前 8 位 —— 同 size 不同内容簇不碰撞(否则 cluster-3 撞簇)
|
||
name := fmt.Sprintf("workflow:cluster-%d-%s", len(cluster), contentFingerprint(rep))
|
||
merge(Pattern{
|
||
Type: "workflow",
|
||
Name: name,
|
||
Data: truncateForPattern(rep, 120),
|
||
Confidence: conf,
|
||
SourceTasks: ids,
|
||
})
|
||
}
|
||
|
||
out := make([]Pattern, 0, len(order))
|
||
for _, n := range order {
|
||
p := *combined[n]
|
||
p.SourceTasks = dedupeStrings(p.SourceTasks)
|
||
if p.Type == "tool_combo" {
|
||
p.ID = "pat_" + p.Name
|
||
} else {
|
||
p.ID = "pat_" + p.Name
|
||
}
|
||
out = append(out, p)
|
||
}
|
||
sort.SliceStable(out, func(i, j int) bool {
|
||
if out[i].Type != out[j].Type {
|
||
return out[i].Type < out[j].Type
|
||
}
|
||
return out[i].Name < out[j].Name
|
||
})
|
||
return out
|
||
}
|
||
|
||
// confidenceFromFreq 设计公式:min(freq*0.1, 1.0)。
|
||
func confidenceFromFreq(freq int) float64 {
|
||
c := float64(freq) * 0.1
|
||
if c > 1.0 {
|
||
c = 1.0
|
||
}
|
||
return c
|
||
}
|
||
|
||
// clusterPaths 贪心成簇:为每条未分配记忆开一个簇,把与其 cosine≥阈值 的其余未分配
|
||
// 记忆并入;最终只保留 size≥minSize 的簇。向量缺失时用记录自带 Vector,全缺则退到
|
||
// 文本 bigram Jaccard(确定性,不依赖网络)。
|
||
func clusterPaths(records []models.MemoryRecord, thresh float64, minSize int) [][]models.MemoryRecord {
|
||
n := len(records)
|
||
if n < minSize {
|
||
return nil
|
||
}
|
||
type sig struct {
|
||
vec []float32
|
||
big map[string]bool
|
||
text string
|
||
}
|
||
sigs := make([]sig, n)
|
||
for i, r := range records {
|
||
sigs[i] = sig{vec: r.Vector, text: r.Content}
|
||
}
|
||
sim := func(a, b int) float64 {
|
||
sa, sb := sigs[a], sigs[b]
|
||
if len(sa.vec) > 0 && len(sb.vec) > 0 {
|
||
return cosineF32(sa.vec, sb.vec)
|
||
}
|
||
if len(sa.vec) == 0 && len(sb.vec) == 0 {
|
||
return bigramJaccard(sa.text, sb.text)
|
||
}
|
||
return 0
|
||
}
|
||
assigned := make([]bool, n)
|
||
var clusters [][]models.MemoryRecord
|
||
for i := 0; i < n; i++ {
|
||
if assigned[i] {
|
||
continue
|
||
}
|
||
cluster := []models.MemoryRecord{records[i]}
|
||
assigned[i] = true
|
||
for j := i + 1; j < n; j++ {
|
||
if assigned[j] {
|
||
continue
|
||
}
|
||
if sim(i, j) >= thresh {
|
||
cluster = append(cluster, records[j])
|
||
assigned[j] = true
|
||
}
|
||
}
|
||
if len(cluster) >= minSize {
|
||
clusters = append(clusters, cluster)
|
||
}
|
||
}
|
||
return clusters
|
||
}
|
||
|
||
// clusterConfidence 簇内平均两两相似度(cosine 或 Jaccard)。
|
||
func clusterConfidence(cluster []models.MemoryRecord) float64 {
|
||
n := len(cluster)
|
||
if n < 2 {
|
||
return 1.0
|
||
}
|
||
sum := 0.0
|
||
pairs := 0
|
||
for i := 0; i < n; i++ {
|
||
for j := i + 1; j < n; j++ {
|
||
var s float64
|
||
a, b := cluster[i], cluster[j]
|
||
if len(a.Vector) > 0 && len(b.Vector) > 0 {
|
||
s = cosineF32(a.Vector, b.Vector)
|
||
} else {
|
||
s = bigramJaccard(a.Content, b.Content)
|
||
}
|
||
sum += s
|
||
pairs++
|
||
}
|
||
}
|
||
if pairs == 0 {
|
||
return 1.0
|
||
}
|
||
return sum / float64(pairs)
|
||
}
|
||
|
||
// ExtractTools 从记忆内容提取工具/命令名(t_639c5a03 重写)。
|
||
// 判据(卡·必须实现 #1):成员必须是**真实的命令/tool 标识** ——
|
||
// - knownTools 白名单(词边界命中),或
|
||
// - "命令式标识" token:在白名单外但形如命令名(含 '-' 或数字,如 systemd-run、sqlite3),
|
||
// 且通过 isValidTool 形态检查(排除扩展名 .md、日期/数字片段 2026 / 2026-09-23、
|
||
// 纯标点、长度 <2、路径/任务 ID、停用词)。
|
||
//
|
||
// 旧实现的"宽松兜底"(任何 ascii token 都进组合键)是 `.md`/`2026+23` 垃圾 pattern 的根因,已删。
|
||
// 确定性:结果排序去重。
|
||
func ExtractTools(content string) []string {
|
||
found := map[string]bool{}
|
||
lower := strings.ToLower(content)
|
||
for t := range knownTools {
|
||
// 词边界匹配(避免 "go" 命中 "google")
|
||
if isValidTool(t) && tokenPresent(lower, t) {
|
||
found[t] = true
|
||
}
|
||
}
|
||
for _, tok := range regexTokenExtract(content) {
|
||
lt := strings.ToLower(tok)
|
||
if !isValidTool(lt) {
|
||
continue
|
||
}
|
||
// 白名单外只放行"命令式标识"(含 - 或数字);普通英文词(log/step/report…)不进组合键。
|
||
if !knownTools[lt] && !strings.ContainsAny(lt, "-0123456789") {
|
||
continue
|
||
}
|
||
found[lt] = true
|
||
}
|
||
out := make([]string, 0, len(found))
|
||
for t := range found {
|
||
out = append(out, t)
|
||
}
|
||
sort.Strings(out)
|
||
return out
|
||
}
|
||
|
||
// isValidTool token 形态判据(lowercase):是否为真实命令/工具标识的形状。
|
||
// 排除:长度 <2 或 >32;含 '/'、'\\'、'_'(路径、usage_track 键、t_9143c968 任务 ID);
|
||
// 以 '.' 开头(".md");无字母(纯数字/纯标点/数字+分隔符);以常见扩展名结尾
|
||
// (report.md / a.py);数字/日期/版本片段(2026、2026-09-23、2026+23、09:23、1.2.3、v1.2.3);
|
||
// 停用词。
|
||
func isValidTool(tok string) bool {
|
||
if len(tok) < 2 || len(tok) > 32 {
|
||
return false
|
||
}
|
||
if strings.ContainsAny(tok, `/_\`) {
|
||
return false
|
||
}
|
||
if tok[0] == '.' || tok[0] == '-' {
|
||
return false // 隐藏文件 / CLI flag
|
||
}
|
||
hasLetter := false
|
||
for i := 0; i < len(tok); i++ {
|
||
if c := tok[i]; (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
|
||
hasLetter = true
|
||
break
|
||
}
|
||
}
|
||
if !hasLetter {
|
||
return false
|
||
}
|
||
if i := strings.LastIndexByte(tok, '.'); i > 0 && commonExts[tok[i:]] {
|
||
return false
|
||
}
|
||
if isNumericFragment(tok) {
|
||
return false
|
||
}
|
||
if toolStopwords[tok] {
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
// isNumericFragment 纯数字 / 日期 / 时间 / 版本片段:
|
||
// - 只由数字与分隔符 [-+:./] 组成("2026"、"2026-09-23"、"2026+23"、"09:23"、"1.2.3"),或
|
||
// - 至多一个前导字母 + 其余全为数字/分隔符("v1.2.3"、"v2")。
|
||
//
|
||
// "gcc-12"/"sqlite3" 这类字母出现在数字之前且不止前导一个 ⇒ 正常标识,返回 false。
|
||
func isNumericFragment(tok string) bool {
|
||
if tok == "" {
|
||
return false
|
||
}
|
||
seenDigit := false
|
||
rest := tok
|
||
if c := rest[0]; (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') {
|
||
rest = rest[1:] // 至多剥一个前导字母
|
||
}
|
||
for _, r := range rest {
|
||
switch {
|
||
case r >= '0' && r <= '9':
|
||
seenDigit = true
|
||
case r == '-' || r == '+' || r == ':' || r == '.' || r == '/':
|
||
// 分隔符放行
|
||
default:
|
||
return false // 还有别的字母/字符 ⇒ 不是纯数字片段
|
||
}
|
||
}
|
||
return seenDigit
|
||
}
|
||
|
||
// tokenPresent 检查 lower 文本中 t 是否作为独立 token 出现(词边界,避免 "go" 命中 "google")。
|
||
func tokenPresent(lower, t string) bool {
|
||
if t == "" {
|
||
return false
|
||
}
|
||
// 切词精确匹配
|
||
for _, w := range strings.Fields(lower) {
|
||
// 剥掉包裹的引号/逗号/括号
|
||
w = strings.Trim(w, `"',()[]|;:`)
|
||
if w == t {
|
||
return true
|
||
}
|
||
}
|
||
// 空格/首尾边界
|
||
if strings.Contains(lower, " "+t+" ") || strings.HasPrefix(lower, t+" ") || strings.HasSuffix(lower, " "+t) {
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
// regexTokenExtract 轻量 token 抽取(无正则依赖,按字符切):
|
||
// 返回形如命令/token 的 ascii 串(长度 ≥2,含字母,可能带数字/_.-)。
|
||
func regexTokenExtract(content string) []string {
|
||
var out []string
|
||
var cur []rune
|
||
flush := func() {
|
||
if len(cur) >= 2 {
|
||
out = append(out, string(cur))
|
||
}
|
||
cur = nil
|
||
}
|
||
for _, r := range content {
|
||
isTok := (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') ||
|
||
(r >= '0' && r <= '9') || r == '_' || r == '-' || r == '.'
|
||
if isTok {
|
||
cur = append(cur, r)
|
||
} else {
|
||
flush()
|
||
}
|
||
}
|
||
flush()
|
||
return out
|
||
}
|
||
|
||
func dedupeSorted(in []string) []string {
|
||
seen := map[string]bool{}
|
||
out := make([]string, 0, len(in))
|
||
for _, s := range in {
|
||
if !seen[s] {
|
||
seen[s] = true
|
||
out = append(out, s)
|
||
}
|
||
}
|
||
sort.Strings(out)
|
||
return out
|
||
}
|
||
|
||
func dedupeStrings(in []string) []string {
|
||
seen := map[string]bool{}
|
||
out := make([]string, 0, len(in))
|
||
for _, s := range in {
|
||
if !seen[s] {
|
||
seen[s] = true
|
||
out = append(out, s)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
func cosineF32(a, b []float32) float64 {
|
||
if len(a) == 0 || len(b) == 0 || len(a) != len(b) {
|
||
return 0
|
||
}
|
||
var dot, na, nb float64
|
||
for i := range a {
|
||
x, y := float64(a[i]), float64(b[i])
|
||
dot += x * y
|
||
na += x * x
|
||
nb += y * y
|
||
}
|
||
if na == 0 || nb == 0 {
|
||
return 0
|
||
}
|
||
c := dot / (float64(math.Sqrt(na)) * float64(math.Sqrt(nb)))
|
||
if c > 1.0 {
|
||
c = 1.0
|
||
}
|
||
if c < -1.0 {
|
||
c = -1.0
|
||
}
|
||
return c
|
||
}
|
||
|
||
// bigramJaccard 字符 bigram Jaccard 相似度(无向量时的确定性回退)。
|
||
func bigramJaccard(a, b string) float64 {
|
||
bg := func(s string) map[string]bool {
|
||
m := make(map[string]bool)
|
||
r := []rune(s)
|
||
for i := 0; i+1 < len(r); i++ {
|
||
m[string(r[i:i+2])] = true
|
||
}
|
||
return m
|
||
}
|
||
ma, mb := bg(a), bg(b)
|
||
if len(ma) == 0 && len(mb) == 0 {
|
||
return 1.0
|
||
}
|
||
if len(ma) == 0 || len(mb) == 0 {
|
||
return 0.0
|
||
}
|
||
inter := 0
|
||
for k := range ma {
|
||
if mb[k] {
|
||
inter++
|
||
}
|
||
}
|
||
uni := len(ma) + len(mb) - inter
|
||
if uni == 0 {
|
||
return 0
|
||
}
|
||
return float64(inter) / float64(uni)
|
||
}
|
||
|
||
// contentFingerprint 取 content 稳定短指纹(FNV 前 8 hex 位)——同 size 不同内容簇去重。
|
||
func contentFingerprint(s string) string {
|
||
if s == "" {
|
||
return "empty"
|
||
}
|
||
// FNV-1a 32
|
||
h := uint32(2166136261)
|
||
for i := 0; i < len(s); i++ {
|
||
h ^= uint32(s[i])
|
||
h *= 16777619
|
||
}
|
||
return fmt.Sprintf("%08x", h)
|
||
}
|
||
|
||
func truncateForPattern(s string, n int) string {
|
||
r := []rune(s)
|
||
if len(r) <= n {
|
||
return s
|
||
}
|
||
return string(r[:n]) + "…"
|
||
}
|
||
|
||
// IsValidPatternName pattern 命名键合法性闸门(t_639c5a03,写入前最后防线)。
|
||
// 合法形态:
|
||
// - tool_combo:<valid-tool>(+<valid-tool>)* —— 每个成员过 isValidTool
|
||
// - workflow:cluster-<N>-<fingerprint>
|
||
//
|
||
// 其余("tool_combo:.md"、"tool_combo:2026+23" 等垃圾键)一律拒绝,
|
||
// UpsertPatterns 拒写 —— 即使调用方判据再退化,垃圾也进不了 graph_nodes。
|
||
func IsValidPatternName(name string) bool {
|
||
i := strings.IndexByte(name, ':')
|
||
if i <= 0 {
|
||
return false
|
||
}
|
||
body := name[i+1:]
|
||
if body == "" {
|
||
return false
|
||
}
|
||
switch name[:i] {
|
||
case "tool_combo":
|
||
for _, t := range strings.Split(body, "+") {
|
||
// 写入闸=形态 + 命令式标识双重:白名单成员或含 -/数字,普通英文词(report/log…)拒。
|
||
if !isValidTool(t) {
|
||
return false
|
||
}
|
||
if !knownTools[t] && !strings.ContainsAny(t, "-0123456789") {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
case "workflow":
|
||
return strings.HasPrefix(body, "cluster-")
|
||
}
|
||
return false
|
||
}
|