@@ -405,6 +405,19 @@ export function createSummarizer(ctx, service, config, deps = {}) {
405405 }
406406 }
407407
408+ function persistCursor ( sessionId , nextSeq ) {
409+ if ( ! Number . isFinite ( nextSeq ) ) return ;
410+ if ( typeof service . setDistillCursor !== "function" ) {
411+ throw new Error ( "dsh-mneme: persistent summarization cursors are unavailable" ) ;
412+ }
413+ service . setDistillCursor ( sessionId , nextSeq ) ;
414+ }
415+
416+ function commitCursor ( sessionId , nextSeq ) {
417+ persistCursor ( sessionId , nextSeq ) ;
418+ if ( Number . isFinite ( nextSeq ) ) lastDistilledSeq . set ( sessionId , nextSeq ) ;
419+ }
420+
408421 /**
409422 * Issue #239(第 4 项):把这一轮蒸馏推迟到最近的「高峰结束」时刻。每会话只挂
410423 * 一个定时器(重复触发不叠加);被 summarizePeakMaxDeferMinutes 截断时到点照跑
@@ -500,7 +513,10 @@ export function createSummarizer(ctx, service, config, deps = {}) {
500513 let abortedRun = false ;
501514 try {
502515 if ( ! route ) return ;
503- const previousSeq = lastDistilledSeq . get ( session . id ) ;
516+ const persistedCursor = typeof service . getDistillCursor === "function"
517+ ? service . getDistillCursor ( session . id )
518+ : undefined ;
519+ const previousSeq = persistedCursor ?. last_seq ?? lastDistilledSeq . get ( session . id ) ;
504520 const triggerSeq = eventSeq ( triggerEvent ) ;
505521 // 只读取上次成功游标之后、当前 turn/end 之前的事件。旧版 snapshotEvents
506522 // 即使忽略范围参数,collectMessages 仍会按事件 seq 二次过滤。
@@ -519,7 +535,7 @@ export function createSummarizer(ctx, service, config, deps = {}) {
519535 if ( ! collected . messages . length ) {
520536 // 没有可蒸馏的公开文本也算成功消费当前事件窗口,避免每个 turn/end
521537 // 都重新扫描同一批无内容事件;没有 seq 时则不提交不可验证的游标。
522- if ( Number . isFinite ( nextSeq ) ) lastDistilledSeq . set ( session . id , nextSeq ) ;
538+ commitCursor ( session . id , nextSeq ) ;
523539 return ;
524540 }
525541 const messages = collected . messages ;
@@ -536,7 +552,7 @@ export function createSummarizer(ctx, service, config, deps = {}) {
536552 0
537553 ) ;
538554 if ( distillChars < minWindowChars ) {
539- if ( Number . isFinite ( nextSeq ) ) lastDistilledSeq . set ( session . id , nextSeq ) ;
555+ commitCursor ( session . id , nextSeq ) ;
540556 writeAudit ( {
541557 timestamp : new Date ( ) . toISOString ( ) ,
542558 model_id : route ? `${ route . provider } :${ route . model } ` : "unknown" ,
@@ -712,7 +728,11 @@ export function createSummarizer(ctx, service, config, deps = {}) {
712728 ...( dup ? { _mergeInto : dup . memory . id } : { } )
713729 } ) ;
714730 }
731+ persistCursor ( session . id , nextSeq ) ;
715732 } ) ;
733+ } else {
734+ // 空数组是合法成功:没有记忆写入,但本次事件窗口仍然应被持久消费。
735+ persistCursor ( session . id , nextSeq ) ;
716736 }
717737 if ( audit && ( capped > 0 || deduped > 0 ) ) {
718738 audit . metadata = {
@@ -724,6 +744,12 @@ export function createSummarizer(ctx, service, config, deps = {}) {
724744 // 记忆写入和解析都成功后才提交窗口;流失败、中止、解析失败或写入异常
725745 // 都会在此之前退出,从而保留窗口供下一次重试。
726746 if ( Number . isFinite ( nextSeq ) ) lastDistilledSeq . set ( session . id , nextSeq ) ;
747+ } catch ( error ) {
748+ if ( audit ) {
749+ audit . status = "error" ;
750+ audit . errorMessage = String ( error ?. message ?? error ) ;
751+ }
752+ throw error ;
727753 } finally {
728754 // Issue #127:aborted(会话关闭 / 插件 dispose)不占间隔,避免误伤该会话的
729755 // 下一次蒸馏;其余情况(含失败)的打点保留,与 dreamMinIntervalMinutes 一致。
0 commit comments