Skip to content

The agent that grows with you

fix(desktop): reap keepalive-pinned pool backends idle past a streamed-activity TTL

Brooklyn Nicholson654300240a32
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 405f6ba236f..5653d7ca9b6 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -404,7 +404,7 @@ import {
localRouteFallbackProfiles,
undialedSshRouteSeeds
} from './plugin-profile-routes'
-import { clampPoolLimits, parsePoolLimits, POOL_LIMITS_DEFAULTS } from './pool-limits'
+import { clampPoolLimits, parsePoolLimits, POOL_LIMITS_DEFAULTS, POOL_LIMITS_MIN } from './pool-limits'
import { createPoolRetirer } from './pool-retire'
import { createPoolRetirementClient } from './pool-retire-http'
import {
@@ -1976,6 +1976,20 @@ const POOL_KEEPALIVE_FRESH_MS = Math.max(
Number(process.env.HERMES_DESKTOP_POOL_KEEPALIVE_FRESH_MS) || 4 * 60_000
)
+// Pinned-tier TTL (#105239): the renderer's 60s keepalive (touchPoolBackend)
+// refreshes lastActiveAt for every OPEN chat, so the idle reaper's only clock
+// never fires for the pinned tier — every profile whose chat was ever opened
+// held its ~120 MB serve child until app quit (126 processes / 7.5 GB on the
+// reporter's machine, all parented to Hermes.exe). A keepalive proves the
+// chat is open, not that anything streamed: retire a local child whose last
+// streamed turn is older than this window. Re-focusing the chat re-ensures it
+// idempotently (ensureBackend/ensureRegistryBackend reuse), and mid-stream
+// safety is unchanged — activeTurn entries are excluded by the retirer.
+const POOL_PINNED_IDLE_MS = Math.max(
+ POOL_LIMITS_MIN.idleMs,
+ Number(process.env.HERMES_DESKTOP_POOL_PINNED_IDLE_MS) || 60 * 60_000
+)
+
let poolIdleReaper = null
let backendOrphanReapPromise = null
// Auto-reload budget for renderer crashes, shared by EVERY window (primary,
@@ -12131,6 +12145,13 @@ function touchPoolBackend(profile, options: { activeTurn?: boolean } = {}) {
if (typeof options.activeTurn === 'boolean') {
entry.activeTurn = options.activeTurn
+
+ // A prompt turn leasing this backend IS streamed activity (#105239):
+ // the keepalive touch alone only proves the chat is open, so the
+ // pinned-tier TTL reads this stamp, not lastActiveAt.
+ if (options.activeTurn) {
+ entry.lastStreamedAt = Date.now()
+ }
}
return
@@ -12161,10 +12182,26 @@ function startPoolIdleReaper() {
const now = Date.now()
for (const [profile, entry] of [...backendPool.entries()]) {
- if (now - (entry.lastActiveAt || 0) > poolIdleMs()) {
- // Remote descriptors hold no child/slot. Local children require the
- // same admission authority as foreground and LRU reclamation.
- const retiring = entry.process ? poolRetirer.retireIdle(profile, poolIdleMs()) : stopPoolBackend(profile)
+ // Remote descriptors hold no child/slot. Local children require the
+ // same admission authority as foreground and LRU reclamation.
+ // Pinned-tier TTL (#105239): the keepalive refreshes lastActiveAt for
+ // every open chat, so that clock alone never fires for the pinned tier.
+ // A local child whose last STREAMED turn (activeTurn touch) is older
+ // than POOL_PINNED_IDLE_MS is idle even while keepalive-fresh; entries
+ // without the stamp keep the legacy lastActiveAt clock.
+ const idleFor = now - (entry.lastActiveAt || 0)
+ const streamedIdleFor = entry.lastStreamedAt ? now - entry.lastStreamedAt : null
+ const reapable = idleFor > poolIdleMs() || (streamedIdleFor !== null && streamedIdleFor > POOL_PINNED_IDLE_MS)
+
+ if (reapable) {
+ const retiring = entry.process
+ ? poolRetirer.retireIdle(profile, poolIdleMs(), candidate =>
+ Boolean(
+ Date.now() - (candidate.lastActiveAt || 0) > poolIdleMs() ||
+ (candidate.lastStreamedAt ? Date.now() - candidate.lastStreamedAt > POOL_PINNED_IDLE_MS : false)
+ )
+ )
+ : stopPoolBackend(profile)
void retiring.catch(error => rememberLog(`Pool idle retirement failed: ${String(error)}`))
}
diff --git a/apps/desktop/electron/pool-retire.test.ts b/apps/desktop/electron/pool-retire.test.ts
index d9cf2f243b0..8445f099e6b 100644
--- a/apps/desktop/electron/pool-retire.test.ts
+++ b/apps/desktop/electron/pool-retire.test.ts
@@ -83,3 +83,51 @@ test('candidate selection excludes processless descriptors, renderer-leased work
['old', 'fresh']
)
})
+
+test('retireIdle honours the pinned-tier eligibility override while every other safeguard stays armed (#105239)', async () => {
+ // The keepalive refreshes lastActiveAt for every open chat, so a pinned
+ // backend's legacy clock never expires. The reaper passes an override that
+ // also reads lastStreamedAt; the retirer must still veto an active turn,
+ // require the admission permit, and keep identity checks — only the idle
+ // PREDICATE changes.
+ for (const outcome of ['stale-streamed', 'fresh-streamed', 'mid-turn'] as const) {
+ const entry: PoolRetireEntry = {
+ process: {},
+ lastActiveAt: Date.now(),
+ // keepalive-fresh in every case; only streamed-activity age varies.
+ lastStreamedAt: outcome === 'fresh-streamed' ? Date.now() - 5 * 60_000 : Date.now() - 2 * 60 * 60_000,
+ ...(outcome === 'mid-turn' ? { activeTurn: true } : {})
+ }
+ const pool = new Map([['pinned', entry]])
+ const stopped: string[] = []
+
+ const retirer = createPoolRetirer({
+ pool,
+ coordinator: new LocalBackendSpawnCoordinator(3),
+ prepare: async () => 'permit',
+ commit: async () => true,
+ cancel: async () => {},
+ stopBackend: async key => {
+ stopped.push(key)
+ pool.delete(key)
+ }
+ })
+
+ try {
+ await retirer.retireIdle('pinned', 10 * 60_000, candidate =>
+ Boolean(candidate.lastStreamedAt && Date.now() - (candidate.lastStreamedAt || 0) > 60 * 60_000)
+ )
+ } finally {
+ retirer.dispose()
+ }
+
+ // stale-streamed (keepalive-fresh, no streamed turn for 2h) retires;
+ // fresh-streamed fails the override and stays; mid-turn is vetoed by the
+ // retirer even though the override said yes.
+ assert.deepEqual(
+ stopped,
+ outcome === 'stale-streamed' ? ['pinned'] : [],
+ outcome
+ )
+ }
+})
diff --git a/apps/desktop/electron/pool-retire.ts b/apps/desktop/electron/pool-retire.ts
index 285fd0aff05..70f67f02938 100644
--- a/apps/desktop/electron/pool-retire.ts
+++ b/apps/desktop/electron/pool-retire.ts
@@ -4,6 +4,13 @@ export interface PoolRetireEntry {
/** An early veto only; backend admission owns the proof. */
activeTurn?: boolean
lastActiveAt?: null | number
+ /**
+ * Last prompt turn that leased this backend (#105239): a keepalive touch
+ * only proves the chat is open, while this stamp proves streamed activity.
+ * The idle reaper reads it for the pinned-tier TTL; absent means the entry
+ * predates the stamp and keeps the legacy lastActiveAt clock.
+ */
+ lastStreamedAt?: null | number
process?: unknown
}
@@ -147,11 +154,24 @@ export function createPoolRetirer<E extends PoolRetireEntry>(deps: PoolRetirerDe
throw new Error(`Backend for "${key}" was retired; open it explicitly to reconnect.`)
}
},
- retireIdle: (key: string, idleMs: number) =>
+ retireIdle: (key: string, idleMs: number, idleEligibility?: (entry: E) => boolean) =>
enqueue(async () => {
const entry = deps.pool.get(key)
- return entry ? retire(key, entry, () => Date.now() - (entry.lastActiveAt || 0) > idleMs) : false
+ if (!entry) {
+ return false
+ }
+
+ // The default clock is the keepalive-touched lastActiveAt; the reaper
+ // passes an override that also honours the pinned-tier TTL
+ // (lastStreamedAt) so a keepalive-fresh but stream-idle entry can
+ // still be retired (#105239). The retirer keeps every other
+ // safeguard: identity, admission permit, activeTurn veto, proof probe.
+ return retire(
+ key,
+ entry,
+ () => idleEligibility ? idleEligibility(entry) : Date.now() - (entry.lastActiveAt || 0) > idleMs
+ )
}),
evictTo: (keep: number, freshMs: number) =>
enqueue(async () => {