@@ -381,7 +381,7 @@ describe("maybeCompactCodexAppServerSession", () => {
381381 } ) ;
382382 } ) ;
383383
384- it ( "starts native app-server compaction for post-context-engine budget requests " , async ( ) => {
384+ it ( "resumes the bound thread before post-context-engine budget compaction " , async ( ) => {
385385 const fake = createFakeCodexClient ( ) ;
386386 setCodexAppServerClientFactoryForTest ( async ( ) => fake . client ) ;
387387 const sessionFile = await writeTestBinding ( {
@@ -417,6 +417,11 @@ describe("maybeCompactCodexAppServerSession", () => {
417417 { threadId : "thread-1" } ,
418418 { timeoutMs : 60_000 } ,
419419 ) ;
420+ expect ( fake . request . mock . calls . map ( ( [ method ] ) => method ) ) . toEqual ( [
421+ "thread/resume" ,
422+ "thread/compact/start" ,
423+ "thread/unsubscribe" ,
424+ ] ) ;
420425 expect ( result . ok ) . toBe ( true ) ;
421426 expect ( result . compacted ) . toBe ( true ) ;
422427 expect ( result . reason ) . toBeUndefined ( ) ;
@@ -501,6 +506,61 @@ describe("maybeCompactCodexAppServerSession", () => {
501506 } ) ;
502507 } ) ;
503508
509+ it ( "preserves projection when the post-run thread cannot be resumed" , async ( ) => {
510+ const fake = createFakeCodexClient ( ) ;
511+ fake . request . mockRejectedValueOnce (
512+ new CodexAppServerRpcError (
513+ { code : - 32_600 , message : "thread not found: thread-1" } ,
514+ "thread/resume" ,
515+ ) ,
516+ ) ;
517+ setCodexAppServerClientFactoryForTest ( async ( ) => fake . client ) ;
518+ const sessionFile = await writeTestBinding ( {
519+ contextEngine : {
520+ schemaVersion : 1 ,
521+ engineId : "lossless-claw" ,
522+ policyFingerprint : "policy-1" ,
523+ projection : {
524+ schemaVersion : 1 ,
525+ mode : "thread_bootstrap" ,
526+ epoch : "epoch-1" ,
527+ fingerprint : "fingerprint-1" ,
528+ } ,
529+ } ,
530+ } ) ;
531+
532+ const result = requireCompactResult (
533+ await maybeCompactCodexAppServerSession (
534+ {
535+ sessionId : "session-1" ,
536+ sessionKey : "agent:main:session-1" ,
537+ sessionFile,
538+ workspaceDir : tempDir ,
539+ trigger : "budget" ,
540+ currentTokenCount : 456 ,
541+ } ,
542+ { allowNonManualNativeRequest : true } ,
543+ ) ,
544+ ) ;
545+
546+ expect ( fake . request . mock . calls . map ( ( [ method ] ) => method ) ) . toEqual ( [ "thread/resume" ] ) ;
547+ expect ( result ) . toMatchObject ( {
548+ ok : false ,
549+ compacted : false ,
550+ reason : "thread not found: thread-1" ,
551+ failure : { reason : "stale_thread_binding" } ,
552+ } ) ;
553+ expect ( await readCodexAppServerBinding ( sessionFile ) ) . toMatchObject ( {
554+ threadId : "thread-1" ,
555+ contextEngine : {
556+ projection : {
557+ epoch : "epoch-1" ,
558+ fingerprint : "fingerprint-1" ,
559+ } ,
560+ } ,
561+ } ) ;
562+ } ) ;
563+
504564 it ( "skips post-context-engine native compaction when the binding changes before projection clear" , async ( ) => {
505565 const fake = createFakeCodexClient ( ) ;
506566 setCodexAppServerClientFactoryForTest ( async ( ) => fake . client ) ;
@@ -588,7 +648,13 @@ describe("maybeCompactCodexAppServerSession", () => {
588648 let externalWriteStarted = false ;
589649 let externalWriteFinished = false ;
590650 const fake = createFakeCodexClient ( ) ;
591- fake . request . mockImplementation ( async ( ) => {
651+ fake . request . mockImplementation ( async ( method , params ) => {
652+ if ( method === "thread/resume" ) {
653+ return codexThreadResumeResult ( ( params as { threadId : string } ) . threadId ) ;
654+ }
655+ if ( method === "thread/unsubscribe" ) {
656+ return { } ;
657+ }
592658 const response = await expectExternalMutationBlockedDuringNativeRequest ( {
593659 releaseExternalMutation : releaseExternalWrite ,
594660 isExternalMutationStarted : ( ) => externalWriteStarted ,
@@ -672,7 +738,13 @@ describe("maybeCompactCodexAppServerSession", () => {
672738 let externalClearStarted = false ;
673739 let externalClearFinished = false ;
674740 const fake = createFakeCodexClient ( ) ;
675- fake . request . mockImplementation ( async ( ) => {
741+ fake . request . mockImplementation ( async ( method , params ) => {
742+ if ( method === "thread/resume" ) {
743+ return codexThreadResumeResult ( ( params as { threadId : string } ) . threadId ) ;
744+ }
745+ if ( method === "thread/unsubscribe" ) {
746+ return { } ;
747+ }
676748 const response = await expectExternalMutationBlockedDuringNativeRequest ( {
677749 releaseExternalMutation : releaseExternalClear ,
678750 isExternalMutationStarted : ( ) => externalClearStarted ,
@@ -1479,8 +1551,9 @@ describe("maybeCompactCodexAppServerSession", () => {
14791551 } ) ;
14801552
14811553 it ( "detaches a guarded remote start after releasing the binding lock" , async ( ) => {
1482- const fake = createFakeCodexClient ( ) ;
1483- fake . request . mockRejectedValueOnce ( new Error ( "thread/compact/start timed out" ) ) ;
1554+ const fake = createFakeCodexClient ( {
1555+ compactStartError : new Error ( "thread/compact/start timed out" ) ,
1556+ } ) ;
14841557 fake . closeAndWait . mockResolvedValueOnce ( false ) ;
14851558 const sessionFile = await writeTestBinding ( ) ;
14861559
@@ -1896,9 +1969,45 @@ describe("maybeCompactCodexAppServerSession", () => {
18961969 } ) ;
18971970} ) ;
18981971
1972+ function codexThreadResumeResult ( threadId : string ) {
1973+ return {
1974+ thread : {
1975+ id : threadId ,
1976+ sessionId : "session-1" ,
1977+ forkedFromId : null ,
1978+ preview : "" ,
1979+ ephemeral : false ,
1980+ modelProvider : "openai" ,
1981+ createdAt : 1 ,
1982+ updatedAt : 1 ,
1983+ status : { type : "idle" } ,
1984+ path : null ,
1985+ cwd : tempDir ,
1986+ cliVersion : "0.139.0" ,
1987+ source : "unknown" ,
1988+ agentNickname : null ,
1989+ agentRole : null ,
1990+ gitInfo : null ,
1991+ name : null ,
1992+ turns : [ ] ,
1993+ } ,
1994+ model : "gpt-5.5-codex" ,
1995+ modelProvider : "openai" ,
1996+ serviceTier : null ,
1997+ cwd : tempDir ,
1998+ instructionSources : [ ] ,
1999+ approvalPolicy : "never" ,
2000+ approvalsReviewer : "user" ,
2001+ sandbox : { type : "dangerFullAccess" } ,
2002+ permissionProfile : null ,
2003+ reasoningEffort : null ,
2004+ } ;
2005+ }
2006+
18992007function createFakeCodexClient (
19002008 options : {
19012009 autoCompleteCompaction ?: boolean ;
2010+ compactStartError ?: Error ;
19022011 interruptError ?: Error ;
19032012 rejectInterrupt ?: boolean ;
19042013 } = { } ,
@@ -1951,12 +2060,18 @@ function createFakeCodexClient(
19512060 } ;
19522061 const request = vi . fn < CodexAppServerClient [ "request" ] > (
19532062 async ( method : string , params ?: unknown ) => {
2063+ if ( method === "thread/resume" ) {
2064+ return codexThreadResumeResult ( ( params as { threadId : string } ) . threadId ) ;
2065+ }
19542066 if ( method === "turn/interrupt" && options . interruptError ) {
19552067 throw options . interruptError ;
19562068 }
19572069 if ( method === "turn/interrupt" && options . rejectInterrupt ) {
19582070 throw new Error ( "interrupt unavailable" ) ;
19592071 }
2072+ if ( method === "thread/compact/start" && options . compactStartError ) {
2073+ throw options . compactStartError ;
2074+ }
19602075 if ( method === "thread/compact/start" && options . autoCompleteCompaction !== false ) {
19612076 const threadId = ( params as { threadId ?: unknown } ) . threadId ;
19622077 if ( typeof threadId !== "string" ) {
0 commit comments