Agent crashes with 'exit 1 before init' when cwd no longer exists (e.g. after project move)
Summary
When an agent's working directory is moved or deleted after the agent was created, any attempt to recover or resume that agent results in an immediate crash with exit 1 before init. The error appears in the agents list as a single-line truncated stack trace and the agent cannot be interacted with.
This affects both the daemon backend (~/.claude/jobs/<id>/state.json) and likely the desktop app session layer (~/Library/Application Support/Claude/…) — see #53225 for the desktop-app variant of the same root cause.
Steps to Reproduce
- Start an agent in directory
~/Projects/scratch/some-session/ - The agent (or a skill it runs) moves the project to a new path, e.g.
~/Projects/my-report/ - The original scratch directory is deleted as part of the move
- Attempt to recover the agent from the agents list
Observed Behaviour
The agent crashes immediately on spawn with:
exit 1 before init — … (/$bunfs/root/src/entrypoints/cli.js:11884:233)
at async ovO (/$bunfs/root/src/entrypoints/cli.js:24088:68)
at async ryO (/$bunfs/root/src/entrypoints/cli.js:24127:313)
Bun v1.3.14 (macOS arm64)
The daemon log shows the agent settling to (crashed) within milliseconds of spawning — it never initialises. The state.json for the job retains the stale cwd / originCwd pointing to the deleted directory, and every recovery attempt re-crashes for the same reason.
Root Cause
Bun exits with code 1 before the CLI runtime initialises if the process working directory does not exist. The cwd and originCwd fields in ~/.claude/jobs/<id>/state.json are set at agent-creation time and are never updated when the project directory changes. There is no fallback to a valid directory (e.g. $HOME) when the stored cwd is missing.
Expected Behaviour
One or more of the following mitigations:
- At spawn time: if the stored
cwddoes not exist, fall back to$HOME(or the nearest existing ancestor) and emit a warning rather than crashing. - At move/rename time: when a skill or agent action relocates the project root, update
cwd/originCwdin the correspondingstate.json. - In the agents UI: detect a missing
cwdand surface a clear "working directory was moved or deleted" message with an option to reassign the path, instead of showing a raw stack trace.
Environment
- Claude Code 2.1.150
- macOS arm64 (Darwin 25.5.0)
- Bun v1.3.14
- Backend: daemon
Workaround
Manually edit ~/.claude/jobs/<daemonShort>/state.json and update cwd and originCwd to a valid existing directory, then retry recovery.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