Skip to content

Commit 63721f5

Browse files
committed
fix(opencode): bound the event subscription so a stalled server cannot hang a refresh
1 parent e331a86 commit 63721f5

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎packages/provider-opencode/src/server/driver.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,57 @@ it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
316316
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
317317
);
318318

319+
it.effect("gives up on an event endpoint that never answers", () =>
320+
Effect.gen(function* () {
321+
const http = HttpClient.make((request) => {
322+
const url = new URL(request.url);
323+
const location = {
324+
directory: url.searchParams.get("location[directory]") ?? "/work",
325+
};
326+
const json = (body: unknown, status = 200) =>
327+
HttpClientResponse.fromWeb(
328+
request,
329+
new Response(JSON.stringify(body), {
330+
status,
331+
headers: { "content-type": "application/json" },
332+
}),
333+
);
334+
if (url.pathname === "/api/info") {
335+
return Effect.succeed(
336+
json({ version: "2.0.18", pid: 2393556, urls: [], paths: { tmp: "/tmp/opencode" } }),
337+
);
338+
}
339+
if (url.pathname === "/api/event") {
340+
// Accepts the request and then stalls before sending response headers,
341+
// so the subscription never yields a stream.
342+
return Effect.never;
343+
}
344+
if (url.pathname === "/api/model") {
345+
return Effect.succeed(json({ location, data: [] }));
346+
}
347+
if (url.pathname === "/api/provider") {
348+
return Effect.succeed(json({ location, data: [] }));
349+
}
350+
return Effect.succeed(json({ _tag: "NotFoundError", message: "not recorded" }, 404));
351+
});
352+
const instance = yield* create(
353+
{ serverUrl: "http://127.0.0.1:4096", serverPassword: "secret" },
354+
http,
355+
);
356+
yield* Effect.yieldNow;
357+
const refresh = yield* instance.snapshot.refresh.pipe(Effect.forkChild);
358+
yield* Effect.yieldNow;
359+
yield* TestClock.adjust("40 seconds");
360+
361+
const unsettled = yield* Fiber.join(refresh);
362+
assert.strictEqual(unsettled.status, "warning");
363+
assert.strictEqual(
364+
unsettled.message,
365+
"OpenCode 2 is running, but it did not list any models yet.",
366+
);
367+
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
368+
);
369+
319370
it.effect("lists an OpenCode 2 workspace's own skills and commands from its server", () =>
320371
Effect.gen(function* () {
321372
serverStarts.length = 0;

‎packages/provider-opencode/src/server/driver.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import * as FileSystem from "effect/FileSystem";
88
import * as Path from "effect/Path";
99
import * as Schema from "effect/Schema";
1010
import * as Stream from "effect/Stream";
11+
import * as Sse from "effect/encoding/Sse";
1112
import * as HttpClient from "effect/http/HttpClient";
13+
import * as HttpClientError from "effect/http/HttpClientError";
1214
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1315

1416
import * as OpenCode2TextGeneration from "./v2/textGeneration.ts";
@@ -69,6 +71,13 @@ const decodeOpenCodeSettings = Schema.decodeSync(OpenCodeSettings);
6971

7072
const DRIVER_KIND = ProviderDriverKind.make("opencode");
7173

74+
/**
75+
* How long to wait for `/api/event` to hand back a stream. A server that accepts
76+
* the connection without answering would otherwise hold a provider refresh open
77+
* with no deadline at all.
78+
*/
79+
const OPENCODE_2_EVENT_SUBSCRIBE_TIMEOUT = "10 seconds";
80+
7281
function isOpenCodeNativeCommandPath(commandPath: string): boolean {
7382
const normalized = normalizeCommandPath(commandPath);
7483
return (
@@ -297,7 +306,23 @@ export const OpenCodeDriver: ProviderDriver<
297306
let readyOpenCode2ServerUrl: string | undefined;
298307
const loadOpenCode2Models = openCode2Server.withConnection(({ client, events, url }) =>
299308
Effect.gen(function* () {
300-
const eventStream = yield* events.pipe(Effect.option);
309+
// Opening `/api/event` is a request the server can stall on before it sends
310+
// response headers, so it needs its own deadline. Otherwise no catalog
311+
// deadline can start and a refresh waits indefinitely.
312+
const eventStream: Option.Option<
313+
Stream.Stream<
314+
OpenCode2Client.OpenCode2StreamEvent,
315+
| HttpClientError.HttpClientError
316+
| OpenCode2Client.OpenCode2SilentStreamError
317+
| Sse.Retry
318+
| Sse.SseError
319+
>
320+
> = yield* events.pipe(
321+
Effect.option,
322+
Effect.timeoutOption(OPENCODE_2_EVENT_SUBSCRIBE_TIMEOUT),
323+
Effect.map(Option.flatten),
324+
Effect.orElseSucceed(() => Option.none()),
325+
);
301326
const runLoader = (readiness: OpenCode2CatalogReadiness) =>
302327
makeOpenCode2ModelLoader(
303328
loadOpenCode2Catalog(client, host.paths.cwd),

0 commit comments

Comments
 (0)