Skip to content

fix(daemon): stop the worker holding the user's project as its cwd (#3706) - #3727

Merged
thedotmack merged 2 commits into
thedotmack:mainfrom
ntdatt812:fix/3706-daemon-cwd
Sep 11, 2026
Merged

thedotmack merged 2 commits into
thedotmack:mainfrom
ntdatt812:fix/3706-daemon-cwd

Conversation

@ntdatt812

Copy link
Copy Markdown
Contributor

Closes #3706.

A process holds an open handle on its working directory. The daemon spawns are reached from hooks, which run in the session's project folder, and none of the three spawn sites set a directory — so the daemon inherited the project folder. On Windows that locks it: rename and move fail for as long as the process lives, and a daemon outlives the session that started it.

Measured on Windows 11

The mechanism, isolated from claude-mem entirely — one process, one directory, nothing else:

HELD:   rename FAILED -> The process cannot access the file because it is being used by another process.
EXITED: rename SUCCEEDED

A process started with -WorkingDirectory <dir> blocks Rename-Item <dir> with exactly the message from the report, and the same rename succeeds the moment it exits. That is the whole bug: the daemon simply never exits.

Three spawn sites, all of them

site before
ProcessManager.spawnDaemon — Windows Start-Process … -WindowStyle Hidden, no -WorkingDirectory
ProcessManager.spawnDaemon — POSIX spawnHidden(…, { detached, stdio, env }), no cwd
worker-utils lazy-spawn spawnHidden(…, { detached, stdio }), no cwd
ServerService.spawnServerDaemon spawn(…, { detached, stdio, windowsHide }), no cwd

The issue names the first and notes the POSIX ones are the same pattern; I fixed all of them rather than the Windows one alone, because the POSIX daemons still pin the directory against unmount — quieter, but the same handle.

All now stand in claude-mem's own data directory, via a small daemonWorkingDirectory() that carries the reason. It always exists by the time a daemon starts and is never a directory the user is reorganising.

Tests

