Skip to content

The agent that grows with you

fix(state): escape a result's own _stored key so reads never decode it

unsupportedpastelsdac05882f9cd
diff --git a/hermes_state_terminal.py b/hermes_state_terminal.py
index bb2400defb7..af444a2c9c8 100644
--- a/hermes_state_terminal.py
+++ b/hermes_state_terminal.py
@@ -59,9 +59,14 @@ def encode_result(result):
"""The stored row keeps the reply once. When the turn's closing assistant message carries
exactly ``final_response``, its ``content`` is stored as null and the row gets
``_stored: {v: 1, reply_ref: <that message's index>}``. Retirement drops ``messages`` and
- keeps ``final_response``, so a deleted session's exact retry still has its reply."""
- value = result.get('result') if isinstance(result, dict) else None
- if not isinstance(value, dict) or _STORED in result or not isinstance(value.get('messages'), list):
+ keeps ``final_response``, so a deleted session's exact retry still has its reply. A result
+ that already has ``_stored`` is stored whole with that value escaped as ``{v: 1, raw: ...}``."""
+ if not isinstance(result, dict):
+ return result
+ if _STORED in result:
+ return {**result, _STORED: {'v': 1, 'raw': result[_STORED]}}
+ value = result.get('result')
+ if not isinstance(value, dict) or not isinstance(value.get('messages'), list):
return result
reply, messages = value.get('final_response'), list(value['messages'])
last = next((i for i in range(len(messages) - 1, -1, -1) if isinstance(messages[i], dict)
@@ -79,6 +84,9 @@ def load_result(text):
result = json.loads(text)
if not isinstance(result, dict) or not isinstance(envelope := result.get(_STORED), dict):
return result
+ if envelope.keys() == {'v', 'raw'} and type(envelope['v']) is int and envelope['v'] == 1:
+ result[_STORED] = envelope['raw']
+ return result
value, ref = result.get('result'), envelope.get('reply_ref')
messages = value.get('messages') if isinstance(value, dict) else None
if not (envelope.keys() == {'v', 'reply_ref'} and type(envelope['v']) is int and envelope['v'] == 1
diff --git a/tests/gateway/test_admission_result_reply_once.py b/tests/gateway/test_admission_result_reply_once.py
index a00742b22b2..34b861e3067 100644
--- a/tests/gateway/test_admission_result_reply_once.py
+++ b/tests/gateway/test_admission_result_reply_once.py
@@ -1,6 +1,8 @@
"""LC-9: a settled turn result stores the reply once, and every reader still gets it exactly."""
import json
+import pytest
+
from gateway.session_api_turn import _settled_api_result
from gateway.session_bot import _admission_outcome
from gateway.session_results import _redacted, admission_result, finish_result, retain_result
@@ -174,13 +176,14 @@ def test_a_legacy_row_with_any_message_fields_reads_back_unchanged(owner):
assert json.dumps(admission_result(owner.db, row['admission_id'])) == old
-def test_a_result_that_already_has_the_envelope_key_is_stored_verbatim(owner):
[email protected]('theirs', ['not ours', {'v': 1, 'reply_ref': 0}, {'v': 1, 'raw': 'x'}])
+def test_a_result_that_already_has_the_envelope_key_round_trips_unchanged(owner, theirs):
+ """Even a producer-built ``_stored`` shaped exactly like the envelope is never decoded."""
result = {'result': {'final_response': 'final', 'current_turn_user_idx': 0, 'messages': [
- {'role': 'user', 'content': 'q'}, {'role': 'assistant', 'content': 'final'}]},
- 'usage': {}, '_stored': 'not ours'}
+ {'role': 'user', 'content': 'q'}, {'role': 'assistant', 'content': None}]},
+ '_stored': theirs, 'usage': {}}
admission_id, expected = _stored_round_trip(owner, result)
assert json.dumps(admission_result(owner.db, admission_id)) == json.dumps(expected)
- assert encode_result(expected) == expected
def test_the_encoded_row_decodes_to_the_identical_dict_and_key_order(owner):