What Happens to In-Progress Tasks When Claude Code Is Restarted
When Claude Code restarts, all in-progress tasks are automatically terminated and removed from the persistent state by the session-lifecycle hook.
When working with the openai/codex-plugin-cc repository, understanding how the companion handles active jobs during restarts is essential for managing long-running workflows. Any tasks that are queued or running when the Claude Code companion restarts do not persist; instead, they are forcefully cleaned up to prevent orphaned processes and stale state entries.
How the Session Lifecycle Hook Terminates Tasks
The termination logic is implemented in plugins/codex/scripts/session-lifecycle-hook.mjs within the cleanupSessionJobs function (lines 42-74). This function executes automatically when the SessionEnd event fires (lines 25-27), which occurs during every companion restart.
Identifying Jobs by Session ID
The cleanup process begins by loading the workspace state file via resolveStateFile from plugins/codex/scripts/lib/state.mjs. It scans for jobs where the sessionId field matches the current CODEX_COMPANION_SESSION_ID environment variable. Only tasks associated with the terminating session are targeted for cleanup, ensuring isolated session management.
Killing Running Process Trees
For each matching job with a status of queued or running, the system invokes terminateProcessTree from plugins/codex/scripts/lib/process.mjs. This function kills the entire process tree associated with the task, guaranteeing that no background processes continue executing after the companion restarts.
Removing Entries from Persistent State
After terminating the processes, the cleanupSessionJobs function removes the affected job entries from the state file and persists the updated state back to disk. This step ensures that terminated tasks do not appear as active or resumable in subsequent sessions.
State Persistence and Task Recovery
The companion tracks all active jobs in a JSON state file located under the workspace root. While finished jobs can be resumed later using codex resume <thread-id>, in-progress tasks that are interrupted by a restart cannot be recovered. The session-lifecycle hook ensures a clean slate by clearing all transient job entries tied to the ended session.
Practical Example: Task Termination During Restart
Consider a scenario where you initiate a long-running task:
# Start a task in the current Claude session
codex task run my-script.js
# While the task is still running, restart the companion
# The SessionEnd hook triggers cleanupSessionJobs automatically
# The running process is killed and the job entry is deleted from state
After the restart completes, the previous task will no longer exist in the state file. You must manually restart the work, as the codex resume command only works for jobs that completed successfully before the restart, not those terminated during the session lifecycle.
Summary
- In-progress tasks do not survive a Claude Code restart; they are forcefully terminated and cleaned up automatically.
- The
cleanupSessionJobsfunction insession-lifecycle-hook.mjshandles the cleanup via the SessionEnd event (lines 25-27). - Jobs are identified by matching
sessionIdtoCODEX_COMPANION_SESSION_IDand terminated usingterminateProcessTreefromprocess.mjs. - Stopped jobs are permanently removed from the JSON state file managed by
state.mjsin the workspace root. - Only completed jobs can be resumed with
codex resume <thread-id>; interrupted in-progress tasks must be restarted manually.
Frequently Asked Questions
Do in-progress tasks automatically resume after Claude Code restarts?
No. According to the openai/codex-plugin-cc source code, in-progress tasks are terminated and removed from the persistent state during restart. They do not automatically resume, and you must manually restart any interrupted work.
How does Claude Code identify which tasks to terminate during a restart?
The system matches jobs by the sessionId field against the current CODEX_COMPANION_SESSION_ID environment variable. Only tasks belonging to the terminating session are targeted for cleanup by the cleanupSessionJobs function, ensuring isolated cleanup per session.
Can I recover a task that was running when the companion restarted?
No. Once the session lifecycle hook runs, the task is killed via terminateProcessTree and removed from the persistent state. Only finished jobs can be resumed later using codex resume <thread-id>; interrupted tasks are lost and must be restarted.
Where is the task state stored in the openai/codex-plugin-cc repository?
Active jobs are stored in a JSON state file under the workspace root, managed by plugins/codex/scripts/lib/state.mjs. The file path is resolved using the resolveStateFile utility function, and the state includes session-linked metadata for each job.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →