The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): release stale PTY sessions on profile switch
Nagisa-3000ad06769eb147
diff --git a/hermes_cli/pty_session.py b/hermes_cli/pty_session.pyindex 5c76977154a..5f69c3cea1e 100644--- a/hermes_cli/pty_session.py+++ b/hermes_cli/pty_session.py@@ -203,6 +203,24 @@ class PtySessionRegistry:self._sessions[key] = sessionreturn session, True+ async def close_other_sessions(self, prefix: str, *, keep_key: str) -> None:+ """Close sessions belonging to the same logical client except ``keep_key``.++ Dashboard profile changes keep the browser's attach token but change the+ canonical session key. The previous profile's detached PTY must not+ remain alive long enough to hold the TUI session lease and reject a+ later return to that chat.+ """+ async with self._attach_lock:+ keys = [+ key for key in self._sessions+ if key != keep_key and (key == prefix or key.startswith(prefix + "\0"))+ ]+ for key in keys:+ session = self._sessions.pop(key, None)+ if session is not None:+ await session.close()+def detach(self, key: str, ws) -> None:s = self._sessions.get(key)if s is not None:diff --git a/hermes_cli/web_routers/chat_ws.py b/hermes_cli/web_routers/chat_ws.pyindex 00252b9df1e..0192d9b9751 100644--- a/hermes_cli/web_routers/chat_ws.py+++ b/hermes_cli/web_routers/chat_ws.py@@ -508,7 +508,8 @@ async def pty_ws(ws: WebSocket) -> None:await _pty_fail(ws, exc)return- attach_token = ws.query_params.get("attach") or None+ raw_attach_token = ws.query_params.get("attach") or None+ attach_token = raw_attach_tokenregistry_resume = raw_resumeif raw_resume and env:registry_resume = env.get("HERMES_TUI_RESUME") or raw_resume@@ -534,6 +535,7 @@ async def pty_ws(ws: WebSocket) -> None:# Keep-alive path: the PTY outlives this socket; reattach by token.try:+ await PTY_REGISTRY.close_other_sessions(raw_attach_token, keep_key=attach_token)session, _created = await PTY_REGISTRY.attach_or_spawn(attach_token, spawn=_spawn)except (PtyUnavailableError, FileNotFoundError, OSError, RegistryFull) as exc:await _pty_fail(ws, exc)diff --git a/tests/hermes_cli/test_pty_session.py b/tests/hermes_cli/test_pty_session.pyindex 5ff9f5efb85..cd5a8c4346f 100644--- a/tests/hermes_cli/test_pty_session.py+++ b/tests/hermes_cli/test_pty_session.py@@ -427,3 +427,26 @@ async def test_close_all_survives_key_popped_by_concurrent_reap():assert not reg._sessionsassert all(b.closed for b in bridges)+++async def test_close_other_sessions_removes_old_profile_session():+ from hermes_cli.pty_session import PtySession++ reg = make_registry()+ old_bridge = FakeBridge([b""])+ current_bridge = FakeBridge([b""])+ old = PtySession("token\0alpha\0session-a", old_bridge, buffer_cap=1024, read_timeout=0.01)+ current = PtySession("token\0beta\0session-b", current_bridge, buffer_cap=1024, read_timeout=0.01)+ await old.start()+ await current.start()+ old.detach(None)+ reg._sessions[old.key] = old+ reg._sessions[current.key] = current++ await reg.close_other_sessions("token", keep_key=current.key)++ assert old_bridge.closed+ assert old.key not in reg._sessions+ assert reg._sessions[current.key] is current+ await reg.close_all()