fix(runner): surface dangling-symlink errors instead of misleading EEXIST
Closes #890.
Summary
When a session's workspace path is a symbolic link to a directory that no longer exists, the runner currently fails with a misleading kernel-level error:
Unable to create directory at '/path'. System error: EEXIST: file already exists, mkdir '/path'.
The user has no way to tell that the dangling symlink is the actual problem.
Root cause
In cli/src/runner/run.ts (the simple sessionType branch around lines 297-339):
fs.access(directory)follows symlinks. When the target is missing, it throwsENOENT.- The catch block falls through to
fs.mkdir(directory, { recursive: true }). mkdirwithrecursive: trueonly toleratesEEXISTagainst an existing directory. A dangling symlink is not a directory, somkdirthrowsEEXIST.- The error reporter renders the kernel error verbatim.
Fix
Replace the inline fs.access + fs.mkdir pair with a small validateWorkspaceDirectory helper that uses fs.lstat to inspect symlinks without following them. The helper distinguishes:
- missing path -> approval flow / mkdir as before
- existing directory -> ok
- regular file at the workspace path -> "non-directory file" error
- symlink to an existing directory -> ok
- symlink to a non-directory -> "not a directory" error
- dangling symlink -> diagnostic naming both the symlink path and the missing target, with recovery options (recreate the target, remove the symlink, archive the session)
The existing mkdir errno switch (EACCES, ENOTDIR, ENOSPC, EROFS) is preserved and extended with an EEXIST race-recheck so the kernel error code never leaks.
Tests
cli/src/runner/validateWorkspaceDirectory.test.ts covers:
- 7 fs-touching cases against a real tmpdir + symlinks (missing path, approval gate, existing directory, regular file, symlink-to-dir, dangling symlink, symlink-to-file).
- 6 cases for the pure errno-to-message mapper (EACCES, ENOTDIR, ENOSPC, EROFS, EEXIST, unknown code).
Locally: + 'bun typecheck' + clean across cli/web/hub; + 'bun run test' + clean (977 cli + 416 hub tests pass; the only failure is the pre-existing flaky + 'runner.integration.test.ts > should detect version mismatch and kill old runner' + which fails on + 'upstream/main' + without my changes too).
Manual repro
Against the actual broken path that triggered this report:
/home/u/coding/hapi-scratchlist-per-session -> /home/u/coding/hapi/worktrees/scratchlist-per-session
^^^^^^^^^^^^^^^^^^^^^^^^^ deleted
Before: + 'HTTP 500 EEXIST: file already exists, mkdir ...' +
After:
Workspace path '/home/u/coding/hapi-scratchlist-per-session' is a symbolic link to
'/home/u/coding/hapi/worktrees/scratchlist-per-session', which no longer exists. This
usually means the target was deleted (e.g. via ` + '`git worktree remove`' + `) without removing
the symlink. Recovery: recreate the directory at the target path, remove the dangling
symlink (` + '`rm \'/home/u/coding/hapi-scratchlist-per-session\'`' + `), or archive this session.
Out of scope
- Auto-cleanup of dangling symlinks (the user may have intent).
- Auto-recreation of the worktree.
- Behavior changes for archived sessions (separate concern).
- The
+ 'worktree' +sessionType branch is intentionally untouched - its existing "Worktree sessions require an existing Git repository" error message is already a useful diagnostic for missing base paths.
Made with Cursor
Addressed in 073add02.
The Major finding (rm '${directory}' was a copy-pasteable shell command that broke if the path contained a single quote) is fixed by dropping the literal command shape and describing the recovery action in prose. The path is still mentioned for clarity but no longer presented as something to paste into a shell.
Added a regression test (does not produce a copy-pasteable rm command when the path contains a single quote) that mkdir's a directory whose name contains ', creates a dangling symlink under it, and asserts the resulting error message never contains the literal \rm ...`` shape.
Local gates: bun typecheck clean across cli/web/hub; bun run test clean (978 cli + 416 hub pass; the only failure is the pre-existing flaky runner.integration.test.ts > should detect version mismatch which fails on upstream/main without my changes too).