diff --git a/src/server/relay.ts b/src/server/relay.ts index 7e34790cf8..a35582934f 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -2507,13 +2507,20 @@ async function forwardCaptured( // the main conversation (see pipePluginChatWithStrip docs). const p = prepared; const tagLog = (msg: string) => log("info", `[${p.session.id}] ${msg}`); + // #6466: side-lane requests (title-gen / auto-review / single-shot) + // are best-effort utility calls. A degenerate or empty response must + // end the turn as-is — never re-issue (double-bill a utility call) + // and never emit an in-band #870 error that a host like DSH would + // write into the session title. Passing refetch=undefined disables + // the pipe's degenerate gate (retry + #870) and cut retries; the + // non-side-lane non-injected path below keeps the gate unchanged. if (p.protocol === "responses") { await pipePluginResponsesWithStrip( responseBody, res, undefined, tagLog, - makeContinuationRefetch({ + p.sidePassthrough ? undefined : makeContinuationRefetch({ protocol: "responses", body, model: throttleModel ?? p.google?.model, @@ -2538,7 +2545,7 @@ async function forwardCaptured( p.protocol, undefined, tagLog, - makeContinuationRefetch({ + p.sidePassthrough ? undefined : makeContinuationRefetch({ protocol: p.protocol, body, model: throttleModel ?? p.google?.model, diff --git a/tests/issue6466-side-lane-skip-degenerate-gate.test.ts b/tests/issue6466-side-lane-skip-degenerate-gate.test.ts new file mode 100644 index 0000000000..5bd0f24a26 --- /dev/null +++ b/tests/issue6466-side-lane-skip-degenerate-gate.test.ts @@ -0,0 +1,135 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { DSH_TITLE_SYSTEM_PREFIX } from "../src/server/side-request.ts"; + +// #6466: the dsh title-gen sidecar (and every other side-lane request) is a +// best-effort utility call. Before this fix it rode the non-injected streaming +// branch with a LIVE continuation refetch, so the pipe's degenerate-turn gate +// (#732/#821 retry + #870 in-band error) fired for it: a title request that +// came back with no visible text (reasoning-only under effort=max) was +// re-issued (double-billing a utility call) and, on a second empty response, +// the client received an in-band stream error that DSH wrote straight into the +// session title (`❌ [ACP] stream error: the turn degenerated again ...`). +// +// The fix (src/server/relay.ts) passes refetch=undefined for sidePassthrough +// requests, which disables the pipe's degenerate gate AND the cut retries. +// This test pins BOTH directions (gate discipline #3617): +// - side lane: a degenerate response ends as-is — no retry, no #870. +// - non-side-lane non-injected: the gate still fires (retry + #870) — the +// main path is unchanged. +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-6466-side-degen-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; + +const TITLE_SYSTEM = `${DSH_TITLE_SYSTEM_PREFIX} from the supplied human messages.\nReturn only the title on one line.`; +const MAIN_SYSTEM = "MAIN OPERATING SYSTEM"; + +// A degenerate chat-completion SSE turn: the ONLY visible text is a render-tag +// echo that the strip pipe removes (sawStrippedEcho=true, answerChars=0), and +// the turn ends on a clean stop — exactly the shape that trips the +// degenerate-turn gate (clean stop + no usable output + not bare). +const TAG_OPEN = "\x3cacp tokens=\"247\" type=\"text\"\x3e"; +const TAG_CLOSE = "\x3c/acp\x3e"; +function chunk(delta: Record, finishReason: string | null = null) { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: finishReason }] })}\n\n`; +} +const DEGENERATE_SSE = + chunk({ role: "assistant" }) + + chunk({ content: `${TAG_OPEN}m00155${TAG_CLOSE}` }) + + chunk({}, "stop") + + "data: [DONE]\n\n"; + +async function harness() { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + mkdirSync(join(dir, "billion-context", "sessions"), { recursive: true }); + mkdirSync(join(dir, "sessions"), { recursive: true }); + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: false, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + // Count upstream hits so the test can detect the degenerate retry (a + // re-issue is a second hit on the same upstream). + let upstreamHits = 0; + const upstream = await new Promise<{ url: string; close: () => Promise }>((resolve) => { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record; + upstreamHits += 1; + if (body.stream === true) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(DEGENERATE_SSE); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chat_test", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], usage: { prompt_tokens: 10000, completion_tokens: 10, total_tokens: 10010 } })); + } + }); + }); + server.listen(0, "127.0.0.1", () => resolve({ url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: async () => { server.close(); } })); + }); + // injectTool:false keeps the non-side-lane control request on the + // NON-injected streaming branch (the one carrying the degenerate gate) + // rather than the compress-rewriter loop. + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstream.url, routes: { [upstream.url]: { models: { "fake-a": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: false, injectNudge: false, preserveRecentMessages: 1, preserveRecentTokens: 0, minCompressRangeChars: 100 }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + await once(proxy, "listening"); + const origin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const send = async (conversationId: string, body: Record, plugin?: string) => { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId }; + if (plugin !== undefined) { headers["x-bili-plugin"] = plugin; headers["x-bili-plugin-conversation"] = conversationId; } + const response = await fetch(`${origin}/bili/${upstream.url}/v1/chat/completions`, { method: "POST", headers, body: JSON.stringify(body) }); + const text = await response.text(); + assert.equal(response.status, 200, text); + return text; + }; + return { + upstreamHits: () => upstreamHits, + send, + close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); }, + }; +} + +test("#6466: a degenerate dsh title sidecar ends as-is — no retry, no #870 in-band error", async () => { + const h = await harness(); + try { + const history = [{ role: "user", content: "set up the workspace" }, { role: "assistant", content: "done" }]; + const titleBody = { model: "fake-a", max_completion_tokens: 4096, stream: true, messages: [{ role: "system", content: TITLE_SYSTEM }, ...history, { role: "user", content: `[${JSON.stringify(history)}]` }] }; + const text = await h.send("conv-side", titleBody, "dsh"); + assert.equal(h.upstreamHits(), 1, "side lane must not re-issue the request (no degenerate retry)"); + assert.ok(!text.includes("stream_error"), `no in-band stream error for a side lane; got: ${text}`); + assert.ok(!text.includes("degenerated again"), `no #870 escalation for a side lane; got: ${text}`); + assert.ok(!text.includes("[ACP] stream error"), `no [ACP] marker for a side lane; got: ${text}`); + } finally { + await h.close(); + } +}); + +test("#6466 control: a degenerate non-side-lane non-injected turn still retries + escalates (#732/#821/#870)", async () => { + const h = await harness(); + try { + const mainBody = { model: "fake-a", max_completion_tokens: 8192, stream: true, messages: [{ role: "system", content: MAIN_SYSTEM }, { role: "user", content: "do something" }, { role: "assistant", content: "ok" }, { role: "user", content: "now do more" }], tools: [{ type: "function", function: { name: "bash", description: "run a command", parameters: { type: "object", properties: {} } } }] }; + const text = await h.send("conv-main", mainBody); + assert.equal(h.upstreamHits(), 2, "non-side-lane degenerate turn must retry once (re-issue the upstream)"); + assert.ok(text.includes("stream_error"), `non-side-lane degenerate turn must escalate to an in-band error; got: ${text}`); + assert.ok(text.includes("degenerated again"), `non-side-lane #870 message; got: ${text}`); + } finally { + await h.close(); + } +}); diff --git a/tests/proxy-noinject-tag-strip.test.ts b/tests/proxy-noinject-tag-strip.test.ts index 0cfffd55d8..c244ba6a9a 100644 --- a/tests/proxy-noinject-tag-strip.test.ts +++ b/tests/proxy-noinject-tag-strip.test.ts @@ -435,7 +435,10 @@ test("#475 G3: fake-completion buffering + streaming request answered with JSON await h.close(); } }); -test("#4204 side-lane continuation 429 remains scoped to its request model", async () => { +// #6466 removed the side-lane continuation refetch (side lane ends as-is), so the +// #4204 429-scoping mechanism is now exercised through a main-lane (non-side) +// non-injected request, which still performs the continuation refetch. +test("#4204 non-injected continuation 429 remains scoped to its request model", async () => { const previousThrottle = { BILI_THROTTLE_429: process.env.BILI_THROTTLE_429, BILI_THROTTLE_429_MAX_CONSECUTIVE: process.env.BILI_THROTTLE_429_MAX_CONSECUTIVE, @@ -472,11 +475,11 @@ test("#4204 side-lane continuation 429 remains scoped to its request model", asy const side = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": "side-throttle-model", authorization: "Bearer same-key" }, - body: JSON.stringify({ model: "model-a", max_tokens: 100, stream: true, messages: [{ role: "user", content: "title?" }] }), + body: JSON.stringify({ model: "model-a", max_tokens: 4096, stream: true, messages: [{ role: "user", content: "answer?" }] }), }); assert.equal(side.status, 200); await side.text(); - assert.equal(h.hits(), 2, "the empty side turn performs exactly one continuation refetch"); + assert.equal(h.hits(), 2, "the empty non-injected turn performs exactly one continuation refetch"); assert.equal(activeThrottleHolds().length, 1); assert.equal(activeThrottleHolds()[0].model, "model-a", "the continuation's exact KeyRing marker scopes the hold"); @@ -485,7 +488,7 @@ test("#4204 side-lane continuation 429 remains scoped to its request model", asy headers: { "content-type": "application/json", "x-acp-session": "side-throttle-other", authorization: "Bearer same-key" }, body: JSON.stringify({ model: "model-b", max_tokens: 1024, stream: true, messages: [{ role: "user", content: "answer?" }] }), }); - assert.equal(otherModel.status, 200, "the side retry must not widen its model-local hold to the shared credential"); + assert.equal(otherModel.status, 200, "the model-B request must not widen model A's model-local hold to the shared credential"); await otherModel.text(); assert.equal(h.hits(), 3, "model B reaches the upstream instead of being locally blocked"); assert.equal(activeThrottleHolds().length, 1); @@ -500,7 +503,10 @@ test("#4204 side-lane continuation 429 remains scoped to its request model", asy } }); -test("#4204 Google side-lane stream keeps its path model through continuation throttling", async () => { +// #6466 removed the side-lane continuation refetch (side lane ends as-is), so the +// #4204 429-scoping mechanism is now exercised through a main-lane (non-side) +// non-injected request, which still performs the continuation refetch. +test("#4204 Google non-injected stream keeps its path model through continuation throttling", async () => { const previousThrottle = { BILI_THROTTLE_429: process.env.BILI_THROTTLE_429, BILI_THROTTLE_429_MAX_CONSECUTIVE: process.env.BILI_THROTTLE_429_MAX_CONSECUTIVE, @@ -537,19 +543,19 @@ test("#4204 Google side-lane stream keeps its path model through continuation th const first = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1beta/models/path-side-a:streamGenerateContent?alt=sse`, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": "google-side-a", authorization: "Bearer google-side-key" }, - body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "title?" }] }], generationConfig: { maxOutputTokens: 64 } }), + body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "answer?" }] }], generationConfig: { maxOutputTokens: 1024 } }), }); assert.equal(first.status, 200); const firstText = await first.text(); - assert.equal(firstText.includes(OPEN_MARK), false, "path-streamed side response still takes the render-tag strip pipe"); - assert.equal(h.hits(), 2, "the tag-only side stream issues one continuation refetch"); + assert.equal(firstText.includes(OPEN_MARK), false, "path-streamed non-injected response still takes the render-tag strip pipe"); + assert.equal(h.hits(), 2, "the tag-only non-injected stream issues one continuation refetch"); assert.equal(activeThrottleHolds().length, 1); assert.equal(activeThrottleHolds()[0].model, "path-side-a", "the continuation 429 uses the Gemini URL model"); const second = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1beta/models/path-side-b:streamGenerateContent?alt=sse`, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": "google-side-b", authorization: "Bearer google-side-key" }, - body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "answer?" }] }], generationConfig: { maxOutputTokens: 64 } }), + body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "answer?" }] }], generationConfig: { maxOutputTokens: 1024 } }), }); assert.equal(second.status, 200, "the model-A continuation hold does not block another model on the same credential"); await second.text();