fix(codex): sanitize replayed function_call.name to Responses API pattern (#31666)

A degenerate tool name stored in conversation history (dots, spaces,
unicode from an earlier model degeneration) bricks every subsequent
Codex Responses turn with a non-retryable HTTP 400:
  Invalid input[N].name: string does not match pattern '^[a-zA-Z0-9_-]+'

The 400 replays forever until the user manually starts a new session.

Add _sanitize_replayed_fn_name() — replaces invalid chars with '_'
(runs collapsed), degrades all-invalid names to 'fn' instead of empty
(an empty name would trade one 400 for a preflight ValueError).  Applied
at both replay sites: the chat-message converter and the preflight
choke-point.  Live tool-definition names are left untouched — they must
match the dispatch registry exactly.  Pairing is by call_id, so
renaming a replayed function_call is safe.

call_id overflow (the sibling half of #49224) was already fixed on main
by #73492 (_clamp_responses_call_id); this commit covers the remaining
invalid-name defect.

Credit: @Morad37 (#31678 — identified the bug, the replay sites, and
the regex contract), @lubosxyz (#49224 — replace-not-strip semantics
and 'fn' fallback to avoid the empty-name trap).

Fixes #31666
This commit is contained in:
kshitijk4poor
2026-08-26 01:45:36 +05:30
committed by kshitij
parent 34c5fcb2a2
commit 31485d50ea
2 changed files with 128 additions and 30 deletions
+39 -2
View File
@@ -288,6 +288,43 @@ def _clamp_responses_call_id(call_id: str) -> str:
return f"call_{digest}"
# The Responses API enforces the same 64-char cap on function names as on
# input item ids (_MAX_RESPONSES_ITEM_ID_LENGTH) — names over the cap are
# rejected with the same non-retryable 400 as pattern violations.
_VALID_RESPONSES_FN_NAME_RE = re.compile(r"[a-zA-Z0-9_-]{1,64}")
def _sanitize_replayed_fn_name(name: str) -> str:
"""Coerce a *replayed* function_call name to the Responses API contract.
The Responses API requires ``function_call.name`` to match
``^[a-zA-Z0-9_-]+$`` and rejects the whole request with a non-retryable
HTTP 400 otherwise (issue #31666). A name with invalid characters (dots,
spaces, unicode — e.g. from an earlier model degeneration) stored in
conversation history therefore bricks every subsequent turn of the
session: the 400 replays forever until the user manually starts a new
conversation.
Invalid characters are replaced with ``_`` (runs collapsed) rather than
stripped, so an all-invalid name degrades to the ``"fn"`` placeholder
instead of an empty string — an empty name would just trade one
non-retryable 400 for a preflight ValueError. Valid names pass through
unchanged, preserving prompt-cache prefixes.
Apply this ONLY to replayed function_call input items, never to live
tool definitions: tool schema names must match the dispatch registry
exactly. Pairing with function_call_output is by call_id, so renaming
a replayed function_call is safe.
"""
if not isinstance(name, str):
return "fn"
if _VALID_RESPONSES_FN_NAME_RE.fullmatch(name):
return name
coerced = re.sub(r"[^A-Za-z0-9_-]", "_", name.strip())
coerced = re.sub(r"_+", "_", coerced).strip("_")
return coerced[:64] or "fn"
def _split_responses_tool_id(raw_id: Any) -> tuple[Optional[str], Optional[str]]:
"""Split a stored tool id into (call_id, response_item_id)."""
if not isinstance(raw_id, str):
@@ -688,7 +725,7 @@ def _chat_messages_to_responses_input(
items.append({
"type": "function_call",
"call_id": _clamp_responses_call_id(call_id),
"name": fn_name,
"name": _sanitize_replayed_fn_name(fn_name),
"arguments": arguments,
})
item_sources.append(msg)
@@ -813,7 +850,7 @@ def _preflight_codex_input_items(
{
"type": "function_call",
"call_id": call_id.strip(),
"name": name.strip(),
"name": _sanitize_replayed_fn_name(name),
"arguments": arguments,
}
)
+89 -28
View File
@@ -4,6 +4,7 @@ import pytest
from agent.codex_responses_adapter import (
_chat_messages_to_responses_input,
_sanitize_replayed_fn_name,
_format_responses_error,
_normalize_codex_response,
_neutralize_harmony_tokens,
@@ -227,8 +228,6 @@ def test_normalize_codex_response_treats_summary_only_reasoning_as_incomplete():
assert assistant_message.codex_reasoning_items is None
# ---------------------------------------------------------------------------
# Server-side built-in tool calls (xAI native web_search, code interpreter,
# etc.) come back as discrete ``*_call`` output items that xAI's
@@ -243,10 +242,6 @@ def test_normalize_codex_response_treats_summary_only_reasoning_as_incomplete():
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Replayed assistant message items with an oversized server-assigned ``id``
# (Codex issues 400+ char base64 blobs) must never reach the API — the
@@ -261,10 +256,6 @@ _OVERSIZED_ITEM_ID = "x" * 408
_VALID_ITEM_ID = "msg_abc123"
# The codex app-server overflows the Responses 64-char call_id limit for
# MCP-routed tools, e.g. codex_mcp__hermes-tools__web_search_exec-<uuid> (#73492).
_OVERSIZED_CALL_ID = "codex_mcp__hermes-tools__web_search_exec-" + "0" * 43
@@ -331,8 +322,96 @@ def test_chat_messages_to_responses_input_keeps_short_call_id():
assert output["call_id"] == "call_abc123"
def test_sanitize_replayed_fn_name_valid_passthrough():
"""Valid names pass through unchanged (identity — cache-prefix safe)."""
for name in ("web_search", "exec-command", "a1_B2-c3", "x" * 64):
assert _sanitize_replayed_fn_name(name) == name
def test_sanitize_replayed_fn_name_coerces_invalid_chars():
assert _sanitize_replayed_fn_name("exec.command") == "exec_command"
assert _sanitize_replayed_fn_name("run shell cmd") == "run_shell_cmd"
assert _sanitize_replayed_fn_name("weird..__name") == "weird_name"
assert _sanitize_replayed_fn_name(" tool! ") == "tool"
def test_sanitize_replayed_fn_name_degenerate_inputs():
"""All-invalid / non-string names degrade to a placeholder, never empty —
an empty name would trade the API 400 for a preflight ValueError."""
assert _sanitize_replayed_fn_name("") == "fn"
assert _sanitize_replayed_fn_name("...") == "fn"
assert _sanitize_replayed_fn_name("日本語") == "fn"
assert _sanitize_replayed_fn_name(None) == "fn"
assert len(_sanitize_replayed_fn_name("a." * 100)) <= 64
def test_chat_messages_to_responses_input_sanitizes_replayed_fn_name():
"""A degenerate tool name stored in history must not brick the replay
with a non-retryable 400 (#31666)."""
messages = [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"call_id": "call_abc123",
"function": {"name": "exec.command", "arguments": "{}"},
}
],
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "some result",
},
]
items = _chat_messages_to_responses_input(messages)
call = next(i for i in items if i.get("type") == "function_call")
output = next(i for i in items if i.get("type") == "function_call_output")
assert call["name"] == "exec_command"
# Pairing is by call_id and must survive the rename.
assert call["call_id"] == output["call_id"] == "call_abc123"
def test_preflight_codex_input_items_sanitizes_replayed_fn_name():
"""The preflight choke-point also coerces invalid replayed names
(covers callers that build input items without the chat converter)."""
normalized = _preflight_codex_input_items(
[
{
"type": "function_call",
"call_id": "call_1",
"name": "bad name!",
"arguments": "{}",
},
{"type": "function_call_output", "call_id": "call_1", "output": "ok"},
]
)
call = next(i for i in normalized if i.get("type") == "function_call")
assert call["name"] == "bad_name"
def test_preflight_codex_api_kwargs_leaves_tool_definition_names_alone():
"""Live tool schema names must NOT be rewritten — they have to match the
dispatch registry exactly. Sanitization is replay-only."""
kwargs = _preflight_codex_api_kwargs(
{
"model": "gpt-5-codex",
"instructions": "x",
"input": [{"role": "user", "content": "hi"}],
"tools": [
{
"type": "function",
"name": "my_tool",
"description": "",
"parameters": {"type": "object", "properties": {}},
}
],
}
)
assert kwargs["tools"][0]["name"] == "my_tool"
def test_preflight_codex_input_items_drops_short_id_for_github_responses():
@@ -408,8 +487,6 @@ def test_preflight_passes_native_web_search_tool_through():
assert any(t.get("type") == "function" and t.get("name") == "read_file" for t in tools)
# ---------------------------------------------------------------------------
# _format_responses_error — adapted from anomalyco/opencode#28757.
# Provider failures should surface BOTH the code (rate_limit_exceeded /
@@ -419,25 +496,11 @@ def test_preflight_passes_native_web_search_tool_through():
# ---------------------------------------------------------------------------
def test_format_responses_error_message_only():
err = {"message": "Upstream model unavailable"}
assert _format_responses_error(err, "failed") == "Upstream model unavailable"
def test_normalize_codex_response_failed_includes_code_in_error():
"""Regression: response_status == 'failed' should surface the error
code, not just the message. Used to leak a bare 'Slow down' string
@@ -461,8 +524,6 @@ def test_normalize_codex_response_failed_includes_code_in_error():
_normalize_codex_response(response)
# ---------------------------------------------------------------------------
# Reasoning-channel answer salvage (xAI grok) — grok-4.x on the xAI
# /v1/responses surface sometimes emits its final answer inside the