The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): bound the remote-owner memo in the main process
Brooklyn Nicholson88f5a5c07c19
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex a3250590f4e..7b6e2757f4c 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -126,6 +126,7 @@ import { installCommandScreenshot } from './command-screenshot'import { composerImageTimestamp } from './composer-image-name'import { writeComposerPaste } from './composer-paste'import { applyConnectionChange, teardownSshState } from './connection-apply'+import { createRemoteOwnerCache } from './remote-owner-cache'import {connectionInstallIds,evictConnectionCaches,@@ -16944,11 +16945,11 @@ async function remoteSessionList(profile, searchParams) {}// #85834: find which remote profile owns a session id when the caller gave no-// profile hint (pure lookup lives in profile-session-routing.ts). Results are-// memoized briefly so a burst of hint-less reads (transcript + messages)-// costs one sweep across the configured remotes.-const remoteOwnerBySessionId = new Map<string, { at: number; profile: null | string }>()-const REMOTE_OWNER_CACHE_TTL_MS = 30_000+// profile hint (pure lookup lives in profile-session-routing.ts; the bounded+// memo lives in remote-owner-cache.ts — #58485: it only ever INSERTS, so the+// raw Map grew one entry per session id ever resolved, unbounded, on the main+// process heap).+const remoteOwnerCache = createRemoteOwnerCache()async function remoteOwnerProfileForSession(sessionId: string) {if (!sessionId) {@@ -16961,9 +16962,9 @@ async function remoteOwnerProfileForSession(sessionId: string) {return null}- const cached = remoteOwnerBySessionId.get(sessionId)+ const cached = remoteOwnerCache.fresh(sessionId)- if (cached && Date.now() - cached.at < REMOTE_OWNER_CACHE_TTL_MS) {+ if (cached) {return cached.profile}@@ -16971,7 +16972,7 @@ async function remoteOwnerProfileForSession(sessionId: string) {remoteSessionList(profile, params)).catch(() => null)- remoteOwnerBySessionId.set(sessionId, { at: Date.now(), profile: owner })+ remoteOwnerCache.remember(sessionId, owner)return owner}diff --git a/apps/desktop/electron/remote-owner-cache.test.ts b/apps/desktop/electron/remote-owner-cache.test.tsnew file mode 100644index 00000000000..aa6a6bf8d37--- /dev/null+++ b/apps/desktop/electron/remote-owner-cache.test.ts@@ -0,0 +1,45 @@+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')+ })+})diff --git a/apps/desktop/electron/remote-owner-cache.ts b/apps/desktop/electron/remote-owner-cache.tsnew file mode 100644index 00000000000..afe7cd4c04c--- /dev/null+++ b/apps/desktop/electron/remote-owner-cache.ts@@ -0,0 +1,48 @@+/**+ * Main-process memo: which remote profile owns a session id when the caller gave+ * no profile hint (#85834).+ *+ * #58485: the memo only ever INSERTS — the TTL marks entries stale but never+ * evicts — so a long-lived main process kept one entry per session id it had+ * ever resolved. That is unbounded growth on the main-process heap (the+ * renderer-side leak fixes never touched it). Entries are FIFO-evicted past+ * `limit`, the same shape as titleCache's bound.+ */++export interface RemoteOwnerEntry {+ at: number+ profile: null | string+}++export interface RemoteOwnerCache {+ /** Fresh memo for `sessionId`, else null (absent or older than `ttlMs`). */+ fresh(sessionId: string): null | RemoteOwnerEntry+ /** Record a lookup result, FIFO-evicting the oldest entry past `limit`. */+ remember(sessionId: string, profile: null | string): void+ /** Test/inspection hook: current size. */+ size(): number+}++export function createRemoteOwnerCache({+ limit = 500,+ now = () => Date.now(),+ ttlMs = 30_000+}: { limit?: number; now?: () => number; ttlMs?: number } = {}): RemoteOwnerCache {+ const entries = new Map<string, RemoteOwnerEntry>()++ return {+ fresh(sessionId) {+ const cached = entries.get(sessionId)++ return cached && now() - cached.at < ttlMs ? cached : null+ },+ remember(sessionId, profile) {+ if (entries.size >= limit) {+ entries.delete(entries.keys().next().value)+ }++ entries.set(sessionId, { at: now(), profile })+ },+ size: () => entries.size+ }+}