Files
orca/src/shared/powershell-native-argument.ts
T
OrcaWinandOrca Worker 5df4fef917 fix(windows): codex and claude get a prompt's double quotes intact in PowerShell (#30424)
* fix(windows): codex and claude get a prompt's double quotes in Windows PowerShell

Windows PowerShell 5.1 (and pwsh before 7.3) does not escape an embedded
`"` when it builds a native program's command line, so every quote in a
launch prompt was dropped, and quotes could split the prompt into extra
arguments. Orca's PowerShell codex and claude wrappers now escape their
arguments at runtime for the PowerShell that is actually running, through
one shared __OrcaNativeArgs function. pwsh 7.3+ already escapes, so it
gets the text untouched.

* fix(windows): escape agent args for .cmd shims under pwsh 7.3+ too

pwsh 7.3+ defaults to the Windows argument mode, which still passes
.cmd/.bat targets the Legacy way, so a codex or claude .cmd shim kept
dropping quotes there. The function now takes the program it escapes
for and names the modes that escape by themselves, and anchors the 5.1
trailing-backslash fix at the true end of the text (`$` also matched
before a final newline). Tests now run the real codex wrapper with a
quoted prompt, cover pwsh's Legacy mode and the newline case, and pin
the claude wrapper's call.

* fix(windows): strip comment lines from the PowerShell bootstrap payload

The shared escaping function pushed the encoded bootstrap to 27,324 of
the 28,000-character budget, leaving almost no room for a startup
command to ride in -EncodedCommand, so short commands fell back to
stdin (the path #7236 broke). Comment lines document the source but
only spend that budget in the payload; dropping them frees 3,732
encoded characters, more headroom than main had.

* fix(windows): quote agent args with "" where cmd.exe or Windows PowerShell reads them

Escaping embedded quotes as \" let pwsh 7.3+'s .cmd quoting expose a
prompt's `&` to cmd.exe, which then ran the rest as a command. Where
cmd.exe reads the line, or Windows PowerShell counts quotes naively,
the function now quotes the argument itself and writes each quote as
"", as process-host's windows-command-line.ts already does. That also
delivers text 5.1 used to strip, so its special cases go away.

The codex and claude wrappers now come as one fragment with the
function they call, the bootstrap test pins the startup-command budget
instead of restating the strip, and the cwd-restore comment leaves the
payload too.

---------

Co-authored-by: Orca Worker <orca-worker@localhost>
2026-10-18 17:29:54 -07:00

54 lines
2.5 KiB
TypeScript

export function quotePowerShellLiteral(value: string): string {
if (/[\r\n]/.test(value)) {
return quotePowerShellMultilineLiteral(value)
}
// Why: PowerShell also ends single-quoted strings at typographic single quotes.
return `'${value.replace(/['\u2018\u2019\u201A\u201B]/g, '$&$&')}'`
}
/**
* Why one physical line: a raw line break typed into PowerShell submits the line, and Windows
* PowerShell 5.1 without PSReadLine then waits at `>>` for an empty line that never comes.
* Backtick escapes keep every byte, so `\r\n` stays `\r\n` exactly as the single-quoted form
* delivered it.
*/
function quotePowerShellMultilineLiteral(value: string): string {
const escaped = value.replace(/[`$"\u201C\u201D\u201E\r\n]/g, (char) =>
char === '\r' ? '`r' : char === '\n' ? '`n' : `\`${char}`
)
return `"${escaped}"`
}
/**
* PowerShell function that escapes arguments for `$Program`, for Orca's agent wrappers to splat.
*
* Why at runtime: the escaping depends on the PowerShell reading the line. Legacy argv passing
* (Windows PowerShell 5.1, pwsh before 7.3, and 7.3's `Windows` mode for `.cmd`/`.bat`) leaves
* embedded `"` unescaped, so the program drops them; otherwise PowerShell escapes them itself and
* must get the text untouched.
*
* Where cmd.exe reads the line, or Windows PowerShell decides quoting by counting `"` naively, the
* argument is quoted here with `""` for each quote, as process-host's windows-command-line.ts does:
* a `\"` would flip cmd's quote parity and turn a later `&` into an operator.
*/
export function getPowerShellEscapeNativeArgsFunction(): string {
return `function Global:__OrcaEscapeNativeArgs([string]$Program, [object[]]$Arguments) {
Set-StrictMode -Off
$mode = $PSNativeCommandArgumentPassing
$cmd = $Program -match '\\.(bat|cmd)$'
if ($mode -eq 'Standard' -or ($mode -eq 'Windows' -and -not $cmd)) { return $Arguments }
$selfQuote = $cmd -or $PSVersionTable.PSVersion.Major -lt 6
foreach ($arg in $Arguments) {
if ($arg -isnot [string] -or $arg -notmatch '["\\\\]') { $arg; continue }
if ($selfQuote) { '"' + (($arg -replace '(\\\\*)"', '$1$1""') -replace '(\\\\+)\\z', '$1$1') + '"' }
else { $arg -replace '(\\\\*)"', '$1$1\\"' }
}
}`
}
export function quotePowerShellNativeArgument(value: string): string {
// Why: Windows PowerShell 5.1 drops unescaped embedded quotes when it
// constructs argv for native executables such as wsl.exe.
return quotePowerShellLiteral(value.replace(/(\\*)"/g, '$1$1\\"'))
}