@@ -23,7 +23,12 @@ const state = vi.hoisted(() => ({
2323 normalizeProviderModelIdWithRuntimeMock : vi . fn (
2424 ( _params : ProviderModelNormalizationParams ) => undefined ,
2525 ) ,
26+ runCliTurnCompactionLifecycleMock : vi . fn (
27+ async ( params : { sessionEntry ?: SessionEntry } ) => params . sessionEntry ,
28+ ) ,
29+ emitAgentEventMock : vi . fn ( ) ,
2630 deliveryFreshEntries : [ ] as Array < SessionEntry | undefined > ,
31+ deliveryCalls : 0 ,
2732} ) ) ;
2833
2934vi . mock ( "../config/io.js" , ( ) => ( {
@@ -144,14 +149,28 @@ vi.mock("./command/attempt-execution.runtime.js", async () => {
144149} ) ;
145150
146151vi . mock ( "./command/cli-compaction.js" , ( ) => ( {
147- runCliTurnCompactionLifecycle : async ( params : { sessionEntry ?: SessionEntry } ) =>
148- params . sessionEntry ,
152+ runCliTurnCompactionLifecycle : ( params : { sessionEntry ?: SessionEntry } ) =>
153+ state . runCliTurnCompactionLifecycleMock ( params ) ,
149154} ) ) ;
150155
156+ vi . mock ( "../infra/agent-events.js" , async ( ) => {
157+ const actual = await vi . importActual < typeof import ( "../infra/agent-events.js" ) > (
158+ "../infra/agent-events.js" ,
159+ ) ;
160+ return {
161+ ...actual ,
162+ emitAgentEvent : ( ...args : Parameters < typeof actual . emitAgentEvent > ) => {
163+ state . emitAgentEventMock ( ...args ) ;
164+ return actual . emitAgentEvent ( ...args ) ;
165+ } ,
166+ } ;
167+ } ) ;
168+
151169vi . mock ( "./command/delivery.runtime.js" , ( ) => ( {
152170 deliverAgentCommandResult : async ( params : {
153171 resolveFreshSessionEntryForDelivery ?: ( ) => Promise < SessionEntry | undefined > ;
154172 } ) => {
173+ state . deliveryCalls += 1 ;
155174 state . deliveryFreshEntries . push ( await params . resolveFreshSessionEntryForDelivery ?.( ) ) ;
156175 return { deliverySucceeded : true } ;
157176 } ,
@@ -167,7 +186,11 @@ beforeEach(async () => {
167186 vi . clearAllMocks ( ) ;
168187 state . loadManifestModelCatalogMock . mockReturnValue ( [ ] ) ;
169188 state . normalizeProviderModelIdWithRuntimeMock . mockImplementation ( ( ) => undefined ) ;
189+ state . runCliTurnCompactionLifecycleMock . mockImplementation (
190+ async ( params : { sessionEntry ?: SessionEntry } ) => params . sessionEntry ,
191+ ) ;
170192 state . deliveryFreshEntries = [ ] ;
193+ state . deliveryCalls = 0 ;
171194 const tmpDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-rotation-e2e-" ) ) ;
172195 state . workspaceDir = path . join ( tmpDir , "workspace" ) ;
173196 state . agentDir = path . join ( tmpDir , "agent" ) ;
@@ -353,6 +376,119 @@ describe("agentCommand compaction transcript rotation", () => {
353376 ] ) ;
354377 } ) ;
355378
379+ it ( "delivers the already-generated reply when post-turn compaction fails" , async ( ) => {
380+ state . runCliTurnCompactionLifecycleMock . mockRejectedValueOnce (
381+ new Error (
382+ "CLI transcript compaction failed for openai/gpt-5.5: Summarization failed: Connection error." ,
383+ ) ,
384+ ) ;
385+ state . runAgentAttemptMock . mockResolvedValueOnce (
386+ makeResult ( {
387+ sessionId : "yuyu-home" ,
388+ text : "reply generated before compaction failed" ,
389+ } ) ,
390+ ) ;
391+
392+ const result = await agentCommand ( {
393+ message : "room message" ,
394+ sessionId : "yuyu-home" ,
395+ cwd : state . workspaceDir ,
396+ deliver : true ,
397+ } ) ;
398+
399+ expect ( state . runCliTurnCompactionLifecycleMock ) . toHaveBeenCalledTimes ( 1 ) ;
400+ expect ( state . deliveryCalls ) . toBe ( 1 ) ;
401+ expect ( result ) . toMatchObject ( { deliverySucceeded : true } ) ;
402+
403+ // The turn must settle as a normal lifecycle "end", never a post-turn
404+ // "error": a swallowed compaction failure cannot reclassify a delivered turn.
405+ const lifecyclePhases = state . emitAgentEventMock . mock . calls
406+ . map ( ( [ event ] ) => event as { stream ?: string ; data ?: { phase ?: string } } )
407+ . filter ( ( event ) => event ?. stream === "lifecycle" )
408+ . map ( ( event ) => event . data ?. phase ) ;
409+ expect ( lifecyclePhases ) . toContain ( "end" ) ;
410+ expect ( lifecyclePhases ) . not . toContain ( "error" ) ;
411+ } ) ;
412+
413+ it ( "delivers a media-only reply (no assistant text) when post-turn compaction fails" , async ( ) => {
414+ state . runCliTurnCompactionLifecycleMock . mockRejectedValueOnce (
415+ new Error (
416+ "CLI transcript compaction failed for openai/gpt-5.5: Summarization failed: Connection error." ,
417+ ) ,
418+ ) ;
419+ state . runAgentAttemptMock . mockResolvedValueOnce ( {
420+ payloads : [ { mediaUrls : [ "https://example.com/generated.png" ] } ] ,
421+ meta : {
422+ durationMs : 1 ,
423+ stopReason : "end_turn" ,
424+ executionTrace : {
425+ runner : "cli" as const ,
426+ fallbackUsed : false ,
427+ winnerProvider : "openai" ,
428+ winnerModel : "gpt-5.5" ,
429+ } ,
430+ agentMeta : {
431+ sessionId : "media-only-session" ,
432+ provider : "openai" ,
433+ model : "gpt-5.5" ,
434+ } ,
435+ } ,
436+ } ) ;
437+
438+ const result = await agentCommand ( {
439+ message : "generate an image" ,
440+ sessionId : "media-only-session" ,
441+ cwd : state . workspaceDir ,
442+ deliver : true ,
443+ } ) ;
444+
445+ // A media reply has no assistant text but is still deliverable, so the
446+ // compaction failure must not discard it.
447+ expect ( state . runCliTurnCompactionLifecycleMock ) . toHaveBeenCalledTimes ( 1 ) ;
448+ expect ( state . deliveryCalls ) . toBe ( 1 ) ;
449+ expect ( result ) . toMatchObject ( { deliverySucceeded : true } ) ;
450+ } ) ;
451+
452+ it ( "keeps a no-reply turn's compaction failure fatal instead of an empty success" , async ( ) => {
453+ const noReplyResult : EmbeddedAgentRunResult = {
454+ payloads : [ ] ,
455+ meta : {
456+ durationMs : 1 ,
457+ stopReason : "end_turn" ,
458+ executionTrace : {
459+ runner : "cli" as const ,
460+ fallbackUsed : false ,
461+ winnerProvider : "openai" ,
462+ winnerModel : "gpt-5.5" ,
463+ } ,
464+ agentMeta : {
465+ sessionId : "no-reply-session" ,
466+ provider : "openai" ,
467+ model : "gpt-5.5" ,
468+ } ,
469+ } ,
470+ } ;
471+ state . runCliTurnCompactionLifecycleMock . mockRejectedValueOnce (
472+ new Error (
473+ "CLI transcript compaction failed for openai/gpt-5.5: Summarization failed: Connection error." ,
474+ ) ,
475+ ) ;
476+ state . runAgentAttemptMock . mockResolvedValueOnce ( noReplyResult ) ;
477+
478+ await expect (
479+ agentCommand ( {
480+ message : "prompt that produced no assistant reply" ,
481+ sessionId : "no-reply-session" ,
482+ cwd : state . workspaceDir ,
483+ deliver : true ,
484+ } ) ,
485+ ) . rejects . toThrow ( "Summarization failed: Connection error." ) ;
486+
487+ // Compaction was reached, the failure stayed fatal, and nothing was delivered.
488+ expect ( state . runCliTurnCompactionLifecycleMock ) . toHaveBeenCalledTimes ( 1 ) ;
489+ expect ( state . deliveryCalls ) . toBe ( 0 ) ;
490+ } ) ;
491+
356492 it ( "resumes the next turn from the rotated successor" , async ( ) => {
357493 const storePath = requireStorePath ( ) ;
358494 const sessionsDir = await fs . realpath ( path . dirname ( storePath ) ) ;
0 commit comments