Files
memoryweave/go/internal/distill/rules.go
T
小唯 07599f0a22 feat(distill): 硬规则拒绝可观测 rejected_by_rule 真计数 + lane=drop 死路定性 (t_a513b870)
- AC1: HardRulesPass 拆出 HardRulesPassWithReason,拒绝分支登记并发安全计数
  (empty/too_short/chitchat/qa/sys_template/low_density/too_long 7 桶)
  + 至少一行 [GATE][REJECT] rule=<x> 日志;系统样板移入硬规则层,drop 只在硬规则层发生。
- AC2: rejected_by_rule 挂到已有 /api/v1/metrics(不开新端口),
  字段 rejected_by_rule / rejected_by_rule_<bucket> / entered_queue / gate_attempts;
  handler 主体抽成 routes.MetricsJSON 便于单测。
- AC3: 单测覆盖全部分桶 + 并发 200 + httptest 真实端点读回 JSON。
- AC4(a): 承认 lane=drop 不可达(硬规则更严先拦),保留分支加防御注释,闸门只走 keep/observe。
- 发现 chitchat 规则同样不可达(pattern 全 <10 字,恒被 too_short 先短路)→ 测试钉死。
- 未打二进制/未改 drop-in/未重启 zhiyid(二进制由小怡统一构建部署)。
2026-11-10 04:13:09 +08:00

153 lines
5.2 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 — 硬规则过滤
// 蒸馏前筛选:跳过闲聊、纯知识问答、低密度内容
//
// t_a513b870:HardRulesPass 增加带原因的变体 HardRulesPassWithReason,并统一在此
// 登记拒绝计数(RecordHardReject)——使「闸门拦掉多少」可观测,不再是黑洞。
package distill
import (
"strings"
"unicode/utf8"
"github.com/xiaoxue/memoryweave/internal/textfilter"
)
// HardRulesPass 硬规则过滤 → true = 通过,可进入蒸馏队列。
// 薄封装:委托 HardRulesPassWithReason,并登记计数。禁止新增逻辑在此(保持语义单点)。
func HardRulesPass(content string) bool {
pass, _ := HardRulesPassWithReason(content)
return pass
}
// HardRulesPassWithReason 硬规则过滤,返回 (是否通过, 拒绝原因分桶)。
// 拒绝时登记计数 + 打 [GATE][REJECT] 日志(AC1)。规则顺序即判定优先级:
// 空 → 系统样板 → 过短(含人侧短事实豁免)→ 闲聊 → 知识问答 → 密度 → 超长。
func HardRulesPassWithReason(content string) (bool, string) {
// 规则1: 无用户消息(纯系统消息) → 跳过
if strings.TrimSpace(content) == "" {
reject("empty", content)
return false, "empty"
}
// 规则1.5: 系统样板(t_a513b870 把原属 ClassifyAdmission 的 drop 提前到硬规则层,
// 使 AC4(a)「drop 只在硬规则层发生」成立,并让「系统样板」成为可见拒绝分桶)
if textfilter.IsSystemTemplate(content) {
reject("sys_template", content)
return false, "sys_template"
}
// 规则2: 太短的消息 (< 10字符) → 跳过
// 例外:人侧结构化短事实(画像/心迹/牵挂/偏好/习惯/目标)天然短但 100% 是用户事实,
// 不因字数被丢弃——否则"牧尘不在招商局工作"(9 字) 这类关键事实永远到不了 memories。
if utf8.RuneCountInString(content) < 10 {
if isSoulfulShortFact(content) {
pass("too_short", content) // 豁免但记录一次 pass
return true, ""
}
reject("too_short", content)
return false, "too_short"
}
// 规则3: 纯闲聊/问候 → 跳过
chitchatPatterns := []string{
"你好", "在吗", "在?", "好的", "谢谢", "不客气",
"ok", "OK", "嗯", "哦", "知道了",
}
lower := strings.ToLower(strings.TrimSpace(content))
for _, pattern := range chitchatPatterns {
if strings.EqualFold(lower, pattern) || lower == pattern {
reject("chitchat", content)
return false, "chitchat"
}
}
// 规则4: 纯知识问答(如 "什么是...") → 跳过
qaPrefixes := []string{"什么是", "怎么定义", "解释一下", "介绍一下"}
for _, prefix := range qaPrefixes {
if strings.HasPrefix(content, prefix) {
reject("qa", content)
return false, "qa"
}
}
// 规则5: 内容密度检查 (信息密度 < 0.3 → 跳过)
density := contentDensity(content)
if density < 0.3 {
reject("low_density", content)
return false, "low_density"
}
// 规则6: 长度适中(太长可能是粘贴/日志,跳过蒸馏,交给 episode 保留)
if utf8.RuneCountInString(content) > 5000 {
reject("too_long", content)
return false, "too_long"
}
RecordGatePass()
return true, ""
}
// reject 拒绝分支的统一登记点(计数 + 日志)。
func reject(rule string, content string) {
RecordHardReject(rule, content)
}
// pass 通过分支的计数登记(进入队列前由 Enqueue append 时再 RecordEnteredQueue)。
func pass(rule string, content string) {
// rule 仅用于豁免场景的调试定位(如 too_short 豁免),计数器不需分桶记录 pass,
// 只记总评估次数。此处保留参数签名以明确「这条走了豁免通过」。
RecordGatePass()
}
// contentDensity 内容信息密度 = 有意义字符 / 总字符
// 过滤空白、标点、重复字符
func contentDensity(text string) float64 {
if len(text) == 0 {
return 0
}
meaningful := 0
spaces := 0
prev := rune(0)
repeats := 0
for _, r := range text {
if r == ' ' || r == '\t' || r == '\n' || r == '\r' {
spaces++
} else if r == ',' || r == '。' || r == '!' || r == '?' ||
r == '.' || r == '!' || r == '?' || r == ';' || r == ';' {
// 标点,不计入 meaningful
} else if r == prev {
repeats++
} else {
meaningful++
}
prev = r
}
// 有空格和重复扣分
total := len(text)
adjusted := meaningful - spaces/2 - repeats/3
if adjusted < 0 {
adjusted = 0
}
return float64(adjusted) / float64(total)
}
// ─── 深度整合触发条件 ────────────────────────────────────
// ShouldDeepConsolidate 深度整合触发条件
// 条件: (新增蒸馏 > 50 且 距上次 > 24h) 或 (距上次 > 48h)
func ShouldDeepConsolidate(distilledSinceLast int, hoursSinceLast float64) bool {
return (distilledSinceLast > 50 && hoursSinceLast > 24) || hoursSinceLast > 48
}
// ─── 批量蒸馏触发条件 ────────────────────────────────────
// ShouldBatchDistill 批量蒸馏触发条件
// 条件: 队列 >= 10 或 距上次 > 5 分钟
func ShouldBatchDistill(queueLen int, secondsSinceLast float64) bool {
return queueLen >= 10 || secondsSinceLast > 300
}