@@ -52,6 +52,26 @@ function totalChars(memories) {
5252 return memories . reduce ( ( sum , m ) => sum + ( m . title ?. length ?? 0 ) + ( m . content ?. length ?? 0 ) , 0 ) ;
5353}
5454
55+ // 阈值口径的唯一实现:活跃记忆(未归档、非 summary/document)的条数与正文字符数。
56+ // 触发判定(shouldTrigger)与「成功轮把基线落库」(runDream 的 finish)都走这里——
57+ // 两处若各写一份过滤条件,重启播种读到的基线就会和运行期判定口径漂移(一个把
58+ // document 算进去、一个不算),阈值随之失真,且这种漂移只会在重启后才显形。
59+ function activeStoreSize ( service ) {
60+ const memories = service . all ( ) . filter ( ( m ) => ! m . archived && m . type !== "summary" && m . type !== "document" ) ;
61+ return { count : memories . length , chars : totalChars ( memories ) } ;
62+ }
63+
64+ // 基线的唯一合法形状:两个非负整数;其余(缺字段、负数、非整数、整个对象缺失)
65+ // 一律视为「没有基线」→ 调用方退回 {0,0}。播种值来自持久化列,可能是老库的 NULL
66+ // 或手工改坏的库,判定必须比调用点更严格,而不是相信数据库里存的一定合法。
67+ function toBaseline ( raw ) {
68+ if ( ! raw || typeof raw !== "object" ) return null ;
69+ const count = Number . isInteger ( raw . count ) && raw . count >= 0 ? raw . count : null ;
70+ const chars = Number . isInteger ( raw . chars ) && raw . chars >= 0 ? raw . chars : null ;
71+ if ( count === null || chars === null ) return null ;
72+ return { count, chars } ;
73+ }
74+
5575// ---------------------------------------------------------------- audit
5676
5777/**
@@ -760,15 +780,21 @@ export async function maintainIndexAfterDream(decisions, service, semantic) {
760780// 用户配得比这更大的 dreamMinIntervalMinutes 基数压小(见 effectiveMinIntervalMs)。
761781const FAILURE_BACKOFF_CAP_MS = 30 * 60 * 1000 ;
762782
763- export function createDreamScheduler ( { onRun, thresholdCount = 10 , thresholdChars = 5000 , delayMs = 2000 , minIntervalMs = 0 , failureBackoff = false , logger, semantic = null , lastRunAtSeed = 0 , peakHours = "" , peakMaxDeferMinutes = 120 , auditPeakSkip = null , now = ( ) => Date . now ( ) , setTimeoutFn = setTimeout , clearTimeoutFn = clearTimeout } ) {
783+ export function createDreamScheduler ( { onRun, thresholdCount = 10 , thresholdChars = 5000 , delayMs = 2000 , minIntervalMs = 0 , failureBackoff = false , logger, semantic = null , lastRunAtSeed = 0 , baselineSeed = null , peakHours = "" , peakMaxDeferMinutes = 120 , auditPeakSkip = null , now = ( ) => Date . now ( ) , setTimeoutFn = setTimeout , clearTimeoutFn = clearTimeout } ) {
764784 let pendingTimer = null ;
765785 // Issue #239(第 4 项)镜像到巩固:高峰顺延定时器。与 pendingTimer 分开——两者
766786 // 语义不同(一个是「马上要跑」,一个是「等出高峰再跑」),合成一个变量会让
767787 // maybeSchedule 的守卫在顺延期间把新的写入触发误当成「已有待跑」而吞掉。
768788 let deferTimer = null ;
769789 let running = false ;
770790 let disposed = false ;
771- let baseline = { count : 0 , chars : 0 } ;
791+ // Issue #89(基线半边):阈值基线跨重启持久化。baselineSeed 由调用方从
792+ // dream_runs 读上次成功轮结束时的库规模(store.lastDreamBaseline)——它只活在
793+ // 闭包内存里时进程重启即归零,而库里记忆远多于阈值,重启后第一条写入就满足
794+ // overBase、绕过阈值开跑整轮(#291 只补了 lastRunAt 那半边)。缺种子(新库、
795+ // 升级后还没跑过成功轮)或种子非法时退回 {0,0}:不假装知道基线,行为与升级前
796+ // 逐字节一致——修的是「知道却不认」,不是凭空造一个基线。
797+ let baseline = toBaseline ( baselineSeed ) ?? { count : 0 , chars : 0 } ;
772798 let inFlight = null ;
773799 // Issue #89(请求 2):上一次实际开跑时刻。失败/degraded 的 run 也占用
774800 // 最小间隔——节流的目的正是防止失败调用连发;间隔内的触发静默跳过。
@@ -792,9 +818,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
792818 }
793819
794820 function shouldTrigger ( service ) {
795- const memories = service . all ( ) . filter ( ( m ) => ! m . archived && m . type !== "summary" && m . type !== "document" ) ;
796- const count = memories . length ;
797- const chars = totalChars ( memories ) ;
821+ const { count, chars } = activeStoreSize ( service ) ;
798822 const overBase = count >= baseline . count + thresholdCount || chars >= baseline . chars + thresholdChars ;
799823 const overAbs = count >= thresholdCount || chars >= thresholdChars ;
800824 return { trigger : overAbs && overBase , count, chars } ;
@@ -881,7 +905,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
881905 if ( result && result . ok ) {
882906 consecutiveFailures = 0 ; // Issue #292:成功清零,下次触发回到基数间隔
883907 try {
884- baseline = shouldTrigger ( service ) ;
908+ // Issue #89(基线半边):run 自报的基线(与审计行同一份数字)优先——重启
909+ // 播种读的就是它,两条路径共用唯一口径;no-op 桩不回这个字段时退回现场
910+ // 计算,最小测试替身的行为逐字节不变。
911+ baseline = toBaseline ( result . baseline ) ?? shouldTrigger ( service ) ;
885912 } catch ( error ) {
886913 // Store closed mid-flight: keep the last known baseline.
887914 logger ?. warn ?. ( `dsh-mneme dream: baseline refresh failed: ${ String ( error ) } ` ) ;
@@ -986,6 +1013,19 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
9861013 const applied = result . applied ?? 0 ;
9871014 const summaryStored = result . summary ?? false ;
9881015 const receipt = buildReceipt ( { runId, status, snapshotHash, inputCount : snapshot . size , applied, summaryStored } ) ;
1016+ // Issue #89(基线半边):成功轮把「本轮结束后的库规模」落进审计行 = 阈值基线的
1017+ // 持久化副本,重启后由 store.lastDreamBaseline 播种。判定条件必须与调度器刷新
1018+ // 基线的条件严格一致(result.ok)——非 ok 轮不推进基线是既有语义,若这里也落值,
1019+ // 重启后播种就会拿到一个「其实没推进过」的假基线,阈值反而被抬高。
1020+ // 取不到(库已关闭等)就落 NULL:宁可不持久化,也不写一个错的基线。
1021+ let baselineSnapshot = null ;
1022+ if ( result . ok ) {
1023+ try {
1024+ baselineSnapshot = activeStoreSize ( service ) ;
1025+ } catch {
1026+ baselineSnapshot = null ;
1027+ }
1028+ }
9891029 try {
9901030 service . saveDreamRun ( {
9911031 id : runId ,
@@ -1023,12 +1063,18 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
10231063 // 而非内联进 decisions——degraded 的 decisions 是合法子集,下游按「决策
10241064 // 数组」消费,内联标记会污染其它读者(失败轮的 _validationFailed 是整单
10251065 // 拒绝哨兵,语义与「部分应用」不同,也不复用)。
1026- skipped : result . skipped
1066+ skipped : result . skipped ,
1067+ // Issue #89(基线半边):成功的 run 才带这两个值(见上方 baselineSnapshot
1068+ // 的注释)。两列要么同时有值、要么同时为 NULL,读侧以「同时有值」认账。
1069+ store_count : baselineSnapshot ?. count ,
1070+ store_chars : baselineSnapshot ?. chars
10271071 } ) ;
10281072 } catch ( error ) {
10291073 logger ?. warn ?. ( `dsh-mneme dream: failed to record audit run: ${ String ( error ) } ` ) ;
10301074 }
1031- return { ...result , runId, receipt, snapshotHash } ;
1075+ // Issue #89(基线半边):把「与审计行同一份数字」的基线回给调度器,两条路径
1076+ // (进程内刷新 / 跨重启播种)因此共享唯一口径;调度器拿不到时会退出现场计算。
1077+ return { ...result , runId, receipt, snapshotHash, baseline : baselineSnapshot ?? undefined } ;
10321078 } ;
10331079 if ( ! route ) {
10341080 logger ?. warn ?. ( "dsh-mneme dream: no llm route available" ) ;
0 commit comments