fix(opencode): only route POST requests to LLM endpoint paths (#4464)

## Description

The opencode transport routes any request whose path ends in `/messages`
or `/responses` to the Headroom proxy, so a `GET
https://chat.example.com/api/v2/messages` or `GET
.../forms/123/responses` is sent to the proxy (the latter also rewritten
to `/v1/responses`). Every endpoint `isLlmEndpointPath` matches
(`/chat/completions`, `/responses`, `/messages`, `:generateContent`,
`:streamGenerateContent`) is a POST, so this adds a method check: only
POST requests are routed. A missing method counts as GET, matching
`fetch` and `node:http` defaults.

This is a narrow partial fix, not one of the four options in the issue:
it keeps the bare-suffix rule so prefixed gateways still work, and needs
no config. It does **not** cover a non-LLM *POST* to a `/messages` or
`/responses` path; that remainder still needs one of the issue's options
(or `excludeHosts`). Happy to change direction if you prefer a different
approach.

Refs #4417

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)

## Changes Made

- `plugins/opencode/src/transport.ts`: `shouldRoute` takes the request
method; fetch reads it from `init.method` or the `Request`, Node
`http(s).request` from `options.method`. Non-POST requests pass through
untouched.
- `plugins/opencode/src/transport.test.ts`: two tests (fetch and
`https.request`) for GET/DELETE/no-method requests to `/messages` and
`/responses` suffix paths, plus a lowercase `post` that must still
route.

## Testing

- [ ] Unit tests pass (`pytest`) — N/A, no Python changed, not run
- [ ] Linting passes (`ruff check .`) — N/A, not run
- [ ] Type checking passes (`mypy headroom`) — N/A, not run
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
# plugins/opencode, before the fix (new tests only)
Tests  2 failed | 36 passed (38)    # src/transport.test.ts
# after the fix
$ npx vitest run
 Test Files  3 passed (3)
      Tests  60 passed (60)
$ npm run typecheck   # tsc --noEmit, no errors
$ npm run build       # ok
```

## Real Behavior Proof

- Environment: Linux, Node v20.19.2, plugin built with `npm run build`
from origin/main 3fef15d3 and from this branch.
- Exact command / steps: a small script installs
`installHeadroomTransport` from `dist/index.js` against a local fake
proxy, then calls `fetch` for `GET
http://chat.example.invalid/api/v2/messages`, `GET
http://forms.example.invalid/forms/123/responses`, `POST
http://api.example.invalid/v1/messages`.
- Observed result:
```text
### BEFORE (origin/main build)
proxy saw: ["GET /api/v2/messages","GET /v1/responses","POST /v1/messages"]
### AFTER (patched build)
GET http://chat.example.invalid/api/v2/messages -> not proxied, direct fetch failed: UND_ERR_CONNECT_TIMEOUT
GET http://forms.example.invalid/forms/123/responses -> not proxied, direct fetch failed: UND_ERR_CONNECT_TIMEOUT
POST http://api.example.invalid/v1/messages -> 200 proxy
proxy saw: ["POST /v1/messages"]
```
(The direct failures are expected: `.invalid` hosts do not resolve; the
point is the proxy no longer receives them.)
- Not tested: a real opencode session against a real provider;
macOS/Windows; non-LLM POSTs to suffix paths (still routed, see
Description).

## Runtime Rollout Safety

