@@ -7466,6 +7466,116 @@ describe("CodexAdapterV2 post-settle continuation", () => {
74667466 ) ,
74677467 ) ;
74687468
7469+ it . effect ( "unarchives an unloaded Codex thread before rolling it back" , ( ) =>
7470+ Effect . gen ( function * ( ) {
7471+ const nativeThreadId = "archived-rollback-thread" ;
7472+ const resumeParams = {
7473+ threadId : nativeThreadId ,
7474+ excludeTurns : true ,
7475+ cwd : CODEX_TEST_RUNTIME_POLICY . cwd ,
7476+ model : CODEX_TEST_MODEL_SELECTION . model ,
7477+ config : CodexAdapterV2 . CODEX_THREAD_CONFIG ,
7478+ } ;
7479+ const thread = codexReplayThreadResult ( { nativeThreadId, forkedFromId : null } ) ;
7480+ const exchange = (
7481+ id : number ,
7482+ method : string ,
7483+ params : unknown ,
7484+ reply : { readonly result : unknown } | { readonly error : unknown } ,
7485+ ) : Array < CodexReplay . CodexAppServerReplayEntry > => [
7486+ { type : "expect_outbound" , label : method , frame : { id, method, params } } ,
7487+ { type : "emit_inbound" , label : method , frame : { id, ...reply } } ,
7488+ ] ;
7489+ const transcript = makeCodexReplayTranscript ( {
7490+ scenario : "codex-archived-rollback" ,
7491+ entries : [
7492+ ...codexReplayPreamble ( {
7493+ nativeThreadId,
7494+ nativeTurnId : "archived-rollback-turn" ,
7495+ prompt : "unused" ,
7496+ } ) . slice ( 0 , 5 ) ,
7497+ ...exchange (
7498+ 3 ,
7499+ "thread/read" ,
7500+ { threadId : nativeThreadId , includeTurns : false } ,
7501+ {
7502+ result : {
7503+ thread : {
7504+ id : nativeThreadId ,
7505+ historyMode : "paginated" ,
7506+ status : { type : "notLoaded" } ,
7507+ } ,
7508+ } ,
7509+ } ,
7510+ ) ,
7511+ ...exchange ( 4 , "thread/resume" , resumeParams , {
7512+ error : { code : - 32600 , message : `session ${ nativeThreadId } is archived` } ,
7513+ } ) ,
7514+ ...exchange ( 5 , "thread/unarchive" , { threadId : nativeThreadId } , { result : thread } ) ,
7515+ ...exchange ( 6 , "thread/resume" , resumeParams , { result : thread } ) ,
7516+ ...exchange (
7517+ 7 ,
7518+ "thread/turns/list" ,
7519+ {
7520+ threadId : nativeThreadId ,
7521+ cursor : null ,
7522+ limit : 1 ,
7523+ sortDirection : "desc" ,
7524+ itemsView : "summary" ,
7525+ } ,
7526+ {
7527+ result : {
7528+ data : [ { id : "native-turn-second" , items : [ ] , status : "completed" , error : null } ] ,
7529+ nextCursor : null ,
7530+ } ,
7531+ } ,
7532+ ) ,
7533+ ...exchange (
7534+ 8 ,
7535+ "thread/revert" ,
7536+ { threadId : nativeThreadId , beforeTurnId : "native-turn-second" } ,
7537+ { result : thread } ,
7538+ ) ,
7539+ ] ,
7540+ } ) ;
7541+ const harness = yield * makeCodexReplayHarness ( transcript ) ;
7542+ const now = yield * DateTime . now ;
7543+ const firstTurn = codexReplaySourceTurn ( {
7544+ id : "provider-turn-first" ,
7545+ ordinal : 1 ,
7546+ nativeId : "native-turn-first" ,
7547+ providerThreadId : harness . providerThread . id ,
7548+ now,
7549+ } ) ;
7550+ const secondTurn = codexReplaySourceTurn ( {
7551+ id : "provider-turn-second" ,
7552+ ordinal : 2 ,
7553+ nativeId : "native-turn-second" ,
7554+ providerThreadId : harness . providerThread . id ,
7555+ now,
7556+ } ) ;
7557+
7558+ const snapshot = yield * harness . runtime . rollbackThread ( {
7559+ providerThread : harness . providerThread ,
7560+ target : {
7561+ type : "provider_turn" ,
7562+ checkpointId : CheckpointId . make ( "checkpoint-archived-rollback" ) ,
7563+ appRunOrdinal : 1 ,
7564+ providerTurn : firstTurn ,
7565+ } ,
7566+ providerThreadTurns : [ firstTurn , secondTurn ] ,
7567+ } ) ;
7568+
7569+ assert . equal ( snapshot . providerThread . nativeThreadRef ?. nativeId , nativeThreadId ) ;
7570+ assert . deepEqual ( snapshot . providerThread . nativeConversationHeadRef , firstTurn . nativeTurnRef ) ;
7571+ } ) . pipe (
7572+ Effect . scoped ,
7573+ Effect . provide (
7574+ Layer . mergeAll ( IdAllocator . layer , McpProviderSessions . layer , NodeServices . layer ) ,
7575+ ) ,
7576+ ) ,
7577+ ) ;
7578+
74697579 it . effect (
74707580 "falls back to fork-local thread/revert on paginated history when the source turn lacks a native reference" ,
74717581 ( ) =>
0 commit comments