fix: optimize Windows apply-patch fs path

This commit is contained in:
Codex
2026-06-26 17:05:09 +00:00
parent 9bce21b9ef
commit 624904c383
5 changed files with 404 additions and 22 deletions
+71 -10
View File
@@ -5,6 +5,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { type UniDeskConfig, repoRoot } from "./config";
import {
ApplyPatchV2Error,
decodeApplyPatchV2BulkRead,
formatApplyPatchV2BulkReplacementPayload,
isApplyPatchV2HelpArgs,
@@ -2860,18 +2861,32 @@ type WindowsApplyPatchFsOperation =
const windowsApplyPatchWriteB64ChunkChars = 12_000;
function createWindowsApplyPatchFileSystem(config: UniDeskConfig, invocation: ParsedSshInvocation): ApplyPatchV2FileSystem {
export function createWindowsApplyPatchFileSystem(invocation: ParsedSshInvocation, runRemoteCommand: (remoteCommand: string, input?: string) => Promise<SshCaptureResult>): ApplyPatchV2FileSystem {
async function checked(operation: WindowsApplyPatchFsOperation, args: string[], input?: string): Promise<SshCaptureResult> {
const command = buildWindowsPowerShellInvocation(windowsApplyPatchFsScript(invocation.route.workspace, operation, args));
const result = await runSshCaptureRemoteCommand(config, invocation, command, input);
const startedAtMs = Date.now();
let result: SshCaptureResult;
try {
result = await runRemoteCommand(command, input);
} catch (error) {
throw new ApplyPatchV2Error(`windows apply-patch fs operation failed: ${operation}`, windowsApplyPatchFsFailureDetails({
operation,
args,
input,
invocation,
remoteElapsedMs: Math.max(0, Date.now() - startedAtMs),
cause: error instanceof Error ? { name: error.name, message: error.message } : { message: String(error) },
}));
}
if (result.exitCode === 0) return result;
throw new Error(`windows apply-patch fs operation failed: ${operation} ${JSON.stringify({
route: invocation.route.raw,
args: args.slice(0, 4),
exitCode: result.exitCode,
stdout: result.stdout.slice(-2000),
stderr: result.stderr.slice(-4000),
})}`);
throw new ApplyPatchV2Error(`windows apply-patch fs operation failed: ${operation}`, windowsApplyPatchFsFailureDetails({
operation,
args,
input,
invocation,
result,
remoteElapsedMs: Math.max(0, Date.now() - startedAtMs),
}));
}
return {
@@ -2920,6 +2935,52 @@ function createWindowsApplyPatchFileSystem(config: UniDeskConfig, invocation: Pa
};
}
function windowsApplyPatchFsFailureDetails(options: {
operation: WindowsApplyPatchFsOperation;
args: string[];
input?: string;
invocation: ParsedSshInvocation;
result?: SshCaptureResult;
remoteElapsedMs: number;
cause?: Record<string, unknown>;
}): Record<string, unknown> {
const { operation, args, input, invocation, result } = options;
const inputBytes = input === undefined ? 0 : Buffer.byteLength(input, "utf8");
const expected = windowsApplyPatchExpectedWrite(operation, args);
const targetCount = windowsApplyPatchBulkTargetCount(operation, args);
return {
operation,
route: invocation.route.raw,
...(operation === "read-bulk-b64" || operation === "apply-replacements-bulk-stdin" ? {} : { filePath: args[0] ?? "" }),
...(targetCount === null ? {} : { targetCount }),
args: args.slice(0, 4),
inputBytes,
...(inputBytes === 0 ? {} : { inputSha256: createHash("sha256").update(input, "utf8").digest("hex") }),
...(expected.expectedBytes === undefined ? {} : { expectedBytes: expected.expectedBytes }),
...(expected.expectedSha256 === undefined ? {} : { expectedSha256: expected.expectedSha256 }),
remoteElapsedMs: options.remoteElapsedMs,
landed: false,
...(result === undefined ? {} : {
exitCode: result.exitCode,
stdoutTail: result.stdout.slice(-2000),
stderrTail: result.stderr.slice(-4000),
}),
...(options.cause === undefined ? {} : { cause: options.cause }),
};
}
function windowsApplyPatchExpectedWrite(operation: WindowsApplyPatchFsOperation, args: string[]): { expectedBytes?: string; expectedSha256?: string } {
if (operation === "write-b64-stdin") return { expectedBytes: args[1], expectedSha256: args[2] };
if (operation === "write-b64-commit") return { expectedBytes: args[2], expectedSha256: args[3] };
return {};
}
function windowsApplyPatchBulkTargetCount(operation: WindowsApplyPatchFsOperation, args: string[]): number | null {
if (operation !== "read-bulk-b64" && operation !== "apply-replacements-bulk-stdin") return null;
const count = Number(args[0]);
return Number.isSafeInteger(count) && count >= 0 ? count : null;
}
function windowsApplyPatchFsScript(basePath: string | null, operation: WindowsApplyPatchFsOperation, args: string[]): string {
const target = args[0] ?? "";
const arg1 = args[1] ?? "";
@@ -3362,7 +3423,7 @@ export async function runSsh(config: UniDeskConfig, providerId: string, args: st
if (operationName === "apply-patch") {
const applyPatch = effectiveApplyPatchV2Invocation(invocation, normalizedArgs.slice(1));
const executor: ApplyPatchV2Executor = applyPatch.invocation.route.plane === "win"
? { fs: createWindowsApplyPatchFileSystem(config, applyPatch.invocation) }
? { fs: createWindowsApplyPatchFileSystem(applyPatch.invocation, (remoteCommand, input) => runSshCaptureRemoteCommand(config, applyPatch.invocation, remoteCommand, input)) }
: { run: (command, input) => runSshCaptureCommand(config, applyPatch.invocation, command, input) };
return await runApplyPatchV2({
executor,