Skip to content

Commit 92383ff

Browse files
mweinbachclaude
andcommitted
fix(server): Windows Codex install uses system tar and retries rename
Use System32 tar.exe instead of bare `tar` (which can resolve to Git's MSYS tar and misread drive letters), split tar output on CRLF, retry the staging rename while Windows briefly holds the verified codex.exe, and sweep stale staging directories at startup. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6f6197c commit 92383ff

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

‎apps/server/src/provider/CodexInstallation.ts‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @effect-diagnostics nodeBuiltinImport:off - Effect has no incremental digest.
22
import { ProviderDriverKind, type ProviderInstallState } from "@t3tools/contracts";
3+
import { cliArchiveTarCommand } from "@t3tools/shared/cliRelease";
34
import * as HostProcess from "@t3tools/shared/HostProcess";
45
import { resolveCommandPath, resolveSpawnCommand } from "@t3tools/shared/shell";
56
import * as Clock from "effect/Clock";
@@ -13,6 +14,7 @@ import * as FileSystem from "effect/FileSystem";
1314
import * as Layer from "effect/Layer";
1415
import * as Option from "effect/Option";
1516
import * as Path from "effect/Path";
17+
import * as Schedule from "effect/Schedule";
1618
import * as Schema from "effect/Schema";
1719
import * as Scope from "effect/Scope";
1820
import * as Semaphore from "effect/Semaphore";
@@ -511,8 +513,10 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
511513
message: "Extracting Codex.",
512514
}) satisfies ProviderInstallState,
513515
);
514-
const entries = (yield* runCommand("tar", ["-tzf", archivePath])).trim().split("\n");
515-
const types = (yield* runCommand("tar", ["-tvzf", archivePath])).trim().split("\n");
516+
// Bare `tar` on Windows can resolve to Git's MSYS tar, which reads `C:\...` as a remote host.
517+
const tarCommand = cliArchiveTarCommand(platform, environment);
518+
const entries = (yield* runCommand(tarCommand, ["-tzf", archivePath])).trim().split(/\r?\n/u);
519+
const types = (yield* runCommand(tarCommand, ["-tvzf", archivePath])).trim().split(/\r?\n/u);
516520
if (
517521
entries.length > 10000 ||
518522
entries.length !== types.length ||
@@ -528,7 +532,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
528532
})
529533
)
530534
return yield* installationError("extract", "The Codex archive contains unsafe entries.");
531-
yield* runCommand("tar", ["-xzf", archivePath, "-C", runtime]);
535+
yield* runCommand(tarCommand, ["-xzf", archivePath, "-C", runtime]);
532536
yield* SubscriptionRef.update(
533537
state,
534538
(current) =>
@@ -552,7 +556,12 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
552556
}),
553557
{ flag: "wx", mode: 0o600 },
554558
);
555-
yield* fs.rename(runtime, destination);
559+
// On Windows, Defender and kernel process rundown can briefly hold the
560+
// verified codex.exe handle open right after `--version` exits.
561+
const renameRuntime = fs.rename(runtime, destination);
562+
yield* platform === "win32"
563+
? renameRuntime.pipe(Effect.retry({ times: 10, schedule: Schedule.spaced("100 millis") }))
564+
: renameRuntime;
556565
yield* activate();
557566
},
558567
Effect.scoped,
@@ -691,6 +700,26 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
691700
);
692701

693702
yield* Effect.gen(function* () {
703+
// Clear staging directories left behind by an interrupted install so they
704+
// neither leak disk nor make an otherwise empty runtime look removable.
705+
if (yield* fs.exists(managedDirectory)) {
706+
const entries = yield* fs
707+
.readDirectory(managedDirectory)
708+
.pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));
709+
for (const entry of entries) {
710+
if (entry.startsWith(".install-") || entry.startsWith(".active-")) {
711+
yield* fs
712+
.remove(path.join(managedDirectory, entry), { recursive: true, force: true })
713+
.pipe(Effect.ignore);
714+
}
715+
}
716+
const remaining = yield* fs
717+
.readDirectory(managedDirectory)
718+
.pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));
719+
if (remaining.length === 0) {
720+
yield* fs.remove(managedDirectory, { recursive: true, force: true }).pipe(Effect.ignore);
721+
}
722+
}
694723
const canRemove = yield* fs.exists(managedDirectory);
695724
yield* SubscriptionRef.update(state, (current) => ({ ...current, canRemove }));
696725
if (yield* reuseLocal()) return;

0 commit comments

Comments
 (0)