Files
orca/src/main/powershell-osc133-bootstrap.ts
T
98d02bca47 fix: support windows ssh hosts (#5004)
* feat: add windows ssh relay base support

* feat: support windows ssh relay runtime services

* fix: default windows ssh pty cwd to user profile

* fix: support windows hosts over system ssh

* fix: preserve degraded windows relay native deps

* fix: gate windows shell args by relay platform

* fix: preserve windows relay fallback pipes

* test: align windows native deps relay fixture

* fix: build valid windows install lock command

* fix: address windows SSH relay review findings

Resolve correctness, efficiency, and reuse issues found reviewing the
Windows SSH native-host support:

- GC liveness on Windows now probes the actual named pipe (via node
  net.connect against markers + deterministic candidates) instead of
  substring-matching Win32_Process command lines, which could remove a
  live relay dir. Reports ALIVE conservatively only when there is no
  liveness signal at all (no markers and no seed pipes).
- Resolve the remote node path once per deploy and thread it through
  install/repair/launch instead of re-resolving 3-7x.
- Replace the 200ms node -e poll loop with a single long-lived remote
  wait process during Windows relay startup.
- Skip the no-op executable command on Windows in uploadRelay.
- Make the Windows fallback pipe name deterministic and recoverable
  (drop the global counter), with an extra reconnect attempt.
- Normalize the prepended node bin dir to backslashes on Windows PATH.
- Batch the system-SSH Windows directory upload into a single streamed
  JSON package instead of one ssh process per file.
- Extract relay endpoint/marker helpers into ssh-relay-endpoints.ts and
  consolidate the PowerShell EncodedCommand encoding into the shared
  powershell-command-encoding module.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Support cancellation and timeouts in Windows port scanning

- Propagate the request AbortSignal and a 5-second timeout to both
  PowerShell and netstat child processes during Windows port scanning.
- Avoid spawning the netstat fallback process if the port scan has
  already been aborted.
- Wrap the .NET OSArchitecture check in a try/catch block during SSH
  Windows platform detection to robustly fall back to environment
  variables if needed.

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
2026-06-09 01:17:34 -07:00

87 lines
3.3 KiB
TypeScript

import { getPowerShellOmpShellWrapper } from './pty/omp-shell-wrapper'
export { encodePowerShellCommand } from '../shared/powershell-command-encoding'
const POWERSHELL_OSC133_BOOTSTRAP = `# Orca OSC 133 shell integration for PowerShell.
if ((Test-Path variable:global:__OrcaOsc133State) -and
$null -ne $Global:__OrcaOsc133State.OriginalPrompt) {
return
}
if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") {
return
}
# Profiles have already loaded normally by the time -EncodedCommand runs.
# Wrap the user's final prompt/readline state; do not source profiles here.
# Preserve Windows CJK output by keeping ConPTY on UTF-8 without bypassing
# profile loading or execution-policy checks.
try {
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
[Console]::InputEncoding = [System.Text.UTF8Encoding]::new()
$OutputEncoding = [Console]::OutputEncoding
} catch { Write-Error $_ -ErrorAction Continue }
# Profiles can re-export user defaults after Orca's spawn env is set.
if ($env:ORCA_OPENCODE_CONFIG_DIR) { $env:OPENCODE_CONFIG_DIR = $env:ORCA_OPENCODE_CONFIG_DIR }
if ($env:ORCA_PI_CODING_AGENT_DIR) { $env:PI_CODING_AGENT_DIR = $env:ORCA_PI_CODING_AGENT_DIR }
if (-not $env:ORCA_PI_CODING_AGENT_DIR -and $env:ORCA_OMP_CODING_AGENT_DIR) {
$env:PI_CODING_AGENT_DIR = $env:ORCA_OMP_CODING_AGENT_DIR
}
${getPowerShellOmpShellWrapper()}
if ($env:ORCA_CODEX_HOME) { $env:CODEX_HOME = $env:ORCA_CODEX_HOME }
$Global:__OrcaOsc133State = @{
OriginalPrompt = $function:prompt
OriginalReadLine = $function:PSConsoleHostReadLine
HasSeenPrompt = $false
HasPSReadLine = $null -ne (Get-Module -Name PSReadLine)
Esc = [char]27
Bel = [char]7
}
function Global:prompt {
# Capture FIRST; any other expression can clobber PowerShell's success bit.
$fakeExitCode = [int](!$global:?)
Set-StrictMode -Off
$result = ""
# Emit D from prompt, not readline state. Some profile setups bypass
# PSConsoleHostReadLine; the consumer only needs completion.
if ($Global:__OrcaOsc133State.HasSeenPrompt) {
$result += "$($Global:__OrcaOsc133State.Esc)]133;D;$fakeExitCode$($Global:__OrcaOsc133State.Bel)"
}
$Global:__OrcaOsc133State.HasSeenPrompt = $true
$result += "$($Global:__OrcaOsc133State.Esc)]133;A$($Global:__OrcaOsc133State.Bel)"
# Preserve the previous success/failure value for prompts that inspect it.
if ($fakeExitCode -ne 0) { Write-Error "failure" -ea ignore }
$result += $Global:__OrcaOsc133State.OriginalPrompt.Invoke()
$result += "$($Global:__OrcaOsc133State.Esc)]133;B$($Global:__OrcaOsc133State.Bel)"
$result
}
if ($Global:__OrcaOsc133State.HasPSReadLine -and
$null -ne $Global:__OrcaOsc133State.OriginalReadLine) {
function Global:PSConsoleHostReadLine {
$commandLine = $Global:__OrcaOsc133State.OriginalReadLine.Invoke()
[Console]::Write("$($Global:__OrcaOsc133State.Esc)]133;C$($Global:__OrcaOsc133State.Bel)")
return $commandLine
}
}
`
export function getPowerShellOsc133Bootstrap(): string {
return POWERSHELL_OSC133_BOOTSTRAP
}
export function isPowerShellExecutableName(shellName: string): boolean {
const normalized = shellName.toLowerCase()
return (
normalized === 'pwsh' ||
normalized === 'pwsh.exe' ||
normalized === 'powershell' ||
normalized === 'powershell.exe'
)
}