Skip to content

test(daemon): stop mutating process-global cwd in the daemon cwd-repair tests - #14750

Merged
nwparker merged 1 commit into
mainfrom
nwparker/pty-subprocess-no-global-chdir
Aug 15, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/pty-subprocess-no-global-chdir

Conversation

@nwparker

@nwparker nwparker commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 +14 −19 −5
Prod 1 +26 0 +26

The problem

The two daemon cwd-repair tests really did move the test process:

process.chdir(deletedDaemonCwd)
rmSync(deletedDaemonCwd, { recursive: true, force: true })
…
expect(process.cwd()).toBe(realpathSync(ptyEnv.userDataPath))

process.cwd() and process.chdir() are process-global, so for that window every other test file sharing the worker process saw a deleted working directory.

That was already a latent hazard. Splitting pty-subprocess.test.ts into ten files (#14728) made the window overlap far more concurrent work, and these two started failing intermittently in full-suite runs while passing in isolation — they failed in two separate full runs, one the macOS variant and one the Linux variant.

The fix

Stub the boundary instead of moving the process: report a non-existent daemon cwd from process.cwd(), and spy on process.chdir() to capture the repair target.

The assertion gets stricter, not weaker. It previously observed where the process happened to land; it now names the directory repairDaemonCwd() actually selected:

expect(chdirSpy).toHaveBeenCalledWith(ptyEnv.userDataPath)

The shared helper lives in pty-subprocess-test-harness.ts next to the other daemon env lifecycle helpers.

Verification

  • pty-subprocess family: 131 passing across 4 consecutive runs
  • Full suite: 52,813 passing, 0 failures — both cwd-repair tests green, having failed in prior full runs
  • No test in the repo mutates process-global cwd any more (grep for process.chdir outside the spy returns nothing)

Note

The product behavior is unchanged — repairDaemonCwd() still calls process.chdir() for real; only the tests stop doing so.

@coderabbitai

coderabbitai Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Added stubMissingDaemonCwd to isolate process cwd behavior in tests. Updated macOS and POSIX PTY cwd-repair tests to use the helper, verify repair targets the daemon user-data path, restore process stubs, and confirm that the PTY retains the captured terminal cwd. Removed an unused import.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, fix, rationale, and verification, but it omits most required template sections, including the linked issue and testing checklist. Complete the required template sections, especially ELI5, What Changed, Why, Linked Issue, Visual Proof, Testing, Review, Checklist, AI Disclosure, and Author.
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main test change: preventing process-global cwd mutation in daemon cwd-repair tests.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/main/daemon/pty-subprocess-test-harness.ts (1)

18-27: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Shorten this JSDoc.

Keep the non-obvious reason for the stub. Remove the detailed history and implementation description.

As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.”

Proposed change
-/**
- * Makes the daemon look like it is sitting in a deleted cwd, without moving the
- * test process there.
- *
- * Why: `process.cwd()` and `process.chdir()` are process-global. The previous
- * version of these tests really did chdir into a temp dir and delete it, so every
- * other test file sharing the worker saw a missing cwd for that window — an
- * intermittent failure that got likelier once this suite was split across files.
- * Stubbing keeps the cwd-repair assertion exact and the process untouched.
- */
+/** Stubs a missing daemon cwd without mutating the shared test-process cwd. */

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8cd508e3-8bca-473f-8a4b-bb421d24cd15

📥 Commits

Reviewing files that changed from the base of the PR and between 5b7f442 and f2163a0.

📒 Files selected for processing (2)
  • src/main/daemon/pty-subprocess-test-harness.ts
  • src/main/daemon/pty-subprocess.test.ts

…ir tests

The two cwd-repair tests really did `process.chdir()` into a temp directory and
delete it, so for that window every other test file sharing the worker process saw
a missing cwd. That was already a hazard; splitting pty-subprocess.test.ts into ten
files made the window overlap far more work, and the tests started failing
intermittently in full-suite runs while passing in isolation.

Stub the boundary instead: report a non-existent daemon cwd via `process.cwd()` and
spy on `process.chdir()` to capture the repair target. The assertion gets stricter
rather than weaker — it now names the directory the repair chose instead of
observing where the process happened to land — and no process-global state moves.

Both cwd-repair tests had failed at least once in prior full-suite runs; the suite
is green across repeated runs with this change.
@nwparker
nwparker force-pushed the nwparker/pty-subprocess-no-global-chdir branch from f2163a0 to 36aa100 Compare August 15, 2026 10:29
@nwparker
nwparker merged commit 92fb276 into main Aug 15, 2026
51 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ir tests (stablyai#14750)

The two cwd-repair tests really did `process.chdir()` into a temp directory and
delete it, so for that window every other test file sharing the worker process saw
a missing cwd. That was already a hazard; splitting pty-subprocess.test.ts into ten
files made the window overlap far more work, and the tests started failing
intermittently in full-suite runs while passing in isolation.

Stub the boundary instead: report a non-existent daemon cwd via `process.cwd()` and
spy on `process.chdir()` to capture the repair target. The assertion gets stricter
rather than weaker — it now names the directory the repair chose instead of
observing where the process happened to land — and no process-global state moves.

Both cwd-repair tests had failed at least once in prior full-suite runs; the suite
is green across repeated runs with this change.
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