Skip to content

The agent that grows with you

refactor(desktop): one hold state machine for the boot and pool waits, one UpdateHoldWire

Teknium7b27ff08a6c5
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 565a4ded13b..f63c54ae586 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -602,17 +602,16 @@ import {
waitForUpdateClearance
} from './update-gate'
import { reportHandoffResult } from './update-handoff-report'
+import type { UpdateHoldWire } from './update-hold-types'
import {
createUpdateHoldScreen,
- holdGraceClock,
type MarkerGateCallbacks,
markerGateProbe,
registerUpdateHoldIpc,
- type UpdateHoldWire,
waitForPoolUpdateClearance
} from './update-hold-wiring'
import { describeSkippedPrewrite, markerPath, updateHandoffConflict, writeUpdateMarker } from './update-marker'
-import { type HeldState, heldWaitMessage } from './update-marker-gate'
+import { heldWaitMessage, holdTicker } from './update-marker-gate'
import { updateConnectionsBeforeLocal } from './update-order'
import {
resolveUpdaterMechanism,
@@ -3031,10 +3030,8 @@ async function waitForUpdateToFinish() {
// run's, never an older one (C2 started_at match).
let parkedRunStartedAt: number | null = null
// A dead marker whose checkout a leftover process still holds (R6), or whose
- // ownership the helper could not establish: its state this poll, and since
- // when it has blocked this wait (the blocked screen's grace, R8 D3).
- let held: HeldState | null = null
- const blockedHold = holdGraceClock()
+ // ownership the helper could not establish (the blocked screen, R8 D3).
+ const hold = holdTicker({ show: state => showUpdateHold(state), clear: () => clearUpdateHold() })
// The wait ended because the user chose Start anyway, not because the
// update finished (R8 m7): no bundle-swap relaunch mid-hold.
let overridden = false
@@ -3044,9 +3041,7 @@ async function waitForUpdateToFinish() {
parkedRunStartedAt = marker.startedAt
overridden = false
},
- onHeld: state => {
- held = state
- },
+ onHeld: hold.onHeld,
onOverride: () => {
overridden = true
}
@@ -3060,22 +3055,15 @@ async function waitForUpdateToFinish() {
rememberLog(`[updates] update in progress (${reason}); deferring backend start until it finishes`)
}
- const heldNow: HeldState | null = held
- held = null
-
- const blocked = blockedHold(reason, heldNow)
-
// Never a timeout (R8 D3): past the grace the boot shows the blocked
// screen and stays parked until the hold ends, the user quits, or the
// user confirms Start anyway (registerUpdateHoldIpc).
- if (blocked) {
- showUpdateHold(blocked)
+ const { held: heldNow, shown } = hold.tick(reason)
+ if (shown) {
return
}
- clearUpdateHold()
-
if (reason === 'marker' && heldNow) {
await advanceBootProgress('backend.update-wait', heldWaitMessage(heldNow), 12)
diff --git a/apps/desktop/electron/update-hold-types.ts b/apps/desktop/electron/update-hold-types.ts
new file mode 100644
index 00000000000..0846ff90c1f
--- /dev/null
+++ b/apps/desktop/electron/update-hold-types.ts
@@ -0,0 +1,15 @@
+// What the blocked boot screen shows while an update hold keeps the local
+// backend stopped (R8 D3). Shared by the main process (update-hold-wiring.ts)
+// and the renderer (src/global.d.ts), so the wire shape has one definition.
+export interface UpdateHoldWire {
+ holdId: string
+ /** `held`: a leftover process holds the checkout lock; `busy`/`error`: ownership could not be verified. */
+ verdict: 'held' | 'busy' | 'error'
+ /** The update process that wrote the marker (exited), when known. */
+ ownerPid: number | null
+ /** Epoch ms the hold was first seen. */
+ since: number
+ /** Epoch ms of the latest check. */
+ checkedAt: number
+ logPath: string
+}
diff --git a/apps/desktop/electron/update-hold-wiring.test.ts b/apps/desktop/electron/update-hold-wiring.test.ts
index 8b899fd9bf4..c5292d3c6f3 100644
--- a/apps/desktop/electron/update-hold-wiring.test.ts
+++ b/apps/desktop/electron/update-hold-wiring.test.ts
@@ -2,7 +2,8 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
-import { registerUpdateHoldIpc, type UpdateHoldWire } from './update-hold-wiring'
+import type { UpdateHoldWire } from './update-hold-types'
+import { registerUpdateHoldIpc } from './update-hold-wiring'
const HOLD: UpdateHoldWire = {
holdId: 'hold-1',
diff --git a/apps/desktop/electron/update-hold-wiring.ts b/apps/desktop/electron/update-hold-wiring.ts
index d15929a97cb..2601f19b47c 100644
--- a/apps/desktop/electron/update-hold-wiring.ts
+++ b/apps/desktop/electron/update-hold-wiring.ts
@@ -8,12 +8,13 @@ import path from 'node:path'
import type { IpcMain, IpcMainInvokeEvent } from 'electron'
import { type UpdateGateDeps, waitForUpdateClearance, type WaitForUpdateClearanceOptions } from './update-gate'
+import type { UpdateHoldWire } from './update-hold-types'
import { cachedCreateTimeProbe } from './update-marker'
import {
allowStartOverHold,
type HeldState,
heldWaitMessage,
- HOLD_SCREEN_GRACE_MS,
+ holdTicker,
liveMarkerProbe,
PRIMARY_HOLD_OWNER,
requestHoldRecheck,
@@ -22,17 +23,6 @@ import {
} from './update-marker-gate'
import { runMarkerHelper } from './updater/marker-helper'
-// What the blocked boot screen shows (R8 D3). The renderer mirrors this as
-// `DesktopUpdateHold` in src/global.d.ts.
-export interface UpdateHoldWire {
- holdId: string
- verdict: 'held' | 'busy' | 'error'
- ownerPid: number | null
- since: number
- checkedAt: number
- logPath: string
-}
-
export interface MarkerGateCallbacks {
onLiveMarker?: (marker: { startedAt: number | null }) => void
onHeld?: (state: HeldState) => void
@@ -75,21 +65,6 @@ export function markerGateProbe(host: MarkerGateHost, { onLiveMarker, onHeld, on
})
}
-/**
- * A wait's blocked-screen clock: the hold to show once a blocking marker hold
- * has lasted HOLD_SCREEN_GRACE_MS without a break, else null. Never a timeout:
- * past the grace the wait stays parked behind the screen.
- */
-export function holdGraceClock(now: () => number = Date.now) {
- let blockedSince: number | null = null
-
- return (reason: string | null, held: HeldState | null): HeldState | null => {
- blockedSince = reason === 'marker' && held?.blocking ? (blockedSince ?? now()) : null
-
- return held && blockedSince !== null && now() - blockedSince >= HOLD_SCREEN_GRACE_MS ? held : null
- }
-}
-
export interface UpdateHoldScreenHost {
hermesHome: string
log: (line: string) => void
@@ -276,13 +251,16 @@ export async function waitForPoolUpdateClearance(host: PoolUpdateWaitHost): Prom
const { profile } = host
let poolAnnounced = false
let poolHoldLogged: string | null = null
- let poolHeld: HeldState | null = null
- const poolBlockedHold = holdGraceClock()
const holdOwner = `pool:${host.poolKey}`
+ const hold = holdTicker({
+ show: state => host.showHold(state, holdOwner),
+ clear: () => host.clearHold(holdOwner)
+ })
+
const poolGateDeps = host.gateDeps({
onHeld: state => {
- poolHeld = state
+ hold.onHeld(state)
if (state.blocking && poolHoldLogged !== state.holdId) {
poolHoldLogged = state.holdId
@@ -304,14 +282,7 @@ export async function waitForPoolUpdateClearance(host: PoolUpdateWaitHost): Prom
host.log(`[updates] update in progress (${reason}); deferring pool backend start for profile "${profile}"`)
}
- const blocked = poolBlockedHold(reason, poolHeld)
- poolHeld = null
-
- if (blocked) {
- host.showHold(blocked, holdOwner)
- } else {
- host.clearHold(holdOwner)
- }
+ hold.tick(reason)
},
pollMs: host.pollMs,
timeoutMs: host.timeoutMs
diff --git a/apps/desktop/electron/update-marker-gate.test.ts b/apps/desktop/electron/update-marker-gate.test.ts
index ad8aa4ec092..b01d099fb5d 100644
--- a/apps/desktop/electron/update-marker-gate.test.ts
+++ b/apps/desktop/electron/update-marker-gate.test.ts
@@ -16,6 +16,8 @@ import {
allowStartOverHold,
type HeldState,
heldWaitMessage,
+ HOLD_SCREEN_GRACE_MS,
+ holdTicker,
liveMarkerProbe,
PRIMARY_HOLD_OWNER,
requestHoldRecheck,
@@ -304,3 +306,60 @@ test('without a protocol-2 script the gate judges dead = not running and deletes
assert.equal(await liveMarkerProbe({ hermesHome: home, reclaim: null })(), false)
assert.equal(fs.readFileSync(markerPath(home), 'utf8'), body)
})
+
+// Review 5411222842 (shape): the boot and pool waits share one hold state
+// machine. A blocking hold shows only after an unbroken grace, any break
+// restarts the grace and clears this wait's hold, and nothing ever times out.
+describe('holdTicker', () => {
+ const state = (blocking: boolean): HeldState => ({
+ verdict: 'held',
+ ownerPid: 7,
+ livePid: null,
+ holdId: 'h1',
+ since: 0,
+ checkedAt: 0,
+ blocking
+ })
+
+ test('shows a blocking hold after an unbroken grace and clears on any break', () => {
+ let clock = 0
+ const shown: string[] = []
+
+ const hold = holdTicker({
+ show: s => shown.push(`show:${s.holdId}`),
+ clear: () => shown.push('clear'),
+ now: () => clock
+ })
+
+ hold.onHeld(state(true))
+ assert.deepEqual(hold.tick('marker'), { held: state(true), shown: false })
+ clock += HOLD_SCREEN_GRACE_MS
+ hold.onHeld(state(true))
+ assert.equal(hold.tick('marker').shown, true)
+ // A poll with no held verdict (the probe saw a live owner) breaks the hold.
+ assert.deepEqual(hold.tick('marker'), { held: null, shown: false })
+ hold.onHeld(state(true))
+ clock += HOLD_SCREEN_GRACE_MS - 1
+ assert.equal(hold.tick('marker').shown, false, 'the grace restarts after a break')
+ clock += 10 * OLD_HELD_CEILING_MS
+ hold.onHeld(state(true))
+ assert.equal(hold.tick('marker').shown, true, 'never a timeout: still parked behind the screen')
+ assert.deepEqual(shown, ['clear', 'show:h1', 'clear', 'clear', 'show:h1'])
+ })
+
+ test('a non-blocking hold or a non-marker reason never shows the screen', () => {
+ let clock = 0
+ const hold = holdTicker({ show: () => assert.fail('shown'), clear: () => {}, now: () => clock })
+
+ for (const [reason, blocking] of [
+ ['marker', false],
+ ['in-flight', true]
+ ] as const) {
+ hold.onHeld(state(blocking))
+ hold.tick(reason)
+ clock += HOLD_SCREEN_GRACE_MS * 2
+ hold.onHeld(state(blocking))
+ assert.equal(hold.tick(reason).shown, false)
+ }
+ })
+})
diff --git a/apps/desktop/electron/update-marker-gate.ts b/apps/desktop/electron/update-marker-gate.ts
index 14125ca0ff1..3709a46b6cf 100644
--- a/apps/desktop/electron/update-marker-gate.ts
+++ b/apps/desktop/electron/update-marker-gate.ts
@@ -143,6 +143,40 @@ export class UpdateHoldBoard {
}
}
+/**
+ * One wait's hold state machine, shared by the boot and pool/profile waits:
+ * `onHeld` keeps the probe's latest held state, and `tick` (once per wait
+ * poll) consumes it, shows it once a blocking hold has lasted
+ * HOLD_SCREEN_GRACE_MS without a break, and clears this wait's hold
+ * otherwise. Never a timeout: past the grace the wait stays parked behind
+ * the blocked screen (R8 D3).
+ */
+export function holdTicker(screen: { show: (state: HeldState) => void; clear: () => void; now?: () => number }) {
+ const now = screen.now ?? Date.now
+ let held: HeldState | null = null
+ let blockedSince: number | null = null
+
+ return {
+ onHeld(state: HeldState) {
+ held = state
+ },
+ tick(reason: string | null): { held: HeldState | null; shown: boolean } {
+ const seen = held
+ held = null
+ blockedSince = reason === 'marker' && seen?.blocking ? (blockedSince ?? now()) : null
+ const shown = seen !== null && blockedSince !== null && now() - blockedSince >= HOLD_SCREEN_GRACE_MS
+
+ if (shown) {
+ screen.show(seen)
+ } else {
+ screen.clear()
+ }
+
+ return { held: seen, shown }
+ }
+ }
+}
+
/**
* The user confirmed "Start anyway" over this exact held marker body. The
* caller logs the override; the marker is left in place.
@@ -205,7 +239,9 @@ async function refreshVerdict(
}
if (!previous || STILL_RUNNING.has(previous.verdict.kind) !== STILL_RUNNING.has(verdict.kind)) {
- log?.(`[updates] dead update marker: script helper says ${verdict.kind}${'pid' in verdict ? ` ${verdict.pid}` : ''}`)
+ log?.(
+ `[updates] dead update marker: script helper says ${verdict.kind}${'pid' in verdict ? ` ${verdict.pid}` : ''}`
+ )
}
return { ...previous, verdict, at: now(), generation }
diff --git a/apps/desktop/src/components/update-hold-overlay.tsx b/apps/desktop/src/components/update-hold-overlay.tsx
index 2ff0973601a..da1f13967f6 100644
--- a/apps/desktop/src/components/update-hold-overlay.tsx
+++ b/apps/desktop/src/components/update-hold-overlay.tsx
@@ -2,7 +2,7 @@ import { useStore } from '@nanostores/react'
import { useEffect, useId, useState } from 'react'
import { Button } from '@/components/ui/button'
-import type { DesktopUpdateHold } from '@/global'
+import type { UpdateHoldWire } from '@/global'
import { useI18n } from '@/i18n'
import { AlertTriangle, FileText, Loader2, Power, RefreshCw } from '@/lib/icons'
import { $desktopBoot } from '@/store/boot'
@@ -27,7 +27,7 @@ export function UpdateHoldOverlay() {
const [busy, setBusy] = useState<Busy>(null)
const [confirming, setConfirming] = useState(false)
const [refused, setRefused] = useState(false)
- const hold: DesktopUpdateHold | null = boot.error ? null : (boot.updateHold ?? null)
+ const hold: UpdateHoldWire | null = boot.error ? null : (boot.updateHold ?? null)
const holdId = hold?.holdId ?? null
const checkedAt = hold?.checkedAt ?? null
diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts
index 946af79ee6c..6d56e770331 100644
--- a/apps/desktop/src/global.d.ts
+++ b/apps/desktop/src/global.d.ts
@@ -8,6 +8,7 @@ import type { MachineProfile } from '../electron/machine-profile'
import type { HermesNotification } from '../electron/notification-types'
import type { PoolLimits } from '../electron/pool-limits'
import type { KeepAwakeMode } from '../electron/power-save'
+import type { UpdateHoldWire } from '../electron/update-hold-types'
import type { UpdateRunReport } from '../electron/updater/update-metrics'
import type { GrowRequest } from '../electron/window-growth'
@@ -1401,23 +1402,12 @@ export interface DesktopBootProgress {
statusCode?: number | null
timestamp: number
/** Set while an earlier update's hold keeps the local backend from starting (blocked boot screen). */
- updateHold?: DesktopUpdateHold | null
-}
-
-/** What holds the install while the boot is blocked (electron/update-hold-wiring.ts UpdateHoldWire). */
-export interface DesktopUpdateHold {
- holdId: string
- /** `held`: a leftover process holds the checkout lock; `busy`/`error`: ownership could not be verified. */
- verdict: 'held' | 'busy' | 'error'
- /** The update process that wrote the marker (exited), when known. */
- ownerPid: number | null
- /** Epoch ms the hold was first seen. */
- since: number
- /** Epoch ms of the latest check. */
- checkedAt: number
- logPath: string
+ updateHold?: UpdateHoldWire | null
}
+/** What holds the install while the boot is blocked: one definition, shared with the main process. */
+export type { UpdateHoldWire } from '../electron/update-hold-types'
+
// First-launch install ("bootstrap") event types -- emitted by
// electron/bootstrap-runner.ts and observed by the renderer install overlay.
// Mirrors the event shapes emitted by runBootstrap()'s onEvent callback.
diff --git a/apps/desktop/src/store/boot.ts b/apps/desktop/src/store/boot.ts
index a0422dc7aa0..0600c27d330 100644
--- a/apps/desktop/src/store/boot.ts
+++ b/apps/desktop/src/store/boot.ts
@@ -1,6 +1,6 @@
import { atom } from 'nanostores'
-import type { DesktopBootProgress, DesktopUpdateHold } from '@/global'
+import type { DesktopBootProgress, UpdateHoldWire } from '@/global'
import { translateNow } from '@/i18n'
export interface DesktopBootState extends DesktopBootProgress {
@@ -47,7 +47,7 @@ export function applyDesktopBootProgress(progress: DesktopBootProgress) {
}
/** The blocked-update screen's hold alone, outside the boot steps (R8 M6). */
-export function applyDesktopUpdateHold(hold: DesktopUpdateHold | null) {
+export function applyDesktopUpdateHold(hold: UpdateHoldWire | null) {
$desktopBoot.set({ ...$desktopBoot.get(), updateHold: hold })
}