Repository navigation
fix(daemon): stop the worker holding the user's project as its cwd (#3706) - #3727
Conversation
Greptile SummaryDetached 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/5No 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.
What T-Rex did
Reviews (3): Last reviewed commit: "fix(daemon): create the working director..." | Re-trigger Greptile |
|
Thanks for testing the missing-data-directory case rather than assuming it — that was the right thing to probe, since a Raising something the review did not flag, because it applies here too. The same shipping gap found on #3726 exists on this PR: 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 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:
All four take |
|
Correcting myself, and it turned out to matter. In my last comment I wrote that all four spawn sites take 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":
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 Fixed in 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 The one failure is still Thank you for probing the missing-directory case — it is the reason I went back and read |
|
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. |
|
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 |
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.
6de9ca2 to
bc1596f
Compare
|
Rebased onto current
Checked on the rebased branch: |
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]>
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]>
…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
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
* 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]>
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:
A process started with
-WorkingDirectory <dir>blocksRename-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
ProcessManager.spawnDaemon— WindowsStart-Process … -WindowStyle Hidden, no-WorkingDirectoryProcessManager.spawnDaemon— POSIXspawnHidden(…, { detached, stdio, env }), nocwdworker-utilslazy-spawnspawnHidden(…, { detached, stdio }), nocwdServerService.spawnServerDaemonspawn(…, { detached, stdio, windowsHide }), nocwdThe 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 underdaemon working directory (#3706):-WorkingDirectoryand names the data directory'in the directory is doubled for PowerShell, like the two paths already aredaemonWorkingDirectory()is the data dir and is notprocess.cwd()— the property that actually matters, stated as suchTwo existing
#3195assertions 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
-WorkingDirectoryfrom the builder fails four cases:both new command-shape cases and both
#3195string assertions, which is the right blast radius for a change to that one string.The single failure is
resolveWorkerRuntimePath > should look up Bun on non-Windows when caller is Node, which fails identically onmainon 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, sotsccannot resolve thebunandnodetype libraries. Saying so rather than implying a green check.