From 40894a41f87bfba78e9e78d57684bdf64486fba2 Mon Sep 17 00:00:00 2001 From: Mazurov Anton Date: Fri, 6 Nov 2026 13:39:21 +0300 Subject: [PATCH] fix(acp): retry transient -32603 during session model catalog probes opencode answers ACP session/new with a transient -32603 while its providers still open, and a single probe failure degraded the chat model picker to one pinned model. Retry initialize and session/new up to 3 times with abort-aware backoff, mirroring the existing sweep retry. --- .../acp/discovery/acp-model-catalog-probe.ts | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/acp/discovery/acp-model-catalog-probe.ts b/src/main/acp/discovery/acp-model-catalog-probe.ts index 7df8a4a31f9..482cfdc54b0 100644 --- a/src/main/acp/discovery/acp-model-catalog-probe.ts +++ b/src/main/acp/discovery/acp-model-catalog-probe.ts @@ -190,6 +190,46 @@ export function createAcpModelCatalogProbe( const folderlessListing = (spec: AcpLaunchSpec) => new AgentModelCatalogListingStoppedError(`${spec.agent} lists in a chat's folder`) +// Agents (notably opencode) answer session/new with a transient -32603 while +// their providers still open; without a retry one probe failure degrades the picker. +const TRANSIENT_AGENT_RETRY_DELAYS_MS = [250, 500, 1_000] as const + +const isTransientAgentFailure = (error: unknown): boolean => + error instanceof AcpAgentError && error.code === -32603 + +async function retryTransientAgentFailure( + run: () => Promise, + operation?: StructuredAgentResolutionOperation +): Promise { + for (let attempt = 0; ; attempt++) { + try { + return await run() + } catch (error) { + if (attempt >= TRANSIENT_AGENT_RETRY_DELAYS_MS.length || !isTransientAgentFailure(error)) { + throw error + } + assertStructuredAgentResolutionCurrent(operation) + await sleep(TRANSIENT_AGENT_RETRY_DELAYS_MS[attempt], operation?.signal) + } + } +} + +async function sleep(ms: number, signal?: AbortSignal): Promise { + if (signal?.aborted) { + return + } + await new Promise((resolve) => { + const timer = setTimeout(finish, ms) + const onAbort = (): void => finish() + function finish(): void { + clearTimeout(timer) + signal?.removeEventListener('abort', onAbort) + resolve() + } + signal?.addEventListener('abort', onAbort) + }) +} + async function readSessionModels( spec: AcpLaunchSpec, listing: AcpSessionModelDiscovery, @@ -199,14 +239,17 @@ async function readSessionModels( selectedModels?: readonly string[], ready?: (cleanup: () => Promise) => void ): Promise<{ models: AgentSessionModelOption[]; attemptedModels: string[] }> { - const initialized = await connection.initialize() + const initialized = await retryTransientAgentFailure(() => connection.initialize(), operation) assertStructuredAgentResolutionCurrent(operation) observeAcpForkSupport(spec.agent, initialized) const deletes = Boolean(initialized.agentCapabilities?.sessionCapabilities?.delete) if (!deletes && !listing.sessionFolderArgs) { throw new Error(`${spec.agent} cannot delete a session, so it lists its models only in chats`) } - const started = await connection.start({ cwd: workspace, mcpServers: [] }) + const started = await retryTransientAgentFailure( + () => connection.start({ cwd: workspace, mcpServers: [] }), + operation + ) let stopped = false const cleanup = async (): Promise => { stopped = true