Skip to content

The agent that grows with you

refactor(gateway): compute the failed-turn notice once; the reply and the boundary row share it

kshitijk4poorb9f543951e7f
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index ed721f72b2d..ca04c21e2e7 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -1530,8 +1530,6 @@ class GatewayTurnMixin:
def _hmwa_add_failed_turn_notice(self, response, notice):
"""Make failed-turn delivery explicit without replacing the provider-specific guidance."""
response = str(response or "").strip()
- if notice in response:
- return response
return f"{response}\n\n{notice}" if response else notice
def _hmwa_failed_turn_notice(self, agent_result):
@@ -1550,13 +1548,6 @@ class GatewayTurnMixin:
return self._PARTIAL_FAILED_TURN_NOTICE
return self._FAILED_TURN_NOTICE
- @staticmethod
- def _hmwa_failed_turn_boundary_row(notice, ts):
- """Gateway-owned assistant row closing a failed turn: a user-only tail lets alternation repair
- merge this request into an unrelated future message and replay stale side effects. Carries
- only the stable safety statement, never raw provider details."""
- return {"role": "assistant", "content": notice, "timestamp": ts}
-
def _hmwa_classify_turn_failure(self, agent_result, history, session_entry):
"""Classify a finished turn for transcript persistence. Returns
``(agent_failed_early, hidden_reasoning_incomplete, is_context_overflow_failure)``.
@@ -1663,10 +1654,12 @@ class GatewayTurnMixin:
async def _hmwa_persist_turn_transcript(
self, *, event, source, session_entry, session_key, agent_result, agent_messages,
prepared, response, agent_failed_early, hidden_reasoning_incomplete, is_context_overflow_failure,
+ failed_turn_notice=None,
):
"""Persist this turn to the transcript (session_meta on first turn, closed failed turn on
transient failure, nothing on context overflow), update last_prompt_tokens, and re-baseline the
- cached agent's message count."""
+ cached agent's message count. *failed_turn_notice* is the SAME string the user was shown; it
+ closes the failed turn as a gateway-owned assistant row."""
from gateway.run import _resolve_gateway_model
ts = time.time() # Unix epoch float — consistent with DB storage
store = self.async_session_store
@@ -1708,10 +1701,13 @@ class GatewayTurnMixin:
)
else:
await store.append_to_transcript(sid, _user_row, skip_db=agent_persisted)
- # The boundary belongs to the user row it closes; a deduped retry already has one.
- await store.append_to_transcript(
- sid, self._hmwa_failed_turn_boundary_row(self._hmwa_failed_turn_notice(agent_result), ts),
- )
+ # Gateway-owned assistant row closing the failed turn: a user-only tail lets
+ # alternation repair merge this request into an unrelated future message and replay
+ # stale side effects. Belongs to the user row it closes — a deduped retry has one.
+ await store.append_to_transcript(sid, {
+ "role": "assistant", "timestamp": ts,
+ "content": failed_turn_notice or self._hmwa_failed_turn_notice(agent_result),
+ })
else:
# Only the NEW messages: history_offset (what the agent saw), not len(history), which
# counts session_meta entries stripped before the agent saw them.
@@ -1816,10 +1812,10 @@ class GatewayTurnMixin:
await self.async_session_store.append_to_transcript(
session_entry.session_id, self._hmwa_user_transcript_entry(event, prepared, time.time()),
)
- await self.async_session_store.append_to_transcript(
- session_entry.session_id,
- self._hmwa_failed_turn_boundary_row(self._PARTIAL_FAILED_TURN_NOTICE, time.time()),
- )
+ # Same boundary row as the persist path: tool effects are unknown after an exception.
+ await self.async_session_store.append_to_transcript(session_entry.session_id, {
+ "role": "assistant", "content": self._PARTIAL_FAILED_TURN_NOTICE, "timestamp": time.time(),
+ })
except Exception:
logger.debug("Failed to persist inbound user message after agent exception", exc_info=True)
# Never expose raw exception types/messages to end users (info-leakage risk).
@@ -2058,8 +2054,11 @@ class GatewayTurnMixin:
agent_failed_early, hidden_reasoning_incomplete, is_context_overflow_failure = (
self._hmwa_classify_turn_failure(agent_result, history, session_entry)
)
- if agent_failed_early and not is_context_overflow_failure:
- response = self._hmwa_add_failed_turn_notice(response, self._hmwa_failed_turn_notice(agent_result))
+ failed_turn_notice = None
+ if (agent_failed_early or hidden_reasoning_incomplete) and not is_context_overflow_failure:
+ failed_turn_notice = self._hmwa_failed_turn_notice(agent_result)
+ if agent_failed_early and failed_turn_notice:
+ response = self._hmwa_add_failed_turn_notice(response, failed_turn_notice)
response, session_entry = await self._hmwa_compression_exhaustion_reset(
agent_result, response, session_entry, session_key, source,
)
@@ -2068,7 +2067,7 @@ class GatewayTurnMixin:
agent_result=agent_result, agent_messages=agent_messages, prepared=prepared,
response=response, agent_failed_early=agent_failed_early,
hidden_reasoning_incomplete=hidden_reasoning_incomplete,
- is_context_overflow_failure=is_context_overflow_failure,
+ is_context_overflow_failure=is_context_overflow_failure, failed_turn_notice=failed_turn_notice,
)
return await self._hmwa_deliver_turn_response(
event, source, session_entry, session_key, run_generation,
diff --git a/tests/gateway/test_42039_duplicate_user_message.py b/tests/gateway/test_42039_duplicate_user_message.py
index 73dd8f70dac..38129eb7fb0 100644
--- a/tests/gateway/test_42039_duplicate_user_message.py
+++ b/tests/gateway/test_42039_duplicate_user_message.py
@@ -152,7 +152,7 @@ async def test_agent_failed_early_skip_db_when_agent_has_session_db(
_assert_user_call_has_skip_db(
runner.session_store.append_to_transcript.call_args_list, True
)
- assert "not processed" in response
+ assert runner._FAILED_TURN_NOTICE in response
transcript_rows = [
call.args[1]
@@ -160,7 +160,7 @@ async def test_agent_failed_early_skip_db_when_agent_has_session_db(
if len(call.args) >= 2 and call.args[1].get("role") in {"user", "assistant"}
]
assert [row["role"] for row in transcript_rows] == ["user", "assistant"]
- assert "not processed" in transcript_rows[-1]["content"]
+ assert transcript_rows[-1]["content"] == runner._FAILED_TURN_NOTICE
# The next unrelated input remains its own turn instead of alternation repair
# merging the failed mutating request into it.
diff --git a/tests/gateway/test_failure_writer_ownership.py b/tests/gateway/test_failure_writer_ownership.py
index eb1cd5b10bf..d93aac7b5b6 100644
--- a/tests/gateway/test_failure_writer_ownership.py
+++ b/tests/gateway/test_failure_writer_ownership.py
@@ -104,10 +104,8 @@ def test_failure_owner_follows_only_live_lineage_markers(tmp_path):
assert store.has_input_owner(sid, owner), location
live_messages = db.get_messages(child)
assert live_messages[-1]["role"] == "assistant"
- assert "Some actions may already have run" in live_messages[-1]["content"]
- assert "not processed" not in live_messages[-1]["content"]
- assert "Some actions may already have run" in reply
- assert "not processed" not in reply
+ assert live_messages[-1]["content"] == runner._PARTIAL_FAILED_TURN_NOTICE
+ assert runner._PARTIAL_FAILED_TURN_NOTICE in reply
if not owned:
persisted_user = live_messages[-2]
assert persisted_user["content"] == prepared.persist_user_message
diff --git a/tests/gateway/test_incomplete_gateway_turns.py b/tests/gateway/test_incomplete_gateway_turns.py
index 1f429afe580..51783948a0a 100644
--- a/tests/gateway/test_incomplete_gateway_turns.py
+++ b/tests/gateway/test_incomplete_gateway_turns.py
@@ -151,8 +151,7 @@ async def test_incomplete_codex_turn_closes_transcript_without_slack_delivery(mo
assert transcript_roles == ["session_meta", "user", "assistant"]
assert runner.session_store.append_to_transcript.call_args_list[1].args[1]["content"] == "hello"
boundary = runner.session_store.append_to_transcript.call_args_list[2].args[1]["content"]
- assert "not processed" in boundary
- assert "remained incomplete" not in boundary
+ assert boundary == runner._FAILED_TURN_NOTICE
assert adapter.processing_hooks == [
("start", "m-1"),
("complete", "m-1", ProcessingOutcome.SUCCESS),