diff --git a/src/renderer/src/runtime/runtime-rpc-client.test.ts b/src/renderer/src/runtime/runtime-rpc-client.test.ts index c5b67286b..5e6869328 100644 --- a/src/renderer/src/runtime/runtime-rpc-client.test.ts +++ b/src/renderer/src/runtime/runtime-rpc-client.test.ts @@ -201,6 +201,43 @@ describe('runtime RPC client routing', () => { ]) }) + it('bounds successful remote compatibility checks by evicting old environments', async () => { + runtimeEnvironmentCall.mockImplementation(({ method }: { method: string }) => { + const result = + method === 'status.get' + ? { + runtimeId: 'remote-runtime', + graphStatus: 'ready', + runtimeProtocolVersion: RUNTIME_PROTOCOL_VERSION, + minCompatibleRuntimeClientVersion: MIN_COMPATIBLE_RUNTIME_CLIENT_VERSION + } + : { ok: true } + return Promise.resolve({ + id: method, + ok: true, + result, + _meta: { runtimeId: 'remote-runtime' } + }) + }) + + for (let i = 0; i < 33; i += 1) { + await callRuntimeRpc({ kind: 'environment', environmentId: `env-${i}` }, 'repo.list') + } + + runtimeEnvironmentCall.mockClear() + await callRuntimeRpc({ kind: 'environment', environmentId: 'env-0' }, 'worktree.list') + expect(runtimeEnvironmentCall.mock.calls.map((call) => call[0].method)).toEqual([ + 'status.get', + 'worktree.list' + ]) + + runtimeEnvironmentCall.mockClear() + await callRuntimeRpc({ kind: 'environment', environmentId: 'env-32' }, 'worktree.list') + expect(runtimeEnvironmentCall.mock.calls.map((call) => call[0].method)).toEqual([ + 'worktree.list' + ]) + }) + it('throws structured runtime RPC failures', () => { const failure = { id: 'rpc-1', diff --git a/src/renderer/src/runtime/runtime-rpc-client.ts b/src/renderer/src/runtime/runtime-rpc-client.ts index dd974e303..9ff3e26e6 100644 --- a/src/renderer/src/runtime/runtime-rpc-client.ts +++ b/src/renderer/src/runtime/runtime-rpc-client.ts @@ -6,6 +6,7 @@ import { assertRuntimeStatusCompatible } from './runtime-protocol-compat' export type RuntimeClientTarget = { kind: 'local' } | { kind: 'environment'; environmentId: string } +const RUNTIME_COMPATIBILITY_CACHE_MAX = 32 const compatibleRuntimeEnvironments = new Map>() export class RuntimeRpcCallError extends Error { @@ -79,6 +80,8 @@ async function ensureRuntimeEnvironmentCompatible( ): Promise { const cached = compatibleRuntimeEnvironments.get(environmentId) if (cached) { + compatibleRuntimeEnvironments.delete(environmentId) + compatibleRuntimeEnvironments.set(environmentId, cached) await cached return } @@ -93,15 +96,34 @@ async function ensureRuntimeEnvironmentCompatible( ) assertRuntimeStatusCompatible(status) })() - compatibleRuntimeEnvironments.set(environmentId, check) + rememberRuntimeEnvironmentCompatibility(environmentId, check) try { await check } catch (error) { - compatibleRuntimeEnvironments.delete(environmentId) + if (compatibleRuntimeEnvironments.get(environmentId) === check) { + compatibleRuntimeEnvironments.delete(environmentId) + } throw error } } +function rememberRuntimeEnvironmentCompatibility( + environmentId: string, + check: Promise +): void { + // Why: saved/removed remote runtimes can churn through unique ids in long + // renderer sessions; successful compatibility promises should not grow forever. + compatibleRuntimeEnvironments.delete(environmentId) + compatibleRuntimeEnvironments.set(environmentId, check) + while (compatibleRuntimeEnvironments.size > RUNTIME_COMPATIBILITY_CACHE_MAX) { + const oldest = compatibleRuntimeEnvironments.keys().next().value + if (oldest === undefined) { + break + } + compatibleRuntimeEnvironments.delete(oldest) + } +} + export function clearRuntimeCompatibilityCache(environmentId?: string | null): void { const trimmed = environmentId?.trim() if (trimmed) {