From afa5444242e4fe2290a90754358870558566423e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 16:35:37 +0000 Subject: [PATCH] refactor(sandbox): dedupe sandbox list helpers --- src/agents/sandbox/manage.ts | 59 ++++++++++++++---------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/agents/sandbox/manage.ts b/src/agents/sandbox/manage.ts index 89c80f95bd8..146f2879be6 100644 --- a/src/agents/sandbox/manage.ts +++ b/src/agents/sandbox/manage.ts @@ -23,14 +23,18 @@ export type SandboxBrowserInfo = SandboxBrowserRegistryEntry & { imageMatch: boolean; }; -export async function listSandboxContainers(): Promise { - const config = loadConfig(); - const registry = await readRegistry(); - const results: SandboxContainerInfo[] = []; +async function listSandboxRegistryItems< + TEntry extends { containerName: string; image: string; sessionKey: string }, +>(params: { + read: () => Promise<{ entries: TEntry[] }>; + resolveConfiguredImage: (agentId: string) => string; +}): Promise> { + const registry = await params.read(); + const results: Array = []; for (const entry of registry.entries) { const state = await dockerContainerState(entry.containerName); - // Get actual image from container + // Get actual image from container. let actualImage = entry.image; if (state.exists) { try { @@ -46,7 +50,7 @@ export async function listSandboxContainers(): Promise { } } const agentId = resolveSandboxAgentId(entry.sessionKey); - const configuredImage = resolveSandboxConfigForAgent(config, agentId).docker.image; + const configuredImage = params.resolveConfiguredImage(agentId); results.push({ ...entry, image: actualImage, @@ -58,38 +62,21 @@ export async function listSandboxContainers(): Promise { return results; } +export async function listSandboxContainers(): Promise { + const config = loadConfig(); + return listSandboxRegistryItems({ + read: readRegistry, + resolveConfiguredImage: (agentId) => resolveSandboxConfigForAgent(config, agentId).docker.image, + }); +} + export async function listSandboxBrowsers(): Promise { const config = loadConfig(); - const registry = await readBrowserRegistry(); - const results: SandboxBrowserInfo[] = []; - - for (const entry of registry.entries) { - const state = await dockerContainerState(entry.containerName); - let actualImage = entry.image; - if (state.exists) { - try { - const result = await execDocker( - ["inspect", "-f", "{{.Config.Image}}", entry.containerName], - { allowFailure: true }, - ); - if (result.code === 0) { - actualImage = result.stdout.trim(); - } - } catch { - // ignore - } - } - const agentId = resolveSandboxAgentId(entry.sessionKey); - const configuredImage = resolveSandboxConfigForAgent(config, agentId).browser.image; - results.push({ - ...entry, - image: actualImage, - running: state.running, - imageMatch: actualImage === configuredImage, - }); - } - - return results; + return listSandboxRegistryItems({ + read: readBrowserRegistry, + resolveConfiguredImage: (agentId) => + resolveSandboxConfigForAgent(config, agentId).browser.image, + }); } export async function removeSandboxContainer(containerName: string): Promise {