The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): keep the retired-session marker past the retry horizon
unsupportedpastels45f4f06a4665
diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.pyindex e3eb636909b..cc0e08d117f 100644--- a/hermes_state_mutation_retirement.py+++ b/hermes_state_mutation_retirement.py@@ -216,10 +216,12 @@ _EXPIRED = "json_valid(value) AND json_extract(value, '$.retired_at') < ?"def prune_retired_tombstones(conn, older_than=RETRY_HORIZON_S):"""Reclaim deletion tombstones retired more than *older_than* seconds ago: the terminal admission- with its identity pointer and compacted result, terminal workers and the retired-session marker.+ with its identity pointer and compacted result, and terminal workers. The retired-session marker+ is the deletion fence itself and is never reclaimed: it holds only ``retired_at``, and without it+ a deleted id could be recreated, re-admitted or reissued as a Bot Chat generation.Tombstones written before ``retired_at`` existed are stamped now and age from this pass."""from hermes_state_terminal import ADMISSION_PREFIX, RESULT_PREFIX, WORKER_PREFIX, identity_key- prefixes = (ADMISSION_PREFIX, WORKER_PREFIX, RETIRED_PREFIX)+ prefixes = (ADMISSION_PREFIX, WORKER_PREFIX)now = time.time()for prefix in prefixes:conn.execute("UPDATE state_meta SET value=json_set(value, '$.retired_at', ?) WHERE key GLOB ? "@@ -230,5 +232,4 @@ def prune_retired_tombstones(conn, older_than=RETRY_HORIZON_S):row = json.loads(raw)conn.execute('DELETE FROM state_meta WHERE key IN (?,?,?)', (key, RESULT_PREFIX + row['admission_id'],identity_key(row['principal_id'], row['target_session_id'], row['request_id'])))- for prefix in prefixes[1:]:- conn.execute(f'DELETE FROM state_meta WHERE key GLOB ? AND {_EXPIRED}', (prefix + '*', cutoff))+ conn.execute(f'DELETE FROM state_meta WHERE key GLOB ? AND {_EXPIRED}', (WORKER_PREFIX + '*', cutoff))diff --git a/tests/hermes_state/test_retired_fence_outlives_horizon.py b/tests/hermes_state/test_retired_fence_outlives_horizon.pynew file mode 100644index 00000000000..e8c3d9923fc--- /dev/null+++ b/tests/hermes_state/test_retired_fence_outlives_horizon.py@@ -0,0 +1,52 @@+"""The retired-session marker is the deletion fence: the prune pass reclaims a deleted session's+tombstones after the retry horizon, but never the marker, so the id stays deleted."""+import time+from types import SimpleNamespace++import pytest++import hermes_state_runtime as rt+from gateway.session_results import retain_result+from hermes_state import SessionDB+++def _delete_8_days_ago(db, epoch, sid):+ snap = db.get_session(sid)+ at = time.time() - 8 * 86400+ with pytest.MonkeyPatch.context() as clock:+ clock.setattr(time, "time", lambda: at)+ rt.mutate_runtime_session(db, epoch=epoch, principal_id="human", session_id=sid, request_id="del",+ operation="delete", payload={}, expected_revision=snap["runtime_revision"],+ expected_generation=snap["runtime_generation"])+++def test_a_deleted_session_is_never_recreated_after_the_horizon(tmp_path):+ from hermes_state_mutation_retirement import RETIRED_PREFIX+ with SessionDB(tmp_path / "state.db") as db:+ epoch = rt.begin_runtime_epoch(db, instance_id="owner")+ db.create_session("s", source="api_server")+ args = dict(principal_id="api", session_id="s", request_id="r1", payload={"text": "charge card"})+ rt.admit_session_input(db, epoch=epoch, **args)+ row = rt.claim_session_input(db, epoch=epoch, session_id="s")+ retain_result(db, epoch=epoch, row=row, result={"result": {"final_response": "done"}, "usage": {}})+ _delete_8_days_ago(db, epoch, "s")+ db.prune_sessions(older_than_days=90)+ assert db.get_meta(RETIRED_PREFIX + "s") is not None+ with pytest.raises(rt.RuntimeStoreError, match="not_found"):+ db.ensure_session("s", source="unknown") # a late backfill+ with pytest.raises(rt.RuntimeStoreError, match="not_found"):+ rt.admit_session_input(db, epoch=epoch, **args) # the old exact retry+ assert db.get_session("s") is None+++def test_a_deleted_bot_chat_generation_is_never_reissued(tmp_path):+ from gateway.session_bot import _bot_chat_creation_id+ from gateway.session_local_recovery import local_identity+ with SessionDB(tmp_path / "state.db") as db:+ epoch = rt.begin_runtime_epoch(db, instance_id="owner")+ authority, actor = SimpleNamespace(profile_id="owned", db=db), SimpleNamespace(subject="owner")+ sid = local_identity("owned", "owner", "bot-chat:owned")+ db.create_session(sid, source="cli")+ _delete_8_days_ago(db, epoch, sid)+ db.prune_sessions(older_than_days=90)+ assert _bot_chat_creation_id(authority, actor) == "bot-chat:owned:1"diff --git a/tests/hermes_state/test_terminal_retirement.py b/tests/hermes_state/test_terminal_retirement.pyindex 55949a04b6b..32c4341c850 100644--- a/tests/hermes_state/test_terminal_retirement.py+++ b/tests/hermes_state/test_terminal_retirement.py@@ -333,7 +333,7 @@ def test_retirement_drops_the_turn_user_row_copy(tmp_path):def test_tombstones_past_the_retry_horizon_are_reclaimed_by_the_prune_pass(tmp_path, monkeypatch):"""Deletion tombstones keep byte-exact retry replies for the 7-day horizon, then the maintenance- prune pass reclaims every row they hold (B2-06)."""+ prune pass reclaims every row they hold but the deletion fence itself (B2-06)."""import timeimport hermes_state_mutation_retirement as retirementnow = time.time()@@ -365,7 +365,8 @@ def test_tombstones_past_the_retry_horizon_are_reclaimed_by_the_prune_pass(tmp_passert len(rows_naming('old', old_id)) == 4db.prune_sessions(older_than_days=90)assert len(rows_naming('young', young_id)) == 4- assert rows_naming('old', old_id) == [], 'tombstones past the horizon were not reclaimed'+ assert rows_naming('old', old_id) == [retirement.RETIRED_PREFIX + 'old'], \+ 'tombstones past the horizon were not reclaimed (the deletion fence stays)'with pytest.raises(rt.RuntimeStoreError, match='not_found'):rt.admit_session_input(db, epoch=epoch, **old)replay = rt.admit_session_input(db, epoch=epoch, **young)