Fix PowerShell worktree terminal cwd#7435
Conversation
📝 WalkthroughWalkthroughThis PR modifies PowerShell shell argument generation on Windows to restore the launch working directory after profile loading. A new helper, getPowerShellRestoreCwdCommand, builds a literal Set-Location command appended to the existing OSC 133 bootstrap payload within getPowerShellEncodedCommand, which now accepts a cwd parameter. The call site passes nativeCwd accordingly. Corresponding tests are updated to decode the base64/UTF-16LE encoded PowerShell command and assert on decoded substrings and ordering rather than exact encoded byte matches, including verification of quoting for paths containing single quotes. Changes
Sequence Diagram(s)sequenceDiagram
participant resolveWindowsShellLaunchArgs
participant getPowerShellEncodedCommand
participant getPowerShellRestoreCwdCommand
resolveWindowsShellLaunchArgs->>getPowerShellEncodedCommand: call with nativeCwd
getPowerShellEncodedCommand->>getPowerShellRestoreCwdCommand: build restore command(cwd)
getPowerShellRestoreCwdCommand-->>getPowerShellEncodedCommand: Set-Location -LiteralPath command
getPowerShellEncodedCommand-->>resolveWindowsShellLaunchArgs: encoded bootstrap + restore command
Related PRs: None identified. Suggested labels: windows, powershell, tests Suggested reviewers: None identified. Poem 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: aaafb973-b15f-483f-a3e8-200ba5275a8d
📒 Files selected for processing (2)
src/main/providers/windows-shell-args.test.tssrc/main/providers/windows-shell-args.ts
| function getPowerShellRestoreCwdCommand(cwd: string): string { | ||
| return [ | ||
| '', | ||
| '# Profiles can change location; restore the PTY cwd after profile loading.', | ||
| `Set-Location -LiteralPath ${quoteStartupArg(cwd, 'powershell')} -ErrorAction SilentlyContinue` | ||
| ].join('\n') | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Silent Set-Location failure leaves cwd unrestored with no diagnostic.
-ErrorAction SilentlyContinue means that if the worktree path is unavailable at shell-start time (e.g. deleted mid-launch), the entire fix silently no-ops and the user is left in whatever directory the profile switched to — with no signal that the restore failed. Since this is precisely the scenario the PR is meant to prevent, consider at least surfacing a warning (e.g. Write-Warning to stderr) instead of fully swallowing the error, so the failure mode is visible rather than silently regressing to the pre-fix behavior.
💡 Optional: surface a diagnostic on restore failure
function getPowerShellRestoreCwdCommand(cwd: string): string {
return [
'',
'# Profiles can change location; restore the PTY cwd after profile loading.',
- `Set-Location -LiteralPath ${quoteStartupArg(cwd, 'powershell')} -ErrorAction SilentlyContinue`
+ `try { Set-Location -LiteralPath ${quoteStartupArg(cwd, 'powershell')} -ErrorAction Stop } catch { Write-Warning "Failed to restore working directory: $_" }`
].join('\n')
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| function getPowerShellRestoreCwdCommand(cwd: string): string { | |
| return [ | |
| '', | |
| '# Profiles can change location; restore the PTY cwd after profile loading.', | |
| `Set-Location -LiteralPath ${quoteStartupArg(cwd, 'powershell')} -ErrorAction SilentlyContinue` | |
| ].join('\n') | |
| } | |
| function getPowerShellRestoreCwdCommand(cwd: string): string { | |
| return [ | |
| '', | |
| '# Profiles can change location; restore the PTY cwd after profile loading.', | |
| `try { Set-Location -LiteralPath ${quoteStartupArg(cwd, 'powershell')} -ErrorAction Stop } catch { Write-Warning "Failed to restore working directory: $_" }` | |
| ].join('\n') | |
| } |
Summary
Fix Windows PowerShell terminal launches so Orca restores the selected worktree cwd after user profiles load.
Root cause: Orca already passes the worktree path as the PTY cwd, but a user's PowerShell profile can run
Set-Locationand move the interactive shell elsewhere before Codex, Claude, OpenCode, or a plain terminal prompt starts. The PowerShell bootstrap now re-applies the validated cwd withSet-Location -LiteralPath ...after profile loading and before startup commands are delivered.Screenshots
No visual change.
Testing
pnpm lintmainwith an unrelated switch-exhaustiveness error insrc/renderer/src/components/settings/SourceControlActionRepoOverrideNote.tsx:35:5.pnpm oxlint src/main/providers/windows-shell-args.ts src/main/providers/windows-shell-args.test.ts --quiet✅pnpm typecheckpnpm testmain(for example/bin/shENOENT, symlink EPERM, unrelated snapshots/fixture assertions).pnpm vitest run --config config/vitest.config.ts src/main/providers/windows-shell-args.test.ts src/main/runtime/orca-runtime-terminal-cwd.test.ts✅pnpm buildManual validation: launched Orca dev on Windows with a PowerShell profile that normally changes directory, opened a worktree terminal, and confirmed
Get-Locationstays in the selected worktree instead of the profile's default directory.AI Review Report
Codex reviewed the change for the terminal startup flow and checked:
wsl.exe, Git Bash, andcmd.exelaunch paths are unchanged.-LiteralPathso spaces and apostrophes do not become globbing or injection issues.No follow-up code changes were needed after review.
Security Audit
Reviewed command execution and path handling risks:
quoteStartupArg(..., 'powershell')).Set-Location -LiteralPathavoids wildcard expansion and handles special path characters safely.validateWorkingDirectoryand guarded cwd resolution remain in place before spawning the PTY.Follow-up: none known.
Notes
Documents\Workspace, causing Orca worktree terminals and agent launches to start outside the selected worktree.