File size: 28,771 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | import path from "node:path";
import { resolveStateDir } from "../../config/paths.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { ScheduledTaskAutoStartRecoveryError } from "../../daemon/schtasks-update-recovery.js";
import { formatErrorMessage } from "../../infra/errors.js";
import { tryReadJson } from "../../infra/json-files.js";
import type { PackageUpdateTransaction } from "../../infra/package-update-steps.js";
import { validateUpdateCandidateCanary } from "../../infra/update-candidate-canary.js";
import type { UpdateCandidateRehearsal } from "../../infra/update-candidate-rehearsal.js";
import { readUpdateStateSchemaVersions } from "../../infra/update-candidate-state.js";
import {
createUpdateDoctorConfigWarningStep,
type UpdateDoctorConfigChange,
} from "../../infra/update-doctor-config.js";
import { resolveUpdateFinalizationTimeoutMs } from "../../infra/update-finalization-budget.js";
import {
canResolveRegistryVersionForPackageTarget,
verifyPackageUpdateRecovery,
} from "../../infra/update-global.js";
import { UpdateRequesterRevokedError } from "../../infra/update-requester-authority.js";
import { recordUpdateRunPhase } from "../../infra/update-run-ledger.js";
import { readCurrentGitUpdateRecovery } from "../../infra/update-runner-git-recovery.js";
import type { UpdateRunResult } from "../../infra/update-runner.js";
import { defaultRuntime } from "../../runtime.js";
import {
parsePackageOpenClawSchemaVersions,
type OpenClawSchemaVersions,
} from "../../state/openclaw-schema-versions.js";
import { formatCliCommand } from "../command-format.js";
import {
checkTargetDatabaseSchemasForContexts,
formatSchemaRefusalLines,
hasSchemaRefusal,
} from "./schema-preflight.js";
import {
normalizeTag,
readPackageVersion,
resolveGitInstallDir,
UpdatePreMutationError,
} from "./shared.js";
import { inspectUpdateDatabaseContexts } from "./update-command-database-context.js";
import type { MutableUpdateExecutionParams } from "./update-command-execution.types.js";
import {
createBeforeGitMutation,
recordInspectedGitTarget,
} from "./update-command-git-admission.js";
import { updateGitInstall } from "./update-command-git.js";
import {
formatUpdateAncestryBlockMessage,
handoffUpdateFromGateway,
} from "./update-command-handoff.js";
import {
captureOwnedManagedUpdateContext,
readUpdateCandidateSource,
revalidateUpdateDatabaseContext,
type OwnedManagedUpdateContext,
} from "./update-command-managed-context.js";
import {
runPackageInstallUpdate,
preparePackageDoctorContext,
type PackageInstallUpdateParams,
} from "./update-command-package.js";
import { assertUpdateCommandRecovery } from "./update-command-recovery.js";
import { runUpdateCommandRepair } from "./update-command-repair.js";
import {
createUpdateCommandFailureResult,
type MutableUpdateExecutionResult,
} from "./update-command-result.js";
import { isUpdatedInstallGatewayExecutorSupported } from "./update-command-service-command.js";
import { resolveUpdatedInstallCommandEnv } from "./update-command-service-env.js";
import {
collectServiceInspectionFailureFacts,
GatewayServiceUpdateOwnershipError,
} from "./update-command-service-plan.js";
import {
maybeRestartServiceAfterFailedMutableUpdate,
maybeStopManagedServiceBeforeMutableUpdate,
shouldBlockMutableUpdateFromGatewayServiceEnv,
UpdateCommandAbort,
type PreManagedServiceStop,
} from "./update-command-service.js";
import {
recordPreviousGatewayVerification,
verifyPreviousGatewayForUpdate,
} from "./update-command-verification.js";
export async function executeMutableUpdate(
params: MutableUpdateExecutionParams,
): Promise<MutableUpdateExecutionResult | null> {
const { opts, updateStepTimeoutMs } = params;
const originalRun = opts.run;
const requesterAuthority = originalRun?.requesterAuthority;
const assertRequesterCurrent = () => {
if (opts.run !== originalRun || requesterAuthority?.isCurrent() === false) {
throw new UpdateRequesterRevokedError();
}
};
const assertExecutionCurrent = () => {
assertUpdateCommandRecovery(opts);
assertRequesterCurrent();
};
const mode: UpdateRunResult["mode"] =
params.updateInstallKind === "git"
? "git"
: (params.packageInstallTarget?.manager ?? "unknown");
if (opts.recovery) {
throw new UpdatePreMutationError(
"rollback-state-unverified",
"Full-state checkpoint recovery is deferred.",
);
}
assertUpdateCommandRecovery(opts);
const stagedPluginAdmission =
params.updateInstallKind === "package" &&
!canResolveRegistryVersionForPackageTarget(params.packageInstallSpec ?? params.tag);
let preManagedServiceStop: PreManagedServiceStop | undefined;
let ownedManagedUpdateContext: OwnedManagedUpdateContext | undefined;
let admission: Awaited<ReturnType<typeof inspectUpdateDatabaseContexts>> | undefined;
let gitContextPrepared = false;
let admittedTargetSchemaVersions = params.packageTargetSchemaVersions;
const recheckSchemas = async (versions: OpenClawSchemaVersions | undefined) => {
if (!admission) {
throw new UpdatePreMutationError(
"database-schema-preflight",
"Database admission was not inspected.",
);
}
await inspectUpdateDatabaseContexts({
roots: [...admission.services.keys()],
updateInstallKind: params.updateInstallKind === "git" ? "git" : "package",
shouldRestart: params.shouldRestart,
jsonMode: Boolean(opts.json),
timeoutMs: updateStepTimeoutMs,
invocationCwd: params.invocationCwd,
managedServiceRootRedirect: params.managedServiceRootRedirect,
expectedServices: admission.services,
legacyConfigPlan: params.legacyConfigPlan,
});
admission.contexts = await Promise.all(admission.contexts.map(revalidateUpdateDatabaseContext));
const schemas = await checkTargetDatabaseSchemasForContexts(versions, admission.contexts);
if (hasSchemaRefusal(schemas)) {
throw new UpdatePreMutationError(
"database-schema-preflight",
formatSchemaRefusalLines(schemas).join("\n"),
);
}
admittedTargetSchemaVersions = versions;
};
const preflightPlugins = async (targetVersion: string | null) => {
await recheckSchemas(admittedTargetSchemaVersions);
const { preflightConfiguredNpmPluginTargets } =
await import("./update-command-plugin-preflight.js");
const context = admission!.contexts.at(-1)!;
const warnings = await preflightConfiguredNpmPluginTargets({
config: context.configSnapshot.sourceConfig,
env: context.env,
targetVersion,
channel: params.channel,
timeoutMs: params.updateStepTimeoutMs,
});
await recheckSchemas(admittedTargetSchemaVersions);
for (const warning of warnings) {
defaultRuntime[opts.json ? "error" : "log"](warning.message);
}
};
let recoveryEnv: NodeJS.ProcessEnv | undefined;
let packageTransaction: PackageUpdateTransaction | undefined;
let schemaVersions: Awaited<ReturnType<typeof readUpdateStateSchemaVersions>> | undefined;
let candidateSchemaVersions: OpenClawSchemaVersions | undefined;
let previousSchemaVersions: OpenClawSchemaVersions | undefined;
let previousVerified = false;
let observedGatewayStartupMs: number | undefined;
let activationConfig: MutableUpdateExecutionResult["activationConfig"];
const onConfigSnapshot: PackageInstallUpdateParams["onConfigSnapshot"] = (snapshot) => {
activationConfig = snapshot;
};
let candidateFailureReason: string | undefined;
let doctorConfigWrites = false;
const doctorConfigChanges: UpdateDoctorConfigChange[] = [];
let validatedConfigSnapshot: { config: OpenClawConfig; hash?: string | null } | undefined;
const getDoctorContext: PackageInstallUpdateParams["getDoctorContext"] = () =>
preparePackageDoctorContext({
capable: doctorConfigWrites,
runId: originalRun?.runId,
executorFence: originalRun?.executorFence,
requester: requesterAuthority?.requester,
inputHash: validatedConfigSnapshot?.hash,
changes: doctorConfigChanges,
assertCurrent: assertExecutionCurrent,
assertRequesterCurrent,
});
const originalRecovery = () =>
params.installKind === "git"
? readCurrentGitUpdateRecovery(params.root, updateStepTimeoutMs)
: verifyPackageUpdateRecovery(params.root);
const gitMutationRoots =
params.updateInstallKind === "git"
? params.switchToGit
? [params.root, resolveGitInstallDir()]
: [params.root]
: null;
const stopManagedServiceBeforeMutableUpdate = async (
mutationRoots: readonly string[] = [params.root],
phase: "inspect" | "prepare" = "prepare",
) => {
if (params.updateInstallKind !== "package" && params.updateInstallKind !== "git") {
return;
}
try {
for (const mutationRoot of new Set(mutationRoots)) {
preManagedServiceStop = await maybeStopManagedServiceBeforeMutableUpdate({
updateInstallKind: params.updateInstallKind,
root: mutationRoot,
shouldRestart: params.shouldRestart,
jsonMode: Boolean(opts.json),
timeoutMs: updateStepTimeoutMs,
phase,
expectedService: admission?.services.get(mutationRoot),
updateRun: opts.run,
recovery: opts.recovery,
onStopped: (state) => {
preManagedServiceStop = state;
},
handoffFromGateway: (state) =>
handoffUpdateFromGateway({
state,
root: mutationRoot,
opts,
// Pin the inspected package. Extended-stable resolves its protected
// selector again because its public CLI contract forbids --tag.
tag:
params.updateInstallKind === "package" && params.channel !== "extended-stable"
? (normalizeTag(params.packageInstallSpec) ?? undefined)
: undefined,
mode,
timeoutMs: updateStepTimeoutMs,
devTarget: params.devTarget,
nodeRunner: params.packageUpdateNodeRunner,
invocationCwd: params.invocationCwd,
stopProgress: params.stop,
}),
});
if (preManagedServiceStop.windowsTaskAutoStartRecovery) {
params.recoveryState.windowsTaskAutoStartRecovery =
preManagedServiceStop.windowsTaskAutoStartRecovery;
}
if (
preManagedServiceStop.stopped ||
preManagedServiceStop.serviceUpdateVerdict?.kind === "owned" ||
preManagedServiceStop.blockMessage ||
shouldBlockMutableUpdateFromGatewayServiceEnv({ preManagedServiceStop }) ||
!preManagedServiceStop.inspected ||
!preManagedServiceStop.running ||
!params.shouldRestart
) {
break;
}
}
} catch (err) {
if (err instanceof ScheduledTaskAutoStartRecoveryError) {
recoveryEnv = err.serviceEnv;
params.recoveryState.triageTarget.env = err.serviceEnv;
throw err;
}
if (err instanceof UpdateCommandAbort || err instanceof UpdatePreMutationError) {
throw err;
}
if (err instanceof GatewayServiceUpdateOwnershipError) {
throw new UpdatePreMutationError("managed-service-preflight", err.message, {
failureFacts: err.failureFacts,
});
}
params.stop();
throw new UpdatePreMutationError(
"managed-service-stop-failed",
`Failed to stop managed gateway service before update: ${String(err)}`,
{ cause: err },
);
}
if (phase === "inspect" && preManagedServiceStop?.serviceUpdateVerdict?.kind === "foreign") {
preManagedServiceStop = undefined;
}
try {
ownedManagedUpdateContext = await captureOwnedManagedUpdateContext({
stopState: preManagedServiceStop,
processEnv: process.env,
invocationCwd: params.invocationCwd,
});
if (ownedManagedUpdateContext) {
params.recoveryState.triageTarget.env = ownedManagedUpdateContext.env;
}
} catch (err) {
params.stop();
await maybeRestartServiceAfterFailedMutableUpdate({
recovery: await originalRecovery(),
updateRun: opts.run,
preManagedServiceStop,
jsonMode: Boolean(opts.json),
nodeRunner: params.packageUpdateNodeRunner,
timeoutMs: updateStepTimeoutMs,
invocationCwd: params.invocationCwd,
});
throw new Error(`Failed to capture managed gateway update state: ${String(err)}`, {
cause: err,
});
}
const inspectionFailure = {
failureFacts: collectServiceInspectionFailureFacts(
preManagedServiceStop?.serviceUpdateVerdict,
),
};
if (shouldBlockMutableUpdateFromGatewayServiceEnv({ preManagedServiceStop })) {
params.stop();
throw new UpdatePreMutationError(
"managed-service-preflight",
[
`${params.updateInstallKind === "git" ? "Git updates" : "Package updates"} cannot run from inside the gateway service process.`,
"That path replaces the active OpenClaw dist tree while the live gateway may still lazy-load old chunks.",
`Run \`${formatCliCommand("openclaw update")}\` from a terminal outside the gateway service.`,
].join("\n"),
inspectionFailure,
);
}
if (preManagedServiceStop?.blockMessage) {
params.stop();
throw new UpdatePreMutationError(
"managed-service-preflight",
formatUpdateAncestryBlockMessage(preManagedServiceStop.blockMessage),
inspectionFailure,
);
}
};
let result: UpdateRunResult;
let failure: MutableUpdateExecutionResult["failure"];
let mutationStarted = false;
const validateCandidate = async (root: string) => {
assertUpdateCommandRecovery(opts);
const env = ownedManagedUpdateContext?.env ?? opts.run?.env ?? process.env;
if (opts.run) {
recordUpdateRunPhase(opts.run.runId, "validating", undefined, { env: opts.run.env });
}
const validate = async (
signal?: AbortSignal,
rehearsal?: UpdateCandidateRehearsal,
assertCurrent?: () => void,
) => {
signal?.throwIfAborted();
try {
if (params.updateInstallKind === "package") {
// The staged manifest owns schema support, including artifacts without registry metadata.
await recheckSchemas(
parsePackageOpenClawSchemaVersions(
await tryReadJson<unknown>(path.join(root, "package.json")),
) ?? admittedTargetSchemaVersions,
);
signal?.throwIfAborted();
assertCurrent?.();
}
if (stagedPluginAdmission) {
// Explicit artifacts acquire their version before rehearsal or activation.
await preflightPlugins(await readPackageVersion(root));
signal?.throwIfAborted();
assertCurrent?.();
await params.prepareMutableUpdate(
ownedManagedUpdateContext?.env ?? admission?.managedEnv,
);
signal?.throwIfAborted();
assertCurrent?.();
}
} catch (error) {
if (error instanceof UpdatePreMutationError) {
candidateFailureReason = error.reason;
}
throw error;
}
if (
params.shouldRestart &&
opts.run &&
preManagedServiceStop?.serviceUpdateVerdict?.kind === "owned"
) {
const executor = opts.run.executorFence;
if (!executor) {
throw new UpdatePreMutationError(
"target-native-unsupported",
"Native candidate admission requires its original update executor.",
);
}
const supported = await isUpdatedInstallGatewayExecutorSupported({
root,
env: resolveUpdatedInstallCommandEnv({
processEnv: env,
invocationCwd: params.invocationCwd,
}),
executor,
timeoutMs: updateStepTimeoutMs,
nodeRunner: params.packageUpdateNodeRunner,
signal,
});
assertUpdateCommandRecovery(opts);
if (!supported) {
candidateFailureReason = "target-native-unsupported";
throw new UpdatePreMutationError(
candidateFailureReason,
"Target runtime cannot fence update-owned native commands; refusing before Gateway stop or package activation.",
);
}
}
const snapshot = rehearsal
? { config: rehearsal.sourceConfig, hash: rehearsal.sourceConfigHash }
: (validatedConfigSnapshot ??
(await readUpdateCandidateSource(env, params.legacyConfigPlan)));
const validation = await validateUpdateCandidateCanary({
root,
config: snapshot.config,
stateDir: resolveStateDir(env),
env,
signal,
rehearsal,
assertCurrent,
nodeRunner: params.packageUpdateNodeRunner,
timeoutMs: params.timeoutMs,
onStep: (step) => params.progress?.onStepComplete?.({ ...step, index: 0, total: 0 }),
});
assertUpdateCommandRecovery(opts);
doctorConfigChanges.push(...(validation.doctorConfigChanges ?? []));
if (validation.status === "ok") {
validatedConfigSnapshot = snapshot;
candidateSchemaVersions = validation.candidateSchemaVersions;
doctorConfigWrites = validation.doctorConfigWrites === true;
observedGatewayStartupMs = validation.steps.find(
(step) => step.name === "candidate gateway canary" && step.exitCode === 0,
)?.durationMs;
}
return validation;
};
let validation = await validate();
if (validation.status === "error") {
candidateFailureReason = validation.reason;
const repair = await runUpdateCommandRepair({
root: params.root,
candidateRoot: root,
env,
run: opts.run,
phase: "validating",
nodeRunner: params.packageUpdateNodeRunner,
result: {
status: "error",
mode,
root,
reason: validation.reason,
before: { version: await readPackageVersion(params.root) },
after: { version: await readPackageVersion(root) },
steps: validation.steps,
durationMs: validation.durationMs,
},
validate: async (signal, assertCurrent, rehearsal) => {
const repairValidation = await validate(signal, rehearsal, assertCurrent);
return {
ok: repairValidation.status === "ok",
score: repairValidation.steps.filter((step) => step.exitCode === 0).length,
summary:
repairValidation.status === "ok"
? "Candidate validation passed."
: repairValidation.logTail.join("\n"),
};
},
});
if (repair.status !== "repaired") {
if (repair.reason === "requester-revoked") {
candidateFailureReason = repair.reason;
}
return validation.steps;
}
candidateFailureReason = undefined;
// Repair's disposable state is gone; only surviving candidate changes may authorize activation.
validation = await validate();
candidateFailureReason = validation.status === "error" ? validation.reason : undefined;
}
if (validation.status === "ok" && !doctorConfigWrites && doctorConfigChanges.length) {
const warning = createUpdateDoctorConfigWarningStep(root, doctorConfigChanges);
validation.steps.push(warning);
params.progress?.onStepComplete?.({ ...warning, index: 0, total: 0 });
}
return validation.steps;
};
const beforeActivate = async (roots: readonly string[] = [params.root]) => {
assertExecutionCurrent();
const env = ownedManagedUpdateContext?.env ?? opts.run?.env ?? process.env;
const snapshot = await readUpdateCandidateSource(env, params.legacyConfigPlan);
if (
validatedConfigSnapshot?.hash !== undefined &&
snapshot.hash !== validatedConfigSnapshot.hash
) {
throw new UpdatePreMutationError(
"invalid-config",
"Config changed during candidate validation; rerun the update before activating.",
);
}
const config = snapshot.config;
await recheckSchemas(admittedTargetSchemaVersions);
previousSchemaVersions = parsePackageOpenClawSchemaVersions(
await tryReadJson<unknown>(path.join(params.root, "package.json")),
);
schemaVersions = candidateSchemaVersions
? await readUpdateStateSchemaVersions({
stateDir: resolveStateDir(env),
config,
env,
timeoutMs: params.updateStepTimeoutMs,
})
: undefined;
if (
preManagedServiceStop?.running &&
preManagedServiceStop.serviceUpdateVerdict?.kind === "owned"
) {
previousVerified = await verifyPreviousGatewayForUpdate({
root: params.root,
config,
env,
opts,
timeoutMs: params.timeoutMs,
observedStartupMs: observedGatewayStartupMs,
assertCurrent: assertExecutionCurrent,
});
// Recovery retains the observed verdict even if its receipt cannot be written.
assertExecutionCurrent();
recordPreviousGatewayVerification(opts.run, previousVerified);
}
// Health and candidate work can outlive the inspected service/config generation.
await recheckSchemas(admittedTargetSchemaVersions);
assertExecutionCurrent();
const activationTimeoutMs = await resolveUpdateFinalizationTimeoutMs(updateStepTimeoutMs, {
env,
databases: schemaVersions,
observedStartupMs: observedGatewayStartupMs,
pluginCount: Object.keys(config.plugins?.entries ?? {}).length,
nodeRunner: params.packageUpdateNodeRunner,
});
assertExecutionCurrent();
await params.prepareMutableUpdate(env, activationTimeoutMs);
assertExecutionCurrent();
if (opts.run) {
recordUpdateRunPhase(opts.run.runId, "activating", undefined, { env: opts.run.env });
}
await stopManagedServiceBeforeMutableUpdate(roots);
await recheckSchemas(admittedTargetSchemaVersions);
assertExecutionCurrent();
// Git owns this fence after its post-stop schema check completes.
if (params.updateInstallKind === "package") {
preManagedServiceStop?.windowsTaskAutoStartRecovery?.beginMutation();
}
mutationStarted = true;
params.onActivation?.();
};
try {
if (params.updateInstallKind === "package" || params.updateInstallKind === "git") {
admission = await inspectUpdateDatabaseContexts({
roots: gitMutationRoots ?? [params.root],
updateInstallKind: params.updateInstallKind,
shouldRestart: params.shouldRestart,
jsonMode: Boolean(opts.json),
timeoutMs: updateStepTimeoutMs,
invocationCwd: params.invocationCwd,
managedServiceRootRedirect: params.managedServiceRootRedirect,
legacyConfigPlan: params.legacyConfigPlan,
});
}
if (params.updateInstallKind === "package") {
if (!stagedPluginAdmission) {
await preflightPlugins(params.packageTargetVersion ?? null);
}
await stopManagedServiceBeforeMutableUpdate(undefined, "inspect");
if (!stagedPluginAdmission) {
await params.prepareMutableUpdate(admission?.managedEnv);
}
const packageUpdate: PackageInstallUpdateParams = {
reapplyLocalOverrides: opts.reapplyLocalOverrides,
root: params.root,
installKind: params.installKind,
tag: params.tag,
installSpec: params.packageInstallSpec ?? undefined,
timeoutMs: updateStepTimeoutMs,
startedAt: params.startedAt,
progress: params.progress,
invocationCwd: params.invocationCwd,
honorPackageRoot:
params.managedServiceRootRedirect !== null ||
params.managedServiceNodeRunner !== undefined,
nodeRunner: params.packageUpdateNodeRunner,
installEnv: params.packageInstallEnv,
installTarget: params.packageInstallTarget,
validateCandidate,
beforeActivate,
assertCurrent: assertExecutionCurrent,
managedServiceEnv: preManagedServiceStop?.serviceEnv,
onTransaction: (transaction) => {
packageTransaction = transaction;
},
onConfigSnapshot,
getDoctorContext,
};
await recheckSchemas(params.packageTargetSchemaVersions);
result = params.stagedPackage
? await params.stagedPackage.run(packageUpdate)
: await runPackageInstallUpdate(packageUpdate);
} else {
result = await updateGitInstall({
root: params.root,
switchToGit: params.switchToGit,
installKind: params.installKind,
timeoutMs: params.timeoutMs,
startedAt: params.startedAt,
progress: params.progress,
channel: params.channel,
tag: params.tag,
devTarget: params.devTarget,
assertCurrent: assertExecutionCurrent,
inspectGitTarget: async (target) => {
recordInspectedGitTarget(opts.run, target, assertExecutionCurrent);
await recheckSchemas(target.schemaVersions);
if (!gitContextPrepared) {
await stopManagedServiceBeforeMutableUpdate(gitMutationRoots ?? undefined, "inspect");
await params.prepareMutableUpdate(admission?.managedEnv);
// Revalidation retains activation's stop and recovery state.
gitContextPrepared = true;
}
},
onTransaction: (transaction) => {
packageTransaction = transaction;
},
onConfigSnapshot,
getDoctorContext,
// Foreign inspection metadata cannot authorize backup or Doctor writes.
getManagedServiceEnv: () => ownedManagedUpdateContext?.env,
getSnapshotSource: async () => {
const env =
ownedManagedUpdateContext?.env ?? admission?.managedEnv ?? opts.run?.env ?? process.env;
const source = await readUpdateCandidateSource(env, params.legacyConfigPlan);
return { config: source.config, env };
},
jsonMode: Boolean(opts.json),
invocationCwd: params.invocationCwd,
nodeRunner: params.packageUpdateNodeRunner,
validateCandidate: async (candidateRoot) => {
const steps = await validateCandidate(candidateRoot);
const failed = steps.find((step) => step.exitCode !== 0 && !step.advisory);
if (failed) {
throw new UpdatePreMutationError(
failed.name,
failed.stderrTail ?? "Candidate validation failed.",
{ failureFacts: failed.failureFacts },
);
}
},
beforeGitMutation:
params.updateInstallKind === "git"
? createBeforeGitMutation({
updateRun: opts.run,
roots: gitMutationRoots ?? [params.root],
shouldRestart: params.shouldRestart,
stopManagedService: beforeActivate,
getPreManagedServiceStop: () => preManagedServiceStop,
checkTargetSchemas: recheckSchemas,
prepareMutableUpdate: () =>
params.prepareMutableUpdate(
ownedManagedUpdateContext?.env ?? admission?.managedEnv,
),
switchToGit: params.switchToGit,
})
: undefined,
allowGatewayServiceRepair: false,
allowGatewayActivation: false,
});
}
} catch (err) {
params.stop();
if (err instanceof UpdateCommandAbort) {
return null;
}
const preMutationFailure = err instanceof UpdatePreMutationError;
failure = { cause: err, detail: formatErrorMessage(err) };
defaultRuntime.error(failure.detail);
// Only explicit pre-mutation refusal permits original-runtime recovery.
// Mutable exceptions retain an unsafe outcome through cleanup/reporting.
result = createUpdateCommandFailureResult({
durationMs: Date.now() - params.startedAt,
mode,
root: params.root,
recovery: preMutationFailure
? await originalRecovery()
: { serviceRestartSafe: false, reason: "runtime-verification-failed" },
failure,
});
}
if (candidateFailureReason && result.status === "error") {
result.reason = candidateFailureReason;
}
return {
result,
failure,
mutationStarted,
preManagedServiceStop,
ownedManagedUpdateContext,
recoveryEnv,
packageTransaction,
schemaVersions,
candidateSchemaVersions,
previousSchemaVersions,
previousVerified,
activationConfig,
};
}
|