Skip to content

The agent that grows with you

fix(state): fence late accounting backfills against retired session IDs

teknium1092ff048fa00
diff --git a/hermes_state_worker_lifecycle.py b/hermes_state_worker_lifecycle.py
index 4675b546e65..814dffe99bf 100644
--- a/hermes_state_worker_lifecycle.py
+++ b/hermes_state_worker_lifecycle.py
@@ -176,6 +176,13 @@ def insert_session_row_in_transaction(
validated before entry; this helper never opens or commits a DB.
"""
from hermes_state_sessions import _UPSERT_KEEP_EXISTING_SQL
+ from hermes_state_mutation_retirement import RETIRED_PREFIX
+ # A delayed constructor/accounting backfill must not recreate a deleted session
+ # beside its durable tombstone (the exact delete retry would then report a stale
+ # success while a fresh delete collides with the existing marker).
+ if (conn.execute('SELECT 1 FROM sessions WHERE id=?', (session_id,)).fetchone() is None
+ and conn.execute('SELECT 1 FROM state_meta WHERE key=?', (RETIRED_PREFIX + session_id,)).fetchone()):
+ raise RuntimeStoreError('not_found')
if not (profile_name or "").strip():
profile_name = self._own_profile_name()
system_prompt_hash = self._store_system_prompt(conn, system_prompt)
diff --git a/tests/state/test_terminal_retirement.py b/tests/state/test_terminal_retirement.py
index 0a5755ef3b8..2ed4eea1dde 100644
--- a/tests/state/test_terminal_retirement.py
+++ b/tests/state/test_terminal_retirement.py
@@ -1,5 +1,7 @@
"""Deletion removes history but retains exact, scoped terminal evidence."""
import sqlite3
+from types import SimpleNamespace
+
import pytest
from hermes_state import SessionDB
import hermes_state_runtime as rt
@@ -88,3 +90,34 @@ def test_nonterminal_obligations_prevent_any_retirement(tmp_path):
assert db.get_session(status) is not None
with db._read_ctx() as c:
assert not c.execute("SELECT 1 FROM state_meta WHERE key LIKE 'gateway.retired_session.v1.%'").fetchall()
+
+
+def test_late_accounting_backfill_cannot_resurrect_a_retired_session(tmp_path):
+ """A delayed background-review usage callback lands after delete committed: the
+ retirement fence must refuse the missing-row backfill instead of recreating the
+ session beside its durable tombstone."""
+ with SessionDB(tmp_path / 'state.db') as db:
+ db.create_session('retired', source='cli')
+ db.append_message('retired', 'user', 'history')
+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')
+ snap = db.get_session('retired')
+ delete = dict(principal_id='human', session_id='retired', request_id='delete', operation='delete',
+ payload={}, expected_revision=snap['runtime_revision'],
+ expected_generation=snap['runtime_generation'])
+ receipt = rt.mutate_runtime_session(db, epoch=epoch, **delete)
+ # The real delayed callback: a background-review fork reporting into its parent.
+ from agent.background_review import _record_review_usage_to_parent
+ parent = SimpleNamespace(_session_db=db, session_id='retired')
+ _record_review_usage_to_parent(parent, {'model': 'm', 'provider': 'p', 'base_url': None,
+ 'api_calls': 1, 'input_tokens': 3, 'output_tokens': 1})
+ for late in (lambda: db.update_token_counts('retired', input_tokens=5, output_tokens=2, model='m'),
+ lambda: db.ensure_session('retired', source='unknown')):
+ with pytest.raises(rt.RuntimeStoreError, match='not_found'):
+ late()
+ assert db.get_session('retired') is None, 'late accounting resurrected a deleted session'
+ with db._read_ctx() as c:
+ assert c.execute("SELECT COUNT(*) FROM session_model_usage WHERE session_id='retired'").fetchone()[0] == 0
+ assert rt.mutate_runtime_session(db, epoch=epoch, **delete) == receipt
+ # Live sessions keep the legacy missing-row backfill.
+ db.update_token_counts('fresh', input_tokens=1, output_tokens=1, model='m')
+ assert db.get_session('fresh')['source'] == 'unknown'