Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions src/installer/install.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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"));
Expand Down
71 changes: 69 additions & 2 deletions test/installer/fork-install.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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")}`;
Expand Down Expand Up @@ -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 }));
Expand Down
Loading