@@ -19,6 +19,7 @@ import {
1919 buildTurnStartParams ,
2020 hasConfiguredMcpServer ,
2121 isRecoverableThreadResumeError ,
22+ isThreadActiveWriterError ,
2223 makeMemoryConsolidationNotificationFilter ,
2324 openCodexThread ,
2425} from "./CodexSessionRuntime.ts" ;
@@ -570,13 +571,93 @@ describe("isRecoverableThreadResumeError", () => {
570571 } ) ;
571572} ) ;
572573
574+ describe ( "isThreadActiveWriterError" , ( ) => {
575+ it ( "matches Codex thread writer conflicts" , ( ) => {
576+ NodeAssert . equal (
577+ isThreadActiveWriterError (
578+ new CodexErrors . CodexAppServerRequestError ( {
579+ code : - 32603 ,
580+ errorMessage : "thread provider-thread already has an active writer" ,
581+ } ) ,
582+ ) ,
583+ true ,
584+ ) ;
585+ } ) ;
586+
587+ it ( "ignores unrelated writer errors" , ( ) => {
588+ NodeAssert . equal (
589+ isThreadActiveWriterError (
590+ new CodexErrors . CodexAppServerRequestError ( {
591+ code : - 32603 ,
592+ errorMessage : "log file already has an active writer" ,
593+ } ) ,
594+ ) ,
595+ false ,
596+ ) ;
597+ } ) ;
598+ } ) ;
599+
573600describe ( "openCodexThread" , ( ) => {
601+ it . effect ( "forks the thread when another Codex process owns its writer" , ( ) =>
602+ Effect . gen ( function * ( ) {
603+ const calls : Array < {
604+ method : "thread/start" | "thread/resume" | "thread/fork" ;
605+ payload : unknown ;
606+ } > = [ ] ;
607+ const forked = makeThreadOpenResponse ( "forked-thread" ) ;
608+ const client = {
609+ request : < M extends "thread/start" | "thread/resume" | "thread/fork" > (
610+ method : M ,
611+ payload : CodexRpc . ClientRequestParamsByMethod [ M ] ,
612+ ) => {
613+ calls . push ( { method, payload } ) ;
614+ if ( method === "thread/resume" ) {
615+ return Effect . fail (
616+ new CodexErrors . CodexAppServerRequestError ( {
617+ code : - 32603 ,
618+ errorMessage : "thread provider-thread already has an active writer" ,
619+ } ) ,
620+ ) ;
621+ }
622+ return Effect . succeed ( forked as CodexRpc . ClientRequestResponsesByMethod [ M ] ) ;
623+ } ,
624+ } ;
625+
626+ const opened = yield * openCodexThread ( {
627+ client,
628+ threadId : ThreadId . make ( "thread-1" ) ,
629+ runtimeMode : "full-access" ,
630+ cwd : "/tmp/project" ,
631+ requestedModel : "gpt-5.3-codex" ,
632+ serviceTier : undefined ,
633+ resumeThreadId : "provider-thread" ,
634+ } ) ;
635+
636+ NodeAssert . equal ( opened . thread . id , "forked-thread" ) ;
637+ NodeAssert . deepStrictEqual (
638+ calls . map ( ( call ) => call . method ) ,
639+ [ "thread/resume" , "thread/fork" ] ,
640+ ) ;
641+ NodeAssert . deepStrictEqual ( calls [ 1 ] ?. payload , {
642+ threadId : "provider-thread" ,
643+ cwd : "/tmp/project" ,
644+ approvalPolicy : "never" ,
645+ sandbox : "danger-full-access" ,
646+ approvalsReviewer : "user" ,
647+ model : "gpt-5.3-codex" ,
648+ } ) ;
649+ } ) ,
650+ ) ;
651+
574652 it . effect ( "falls back to thread/start when resume fails recoverably" , ( ) =>
575653 Effect . gen ( function * ( ) {
576- const calls : Array < { method : "thread/start" | "thread/resume" ; payload : unknown } > = [ ] ;
654+ const calls : Array < {
655+ method : "thread/start" | "thread/resume" | "thread/fork" ;
656+ payload : unknown ;
657+ } > = [ ] ;
577658 const started = makeThreadOpenResponse ( "fresh-thread" ) ;
578659 const client = {
579- request : < M extends "thread/start" | "thread/resume" > (
660+ request : < M extends "thread/start" | "thread/resume" | "thread/fork" > (
580661 method : M ,
581662 payload : CodexRpc . ClientRequestParamsByMethod [ M ] ,
582663 ) => {
@@ -614,7 +695,7 @@ describe("openCodexThread", () => {
614695 it . effect ( "propagates non-recoverable resume failures" , ( ) =>
615696 Effect . gen ( function * ( ) {
616697 const client = {
617- request : < M extends "thread/start" | "thread/resume" > (
698+ request : < M extends "thread/start" | "thread/resume" | "thread/fork" > (
618699 method : M ,
619700 _payload : CodexRpc . ClientRequestParamsByMethod [ M ] ,
620701 ) => {
0 commit comments