Skip to content

fix: resolve working directory from primary mount on HostBackend#392

Open
RayCharlizard wants to merge 1 commit intoaaddrick:mainfrom
RayCharlizard:fix/resolve-workdir-host-backend
Open

fix: resolve working directory from primary mount on HostBackend#392
RayCharlizard wants to merge 1 commit intoaaddrick:mainfrom
RayCharlizard:fix/resolve-workdir-host-backend

Conversation

@RayCharlizard
Copy link
Copy Markdown
Contributor

Summary

  • On HostBackend, resolveWorkDir() falls back to os.homedir() for every Cowork session because the Electron app sends cwd=/sessions/{name} (a session-root guest path) and translateGuestPath() requires /sessions/{name}/mnt/{mount}/... — the session root never matches.
  • This means Claude Code always starts in ~ instead of the user's project directory, breaking file access and context loading.
  • The fix adds primary-mount derivation (mirroring BwrapBackend's existing logic) as a fallback before resorting to homedir.

Root Cause

translateGuestPath() uses a regex that requires a /mnt/{mount} component in the path. Session-root paths like /sessions/bold-sharp-clarke don't have one, so translation always fails. BwrapBackend avoids this by overriding spawn() with its own CWD derivation from the primary user mount. HostBackend goes through resolveWorkDir() which lacked equivalent logic.

Related: #389 (fixed a similar guest-path translation gap for CLAUDE_COWORK_MEMORY_PATH_OVERRIDE).

The Fix

When translateGuestPath() can't resolve a session-root CWD, derive the working directory from the primary user mount (first non-dotfile, non-uploads key in mountMap) — the same heuristic BwrapBackend uses. Falls back to homedir only when no user mount exists.

Verification

Tested with a Node.js harness simulating exact spawn parameters from live session logs:

Scenario Before After
Session root + user mount ~/ (bug) /home/user/project/ (correct)
Session root + no user mount ~/ ~/ (correct fallback)
Full guest mount path Translated correctly Unchanged
Host path passthrough Passed through Unchanged
sharedCwdPath precedence Takes precedence Unchanged

Test plan

  • Start a Cowork session via Projects sidebar → verify Claude Code CWD is the project directory, not ~/
  • Start a Cowork session via "New task" → verify same behavior
  • Start a session with no folder selected → verify graceful fallback to homedir

🤖 Generated with Claude Code

The Electron app sends `cwd=/sessions/{name}` (a session-root guest
path) for every Cowork session. `resolveWorkDir()` attempts to
translate this via `translateGuestPath()`, but that function's regex
requires `/sessions/{name}/mnt/{mount}/...` — the session root has no
`/mnt/` component, so translation always fails and CWD falls back to
`os.homedir()`.

BwrapBackend avoids this because it overrides `spawn()` and derives CWD
from the primary user mount (first non-dotfile, non-uploads key in
`mountMap`). HostBackend goes through `resolveWorkDir()` which lacked
this fallback.

Add the same primary-mount derivation to `resolveWorkDir()`: when the
CWD is a session-root guest path that `translateGuestPath()` cannot
resolve, find the primary user mount from `mountMap` and use its host
path. Falls back to homedir only when no user mount exists.

Verified with a Node.js test harness simulating the exact spawn
parameters from live session logs — the fix produces the correct
project directory while all edge cases (no user mount, empty mountMap,
host paths, sharedCwdPath precedence) behave correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant