@@ -2959,6 +2959,100 @@ describe("CodexAdapterV2 post-settle continuation", () => {
29592959 ) ,
29602960 ) ;
29612961
2962+ it . effect . each ( [ "before" , "after" ] as const ) (
2963+ "compacts the turn with the compaction item when another turn starts %s the start response" ,
2964+ ( strayTurn ) =>
2965+ Effect . scoped (
2966+ Effect . gen ( function * ( ) {
2967+ const nativeThreadId = `compact-stray-${ strayTurn } ` ;
2968+ const item = { type : "contextCompaction" , id : "compact-item" } ;
2969+ const notification = ( label : string , method : string , params : unknown ) => ( {
2970+ type : "emit_inbound" as const ,
2971+ label,
2972+ frame : { method, params : { threadId : nativeThreadId , ...( params as object ) } } ,
2973+ } ) ;
2974+ const response = {
2975+ type : "emit_inbound" as const ,
2976+ label : "compact" ,
2977+ frame : { id : 3 , result : { } } ,
2978+ } ;
2979+ // Codex continues a goal on its own, then the compaction replaces it.
2980+ const stray = [
2981+ notification ( "stray start" , "turn/started" , {
2982+ turn : makeCodexReplayTurn ( { id : "stray-turn" , status : "inProgress" } ) ,
2983+ } ) ,
2984+ notification ( "stray end" , "turn/completed" , {
2985+ turn : makeCodexReplayTurn ( { id : "stray-turn" , status : "interrupted" } ) ,
2986+ } ) ,
2987+ ] ;
2988+ const transcript = makeCodexReplayTranscript ( {
2989+ scenario : `native-compaction-stray-${ strayTurn } ` ,
2990+ entries : [
2991+ ...codexReplayPreamble ( {
2992+ nativeThreadId,
2993+ nativeTurnId : "unused" ,
2994+ prompt : "unused" ,
2995+ } ) . slice ( 0 , 5 ) ,
2996+ {
2997+ type : "expect_outbound" ,
2998+ label : "compact" ,
2999+ frame : {
3000+ id : 3 ,
3001+ method : "thread/compact/start" ,
3002+ params : { threadId : nativeThreadId } ,
3003+ } ,
3004+ } ,
3005+ ...( strayTurn === "before" ? [ ...stray , response ] : [ response , ...stray ] ) ,
3006+ notification ( "start" , "turn/started" , {
3007+ turn : makeCodexReplayTurn ( { id : "compact-turn" , status : "inProgress" } ) ,
3008+ } ) ,
3009+ notification ( "item/started" , "item/started" , { turnId : "compact-turn" , item } ) ,
3010+ notification ( "item/completed" , "item/completed" , { turnId : "compact-turn" , item } ) ,
3011+ notification ( "complete" , "turn/completed" , {
3012+ turn : makeCodexReplayTurn ( { id : "compact-turn" , status : "completed" } ) ,
3013+ } ) ,
3014+ ] ,
3015+ } ) ;
3016+ const harness = yield * makeCodexReplayHarness ( transcript ) ;
3017+ yield * harness . runtime . compactThread ! (
3018+ makeCodexTestTurnInput ( {
3019+ threadId : harness . threadId ,
3020+ providerThread : harness . providerThread ,
3021+ now : yield * DateTime . now ,
3022+ attemptId : RunAttemptId . make ( "compact-attempt" ) ,
3023+ text : "/compact" ,
3024+ } ) ,
3025+ ) ;
3026+ yield * harness . firstTerminal ;
3027+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
3028+ assert . deepEqual (
3029+ harness . terminalEvents ( ) . map ( ( event ) => [ event . providerTurnId , event . status ] ) ,
3030+ [
3031+ [
3032+ idAllocator . derive . providerTurn ( {
3033+ driver : CodexAdapterV2 . CODEX_DRIVER_KIND ,
3034+ nativeTurnId : "compact-turn" ,
3035+ } ) ,
3036+ "completed" ,
3037+ ] ,
3038+ ] ,
3039+ ) ;
3040+ assert . deepEqual (
3041+ harness . events . flatMap ( ( event ) =>
3042+ event . type === "turn_item.updated" && event . turnItem . type === "compaction"
3043+ ? [ event . turnItem . status ]
3044+ : [ ] ,
3045+ ) ,
3046+ [ "running" , "completed" ] ,
3047+ ) ;
3048+ } ) . pipe (
3049+ Effect . provide (
3050+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
3051+ ) ,
3052+ ) ,
3053+ ) ,
3054+ ) ;
3055+
29623056 it . effect ( "resumes a provider thread without requesting or decoding its history" , ( ) =>
29633057 Effect . scoped (
29643058 Effect . gen ( function * ( ) {
@@ -8474,6 +8568,125 @@ describe("CodexAdapterV2 post-settle continuation", () => {
84748568 ) ,
84758569 ) ;
84768570
8571+ it . effect ( "continues a held goal run while a compaction waits for its own turn" , ( ) =>
8572+ Effect . gen ( function * ( ) {
8573+ const item = { type : "contextCompaction" , id : "compact-item" } ;
8574+ const transcript = makeCodexReplayTranscript ( {
8575+ scenario : "goal-continuation-during-compaction" ,
8576+ entries : [
8577+ ...sessionStart ,
8578+ ...request ( 3 , "thread/goal/get" , { threadId : nativeThreadId } , { goal : null } ) ,
8579+ ...request (
8580+ 4 ,
8581+ "thread/goal/set" ,
8582+ { threadId : nativeThreadId , objective, status : "paused" } ,
8583+ { goal : codexGoal ( "paused" , 0 ) } ,
8584+ ) ,
8585+ ...withReplayRequestId (
8586+ codexReplayPreamble ( {
8587+ nativeThreadId,
8588+ nativeTurnId : "goal-turn-1" ,
8589+ prompt : objective ,
8590+ } ) . slice ( 5 ) ,
8591+ 5 ,
8592+ ) ,
8593+ ...request (
8594+ 6 ,
8595+ "thread/goal/set" ,
8596+ { threadId : nativeThreadId , status : "active" } ,
8597+ { goal : codexGoal ( "active" , 0 ) } ,
8598+ ) ,
8599+ notification ( "goal active" , "thread/goal/updated" , {
8600+ threadId : nativeThreadId ,
8601+ turnId : "goal-turn-1" ,
8602+ goal : codexGoal ( "active" , 0 ) ,
8603+ } ) ,
8604+ notification ( "first turn done" , "turn/completed" , {
8605+ threadId : nativeThreadId ,
8606+ turn : makeCodexReplayTurn ( { id : "goal-turn-1" , status : "completed" } ) ,
8607+ } ) ,
8608+ ...request ( 7 , "thread/compact/start" , { threadId : nativeThreadId } , { } ) ,
8609+ notification ( "continuation" , "turn/started" , {
8610+ threadId : nativeThreadId ,
8611+ turn : makeCodexReplayTurn ( { id : "goal-turn-2" , status : "inProgress" } ) ,
8612+ } ) ,
8613+ notification ( "continuation replaced" , "turn/completed" , {
8614+ threadId : nativeThreadId ,
8615+ turn : makeCodexReplayTurn ( { id : "goal-turn-2" , status : "interrupted" } ) ,
8616+ } ) ,
8617+ // Otherwise the finished compaction waits for the next goal turn.
8618+ notification ( "goal paused" , "thread/goal/updated" , {
8619+ threadId : nativeThreadId ,
8620+ goal : codexGoal ( "paused" , 10 ) ,
8621+ } ) ,
8622+ notification ( "compaction" , "turn/started" , {
8623+ threadId : nativeThreadId ,
8624+ turn : makeCodexReplayTurn ( { id : "compact-turn" , status : "inProgress" } ) ,
8625+ } ) ,
8626+ ...( [ "item/started" , "item/completed" ] as const ) . map ( ( method ) =>
8627+ notification ( method , method , {
8628+ threadId : nativeThreadId ,
8629+ turnId : "compact-turn" ,
8630+ item,
8631+ } ) ,
8632+ ) ,
8633+ notification ( "compaction done" , "turn/completed" , {
8634+ threadId : nativeThreadId ,
8635+ turn : makeCodexReplayTurn ( { id : "compact-turn" , status : "completed" } ) ,
8636+ } ) ,
8637+ ] ,
8638+ } ) ;
8639+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
8640+ const providerTurnId = ( nativeTurnId : string ) =>
8641+ idAllocator . derive . providerTurn ( {
8642+ driver : CodexAdapterV2 . CODEX_DRIVER_KIND ,
8643+ nativeTurnId,
8644+ } ) ;
8645+ const compacted = yield * Deferred . make < void > ( ) ;
8646+ const harness = yield * makeCodexReplayHarness ( transcript , ( event ) =>
8647+ event . type === "turn.terminal" && event . providerTurnId === providerTurnId ( "compact-turn" )
8648+ ? Deferred . succeed ( compacted , undefined )
8649+ : Effect . void ,
8650+ ) ;
8651+ yield * harness . runtime . startTurn ( yield * goalTurnInput ( harness , `/goal ${ objective } ` ) ) ;
8652+ yield * harness . runtime . compactThread ! (
8653+ makeCodexTestTurnInput ( {
8654+ threadId : harness . threadId ,
8655+ providerThread : harness . providerThread ,
8656+ now : yield * DateTime . now ,
8657+ attemptId : RunAttemptId . make ( "compact-attempt" ) ,
8658+ text : "/compact" ,
8659+ } ) ,
8660+ ) ;
8661+ yield * Deferred . await ( compacted ) ;
8662+
8663+ assert . deepEqual (
8664+ harness . events . flatMap ( ( event ) =>
8665+ event . type === "provider_turn.updated" && event . providerTurn . status === "running"
8666+ ? [ [ event . providerTurn . id , event . providerTurn . runAttemptId ] ]
8667+ : [ ] ,
8668+ ) ,
8669+ [
8670+ [ providerTurnId ( "goal-turn-1" ) , RunAttemptId . make ( "goal-attempt" ) ] ,
8671+ [ providerTurnId ( "goal-turn-2" ) , RunAttemptId . make ( "goal-attempt" ) ] ,
8672+ [ providerTurnId ( "compact-turn" ) , RunAttemptId . make ( "compact-attempt" ) ] ,
8673+ ] ,
8674+ ) ;
8675+ assert . deepEqual (
8676+ harness . terminalEvents ( ) . map ( ( event ) => [ event . providerTurnId , event . status ] ) ,
8677+ [
8678+ [ providerTurnId ( "goal-turn-2" ) , "interrupted" ] ,
8679+ [ providerTurnId ( "compact-turn" ) , "completed" ] ,
8680+ ] ,
8681+ ) ;
8682+ } ) . pipe (
8683+ Effect . scoped ,
8684+ Effect . provide (
8685+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
8686+ ) ,
8687+ ) ,
8688+ ) ;
8689+
84778690 it . effect ( "settles /goal pause with a reply instead of a native turn" , ( ) =>
84788691 Effect . gen ( function * ( ) {
84798692 const transcript = makeCodexReplayTranscript ( {
0 commit comments