Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(renderer): never defer a viewer release for a node that cannot hibernate yet
enes0f8e6c57adc2
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsxindex 8a775637f..e0cacb51e 100644--- a/src/renderer/nodes/TerminalNode.tsx+++ b/src/renderer/nodes/TerminalNode.tsx@@ -65,6 +65,7 @@ import {offscreenCoreIsRemote,offscreenDisposeMs,planOffscreenVisibility,+ releaseStillEnabled,shouldDeferReleaseForEco,OFFSCREEN_DEFER_RETRY_MS,OFFSCREEN_DISPOSE_MS_DEFAULT@@ -3162,7 +3163,7 @@ export function TerminalNode({// The setting can also have been switched OFF while this timer counted down; a fire// that disposed anyway would take a buffer the user has just asked us to keep.const liveSettings = useSettings.getState().settings- if (offscreenDisposeMs(liveSettings.offscreenTerminalMinutes) === null) return+ if (!releaseStillEnabled(liveSettings.offscreenTerminalMinutes)) return// Nothing to give back (no session yet, or one that was closed/ended under us) ⇒ no// dispose. A null ref means no lifecycle run is live at all, which answers the same way.if (!offscreenLiveRef.current?.()) return@@ -3174,11 +3175,17 @@ export function TerminalNode({// ~15 MB; the CLI is hundreds. So wait for the big prize, on the sweep's own cadence,// and only up to the capped total — a node that can never hibernate must not hold its// viewer forever.+ const ecoStatus = useAgentStatus.getState().byId[id]if (shouldDeferReleaseForEco({ecoEnabled: liveSettings.agentHibernationEnabled,resumableAgent: hibernationTargetRef.current,- hibernated: !!useAgentStatus.getState().byId[id]?.hibernated,+ hibernated: !!ecoStatus?.hibernated,+ // Same "unknown idle is not idle" rule the policy applies: with no hook event seen+ // in this run there is no idle clock, `planHibernation` refuses this node outright,+ // and waiting for a hibernation that cannot come would hold every warm node's+ // viewer for the full cap after each app restart.+ idleKnown: ecoStatus?.lastEventAt !== undefined,offscreenElapsedMs: Date.now() - (offscreenSinceRef.current ?? Date.now()),idleMinutes: liveSettings.agentHibernationIdleMinutes,offscreenMinutes:diff --git a/src/renderer/terminal/offscreen-policy.test.ts b/src/renderer/terminal/offscreen-policy.test.tsindex 0836836d0..e95e9e7a4 100644--- a/src/renderer/terminal/offscreen-policy.test.ts+++ b/src/renderer/terminal/offscreen-policy.test.ts@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest'import {OFFSCREEN_DISPOSE_MS_DEFAULT,offscreenDisposeMs,+ releaseStillEnabled,mayDisposeOffscreen,offscreenCoreIsRemote,planOffscreenVisibility,@@ -107,6 +108,7 @@ describe('shouldDeferReleaseForEco — hibernate first, then release the viewer'ecoEnabled: true,resumableAgent: true,hibernated: false,+ idleKnown: true,offscreenElapsedMs: 10 * 60_000,idleMinutes: 30,offscreenMinutes: 10@@ -125,6 +127,15 @@ describe('shouldDeferReleaseForEco — hibernate first, then release the viewer'expect(shouldDeferReleaseForEco({ ...base, resumableAgent: false })).toBe(false)})+ it('never defers while the idle clock is UNKNOWN — the policy refuses those nodes anyway', () => {+ // `lastEventAt` is transient by design, so after every app restart a warm agent node has no+ // clock until it takes a turn — and `planHibernation`'s "unknown idle is not idle" rule+ // refuses it. Deferring for it would hold every warm node's viewer for the whole cap, i.e.+ // switching Eco ON would mean a memory REGRESSION for the first stretch of each session.+ expect(shouldDeferReleaseForEco({ ...base, idleKnown: false })).toBe(false)+ expect(shouldDeferReleaseForEco({ ...base, idleKnown: true })).toBe(true)+ })+it('never defers on an unusable idle window (planHibernation refuses those outright)', () => {for (const idleMinutes of [0, -5, NaN])expect(shouldDeferReleaseForEco({ ...base, idleMinutes }), String(idleMinutes)).toBe(false)@@ -138,3 +149,15 @@ describe('shouldDeferReleaseForEco — hibernate first, then release the viewer'expect(shouldDeferReleaseForEco({ ...base, offscreenElapsedMs: 90 * 60_000 })).toBe(false)})})++describe('releaseStillEnabled — the fire-time re-ask of the setting itself', () => {+ it('refuses a release whose feature was switched off while the timer counted down', () => {+ // A timer armed ten minutes ago outlives the setting that armed it, and the Eco deferral makes+ // that window longer still. Disposing anyway would take the buffer the user just asked us to+ // keep — indistinguishable, from the outside, from the feature being broken.+ expect(releaseStillEnabled(10)).toBe(true)+ expect(releaseStillEnabled(undefined)).toBe(true) // unset = the default window, still on+ expect(releaseStillEnabled(0)).toBe(false)+ expect(releaseStillEnabled(-5)).toBe(false)+ })+})diff --git a/src/renderer/terminal/offscreen-policy.ts b/src/renderer/terminal/offscreen-policy.tsindex 842115353..d0c142be4 100644--- a/src/renderer/terminal/offscreen-policy.ts+++ b/src/renderer/terminal/offscreen-policy.ts@@ -43,6 +43,17 @@ export function mayDisposeOffscreen(i: {return !i.visible && !i.remote && !i.selected}+/**+ * Is the offscreen release still switched ON — asked at FIRE time, not only when the timer was+ * armed. A timer armed ten minutes ago outlives the setting that armed it: the user can turn the+ * feature off (or to 0) while it counts down, and a fire that disposed anyway would take the very+ * buffer they just asked us to keep, with no way to tell the difference from a bug. The deferral+ * below makes this window longer still, which is what turned a latent case into a real one.+ */+export function releaseStillEnabled(settingMinutes: number | undefined): boolean {+ return offscreenDisposeMs(settingMinutes) !== null+}+/** How often a DEFERRED release re-asks. The hibernation sweep's own cadence, because that is* exactly what the deferral is waiting for: one sweep after the node hibernates, the viewer goes. */export const OFFSCREEN_DEFER_RETRY_MS = 60_000@@ -71,6 +82,14 @@ export const OFFSCREEN_DEFER_RETRY_MS = 60_000* (a working node goes done, a session id arrives late), and asking for it here would re-implement* `planHibernation` in a second place. The question asked is the durable one — could this node's* CLI ever be quit and resumed at all — and the cap covers the rest.+ *+ * ONE exception to that, and it is not a moving target at all: `idleKnown`. The policy's "unknown+ * idle is NOT idle" rule refuses any candidate with no `lastEventAt`, and that clock is TRANSIENT+ * by design — a relaunch has seen no hook events yet. So after every app restart, every warm+ * agent node is one hibernation can provably deliver nothing for until it takes a turn, and+ * deferring for them would hold every viewer for the whole 40-minute cap: Eco switched on would+ * MEAN a memory regression for the first stretch of each session. Waiting is only justified when+ * the thing waited for can actually happen.*/export function shouldDeferReleaseForEco(i: {ecoEnabled: boolean@@ -78,12 +97,18 @@ export function shouldDeferReleaseForEco(i: {resumableAgent: boolean/** Already hibernated — the CLI is gone, so the release is now a pure win and must proceed. */hibernated: boolean+ /**+ * Has ANY hook event been seen for this node in this app run (`lastEventAt` is set)? Without+ * one, `planHibernation` refuses the node outright — unknown idle is not idle — so there is+ * nothing to wait for. See the header.+ */+ idleKnown: boolean/** How long this node has been continuously out of view. */offscreenElapsedMs: numberidleMinutes: numberoffscreenMinutes: number}): boolean {- if (!i.ecoEnabled || !i.resumableAgent || i.hibernated) return false+ if (!i.ecoEnabled || !i.resumableAgent || i.hibernated || !i.idleKnown) return false// An unusable window means Eco is off in practice (`planHibernation` refuses a non-positive one),// so there is nothing to wait for — release on the ordinary schedule.if (!(i.idleMinutes > 0)) return false