Skip to content

The agent that grows with you

fix(desktop): exclude process-less descriptors from backend pool LRU cap

Teknium9e062912b9c3
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 2776d84510a..0b2017a42c5 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -108,6 +108,7 @@ import {
shouldRemoveAppBundle,
uninstallArgsForMode
} from './desktop-uninstall'
+import { selectPoolEvictions } from './pool-eviction'
import { describeDevCdpDecision, resolveDevCdpPort } from './dev-cdp'
import { installEmbedReferer } from './embed-referer'
import { createEventDeduper } from './event-dedupe'
@@ -9420,31 +9421,22 @@ function touchPoolBackend(profile) {
}
}
-// Evict least-recently-used pool backends until at most `keep` remain — but only
-// ever evict backends without a live renderer socket (stale beyond the keepalive
-// window). When every backend is actively kept alive we let the pool exceed the
-// soft cap rather than kill a running session.
+// Evict least-recently-used SPAWNED pool backends until at most `keep` remain —
+// but only ever evict backends without a live renderer socket (stale beyond the
+// keepalive window). When every backend is actively kept alive we let the pool
+// exceed the soft cap rather than kill a running session. Process-less
+// descriptor entries (remote/cloud registry sources, per-profile remote
+// overrides — `entry.process === null`) are excluded from the cap entirely:
+// they hold no local process, so counting them used to let a roster refresh
+// across N registered remote connections LRU-evict a REAL local backend that
+// was merely idle past the keepalive window. Descriptors are still reclaimed
+// by the idle reaper.
function evictLruPoolBackends(keep) {
- if (backendPool.size <= keep) {
- return
- }
-
- const now = Date.now()
-
- const evictable = [...backendPool.entries()]
- .filter(([, entry]) => now - (entry.lastActiveAt || 0) > POOL_KEEPALIVE_FRESH_MS)
- .sort((a, b) => (a[1].lastActiveAt || 0) - (b[1].lastActiveAt || 0))
-
- let removable = backendPool.size - Math.max(0, keep)
-
- for (const [profile] of evictable) {
- if (removable <= 0) {
- break
- }
+ const evictions = selectPoolEvictions(backendPool.entries(), Math.max(0, keep), Date.now(), POOL_KEEPALIVE_FRESH_MS)
+ for (const profile of evictions) {
rememberLog(`Evicting idle profile backend "${profile}" (LRU cap ${POOL_MAX_BACKENDS})`)
stopPoolBackend(profile)
- removable -= 1
}
}
diff --git a/apps/desktop/electron/pool-eviction.test.ts b/apps/desktop/electron/pool-eviction.test.ts
new file mode 100644
index 00000000000..cb44e8d285c
--- /dev/null
+++ b/apps/desktop/electron/pool-eviction.test.ts
@@ -0,0 +1,85 @@
+/**
+ * Tests for electron/pool-eviction.ts — LRU cap accounting for the desktop
+ * backend pool. The cap exists to bound SPAWNED local backends (real child
+ * processes); process-less descriptor entries (remote/cloud registry sources,
+ * per-profile remote overrides) must not count against it, or a roster
+ * refresh across N registered remote connections evicts a real local backend
+ * that was merely idle past the keepalive window.
+ */
+
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { selectPoolEvictions } from './pool-eviction'
+
+const NOW = 1_000_000
+const FRESH_MS = 90_000
+
+/** A spawned local backend entry (has a child process). */
+const spawned = (idleMs: number) => ({ process: { pid: 123 }, lastActiveAt: NOW - idleMs })
+
+/** A process-less remote/cloud descriptor entry. */
+const descriptor = (idleMs: number) => ({ process: null, lastActiveAt: NOW - idleMs })
+
+test('process-less descriptors do not count toward the cap', () => {
+ // 1 real spawned backend idle beyond the keepalive window + 3 remote
+ // descriptors: total size (4) exceeds keep (2), but only ONE entry holds a
+ // process, so nothing may be evicted. This is the roster-refresh regression:
+ // the old size-based accounting evicted the real local backend here.
+ const entries: [string, ReturnType<typeof spawned>][] = [
+ ['default', spawned(120_000)],
+ ['conn:homelab::default', descriptor(0)],
+ ['conn:office::default', descriptor(0)],
+ ['conn:cloud-a::default', descriptor(0)]
+ ]
+
+ assert.deepEqual(selectPoolEvictions(entries, 2, NOW, FRESH_MS), [])
+})
+
+test('spawned backends over the cap are still LRU-evicted', () => {
+ const entries: [string, ReturnType<typeof spawned>][] = [
+ ['a', spawned(500_000)],
+ ['b', spawned(300_000)],
+ ['c', spawned(100_000)],
+ // Descriptors interleaved: must neither inflate the count nor be evicted.
+ ['conn:x::a', descriptor(999_000)]
+ ]
+
+ // keep=2 → one spawned backend over; evict the least-recently-used ('a').
+ assert.deepEqual(selectPoolEvictions(entries, 2, NOW, FRESH_MS), ['a'])
+})
+
+test('fresh spawned backends are spared even over the cap', () => {
+ const entries: [string, ReturnType<typeof spawned>][] = [
+ ['a', spawned(1_000)],
+ ['b', spawned(2_000)],
+ ['c', spawned(3_000)]
+ ]
+
+ // All within the keepalive window → the pool may exceed the soft cap.
+ assert.deepEqual(selectPoolEvictions(entries, 1, NOW, FRESH_MS), [])
+})
+
+test('evicts only enough stale spawned backends to reach the cap', () => {
+ const entries: [string, ReturnType<typeof spawned>][] = [
+ ['a', spawned(500_000)],
+ ['b', spawned(400_000)],
+ ['c', spawned(300_000)],
+ ['d', spawned(1_000)]
+ ]
+
+ // 4 spawned, keep 2 → remove 2, oldest first.
+ assert.deepEqual(selectPoolEvictions(entries, 2, NOW, FRESH_MS), ['a', 'b'])
+})
+
+test('descriptor-only pools never evict', () => {
+ const entries: [string, ReturnType<typeof descriptor>][] = [
+ ['conn:a::p', descriptor(999_000)],
+ ['conn:b::p', descriptor(999_000)],
+ ['conn:c::p', descriptor(999_000)],
+ ['conn:d::p', descriptor(999_000)]
+ ]
+
+ assert.deepEqual(selectPoolEvictions(entries, 2, NOW, FRESH_MS), [])
+})
diff --git a/apps/desktop/electron/pool-eviction.ts b/apps/desktop/electron/pool-eviction.ts
new file mode 100644
index 00000000000..628a3311993
--- /dev/null
+++ b/apps/desktop/electron/pool-eviction.ts
@@ -0,0 +1,58 @@
+// LRU cap accounting for the desktop backend pool.
+//
+// The pool holds two very different kinds of entries under one Map:
+// 1. SPAWNED local profile backends — a real child process each (the thing
+// the POOL_MAX_BACKENDS cap exists to bound).
+// 2. Process-less connection DESCRIPTORS — remote/cloud registry sources and
+// per-profile remote overrides (`entry.process === null`). These hold no
+// local process; their only cost is a cached descriptor.
+//
+// Counting both kinds against the cap meant a roster refresh across N
+// registered remote connections could push the Map size over the cap and
+// LRU-evict a REAL spawned backend that had merely been idle past the
+// keepalive window. Cap accounting (and cap-driven eviction) therefore only
+// considers entries with a live child process; descriptor entries remain
+// subject to the idle reaper, just not to the process cap.
+
+export interface PoolEvictionEntry {
+ lastActiveAt?: null | number
+ process?: unknown
+}
+
+/**
+ * Pick which pool keys the LRU cap should evict so that at most `keep`
+ * SPAWNED backends remain. Only entries with a live child process count
+ * toward the cap or are eligible for cap eviction, and — as before — only
+ * entries idle beyond `freshMs` may be evicted (an actively kept-alive pool
+ * may exceed the soft cap rather than kill a running session).
+ */
+export function selectPoolEvictions<K>(
+ entries: Iterable<[K, PoolEvictionEntry]>,
+ keep: number,
+ now: number,
+ freshMs: number
+): K[] {
+ const spawned = [...entries].filter(([, entry]) => Boolean(entry.process))
+
+ if (spawned.length <= keep) {
+ return []
+ }
+
+ const evictable = spawned
+ .filter(([, entry]) => now - (entry.lastActiveAt || 0) > freshMs)
+ .sort((a, b) => (a[1].lastActiveAt || 0) - (b[1].lastActiveAt || 0))
+
+ let removable = spawned.length - Math.max(0, keep)
+ const evictions: K[] = []
+
+ for (const [key] of evictable) {
+ if (removable <= 0) {
+ break
+ }
+
+ evictions.push(key)
+ removable -= 1
+ }
+
+ return evictions
+}