hermes-agent/apps/desktop/electron/remote-owner-cache.test.ts
Brooklyn Nicholson 88f5a5c07c fix(desktop): bound the remote-owner memo in the main process
The #85834 remote-owner memo (`remoteOwnerBySessionId`) only ever
INSERTS: the 30s TTL marks entries stale but never evicts them, so a
long-lived main process kept one Map entry per session id it had ever
resolved a hint-less /api/sessions/:id read for. Session ids churn
through normal use, so this is unbounded growth on the main-process
heap — the half the renderer-side leak fixes (#77311 series) never
touched (#58485).

The memo now lives in `remote-owner-cache.ts` as a pure, injectable
(cache limit / clock / TTL) FIFO-bounded cache — the same shape as
titleCache's bound (500 entries). Survey of the other main.ts
registries named in the issue: titleCache (500 FIFO), hermesLog (300),
recentFileReveals (self-pruning), renderTitleQueue (drained by 2
concurrent 8s-timeout jobs), sshConnections (full lifecycle), and the
baseUrl-keyed caches (gatewayAuthProvidersCache, _nativeTokens — one
entry per configured remote) are all bounded on current main.

Refs #58485
2026-09-29 17:35:16 -05:00

45 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { createRemoteOwnerCache } from './remote-owner-cache'
describe('createRemoteOwnerCache (#58485)', () => {
it('returns a fresh entry within the TTL and null past it', () => {
let clock = 1_000
const cache = createRemoteOwnerCache({ now: () => clock, ttlMs: 30_000 })
expect(cache.fresh('s1')).toBeNull()
cache.remember('s1', 'remote-a')
expect(cache.fresh('s1')?.profile).toBe('remote-a')
clock += 29_999
expect(cache.fresh('s1')?.profile).toBe('remote-a')
clock += 1
expect(cache.fresh('s1')).toBeNull()
})
it('never grows past the limit: the oldest entry is FIFO-evicted', () => {
const cache = createRemoteOwnerCache({ limit: 3 })
for (const id of ['s1', 's2', 's3', 's4']) {
cache.remember(id, null)
}
expect(cache.size()).toBe(3)
// s1 was evicted to make room for s4; the newest entries survive.
expect(cache.fresh('s1')).toBeNull()
expect(cache.fresh('s4')).not.toBeNull()
expect(cache.fresh('s3')).not.toBeNull()
})
it('re-remembering an existing id refreshes it without growing the map', () => {
const cache = createRemoteOwnerCache({ limit: 2 })
cache.remember('s1', null)
cache.remember('s2', null)
cache.remember('s1', 'remote-a')
expect(cache.size()).toBe(2)
expect(cache.fresh('s1')?.profile).toBe('remote-a')
})
})