mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 17:31:06 +00:00
fix(migrations): avoid partial Hermes config apply after conflict
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { summarizeMigrationItems } from "openclaw/plugin-sdk/migration";
|
import { markMigrationItemSkipped, summarizeMigrationItems } from "openclaw/plugin-sdk/migration";
|
||||||
import {
|
import {
|
||||||
archiveMigrationItem,
|
archiveMigrationItem,
|
||||||
copyMigrationFileItem,
|
copyMigrationFileItem,
|
||||||
@@ -18,6 +18,33 @@ import { buildHermesPlan } from "./plan.js";
|
|||||||
import { applySecretItem } from "./secrets.js";
|
import { applySecretItem } from "./secrets.js";
|
||||||
import { resolveTargets } from "./targets.js";
|
import { resolveTargets } from "./targets.js";
|
||||||
|
|
||||||
|
const HERMES_REASON_BLOCKED_BY_APPLY_CONFLICT = "blocked by earlier apply conflict";
|
||||||
|
|
||||||
|
function withCachedConfigRuntime(
|
||||||
|
runtime: MigrationProviderContext["runtime"] | undefined,
|
||||||
|
fallbackConfig: MigrationProviderContext["config"],
|
||||||
|
): MigrationProviderContext["runtime"] | undefined {
|
||||||
|
if (!runtime?.config.writeConfigFile) {
|
||||||
|
return runtime;
|
||||||
|
}
|
||||||
|
let cachedConfig: MigrationProviderContext["config"] | undefined;
|
||||||
|
const loadConfig = () => {
|
||||||
|
cachedConfig ??= structuredClone(runtime.config.loadConfig?.() ?? fallbackConfig);
|
||||||
|
return cachedConfig;
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
...runtime,
|
||||||
|
config: {
|
||||||
|
...runtime.config,
|
||||||
|
loadConfig,
|
||||||
|
writeConfigFile: async (next, options) => {
|
||||||
|
cachedConfig = structuredClone(next);
|
||||||
|
await runtime.config.writeConfigFile(next, options);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export async function applyHermesPlan(params: {
|
export async function applyHermesPlan(params: {
|
||||||
ctx: MigrationProviderContext;
|
ctx: MigrationProviderContext;
|
||||||
plan?: MigrationPlan;
|
plan?: MigrationPlan;
|
||||||
@@ -27,35 +54,42 @@ export async function applyHermesPlan(params: {
|
|||||||
const reportDir = params.ctx.reportDir ?? path.join(params.ctx.stateDir, "migration", "hermes");
|
const reportDir = params.ctx.reportDir ?? path.join(params.ctx.stateDir, "migration", "hermes");
|
||||||
const targets = resolveTargets(params.ctx);
|
const targets = resolveTargets(params.ctx);
|
||||||
const items: MigrationItem[] = [];
|
const items: MigrationItem[] = [];
|
||||||
|
const runtime = withCachedConfigRuntime(params.ctx.runtime ?? params.runtime, params.ctx.config);
|
||||||
|
const applyCtx = { ...params.ctx, runtime };
|
||||||
|
let blockedByApplyConflict = false;
|
||||||
for (const item of plan.items) {
|
for (const item of plan.items) {
|
||||||
if (item.status !== "planned") {
|
if (item.status !== "planned") {
|
||||||
items.push(item);
|
items.push(item);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (blockedByApplyConflict) {
|
||||||
|
items.push(markMigrationItemSkipped(item, HERMES_REASON_BLOCKED_BY_APPLY_CONFLICT));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let appliedItem: MigrationItem;
|
||||||
if (item.id === "config:default-model") {
|
if (item.id === "config:default-model") {
|
||||||
items.push(
|
appliedItem = await applyModelItem(applyCtx, item);
|
||||||
await applyModelItem(
|
|
||||||
{ ...params.ctx, runtime: params.ctx.runtime ?? params.runtime },
|
|
||||||
item,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} else if (item.kind === "config") {
|
} else if (item.kind === "config") {
|
||||||
items.push(
|
appliedItem = await applyConfigItem(applyCtx, item);
|
||||||
await applyConfigItem(
|
|
||||||
{ ...params.ctx, runtime: params.ctx.runtime ?? params.runtime },
|
|
||||||
item,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} else if (item.kind === "manual") {
|
} else if (item.kind === "manual") {
|
||||||
items.push(applyManualItem(item));
|
appliedItem = applyManualItem(item);
|
||||||
} else if (item.action === "archive") {
|
} else if (item.action === "archive") {
|
||||||
items.push(await archiveMigrationItem(item, reportDir));
|
appliedItem = await archiveMigrationItem(item, reportDir);
|
||||||
} else if (item.kind === "secret") {
|
} else if (item.kind === "secret") {
|
||||||
items.push(await applySecretItem(params.ctx, item, targets));
|
appliedItem = await applySecretItem(params.ctx, item, targets);
|
||||||
} else if (item.action === "append") {
|
} else if (item.action === "append") {
|
||||||
items.push(await appendItem(item));
|
appliedItem = await appendItem(item);
|
||||||
} else {
|
} else {
|
||||||
items.push(await copyMigrationFileItem(item, reportDir, { overwrite: params.ctx.overwrite }));
|
appliedItem = await copyMigrationFileItem(item, reportDir, {
|
||||||
|
overwrite: params.ctx.overwrite,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
items.push(appliedItem);
|
||||||
|
if (
|
||||||
|
item.kind === "config" &&
|
||||||
|
(appliedItem.status === "conflict" || appliedItem.status === "error")
|
||||||
|
) {
|
||||||
|
blockedByApplyConflict = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const result: MigrationApplyResult = {
|
const result: MigrationApplyResult = {
|
||||||
|
|||||||
Reference in New Issue
Block a user