Skip to content

The agent that grows with you

fix(state): housekeeping keeps the logical owner of a retained local reset child

Teknium9faa487040d7
diff --git a/hermes_state_mutation_retirement.py b/hermes_state_mutation_retirement.py
index df8ab85048f..f940711d784 100644
--- a/hermes_state_mutation_retirement.py
+++ b/hermes_state_mutation_retirement.py
@@ -119,11 +119,37 @@ def retire_mutation_receipts(conn, session_ids):
conn.execute('UPDATE state_meta SET value=? WHERE key=?', (_json(receipt), key))
+def _retained_local_owners(conn, swept):
+ """Ids a kept local conversation still needs: the creation id (policy, FIFO, generation) and
+ every earlier segment of its receipt lineage, while the current physical target survives.
+ A reset/compression ends the root and the sweep would age it alone; the retained child would
+ then have history but no owner to admit into or restore from. Malformed receipts name nothing."""
+ from hermes_state_local import POLICY_PREFIX
+ kept = set()
+ for (raw,) in conn.execute('SELECT value FROM state_meta WHERE key GLOB ?', (POLICY_PREFIX + '*',)):
+ try:
+ receipt = json.loads(raw)
+ except (TypeError, ValueError):
+ continue
+ entry = receipt.get('entry') if isinstance(receipt, dict) else None
+ target = entry.get('session_id') if isinstance(entry, dict) else None
+ if not isinstance(target, str) or target in swept:
+ continue
+ lineage = receipt.get('lineage')
+ kept.update(x for x in [receipt.get('session_id'), *(lineage if isinstance(lineage, list) else [])]
+ if isinstance(x, str))
+ return kept
+
+
def retire_prunable(conn, session_ids):
"""Sweep variant of :func:`retire_sessions`: fence the idle sessions and return only those ids.
A session with live or unknown work is skipped, so one busy row cannot abort a whole
- prune/empty-session sweep (explicit deletes still refuse with ``session_busy``)."""
- quiet = [sid for sid in session_ids if conn.execute(_LIVE_LEDGER_SQL, (sid, sid)).fetchone() is None]
+ prune/empty-session sweep (explicit deletes still refuse with ``session_busy``). The logical
+ owner of a kept local reset/compression target is skipped too (explicit deletes remove the
+ whole conversation through :func:`delete_in_transaction`)."""
+ owners = _retained_local_owners(conn, set(session_ids))
+ quiet = [sid for sid in session_ids if sid not in owners
+ and conn.execute(_LIVE_LEDGER_SQL, (sid, sid)).fetchone() is None]
retire_sessions(conn, quiet)
return quiet
diff --git a/tests/hermes_state/test_reset_owner_housekeeping.py b/tests/hermes_state/test_reset_owner_housekeeping.py
new file mode 100644
index 00000000000..50de562d5de
--- /dev/null
+++ b/tests/hermes_state/test_reset_owner_housekeeping.py
@@ -0,0 +1,56 @@
+"""Housekeeping keeps the logical owner of a retained local reset child.
+
+A local reset keeps policy/FIFO/generation on the creation id and advances the receipt to a
+physical child. Retention and empty-session sweeps age the ended root on its own; deleting it
+would leave the child's history behind with no owner to admit into or restore from.
+"""
+import time
+import uuid
+from contextlib import closing
+
+import pytest
+
+from hermes_state import SessionDB
+import hermes_state_runtime as rt
+from tests.hermes_state.test_target_advance_fence import _local_session, _mutate
+
+
+def _store_reset(db, epoch, sid):
+ from gateway.session import SessionEntry
+ from hermes_state_local import local_receipt
+ from hermes_state_local_lineage import reset_local_target
+ old = SessionEntry.from_dict(local_receipt(db, sid)['entry'])
+ child = uuid.uuid4().hex
+ entry = SessionEntry(old.session_key, child, old.created_at, old.updated_at, origin=old.origin,
+ platform=old.platform, chat_type=old.chat_type, is_fresh_reset=True)
+ reset_local_target(db, epoch=epoch, parent_session_id=sid, entry=entry.to_dict())
+ return child
+
+
[email protected]('sweep', ['retention', 'empty'])
[email protected]('producer', ['canonical', 'session_store'])
+def test_sweeps_keep_the_owner_a_retained_reset_child_needs(tmp_path, producer, sweep):
+ from hermes_state_local_lineage import local_physical_target
+ with closing(SessionDB(tmp_path / 'state.db')) as db:
+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')
+ sid = _local_session(db, epoch, tmp_path)
+ child = (_mutate(db, epoch, sid, 'reset')['target_session_id'] if producer == 'canonical'
+ else _store_reset(db, epoch, sid))
+ db.append_message(child, 'user', 'hi')
+ db.append_message(child, 'assistant', 'hello')
+ aged = time.time() - 200 * 86400
+ db._execute_write(lambda c: c.execute(
+ 'UPDATE sessions SET started_at=?, last_activity_at=? WHERE id=?', (aged, aged, sid)))
+ # An unrelated aged, empty, ended session is still swept: the filter is ownership, not a blanket skip.
+ db.create_session('stray', source='cli')
+ db._execute_write(lambda c: c.execute(
+ 'UPDATE sessions SET started_at=?, last_activity_at=?, ended_at=? WHERE id=?', (aged, aged, aged, 'stray')))
+ swept = db.prune_sessions(older_than_days=90) if sweep == 'retention' else db.delete_empty_sessions()
+ assert swept == 1 and db.get_session('stray') is None
+ assert db.get_session(sid) is not None
+ assert [m['content'] for m in db.get_messages(child)] == ['hi', 'hello']
+ with db._read_ctx() as conn:
+ assert local_physical_target(conn, sid) == child
+ admitted = rt.admit_session_input(db, epoch=epoch, principal_id='human', session_id=sid,
+ request_id='after', payload={'text': 'after'})
+ assert admitted['status'] == 'queued'