Files
memoryweave/go/internal/distill/density_gate.go
T

94 lines
5.6 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.
// 织忆 MemoryWeave — 蒸馏产出信息密度判据(kanban t_ab5b2461)
//
// 病(2026-10-30 全面评估 + 本卡按 Source 分组定位):memories 表 content 中位数 35 字、
// 83.3% <60 字、含可执行标记率低 —— 蒸馏产物太短,召回出来没法直接用。
// 分组实测(groups_before.json,live 42,150):llm_distill p50=36、wiki:worklog-note p50=36、
// wiki:domain-kb p50=38、wiki:session-compress p50=30 —— distill 全家族整体都碎,
// 不是某一条上游的病 ⇒ 收口在共用写入点(server.go OnDistillComplete 写库循环 +
// engine.go 提取 prompt),不逐脚本打补丁。
//
// 设计(延续 procedural_gate.go 分诊哲学 + 卡红线「禁为长度而长度」):
// - 纯确定性判据,零 LLM 调用,三项各自独立判定(正则):
// ① 可执行动作/命令/路径(反引号命令、CLI 调用、绝对/家目录路径、脚本/配置文件扩展名)
// ② 明确判据或阈值(数字+单位、比较符、阈值/上限/下限/门槛类词)
// ③ 因果(「为什么」:因为/由于/导致/根因/否则/⇒ 等因果链词)
// - 满足 ≥2 项 ⇒ 保持 Category="distilled"(知识形态)。
// - <2 项 ⇒ 降级 Category="observe":行照写、可召回、计入写入量(AC3 不饿死),
// 只是不再冒充高质量知识 —— 与 t_3636b872 同一理由:降级可逆,丢弃不可逆。
// - 不做长度硬门槛:长度是三项判据的**结果**不是目标(卡红线)。
//
// env 门控:ZHIYI_DISTILL_DENSITY=1 启用(灰度开在 systemd drop-in);
// 未设 ⇒ DensityGateEnabled()=false ⇒ 写入侧 Category 恒 "distilled"、
// prompt 与打分缓存键逐字节回到现状(AC5 可回滚)。
// 回滚:rm 42-density-gate.conf + daemon-reload + zhiyi-restart-with-lock.sh restart。
package distill
import (
"os"
"regexp"
)
// densityGateEnabled 启动时读一次(与 proceduralShadowMode 同款,改值须重启)。
var densityGateEnabled = os.Getenv("ZHIYI_DISTILL_DENSITY") == "1"
// DensityGateEnabled 写入侧(server.go)与打分缓存(score_cache.go)共用的开关读口。
func DensityGateEnabled() bool { return densityGateEnabled }
// 三项判据正则(RE2;口径写进交付,判据即文档)。
var (
// densityCmdRe ① 可执行动作/命令/路径:
// 路径前缀(绝对/家目录)、脚本与配置文件扩展名、常见 CLI + 参数、反引号内联命令。
densityCmdRe = regexp.MustCompile(
`(?:~/|/(?:home|var|etc|data|usr|opt|tmp|srv|root)/|\.(?:sh|py|go|rs|conf|json|jsonl|ya?ml|toml|db|service|sql)\b` +
`|\b(?:git|curl|wget|grep|rg|sed|awk|jq|yq|systemctl|journalctl|python3?|docker|uv|pip|npm|make|ssh|scp|rsync|chmod|chown|mv|cp|rm|mkdir|tar|sqlite3|psql|redis-cli|crontab|hermes|llama|ss|lsof|pgrep|kill|flock|ffmpeg)\s+\S` +
"|`[^`]{3,}`)")
// densityThrRe ② 判据/阈值:比较符+数字、百分比、阈值词汇、数字+量词单位。
densityThrRe = regexp.MustCompile(
`(?:[≥≤><]=?\s*\d|\d+(?:\.\d+)?\s*[%%]|阈值|上限|下限|门槛|限额|配额|\bcap\b|\blimit\b|\bcritical\b` +
`|\d+\s*(?:ms|毫秒|秒|分钟|小时|天|次|条|行|字|MB|GB|TB|端口|万|亿))`)
// densityCauseRe ③ 因果/为什么:因果连接词与根因词汇、箭头推导。
densityCauseRe = regexp.MustCompile(
`(?:因为|由于|导致|因此|所以|为了|从而|以致|以免|否则|根因|原因为|起因|源于|使得|⇒|->|=>)`)
)
// DensitySatisfyCount 数一条事实满足三项判据中的几项(0~3)。
// 纯函数:同一输入永远同一结果,可单测、零副作用。
func DensitySatisfyCount(fact string) int {
n := 0
if densityCmdRe.MatchString(fact) {
n++
}
if densityThrRe.MatchString(fact) {
n++
}
if densityCauseRe.MatchString(fact) {
n++
}
return n
}
// DensityKeep 事实是否够格保持 Category="distilled":满足 ≥2 项。
// 注意:本函数只在 DensityGateEnabled() 时被调用;关态下调用方保持 "distilled" 不动。
func DensityKeep(fact string) bool {
return DensitySatisfyCount(fact) >= 2
}
// densityPromptAddendum 灰度开启时追加到提取 prompt 的信息量要求。
// 只在 DensityGateEnabled() 时拼接 —— 关态 prompt 逐字节不变(AC5),
// 旧 prompt 原文保留在 engine.go 的 instr/outFmt 常量里,一字未动。
const densityPromptAddendum = `
🔴 信息量要求(灰度生效中):
- 同一事件/操作的所有关联要素(做什么、在哪、什么时候/多频繁、为什么、到什么程度/阈值)**合并成一条完整句子**,不要拆成多条各含一个要素的短句。
- 目标:每条 FACT 是一句自洽、自足的陈述,读的人(或后续召回)不需要再拼其它条就能直接用。
- 合并示例:原文碎片「巡检脚本是 disk-check.sh」「周期每 6 小时」「原因是磁盘写满会拖垮服务」
→ 输出一条:「磁盘巡检每 6 小时执行一次 disk-check.sh,因为磁盘写满会拖垮服务。」
而不是三条各含一个要素的短句。(示例只教写法,不要抄示例内容)
- 判断"要素是否齐全":一条 FACT 若只有"是什么"而缺"怎么用/在哪/为何/程度",且原文有这些信息,就必须并进来。
- 纯元信息行(如「该条记录日期为…」「文件保存在…」这类只有溯源没有知识载荷的)不要输出为事实。
- 禁止为凑长度堆修饰词、重复同一事实、或把无关信息硬塞进一条:原文只有短句就照实提取,
长是信息完整的结果,不是目标。宁可"完整但不长",也不要"长但不自洽"。
`