feat(distill): GA-P0-5 概念/实体提取路径接 echo 防线(防元污染闭环复发)
- 新增 distill.IsPromptEchoEntity(与 IsPromptEcho 同源读 promptEchoLiterals,清单零改动) - server.go [graph/notes] 处理器过滤候选实体,命中即剔并打 [graph/notes][ECHO-DROP] - 覆盖 3 个复活节点 n_galway/n_rothfuss/n_the_name_of_the_wind 同类输入 - 禁动 storage/rust(AC4 守住);commit 由小怡补(原 worker 未提交,共享树有覆盖风险) 卡: t_96580838
This commit is contained in:
@@ -662,6 +662,24 @@ func NewServer() http.Handler {
|
||||
mode = "direct"
|
||||
} else if cands := entityIndex.Extract(entity, 2); len(cands) > 0 {
|
||||
// 兜底:entity 是整句自然语言(调用方原样传用户提问)→ 本地把它拆成候选实体
|
||||
// 2026-10-12 GA-P0-5:概念/实体提取路径接 echo 防线 —— 审计笔记把污染词当证据
|
||||
// 列出来 → 蒸馏成 episode → 兜底拆实体时会把已删除污染节点(n_galway / n_rothfuss /
|
||||
// n_the_name_of_the_wind)重建。候选实体若只是 prompt 教学示例的回显残片
|
||||
// (逐字相等 / illustration 类 ≥4 字子串,判据与蒸馏侧 IsPromptEcho 同源)
|
||||
// ⇒ 不入图;[ECHO-DROP] 日志供验证防线在跑。
|
||||
var echoDropped int
|
||||
kept := cands[:0]
|
||||
for _, c := range cands {
|
||||
if distill.IsPromptEchoEntity(c) {
|
||||
echoDropped++
|
||||
continue
|
||||
}
|
||||
kept = append(kept, c)
|
||||
}
|
||||
if echoDropped > 0 {
|
||||
log.Printf("[graph/notes][ECHO-DROP] 剔除回显残片 %d 个(%q)→ 不参与笔记匹配/不入图", echoDropped, cands)
|
||||
cands = kept
|
||||
}
|
||||
const maxSearchEntities = 8 // 参与笔记匹配的实体上限:候选 + 少量邻居,防噪声淹没
|
||||
ents := make([]string, 0, maxSearchEntities)
|
||||
seen := make(map[string]bool, maxSearchEntities)
|
||||
|
||||
@@ -95,6 +95,51 @@ func IsPromptEcho(s string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ─── 概念/实体提取路径([graph/notes] 兜底拆实体)──────────────────────────────
|
||||
//
|
||||
// 背景(GA-P0-5):蒸馏侧 IsPromptEcho 只罩 LLM 输出的 facts/entities,图谱污染闭环
|
||||
// 还有另一条腿 —— 审计笔记把污染词当证据列出来 → 笔记被蒸馏成 episode → 调用方把整句
|
||||
// 提问原样传给 /api/v1/graph/notes → entity_index 从图谱节点名词表拆出候选实体 → 若
|
||||
// 候选只是 prompt 教学示例的回显残片,会被当真实实体写回图谱 ⇒ 已删除的污染节点
|
||||
// (n_galway / n_rothfuss / n_the_name_of_the_wind)被重建。
|
||||
//
|
||||
// 判据与 IsPromptEcho 完全同源(都读 promptEchoLiterals,清单本身零改动):逐字相等
|
||||
// (归一化后)或 illustration 类(allowSubstr=true)的 ≥4 字子串残片即算回显;占位符类
|
||||
// (allowSubstr=false)只认逐字相等。唯一差别是输入形态:图谱节点名是「n_the_name_of_the_wind」
|
||||
// 这种规整形,需先还原成可读文本(去 n_、下划线→空格、小写)再比对。
|
||||
//
|
||||
// 与 IsPromptEcho 一样**只拦「逐字相等/残片」级**,不拦普通包含 —— 例如「用户提到
|
||||
// galway」里 galway 若是真实实体,只要它不在某条示例字面量的残片里就放行;误杀面与
|
||||
// 蒸馏侧一致(那里已实测会杀 John / Patrick Rothfuss 这类单字残片)。
|
||||
func IsPromptEchoEntity(s string) bool {
|
||||
n := normalizeEchoEntity(s)
|
||||
if n == "" {
|
||||
return false
|
||||
}
|
||||
nr := utf8.RuneCountInString(n)
|
||||
for _, l := range promptEchoLiterals {
|
||||
ln := strings.ToLower(normalizeEcho(l.text))
|
||||
if ln == "" {
|
||||
continue
|
||||
}
|
||||
if n == ln {
|
||||
return true
|
||||
}
|
||||
if l.allowSubstr && nr >= 4 && strings.Contains(ln, n) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// normalizeEchoEntity 把图谱节点名归一成可与字面量比对的形态。
|
||||
func normalizeEchoEntity(s string) string {
|
||||
s = strings.TrimPrefix(s, "n_")
|
||||
s = strings.ReplaceAll(s, "_", " ")
|
||||
s = strings.ToLower(s)
|
||||
return normalizeEcho(s)
|
||||
}
|
||||
|
||||
// EchoStats 记录本次剔除量,供日志观测。
|
||||
type EchoStats struct {
|
||||
FactsDropped int
|
||||
|
||||
@@ -154,3 +154,70 @@ func TestParseDistillOutput_MixedKeepsReal(t *testing.T) {
|
||||
t.Fatalf("评分应保留: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 概念/实体提取路径(GA-P0-5)────────────────────────────────────────────
|
||||
//
|
||||
// 图谱污染闭环第二腿:审计笔记把污染词当证据列出来 → 蒸馏成 episode → 兜底拆实体时
|
||||
// 已删除的污染节点(n_galway / n_rothfuss / n_the_name_of_the_wind)被重建。
|
||||
// IsPromptEchoEntity 把图谱节点名(规整形)归一后,按与 IsPromptEcho 同源判据拦截。
|
||||
|
||||
func TestIsPromptEchoEntity_RevivedNodesDropped(t *testing.T) {
|
||||
// AC2:3 个复活节点(归一化形,含 n_ 前缀 / 下划线两种形态)经提取路径被 DROP。
|
||||
revived := []string{
|
||||
"n_galway", "galway",
|
||||
"n_rothfuss", "rothfuss",
|
||||
"n_the_name_of_the_wind", "the_name_of_the_wind",
|
||||
"The Name of the Wind", // 人类可读残片
|
||||
}
|
||||
for _, s := range revived {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Errorf("污染节点应被 ECHO-DROP:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPromptEchoEntity_ExactMatchStillWorks(t *testing.T) {
|
||||
// 逐字相等(归一化后)对实体路径同样生效 —— 与 IsPromptEcho 同源。
|
||||
yes := []string{"用户昨天买了苹果。", "用户昨天买了苹果", "Galway, Ireland", "慈善篮球赛", "独立事实1"}
|
||||
for _, s := range yes {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Errorf("应判为回显但未判:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPromptEchoEntity_RealEntitiesKept(t *testing.T) {
|
||||
// AC3 正例:真实实体绝不能被误杀(≥5 个)。
|
||||
real := []string{
|
||||
"小怡", "牧尘", "招商台账", "Redis", "织忆", "待办.md",
|
||||
"用户上周去了北京并签了合同",
|
||||
"小怡把生产缓存切到 Redis L2 并设了 7 天 TTL",
|
||||
}
|
||||
for _, s := range real {
|
||||
if IsPromptEchoEntity(s) {
|
||||
t.Errorf("真实实体被误杀:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPromptEchoEntity_PlaceholderNotSubstring(t *testing.T) {
|
||||
// 占位符类(allowSubstr=false)只认逐字相等:残片「待办」不该被「待办.md」示例杀。
|
||||
no := []string{"待办", "实体名实体", "独立"}
|
||||
for _, s := range no {
|
||||
if IsPromptEchoEntity(s) {
|
||||
t.Errorf("占位符类字面量不应做子串拦截,%q 被误杀", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 漂移护栏:3 个复活节点必须永远被拦 ──────────────────────────────────────
|
||||
// 若 prompt 或字面量清单改动导致 galway/rothfuss/the_name_of_the_wind 漏网,
|
||||
// 图谱污染闭环会静默复发 —— 这条红 = 清单过期,去核对 echo_guard.go。
|
||||
func TestIsPromptEchoEntity_DriftGuardRevivedNodes(t *testing.T) {
|
||||
mustCatch := []string{"n_galway", "n_rothfuss", "n_the_name_of_the_wind"}
|
||||
for _, s := range mustCatch {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Fatalf("复活节点 %q 已漏网(清单过期/判据被改)⇒ 污染闭环复发", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user