diff --git a/packages/app/src/composer/attachments/destination.ts b/packages/app/src/composer/attachments/destination.ts index 04efe651853..a8431480b90 100644 --- a/packages/app/src/composer/attachments/destination.ts +++ b/packages/app/src/composer/attachments/destination.ts @@ -28,7 +28,8 @@ export function useAttachmentDestination(controls: Accessor) { 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}`) diff --git a/packages/app/src/runtime/server/client.tsx b/packages/app/src/runtime/server/client.tsx index 5f79979a1c9..c28e0417e20 100644 --- a/packages/app/src/runtime/server/client.tsx +++ b/packages/app/src/runtime/server/client.tsx @@ -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(/\/+$/, "") +} diff --git a/packages/app/src/runtime/server/request-queue-machine.test.ts b/packages/app/src/runtime/server/request-queue-machine.test.ts index c6757c22952..51e5002c41d 100644 --- a/packages/app/src/runtime/server/request-queue-machine.test.ts +++ b/packages/app/src/runtime/server/request-queue-machine.test.ts @@ -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) + }) +}) diff --git a/packages/app/src/runtime/server/request-queue-machine.ts b/packages/app/src/runtime/server/request-queue-machine.ts index 6a97096e8e6..6f70da99b0a 100644 --- a/packages/app/src/runtime/server/request-queue-machine.ts +++ b/packages/app/src/runtime/server/request-queue-machine.ts @@ -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 diff --git a/packages/app/src/runtime/server/request-queue.test.ts b/packages/app/src/runtime/server/request-queue.test.ts index 797d9d30990..1474ab14bb4 100644 --- a/packages/app/src/runtime/server/request-queue.test.ts +++ b/packages/app/src/runtime/server/request-queue.test.ts @@ -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() diff --git a/packages/app/src/runtime/server/request-queue.ts b/packages/app/src/runtime/server/request-queue.ts index 37d5c2aebb8..72666f62582 100644 --- a/packages/app/src/runtime/server/request-queue.ts +++ b/packages/app/src/runtime/server/request-queue.ts @@ -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)