fix(responses): clamp call_id to 64 chars and sanitize replayed fn names - #49224
Open
lubosxyz wants to merge 1 commit into
Open
fix(responses): clamp call_id to 64 chars and sanitize replayed fn names#49224lubosxyz wants to merge 1 commit into
lubosxyz wants to merge 1 commit into
Conversation
The Responses API enforces a 64-character hard limit on call_id values.
In the background-review replay path, _chat_messages_to_responses_input
reconstructs a call_id from the stored response_item_id when no explicit
call_id is present: 'call_' + fc_id[3:]. A typical fc_ id is ~67 chars,
making the synthetic call_id 69 chars — over the limit. The API rejects
this with a non-retryable HTTP 400 ('Invalid input[N].call_id: string too
long, max 64'), which propagates as a BrokenPipeError crash in the codex
app-server pipe and takes the whole gateway turn down.
A second related issue: replayed function_call names can carry invalid
characters (dots, spaces) if the model degenerated on an earlier turn.
The API requires names to match ^[a-zA-Z0-9_-]+ and rejects other chars
with a non-retryable HTTP 400 ('Invalid input[N].name').
Fixes:
- Add _clamp_call_id(): deterministic SHA-256 hash-clamp for ids > 64
chars. Determinism is critical — a function_call and its paired
function_call_output share the same raw call_id string, so hashing
identically keeps the pair matched. Short ids pass through unchanged,
preserving prompt-cache prefix hits.
- Add _sanitize_fn_name(): coerce replayed function_call names to the
^[a-zA-Z0-9_-]+ contract. Applied only to replay input items (not
live tool definitions, which must match the tool registry).
- Apply both helpers in _preflight_codex_input_items, the single
normalisation choke-point before the request is sent to the API.
Contributor
|
Thanks for targeting a real Responses replay failure. Current Problems
Suggested changes
Automated hermes-sweeper review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When the background-review replay path fires in a long-running Codex app-server session, the gateway crashes with a
BrokenPipeErrorthat kills the entire turn:A second, related 400 fires when a replayed function_call carries an invalid name:
Both errors are non-retryable — the Responses API returns HTTP 400, not 429 or 5xx — so neither the retry loop nor the credential pool recovers. The gateway turn is lost.
Root cause
call_id overflow —
agent/codex_responses_adapter.py,_chat_messages_to_responses_input(assistant tool_calls block):When a tool call is stored with only a
response_item_id(anfc_string, typically ~67 characters) and no explicitcall_id, the reconstruction synthesises:This synthesised value is then passed unchanged through
_preflight_codex_input_items(call_id.strip()) and emitted directly to the API.Invalid function name — same file, same function, function_call replay item:
A replayed function_call carries the
nameas-is from conversation history. If the model degenerated on a previous turn and emitted a name containing dots, spaces, or other characters outside^[a-zA-Z0-9_-]+, the replay fails with a 400.Fix
Two new helpers added in
agent/codex_responses_adapter.py, in the ID-helpers section alongside_deterministic_call_id:_clamp_call_id(call_id: str) -> strlen(cid) <= 64(the common case — no regression, prompt-cache prefix hits preserved).call_orfc_).function_calland its pairedfunction_call_outputcarry the same raw call_id string, so hashing identically maps both to the same clamped value, keeping the pair matched as required by the Responses API._sanitize_fn_name(name: str) -> str[A-Za-z0-9_-]with_, collapses runs, strips leading/trailing underscores, truncates to 64 chars.call_id, so renaming a replayed name is safe.Both helpers are applied in
_preflight_codex_input_items, the single normalisation choke-point executed immediately before every Responses API request.Repro
api_mode = "codex_app_server") for several turns involving tool calls with longfc_ids (e.g.fc_+ 64 hex chars = 67 chars total)._chat_messages_to_responses_inputreconstructscall_idas"call_" + fc_id[3:]→ 69 chars._preflight_codex_input_itemsemits the 69-char value verbatim.Without this fix the gateway logs show:
Test plan
_clamp_call_id("call_" + "x" * 60)→len(...) == 64;"call_" + "x" * 10passes unchanged; same input always maps to same output (determinism);function_callandfunction_call_outputwith the same raw id clamp identically._sanitize_fn_name("exec.command")→"exec_command";_sanitize_fn_name("a" * 70)→len == 64; empty / all-invalid →"fn".function_callin the input list with a 69-charcall_id; assert_preflight_codex_input_itemsno longer raises and the outputcall_idis<= 64chars._preflight_codex_input_itemsstill pass; normal short call_ids are not mutated.🤖 Generated with Claude Code