diff --git a/src/server.ts b/src/server.ts index 73153a2511..442852721f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2442,7 +2442,15 @@ async function handle( return; } log("info", `[${session.id}] side request (${requestAgent !== undefined ? `agent=${requestAgent}` : `max_tokens<=${SIDE_REQUEST_MAX_TOKENS}`}) → passthrough + tag strip only, kernel state untouched`); - const sideBody = scrubAnthropicPck(protocol, bodyBuffer, log); + let sideBody = scrubAnthropicPck(protocol, bodyBuffer, log); + const sideInput = (parsed as ResponsesRequestBody).input; + if (protocol === "responses" && Array.isArray(sideInput)) { + const { items, replaced, dropped } = replaceBiliCompactionItems(sideInput); + if (replaced + dropped > 0) { + sideBody = Buffer.from(JSON.stringify({ ...parsed, input: items })); + log("info", `[${session.id}] side request normalized bili compaction handoffs (replaced=${replaced}, dropped=${dropped})`); + } + } const sidePrepared: Prepared = { body: sideBody, session, diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts index 8f93aae3f4..86b8e88ecd 100644 --- a/tests/codex-compact-e2e.test.ts +++ b/tests/codex-compact-e2e.test.ts @@ -23,6 +23,34 @@ import { buildTriggerForgeBody } from "../src/codex-compact.ts"; const CODEX_UA = "codex_cli_rs/0.1.0 (linux x86_64)"; const SESSION = "trig-sess"; +test("Responses side requests normalize local compaction handoffs before forwarding", async () => { + await withHarness({ firstTurnTokens: 100, strictCompactionIds: true }, async (h) => { + const real = { type: "compaction", id: "cmp_upstream", encrypted_content: "opaque-upstream-blob" }; + const r = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + model: "gpt-resp", stream: true, max_output_tokens: 100, + input: [ + { type: "compaction", id: "fc_bili_local", encrypted_content: "bili:acp:summary retained" }, + real, + { type: "message", role: "user", content: "Create a title" }, + ], + }), + }); + const text = await r.text(); + assert.equal(r.status, 200, text); + assert.equal(h.bodies.length, 1); + const sent = JSON.parse(h.bodies[0]); + assert.equal(sent.input[0].type, "message"); + assert.match(sent.input[0].content[0].text, /summary retained/); + assert.deepEqual(sent.input[1], real); + assert.equal(sent.max_output_tokens, 100); + assert.equal(sent.tools, undefined); + assert.ok(!h.bodies[0].includes("fc_bili_local")); + }); +}); + function sse(type: string, data: unknown): string { return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; } diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index 2481023212..8b3a306867 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -26,6 +26,12 @@ export interface WireRule { } export const WIRE_RULES: readonly WireRule[] = [ + { + id: "WC-012", + wire: "responses", + summary: "a supplied compaction input item id must begin with cmp", + provenance: "#1763: user-reported upstream rejection for an fc_bili_ compaction item, Expected an ID that begins with 'cmp'; reproduced with a strict loopback upstream in tests/codex-compact-e2e.test.ts.", + }, { id: "WC-009", wire: "responses", @@ -202,20 +208,26 @@ function validateGeminiSchema(schema: unknown, path: string, out: string[]): voi validateGeminiSchema(schema.items, `${path}.items`, out); } -/** WC-005, WC-009, WC-011 on a Responses-API body (flat function entries). */ +/** WC-005, WC-009, WC-011, WC-012 on a Responses-API body (flat function entries). */ export function validateResponsesBody(body: unknown): string[] { const out: string[] = []; + if (!isPlainObject(body)) return out; // WC-011 (#1733): runs before the tools early-return — the adjacency ban // applies to any array input, tools or not. - if (isPlainObject(body) && Array.isArray(body.input)) { + if (Array.isArray(body.input)) { for (let i = 1; i < body.input.length; i++) { const prev = body.input[i - 1]; const cur = body.input[i]; if (isPlainObject(prev) && prev.type === "configuration_update" && isPlainObject(cur) && cur.type === "configuration_update") out.push(`WC-011 input[${i}]: consecutive configuration_update items are not allowed`); } + body.input.forEach((item, i) => { + if (isPlainObject(item) && item.type === "compaction" && item.id !== undefined + && (typeof item.id !== "string" || !item.id.startsWith("cmp"))) + out.push(`WC-012 input[${i}].id: expected an ID that begins with 'cmp'`); + }); } - if (!isPlainObject(body) || !Array.isArray(body.tools)) return out; + if (!Array.isArray(body.tools)) return out; body.tools.forEach((t, i) => { if (!isPlainObject(t)) return; if (t.type !== "function") return; diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index 6673917249..38b9c26a65 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -56,6 +56,12 @@ import { WIRE_RULES, VALIDATORS, startFakeUpstream, type Wire } from "./wire-con type ToolShape = Record; +test("WC-012 validates compaction IDs without changing function-call ID rules", () => { + assert.deepEqual(VALIDATORS.responses({ input: [{ type: "compaction", id: "cmp_real", encrypted_content: "opaque" }] }), []); + assert.deepEqual(VALIDATORS.responses({ input: [{ type: "function_call", id: "fc_real" }] }), []); + assert.match(VALIDATORS.responses({ input: [{ type: "compaction", id: "fc_bili_local" }] })[0], /WC-012/); +}); + function syntheticBody(wire: Wire, tool: unknown): Record { if (wire === "google") return { tools: [{ functionDeclarations: [tool] }] }; return { tools: [tool] }; @@ -104,6 +110,7 @@ test("wire-contract ledger: every rule has a live enforcement clause", () => { { tools: [{ type: "function", function: { name: "ok", parameters: { anyOf: [] } } }] }, ], responses: [ + { input: [{ type: "compaction", id: "fc_bili_local", encrypted_content: "bili:acp:summary" }] }, { tools: [{ type: "function", name: "bad name", parameters: { type: "object", properties: {} } }] }, { tools: [{ type: "function", name: "ok", parameters: { type: "array" } }] }, { tools: [{ type: "function", name: "ok", parameters: { type: "object", properties: {}, anyOf: [] } }] },