The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): announce backend-reclaimed sessions to their clients
Brooklyn Nicholson6debeb083662
diff --git a/tui_gateway/server.py b/tui_gateway/server.pyindex 327ae0f7171..b2bf7e0a5e4 100644--- a/tui_gateway/server.py+++ b/tui_gateway/server.py@@ -793,6 +793,39 @@ def _finalize_session(session: dict | None, end_reason: str = "tui_close") -> Nopass+# End reasons where the BACKEND reclaimed a session the client never asked to+# close: the idle-TTL reaper, the LRU cap, and the WS-orphan reap. A client+# holding that live session id gets no signal today — its next prompt fails+# against an id the backend has already forgotten, which reads as the session+# silently vanishing rather than being reclaimed. ``tui_close`` and friends are+# deliberately absent: the client initiated those and already knows.+_RECLAIM_END_REASONS = frozenset({"idle_timeout", "lru_evict", "ws_orphan_reap"})+++def _announce_session_reclaimed(session: dict, end_reason: str) -> None:+ """Tell connected clients a session was reclaimed out from under them.++ Broadcast rather than session-targeted: the reap paths run on background+ timer threads with no contextvar binding, and the WS-orphan case has by+ definition lost its own transport — ``_emit`` would bottom out on stdio and+ the peer that owns the session would never see it. Best-effort; a failed+ notify must never break teardown.+ """+ if end_reason not in _RECLAIM_END_REASONS:+ return+ try:+ _broadcast_global_event(+ "session.reclaimed",+ {+ "session_id": str(session.get("_sid") or ""),+ "stored_session_id": str(session.get("session_key") or ""),+ "reason": end_reason,+ },+ )+ except Exception:+ logger.debug("session.reclaimed broadcast failed", exc_info=True)++def _teardown_session(session: dict | None, *, end_reason: str = "tui_close") -> None:"""Fully tear down a session: finalize, unregister, close agent + worker.@@ -806,6 +839,7 @@ def _teardown_session(session: dict | None, *, end_reason: str = "tui_close") ->if not session:return_finalize_session(session, end_reason=end_reason)+ _announce_session_reclaimed(session, end_reason)try:from tools.approval import unregister_gateway_notify