Skip to content

The agent that grows with you

fix(state): owner rewind of a compaction carrier keeps its handoff

Teknium1a2d911f08b1
diff --git a/hermes_state_mutations.py b/hermes_state_mutations.py
index 81b14ffe359..83215aece95 100644
--- a/hermes_state_mutations.py
+++ b/hermes_state_mutations.py
@@ -99,16 +99,30 @@ def _rewind(db, conn, session_id, payload):
from hermes_state_local_lineage import local_physical_target
physical = local_physical_target(conn, session_id)
require_idle(db, conn, list({session_id, physical}))
- target, ids, head, replacement = rewind_in_transaction(db, conn, physical,
- payload['target_message_id'], preserve_compaction_handoff=payload.get('preserve_compaction_handoff', False))
+ # A composite compaction carrier (summary + the next ask in one row) keeps its handoff, as
+ # main's rewind does; the receipt reports the live ask, so /retry resubmits it, not the summary.
+ live = _carrier_live_view(db, conn, physical, payload['target_message_id'])
+ target, ids, head, replacement = rewind_in_transaction(db, conn, physical, payload['target_message_id'],
+ preserve_compaction_handoff=live is not None or payload.get('preserve_compaction_handoff', False))
# Receipts are JSON: publish the same public message shape get_messages() returns (decoded
# content/tool_calls, no internal display_identity BLOB / display_order).
target = db._row_to_message_dict(target, warn_context='rewind receipt', summary_flag=True)
+ if live is not None:
+ target['content'] = live.get('content')
conn.execute('UPDATE sessions SET runtime_generation=runtime_generation+1 WHERE id=?', (session_id,))
return {session_id, physical}, {'rewound_count': len(ids), 'target_message': target,
'new_head_id': head, 'replacement_message_id': replacement}
+def _carrier_live_view(db, conn, session_id, message_id):
+ from agent.context_compressor import split_user_originated_turn
+ row = conn.execute('SELECT * FROM messages WHERE id=? AND session_id=? AND active=1',
+ (message_id, session_id)).fetchone()
+ message = row and db._row_to_message_dict(row, warn_context='rewind target', summary_flag=True)
+ handoff, live = split_user_originated_turn(message) if message else (None, None)
+ return live if handoff is not None else None
+
+
def _import(db, conn, session_id, payload):
rows = payload['sessions']
if len(rows) > db._IMPORT_MAX_SESSIONS:
diff --git a/tests/hermes_state/test_runtime_rewind_carrier_handoff.py b/tests/hermes_state/test_runtime_rewind_carrier_handoff.py
new file mode 100644
index 00000000000..abd50840ccb
--- /dev/null
+++ b/tests/hermes_state/test_runtime_rewind_carrier_handoff.py
@@ -0,0 +1,32 @@
+"""An owner ``rewind`` of a composite compaction carrier keeps its handoff (main parity)."""
+from agent.context_compressor import HISTORICAL_TASK_HEADING, SUMMARY_PREFIX, _SUMMARY_END_MARKER
+from hermes_state import SessionDB
+import hermes_state_runtime as rt
+
+
+def _rewind(db, target, request_id, **extra):
+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')
+ session = db.get_session('s')
+ return rt.mutate_runtime_session(db, epoch=epoch, principal_id='human', session_id='s', request_id=request_id,
+ expected_revision=session.get('runtime_revision') or 0, expected_generation=session['runtime_generation'],
+ operation='rewind', payload={'target_message_id': target, **extra})
+
+
+def test_rewind_of_carrier_keeps_handoff_and_reports_the_live_ask(tmp_path):
+ carrier = f"{SUMMARY_PREFIX}\n{HISTORICAL_TASK_HEADING}\nold task\n\n{_SUMMARY_END_MARKER}\n\nREAL ASK"
+ with SessionDB(db_path=tmp_path / 'state.db') as db:
+ db.create_session('s', source='test')
+ db.append_message('s', 'user', 'older ask')
+ db.append_message('s', 'assistant', 'older reply')
+ target = db.append_message('s', 'user', carrier)
+ db.append_message('s', 'assistant', 'reply')
+ receipt = _rewind(db, target, 'rewind')
+ assert receipt['rewound_count'] == 2
+ assert receipt['target_message']['content'] == 'REAL ASK'
+ active = db.get_messages_as_conversation('s')
+ assert [m['role'] for m in active] == ['user', 'assistant', 'user']
+ assert SUMMARY_PREFIX in active[-1]['content'] and 'REAL ASK' not in active[-1]['content']
+ assert db.get_session('s')['runtime_generation'] == 1
+ # A rewind to before the compacted span still drops the summary.
+ _rewind(db, db.get_messages('s')[0]['id'], 'rewind-all')
+ assert db.get_messages_as_conversation('s') == []