Files
hermes-agent/apps/desktop/electron/pool-limits.test.ts
T
ClintonEmok c401756a6a fix(desktop): pool sizing as a live device preference in Settings (#91545)
Hover-intent prewarm sweeps across the Bots rail spawned past the pool cap,
LRU-evicting the backend the user was about to click — an evict/respawn
cascade that made profile switching progressively slower (#91545).

- prewarmProfileBackend skips speculative spawns once every pool slot holds
  an open socket; the real click still spawns on demand.
- Pool max/idle become a device preference (Settings -> Advanced), persisted
  atomically in userData (pool-limits.json) and applied live over IPC; the
  HERMES_DESKTOP_POOL_* env vars remain the initial fallback. Defaults are
  unchanged (3 backends / 10 min idle).

Squash of the 3-commit PR #92581 branch (a00dc088c5..783899d12f) applied
via diff onto the spawn-coordinator salvage; import + constant-block
conflicts resolved so the coordinator is constructed from, and follows,
the live preference (setLimit added in the next commit).
2026-09-03 12:19:05 +05:30

48 lines
1.8 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { clampPoolLimits, parsePoolLimits, POOL_LIMITS_BOUNDS, POOL_LIMITS_DEFAULTS, POOL_LIMITS_MIN } from './pool-limits'
describe('parsePoolLimits', () => {
it('falls back to defaults for null/empty/corrupt input', () => {
expect(parsePoolLimits(null)).toEqual(POOL_LIMITS_DEFAULTS)
expect(parsePoolLimits(undefined)).toEqual(POOL_LIMITS_DEFAULTS)
expect(parsePoolLimits('')).toEqual(POOL_LIMITS_DEFAULTS)
expect(parsePoolLimits('not json {')).toEqual(POOL_LIMITS_DEFAULTS)
})
it('parses a valid persisted blob', () => {
expect(parsePoolLimits(JSON.stringify({ maxBackends: 11, idleMs: 7_200_000 }))).toEqual({
maxBackends: 11,
idleMs: 7_200_000
})
})
it('fills missing keys from defaults', () => {
expect(parsePoolLimits(JSON.stringify({ maxBackends: 5 }))).toEqual({ ...POOL_LIMITS_DEFAULTS, maxBackends: 5 })
expect(parsePoolLimits('{}')).toEqual(POOL_LIMITS_DEFAULTS)
})
it('ignores non-numeric junk instead of NaN-poisoning the pool', () => {
expect(parsePoolLimits(JSON.stringify({ maxBackends: 'lots', idleMs: null }))).toEqual(POOL_LIMITS_DEFAULTS)
})
})
describe('clampPoolLimits', () => {
it('clamps below the floors', () => {
expect(clampPoolLimits({ maxBackends: 0 }).maxBackends).toBe(POOL_LIMITS_MIN.maxBackends)
expect(clampPoolLimits({ idleMs: 100 }).idleMs).toBe(POOL_LIMITS_MIN.idleMs)
})
it('clamps absurdly high backend counts', () => {
expect(clampPoolLimits({ maxBackends: 10_000 }).maxBackends).toBeLessThanOrEqual(64)
})
it('clamps idleMs to the shared ceiling (7 days)', () => {
expect(clampPoolLimits({ idleMs: 999_000_000 }).idleMs).toBe(POOL_LIMITS_BOUNDS.idleMsMax)
})
it('floors fractional values', () => {
expect(clampPoolLimits({ maxBackends: 2.9 }).maxBackends).toBe(2)
})
})