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 项)镜像到巩固:错峰时段解析与「最近的高峰结束时刻」从独立的
@@ -1508,6 +1509,11 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
15081509 // bounds the review queue; overflow is skipped with a warning.
15091510 let frozenCount = 0 ;
15101511 const frozenIds = [ ] ;
1512+ // Issue #641(enforce 档):T1 supersede 的实质产出计数与向量清理清单。声明在
1513+ // freeze/enforce 两个分支外层——noChange 判定与 maintainIndexAfterDream 都在
1514+ // 它们之后、函数作用域里消费。
1515+ let ledgerApplied = 0 ;
1516+ let ledgerVectorFixups = [ ] ;
15111517 // v0.8.1(issue #170 复核项 3):跨 scope 的 conflict 决策两种模式都一律
15121518 // 停车人工裁决、绝不自动归档——否则普通 dream 的 LLM 能归档别的 scope 的
15131519 // 唯一副本,sleep 路径(scope-candidate-queue)的保护在这里缺位。同 scope
@@ -1520,9 +1526,61 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
15201526 || scopeKeyOf ( a . workspace_scope ) !== scopeKeyOf ( b . workspace_scope )
15211527 ) ;
15221528 } ;
1523- const applyList = freezeEnabled
1529+ // Issue #641(beta 第二阶段,enforce 档):dream 主链路的 conflict 决策改为
1530+ // 确定性处置。LLM 判出的 conflict 对先经 triagePair 再分诊:判为 dup 的走
1531+ // supersedeInfoFirstPair(与 sleep 自动档同一条契约);其余(cross-scope-dup /
1532+ // evolution / conflict)只落 conflict_ledger 账本(tier + 信号),不停车、
1533+ // 不归档。#616 的父子绑定不继承:enforce 与 freeze 同开时 freeze 让位——
1534+ // 本段降级 shadow(conflict 照旧停车),绝不两套处置语义同时生效。
1535+ const ledgerEnforce = ( config . conflictLedgerMode ?? "off" ) === "enforce" && ! freezeEnabled ;
1536+ if ( ( config . conflictLedgerMode ?? "off" ) === "enforce" && freezeEnabled ) {
1537+ logger ?. warn ?. ( "dsh-mneme dream: conflictLedgerMode=enforce coexists with conflictFreezeEnabled — degrading to freeze; disable one of them" ) ;
1538+ }
1539+ if ( ledgerEnforce ) {
1540+ let t1Resolved = 0 ;
1541+ let marked = 0 ;
1542+ for ( const d of decisions . filter ( ( x ) => x . action === "conflict" ) ) {
1543+ const a = snapshot . get ( d . winner ) ;
1544+ const b = snapshot . get ( d . loser ) ;
1545+ if ( ! a || ! b ) continue ;
1546+ try {
1547+ const verdict = triagePair ( a , b , pairSimilarity . get ( similarityKey ( d . winner , d . loser ) ) ) ;
1548+ if ( verdict . tier === "dup" ) {
1549+ const res = supersedeInfoFirstPair ( { a, b } , service , logger , config ) ;
1550+ if ( res . applied ) {
1551+ t1Resolved += 1 ;
1552+ ledgerApplied += 1 ;
1553+ // 败者向量清理走下面的 maintainIndexAfterDream:T1 的 supersede 是
1554+ // 本 helper 独立落地的,不在 applyList 里,必须显式补进维护清单
1555+ // (否则索引里留一行已归档记忆的向量,「清得掉才检索得到」失衡)。
1556+ ledgerVectorFixups . push ( { action : "supersede" , winner : res . winnerId , loser : res . loserId } ) ;
1557+ service . setConflictLedgerStatus ( a . id , b . id , "superseded" ) ;
1558+ try {
1559+ service . saveConflictReviewed ?. ( { run_id : runId , memory_a : a . id , memory_b : b . id , outcome : "supersede" , judged_at : new Date ( ) . toISOString ( ) } ) ;
1560+ } catch ( error ) {
1561+ logger ?. warn ?. ( `dsh-mneme dream: conflict_reviewed bookkeeping failed for ${ a . id } /${ b . id } : ${ String ( error ) } ` ) ;
1562+ }
1563+ } else {
1564+ // CAS 没过(两侧在快照后被改过):不冒险自动处置,落账本留观察。
1565+ service . saveConflictLedgerEntry ( { run_id : runId , memory_a : a . id , memory_b : b . id , tier : verdict . tier , similarity : verdict . signals . similarity , signals : verdict . signals } ) ;
1566+ }
1567+ } else {
1568+ service . saveConflictLedgerEntry ( { run_id : runId , memory_a : a . id , memory_b : b . id , tier : verdict . tier , similarity : verdict . signals . similarity , signals : verdict . signals } ) ;
1569+ }
1570+ marked += 1 ;
1571+ } catch ( error ) {
1572+ logger ?. warn ?. ( `dsh-mneme dream: ledger conflict mark failed for ${ d . winner } /${ d . loser } : ${ String ( error ) } ` ) ;
1573+ }
1574+ }
1575+ if ( marked > 0 ) {
1576+ logger ?. info ?. ( `[dsh-mneme] dream: ledger enforce marked ${ marked } conflict decision(s), ${ t1Resolved } deterministically superseded` ) ;
1577+ }
1578+ }
1579+ const applyList = ledgerEnforce
15241580 ? decisions . filter ( ( d ) => d . action !== "conflict" )
1525- : decisions . filter ( ( d ) => d . action !== "conflict" || ! isCrossScopeDecision ( d ) ) ;
1581+ : freezeEnabled
1582+ ? decisions . filter ( ( d ) => d . action !== "conflict" )
1583+ : decisions . filter ( ( d ) => d . action !== "conflict" || ! isCrossScopeDecision ( d ) ) ;
15261584 if ( freezeEnabled ) {
15271585 const conflictsToFreeze = decisions . filter ( ( d ) => d . action === "conflict" ) ;
15281586 if ( conflictsToFreeze . length > 0 ) {
@@ -1552,9 +1610,11 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
15521610 logger ?. warn ?. ( `dsh-mneme dream: conflict freeze lookup failed: ${ String ( error ) } ` ) ;
15531611 }
15541612 }
1555- } else {
1613+ } else if ( ! ledgerEnforce ) {
15561614 // 非 freeze:跨 scope conflict 停车(绝不进 applyDecisions)。带专属
15571615 // reason + 模型给出的裁决理由,队列里读起来是「归属裁决」而非重复项。
1616+ // ledger enforce 档不进这里:conflict 决策已在上面落账本(tier 含
1617+ // cross-scope-dup),零停车零归档(#641)。
15581618 const crossScope = decisions . filter ( ( d ) => d . action === "conflict" && isCrossScopeDecision ( d ) ) ;
15591619 for ( const d of crossScope ) {
15601620 try {
@@ -1627,12 +1687,14 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
16271687 // happened and the scheduler refreshes the baseline on a false positive.
16281688 // Frozen conflicts are substantive output (parked for review), so a run
16291689 // that only froze conflicts is not a noop.
1630- const noChange = frozenCount === 0 && applied === 0 && committed . every ( ( c ) => c . action === "keep" ) ;
1690+ // Issue #641:ledger enforce 的 T1 supersede 是独立于 applyList 落地的实质产出,
1691+ // 计入「本轮有没有干活」,否则有归档产出的轮会被当 noop 刷基线(#634 同族误报)。
1692+ const noChange = frozenCount === 0 && applied === 0 && ledgerApplied === 0 && committed . every ( ( c ) => c . action === "keep" ) ;
16311693
16321694 // Keep the vector index consistent with the post-dream store state.
16331695 if ( semantic ?. embedder && semantic ?. vectorIndex ) {
16341696 try {
1635- await maintainIndexAfterDream ( applyList , service , semantic ) ;
1697+ await maintainIndexAfterDream ( ledgerVectorFixups . length ? [ ... applyList , ... ledgerVectorFixups ] : applyList , service , semantic ) ;
16361698 } catch ( error ) {
16371699 logger ?. warn ?. ( `[dsh-mneme] dream index maintenance failed: ${ String ( error ) } ` ) ;
16381700 }
0 commit comments