Skip to content

The agent that grows with you

fix(desktop): keep update gate closed on indeterminate helper results

Teknium0b4eb2e8c3c2
diff --git a/apps/desktop/electron/update-marker-gate.test.ts b/apps/desktop/electron/update-marker-gate.test.ts
index 96ba4162fd3..7c8cb2413dc 100644
--- a/apps/desktop/electron/update-marker-gate.test.ts
+++ b/apps/desktop/electron/update-marker-gate.test.ts
@@ -75,10 +75,39 @@ describe.skipIf(process.platform === 'win32')('gate over a dead marker (R6)', ()
assert.equal(await gate(root, home, () => clock)(), false, 'a later gate wait does not restart the ceiling')
})
+ test('a failing protocol-2 helper keeps the gate parked and is retried after recovery', async () => {
+ const { root, home } = fakeHelperCheckout()
+ const body = `${await deadPid()}\n${minutesAgo(1)}\nct:1.000\n`
+ fs.writeFileSync(markerPath(home), body)
+ fs.writeFileSync(path.join(home, 'helper-verdict'), 'held')
+ fs.writeFileSync(path.join(home, 'helper-exit'), '1')
+ let clock = Date.now()
+ const probe = gate(root, home, () => clock)
+ assert.equal(await probe(), true, 'an operational failure is not legacy capability absence')
+ fs.writeFileSync(path.join(home, 'helper-exit'), '0')
+ clock += 5_000
+ assert.equal(await probe(), true)
+ assert.equal(helperCalls(home).length, 2, 'same waiter retries the failed helper')
+ fs.writeFileSync(path.join(home, 'helper-verdict'), 'reclaimed')
+ clock += 5_000
+ assert.equal(await probe(), false)
+ })
+
+ test('sidecar contention over an old marker is indeterminate, not an expired held checkout', async () => {
+ const { root, home } = fakeHelperCheckout()
+ fs.writeFileSync(markerPath(home), `${await deadPid()}\n${minutesAgo(180)}\nct:1.000\n`)
+ fs.writeFileSync(path.join(home, 'helper-verdict'), 'busy')
+ const states: HeldState[] = []
+ assert.equal(await gate(root, home, undefined, { onHeld: s => states.push(s) })(), true)
+ assert.equal(states[0].remainingMs, null)
+ assert.equal(states[0].expired, false)
+ assert.match(heldWaitMessage(states[0]), /verify.*ownership/)
+ })
+
test('a malformed held marker counts its ceiling from the first sighting, across gate waits', async () => {
const { root, home } = fakeHelperCheckout()
fs.writeFileSync(markerPath(home), `garbage-${process.pid}-${Date.now()}\n`)
- fs.writeFileSync(path.join(home, 'helper-verdict'), 'busy')
+ fs.writeFileSync(path.join(home, 'helper-verdict'), 'held')
let clock = Date.now()
assert.equal(await gate(root, home, () => clock)(), true)
@@ -136,7 +165,7 @@ describe.skipIf(process.platform === 'win32')('gate over a dead marker (R6)', ()
})
test('`unsupported` (old checkout) opens the gate WITHOUT deleting; asked once per distinct body', async () => {
- const { root, home } = fakeHelperCheckout()
+ const { root, home } = fakeHelperCheckout('')
const body = `${await deadPid()}\n${minutesAgo(1)}\n`
fs.writeFileSync(markerPath(home), body)
fs.writeFileSync(path.join(home, 'helper-verdict'), 'usage: unknown option --marker-op')
@@ -145,13 +174,13 @@ describe.skipIf(process.platform === 'win32')('gate over a dead marker (R6)', ()
assert.equal(await hasLiveMarker(), false)
assert.equal(await hasLiveMarker(), false)
- assert.equal(helperCalls(home).length, 1)
+ assert.equal(helperCalls(home).length, 0, 'legacy helper is never invoked')
assert.equal(fs.readFileSync(markerPath(home), 'utf8'), body, 'dead = not running, and left in place')
const next = `${await deadPid()}\n${minutesAgo(1)}\n`
fs.writeFileSync(markerPath(home), next)
assert.equal(await hasLiveMarker(), false)
- assert.equal(helperCalls(home).length, 2, 'a different dead body is asked about again')
+ assert.equal(helperCalls(home).length, 0, 'legacy capability absence remains unsupported')
})
test('a live owner never reaches the helper; an absent marker neither', async () => {
diff --git a/apps/desktop/electron/update-marker-gate.ts b/apps/desktop/electron/update-marker-gate.ts
index 9b625c7b725..e2c08db42b1 100644
--- a/apps/desktop/electron/update-marker-gate.ts
+++ b/apps/desktop/electron/update-marker-gate.ts
@@ -17,7 +17,7 @@
* `busy` / `live` answer is re-asked every `reprobeMs` (5 s) because only the
* script can see the lock being released.
*
- * `held` / `busy` have a ceiling (review R6 m7): no owner identity is alive,
+ * Only verified `held` has a ceiling (review R6 m7): no owner identity is alive,
* only some process still holding the checkout lock, and that can be a
* leaked long-lived one. The scripts stop waiting on it after RELEASE_WAIT_S
* (7200 s); the gate stops blocking after the same span, counted from the
@@ -27,6 +27,7 @@
* running", logs it once and reports it through `onHeld` so the caller can
* tell the user; the marker is left exactly as it is. A helper `live <pid>`
* names a live identity and is waited out like any live marker (C1 rule 3).
+ * `busy`/`error` are indeterminate, retried without granting clearance.
*/
import { type CreateTimeProbe, inspectUpdateMarker } from './update-marker'
@@ -39,7 +40,7 @@ export const HELD_CEILING_MS = 7_200_000
/** Why a dead marker still keeps the gate closed, as the script helper put it. */
export interface HeldState {
- verdict: 'held' | 'busy' | 'live'
+ verdict: 'held' | 'busy' | 'live' | 'error'
/** Line 1 of the marker: the update process that started it (exited), when it parses. */
ownerPid: number | null
/** The live process the helper named (`live <pid>`), else null. */
@@ -67,7 +68,7 @@ export interface LiveMarkerProbeOptions {
heldCeilingMs?: number
}
-const STILL_RUNNING = new Set(['held', 'busy', 'live'])
+const STILL_RUNNING = new Set(['held', 'busy', 'live', 'error'])
// First sighting of each held body, process-wide: a later gate wait (a pool
// backend, a reconnect) continues the same ceiling instead of restarting it.
@@ -137,7 +138,8 @@ export function liveMarkerProbe({
const startedAt = inspection.marker?.startedAt ?? null
const since = heldSince(key, startedAt, now())
- const remainingMs = verdict.kind === 'live' ? null : Math.max(0, since + heldCeilingMs - now())
+ // `busy`/`error` did not establish ownership: stale bytes cannot grant clearance.
+ const remainingMs = verdict.kind === 'held' ? Math.max(0, since + heldCeilingMs - now()) : null
const state: HeldState = {
verdict: verdict.kind as HeldState['verdict'],
@@ -177,6 +179,10 @@ function duration(ms: number): string {
/** Boot-progress text while a dead marker's checkout is still held. */
export function heldWaitMessage(state: HeldState): string {
+ if (state.verdict === 'busy' || state.verdict === 'error') {
+ return 'Hermes could not verify update ownership yet — startup is paused while it retries. Details are in logs/update.log.'
+ }
+
if (state.livePid !== null) {
return `An update is still finishing (process ${state.livePid}) — Hermes will start automatically when it completes…`
}
diff --git a/apps/desktop/electron/updater/marker-helper.test.ts b/apps/desktop/electron/updater/marker-helper.test.ts
index 90f59bd1282..61d0749146c 100644
--- a/apps/desktop/electron/updater/marker-helper.test.ts
+++ b/apps/desktop/electron/updater/marker-helper.test.ts
@@ -55,13 +55,13 @@ test.each([
['foreign', { kind: 'foreign' }],
['live 4242', { kind: 'live', pid: 4242 }],
['taken 77\n', { kind: 'taken', pid: 77 }],
- ['live', { kind: 'unsupported' }],
- ['taken x', { kind: 'unsupported' }],
- ['taken 0', { kind: 'unsupported' }],
- ['held 5', { kind: 'unsupported' }],
- ['reclaimed\nheld', { kind: 'unsupported' }],
- ['usage: posix.sh [--branch B]', { kind: 'unsupported' }],
- ['', { kind: 'unsupported' }]
+ ['live', { kind: 'error' }],
+ ['taken x', { kind: 'error' }],
+ ['taken 0', { kind: 'error' }],
+ ['held 5', { kind: 'error' }],
+ ['reclaimed\nheld', { kind: 'error' }],
+ ['usage: posix.sh [--branch B]', { kind: 'error' }],
+ ['', { kind: 'error' }]
])('verdict line %j', (stdout, expected) => {
assert.deepEqual(parseMarkerHelperVerdict(stdout), expected)
})
@@ -104,21 +104,21 @@ describe.skipIf(process.platform === 'win32')('runMarkerHelper against a real sc
})
test.each([
- ['usage exit 64 (old checkout rejects --marker-op)', '64', 'reclaimed'],
+ ['protocol-2 helper exits 64', '64', 'reclaimed'],
['nonzero exit even with a verdict', '1', 'reclaimed'],
['garbage output', '0', 'sure, done'],
['two lines', '0', 'reclaimed\nheld']
- ])('%s => unsupported', async (_label, exit, verdict) => {
+ ])('%s => error', async (_label, exit, verdict) => {
const { root, home } = fakeHelperCheckout()
fs.writeFileSync(path.join(home, 'helper-verdict'), verdict)
fs.writeFileSync(path.join(home, 'helper-exit'), exit)
assert.deepEqual(await runMarkerHelper('reclaim', { updateRoot: root, hermesHome: home, isWindows: false }), {
- kind: 'unsupported'
+ kind: 'error'
})
})
- test('a helper past its timeout is killed and unsupported', async () => {
+ test('a helper past its timeout is killed and reports error', async () => {
const { root, home } = fakeHelperCheckout()
fs.writeFileSync(path.join(home, 'helper-verdict'), 'reclaimed')
fs.writeFileSync(path.join(home, 'helper-sleep'), '5')
@@ -126,7 +126,7 @@ describe.skipIf(process.platform === 'win32')('runMarkerHelper against a real sc
assert.deepEqual(
await runMarkerHelper('reclaim', { updateRoot: root, hermesHome: home, isWindows: false, timeoutMs: 300 }),
- { kind: 'unsupported' }
+ { kind: 'error' }
)
assert.ok(Date.now() - started < 4000, 'bounded by the timeout')
})
diff --git a/apps/desktop/electron/updater/marker-helper.ts b/apps/desktop/electron/updater/marker-helper.ts
index 35813200e2e..2f9ba42dc59 100644
--- a/apps/desktop/electron/updater/marker-helper.ts
+++ b/apps/desktop/electron/updater/marker-helper.ts
@@ -10,8 +10,9 @@
* -MarkerOp <op> -InstallRoot <root> [-DesktopPid P] [-HandoffRun R]
*
* One stdout line: `absent | reclaimed | held | busy | live <pid> | withdrawn |
- * taken <pid> | foreign`. Anything else, a nonzero exit, a timeout or a
- * missing script is `unsupported` (an older checkout).
+ * taken <pid> | foreign`. Anything else, a nonzero exit, a timeout or
+ * malformed output is `error` (retry without clearance). Only a missing or
+ * pre-protocol-2 script is `unsupported` (an older checkout).
*/
import { type ChildProcess, spawn } from 'node:child_process'
@@ -23,7 +24,7 @@ import { resolveUpdateScriptHandoff } from '../updater-process'
export type MarkerHelperOp = 'reclaim' | 'withdraw'
export type MarkerHelperVerdict =
- | { kind: 'absent' | 'reclaimed' | 'held' | 'busy' | 'withdrawn' | 'foreign' | 'unsupported' }
+ | { kind: 'absent' | 'reclaimed' | 'held' | 'busy' | 'withdrawn' | 'foreign' | 'unsupported' | 'error' }
| { kind: 'live' | 'taken'; pid: number }
export const MARKER_HELPER_TIMEOUT_MS = 20_000
@@ -135,22 +136,22 @@ const defaultSpawn: HelperSpawn = (command, args, { env, timeout, windowsHide })
const BARE_VERDICTS = new Set(['absent', 'reclaimed', 'held', 'busy', 'withdrawn', 'foreign'])
-/** Parse the helper's one verdict line; anything else is `unsupported`. */
+/** Parse the helper's one verdict line; anything else is an operational error. */
export function parseMarkerHelperVerdict(stdout: string): MarkerHelperVerdict {
const lines = stdout.split(/\r?\n/).map(line => line.trim()).filter(Boolean)
if (lines.length !== 1) {
- return { kind: 'unsupported' }
+ return { kind: 'error' }
}
const [word, arg, ...extra] = lines[0].split(/\s+/)
if (extra.length) {
- return { kind: 'unsupported' }
+ return { kind: 'error' }
}
if (arg === undefined) {
- return BARE_VERDICTS.has(word) ? ({ kind: word } as MarkerHelperVerdict) : { kind: 'unsupported' }
+ return BARE_VERDICTS.has(word) ? ({ kind: word } as MarkerHelperVerdict) : { kind: 'error' }
}
const pid = /^\d+$/.test(arg) ? Number(arg) : NaN
@@ -159,7 +160,7 @@ export function parseMarkerHelperVerdict(stdout: string): MarkerHelperVerdict {
return { kind: word, pid }
}
- return { kind: 'unsupported' }
+ return { kind: 'error' }
}
/** The exact command line for one helper op (exported for tests). */
@@ -208,7 +209,7 @@ export function markerHelperCommand(
export async function runMarkerHelper(op: MarkerHelperOp, options: MarkerHelperOptions): Promise<MarkerHelperVerdict> {
const scriptPath = handoffScriptPath(options.updateRoot, options.isWindows)
- if (!scriptPath) {
+ if (!scriptPath || readHandoffProtocol(scriptPath) < 2) {
return { kind: 'unsupported' }
}
@@ -221,8 +222,8 @@ export async function runMarkerHelper(op: MarkerHelperOp, options: MarkerHelperO
windowsHide: true
})
- return result.code === 0 ? parseMarkerHelperVerdict(result.stdout) : { kind: 'unsupported' }
+ return result.code === 0 ? parseMarkerHelperVerdict(result.stdout) : { kind: 'error' }
} catch {
- return { kind: 'unsupported' }
+ return { kind: 'error' }
}
}