From 062555ce9874a44eb50a74f79ee32ff62fb50fff Mon Sep 17 00:00:00 2001 From: Xiangzhe <32761048+xz-dev@users.noreply.github.com> Date: Thu, 6 Aug 2026 09:41:43 +0800 Subject: [PATCH] feat(admission): add adaptive overload protection for LLM routes (#9262) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Validated in local merge-train (devbox-vm-06-dev002) @ combined-tip (FAST gates — only pre-existing audit.test.ts flake). --- tests/unit/ollama-transform.test.ts | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/unit/ollama-transform.test.ts b/tests/unit/ollama-transform.test.ts index 22cab56898..79e901eb37 100644 --- a/tests/unit/ollama-transform.test.ts +++ b/tests/unit/ollama-transform.test.ts @@ -100,6 +100,50 @@ test("transformToOllama handles string tool_call id normally", async () => { assert.ok(toolCallLine, "Should produce a tool call line"); }); +test("transformToOllama passes through non-ok shared responses without rewriting status or body", async () => { + const errorBody = { + error: { + message: "Request too large for current capacity", + type: "server_error", + code: "admission_oversized", + }, + }; + const upstream = new Response(JSON.stringify(errorBody), { + status: 503, + headers: { + "Content-Type": "application/json", + "Retry-After": "1", + }, + }); + + const result = transformToOllama(upstream, "llama3.2"); + assert.equal(result.status, 503); + assert.equal(result.headers.get("Retry-After"), "1"); + assert.match(String(result.headers.get("Content-Type") || ""), /application\/json/i); + + const payload = await result.json(); + assert.equal(payload.error?.code, "admission_oversized"); + assert.equal(payload.error?.type, "server_error"); + assert.equal(payload.error?.message, "Request too large for current capacity"); +}); + +test("transformToOllama leaves successful non-SSE responses untouched", async () => { + const body = { choices: [{ message: { role: "assistant", content: "hello" } }] }; + const upstream = new Response(JSON.stringify(body), { + status: 200, + headers: { + "Content-Type": "application/json", + "X-Sentinel": "preserved", + }, + }); + + const result = transformToOllama(upstream, "llama3.2"); + assert.equal(result, upstream); + assert.equal(result.status, 200); + assert.equal(result.headers.get("X-Sentinel"), "preserved"); + assert.deepEqual(await result.json(), body); +}); + test("transformToOllama emits reasoning aliases as native thinking", async () => { const inputSSE = [ `data: ${JSON.stringify({