@@ -7702,6 +7702,121 @@ describe("CodexAdapterV2 post-settle continuation", () => {
77027702 ) ,
77037703 ) ;
77047704
7705+ it . effect ( "unarchives an unloaded Codex thread before rolling it back" , ( ) =>
7706+ Effect . gen ( function * ( ) {
7707+ const nativeThreadId = "archived-rollback-thread" ;
7708+ const resumeParams = {
7709+ threadId : nativeThreadId ,
7710+ excludeTurns : true ,
7711+ cwd : CODEX_TEST_RUNTIME_POLICY . cwd ,
7712+ model : CODEX_TEST_MODEL_SELECTION . model ,
7713+ config : CodexAdapterV2 . CODEX_THREAD_CONFIG ,
7714+ } ;
7715+ const thread = codexReplayThreadResult ( { nativeThreadId, forkedFromId : null } ) ;
7716+ const exchange = (
7717+ id : number ,
7718+ method : string ,
7719+ params : unknown ,
7720+ reply : { readonly result : unknown } | { readonly error : unknown } ,
7721+ ) : Array < CodexReplay . CodexAppServerReplayEntry > => [
7722+ { type : "expect_outbound" , label : method , frame : { id, method, params } } ,
7723+ { type : "emit_inbound" , label : method , frame : { id, ...reply } } ,
7724+ ] ;
7725+ const transcript = makeCodexReplayTranscript ( {
7726+ scenario : "codex-archived-rollback" ,
7727+ entries : [
7728+ ...codexReplayPreamble ( {
7729+ nativeThreadId,
7730+ nativeTurnId : "archived-rollback-turn" ,
7731+ prompt : "unused" ,
7732+ } ) . slice ( 0 , 5 ) ,
7733+ ...exchange (
7734+ 3 ,
7735+ "thread/read" ,
7736+ { threadId : nativeThreadId , includeTurns : false } ,
7737+ {
7738+ result : {
7739+ thread : {
7740+ id : nativeThreadId ,
7741+ historyMode : "paginated" ,
7742+ status : { type : "notLoaded" } ,
7743+ } ,
7744+ } ,
7745+ } ,
7746+ ) ,
7747+ ...exchange ( 4 , "thread/resume" , resumeParams , {
7748+ error : { code : - 32600 , message : `session ${ nativeThreadId } is archived` } ,
7749+ } ) ,
7750+ ...exchange ( 5 , "thread/unarchive" , { threadId : nativeThreadId } , { result : thread } ) ,
7751+ ...exchange ( 6 , "thread/resume" , resumeParams , { result : thread } ) ,
7752+ ...exchange (
7753+ 7 ,
7754+ "thread/turns/list" ,
7755+ {
7756+ threadId : nativeThreadId ,
7757+ cursor : null ,
7758+ limit : 1 ,
7759+ sortDirection : "desc" ,
7760+ itemsView : "summary" ,
7761+ } ,
7762+ {
7763+ result : {
7764+ data : [ { id : "native-turn-second" , items : [ ] , status : "completed" , error : null } ] ,
7765+ nextCursor : null ,
7766+ } ,
7767+ } ,
7768+ ) ,
7769+ ...exchange (
7770+ 8 ,
7771+ "thread/revert" ,
7772+ { threadId : nativeThreadId , beforeTurnId : "native-turn-second" } ,
7773+ { result : thread } ,
7774+ ) ,
7775+ ] ,
7776+ } ) ;
7777+ const harness = yield * makeCodexReplayHarness ( transcript ) ;
7778+ const now = yield * DateTime . now ;
7779+ const firstTurn = codexReplaySourceTurn ( {
7780+ id : "provider-turn-first" ,
7781+ ordinal : 1 ,
7782+ nativeId : "native-turn-first" ,
7783+ providerThreadId : harness . providerThread . id ,
7784+ now,
7785+ } ) ;
7786+ const secondTurn = codexReplaySourceTurn ( {
7787+ id : "provider-turn-second" ,
7788+ ordinal : 2 ,
7789+ nativeId : "native-turn-second" ,
7790+ providerThreadId : harness . providerThread . id ,
7791+ now,
7792+ } ) ;
7793+
7794+ const snapshot = yield * harness . runtime . rollbackThread ( {
7795+ providerThread : harness . providerThread ,
7796+ target : {
7797+ type : "provider_turn" ,
7798+ checkpointId : CheckpointId . make ( "checkpoint-archived-rollback" ) ,
7799+ appRunOrdinal : 1 ,
7800+ providerTurn : firstTurn ,
7801+ } ,
7802+ providerThreadTurns : [ firstTurn , secondTurn ] ,
7803+ } ) ;
7804+
7805+ assert . equal ( snapshot . providerThread . nativeThreadRef ?. nativeId , nativeThreadId ) ;
7806+ assert . deepEqual ( snapshot . providerThread . nativeConversationHeadRef , firstTurn . nativeTurnRef ) ;
7807+ } ) . pipe (
7808+ Effect . scoped ,
7809+ Effect . provide (
7810+ Layer . mergeAll (
7811+ IdAllocator . layer ,
7812+ McpProviderSessions . layer ,
7813+ NodeServices . layer ,
7814+ NodeCrypto . layer ,
7815+ ) ,
7816+ ) ,
7817+ ) ,
7818+ ) ;
7819+
77057820 it . effect (
77067821 "falls back to fork-local thread/revert on paginated history when the source turn lacks a native reference" ,
77077822 ( ) =>
0 commit comments