The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(desktop): extract pool teardown into pool-stop.ts with behavior tests
Teknium9b112e573689
diff --git a/apps/desktop/electron/backend-lifecycle.test.ts b/apps/desktop/electron/backend-lifecycle.test.tsdeleted file mode 100644index e91823fda2b..00000000000--- a/apps/desktop/electron/backend-lifecycle.test.ts+++ /dev/null@@ -1,49 +0,0 @@-import assert from 'node:assert/strict'-import fs from 'node:fs'-import path from 'node:path'-import test from 'node:test'-import { fileURLToPath } from 'node:url'--const source = fs- .readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), 'main.ts'), 'utf8')- .replace(/\r\n/g, '\n')--test('pool backend teardown waits for bounded exit before dropping lifecycle state', () => {- const start = source.indexOf('async function stopPoolBackend(profile)')- assert.notEqual(start, -1)- const snippet = source.slice(start, start + 1200)-- assert.match(snippet, /poolStops\.get\(profile\)/)- assert.match(snippet, /stopBackendChild\(entry\.process\)/)- assert.match(snippet, /await waitForBackendExit\(entry\.process\)/)- assert.match(snippet, /backendPool\.delete\(profile\)/)- assert.ok(- snippet.indexOf('await waitForBackendExit(entry.process)') < snippet.indexOf('backendPool.delete(profile)'),- 'pool state must remain owned until the child exits or is escalated'- )-})--test('repeated pool cleanup reuses one in-flight teardown', () => {- const start = source.indexOf('async function stopPoolBackend(profile)')- const snippet = source.slice(start, start + 1200)-- assert.match(snippet, /if \(stopping\) \{\s*return stopping\s*\}/)- assert.match(snippet, /poolStops\.set\(profile, stop\)/)- assert.match(snippet, /poolStops\.delete\(profile\)/)-- const ensure = source.slice(source.indexOf('async function ensureBackend(profile)'), source.indexOf('async function ensureBackend(profile)') + 700)- assert.match(ensure, /const stopping = poolStops\.get\(key\)/)- assert.match(ensure, /if \(stopping\) \{\s*await stopping\s*\}/)-})--test('desktop quit waits for primary and pool backend teardown exactly once', () => {- const start = source.indexOf("app.on('before-quit'")- assert.notEqual(start, -1)- const snippet = source.slice(start, start + 1800)-- assert.match(snippet, /event\.preventDefault\(\)/)- assert.match(snippet, /if \(backendQuitPromise\)/)- assert.match(snippet, /waitForBackendExit\(dying\)/)- assert.match(snippet, /stopAllPoolBackends\(\)/)- assert.match(snippet, /backendQuitReady = true\s*app\.quit\(\)/)-})diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex eaa30f2851c..520c1938256 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -9533,12 +9533,6 @@ async function ensureBackend(profile) {: connection}- const stopping = poolStopper.inFlight(key)-- if (stopping) {- await stopping- }-// A backend for this key may still be dying (idle reap, LRU eviction, a// just-finished delete). Wait for its bounded exit before reusing or// spawning, so two children never share one profile's HERMES_HOME.diff --git a/apps/desktop/electron/pool-stop.test.ts b/apps/desktop/electron/pool-stop.test.tsnew file mode 100644index 00000000000..55d6c40c879--- /dev/null+++ b/apps/desktop/electron/pool-stop.test.ts@@ -0,0 +1,138 @@+import assert from 'node:assert/strict'++import { test } from 'vitest'++import { createPoolStopper, type PoolStopEntry } from './pool-stop'++// Pins the orphaned-backend class from #62721: fire-and-forget pool stops+// (SIGTERM + immediate entry delete) dropped the process handle before the+// child exited, so a slow/stuck backend survived detached under PID 1.++interface Child {+ exited: boolean+ killed: boolean+}++function harness() {+ const pool = new Map<string, PoolStopEntry>()+ const events: string[] = []+ const exitResolvers = new Map<Child, () => void>()++ const stopper = createPoolStopper({+ pool,+ stopChild: child => {+ ;(child as Child).killed = true+ events.push('stop')+ },+ waitForExit: child =>+ new Promise<void>(resolve => {+ exitResolvers.set(child as Child, () => {+ ;(child as Child).exited = true+ events.push('exit')+ resolve()+ })+ })+ })++ function addChild(key: string): Child {+ const child: Child = { exited: false, killed: false }++ pool.set(key, { process: child })++ return child+ }++ return { addChild, events, exitResolvers, pool, stopper }+}++test('evicts the entry immediately but retains the handle until bounded exit', async () => {+ const { addChild, exitResolvers, pool, stopper } = harness()+ const child = addChild('selena')++ const stop = stopper.stop('selena')++ // No router may hand out the dying backend...+ assert.equal(pool.has('selena'), false)+ // ...but the stop is not done and the in-flight handle is discoverable.+ assert.equal(child.killed, true)+ assert.equal(stopper.inFlight('selena'), stop)++ exitResolvers.get(child)?.()+ await stop++ assert.equal(child.exited, true)+ assert.equal(stopper.inFlight('selena'), undefined)+})++test('concurrent stop requests share one in-flight teardown', async () => {+ const { addChild, events, exitResolvers, stopper } = harness()+ const child = addChild('selena')++ const first = stopper.stop('selena')+ const second = stopper.stop('selena')++ assert.equal(first, second)++ exitResolvers.get(child)?.()+ await first++ // One SIGTERM, one exit — not one per caller.+ assert.deepEqual(events, ['stop', 'exit'])+})++test('stop of an unknown key resolves without signalling anything', async () => {+ const { events, stopper } = harness()++ await stopper.stop('ghost')++ assert.deepEqual(events, [])+ assert.equal(stopper.inFlight('ghost'), undefined)+})++test('stopAll stops every pooled backend and resolves after all exits', async () => {+ const { addChild, exitResolvers, pool, stopper } = harness()+ const a = addChild('a')+ const b = addChild('b')++ let settled = false++ const all = stopper.stopAll().then(() => {+ settled = true+ })++ assert.equal(pool.size, 0)+ assert.equal(a.killed, true)+ assert.equal(b.killed, true)++ exitResolvers.get(a)?.()+ await Promise.resolve()+ assert.equal(settled, false, 'must wait for EVERY child, not the first')++ exitResolvers.get(b)?.()+ await all+ assert.equal(settled, true)+})++test('a respawn can await the in-flight stop before reusing the key', async () => {+ const { addChild, exitResolvers, stopper } = harness()+ const child = addChild('selena')+ const order: string[] = []++ void stopper.stop('selena')++ const respawn = (async () => {+ const dying = stopper.inFlight('selena')++ if (dying) {+ await dying+ }++ order.push('spawn')+ })()++ order.push('exit-signal')+ exitResolvers.get(child)?.()+ await respawn++ assert.deepEqual(order, ['exit-signal', 'spawn'])+})