@@ -6204,6 +6204,65 @@ describe("runCodexAppServerAttempt", () => {
62046204 expect ( savedBinding ?. threadId ) . toBe ( "thread-1" ) ;
62056205 } ) ;
62066206
6207+ it ( "resumes a bound Codex thread when stale mirrored token totals remain over budget" , async ( ) => {
6208+ const sessionFile = path . join ( tempDir , "session.jsonl" ) ;
6209+ const workspaceDir = path . join ( tempDir , "workspace" ) ;
6210+ const agentDir = path . join ( tempDir , "agent" ) ;
6211+ await writeExistingBinding ( sessionFile , workspaceDir , { dynamicToolsFingerprint : "[]" } ) ;
6212+ await fs . writeFile (
6213+ path . join ( path . dirname ( sessionFile ) , "sessions.json" ) ,
6214+ JSON . stringify ( {
6215+ "agent:main:session-1" : {
6216+ sessionFile,
6217+ totalTokens : 96_000 ,
6218+ } ,
6219+ } ) ,
6220+ ) ;
6221+ const rolloutDir = path . join ( agentDir , "codex-home" , "sessions" ) ;
6222+ await fs . mkdir ( rolloutDir , { recursive : true } ) ;
6223+ await fs . writeFile (
6224+ path . join ( rolloutDir , "rollout-thread-existing.jsonl" ) ,
6225+ `${ JSON . stringify ( {
6226+ payload : {
6227+ type : "token_count" ,
6228+ info : {
6229+ total_token_usage : {
6230+ total_tokens : 97_000 ,
6231+ } ,
6232+ last_token_usage : {
6233+ total_tokens : 12_000 ,
6234+ } ,
6235+ } ,
6236+ } ,
6237+ } ) } \n`,
6238+ ) ;
6239+ const { requests, waitForMethod, completeTurn } = createResumeHarness ( ) ;
6240+ const params = createParams ( sessionFile , workspaceDir ) ;
6241+ params . agentDir = agentDir ;
6242+ params . config = {
6243+ agents : {
6244+ defaults : {
6245+ compaction : {
6246+ truncateAfterCompaction : true ,
6247+ maxActiveTranscriptBytes : "1mb" ,
6248+ } ,
6249+ } ,
6250+ } ,
6251+ } as never ;
6252+
6253+ const run = runCodexAppServerAttempt ( params , {
6254+ pluginConfig : { appServer : { mode : "yolo" } } ,
6255+ } ) ;
6256+ await waitForMethod ( "turn/start" ) ;
6257+ await completeTurn ( { threadId : "thread-existing" , turnId : "turn-1" } ) ;
6258+ await run ;
6259+
6260+ expect ( requests . map ( ( entry ) => entry . method ) ) . toContain ( "thread/resume" ) ;
6261+ expect ( requests . map ( ( entry ) => entry . method ) ) . not . toContain ( "thread/start" ) ;
6262+ const savedBinding = await readCodexAppServerBinding ( sessionFile ) ;
6263+ expect ( savedBinding ?. threadId ) . toBe ( "thread-existing" ) ;
6264+ } ) ;
6265+
62076266 it ( "preserves bound auth when rotating an over-budget native rollout" , async ( ) => {
62086267 const sessionFile = path . join ( tempDir , "session.jsonl" ) ;
62096268 const workspaceDir = path . join ( tempDir , "workspace" ) ;
0 commit comments