claude-code - 💡(How to fix) Fix Background job (daemon) Bash cwd overridden to ~/.claude, ignoring job's recorded cwd/originCwd

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

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.json still 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:

  • EnterWorktree delegates to a user WorktreeCreate hook that keys off the inherited cwd.
  • Because the hook was handed the substituted $HOME/.claude cwd, 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)

Root Cause

  • EnterWorktree delegates to a user WorktreeCreate hook that keys off the inherited cwd.
  • Because the hook was handed the substituted $HOME/.claude cwd, 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.

Code Example

"cwd": "<project-path>"
"originCwd": "<project-path>"
"linkScanPath": ".../projects/<encoded-project-path>/<...>.jsonl"
"cliVersion": "2.1.142"
"backend": "daemon"
RAW_BUFFERClick to expand / collapse

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.json still 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:

  • EnterWorktree delegates to a user WorktreeCreate hook that keys off the inherited cwd.
  • Because the hook was handed the substituted $HOME/.claude cwd, 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)

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Background job (daemon) Bash cwd overridden to ~/.claude, ignoring job's recorded cwd/originCwd