Skip to content

The agent that grows with you

fix(cli): unknown liveness of a sibling lease no longer refuses a different session's claim

KoNit-Kc53d5b741c8c
diff --git a/hermes_cli/active_sessions.py b/hermes_cli/active_sessions.py
index 12b1d37ea1d..7de9cef8af9 100644
--- a/hermes_cli/active_sessions.py
+++ b/hermes_cli/active_sessions.py
@@ -338,8 +338,17 @@ def _pid_liveness(pid: Any, process_start_time: Any = None, *, lenient: bool = F
return abs(current_start - expected_start) < 0.001
-def _prune_dead(entries: list[dict[str, Any]], *, strict: bool = False) -> list[dict[str, Any]]:
- """Keep entries whose owner is alive; tracked/strict entries must be provably so."""
+def _prune_dead(
+ entries: list[dict[str, Any]], *, strict: bool = False, target_session_id: str | None = None,
+) -> list[dict[str, Any]]:
+ """Keep entries whose owner is alive; tracked/strict entries must be provably so.
+
+ With ``target_session_id`` only THAT session's owner has to be provable: an
+ unrelated sibling whose liveness is unknowable (pid present, start time
+ unreadable — an LXC ``/proc`` after a backend restart) stays in the live set,
+ so it still fences its own session and still counts toward capacity, but no
+ longer refuses every claim/release for a different session id. See #113683.
+ """
live: list[dict[str, Any]] = []
for entry in entries:
tracked = strict or bool(entry.get("track_liveness"))
@@ -347,7 +356,9 @@ def _prune_dead(entries: list[dict[str, Any]], *, strict: bool = False) -> list[
entry.get("pid"), entry.get("process_start_time"), lenient=not tracked
)
if state is None:
- raise ActiveSessionRegistryError("active session owner liveness is unknown")
+ if target_session_id is None or str(entry.get("session_id") or "") == str(target_session_id):
+ raise ActiveSessionRegistryError("active session owner liveness is unknown")
+ state = True
if state:
live.append(entry)
return live
@@ -394,16 +405,20 @@ def _holds_session(entries: list[dict[str, Any]], session_id: str) -> bool:
def _read_live_entries(
- state_path: Path, *, track_liveness: bool, warn: str,
+ state_path: Path, *, track_liveness: bool, warn: str, target_session_id: str | None = None,
) -> Optional[tuple[list[dict[str, Any]], list[dict[str, Any]]]]:
"""``(raw, pruned)`` from the registry, or None when it is unreadable.
Liveness-tracked callers re-raise instead (they must not proceed on an unprovable
registry); untracked callers get ``warn`` logged and decide how to degrade.
+ ``target_session_id`` is the session the caller is about to claim/release (see
+ ``_prune_dead``).
"""
try:
raw_entries = _read_entries(state_path, strict=True)
- return raw_entries, _prune_dead(raw_entries, strict=track_liveness)
+ return raw_entries, _prune_dead(
+ raw_entries, strict=track_liveness, target_session_id=target_session_id
+ )
except ActiveSessionRegistryError:
if track_liveness:
raise
@@ -473,6 +488,7 @@ def try_acquire_active_session(
state_path, track_liveness=track_liveness,
warn="Active-session registry is unavailable; refusing the session "
"rather than risking a concurrent writer",
+ target_session_id=key,
)
if loaded is None:
return None, ActiveSessionRefusal(
@@ -538,6 +554,7 @@ def release_active_session(lease: ActiveSessionLease) -> None:
state_path, track_liveness=lease.track_liveness,
warn="Active-session registry is unavailable; preserving it while "
"releasing an untracked lease",
+ target_session_id=lease.session_id,
)
if loaded is not None:
_drop_lease(state_path, loaded[1], lease.lease_id)
@@ -565,6 +582,7 @@ def transfer_active_session(
state_path, track_liveness=lease.track_liveness,
warn="Active-session registry is unavailable; refusing to overwrite "
"it during lease transfer",
+ target_session_id=lease.session_id,
)
if loaded is None:
return False
diff --git a/tests/hermes_cli/test_active_sessions.py b/tests/hermes_cli/test_active_sessions.py
index 949f6976b4c..4aed96286ce 100644
--- a/tests/hermes_cli/test_active_sessions.py
+++ b/tests/hermes_cli/test_active_sessions.py
@@ -467,19 +467,57 @@ def test_liveness_guard_rejects_unknown_pid_state(tmp_path, monkeypatch):
with active_sessions.active_session_liveness_guard("session-1"):
pass
- original = 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.
+ # The unknown owner is unrelated to a NEW session id: it stays in the live
+ # set (fail closed on capacity, #94595) but no longer refuses the claim
+ # outright (#113683).
lease, message = active_sessions.try_acquire_active_session(
session_id="cli-cap-session",
surface="cli",
config={"max_concurrent_sessions": 1},
)
assert lease is None
- assert getattr(message, "reason", None) == (
- active_sessions.SESSION_COORDINATION_UNAVAILABLE
+ assert getattr(message, "reason", None) == active_sessions.MAX_CONCURRENT_SESSIONS
+ assert "unknown-owner" in state_path.read_text(encoding="utf-8")
+
+ lease, message = active_sessions.try_acquire_active_session(
+ session_id="cli-cap-session", surface="cli", config={},
)
- assert state_path.read_text(encoding="utf-8") == original
+ assert lease is not None and message is None
+ assert "unknown-owner" in state_path.read_text(encoding="utf-8")
+
+
+def test_unknown_sibling_liveness_only_fences_its_own_session(tmp_path, monkeypatch):
+ """A desktop (liveness-tracked) claim, release and transfer survive an unrelated
+ sibling whose owner pid exists but whose start time cannot be read (LXC /proc after
+ a backend restart, #113683); the SAME session id still fails closed."""
+ home = tmp_path / ".hermes"
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ state_path = home / "runtime" / "active_sessions.json"
+ active_sessions._write_entries(state_path, [{
+ "lease_id": "stale-sibling", "session_id": "old-chat", "surface": "desktop",
+ "pid": 1, "process_start_time": 1234.5, "track_liveness": True,
+ }])
+ original_start = active_sessions._process_start_time
+ monkeypatch.setattr(
+ active_sessions, "_process_start_time",
+ lambda pid: None if int(pid) == 1 else original_start(pid),
+ )
+ assert active_sessions._pid_liveness(1, 1234.5) is None
+
+ with pytest.raises(active_sessions.ActiveSessionRegistryError, match="liveness is unknown"):
+ active_sessions.try_acquire_active_session(
+ session_id="old-chat", surface="desktop", config={}, track_liveness=True,
+ )
+
+ lease, message = active_sessions.try_acquire_active_session(
+ session_id="new-chat", surface="desktop", config={}, track_liveness=True,
+ )
+ assert lease is not None and message is None
+ assert active_sessions.transfer_active_session(lease, session_id="new-chat-rotated")
+ lease.release()
+ assert lease.released is True
+ remaining = active_sessions._read_entries(state_path)
+ assert [e["lease_id"] for e in remaining] == ["stale-sibling"]
def test_liveness_release_failure_is_retryable(tmp_path, monkeypatch):