Skip to content

The agent that grows with you

fix(desktop): bound the supervisor respawn so a crash-looping backend stops cycling

teknium18e5b0e2a1b38
diff --git a/apps/desktop/electron/backend-exit-recovery.test.ts b/apps/desktop/electron/backend-exit-recovery.test.ts
index 696d2f8d094..f7fc379518a 100644
--- a/apps/desktop/electron/backend-exit-recovery.test.ts
+++ b/apps/desktop/electron/backend-exit-recovery.test.ts
@@ -66,3 +66,26 @@ test('a stale exit is not claimed while the slot has an owner, a start is pendin
// Nothing above consumed the latch.
assert.equal(latch.claim(slotState(empty)), true)
})
+
+test('a backend that dies after every ready is respawned at most maxRespawns times per window, then reported as crash-looping', () => {
+ let clock = 1_000
+ const latch = createBackendExitRecoveryLatch({ maxRespawns: 3, windowMs: 120_000, now: () => clock })
+ const empty = { hasCurrentOwner: false, hasPendingStart: false, intentionalTeardown: false }
+
+ // ready -> dies -> respawn, three times within the window.
+ for (let i = 0; i < 3; i++) {
+ assert.equal(latch.claim(empty), true, `respawn ${i + 1}`)
+ assert.equal(latch.isCrashLooping(), false)
+ latch.reset()
+ clock += 5_000
+ }
+
+ // The fourth death inside the window is a crash loop: no respawn.
+ assert.equal(latch.claim(empty), false)
+ assert.equal(latch.isCrashLooping(), true)
+
+ // Once the window has passed the supervisor may try again.
+ clock += 120_000
+ assert.equal(latch.claim(empty), true)
+ assert.equal(latch.isCrashLooping(), false)
+})
diff --git a/apps/desktop/electron/backend-exit-recovery.ts b/apps/desktop/electron/backend-exit-recovery.ts
index fa5177f570d..e7a5223ae7f 100644
--- a/apps/desktop/electron/backend-exit-recovery.ts
+++ b/apps/desktop/electron/backend-exit-recovery.ts
@@ -23,20 +23,52 @@ export type BackendExitRecoveryState = {
intentionalTeardown: boolean
}
-export function createBackendExitRecoveryLatch() {
+export type BackendExitRecoveryOptions = {
+ /** Respawns the supervisor grants per `windowMs` before it stops and lets the user relaunch. */
+ maxRespawns?: number
+ windowMs?: number
+ now?: () => number
+}
+
+export function createBackendExitRecoveryLatch({
+ maxRespawns = 3,
+ windowMs = 120_000,
+ now = Date.now
+}: BackendExitRecoveryOptions = {}) {
let claimed = false
+ let respawnedAt: number[] = []
+ let crashLooping = false
return {
- /** True exactly once per empty slot; `reset()` when a backend becomes ready again. */
+ /**
+ * True exactly once per empty slot; `reset()` when a backend becomes ready
+ * again. A backend that dies again shortly after every ready re-arms the
+ * latch each time, so the grant is also bounded: more than `maxRespawns`
+ * within `windowMs` is a crash loop, and the supervisor stops respawning
+ * (`isCrashLooping()`) instead of cycling child + error toast forever.
+ */
claim(state: BackendExitRecoveryState): boolean {
if (claimed || state.hasCurrentOwner || state.hasPendingStart || state.intentionalTeardown) {
return false
}
+ const at = now()
+ respawnedAt = respawnedAt.filter(t => at - t < windowMs)
+ crashLooping = respawnedAt.length >= maxRespawns
+
+ if (crashLooping) {
+ return false
+ }
+
+ respawnedAt.push(at)
claimed = true
return true
},
+ /** True when the last `claim` was refused because the respawn budget for the window is spent. */
+ isCrashLooping(): boolean {
+ return crashLooping
+ },
reset(): void {
claimed = false
}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index ae84def052e..fc471b7ea09 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -12999,6 +12999,15 @@ function scheduleUnexpectedPrimaryRecovery({ code = null, signal = null, error =
})
if (!claimed) {
+ if (primaryExitRecovery.isCrashLooping()) {
+ const message =
+ 'Hermes backend keeps crashing right after it restarts; not restarting it again. Relaunch Hermes Desktop.'
+ rememberLog(`[supervisor] ${message}`)
+ sendBackendExit({ code, signal, error: message })
+
+ return true
+ }
+
return false
}
diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.md
index 37adf4a1806..e16ad39261b 100644
--- a/website/docs/user-guide/desktop.md
+++ b/website/docs/user-guide/desktop.md
@@ -538,7 +538,7 @@ If a Desktop chat or bot stops responding while the connection still shows **Con
### The local backend stopped in the background
-If the local Hermes backend process exits after it was ready, Desktop restarts it on its own and shows a **Hermes stopped working in the background** notice; the chat reconnects once the replacement is up. `HERMES_HOME/logs/desktop.log` records the exit code together with the backend's last output lines (`Hermes backend exited (1)` followed by `Recent backend output:`), so the reason it died is in the log even when the app recovered by itself. A backend that keeps dying within seconds of every restart points at the backend itself — look at the traceback in that tail.
+If the local Hermes backend process exits after it was ready, Desktop restarts it on its own and shows a **Hermes stopped working in the background** notice; the chat reconnects once the replacement is up. `HERMES_HOME/logs/desktop.log` records the exit code together with the backend's last output lines (`Hermes backend exited (1)` followed by `Recent backend output:`), so the reason it died is in the log even when the app recovered by itself. A backend that keeps dying within seconds of every restart points at the backend itself — look at the traceback in that tail. After three such restarts within two minutes Desktop stops respawning and shows a **keeps crashing** notice instead of cycling; relaunch the app once the cause is fixed.
### Failed turns name the failing layer