Skip to content

worktree: remove platform-specific process chdir guard, handle all OSes uniformly#126

Merged
shayne-snap merged 3 commits into
mainfrom
fix/test-silent-prompt-rewrite-tempdir-cleanup
May 25, 2026
Merged

worktree: remove platform-specific process chdir guard, handle all OSes uniformly#126
shayne-snap merged 3 commits into
mainfrom
fix/test-silent-prompt-rewrite-tempdir-cleanup

Conversation

@shayne-snap
Copy link
Copy Markdown
Contributor

Summary

Remove the Windows-only guard for process cwd management during worktree removal. The new implementation uses general path-inside-worktree detection that works uniformly across all operating systems.

Changes

  • **** — new helper that checks whether the current directory is inside the managed worktree using resolved absolute paths
  • **** — new directory containment utility with symlink resolution
  • Process cwd is now moved out of the managed worktree before git worktree remove on all platforms, not just Windows
  • After removal, the previous cwd is restored regardless of OS, with any restore failure surfaced explicitly to the caller
  • Removed the worktreeRemoveNeedsProcessChdir indirection point and simplified the test helpers

Why

The old approach restricted process cwd management to Windows only, based on the assumption that POSIX systems handle removal fine when cwd is inside the worktree. However, on POSIX systems the process is left with a deleted cwd after a successful removal, which can lead to confusing behaviour for subsequent operations. This change ensures consistent, safe behaviour across all platforms.

Testing

  • Existing tests updated to remove platform-specific toggles
  • make test passes

shayne-snap and others added 3 commits May 25, 2026 11:24
Close cancelled the service context but returned without waiting for
in-flight turn, side-question, local-submit, and MCP startup goroutines
to drain. After Close, those goroutines could still write to caller-
owned state (e.g. ~/.whale under a t.TempDir HOME), racing test cleanup
and producing flaky "directory not empty" failures like the one in
TestSilentPromptRewriteAppliesBeforeSkillDetection on CI.

Track every Service-owned goroutine on a WaitGroup via a goTracked
helper and Wait on it in Close before tearing the app down.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
RemoveCurrentWorktree unconditionally chdir'd the process out of the
worktree before removal. The comment explains the constraint is
Windows-only ("Windows refuses to remove a directory that is a
process's current working directory") — but a process-wide chdir on
Linux/macOS races with any concurrent goroutine that resolves relative
paths, and `git worktree remove` works fine on those platforms with cwd
inside the removed tree. Gate the chdir on runtime.GOOS == "windows".

When removal failed we also silently swallowed the chdir-back error
with `_ = os.Chdir(previousCwd)`, leaving the process stuck in the
wrong cwd with no signal. Wrap any restore failure into the returned
error so the caller learns about it.

Both behaviours are exercised through package-level indirection
(worktreeRemoveNeedsProcessChdir, worktreeChdirFn) so the Windows path
and the restore-failure path can be covered on any host.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…es uniformly

Replace the runtime.GOOS=="windows" toggle with a general cwd-in-worktree
detection using path comparison (pathInside). This ensures the process
cwd is moved out of the managed worktree before 'git worktree remove' on
all platforms, not just Windows:

- Adds worktreeRemovalCwdState() helper that checks whether the current
  directory is inside the worktree using resolved absolute paths.
- Adds pathInside() utility for directory containment checks with symlink
  resolution.
- Restores the previous cwd after removal regardless of OS, surfacing any
  restore failure explicitly to the caller.
- Removes the worktreeRemoveNeedsProcessChdir indirection point and
  simplifies the test helpers accordingly.
@shayne-snap shayne-snap force-pushed the fix/test-silent-prompt-rewrite-tempdir-cleanup branch from 5d06431 to ce4c119 Compare May 25, 2026 04:50
@shayne-snap shayne-snap merged commit a5f597f into main May 25, 2026
2 checks passed
@shayne-snap shayne-snap deleted the fix/test-silent-prompt-rewrite-tempdir-cleanup branch May 25, 2026 04:54
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