The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): a canonical rewind refuses a target that is not a live user row
rodricksz4h5ad26c2c7c548
diff --git a/hermes_state_mutations.py b/hermes_state_mutations.pyindex 95afd1629cb..5bf6688e34a 100644--- a/hermes_state_mutations.py+++ b/hermes_state_mutations.py@@ -122,6 +122,14 @@ def _rewind(db, conn, session_id, payload):from hermes_state_local_lineage import local_physical_targetphysical = local_physical_target(conn, session_id)require_idle(db, conn, list({session_id, physical}))+ # The client names the row: like a branch boundary it must be an active row of the physical+ # target. A row compaction archived (still shown, still carrying its id) would deactivate the+ # summary and every later turn, since both have larger ids.+ row = conn.execute('SELECT role FROM messages WHERE id=? AND session_id=? AND active=1',+ (payload['target_message_id'], physical)).fetchone()+ if row is None or row['role'] != 'user':+ raise RuntimeStoreError('invalid_params', field='target_message_id',+ detail='target user message is no longer in session history')# 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'])diff --git a/tests/hermes_state/test_runtime_rewind_mutations.py b/tests/hermes_state/test_runtime_rewind_mutations.pyindex 967859dde52..85b56073613 100644--- a/tests/hermes_state/test_runtime_rewind_mutations.py+++ b/tests/hermes_state/test_runtime_rewind_mutations.py@@ -32,3 +32,45 @@ def test_rewind_refuses_queued_work(tmp_path):rt.mutate_runtime_session(db, epoch=epoch, principal_id='human', session_id='s', request_id='rewind',expected_revision=0, expected_generation=0, operation='rewind', payload={'target_message_id': target})assert db.get_messages('s')[0]['content'] == 'keep'+++def _compacted_session(db):+ """Q1..A2 archived by in-place compaction (still listed with include_compacted), then Q3/A3."""+ db.create_session('s', source='test')+ db.append_messages_batch('s', [{'role': 'user', 'content': 'Q1'}, {'role': 'assistant', 'content': 'A1'},+ {'role': 'user', 'content': 'Q2'}, {'role': 'assistant', 'content': 'A2'}])+ db.archive_and_compact('s', [{'role': 'user', 'content': '[CONTEXT SUMMARY] Q1..A2'},+ {'role': 'assistant', 'content': 'ok, continuing'}])+ db.append_messages_batch('s', [{'role': 'user', 'content': 'Q3'}, {'role': 'assistant', 'content': 'A3'}])+ shown = {m['content']: m['id'] for m in db.get_messages('s', include_compacted=True)}+ return shown, rt.begin_runtime_epoch(db, instance_id='owner')+++def _rewind_to(db, epoch, target, request_id='rewind'):+ return rt.mutate_runtime_session(db, epoch=epoch, principal_id='human', session_id='s', request_id=request_id,+ expected_revision=0, expected_generation=0, operation='rewind', payload={'target_message_id': target})+++def test_rewind_refuses_a_row_compaction_archived(tmp_path):+ with SessionDB(db_path=tmp_path / 'state.db') as db:+ shown, epoch = _compacted_session(db)+ live = ['[CONTEXT SUMMARY] Q1..A2', 'ok, continuing', 'Q3', 'A3']+ assert [m['content'] for m in db.get_messages('s')] == live+ with pytest.raises(rt.RuntimeStoreError, match='invalid_params') as refused:+ _rewind_to(db, epoch, shown['Q2'])+ assert refused.value.field == 'target_message_id'+ assert [m['content'] for m in db.get_messages('s')] == live+ assert db.get_session('s')['runtime_generation'] == 0+ # The live turn after the compaction still rewinds normally.+ receipt = _rewind_to(db, epoch, shown['Q3'], request_id='live')+ assert receipt['rewound_count'] == 2+ assert [m['content'] for m in db.get_messages('s')] == live[:2]++[email protected]('target', ['missing', 'assistant'])+def test_rewind_refuses_a_target_that_is_not_a_live_user_row(tmp_path, target):+ with SessionDB(db_path=tmp_path / 'state.db') as db:+ shown, epoch = _compacted_session(db)+ with pytest.raises(rt.RuntimeStoreError, match='invalid_params'):+ _rewind_to(db, epoch, 99999 if target == 'missing' else shown['A3'])+ assert [m['content'] for m in db.get_messages('s')][-2:] == ['Q3', 'A3']