From 2ed7c59e047f220fa32d319a6eec702df7f3824e Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 7 Sep 2026 00:41:43 +0800 Subject: [PATCH] fix #589: forward trailing usage-only frame on raw tool-call rounds OpenAI stream_options.include_usage clients (dsh, OpenAI SDK) read usage from the trailing empty-choices frame. The loop adapter parsed it into the internal ledger then dropped it, so raw tool-call rounds reported 0/0 to the host. Forward it verbatim on sawRealToolCall rounds, patched with the #408 prepare-time credit via the extracted patchUsageChunk helper (also reused by the finish-chunk raw branch). Rebuilt rounds stay unchanged (emitCompletion embeds usage; forwarding the uncredited raw frame there would duplicate and undercut the backfill). --- src/loop/adapter-openai.ts | 49 ++++++++++++--------- tests/issue589-usage-frame.test.ts | 68 ++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 tests/issue589-usage-frame.test.ts diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 64a5ed08d0..8aaef3dd81 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -115,6 +115,27 @@ function stripFinishReasonChunk(buf: Buffer): Buffer { } } +function patchUsageChunk(eventStr: string, parsed: Record, u: Record, hostCredit: number): Buffer { + const pu = typeof u.prompt_tokens === "number" ? u.prompt_tokens : undefined; + const tu = typeof u.total_tokens === "number" ? u.total_tokens : undefined; + if (hostCredit > 0 && (pu !== undefined || tu !== undefined)) { + const patched = { + ...parsed, + usage: { + ...u, + ...(pu !== undefined ? { prompt_tokens: pu + hostCredit } : {}), + ...(tu !== undefined ? { total_tokens: tu + hostCredit } : {}), + }, + }; + const out = eventStr + .split("\n") + .map((l) => (l.startsWith("data:") ? `data: ${JSON.stringify(patched)}` : l)) + .join("\n"); + return Buffer.from(out + "\n\n", "utf8"); + } + return Buffer.from(eventStr + "\n\n", "utf8"); +} + export function createOpenaiAdapter(requestBody: Record, clientSystem?: string, hostCredit = 0): CompressLoopAdapter { const model = (requestBody.model as string) ?? "unknown"; let responseId = `chatcmpl-proxy-${Date.now()}`; @@ -311,6 +332,13 @@ export function createOpenaiAdapter(requestBody: Record, client outputTokens: typeof u.completion_tokens === "number" ? u.completion_tokens : undefined, cachedTokens: typeof pd?.cached_tokens === "number" ? pd.cached_tokens : undefined, } as ParsedStreamEvent; + // #589: include_usage clients (dsh, OpenAI SDK) read usage + // from this trailing empty-choices frame; raw tool-call rounds + // must forward it (with the prepare-time credit), not swallow + // it into the internal ledger. + if (sawRealToolCall) { + yield { kind: "meta", chunk: patchUsageChunk(eventStr, parsed, u, hostCredit) } as ParsedStreamEvent; + } } continue; } @@ -334,26 +362,7 @@ export function createOpenaiAdapter(requestBody: Record, client // post-fold usage) reaches the host verbatim — add the // prepare-time credit back so the host anchors on the // uncompressed baseline. - let chunk = rawBuf; - if (hostCredit > 0 && u) { - const pu = typeof u.prompt_tokens === "number" ? u.prompt_tokens : undefined; - const tu = typeof u.total_tokens === "number" ? u.total_tokens : undefined; - if (pu !== undefined || tu !== undefined) { - const patched = { - ...parsed, - usage: { - ...u, - ...(pu !== undefined ? { prompt_tokens: pu + hostCredit } : {}), - ...(tu !== undefined ? { total_tokens: tu + hostCredit } : {}), - }, - }; - const out = eventStr - .split("\n") - .map((l) => (l.startsWith("data:") ? `data: ${JSON.stringify(patched)}` : l)) - .join("\n"); - chunk = Buffer.from(out + "\n\n", "utf8"); - } - } + const chunk = patchUsageChunk(eventStr, parsed, u ?? {}, hostCredit); // This verbatim chunk IS the round's authoritative completion // (suppressCompletion); write it once and never fall through // to the text/reasoning branches (which would re-emit the same diff --git a/tests/issue589-usage-frame.test.ts b/tests/issue589-usage-frame.test.ts new file mode 100644 index 0000000000..fac0eaab16 --- /dev/null +++ b/tests/issue589-usage-frame.test.ts @@ -0,0 +1,68 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createOpenaiAdapter } from "../src/loop/index.ts"; + +function streamOf(events: string[]): ReadableStream { + let i = 0; + return new ReadableStream({ + pull(controller) { + if (i < events.length) { + controller.enqueue(Buffer.from(events[i++], "utf8")); + } else { + controller.close(); + } + }, + }); +} + +const chunk = (o: unknown): string => `data: ${JSON.stringify(o)}\n\n`; + +const toolDelta = { id: "c1", object: "chat.completion.chunk", created: 1, model: "m", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "get_weather", arguments: "" } }] } }] }; +const finishChunk = { id: "c1", object: "chat.completion.chunk", created: 1, model: "m", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }; +const usageFrame = { id: "c1", object: "chat.completion.chunk", created: 1, model: "m", choices: [], usage: { prompt_tokens: 50000, completion_tokens: 10, total_tokens: 50010 } }; + +async function collect(adapter: ReturnType, events: string[]): Promise<{ meta: string; kinds: string[] }> { + let meta = ""; + const kinds: string[] = []; + for await (const ev of adapter.parseStream(streamOf(events), 1)) { + kinds.push(ev.kind); + if (ev.kind === "meta") meta += ev.chunk.toString("utf8"); + } + return { meta, kinds }; +} + +test("#589: raw tool-call round forwards the trailing usage-only frame, patched with credit", async () => { + const adapter = createOpenaiAdapter({ model: "m" }, undefined, 40000); + const { meta } = await collect(adapter, [chunk(toolDelta), chunk(finishChunk), chunk(usageFrame), "data: [DONE]\n\n"]); + const usageAt = meta.indexOf('"prompt_tokens":90000'); + const finishAt = meta.indexOf('"finish_reason":"tool_calls"'); + const doneAt = meta.indexOf("[DONE]"); + assert.ok(usageAt >= 0, `expected patched usage frame in client stream: ${meta}`); + assert.ok(meta.includes('"total_tokens":90010'), meta); + assert.ok(finishAt >= 0 && usageAt > finishAt, `usage frame must come after the finish chunk: ${meta}`); + assert.ok(doneAt >= 0 && usageAt < doneAt, `usage frame must come before [DONE]: ${meta}`); +}); + +test("#589: trailing usage-only frame forwarded verbatim when no credit", async () => { + const adapter = createOpenaiAdapter({ model: "m" }, undefined, 0); + const { meta } = await collect(adapter, [chunk(toolDelta), chunk(finishChunk), chunk(usageFrame), "data: [DONE]\n\n"]); + assert.ok(meta.includes('"prompt_tokens":50000'), meta); + assert.ok(!meta.includes('"prompt_tokens":90000'), meta); +}); + +test("#589: non-tool rounds keep swallowing the trailing frame (rebuild embeds usage)", async () => { + const adapter = createOpenaiAdapter({ model: "m" }, undefined, 40000); + const textDelta = { id: "c1", object: "chat.completion.chunk", created: 1, model: "m", choices: [{ index: 0, delta: { content: "hi there" } }] }; + const finishText = { id: "c1", object: "chat.completion.chunk", created: 1, model: "m", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }; + const { meta } = await collect(adapter, [chunk(textDelta), chunk(finishText), chunk(usageFrame), "data: [DONE]\n\n"]); + assert.ok(!meta.includes("prompt_tokens"), `rebuild round must not forward the raw usage frame: ${meta}`); +}); + +test("#589: usage-only frame without usage field is not forwarded", async () => { + const adapter = createOpenaiAdapter({ model: "m" }, undefined, 40000); + const emptyChoices = { id: "c1", object: "chat.completion.chunk", created: 1, model: "m", choices: [] }; + const { meta } = await collect(adapter, [chunk(toolDelta), chunk(finishChunk), chunk(emptyChoices), "data: [DONE]\n\n"]); + assert.ok(!meta.includes("chat.completion.chunk\" }"), meta); + const frames = meta.split("data: ").length - 1; + assert.equal(frames, 3, `expected only tool-delta + finish + [DONE] frames: ${meta}`); +});