|
| 1 | +// 冲突分诊(issue #641 beta 第一阶段,shadow 档):把 findPotentialConflicts 送进 |
| 2 | +// 冲突相位的每一对,先过确定性分诊,落进 conflict_ledger 账本。 |
| 3 | +// |
| 4 | +// 为什么分诊先行:真实库 788 对人审队列的全量分类(只读挖掘,口径见 #641 正文) |
| 5 | +// ——46% 近重复 + 20% 跨作用域重复 + 5% 时序演进,真矛盾只有 ~27%。前 71% 是该被 |
| 6 | +// 机械处置(merge / 去重 / 取代链)的工作量,现行窄 prompt(conflict/keep 二选一) |
| 7 | +// 把它们全逼成了「输赢」,再由人来逐对裁决(≈30 条/天,且 92% 的对从未两侧同场)。 |
| 8 | +// 分诊把「谁该机械处理、谁才是真矛盾」从 LLM 手里拿回来:E13b 实测 winner-take-all |
| 9 | +// 裁决 P(毒胜出)=86.8%,LLM 在这条链上只允许降级为分类器。 |
| 10 | +// |
| 11 | +// 四档判据(全部确定性,本文件是唯一实现;enforce 阶段的处置接线另有 PR,本文件 |
| 12 | +// 在 shadow 档只产分类与信号,不做任何记忆行处置): |
| 13 | +// cross-scope-dup 任一 scope 维键不同——同一内容落两个归属(#170 scopeReason 口径, |
| 14 | +// 真实库占 20%) |
| 15 | +// dup 同 scope + sim ≥ dupMin + created_at 差 < 24h——同日双录 |
| 16 | +// (真实库 76% 的对两侧差 <1 天;#616 实测 55-57% 的新条反而更短, |
| 17 | +// 新鲜度对这类对无区分力,后续处置按信息量裁决) |
| 18 | +// evolution 同 scope + created_at 差 ≥ 7 天——时序演进展期(T3 superseded_by |
| 19 | +// 取代链的候选池,enforce 阶段接线;entity_attrs 失效信号并入也在 |
| 20 | +// 那一步,见 #585) |
| 21 | +// conflict 其余——真矛盾残余(真实库 ~27%;账本标记,消解推迟到检索时/ |
| 22 | +// 会话纠正/巩固重蒸馏三时刻) |
| 23 | +// |
| 24 | +// 「未标注 scope」不算跨作用域:scopeKeyOf 把 NULL 归一成 null,两条都未标注时 |
| 25 | +// null !== null 为 false、判同档——#616 指出这个判据用于**自动归档**是洞(归属未知 |
| 26 | +// ≠归属相同),但在 shadow 分诊里零处置、无害;signals.annotated=false 显式落账, |
| 27 | +// 让分析时能把「未标注」切出来,而不是让它静默混进同 scope 桶。 |
| 28 | +import { scopeKeyOf } from "../scope.js"; |
| 29 | + |
| 30 | +const DAY_MS = 86_400_000; |
| 31 | + |
| 32 | +// T1(dup)与 T3(evolution)的时间界。24h 来自真实库分布(76% 的对 <1 天,且 |
| 33 | +// 1-7 天区间的「疑似同日双录」残留在 1-7 天档里很难与快速演进区分,宁保守归 |
| 34 | +// conflict——分诊错档在 shadow 期只是统计噪音,enforce 后错档会错杀);7 天是 |
| 35 | +// 演进展期的保守下限,真实库 7 天以上的对仅 0.5%(4/788)。 |
| 36 | +export const DUP_MAX_AGE_MS = DAY_MS; |
| 37 | +export const EVOLUTION_MIN_AGE_MS = 7 * DAY_MS; |
| 38 | + |
| 39 | +export const LEDGER_TIERS = ["dup", "cross-scope-dup", "evolution", "conflict"]; |
| 40 | + |
| 41 | +/** |
| 42 | + * Classify one candidate pair into a ledger tier. Pure function — no I/O, no |
| 43 | + * config dependency beyond the tunables passed in, so the same verdict is |
| 44 | + * reproducible from the recorded signals alone. |
| 45 | + * |
| 46 | + * @param {object} a candidate side A (needs agent_scope, workspace_scope, created_at) |
| 47 | + * @param {object} b candidate side B |
| 48 | + * @param {number} similarity detector cosine similarity (pair already passed the |
| 49 | + * detection threshold, so ≥ ~0.75-0.92 depending on strictness) |
| 50 | + * @param {{dupMinSimilarity?: number, now?: number}} opts dupMin 默认 0.90(#616 |
| 51 | + * 重测:0.95 在现库覆盖为 0,真实库 reason 相似度回声集中在 0.85-0.93, |
| 52 | + * 0.90 是把回声带接进 dup 档的落点);now 仅测试注入 |
| 53 | + * @returns {{tier: string, signals: object}} |
| 54 | + */ |
| 55 | +export function triagePair(a, b, similarity, opts = {}) { |
| 56 | + const dupMin = typeof opts.dupMinSimilarity === "number" ? opts.dupMinSimilarity : 0.9; |
| 57 | + const now = typeof opts.now === "number" ? opts.now : Date.now(); |
| 58 | + |
| 59 | + const crossScope = |
| 60 | + scopeKeyOf(a.agent_scope) !== scopeKeyOf(b.agent_scope) || |
| 61 | + scopeKeyOf(a.workspace_scope) !== scopeKeyOf(b.workspace_scope); |
| 62 | + const annotated = (a.agent_scope != null || a.workspace_scope != null) && |
| 63 | + (b.agent_scope != null || b.workspace_scope != null); |
| 64 | + |
| 65 | + // created_at 缺失不下结论:宁归 conflict(账本里最保守的一档),也不编造时间信号。 |
| 66 | + const createdA = Date.parse(a.created_at ?? ""); |
| 67 | + const createdB = Date.parse(b.created_at ?? ""); |
| 68 | + const gapKnown = Number.isFinite(createdA) && Number.isFinite(createdB); |
| 69 | + const gapDays = gapKnown ? Math.abs(createdA - createdB) / DAY_MS : null; |
| 70 | + |
| 71 | + let tier; |
| 72 | + if (crossScope) { |
| 73 | + tier = "cross-scope-dup"; |
| 74 | + } else if (gapKnown && typeof similarity === "number" && similarity >= dupMin && gapDays < DUP_MAX_AGE_MS / DAY_MS) { |
| 75 | + tier = "dup"; |
| 76 | + } else if (gapKnown && gapDays * DAY_MS >= EVOLUTION_MIN_AGE_MS) { |
| 77 | + tier = "evolution"; |
| 78 | + } else { |
| 79 | + tier = "conflict"; |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + tier, |
| 84 | + signals: { |
| 85 | + crossScope, |
| 86 | + annotated, |
| 87 | + gapDays: gapDays == null ? null : Math.round(gapDays * 1000) / 1000, |
| 88 | + dupMin, |
| 89 | + similarity: typeof similarity === "number" ? similarity : null |
| 90 | + } |
| 91 | + }; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Triage + record every selected pair into the conflict_ledger. Bookkeeping |
| 96 | + * only: a store failure on one pair must never fail the phase (same |
| 97 | + * best-effort discipline as saveConflictReviewed / saveDifferentiatedPair — |
| 98 | + * 记账本身绝不能反过来把巩固打断)。 |
| 99 | + * |
| 100 | + * @returns tally object {dup, cross-scope-dup, evolution, conflict, recorded, failed} |
| 101 | + * or null when the caller shouldn't record (caller decides mode). |
| 102 | + */ |
| 103 | +export function recordLedgerTriage(service, runId, pairs, opts = {}) { |
| 104 | + const tally = { dup: 0, "cross-scope-dup": 0, evolution: 0, conflict: 0, recorded: 0, failed: 0 }; |
| 105 | + for (const p of pairs) { |
| 106 | + try { |
| 107 | + const verdict = triagePair(p.a, p.b, p.similarity, opts); |
| 108 | + service.saveConflictLedgerEntry({ |
| 109 | + run_id: runId, |
| 110 | + memory_a: p.a.id, |
| 111 | + memory_b: p.b.id, |
| 112 | + tier: verdict.tier, |
| 113 | + similarity: p.similarity, |
| 114 | + signals: verdict.signals |
| 115 | + }); |
| 116 | + tally[verdict.tier] += 1; |
| 117 | + tally.recorded += 1; |
| 118 | + } catch (error) { |
| 119 | + tally.failed += 1; |
| 120 | + opts.logger?.warn?.(`dsh-mneme sleep: failed to record ledger triage for ${p.a?.id}/${p.b?.id}: ${String(error)}`); |
| 121 | + } |
| 122 | + } |
| 123 | + return tally; |
| 124 | +} |
0 commit comments