fix(opencode): apply each discovery result as it arrives

resolveConfiguredWireRoots awaited Promise.allSettled over config.get()
and provider.list(), so a successful config response that named a custom
provider base was ignored while the provider listing was still pending. A
stalled /provider call therefore left a configured wire bypassing Headroom
indefinitely even though the base was already known.

Resolve the two reads independently: each fulfilled response widens the
wire roots as soon as it lands, and a failed or missing read only costs its
own bases. Startup still never awaits discovery, so a server that is still
coming up cannot block the plugin.

Adds a regression test where config succeeds, provider.list() never
settles, and the configured fetch and Node paths must already route while
a non-endpoint path on the same base still passes through.
This commit is contained in:
Parideboy
2026-11-10 20:07:55 +01:00
parent 98b5ba2aa9
commit 3e84b5f2f5
3 changed files with 103 additions and 21 deletions
+20 -9
View File
@@ -20400,15 +20400,26 @@ function resolveConfiguredWireRoots(client, onResolved) {
if (!client?.config?.get && !client?.provider?.list) {
return;
}
const settled = [client.config?.get?.(), client.provider?.list?.()];
void Promise.allSettled(settled).then(([configResult, providerResult]) => {
const config2 = configResult?.status === "fulfilled" ? configResult.value : void 0;
const providers = providerResult?.status === "fulfilled" ? providerResult.value : void 0;
onResolved(
collectBaseUrls(config2?.data ?? config2, providers?.data ?? providers)
);
}).catch(() => {
});
const apply = (read, pick2) => {
if (!read) {
return;
}
void Promise.resolve().then(read).then((value) => {
const urls = pick2(value);
if (urls.length > 0) {
onResolved(urls);
}
}).catch(() => {
});
};
apply(
client.config?.get?.bind(client.config),
(config2) => collectBaseUrls(config2?.data ?? config2, void 0)
);
apply(
client.provider?.list?.bind(client.provider),
(providers) => collectBaseUrls(void 0, providers?.data ?? providers)
);
}
function normalizeProxyUrl2(url2) {
return trimTrailingSlashes(url2);
+52
View File
@@ -1,3 +1,5 @@
import http from "node:http";
import https from "node:https";
import { afterEach, describe, expect, it, vi } from "vitest";
import HeadroomOpenCodePlugin, { HeadroomPlugin, headroomSetup } from "./plugin.js";
@@ -159,6 +161,56 @@ describe("HeadroomPlugin", () => {
}
});
it("applies a config result without waiting for a pending provider listing (#4747)", async () => {
const fetchMock = vi.fn(async (..._args: unknown[]) => new Response("ok"));
vi.stubGlobal("fetch", fetchMock);
const fakeRequest = { on: vi.fn(), end: vi.fn(), destroy: vi.fn() };
const httpSpy = vi.spyOn(http, "request").mockImplementation(() => fakeRequest as never);
const httpsSpy = vi.spyOn(https, "request").mockImplementation(() => fakeRequest as never);
// The provider listing never settles: a stalled server must not hold back a
// base the config already reported.
const client = {
config: {
get: async () => ({
data: {
provider: {
acme: { options: { baseURL: "https://gateway.invalid/tenant/acme" } },
},
},
}),
},
provider: { list: () => new Promise<never>(() => {}) },
};
const plugin = await HeadroomPlugin(
{ ...(pluginInput() as Record<string, unknown>), client } as never,
{ proxyUrl: "http://127.0.0.1:8787" },
);
try {
// The read is still not awaited by the plugin, but nothing else blocks it.
await vi.waitFor(() => {
expect(transportWireRoots()).toContain("https://gateway.invalid/tenant/acme");
});
await fetch("https://gateway.invalid/tenant/acme/chat/completions", { method: "POST" });
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(String(fetchMock.mock.calls[0][0])).toBe("http://127.0.0.1:8787/v1/chat/completions");
https.request("https://gateway.invalid/tenant/acme/chat/completions", { method: "POST" });
expect(httpSpy).toHaveBeenCalledTimes(1);
expect(httpsSpy).not.toHaveBeenCalled();
// Still a root, not a blanket route for the host.
await fetch("https://gateway.invalid/tenant/acme/files/1", { method: "POST" });
expect(String(fetchMock.mock.calls[1][0])).toBe("https://gateway.invalid/tenant/acme/files/1");
} finally {
await plugin.dispose?.();
httpSpy.mockRestore();
httpsSpy.mockRestore();
}
});
it("treats an empty HEADROOM_PROXY_URL as unset and falls through to HEADROOM_BASE_URL", async () => {
vi.stubEnv("HEADROOM_PROXY_URL", "");
vi.stubEnv("HEADROOM_BASE_URL", "http://127.0.0.1:9999");
+31 -12
View File
@@ -70,6 +70,11 @@ function collectBaseUrls(config: unknown, providers: unknown): string[] {
// loading it, and a server that is not serving yet must not disable routing.
// So install first with whatever is known, then widen the rule when the answer
// arrives. Requests that land in between fall back to the path heuristics.
//
// Each endpoint is applied on its own: keeping a base back because the other
// request is still pending would leave a configured wire bypassing Headroom for
// as long as that request stalls (#4747 review). A failed or missing read only
// costs its own bases, never the other's.
function resolveConfiguredWireRoots(
client: { config?: { get?: () => Promise<unknown> }; provider?: { list?: () => Promise<unknown> } } | undefined,
onResolved: (urls: string[]) => void,
@@ -77,18 +82,32 @@ function resolveConfiguredWireRoots(
if (!client?.config?.get && !client?.provider?.list) {
return;
}
const settled = [client.config?.get?.(), client.provider?.list?.()];
void Promise.allSettled(settled)
.then(([configResult, providerResult]) => {
const config = configResult?.status === "fulfilled" ? configResult.value : undefined;
const providers = providerResult?.status === "fulfilled" ? providerResult.value : undefined;
onResolved(
collectBaseUrls((config as { data?: unknown })?.data ?? config, (providers as { data?: unknown })?.data ?? providers),
);
})
.catch(() => {
// Routing stays on the path heuristics; not worth failing a session for.
});
const apply = (read: (() => Promise<unknown>) | undefined, pick: (value: unknown) => string[]) => {
if (!read) {
return;
}
// Promise.resolve() turns a synchronous throw into a rejection, and the
// catch keeps a failing read on the path heuristics instead of surfacing.
void Promise.resolve()
.then(read)
.then((value) => {
const urls = pick(value);
if (urls.length > 0) {
onResolved(urls);
}
})
.catch(() => {
// Routing stays on the path heuristics; not worth failing a session for.
});
};
apply(client.config?.get?.bind(client.config), (config) =>
collectBaseUrls((config as { data?: unknown })?.data ?? config, undefined),
);
apply(client.provider?.list?.bind(client.provider), (providers) =>
collectBaseUrls(undefined, (providers as { data?: unknown })?.data ?? providers),
);
}
function normalizeProxyUrl(url: string): string {