Background job (daemon) Bash cwd overridden to ~/.claude, ignoring job's recorded cwd/originCwd
Summary
-------
When a job runs under the daemon backend (a background job), the Bash tool environment and the agent's reported "Primary working directory" do not honor the job's own recorded cwd / originCwd. They are overridden to $HOME/.claude (the Claude config/home directory). Every Bash invocation resets cwd to $HOME/.claude, and the agent's Environment block reports the primary working directory as $HOME/.claude, even though the job was launched from a different project directory.
The job's own persisted metadata (state.json) correctly records the project directory. Only the runtime substitutes $HOME/.claude.
Expected behavior
-----------------
For a daemon-backend background job, the Bash tool working directory and the agent's primary working directory should match the job's recorded cwd / originCwd (the project directory the job was launched in).
Actual behavior
----------------
- Every Bash invocation has cwd
$HOME/.claude. - The agent Environment block reports primary working directory as
$HOME/.claude. - The job's
state.jsonstill correctly records the original project directory, so the override happens only at runtime, not in the persisted metadata.
Evidence (redacted job state.json fields)
-----------------------------------------
Relevant fields from the job's state.json (paths/values generalized):
"cwd": "<project-path>"
"originCwd": "<project-path>"
"linkScanPath": ".../projects/<encoded-project-path>/<...>.jsonl"
"cliVersion": "2.1.142"
"backend": "daemon"
cwd and originCwd both point at <project-path> (a project repo, not $HOME/.claude). linkScanPath is also under the encoded <project-path>. Despite this, at runtime pwd from the Bash tool returns $HOME/.claude.
Repro / Impact
--------------
The override silently propagates into tools that key off the inherited cwd. Concretely:
EnterWorktreedelegates to a userWorktreeCreatehook that keys off the inherited cwd.- Because the hook was handed the substituted
$HOME/.claudecwd, it created a git worktree of the wrong repo: it worktree'd$HOME/.claude(which itself happens to be a git repo) instead of the intended project repo. - The agent had to detect the mistake, discard the stray worktree, and manually create an isolated workspace in the correct repo.
Net effect: silent wrong-repo isolation. This was only caught because $HOME/.claude happened to be a separate git repo with obviously different contents; in a setup where $HOME/.claude is not a git repo (or has similar contents) the failure could go unnoticed.
Environment
-----------
cliVersion: 2.1.142- backend: daemon (background job)
- Platform: macOS (darwin)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