fix(gateway): a deduped platform retry of a failed turn adds no second boundary row

The boundary row closes the user row it follows. When Telegram redelivers
the same failed message the dedupe branch skips the user row, so writing the
boundary unconditionally stacked two consecutive assistant rows and the
notice would be concatenated twice on replay. Append it only alongside the
user row; one test replays the retry.
This commit is contained in:
kshitijk4poor
2026-09-11 11:54:44 +05:30
committed by kshitij
parent 07b0c7e1bc
commit 79680b04bd
2 changed files with 30 additions and 3 deletions
+4 -3
View File
@@ -1708,9 +1708,10 @@ class GatewayTurnMixin:
)
else:
await store.append_to_transcript(sid, _user_row, skip_db=agent_persisted)
await store.append_to_transcript(
sid, self._hmwa_failed_turn_boundary_row(self._hmwa_failed_turn_notice(agent_result), ts),
)
# 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),
)
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.
@@ -171,6 +171,32 @@ async def test_agent_failed_early_skip_db_when_agent_has_session_db(
assert replay[-1]["content"] == "unrelated question"
@pytest.mark.asyncio
async def test_deduped_retry_of_failed_turn_adds_no_second_boundary(monkeypatch, tmp_path):
"""A platform retry of the same failed message (dedupe skips the user row) must not stack a
second boundary behind the first — that would be two consecutive assistant rows."""
runner = _bootstrap(monkeypatch, tmp_path)
runner.session_store.has_platform_message_id.return_value = True
runner._run_agent = AsyncMock(
return_value={
"failed": True,
"final_response": "API call failed after 3 retries: 429 Too Many Requests",
"error": "429 Too Many Requests — rate limit exceeded",
"messages": [],
"history_offset": 0,
"last_prompt_tokens": 0,
}
)
await runner._handle_message_with_agent(_event(), _source(), "agent:main:telegram:group:-1001:12345", 1)
assert [
call.args[1]["role"] for call in runner.session_store.append_to_transcript.call_args_list
if len(call.args) >= 2 and call.args[1].get("role") in {"user", "assistant"}
] == []
@pytest.mark.asyncio
async def test_failed_turn_with_tool_activity_does_not_recommend_blind_retry(
monkeypatch, tmp_path