From 3eec3863d3c80a5551aa579b2053d72e0aba9f53 Mon Sep 17 00:00:00 2001 From: Afloat Date: Fri, 20 Nov 2026 18:52:02 +0800 Subject: [PATCH] fix(responses): preserve content indices in synthesized streams Set content_index=0 on the single-part text and marker lifecycles and legacy stream-error completions. Retain the source content index when proxy and plugin filters flush pending text, including plugin retry reframing. This prevents official SDK consumers from rejecting valid answers such as text ending in a buffered "m" as a missing content part. Record WC-026, enforce it in the matching fake and exercise actual OpenAI SDK consumption. The unchanged production fails ten focused regressions with ten controls passing; the fixed relevant suites pass all 193 tests. Typecheck, build, unused-export and test-import-graph gates also pass. --- src/loop/adapter-responses.ts | 14 +-- src/plugin.ts | 6 +- src/stream-error.ts | 8 +- tests/proxy-stream-error.test.ts | 1 + tests/responses-content-index.test.ts | 134 ++++++++++++++++++++++++++ tests/wire-contract-fakes.ts | 16 +++ tests/wire-contract.test.ts | 22 +++++ 7 files changed, 187 insertions(+), 14 deletions(-) create mode 100644 tests/responses-content-index.test.ts diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index 24f9d14605..eda5d21d50 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -237,10 +237,10 @@ function buildMessageItemSequence(itemId: string, outputIndex: number, text: str return Buffer.from( [ `event: response.output_item.added\ndata: ${JSON.stringify({ type: "response.output_item.added", output_index: outputIndex, item })}\n\n`, - `event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: outputIndex, part })}\n\n`, - `event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: outputIndex, delta: text })}\n\n`, - `event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: outputIndex, text })}\n\n`, - `event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: outputIndex, part: { type: "output_text", text } })}\n\n`, + `event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: outputIndex, content_index: 0, part })}\n\n`, + `event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: outputIndex, content_index: 0, delta: text })}\n\n`, + `event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: outputIndex, content_index: 0, text })}\n\n`, + `event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: outputIndex, content_index: 0, part: { type: "output_text", text } })}\n\n`, `event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: outputIndex, item: doneItem })}\n\n`, ].join(""), "utf8", @@ -413,12 +413,12 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp // identity filters, byte-verbatim (never rewritten; matches the // openai adapter's reasoning_filter and #1960/KDD#10). const reasoningFilters = new Map(); - let lastTextRef: { itemId: string; outputIndex: number } | null = null; + let lastTextRef: { itemId: string; outputIndex: number; contentIndex: unknown } | null = null; const flushFilter = function* (): Generator { const tail = tagFilter.flush(); if (tail.length > 0) { const raw = lastTextRef - ? rebuildResponsesEvent("response.output_text.delta", { type: "response.output_text.delta", item_id: lastTextRef.itemId, output_index: lastTextRef.outputIndex, delta: tail }) + ? rebuildResponsesEvent("response.output_text.delta", { type: "response.output_text.delta", item_id: lastTextRef.itemId, output_index: lastTextRef.outputIndex, content_index: lastTextRef.contentIndex, delta: tail }) : undefined; yield { kind: "text", delta: tail, ...(raw ? { raw } : {}) } as ParsedStreamEvent; } @@ -545,7 +545,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp const itemId = typeof obj.item_id === "string" ? obj.item_id : ""; const outputIndex = typeof obj.output_index === "number" ? obj.output_index : 0; const mapped = remapped.get(itemId); - lastTextRef = { itemId: mapped ? mapped.id : itemId, outputIndex: mapped ? mapped.index : outputIndex }; + lastTextRef = { itemId: mapped ? mapped.id : itemId, outputIndex: mapped ? mapped.index : outputIndex, contentIndex: obj.content_index }; const clean = tagFilter.push(delta); if (clean.length > 0) { if (mapped) { diff --git a/src/plugin.ts b/src/plugin.ts index 867038aef8..52488ebbab 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -4612,13 +4612,13 @@ export async function pipePluginResponsesWithStrip( if (proseAcc.length === 0 || !session) return; scanModelOutputForPhantomAttachments(session.id, proseAcc); }; - let lastDeltaMeta: { item_id?: unknown; output_index?: unknown } | null = null; + let lastDeltaMeta: { item_id?: unknown; output_index?: unknown; content_index?: unknown } | null = null; const flushTail = (after: string) => { const tail = tagFilter.flush(); if (tail.length > 0) { visibleTextChars += tail.length; answerProse += tail; - const meta = inRetry && heldItemId !== undefined ? { item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {}); + const meta = inRetry && heldItemId !== undefined ? { ...lastDeltaMeta, item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {}); appendReplyDelta(meta, tail); return `data: ${JSON.stringify({ type: "response.output_text.delta", ...meta, delta: tail })}\n\n` + after; } @@ -5070,7 +5070,7 @@ export async function pipePluginResponsesWithStrip( continue; } const clean = tagFilter.push(delta); - lastDeltaMeta = { item_id: ev["item_id"], output_index: ev["output_index"] }; + lastDeltaMeta = { item_id: ev["item_id"], output_index: ev["output_index"], content_index: ev["content_index"] }; if (!inRetry && heldItemId === undefined && ev["item_id"] !== undefined) { heldItemId = ev["item_id"]; heldOutputIndex = ev["output_index"]; diff --git a/src/stream-error.ts b/src/stream-error.ts index c1495a747e..2054993913 100644 --- a/src/stream-error.ts +++ b/src/stream-error.ts @@ -90,10 +90,10 @@ function legacyCompletionChunk(protocol: Protocol, visible: string, state?: Lega const oi = 0; const errorItem = { type: "message", id: itemId, role: "assistant", content: [{ type: "output_text", text: visible }] }; return `event: response.output_item.added\ndata: ${JSON.stringify({ type: "response.output_item.added", output_index: oi, item: { type: "message", id: itemId, role: "assistant", content: [] } })}\n\n` + - `event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: oi, part: { type: "output_text", text: "" } })}\n\n` + - `event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: oi, delta: visible })}\n\n` + - `event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: oi, text: visible })}\n\n` + - `event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: oi, part: { type: "output_text", text: visible } })}\n\n` + + `event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: oi, content_index: 0, part: { type: "output_text", text: "" } })}\n\n` + + `event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: oi, content_index: 0, delta: visible })}\n\n` + + `event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: oi, content_index: 0, text: visible })}\n\n` + + `event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: oi, content_index: 0, part: { type: "output_text", text: visible } })}\n\n` + `event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: oi, item: errorItem })}\n\n` + `event: response.completed\ndata: ${JSON.stringify({ type: "response.completed", response: { status: "completed", output: [errorItem] } })}\n\n`; } diff --git a/tests/proxy-stream-error.test.ts b/tests/proxy-stream-error.test.ts index 61c9b211fa..f5d1eade21 100644 --- a/tests/proxy-stream-error.test.ts +++ b/tests/proxy-stream-error.test.ts @@ -163,6 +163,7 @@ test("emitStreamError: responses errorShape=\"completion\" emits a full item lif for (const block of itemEvents) { assert.match(block, new RegExp(`"item_id":"${itemId}"`)); assert.match(block, /"output_index":0/); + assert.match(block, /"content_index":0/); } // The completed response contains the error item in its output array. const completed = out.split("\n\n").find((b) => b.includes("response.completed")); diff --git a/tests/responses-content-index.test.ts b/tests/responses-content-index.test.ts new file mode 100644 index 0000000000..0c44b5cf83 --- /dev/null +++ b/tests/responses-content-index.test.ts @@ -0,0 +1,134 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { ServerResponse } from "node:http"; +import { createRequire } from "node:module"; +import { createResponsesAdapter } from "../src/loop/adapter-responses.ts"; +import { pipePluginResponsesWithStrip } from "../src/plugin.ts"; +import { emitStreamError } from "../src/stream-error.ts"; +import { validateResponsesContentIndex } from "./wire-contract-fakes.ts"; + +function frame(event: Record): string { + return `event: ${String(event.type)}\ndata: ${JSON.stringify(event)}\n\n`; +} + +function created(): string { + return frame({ type: "response.created", response: { id: "resp_fixture", object: "response", status: "in_progress", output: [] } }); +} + +function payloads(raw: string): Record[] { + return raw.split("\n").filter((line) => line.startsWith("data: ")).map((line) => JSON.parse(line.slice(6)) as Record); +} + +function collector(): { res: ServerResponse; chunks: string[] } { + const chunks: string[] = []; + const res = { + write(chunk: string | Buffer) { chunks.push(chunk.toString()); return true; }, + end() { return this; }, + once() { return this; }, + destroyed: false, + writableEnded: false, + } as unknown as ServerResponse; + return { res, chunks }; +} + +async function consume(raw: string) { + const requireSdk = createRequire(import.meta.resolve("@earendil-works/pi-coding-agent")); + const { default: OpenAI } = await import(requireSdk.resolve("openai")); + const client = new OpenAI({ + apiKey: "synthetic-test-key", + fetch: async () => new Response(raw, { headers: { "content-type": "text/event-stream" } }), + }); + const stream = client.responses.stream({ model: "synthetic", input: "synthetic" }); + const deltas: Array<{ delta: string; content_index: number; output_index: number }> = []; + stream.on("response.output_text.delta", (event: { delta: string; content_index: number; output_index: number }) => deltas.push(event)); + const response = await stream.finalResponse(); + return { deltas, response }; +} + +function upstreamText(text: string, contentIndex: number, id = "msg_fixture"): string { + const earlier = Array.from({ length: contentIndex }, () => ({ type: "output_text", text: "earlier part" })); + const content = [...earlier, { type: "output_text", text }]; + const item = { type: "message", id, role: "assistant", content }; + const ref = { item_id: id, output_index: 0, content_index: contentIndex }; + return created() + [ + { type: "response.output_item.added", output_index: 0, item: { ...item, content: [] } }, + ...earlier.map((part, index) => ({ type: "response.content_part.added", item_id: id, output_index: 0, content_index: index, part })), + { type: "response.content_part.added", ...ref, part: { type: "output_text", text: "" } }, + { type: "response.output_text.delta", ...ref, delta: text }, + { type: "response.output_text.done", ...ref, text }, + { type: "response.content_part.done", ...ref, part: { type: "output_text", text } }, + { type: "response.output_item.done", output_index: 0, item }, + { type: "response.completed", response: { id: "resp_fixture", status: "completed", output: [item] } }, + ].map(frame).join(""); +} + +test("official Responses SDK consumes a legacy stream-error completion", async () => { + const { res, chunks } = collector(); + emitStreamError(res, "responses", "kaboom", undefined, "completion"); + const { deltas, response } = await consume(created() + chunks.join("")); + assert.deepEqual(deltas.map((event) => event.delta), ["\nāŒ [ACP] stream error: kaboom"]); + assert.equal(response.status, "completed"); + assert.equal(response.output[0].content[0].text, deltas[0].delta); +}); + +test("official Responses SDK still rejects the default native error", async () => { + const { res, chunks } = collector(); + emitStreamError(res, "responses", "kaboom"); + await assert.rejects(consume(created() + chunks.join("")), { type: "error", code: "stream_error", message: "[acp-proxy: kaboom]" }); +}); + +for (const kind of ["text", "marker"] as const) { + test(`official Responses SDK consumes synthesized ${kind} content`, async () => { + const adapter = createResponsesAdapter(); + const chunk = kind === "text" ? adapter.emitText("generated text") : adapter.emitMarker("acp_status", "ready"); + const events = payloads(chunk.toString()); + const item = events.find((event) => event.type === "response.output_item.done")?.item; + assert.ok(item); + const { deltas } = await consume(created() + chunk + frame({ type: "response.completed", response: { id: "resp_fixture", status: "completed", output: [item] } })); + assert.equal(deltas.length, 1); + assert.equal(deltas[0].content_index, 0); + if (kind === "text") assert.equal(deltas[0].delta, "generated text"); + else assert.match(deltas[0].delta, /\[ACP\]/); + assert.deepEqual(events.flatMap(validateResponsesContentIndex), []); + }); +} + +for (const mode of ["proxy", "plugin"] as const) { + for (const contentIndex of [0, 1]) { + test(`${mode} Responses text flush preserves content index ${contentIndex}`, async () => { + const input = upstreamText("Length: 10 m", contentIndex); + const { res, chunks } = collector(); + if (mode === "plugin") { + await pipePluginResponsesWithStrip(new Response(input).body!, res); + } else { + const adapter = createResponsesAdapter(); + for await (const event of adapter.parseStream(new Response(input).body!, 1)) { + if (event.kind === "meta") chunks.push(event.chunk.toString()); + else if (event.kind === "text" && event.raw) chunks.push(event.raw.toString()); + } + chunks.push(adapter.emitCompletion().toString()); + } + const { deltas, response } = await consume(chunks.join("")); + assert.deepEqual(deltas.map((event) => event.delta), ["Length: 10 ", "m"]); + for (const delta of deltas) assert.equal(delta.content_index, contentIndex); + assert.equal(response.output[0].content[contentIndex].text, "Length: 10 m"); + if (contentIndex > 0) assert.equal(response.output[0].content[0].text, "earlier part"); + assert.deepEqual(payloads(chunks.join("")).flatMap(validateResponsesContentIndex), []); + }); + } +} + +test("plugin Responses retry flush preserves its content index", async () => { + const { res, chunks } = collector(); + let retries = 0; + const echo = "\x3cacp tokens=\"1\" type=\"text\"\x3em00001\x3c/acp\x3e"; + await pipePluginResponsesWithStrip(new Response(upstreamText(echo, 0)).body!, res, undefined, undefined, async () => { + retries++; + return new Response(upstreamText("Length: 10 m", 0, "msg_retry")).body; + }); + assert.equal(retries, 1); + const { deltas } = await consume(chunks.join("")); + assert.equal(deltas.map((event) => event.delta).join(""), "Length: 10 m"); + for (const delta of deltas) assert.equal(delta.content_index, 0); + assert.deepEqual(payloads(chunks.join("")).flatMap(validateResponsesContentIndex), []); +}); diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index 9b6797ee1b..ecb00b1426 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -31,6 +31,13 @@ interface WireRule { } export const WIRE_RULES: readonly WireRule[] = [ + { + id: "WC-026", + wire: "responses", + lane: "responses-sse", + summary: "Responses text/content-part SSE events carry a non-negative integer content_index identifying the part inside the indexed output item", + provenance: "OpenAI openai-node v6.26.0 requires content_index in ResponseTextDeltaEvent/ResponseTextDoneEvent/ResponseContentPartAddedEvent/ResponseContentPartDoneEvent (https://github.com/openai/openai-node/blob/v6.26.0/src/resources/responses/responses.ts); ResponseStream indexes output.content[event.content_index] (https://github.com/openai/openai-node/blob/v6.26.0/src/lib/responses/ResponseStream.ts). Reproduced against that official SDK: bili's synthesized messages, legacy stream-error completions and filtered text tails omitted indices, so finalResponse() rejected with 'missing content at index undefined'. Gates exercise the actual SDK in proxy/plugin paths and the emitted error frames; the fake validates its outgoing SSE frames with the same constraint.", + }, { id: "WC-017", wire: "responses", @@ -772,6 +779,8 @@ interface FakeUpstream { } function sse(res: http.ServerResponse, events: Array<[string, unknown]>, opts?: { done?: boolean }): void { + const violations = events.flatMap(([, data]) => validateResponsesContentIndex(data)); + if (violations.length) throw new Error(violations.join("; ")); res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache", connection: "keep-alive" }); // OpenAI streams carry no `event:` line at all — only Anthropic/Gemini name their events. for (const [ev, data] of events) { @@ -811,6 +820,13 @@ function openAiChatReply(res: http.ServerResponse, parsed: Record= 0 + ? [] + : ["WC-026 Responses content-part events require a non-negative integer content_index"]; +} + function responsesReply(res: http.ServerResponse, parsed: Record, text: string): void { const msgId = "msg_fake"; const msg = { type: "message", id: msgId, role: "assistant", content: [{ type: "output_text", text }] }; diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index bda8c0071e..6ccfe5e93f 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -21,6 +21,8 @@ import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; import { readFileSync } from "node:fs"; +import { emitStreamError } from "../src/stream-error.ts"; +import { validateResponsesContentIndex } from "./wire-contract-fakes.ts"; process.env.NODE_ENV = "test"; @@ -57,6 +59,25 @@ import { verifySdkHmac, WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpst type ToolShape = Record; +test("WC-026 Responses content-part indices are explicit, including legacy error completions", () => { + for (const type of ["response.content_part.added", "response.output_text.delta", "response.output_text.done", "response.content_part.done"]) { + for (const content_index of [0, 2]) assert.deepEqual(validateResponsesContentIndex({ type, content_index }), []); + for (const content_index of [undefined, null, "0", -1, 0.5]) assert.match(validateResponsesContentIndex({ type, content_index })[0], /WC-026/); + } + assert.deepEqual(validateResponsesContentIndex({ type: "response.output_item.done" }), []); + const frames: Record[] = []; + const res = { + write(chunk: string) { + for (const line of chunk.split("\n")) if (line.startsWith("data: ")) frames.push(JSON.parse(line.slice(6))); + return true; + }, + end() { return this; }, + } as unknown as http.ServerResponse; + emitStreamError(res, "responses", "fixture failure", undefined, "completion"); + assert.equal(frames.length, 7); + assert.deepEqual(frames.flatMap(validateResponsesContentIndex), []); +}); + test("WC-013: WebSocket transport envelopes omit HTTP-only fields", () => { assert.deepEqual(validateResponsesWsCreate({ type: "response.create", input: [] }), []); for (const key of ["stream", "background", "stream_options"]) assert.match(validateResponsesWsCreate({ type: "response.create", [key]: true })[0], /WC-013/); @@ -369,6 +390,7 @@ test("wire-contract ledger: every lane-scoped rule has a live enforcement clause // Lane-scoped constraints hold on ONE client's lane only — the shared wire validator // cannot enforce them (WC-013 forbids exactly what WC-017 requires, same frame shape). const laneProbes: Array<{ ruleId: string; validate: (body: unknown) => string[]; body: unknown }> = [ + { ruleId: "WC-026", validate: validateResponsesContentIndex, body: { type: "response.output_text.delta", output_index: 0, delta: "x" } }, { ruleId: "WC-017", validate: validateCodexResponsesWsCreate, body: { type: "response.create", input: [] } }, // WC-019/020/021 are the Vertex Anthropic lane (lane: "vertex-anthropic") — the wire rules that // hold only on that lane, so the shared anthropic validator must not carry them. WC-019 is