- Rollout-managed feature(s): none
- Minimum rollout channel: N/A
- Stable/default behavior changed: yes, non-POST requests to LLM-suffix
paths are no longer proxied. LLM calls are POST, so they are unaffected.
- Kill switch / disable path: none needed; revert the commit.
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation — N/A
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`

## Additional Notes

Drafted with AI assistance; the tests above were run locally.

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4464"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

---------

Co-authored-by: liziye627-design <232619260+liziye627-design@users.noreply.github.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
CyberSprout
2026-11-03 23:18:28 -06:00
committed by GitHub
co-authored by liziye627-design JD Davis
parent 4b788fd04d
commit 336d462253
4 changed files with 63 additions and 12 deletions
+8 -4
View File
@@ -19955,8 +19955,11 @@ function isRoutableUpstream(url2, proxy, excludeHosts) {
}
return true;
}
function shouldRoute(url2, proxy, excludeHosts) {
return isRoutableUpstream(url2, proxy, excludeHosts) && isLlmEndpointPath(url2.pathname);
function isLlmMethod(method) {
return typeof method === "string" && method.toUpperCase() === "POST";
}
function shouldRoute(url2, proxy, excludeHosts, method) {
return isLlmMethod(method) && isRoutableUpstream(url2, proxy, excludeHosts) && isLlmEndpointPath(url2.pathname);
}
function modelBaseRoutesThroughProxy(baseUrl, proxyUrl, excludeHosts = []) {
try {
@@ -20021,7 +20024,8 @@ function mergeFetchHeaders(input2, init, upstream, originalPath = void 0, projec
}
function withRoutedFetchInput(input2, init, proxy, project, excludeHosts, sessionToken) {
const upstream = requestUrl(input2);
if (!shouldRoute(upstream, proxy, excludeHosts)) {
const method = init?.method ?? (input2 instanceof Request ? input2.method : "GET");
if (!shouldRoute(upstream, proxy, excludeHosts, method)) {
return [input2, init];
}
const { url: nextUrl, originalPath } = routedUrlForOpenCode(upstream, proxy);
@@ -20093,7 +20097,7 @@ function headersForNodeRequest(options, upstream, originalPath, project, session
return result;
}
function routedNodeOptions(parts, proxy, project, excludeHosts, sessionToken) {
if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts)) {
if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts, parts.options.method ?? "GET")) {
return void 0;
}
const { url: nextUrl, originalPath } = routedUrlForOpenCode(parts.url, proxy);
@@ -170,8 +170,11 @@ function isRoutableUpstream(url, proxy, excludeHosts) {
}
return true;
}
function shouldRoute(url, proxy, excludeHosts) {
return isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);
function isLlmMethod(method) {
return typeof method === "string" && method.toUpperCase() === "POST";
}
function shouldRoute(url, proxy, excludeHosts, method) {
return isLlmMethod(method) && isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);
}
function routedUrl(upstream, proxy) {
return new URL(`${upstream.pathname}${upstream.search}`, proxy.origin);
@@ -229,7 +232,8 @@ function mergeFetchHeaders(input, init, upstream, originalPath = void 0, project
}
function withRoutedFetchInput(input, init, proxy, project, excludeHosts, sessionToken) {
const upstream = requestUrl(input);
if (!shouldRoute(upstream, proxy, excludeHosts)) {
const method = init?.method ?? (input instanceof Request ? input.method : "GET");
if (!shouldRoute(upstream, proxy, excludeHosts, method)) {
return [input, init];
}
const { url: nextUrl, originalPath } = routedUrlForOpenCode(upstream, proxy);
@@ -301,7 +305,7 @@ function headersForNodeRequest(options, upstream, originalPath, project, session
return result;
}
function routedNodeOptions(parts, proxy, project, excludeHosts, sessionToken) {
if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts)) {
if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts, parts.options.method ?? "GET")) {
return void 0;
}
const { url: nextUrl, originalPath } = routedUrlForOpenCode(parts.url, proxy);
+34
View File
@@ -233,6 +233,40 @@ describe("Headroom OpenCode transport", () => {
globalThis.fetch = originalFetch;
});
it("passes non-POST fetches to /messages and /responses suffix paths through unchanged (#4417)", async () => {
const originalFetch = globalThis.fetch;
const fetchMock = vi.fn(async (..._args: FetchCall) => new Response("ok"));
globalThis.fetch = fetchMock as unknown as typeof fetch;
installHeadroomTransport({ proxyUrl: "http://127.0.0.1:8787/v1" });
await fetch("https://chat.example.com/api/v2/messages");
await fetch("https://forms.example.com/forms/123/responses", { method: "GET" });
await fetch(new Request("https://chat.example.com/api/v2/messages", { method: "DELETE" }));
await fetch("https://api.anthropic.com/v1/messages", { method: "post" });
expect(fetchMock).toHaveBeenCalledTimes(4);
expect(fetchMock.mock.calls[0][0]).toBe("https://chat.example.com/api/v2/messages");
expect(fetchMock.mock.calls[1][0]).toBe("https://forms.example.com/forms/123/responses");
expect(new Headers(fetchMock.mock.calls[1][1]?.headers).get("x-headroom-original-path")).toBeNull();
expect((fetchMock.mock.calls[2][0] as Request).url).toBe("https://chat.example.com/api/v2/messages");
expect(fetchMock.mock.calls[3][0]).toEqual(new URL("http://127.0.0.1:8787/v1/messages"));
globalThis.fetch = originalFetch;
});
it("leaves non-POST Node https.request calls to LLM-suffix paths alone (#4417)", () => {
installHeadroomTransport({ proxyUrl: "http://127.0.0.1:8787/v1" });
const req = https.request("https://chat.example.com/api/v2/messages", { method: "GET" });
req.on("error", () => {});
expect(req.host).toBe("chat.example.com");
req.destroy();
const req2 = https.request("https://forms.example.com/forms/123/responses");
req2.on("error", () => {});
expect(req2.host).toBe("forms.example.com");
req2.destroy();
});
it("routes Gemini native :generateContent and :streamGenerateContent through the proxy", async () => {
const originalFetch = globalThis.fetch;
const fetchMock = vi.fn(async (..._args: FetchCall) => new Response("ok"));
+13 -4
View File
@@ -280,8 +280,16 @@ function isRoutableUpstream(url: URL, proxy: URL, excludeHosts: string[]): boole
return true;
}
function shouldRoute(url: URL, proxy: URL, excludeHosts: string[]): boolean {
return isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);
// Every LLM endpoint matched above is a POST. A GET/DELETE/... to a path that
// merely ends in /messages or /responses (chat APIs, form responses) is not an
// LLM call and must reach its original URL (#4417). A missing method means
// GET, matching fetch and node:http defaults.
function isLlmMethod(method: unknown): boolean {
return typeof method === "string" && method.toUpperCase() === "POST";
}
function shouldRoute(url: URL, proxy: URL, excludeHosts: string[], method: unknown): boolean {
return isLlmMethod(method) && isRoutableUpstream(url, proxy, excludeHosts) && isLlmEndpointPath(url.pathname);
}
// OpenCode 2.x hands the plugin model base URLs (e.g.
@@ -377,7 +385,8 @@ function withRoutedFetchInput(
sessionToken: string | undefined,
): FetchArgs {
const upstream = requestUrl(input);
if (!shouldRoute(upstream, proxy, excludeHosts)) {
const method = init?.method ?? (input instanceof Request ? input.method : "GET");
if (!shouldRoute(upstream, proxy, excludeHosts, method)) {
return [input, init];
}
@@ -471,7 +480,7 @@ function routedNodeOptions(
excludeHosts: string[],
sessionToken: string | undefined,
): Record<string, unknown> | undefined {
if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts)) {
if (!parts.url || !shouldRoute(parts.url, proxy, excludeHosts, parts.options.method ?? "GET")) {
return undefined;
}