Skip to content

The agent that grows with you

fix(desktop): respawn after stale backend exit loses owner

joaomarcos27c1f491980e
diff --git a/apps/desktop/electron/backend-stale-exit-recovery.test.ts b/apps/desktop/electron/backend-stale-exit-recovery.test.ts
new file mode 100644
index 00000000000..05798042d16
--- /dev/null
+++ b/apps/desktop/electron/backend-stale-exit-recovery.test.ts
@@ -0,0 +1,27 @@
+import assert from 'node:assert/strict'
+import { test } from 'vitest'
+
+import { claimStaleBackendExitRecovery } from './backend-stale-exit-recovery'
+
+test('current owner exit is eligible for recovery', () => {
+ assert.equal(claimStaleBackendExitRecovery({ hasCurrentProcess: false, hasPendingStart: false, intentionalTeardown: false, recoveryClaimed: false }), true)
+})
+
+test('stale exit with a new owner is ignored', () => {
+ assert.equal(claimStaleBackendExitRecovery({ hasCurrentProcess: true, hasPendingStart: false, intentionalTeardown: false, recoveryClaimed: false }), false)
+})
+
+test('stale exit with an empty state recovers once', () => {
+ const state = { hasCurrentProcess: false, hasPendingStart: false, intentionalTeardown: false, recoveryClaimed: false }
+ assert.equal(claimStaleBackendExitRecovery(state), true)
+ assert.equal(claimStaleBackendExitRecovery(state), false)
+})
+
+test('stale exit during intentional teardown does not recover', () => {
+ assert.equal(claimStaleBackendExitRecovery({ hasCurrentProcess: false, hasPendingStart: false, intentionalTeardown: true, recoveryClaimed: false }), false)
+})
+
+test('double stale exits coalesce to one recovery', () => {
+ const state = { hasCurrentProcess: false, hasPendingStart: false, intentionalTeardown: false, recoveryClaimed: false }
+ assert.equal([claimStaleBackendExitRecovery(state), claimStaleBackendExitRecovery(state)].filter(Boolean).length, 1)
+})
diff --git a/apps/desktop/electron/backend-stale-exit-recovery.ts b/apps/desktop/electron/backend-stale-exit-recovery.ts
new file mode 100644
index 00000000000..de38ee2b257
--- /dev/null
+++ b/apps/desktop/electron/backend-stale-exit-recovery.ts
@@ -0,0 +1,12 @@
+export type StaleExitRecoveryState = {
+ hasCurrentProcess: boolean
+ hasPendingStart: boolean
+ intentionalTeardown: boolean
+ recoveryClaimed: boolean
+}
+
+export function claimStaleBackendExitRecovery(state: StaleExitRecoveryState): boolean {
+ if (state.hasCurrentProcess || state.hasPendingStart || state.intentionalTeardown || state.recoveryClaimed) return false
+ state.recoveryClaimed = true
+ return true
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 35b5a132e14..d3b8a53e882 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -30,6 +30,7 @@ import {
} from 'electron'
import { classifyActiveRuntime } from './active-runtime-state'
+import { claimStaleBackendExitRecovery } from './backend-stale-exit-recovery'
import {
destroyKeepaliveAgents,
downloadAgentFor,
@@ -1467,6 +1468,7 @@ const backendDialClaims = new BackendDialClaims()
// True while connection-config:apply soft-rehomes the primary — suppresses the
// backend-exit toast so an intentional kill doesn't look like a crash.
let softRehomeInProgress = false
+let staleExitRecoveryClaimed = false
// Additional per-profile backends, keyed by profile name. The PRIMARY backend
// (the desktop's launch profile) stays managed by backendConnectionState +
// startHermes(); this pool only holds EXTRA profile
@@ -12950,6 +12952,7 @@ async function prepareProfileRenameRequest(request) {
}
function startHermes() {
+ staleExitRecoveryClaimed = false
return localBackendLifecycle.start(runHermesStart)
}
@@ -13235,6 +13238,16 @@ async function runHermesStart() {
if (!backendConnectionState.clearForCurrentProcess(processOwner)) {
rememberLog(`Ignoring stale Hermes backend error: ${error.message}`)
+ if (claimStaleBackendExitRecovery({
+ hasCurrentProcess: backendConnectionState.getProcess() !== null,
+ hasPendingStart: backendConnectionState.getPendingPromise() !== null,
+ intentionalTeardown: softRehomeInProgress || isQuittingForHandoff,
+ recoveryClaimed: staleExitRecoveryClaimed
+ })) {
+ staleExitRecoveryClaimed = true
+ sendBackendExit({ code: null, signal: null, error: error.message })
+ startHermes().catch(() => {})
+ }
rejectBackendStart?.(new Error('Hermes backend start was superseded by a newer connection attempt.'))
return
@@ -13259,6 +13272,17 @@ async function runHermesStart() {
if (!backendConnectionState.clearForCurrentProcess(processOwner)) {
rememberLog(formatBackendExitLine('Ignoring stale Hermes backend exit', code, signal, primaryOutputTail))
+ if (claimStaleBackendExitRecovery({
+ hasCurrentProcess: backendConnectionState.getProcess() !== null,
+ hasPendingStart: backendConnectionState.getPendingPromise() !== null,
+ intentionalTeardown: softRehomeInProgress || isQuittingForHandoff,
+ recoveryClaimed: staleExitRecoveryClaimed
+ })) {
+ staleExitRecoveryClaimed = true
+ sendBackendExit({ code, signal })
+ startHermes().catch(() => {})
+ }
+
if (!backendReady) {
rejectBackendStart?.(new Error('Hermes backend start was superseded by a newer connection attempt.'))
}