diff --git a/CHANGELOG.md b/CHANGELOG.md index c879aa1ae..ec584eb19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f ### Fixed +- **Windows updates no longer fail when the update status is read while it is written.** Host update state, the server worker's status and upgrade plans retry their atomic replacement for a moment when a concurrent reader holds the file, as the server's own update state already did. - **GitHub workflow watches no longer regress to stale run states.** Workflow updates retain GitHub's run timestamp and ignore older snapshots for the same or an earlier attempt, so a delayed `queued` response cannot follow a newer `in_progress` delivery. - **Keep sidebar transcript results aligned with expandable hits on every linked host.** Each daemon now searches its own active conversations, ignoring abandoned branches and pre-compaction messages. diff --git a/lib/hosts/update-worker.ts b/lib/hosts/update-worker.ts index c74bc663a..a81810383 100644 --- a/lib/hosts/update-worker.ts +++ b/lib/hosts/update-worker.ts @@ -120,7 +120,17 @@ export function writeHostUpdateJson(path: string, value: unknown): void { if (existsSync(path)) assertOwnedPath(path); const temporary = `${path}.${randomUUID()}.tmp`; writeFileSync(temporary, JSON.stringify(value), { mode: 0o600, flag: "wx" }); - try { renameSync(temporary, path); } finally { rmSync(temporary, { force: true }); } + try { replaceFileSync(temporary, path); } finally { rmSync(temporary, { force: true }); } +} +/** Renames `temporary` over `path`. On Windows a concurrent reader of `path` can briefly refuse the replacement: retry, never unlink the previous complete file. */ +export function replaceFileSync(temporary: string, path: string): void { + for (let attempt = 0; ; attempt++) { + try { return renameSync(temporary, path); } + catch (error) { + if (process.platform !== "win32" || !["EPERM", "EACCES", "EBUSY"].includes((error as NodeJS.ErrnoException).code ?? "") || attempt >= 20) throw error; + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50); + } + } } export type HostUpdateDecision = "commit" | "cancel"; diff --git a/lib/server-update-worker.ts b/lib/server-update-worker.ts index cc576abce..1357275f1 100644 --- a/lib/server-update-worker.ts +++ b/lib/server-update-worker.ts @@ -5,13 +5,13 @@ // never the Bun server or the hub. self-update.ts prepares everything the worker uses and verifies the result. import { execFile } from "node:child_process"; import { randomUUID } from "node:crypto"; -import { existsSync, lstatSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, lstatSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import { setTimeout as pause } from "node:timers/promises"; import { promisify } from "node:util"; import { requestHostControl, type HostControlStatus } from "./hosts/host-control"; -import { assertOwnedPath, HOST_VERSION_PATTERN, hostLaunch, moveHostRelease, sharedHostInstallReason, validateApprovedReleasePin } from "./hosts/update-worker"; +import { assertOwnedPath, HOST_VERSION_PATTERN, hostLaunch, moveHostRelease, replaceFileSync, sharedHostInstallReason, validateApprovedReleasePin } from "./hosts/update-worker"; import { getAgentDir } from "./omp/paths"; import { isProcessAlive } from "./process-identity"; import { atomicWrite, attemptPath, isActiveLease, leasePath, markerPath, patchStatus, readStateJson, removeSecureFile, renewLease, secureRegularFile, statusPath, type SelfUpdateStage, type StoredStatus } from "./self-update-state"; @@ -267,7 +267,7 @@ export function reportPlan(attemptId: string, participantId: string | null, stat } const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`; writeFileSync(temporary, JSON.stringify(plan), { mode: 0o600, flag: "wx" }); - try { renameSync(temporary, file); } finally { rmSync(temporary, { force: true }); } + try { replaceFileSync(temporary, file); } finally { rmSync(temporary, { force: true }); } } catch (error) { console.error(`Could not record the server update plan's progress: ${message(error)}`); } diff --git a/lib/upgrade-plan.ts b/lib/upgrade-plan.ts index 540f57e69..65284143e 100644 --- a/lib/upgrade-plan.ts +++ b/lib/upgrade-plan.ts @@ -1,5 +1,5 @@ import { createHash, randomUUID } from "node:crypto"; -import { existsSync, mkdirSync, readFileSync, realpathSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readFileSync, realpathSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs"; import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; import { setTimeout as pause } from "node:timers/promises"; import { getAgentDir } from "./omp/paths"; @@ -9,7 +9,7 @@ import { checkNpmUpdate, detectInstallMethod } from "./npm-update"; import { isUpdateDisabled } from "./update-policy"; import { requestHostControl } from "./hosts/host-control"; import { getHostSoftwareUpdates, getHostUpgradeInventory, updateHostSoftware, type HostUpgradeInventory } from "./hosts/hub"; -import { assertOwnedPath, fetchHostUpdateBytes, HOST_VERSION_PATTERN, validateApprovedReleasePin } from "./hosts/update-worker"; +import { assertOwnedPath, fetchHostUpdateBytes, HOST_VERSION_PATTERN, replaceFileSync, validateApprovedReleasePin } from "./hosts/update-worker"; import { HostOpError } from "./hosts/link-protocol"; import type { ApprovedReleasePin, HostUpdateCompatibility } from "./hosts/update-types"; import type { UpgradeApproval, UpgradeParticipant, UpgradePlan } from "./upgrade-types"; @@ -102,7 +102,7 @@ function writePlan(plan: UpgradePlan): void { if (existsSync(path)) assertOwnedPath(path); const temporary = `${path}.${randomUUID()}.tmp`; writeFileSync(temporary, JSON.stringify(plan), { flag: "wx", mode: 0o600 }); - try { renameSync(temporary, path); } finally { if (existsSync(temporary)) unlinkSync(temporary); } + try { replaceFileSync(temporary, path); } finally { if (existsSync(temporary)) unlinkSync(temporary); } } function validParticipant(value: unknown): value is UpgradeParticipant {