tests/infrastructure/process-manager.test.ts, three added under daemon working directory (#3706):

  • the built command carries -WorkingDirectory and names the data directory
  • a ' in the directory is doubled for PowerShell, like the two paths already are
  • daemonWorkingDirectory() is the data dir and is not process.cwd() — the property that actually matters, stated as such

Two existing #3195 assertions pin the exact command string, so they move with the change; one now passes an explicit directory and the other keeps using the default, which is why the default stays covered.

Mutation-checked — dropping -WorkingDirectory from the builder fails four cases:

51 pass, 5 fail   (4 mine + the pre-existing one below)

both new command-shape cases and both #3195 string assertions, which is the right blast radius for a change to that one string.

bun test tests/infrastructure/process-manager.test.ts
55 pass / 1 fail

The single failure is resolveWorkerRuntimePath > should look up Bun on non-Windows when caller is Node, which fails identically on main on this machine — it asserts non-Windows lookup behaviour and I am on Windows. Measured on a clean checkout before this branch, not assumed.

I did not run a typecheck: this checkout has no installed node_modules, so tsc cannot resolve the bun and node type libraries. Saying so rather than implying a green check.

@greptile-apps

greptile-apps Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

Detached worker and server startup now creates and uses the configured claude-mem data directory as its working directory, preventing long-lived daemon processes from retaining the invoking project directory. Runtime coverage confirmed the exercised worker launch paths start from a fresh data directory, and the focused ProcessManager test suite passes.

Confidence Score: 5/5

No blocking failure remains; the exercised detached worker startup paths create and use the configured data directory.

No accepted blocking findings remain after runtime validation of fresh-directory daemon startup and the focused regression suite.

T-Rex T-Rex Logs

What T-Rex did

  • The pre-baseline run established the test harness behavior by showing that the ProcessManager launch was blocked by the sandbox /usr/bin/setsid execution, with hook working directory behavior and a missing ServerService mkdir.
  • The runtime harness after the directory-creation change showed successful ProcessManager and hook launches, with the child working directory set to the fresh data directory.
  • A cleanup capture confirmed that no harness daemon process or listener remained after the test run.

View all artifacts

T-Rex Ran code and verified through T-Rex

Reviews (3): Last reviewed commit: "fix(daemon): create the working director..." | Re-trigger Greptile

@ntdatt812

Copy link
Copy Markdown
Contributor Author

Thanks for testing the missing-data-directory case rather than assuming it — that was the right thing to probe, since a cwd that does not exist makes spawn fail with ENOENT, and it would have turned this fix into a launch failure on first run.

Raising something the review did not flag, because it applies here too. The same shipping gap found on #3726 exists on this PR: plugin/scripts/worker-service.cjs is committed and is what runs, and the shipped copy still has no working directory —

$ grep -c "WorkingDirectory" plugin/scripts/worker-service.cjs
0
$ grep -o "Start-Process -FilePath[^\"]\{0,60\}" plugin/scripts/worker-service.cjs
Start-Process -FilePath '${r(t)}' -ArgumentList @('

So as it stands the source is fixed and the daemon users actually launch is not.

I did not commit a rebuilt bundle, for the reason I set out on #3726 in more detail: rebuilding on clean main, with none of my changes, already rewrites four files (834 insertions / 634 deletions) and grows worker-service.cjs by 418 KB, which trips the build's own advisory size guard and its #2584 "server-only dependency leaked into the worker bundle" hint. That is a question about main, not about this change, and burying it in a daemon-cwd fix would make both unreviewable.

Happy to push the rebuild here the moment you would rather have it in this PR than as its own commit.

One thing worth pinning while the cwd is on the table. Your test exercised the server daemon; the branch changes three spawn sites, and the other two are the ones a hook reaches:

site spawn
ProcessManager.spawnDaemon (Windows) Start-Process … -WorkingDirectory
ProcessManager.spawnDaemon (POSIX) spawnHidden(…, { cwd })
worker-utils lazy-spawn spawnHidden(…, { cwd })
ServerService.spawnServerDaemon spawn(…, { cwd })

All four take DATA_DIR, which paths.ts creates, so the ENOENT case you probed cannot arise from a fresh install — but if you would like that stated as a test rather than as a property of paths.ts, I will add one asserting the directory exists before any spawn uses it. It is the kind of coupling that holds today and quietly stops holding later.

@ntdatt812

Copy link
Copy Markdown
Contributor Author

Correcting myself, and it turned out to matter.

In my last comment I wrote that all four spawn sites take DATA_DIR, "which paths.ts creates". That is wrong. paths.ts resolves DATA_DIR and exports an ensureDir(dirPath) helper, but it never calls it for DATA_DIR itself — creation is left to whichever caller gets there first.

That makes the failure you probed a real one rather than a hypothetical, just reached by a different route than "the directory is missing at test time":

  • spawn(..., { cwd }) fails with ENOENT when the cwd does not exist
  • Start-Process -WorkingDirectory refuses outright

Before this branch, no daemon passed a cwd, so a missing data directory cost nothing. My change made the daemons depend on it existing — so on a fresh install, if a hook reaches a daemon spawn before anything has created ~/.claude-mem, the fix for a locked folder becomes a daemon that will not start. Your test passed because something did create it first in that run; that ordering is not a guarantee, and I asserted it as one.

Fixed in 6de9ca2: all three spawn sites mkdirSync(..., { recursive: true }) before using the directory as a cwd. Idempotent, so the normal case is one stat.

Pinned by a test that deletes the directory first, which is the state the assertion is actually about:

rmSync(DATA_DIR, { recursive: true, force: true });
expect(existsSync(DATA_DIR)).toBe(false);
const dir = daemonWorkingDirectory();
expect(existsSync(dir)).toBe(true);

Mutation-checked — removing the mkdirSync from daemonWorkingDirectory() fails exactly that case and nothing else.

bun test tests/infrastructure/process-manager.test.ts
56 pass / 1 fail    (three consecutive runs, identical)

The one failure is still resolveWorkerRuntimePath > should look up Bun on non-Windows when caller is Node, which fails the same way on main on this Windows machine.

Thank you for probing the missing-directory case — it is the reason I went back and read paths.ts instead of trusting my own sentence.

@stantheman0128

Copy link
Copy Markdown
Contributor

Support from the Windows cabinet.

A process holding its cwd open is the same class of Windows handle leak as a LISTEN socket that outlives the PID. We have seen the rename lock on this box in other tools; the isolated Start-Process repro in the PR body is the right height.

Fixing every spawn site (ProcessManager Windows/POSIX, hook lazy-spawn, server daemon) is the class, not a single Start-Process flag. Complements #3529 (hidden window) and #3309 (socket inheritance / spawn locks). Those are different handles. Keep all three.

Creating the data dir before passing it as cwd is load-bearing. Before this PR a missing ~/.claude-mem was harmless because nothing passed cwd; after it, Start-Process and spawn both refuse. The rmSync test is the one that would go red if that mkdir disappeared.

The shipped-bundle gap is the same one you already flagged on #3726. I would rather see CredApi and this cwd fix land in source than wait on the #2584 rebuild.

@thedotmack

Copy link
Copy Markdown
Owner

NIGHT SHIP issue-patch-ship: Gate FAIL

Real Windows cwd-lock bug, but not a safe night-ship: CONFLICTING, four spawn sites, and the author notes the shipped plugin/scripts/worker-service.cjs still inherits the project cwd. Source-only would not reach users without a bundle rebuild (out of scope).

A process keeps an open handle on its working directory. The daemon spawns
are reached from hooks, which run in the session's project folder, and none of
the three spawn sites set a directory -- so the daemon inherited it and, on
Windows, held it for the daemon's whole life. Since the daemon outlives the
session, renaming or moving the project failed with "The process cannot access
the file because it is being used by another process" until the user found and
killed bun.exe. POSIX allows the rename but still pins the directory against
unmount.

All three now stand in claude-mem's own data directory: the Windows
Start-Process command gains -WorkingDirectory, and the two detached spawns
gain cwd. The data directory always exists by the time a daemon starts and is
never a directory the user is reorganising.

Closes thedotmack#3706
Passing a cwd that does not exist is worse than passing none: spawn fails
with ENOENT and Start-Process refuses outright, so on a fresh install this
fix would have turned the first daemon launch into a failure.

paths.ts resolves DATA_DIR but does not create it -- ensureDir is a helper
callers must call. Today some earlier caller happens to create it before any
daemon spawns, which is why this did not show up in testing, but the spawn
sites must not depend on that ordering. All three now mkdir -p first, which
is idempotent.
@ntdatt812

ntdatt812 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased onto current main, now mergeable. Taking the three gate points in turn:

  • Conflict: it was only in tests/infrastructure/process-manager.test.ts, where fix: say why the worker died instead of listing suspects #3894's probeWorkerBootFailure block and this PR's working-directory block were both added at the same spot. Both are kept. All three source files merged cleanly, and the diff is still the original four files.
  • Spawn sites: every place that starts the long-lived worker now gets an explicit working directory instead of inheriting the hook's cwd, and that directory is created before it is handed to spawn, since a missing cwd makes spawn fail with ENOENT. The sites are the POSIX spawnDaemon path, the Windows Start-Process -WorkingDirectory, the ServerService launcher and the worker-utils launcher. All four use the claude-mem data directory. The two ProcessManager sites share daemonWorkingDirectory(), while ServerService and worker-utils resolve the same directory inline (paths.dataDir() / DATA_DIR). If you would rather have a single place to change, I can route those two through the helper as well.
  • Bundle: this is source-only, as you said. I left plugin/scripts/worker-service.cjs alone because a regenerated bundle in a contributor PR tends to conflict with every other change to it. If you would rather have the rebuilt bundle in this PR, I'll add it.

Checked on the rebased branch: tests/infrastructure/process-manager.test.ts 64 pass, 1 fail. The failure (resolveWorkerRuntimePath › should look up Bun on non-Windows) fails identically on a clean checkout of main when run on Windows. npm run typecheck is clean for both configurations.

@thedotmack
thedotmack merged commit fa0f4e7 into thedotmack:main Sep 11, 2026
7 checks passed
cursor Bot pushed a commit that referenced this pull request Sep 11, 2026
Package-facing patch bump so 13.24.15 ships #3727 / #3706 (daemon no longer inherits user project cwd). Rebuilds plugin bundles so the injected version matches.

Co-authored-by: Alex Newman <[email protected]>
thedotmack added a commit that referenced this pull request Sep 11, 2026
Package-facing patch bump so 13.24.15 ships #3727 / #3706 (daemon no longer inherits user project cwd). Rebuilds plugin bundles so the injected version matches.

Co-authored-by: Cursor Agent <[email protected]>
Co-authored-by: Alex Newman <[email protected]>
thedotmack added a commit to stantheman0128/claude-mem that referenced this pull request Sep 30, 2026
…h the helper

Conflict resolution re-applies this PR's hidden-daemon helper on top of what
main landed since:
- ProcessManager: keep main's -WorkingDirectory / daemonWorkingDirectory()
  cwd pin (thedotmack#3727), BUN_NOT_FOUND_MESSAGE and the async 'error' listener on the
  POSIX branch (thedotmack#4039); spawnDetachedWorkerDaemon carries all of them, and
  spawnDaemon becomes a thin wrapper around it.
- worker-utils: ensureWorkerRunning's lazy spawn goes through the helper, and
  so does ensureWorkerReadyWithin (added after this PR), which on main still
  spawned with Node detached and no cwd at all.
- version-recycle test: the helper returns a pid number (0 on Windows).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01697ztU3scSYRfW49cPr9Du
thedotmack added a commit that referenced this pull request Sep 30, 2026
Replace the probe-and-relocate guard with an unconditional pin: the --daemon
branch moves the process into daemonWorkingDirectory() (the data dir) before
anything spawns a child, falling back to the home directory, then the OS temp
directory, and never throwing. The launchers already pass that cwd (#3727);
this covers every other launch path too (a manual `--daemon` from any shell,
an older launcher), so an ACL-locked or deleted inherited cwd can no longer
make cross-spawn's chdir-back throw EPERM on every child spawn, and the daemon
never holds a user folder open (#3706).

The narrow before_send drop of chdir EPERM/EACCES exceptions stays as the
residual filter. The generated worker-service.cjs is not part of this PR
(rebuilt at release).

Tests: default pin to the data dir, fallback when a chdir is refused, never
throws when every candidate fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01697ztU3scSYRfW49cPr9Du
thedotmack added a commit that referenced this pull request Sep 30, 2026
* fix(worker): guard against un-chdir-able inherited cwd on Windows

The worker can be launched with an inherited working directory it cannot
chdir back into — notably on Windows when a Microsoft Store install (e.g.
OpenAI Codex under Program Files\WindowsApps\...) leaves the daemon in an
ACL-locked directory. cross-spawn (used by the MCP stdio transport that
launches chroma-mcp) captures process.cwd(), temporarily chdir's into the
child's cwd, then chdir's BACK to the original in a finally that is not
guarded — so an un-chdir-able original cwd throws EPERM on every spawn,
which the logger error-sink bridges into error tracking as noise.

- worker-service: ensureSafeWorkingDirectory() runs at the top of start(),
  before any child spawn. It probes the exact operation that breaks and only
  relocates (to home dir, falling back to temp dir) when the current cwd is
  un-chdir-able. Never throws.
- telemetry: before_send now drops EPERM/EACCES chdir environment faults so
  any residual variant stops filing error-tracking issues. Narrow match
  (chdir syscall AND a permission signal); covers both capture paths.

Rebuilt plugin/scripts/worker-service.cjs.

Generated-By: PostHog Code
Task-Id: cd4a426c-89b0-4e4f-a482-9813b58a11cc

* fix(worker): pin the daemon's cwd to the data dir at boot

Replace the probe-and-relocate guard with an unconditional pin: the --daemon
branch moves the process into daemonWorkingDirectory() (the data dir) before
anything spawns a child, falling back to the home directory, then the OS temp
directory, and never throwing. The launchers already pass that cwd (#3727);
this covers every other launch path too (a manual `--daemon` from any shell,
an older launcher), so an ACL-locked or deleted inherited cwd can no longer
make cross-spawn's chdir-back throw EPERM on every child spawn, and the daemon
never holds a user folder open (#3706).

The narrow before_send drop of chdir EPERM/EACCES exceptions stays as the
residual filter. The generated worker-service.cjs is not part of this PR
(rebuilt at release).

Tests: default pin to the data dir, fallback when a chdir is refused, never
throws when every candidate fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01697ztU3scSYRfW49cPr9Du

---------

Co-authored-by: posthog[bot] <206114724+posthog[bot]@users.noreply.github.com>
Co-authored-by: Alex Newman <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Windows: worker daemon inherits project folder as CWD, permanently locking it (folder cannot be renamed or moved)

3 participants