@bitkyc08/opencodex
Version:
Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code
107 lines (94 loc) • 6.66 kB
text/typescript
/** Remote platform is discovered only through the link's pinned SSH host key. */
import { buildExecArgv, buildWindowsProbeArgv, REMOTE_COMMAND_NOT_FOUND, REMOTE_PLATFORM_UNSUPPORTED } from "./ssh-argv";
import type { ExplicitRoute } from "./explicit-route";
import { SshDiagnosticError, sshFailureHint, type SshRunner, type SshRunResult } from "./ssh-runner";
export type RemotePlatform = "windows" | "posix" | "unknown";
const WINDOWS_UNAME = /^(?:Windows|MSYS|MINGW|CYGWIN)/i;
const POSIX_UNAME = /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/;
// Windows PowerShell says "the name of a cmdlet", PowerShell 7 "a name of a cmdlet". Localized
// installs say neither, which is what confirmWindowsShell is for.
const WINDOWS_SHELL_REFUSAL = /is not recognized as (?:an internal or external command|(?:the|a) name of a cmdlet)/i;
/** ssh's own failure status: the remote shell never ran, so there is nothing to ask. */
const SSH_TRANSPORT_FAILURE = 255;
/** Remote argv that prints the kernel name; under cmd or PowerShell, `sh` itself is refused. */
export const REMOTE_UNAME_ARGV: readonly string[] = ["sh", "-c", "uname -s 2>/dev/null", "ocx-platform"];
const REMOTE_FRAMED_UNAME_ARGV: readonly string[] = ["sh", "-c", "printf 'ocx-platform:%s\\n' \"$(uname -s 2>/dev/null)\"", "ocx-platform"];
/** Does this output come from cmd.exe or PowerShell refusing a POSIX command? */
export function isWindowsShellRefusal(output: string): boolean {
return WINDOWS_SHELL_REFUSAL.test(output);
}
/**
* Is this one line cmd's answer to WINDOWS_OS_PROBE? cmd may print the closing quote of the
* `cmd.exe /c "<command>"` line Win32-OpenSSH builds (#8972), so one trailing `"` is accepted;
* nothing else is.
*/
export function isWindowsNtAnswer(line: string): boolean {
return /^Windows_NT"?$/.test(line.trim());
}
/** A platform failure that tells the dashboard why: the message, then the failing call's stderr reason. */
function platformError(message: string, failed: SshRunResult): SshDiagnosticError {
const reason = sshFailureHint(failed.stderr);
return new SshDiagnosticError(message, reason ? `${message}: ${reason}` : message);
}
/** Classify one remote command result. Transport failures and odd output stay unknown. */
export function classifyRemotePlatform(result: SshRunResult): RemotePlatform {
if (result.code === REMOTE_PLATFORM_UNSUPPORTED) return "windows";
if (isWindowsShellRefusal(result.stderr) || isWindowsShellRefusal(result.stdout)) return "windows";
if (result.code !== 0) return "unknown";
const name = result.stdout.trim().split(/\r?\n/)[0] ?? "";
if (WINDOWS_UNAME.test(name)) return "windows";
return POSIX_UNAME.test(name) ? "posix" : "unknown";
}
/**
* Could this failure be a Windows shell whose refusal text is localized? Success, the prelude's own
* 125/127 answers and ssh transport failures are already explained, so they never cost another call.
*/
export function mayBeWindowsShell(result: SshRunResult): boolean {
return result.code !== 0 && result.code !== REMOTE_PLATFORM_UNSUPPORTED
&& result.code !== REMOTE_COMMAND_NOT_FOUND && result.code !== SSH_TRANSPORT_FAILURE;
}
/** Ask the host the fixed, locale-independent WINDOWS_OS_PROBE. Any failure answers false. */
export async function confirmWindowsShell(runner: SshRunner, alias: string, route: ExplicitRoute | null, knownHostsFile: string, timeoutMs = 30_000): Promise<boolean> {
try {
const result = await runner.run(buildWindowsProbeArgv({ alias, knownHostsFile, route }), { timeoutMs });
return result.code === 0 && isWindowsNtAnswer(result.stdout.trim().split(/\r?\n/)[0] ?? "");
} catch { return false; }
}
/** classifyRemotePlatform, plus the locale-independent follow-up when the answer could be Windows. */
export async function detectRemotePlatform(runner: SshRunner, alias: string, route: ExplicitRoute | null, knownHostsFile: string, result: SshRunResult): Promise<RemotePlatform> {
const platform = classifyRemotePlatform(result);
if (platform !== "unknown" || !mayBeWindowsShell(result)) return platform;
return await confirmWindowsShell(runner, alias, route, knownHostsFile) ? "windows" : "unknown";
}
/** Windows when the fixed %OS% probe answers Windows_NT, otherwise POSIX from uname; never a guess. */
export async function requireRemotePlatform(
runner: Pick<SshRunner, "run">,
alias: string,
/** The link's explicit route (src/link/explicit-route.ts), or null. */
route: ExplicitRoute | null,
knownHostsFile: string,
/** A route to the same machine (HostKeyAlias stays alias), a tighter deadline, and an abort. */
over: { destination?: string; timeoutMs?: number; signal?: AbortSignal } = {},
): Promise<"posix" | "windows"> {
const timeoutMs = over.timeoutMs ?? 10_000;
const runOptions = over.signal ? { timeoutMs, signal: over.signal } : { timeoutMs };
const windows = await runner.run(buildWindowsProbeArgv({ alias, knownHostsFile, destination: over.destination, route }), runOptions);
if (windows.code === 255) throw platformError("SSH platform discovery failed", windows);
if (windows.code === 0 && isWindowsNtAnswer(windows.stdout)) return "windows";
const result = await runner.run(buildExecArgv({ alias, knownHostsFile, destination: over.destination, argv: REMOTE_UNAME_ARGV, route }), runOptions);
const platform = classifyRemotePlatform(result);
if (platform === "unknown") throw platformError("Remote platform could not be determined", result);
return platform;
}
/** Fan-out ignores login-shell output and trusts only one framed uname line. */
export async function requireRemotePlatformFramed(runner: Pick<SshRunner, "run">, alias: string, route: ExplicitRoute | null, knownHostsFile: string): Promise<"posix" | "windows"> {
const windows = await runner.run(buildWindowsProbeArgv({ alias, knownHostsFile, route }), { timeoutMs: 10_000 });
if (windows.code === SSH_TRANSPORT_FAILURE) throw platformError("SSH platform discovery failed", windows);
if (windows.code === 0 && windows.stdout.split(/\r?\n/).some(isWindowsNtAnswer)) return "windows";
const result = await runner.run(buildExecArgv({ alias, knownHostsFile, argv: REMOTE_FRAMED_UNAME_ARGV, route }), { timeoutMs: 10_000 });
if (result.code === SSH_TRANSPORT_FAILURE) throw platformError("SSH platform discovery failed", result);
const frames = result.stdout.split(/\r?\n/).filter(line => line.startsWith("ocx-platform:"));
const name = frames.length === 1 ? frames[0]!.slice("ocx-platform:".length) : "";
if (result.code !== 0 || !POSIX_UNAME.test(name)) throw platformError("Remote platform could not be determined", result);
return WINDOWS_UNAME.test(name) ? "windows" : "posix";
}