Skip to content

Commit 6bebbd7

Browse files
committed
fix(update): preserve automatic update policy and outcomes
Keep saved channels authoritative, retire stopped schedulers, and record automatic failures before campaign completion. Use managed handoff for automatic installation and preserve scoped web reconciliation across reloads.
1 parent eeedc95 commit 6bebbd7

27 files changed

Lines changed: 1930 additions & 518 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3868,7 +3868,6 @@ ui/src/app/stale-chunk-reload.ts 1
38683868
ui/src/app/startup-settings.ts 1
38693869
ui/src/app/theme.ts 4
38703870
ui/src/app/update-schedule-dto.ts 2
3871-
ui/src/app/update-success-notice.ts 1
38723871
ui/src/app/user-profile.ts 2
38733872
ui/src/app/web-push.runtime.ts 2
38743873
ui/src/components/agent-select.ts 3
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"startupJsGzipBytes": 346777,
3-
"reason": "Sender-agent avatars on forwarded cross-session messages (PR #133439): agent-avatar resolution, attribution label rules, and direct-thread identity chrome measured 346777 B, +592 B over the prior baseline; below the 358400 B ceiling; hard cap and allowances unchanged",
4-
"updatedAt": "2026-08-30"
2+
"startupJsGzipBytes": 347598,
3+
"reason": "Automatic update outcome continuity: one scoped pending/verified browser notice, admission deadline, and shared status refresh ownership measured 347598 B in the isolated production build (+821 B, 0.24%); fixed 358400 B cap and allowances unchanged",
4+
"updatedAt": "2026-09-01"
55
}

‎docs/cli/update.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ print progress steps.
6969
completion profiles and caches are still repaired when needed; installing
7070
completion in a new shell profile remains an interactive choice.
7171

72+
`--tag` changes only this package update. A saved `update.channel` continues to
73+
govern later foreground and automatic updates, even after a one-off beta
74+
install. Use `--channel` to change that policy.
75+
7276
For source checkouts, `--dry-run` previews the update flow without fetching Git
7377
refs or checking working-tree changes. The real update checks for uncommitted
7478
changes before modifying the checkout. Use `openclaw update status` to inspect
@@ -256,9 +260,12 @@ aligned:
256260

257261
### Restart handoff
258262

259-
The Gateway core auto-updater (when enabled via config) launches the CLI
260-
update path outside the live Gateway request handler. Control-plane
261-
`update.run` package-manager updates and supervised git-checkout updates use
263+
The Gateway core auto-updater requires a managed service restart path. It hands
264+
the CLI update to a detached helper before the Gateway exits. A foreground
265+
Gateway keeps update hints but leaves installation and activation to the
266+
operator: stop it, run `openclaw update`, then launch it again.
267+
268+
Control-plane `update.run` package-manager updates and supervised git-checkout updates use
262269
the same managed-service handoff instead of replacing the package tree or
263270
rebuilding `dist/` inside the live Gateway process: the Gateway starts a
264271
detached helper and exits, and that helper runs `openclaw update --yes --json`

‎docs/install/updating.md‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ backup.
1919

2020
## Recommended: `openclaw update`
2121

22-
Detects your install type (npm, pnpm, Bun, or git), fetches the latest version, runs `openclaw doctor`, and restarts the gateway.
22+
Detects your install type (npm, pnpm, Bun, or git), fetches the latest version, runs `openclaw doctor`, and restarts a managed Gateway service.
2323

