Skip to content

Commit 39305af

Browse files
committed
fix(plugins): retain rollback marker snapshots
1 parent 4523e2e commit 39305af

2 files changed

Lines changed: 68 additions & 5 deletions

File tree

‎src/plugins/install-record-commit.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { InstalledPluginIndex } from "./installed-plugin-index.js";
1717
import {
1818
hasRetainedManagedNpmInstallMarker,
1919
markRetainedManagedNpmInstall,
20+
resolveRetainedManagedNpmInstallMarkerPath,
2021
} from "./managed-npm-retention.js";
2122
import { writeManagedNpmPlugin } from "./test-helpers/managed-npm-plugin.js";
2223

@@ -853,6 +854,67 @@ describe("commitConfigWithPendingPluginInstalls", () => {
853854
}
854855
});
855856

857+
it("restores earlier active markers when clearing a later marker fails", async () => {
858+
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-record-commit-"));
859+
const installPaths = ["codex", "voice-call"].map((pluginId) =>
860+
path.join(
861+
stateDir,
862+
"npm",
863+
"projects",
864+
`${pluginId}-v2`,
865+
"node_modules",
866+
"@openclaw",
867+
pluginId,
868+
),
869+
);
870+
for (const [index, installPath] of installPaths.entries()) {
871+
fs.mkdirSync(installPath, { recursive: true });
872+
await markRetainedManagedNpmInstall({
873+
packageDir: installPath,
874+
pluginId: index === 0 ? "codex" : "voice-call",
875+
retainedAt: "2026-04-25T00:00:00.000Z",
876+
reason: "test-retained-generation",
877+
});
878+
}
879+
const laterMarkerPath = resolveRetainedManagedNpmInstallMarkerPath(installPaths[1] ?? "");
880+
const realRm = fs.promises.rm.bind(fs.promises);
881+
const rmSpy = vi.spyOn(fs.promises, "rm").mockImplementation(async (target, options) => {
882+
if (String(target) === laterMarkerPath) {
883+
const error = new Error("marker clear failed") as NodeJS.ErrnoException;
884+
error.code = "EIO";
885+
throw error;
886+
}
887+
return await realRm(target, options);
888+
});
889+
890+
try {
891+
await expect(
892+
commitPluginInstallRecordsWithConfig({
893+
previousInstallRecords: {},
894+
nextInstallRecords: Object.fromEntries(
895+
installPaths.map((installPath, index) => {
896+
const pluginId = index === 0 ? "codex" : "voice-call";
897+
return [
898+
pluginId,
899+
{
900+
source: "npm",
901+
spec: `@openclaw/${pluginId}@2.0.0`,
902+
installPath,
903+
},
904+
];
905+
}),
906+
),
907+
nextConfig: {},
908+
}),
909+
).rejects.toThrow("marker clear failed");
910+
911+
expect(installPaths.every(hasRetainedManagedNpmInstallMarker)).toBe(true);
912+
} finally {
913+
rmSpy.mockRestore();
914+
fs.rmSync(stateDir, { recursive: true, force: true });
915+
}
916+
});
917+
856918
it("rolls back plugin index writes when the config write fails", async () => {
857919
const existingRecords: Record<string, PluginInstallRecord> = {
858920
existing: {

‎src/plugins/install-record-commit.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ async function removeCreatedRetainedManagedNpmInstallMarkers(markerPaths: string
343343

344344
async function clearActiveRetainedManagedNpmInstallMarkers(
345345
nextInstallRecords: Record<string, PluginInstallRecord>,
346-
): Promise<Array<{ markerPath: string; contents: string }>> {
347-
const clearedMarkers: Array<{ markerPath: string; contents: string }> = [];
346+
clearedMarkers: Array<{ markerPath: string; contents: string }>,
347+
): Promise<void> {
348348
for (const record of Object.values(nextInstallRecords)) {
349349
if (record.source !== "npm" || !record.installPath?.trim()) {
350350
continue;
@@ -366,10 +366,10 @@ async function clearActiveRetainedManagedNpmInstallMarkers(
366366
}
367367
const cleared = await clearRetainedManagedNpmInstallMarker(record.installPath);
368368
if (cleared) {
369+
// Record each cleared marker immediately so a later filesystem failure can roll it back.
369370
clearedMarkers.push({ markerPath, contents });
370371
}
371372
}
372-
return clearedMarkers;
373373
}
374374

375375
async function restoreClearedRetainedManagedNpmInstallMarkers(
@@ -414,8 +414,9 @@ async function commitPluginInstallRecordsWithWriter(params: {
414414
// Keep partial progress visible to the rollback path.
415415
createdMarkerPaths: retainedMarkerPaths,
416416
});
417-
clearedMarkerSnapshots.push(
418-
...(await clearActiveRetainedManagedNpmInstallMarkers(prepared.nextInstallRecords)),
417+
await clearActiveRetainedManagedNpmInstallMarkers(
418+
prepared.nextInstallRecords,
419+
clearedMarkerSnapshots,
419420
);
420421
const installRecordsChanged = !pluginInstallRecordMapsEqual(
421422
prepared.previousInstallRecords,

0 commit comments

Comments
 (0)