Skip to content

The agent that grows with you

fix(gateway): keep a compressed turn's transcript whole in its stored receipt

Teknium305b2c84c95c
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.py
index c8495e8d0de..7ed7b8d2de0 100644
--- a/gateway/run_turn_runner.py
+++ b/gateway/run_turn_runner.py
@@ -1216,6 +1216,9 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
"compression_deferred": result.get("compression_deferred", False),
"tools": ctx.tools_holder[0] or [],
"history_offset": history_offset, "compacted_in_place": compacted_in_place, "session_id": effective_session_id,
+ # 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 {}),
**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
new file mode 100644
index 00000000000..4523d1463a7
--- /dev/null
+++ b/tests/gateway/test_runner_compressed_receipt.py
@@ -0,0 +1,34 @@
+"""A TurnRunner result that compressed the transcript is stored whole, never cut to a turn suffix."""
+import pytest
+
+from tests.gateway.test_compression_failure_session_sync import (
+ _CompressionThenFailureAgent, _install_compression_failure_agent, _run_compression_failure_turn, _runner,
+ _SessionStore)
+from gateway.config import Platform
+from gateway.session import SessionSource
+
+
+class _CompressingAgent(_CompressionThenFailureAgent):
+ rotate = True
+
+ def run_conversation(self, user_message, conversation_history=None, task_id=None, **_kwargs):
+ if self.rotate:
+ self.session_id = 'session-after-compression'
+ else:
+ self._last_compaction_in_place = True
+ return {'final_response': 'compressed answer', 'completed': True, 'api_calls': 1, 'messages': [
+ {'role': 'user', 'content': '[CONTEXT COMPACTION] summary', '_compressed_summary': True},
+ {'role': 'user', 'content': user_message}, {'role': 'assistant', 'content': 'compressed answer'}]}
+
+
[email protected]('rotate', [True, False], ids=['rotated', 'in_place'])
+def test_runner_compression_keeps_the_replacement_transcript_in_the_receipt(monkeypatch, rotate):
+ from hermes_state_terminal import compact_result
+ monkeypatch.setattr(_CompressingAgent, 'rotate', rotate)
+ _install_compression_failure_agent(monkeypatch, _CompressingAgent)
+ 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='continue')['result']
+ 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')
+