Skip to content

The agent that grows with you

fix(desktop): settle managed SSH updates before the local updater handoff

Konstantin Khlopkovf429fef30cf5
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 5653d7ca9b6..d60f8e03968 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -200,6 +200,7 @@ import {
updateEligibility,
upsertConnection
} from './connection-registry'
+import type { RegistryConnection } from './connection-registry'
import type { RosterProfileMetadata } from './connection-registry'
import { liveWindowState, overlayWindowState } from './connection-window-state'
import { describeCrashReason, installCrashForensics } from './crash-forensics'
@@ -556,6 +557,7 @@ import {
} from './translucency'
import { updateGateReason, waitForUpdateClearance } from './update-gate'
import { readLiveUpdateMarker, updateHandoffConflict, writeUpdateMarker } from './update-marker'
+import { updateConnectionsBeforeLocal } from './update-order'
import {
resolveUpdaterMechanism,
type UpdaterApplyResultWire,
@@ -4625,6 +4627,15 @@ async function applyUpdates(): Promise<UpdaterApplyResultWire> {
let handedOff: boolean = false
try {
+ // The local handoff asks the window to exit and the update scripts only
+ // wait so long for that PID — never start that deadline while quit would
+ // still be gated on a managed SSH update or its recovery transaction
+ // (before-quit joins the same operations; the updater must not race them).
+ await waitForManagedUpdateOperations(() => [
+ ...managedConnectionUpdates.values(),
+ ...managedConnectionRecoveries.values()
+ ])
+
const packaged: UpdaterStrategy | null = await resolvePackagedUpdateStrategy()
const strategy: UpdaterStrategy = packaged ?? resolveCheckoutUpdateStrategy()
const result: UpdaterApplyResultWire = await desktopMetrics.trackUpdateApply(packaged, strategy)
@@ -16678,18 +16689,20 @@ ipcMain.handle('hermes:connections:update-all', async (_event, payload) => {
Array.isArray((payload as any)?.excludeIds) ? (payload as any).excludeIds.map((id: unknown) => String(id)) : []
)
- const results = await Promise.all(
- registry.connections
- .filter(connection => !excludeIds.has(connection.id))
- .map(async connection => {
- const base = { connectionId: connection.id, label: connection.label, kind: connection.kind }
- const eligibility = updateEligibility(connection)
+ // Remote entries settle before the local handoff runs: the local updater
+ // waits on the window PID exiting, so a still-running managed SSH update
+ // would eat into (or outlive) that deadline. Order of results is preserved.
+ const results = await updateConnectionsBeforeLocal(
+ registry.connections.filter(connection => !excludeIds.has(connection.id)),
+ async (connection: RegistryConnection) => {
+ const base = { connectionId: connection.id, label: connection.label, kind: connection.kind }
+ const eligibility = updateEligibility(connection)
- if (!eligibility.eligible) {
+ if (!eligibility.eligible) {
return { ...base, ok: false, skipped: true, reason: eligibility.reason }
- }
+ }
- try {
+ try {
if (connection.kind === 'local') {
// The app-managed runtime updates through the same pipeline as the
// Settings → Updates button (marker + venv gate + relaunch flow).
@@ -16723,10 +16736,10 @@ ipcMain.handle('hermes:connections:update-all', async (_event, payload) => {
}
return { ...base, ok: true, detail: body?.message || 'update started' }
- } catch (error: any) {
- return { ...base, ok: false, error: String(error?.message || error) }
- }
- })
+ } catch (error: any) {
+ return { ...base, ok: false, error: String(error?.message || error) }
+ }
+ }
)
return { ok: true, results }
diff --git a/apps/desktop/electron/update-order.test.ts b/apps/desktop/electron/update-order.test.ts
new file mode 100644
index 00000000000..767b1a48b28
--- /dev/null
+++ b/apps/desktop/electron/update-order.test.ts
@@ -0,0 +1,61 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { updateConnectionsBeforeLocal } from './update-order'
+
+test('a local handoff waits for remote updates that outlive its exit deadline', async () => {
+ let finishRemote: () => void = () => {}
+
+ const remote = new Promise<void>(resolve => {
+ finishRemote = resolve
+ })
+
+ const started: string[] = []
+ const connections = [{ kind: 'local' }, { kind: 'ssh' }, { kind: 'remote' }]
+
+ const result = updateConnectionsBeforeLocal(connections, async connection => {
+ started.push(connection.kind)
+
+ if (connection.kind === 'ssh') {
+ await remote
+ }
+
+ return connection.kind
+ })
+
+ await Promise.resolve()
+ await Promise.resolve()
+ const startedBeforeRemoteCompletion = [...started]
+ finishRemote()
+ assert.deepEqual(await result, ['local', 'ssh', 'remote'])
+ assert.deepEqual(startedBeforeRemoteCompletion, ['ssh', 'remote'])
+ assert.deepEqual(started, ['ssh', 'remote', 'local'])
+})
+
+test('an empty selection does nothing and a local-only selection runs once', async () => {
+ let calls = 0
+ const update = async () => ++calls
+ assert.deepEqual(await updateConnectionsBeforeLocal([], update), [])
+ assert.deepEqual(await updateConnectionsBeforeLocal([{ kind: 'local' }], update), [1])
+})
+
+test('remote updates still run concurrently with each other', async () => {
+ let running = 0
+ let maxRunning = 0
+
+ const results = await updateConnectionsBeforeLocal(
+ [{ kind: 'ssh' }, { kind: 'remote' }, { kind: 'ssh' }],
+ async connection => {
+ running += 1
+ maxRunning = Math.max(maxRunning, running)
+ await new Promise(resolve => setTimeout(resolve, 5))
+ running -= 1
+
+ return connection.kind
+ }
+ )
+
+ assert.deepEqual(results, ['ssh', 'remote', 'ssh'])
+ assert.equal(maxRunning, 3)
+})
diff --git a/apps/desktop/electron/update-order.ts b/apps/desktop/electron/update-order.ts
new file mode 100644
index 00000000000..5b68f058582
--- /dev/null
+++ b/apps/desktop/electron/update-order.ts
@@ -0,0 +1,22 @@
+/**
+ * A local update hands control to a detached updater that waits for the
+ * Desktop window to exit, so every remote update transaction must settle
+ * first — otherwise a managed SSH update can outlive the local updater's
+ * exit deadline and abort the whole update.
+ */
+export async function updateConnectionsBeforeLocal<T extends { kind: string }, R>(
+ connections: T[],
+ update: (connection: T) => Promise<R>
+): Promise<R[]> {
+ const results = new Array<R>(connections.length)
+ const indexed = connections.map((connection, index) => ({ connection, index }))
+
+ const run = async ({ connection, index }: (typeof indexed)[number]) => {
+ results[index] = await update(connection)
+ }
+
+ await Promise.all(indexed.filter(({ connection }) => connection.kind !== 'local').map(run))
+ await Promise.all(indexed.filter(({ connection }) => connection.kind === 'local').map(run))
+
+ return results
+}