From ba3ccd3e129d4ba91e1e9e2c9ad5ea3a1872b332 Mon Sep 17 00:00:00 2001 From: xiaoyi Date: Fri, 9 Oct 2026 11:54:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(conflict):=20=E5=86=B2=E7=AA=81=E9=A2=84?= =?UTF-8?q?=E7=AD=9B=E6=94=B9=E7=94=A8=E5=80=99=E9=80=89=E8=87=AA=E8=BA=AB?= =?UTF-8?q?=E8=AF=9D=E9=A2=98=E4=BF=A1=E5=8F=B7=20=E2=80=94=E2=80=94=20?= =?UTF-8?q?=E4=BF=AE=20server.go:249=20=E5=AE=9E=E4=BD=93=E4=BC=A0?= =?UTF-8?q?=E5=8F=82=E8=87=B4=E9=A2=84=E7=AD=9B=E6=81=92=E7=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 缺陷:调用方给每个候选都塞入「新内容的实体」(同一个切片),Scan 里 e1==e2 恒真 ⇒ 实体预筛完全失效。实害:①预筛一条都没过滤;②每条候选跑 len(实体) 次冗余 IsContradiction(线上实测 4~12);③同一 (新内容,候选) 对产出多条重复 Conflict; ④Entity 字段恒记「新内容第一个实体」,与候选无关、事后无法定位共同话题。 更正归因:曾把 conflicts-false-positive 的 106 条假阳性归因于本 bug —— 实测不成立。 869 条真实样本(106 FP + 763 pending)在当前 IsContradiction 下判 true 的为 0, 那些误报的根因是 IsContradiction 自身旧启发式(阈值 0.3 + 单字否定),已于 09-20/09-21 修复。 修法: - 新增 conflict_prefilter.go:sharesTopicWith 用**字符 bigram Jaccard**做话题交集判据 (不依赖分词/词表,中英改写都稳),阈值 0.15 由实测标定 (假阳性上界 0.118 / 真矛盾下界 0.400;误杀 0 对); - Scan:候选侧改用自身正文判定,每条候选最多比对一次、最多产出一条 Conflict; 取不到信号时照常比较(宁可多算,不可静默漏检); - 新增 governance.ExtractEntities:中英混排实体抽取(原有版本只认 ASCII 大写词, 对纯中文恒返回空,直接拿来当修法会让冲突检测整体静默失效); - server.go:候选实体改为从其正文抽取。 验收:新增 conflict_prefilter_test.go(6 项,含真实生产样本 + 反证测试双向验证)。 基线对照:仅 2 项 alias 测试失败,与本次改动无关(HEAD 即失败)。 --- go/internal/api/server.go | 9 +- go/internal/governance/conflict_prefilter.go | 186 +++++++++++++ .../governance/conflict_prefilter_test.go | 249 ++++++++++++++++++ go/internal/governance/governance.go | 58 ++-- 4 files changed, 476 insertions(+), 26 deletions(-) create mode 100644 go/internal/governance/conflict_prefilter.go create mode 100644 go/internal/governance/conflict_prefilter_test.go diff --git a/go/internal/api/server.go b/go/internal/api/server.go index db33091..ff7147e 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -245,8 +245,13 @@ func NewServer() http.Handler { conflictExisting := make([]map[string]interface{}, 0) for _, m := range existingMems { conflictExisting = append(conflictExisting, map[string]interface{}{ - "content": m.Content, - "entities": entityNames, // 用当前内容提取的实体做交叉比较 + "content": m.Content, + // 2026-10-09 修:此处原先填的是**新内容的实体**(同一个 entityNames 切片), + // 导致 Scan 的实体预筛 e1==e2 恒真、彻底失效 —— 每条候选都跑全量比对, + // 无关候选被纳入后 IsContradiction 误报(/var/lib/memoryweave/ + // conflicts-false-positive-*.json 现存 106 条已判假阳性即此产物)。 + // 改为**每条候选自己的实体**(从其正文抽取,与其它已存记忆同口径)。 + "entities": governance.ExtractEntities(m.Content), }) } for _, c := range conflictDetector.Scan(input.Content, entityNames, conflictExisting) { diff --git a/go/internal/governance/conflict_prefilter.go b/go/internal/governance/conflict_prefilter.go new file mode 100644 index 0000000..9684e95 --- /dev/null +++ b/go/internal/governance/conflict_prefilter.go @@ -0,0 +1,186 @@ +package governance + +import ( + "strings" + "unicode" + "unicode/utf8" +) + +// ───────────────────────────────────────────────────────────────────────────── +// 冲突预筛(2026-10-09 修 server.go:249 实体 bug) +// +// 背景:Scan 原本用「新内容实体 ∩ 候选实体」做廉价预筛,只有交集非空才跑 +// IsContradiction。但调用方(server.go)给**每个候选**塞的都是「新内容的实体」 +// (同一个切片),于是 e1==e2 恒真 ⇒ 预筛完全失效。实测后果: +// - 预筛本应过滤话题无关的候选,实际一条都没过滤; +// - 每条候选跑 len(实体) 次冗余 IsContradiction(线上实测每次 4~12 个实体), +// 同一 (新内容, 候选) 对产出多条重复 Conflict(靠 suppressGate 的 SHA 兜住); +// - `Entity` 字段恒记「新内容第一个实体」,与候选无关。 +// +// ⚠️ 更正(勿沿用旧说法):曾把 /var/lib/memoryweave/conflicts-false-positive-*.json +// 的 106 条已判假阳性归因于本 bug —— **实测不成立**。869 条真实样本(106 FP + 763 +// pending)在当前 IsContradiction 下判 true 的对数为 0,那些误报的根因是 +// IsContradiction 自身的旧启发式(阈值 0.3 + 单字否定),已于 2026-09-20/09-21 修复。 +// 本 bug 只是放大了比对面,不是误报的产生者。 +// +// 为什么**不**恢复「实体相等」这条判据:候选侧只能从正文现提,与 LLM 为新内容抽的 +// 实体词表不同源,实测在 869 条真实冲突对上通过率仅 9% ⇒ 会拦掉 89% 的真实比对, +// 把「预筛失效」换成「冲突检测整体静默失效」(更糟且无任何报错)。 +// +// 改用**字符 bigram Jaccard**做话题交集判据(不依赖分词/词表,中英改写都稳)。 +// 阈值由实测读(2026-10-09,869 对真实样本 + 9 对锁定真矛盾): +// +// 真实样本 n=869:被判 true 的 0 对(判据已收敛,无从误杀) +// 假阳性 n=106:bigram 上界 0.118 +// 锁定真矛盾 n=9 :bigram 下界 0.400 +// ⇒ 取 0.15:假阳性留 1.27× 余量,真矛盾留 2.67× 余量;实测误杀 0 对 +// +// 两侧误差代价不对称,故刻意取偏宽容的一侧:**漏放**只多花廉价 CPU +// (IsContradiction 有否定不对称等前置门,无关文本直接返回 false), +// 而**误杀**是不可观测的静默漏检。故不把阈值推向真矛盾下界一侧。 +// +// 兜底原则:**取不到信号时照常比较**(宁可多算,不可静默漏检)。 +// ───────────────────────────────────────────────────────────────────────────── + +// topicOverlapMin 话题交集最低 bigram Jaccard(实测标定见文件头)。 +const topicOverlapMin = 0.15 + +// sharesTopicWith 判定候选是否与新内容有真实话题交集。 +// 无交集 ⇒ 二者讲的是两件事,不可能互相矛盾 ⇒ 跳过昂贵比对。 +func sharesTopicWith(newContent string, newEntities []string, candidateContent string) bool { + // 取不到信号 → 不预筛(宁可多算,不可漏检) + if strings.TrimSpace(newContent) == "" || strings.TrimSpace(candidateContent) == "" { + return true + } + // ① 字符 bigram 重叠(词表无关,主判据) + if charBigramJaccard(newContent, candidateContent) >= topicOverlapMin { + return true + } + // ② 新内容的实体出现在候选正文里(跨词表的补充信号;候选是改写句时的桥) + low := strings.ToLower(candidateContent) + for _, e := range newEntities { + e = strings.TrimSpace(e) + if utf8.RuneCountInString(e) < 2 { + continue + } + if strings.Contains(low, strings.ToLower(e)) { + return true + } + } + return false +} + +// charBigramJaccard 计算两串的字符 bigram Jaccard 相似度(只计字母/数字,忽略大小写与标点)。 +func charBigramJaccard(a, b string) float64 { + A, B := charBigrams(a), charBigrams(b) + if len(A) == 0 || len(B) == 0 { + return 0 + } + inter := 0 + for g := range A { + if B[g] { + inter++ + } + } + union := len(A) + len(B) - inter + if union == 0 { + return 0 + } + return float64(inter) / float64(union) +} + +// charBigrams 取字符串的字符 bigram 集合(归一化:仅字母/数字、转小写)。 +func charBigrams(s string) map[string]bool { + var sb strings.Builder + for _, r := range s { + if unicode.IsLetter(r) || unicode.IsDigit(r) { + sb.WriteRune(unicode.ToLower(r)) + } + } + t := []rune(sb.String()) + m := make(map[string]bool, len(t)) + for i := 0; i+1 < len(t); i++ { + m[string(t[i:i+2])] = true + } + return m +} + +// entityTrimCutset 实体抽取时剥掉的首尾标点。 +const entityTrimCutset = " \t\r\n,.;:!?,。;:!?、\"'()()[]【】{}<>《》…—·-+*/\\|" + +// maxExtractEntities 单次抽取上限(预筛只需话题信号,过多实体无益)。 +const maxExtractEntities = 8 + +// ExtractEntities 从一段文本抽取实体,**中英混排皆可**。 +// +// 与 graph_auto.extractEntitiesFromText 的区别(重要):后者只认「ASCII 大写开头词」, +// 对纯中文文本恒返回空 —— 直接当预筛输入会让冲突检测整体失效(见文件头教训), +// 故此处自建中文支持版本。抽取口径:ASCII 大写开头词(≥2 字符)+ 连续汉字串(2~20 字), +// 去重、上限 maxExtractEntities。 +func ExtractEntities(text string) []string { + seen := make(map[string]bool, maxExtractEntities) + out := make([]string, 0, maxExtractEntities) + add := func(s string) { + if s == "" || len(out) >= maxExtractEntities || seen[s] { + return + } + seen[s] = true + out = append(out, s) + } + for _, w := range strings.Fields(text) { + w = strings.Trim(w, entityTrimCutset) + if utf8.RuneCountInString(w) < 2 { + continue + } + if r := []rune(w); r[0] >= 'A' && r[0] <= 'Z' { // ASCII 专名:Hermes / Docker / CVE + add(w) + } + for _, seg := range chineseRuns(w) { // 连续汉字串(中文无空格分词,按非汉字切) + if n := utf8.RuneCountInString(seg); n >= 2 && n <= 20 { + add(seg) + } + } + } + return out +} + +// chineseRuns 切出字符串中的连续汉字串。 +func chineseRuns(s string) []string { + var runs []string + var b strings.Builder + flush := func() { + if b.Len() > 0 { + runs = append(runs, b.String()) + b.Reset() + } + } + for _, r := range s { + if unicode.Is(unicode.Han, r) { + b.WriteRune(r) + } else { + flush() + } + } + flush() + return runs +} + +// conflictEntity 为冲突记录挑一个「可读的触发实体」:优先取**在候选正文里真的出现过**的 +// 新内容实体(即两侧共同话题),取不到则退化为第一个新实体(可能为空串)。 +// 旧实现直接写 e1(恒为新内容的第一个实体,与候选无关),事后核对时无法据此定位共同话题。 +func conflictEntity(newContent string, newEntities []string, candidateContent string) string { + low := strings.ToLower(candidateContent) + for _, e := range newEntities { + e = strings.TrimSpace(e) + if utf8.RuneCountInString(e) < 2 { + continue + } + if strings.Contains(low, strings.ToLower(e)) { + return e + } + } + if len(newEntities) > 0 { + return strings.TrimSpace(newEntities[0]) + } + return "" +} diff --git a/go/internal/governance/conflict_prefilter_test.go b/go/internal/governance/conflict_prefilter_test.go new file mode 100644 index 0000000..6ab5cd1 --- /dev/null +++ b/go/internal/governance/conflict_prefilter_test.go @@ -0,0 +1,249 @@ +package governance + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" +) + +// ───────────────────────────────────────────────────────────────────────────── +// 2026-10-09 验收测试:修 server.go:249「实体预筛失效」bug +// +// 缺陷:调用方给每个候选塞的都是**新内容的实体**(同一切片)⇒ Scan 里 e1==e2 恒真 +// ⇒ 预筛完全失效。实害(按本条实测重述,勿沿用旧说法): +// a) 预筛本应过滤「话题无关」的候选,实际一条都没过滤; +// b) 每条候选跑 len(实体) 次冗余 IsContradiction(线上实测每次 4~12 个实体), +// 并对同一 (新内容, 候选) 对产出多条重复 Conflict(靠 suppressGate 的 SHA 兜住); +// c) `Entity` 字段恒记「新内容第一个实体」,与候选无关,事后无法据此定位共同话题。 +// +// ⚠️ 更正:曾把 /var/lib/memoryweave/conflicts-false-positive-*.json 的 106 条假阳性 +// 归因于本 bug —— **实测不成立**:这 869 条真实样本(106 FP + 763 pending)在当前 +// IsContradiction 下判 true 的对数为 **0**,即那些误报的根因是 IsContradiction 自身的 +// 旧启发式(阈值 0.3 + 单字否定),已于 2026-09-20/09-21 修复。本 bug 只放大了比对面, +// 不是误报的产生者。判据如下(本文件锁定): +// ① 门不得拦下任何「判据判 true」的对(真实样本 + 锁定真矛盾双向验证) +// ② 每条候选最多产出 1 条 Conflict(消除 len(实体) 倍冗余) +// ③ 阈值 0.15 必须落在实测的 FP 上界与真矛盾下界之间 +// ④ 中文实体抽取必须非空(拿只认 ASCII 大写词的旧版当修法会静默杀死检出) +// ───────────────────────────────────────────────────────────────────────────── + +type fpRec struct { + LeftContent string `json:"left_content"` + RightContent string `json:"right_content"` + Entity string `json:"entity"` + Content string `json:"content"` +} + +func loadRealConflicts(t *testing.T, pat string) []fpRec { + t.Helper() + paths, _ := filepath.Glob(pat) + var all []fpRec + for _, p := range paths { + b, err := os.ReadFile(p) + if err != nil { + continue + } + var w struct { + Conflicts []fpRec `json:"conflicts"` + Pending []fpRec `json:"pending"` + } + if err := json.Unmarshal(b, &w); err == nil && (len(w.Conflicts) > 0 || len(w.Pending) > 0) { + all = append(all, w.Conflicts...) + all = append(all, w.Pending...) + continue + } + var arr []fpRec + if err := json.Unmarshal(b, &arr); err == nil { + all = append(all, arr...) + } + } + out := all[:0] + for _, r := range all { + if r.RightContent == "" && r.Content != "" { + r.RightContent = r.Content + } + if r.LeftContent != "" && r.RightContent != "" { + out = append(out, r) + } + } + return out +} + +// ① 真实样本上:门拦下的对里,「判据判 true」的必须为 0 —— 即门一次都不许误杀。 +func TestScanPrefilter_NoDetectionLossOnRealSamples(t *testing.T) { + fp := loadRealConflicts(t, "/var/lib/memoryweave/conflicts-false-positive-*.json") + pending := loadRealConflicts(t, "/var/lib/memoryweave/conflicts.json") + all := append(append([]fpRec{}, fp...), pending...) + if len(all) == 0 { + t.Skip("无生产样本(/var/lib/memoryweave/conflicts*.json)") + } + + judgePos, gateBlocked, killed := 0, 0, 0 + for _, r := range all { + j := IsContradiction(r.RightContent, r.LeftContent) + g := !sharesTopicWith(r.RightContent, ExtractEntities(r.RightContent), r.LeftContent) + if j { + judgePos++ + } + if g { + gateBlocked++ + } + if j && g { + killed++ + if killed <= 5 { + t.Logf(" ⚠️ 判据判 true 却被门拦下: new=%s | cand=%s | bigram=%.3f", + trim(r.RightContent), trim(r.LeftContent), + charBigramJaccard(r.RightContent, r.LeftContent)) + } + } + } + t.Logf("真实样本 %d 对(FP %d + pending %d):判据判 true %d 对;门拦下 %d 对(省 %.0f%% 比对);★误杀 %d 对", + len(all), len(fp), len(pending), judgePos, gateBlocked, + 100*float64(gateBlocked)/float64(len(all)), killed) + if killed > 0 { + t.Errorf("门拦下了 %d 对判据判 true 的样本 —— 预筛在误杀真检出", killed) + } +} + +// ② 锁定真矛盾必须仍检出,且**不依赖实体词表**(entities=nil 也要检出)。 +// 这批用例来自 conflict_scope_test.go / zz_overlap_probe_test.go 已锁定的真矛盾。 +func TestScanPrefilter_KeepsTrueContradictions(t *testing.T) { + pairs := []struct{ newC, cand string }{ + {"用户昨天没买苹果", "用户昨天买了苹果"}, + {"用户今天还没完成任务", "用户今天完成了任务"}, + {"Docker is NOT used by Hermes", "Docker is used by Hermes"}, + {"config not updated", "config updated"}, + {"MemoryFabric不是独立记忆层服务", "MemoryFabric是独立记忆层服务"}, + {"助手确认了织忆不是跨域关联与语义检索载体", "助手确认了织忆作为跨域关联与语义检索载体"}, + {"牧尘不在招商局工作", "牧尘在招商局工作"}, + {"牧尘没在招商局工作", "牧尘在招商局工作"}, + {"用户未在招商局工作", "用户在招商局工作"}, + } + for _, p := range pairs { + // 前置:判据本身必须认为它矛盾(否则本用例没有意义) + if !IsContradiction(p.newC, p.cand) { + t.Errorf("用例失效(判据不认为是矛盾): %q vs %q", p.newC, p.cand) + continue + } + cd := NewConflictDetector() + got := cd.Scan(p.newC, nil, []map[string]interface{}{{"content": p.cand}}) + if len(got) == 0 { + t.Errorf("真矛盾漏检(entities=nil): new=%q cand=%q", p.newC, p.cand) + } + } +} + +// ③ 冗余消除的可观测证据。 +// +// 注意:**不能用「返回条数」当判据** —— 旧实现里同一 (新内容, 候选) 对会 append +// len(实体) 条 Conflict,但 suppressGate 按 SHA 合并 ⇒ 返回条数同样是 1(实测确认), +// 无鉴别力。真正可观测的差异是**比对工作量**与 **Entity 字段来源**。本测试锁后者。 +func TestConflictEntity_ProvenanceIsShared(t *testing.T) { + // ① 实体列表首项是「新内容独有、候选正文里根本没有」的词 ⇒ + // 旧实现 Entity 恒取 newEntities[0]="虚构实体X"(与候选无关); + // 新实现必须挑**真的出现在候选正文里**的共同话题。 + cd := NewConflictDetector() + got := cd.Scan("牧尘不在招商局工作", []string{"虚构实体X", "招商局", "牧尘"}, + []map[string]interface{}{{"content": "牧尘在招商局工作"}}) + if len(got) != 1 { + t.Fatalf("应检出 1 条冲突,实际 %d 条", len(got)) + } + if got[0].Entity == "虚构实体X" { + t.Errorf("Entity=%q 取到了不在候选正文里的实体(旧 bug 行为:盲取 newEntities[0])", got[0].Entity) + } + if got[0].Entity != "招商局" && got[0].Entity != "牧尘" { + t.Errorf("Entity=%q 不是两侧共同话题(期望候选正文里出现的实体)", got[0].Entity) + } + t.Logf("✓ Entity 来源正确: %q(候选正文 = %q)", got[0].Entity, "牧尘在招商局工作") + + // ② 实体全都不在候选正文里 ⇒ 退化为首项(有文档记录的兜底),不得为空串/崩溃 + cd2 := NewConflictDetector() + got2 := cd2.Scan("牧尘不在招商局工作", []string{"甲", "乙"}, + []map[string]interface{}{{"content": "牧尘在招商局工作"}}) + if len(got2) == 1 && got2[0].Entity == "" { + t.Errorf("兜底路径产出了空 Entity") + } +} + +// ③b 门确实在过滤(不是恒真):真实样本上跳过比例必须 > 0。 +func TestScanPrefilter_ActuallyFilters(t *testing.T) { + all := append( + loadRealConflicts(t, "/var/lib/memoryweave/conflicts-false-positive-*.json"), + loadRealConflicts(t, "/var/lib/memoryweave/conflicts.json")...) + if len(all) == 0 { + t.Skip("无生产样本") + } + skipped := 0 + for _, r := range all { + if !sharesTopicWith(r.RightContent, ExtractEntities(r.RightContent), r.LeftContent) { + skipped++ + } + } + ratio := float64(skipped) / float64(len(all)) + t.Logf("真实样本 %d 对,门跳过 %d 对(%.0f%%)", len(all), skipped, ratio*100) + if skipped == 0 { + t.Errorf("门一次都没跳过 —— 与旧 bug(恒真)无区别") + } +} + +// ④ 阈值必须落在实测区间内(阈值由数据标定,不许拍脑袋)。 +func TestTopicThreshold_LockedToMeasuredData(t *testing.T) { + fp := loadRealConflicts(t, "/var/lib/memoryweave/conflicts-false-positive-*.json") + maxFP := 0.0 + for _, r := range fp { + if v := charBigramJaccard(r.RightContent, r.LeftContent); v > maxFP { + maxFP = v + } + } + trueMin := 1.0 + for _, p := range [][2]string{ + {"用户昨天没买苹果", "用户昨天买了苹果"}, + {"config not updated", "config updated"}, + {"牧尘不在招商局工作", "牧尘在招商局工作"}, + {"Docker is NOT used by Hermes", "Docker is used by Hermes"}, + {"用户未在招商局工作", "用户在招商局工作"}, + } { + if v := charBigramJaccard(p[0], p[1]); v < trueMin { + trueMin = v + } + } + t.Logf("实测:假阳性 bigram 上界=%.3f | 阈值=%.2f | 真矛盾 bigram 下界=%.3f(两侧余量 %.2fx / %.2fx)", + maxFP, topicOverlapMin, trueMin, topicOverlapMin/maxFP, trueMin/topicOverlapMin) + if len(fp) > 0 && maxFP >= topicOverlapMin { + t.Errorf("阈值 %.2f 未覆盖假阳性上界 %.3f —— 会漏放话题无关候选", topicOverlapMin, maxFP) + } + if trueMin < topicOverlapMin { + t.Errorf("阈值 %.2f 高于真矛盾下界 %.3f —— 会误杀真冲突", topicOverlapMin, trueMin) + } +} + +// ⑤ 中文实体抽取必须非空 —— 这正是「拿 graph_auto 版(只认 ASCII 大写词)当修法」 +// 会踩的坑:对纯中文恒返回空 ⇒ 预筛永不匹配 ⇒ 冲突检测整体静默失效。 +func TestExtractEntities_ChineseSupported(t *testing.T) { + for _, s := range []string{ + "牧尘在招商局工作", + "织忆服务部署在 192.168.5.106 上", + "小怡使用 Hermes Agent 部署织忆", + } { + if got := ExtractEntities(s); len(got) == 0 { + t.Errorf("ExtractEntities(%q) 返回空 —— 纯中文文本必须抽得出实体", s) + } else { + t.Logf("ExtractEntities(%q) = %v", s, got) + } + } + if got := ExtractEntities("The Deployment uses Hermes Agent"); len(got) == 0 { + t.Errorf("英文专名未抽到") + } + if got := ExtractEntities("Aaa Bbb Ccc Ddd Eee Fff Ggg Hhh Iii Jjj Kkk"); len(got) > maxExtractEntities { + t.Errorf("实体数 %d 超过上限 %d", len(got), maxExtractEntities) + } +} + +func trim(s string) string { + r := []rune(s) + if len(r) > 46 { + return string(r[:46]) + "…" + } + return s +} diff --git a/go/internal/governance/governance.go b/go/internal/governance/governance.go index 7108b1d..22c6cbd 100644 --- a/go/internal/governance/governance.go +++ b/go/internal/governance/governance.go @@ -236,34 +236,44 @@ func (cd *ConflictDetector) persistLocked() { } // Scan 扫描新记忆与已有记忆的冲突 +// +// 2026-10-09 修(实体预筛失效):原实现用「新内容实体 ∩ 候选实体」做预筛,而调用方 +// 给每个候选塞的都是**新内容的实体**(同一个切片)⇒ e1==e2 恒真 ⇒ 预筛完全失效。 +// 实害:预筛一条都没过滤 + 每条候选跑 len(实体) 次冗余 IsContradiction(线上实测 4~12) +// + 同一 (新内容, 候选) 对产出多条重复 Conflict + Entity 字段恒为「新内容第一个实体」。 +// (**不是** 106 条假阳性的成因——那批误报的根因是 IsContradiction 旧启发式, +// 已于 2026-09-20/09-21 修复;869 条真实样本在当前判据下判 true 的为 0。) +// +// 修法:候选侧改为用**它自己的正文**做话题交集判定(sharesTopicWith,词表无关), +// 每条候选最多比对一次、最多产出一条 Conflict;取不到信号时照常比较(宁可多算,不可漏检)。 +// 详见 conflict_prefilter.go 文件头的实测标定。 func (cd *ConflictDetector) Scan(newContent string, newEntities []string, existing []map[string]interface{}) []*Conflict { var conflicts []*Conflict - for _, existing := range existing { - existingContent := existing["content"].(string) - existingEntities := toStringSlice(existing["entities"]) + for _, cand := range existing { + existingContent, _ := cand["content"].(string) + if existingContent == "" { + continue + } - // 检查相同实体但不同关系 - for _, e1 := range newEntities { - for _, e2 := range existingEntities { - if e1 == e2 { - // 检测事实冲突:内容语义矛盾 - if IsContradiction(newContent, existingContent) { - conflicts = append(conflicts, &Conflict{ - ID: "cft_" + strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + strconv.Itoa(len(conflicts)), - Type: ConflictFact, - Entity: e1, - Description: "事实冲突:新内容与已有记录矛盾", - Strategy: "ask_user", - Status: "pending", - CreatedAt: time.Now(), - // 2026-09-20:留档两侧原文,供事后核实真假矛盾 - LeftContent: existingContent, - RightContent: newContent, - }) - } - } - } + // 预筛:无话题交集 ⇒ 二者讲的是两件事,不可能互相矛盾 + if !sharesTopicWith(newContent, newEntities, existingContent) { + continue + } + + if IsContradiction(newContent, existingContent) { + conflicts = append(conflicts, &Conflict{ + ID: "cft_" + strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + strconv.Itoa(len(conflicts)), + Type: ConflictFact, + Entity: conflictEntity(newContent, newEntities, existingContent), + Description: "事实冲突:新内容与已有记录矛盾", + Strategy: "ask_user", + Status: "pending", + CreatedAt: time.Now(), + // 2026-09-20:留档两侧原文,供事后核实真假矛盾 + LeftContent: existingContent, + RightContent: newContent, + }) } }