The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): replay the sidecar-only assistant row when rebuilding history from the transcript
teknium1578bff8ef71a
diff --git a/agent/turn_final_response.py b/agent/turn_final_response.pyindex 889c2e99af0..9dba07c3427 100644--- a/agent/turn_final_response.py+++ b/agent/turn_final_response.py@@ -171,6 +171,10 @@ def finish_text_response()codex_ack_continuations += 1interim_msg = agent._build_assistant_message(assistant_message, "incomplete")+ if _promoted:+ # Same sidecar as the final row: the wire copy must carry the promoted text, not only+ # ``reasoning_content``, or the continuation replays an empty assistant turn.+ interim_msg["api_content"] = final_responseappend_message(messages, interim_msg)agent._emit_interim_assistant_message(interim_msg)append_message(messages, {"role": "user", "content": _CODEX_ACK_CONTINUATION_NUDGE})diff --git a/gateway/run.py b/gateway/run.pyindex ffd2d59a9c5..e3cdcdf6882 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -1192,6 +1192,20 @@ def _message_timestamps_enabled(user_config: Optional[dict]) -> bool:return bool(mt)+def _has_replayable_sidecar(role: Any, content: Any, msg: Dict[str, Any]) -> bool:+ """True for an assistant row whose reply lives only in the ``api_content`` sidecar.++ A reasoning-only clean stop persists ``content=""`` and the promoted text in ``api_content``+ (agent/turn_final_response.py). Gating replay on ``content`` alone dropped that row, so the+ next gateway turn lost the assistant's answer and replayed user->user."""+ return (+ role == "assistant"+ and not content+ and isinstance(msg.get("api_content"), str)+ and bool(msg.get("api_content"))+ )++def _build_gateway_agent_history(history: List[Dict[str, Any]], *, channel_prompt: Optional[str] = None,inject_timestamps: bool = False) -> tuple[List[Dict[str, Any]], Optional[str]]:@@ -1226,7 +1240,7 @@ def _build_gateway_agent_history(if "tool_calls" in msg or "tool_call_id" in msg or role == "tool":clean_msg = {k: v for k, v in msg.items() if k not in {"timestamp", "observed"}}agent_history.append(clean_msg)- elif content:+ elif content or _has_replayable_sidecar(role, content, msg):replay_timestamp = msg.get("timestamp")# Clean before rendering: a timestamp prefix hides recovery notes# from the startswith-based stripper. Retain an embedded original time.diff --git a/tests/gateway/test_replay_entry_fields.py b/tests/gateway/test_replay_entry_fields.pyindex 4bb27e82756..99c97340492 100644--- a/tests/gateway/test_replay_entry_fields.py+++ b/tests/gateway/test_replay_entry_fields.py@@ -143,3 +143,19 @@ class TestGatewayHistoryBuildForwardsSidecar:agent_history, _obs = _build_gateway_agent_history(history)assert agent_history[0]["api_content"] == "hi\n\nCTX"+++def test_gateway_history_keeps_sidecar_only_assistant_row():+ """A reasoning-only clean stop persists content="" with the promoted reply in ``api_content``+ (agent/turn_final_response.py); the gateway rebuild must replay it, not drop it (user->user)."""+ from gateway.run import _build_gateway_agent_history++ history = [+ {"role": "user", "content": "2+2?"},+ {"role": "assistant", "content": "", "reasoning": "The answer is 4.", "api_content": "The answer is 4."},+ {"role": "user", "content": "thanks"},+ ]+ agent_history, _ = _build_gateway_agent_history(history)+ assert [m["role"] for m in agent_history] == ["user", "assistant", "user"]+ assert agent_history[1]["api_content"] == "The answer is 4."+ assert agent_history[1]["reasoning"] == "The answer is 4."