@@ -24,6 +24,7 @@ function createCompactionContext(params: {
2424 agentId ?: string ;
2525 initialCount : number ;
2626 info ?: ( message : string , meta ?: Record < string , unknown > ) => void ;
27+ warn ?: ( message : string , meta ?: Record < string , unknown > ) => void ;
2728 messages ?: AgentMessage [ ] ;
2829} ) : EmbeddedAgentSubscribeContext {
2930 // Minimal context preserves only the compaction counters and callbacks the
@@ -46,7 +47,7 @@ function createCompactionContext(params: {
4647 log : {
4748 debug : vi . fn ( ) ,
4849 info : params . info ?? vi . fn ( ) ,
49- warn : vi . fn ( ) ,
50+ warn : params . warn ?? vi . fn ( ) ,
5051 } ,
5152 ensureCompactionPromise : vi . fn ( ) ,
5253 noteCompactionRetry : vi . fn ( ) ,
@@ -102,14 +103,15 @@ function makeCompactionSummaryMessage(timestamp?: number): AgentMessage {
102103 } as AgentMessage ;
103104}
104105
105- function finishCompaction ( ctx : EmbeddedAgentSubscribeContext ) : void {
106- handleCompactionEnd ( ctx , {
106+ const completedCompactionEnd = ( ) =>
107+ ( {
107108 type : "compaction_end" ,
108109 reason : "threshold" ,
109- result : { kept : 12 } ,
110- willRetry : false ,
111- aborted : false ,
112- } ) ;
110+ outcome : { status : "completed" , tokensBefore : 100 , tokensAfter : 50 , willRetry : false } ,
111+ } ) as const ;
112+
113+ function finishCompaction ( ctx : EmbeddedAgentSubscribeContext ) : void {
114+ handleCompactionEnd ( ctx , completedCompactionEnd ( ) ) ;
113115}
114116
115117function loggedInfoMetaAt ( info : ReturnType < typeof vi . fn > , index : number ) : Record < string , unknown > {
@@ -199,13 +201,7 @@ describe("compaction lifecycle logging", () => {
199201 type : "compaction_start" ,
200202 reason : "threshold" ,
201203 } ) ;
202- handleCompactionEnd ( ctx , {
203- type : "compaction_end" ,
204- reason : "threshold" ,
205- result : { kept : 12 } ,
206- willRetry : false ,
207- aborted : false ,
208- } ) ;
204+ handleCompactionEnd ( ctx , completedCompactionEnd ( ) ) ;
209205
210206 expect ( loggedInfoMessageAt ( info , 0 ) ) . toBe ( "embedded run auto-compaction start" ) ;
211207 const startMeta = loggedInfoMetaAt ( info , 0 ) ;
@@ -228,7 +224,7 @@ describe("compaction lifecycle logging", () => {
228224 ) ;
229225 } ) ;
230226
231- it ( "logs manual compaction as incomplete when no result is produced " , async ( ) => {
227+ it ( "logs a benign manual skip at info " , async ( ) => {
232228 const tmp = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-compaction-incomplete-log-" ) ) ;
233229 const storePath = path . join ( tmp , "sessions.json" ) ;
234230 const sessionKey = "main" ;
@@ -252,9 +248,7 @@ describe("compaction lifecycle logging", () => {
252248 handleCompactionEnd ( ctx , {
253249 type : "compaction_end" ,
254250 reason : "manual" ,
255- result : undefined ,
256- willRetry : false ,
257- aborted : false ,
251+ outcome : { status : "skipped" , reason : "Nothing to compact (session too small)" } ,
258252 } ) ;
259253
260254 expect ( loggedInfoMessageAt ( info , 0 ) ) . toBe ( "embedded run manual compaction start" ) ;
@@ -266,19 +260,49 @@ describe("compaction lifecycle logging", () => {
266260 "embedded run manual compaction start: runId=run-test reason=manual" ,
267261 ) ;
268262
269- expect ( loggedInfoMessageAt ( info , 1 ) ) . toBe ( "embedded run manual compaction incomplete " ) ;
263+ expect ( loggedInfoMessageAt ( info , 1 ) ) . toBe ( "embedded run manual compaction skipped " ) ;
270264 const endMeta = loggedInfoMetaAt ( info , 1 ) ;
271265 expect ( endMeta . event ) . toBe ( "embedded_run_compaction_end" ) ;
272266 expect ( endMeta . reason ) . toBe ( "manual" ) ;
267+ expect ( endMeta . outcome ) . toBe ( "skipped" ) ;
273268 expect ( endMeta . runId ) . toBe ( "run-test" ) ;
274269 expect ( endMeta . completed ) . toBe ( false ) ;
275- expect ( endMeta . aborted ) . toBe ( false ) ;
270+ expect ( endMeta . reasonClass ) . toBe ( "no_compactable_entries" ) ;
276271 expect ( endMeta . consoleMessage ) . toBe (
277- "embedded run manual compaction incomplete : runId=run-test reason=manual aborted=false willRetry=false " ,
272+ "embedded run manual compaction skipped : runId=run-test reason=no_compactable_entries " ,
278273 ) ;
279274 } ) ;
280275
281- it ( "defaults legacy synthetic compaction events to threshold logs" , async ( ) => {
276+ it ( "warns with classified, bounded metadata for failed compaction" , ( ) => {
277+ const warn = vi . fn ( ) ;
278+ const ctx = createCompactionContext ( {
279+ storePath : path . join ( os . tmpdir ( ) , "openclaw-compaction-failed-log" , "sessions.json" ) ,
280+ sessionKey : "main" ,
281+ initialCount : 0 ,
282+ warn,
283+ } ) ;
284+ const reason = `Provider unavailable: ${ "provider detail " . repeat ( 100 ) } ` ;
285+
286+ handleCompactionEnd ( ctx , {
287+ type : "compaction_end" ,
288+ reason : "overflow" ,
289+ outcome : { status : "failed" , reason } ,
290+ } ) ;
291+
292+ expect ( warn ) . toHaveBeenCalledOnce ( ) ;
293+ const metadata = loggedInfoMetaAt ( warn , 0 ) ;
294+ expect ( metadata ) . toMatchObject ( {
295+ event : "embedded_run_compaction_end" ,
296+ reason : "overflow" ,
297+ outcome : "failed" ,
298+ reasonClass : "unknown" ,
299+ outcomeReason : reason ,
300+ } ) ;
301+ expect ( String ( metadata . reasonDetail ) ) . toHaveLength ( 100 ) ;
302+ expect ( String ( metadata . consoleMessage ) ) . not . toContain ( "provider detail provider detail" ) ;
303+ } ) ;
304+
305+ it ( "defaults an unknown synthetic compaction start to threshold logs" , async ( ) => {
282306 const tmp = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-compaction-legacy-log-" ) ) ;
283307 const storePath = path . join ( tmp , "sessions.json" ) ;
284308 const sessionKey = "main" ;
@@ -298,12 +322,7 @@ describe("compaction lifecycle logging", () => {
298322 handleCompactionStart ( ctx , {
299323 type : "compaction_start" ,
300324 } ) ;
301- handleCompactionEnd ( ctx , {
302- type : "compaction_end" ,
303- result : { kept : 12 } ,
304- willRetry : false ,
305- aborted : false ,
306- } ) ;
325+ handleCompactionEnd ( ctx , completedCompactionEnd ( ) ) ;
307326
308327 expect ( loggedInfoMessageAt ( info , 0 ) ) . toBe ( "embedded run auto-compaction start" ) ;
309328 const startMeta = loggedInfoMetaAt ( info , 0 ) ;
@@ -344,13 +363,7 @@ describe("handleCompactionEnd", () => {
344363 initialCount : 1 ,
345364 } ) ;
346365
347- handleCompactionEnd ( ctx , {
348- type : "compaction_end" ,
349- reason : "threshold" ,
350- result : { kept : 12 } ,
351- willRetry : false ,
352- aborted : false ,
353- } ) ;
366+ handleCompactionEnd ( ctx , completedCompactionEnd ( ) ) ;
354367
355368 await waitForCompactionCount ( {
356369 storePath,
@@ -359,7 +372,7 @@ describe("handleCompactionEnd", () => {
359372 } ) ;
360373
361374 expect ( await readCompactionCount ( storePath , sessionKey ) ) . toBe ( 2 ) ;
362- expect ( ctx . noteCompactionTokensAfter ) . toHaveBeenCalledWith ( undefined ) ;
375+ expect ( ctx . noteCompactionTokensAfter ) . toHaveBeenCalledWith ( 50 ) ;
363376 } ) ;
364377
365378 it ( "clears stale assistant usage before compaction and preserves fresh usage after it" , async ( ) => {
@@ -401,8 +414,8 @@ describe("handleCompactionEnd", () => {
401414 // A failed/aborted end never rewrote history; zeroing usage here would
402415 // disable the persistent-error compaction trigger for degraded sessions.
403416 const failureEnds = [
404- { name : "failed" , result : undefined , aborted : false } ,
405- { name : "aborted" , result : { kept : 12 } , aborted : true } ,
417+ { name : "failed" , outcome : { status : "failed" , reason : "summary failed" } } ,
418+ { name : "aborted" , outcome : { status : " aborted" } } ,
406419 ] as const ;
407420 for ( const failure of failureEnds ) {
408421 const liveUsage = makeUsageSnapshot ( 123_000 ) ;
@@ -425,9 +438,7 @@ describe("handleCompactionEnd", () => {
425438 handleCompactionEnd ( ctx , {
426439 type : "compaction_end" ,
427440 reason : "threshold" ,
428- result : failure . result ,
429- willRetry : false ,
430- aborted : failure . aborted ,
441+ outcome : failure . outcome ,
431442 } ) ;
432443
433444 const liveAssistant = messages [ 0 ] as Extract < AgentMessage , { role : "assistant" } > ;
0 commit comments