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.
This commit is contained in:
Mazurov Anton
2026-11-10 22:10:27 +00:00
committed by Kelvin Amoaba
parent 557d588e69
commit 40894a41f8
@@ -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<T>(
run: () => Promise<T>,
operation?: StructuredAgentResolutionOperation
): Promise<T> {
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<void> {
if (signal?.aborted) {
return
}
await new Promise<void>((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<unknown>) => 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<void> => {
stopped = true