@@ -177,6 +177,64 @@ describe("AgentSession compaction", () => {
177177 subscription . unsubscribe ( ) ;
178178 } ) ;
179179
180+ it ( "projects a rejected manual cancellation as aborted without recording compaction" , async ( ) => {
181+ const sessionManager = SessionManager . inMemory ( ) ;
182+ sessionManager . appendMessage ( { role : "user" , content : "old prompt" , timestamp : 1 } ) ;
183+ sessionManager . appendMessage ( {
184+ ...createAssistant ( testModel , [ { type : "text" , text : "old answer" } ] ) ,
185+ timestamp : 2 ,
186+ } ) ;
187+ const handlers = createCompactionHandlers ( ) ;
188+ const syntheticError = new Error ( "synthetic manual cancellation rejection" ) ;
189+ const abortActiveCompaction = ( ) => session . abortCompaction ( ) ;
190+ handlers . set ( "session_before_compact" , [
191+ async ( ) => {
192+ abortActiveCompaction ( ) ;
193+ throw syntheticError ;
194+ } ,
195+ ] ) ;
196+ streamMocks . streamSimple . mockImplementation (
197+ ( _activeModel : Model , _context : Context , options ?: SimpleStreamOptions ) => {
198+ if ( options ?. signal ?. aborted ) {
199+ throw syntheticError ;
200+ }
201+ return createAssistantResultStream (
202+ createAssistant ( testModel , [ { type : "text" , text : "unexpected compaction" } ] ) ,
203+ ) ;
204+ } ,
205+ ) ;
206+ const { session } = await createTestSession ( {
207+ sessionManager,
208+ resourceLoader : createResourceLoader ( handlers ) ,
209+ } ) ;
210+ const onAgentEvent = vi . fn ( ) ;
211+ const subscription = subscribeEmbeddedAgentSession ( {
212+ session,
213+ runId : "run-rejected-manual-cancellation" ,
214+ onAgentEvent,
215+ } ) ;
216+
217+ await expect ( session . compact ( ) ) . rejects . toBe ( syntheticError ) ;
218+
219+ const compactionEvents = onAgentEvent . mock . calls
220+ . map ( ( [ event ] ) => event )
221+ . filter ( ( event ) => event . stream === "compaction" ) ;
222+ expect ( compactionEvents ) . toHaveLength ( 2 ) ;
223+ expect ( compactionEvents . at ( - 1 ) ) . toEqual ( {
224+ stream : "compaction" ,
225+ data : {
226+ phase : "end" ,
227+ outcome : "aborted" ,
228+ completed : false ,
229+ willRetry : false ,
230+ } ,
231+ } ) ;
232+ expect ( subscription . getCompactionCount ( ) ) . toBe ( 0 ) ;
233+ expect ( subscription . getLastCompactionTokensAfter ( ) ) . toBeUndefined ( ) ;
234+ expect ( sessionManager . getEntries ( ) . some ( ( entry ) => entry . type === "compaction" ) ) . toBe ( false ) ;
235+ subscription . unsubscribe ( ) ;
236+ } ) ;
237+
180238 it ( "caps extension summaries before persistence and manual return" , async ( ) => {
181239 const sessionManager = SessionManager . inMemory ( ) ;
182240 sessionManager . appendMessage ( { role : "user" , content : "old prompt" , timestamp : 1 } ) ;
0 commit comments