The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): auto-reload a live window whose renderer is killed (#122255)
brooklyn!60329f766ffb
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex b3edce31988..b8a1bc6dac4 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -12210,6 +12210,12 @@ function startHermes({ supervisorRecovery = false }: { supervisorRecovery?: boolreturn start}+// A quit or update handoff kills renderers while their windows can still report+// live; the renderer lifecycle must treat that as teardown, not a crash to reload.+function rendererTeardownInProgress(): boolean {+ return isQuittingForHandoff || backendShutdown.hasStarted()+}+function primaryRecoveryState() {return {hasCurrentOwner: backendConnectionState.getProcess() !== null || backendConnectionState.getPromise() !== null,@@ -13050,6 +13056,7 @@ function spawnSecondaryWindow({win.webContents.reload()}},+ isIntentionalTeardown: rendererTeardownInProgress,reloadWindowMs: RENDERER_RELOAD_WINDOW_MS,reloadMax: RENDERER_RELOAD_MAX,recentReloadTimesRef: rendererReloadTimesRef@@ -13131,6 +13138,7 @@ function spawnBrowserWindow(tabId) {win.webContents.reload()}},+ isIntentionalTeardown: rendererTeardownInProgress,reloadWindowMs: RENDERER_RELOAD_WINDOW_MS,reloadMax: RENDERER_RELOAD_MAX,recentReloadTimesRef: rendererReloadTimesRef@@ -13239,6 +13247,7 @@ function createInstanceWindow(win.webContents.reload()}},+ isIntentionalTeardown: rendererTeardownInProgress,reloadWindowMs: RENDERER_RELOAD_WINDOW_MS,reloadMax: RENDERER_RELOAD_MAX,recentReloadTimesRef: rendererReloadTimesRef@@ -14391,7 +14400,7 @@ function createWindow() {wireCommonWindowHandlers(mainWindow, zoomWiringForWindowKind('chat'))// Per-window renderer lifecycle diagnostics + recovery (#81290). The reload- // policy (crashed/oom → bounded reload via the shared rolling budget, then+ // policy (crashed/oom/killed → bounded reload via the shared rolling budget, then// the #38216 Windows sandbox relaunch check on suppression) is the same// policy this window used before it moved into the shared helper, so a// crashed peer renderer now logs and recovers exactly like the primary one.@@ -14457,10 +14466,10 @@ function createWindow() {reloadUrl: DEV_SERVER || pathToFileURL(resolveRendererIndex()).toString()})},- // #116472: the OS/Chromium can SIGKILL a renderer while the window is live (memory- // reclaim, an external kill). Hermes never does this itself and never reloads it- // (a killed-after-close window must not pop back up), so without this the window sat- // silent with only a desktop.log line. Surface the reason + a recovery button instead.+ // #116472: the OS/Chromium can kill a renderer while the window is live (memory+ // reclaim, an external SIGTERM/SIGKILL). The lifecycle reloads that under the shared+ // budget (#85048); once the budget is spent, or for an unrecoverable reason, surface+ // the reason + a recovery button instead of a silent dead window.onRendererTerminated: details => {// An intentional quit/handoff also tears the renderer down; never pop a// recovery page for it (its window may still be alive when this fires).@@ -14480,6 +14489,7 @@ function createWindow() {})}},+ isIntentionalTeardown: rendererTeardownInProgress,reloadWindowMs: RENDERER_RELOAD_WINDOW_MS,reloadMax: RENDERER_RELOAD_MAX,recentReloadTimesRef: rendererReloadTimesRef,diff --git a/apps/desktop/electron/window-renderer-lifecycle.test.ts b/apps/desktop/electron/window-renderer-lifecycle.test.tsindex ac785594f6b..2f7242622e0 100644--- a/apps/desktop/electron/window-renderer-lifecycle.test.ts+++ b/apps/desktop/electron/window-renderer-lifecycle.test.ts@@ -84,28 +84,25 @@ test('pruneReloadTimes drops timestamps outside the rolling window', () => {assert.deepEqual(pruneReloadTimes([], now, 60_000), [])})-test('shouldReloadAfterRendererGone reloads crashed/oom on a live window', () => {+test('shouldReloadAfterRendererGone reloads crashed/oom/killed on a live window', () => {assert.deepEqual(shouldReloadAfterRendererGone({ reason: 'crashed', isDestroyed: false, recentReloadTimes: [] }), {reload: true})assert.deepEqual(shouldReloadAfterRendererGone({ reason: 'oom', isDestroyed: false, recentReloadTimes: [] }), {reload: true})+ assert.deepEqual(shouldReloadAfterRendererGone({ reason: 'killed', isDestroyed: false, recentReloadTimes: [] }), {+ reload: true+ })})test('shouldReloadAfterRendererGone never reloads expected teardown or unknown reasons', () => {// A window the user closed reports reason 'killed' — reloading would pop it- // back up after close.+ // back up after close. isDestroyed gates that case before reason matching.assert.deepEqual(shouldReloadAfterRendererGone({ reason: 'killed', isDestroyed: true, recentReloadTimes: [] }), {reload: false,suppressedReason: 'expected-teardown'})- // Killed on a live window is a process-initiated loss (e.g. OS reclaim);- // the primary window never reloaded it, so peers don't either.- assert.deepEqual(shouldReloadAfterRendererGone({ reason: 'killed', isDestroyed: false, recentReloadTimes: [] }), {- reload: false,- suppressedReason: 'unrecoverable-reason'- })assert.deepEqual(shouldReloadAfterRendererGone({ reason: 'launch-failed', isDestroyed: false, recentReloadTimes: [] }),{@@ -512,11 +509,15 @@ test('ERR_ABORTED does not consume the load-failure budget', async () => {assert.equal(win.reloadCalls.length, 1)})-test('onRendererTerminated surfaces an external kill on a live window, never a reload or teardown', async () => {+test('a live window whose renderer is killed reloads, then ends on the recovery hook once the budget is spent', async () => {+ // #85048: an external SIGTERM reports reason=killed on a still-live window.const terminated: any[] = []const win = makeFakeWindow()const { options } = makeOptions(win, 'main', {+ reloadWindowMs: 60_000,+ reloadMax: 1,+ now: () => 1000,callbacks: {log: () => undefined,reload: () => win.webContents.reload(),@@ -528,34 +529,58 @@ test('onRendererTerminated surfaces an external kill on a live window, never a rinstallWindowRendererLifecycle(win, options)- // Live window killed externally (OS reclaim / SIGKILL) → surfaced, never reloaded.- win.webContents.emit('render-process-gone', {}, { reason: 'killed', exitCode: 9 })+ win.webContents.emit('render-process-gone', {}, { reason: 'killed', exitCode: 15 })await flushDeferred()- assert.equal(win.reloadCalls.length, 0)- assert.deepEqual(terminated, [{ reason: 'killed', exitCode: 9 }])+ assert.equal(win.reloadCalls.length, 1)+ assert.equal(terminated.length, 0)- // A recoverable crash reloads and must NOT surface the termination page.- win.webContents.emit('render-process-gone', {}, { reason: 'crashed', exitCode: 3 })+ // Killed again inside the window: no reload loop, and not a silent dead window.+ win.webContents.emit('render-process-gone', {}, { reason: 'killed', exitCode: 15 })await flushDeferred()assert.equal(win.reloadCalls.length, 1)- assert.equal(terminated.length, 1)+ assert.deepEqual(terminated, [{ reason: 'killed', exitCode: 15 }])- // Expected teardown (window destroyed) is neither reloaded nor surfaced.- const closing = makeFakeWindow({ destroyed: true })+ // An unrecoverable reason is surfaced without a reload.+ win.webContents.emit('render-process-gone', {}, { reason: 'launch-failed', exitCode: 7 })+ await flushDeferred()+ assert.equal(win.reloadCalls.length, 1)+ assert.equal(terminated.length, 2)+})- const closingOptions = makeOptions(closing, 'main', {- callbacks: {- log: () => undefined,- reload: () => closing.webContents.reload(),- onRendererTerminated: (details: any) => {- terminated.push(details)+test('a killed renderer is never reloaded or surfaced after close or during an intentional quit', async () => {+ const terminated: any[] = []+ let quitting = false++ const makeWindow = (destroyed: boolean) => {+ const win = makeFakeWindow({ destroyed })++ const { options } = makeOptions(win, 'main', {+ isIntentionalTeardown: () => quitting,+ callbacks: {+ log: () => undefined,+ reload: () => win.webContents.reload(),+ onRendererTerminated: (details: any) => {+ terminated.push(details)+ }}- }- })+ })- installWindowRendererLifecycle(closing, closingOptions.options)- closing.webContents.emit('render-process-gone', {}, { reason: 'killed', exitCode: 9 })+ installWindowRendererLifecycle(win, options)++ return win+ }++ // Closed window: killed is expected teardown.+ const closed = makeWindow(true)+ closed.webContents.emit('render-process-gone', {}, { reason: 'killed', exitCode: 9 })++ // Quit / update handoff: the window can still report live while its renderer dies.+ quitting = true+ const live = makeWindow(false)+ live.webContents.emit('render-process-gone', {}, { reason: 'killed', exitCode: 9 })await flushDeferred()- assert.equal(closing.reloadCalls.length, 0)- assert.equal(terminated.length, 1)++ assert.equal(closed.reloadCalls.length, 0)+ assert.equal(live.reloadCalls.length, 0)+ assert.equal(terminated.length, 0)})diff --git a/apps/desktop/electron/window-renderer-lifecycle.ts b/apps/desktop/electron/window-renderer-lifecycle.tsindex 50d82371d1b..7f7e8110906 100644--- a/apps/desktop/electron/window-renderer-lifecycle.ts+++ b/apps/desktop/electron/window-renderer-lifecycle.ts@@ -10,12 +10,15 @@// and unit-testable (mirroring windows-sandbox-fallback.ts / session-windows.ts).//// Policy (matches the primary window's previous behavior, generalized):-// - `render-process-gone` with reason `crashed`/`oom` → bounded reload (rolling-// crash-loop guard shared across ALL windows — one budget per process).-// - `render-process-gone` with any other reason (`killed`, `launch-failed`,-// `clean-exit`, unknown) → log only. `killed` after an expected close/destroy-// is normal teardown, and blindly reloading it would loop windows back up-// after the user closed them.+// - `render-process-gone` with reason `crashed`/`oom`/`killed` on a live window+// → bounded reload (rolling crash-loop guard shared across ALL windows).+// Live-window `killed` covers external SIGTERM / OOM watchdogs; user close+// still short-circuits via `isDestroyed`, and an app quit/update handoff via+// `isIntentionalTeardown` (expected teardown). Once the budget is spent, a+// live window hands off to `onRendererTerminated` so it ends on a visible+// recovery affordance rather than a dead window.+// - `render-process-gone` with other reasons (`launch-failed`, `clean-exit`,+// unknown) → log only.// - `unresponsive` → log only (no reload; Chromium usually follows with// render-process-gone, and forcing a reload while the main thread is wedged// can make things worse).@@ -91,13 +94,16 @@ export interface WindowRendererLifecycleOptions {* (`reloadOnFailedLoad`): the window would otherwise stay blank, so the* caller should load a visible error page in its place. */onFailedLoadBudgetExhausted?: (details?: FailedLoadDetails) => void- /** Called when a LIVE window's renderer is terminated by something other than a- * recoverable crash or an expected teardown (e.g. an external SIGKILL / OS- * reclaim). Deliberately never reloaded — reloading a killed-after-close window- * would pop it back up — so this is the caller's chance to surface WHY instead- * of a silent loss (#116472). */+ /** Called when a LIVE window's renderer is gone and will not be reloaded:+ * an unrecoverable reason (`launch-failed`, unknown), or a recoverable one+ * (`crashed`/`oom`/`killed`) after the crash-loop budget is spent. This is+ * the caller's chance to surface WHY instead of a silent loss (#116472). */onRendererTerminated?: (details?: RendererLifecycleDetails) => void}+ /** True while the app is deliberately tearing windows down (quit, update+ * handoff). A renderer killed then is expected teardown even though its+ * window may still report live, so it is never reloaded or surfaced. */+ isIntentionalTeardown?: () => boolean/** Rolling crash-loop window, ms. Defaults to 60_000 (RENDERER_RELOAD_WINDOW_MS). */reloadWindowMs?: number/** Max reloads per rolling window. Defaults to 3 (RENDERER_RELOAD_MAX). */@@ -124,7 +130,7 @@ export interface LifecycleWindowLike {const DEFAULT_RELOAD_WINDOW_MS = 60_000const DEFAULT_RELOAD_MAX = 3-const RECOVERABLE_REASONS = new Set(['crashed', 'oom'])+const RECOVERABLE_REASONS = new Set(['crashed', 'oom', 'killed'])function safeNow(now: (() => number) | undefined): number {return typeof now === 'function' ? now() : Date.now()@@ -149,10 +155,10 @@ export function pushReloadTime(times: number[], now: number): number[] {/*** Decide whether a render-process-gone event should reload its window.*- * Reload only for `crashed`/`oom` on a live window, bounded by the shared- * rolling crash-loop budget. Anything else — expected teardown (`killed` after- * close/destroy), unrecoverable reasons, unknown reasons — is log-only, exactly- * like the primary window's previous behavior but now per window kind.+ * Reload for `crashed`/`oom`/`killed` on a live window, bounded by the shared+ * rolling crash-loop budget. Expected teardown still short-circuits first via+ * `isDestroyed` (user close reports `killed` with a destroyed window). Other+ * unrecoverable/unknown reasons remain log-only.*/export function shouldReloadAfterRendererGone(details: {reason?: string@@ -279,7 +285,7 @@ export function installWindowRendererLifecycle(const contents = win.webContentsconst onRendererGone = (_event: unknown, details?: RendererLifecycleDetails) => {- const destroyed = win.isDestroyed()+ const destroyed = win.isDestroyed() || options.isIntentionalTeardown?.() === truelog(describeRendererLifecycleEvent({ kind, event: 'render-process-gone', ...details, isDestroyed: destroyed }))@@ -304,13 +310,15 @@ export function installWindowRendererLifecycle(`[renderer:${kind}] suppressing reload: ${budgetRef.current.length} crashes within ${reloadWindowMs}ms (likely a crash loop)`)onCrashLoopSuppressed?.(details)+ // The budget is spent and the window is still live: end on the caller's+ // recovery affordance, not a dead window (#85048).+ onRendererTerminated?.(details)return}- // A live window whose renderer was lost to something other than a recoverable- // crash (e.g. an external SIGKILL / OS reclaim). We deliberately do not reload,- // but the loss must not be silent — let the caller surface WHY (#116472).+ // A live window whose renderer was lost to an unrecoverable reason (e.g.+ // launch-failed). The loss must not be silent — let the caller surface WHY (#116472).if (decision.suppressedReason === 'unrecoverable-reason' && !destroyed) {log(`[renderer:${kind}] renderer terminated while live (reason=${String(details?.reason || 'unknown')}` +@@ -330,7 +338,7 @@ export function installWindowRendererLifecycle(// Deferred: never reload from inside the event handler (see above).setImmediate(() => {- if (win.isDestroyed()) {+ if (win.isDestroyed() || options.isIntentionalTeardown?.() === true) {return}@@ -412,7 +420,7 @@ export function installWindowRendererLifecycle(// Deferred: never reload from inside the event handler (see above).setImmediate(() => {- if (win.isDestroyed()) {+ if (win.isDestroyed() || options.isIntentionalTeardown?.() === true) {return}