From 6c630478b083c258257b29092b7532b39bf32d73 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 13:57:50 +0800 Subject: [PATCH] fix: mirror the codex CLI wire shape in preflight summary calls for codex clients (#2733) --- src/preflight.ts | 19 +- tests/issue2733-preflight-codex-shape.test.ts | 190 ++++++++++++++++++ 2 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 tests/issue2733-preflight-codex-shape.test.ts diff --git a/src/preflight.ts b/src/preflight.ts index d546a98f38..4dc6ee6561 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -21,6 +21,7 @@ import { peekRegistryOutputLimit } from "./registry.js"; import { safePrefix } from "./text-safe.js"; import { applyEstimateCalibration, currentCalibrationFactor } from "./util.js"; import { configuredSummaryPlan, type ConfiguredSummaryPlan } from "./external-summary-runtime.js"; +import { isCodexClient } from "./codex-compact.js"; import type { ExternalSummaryBatchResult } from "./external-summary.js"; import type { ResolvedKernelConfig } from "./compress-settings.js"; import { newExternalCallId } from "./external-summary-marker.js"; @@ -464,7 +465,7 @@ function windowClampedOutput(base: number, window: number | undefined, system: s return Math.min(base, Math.max(MIN_CLAMPED_SUMMARY_OUTPUT, headroom)); } -export function summaryPayload(protocol: PreflightProtocol, model: string, system: string, content: string, stream: boolean, includeMaxOutputTokens: boolean, host?: string, window?: number, billingBlock?: { type: "text"; text: string }): Record { +export function summaryPayload(protocol: PreflightProtocol, model: string, system: string, content: string, stream: boolean, includeMaxOutputTokens: boolean, host?: string, window?: number, billingBlock?: { type: "text"; text: string }, codexClient?: boolean): Record { const maxOutputTokens = windowClampedOutput(summaryOutputTokens(model, host), window, system, content); if (protocol === "anthropic") { // #2189: carry the client's billing-attribution block as system[0] with @@ -491,7 +492,19 @@ export function summaryPayload(protocol: PreflightProtocol, model: string, syste // #663: max_output_tokens is optional — omit it once the upstream has // rejected the parameter (learned per URL+model); the model's default // output cap then applies. - const payload: Record = { model, instructions: system, input: [{ role: "user", content }], stream, store: false }; + // #2733: new-api / anyrouter-family codex relays validate inbound calls as + // "legitimate Codex requests" — input must be typed message items AND carry + // include:["reasoning.encrypted_content"] (the real codex CLI's signature, + // sent unconditionally by codex-rs core client.rs); anything else is 400 + // invalid_responses_request. The main path forwards real codex bytes + // verbatim, so only bili's own side-channel summary call was rejected + // (preflight lane dead → codex local auto-compaction won the race). + // Non-codex clients keep the minimal shape byte-identical. + const input = codexClient + ? [{ type: "message", role: "user", content: [{ type: "input_text", text: content }] }] + : [{ role: "user", content }]; + const payload: Record = { model, instructions: system, input, stream, store: false }; + if (codexClient) payload.include = ["reasoning.encrypted_content"]; if (includeMaxOutputTokens) payload.max_output_tokens = maxOutputTokens; return payload; } @@ -1104,7 +1117,7 @@ async function requestSummaryBody(deps: PreflightDeps, body: string, stream: boo const SSE_DATA_LINE_RE = /(?:^|\n)data:/; async function requestSummary(deps: PreflightDeps, system: string, content: string, stream: boolean, includeMaxOutputTokens: boolean): Promise { - const text = await requestSummaryBody(deps, JSON.stringify(summaryPayload(deps.protocol, deps.model, system, content, stream, includeMaxOutputTokens, safeHost(deps.url), deps.config.modelContextLimit, deps.billingBlock)), stream); + const text = await requestSummaryBody(deps, JSON.stringify(summaryPayload(deps.protocol, deps.model, system, content, stream, includeMaxOutputTokens, safeHost(deps.url), deps.config.modelContextLimit, deps.billingBlock, isCodexClient(deps.headers))), stream); let json: unknown; try { json = JSON.parse(text); diff --git a/tests/issue2733-preflight-codex-shape.test.ts b/tests/issue2733-preflight-codex-shape.test.ts new file mode 100644 index 0000000000..889a65d8cb --- /dev/null +++ b/tests/issue2733-preflight-codex-shape.test.ts @@ -0,0 +1,190 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +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 { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; + +// #2733 regression: new-api / anyrouter-family codex relays validate inbound +// calls as "legitimate Codex requests" — input must be typed message items +// AND carry include:["reasoning.encrypted_content"] (the real codex CLI's +// signature, sent unconditionally by codex-rs core client.rs); anything else +// is HTTP 400 invalid_responses_request. bili's own side-channel summary call +// sent the minimal shape ({role,content}, no include) → preflight lane dead +// on such relays (12/12 rejections in the issue log) → codex local +// auto-compaction won the race. For a codex client the summary payload must +// now mirror the real CLI's wire shape; non-codex clients stay byte-identical. + +const SUMMARY_TEXT = + "PREFLIGHT SUMMARY: the segment held a deterministic load-growth payload across a dozen turns; every raw marker is derivable from the seed and none carries unique state."; + +const INVALID_CODEX_BODY = JSON.stringify({ + error: { message: "invalid codex request (request id: req_2733)", type: "new_api_error", param: "", code: "invalid_responses_request" }, +}); + +type SummaryCall = { stream: boolean; codexShaped: boolean }; + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function completed(inputTokens: number): string { + return sse("response.completed", { + response: { id: "resp_done", status: "completed", output: [], usage: { input_tokens: inputTokens, output_tokens: 5, total_tokens: inputTokens + 5 } }, + }); +} + +// Mirrors the anyrouter-style validator: a summary call (single-item input) +// is rejected unless it carries the real codex CLI signature — typed message +// items with typed content parts AND the include array. +function isCodexShaped(body: { input?: unknown; include?: unknown }): boolean { + const inc = body.include; + if (!Array.isArray(inc) || !inc.includes("reasoning.encrypted_content")) return false; + const input = body.input; + if (!Array.isArray(input) || input.length === 0) return false; + return input.every((item) => { + const i = item as { type?: unknown; content?: unknown }; + if (i.type !== "message") return false; + return Array.isArray(i.content) && i.content.every((p) => typeof (p as { type?: unknown }).type === "string"); + }); +} + +function makeUpstream(summaryCalls: SummaryCall[], strictCodexShape: boolean): http.Server { + return http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + let parsed: { stream?: boolean; input?: unknown; include?: unknown } = {}; + try { parsed = JSON.parse(raw); } catch { /* keep {} */ } + const isSummary = Array.isArray(parsed.input) && parsed.input.length === 1; + if (isSummary) { + const shaped = isCodexShaped(parsed); + summaryCalls.push({ stream: parsed.stream === true, codexShaped: shaped }); + if (strictCodexShape && !shaped) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(INVALID_CODEX_BODY); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output_text: SUMMARY_TEXT })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.write(completed(1000)); + res.end(); + }); + }); +} + +function longInput() { + const input: { type: string; role: string; content: string }[] = []; + for (let i = 0; i < 12; i++) { + input.push({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i} of the long conversation. ` + `MARKER_${i}_content_`.repeat(250) }); + } + return input; +} + +async function startProxy(upstreamPort: number): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-resp": { context: 10_000 } } } }, + modelContextLimit: 10_000, + kernelConfig: defaultConfig(10_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return proxy; +} + +test("#2733: codex client → summary payload carries the real CLI wire shape; strict relay accepts it, fold applies", async () => { + const summaryCalls: SummaryCall[] = []; + const upstream = makeUpstream(summaryCalls, true); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const proxy = await startProxy(upstreamPort); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; + + try { + const r = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": "codex_cli_rs/0.25.0" }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "s2733-codex", instructions: "You are the test coding agent.", input: longInput() }), + }); + assert.equal(r.status, 200, `preflight must fold despite the strict codex-shape validator (before the fix every summary call was 400'd), got ${r.status}`); + const text = await r.text(); + void text; + + assert.ok(summaryCalls.length >= 1, `a summarization call must have been made (got ${summaryCalls.length})`); + assert.ok(summaryCalls.every((c) => c.codexShaped), + `every summary call must carry the codex CLI signature (typed message items + include:["reasoning.encrypted_content"]), got ${JSON.stringify(summaryCalls)}`); + + const sess = listSessions().find((s) => s.meta.label === "s2733-codex"); + assert.ok(sess, "session exists"); + assert.ok((sess!.state.blocks ?? []).some((b) => b.active), "the preflight fold landed on the session"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); + +test("#2733: non-codex client → summary payload stays the legacy minimal shape (no codex signature leak)", async () => { + const summaryCalls: SummaryCall[] = []; + const upstream = makeUpstream(summaryCalls, false); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const proxy = await startProxy(upstreamPort); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; + + try { + const r = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": "openai-node/4.0.0" }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "s2733-noncodex", instructions: "You are the test coding agent.", input: longInput() }), + }); + assert.equal(r.status, 200); + await r.text(); + + assert.ok(summaryCalls.length >= 1, `a summarization call must have been made (got ${summaryCalls.length})`); + assert.ok(summaryCalls.every((c) => !c.codexShaped), + `non-codex clients must keep the legacy minimal shape (no typed items, no include), got ${JSON.stringify(summaryCalls)}`); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +});