Skip to content

The agent that grows with you

fix: preserve structured Unicode through worker payload and receipt encoding

Tekniumf56e660e0184
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index 68179192e8a..a47e47fbec4 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -437,7 +437,7 @@ def persist_worker_message(db, *, epoch: int, execution_id: str, session_id: str
message = conn.execute('INSERT INTO messages(session_id,role,content,timestamp) VALUES(?,?,?,?)', (session_id, role, db._encode_content(content), now))
conn.execute('UPDATE sessions SET message_count=message_count+1,last_activity_at=?,runtime_revision=runtime_revision+1 WHERE id=?', (now, session_id))
result = {'message_id': message.lastrowid}
- conn.execute('INSERT INTO worker_receipts(execution_id,sequence,payload_digest,result_json) VALUES(?,?,?,?)', (execution_id, sequence, digest, _json(result)))
+ conn.execute('INSERT INTO worker_receipts(execution_id,sequence,payload_digest,result_json) VALUES(?,?,?,?)', (execution_id, sequence, digest, json.dumps(result, ensure_ascii=True, allow_nan=False)))
conn.execute("UPDATE worker_executions SET last_sequence=?,status='running' WHERE execution_id=?", (sequence, execution_id))
return result
return db._execute_write(write)
@@ -548,7 +548,7 @@ def mutate_worker_execution(db, *, epoch, execution_id, session_id, generation,
if type(sequence) is not int or sequence < 1 or not isinstance(operation, str) or operation not in handlers:
raise RuntimeStoreError('invalid_params')
encoded = _json(payload)
- if len(encoded.encode('utf-8')) > 4 * 1024 * 1024:
+ if len(encoded.encode('utf-8', errors='surrogatepass')) > 4 * 1024 * 1024:
raise RuntimeStoreError('invalid_params')
digest = admission_fingerprint(canonical_target=session_id,
payload={'operation': operation, 'payload': json.loads(encoded)})
@@ -570,7 +570,7 @@ def mutate_worker_execution(db, *, epoch, execution_id, session_id, generation,
# Each SQLite retry gets fresh rows; rolled-back annotations must not escape.
result = handlers[operation](db, conn, session_id, json.loads(encoded))
conn.execute('INSERT INTO worker_receipts(execution_id,sequence,payload_digest,result_json) VALUES(?,?,?,?)',
- (execution_id, sequence, digest, _json(result)))
+ (execution_id, sequence, digest, json.dumps(result, ensure_ascii=True, allow_nan=False)))
conn.execute("UPDATE worker_executions SET last_sequence=?,status=? WHERE execution_id=?",
(sequence, 'terminal' if operation == 'execution.finish' else 'running', execution_id))
conn.execute('UPDATE sessions SET runtime_revision=runtime_revision+1 WHERE id=?', (session_id,))
diff --git a/tests/state/test_runtime_worker_operations.py b/tests/state/test_runtime_worker_operations.py
index 704b204f094..6fbde4aeb20 100644
--- a/tests/state/test_runtime_worker_operations.py
+++ b/tests/state/test_runtime_worker_operations.py
@@ -21,7 +21,7 @@ def test_structured_worker_receipts_are_atomic_and_scoped(tmp_path):
operation=op, payload=payload)
assert apply(1, 'turn.acquire', {'holder': 'worker-lease', 'ttl_seconds': 300})['value']
messages = [
- {'role': 'user', 'content': [{'type': 'text', 'text': 'hello'}], 'api_content': 'wire'},
+ {'role': 'user', 'content': [{'type': 'text', 'text': 'hello\ud800'}], 'api_content': 'wire'},
{'role': 'assistant', 'content': None, 'reasoning': 'private reason',
'tool_calls': [{'id': 'call1', 'type': 'function', 'function': {'name': 'terminal', 'arguments': '{}'}}]},
{'role': 'tool', 'content': 'tool-marker', 'tool_call_id': 'call1', 'tool_name': 'terminal'},
@@ -46,12 +46,12 @@ def test_structured_worker_receipts_are_atomic_and_scoped(tmp_path):
apply(3, 'SQL', {'sql': 'DELETE FROM messages'})
assert apply(3, 'turn.renew', {'holder': 'worker-lease', 'ttl_seconds': 300})['value']
assert apply(4, 'turn.release', {'holder': 'worker-lease'}) == {'value': None}
- assistant = {'role': 'assistant', 'content': 'canonical winner'}
+ assistant = {'role': 'assistant', 'content': [{'type': 'text', 'text': 'canonical winner\ud800'}]}
winner = apply(5, 'transcript.append', {'messages': [assistant]})
repaired = apply(6, 'transcript.append', {'messages': [{'role': 'assistant',
'content': 'loser', '_row_id': winner['annotations'][0]['_row_id']}]})
assert repaired['count'] == 0
- assert repaired['annotations'][0]['_canonical_content'] == 'canonical winner'
+ assert repaired['annotations'][0]['_canonical_content'] == assistant['content']
terminal = apply(7, 'execution.finish', {})
assert terminal['status'] == 'terminal'
assert apply(7, 'execution.finish', {}) == terminal