From 32ee09a6b05af278231522461817523394112e90 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Wed, 18 Nov 2026 15:10:38 +0000 Subject: [PATCH 1/2] fix(hosts): leave a legacy host's recovery channel alone --- lib/hosts/hub.ts | 3 + lib/hosts/legacy-recovery-channel.test.mjs | 70 ++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 lib/hosts/legacy-recovery-channel.test.mjs diff --git a/lib/hosts/hub.ts b/lib/hosts/hub.ts index e5d488e9d..30e680d90 100644 --- a/lib/hosts/hub.ts +++ b/lib/hosts/hub.ts @@ -877,6 +877,9 @@ function reattach(link: Link, listed: HelloChannel[], refreshExisting = true): v if (refreshExisting || identityChanged) existing.deliverResync(); continue; } + // The legacy recovery's own channel runs its bootstrap, not a session: a wrapper adopting it numbers its commands from 1 like the + // bootstrap, so the recovery reader takes the wrapper's get_state reply for the bootstrap's, closes the channel and drops the real result. + if (hello.creationId !== undefined && hello.creationId === link.recovery?.id) continue; if ([...link.pending.values()].some(op => op.op === "open" && op.sessionId === sessionId)) continue; const child = new RemoteRpcTransport(link.id, ch, sessionId); child.spawned = true; diff --git a/lib/hosts/legacy-recovery-channel.test.mjs b/lib/hosts/legacy-recovery-channel.test.mjs new file mode 100644 index 000000000..08dc65223 --- /dev/null +++ b/lib/hosts/legacy-recovery-channel.test.mjs @@ -0,0 +1,70 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { setTimeout as sleep } from "node:timers/promises"; +import test from "node:test"; +import { mock } from "bun:test"; + +const root = mkdtempSync(join(tmpdir(), "legacy-recovery-channel-")); +const previous = process.env.PI_CODING_AGENT_DIR; +process.env.PI_CODING_AGENT_DIR = root; +const { releaseRepo } = await import("../release-repo.ts"); +const realRecoveryRelease = await import("../recovery-release.ts"); +mock.module("../recovery-release.ts", () => ({ ...realRecoveryRelease, + recoveryHelperRelease: async () => ({ version: "0.7.2", repo: releaseRepo(), assets: [{ name: "ompweb-linux-x64.tar.gz", digest: `sha256:${"c".repeat(64)}` }] }), +})); +const hub = await import("./hub.ts"); +const { readLegacyUpgrade } = await import("./legacy-upgrades.ts"); +const { linkHost, until } = await import("./link-driver.test-helper.mjs"); +const { LINK_PROTOCOL_VERSION } = await import("./link-protocol.ts"); +const operationId = "12345678-1234-4234-8234-123456789012"; +const recoverySession = "01a1eed6-de03-7591-84fb-20c20bb3ecca"; +test.after(() => { + hub.flushLinkSamples(); + if (previous === undefined) delete process.env.PI_CODING_AGENT_DIR; + else process.env.PI_CODING_AGENT_DIR = previous; + rmSync(root, { recursive: true, force: true }); +}); + +// A 0.7.1 daemon lists the recovery omp it opened as a channel with that omp's own session id (creationId = the operation). +// The server must not adopt it as a session: the adoption's get_state is `id: 1` like the bootstrap's bash, and its reply +// was taken for the bootstrap's, which closed the channel seconds in and lost the real result (ai-worker-2, 2026-11-18). +test("a legacy host's recovery channel is never adopted, so the bootstrap's own result is the one that is read", async (t) => { + const link = await linkHost("Legacy recovery channel", { hello: { v: LINK_PROTOCOL_VERSION, platform: "linux", home: "/tmp" } }); + const opens = []; + const commands = []; + let closes = 0; + const serving = link.serve({ + checkUpdates: () => ({ ompweb: { currentVersion: "0.7.1", availableVersion: "0.7.2", updateAvailable: true, canUpdate: false, + blockedReason: null, updateCommand: "", installMethod: "binary", protocolVersion: LINK_PROTOCOL_VERSION }, omp: { currentVersion: null }, operation: null }), + open: (args) => { + opens.push(args); + return { ch: "recovery", joined: true, pending: [], streaming: null, cwd: "/tmp", fellBack: false, launchAdvisor: false, advisorSpawned: false, autoApproveExtension: false }; + }, + close: () => { closes++; return {}; }, + }, async (message) => { + if (message.type !== "stdin") return; + const command = JSON.parse(message.line); + commands.push(command); + // The runner answers any command with its own id, as the daemon maps it back to the sender's. + if (command.op === "get_state") await link.post({ type: "stdout", ch: message.ch, line: JSON.stringify({ t: "res", id: command.id, ok: true, data: { sessionId: recoverySession, sessionFile: "", isStreaming: false, isCompacting: false } }) }); + }); + t.after(async () => { hub.closeHostLink(link.id); await serving; }); + + await hub.updateHostSoftware(link.id, "ompweb", "0.7.2", { force: true, operationId }); + await until(() => commands.length === 2, "both recovery lines"); + await link.post({ type: "channels_changed", channels: [{ ch: "recovery", sessionId: recoverySession, cwd: "/tmp", pending: [], creationId: operationId }] }); + await sleep(100); + assert.equal(opens.length, 1, "only the recovery's own open: no join of the channel as a session"); + assert.deepEqual(commands.map((command) => command.op ?? command.type), ["bash", "bash"], "nothing but the bootstrap is sent to its channel"); + assert.equal(closes, 0); + assert.equal(readLegacyUpgrade(link.id)?.operation.state, "running"); + + // The bootstrap fails after the channel was listed: that exit code is what reaches the operation. + await link.post({ type: "stdout", ch: "recovery", line: JSON.stringify({ t: "res", id: 1, ok: true, data: { exitCode: 1 } }) }); + await until(() => closes === 1, "the recovery channel's close after the bootstrap's result"); + const state = readLegacyUpgrade(link.id)?.operation; + assert.equal(state?.state, "failed"); + assert.match(state?.error ?? "", /exited with code 1/); +}); From 1feda6937a6f52258dd99018f99dc3305d7aec84 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Wed, 18 Nov 2026 15:10:42 +0000 Subject: [PATCH 2/2] docs: note the recovery channel is never adopted --- AGENTS.md | 2 +- CHANGELOG.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 560854798..210473dee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1787,7 +1787,7 @@ during the wait. - **Settings → System** prepares and reviews a durable server-and-host upgrade plan, including owned compiled releases. `POST /api/app-update {action:"prepare"}` returns the plan; commit requires `{action:"commit",attemptId,approval:{planId,force}}`. Approval is bound to release identity and the registered-host/session snapshot. An incompatible host without a verified recovery path blocks server cutover. - **Settings → Hosts** manages individual daemons, including **This machine**. Normal `POST {target:"ompweb",version}` does not interrupt activity. `force:true` requires distinct GUI confirmation and may end sessions/subagents. Machine-readable `canForceUpdate` and blockers govern the action, never English message parsing. Force does not waive integrity, ownership, process identity, disabled updates, local drains or service permissions. - **Update omp** (`POST {target:"omp",version}`, no `force`; same auth/origin checks and `OMP_WEB_DISABLE_AUTOUPDATE` gate). Between ompweb releases a host gets a newer omp from the fork's releases: `checkUpdates` reports `omp: { currentVersion, availableVersion, updateAvailable, canUpdate, blockedReason, blockers, operation, runnerChannel: true }` (a daemon without `runnerChannel` reports only a version, and the hub keeps just that). The source is the newest published release of `omp-runner.json`'s repo that attaches this platform's runner and whose `ompweb-omp.json` names this ompweb's `RUNNER_PROTOCOL` (`findRunnerRelease`: 100 releases a page, at most `RELEASE_PAGE_CAP` pages, so a release that speaks another protocol is skipped, never offered); it is available when that release's tag is another build than the running omp and, where both have a stamp, a newer one (never a downgrade). The daemon installs it in-process (no worker, no restart; `hostUpdateBlocksNewWork` is false for it) into `/omp-web/omp///`: downloaded into a fresh staging directory, checked there (SHA-256, `--runner-info` protocol, `--smoke-test`), renamed into place and only then pointed at by `current.json`. A failure at any step leaves the pointer and the selected runner exactly as they were. Running sessions keep their binary, new spawns use the pointed one, and an ompweb update starts from its own pinned runner (a new version has a new directory). A caller's `operationId` names one omp update: a replay returns the stored operation, running or settled, installing nothing again; the same id for another version, an id an ompweb operation holds, or the id of an earlier attempt a later one superseded (`omp-update.json` keeps the last 32 in `earlier`) is `operation_conflict`; an ompweb update request with an omp operation's id is too. **It never touches ompweb's record.** It keeps its own (`/omp-web/host-update/omp-update.json`, reported as `omp.operation`; `HostUpdateInfo.operation` stays ompweb's, which a coordinated upgrade verifies in `status.json`) and takes the shared claim lock (`lock.json`, which records its `target`), so the two never run together: an omp update is refused with blocker/error code `ompweb_update_active` while an ompweb operation is running (prepared, committing and recovery-required included) or any ompweb claim is live, and `omp_install_active` while another omp install runs; an ompweb update is blocked by `omp_install_active` (its start fails `update_in_progress`) while an omp install holds the claim. Also blocked by the disable switch, or by an omp the install does not choose (`OMP_WEB_OMP_BIN`: `unsupported_install`). The hub answers a blocked omp POST with the first blocker's code. -- **Recovery plane.** Modern candidates may acknowledge an approved future server protocol through an operation-bound preflight nonce and maintenance receipt. Legacy recovery admits only a fixed bootstrap on one operation-bound runner channel, never general old application traffic. Windows uses native process/service/task handling. Old daemons copy only `update-worker.ts`; its startup must work without sibling modules and load shared lifecycle code from verified sources before replacing an install. +- **Recovery plane.** Modern candidates may acknowledge an approved future server protocol through an operation-bound preflight nonce and maintenance receipt. Legacy recovery admits only a fixed bootstrap on one operation-bound runner channel, never general old application traffic. That channel (`creationId` = the operation id) is the bootstrap's alone: `reattach` skips it, because a wrapper adopting it would send `get_state` as command id 1, and the recovery reader takes any id-`requestId` reply for the bootstrap's. Windows uses native process/service/task handling. Old daemons copy only `update-worker.ts`; its startup must work without sibling modules and load shared lifecycle code from verified sources before replacing an install. - **Completion and rollback.** `upgrade-plan.ts`, `self-update.ts` and `server-update-worker.ts` persist progress; POST acceptance and shell output are not success. Verify the expected release, live process identity and operation/boot receipt after reconnect. Retain the prior install until healthy, and report partial recovery explicitly. See `docs/superpowers/specs/2026-11-14-coordinated-gui-upgrades-design.md` when changing upgrade or bootstrap contracts. - **Published-release proof.** `lib/old-server-upgrade.integration.test.mjs` (`OMPWEB_OLD_SERVER_E2E=1`, Linux CI) installs npm 0.6.0, 0.7.0 and 0.7.1 with real npm, updates each through its own GUI (`POST /api/app-update` prepare/commit) to this checkout, then recovers it. The old servers never send their launcher the arm message, so on a plain unsupervised `ompweb` the worker's SIGTERM ends the launcher with the server, nobody answers its restart request, and it reports success with nothing running: the user runs `ompweb` again. That code is the old install's own `bin/omp-web-update-worker.js` and `bin/omp-web.js`; current code cannot change it. An old daemon only upgrades through Settings → Hosts Force (`forceMethod:"recovery"`). - `/api/omp-update` keeps only `restart`, an explicit server-side maintenance API for existing callers. The settings UI and AppShell no longer use it as a global update control or send a server-wide session restart after a host update. diff --git a/CHANGELOG.md b/CHANGELOG.md index b0e2dfe5b..68687c2de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f ## [Unreleased] +### Fixed + +- **Force update of a 0.7.x host no longer joins its own recovery channel.** The legacy recovery's omp was listed by the daemon like any session, so this server adopted it: its `get_state` (command id 1, like the bootstrap's) was answered with an id-1 reply that the recovery reader took for the bootstrap's result, which closed the channel seconds in and lost the real exit code. A failed bootstrap then showed only as "no receipt within 30 minutes". The recovery channel (matched by its operation id) is now left alone, so the bootstrap's own result is read. + ## [v0.7.2] - 2026-11-18 ### Upgrading from 0.7.1 or older