这批是 10-26~27 已在生产二进制(zhiyid-protectrails-20261027)里运行、 但一直留在工作树未提交的改动。收口原因:三件改造要开分支,工作树脏 = 分支基准与回滚点都不存在。 主要内容: - api/routes/contract.go + trace.go:召回响应契约字段(trace_id/status/scope/ continuation/channel)与 /api/v1/trace 回溯端点 - governance/entity_quality.go:实体质量闸 - storage/trace.go + recall.go/recall_write_buffer.go:召回写入缓冲与轨迹 - 测试:contract/recall_contract_fields/recall_lifecycle/stale_lifecycle/ entity_quality/trace 同时修一个 flaky:recall_lifecycle_test 原本硬编码「今天 11:00 已过」, 凌晨跑就会把「已失效」fixture 当成仍有效。改成相对时间(过期=now-1h), 生命周期判据与运行时刻无关。修完 go test ./internal/api/routes ./internal/storage 全绿。
143 lines
6.0 KiB
Go
143 lines
6.0 KiB
Go
// 织忆 MemoryWeave — 图谱实体质量闸(P0-4,2026-10-26)
|
||
//
|
||
// 背景(CS249r 评审实测):graph_expanded 抽样 15 条里 8 条是 ≤4 字垃圾实体
|
||
// ("条""里存着""开发"),既污染上下文又抬延迟。根因是实体抽取「任意 2-8 字中文串都当实体」,
|
||
// 且扩展前不检查节点在图里的度数 —— 孤边节点(度=1)被当成语义邻居注入。
|
||
//
|
||
// 两道闸(都在**导航前**执行,省算力):
|
||
// ① isJunkEntity:长度 <2 rune / 纯虚词量词代词 / 常见填充短语 / 纯 ASCII 停用词 ⇒ 丢弃;
|
||
// ② 度数闸:起点实体在图中的边数 <2 ⇒ 不导航、不注入(孤边不构成「关联」)。
|
||
//
|
||
// 观测:两道闸的拒绝计数写入 debug 日志(ZHIYI_GRAPH_EXPAND_DEBUG=1)与 recall trace
|
||
// (graph_expand 阶段 note),保证「下次坏了不沉默」。
|
||
package governance
|
||
|
||
import (
|
||
"strings"
|
||
"unicode/utf8"
|
||
)
|
||
|
||
// junkEntitySet 高置信垃圾实体词表:中文虚词/代词/量词/时间副词/填充短语 + 英文停用词。
|
||
// 原则:只收**几乎不可能**是真实实体名的词;宁可漏筛(由度数闸兜底),不可误杀专名。
|
||
var junkEntitySet = map[string]struct{}{}
|
||
|
||
func init() {
|
||
words := []string{
|
||
// 虚词 / 助词 / 连词
|
||
"的", "了", "着", "过", "地", "得", "所", "以", "于", "与", "及", "或", "而", "且", "则", "即",
|
||
"因为", "所以", "但是", "而且", "然后", "如果", "虽然", "不过", "而是", "就是", "只是", "还是",
|
||
"并且", "以及", "或者", "否则", "因此", "于是", "关于", "对于", "由于", "根据", "按照", "通过",
|
||
// 代词 / 指示词
|
||
"我", "你", "他", "她", "它", "我们", "你们", "他们", "她们", "它们", "自己", "别人", "大家",
|
||
"这个", "那个", "这些", "那些", "这样", "那样", "这种", "那种", "这里", "那里", "里面", "上面",
|
||
"下面", "前面", "后面", "其中", "其他", "其它", "什么", "怎么", "怎样", "为什么", "哪个", "哪些",
|
||
// 数量 / 量词 / 程度
|
||
"一个", "一条", "一次", "一些", "一种", "一件", "一点", "一下", "一直", "一样", "一起",
|
||
"两个", "三个", "几个", "多个", "很多", "更多", "多条", "几条", "每条", "各种", "某种", "全部", "所有", "每个", "各种", "部分",
|
||
"一条条", "一条里", "条", "个", "次", "些", "种", "只", "件", "篇", "位", "名", "层",
|
||
"非常", "比较", "特别", "十分", "更加", "最为", "可能", "应该", "必须", "可以", "能够", "需要",
|
||
// 常见谓词 / 填充动词短语(低信息量,几乎不可能是实体)
|
||
"里存着", "存着", "里存在", "有着", "存在", "进行", "使用", "支持", "包括", "包含", "属于",
|
||
"作为", "成为", "变成", "做到", "做好", "来看", "来说", "来讲", "看到", "看看", "发现",
|
||
"出现", "提供", "实现", "完成", "开始", "结束", "继续", "返回", "调用", "处理", "执行",
|
||
"开发", "编写", "添加", "修改", "删除", "更新", "检查", "确认", "验证", "测试", "部署",
|
||
// 时间 / 方位
|
||
"现在", "今天", "昨天", "明天", "之前", "之后", "以前", "以后", "当时", "当前", "目前",
|
||
"最近", "刚才", "马上", "已经", "正在", "曾经", "从来", "后来", "最终", "最后", "首先",
|
||
"时候", "时间", "期间", "上面", "下面", "左边", "右边", "中间", "附近", "里面", "外面",
|
||
// 英文停用词(小写比较)
|
||
"the", "and", "for", "are", "but", "not", "you", "all", "any", "can", "had", "her", "was",
|
||
"one", "our", "out", "day", "get", "has", "him", "his", "how", "its", "new", "now", "old",
|
||
"see", "two", "way", "who", "boy", "did", "she", "use", "her", "way", "may", "say",
|
||
"this", "that", "with", "from", "have", "will", "your", "they", "than", "then", "them",
|
||
"into", "more", "some", "such", "only", "over", "also", "just", "like", "make", "made",
|
||
}
|
||
for _, w := range words {
|
||
junkEntitySet[w] = struct{}{}
|
||
}
|
||
}
|
||
|
||
// IsJunkEntity 判定实体是否应被图谱扩展丢弃(P0-4 第①道闸)。
|
||
func IsJunkEntity(e string) bool {
|
||
e = strings.TrimSpace(e)
|
||
if e == "" {
|
||
return true
|
||
}
|
||
if utf8.RuneCountInString(e) < 2 {
|
||
return true // 单字("条""里")不构成实体
|
||
}
|
||
// 归一化:去首尾标点/引号,压缩空白
|
||
e = strings.Trim(e, " \t\r\n\"'`,。;:!?、()()【】[]{}<>《》—-·.,;:!?")
|
||
if e == "" {
|
||
return true
|
||
}
|
||
if utf8.RuneCountInString(e) < 2 {
|
||
return true
|
||
}
|
||
lower := strings.ToLower(e)
|
||
if _, bad := junkEntitySet[lower]; bad {
|
||
return true
|
||
}
|
||
if _, bad := junkEntitySet[e]; bad {
|
||
return true
|
||
}
|
||
// 全由虚词/量词字组成(如「里存着」之外的「的了着」「个条」组合)
|
||
if allFunctionChars(e) {
|
||
return true
|
||
}
|
||
// 纯标点/符号(无任何字母/数字/汉字)
|
||
if !hasSemanticChar(e) {
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
// functionChars 单字虚词/量词集合:整串都由这些字组成 ⇒ 无信息量。
|
||
var functionChars = map[rune]struct{}{}
|
||
|
||
func init() {
|
||
for _, r := range "的了着过地得所以于与及或而且则即是在有和与就都也还又很更最可会能要没对为从到把被让给吗呢吧啊呀哦嗯这那我你他她它们个条次些种只件篇位名层里上下中前后内外" {
|
||
functionChars[r] = struct{}{}
|
||
}
|
||
}
|
||
|
||
func allFunctionChars(s string) bool {
|
||
for _, r := range s {
|
||
if _, ok := functionChars[r]; !ok {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
func hasSemanticChar(s string) bool {
|
||
for _, r := range s {
|
||
switch {
|
||
case r >= 0x4E00 && r <= 0x9FFF: // 汉字
|
||
return true
|
||
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z':
|
||
return true
|
||
case r >= '0' && r <= '9':
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// FilterEntitiesForGraph 批量过滤垃圾实体,返回保留集与被拒数(观测用)。
|
||
func FilterEntitiesForGraph(entities []string) ([]string, int) {
|
||
if len(entities) == 0 {
|
||
return entities, 0
|
||
}
|
||
out := make([]string, 0, len(entities))
|
||
rejected := 0
|
||
for _, e := range entities {
|
||
if IsJunkEntity(e) {
|
||
rejected++
|
||
continue
|
||
}
|
||
out = append(out, e)
|
||
}
|
||
return out, rejected
|
||
}
|