fix: roll back plugin index on config write conflicts

This commit is contained in:
Shakker
2026-04-26 01:08:49 +01:00
parent ca0232ff0e
commit be1d656514
6 changed files with 118 additions and 26 deletions

View File

@@ -0,0 +1,33 @@
import { replaceConfigFile } from "../config/config.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import type { PluginInstallRecord } from "../config/types.plugins.js";
import {
PLUGIN_INSTALLS_CONFIG_PATH,
writePersistedInstalledPluginIndexInstallRecords,
} from "../plugins/installed-plugin-index-records.js";
export async function commitPluginInstallRecordsWithConfig(params: {
previousInstallRecords: Record<string, PluginInstallRecord>;
nextInstallRecords: Record<string, PluginInstallRecord>;
nextConfig: OpenClawConfig;
baseHash?: string;
}): Promise<void> {
await writePersistedInstalledPluginIndexInstallRecords(params.nextInstallRecords);
try {
await replaceConfigFile({
nextConfig: params.nextConfig,
...(params.baseHash !== undefined ? { baseHash: params.baseHash } : {}),
writeOptions: { unsetPaths: [Array.from(PLUGIN_INSTALLS_CONFIG_PATH)] },
});
} catch (error) {
try {
await writePersistedInstalledPluginIndexInstallRecords(params.previousInstallRecords);
} catch (rollbackError) {
throw new Error(
"Failed to commit plugin install records and could not restore the previous plugin index",
{ cause: rollbackError },
);
}
throw error;
}
}