fix(app): keep API paths working for a server under a path prefix

Attachment uploads resolved /api/... against the origin and dropped the
prefix, and the request queue matched exact /api/... paths, so the event
stream took a queue slot and priorities stopped applying.
This commit is contained in:
Brendan 2026-11-18 06:30:09 +00:00 • committed by opencode-agent[bot]
parent 9854644b7f
commit fd40bb220e
6 changed files with 58 additions and 5 deletions

View file

@ -28,7 +28,8 @@ export function useAttachmentDestination(controls: Accessor<ComposerControls>) {
const info = await sdk.api.server.info({ signal })
// One directory per upload keeps the original filename without collisions; the server
// normalizes the separators and returns the resolved path.
const url = new URL("/api/experimental/fs/write", sdk.http.url)
// Relative to the server URL, so a server behind a path prefix keeps it.
const url = new URL("api/experimental/fs/write", sdk.http.url.endsWith("/") ? sdk.http.url : `${sdk.http.url}/`)
url.searchParams.set("location[directory]", location().directory)
url.searchParams.set("path", `${info.paths.tmp}/uploads/${uuid()}/${file.name}`)

View file

@ -229,7 +229,10 @@ export function createServerTransport(input: {
http: ServerConnection.HttpBase
fetch?: typeof globalThis.fetch
}): ServerTransport {
const queue = createRequestQueue({ fetch: input.fetch ?? globalThis.fetch })
const queue = createRequestQueue({
fetch: input.fetch ?? globalThis.fetch,
prefix: () => serverPathPrefix(state.current.http.url),
})
// Keeps the endpoint as plain values: a plugin's endpoint can be a store proxy that already reads the next password,
// and the client built here keeps the one it started with.
@ -290,3 +293,10 @@ function createDirSdkContext(directory: string, serverSDK: ServerSDKBase): Locat
event: serverSDK.event.location(directory),
}
}
/** The path a server URL sits under, such as `/opencode` for `https://host/opencode/`; empty at the root. */
function serverPathPrefix(url: string) {
if (!URL.canParse(url)) return ""
return new URL(url).pathname.replace(/\/+$/, "")
}

View file

@ -1,5 +1,6 @@
import { describe, expect, test } from "bun:test"
import {
classifyRequest,
createRequestQueueState,
selectInflightCount,
selectQueuedCount,
@ -125,3 +126,26 @@ describe("transitionRequestQueue", () => {
])
})
})
describe("classifyRequest", () => {
test("classifies API paths under a server path prefix like those at the root", () => {
const paths = [
["GET", "/api/event"],
["GET", "/api/session"],
["GET", "/api/session/ses_1/message"],
["GET", "/api/vcs/diff"],
["POST", "/api/worktree"],
["GET", "/api/project"],
] as const
for (const [method, path] of paths)
expect(classifyRequest(method, new URL(`http://server/opencode${path}`), "/opencode")).toEqual(
classifyRequest(method, new URL(`http://server${path}`)),
)
})
test("leaves paths outside the prefix alone", () => {
expect(classifyRequest("GET", new URL("http://server/api/event"), "/opencode").bypass).toBe(true)
expect(classifyRequest("GET", new URL("http://server/opencode-other/api/event"), "/opencode").bypass).toBe(false)
})
})

View file

@ -149,9 +149,11 @@ export function isRequestInflight(state: RequestQueueState, id: number) {
/**
* The event stream bypasses the queue. Writes go first. Session lists and the first transcript page unblock
* navigation, so they go ahead of location reads. Older pages and filtered indexes keep background priority.
* `prefix` is the server URL's path, such as `/opencode`, when the server sits under one.
*/
export function classifyRequest(method: string, url: URL): RequestClassification {
const pathname = url.pathname
export function classifyRequest(method: string, url: URL, prefix = ""): RequestClassification {
// A server behind a path prefix serves its API under it; classify the path the server sees.
const pathname = prefix && url.pathname.startsWith(`${prefix}/`) ? url.pathname.slice(prefix.length) : url.pathname
// The event stream is long-lived; never count it against the request budget.
if (pathname === "/api/event") return bypass

View file

@ -8,6 +8,7 @@ function setup(input?: {
stallMs?: number
headersTimeoutMs?: number
setupHeadersTimeoutMs?: number
prefix?: string
}) {
const pending: Array<{ url: string; signal: AbortSignal; resolve: () => void }> = []
const logs: Array<{ message: string; data: RequestStallSnapshot }> = []
@ -19,6 +20,7 @@ function setup(input?: {
stallMs: input?.stallMs,
headersTimeoutMs: input?.headersTimeoutMs,
setupHeadersTimeoutMs: input?.setupHeadersTimeoutMs,
prefix: () => input?.prefix ?? "",
now: () => clock,
log: (message, data) => logs.push({ message, data }),
fetch: Object.assign(
@ -159,6 +161,18 @@ describe("createRequestQueue", () => {
expect(input.queue.inflight()).toBe(1)
})
test("never counts the event stream of a server under a path prefix against the budget", async () => {
const input = setup({ limit: 1, prefix: "/opencode" })
void input.queue.fetch("http://server/opencode/api/session")
void input.queue.fetch("http://server/opencode/api/event")
await input.settle()
expect(input.pending.map((item) => new URL(item.url).pathname).toSorted()).toEqual([
"/opencode/api/event",
"/opencode/api/session",
])
expect(input.queue.inflight()).toBe(1)
})
test("a queued request leaves without being sent as soon as its caller aborts, while every slot stays stuck", async () => {
const input = setup({ limit: 1, stallMs: 5 })
const controller = new AbortController()

View file

@ -26,6 +26,8 @@ const setupRequestHeadersTimeoutMs = 10 * 60_000
export function createRequestQueue(input: {
fetch: typeof globalThis.fetch
/** The server URL's path, such as `/opencode`, when the server sits under a prefix. */
prefix?: () => string
limit?: number
slowLimit?: number
stallMs?: number
@ -106,7 +108,7 @@ export function createRequestQueue(input: {
const fetch: typeof globalThis.fetch = Object.assign(
async (resource: RequestInfo | URL, init?: RequestInit) => {
const request = new Request(resource, init)
const kind = classifyRequest(request.method, new URL(request.url))
const kind = classifyRequest(request.method, new URL(request.url), input.prefix?.())
if (kind.bypass) return base(request)