fix(codex): drop foreign replay message ids

(cherry picked from commit b58b94e16ccf5c8e01813f25f6a1a94dfb17f79b)
This commit is contained in:
fangliquanflq
2026-08-22 23:15:22 +08:00
committed by kshitij
parent 7959e3b0ff
commit f7b6a2b59f
3 changed files with 80 additions and 5 deletions
+18 -5
View File
@@ -317,11 +317,18 @@ def _message_item(
return item
def _assistant_message_item(raw: Dict[str, Any], content: List[Dict[str, Any]], *, is_github_responses: bool) -> Dict[str, Any]:
def _assistant_message_item(
raw: Dict[str, Any], content: List[Dict[str, Any]], *, is_github_responses: bool,
current_issuer_kind: Optional[str] = None,
) -> Dict[str, Any]:
"""Replayable assistant ``message`` item from a stored one. ``id`` is kept only when short enough and never for
GitHub Copilot (ids bind to a backend connection; stale → 401); ``phase`` is preserved per OpenAI's cache guidance."""
GitHub Copilot (ids bind to a backend connection; stale → 401); ``phase`` is preserved per OpenAI's cache guidance.
The ChatGPT Codex backend additionally rejects ids that do not begin with ``msg`` (foreign Responses issuers
mint short UUIDs), so those are dropped there while other issuers' policies are unchanged."""
item_id, phase = raw.get("id"), raw.get("phase")
keep_id = not is_github_responses and _nonblank(item_id) and len(item_id.strip()) <= _MAX_RESPONSES_ITEM_ID_LENGTH
if keep_id and current_issuer_kind == "codex_backend" and not item_id.strip().startswith("msg"):
keep_id = False
return _message_item(
content, status=_normalize_responses_message_status(raw.get("status")),
item_id=item_id.strip() if keep_id else None, phase=phase.strip() if _nonblank(phase) else None,
@@ -360,7 +367,9 @@ def _replay_reasoning_items(
return replayed
def _replay_message_items(msg: Dict[str, Any], *, is_github_responses: bool) -> List[Dict[str, Any]]:
def _replay_message_items(
msg: Dict[str, Any], *, is_github_responses: bool, current_issuer_kind: Optional[str] = None,
) -> List[Dict[str, Any]]:
"""Replay exact assistant message items (id/phase) for prefix-cache hits."""
replayed: List[Dict[str, Any]] = []
for raw_item in _as_list(msg.get("codex_message_items")):
@@ -372,7 +381,9 @@ def _replay_message_items(msg: Dict[str, Any], *, is_github_responses: bool) ->
if isinstance(part, dict) and str(part.get("type") or "").strip() in _OUTPUT_TEXT_TYPES
]
if content:
replayed.append(_assistant_message_item(raw_item, content, is_github_responses=is_github_responses))
replayed.append(_assistant_message_item(
raw_item, content, is_github_responses=is_github_responses, current_issuer_kind=current_issuer_kind,
))
return replayed
@@ -490,7 +501,9 @@ def _chat_messages_to_responses_input(
native_compaction_eligible=native_compaction_eligible,
)
emit(reasoning_items, msg)
message_items = _replay_message_items(msg, is_github_responses=is_github_responses)
message_items = _replay_message_items(
msg, is_github_responses=is_github_responses, current_issuer_kind=current_issuer_kind,
)
emit(message_items, msg)
if not message_items:
# Every reasoning item needs a following item (else missing_following_item), hence the "" fallback.
@@ -310,6 +310,7 @@ def test_normalize_codex_response_treats_summary_only_reasoning_as_incomplete():
_OVERSIZED_ITEM_ID = "x" * 408
_VALID_ITEM_ID = "msg_abc123"
_FOREIGN_ITEM_ID = "123e4567-e89b-12d3-a456-426614174000"
# The codex app-server overflows the Responses 64-char call_id limit for
@@ -522,6 +523,38 @@ def test_preflight_codex_input_items_drops_short_id_for_github_responses():
assert items[0]["content"] == [{"type": "output_text", "text": "pong"}]
def test_chat_messages_to_responses_input_drops_foreign_id_for_codex_backend():
messages = [
{
"role": "assistant",
"content": "pong",
"codex_message_items": [
{
"type": "message",
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": "pong"}],
"id": _FOREIGN_ITEM_ID,
"phase": "final_answer",
}
],
}
]
codex_items = _chat_messages_to_responses_input(
messages, current_issuer_kind="codex_backend"
)
xai_items = _chat_messages_to_responses_input(
messages, current_issuer_kind="xai_responses"
)
codex_message = next(item for item in codex_items if item.get("type") == "message")
xai_message = next(item for item in xai_items if item.get("type") == "message")
assert "id" not in codex_message
assert codex_message["phase"] == "final_answer"
assert xai_message["id"] == _FOREIGN_ITEM_ID
def test_preflight_codex_api_kwargs_drops_oversized_message_id_end_to_end():
kwargs = _preflight_codex_api_kwargs(
{
@@ -229,6 +229,35 @@ class TestCodexBuildKwargs:
message_item = next(item for item in kw["input"] if item.get("type") == "message")
assert message_item["id"] == "msg_short_id"
def test_codex_backend_drops_foreign_message_item_id_end_to_end(self, transport):
messages = [
{
"role": "assistant",
"content": "pong",
"codex_message_items": [
{
"type": "message",
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": "pong"}],
"id": "123e4567-e89b-12d3-a456-426614174000",
"phase": "final_answer",
}
],
},
]
kw = transport.build_kwargs(
model="gpt-5.5",
messages=messages,
tools=[],
is_codex_backend=True,
)
message_item = next(item for item in kw["input"] if item.get("type") == "message")
assert "id" not in message_item
assert message_item["phase"] == "final_answer"
@pytest.mark.parametrize("model", [
"gpt-5.5",
"gpt-5.5-pro",