Skip to content

The agent that grows with you

fix(sessions): repair closed lanes through canonical disconnect cleanup

Teknium18955c35b552
diff --git a/evals/desktop_bug_campaign/leases_live.py b/evals/desktop_bug_campaign/leases_live.py
index 41b1ad5d2a1..4a0e1e4031e 100644
--- a/evals/desktop_bug_campaign/leases_live.py
+++ b/evals/desktop_bug_campaign/leases_live.py
@@ -1,7 +1,9 @@
"""Real WS/backend-process lease recovery probe; deterministic HTTP inference.
Run: .venv/bin/python evals/desktop_bug_campaign/leases_live.py --output DIR
-Fault injection only cancels an actual disconnected session's orphan timer.
+Fault injection cancels an actual disconnected session's orphan timer and,
+optionally, restores its real closed transport to model missed detachment.
+Delegation uses the real registry/executor with an Event-blocked fixture runner.
No ownership predicate, lease registry, agent, or lifecycle function is replaced.
"""
import argparse
diff --git a/tests/tui_gateway/test_protocol.py b/tests/tui_gateway/test_protocol.py
index b38e44fc92a..1e32a1a4e69 100644
--- a/tests/tui_gateway/test_protocol.py
+++ b/tests/tui_gateway/test_protocol.py
@@ -1066,7 +1066,8 @@ def test_enforce_session_cap_evicts_oldest_detached_only(server, monkeypatch):
assert evicted == ["old_detached", "new_detached"]
-def test_idle_reaper_rearms_missing_ws_orphan_timer(server, monkeypatch, tmp_path):
[email protected]("closed_transport", [False, True])
+def test_idle_reaper_rearms_missing_ws_orphan_timer(server, monkeypatch, tmp_path, closed_transport):
"""A detached lane cannot keep its lease forever if initial timer setup was lost."""
from hermes_cli.active_sessions import (
active_session_registry_snapshot,
@@ -1107,8 +1108,12 @@ def test_idle_reaper_rearms_missing_ws_orphan_timer(server, monkeypatch, tmp_pat
}
server._sessions.clear()
+ class ClosedTransport:
+ _closed = True
+
+ dead_transport = ClosedTransport() if closed_transport else server._detached_ws_transport
server._sessions.update({
- sid: _session(sid, orphan_lease, server._detached_ws_transport),
+ sid: _session(sid, orphan_lease, dead_transport),
sibling_sid: _session(sibling_sid, sibling_lease, object()),
})
server._pending_ws_reaps.clear()
diff --git a/tui_gateway/session_reaper.py b/tui_gateway/session_reaper.py
index ce7f196e9ef..9412ddcd70f 100644
--- a/tui_gateway/session_reaper.py
+++ b/tui_gateway/session_reaper.py
@@ -192,6 +192,15 @@ def _repair_missing_ws_orphan_reaps() -> None:
"""
if _WS_ORPHAN_REAP_GRACE_S <= 0:
return
+ # A socket can be closed before its disconnect cleanup reaches the sentinel.
+ # Reuse that cleanup (including surviving viewers), never revoke a fence from
+ # a stale liveness snapshot.
+ with _sessions_lock:
+ closed_transports = [session.get("transport") for session in _sessions.values()
+ if session.get("transport") is not _detached_ws_transport
+ and _transport_is_dead(session.get("transport"))]
+ for transport in closed_transports:
+ _close_sessions_for_transport(transport)
with _sessions_lock:
missing = [
sid for sid, session in _sessions.items()
diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md
index 1ac7ce32e46..673b7ad1fc5 100644
--- a/website/docs/user-guide/configuration.md
+++ b/website/docs/user-guide/configuration.md
@@ -2815,6 +2815,6 @@ dashboard:
- `oauth` / `basic_auth` / `drain_auth` — auth provider config read by the bundled dashboard-auth plugins. The drain secret itself is **not** set here; it's provisioned via the `HERMES_DASHBOARD_DRAIN_SECRET` env var. See [Web Dashboard](/user-guide/features/web-dashboard) for full auth setup.
- `ws_ping_interval` / `ws_ping_timeout` — WebSocket keepalive tuning for non-loopback binds (loopback connections never ping). Raise these on high-latency links (Tailscale, distant SSH tunnels) where the 20 s defaults can manufacture spurious 1006 disconnects.
- `ssh_isolated_idle_grace_s` (default `900`) — a Desktop-owned `hermes serve --isolated` backend reached over SSH is detached from the SSH session on purpose, so a laptop that sleeps mid-connection cannot tear it down; each dark-wake reconnect used to leave another backend holding `state.db`. The backend now retires itself once no client WebSocket has been connected for this long and no agent turn is running (a turn keeps it alive; an unreadable turn state keeps it alive too). Set high if you rely on a detached backend finishing long work after the laptop sleeps. Such backends also send a slow WebSocket ping (60 s, 10 min timeout) so a half-open tunnel is noticed.
-- `ws_orphan_reap_grace_s` — how long a WS-detached session waits before the orphan reaper collects it. Raise alongside the keepalive values if clients reconnect slowly. Periodic session maintenance also re-arms a missing orphan timer, so a detached chat cannot keep its ownership lease solely because its initial timer was lost. Reconnecting cancels that timer; active delegated work and healthy running turns remain protected by the normal orphan-reaper checks. (`HERMES_TUI_WS_ORPHAN_REAP_GRACE_S` remains as an internal override.)
+- `ws_orphan_reap_grace_s` — how long a WS-detached session waits before the orphan reaper collects it. Raise alongside the keepalive values if clients reconnect slowly. Periodic session maintenance also completes cleanup for closed sockets and re-arms a missing orphan timer, so a detached chat cannot keep its ownership lease solely because its initial cleanup or timer was lost. Reconnecting cancels that timer; active delegated work and healthy running turns remain protected by the normal orphan-reaper checks. (`HERMES_TUI_WS_ORPHAN_REAP_GRACE_S` remains as an internal override.)
- `ws_orphan_activity_stale_s` (default `600`) — how long a detached **running** turn's activity clock (the same clock the `agent.turn_liveness` watchdog samples: API waits, stream tokens, tool heartbeats) must be idle before the orphan reaper interrupts it. A client-absent turn that is still actively producing keeps running to completion detached — closing the laptop, backgrounding the mobile app, or a desktop update no longer cancels healthy long turns; only a genuinely wedged turn is interrupted. Set `0` to interrupt at the grace window regardless of activity (old behavior).
- `startup_orphan_sweep` (default `true`) — the WS-orphan reap timer above is in-process, so a gateway restart (update, crash, systemd) before it fires leaves the session row open forever — phantom "active" work in `/resume` and dashboards. On every gateway boot — both the stdio TUI (`entry.main`) and the desktop/dashboard WebSocket sidecar (`handle_ws`) — rows with source `tui` / `desktop` / `subagent` whose start time **and** newest message are both older than the session TTL (`HERMES_TUI_SESSION_TTL_S`, default 6 hours) are closed with `end_reason: startup_orphan_reap`. Messaging-platform sessions (Telegram, Discord, …) are never touched, live in-memory sessions (a client that already resumed) are excluded, and swept sessions remain resumable.