@@ -29,6 +29,7 @@ import {
2929 getConfig ,
3030 listJobs ,
3131 setConfig ,
32+ updateState ,
3233 upsertJob ,
3334 writeJobFile
3435} from "./lib/state.mjs" ;
@@ -323,6 +324,22 @@ function isActiveJobStatus(status) {
323324 return status === "queued" || status === "running" ;
324325}
325326
327+ function isTerminalJobRecord ( stateJob , storedJob = null ) {
328+ // PR #346 review: launch/cancel races can expose either the summary state row
329+ // or the per-job file first, so both records must agree the job is active
330+ // before a launcher writes worker-owned fields such as pid.
331+ return ! isActiveJobStatus ( stateJob ?. status ) || Boolean ( storedJob && ! isActiveJobStatus ( storedJob . status ) ) ;
332+ }
333+
334+ function resolveLaunchStatus ( stateJob , storedJob = null , fallback = "queued" ) {
335+ for ( const status of [ stateJob ?. status , storedJob ?. status ] ) {
336+ if ( status && ! isActiveJobStatus ( status ) ) {
337+ return status ;
338+ }
339+ }
340+ return stateJob ?. status ?? storedJob ?. status ?? fallback ;
341+ }
342+
326343function getCurrentClaudeSessionId ( ) {
327344 return process . env [ SESSION_ID_ENV ] ?? null ;
328345}
@@ -619,6 +636,17 @@ function buildTaskRunMetadata({ prompt, resumeLast = false }) {
619636}
620637
621638function renderQueuedTaskLaunch ( payload ) {
639+ // PR #346 review: a terminal enqueue payload means launch did not dispatch a
640+ // worker, so emitting the dispatched sentinel would make hooks and humans
641+ // believe there is live background work to poll.
642+ if ( ! isActiveJobStatus ( payload . status ) ) {
643+ if ( payload . status === "cancelled" ) {
644+ return `Codex task ${ payload . jobId } was cancelled before a worker launched; no work started.\n` ;
645+ }
646+ const detail = payload . errorMessage ? `: ${ payload . errorMessage } ` : "." ;
647+ return `Codex task ${ payload . jobId } failed before a worker launched${ detail } \n` ;
648+ }
649+
622650 const statusToken = buildTaskDispatchedStatusToken ( payload . jobId ) ;
623651 return [
624652 statusToken ,
@@ -712,17 +740,153 @@ async function runForegroundCommand(job, runner, options = {}) {
712740 return execution ;
713741}
714742
715- function spawnDetachedTaskWorker ( cwd , jobId ) {
716- const scriptPath = path . join ( ROOT_DIR , "scripts" , "codex-companion.mjs" ) ;
717- const child = spawn ( process . execPath , [ scriptPath , "task-worker" , "--cwd" , cwd , "--job-id" , jobId ] , {
718- cwd,
719- env : process . env ,
720- detached : true ,
721- stdio : "ignore" ,
722- windowsHide : true
743+ function formatSpawnFailureMessage ( error ) {
744+ const detail = error instanceof Error ? error . message : String ( error ?? "" ) ;
745+ return detail ? `Failed to launch background worker: ${ detail } ` : "Failed to launch background worker." ;
746+ }
747+
748+ function persistQueuedTaskRecord ( workspaceRoot , queuedRecord ) {
749+ // PR #346 review: the launch record is inserted through updateState so the
750+ // summary row and stored job file are created from the same read-modify-write
751+ // transition before any worker is allowed to observe the request.
752+ updateState ( workspaceRoot , ( state ) => {
753+ const existingIndex = state . jobs . findIndex ( ( candidate ) => candidate . id === queuedRecord . id ) ;
754+ const nextRecord = {
755+ ...queuedRecord ,
756+ updatedAt : nowIso ( )
757+ } ;
758+ if ( existingIndex === - 1 ) {
759+ state . jobs . unshift ( nextRecord ) ;
760+ } else {
761+ state . jobs [ existingIndex ] = {
762+ ...state . jobs [ existingIndex ] ,
763+ ...nextRecord
764+ } ;
765+ }
766+ writeJobFile ( workspaceRoot , queuedRecord . id , nextRecord ) ;
767+ } ) ;
768+ }
769+
770+ function markTaskLaunchFailed ( workspaceRoot , jobId , errorMessage , fallbackLogFile = null ) {
771+ let failedJob = null ;
772+ let preservedJob = null ;
773+
774+ // PR #346 review: launch failures must become a durable failed job via one
775+ // state read-modify-write, otherwise foreground waiters can hang on a queued
776+ // record whose worker never existed.
777+ updateState ( workspaceRoot , ( state ) => {
778+ const jobIndex = state . jobs . findIndex ( ( candidate ) => candidate . id === jobId ) ;
779+ if ( jobIndex === - 1 ) {
780+ return ;
781+ }
782+
783+ const stateJob = state . jobs [ jobIndex ] ;
784+ const storedJob = readStoredJob ( workspaceRoot , jobId ) ;
785+ if ( isTerminalJobRecord ( stateJob , storedJob ) ) {
786+ preservedJob = {
787+ ...stateJob ,
788+ ...( storedJob ?? { } )
789+ } ;
790+ return ;
791+ }
792+
793+ const completedAt = nowIso ( ) ;
794+ failedJob = {
795+ ...( storedJob ?? { } ) ,
796+ ...stateJob ,
797+ status : "failed" ,
798+ phase : "failed" ,
799+ pid : null ,
800+ completedAt,
801+ updatedAt : completedAt ,
802+ errorMessage,
803+ logFile : stateJob . logFile ?? storedJob ?. logFile ?? fallbackLogFile
804+ } ;
805+ state . jobs [ jobIndex ] = failedJob ;
806+ writeJobFile ( workspaceRoot , jobId , failedJob ) ;
807+ } ) ;
808+
809+ if ( failedJob ) {
810+ appendLogLine ( failedJob . logFile , errorMessage ) ;
811+ return failedJob ;
812+ }
813+ return preservedJob ;
814+ }
815+
816+ function persistSpawnedTaskWorkerPid ( workspaceRoot , jobId , childPid ) {
817+ let shouldKillWorker = false ;
818+ let launchStatus = "queued" ;
819+ let updatedJob = null ;
820+
821+ // PR #346 review: pid persistence must re-check cancellation inside the same
822+ // state read-modify-write that writes the pid. A cancel that already made the
823+ // job terminal wins, and the just-spawned worker is killed after the state
824+ // update instead of being resurrected by stale launch data.
825+ updateState ( workspaceRoot , ( state ) => {
826+ const jobIndex = state . jobs . findIndex ( ( candidate ) => candidate . id === jobId ) ;
827+ if ( jobIndex === - 1 ) {
828+ shouldKillWorker = true ;
829+ launchStatus = "cancelled" ;
830+ return ;
831+ }
832+
833+ const stateJob = state . jobs [ jobIndex ] ;
834+ const storedJob = readStoredJob ( workspaceRoot , jobId ) ;
835+ if ( isTerminalJobRecord ( stateJob , storedJob ) ) {
836+ shouldKillWorker = true ;
837+ launchStatus = resolveLaunchStatus ( stateJob , storedJob , "cancelled" ) ;
838+ updatedJob = {
839+ ...stateJob ,
840+ ...( storedJob ?? { } )
841+ } ;
842+ return ;
843+ }
844+
845+ const updatedAt = nowIso ( ) ;
846+ updatedJob = {
847+ ...( storedJob ?? { } ) ,
848+ ...stateJob ,
849+ pid : childPid ,
850+ updatedAt
851+ } ;
852+ state . jobs [ jobIndex ] = updatedJob ;
853+ writeJobFile ( workspaceRoot , jobId , updatedJob ) ;
854+ launchStatus = updatedJob . status ;
723855 } ) ;
856+
857+ return { shouldKillWorker, launchStatus, job : updatedJob } ;
858+ }
859+
860+ function spawnDetachedTaskWorker ( cwd , jobId , options = { } ) {
861+ const scriptPath = path . join ( ROOT_DIR , "scripts" , "codex-companion.mjs" ) ;
862+ let child = null ;
863+
864+ try {
865+ child = spawn ( process . execPath , [ scriptPath , "task-worker" , "--cwd" , cwd , "--job-id" , jobId ] , {
866+ cwd,
867+ env : process . env ,
868+ detached : true ,
869+ stdio : "ignore" ,
870+ windowsHide : true
871+ } ) ;
872+ } catch ( error ) {
873+ return { child : null , error } ;
874+ }
875+
876+ const handleSpawnError = ( error ) => {
877+ options . onError ?. ( error ) ;
878+ } ;
879+ if ( typeof child . once === "function" ) {
880+ child . once ( "error" , handleSpawnError ) ;
881+ } else if ( typeof child . on === "function" ) {
882+ child . on ( "error" , handleSpawnError ) ;
883+ }
884+
724885 child . unref ( ) ;
725- return child ;
886+ if ( ! normalizeWorkerPid ( child . pid ) ) {
887+ return { child, error : new Error ( "missing worker pid" ) } ;
888+ }
889+ return { child, error : null } ;
726890}
727891
728892function enqueueBackgroundTask ( cwd , job , request ) {
@@ -739,8 +903,7 @@ function enqueueBackgroundTask(cwd, job, request) {
739903 logFile,
740904 request
741905 } ;
742- writeJobFile ( job . workspaceRoot , job . id , queuedRecord ) ;
743- upsertJob ( job . workspaceRoot , queuedRecord ) ;
906+ persistQueuedTaskRecord ( job . workspaceRoot , queuedRecord ) ;
744907
745908 // PR #346 review: /codex:cancel can land after the durable queued record is
746909 // written but before worker launch; re-read the job and do not spawn a worker
@@ -760,24 +923,36 @@ function enqueueBackgroundTask(cwd, job, request) {
760923 } ;
761924 }
762925
763- const child = spawnDetachedTaskWorker ( cwd , job . id ) ;
764- // PR #346 review: keep the pre-spawn record, then persist the worker pid for cancellation.
765- const spawnedRecord = readStoredJob ( job . workspaceRoot , job . id ) ?? latestQueuedRecord ;
766- if ( isActiveJobStatus ( spawnedRecord . status ) ) {
767- writeJobFile ( job . workspaceRoot , job . id , {
768- ...spawnedRecord ,
769- pid : child . pid ?? null
770- } ) ;
771- upsertJob ( job . workspaceRoot , {
772- id : job . id ,
773- pid : child . pid ?? null
774- } ) ;
926+ const recordLaunchFailure = ( error ) =>
927+ markTaskLaunchFailed ( job . workspaceRoot , job . id , formatSpawnFailureMessage ( error ) , logFile ) ;
928+ const { child, error : spawnError } = spawnDetachedTaskWorker ( cwd , job . id , {
929+ onError : recordLaunchFailure
930+ } ) ;
931+ if ( spawnError || ! child ) {
932+ const failedJob = recordLaunchFailure ( spawnError ?? new Error ( "missing child process" ) ) ;
933+ return {
934+ payload : {
935+ jobId : job . id ,
936+ status : failedJob ?. status ?? "failed" ,
937+ title : job . title ,
938+ summary : job . summary ,
939+ logFile,
940+ errorMessage : failedJob ?. errorMessage ?? formatSpawnFailureMessage ( spawnError )
941+ } ,
942+ logFile
943+ } ;
944+ }
945+
946+ const { shouldKillWorker, launchStatus } = persistSpawnedTaskWorkerPid ( job . workspaceRoot , job . id , child . pid ) ;
947+ if ( shouldKillWorker ) {
948+ appendLogLine ( logFile , `Terminating background worker because job is ${ launchStatus } .` ) ;
949+ terminateProcessTree ( child . pid ) ;
775950 }
776951
777952 return {
778953 payload : {
779954 jobId : job . id ,
780- status : "queued" ,
955+ status : launchStatus ,
781956 title : job . title ,
782957 summary : job . summary ,
783958 logFile
@@ -1184,27 +1359,35 @@ async function handleCancel(argv) {
11841359 appendLogLine ( job . logFile , "Cancelled by user." ) ;
11851360
11861361 const completedAt = nowIso ( ) ;
1187- const nextJob = {
1188- ...job ,
1189- status : "cancelled" ,
1190- phase : "cancelled" ,
1191- pid : null ,
1192- completedAt,
1193- errorMessage : "Cancelled by user."
1194- } ;
1362+ let nextJob = null ;
1363+ // PR #346 review: cancellation is the terminal launch/cancel transition, so
1364+ // the state row and stored job file are updated from one read-modify-write
1365+ // instead of a stale stored-job write followed by a separate state patch.
1366+ updateState ( workspaceRoot , ( state ) => {
1367+ const jobIndex = state . jobs . findIndex ( ( candidate ) => candidate . id === job . id ) ;
1368+ const stateJob = jobIndex === - 1 ? job : state . jobs [ jobIndex ] ;
1369+ const latestStoredJob = readStoredJob ( workspaceRoot , job . id ) ?? existing ;
1370+ nextJob = {
1371+ ...latestStoredJob ,
1372+ ...stateJob ,
1373+ status : "cancelled" ,
1374+ phase : "cancelled" ,
1375+ pid : null ,
1376+ completedAt,
1377+ updatedAt : completedAt ,
1378+ errorMessage : "Cancelled by user."
1379+ } ;
11951380
1196- writeJobFile ( workspaceRoot , job . id , {
1197- ...existing ,
1198- ...nextJob ,
1199- cancelledAt : completedAt
1200- } ) ;
1201- upsertJob ( workspaceRoot , {
1202- id : job . id ,
1203- status : "cancelled" ,
1204- phase : "cancelled" ,
1205- pid : null ,
1206- errorMessage : "Cancelled by user." ,
1207- completedAt
1381+ if ( jobIndex === - 1 ) {
1382+ state . jobs . unshift ( nextJob ) ;
1383+ } else {
1384+ state . jobs [ jobIndex ] = nextJob ;
1385+ }
1386+
1387+ writeJobFile ( workspaceRoot , job . id , {
1388+ ...nextJob ,
1389+ cancelledAt : completedAt
1390+ } ) ;
12081391 } ) ;
12091392
12101393 const payload = {
0 commit comments