fix(server): Windows Codex install uses system tar and retries rename #18523
Mergedmweinbach(GitHub profile, opens in a new tab)merged 9 commits into
main fromrefs/pull/18523/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
coderabbitai[bot](GitHub profile, opens in a new tab) requested changes
apps/server/src/provider/CodexInstallation.ts line 718OutdatedResolvedby mweinbach(GitHub profile, opens in a new tab)@@ -691,3 +700,19 @@691700 );692701693702 yield* Effect.gen(function* () {703+ // Clear staging directories left behind by an interrupted install so they704+ // neither leak disk nor make an otherwise empty runtime look removable.705+ if (yield* fs.exists(managedDirectory)) {706+ const entries = yield* fs707+ .readDirectory(managedDirectory)708+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));709+ for (const entry of entries) {710+ if (entry.startsWith(".install-") || entry.startsWith(".active-")) {711+ yield* fs712+ .remove(path.join(managedDirectory, entry), { recursive: true, force: true })713+ .pipe(Effect.ignore);714+ }715+ }716+ const remaining = yield* fs717+ .readDirectory(managedDirectory)718+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));- apps/server/src/provider/CodexInstallation.ts lines 563 to 565Resolvedby mweinbach(GitHub profile, opens in a new tab)@@ -552,4 +556,9 @@552556 }),553557 { flag: "wx", mode: 0o600 },554558 );555- yield* fs.rename(runtime, destination);559+ // On Windows, Defender and kernel process rundown can briefly hold the560+ // 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;apps/server/src/provider/CodexInstallation.ts lines 710 to 713OutdatedResolvedby mweinbach(GitHub profile, opens in a new tab)@@ -691,3 +700,14 @@691700 );692701693702 yield* Effect.gen(function* () {703+ // Clear staging directories left behind by an interrupted install so they704+ // neither leak disk nor make an otherwise empty runtime look removable.705+ if (yield* fs.exists(managedDirectory)) {706+ const entries = yield* fs707+ .readDirectory(managedDirectory)708+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));709+ for (const entry of entries) {710+ if (entry.startsWith(".install-") || entry.startsWith(".active-")) {711+ yield* fs712+ .remove(path.join(managedDirectory, entry), { recursive: true, force: true })713+ .pipe(Effect.ignore);apps/server/src/provider/CodexInstallation.ts lines 718 to 720OutdatedResolvedby mweinbach(GitHub profile, opens in a new tab)@@ -691,3 +700,21 @@691700 );692701693702 yield* Effect.gen(function* () {703+ // Clear staging directories left behind by an interrupted install so they704+ // neither leak disk nor make an otherwise empty runtime look removable.705+ if (yield* fs.exists(managedDirectory)) {706+ const entries = yield* fs707+ .readDirectory(managedDirectory)708+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));709+ for (const entry of entries) {710+ if (entry.startsWith(".install-") || entry.startsWith(".active-")) {711+ yield* fs712+ .remove(path.join(managedDirectory, entry), { recursive: true, force: true })713+ .pipe(Effect.ignore);714+ }715+ }716+ const remaining = yield* fs717+ .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);
- apps/server/src/provider/CodexInstallation.ts line 716OutdatedResolvedby mweinbach(GitHub profile, opens in a new tab)@@ -691,3 +700,17 @@691700 );692701693702 yield* Effect.gen(function* () {703+ // Clear staging directories left behind by an interrupted install so they704+ // neither leak disk nor make an otherwise empty runtime look removable.705+ if (yield* fs.exists(managedDirectory)) {706+ const entries = yield* fs707+ .readDirectory(managedDirectory)708+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));709+ for (const entry of entries) {710+ if (entry.startsWith(".install-") || entry.startsWith(".active-")) {711+ yield* fs712+ .remove(path.join(managedDirectory, entry), { recursive: true, force: true })713+ .pipe(Effect.ignore);714+ }715+ }716+ // An unreadable directory is not an empty one; never delete on a failed read.
- apps/server/src/provider/CodexInstallation.ts line 728OutdatedResolvedby mweinbach(GitHub profile, opens in a new tab)@@ -693,0 +708,21 @@708+ if (yield* fs.exists(managedDirectory)) {709+ const entries = yield* fs710+ .readDirectory(managedDirectory)711+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));712+ const now = yield* Clock.currentTimeMillis;713+ for (const entry of entries) {714+ if (!entry.startsWith(".install-") && !entry.startsWith(".active-")) continue;715+ const target = path.join(managedDirectory, entry);716+ const info = yield* fs.stat(target).pipe(Effect.option);717+ if (Option.isNone(info)) continue;718+ const modifiedAt = Option.match(info.value.mtime, {719+ onNone: () => now,720+ onSome: (date) => date.getTime(),721+ });722+ if (now - modifiedAt < STAGING_STALE_AFTER_MS) continue;723+ yield* fs.remove(target, { recursive: true, force: true }).pipe(Effect.ignore);724+ }725+ // An unreadable directory is not an empty one; never delete on a failed read.726+ const remaining = yield* fs.readDirectory(managedDirectory).pipe(Effect.option);727+ if (Option.isSome(remaining) && remaining.value.length === 0) {728+ yield* fs.remove(managedDirectory, { recursive: true, force: true }).pipe(Effect.ignore);
- apps/server/src/provider/CodexInstallation.test.ts line 65OutdatedResolvedby mweinbach(GitHub profile, opens in a new tab)@@ -50,3 +58,8 @@5058 let downloads = 0;5159 const crypto = yield* Crypto.Crypto;5260 const sha256 = Hex.encode(yield* crypto.digest("SHA-256", archive).pipe(Effect.orDie));61+ let runtimeRenameFailures = input.runtimeRenameFailures ?? 0;62+ const lockingFs: FileSystem.FileSystem = {63+ ...fs,64+ rename: (from, to) =>65+ /[/\\]runtime$/u.test(from) && runtimeRenameFailures-- > 0
- apps/server/src/provider/CodexInstallation.test.ts line 469OutdatedResolvedby mweinbach(GitHub profile, opens in a new tab)@@ -360,0 +449,21 @@449+ readonly command: string;450+ readonly args: ReadonlyArray<string>;451+ };452+ commands.push(executable);453+ const names = Object.keys(windowsPackage);454+ if (args[0] === "-tzf") return processHandle(`${names.join("\r\n")}\r\n`);455+ if (args[0] === "-tvzf")456+ return processHandle(`${names.map((name) => `-rwxr-xr-x 0 0 ${name}`).join("\r\n")}\r\n`);457+ const runtime = args[3] ?? "";458+ for (const [name, contents] of Object.entries(windowsPackage)) {459+ yield* fs.makeDirectory(`${runtime}/${name.split("/").slice(0, -1).join("/")}`, {460+ recursive: true,461+ });462+ yield* fs.writeFileString(`${runtime}/${name}`, contents);463+ }464+ return processHandle("");465+ }).pipe(Effect.orDie),466+ );467+468+// Live clock: the Windows rename retry waits between attempts.469+it.live("installs on Windows via System32 tar with CRLF output and a locked runtime", () =>
- apps/server/src/provider/CodexInstallation.ts lines 724 to 725Resolvedby mweinbach(GitHub profile, opens in a new tab)@@ -694,0 +704,21 @@704+ // Clear staging directories left behind by an interrupted install so they705+ // neither leak disk nor make an otherwise empty runtime look removable. Two706+ // servers can share a home, so one touched within the last hour may be707+ // another server's install in progress and stays.708+ let canRemove = false;709+ if (yield* fs.exists(managedDirectory)) {710+ const entries = yield* fs711+ .readDirectory(managedDirectory)712+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));713+ const now = yield* Clock.currentTimeMillis;714+ for (const entry of entries) {715+ if (!entry.startsWith(".install-") && !entry.startsWith(".active-")) continue;716+ const target = path.join(managedDirectory, entry);717+ const info = yield* fs.stat(target).pipe(Effect.option);718+ if (Option.isNone(info)) continue;719+ const modifiedAt = Option.match(info.value.mtime, {720+ onNone: () => now,721+ onSome: (date) => date.getTime(),722+ });723+ if (now - modifiedAt < STAGING_STALE_AFTER_MS) continue;724+ yield* fs.remove(target, { recursive: true, force: true }).pipe(Effect.ignore);
coderabbitai[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge merged this364f508
juliusmarminge closed this
Sign in to join this conversation.
Codex Review Summary#
This comment shows the latest Codex review activity on this pull request.
0dac4ddℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.