diff --git a/AGENTS.md b/AGENTS.md index 560854798..a4e3bbe5e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -408,6 +408,7 @@ The table is static imports, not a runtime glob, so `bun build --compile` (`scri The daemon owns lifecycle, streaming snapshots and first-answer dialog settlement. Web callbacks choose the last-active eligible server and are not blindly retried. `channels_changed` discovers background runs; `sessions_changed` invalidates lists; + `omp_version` reports omp found after a hello that said none (the daemon retries the runner fetch and the read, 30 s doubling to 5 min); one link is the automatic notification publisher. Cross-server fences live in the daemon: a history edit takes the acknowledged `editLease` op before preflight, and a `peer_message_command` marker makes another server's identical echo diff --git a/CHANGELOG.md b/CHANGELOG.md index d4908cd3f..8fa83be0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f - **In-app updates work again on a Windows install without administrator rights.** The update looked for the service supervising the host daemon among its parent processes and skipped Task Scheduler's `svchost.exe` by its executable path, which Windows does not show to a non-administrator. It then took the daemon started by the `omp-web-host` scheduled task for the `Schedule` service, could not control that service and refused the update with `not_controllable` (#537). Service hosts are now recognized by process name, so the scheduled task is found. +- **A host no longer stays "omp not installed" once its runner is there.** The host daemon read the omp version once, at start, while the runner download it had just started in the background (or a fresh runner's first start, which Windows' antivirus can hold past the 30 s probe) was still running. Its server then showed **omp not installed** and refused every message until the daemon restarted, although Settings → Hosts listed the runner's version. The daemon now reads the version again as soon as the download finishes, and otherwise after 30 seconds, doubling up to 5 minutes; each try also downloads the runner again if the first download failed (a host started offline). It tells its servers as soon as omp answers, without reconnecting. A server that predates this learns the version the next time the daemon reconnects to it. + ## [v0.7.3] - 2026-11-18 ### Fixed diff --git a/lib/hosts/daemon.test.mjs b/lib/hosts/daemon.test.mjs index b6734c191..ae664c59e 100644 --- a/lib/hosts/daemon.test.mjs +++ b/lib/hosts/daemon.test.mjs @@ -1,6 +1,6 @@ import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; -import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, utimesSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import test, { after } from "node:test"; @@ -536,6 +536,52 @@ test("an http:// loopback server links without --allow-http", async () => { } }); +test("a host whose omp was missing at start reads it again on its own, without a server asking", async () => { + const log = captureLog(); + renameSync(fakeOmp, `${fakeOmp}.missing`); + const server = scriptedServer(); + let daemon; + try { + daemon = runHostDaemon({ url: server.url, token: "t", allowHttp: false, ompVersionRetryMs: 20 }); + await waitFor(() => server.hellos.length === 1, "the hello"); + assert.equal(server.hellos[0].ompVersion, null); + // Past the first retries (20, 40, 80 ms): each miss arms the next. + await sleep(200); + assert.equal(server.up.some((m) => m.type === "omp_version"), false, "nothing while omp is missing"); + renameSync(`${fakeOmp}.missing`, fakeOmp); + await waitFor(() => server.up.some((m) => m.type === "omp_version"), "the retried omp_version"); + assert.equal(server.up.find((m) => m.type === "omp_version").ompVersion, "14.0.0-fake"); + } finally { + if (existsSync(`${fakeOmp}.missing`)) renameSync(`${fakeOmp}.missing`, fakeOmp); + await daemon?.stop(); + log.restore(); + } +}); + +test("a failed runner fetch is tried again with the omp version read, and its server is told once omp answers", async () => { + const log = captureLog(); + renameSync(fakeOmp, `${fakeOmp}.missing`); + const server = scriptedServer(); + let daemon, fetches = 0; + try { + // The first fetch fails (offline); the next one, from the retry, brings the runner. + const ensureRunner = async () => { + if (++fetches === 1) throw new Error("offline"); + await sleep(50); + renameSync(`${fakeOmp}.missing`, fakeOmp); + }; + daemon = runHostDaemon({ url: server.url, token: "t", allowHttp: false, ensureRunner, ompVersionRetryMs: 20 }); + await waitFor(() => server.up.some((m) => m.type === "omp_version"), "the omp_version message"); + assert.equal(server.hellos[0].ompVersion, null); + assert.equal(server.up.find((m) => m.type === "omp_version").ompVersion, "14.0.0-fake"); + assert.equal(fetches, 2); + } finally { + if (existsSync(`${fakeOmp}.missing`)) renameSync(`${fakeOmp}.missing`, fakeOmp); + await daemon?.stop(); + log.restore(); + } +}); + test("a running host deletes expired staged uploads every hour, not only when it starts or takes an upload", async (t) => { const log = captureLog(); t.mock.timers.enable({ apis: ["setInterval"] }); diff --git a/lib/hosts/daemon.ts b/lib/hosts/daemon.ts index 252f958fc..1f25044f9 100644 --- a/lib/hosts/daemon.ts +++ b/lib/hosts/daemon.ts @@ -97,7 +97,13 @@ export interface HostDaemon { control(command: HostControlCommand): HostControlStatus; } /** `hostSocket`: where omps started from a TUI attach (`hostSocketPath()`); unset, none can. */ -type DaemonOptions = { allowHttp: boolean; url?: string; token?: string; links?: HostLink[]; hostSocket?: string }; +type DaemonOptions = { allowHttp: boolean; url?: string; token?: string; links?: HostLink[]; hostSocket?: string; + /** Fetches the omp runner when this install has none (`ensureOmpRunner`); each omp version retry runs it first. */ + ensureRunner?: () => Promise; + /** Tests. */ ompVersionRetryMs?: number }; +/** A daemon whose omp version read found nothing reads it again after this, doubling up to OMP_VERSION_RETRY_MAX_MS. */ +const OMP_VERSION_RETRY_MS = 30_000; +const OMP_VERSION_RETRY_MAX_MS = 5 * 60_000; export function runHostDaemon(options: DaemonOptions): HostDaemon { const explicit = options.url !== undefined && options.token !== undefined ? [{ url: options.url, token: options.token }] : options.links; const initial = explicit ?? readHostLinks(); @@ -115,6 +121,7 @@ export function runHostDaemon(options: DaemonOptions): HostDaemon { let stopping: Promise | undefined; let updateStateWarningLogged = false; let reloadTimer: NodeJS.Timeout | undefined; + let versionRetry: NodeJS.Timeout | undefined, versionRetryMs = options.ompVersionRetryMs ?? OMP_VERSION_RETRY_MS; let drained = process.env.OMP_WEB_HOST_DRAINED === "1"; let preparationsInFlight = 0, operationsInFlight = 0, nativeUpdatesInFlight = 0; const executable = realpathSync(process.execPath); @@ -510,7 +517,7 @@ export function runHostDaemon(options: DaemonOptions): HostDaemon { } function shutdown(code: number): Promise { if (stopping) return stopping; - stopped = true; watcher.close(); clearTimeout(reloadTimer); clearInterval(sweepTimer); clearInterval(capacityTimer); uploadRetention.stop(); + stopped = true; watcher.close(); clearTimeout(reloadTimer); clearInterval(sweepTimer); clearInterval(capacityTimer); clearTimeout(versionRetry); uploadRetention.stop(); clearTimeout(recoveryTimer); // Freeze before exit handlers observe children stopping. resume.freeze(); @@ -548,7 +555,37 @@ export function runHostDaemon(options: DaemonOptions): HostDaemon { .finally(() => { operationsInFlight--; if (!stopped) for (const link of links.values()) link.start(); + if (!stopped && ompVersion === null) void refreshOmpVersion(); }); + // A null version in the hello marks this host "omp not installed" and its sessions refused. The runner's first fetch, + // or a first start Windows' antivirus holds past the probe, can outlast the startup read: read again until omp answers, + // then tell every server (a link made later says it in its hello). One read at a time. + let refreshing: Promise | undefined; + function scheduleVersionRetry(): void { + clearTimeout(versionRetry); + versionRetry = setTimeout(() => void refreshOmpVersion(), versionRetryMs); + versionRetry.unref?.(); + versionRetryMs = Math.min(versionRetryMs * 2, OMP_VERSION_RETRY_MAX_MS); + } + async function readVersionAgain(): Promise { + await versionRead; + if (stopped || ompVersion !== null) return; + clearTimeout(versionRetry); // this read is the retry; a miss arms the next one + // A fetch that failed at start (offline) is tried again; one still running is joined. + await options.ensureRunner?.().catch(error => log("could not fetch the omp runner: " + reason(error))); + if (stopped || ompVersion !== null) return; + const version = await getOmpVersion(true).catch(error => { log("could not read omp version: " + reason(error)); return null; }); + if (stopped || ompVersion !== null) return; + if (version === null) { + scheduleVersionRetry(); + return; + } + ompVersion = version; + for (const link of links.values()) link.queue({ type: "omp_version", ompVersion: version }, "omp_version"); + } + function refreshOmpVersion(): Promise { + return refreshing ??= readVersionAgain().finally(() => { refreshing = undefined; }); + } const started = Promise.all([startupSweep, startupRecovery, versionRead, hostSocket]).then(() => {}); return { stop: () => shutdown(0), done, control, started }; } diff --git a/lib/hosts/host-cli.ts b/lib/hosts/host-cli.ts index 4f2b9c57d..b8456aa6f 100644 --- a/lib/hosts/host-cli.ts +++ b/lib/hosts/host-cli.ts @@ -80,10 +80,11 @@ async function main(argv: string[]): Promise { try { // The runners of earlier "Update omp" installs: no session of this daemon is running from them any more. sweepOmpRunners(); - // Sessions need the runner; until it is here they fail with OMP_NOT_FOUND and the next start retries. - void ensureOmpRunner().catch((error: unknown) => console.error(`ompweb host: ${error instanceof Error ? error.message : String(error)}`)); + // Sessions need the runner; until it is here they fail with OMP_NOT_FOUND. The daemon joins this fetch, tries it + // again while omp is missing and tells its servers once omp answers. + void ensureOmpRunner().catch(() => {}); // the daemon joins it and logs a failure // Windows has no host socket yet (a current-user named pipe is open in the runner spec). - daemon = runHostDaemon({ allowHttp: argv.includes("--allow-http"), hostSocket: process.platform === "win32" ? undefined : hostSocketPath() }); + daemon = runHostDaemon({ allowHttp: argv.includes("--allow-http"), hostSocket: process.platform === "win32" ? undefined : hostSocketPath(), ensureRunner: ensureOmpRunner }); // Worker cutover starts fail-closed; ordinary hosts have no maintenance environment. if (process.env.OMP_WEB_HOST_DRAINED === "1") daemon.control("drain"); const stop = () => void daemon!.stop(); diff --git a/lib/hosts/hub.test.mjs b/lib/hosts/hub.test.mjs index 7c79371c0..e23577bab 100644 --- a/lib/hosts/hub.test.mjs +++ b/lib/hosts/hub.test.mjs @@ -307,6 +307,19 @@ test("hello without omp marks the host omp-missing and spawnRpc rejects", async await down.cancel(); }); +test("an omp_version after a hello without omp brings the host online", async () => { + const link = await linkedHost("Late omp", { ompVersion: null }); + assert.equal(link.remote.status(), "omp-missing"); + let changes = 0; + const stop = hub.onHostsChange(() => changes++); + assert.deepEqual(linkPost(link, [{ type: "omp_version", seq: 1, ompVersion: "19.0.0" }]), [1]); + stop(); + assert.equal(link.remote.status(), "online"); + assert.ok(changes > 0, "Settings and the sidebar hear of it"); + assert.equal(hub.listHosts().find((h) => h.id === link.id).ompVersion, "19.0.0"); + await link.down.cancel(); +}); + test("a second link with the same token closes the first socket with 4409, and frames still arriving on it are ignored", async () => { const a = await linkedHost("Two daemons"); assert.deepEqual(linkPost(a, [{ type: "stdout", seq: 500, ch: "none", line: "x" }]), [500]); diff --git a/lib/hosts/hub.ts b/lib/hosts/hub.ts index c2cbc4e38..279017aaa 100644 --- a/lib/hosts/hub.ts +++ b/lib/hosts/hub.ts @@ -1003,6 +1003,9 @@ function applyUp(link: Link, message: UpMessage | UnknownMessage, whole?: UpMess return link.channels.get(message.ch)?.deliverLine(message.line); case "stderr": return link.channels.get(message.ch)?.deliverStderr(message.chunk); + case "omp_version": + link.ompVersion = message.ompVersion; + return setStatus(link, link.status === "omp-missing" ? "online" : link.status); case "resync": return link.channels.get(message.ch)?.deliverResync(); case "exit": { diff --git a/lib/hosts/link-protocol.ts b/lib/hosts/link-protocol.ts index 49ec8133b..dd59a01ac 100644 --- a/lib/hosts/link-protocol.ts +++ b/lib/hosts/link-protocol.ts @@ -183,6 +183,9 @@ export type UpMessage = | { type: "resync"; seq: number; ch: string } | { type: "notifier"; seq: number; on: boolean } | { type: "sessions_changed"; seq: number } + /** The omp version found after the hello said none (the runner arrived or first answered later); a hub that predates + * it skips the type and keeps "omp not installed" until the next hello. */ + | { type: "omp_version"; seq: number; ompVersion: string } | { type: "channels_changed"; seq: number; channels: HelloChannel[] } /** The daemon's link samples, at most LINK_LIMITS.statsSamples; a hub that predates it skips the type. Advisory: the * receiver keeps the valid ones and counts the rest in `invalid` (decodeUp only; a sender leaves it out). */ @@ -305,7 +308,7 @@ export function isHostOp(name: string): name is HostOpName { } const DOWN_TYPES: readonly string[] = ["welcome", "stdin", "kill", "interest", "host_tools", "req", "ping", "pong", "ack"]; -const UP_TYPES: readonly string[] = ["hello", "stdout", "stderr", "exit", "resync", "notifier", "sessions_changed", "channels_changed", "stats", "capacity", "config_state", "res", "upload_ack", "workspace_file", "ping", "pong", "ack"]; +const UP_TYPES: readonly string[] = ["hello", "stdout", "stderr", "exit", "resync", "notifier", "sessions_changed", "omp_version", "channels_changed", "stats", "capacity", "config_state", "res", "upload_ack", "workspace_file", "ping", "pong", "ack"]; /** `otherDirection`: the types only the other direction decodes; one of those arrived on the wrong way. */ function unknownMessage(o: Obj, otherDirection: readonly string[]): UnknownMessage { @@ -524,6 +527,8 @@ export function decodeUp(line: string): UpMessage | UnknownMessage { return { type: "notifier", seq: seqNum(o, "seq"), on: bool(o, "on") }; case "sessions_changed": return { type: "sessions_changed", seq: seqNum(o, "seq") }; + case "omp_version": + return { type: "omp_version", seq: seqNum(o, "seq"), ompVersion: str(o, "ompVersion") }; case "channels_changed": return { type: "channels_changed", seq: seqNum(o, "seq"), channels: channels(o) }; case "stats": diff --git a/lib/omp/omp-cli.ts b/lib/omp/omp-cli.ts index 15589cfe9..44aea1009 100644 --- a/lib/omp/omp-cli.ts +++ b/lib/omp/omp-cli.ts @@ -177,7 +177,9 @@ export function resolveOmpBin(fresh = false): string | null { /** Return the installed CLI version, using file metadata to invalidate cached * results without launching omp on every visit. An expiry covers opaque * launchers; concurrent callers share a probe. */ -export function getOmpVersion(): Promise { +export function getOmpVersion(fresh = false): Promise { + // fresh: a probe of its own, past the miss caches; one in flight may have looked before the binary arrived. + if (fresh) { binMissAt = 0; versionMiss = null; return probeOmpVersion(); } versionProbe ??= probeOmpVersion().finally(() => { versionProbe = null; });