Skip to content

The agent that grows with you

fix(gateway): hand the loop's proven turn boundary to the stored receipt

Teknium4184782fc050
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.py
index 7ed7b8d2de0..37568dad8dc 100644
--- a/gateway/run_turn_runner.py
+++ b/gateway/run_turn_runner.py
@@ -1219,6 +1219,9 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
# Same rule as the direct API path (``_finish_turn_result``): the returned `messages` replaced the
# transcript, so a stored receipt must keep it whole instead of cutting it to this turn's suffix.
**({"_compressed": True} if compacted_in_place or effective_session_id != ctx.session_id else {}),
+ # The loop's proven current-turn row (``export_current_turn_boundary``) for the same `messages`: the
+ # stored receipt cuts there instead of re-guessing from text (multimodal or rewritten input + a nudge).
+ **({"current_turn_user_idx": result["current_turn_user_idx"]} if "current_turn_user_idx" in result else {}),
**usage,
**({"persisted_turn": persisted_turn} if persisted_turn else {}),
}
diff --git a/tests/gateway/test_runner_compressed_receipt.py b/tests/gateway/test_runner_compressed_receipt.py
index 4523d1463a7..54ad1c9f210 100644
--- a/tests/gateway/test_runner_compressed_receipt.py
+++ b/tests/gateway/test_runner_compressed_receipt.py
@@ -32,3 +32,25 @@ def test_runner_compression_keeps_the_replacement_transcript_in_the_receipt(monk
assert stored['messages'] == result['messages'] and len(stored['messages']) == 3, (
'compression replacement cut to the turn suffix: the next chained turn re-sends uncompressed history')
+
+class _NudgedImageAgent(_CompressionThenFailureAgent):
+ def run_conversation(self, user_message, conversation_history=None, task_id=None, **_kwargs):
+ # The stored row differs from the payload text (image hint appended), then a verify-on-stop nudge.
+ messages = [*(conversation_history or []), {'role': 'user', 'content': user_message + '\n[Image attached at: /x.png]'},
+ {'role': 'assistant', 'content': '', 'tool_calls': [{'id': 't1', 'type': 'function',
+ 'function': {'name': 'read_file', 'arguments': '{}'}}]},
+ {'role': 'tool', 'tool_call_id': 't1', 'content': 'tool-output-sentinel'},
+ {'role': 'user', 'content': 'Before finishing, verify.', '_verification_stop_synthetic': True},
+ {'role': 'assistant', 'content': 'answer'}]
+ return {'final_response': 'answer', 'completed': True, 'api_calls': 2, 'messages': messages,
+ 'current_turn_user_idx': len(conversation_history or [])}
+
+
+def test_runner_passes_the_proven_turn_boundary_so_a_nudge_cannot_erase_tool_output(monkeypatch):
+ from hermes_state_terminal import compact_result
+ _install_compression_failure_agent(monkeypatch, _NudgedImageAgent)
+ source = SessionSource(platform=Platform.TELEGRAM, chat_id='12345', chat_type='dm', user_id='user-1')
+ result = _run_compression_failure_turn(_runner(_SessionStore()), source)
+ stored = compact_result({'result': result}, user_message=[{'type': 'text', 'text': 'continue'}])['result']
+ assert 'tool-output-sentinel' in str(stored['messages']), 'boundary re-guessed onto the nudge'
+ assert stored['messages'][-1] == {'role': 'assistant', 'content': 'answer'} and len(stored['messages']) == 4