Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/codex-compact-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
}
Expand Down
18 changes: 15 additions & 3 deletions tests/wire-contract-fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions tests/wire-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ import { WIRE_RULES, VALIDATORS, startFakeUpstream, type Wire } from "./wire-con

type ToolShape = Record<string, unknown>;

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<string, unknown> {
if (wire === "google") return { tools: [{ functionDeclarations: [tool] }] };
return { tools: [tool] };
Expand Down Expand Up @@ -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: [] } }] },
Expand Down
Loading