Skip to content

The agent that grows with you

fix(sessions): rearm lost detached chat teardown timers

fangliquanflq919584e97115
diff --git a/evals/desktop_bug_campaign/leases_live.py b/evals/desktop_bug_campaign/leases_live.py
new file mode 100644
index 00000000000..41b1ad5d2a1
--- /dev/null
+++ b/evals/desktop_bug_campaign/leases_live.py
@@ -0,0 +1,257 @@
+"""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.
+No ownership predicate, lease registry, agent, or lifecycle function is replaced.
+"""
+import argparse
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+import tempfile
+import threading
+import time
+import urllib.request
+
+
+def backend(port):
+ from hermes_cli.web_server import app, start_server
+ from tui_gateway import server as gw
+
+ work = {}
+
+ @app.post('/lease-probe/{action}/{sid}')
+ def control(action: str, sid: str):
+ if action == 'remember-transport':
+ work['transport:' + sid] = gw._sessions[sid]['transport']
+ elif action == 'lose-detach':
+ with gw._sessions_lock:
+ rec = gw._sessions[sid]
+ assert gw._ws_session_is_detached(rec)
+ old_transport = work['transport:' + sid]
+ assert gw._transport_is_dead(old_transport)
+ gw._cancel_ws_orphan_reap(sid)
+ rec['transport'] = old_transport
+ elif action == 'lose-timer':
+ with gw._sessions_lock:
+ rec = gw._sessions[sid]
+ assert gw._ws_session_is_detached(rec)
+ assert sid in gw._pending_ws_reaps
+ gw._cancel_ws_orphan_reap(sid)
+ elif action == 'delegate':
+ from tools.async_delegation import dispatch_async_delegation
+ release, started, interrupted = threading.Event(), threading.Event(), threading.Event()
+ work[sid] = (release, started, interrupted)
+ def runner():
+ started.set()
+ release.wait(90)
+ return {'summary': 'LEASE_DELEGATE_DONE', 'status': 'completed'}
+ rec = gw._sessions[sid]
+ handle = dispatch_async_delegation(goal='lease protection control', context=None,
+ toolsets=[], role='leaf', model=None, session_key=rec['session_key'],
+ origin_ui_session_id=sid, parent_session_id=rec['session_key'], runner=runner,
+ interrupt_fn=interrupted.set)
+ assert handle['status'] == 'dispatched', handle
+ assert started.wait(10)
+ elif action == 'finish-delegate':
+ work[sid][0].set()
+ elif action == 'sweep':
+ gw._reap_idle_sessions()
+ with gw._sessions_lock:
+ rec = gw._sessions.get(sid)
+ return {'resident': rec is not None,
+ 'detached': gw._ws_session_is_detached(rec),
+ 'pending': sid in gw._pending_ws_reaps,
+ 'running': bool(rec and rec.get('running')),
+ 'delegating': bool(rec and gw._session_has_active_delegations(sid, rec)),
+ 'delegate_interrupted': sid in work and work[sid][2].is_set()}
+
+ start_server(host='127.0.0.1', port=port, open_browser=False, headless=True)
+
+
+def fixture(port):
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+ class Handler(BaseHTTPRequestHandler):
+ def log_message(self, *args):
+ pass
+ def do_POST(self):
+ req = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
+ text = 'LEASE_RECOVERY_REPLY' if 'RECOVERY_TURN_ONLY' in str(req.get('messages', [])) else 'LEASE_PROBE_REPLY'
+ self.send_response(200)
+ if req.get('stream'):
+ self.send_header('Content-Type', 'text/event-stream')
+ self.end_headers()
+ for delta, finish in [({'role': 'assistant', 'content': text}, None), ({}, 'stop')]:
+ event = {'id': 'probe', 'object': 'chat.completion.chunk', 'model': 'probe',
+ 'choices': [{'index': 0, 'delta': delta, 'finish_reason': finish}]}
+ self.wfile.write(('data: ' + json.dumps(event) + '\n\n').encode())
+ self.wfile.write(b'data: [DONE]\n\n')
+ else:
+ self.send_header('Content-Type', 'application/json')
+ self.end_headers()
+ self.wfile.write(json.dumps({'id': 'probe', 'object': 'chat.completion', 'model': 'probe',
+ 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': text}, 'finish_reason': 'stop'}],
+ 'usage': {'prompt_tokens': 10, 'completion_tokens': 3, 'total_tokens': 13}}).encode())
+ httpd = ThreadingHTTPServer(('127.0.0.1', port), Handler)
+ threading.Thread(target=httpd.serve_forever, daemon=True).start()
+ return httpd
+
+
+class Client:
+ counter = 0
+ def __init__(self, port):
+ from websockets.sync.client import connect
+ self.ws = connect(f'ws://127.0.0.1:{port}/api/ws?token=lease-probe')
+ def call(self, method, params):
+ Client.counter += 1
+ rid = Client.counter
+ self.ws.send(json.dumps({'jsonrpc': '2.0', 'id': rid, 'method': method, 'params': params}))
+ while True:
+ frame = json.loads(self.ws.recv(timeout=90))
+ if frame.get('id') == rid:
+ return frame
+ def close(self):
+ self.ws.close()
+
+
+def main(out, closed_transport=False):
+ out.mkdir(parents=True, exist_ok=True)
+ repo = Path(__file__).resolve().parents[2]
+ home = Path(tempfile.mkdtemp(prefix='leases-live-'))
+ (home / 'config.yaml').write_text('''model:
+ default: probe
+ provider: custom
+ base_url: http://127.0.0.1:18042/v1
+ api_key: probe-key
+dashboard:
+ turn_isolation: false
+ ws_orphan_reap_grace_s: 3
+ startup_orphan_sweep: false
+memory:
+ memory_enabled: false
+ user_profile_enabled: false
+''')
+ env = {'PATH': os.environ.get('PATH', ''), 'HOME': str(home / 'user'), 'LANG': 'C.UTF-8'}
+ (home / 'user').mkdir()
+ env.update(HERMES_HOME=str(home), HERMES_DASHBOARD_SESSION_TOKEN='lease-probe',
+ PYTHONPATH=str(repo), OPENAI_API_KEY='probe-key')
+ processes = []
+ logs = []
+ service = fixture(18042)
+ results = {'sha': subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=repo, text=True).strip(),
+ 'home': str(home), 'fidelity': 'real backend subprocesses + WebSocket; deterministic local HTTP inference; lost-timer fault injection', 'checks': []}
+ def check(name, ok, detail=None):
+ results['checks'].append({'name': name, 'pass': bool(ok), 'detail': detail})
+ print(json.dumps(results['checks'][-1]), flush=True)
+ def control(port, action, sid):
+ req = urllib.request.Request(f'http://127.0.0.1:{port}/lease-probe/{action}/{sid}', data=b'', method='POST', headers={'X-Hermes-Session-Token': 'lease-probe'})
+ return json.load(urllib.request.urlopen(req, timeout=30))
+ def wait_idle(port, sid):
+ deadline = time.monotonic() + 90
+ while time.monotonic() < deadline:
+ state = control(port, 'state', sid)
+ if not state['running']:
+ return state
+ time.sleep(.1)
+ raise TimeoutError('turn')
+ try:
+ for port in [18040, 18041]:
+ log = (out / f'backend-{port}.log').open('w'); logs.append(log)
+ p = subprocess.Popen([sys.executable, str(Path(__file__).resolve()), '--backend', str(port)], cwd=repo, env=env, stdin=subprocess.DEVNULL, stdout=log, stderr=subprocess.STDOUT)
+ processes.append(p)
+ deadline = time.monotonic() + 60
+ while time.monotonic() < deadline:
+ try:
+ control(port, 'state', 'none'); break
+ except Exception:
+ if p.poll() is not None:
+ raise RuntimeError(f'backend exited {p.returncode}')
+ time.sleep(.2)
+ else:
+ raise TimeoutError('backend readiness')
+ a = Client(18040)
+ created = a.call('session.create', {'source': 'desktop', 'close_on_disconnect': False})
+ print('CREATE', created, flush=True)
+ sid = created['result']['session_id']; stored = created['result']['stored_session_id']
+ submitted = a.call('prompt.submit', {'session_id': sid, 'text': 'Reply LEASE_PROBE_REPLY'})
+ check('initial prompt accepted', 'result' in submitted, submitted)
+ wait_idle(18040, sid)
+ b = Client(18041)
+ resumed = b.call('session.resume', {'session_id': stored, 'source': 'desktop', 'lazy': True, 'omit_messages': True})
+ print('RESUME', resumed, flush=True)
+ sid_b = resumed['result']['session_id']
+ refused = b.call('prompt.submit', {'session_id': sid_b, 'text': 'Reply LEASE_PROBE_REPLY'})
+ check('foreign backend refused while owner attached', refused.get('error', {}).get('code') == 4090, refused)
+ a.close()
+ deadline = time.monotonic() + 2
+ while time.monotonic() < deadline:
+ if control(18040, 'state', sid)['detached']:
+ break
+ time.sleep(.02)
+ lost = control(18040, 'lose-timer', sid)
+ check('fault precondition resident detached without timer', lost['resident'] and lost['detached'] and not lost['pending'], lost)
+ control(18040, 'sweep', sid)
+ # Reconnect within the repaired grace: same runtime and fence survive.
+ a = Client(18040)
+ reattached = a.call('session.resume', {'session_id': stored, 'source': 'desktop', 'lazy': True, 'omit_messages': True})
+ check('same backend reconnect retains runtime', reattached.get('result', {}).get('session_id') == sid, {'session_id': reattached.get('result', {}).get('session_id')})
+ time.sleep(4)
+ state = control(18040, 'state', sid)
+ check('reconnect cancels repaired timer', state['resident'] and not state['detached'] and not state['pending'], state)
+ refused = b.call('prompt.submit', {'session_id': sid_b, 'text': 'Still fenced'})
+ check('foreign backend remains fenced after reconnect', refused.get('error', {}).get('code') == 4090, refused)
+ control(18040, 'delegate', sid)
+ control(18040, 'remember-transport', sid)
+ a.close()
+ deadline = time.monotonic() + 2
+ while time.monotonic() < deadline:
+ if control(18040, 'state', sid)['detached']:
+ break
+ time.sleep(.02)
+ fault = control(18040, 'lose-detach' if closed_transport else 'lose-timer', sid)
+ check('dead transport fault established', fault['resident'] and not fault['pending'] and (not fault['detached'] if closed_transport else fault['detached']), fault)
+ control(18040, 'sweep', sid)
+ time.sleep(4)
+ state = control(18040, 'state', sid)
+ check('real async registry work survives repaired reap', state['resident'] and state['delegating'] and not state['delegate_interrupted'], state)
+ refused = b.call('prompt.submit', {'session_id': sid_b, 'text': 'Still fenced during delegation'})
+ check('foreign backend fenced during detached delegation', refused.get('error', {}).get('code') == 4090, refused)
+ control(18040, 'finish-delegate', sid)
+ time.sleep(7)
+ state = control(18040, 'state', sid)
+ check('periodic maintenance recovers lost timer', not state['resident'], state)
+ retry = b.call('prompt.submit', {'session_id': sid_b, 'text': 'RECOVERY_TURN_ONLY'})
+ check('foreign backend submits after recovery', 'result' in retry, retry)
+ wait_idle(18041, sid_b)
+ import sqlite3
+ with sqlite3.connect(home / 'state.db') as db:
+ replies = db.execute("SELECT count(*) FROM messages WHERE session_id=? AND role='assistant' AND content LIKE ?", (stored, '%LEASE_RECOVERY_REPLY%')).fetchone()[0]
+ check('post-recovery reply persisted to receiver database', replies >= 1, {'assistant_replies': replies})
+ b.close()
+ finally:
+ for p in processes:
+ p.terminate()
+ for p in processes:
+ try:
+ p.wait(timeout=20)
+ except subprocess.TimeoutExpired:
+ p.kill(); p.wait(timeout=10)
+ for log in logs:
+ log.close()
+ service.shutdown()
+ (out / 'result.json').write_text(json.dumps(results, indent=2))
+ return 0 if all(x['pass'] for x in results['checks']) else 1
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--backend', type=int)
+ parser.add_argument('--output', type=Path)
+ parser.add_argument('--closed-transport', action='store_true')
+ args = parser.parse_args()
+ if args.backend:
+ backend(args.backend)
+ else:
+ sys.exit(main(args.output, args.closed_transport))
diff --git a/tests/tui_gateway/test_protocol.py b/tests/tui_gateway/test_protocol.py
index 0c925ef42bd..b38e44fc92a 100644
--- a/tests/tui_gateway/test_protocol.py
+++ b/tests/tui_gateway/test_protocol.py
@@ -2,6 +2,8 @@
import io
import json
+import os
+import subprocess
import sys
import threading
import time
@@ -1064,6 +1066,95 @@ 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):
+ """A detached lane cannot keep its lease forever if initial timer setup was lost."""
+ from hermes_cli.active_sessions import (
+ active_session_registry_snapshot,
+ try_acquire_active_session,
+ )
+
+ home = tmp_path / ".hermes"
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ sid = "detached-without-reaper"
+ sibling_sid = "live-sibling"
+ orphan_lease, message = try_acquire_active_session(
+ session_id=sid,
+ surface="desktop",
+ config={},
+ registry_home=home,
+ track_liveness=True,
+ )
+ assert orphan_lease is not None and message is None
+ sibling_lease, message = try_acquire_active_session(
+ session_id=sibling_sid,
+ surface="desktop",
+ config={},
+ registry_home=home,
+ track_liveness=True,
+ )
+ assert sibling_lease is not None and message is None
+
+ def _session(session_key, lease, transport):
+ return {
+ "active_session_lease": lease,
+ "created_at": time.time(),
+ "history": [],
+ "history_lock": threading.Lock(),
+ "last_active": time.time(),
+ "session_key": session_key,
+ "source": "tui",
+ "transport": transport,
+ }
+
+ server._sessions.clear()
+ server._sessions.update({
+ sid: _session(sid, orphan_lease, server._detached_ws_transport),
+ sibling_sid: _session(sibling_sid, sibling_lease, object()),
+ })
+ server._pending_ws_reaps.clear()
+ monkeypatch.setattr(server, "_WS_ORPHAN_REAP_GRACE_S", 0.05)
+ monkeypatch.setattr(server, "_SESSION_TTL_S", 3600.0)
+ monkeypatch.setattr(server, "_flush_dirty_sessions", lambda: 0)
+ monkeypatch.setattr(server, "_enforce_session_cap", lambda: None)
+
+ server._reap_idle_sessions()
+
+ deadline = time.monotonic() + 2.0
+ while (sid in server._sessions or not orphan_lease.released) and time.monotonic() < deadline:
+ time.sleep(0.01)
+ assert sid not in server._sessions
+ assert orphan_lease.released is True
+ assert sibling_sid in server._sessions
+ assert [entry["session_id"] for entry in active_session_registry_snapshot(home)] == [sibling_sid]
+
+ repo_root = Path(__file__).resolve().parents[2]
+ env = os.environ.copy()
+ env["HERMES_HOME"] = str(home)
+ env["PYTHONPATH"] = os.pathsep.join(
+ part for part in (str(repo_root), env.get("PYTHONPATH", "")) if part
+ )
+ successor = subprocess.run(
+ [
+ sys.executable,
+ "-c",
+ (
+ "from hermes_cli.active_sessions import try_acquire_active_session; "
+ f"lease, refusal = try_acquire_active_session(session_id={sid!r}, surface='desktop', "
+ "config={}, track_liveness=True); "
+ "assert lease is not None and refusal is None, refusal; lease.release()"
+ ),
+ ],
+ cwd=repo_root,
+ env=env,
+ stdin=subprocess.DEVNULL,
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+ assert successor.returncode == 0, successor.stderr
+ assert [entry["session_id"] for entry in active_session_registry_snapshot(home)] == [sibling_sid]
+
+
def test_sync_session_key_after_compress_reanchors_active_session_lease(
server, monkeypatch, tmp_path
):
diff --git a/tui_gateway/session_reaper.py b/tui_gateway/session_reaper.py
index a8eaccd91ec..ce7f196e9ef 100644
--- a/tui_gateway/session_reaper.py
+++ b/tui_gateway/session_reaper.py
@@ -170,6 +170,7 @@ def _reap_idle_sessions() -> None:
_close_session_by_id(
sid, end_reason="idle_timeout",
predicate=lambda session, vs=sid: _session_is_evictable(vs, session, time.time()))
+ _repair_missing_ws_orphan_reaps()
_enforce_session_cap()
_reclaim_orphaned_leases()
# Long-lived processes: gen2 GC rarely runs at steady state and glibc retains freed pages as RSS, so trim
@@ -181,6 +182,25 @@ def _reap_idle_sessions() -> None:
logger.debug("idle reaper memory trim failed: %s: %s", type(exc).__name__, exc)
+def _repair_missing_ws_orphan_reaps() -> None:
+ """Re-arm detached sessions whose disconnect path lost its teardown timer.
+
+ A resident record otherwise vouches for its lease during every orphan sweep,
+ while the live process prevents PID pruning. Reusing the normal WS grace
+ path preserves reconnect and in-flight-work protections instead of stealing
+ the lease directly.
+ """
+ if _WS_ORPHAN_REAP_GRACE_S <= 0:
+ return
+ with _sessions_lock:
+ missing = [
+ sid for sid, session in _sessions.items()
+ if _ws_session_is_detached(session) and sid not in _pending_ws_reaps
+ ]
+ for sid in missing:
+ _schedule_ws_orphan_reap(sid)
+
+
def _reclaim_orphaned_leases() -> None:
"""Hand the registry the lease ids we still own so it can drop the rest."""
try:
diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md
index c08057558f7..1ac7ce32e46 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. (`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 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_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.