File size: 13,729 Bytes
eb3f11e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | import { isRecord } from "@openclaw/normalization-core/record-coerce";
import { theme } from "../../../packages/terminal-core/src/theme.js";
import { readConfigFileSnapshot } from "../../config/config.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import type { UpdateChannel } from "../../infra/update-channels.js";
import { compareSemverStrings } from "../../infra/update-check.js";
import { normalizeUpdatePostInstallDoctorWarnings } from "../../infra/update-doctor-result.js";
import { updateInstallRootsMatch } from "../../infra/update-install-root.js";
import { recordUpdateRunStep } from "../../infra/update-run-ledger.js";
import { updateRunStepsFromResultStep } from "../../infra/update-run-step.js";
import type { UpdateRunResult } from "../../infra/update-runner.js";
import { loadInstalledPluginIndexInstallRecords } from "../../plugins/installed-plugin-index-records.js";
import { withPluginLifecycleLease } from "../../plugins/plugin-lifecycle-lease.js";
import { defaultRuntime } from "../../runtime.js";
import { VERSION } from "../../version.js";
import { readPackageVersion, type UpdateCommandOptions } from "./shared.js";
import { preparePostCorePluginConfig } from "./update-command-config.js";
import { completePostCorePluginUpdate } from "./update-command-fresh-doctor.js";
import { collectPostCorePluginFailureFacts } from "./update-command-plugins-internals.js";
import { updatePluginsAfterCoreUpdate } from "./update-command-plugins.js";
import {
continuePostCoreUpdateInFreshProcess,
shouldResumePostCoreUpdateInFreshProcess,
} from "./update-command-post-core.js";
import { completeSourceUpdateRuntime } from "./update-command-runtime.js";
import { withOwnedManagedUpdateEnv } from "./update-command-service-env.js";
export async function convergeUpdatePlugins(params: {
coreAlreadyCurrent?: boolean;
result: UpdateRunResult;
root: string;
previousInstallRoot?: string;
installKindChanged: boolean;
configSnapshot: Awaited<ReturnType<typeof readConfigFileSnapshot>>;
requestedChannel: UpdateChannel | null;
storedChannel: UpdateChannel | null;
channel: UpdateChannel;
downgradeRisk: boolean;
opts: UpdateCommandOptions;
ownedManagedUpdateEnv?: NodeJS.ProcessEnv;
preUpdatePluginInstallRecords: Awaited<ReturnType<typeof loadInstalledPluginIndexInstallRecords>>;
startedAt: number;
packageUpdateNodeRunner?: string;
updateStepTimeoutMs: number;
beforeDoctor?: () => Promise<void>;
assertCurrent?: () => void;
}): Promise<{
resultWithPostUpdate: UpdateRunResult;
postUpdateConfigSnapshot?: Awaited<ReturnType<typeof readConfigFileSnapshot>>;
detail?: string;
cancelled?: boolean;
}> {
// The finalizer also fences replacement of the original run and executor objects.
const assertCurrent = params.assertCurrent ?? params.opts.run?.executorFence?.assertCurrent;
assertCurrent?.();
const postUpdateRoot = params.result.root ?? params.root;
const preUpdateConfig = params.configSnapshot.valid
? {
sourceConfig: params.configSnapshot.sourceConfig,
authoredConfig: isRecord(params.configSnapshot.parsed)
? (params.configSnapshot.parsed as OpenClawConfig) // SAFETY: valid snapshot validated this authored record.
: params.configSnapshot.sourceConfig,
}
: undefined;
const postUpdateInstalledVersion = await readPackageVersion(postUpdateRoot);
assertCurrent?.();
const versionComparison =
postUpdateInstalledVersion && VERSION
? compareSemverStrings(VERSION, postUpdateInstalledVersion)
: null;
const runtimeRootChanged = !updateInstallRootsMatch(
params.previousInstallRoot ?? params.root,
postUpdateRoot,
);
const retainedDifferentRuntime =
params.coreAlreadyCurrent === true &&
(runtimeRootChanged || (versionComparison !== null && versionComparison !== 0));
const shouldResumePostCoreInFreshProcess =
(!params.coreAlreadyCurrent || retainedDifferentRuntime) &&
shouldResumePostCoreUpdateInFreshProcess({
// An already-current install can still differ from the retained updater.
// Route by that runtime transition without changing the reported core result.
result: retainedDifferentRuntime
? {
...params.result,
status: "ok",
before: { ...params.result.before, version: VERSION },
after: { ...params.result.after, version: postUpdateInstalledVersion },
}
: params.result,
downgradeRisk: params.downgradeRisk || (versionComparison !== null && versionComparison > 0),
installKindChanged:
params.installKindChanged || (retainedDifferentRuntime && runtimeRootChanged),
});
let postUpdateConfigSnapshot: Awaited<ReturnType<typeof readConfigFileSnapshot>> | undefined;
if (
params.requestedChannel &&
params.configSnapshot.valid &&
params.requestedChannel !== params.storedChannel &&
!params.opts.json
) {
const verb = shouldResumePostCoreInFreshProcess ? "will be set" : "set";
defaultRuntime.log(theme.muted(`Update channel ${verb} to ${params.requestedChannel}.`));
}
if (params.opts.run) {
// Track convergence without advancing the monotonic run phase past restart.
// The service verifier owns "verifying" after the final activation.
recordUpdateRunStep(
params.opts.run.runId,
{
step: "post-update verification",
status: "in_progress",
startedAtMs: Date.now(),
},
{ env: params.opts.run.env },
);
}
return await withOwnedManagedUpdateEnv(params.ownedManagedUpdateEnv, async () => {
const previousCompatibilityHostVersion = process.env.OPENCLAW_COMPATIBILITY_HOST_VERSION;
const compatibilityDowngradeTarget =
versionComparison != null && versionComparison > 0 ? postUpdateInstalledVersion : null;
if (compatibilityDowngradeTarget) {
// The parent still reports its pre-update VERSION. Convergence and fresh
// completion must both use the installed target's compatibility contract.
process.env.OPENCLAW_COMPATIBILITY_HOST_VERSION = compatibilityDowngradeTarget;
}
try {
let postCorePluginUpdate;
const doctorWarnings: string[] = [];
let pluginsUpdatedInFreshProcess = false;
if (shouldResumePostCoreInFreshProcess) {
const freshProcessResult = await continuePostCoreUpdateInFreshProcess({
root: postUpdateRoot,
channel: params.channel,
requestedChannel: params.requestedChannel,
opts: params.opts,
pluginInstallRecords: params.preUpdatePluginInstallRecords,
updateStartedAtMs: params.startedAt,
timeoutMs: params.updateStepTimeoutMs,
nodeRunner: params.packageUpdateNodeRunner,
preUpdateConfig,
});
assertCurrent?.();
if (freshProcessResult.exitCode !== undefined) {
return {
resultWithPostUpdate: {
...params.result,
status: "error" as const,
reason: "post-core-update-failed",
},
detail: freshProcessResult.error,
cancelled: freshProcessResult.exitCode === 130 || freshProcessResult.exitCode === 143,
};
}
pluginsUpdatedInFreshProcess = freshProcessResult.resumed;
postCorePluginUpdate = freshProcessResult.pluginUpdate;
}
if (retainedDifferentRuntime && !pluginsUpdatedInFreshProcess) {
return {
resultWithPostUpdate: {
...params.result,
status: "error" as const,
reason: "post-core-update-failed",
},
detail:
"The installed target could not resume plugin convergence. Run openclaw update using the installed target executable.",
};
}
if (!pluginsUpdatedInFreshProcess) {
postCorePluginUpdate = await withPluginLifecycleLease({ assertCurrent }, async (lease) => {
await completeSourceUpdateRuntime({
root: postUpdateRoot,
timeoutMs: params.updateStepTimeoutMs,
lease,
beforePersistentEffect: assertCurrent,
});
assertCurrent?.();
const preparedConfig = await preparePostCorePluginConfig({
requestedChannel: params.requestedChannel,
preUpdateConfig,
suppressFutureVersionWarning: shouldResumePostCoreInFreshProcess,
});
assertCurrent?.();
postUpdateConfigSnapshot = preparedConfig.configSnapshot;
const pluginInstallRecords = await loadInstalledPluginIndexInstallRecords();
assertCurrent?.();
return await updatePluginsAfterCoreUpdate({
root: postUpdateRoot,
channel: params.channel,
...preparedConfig,
json: params.opts.json,
acceptCapabilities: params.opts.acceptCapabilities,
timeoutMs: params.updateStepTimeoutMs,
pluginInstallRecords,
assertCurrent,
});
});
}
assertCurrent?.();
if (postCorePluginUpdate && (!params.coreAlreadyCurrent || postCorePluginUpdate.changed)) {
// Release the plugin lease before fresh Doctor. The finalizer either
// retains its stopped interval or parks an already-current core here.
const completedPluginUpdate = await completePostCorePluginUpdate({
root: postUpdateRoot,
pluginUpdate: postCorePluginUpdate,
freshDoctorRequired: postCorePluginUpdate.changed,
beforeDoctor: params.beforeDoctor,
yes: params.opts.yes === true,
json: params.opts.json === true,
timeoutMs: params.updateStepTimeoutMs,
onWarnings: (warnings) => {
doctorWarnings.push(...warnings);
},
...(params.packageUpdateNodeRunner ? { nodeRunner: params.packageUpdateNodeRunner } : {}),
});
assertCurrent?.();
postCorePluginUpdate = completedPluginUpdate.pluginUpdate;
postUpdateConfigSnapshot = completedPluginUpdate.configSnapshot;
}
const resultWithPostUpdate: UpdateRunResult = {
...params.result,
steps: [...params.result.steps],
...(postCorePluginUpdate
? {
status: postCorePluginUpdate.status === "error" ? "error" : params.result.status,
...(postCorePluginUpdate.status === "error" ? { reason: "post-update-plugins" } : {}),
postUpdate: {
...params.result.postUpdate,
plugins: postCorePluginUpdate,
},
}
: {}),
};
const failureFacts = postCorePluginUpdate
? collectPostCorePluginFailureFacts(postCorePluginUpdate)
: [];
if (failureFacts.length) {
resultWithPostUpdate.steps.push({
name: "post-update verification",
command: "openclaw plugins update",
cwd: postUpdateRoot,
durationMs: 0,
exitCode: 1,
failureFacts,
});
}
resultWithPostUpdate.steps.push(
...normalizeUpdatePostInstallDoctorWarnings(doctorWarnings).map((message, index) => ({
name: `post-plugin doctor warning ${index + 1}`,
command: "openclaw doctor --fix",
cwd: postUpdateRoot,
durationMs: 0,
exitCode: 0,
advisory: { kind: "package-post-install-doctor" as const, message },
})),
);
const pluginAdvisories = [
...(postCorePluginUpdate?.warnings ?? []).filter(
(warning) =>
warning.reason === "plugin-target-unavailable" || warning.reason === "doctor-advisory",
),
// Committed handoff files can acknowledge success without npm details.
...(postCorePluginUpdate?.npm?.outcomes ?? []).filter(
(outcome) => outcome.code === "source-bundled-plugin",
),
];
resultWithPostUpdate.steps.push(
...pluginAdvisories.map((warning, index) => ({
name: `finalize:plugins:${index}`,
command: "openclaw plugins update",
cwd: postUpdateRoot,
durationMs: 0,
exitCode: 0,
advisory: { kind: "recoverable-maintenance" as const, message: warning.message },
})),
);
if (
params.coreAlreadyCurrent &&
resultWithPostUpdate.status !== "error" &&
(postCorePluginUpdate?.changed ||
(params.requestedChannel !== null && params.requestedChannel !== params.storedChannel))
) {
resultWithPostUpdate.status = "ok";
delete resultWithPostUpdate.reason;
}
if (params.opts.run) {
for (const step of resultWithPostUpdate.steps.flatMap(updateRunStepsFromResultStep)) {
if (step.step.startsWith("warning:")) {
recordUpdateRunStep(params.opts.run.runId, step, { env: params.opts.run.env });
}
}
recordUpdateRunStep(
params.opts.run.runId,
{
step: "post-update verification",
status: postCorePluginUpdate?.status === "error" ? "failed" : "completed",
endedAtMs: Date.now(),
...(failureFacts.length ? { failureFacts } : {}),
},
{ env: params.opts.run.env },
);
}
return { resultWithPostUpdate, postUpdateConfigSnapshot };
} finally {
if (compatibilityDowngradeTarget) {
if (previousCompatibilityHostVersion === undefined) {
delete process.env.OPENCLAW_COMPATIBILITY_HOST_VERSION;
} else {
process.env.OPENCLAW_COMPATIBILITY_HOST_VERSION = previousCompatibilityHostVersion;
}
}
}
});
}
|