@@ -308,4 +308,89 @@ describe("IdleCompactionService", () => {
308308 ) ;
309309 } ,
310310 ) ;
311+
312+ it . effect ( "keeps the latest run's compaction when an earlier run reports completed again" , ( ) =>
313+ Effect . scoped (
314+ Effect . gen ( function * ( ) {
315+ yield * TestClock . setTime ( NOW_MS ) ;
316+ const base = records ( { cacheExpiresInMs : 10 * MINUTE_MS } ) ;
317+ const laterRunId = RunId . make ( "run-2" ) ;
318+ const laterMessageId = MessageId . make ( "message-2" ) ;
319+ const earlierRun = base . runs [ 0 ] ! ;
320+ const laterRun = {
321+ ...earlierRun ,
322+ id : laterRunId ,
323+ ordinal : 2 ,
324+ userMessageId : laterMessageId ,
325+ } ;
326+ const data : OrchestrationV2ThreadProjection = {
327+ ...base ,
328+ runs : [ earlierRun , laterRun ] ,
329+ messages : [
330+ ...base . messages ,
331+ { ...base . messages [ 0 ] ! , id : laterMessageId , runId : laterRunId , text : "Keep going" } ,
332+ ] ,
333+ providerThreads : [ { ...base . providerThreads [ 0 ] ! , lastRunOrdinal : 2 } ] ,
334+ attempts : [ ] ,
335+ nodes : [ ] ,
336+ subagents : [ ] ,
337+ providerSessions : [ ] ,
338+ plans : [ ] ,
339+ checkpointScopes : [ ] ,
340+ checkpoints : [ ] ,
341+ contextHandoffs : [ ] ,
342+ visibleTurnItems : [ ] ,
343+ updatedAt : at ( ) ,
344+ } ;
345+ const completed = ( run : typeof earlierRun ) => ( {
346+ id : EventId . make ( `completed-${ run . id } ` ) ,
347+ type : "run.updated" as const ,
348+ threadId,
349+ runId : run . id ,
350+ occurredAt : at ( ) ,
351+ payload : run ,
352+ } ) ;
353+ const dispatched : Array < string > = [ ] ;
354+ const streamDone = yield * Deferred . make < void > ( ) ;
355+ const dependencies = Layer . mergeAll (
356+ Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
357+ // A delegated completion on the earlier run lands after the later run completed.
358+ streamDomainEvents : Stream . make ( completed ( laterRun ) , completed ( earlierRun ) ) . pipe (
359+ Stream . ensuring ( Deferred . succeed ( streamDone , undefined ) ) ,
360+ ) ,
361+ dispatch : ( command ) =>
362+ Effect . sync ( ( ) => {
363+ dispatched . push ( command . commandId ) ;
364+ return { sequence : 1 , storedEvents : [ ] } ;
365+ } ) ,
366+ } ) ,
367+ Layer . mock ( ProjectionStore . ProjectionStoreV2 ) ( {
368+ getThread : ( ) => Effect . succeed ( data . thread ) ,
369+ getThreadRecords : ( _threadId , _keys , options ) =>
370+ Effect . succeed ( {
371+ ...data ,
372+ messages : data . messages . filter (
373+ ( message ) =>
374+ options ?. messageRunIds === undefined ||
375+ ( message . runId !== null && options . messageRunIds . includes ( message . runId ) ) ,
376+ ) ,
377+ } ) ,
378+ } ) ,
379+ Layer . mock ( ServerSettings . ServerSettingsService ) ( {
380+ getSettings : Effect . succeed ( {
381+ ...DEFAULT_SERVER_SETTINGS ,
382+ idleCompaction : "beforeCacheExpires" ,
383+ } ) ,
384+ } ) ,
385+ ) ;
386+ yield * Effect . gen ( function * ( ) {
387+ const service = yield * IdleCompactionService . make ;
388+ yield * service . start ( ) ;
389+ yield * Deferred . await ( streamDone ) ;
390+ yield * TestClock . adjust ( 9 * MINUTE_MS ) ;
391+ } ) . pipe ( Effect . provide ( dependencies ) ) ;
392+ expect ( dispatched ) . toEqual ( [ "server:idle-compact:idle-thread:run-2" ] ) ;
393+ } ) ,
394+ ) ,
395+ ) ;
311396} ) ;
0 commit comments