fix: bound runtime compatibility cache (#4133)

This commit is contained in:
Neil
2026-05-31 05:32:02 -07:00
committed by GitHub
parent 4621560665
commit cc20fee202
2 changed files with 61 additions and 2 deletions
@@ -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',
+24 -2
View File
@@ -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<string, Promise<void>>()
export class RuntimeRpcCallError extends Error {
@@ -79,6 +80,8 @@ async function ensureRuntimeEnvironmentCompatible(
): Promise<void> {
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>
): 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) {