Skip to content

Commit cd731fb

Browse files
committed
fix(plugins): avoid mutating retained rollback generations
1 parent fbf0b1b commit cd731fb

2 files changed

Lines changed: 112 additions & 4 deletions

File tree

‎src/plugins/install.npm-spec.test.ts‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,89 @@ describe("installPluginFromNpmSpec", () => {
999999
await expect(newModule.default.runAttempt()).resolves.toEqual({ chunk: "new" });
10001000
});
10011001

1002+
it("does not mutate a retained generation when an exact rollback reuses its artifact key", async () => {
1003+
const npmRoot = path.join(suiteTempRootTracker.makeTempDir(), "npm");
1004+
const packageName = "@openclaw/codex";
1005+
const install = async (version: string, options: { mode?: "update" }) =>
1006+
installPluginFromNpmSpec({
1007+
spec: `${packageName}@${version}`,
1008+
npmDir: npmRoot,
1009+
mode: options.mode,
1010+
logger: { info: () => {}, warn: () => {} },
1011+
});
1012+
1013+
mockNpmViewAndInstall({
1014+
spec: `${packageName}@2.0.0`,
1015+
packageName,
1016+
version: "2.0.0",
1017+
pluginId: "codex",
1018+
npmRoot,
1019+
integrity: "sha512-codex-v2",
1020+
shasum: "codexv2sha",
1021+
indexJs: `module.exports = {
1022+
version: "v2",
1023+
runAttempt: async () => (await import("./run-attempt-v2.js")).default,
1024+
};\n`,
1025+
extraDistFiles: {
1026+
"run-attempt-v2.js": "module.exports = { chunk: 'v2' };\n",
1027+
},
1028+
expectedDependencySpec: "2.0.0",
1029+
});
1030+
const first = await install("2.0.0", {});
1031+
expect(first.ok).toBe(true);
1032+
if (!first.ok) {
1033+
return;
1034+
}
1035+
const retainedModule = await import(
1036+
pathToFileURL(path.join(first.targetDir, "dist", "index.js")).href
1037+
);
1038+
const retainedPackageDir = first.targetDir;
1039+
1040+
mockNpmViewAndInstall({
1041+
spec: `${packageName}@3.0.0`,
1042+
packageName,
1043+
version: "3.0.0",
1044+
pluginId: "codex",
1045+
npmRoot,
1046+
integrity: "sha512-codex-v3",
1047+
shasum: "codexv3sha",
1048+
indexJs: "module.exports = { version: 'v3' };\n",
1049+
replaceExisting: true,
1050+
expectedDependencySpec: "3.0.0",
1051+
});
1052+
const update = await install("3.0.0", { mode: "update" });
1053+
expect(update.ok).toBe(true);
1054+
if (!update.ok) {
1055+
return;
1056+
}
1057+
await markRetainedManagedNpmInstall({
1058+
packageDir: retainedPackageDir,
1059+
pluginId: "codex",
1060+
reason: "test-rollback-retention",
1061+
});
1062+
1063+
mockNpmViewAndInstall({
1064+
spec: `${packageName}@2.0.0`,
1065+
packageName,
1066+
version: "2.0.0",
1067+
pluginId: "codex",
1068+
npmRoot,
1069+
integrity: "sha512-codex-v2",
1070+
shasum: "codexv2sha",
1071+
indexJs: "module.exports = { version: 'v2-rollback' };\n",
1072+
replaceExisting: true,
1073+
expectedDependencySpec: "2.0.0",
1074+
});
1075+
const rollback = await install("2.0.0", { mode: "update" });
1076+
expect(rollback.ok).toBe(true);
1077+
if (!rollback.ok) {
1078+
return;
1079+
}
1080+
expect(rollback.targetDir).not.toBe(retainedPackageDir);
1081+
await expect(retainedModule.default.runAttempt()).resolves.toEqual({ chunk: "v2" });
1082+
expect(fs.existsSync(path.join(retainedPackageDir, "dist", "run-attempt-v2.js"))).toBe(true);
1083+
});
1084+
10021085
it("installs into a fresh generation when the legacy npm target is retained", async () => {
10031086
const npmRoot = path.join(suiteTempRootTracker.makeTempDir(), "npm");
10041087
const packageName = "@openclaw/codex";
@@ -1044,7 +1127,7 @@ describe("installPluginFromNpmSpec", () => {
10441127
expect(fs.existsSync(legacyPackageDir)).toBe(true);
10451128
});
10461129

1047-
it("allows plain installs to reactivate an existing retained generation", async () => {
1130+
it("allocates a fresh generation when a plain install selects a retained artifact", async () => {
10481131
const npmRoot = path.join(suiteTempRootTracker.makeTempDir(), "npm");
10491132
const packageName = "@openclaw/codex";
10501133
const legacyPackageDir = resolveTestPluginPackageDir(npmRoot, packageName);
@@ -1086,7 +1169,8 @@ describe("installPluginFromNpmSpec", () => {
10861169
if (!result.ok) {
10871170
return;
10881171
}
1089-
expect(result.targetDir).toBe(retainedGenerationPackageDir);
1172+
expect(result.targetDir).not.toBe(retainedGenerationPackageDir);
1173+
expect(hasRetainedManagedNpmInstallMarker(result.targetDir)).toBe(false);
10901174
expect(hasRetainedManagedNpmInstallMarker(retainedGenerationPackageDir)).toBe(true);
10911175
expect(hasRetainedManagedNpmInstallMarker(legacyPackageDir)).toBe(true);
10921176
});

‎src/plugins/install.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Installs plugins from package specs, local paths, and catalogs.
2-
import { createHash } from "node:crypto";
2+
import { createHash, randomUUID } from "node:crypto";
33
import { constants as fsConstants, type Dirent } from "node:fs";
44
import fs from "node:fs/promises";
55
import os from "node:os";
@@ -1137,6 +1137,30 @@ function resolveManagedNpmRootForInstall(params: {
11371137
});
11381138
}
11391139

1140+
function resolveManagedNpmInstallRoot(params: {
1141+
npmBaseDir: string;
1142+
packageName: string;
1143+
npmResolution: NpmSpecResolution;
1144+
useGeneration: boolean;
1145+
}): string {
1146+
const generationKey = resolveManagedNpmRootGenerationKey({
1147+
packageName: params.packageName,
1148+
npmResolution: params.npmResolution,
1149+
});
1150+
const npmRoot = resolveManagedNpmRootForInstall(params);
1151+
const installRoot = resolveManagedNpmRootPackageDir(npmRoot, params.packageName);
1152+
if (!params.useGeneration || !hasRetainedManagedNpmInstallMarker(installRoot)) {
1153+
return npmRoot;
1154+
}
1155+
// Never mutate a retained tree: an older process may still hold lazy imports
1156+
// rooted there. A fresh activation root keeps that module graph importable.
1157+
return resolvePluginNpmGenerationProjectDir({
1158+
npmDir: params.npmBaseDir,
1159+
packageName: params.packageName,
1160+
generationKey: `${generationKey}\nactivation\n${randomUUID()}`,
1161+
});
1162+
}
1163+
11401164
async function listManagedNpmPackageDirsForPackage(params: {
11411165
runtime: Awaited<ReturnType<typeof loadPluginInstallRuntime>>;
11421166
npmBaseDir: string;
@@ -1309,7 +1333,7 @@ async function installPluginFromManagedNpmRoot(
13091333
packageName: params.packageName,
13101334
requestedMode: mode,
13111335
});
1312-
const npmRoot = resolveManagedNpmRootForInstall({
1336+
const npmRoot = resolveManagedNpmInstallRoot({
13131337
npmBaseDir,
13141338
packageName: params.packageName,
13151339
npmResolution: params.npmResolution,

0 commit comments

Comments
 (0)