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.
4 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
Related issues (same problem cluster, different layers)
This issue is not a duplicate of the bot-suggested issues, but they share the same root cause: the daemon/app does not validate that stored
cwd/originCwdpaths still exist before using them. A coordinated fix across layers would address them together:| Issue | Layer | Symptom |
|-------|-------|---------|
| #61740 | Daemon spare pool | Pre-warmed spare processes retain deleted worktree cwds, causing orphan session dirs and
/clearerrors || #53225 | Desktop app | No recovery UI when a session's cwd is moved/deleted (
~/Library/Application Support/Claude/…) || #59361 | Daemon backend | Agent's
cwd/originCwdinstate.jsonoverridden to~/.claudeat runtime || #52747 | Bash tool (closed) | Bash tool permanently broken when CWD is deleted mid-session |
| #57920 |
--resumeCLI |claude --resume <id>fails after worktree path is removed |The fix in this issue (spawn-time fallback + update-on-move) would ideally cover both the daemon job layer (
~/.claude/jobs/<id>/state.json) and the desktop app session layer (~/Library/Application Support/Claude/…) to close the whole cluster.Addressed by a merged fix. Please reopen with a fresh repro if you still see this on a current version.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.