1- import { validateDecisions , applyDecisions } from "./dream/decisions.js" ;
1+ import { validateDecisions , applyDecisions , supersedeInfoFirstPair } from "./dream/decisions.js" ;
22import { clusterMemories , findPotentialConflicts , cosineSimilarity } from "./dream/clustering.js" ;
3+ import { triagePair } from "./dream/triage.js" ;
34import { clusterByTag , intersectEvidence } from "./dream/narratives.js" ;
45import { 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 }
0 commit comments