Skip to content

Commit e65819b

Browse files
committed
fix oversized Responses call IDs on replay
1 parent c63589c commit e65819b

3 files changed

Lines changed: 173 additions & 1 deletion

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createHash } from "node:crypto";
12
import type { IncomingMeta, ProviderAdapter } from "./base";
23
import type { AdapterEvent, OcxParsedRequest, OcxProviderConfig } from "../types";
34
import { catalogModelSupportsReasoningSummaries } from "../codex/catalog";
@@ -328,6 +329,55 @@ function isPlainObject(v: unknown): v is Record<string, unknown> {
328329
return !!v && typeof v === "object" && !Array.isArray(v);
329330
}
330331

332+
const MAX_RESPONSES_CALL_ID_LENGTH = 64;
333+
const REPAIRED_CALL_ID_PREFIX = "call_ocx_";
334+
const REPAIRED_CALL_ID_DIGEST_LENGTH = MAX_RESPONSES_CALL_ID_LENGTH - REPAIRED_CALL_ID_PREFIX.length;
335+
336+
/**
337+
* The ChatGPT Responses backend rejects input `call_id` values longer than 64 characters. Codex
338+
* sidechat/fork replay can namespace call ids from routed providers past that limit. Forward mode
339+
* already sends explicit replay input without `previous_response_id`, so it is safe to replace each
340+
* oversized id and every matching call/output occurrence with one deterministic request-local alias.
341+
* Raw API-key continuations are intentionally excluded because an output-only continuation may
342+
* reference a call stored upstream under the original id. Proxy-expanded API-key replays are
343+
* explicit and stateless here, so they are safe to repair too.
344+
*/
345+
function repairOversizedReplayCallIds(body: unknown): unknown {
346+
if (!isPlainObject(body) || !Array.isArray(body.input)) return body;
347+
348+
const occupied = new Set<string>();
349+
for (const item of body.input) {
350+
if (!isPlainObject(item) || typeof item.call_id !== "string") continue;
351+
if (item.call_id.length <= MAX_RESPONSES_CALL_ID_LENGTH) occupied.add(item.call_id);
352+
}
353+
354+
const aliases = new Map<string, string>();
355+
let changed = false;
356+
const input = body.input.map(item => {
357+
if (!isPlainObject(item) || typeof item.call_id !== "string") return item;
358+
const original = item.call_id;
359+
if (original.length <= MAX_RESPONSES_CALL_ID_LENGTH) return item;
360+
361+
let alias = aliases.get(original);
362+
if (!alias) {
363+
let salt = 0;
364+
do {
365+
const hashInput = salt === 0 ? original : `${original}\0${salt}`;
366+
const digest = createHash("sha256").update(hashInput).digest("hex");
367+
alias = `${REPAIRED_CALL_ID_PREFIX}${digest.slice(0, REPAIRED_CALL_ID_DIGEST_LENGTH)}`;
368+
salt += 1;
369+
} while (occupied.has(alias));
370+
aliases.set(original, alias);
371+
occupied.add(alias);
372+
}
373+
374+
changed = true;
375+
return { ...item, call_id: alias };
376+
});
377+
378+
return changed ? { ...body, input } : body;
379+
}
380+
331381
/** Flatten a Responses tool-output `output` value (string or content-part array) to plain text. */
332382
function toolOutputText(output: unknown): string {
333383
if (typeof output === "string") return output;
@@ -515,8 +565,13 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
515565
parsed._rawBody,
516566
forward || parsed._previousResponseInputExpanded === true,
517567
);
518-
if (forward) outBody = repairOrphanedInputItems(outBody, unexpandedMiss);
568+
if (forward) {
569+
outBody = repairOrphanedInputItems(outBody, unexpandedMiss);
570+
}
519571
else outBody = stripConflictingHostedTools(outBody);
572+
if (forward || parsed._previousResponseInputExpanded === true) {
573+
outBody = repairOversizedReplayCallIds(outBody);
574+
}
520575
outBody = stripUnsupportedReasoningSummaryDelivery(outBody, parsed.modelId);
521576
return {
522577
url,

‎structure/04_transports-and-sidecars.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ messages until the round completes, reattaching real results to their original c
142142
and synthesizing explicit "no tool result was recorded" answers only when no real result exists
143143
(Kimi/Moonshot 400 `ocx-mrqaiw05-269`; unit `devlog/_plan/260718_dangling_toolcall_hardening`).
144144

145+
Forward-mode OpenAI passthrough also repairs replayed `call_id` values longer than the Responses
146+
API's 64-character limit. Sidechat/fork replay can namespace routed-provider ids beyond that limit,
147+
so each oversized id and all matching call/output items receive the same deterministic,
148+
request-local alias. Raw API-key continuations deliberately preserve ids because an output-only
149+
continuation may reference a call stored upstream under its original id; proxy-expanded API-key
150+
replays are explicit and receive the same repair.
151+
145152
These compatibility guards are covered by focused tests and should stay close to the adapters that
146153
need them.
147154

‎tests/openai-responses-passthrough.test.ts‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,116 @@ describe("OpenAI Responses passthrough sanitization", () => {
432432
expect(body.input).toEqual(input);
433433
});
434434

435+
test("forward mode repairs oversized call ids consistently across paired replay items", () => {
436+
const adapter = createResponsesPassthroughAdapter(provider);
437+
const oversizedCallId = `call_${"x".repeat(80)}`;
438+
const input = [
439+
{ type: "function_call", call_id: oversizedCallId, name: "ping", arguments: "{}" },
440+
{ type: "function_call_output", call_id: oversizedCallId, output: "pong" },
441+
{ type: "function_call", call_id: "call_short", name: "keep", arguments: "{}" },
442+
{ type: "function_call_output", call_id: "call_short", output: "kept" },
443+
];
444+
445+
const body = JSON.parse(adapter.buildRequest({
446+
modelId: "gpt-5.6-sol",
447+
context: { messages: [] },
448+
stream: true,
449+
options: {},
450+
_rawBody: { model: "gpt-5.6-sol", input },
451+
}, meta).body) as { input: Record<string, unknown>[] };
452+
453+
const repairedCallId = body.input[0].call_id as string;
454+
expect(repairedCallId).toStartWith("call_ocx_");
455+
expect(repairedCallId.length).toBeLessThanOrEqual(64);
456+
expect(body.input[1].call_id).toBe(repairedCallId);
457+
expect(body.input[2].call_id).toBe("call_short");
458+
expect(body.input[3].call_id).toBe("call_short");
459+
expect(input[0].call_id).toBe(oversizedCallId);
460+
expect(input[1].call_id).toBe(oversizedCallId);
461+
});
462+
463+
test("forward mode assigns distinct stable aliases to oversized custom and tool-search pairs", () => {
464+
const adapter = createResponsesPassthroughAdapter(provider);
465+
const customCallId = `call_custom_${"a".repeat(80)}`;
466+
const searchCallId = `call_search_${"b".repeat(80)}`;
467+
const input = [
468+
{ type: "custom_tool_call", call_id: customCallId, name: "apply_patch", input: "patch" },
469+
{ type: "custom_tool_call_output", call_id: customCallId, output: "done" },
470+
{ type: "tool_search_call", call_id: searchCallId, execution: "client", arguments: {} },
471+
{ type: "tool_search_output", call_id: searchCallId, tools: [] },
472+
];
473+
474+
const build = () => JSON.parse(adapter.buildRequest({
475+
modelId: "gpt-5.6-sol",
476+
context: { messages: [] },
477+
stream: true,
478+
options: {},
479+
_rawBody: { model: "gpt-5.6-sol", input },
480+
}, meta).body) as { input: Record<string, unknown>[] };
481+
482+
const first = build().input;
483+
const second = build().input;
484+
expect(first[0].call_id).toBe(first[1].call_id);
485+
expect(first[2].call_id).toBe(first[3].call_id);
486+
expect(first[0].call_id).not.toBe(first[2].call_id);
487+
expect((first[0].call_id as string).length).toBeLessThanOrEqual(64);
488+
expect((first[2].call_id as string).length).toBeLessThanOrEqual(64);
489+
expect(second.map(item => item.call_id)).toEqual(first.map(item => item.call_id));
490+
});
491+
492+
test("api-key mode preserves oversized call ids that may reference upstream stored state", () => {
493+
const adapter = createResponsesPassthroughAdapter({
494+
adapter: "openai-responses",
495+
baseUrl: "https://api.openai.example/v1",
496+
authMode: "key" as const,
497+
apiKey: "sk-test",
498+
});
499+
const oversizedCallId = `call_${"stored".repeat(14)}`;
500+
const input = [
501+
{ type: "function_call_output", call_id: oversizedCallId, output: "pong" },
502+
];
503+
504+
const body = JSON.parse(adapter.buildRequest({
505+
...parsedBase,
506+
_rawBody: {
507+
model: "gpt-5.5",
508+
previous_response_id: "resp_stored",
509+
input,
510+
},
511+
}, meta).body) as { previous_response_id: string; input: Array<{ call_id: string }> };
512+
513+
expect(body.previous_response_id).toBe("resp_stored");
514+
expect(body.input[0]?.call_id).toBe(oversizedCallId);
515+
});
516+
517+
test("api-key mode repairs oversized call ids after proxy-expanded replay", () => {
518+
const adapter = createResponsesPassthroughAdapter({
519+
adapter: "openai-responses",
520+
baseUrl: "https://api.openai.example/v1",
521+
authMode: "key" as const,
522+
apiKey: "sk-test",
523+
});
524+
const oversizedCallId = `call_${"expanded".repeat(12)}`;
525+
526+
const body = JSON.parse(adapter.buildRequest({
527+
...parsedBase,
528+
_previousResponseInputExpanded: true,
529+
_rawBody: {
530+
model: "gpt-5.5",
531+
previous_response_id: "resp_expanded",
532+
input: [
533+
{ type: "function_call", call_id: oversizedCallId, name: "ping", arguments: "{}" },
534+
{ type: "function_call_output", call_id: oversizedCallId, output: "pong" },
535+
],
536+
},
537+
}, meta).body) as { previous_response_id?: string; input: Array<{ call_id: string }> };
538+
539+
expect(body.previous_response_id).toBeUndefined();
540+
expect(body.input[0]?.call_id).toStartWith("call_ocx_");
541+
expect(body.input[0]?.call_id.length).toBe(64);
542+
expect(body.input[1]?.call_id).toBe(body.input[0]?.call_id);
543+
});
544+
435545
test("forward expanded replay keeps reasoning items (chain is intact)", () => {
436546
const adapter = createResponsesPassthroughAdapter(provider);
437547
const body = JSON.parse(adapter.buildRequest({

0 commit comments

Comments
 (0)