Skip to content

Commit 1ea7087

Browse files
committed
feat(dream): 冲突分诊账本 enforce 档——确定性处置接线(#641 beta 第二阶段)
enforce 档把人审队列与 LLM winner-take-all 裁决从冲突链路里摘出去(E13b: P(毒胜出)=86.8%;人审 ≈30 条/天且 92% 的对从未两侧同场)。四档处置: - T1 dup:supersedeInfoFirstPair(decisions.js 新共享出口)——信息量优先 (长度 → updated_at → id 三级兜底)、trustEpistemicWeighting 显式关(自动 处置承诺必须确定)、一律 supersede 不走 conflict(E13b 的值级泄漏通道 ——败者前 100 字追加进赢家——在本档不可达)。败者向量清理补进 maintainIndexAfterDream 清单(独立落地不进 applyList,漏清就是幽灵行); dream 的 noChange 同步计入 T1 产出(有归档的轮不许报 noop 刷基线)。 - T2/T3/T4:记账即处置——不停车、不送 LLM、不归档,两条都活着、可测。 T3 的取代链与 T4 的检索时消解是后续 PR;本档先摘掉两个不可靠消费方。 - sleep 与 dream 两条链路同闸:dream 的 conflict 决策降级为分类器——经 triagePair 再分诊后才处置(信号判 dup 的照样确定性 supersede,判据唯一 实现是 triage.js 不是 LLM)。 - #616 父子绑定不继承:enforce 与 freeze 同开时 freeze 让位(sleep 降 shadow / dream 降 freeze,warn 显式说出)——宁可少处置,不在两套处置 语义打架的配置下跑。测试锁死。 默认 off 逐字节零变化;shadow 与 #643 完全一致。新增 8 条回归 (sleep e2e 两档 + freeze 共存 + helper 契约三条 + dream e2e 两条), 全量 3285 tests / 3284 pass / 0 fail / 1 skipped;check-sync 120 文件一致。
1 parent 557adbe commit 1ea7087

13 files changed

Lines changed: 675 additions & 53 deletions

File tree

‎dsh-mneme/docs/CONFIGURATION.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
| `conflictFreezeMaxPending` | `100` | 冻结队列上限(≤1000) | — |
270270
| `conflictAutoResolveEnabled` | `false` | 相似度极高的**同 scope** 重复对自动裁决(保留新条、归档旧条),不进人工队列 | opt-in。会归档记忆,所以默认关;跨 scope 对永不自动(归属是用户的决定,#170)。用 supersede 而非 conflict:注记写在被归档侧,幸存记忆正文不动 |
271271
| `conflictAutoResolveMinSimilarity` | `0.95` | 自动裁决的相似度门槛(0.85–1) | 实测 live 库 345 条有相似度的历史行里 ≥0.95 只占 5.8%、≥0.94 占 8.1%——这一档减的是峰值,不是把队列清零。低于 0.85 就不是「重复」而是「相似」,不该自动归档任何一条 |
272-
| `conflictLedgerMode` | `off` | 冲突分诊账本档位(`off` / `shadow`) | beta 第一阶段(#641)。`shadow` = 每轮巩固对冲突候选对做确定性分诊(近重复/跨作用域/演进/真矛盾四档,判据见 `src/dream/triage.js`)并落 `conflict_ledger` 账本,**冻结/LLM 路径零变化**——先测一周真实分布,预注册判据过了才切 enforce(处置接线在后续 PR)。现阶段枚举里没有 enforce:不存在「设了但没行为」的档位。不进 lightMode 强制关闭清单:shadow 只多一张 bookkeeping 表的写入,无 LLM 调用无注入物(同 `actionPipelineEnabled` 的定性) |
272+
| `conflictLedgerMode` | `off` | 冲突分诊账本档位(`off` / `shadow` / `enforce`) | beta(#641)。`shadow` = 每轮巩固对冲突候选对做确定性分诊(近重复/跨作用域/演进/真矛盾四档,判据见 `src/dream/triage.js`)并落 `conflict_ledger` 账本,**冻结/LLM 路径零变化**——先测一周真实分布再切档。`enforce` = 确定性处置:近重复对走信息量优先 supersede(败者归档、胜者正文不动),其余档只记账——不停车、不送 LLM、不归档,人审队列消失。与 `conflictFreezeEnabled` 同开时 freeze 让位(降级 + warn,#616 父子绑定不继承)。T3 取代链与 T4 检索时消解在后续 PR。不进 lightMode 强制关闭清单:shadow 只多一张 bookkeeping 表的写入,enforce 零 LLM 调用(同 `actionPipelineEnabled` 的定性) |
273273
| `conflictLedgerDupMinSimilarity` | `0.9` | 分诊 T1(近重复)档的相似度下限(0.85–1) | 检测阈值(0.85)与自动档(0.95)之间的空带由它接管:真实库 788 对的相似度回声集中在 0.85–0.93,恰好是自动档开了也接不住的那段 |
274274

275275
## 实体

‎dsh-mneme/lib/config.js‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -595,22 +595,27 @@ export const Config = z.object({
595595
conflictAutoResolveEnabled: z.boolean().default(false),
596596
conflictAutoResolveMinSimilarity: z.number().min(0.85).max(1).default(0.95),
597597

598-
// --- conflict ledger(issue #641,beta 第一阶段)--------------------------
598+
// --- conflict ledger(issue #641,beta)-----------------------------------
599599
// 冲突分诊账本的档位。真实库 788 对人审队列的全量分类:46% 近重复 + 20% 跨作用域
600600
// 重复 + 5% 时序演进,真矛盾只有 ~27%——现行窄 prompt 把前 71% 全逼成了「输赢」,
601601
// 再由人逐对裁决(≈30 条/天,且 92% 的对从未两侧同场)。分诊把「谁该机械处理、
602602
// 谁才是真矛盾」从 LLM 手里拿回来(E13b:winner-take-all 裁决 P(毒胜出)=86.8%)。
603603
//
604-
// "off" 默认。不分类、不记账,conflict 相位与 v0.13.0 逐字节一致。
605-
// "shadow" 只分类 + 落 conflict_ledger 账本(bookkeeping,不触发 write hooks),
606-
// freeze/LLM 路径零变化——beta 实验的测量期:shadow 一周拿真实分诊
607-
// 分布,验证是否复现 46/20/5/27,预注册判据过了才切 enforce(处置接线
608-
// 是后续 PR:T1 送合并、T2 去重账本、T3 取代链、T4 惰性消解)。
604+
// "off" 默认。不分类、不记账,conflict 相位与本键引入前逐字节一致。
605+
// "shadow" 只分类 + 落 conflict_ledger 账本(bookkeeping,不触发 write hooks),
606+
// freeze/LLM 路径零变化——beta 实验的测量期:shadow 一周拿真实分诊
607+
// 分布,验证是否复现 46/20/5/27。
608+
// "enforce" 确定性处置:T1(dup)走信息量优先 supersede(supersedeInfoFirstPair,
609+
// 败者归档、胜者正文不动、可追溯注记在被归档侧);T2/T3/T4 记账即
610+
// 处置——不停车、不送 LLM、不归档。人审队列与 LLM 裁决都不再消费
611+
// 冲突候选;与 conflictFreezeEnabled 同开时 freeze 让位(降级 shadow
612+
// + warn,#616 父子绑定不继承)。T3 的取代链与 T4 的检索时消解是
613+
// 后续 PR,enforce 先把人审摘出去。
609614
//
610-
// 现阶段枚举里不放 "enforce":宁可让配置挡住手滑,也不要一个「设了但没行为」的
611-
// 档位——#616 的「假成功」教训(autoMin 0.95 在现库覆盖为 0,开了等于没开,用户
612-
// 以为功能坏了)。判据唯一实现在 src/dream/triage.js。
613-
conflictLedgerMode: z.union([z.const("off"), z.const("shadow")]).default("off"),
615+
// 判据唯一实现在 src/dream/triage.js;处置共享实现在 decisions.js 的
616+
// supersedeInfoFirstPair。每个档位的行为都真实存在——#616 的「假成功」教训
617+
// (autoMin 0.95 在现库覆盖为 0,开了等于没开)。
618+
conflictLedgerMode: z.union([z.const("off"), z.const("shadow"), z.const("enforce")]).default("off"),
614619
// T1(dup)档的相似度下限。检测阈值(normal 0.85)与自动档 autoMin(0.95)之间
615620
// 的空带由它接管:真实库 reason 相似度回声集中在 0.85-0.93,正好落在两个阈值
616621
// 之间、自动档开了也接不住的那一段。下界与检测阈值同源。

‎dsh-mneme/lib/dream.js‎

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { validateDecisions, applyDecisions } from "./dream/decisions.js";
1+
import { validateDecisions, applyDecisions, supersedeInfoFirstPair } from "./dream/decisions.js";
22
import { clusterMemories, findPotentialConflicts, cosineSimilarity } from "./dream/clustering.js";
3+
import { triagePair } from "./dream/triage.js";
34
import { clusterByTag, intersectEvidence } from "./dream/narratives.js";
45
import { scopeKeyOf } from "./scope.js";
56
// Issue #239(第 4 项)镜像到巩固:错峰时段解析与「最近的高峰结束时刻」从独立的
@@ -1487,6 +1488,11 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
14871488
// bounds the review queue; overflow is skipped with a warning.
14881489
let frozenCount = 0;
14891490
const frozenIds = [];
1491+
// Issue #641(enforce 档):T1 supersede 的实质产出计数与向量清理清单。声明在
1492+
// freeze/enforce 两个分支外层——noChange 判定与 maintainIndexAfterDream 都在
1493+
// 它们之后、函数作用域里消费。
1494+
let ledgerApplied = 0;
1495+
let ledgerVectorFixups = [];
14901496
// v0.8.1(issue #170 复核项 3):跨 scope 的 conflict 决策两种模式都一律
14911497
// 停车人工裁决、绝不自动归档——否则普通 dream 的 LLM 能归档别的 scope 的
14921498
// 唯一副本,sleep 路径(scope-candidate-queue)的保护在这里缺位。同 scope
@@ -1499,9 +1505,61 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
14991505
|| scopeKeyOf(a.workspace_scope) !== scopeKeyOf(b.workspace_scope)
15001506
);
15011507
};
1502-
const applyList = freezeEnabled
1508+
// Issue #641(beta 第二阶段,enforce 档):dream 主链路的 conflict 决策改为
1509+
// 确定性处置。LLM 判出的 conflict 对先经 triagePair 再分诊:判为 dup 的走
1510+
// supersedeInfoFirstPair(与 sleep 自动档同一条契约);其余(cross-scope-dup /
1511+
// evolution / conflict)只落 conflict_ledger 账本(tier + 信号),不停车、
1512+
// 不归档。#616 的父子绑定不继承:enforce 与 freeze 同开时 freeze 让位——
1513+
// 本段降级 shadow(conflict 照旧停车),绝不两套处置语义同时生效。
1514+
const ledgerEnforce = (config.conflictLedgerMode ?? "off") === "enforce" && !freezeEnabled;
1515+
if ((config.conflictLedgerMode ?? "off") === "enforce" && freezeEnabled) {
1516+
logger?.warn?.("dsh-mneme dream: conflictLedgerMode=enforce coexists with conflictFreezeEnabled — degrading to freeze; disable one of them");
1517+
}
1518+
if (ledgerEnforce) {
1519+
let t1Resolved = 0;
1520+
let marked = 0;
1521+
for (const d of decisions.filter((x) => x.action === "conflict")) {
1522+
const a = snapshot.get(d.winner);
1523+
const b = snapshot.get(d.loser);
1524+
if (!a || !b) continue;
1525+
try {
1526+
const verdict = triagePair(a, b, pairSimilarity.get(similarityKey(d.winner, d.loser)));
1527+
if (verdict.tier === "dup") {
1528+
const res = supersedeInfoFirstPair({ a, b }, service, logger, config);
1529+
if (res.applied) {
1530+
t1Resolved += 1;
1531+
ledgerApplied += 1;
1532+
// 败者向量清理走下面的 maintainIndexAfterDream:T1 的 supersede 是
1533+
// 本 helper 独立落地的,不在 applyList 里,必须显式补进维护清单
1534+
// (否则索引里留一行已归档记忆的向量,「清得掉才检索得到」失衡)。
1535+
ledgerVectorFixups.push({ action: "supersede", winner: res.winnerId, loser: res.loserId });
1536+
service.setConflictLedgerStatus(a.id, b.id, "superseded");
1537+
try {
1538+
service.saveConflictReviewed?.({ run_id: runId, memory_a: a.id, memory_b: b.id, outcome: "supersede", judged_at: new Date().toISOString() });
1539+
} catch (error) {
1540+
logger?.warn?.(`dsh-mneme dream: conflict_reviewed bookkeeping failed for ${a.id}/${b.id}: ${String(error)}`);
1541+
}
1542+
} else {
1543+
// CAS 没过(两侧在快照后被改过):不冒险自动处置,落账本留观察。
1544+
service.saveConflictLedgerEntry({ run_id: runId, memory_a: a.id, memory_b: b.id, tier: verdict.tier, similarity: verdict.signals.similarity, signals: verdict.signals });
1545+
}
1546+
} else {
1547+
service.saveConflictLedgerEntry({ run_id: runId, memory_a: a.id, memory_b: b.id, tier: verdict.tier, similarity: verdict.signals.similarity, signals: verdict.signals });
1548+
}
1549+
marked += 1;
1550+
} catch (error) {
1551+
logger?.warn?.(`dsh-mneme dream: ledger conflict mark failed for ${d.winner}/${d.loser}: ${String(error)}`);
1552+
}
1553+
}
1554+
if (marked > 0) {
1555+
logger?.info?.(`[dsh-mneme] dream: ledger enforce marked ${marked} conflict decision(s), ${t1Resolved} deterministically superseded`);
1556+
}
1557+
}
1558+
const applyList = ledgerEnforce
15031559
? decisions.filter((d) => d.action !== "conflict")
1504-
: decisions.filter((d) => d.action !== "conflict" || !isCrossScopeDecision(d));
1560+
: freezeEnabled
1561+
? decisions.filter((d) => d.action !== "conflict")
1562+
: decisions.filter((d) => d.action !== "conflict" || !isCrossScopeDecision(d));
15051563
if (freezeEnabled) {
15061564
const conflictsToFreeze = decisions.filter((d) => d.action === "conflict");
15071565
if (conflictsToFreeze.length > 0) {
@@ -1531,9 +1589,11 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
15311589
logger?.warn?.(`dsh-mneme dream: conflict freeze lookup failed: ${String(error)}`);
15321590
}
15331591
}
1534-
} else {
1592+
} else if (!ledgerEnforce) {
15351593
// 非 freeze:跨 scope conflict 停车(绝不进 applyDecisions)。带专属
15361594
// reason + 模型给出的裁决理由,队列里读起来是「归属裁决」而非重复项。
1595+
// ledger enforce 档不进这里:conflict 决策已在上面落账本(tier 含
1596+
// cross-scope-dup),零停车零归档(#641)。
15371597
const crossScope = decisions.filter((d) => d.action === "conflict" && isCrossScopeDecision(d));
15381598
for (const d of crossScope) {
15391599
try {
@@ -1606,12 +1666,14 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
16061666
// happened and the scheduler refreshes the baseline on a false positive.
16071667
// Frozen conflicts are substantive output (parked for review), so a run
16081668
// that only froze conflicts is not a noop.
1609-
const noChange = frozenCount === 0 && applied === 0 && committed.every((c) => c.action === "keep");
1669+
// Issue #641:ledger enforce 的 T1 supersede 是独立于 applyList 落地的实质产出,
1670+
// 计入「本轮有没有干活」,否则有归档产出的轮会被当 noop 刷基线(#634 同族误报)。
1671+
const noChange = frozenCount === 0 && applied === 0 && ledgerApplied === 0 && committed.every((c) => c.action === "keep");
16101672

16111673
// Keep the vector index consistent with the post-dream store state.
16121674
if (semantic?.embedder && semantic?.vectorIndex) {
16131675
try {
1614-
await maintainIndexAfterDream(applyList, service, semantic);
1676+
await maintainIndexAfterDream(ledgerVectorFixups.length ? [...applyList, ...ledgerVectorFixups] : applyList, service, semantic);
16151677
} catch (error) {
16161678
logger?.warn?.(`[dsh-mneme] dream index maintenance failed: ${String(error)}`);
16171679
}

‎dsh-mneme/lib/dream/decisions.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,44 @@ function applyConflict(d, service, snapshot, config = {}) {
766766
return { applied: 1, committed: { action: "conflict", winner: d.winner, loser: d.loser, count_before: 2, count_after: 1 } };
767767
}
768768

769+
/**
770+
* 确定性信息量优先 supersede(issue #641 enforce 档的 T1 处置,供 sleep/dream
771+
* 两条链路共用)。与冻结分支自动裁决档(#603 第 4 层,sleep.js 内联实现)同一
772+
* 条契约,本函数是它的共享出口:
773+
* · 保留**信息量更大**的那条,不是更新的——#616 实测 55-57% 的新条反而更短
774+
* (蒸馏压缩过的一版常常更新),留新条等于把更完整的那条归档掉;
775+
* · 长度相同才退回 updated_at,再退回 id——三级兜底保证同一对每次判出同一个
776+
* 赢家(重放不翻面);
777+
* · trustEpistemicWeighting 显式关:自动处置的承诺必须是确定的,可信度换位
778+
* 不让结果随 epistemic_status 数据漂移;
779+
* · 赢家正文一字不动——supersede 的可追溯注记落在被归档侧(#126 原设计)。
780+
* E13b 证明 conflict 动作的「败者前 100 字追加进赢家」是值级泄漏通道,
781+
* 确定性处置一律走 supersede、不走 conflict。
782+
* CAS 快照 + 事务 + 幂等(败者已归档 → applied 0)全部沿用 applyDecisions,
783+
* 不另写第二份裁决路径。
784+
*
785+
* @param {{a: object, b: object}} p 候选对(完整记忆行:id/title/content/updated_at)
786+
* @returns {{applied: boolean, winnerId: string, loserId: string, winner: object, loser: object}}
787+
*/
788+
export function supersedeInfoFirstPair(p, service, logger = null, config = {}) {
789+
const an = p.a.content?.length ?? 0;
790+
const bn = p.b.content?.length ?? 0;
791+
const [winner, loser] = an !== bn
792+
? (an > bn ? [p.a, p.b] : [p.b, p.a])
793+
: (p.a.updated_at ?? "") !== (p.b.updated_at ?? "")
794+
? ((p.a.updated_at ?? "") > (p.b.updated_at ?? "") ? [p.a, p.b] : [p.b, p.a])
795+
: (p.a.id > p.b.id ? [p.a, p.b] : [p.b, p.a]);
796+
const snapshot = new Map();
797+
for (const m of [winner, loser]) {
798+
snapshot.set(m.id, { updated_at: m.updated_at, content: m.content, title: m.title });
799+
}
800+
const { applied } = applyDecisions(
801+
[{ action: "supersede", winner: winner.id, loser: loser.id }],
802+
service, logger, snapshot, { ...config, trustEpistemicWeighting: false }
803+
);
804+
return { applied: applied > 0, winnerId: winner.id, loserId: loser.id, winner, loser };
805+
}
806+
769807
/**
770808
* supersede(Issue #126,sleepActionSet="full"):演进型重复——新版本取代旧版本。
771809
* 与 conflict 的关键差别:**注记追加到 loser(被取代方)而不是 winner**。conflict

0 commit comments

Comments
 (0)