fix(gateway): replay the sidecar-only assistant row when rebuilding history from the transcript

The parent commit persists a reasoning-only clean stop as ``content=""`` with the
promoted reply in ``api_content``. The messaging gateway rebuilds agent_history
from the transcript every turn, and ``_build_gateway_agent_history`` gated
replay on ``elif content:`` — so that row vanished, the assistant's answer left
the context and the tail replayed user->user. Treat an assistant row whose reply
lives only in a non-empty ``api_content`` sidecar as replayable; the existing
``_build_replay_entry`` keeps the sidecar because content is unchanged.

Also stamp ``api_content`` on the interim row of the stall-guard / codex-ack
continuation branch when the reasoning was promoted, so the wire copy of that
interim assistant turn carries the text instead of only ``reasoning_content``.

Part of #111761
This commit is contained in:
teknium1 authored and Teknium committed 2026-09-16 17:21:57 -07:00
1 parent eb11e7eac9
commit 578bff8ef7
3 files changed
+35 -1

No files matched your search

+4
View File
@@ -171,6 +171,10 @@ def finish_text_response(
)
codex_ack_continuations += 1
interim_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_response
append_message(messages, interim_msg)
agent._emit_interim_assistant_message(interim_msg)
append_message(messages, {"role": "user", "content": _CODEX_ACK_CONTINUATION_NUDGE})
+15 -1
View File
@@ -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.
+16
View File
@@ -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."