The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep backend gate closed through update handoff
Screminpal3b7549d26c0a
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 1e901fef43e..497609105c3 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -2411,16 +2411,20 @@ const UPDATE_WAIT_POLL_MS = 1000const UPDATE_HANDOFF_DWELL_MS = 2500// Gate deps shared by the primary-window boot path and the pool-backend-// spawn path. Consulting BOTH the on-disk marker and the in-process-// updateInFlight flag is load-bearing (#73822): applyUpdates kills its own-// backend BEFORE the Windows venv-blocker scan but only writes the marker-// AFTER it, so a marker-only gate lets the renderer's ~1s reconnect respawn-// a backend inside the update's own critical section — which the scan then-// reports as a blocker, aborting every update attempt.+// spawn path. Consulting the on-disk marker, the in-process updateInFlight+// flag, AND the successful detached hand-off state is load-bearing (#73822):+// applyUpdates kills its own backend BEFORE the Windows venv-blocker scan but+// only writes the marker AFTER it, so a marker-only gate lets the renderer's+// ~1s reconnect respawn a backend inside the update's own critical section —+// which the scan then reports as a blocker, aborting every update attempt.+// The hand-off state closes the later Windows `cmd start` wrapper gap: the+// wrapper exits 0 before the real PowerShell script claims the marker, and+// `finally` clears updateInFlight immediately after the hand-off is accepted.function updateGateDeps() {return {hasLiveMarker: () => Boolean(readLiveUpdateMarker(HERMES_HOME)),- isUpdateInFlight: () => updateInFlight+ isUpdateInFlight: () => updateInFlight,+ isHandoffActive: () => isQuittingForHandoff}}diff --git a/apps/desktop/electron/update-gate.test.ts b/apps/desktop/electron/update-gate.test.tsindex 4a06bf6bdfa..33a1b9dc412 100644--- a/apps/desktop/electron/update-gate.test.ts+++ b/apps/desktop/electron/update-gate.test.ts@@ -7,7 +7,8 @@* it. A marker-only gate therefore let the renderer's reconnect spawn a* fresh backend inside the update's own critical section, which the scan* reported as a blocker — aborting every Desktop update attempt on Windows.- * The gate must consult the in-process updateInFlight flag as well.+ * The gate must consult the in-process updateInFlight flag and the successful+ * detached hand-off state as well.*/import assert from 'node:assert/strict'@@ -16,10 +17,11 @@ import { test } from 'vitest'import { updateGateReason, waitForUpdateClearance } from './update-gate'-function deps(marker: boolean, inFlight: boolean) {+function deps(marker: boolean, inFlight: boolean, handoffActive = false) {return {hasLiveMarker: () => marker,- isUpdateInFlight: () => inFlight+ isUpdateInFlight: () => inFlight,+ isHandoffActive: () => handoffActive}}@@ -43,6 +45,35 @@ test('marker wins as the reported reason when both are set', () => {assert.equal(updateGateReason(deps(true, true)), 'marker')})+test('handoff remains closed after the detached wrapper exits', async () => {+ let handoffActive = true+ let ticks = 0++ const outcome = await waitForUpdateClearance(+ {+ hasLiveMarker: () => false,+ isUpdateInFlight: () => false,+ isHandoffActive: () => handoffActive+ },+ {+ onWaitTick: reason => {+ ticks += 1+ assert.equal(reason, 'handoff')++ if (ticks === 2) {+ handoffActive = false+ }+ },+ pollMs: 1,+ sleep: async () => {},+ timeoutMs: 10_000+ }+ )++ assert.equal(outcome, 'finished')+ assert.equal(ticks, 2)+})+// ---------------------------------------------------------------------------// waitForUpdateClearance// ---------------------------------------------------------------------------@@ -70,7 +101,7 @@ test('parks on the in-flight flag and finishes when it clears', async () => {let ticks = 0const outcome = await waitForUpdateClearance(- { hasLiveMarker: () => false, isUpdateInFlight: () => inFlight },+ { hasLiveMarker: () => false, isUpdateInFlight: () => inFlight, isHandoffActive: () => false },{onWaitTick: reason => {ticks += 1@@ -100,7 +131,7 @@ test('parks across the flag→marker handoff without a gap', async () => {const reasons: string[] = []const outcome = await waitForUpdateClearance(- { hasLiveMarker: () => marker, isUpdateInFlight: () => inFlight },+ { hasLiveMarker: () => marker, isUpdateInFlight: () => inFlight, isHandoffActive: () => false },{onWaitTick: reason => {ticks += 1diff --git a/apps/desktop/electron/update-gate.ts b/apps/desktop/electron/update-gate.tsindex 441cb6b71b5..f7c77c73959 100644--- a/apps/desktop/electron/update-gate.ts+++ b/apps/desktop/electron/update-gate.ts@@ -8,12 +8,14 @@ import { runBackendStartStep } from './backend-start-cancellation'* Pure, dependency-injected gate that parks local backend spawns while an* in-app update is running (#73822, #50238).*- * Two independent signals mean "an update owns the venv right now":+ * Three independent signals mean "an update owns the venv right now":** - the on-disk marker (`HERMES_HOME/.hermes-update-in-progress`), written* by the updater — and by the desktop itself just before hand-off — and* - the in-process `updateInFlight` flag, true for the whole- * `applyUpdates()` critical section.+ * `applyUpdates()` critical section, and+ * - the successful detached hand-off state, which remains true while this+ * Desktop is waiting to quit after the wrapper has handed control away.** The marker alone is NOT enough (#73822): `applyUpdates` kills its own* backend early (`releaseBackendLock`) but only writes the marker AFTER the@@ -27,13 +29,15 @@ import { runBackendStartStep } from './backend-start-cancellation'* waiter could slip through mid-update.*/-export type UpdateGateReason = 'marker' | 'update-in-flight' | null+export type UpdateGateReason = 'marker' | 'update-in-flight' | 'handoff' | nullexport interface UpdateGateDeps {/** True when a live on-disk update marker exists (see update-marker.ts). */hasLiveMarker: () => boolean/** True while this process is inside applyUpdates()' critical section. */isUpdateInFlight: () => boolean+ /** True after a detached updater hand-off is viable and this Desktop will quit. */+ isHandoffActive: () => boolean}/** Why the gate is closed right now, or null when it is open. */@@ -46,6 +50,10 @@ export function updateGateReason(deps: UpdateGateDeps): UpdateGateReason {return 'update-in-flight'}+ if (deps.isHandoffActive()) {+ return 'handoff'+ }+return null}