The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): fail closed on ownership uncertainty and fence every turn source
Teknium5505042f40b8
diff --git a/hermes_cli/active_sessions.py b/hermes_cli/active_sessions.pyindex 6d5fb8d0bb4..4f21e6cd9ab 100644--- a/hermes_cli/active_sessions.py+++ b/hermes_cli/active_sessions.py@@ -540,6 +540,17 @@ def try_acquire_active_session(lease_id = uuid.uuid4().hexkey = str(session_id or "")+ # A session with no stored id yet cannot collide with another writer, and+ # the strict registry schema (rightly) refuses entries with empty session+ # ids. Nothing to fence, nothing to record: hand back a no-op lease.+ if not key and not track_liveness:+ return ActiveSessionLease(+ lease_id=lease_id,+ session_id=key,+ surface=str(surface),+ enabled=False,+ ), None+entry = _lease_entry(lease_id=lease_id,session_id=key,diff --git a/tests/hermes_cli/test_active_sessions.py b/tests/hermes_cli/test_active_sessions.pyindex d2feff3ad90..5741aed1789 100644--- a/tests/hermes_cli/test_active_sessions.py+++ b/tests/hermes_cli/test_active_sessions.py@@ -273,17 +273,19 @@ def test_liveness_registry_corruption_fails_closed_without_overwrite()assert state_path.read_text(encoding="utf-8") == corrupt- # The pre-existing concurrency-cap path remains available/fail-open, but- # must not erase evidence that strict liveness ownership is unknown.+ # Ownership uncertainty fails CLOSED on every path now (#94595): a corrupt+ # registry must refuse the session — with a typed reason — rather than+ # silently readmitting a possible second writer. It still must not erase+ # the evidence.lease, message = active_sessions.try_acquire_active_session(session_id="cli-1",surface="cli",config={"max_concurrent_sessions": 1},)- assert lease is not None and message is None- assert lease.enabled is False- assert state_path.read_text(encoding="utf-8") == corrupt- lease.release()+ assert lease is None+ assert getattr(message, "reason", None) == (+ active_sessions.SESSION_COORDINATION_UNAVAILABLE+ )assert state_path.read_text(encoding="utf-8") == corrupt@@ -410,13 +412,17 @@ def test_liveness_guard_rejects_unknown_pid_state(tmp_path, monkeypatch):passoriginal = state_path.read_text(encoding="utf-8")+ # An unknown pid state means dead-owner pruning cannot be trusted, which+ # means ownership cannot be proven. Fail closed (#94595), preserve the file.lease, message = active_sessions.try_acquire_active_session(session_id="cli-cap-session",surface="cli",config={"max_concurrent_sessions": 1},)- assert lease is not None and message is None- assert lease.enabled is False+ assert lease is None+ assert getattr(message, "reason", None) == (+ active_sessions.SESSION_COORDINATION_UNAVAILABLE+ )assert state_path.read_text(encoding="utf-8") == originaldiff --git a/tests/tui_gateway/test_cross_process_orphan_ownership.py b/tests/tui_gateway/test_cross_process_orphan_ownership.pyindex 157f2215079..481b837c073 100644--- a/tests/tui_gateway/test_cross_process_orphan_ownership.py+++ b/tests/tui_gateway/test_cross_process_orphan_ownership.py@@ -127,9 +127,14 @@ def _stop_child(child: subprocess.Popen[str], release_file: Path) -> None:child.communicate()-def test_unlimited_session_lease_remains_noop_without_liveness_tracking(+def test_unlimited_session_lease_is_real_even_without_a_cap(tmp_path: Path,) -> None:+ """No cap configured must still fence the session (#94595).++ The old contract returned a disabled no-op lease here, which meant two+ processes could run one stored session concurrently by default.+ """home = tmp_path / "untracked-home"lease, message = try_acquire_active_session(@@ -140,7 +145,10 @@ def test_unlimited_session_lease_remains_noop_without_liveness_tracking()assert lease is not None and message is None- assert lease.enabled is False+ assert lease.enabled is True+ entries = active_session_registry_snapshot(registry_home=home)+ assert [entry["session_id"] for entry in entries] == ["untracked-session"]+ lease.release()assert active_session_registry_snapshot(registry_home=home) == []@@ -187,7 +195,11 @@ def test_desktop_claim_fails_closed_when_registry_setup_fails(assert desktop_lease is Noneassert desktop_message == server._SESSION_OWNERSHIP_UNAVAILABLE- assert (tui_lease, tui_message) == (None, None)+ # Every surface fails closed now (#94595): a claim that errored has not+ # proven the session is unowned, and proceeding leaseless reopens the+ # double-writer hole.+ assert tui_lease is None+ assert tui_message == server._SESSION_OWNERSHIP_UNAVAILABLEdef test_server_release_retries_liveness_lease_before_dropping_reference(@@ -363,19 +375,26 @@ def test_automatic_desktop_cleanup_preserves_sibling_and_ends_sole_owner()assert set(reasons) == server._AUTOMATIC_SESSION_END_REASONS- for index, reason in enumerate(reasons):- local_lease, message = server._claim_active_session_slot(- session_id,- live_session_id=f"local-runtime-{index}",- surface="desktop",- profile_home=profile_home,- )- assert local_lease is not None and message is None- assert (- len(active_session_registry_snapshot(registry_home=profile_home)) == 2- )+ # With the per-session fence (#94595) this backend can no longer claim+ # a second lease on a session another backend owns — the exact+ # double-writer state the fence exists to prevent.+ refused_lease, refusal = server._claim_active_session_slot(+ session_id,+ live_session_id="local-runtime",+ surface="desktop",+ profile_home=profile_home,+ )+ assert refused_lease is None+ assert getattr(refusal, "reason", None) == "SESSION_NOT_OWNED"+ assert (+ len(active_session_registry_snapshot(registry_home=profile_home)) == 1+ )- server._finalize_session(_session(local_lease), end_reason=reason)+ # A LEASELESS local record of that session (a viewer / never-ran-a-turn+ # tab) must still preserve the sibling's session on every automatic+ # cleanup reason: the lifecycle guard consults the registry directly.+ for reason in reasons:+ server._finalize_session(_session(None), end_reason=reason)assert ended == []remaining = active_session_registry_snapshot(registry_home=profile_home)@@ -383,14 +402,7 @@ def test_automatic_desktop_cleanup_preserves_sibling_and_ends_sole_owner(assert remaining[0]["session_id"] == session_id# Explicit user close retains force/end semantics even with a sibling.- explicit_lease, message = server._claim_active_session_slot(- session_id,- live_session_id="explicit-runtime",- surface="desktop",- profile_home=profile_home,- )- assert explicit_lease is not None and message is None- server._finalize_session(_session(explicit_lease), end_reason="tui_close")+ server._finalize_session(_session(None), end_reason="tui_close")assert ended == [(session_id, "tui_close")]ended.clear()