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 <noreply@anthropic.com>
This commit is contained in:
1 parent
f5d27b7901
commit
b09b17e6ee
2 files changed
+51
-1
No files matched your search
@@ -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<string | undefined> = [];
|
||||
server = createServer((request, response) => {
|
||||
|
||||
@@ -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<ReturnType<typeof client.server.info>>;
|
||||
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<OpenCodeServiceSelection, { kind: "explicit" | "discovered" }>,
|
||||
bridge: OpenCodeBridge | undefined,
|
||||
|
||||
Reference in new issue
Block a user