From 796baaabe22e7a1f40e182703bbc83102a365716 Mon Sep 17 00:00:00 2001 From: Cecic <1518585189@qq.com> Date: Sat, 8 Aug 2026 18:08:59 +0800 Subject: [PATCH] fix(installer): prevent cleanup from deleting pending release --- src/installer/install.mjs | 49 ++++++++++++++----- test/installer/fork-install.test.mjs | 71 +++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 15 deletions(-) diff --git a/src/installer/install.mjs b/src/installer/install.mjs index c8d220e..2a5229d 100644 --- a/src/installer/install.mjs +++ b/src/installer/install.mjs @@ -56,10 +56,10 @@ export const FORK_INSTALL_STAGES = Object.freeze([ "verify-fork", "extract-fork", "smoke-version", - "move-release", "prepare-bin", "path-add", - "state-switch" + "state-switch", + "move-release" ]); const ENGLISH_MESSAGES = Object.fromEntries( @@ -747,6 +747,7 @@ export async function installForkFromProvider(options) { let stagingRoot = null; let pathAdded = false; let stateSwitched = false; + let releaseCommitted = false; try { const rawManifest = await runStage( options, @@ -804,19 +805,10 @@ export async function installForkFromProvider(options) { releaseId, manifest.platform ); - await runStage(options, "move-release", () => - installImmutableDirectory({ - source: extractedRelease, - destination: finalRelease, - compare: async () => - await sameDirectory(extractedRelease, finalRelease, hashFile) - }) - ); - const finalBinary = join(finalRelease, "package", "bin", "codex.exe"); const [binaryHash, binaryStat] = await Promise.all([ - hashFile(finalBinary), - stat(finalBinary) + hashFile(stagedBinary), + stat(stagedBinary) ]); const buildRecord = { releaseId, @@ -852,6 +844,21 @@ export async function installForkFromProvider(options) { } }); stateSwitched = true; + await runStage(options, "move-release", () => + installImmutableDirectory({ + source: extractedRelease, + destination: finalRelease, + compare: async () => + await sameDirectory(extractedRelease, finalRelease, hashFile) + }) + ); + releaseCommitted = true; + + const finalBinaryStat = await stat(finalBinary); + if (finalBinaryStat.mtimeMs !== buildRecord.mtimeMs) { + buildRecord.mtimeMs = finalBinaryStat.mtimeMs; + await writeState(statePath, nextState); + } const cleanup = await pruneInactiveReleases( installRoot, buildRecord.releaseId, @@ -866,6 +873,22 @@ export async function installForkFromProvider(options) { ...cleanup }; } catch (error) { + if (stateSwitched && !releaseCommitted) { + try { + if (oldState === null) { + await rm(statePath, { force: true }); + } else { + await writeState(statePath, oldState); + } + stateSwitched = false; + } catch (rollbackError) { + throw new AggregateError( + [error, rollbackError], + "fork installation failed and state rollback failed", + { cause: error } + ); + } + } if (pathAdded && !stateSwitched) { try { await removePathEntry(join(installRoot, "bin")); diff --git a/test/installer/fork-install.test.mjs b/test/installer/fork-install.test.mjs index d6c6b7f..2914d9e 100644 --- a/test/installer/fork-install.test.mjs +++ b/test/installer/fork-install.test.mjs @@ -12,8 +12,11 @@ import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import test from "node:test"; -import { installForkFromProvider } from "../../src/installer/install.mjs"; -import { readState } from "../../src/state/store.mjs"; +import { + installForkFromProvider, + pruneInactiveReleases +} from "../../src/installer/install.mjs"; +import { readState, writeStateAtomic } from "../../src/state/store.mjs"; function hash(bytes) { return `sha256:${createHash("sha256").update(bytes).digest("hex")}`; @@ -125,6 +128,70 @@ test("fork install defers cleanup when the running Codex locks the old release", ); }); +test( + "fork install hides the next release from an older cleanup worker until state switches", + async (t) => { + const installRoot = await mkdtemp(join(tmpdir(), "ccu-fork-install-race-")); + t.after(() => rm(installRoot, { recursive: true, force: true })); + await installRevision(installRoot, 1); + let cleanupRaced = false; + + const second = await installRevision(installRoot, 2, { + writeStateAtomic: async (path, state) => { + if (state.active.releaseId === "0.144.5-ccu.i18n.2") { + assert.deepEqual(await readdir(join(installRoot, "releases")), [ + "0.144.5-ccu.i18n.1" + ]); + await pruneInactiveReleases(installRoot, "0.144.5-ccu.i18n.1"); + cleanupRaced = true; + } + await writeStateAtomic(path, state); + } + }); + + assert.equal(cleanupRaced, true); + assert.deepEqual( + await readFile(second.result.state.active.binaryPath), + second.binaryBytes + ); + assert.deepEqual(await readdir(join(installRoot, "releases")), [ + "0.144.5-ccu.i18n.2" + ]); + } +); + +test("fork install restores the previous state when the final move loses a race", async (t) => { + const installRoot = await mkdtemp(join(tmpdir(), "ccu-fork-install-rollback-")); + t.after(() => rm(installRoot, { recursive: true, force: true })); + await installRevision(installRoot, 1); + const statePath = join(installRoot, "state.json"); + + await assert.rejects( + installRevision(installRoot, 2, { + writeStateAtomic: async (path, state) => { + await writeStateAtomic(path, state); + if (state.active.releaseId === "0.144.5-ccu.i18n.2") { + const conflict = join( + installRoot, + "releases", + state.active.releaseId, + state.active.platform, + "package", + "bin", + "codex.exe" + ); + await mkdir(dirname(conflict), { recursive: true }); + await writeFile(conflict, "conflicting release", "utf8"); + } + } + }), + /immutable destination already exists with different content/ + ); + + const stored = await readState(statePath); + assert.equal(stored.active.releaseId, "0.144.5-ccu.i18n.1"); +}); + test("fork install quarantines a strict markerless release residue before reinstalling", async (t) => { const installRoot = await mkdtemp(join(tmpdir(), "ccu-fork-residue-")); t.after(() => rm(installRoot, { recursive: true, force: true }));