The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(state): collapse profile-fence coverage to two invariants (#119121, salvage #119122)
teknium18d1c8e218997
diff --git a/tests/hermes_state/test_hermes_state.py b/tests/hermes_state/test_hermes_state.pyindex ed1724dd8e0..76d73b850f6 100644--- a/tests/hermes_state/test_hermes_state.py+++ b/tests/hermes_state/test_hermes_state.py@@ -5237,13 +5237,11 @@ def test_peer_fallback_never_adopts_a_sibling_profiles_row(tmp_path, monkeypatchdef test_peer_fallback_reset_boundary_is_profile_fenced(tmp_path, monkeypatch):- """The recovery reset fence must not cross profiles either.+ """#119121: the recovery reset fence carries the same profile predicate as the candidate.- A sibling profile's newer session_reset row for the same peer tuple used- to suppress THIS profile's otherwise recoverable session: the NOT EXISTS- boundary subquery carried every peer predicate but profile_name. The- profile's own reset still fences recovery, and a store outside the- profile tree (no derivable owner) keeps the historical unfenced behavior.+ A Telegram DM peer tuple is identical for every bot, so a sibling profile's+ newer session_reset row used to suppress THIS profile's recoverable session.+ The profile's own reset must still fence."""import hermes_state@@ -5260,6 +5258,12 @@ def test_peer_fallback_reset_boundary_is_profile_fenced(tmp_path, monkeypatch):source="telegram", session_key="agent:main:telegram:dm:42", **peer)+ def reset(session_id, session_key, profile_name):+ store.create_session(session_id, "telegram", session_key=session_key,+ profile_name=profile_name, **peer)+ store.append_message(session_id, "user", "/new")+ store.end_session(session_id, "session_reset")+store.create_session("own", "telegram", session_key="agent:main:telegram:dm:42:old",profile_name="default", **peer)store.append_message("own", "user", "default's conversation")@@ -5267,47 +5271,14 @@ def test_peer_fallback_reset_boundary_is_profile_fenced(tmp_path, monkeypatch):lambda c: c.execute("UPDATE sessions SET last_activity_at = 1 WHERE id = 'own'"))- # A sibling profile's later reset for the same tuple must NOT fence- # this profile's recovery.- store.create_session("sibling-reset", "telegram",- session_key="agent:bot2:telegram:dm:42", profile_name="bot2", **peer)- store.append_message("sibling-reset", "user", "/new")- store.end_session("sibling-reset", "session_reset")-- assert recover()["id"] == "own"-- # The profile's OWN later reset still fences recovery.- store.create_session("own-reset", "telegram",- session_key="agent:main:telegram:dm:42:r2", profile_name="default", **peer)- store.append_message("own-reset", "user", "/new")- store.end_session("own-reset", "session_reset")+ reset("sibling-reset", "agent:bot2:telegram:dm:42", "bot2")+ assert recover()["id"] == "own" # a sibling profile's reset is not this profile's boundary- assert recover() is None+ reset("own-reset", "agent:main:telegram:dm:42:r2", "default")+ assert recover() is None # the profile's own reset still fencesfinally:store.close()- # Outside the profile tree there is no owner: every boundary counts.- (tmp_path / "elsewhere").mkdir()- unfenced = SessionDB(db_path=tmp_path / "elsewhere" / "state.db")- try:- peer = {"user_id": "42", "chat_id": "42", "chat_type": "dm"}- unfenced.create_session("own", "telegram", session_key="agent:main:telegram:dm:42:old",- profile_name="default", **peer)- unfenced.append_message("own", "user", "conversation")- unfenced._execute_write(- lambda c: c.execute("UPDATE sessions SET last_activity_at = 1 WHERE id = 'own'")- )- unfenced.create_session("sibling-reset", "telegram",- session_key="agent:bot2:telegram:dm:42", profile_name="bot2", **peer)- unfenced.append_message("sibling-reset", "user", "/new")- unfenced.end_session("sibling-reset", "session_reset")-- assert unfenced.find_latest_gateway_session_for_peer(- source="telegram", session_key="agent:main:telegram:dm:42", **peer- ) is None- finally:- unfenced.close()-def test_child_inherits_parent_profile_only_within_its_key_namespace(db):"""#88381: parent→child ``profile_name`` COALESCE is fenced by ``agent:<ns>:``.diff --git a/tests/hermes_state/test_orphan_gateway_session_repair.py b/tests/hermes_state/test_orphan_gateway_session_repair.pyindex b4557aa00be..33917d1b671 100644--- a/tests/hermes_state/test_orphan_gateway_session_repair.py+++ b/tests/hermes_state/test_orphan_gateway_session_repair.py@@ -310,17 +310,6 @@ class TestAdoption:assert db.adopt_orphaned_gateway_session(orphan, "no_key_donor") is False-def _profiled_store(tmp_path, monkeypatch):- """A ``<HERMES_HOME>/state.db`` store, so ``_own_profile_name()`` returns ``default``."""- import hermes_state-- root = tmp_path / "hermes"- root.mkdir()- monkeypatch.setenv("HERMES_HOME", str(root))- monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", hermes_state._IMPORT_DEFAULT_DB_PATH)- return SessionDB(db_path=root / "state.db")--def _stamp_profile(db, session_id, profile_name):with db._lock:db._conn.execute(@@ -330,63 +319,38 @@ def _stamp_profile(db, session_id, profile_name):db._conn.commit()-class TestDonorProfileFence:- """A donor carries the routing identity adoption stamps onto the orphan, so a- donor from a sibling profile in a shared legacy store would splice this- profile's orphan into the sibling's ``agent:<ns>:`` namespace. Donor and- orphan must share an effective profile (NULL reads as the store's own)."""-- def test_contiguity_donor_must_share_the_orphans_profile(self, tmp_path, monkeypatch):- store = _profiled_store(tmp_path, monkeypatch)- try:- stale, orphan = _incident(store) # orphan auto-stamped 'default'- _stamp_profile(store, stale, "bot2")- record = store.find_orphaned_gateway_sessions()[0]- assert record["adoptable"] is False- assert record["donor_id"] is None-- # A legacy NULL-stamped row still reads as this store's own.- _stamp_profile(store, stale, None)- record = store.find_orphaned_gateway_sessions()[0]- assert record["adoptable"] is True- assert record["donor_id"] == stale-- # A bot2 orphan may still adopt a bot2 donor inside a shared store.- _stamp_profile(store, orphan, "bot2")- _stamp_profile(store, stale, "bot2")- record = store.find_orphaned_gateway_sessions()[0]- assert record["adoptable"] is True- assert record["session_key"] == PEER["session_key"]- finally:- store.close()-- def test_lineage_donor_must_share_the_orphans_profile(self, tmp_path, monkeypatch):- store = _profiled_store(tmp_path, monkeypatch)- try:- stale, _ = _incident(store, parent_link=True)- _stamp_profile(store, stale, "bot2")- record = store.find_orphaned_gateway_sessions()[0]- assert record["adoptable"] is False- assert record["donor_id"] is None- finally:- store.close()-- def test_adoption_reverify_refuses_a_cross_profile_pair(self, tmp_path, monkeypatch):- store = _profiled_store(tmp_path, monkeypatch)- try:- stale, orphan = _incident(store)- _stamp_profile(store, stale, "bot2")- assert store.adopt_orphaned_gateway_session(orphan, stale) is False- assert store.get_session(orphan)["session_key"] is None- assert store.get_session(stale)["end_reason"] == "agent_close"- finally:- store.close()-- def test_unowned_store_keeps_unfenced_donor_matching(self, db):- # ``db`` lives outside the profile tree: no derivable owner, so the- # historical behavior (any compatible keyed predecessor) stands.- stale, _ = _incident(db)- _stamp_profile(db, stale, "bot2")- record = db.find_orphaned_gateway_sessions()[0]- assert record["adoptable"] is True- assert record["donor_id"] == stale+def test_donor_must_share_the_orphans_profile(tmp_path, monkeypatch):+ """#119121: adoption stamps the donor's ``agent:<ns>:`` identity onto the orphan, so a+ sibling profile's keyed row in a shared legacy store must never be named as donor+ (by contiguity or lineage), and the write-time re-verify must refuse the pair even+ from a stale report. A legacy NULL-stamped row still reads as this store's own."""+ import hermes_state++ root = tmp_path / "hermes"+ root.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(root))+ monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", hermes_state._IMPORT_DEFAULT_DB_PATH)+ store = SessionDB(db_path=root / "state.db") # owner: default+ try:+ stale, orphan = _incident(store) # both rows auto-stamped 'default'+ _stamp_profile(store, stale, "bot2")+ record = store.find_orphaned_gateway_sessions()[0]+ assert record["adoptable"] is False and record["donor_id"] is None+ assert store.adopt_orphaned_gateway_session(orphan, stale) is False+ assert store.get_session(orphan)["session_key"] is None++ _stamp_profile(store, stale, None) # legacy unowned row: still ours+ record = store.find_orphaned_gateway_sessions()[0]+ assert record["adoptable"] is True and record["donor_id"] == stale++ with store._lock: # same fence on the lineage path+ store._conn.execute("UPDATE sessions SET parent_session_id = ? WHERE id = ?", (stale, orphan))+ store._conn.commit()+ _stamp_profile(store, stale, "bot2")+ record = store.find_orphaned_gateway_sessions()[0]+ assert record["adoptable"] is False and record["donor_id"] is None+ _stamp_profile(store, stale, "default")+ record = store.find_orphaned_gateway_sessions()[0]+ assert record["evidence"] == "lineage" and record["donor_id"] == stale+ finally:+ store.close()