@@ -307,6 +307,7 @@ const pollProtocolMethods = (events: Queue.Queue<EffectAcpProtocol.AcpProtocolLo
307307const makeInstructionCompactionHarness = Effect . fnUntraced ( function * (
308308 environment : Readonly < Record < string , string > > ,
309309 followAgentWorkState = false ,
310+ drainPromptEvents = true ,
310311) {
311312 const childProcessSpawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
312313 const path = yield * Path . Path ;
@@ -317,6 +318,9 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
317318 const protocolEvents = yield * Queue . unbounded < EffectAcpProtocol . AcpProtocolLogEvent > ( ) ;
318319 const instanceId = ProviderInstanceId . make ( "acp-test-instruction-compaction" ) ;
319320 const threadId = ThreadId . make ( "thread-acp-instruction-compaction" ) ;
321+ let sessionUpdate :
322+ | Parameters < AcpSessionRuntime . AcpSessionRuntime [ "Service" ] [ "handleSessionUpdate" ] > [ 0 ]
323+ | undefined ;
320324 yield * ( yield * McpProviderSessions . McpProviderSessions ) . set ( {
321325 environmentId : EnvironmentId . make ( "environment-acp-instruction-compaction" ) ,
322326 threadId,
@@ -340,9 +344,15 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
340344 environment,
341345 wrapRuntime : ( runtime ) => ( {
342346 ...runtime ,
347+ handleSessionUpdate : ( handler ) => {
348+ sessionUpdate = handler ;
349+ return runtime . handleSessionUpdate ( handler ) ;
350+ } ,
343351 // Make compaction during prompt completion deterministic at the adapter boundary.
344352 prompt : ( payload , options ) =>
345- runtime . prompt ( payload , options ) . pipe ( Effect . tap ( ( ) => runtime . drainEvents ) ) ,
353+ runtime
354+ . prompt ( payload , options )
355+ . pipe ( Effect . tap ( ( ) => ( drainPromptEvents ? runtime . drainEvents : Effect . void ) ) ) ,
346356 } ) ,
347357 } ) ,
348358 } ,
@@ -380,11 +390,36 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
380390 return Effect . fnUntraced ( function * (
381391 messageText : string ,
382392 expectedStatus = "completed" ,
383- reopen = false ,
393+ reopen : boolean | "rollback" | "compact" = false ,
384394 interactionMode : "default" | "plan" = "default" ,
385395 ) {
386396 const turnRuntimePolicy = { ...runtimePolicy , interactionMode } ;
387- if ( reopen ) {
397+ if ( reopen === "compact" ) {
398+ if ( sessionUpdate === undefined ) return yield * Effect . die ( "Missing session handler" ) ;
399+ const sessionId = providerThread . nativeThreadRef ?. nativeId ;
400+ if ( sessionId == null ) return yield * Effect . die ( "Missing native session" ) ;
401+ yield * sessionUpdate ( {
402+ sessionId,
403+ update : {
404+ sessionUpdate : "compaction_update" ,
405+ compactionId : "idle-compact" ,
406+ status : "completed" ,
407+ } ,
408+ } ) ;
409+ }
410+ if ( reopen === "rollback" ) {
411+ const result = yield * session . runtime . rollbackThread ( {
412+ providerThread,
413+ providerThreadTurns : [ ] ,
414+ target : {
415+ type : "thread_start" ,
416+ checkpointId : CheckpointId . make ( "instruction-rollback" ) ,
417+ appRunOrdinal : 0 ,
418+ } ,
419+ } ) ;
420+ providerThread = result . providerThread ;
421+ }
422+ if ( reopen === true ) {
388423 const nativeSessionId = providerThread . nativeThreadRef ?. nativeId ;
389424 if ( nativeSessionId == null ) return yield * Effect . die ( "Expected a persisted native session" ) ;
390425 yield * Scope . close ( session . scope , Exit . void ) ;
@@ -438,6 +473,22 @@ function assertReinjectedInstructions(
438473}
439474
440475describe ( "AcpAdapterV2" , ( ) => {
476+ it . live . each ( [ "rollback" , "compact" ] as const ) (
477+ "reinjects instructions after idle %s with no continuation consumer" ,
478+ ( transition ) =>
479+ Effect . gen ( function * ( ) {
480+ const runTurn = yield * makeInstructionCompactionHarness (
481+ { } ,
482+ false ,
483+ transition !== "rollback" ,
484+ ) ;
485+ assertReinjectedInstructions ( yield * runTurn ( "Start work." ) , "Start work." ) ;
486+ assertReinjectedInstructions (
487+ yield * runTurn ( "Continue after reset." , "completed" , transition ) ,
488+ "Continue after reset." ,
489+ ) ;
490+ } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
491+ ) ;
441492 it . live . each ( [ "details" , "none" ] as const ) (
442493 "fails the turn on an ACP v2 idle error with %s, settles its open tool, and accepts the next prompt" ,
443494 ( details ) =>
0 commit comments