@@ -404,12 +404,29 @@ function collectMessages(session, maxChars = 8000, language = "zh", afterSeq, th
404404 if ( seq === undefined ) return max ;
405405 return max === undefined ? seq : Math . max ( max , seq ) ;
406406 } , undefined ) ;
407+ const transcriptBody = trimTranscript ( lines . join ( "\n" ) , maxChars ) ;
407408 return {
408409 messages : lines . length
409- ? [ createUserMessage ( { content : [ { type : "text" , text : trimTranscript ( lines . join ( "\n" ) , maxChars ) } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } } ) ]
410+ ? [ createUserMessage ( {
411+ content : [ {
412+ type : "text" ,
413+ // Issue #493:转录裸放 user 位时,模型把它当成「轮到我说了」接着对话
414+ // 往下写,输出里连 `[` 都没有(解析器在括号早退处直接判失败)。围栏 +
415+ // 角色重申把「这段文本是被提炼的素材」写进**它所在的那条消息**,而不是
416+ // 只写在 system 里——实测同段转录只加 system 约束仍 0/2 出 JSON。
417+ // 围栏是确定性的:重试收到的转录与首次逐字节相同(既有回归用例依赖)。
418+ text : `${ STR . distillRoleReminder [ language ] } \n\n${ STR . transcriptFenceOpen [ language ] } \n${ transcriptBody } \n${ STR . transcriptFenceClose [ language ] } `
419+ } ] ,
420+ source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" }
421+ } ) ]
410422 : [ ] ,
411423 hasEvents : events . length > 0 ,
412- lastSeq
424+ lastSeq,
425+ // Issue #493:只数**转录正文**的字符数。minWindowChars 闸门与审计里的
426+ // distillChars 量的都是「这一窗有多少可提炼内容」,不是「我们额外包了多少
427+ // 提示词」——若把围栏与角色句算进去,一条「短」也会凭空越过任何阈值,
428+ // 闸门随之失效(实测 summarizeMinWindowChars=10 会被 4 字正文 + 包裹放行)。
429+ transcriptChars : lines . length ? transcriptBody . length : 0
413430 } ;
414431}
415432
@@ -534,6 +551,10 @@ export function createSummarizer(ctx, service, config, deps = {}) {
534551 const deferredRuns = new Map ( ) ;
535552 // Issue #210:只在一次蒸馏完整成功后记录已消费的最后事件 seq。
536553 const lastDistilledSeq = new Map ( ) ;
554+ // Issue #493:per-session 连续解析失败计数(失败护栏用)。同一窗口在确定性
555+ // 失败下会被每个 turn/end 重试一次、且窗口只会越攒越大——没有出口。成功即
556+ // 清零;dispose 一并清理(与 runsUsed / lastRunAt 同生命周期)。
557+ const parseFailureStreak = new Map ( ) ;
537558 let disposed = false ;
538559
539560 /** 写一条 autoSummarize 的 LLM 审计行(审计失败绝不阻塞蒸馏本身)。 */
@@ -701,7 +722,12 @@ export function createSummarizer(ctx, service, config, deps = {}) {
701722 // summarize_compress 成功行的 metadata 里窗口字符数为 0 条),验收 4「默认配置
702723 // 下调用量可量化下降」因此无法对账。本改动只加计量,不动任何跳过决策——
703724 // summarizeMinWindowChars 仍为 0 时行为逐字节不变。
704- const distillChars = messages . reduce (
725+ //
726+ // Issue #493:口径改为只数转录正文(collected.transcriptChars),不再把
727+ // user 消息整段长度当窗口大小——#493 给转录加了围栏与角色句,若继续按
728+ // 「消息总字符数」算,一条 4 字的「短」也会被包裹撑过任何阈值,闸门等于
729+ // 被自己的提示词旁路掉。
730+ const distillChars = collected . transcriptChars ?? messages . reduce (
705731 ( total , message ) => total + ( Array . isArray ( message . content ) ? message . content : [ ] )
706732 . reduce ( ( n , part ) => n + ( typeof part ?. text === "string" ? part . text . length : 0 ) , 0 ) ,
707733 0
@@ -869,12 +895,46 @@ export function createSummarizer(ctx, service, config, deps = {}) {
869895 const parsedResult = parseSummaryJsonResult ( text || assembledText ) ;
870896 // 解析失败不推进 seq 游标,下一次 turn/end 仍会重试同一窗口。
871897 if ( ! parsedResult . ok ) {
898+ // Issue #493:失败也要留证据。原先 metadata 只在成功路径赋值,失败行恒为
899+ // null —— 事后无法回答「模型到底吐了什么形状」。这里只加计量、不做任何
900+ // 跳过决策(raw_head 截断到 300 字符,避免把审计表撑大)。
901+ const raw = text || assembledText || "" ;
902+ const streak = ( parseFailureStreak . get ( session . id ) ?? 0 ) + 1 ;
903+ parseFailureStreak . set ( session . id , streak ) ;
904+ // Issue #493 失败护栏:同一窗口连续失败到阈值后强制消费游标跳过它。
905+ // 确定性失败(finish_reason=stop、同文同温)重试收益为 0,而窗口只会随
906+ // 新事件继续变大——没有出口时它会一直失败下去。默认 0 = 关闭,行为与
907+ // 历史版本逐字节一致(本仓新行为一律 opt-in,见 AGENTS.md)。
908+ const maxParseRetries = config . summarizeParseFailureMaxRetries ?? 0 ;
909+ // nextSeq 必须是有限数才算真的「跳过了窗口」:没有可验证的 seq 时
910+ // commitCursor 是空操作(persistCursor 早退、内存游标也不写),此时若
911+ // 仍按跳过记账并清零 streak,审计会宣称「已放弃」而窗口下一轮照旧重试
912+ // —— 那是最坏的一种假象:用户以为止血了,实际还在烧。
913+ const skipWindow = maxParseRetries > 0 && streak >= maxParseRetries && Number . isFinite ( nextSeq ) ;
914+ if ( skipWindow ) {
915+ // 先提交游标再复位计数:窗口已放弃,下一窗从全新状态起算。
916+ commitCursor ( session . id , nextSeq ) ;
917+ parseFailureStreak . delete ( session . id ) ;
918+ }
872919 if ( audit ) {
873920 audit . status = "error" ;
874921 audit . errorMessage = "invalid summary JSON" ;
922+ audit . metadata = {
923+ distillChars,
924+ parse_failure : true ,
925+ raw_len : raw . length ,
926+ raw_head : raw . slice ( 0 , 300 ) ,
927+ // 有没有出现过 `[` 是区分「模型没按格式写」与「格式对但内容救不回来」
928+ // 的唯一低成本判据,也正是 #493 现场那 20 条失败的关键特征。
929+ raw_has_bracket : raw . includes ( "[" ) ,
930+ streak,
931+ ...( maxParseRetries > 0 ? { maxRetries : maxParseRetries } : { } ) ,
932+ ...( skipWindow ? { skipped : "parse-failure-streak" } : { } )
933+ } ;
875934 }
876935 return ;
877936 }
937+ parseFailureStreak . delete ( session . id ) ;
878938 const parsed = parsedResult . entries ;
879939 // Issue #127 条数上限:0 = 不限(现状)。被截断的条数与去重命中数一并进
880940 // 审计 metadata——此前产出条数完全由模型输出决定,用户无从自查。
@@ -1064,6 +1124,7 @@ export function createSummarizer(ctx, service, config, deps = {}) {
10641124 inFlight . clear ( ) ;
10651125 lastRunAt . clear ( ) ;
10661126 lastDistilledSeq . clear ( ) ;
1127+ parseFailureStreak . clear ( ) ;
10671128 }
10681129 } ;
10691130}
0 commit comments