From b09b17e6ee91c46e8da6f1a4593937b35946195a Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 05:01:50 +0000 Subject: [PATCH] fix(opencode): fail the turn when a shared OpenCode service stops (#10187) When Paseo used an OpenCode v2 service it did not start (a discovered `opencode serve --service`, or a configured server URL), nothing reported that service going away. The connection's `exited` signal resolved only when Paseo itself stopped using it. A turn that was running when the service died was never failed, the event stream retried the dead port every 100 ms, and Stop, send and reload were refused with "active run cancellation was not acknowledged". The shared connection now treats a refused connection as the service exiting, the same signal an owned server's process exit gives. The session fails the turn, stops its stream, and reconnects on the next operation. Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../opencode/v2/external-runtime.test.ts | 27 +++++++++++++++++++ .../providers/opencode/v2/shared-service.ts | 25 ++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/server/src/server/agent/providers/opencode/v2/external-runtime.test.ts b/packages/server/src/server/agent/providers/opencode/v2/external-runtime.test.ts index f4f39f4ae..c197edbfc 100644 --- a/packages/server/src/server/agent/providers/opencode/v2/external-runtime.test.ts +++ b/packages/server/src/server/agent/providers/opencode/v2/external-runtime.test.ts @@ -101,6 +101,33 @@ describe("V2Runtime against an external server", () => { expect(requests).toHaveLength(1); }); + test("reports the external server as exited once it stops accepting connections", async () => { + server = createServer((request, response) => { + if (request.url !== "/api/info") { + response.writeHead(404); + response.end(); + return; + } + response.writeHead(200, { "content-type": "application/json" }); + response.end(JSON.stringify({ version: "2.0.10", pid: 1, urls: [], paths: { tmp: "/tmp" } })); + }); + const port = await listen(server); + const runtime = new V2Runtime({ + logger: createTestLogger(), + externalServer: { url: `http://127.0.0.1:${port}` }, + }); + const connection = await runtime.acquire(); + const exited = connection.exited.then((error) => error.message); + + await close(server); + server = undefined; + await expect(connection.client.server.info()).rejects.toThrow(); + + await expect(exited).resolves.toContain(`OpenCode server at http://127.0.0.1:${port} stopped`); + await connection.release(); + await runtime.shutdown(); + }); + test("selects the external server from provider environment", async () => { const paths: Array = []; server = createServer((request, response) => { diff --git a/packages/server/src/server/agent/providers/opencode/v2/shared-service.ts b/packages/server/src/server/agent/providers/opencode/v2/shared-service.ts index 26fcbb7d2..8769e284c 100644 --- a/packages/server/src/server/agent/providers/opencode/v2/shared-service.ts +++ b/packages/server/src/server/agent/providers/opencode/v2/shared-service.ts @@ -53,7 +53,14 @@ export async function connectSharedService( const signal = init?.signal ? AbortSignal.any([init.signal, lifetime.signal]) : lifetime.signal; - const response = await fetch(request, { ...init, signal }); + let response: Response; + try { + response = await fetch(request, { ...init, signal }); + } catch (error) { + // Paseo does not own this process, so a refused connection is its only exit signal. + if (isConnectionRefused(error)) markExited(error); + throw error; + } const html = response.headers.get("content-type")?.includes("text/html") ?? false; if (!response.ok || html) { const requestUrl = request instanceof Request ? request.url : String(request); @@ -62,6 +69,15 @@ export async function connectSharedService( return response; }, }); + const markExited = (cause: unknown) => { + if (lifetime.signal.aborted) return; + const error = new Error(`OpenCode server at ${server.url} stopped accepting connections`, { + cause, + }); + logger.warn({ url: server.url }, "OpenCode v2 server stopped accepting connections"); + lifetime.abort(error); + resolveExited(error); + }; let info: Awaited>; try { info = await client.server.info({ @@ -117,6 +133,13 @@ export async function connectSharedService( return generation; } +function isConnectionRefused(error: unknown): boolean { + const cause = error instanceof Error ? error.cause : undefined; + return Boolean( + cause && typeof cause === "object" && "code" in cause && cause.code === "ECONNREFUSED", + ); +} + function validateBridgeAddress( selection: Extract, bridge: OpenCodeBridge | undefined,