2424
```bash
2525
openclaw update
@@ -43,6 +43,12 @@ when the beta tag is missing or its version is older than the latest stable
4343
release. Use `--tag beta` for a one-off package update pinned to the raw npm
4444
beta dist-tag instead.
4545

46+
A saved `update.channel` remains the channel for future updates, automatic
47+
checks, and update status. For example, a one-off beta package on a saved stable
48+
channel keeps checking stable afterward. Use `--channel beta` to subscribe to
49+
beta updates. Plugins still follow the installed core version where required
50+
for compatibility.
51+
4652
`--channel extended-stable` is package-only, and installation remains
4753
foreground-only. OpenClaw reads the public npm `extended-stable` selector,
4854
verifies the selected exact package, and installs that exact version. Missing
@@ -376,6 +382,13 @@ specific number of commits behind. It also shows exact and relative build,
376382
verified install, and last-commit times. Existing checkouts show an unknown
377383
install time until their next verified successful update.
378384

385+
Automatic installation requires a managed Gateway service that can hand off
386+
the update and restart safely. A Gateway running directly in a terminal can
387+
still show update hints, but it does not automatically replace its running
388+
installation. Stop that Gateway, run `openclaw update`, and launch it again
389+
afterward, or [install a managed service](/cli/gateway#manage-the-gateway-service) for
390+
unattended updates.
391+
379392
| Channel | Behavior |
380393
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
381394
| `stable` | After a built-in delay with deterministic jitter for a spread rollout, announces an update campaign. |
@@ -401,8 +414,7 @@ fixed target and does not move if upstream `main` advances during the countdown.
401414

402415
Every failed apply ends the campaign so the UI does not remain on
403416
**Updating…**. Failures after a managed-service handoff starts are also recorded
404-
in the restart sentinel and surface after the Gateway returns; direct
405-
unsupervised failures remain in the running Gateway's logs.
417+
in the restart sentinel and surface after the Gateway returns.
406418

407419
`update.checkOnStart: false` disables all automatic update checks, feature
408420
statistics, and update notices, even when `update.auto.enabled` is `true`.
@@ -412,6 +424,9 @@ still run unless `update.checkOnStart` is also disabled. See
412424
[Usage telemetry and update checks](/gateway/telemetry) for the information
413425
sent by the daily check and optional anonymous feature statistics.
414426

427+
Disabling checks also cancels unfinished discovery and its campaign; a late
428+
response from the previous settings cannot start an update afterward.
429+
415430
The gateway also logs an update hint on startup (disable with
416431
`update.checkOnStart: false`). Stored extended-stable selections use this
417432
read-only hint path and the existing 24-hour hint interval, but never invoke

‎src/gateway/server-methods/update-run-campaign.test.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const startManagedServiceUpdateHandoffMock = vi.fn<
5353
}));
5454
const scheduleGatewaySigusr1RestartMock = vi.fn(() => ({ scheduled: true }));
5555
const logGatewayInfoMock = vi.fn();
56+
const writeRestartSentinelMock = vi.fn(async () => undefined);
57+
const recordLatestUpdateRestartSentinelMock = vi.fn();
5658

5759
vi.mock("../../../packages/gateway-protocol/src/index.js", () => ({
5860
validateUpdateRunParams: () => true,
@@ -85,7 +87,7 @@ vi.mock("../../infra/restart-sentinel.js", async () => {
8587
);
8688
return {
8789
...actual,
88-
writeRestartSentinel: async () => undefined,
90+
writeRestartSentinel: writeRestartSentinelMock,
8991
};
9092
});
9193

@@ -147,7 +149,7 @@ vi.mock("../../version.js", () => ({
147149

148150
vi.mock("../server-restart-sentinel.js", () => ({
149151
getLatestUpdateRestartSentinel: () => null,
150-
recordLatestUpdateRestartSentinel: () => undefined,
152+
recordLatestUpdateRestartSentinel: recordLatestUpdateRestartSentinelMock,
151153
refreshLatestUpdateRestartSentinel: async () => null,
152154
}));
153155

@@ -202,6 +204,8 @@ beforeEach(() => {
202204
startManagedServiceUpdateHandoffMock.mockClear();
203205
scheduleGatewaySigusr1RestartMock.mockClear();
204206
logGatewayInfoMock.mockClear();
207+
writeRestartSentinelMock.mockClear();
208+
recordLatestUpdateRestartSentinelMock.mockClear();
205209
});
206210

207211
function setDevCampaignSchedule(upstreamSha = "frozen-upstream-sha"): void {
@@ -310,6 +314,8 @@ function expectNoUpdateMutation(): void {
310314
expect(runGatewayUpdatePreflightMock).not.toHaveBeenCalled();
311315
expect(startManagedServiceUpdateHandoffMock).not.toHaveBeenCalled();
312316
expect(runGatewayUpdateMock).not.toHaveBeenCalled();
317+
expect(writeRestartSentinelMock).not.toHaveBeenCalled();
318+
expect(recordLatestUpdateRestartSentinelMock).not.toHaveBeenCalled();
313319
}
314320

315321
describe("update.run campaign ownership", () => {
@@ -647,11 +653,20 @@ describe("update.run campaign ownership", () => {
647653
);
648654
});
649655

650-
it("clears the campaign adopted by a failed update", async () => {
656+
it("records the failure before ending the adopted campaign", async () => {
657+
let outcomeWhenCampaignEnded: unknown;
658+
clearCampaignMock.mockImplementationOnce(() => {
659+
outcomeWhenCampaignEnded = recordLatestUpdateRestartSentinelMock.mock.calls.at(-1)?.[0];
660+
});
651661
await invokeUpdateRun();
652662

653663
expect(adoptCampaignMock).toHaveBeenCalledOnce();
654664
expect(clearCampaignMock).toHaveBeenCalledOnce();
665+
expect(outcomeWhenCampaignEnded).toMatchObject({
666+
kind: "update",
667+
status: "error",
668+
stats: { reason: "build-failed" },
669+
});
655670
expect(logGatewayInfoMock).toHaveBeenCalledWith("update.run failed; adopted campaign cleared", {
656671
campaignId: "campaign-1",
657672
});
@@ -701,6 +716,8 @@ describe("update.run campaign ownership", () => {
701716

702717
expect(getCampaignStateMock).toHaveBeenCalledOnce();
703718
expect(clearCampaignMock).not.toHaveBeenCalled();
719+
expect(writeRestartSentinelMock).not.toHaveBeenCalled();
720+
expect(recordLatestUpdateRestartSentinelMock).not.toHaveBeenCalled();
704721
expect(logGatewayInfoMock).not.toHaveBeenCalledWith(
705722
"update.run failed; adopted campaign cleared",
706723
expect.anything(),

‎src/gateway/server-methods/update.ts‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export const updateHandlers: GatewayRequestHandlers = {
232232
| { status: "unavailable"; command: string; message: string }
233233
| null = null;
234234
let managedHandoffRestart: ReturnType<typeof scheduleGatewaySigusr1Restart> | null = null;
235-
let ownsManagedServiceHandoff = true;
235+
let ownsUpdateOutcome = false;
236236
let adoptedCampaignId: string | undefined;
237237
const sentinelMeta: UpdateRestartSentinelMeta = {
238238
...(sessionKey ? { sessionKey } : {}),
@@ -286,6 +286,7 @@ export const updateHandlers: GatewayRequestHandlers = {
286286
} else if (adoption?.status === "applying") {
287287
targetFailureReason = "update-campaign-applying";
288288
}
289+
ownsUpdateOutcome = targetFailureReason === undefined;
289290
const adoptedCampaign = adoption?.status === "adopted" ? adoption : undefined;
290291
adoptedCampaignId = adoptedCampaign?.campaignId;
291292
const adoptedDevTarget =
@@ -413,7 +414,7 @@ export const updateHandlers: GatewayRequestHandlers = {
413414
handoffId,
414415
supervisor,
415416
});
416-
ownsManagedServiceHandoff = started.status === "started";
417+
ownsUpdateOutcome = started.status === "started";
417418
sentinelMeta.handoffId = started.handoffId ?? handoffId;
418419
// The owner pairs helper creation with parent exit before any
419420
// persistence can fail. Joiners leave both to the active owner.
@@ -453,11 +454,11 @@ export const updateHandlers: GatewayRequestHandlers = {
453454
status: "skipped",
454455
mode: installSurface.mode,
455456
root: installRoot,
456-
reason: ownsManagedServiceHandoff
457+
reason: ownsUpdateOutcome
457458
? CONTROL_PLANE_UPDATE_HANDOFF_STARTED_REASON
458459
: MANAGED_HANDOFF_ALREADY_RUNNING_REASON,
459460
...(beforeVersion ? { before: { version: beforeVersion } } : {}),
460-
steps: ownsManagedServiceHandoff
461+
steps: ownsUpdateOutcome
461462
? [
462463
{
463464
name: "managed-service update handoff",
@@ -557,27 +558,17 @@ export const updateHandlers: GatewayRequestHandlers = {
557558

558559
result = normalizeControlPlaneUpdateResult(result);
559560

560-
// A failed RPC owns the adopted campaign until it explicitly releases it;
561-
// only a started handoff may leave "applying" for the successor process.
562-
if (
563-
result.status !== "ok" &&
564-
handoff?.status !== "started" &&
565-
adoptedCampaignId !== undefined &&
566-
gatewayUpdateCampaign.getState()?.id === adoptedCampaignId
567-
) {
568-
gatewayUpdateCampaign.clear();
569-
context?.logGateway?.info("update.run failed; adopted campaign cleared", {
570-
campaignId: adoptedCampaignId,
571-
});
572-
}
573-
574561
const payload: RestartSentinelPayload = buildUpdateRestartSentinelPayload({
575562
result,
576563
meta: sentinelMeta,
577564
});
578565

566+
// Rejected requests and retired campaigns cannot replace another update's outcome.
567+
if (ownsUpdateOutcome && adoptedCampaignId !== undefined) {
568+
ownsUpdateOutcome = gatewayUpdateCampaign.getState()?.id === adoptedCampaignId;
569+
}
579570
let sentinelPersisted = false;
580-
if (ownsManagedServiceHandoff) {
571+
if (ownsUpdateOutcome) {
581572
try {
582573
await writeRestartSentinel(payload);
583574
sentinelPersisted = true;
@@ -587,6 +578,21 @@ export const updateHandlers: GatewayRequestHandlers = {
587578
}
588579
}
589580

581+
// Publish the outcome before the terminal campaign event prompts clients to
582+
// read it. Recheck ownership after persistence may have yielded to a replacement.
583+
if (
584+
ownsUpdateOutcome &&
585+
result.status !== "ok" &&
586+
handoff?.status !== "started" &&
587+
adoptedCampaignId !== undefined &&
588+
gatewayUpdateCampaign.getState()?.id === adoptedCampaignId
589+
) {
590+
gatewayUpdateCampaign.clear();
591+
context?.logGateway?.info("update.run failed; adopted campaign cleared", {
592+
campaignId: adoptedCampaignId,
593+
});
594+
}
595+
590596
// Only restart the gateway when the update actually succeeded.
591597
// Restarting after a failed update leaves the process in a broken state
592598
// (corrupted node_modules, partial builds) and causes a crash loop.

‎src/infra/restart-sentinel-store.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -591,22 +591,31 @@ export function writeRestartSentinelRowSync(
591591
rawPayload: RestartSentinelPayload,
592592
): RestartSentinel {
593593
const payload = requireValidPayload(rawPayload);
594-
const current = readRestartSentinelRowSync(db);
595-
const currentRevision =
596-
current.kind === "missing"
597-
? null
598-
: current.kind === "valid"
599-
? current.sentinel.revision
600-
: current.revision;
601-
const revision = nextRevision(
602-
maxRevision(currentRevision, readRestartSentinelRevisionFloorSync(db)),
603-
);
594+
const revision = nextRevision(readRestartSentinelSnapshotSync(db).revision);
604595
const row = buildRestartSentinelRow(payload, revision);
605596
upsertRestartSentinelRowSync(db, row);
606597
advanceRestartSentinelRevisionFloorSync(db, revision);
607598
return { version: 1, payload, revision };
608599
}
609600

601+
/** Read inside a transaction; the floor also identifies an absent, consumed notification. */
602+
export function readRestartSentinelSnapshotSync(db: DatabaseSync): {
603+
state: RestartSentinelRowState;
604+
revision: number | null;
605+
} {
606+
const state = readRestartSentinelRowSync(db);
607+
const currentRevision =
608+
state.kind === "missing"
609+
? null
610+
: state.kind === "valid"
611+
? state.sentinel.revision
612+
: state.revision;
613+
return {
614+
state,
615+
revision: maxRevision(currentRevision, readRestartSentinelRevisionFloorSync(db)),
616+
};
617+
}
618+
610619
export function writeUpdateInstallReceiptRowSync(
611620
db: DatabaseSync,
612621
rawPayload: RestartSentinelPayload,
@@ -635,14 +644,12 @@ export function writeRestartSentinelRowIfRevisionSync(
635644
rawPayload: RestartSentinelPayload,
636645
expectedRevision: number,
637646
): RestartSentinel | null {
638-
const current = readRestartSentinelRowSync(db);
647+
const { state: current, revision: previousRevision } = readRestartSentinelSnapshotSync(db);
639648
if (current.kind !== "valid" || current.sentinel.revision !== expectedRevision) {
640649
return null;
641650
}
642651
const payload = requireValidPayload(rawPayload);
643-
const revision = nextRevision(
644-
maxRevision(expectedRevision, readRestartSentinelRevisionFloorSync(db)),
645-
);
652+
const revision = nextRevision(previousRevision);
646653
const row = buildRestartSentinelRow(payload, revision);
647654
const stateDb = getNodeSqliteKysely<GatewayRestartSentinelDatabase>(db);
648655
const result = executeSqliteQuerySync(

‎src/infra/restart-sentinel.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { resolveOpenClawPackageRoot } from "./openclaw-root.js";
1515
import {
1616
deleteRestartSentinelRowSync,
1717
readRestartSentinelRowSync,
18+
readRestartSentinelSnapshotSync,
1819
readUpdateInstallReceiptRowSync,
1920
writeRestartSentinelRowIfRevisionSync,
2021
writeRestartSentinelRowSync,
@@ -59,6 +60,44 @@ export async function writeRestartSentinel(
5960
);
6061
}
6162

63+
/** Publish an outcome only while its producer and the captured notification are unchanged. */
64+
export async function writeRestartSentinelIfUnchanged(params: {
65+
payload: RestartSentinelPayload;
66+
expectedRevision: number | null;
67+
isCurrent: () => boolean;
68+
}): Promise<RestartSentinel | null> {
69+
return runOpenClawStateWriteTransaction(
70+
({ db }) => {
71+
const current = readRestartSentinelSnapshotSync(db);
72+
if (current.state.kind === "invalid" || !params.isCurrent()) {
73+
return null;
74+
}
75+
return current.revision === params.expectedRevision
76+
? writeRestartSentinelRowSync(db, params.payload)
77+
: null;
78+
},
79+
{},
80+
{ operationLabel: "restart-sentinel.write-if-unchanged" },
81+
);
82+
}
83+
84+
export async function readRestartSentinelSnapshot(): Promise<{
85+
sentinel: RestartSentinel | null;
86+
revision: number | null;
87+
}> {
88+
return runOpenClawStateWriteTransaction(
89+
({ db }) => {
90+
const snapshot = readRestartSentinelSnapshotSync(db);
91+
return {
92+
sentinel: snapshot.state.kind === "valid" ? snapshot.state.sentinel : null,
93+
revision: snapshot.revision,
94+
};
95+
},
96+
{},
97+
{ operationLabel: "restart-sentinel.read-snapshot" },
98+
);
99+
}
100+
62101
function cloneRestartSentinelPayload(payload: RestartSentinelPayload): RestartSentinelPayload {
63102
return structuredClone(payload);
64103
}

0 commit comments

Comments
 (0)