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(browser): a driven node is not discarded, and a discarded one says so
Claude Code5b4a883b3046
diff --git a/src/renderer/nodes/browser-discard-policy.test.ts b/src/renderer/nodes/browser-discard-policy.test.tsindex 73b8e3d83..bffc730d4 100644--- a/src/renderer/nodes/browser-discard-policy.test.ts+++ b/src/renderer/nodes/browser-discard-policy.test.ts@@ -28,3 +28,32 @@ describe('browser discard policy', () => {expect(BROWSER_DISCARD_MS).toBe(5 * 60_000)})})++describe('an active lease suppresses the discard', () => {+ const base = { hiddenMs: BROWSER_DISCARD_MS + 1, loading: false, enabled: true, audible: false }++ it('a driven node is not discarded', () => {+ // Today the target of a five-minute agent task is destroyed the moment the user pans away.+ expect(shouldDiscard({ ...base, driven: true })).toBe(false)+ })++ it('driven is the THIRD suppressor, in the same category as the first two', () => {+ // loading and audible mean "in use even though hidden". So does a live lease.+ expect(shouldDiscard({ ...base, loading: true })).toBe(false)+ expect(shouldDiscard({ ...base, audible: true })).toBe(false)+ expect(shouldDiscard({ ...base, driven: true })).toBe(false)+ expect(shouldDiscard(base)).toBe(true)+ })++ it('absent driven behaves exactly as today — a surface that cannot tell says nothing', () => {+ expect(shouldDiscard(base)).toBe(true)+ expect(shouldDiscard({ ...base, driven: false })).toBe(true)+ expect(shouldDiscard({ ...base, driven: undefined })).toBe(true)+ })++ it('suppresses, it does not exempt: a lease is worth nothing once the window has NOT elapsed', () => {+ // The predicate stays a conjunction — `driven` can only ever subtract a discard, never add one.+ expect(shouldDiscard({ ...base, hiddenMs: BROWSER_DISCARD_MS - 1, driven: true })).toBe(false)+ expect(shouldDiscard({ ...base, enabled: false, driven: true })).toBe(false)+ })+})diff --git a/src/renderer/nodes/browser-discard-policy.ts b/src/renderer/nodes/browser-discard-policy.tsindex 12a4b19f3..0a271de46 100644--- a/src/renderer/nodes/browser-discard-policy.ts+++ b/src/renderer/nodes/browser-discard-policy.ts@@ -9,6 +9,15 @@* result or an interstitial would simply be lost).* - **Never discard a page making noise.** Audible = in use, even off-screen (a call, music, a* video the user is listening to). Chrome exempts audible tabs for the same reason.+ * - **Never discard a page an agent is driving.** A live control lease is "in use even though+ * hidden", the same category as loading and audible. Without this, an agent drives a node, the+ * user pans away, and five minutes later the target is destroyed mid-task with no signal: the+ * restore re-navigates to the stored URL, so cookies survive but form state, scroll position and+ * every post-login SPA state do not, and every element reference from the last read is silently+ * stale. The cost is a whole Chromium renderer held resident until the lease ends — bounded by+ * whatever idle timeout the driver enforces, and preferable to a feature that fails five minutes+ * into every long task. The input is optional, so a surface with no notion of a lease (every+ * surface today) behaves exactly as it did before it existed.* - **The setting is re-read when the timer FIRES, not when it was armed** — so a user who* switches the saver off during a hidden stretch is not discarded by an older timer. The* symmetry is deliberately incomplete: switching the saver ON mid-hide does NOT discard that@@ -29,6 +38,14 @@ export function shouldDiscard(i: {enabled: boolean/** Is the page making sound right now? Optional so a surface that cannot tell says nothing. */audible?: boolean+ /**+ * Is an agent driving this page right now (a live control lease)?+ *+ * Optional for the same reason as `audible`: a surface that cannot tell must behave exactly as it+ * did before the input existed. It is a SUPPRESSOR, never an exemption — the caller re-reads it+ * when the timer fires, so a page whose lease ended is discardable on the next pass.+ */+ driven?: boolean}): boolean {- return i.enabled && !i.loading && !i.audible && i.hiddenMs > BROWSER_DISCARD_MS+ return i.enabled && !i.loading && !i.audible && !i.driven && i.hiddenMs > BROWSER_DISCARD_MS}diff --git a/src/renderer/nodes/useDiscardWhenHidden.test.tsx b/src/renderer/nodes/useDiscardWhenHidden.test.tsxindex e24e51e0d..12c012601 100644--- a/src/renderer/nodes/useDiscardWhenHidden.test.tsx+++ b/src/renderer/nodes/useDiscardWhenHidden.test.tsx@@ -42,6 +42,7 @@ function Harness(props: Partial<DiscardWhenHiddenOptions> & { onEl?: (el: HTMLEluseDiscardWhenHidden(ref, {isLoading: props.isLoading ?? (() => false),isAudible: props.isAudible,+ isDriven: props.isDriven,hasContent: props.hasContent ?? (() => true),onDiscard: props.onDiscard ?? (() => {}),onRestore: props.onRestore ?? (() => {})@@ -227,3 +228,60 @@ describe('useDiscardWhenHidden', () => {expect(shouldDiscard({ hiddenMs: BROWSER_DISCARD_MS + DISCARD_SLACK_MS, loading: false, enabled: true })).toBe(true)})})++describe('a control lease is a transient blocker, like a load or a sound', () => {+ let root: Root | null = null++ beforeEach(() => {+ vi.useFakeTimers()+ vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver)+ useSettings.setState({ settings: { ...DEFAULT_SETTINGS } })+ observed = null+ })+ afterEach(() => {+ act(() => root?.unmount())+ root = null+ vi.unstubAllGlobals()+ vi.useRealTimers()+ })++ it('a lease that ENDS leaves the node discardable on the next retry — never exempt forever', () => {+ // hidden → driven → fire (no discard, re-armed) → lease ends → retry fires → discarded.+ const onDiscard = vi.fn()+ let driven = true+ let el!: HTMLElement+ root = mount({ onDiscard, isDriven: () => driven, onEl: (e) => (el = e) })+ hide(el)+ advance(BROWSER_DISCARD_MS + DISCARD_SLACK_MS)+ expect(onDiscard).not.toHaveBeenCalled()+ // Still driving a minute later — still exempt, and still re-armed.+ advance(DISCARD_RETRY_MS)+ expect(onDiscard).not.toHaveBeenCalled()+ driven = false+ advance(DISCARD_RETRY_MS)+ expect(onDiscard).toHaveBeenCalledTimes(1)+ })++ it('is read at FIRE time, not at arm time', () => {+ // A lease taken after the timer was armed must still be honoured — the whole point of reading+ // every input through optsRef when the timer fires.+ const onDiscard = vi.fn()+ let driven = false+ let el!: HTMLElement+ root = mount({ onDiscard, isDriven: () => driven, onEl: (e) => (el = e) })+ hide(el)+ advance(BROWSER_DISCARD_MS - 1000)+ driven = true+ advance(DISCARD_SLACK_MS + 1000)+ expect(onDiscard).not.toHaveBeenCalled()+ })++ it('a surface that passes no isDriven is discarded exactly as before', () => {+ const onDiscard = vi.fn()+ let el!: HTMLElement+ root = mount({ onDiscard, onEl: (e) => (el = e) })+ hide(el)+ advance(BROWSER_DISCARD_MS + DISCARD_SLACK_MS)+ expect(onDiscard).toHaveBeenCalledTimes(1)+ })+})diff --git a/src/renderer/nodes/useDiscardWhenHidden.ts b/src/renderer/nodes/useDiscardWhenHidden.tsindex 7d5c16a2a..fb2b90d50 100644--- a/src/renderer/nodes/useDiscardWhenHidden.ts+++ b/src/renderer/nodes/useDiscardWhenHidden.ts@@ -41,6 +41,15 @@ export interface DiscardWhenHiddenOptions {/** Is the page making sound right now? Omitted = this surface cannot tell (never treated as* audible — a surface with no webview has nothing playing). */isAudible?: () => boolean+ /**+ * Is an agent driving this page right now (a live control lease)?+ *+ * Read AT FIRE TIME like every other input, and treated as TRANSIENT: a lease that has ended+ * leaves the node discardable on the next retry, never exempt for the whole hidden stretch.+ * Omitted = this surface has no notion of a lease, which is every surface until one pushes lease+ * state into the renderer.+ */+ isDriven?: () => boolean/** Is there anything to release? (A start page / empty node has no guest process.) */hasContent: () => boolean/** Release the page: the caller unmounts its `<webview>` and shows its plate. */@@ -113,10 +122,13 @@ export function useDiscardWhenHidden(const enabled = useSettings.getState().settings.browserMemorySaverconst loading = o.isLoading()const audible = o.isAudible?.() ?? false- if (!shouldDiscard({ hiddenMs: Date.now() - hiddenSince, loading, enabled, audible })) {- // Both blockers END BY THEMSELVES, so they earn a retry rather than disarming the saver for- // the whole hidden stretch. A disabled setting does not: see the policy's doc comment.- if (enabled && (loading || audible)) arm(DISCARD_RETRY_MS)+ const driven = o.isDriven?.() ?? false+ if (!shouldDiscard({ hiddenMs: Date.now() - hiddenSince, loading, enabled, audible, driven })) {+ // All three blockers END BY THEMSELVES, so they earn a retry rather than disarming the+ // saver for the whole hidden stretch. A disabled setting does not: see the policy's doc+ // comment. `driven` belongs with the other two and NOT with the setting — an agent that+ // stops driving must not leave the page exempt until its next hide→show cycle.+ if (enabled && (loading || audible || driven)) arm(DISCARD_RETRY_MS)return}discarded = true