The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): bound the pool-slot wait below the renderer boot budget
kshitijk4poor5810172f5236
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 2efc129649a..d466e13725a 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -282,8 +282,8 @@ import {} from './plugin-profile-routes'import { selectPoolEvictions } from './pool-eviction'import {- type LocalBackendSpawnRequest,LocalBackendSpawnCoordinator,+ type LocalBackendSpawnRequest,releaseLocalBackendSlotAfterExit} from './pool-spawn-coordinator'import { createPoolStopper } from './pool-stop'@@ -1417,6 +1417,10 @@ const profileDeletionGate = new ProfileDeletionGate()const POOL_MAX_BACKENDS = Math.max(1, Number(process.env.HERMES_DESKTOP_POOL_MAX) || 3)const POOL_IDLE_MS = Math.max(60_000, Number(process.env.HERMES_DESKTOP_POOL_IDLE_MS) || 10 * 60_000)const localBackendSpawnCoordinator = new LocalBackendSpawnCoordinator(POOL_MAX_BACKENDS)+// How long a spawn may wait for a free local slot. Must stay under the+// renderer's BACKEND_BOOT_WAIT_TIMEOUT_MS (45s, src/lib/with-timeout.ts) so+// the queued ticket fails before the renderer does and the user sees why.+const POOL_SLOT_WAIT_MS = 30_000// A backend touched within this window has a live renderer socket (the keepalive// pings every 60s for every open profile). LRU eviction must spare these — a@@ -12229,6 +12233,7 @@ function teardownFailedLocalBackend(poolKey: string, entry: any): Promise<void>}const child = entry.process+const teardown = releaseLocalBackendSlotAfterExit(() => releaseLocalBackendSlot(entry),async () => {@@ -12248,6 +12253,7 @@ function teardownFailedLocalBackend(poolKey: string, entry: any): Promise<void>// Keep the settled promise in the WeakMap for the lifetime of this entry.// Error + exit + outer catch may all request cleanup; none may run it twice.failedLocalBackendTeardowns.set(entry, teardown)+return teardown}@@ -12288,9 +12294,21 @@ async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; po}}- const spawnRequest = localBackendSpawnCoordinator.request(poolKey, { timeoutMs: POOL_IDLE_MS })+ // Bound the slot wait BELOW the renderer's backend-boot budget (45s): once+ // the renderer has given up on this spawn, a ticket still queued for the+ // pool-idle window (10 min) would hold the pool key hostage and every+ // later click on the profile would join that stale wait. Failing here+ // surfaces the "all N slots busy" reason instead of a generic boot timeout.+ const spawnRequest = localBackendSpawnCoordinator.request(poolKey, { timeoutMs: POOL_SLOT_WAIT_MS })entry.localBackendSlotKey = poolKeyentry.localBackendSpawnRequest = spawnRequest++ if (localBackendSpawnCoordinator.activeCount >= POOL_MAX_BACKENDS) {+ rememberLog(+ `Profile backend "${profile}" waiting for a free local slot (${localBackendSpawnCoordinator.activeCount}/${POOL_MAX_BACKENDS} busy, ${localBackendSpawnCoordinator.queuedCount} queued)`+ )+ }+entry.releaseLocalBackendSlot = await spawnRequest.acquiredif (entry.localBackendSpawnRequest === spawnRequest) {diff --git a/apps/desktop/electron/pool-spawn-coordinator.test.ts b/apps/desktop/electron/pool-spawn-coordinator.test.tsindex 2c448142afe..4b1fc2beb77 100644--- a/apps/desktop/electron/pool-spawn-coordinator.test.ts+++ b/apps/desktop/electron/pool-spawn-coordinator.test.ts@@ -1,5 +1,8 @@import assert from 'node:assert/strict'import { spawn } from 'node:child_process'+import fs from 'node:fs'+import path from 'node:path'+import { fileURLToPath } from 'node:url'import { test } from 'vitest'@@ -10,9 +13,11 @@ import {const deferred = () => {let resolve!: () => void+const promise = new Promise<void>(done => {resolve = done})+return { promise, resolve }}@@ -45,6 +50,7 @@ test('100 concurrent local requests never hold more than the configured slots',for (const gate of gates.slice(start, start + limit)) {gate.resolve()}+await flush()}@@ -118,6 +124,7 @@ test('100 real child processes never exceed twelve simultaneous local slots', asconst child = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 40)'], {stdio: 'ignore'})+assert.ok(child.pid)livePids.add(child.pid)seenPids.add(child.pid)@@ -153,8 +160,10 @@ test('failed start keeps its slot until the child has actually exited', async ()const childExit = deferred()const releaseFailed = await coordinator.acquire('failed')let successorEntered = false+const successor = coordinator.acquire('successor').then(release => {successorEntered = true+return release})@@ -181,8 +190,10 @@ test('a rejected wait keeps the slot occupied', async () => {const coordinator = new LocalBackendSpawnCoordinator(1)const releaseFailed = await coordinator.acquire('failed')let successorEntered = false+const successor = coordinator.acquire('successor').then(release => {successorEntered = true+return release})@@ -233,8 +244,10 @@ test('a failed or repeated cleanup releases exactly one slot', async () => {const coordinator = new LocalBackendSpawnCoordinator(1)const releaseFirst = await coordinator.acquire('first')let secondEntered = false+const second = coordinator.acquire('second').then(release => {secondEntered = true+return release})@@ -254,3 +267,33 @@ test('a failed or repeated cleanup releases exactly one slot', async () => {releaseSecond()assert.equal(coordinator.activeCount, 0)})+++// ── main.ts wiring ──────────────────────────────────────────────────────────+// The coordinator is only as good as the timeout main.ts hands it. A queued+// ticket that outlives the renderer's backend-boot budget holds the pool key+// hostage: the renderer has already reported "backend didn't come up", and+// every later click on that profile joins the stale wait instead of failing+// fast with a reason.+{+ const here = path.dirname(fileURLToPath(import.meta.url))+ const mainSource = fs.readFileSync(path.join(here, 'main.ts'), 'utf8').replace(/\r\n/g, '\n')++ const withTimeoutSource = fs+ .readFileSync(path.join(here, '..', 'src', 'lib', 'with-timeout.ts'), 'utf8')+ .replace(/\r\n/g, '\n')++ test('main.ts bounds the slot wait below the renderer backend-boot budget', () => {+ const slotWait = Number(/const POOL_SLOT_WAIT_MS = ([\d_]+)/.exec(mainSource)?.[1]?.replace(/_/g, ''))++ const bootBudget = Number(+ /export const BACKEND_BOOT_WAIT_TIMEOUT_MS = ([\d_]+)/.exec(withTimeoutSource)?.[1]?.replace(/_/g, '')+ )++ assert.ok(Number.isFinite(slotWait) && slotWait > 0, 'POOL_SLOT_WAIT_MS must be a literal in main.ts')+ assert.ok(Number.isFinite(bootBudget), 'BACKEND_BOOT_WAIT_TIMEOUT_MS must be a literal')+ assert.ok(slotWait < bootBudget, `slot wait ${slotWait}ms must be below the boot budget ${bootBudget}ms`)+ assert.match(mainSource, /localBackendSpawnCoordinator\.request\(poolKey, \{ timeoutMs: POOL_SLOT_WAIT_MS \}\)/)+ assert.doesNotMatch(mainSource, /request\(poolKey, \{ timeoutMs: POOL_IDLE_MS \}\)/)+ })+}diff --git a/apps/desktop/electron/pool-spawn-coordinator.ts b/apps/desktop/electron/pool-spawn-coordinator.tsindex e847981ccfb..f7c234041d2 100644--- a/apps/desktop/electron/pool-spawn-coordinator.ts+++ b/apps/desktop/electron/pool-spawn-coordinator.ts@@ -60,6 +60,7 @@ export class LocalBackendSpawnCoordinator {}let waiter!: Waiter+const acquired = new Promise<ReleaseLocalBackendSlot>((resolve, reject) => {waiter = { key, resolve, reject, timer: null }this.#queue.push(waiter)@@ -96,6 +97,7 @@ export class LocalBackendSpawnCoordinator {this.#queue.splice(index, 1)this.#clearTimer(waiter)waiter.reject(error)+return true}