Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/provider-opencode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@effect/vitest": "catalog:",
"@t3tools/provider-testing": "workspace:*",
"@types/node": "catalog:",
"undici": "catalog:",
"vite-plus": "catalog:"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// @effect-diagnostics nodeBuiltinImport:off globalFetch:off - Exercises undici response timeouts against a real local server.
import * as NodeHttp from "node:http";

import * as Undici from "undici";
import { describe, expect, it } from "vite-plus/test";

import { fetchWithoutResponseTimeouts } from "./OpenCodeRuntime.ts";

async function heldServer() {
const held = new Map<string, NodeHttp.ServerResponse>();
const waiters = new Map<string, () => void>();
const server = NodeHttp.createServer((request, response) => {
held.set(request.url!, response);
waiters.get(request.url!)?.();
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const address = server.address() as { port: number };
return {
url: (path: string) => `http://127.0.0.1:${address.port}${path}`,
received: (path: string) =>
held.has(path)
? Promise.resolve()
: new Promise<void>((resolve) => waiters.set(path, resolve)),
reply: (path: string) => held.get(path)!.end("true"),
[Symbol.asyncDispose]: () =>
new Promise<void>((resolve) => {
server.closeAllConnections();
server.close(() => resolve());
}),
};
}

function withGlobalDispatcher(dispatcher: Undici.Dispatcher) {
const previous = Undici.getGlobalDispatcher();
Undici.setGlobalDispatcher(dispatcher);
return {
[Symbol.asyncDispose]: async () => {
Undici.setGlobalDispatcher(previous);
await dispatcher.close();
},
};
}

describe("fetchWithoutResponseTimeouts", () => {
it("outlives the dispatcher's header timeout and still aborts", async () => {
await using _dispatcher = withGlobalDispatcher(new Undici.Agent({ headersTimeout: 10 }));
await using server = await heldServer();

const command = fetchWithoutResponseTimeouts(
new Request(server.url("/command"), { method: "POST", body: "{}" }),
);
await server.received("/command");

// Starts after the command, so the command's header timer would have fired first.
const probe = fetch(server.url("/probe"));
await server.received("/probe");
const probeError = await probe.then(
() => undefined,
(error: unknown) => error,
);
expect((probeError as Error).cause).toMatchObject({ code: "UND_ERR_HEADERS_TIMEOUT" });

server.reply("/command");
expect(await (await command).text()).toBe("true");

const controller = new AbortController();
const cancelled = fetchWithoutResponseTimeouts(
new Request(server.url("/cancelled"), {
method: "POST",
body: "{}",
signal: controller.signal,
}),
);
await server.received("/cancelled");
controller.abort();
await expect(cancelled).rejects.toMatchObject({ name: "AbortError" });
});
});
26 changes: 26 additions & 0 deletions packages/provider-opencode/src/server/OpenCodeRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ export const loadOpenCodeCommands = (client: OpencodeClient) =>
),
);

// undici's Dispatcher: built-in fetch accepts one through the non-standard `dispatcher` init field.
interface FetchDispatcher {
readonly dispatch: (options: object, handler: unknown) => boolean;
}

// Built-in fetch on undici 6/7 (Node 22/24) reads the v1 global; undici 8 reads v2 and keeps v1 as a wrapper.
const builtinFetchDispatcherKey = Symbol.for(
Number.parseInt(process.versions.undici ?? "", 10) >= 8
? "undici.globalDispatcher.2"
: "undici.globalDispatcher.1",
);

// Reuses the process dispatcher (pools, env proxy) and only drops undici's 300s response timeouts.
const untimedFetchDispatcher: FetchDispatcher = {
dispatch: (options, handler) =>
(Reflect.get(globalThis, builtinFetchDispatcherKey) as FetchDispatcher).dispatch(
{ ...options, headersTimeout: 0, bodyTimeout: 0 },
handler,
),
};

/** OpenCode answers `session.command` only when the whole run ends; AbortSignal still cancels it. */
export const fetchWithoutResponseTimeouts: typeof fetch = (input, init) =>
// @effect-diagnostics-next-line globalFetch:off - The OpenCode SDK takes a fetch implementation.
fetch(input, { ...init, dispatcher: untimedFetchDispatcher } as RequestInit);

export interface ParsedOpenCodeModelSlug {
readonly providerID: string;
readonly modelID: string;
Expand Down
13 changes: 10 additions & 3 deletions packages/provider-opencode/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2986,6 +2986,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
const request = yield* sdkCall("session.command", commandPayload, (signal) =>
client.session.command(commandPayload, {
signal: AbortSignal.any([signal, abortController.signal]),
fetch: OpenCodeRuntime.fetchWithoutResponseTimeouts,
}),
).pipe(
Effect.asVoid,
Expand All @@ -2994,9 +2995,15 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
turn.finalized ||
turn.admissionGeneration !== generation
? Effect.void
: finalizeTurn(state, turn, "failed", {
failure: makeProviderFailure({ cause, class: "provider_error" }),
}),
: Effect.logWarning("OpenCode command request failed.", {
errorTag: cause._tag,
}).pipe(
Effect.andThen(
finalizeTurn(state, turn, "failed", {
failure: makeProviderFailure({ cause, class: "provider_error" }),
}),
),
),
),
Effect.ensuring(
Effect.sync(() => {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading