@@ -313,6 +313,45 @@ function describeStreamFailure(reason) {
313313 * operation_type, model_id, related_memory_ids); `body(reportUsage)` performs
314314 * the actual stream consumption and is handed a usage reporter for the chunks.
315315 */
316+ /**
317+ * 把喂给模型的清单包进围栏(#493 的另一半:dream / sleep 那 5 处 user 消息以前是裸放)。
318+ * 三个细节各自有原因,别当装饰删:
319+ * - 角色声明落在**开头**:判断「这段文本是什么」的是它所在的那条 user 消息,只写在 system
320+ * 里实测救不回来(#493 的 A/C 臂:只加 system 约束仍 0/2 出 JSON);
321+ * - 正文里的 `<<<` / `>>>` 用零宽空格断链——**只中和标记,不删内容**。不这么做时,一条写着
322+ * 「<<<记忆清单结束>>>」的记忆就能提前闭合围栏,把后面的文字升格成指令;而记忆正文本来就
323+ * 是模型的可影响输入,这是最省事的注入入口;
324+ * - 纯函数、不带 nonce:同一批记忆在下一次巩固阈值到来时会被原样再喂一次,重试收到的 prompt
325+ * 必须与首次逐字节相同,否则「同输入同错」这条诊断依据就没了(#493 的重试 20 次现场就靠它)。
326+ * @param {"json"|"text" } [shape] 该次调用的**输出契约**:巩固/叙述/冲突/模式要 JSON 数组,
327+ * 总览摘要要散文。角色声明里那句「只输出 JSON 数组」必须跟着契约走——对散文契约说它,
328+ * 模型会回 `[]`,而 `summaryText.trim()` 判非空通过,`[]` 就被当成摘要存进库(评审实测)。
329+ */
330+ export function fenceListBody ( language , text , shape = "json" ) {
331+ const open = STR . memoryFenceOpen [ language ] ?? STR . memoryFenceOpen . en ;
332+ const close = STR . memoryFenceClose [ language ] ?? STR . memoryFenceClose . en ;
333+ const table = shape === "text" ? STR . dreamTextRoleReminder : STR . dreamRoleReminder ;
334+ const reminder = table [ language ] ?? table . en ;
335+ const body = String ( text ?? "" ) . replace ( / < < < | > > > / g, ( run ) => `${ run [ 0 ] } \u200B${ run . slice ( 1 ) } ` ) ;
336+ return `${ reminder } \n\n${ open } \n${ body } \n${ close } ` ;
337+ }
338+
339+ /**
340+ * 解析失败行的审计读数(#493 同款,报告者提的第 2 条)。dream 的 warn 日志本来就带
341+ * raw length + 前 300 字,缺的是**可查询**的那一面:日志要翻、按不了、也进不了面板的
342+ * 「LLM 消耗」。`raw_has_bracket` 是区分「模型压根没按格式写」与「格式对但内容救不回来」
343+ * 的唯一低成本判据。
344+ */
345+ export function parseFailureMeta ( text ) {
346+ const raw = typeof text === "string" ? text : "" ;
347+ return {
348+ parse_failure : true ,
349+ raw_len : raw . length ,
350+ raw_head : raw . slice ( 0 , 300 ) . replace ( / \s + / g, " " ) . trim ( ) ,
351+ raw_has_bracket : raw . includes ( "[" )
352+ } ;
353+ }
354+
316355async function runAuditedLlm ( ctx , service , config , spec , body ) {
317356 const audit = config ?. llmAudit ;
318357 if ( audit ?. enabled === false || typeof service ?. saveLlmAudit !== "function" ) return body ( ( ) => { } ) ;
@@ -355,6 +394,17 @@ async function runAuditedLlm(ctx, service, config, spec, body) {
355394 errorMessage = String ( error ?. message ?? error ) ;
356395 throw error ;
357396 } finally {
397+ // spec.metadataFrom(result):调用方按**这一轮的原始输出**决定要不要带读数(解析成功就
398+ // 返回 null → 整键不写)。判据函数必须是纯的:它读的是 result,不读控制流里的缓存,
399+ // 否则「审计说了什么」会随调用方走到哪一行而变(sleep.js 同一条硬约定)。
400+ let metadata ;
401+ if ( result !== undefined && typeof spec . metadataFrom === "function" ) {
402+ try {
403+ metadata = spec . metadataFrom ( result ) ?? undefined ;
404+ } catch {
405+ metadata = undefined ; // 读数失败不许把审计行整个弄没
406+ }
407+ }
358408 try {
359409 service . saveLlmAudit ( {
360410 timestamp,
@@ -368,7 +418,8 @@ async function runAuditedLlm(ctx, service, config, spec, body) {
368418 duration_ms : Date . now ( ) - startedAt ,
369419 status,
370420 error_message : errorMessage ,
371- related_memory_ids : spec . relatedMemoryIds ?? [ ]
421+ related_memory_ids : spec . relatedMemoryIds ?? [ ] ,
422+ ...( metadata ? { metadata } : { } )
372423 } ) ;
373424 } catch ( auditError ) {
374425 ctx . logger ?. warn ?. ( `dsh-mneme: llm audit write failed: ${ String ( auditError ) } ` ) ;
@@ -515,7 +566,7 @@ async function generateNarratives({ ctx, service, config, route, language, effor
515566 ...( withEffort && effort ? { reasoningEffort : effort } : { } ) ,
516567 messages : [
517568 { role : "system" , content : [ { type : "text" , text : STR . prompts . narrative [ language ] } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } } ,
518- { role : "user" , content : [ { type : "text" , text : listing } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } }
569+ { role : "user" , content : [ { type : "text" , text : fenceListBody ( language , listing ) } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } }
519570 ]
520571 } , reportUsage , ( reason ) => { streamFailure = describeStreamFailure ( reason ) ; } ) ) ;
521572 } ;
@@ -1210,7 +1261,8 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
12101261 auditError : ( text ) => {
12111262 decisions = extractJsonArray ( text ) ;
12121263 return Array . isArray ( decisions ) ? null : "no json array in llm output" ;
1213- }
1264+ } ,
1265+ metadataFrom : ( text ) => ( Array . isArray ( decisions ) ? null : parseFailureMeta ( text ) )
12141266 } , ( reportUsage ) => streamText ( ctx , {
12151267 provider : route . provider ,
12161268 model : route . model ,
@@ -1219,7 +1271,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
12191271 ...( withEffort && effort ? { reasoningEffort : effort } : { } ) ,
12201272 messages : [
12211273 { role : "system" , content : [ { type : "text" , text : consolidationPrompt } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } } ,
1222- { role : "user" , content : [ { type : "text" , text : listText } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } }
1274+ { role : "user" , content : [ { type : "text" , text : fenceListBody ( language , listText ) } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } }
12231275 ]
12241276 } , reportUsage , ( reason ) => { streamFailure = describeStreamFailure ( reason ) ; } ) ) ;
12251277 } ;
@@ -1462,7 +1514,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
14621514 ...( withEffort && effort ? { reasoningEffort : effort } : { } ) ,
14631515 messages : [
14641516 { role : "system" , content : [ { type : "text" , text : STR . prompts . dreamSummary [ language ] } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } } ,
1465- { role : "user" , content : [ { type : "text" , text : summaryInputs . map ( ( m ) => `- ${ m . title } : ${ m . content } ` ) . join ( "\n" ) } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } }
1517+ { role : "user" , content : [ { type : "text" , text : fenceListBody ( language , summaryInputs . map ( ( m ) => `- ${ m . title } : ${ m . content } ` ) . join ( "\n" ) , "text ") } ] , source : { kind : "plugin:dsh-mneme" , plugin : "dsh-mneme" } }
14661518 ]
14671519 } , reportUsage , ( reason ) => { summaryStreamFailure = describeStreamFailure ( reason ) ; } ) ) ;
14681520 } ;
0 commit comments