@@ -345,19 +345,36 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
345345 authorizationHeader : "Bearer instruction-compaction-token" ,
346346 browserToolsAvailable : true ,
347347 } ) ;
348+ type Runtime = AcpSessionRuntime . AcpSessionRuntime [ "Service" ] ;
349+ let sessionUpdate : Parameters < Runtime [ "handleSessionUpdate" ] > [ 0 ] | undefined ;
350+ let copilotSessionEvent :
351+ | ( ( event : unknown ) => Effect . Effect < void , EffectAcpErrors . AcpError > )
352+ | undefined ;
348353 const adapter = yield * makeAcpAdapterV2 ( {
349354 instanceId,
350355 flavor : {
351356 driver : ACP_TEST_DRIVER ,
352357 capabilities : AcpProviderCapabilitiesV2 ,
353358 supportsCompaction : true ,
359+ registerExtensions : makeCopilotSubagentRouting ( ) . registerExtensions ,
354360 makeRuntime : makeMockRuntime ( {
355361 childProcessSpawner,
356362 mockAgentPath,
357363 protocolEvents,
358364 environment,
359365 wrapRuntime : ( runtime ) => ( {
360366 ...runtime ,
367+ // The captured handlers deliver frames between turns, while no prompt is in flight.
368+ handleSessionUpdate : ( next ) =>
369+ Effect . sync ( ( ) => {
370+ sessionUpdate = next ;
371+ } ) . pipe ( Effect . andThen ( runtime . handleSessionUpdate ( next ) ) ) ,
372+ handleExtNotification : ( method , payload , next ) =>
373+ Effect . sync ( ( ) => {
374+ if ( method === "github.com/copilot/sessionEvent" ) {
375+ copilotSessionEvent = next as typeof copilotSessionEvent ;
376+ }
377+ } ) . pipe ( Effect . andThen ( runtime . handleExtNotification ( method , payload , next ) ) ) ,
361378 // Make compaction during prompt completion deterministic at the adapter boundary.
362379 prompt : ( payload , options ) =>
363380 runtime . prompt ( payload , options ) . pipe ( Effect . tap ( ( ) => runtime . drainEvents ) ) ,
@@ -381,30 +398,45 @@ const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
381398 const providerThread = yield * runtime . ensureThread ( { threadId, modelSelection, runtimePolicy } ) ;
382399 const now = yield * DateTime . now ;
383400 let ordinal = 0 ;
384- return Effect . fnUntraced ( function * ( messageText : string , expectedStatus = "completed" ) {
385- yield * runtime . startTurn (
386- makeTurnInput ( {
387- threadId,
388- providerThread,
389- instanceId,
390- runtimePolicy,
391- now,
392- ordinal : ++ ordinal ,
393- messageText,
394- } ) ,
395- ) ;
396- const terminal = yield * runtime . events . pipe (
397- Stream . filter ( ( event ) => event . type === "turn.terminal" ) ,
398- Stream . runHead ,
399- ) ;
400- assert . equal ( Option . getOrThrow ( terminal ) . status , expectedStatus ) ;
401- while ( true ) {
402- const event = yield * Queue . take ( protocolEvents ) ;
403- if ( event . direction === "outgoing" && rawProtocolMethod ( event ) === "session/prompt" ) {
404- return rawProtocolPromptTextBlocks ( event ) ;
401+ const sessionId = "mock-session-1" ;
402+ return {
403+ runTurn : Effect . fnUntraced ( function * ( messageText : string , expectedStatus = "completed" ) {
404+ yield * runtime . startTurn (
405+ makeTurnInput ( {
406+ threadId,
407+ providerThread,
408+ instanceId,
409+ runtimePolicy,
410+ now,
411+ ordinal : ++ ordinal ,
412+ messageText,
413+ } ) ,
414+ ) ;
415+ const terminal = yield * runtime . events . pipe (
416+ Stream . filter ( ( event ) => event . type === "turn.terminal" ) ,
417+ Stream . runHead ,
418+ ) ;
419+ assert . equal ( Option . getOrThrow ( terminal ) . status , expectedStatus ) ;
420+ while ( true ) {
421+ const event = yield * Queue . take ( protocolEvents ) ;
422+ if ( event . direction === "outgoing" && rawProtocolMethod ( event ) === "session/prompt" ) {
423+ return rawProtocolPromptTextBlocks ( event ) ;
424+ }
405425 }
406- }
407- } ) ;
426+ } ) ,
427+ idleSessionUpdate : Effect . fnUntraced ( function * ( update : EffectAcpSchema . SessionUpdate ) {
428+ assert . isDefined ( sessionUpdate ) ;
429+ yield * sessionUpdate ( { sessionId, update } ) ;
430+ } ) ,
431+ idleCopilotSessionEvent : Effect . fnUntraced ( function * ( event : {
432+ readonly type : string ;
433+ readonly agentId ?: string ;
434+ readonly data : Record < string , unknown > ;
435+ } ) {
436+ assert . isDefined ( copilotSessionEvent ) ;
437+ yield * copilotSessionEvent ( { sessionId, ...event } ) ;
438+ } ) ,
439+ } ;
408440} ) ;
409441
410442function assertReinjectedInstructions (
@@ -1044,12 +1076,13 @@ describe("AcpAdapterV2", () => {
10441076 ) ;
10451077
10461078 it . live (
1047- "reinjects ACP instructions after protocol compaction, including during reinjection" ,
1079+ "reinjects ACP instructions after protocol compaction, during reinjection or while idle " ,
10481080 ( ) =>
10491081 Effect . gen ( function * ( ) {
1050- const runTurn = yield * makeInstructionCompactionHarness ( {
1051- T3_ACP_COMPACTION_PROMPT_ORDINALS : "2,4" ,
1052- } ) ;
1082+ const { runTurn, idleSessionUpdate, idleCopilotSessionEvent } =
1083+ yield * makeInstructionCompactionHarness ( {
1084+ T3_ACP_COMPACTION_PROMPT_ORDINALS : "2,4" ,
1085+ } ) ;
10531086 yield * runTurn ( "Start work." ) ;
10541087 yield * runTurn ( "Continue until context compacts." ) ;
10551088 assert . deepEqual ( yield * runTurn ( " /repro-skill" ) , [
@@ -1064,14 +1097,41 @@ describe("AcpAdapterV2", () => {
10641097 "Continue after the second compaction." ,
10651098 ) ;
10661099 assert . deepEqual ( yield * runTurn ( "Finish work." ) , [ { type : "text" , text : "Finish work." } ] ) ;
1100+
1101+ yield * idleSessionUpdate ( {
1102+ sessionUpdate : "compaction_update" ,
1103+ compactionId : "compact-idle" ,
1104+ status : "completed" ,
1105+ } ) ;
1106+ assertReinjectedInstructions (
1107+ yield * runTurn ( "Continue after idle compaction." ) ,
1108+ "Continue after idle compaction." ,
1109+ ) ;
1110+ const copilotCompaction = { success : true , outcome : "applied" , trigger : "threshold" } ;
1111+ yield * idleCopilotSessionEvent ( {
1112+ type : "session.compaction_complete" ,
1113+ agentId : "agent-a" ,
1114+ data : copilotCompaction ,
1115+ } ) ;
1116+ assert . deepEqual ( yield * runTurn ( "Continue after subagent compaction." ) , [
1117+ { type : "text" , text : "Continue after subagent compaction." } ,
1118+ ] ) ;
1119+ yield * idleCopilotSessionEvent ( {
1120+ type : "session.compaction_complete" ,
1121+ data : copilotCompaction ,
1122+ } ) ;
1123+ assertReinjectedInstructions (
1124+ yield * runTurn ( "Continue after Copilot compaction." ) ,
1125+ "Continue after Copilot compaction." ,
1126+ ) ;
10671127 } ) . pipe ( Effect . provide ( layerTest ) , Effect . scoped ) ,
10681128 ) ;
10691129
10701130 it . live (
10711131 "reinjects ACP instructions after successful native compact, but not failed compact" ,
10721132 ( ) =>
10731133 Effect . gen ( function * ( ) {
1074- const runTurn = yield * makeInstructionCompactionHarness ( {
1134+ const { runTurn } = yield * makeInstructionCompactionHarness ( {
10751135 T3_ACP_FAIL_PROMPT_ORDINAL : "2" ,
10761136 } ) ;
10771137 yield * runTurn ( "Start work." ) ;
0 commit comments