@@ -49,12 +49,22 @@ function parseJsonArray(text) {
4949 }
5050}
5151
52- /** Same stream consumption contract as dream.js. */
53- async function streamText ( ctx , options , onStreamError ) {
52+ /**
53+ * Same stream consumption contract as dream.js.
54+ *
55+ * `onUsage`(optional, issue #250)receives any usage chunk for token accounting.
56+ * dream.js 那份一直有它;sleep 这份副本没有,导致 conflict / pattern 两条链路的
57+ * token 从未进 llm_audit_logs(面板因此只看到 autoDream 与 autoSummarize)。
58+ */
59+ async function streamText ( ctx , options , onUsage , onStreamError ) {
5460 if ( ! ctx ?. llm ?. stream ) return undefined ;
5561 let text = "" ;
5662 for await ( const chunk of ctx . llm . stream ( options ) ) {
5763 if ( chunk . type === "text-delta" && typeof chunk . text === "string" ) text += chunk . text ;
64+ // DSH 的 StreamChunk 契约把用量嵌在 chunk.usage(TokenUsage:inputTokens /
65+ // outputTokens / totalTokens);兼容直接平铺在 chunk 上的旧形态。归一放在
66+ // 这里,调用侧的 reporter 只需认平铺形状——与 dream.js:239 同一处理。
67+ if ( chunk . type === "usage" && typeof onUsage === "function" ) onUsage ( chunk . usage ?? chunk ) ;
5868 if ( chunk . type === "finish" && ( chunk . reason ?. kind === "error" || chunk . reason ?. kind === "aborted" ) ) {
5969 // Same rc.1 error-as-finish-chunk behavior as dream.js — surface the
6070 // cause instead of discarding it.
@@ -67,6 +77,79 @@ async function streamText(ctx, options, onStreamError) {
6777 return text ;
6878}
6979
80+ /**
81+ * Issue #250:给 sleep 的一条 LLM 调用记一笔 llm_audit_logs(token / 时长 / 状态 /
82+ * 触发源)。与 dream.js 的 runAuditedLlm 同口径但各自独立一份——dream 那两个
83+ * checker 的调用形状绑定着它自己的排程细节,硬抽共享抽象只会收敛回 dream 的形态。
84+ *
85+ * 记账是 best-effort:写审计行失败只 warn,绝不反噬 sleep 本体(CONTRIBUTING 的
86+ * fail-safe 硬约定)。`body(reportUsage)` 负责真实消费流,usage chunk 经
87+ * reportUsage 上报。
88+ *
89+ * 审计诚实性:流被中止/报错时 body 返回 undefined,记 status='error';流式成功但
90+ * 输出不可用时 `spec.auditError` 给出原因,同样记 error——否则同一轮调用会在
91+ * 「phase 报 failed」与「audit 报 success」之间自相矛盾(CONTRIBUTING 的审计诚实性
92+ * 硬约定)。`auditError` 必须是**纯判据函数**:调用方的控制流不能依赖它的副作用,
93+ * 因为审计关闭时这里整段早退、钩子根本不会被调用(dream 正是那样踩的,见 PR 正文)。
94+ */
95+ async function auditedSleepLlm ( ctx , service , config , logger , spec , body ) {
96+ if ( config ?. llmAudit ?. enabled === false || typeof service ?. saveLlmAudit !== "function" ) {
97+ return body ( ( ) => { } ) ;
98+ }
99+ const startedAt = Date . now ( ) ;
100+ const timestamp = new Date ( startedAt ) . toISOString ( ) ;
101+ let inputTokens = 0 ;
102+ let outputTokens = 0 ;
103+ let status = "success" ;
104+ let errorMessage = null ;
105+ try {
106+ const result = await body ( ( usage ) => {
107+ if ( ! usage ) return ;
108+ const i = usage . input_tokens ?? usage . inputTokens ?? usage . prompt_tokens ?? usage . promptTokens ;
109+ const o = usage . output_tokens ?? usage . outputTokens ?? usage . completion_tokens ?? usage . completionTokens ;
110+ if ( Number . isFinite ( i ) ) inputTokens = i ;
111+ if ( Number . isFinite ( o ) ) outputTokens = o ;
112+ } ) ;
113+ if ( result === undefined ) {
114+ status = "error" ;
115+ const streamErr = typeof spec . streamError === "function" ? String ( spec . streamError ( ) ?? "" ) : "" ;
116+ errorMessage = streamErr ? `llm stream aborted or errored (${ streamErr } )` : "llm stream aborted or errored" ;
117+ } else if ( typeof spec . auditError === "function" ) {
118+ // 流式成功但输出不可用,仍然是一次失败的调用——记 error,不与调用方的
119+ // failed 返回值打架。
120+ const message = spec . auditError ( result ) ;
121+ if ( message ) {
122+ status = "error" ;
123+ errorMessage = message ;
124+ }
125+ }
126+ return result ;
127+ } catch ( error ) {
128+ status = "error" ;
129+ errorMessage = String ( error ?. message ?? error ) ;
130+ throw error ;
131+ } finally {
132+ try {
133+ service . saveLlmAudit ( {
134+ timestamp,
135+ trigger_source : spec . triggerSource ,
136+ operation_type : spec . operationType ,
137+ model_id : spec . modelId ,
138+ input_tokens : inputTokens ,
139+ output_tokens : outputTokens ,
140+ total_tokens : inputTokens + outputTokens ,
141+ cost_usd : 0 ,
142+ duration_ms : Date . now ( ) - startedAt ,
143+ status,
144+ error_message : errorMessage ,
145+ related_memory_ids : spec . relatedMemoryIds ?? [ ]
146+ } ) ;
147+ } catch ( auditError ) {
148+ logger ?. warn ?. ( `dsh-mneme sleep: llm audit write failed: ${ String ( auditError ) } ` ) ;
149+ }
150+ }
151+ }
152+
70153/** LLM route (Issue #25): explicit sleepProvider/Model wins, then the dream
71154 * route as a shared explicit fallback, then the agent default model. Sleep
72155 * can pin a cheaper model for its bulk passes without disturbing the dream
@@ -232,7 +315,19 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
232315 const conflictPrompt = actionSet === "full" ? STR . prompts . conflictFull [ language ] : STR . prompts . conflict [ language ] ;
233316 const runConflict = ( withEffort ) => {
234317 conflictStreamFailure = "" ;
235- return streamText ( ctx , {
318+ return auditedSleepLlm ( ctx , service , config , logger , {
319+ triggerSource : "sleep" ,
320+ operationType : "sleep_conflict" ,
321+ modelId : `${ route . provider } :${ route . model } ` ,
322+ // 候选对的双方 id:面板的活动流用 related_memory_ids 解析「沉淀条数」。
323+ relatedMemoryIds : samePairs . flatMap ( ( p ) => [ p . a . id , p . b . id ] ) ,
324+ streamError : ( ) => conflictStreamFailure ,
325+ // 纯判据:输出里没有 JSON 数组 ⇒ 这轮是失败的调用,审计记 error。**不要**把
326+ // 解析结果存进闭包给下面的控制流用——审计关闭时 auditedSleepLlm 整段早退、
327+ // 这个钩子不会被调用,控制流会跟着失效(dream 的既有坑)。代价是审计开启时
328+ // 多解析一次,换来两条路径彻底解耦。
329+ auditError : ( text ) => ( parseJsonArray ( text ) ? null : "no json array in llm output" )
330+ } , ( reportUsage ) => streamText ( ctx , {
236331 provider : route . provider ,
237332 model : route . model ,
238333 purpose : "sleep-conflict" ,
@@ -242,7 +337,7 @@ async function phaseConflicts(ctx, service, config, logger, runId, semantic = nu
242337 { role : "system" , content : [ { type : "text" , text : conflictPrompt } ] , source : { kind : "plugin" , plugin : "dsh-mneme" } } ,
243338 { role : "user" , content : [ { type : "text" , text : listText } ] , source : { kind : "plugin" , plugin : "dsh-mneme" } }
244339 ]
245- } , ( reason ) => { conflictStreamFailure = describeStreamFailure ( reason ) ; } ) ;
340+ } , reportUsage , ( reason ) => { conflictStreamFailure = describeStreamFailure ( reason ) ; } ) ) ;
246341 } ;
247342 const text = await withEffortFallback ( ctx , sleepEffort , ( ) => runConflict ( true ) , ( ) => runConflict ( false ) , ( ) => conflictStreamFailure ) ;
248343 if ( text === undefined ) {
@@ -385,7 +480,16 @@ async function phasePatterns(ctx, service, config, logger, runId, signal = null)
385480 let patternStreamFailure = "" ;
386481 const runPattern = ( withEffort ) => {
387482 patternStreamFailure = "" ;
388- return streamText ( ctx , {
483+ return auditedSleepLlm ( ctx , service , config , logger , {
484+ triggerSource : "sleep" ,
485+ operationType : "sleep_pattern" ,
486+ modelId : `${ route . provider } :${ route . model } ` ,
487+ // 本轮扫描的记忆 id:与 dream 的 relatedMemoryIds 同义,供面板解析沉淀条数。
488+ relatedMemoryIds : memories . map ( ( m ) => m . id ) ,
489+ streamError : ( ) => patternStreamFailure ,
490+ // 纯判据,同 conflict 阶段:不把解析结果存进闭包供控制流使用。
491+ auditError : ( text ) => ( parseJsonArray ( text ) ? null : "no json array in llm output" )
492+ } , ( reportUsage ) => streamText ( ctx , {
389493 provider : route . provider ,
390494 model : route . model ,
391495 purpose : "sleep-pattern" ,
@@ -395,7 +499,7 @@ async function phasePatterns(ctx, service, config, logger, runId, signal = null)
395499 { role : "system" , content : [ { type : "text" , text : STR . prompts . pattern [ language ] . replace ( "N" , String ( maxPatterns ) ) } ] , source : { kind : "plugin" , plugin : "dsh-mneme" } } ,
396500 { role : "user" , content : [ { type : "text" , text : listText } ] , source : { kind : "plugin" , plugin : "dsh-mneme" } }
397501 ]
398- } , ( reason ) => { patternStreamFailure = describeStreamFailure ( reason ) ; } ) ;
502+ } , reportUsage , ( reason ) => { patternStreamFailure = describeStreamFailure ( reason ) ; } ) ) ;
399503 } ;
400504 const text = await withEffortFallback ( ctx , sleepEffort , ( ) => runPattern ( true ) , ( ) => runPattern ( false ) , ( ) => patternStreamFailure ) ;
401505 if ( text === undefined ) {
0 commit comments