Skip to content

The agent that grows with you

fix(desktop): re-arm failed pre-ready recovery

JoaoMarcos4486d18d6f15c2
diff --git a/apps/desktop/electron/backend-exit-recovery.ts b/apps/desktop/electron/backend-exit-recovery.ts
index e7a5223ae7f..a97a217e16a 100644
--- a/apps/desktop/electron/backend-exit-recovery.ts
+++ b/apps/desktop/electron/backend-exit-recovery.ts
@@ -39,6 +39,28 @@ export function createBackendExitRecoveryLatch({
let respawnedAt: number[] = []
let crashLooping = false
+ const blocked = (state: BackendExitRecoveryState) =>
+ state.hasCurrentOwner || state.hasPendingStart || state.intentionalTeardown
+
+ const claim = (state: BackendExitRecoveryState): boolean => {
+ if (claimed || blocked(state)) {
+ 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
+ }
+
return {
/**
* True exactly once per empty slot; `reset()` when a backend becomes ready
@@ -47,25 +69,24 @@ export function createBackendExitRecoveryLatch({
* 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) {
+ claim,
+ /**
+ * Re-arm only the recovery attempt that already owns this latch and failed
+ * before reaching ready. A concurrent owner/start or intentional teardown
+ * keeps the existing claim intact; its eventual ready transition owns the
+ * normal reset. A real retry consumes the same crash-loop budget as every
+ * other supervisor respawn.
+ */
+ retryAfterFailedStart(state: BackendExitRecoveryState): boolean {
+ if (!claimed || blocked(state)) {
return false
}
- respawnedAt.push(at)
- claimed = true
+ claimed = false
- return true
+ return claim(state)
},
- /** True when the last `claim` was refused because the respawn budget for the window is spent. */
+ /** True when the last claim attempt was refused because the respawn budget for the window is spent. */
isCrashLooping(): boolean {
return crashLooping
},