The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tui-gateway): one _reattach_refusal helper for the live-identity/interrupt-settling guard
kshitijk4poorf2077a020989
diff --git a/tui_gateway/methods_prompt.py b/tui_gateway/methods_prompt.pyindex 84b76b13d7d..4455fa22c19 100644--- a/tui_gateway/methods_prompt.py+++ b/tui_gateway/methods_prompt.py@@ -575,10 +575,8 @@ def _(rid, params: dict) -> dict:# Re-bind to the current transport: streaming must stay on the active websocket even# if a disconnect/fallback moved the session to stdio.with _session_resume_lock:- if _sessions.get(sid) is not session:- return _err(rid, 4007, "session no longer live; retry resume")- if session.get("_client_gone_interrupt_requested"):- return _err(rid, 4009, "session disconnect interrupt settling")+ if (refusal := _reattach_refusal(rid, sid, session)) is not None:+ return refusalif (t := current_transport()) is not None:session["transport"] = t_cancel_ws_orphan_reap(sid)diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.pyindex 424266827b7..9190987f883 100644--- a/tui_gateway/methods_session.py+++ b/tui_gateway/methods_session.py@@ -524,10 +524,8 @@ def _resume_live_unpersisted(ctx: _Resume, live_sid: str, live: dict) -> dict:if ctx.owns_db:_release_db(ctx.db)with _session_resume_lock:- if _sessions.get(live_sid) is not live:- return _err(ctx.rid, 4007, "session no longer live; retry resume")- if live.get("_client_gone_interrupt_requested"):- return _err(ctx.rid, 4009, "session disconnect interrupt settling")+ if (refusal := _reattach_refusal(ctx.rid, live_sid, live)) is not None:+ return refusallive["last_active"] = time.time()if (transport := current_transport()) is not None:with live.setdefault("history_lock", threading.Lock()):@@ -637,10 +635,8 @@ def _resume_reuse_live(ctx: _Resume, sid: str, session: dict) -> dict:def _resume_reuse_live_locked(ctx: _Resume, sid: str, session: dict) -> dict:"""Reuse with _session_resume_lock already held (including the eager double-check)."""- if _sessions.get(sid) is not session:- return _err(ctx.rid, 4007, "session no longer live; retry resume")- if session.get("_client_gone_interrupt_requested"):- return _err(ctx.rid, 4009, "session disconnect interrupt settling")+ if (refusal := _reattach_refusal(ctx.rid, sid, session)) is not None:+ return refusal_cancel_ws_orphan_reap(sid) # unconditionally: the fast path must never race the reap Timerpayload = _live_session_payload(sid, session, cols=ctx.cols, touch=True, omit_messages=ctx.omit_messages,transport=current_transport() or _stdio_transport)@@ -898,10 +894,8 @@ def _(rid, params: dict, session: dict) -> dict:"""Attach the frontend to a live TUI session without closing the previously focused one."""sid = str(params.get("session_id") or "")with _session_resume_lock:- if _sessions.get(sid) is not session:- return _err(rid, 4007, "session no longer live; retry resume")- if session.get("_client_gone_interrupt_requested"):- return _err(rid, 4009, "session disconnect interrupt settling")+ if (refusal := _reattach_refusal(rid, sid, session)) is not None:+ return refusalreturn _ok(rid, _live_session_payload(sid, session, touch=True, transport=current_transport() or _stdio_transport,omit_messages=is_truthy_value(params.get("omit_messages", False))))diff --git a/tui_gateway/session_lifecycle.py b/tui_gateway/session_lifecycle.pyindex 3fc43e51f97..51161867640 100644--- a/tui_gateway/session_lifecycle.py+++ b/tui_gateway/session_lifecycle.py@@ -443,6 +443,17 @@ def _cancel_ws_orphan_reap(sid: str) -> None:timer.cancel()+def _reattach_refusal(rid, sid: str, session: dict) -> dict | None:+ """Under ``_session_resume_lock``: the error a reattaching RPC (resume/activate/prompt.submit) must return+ instead of rebinding — ``session`` is no longer the live record for ``sid``, or a client-gone interrupt is+ still settling and the reap Timer must keep polling. None when the reattach may proceed."""+ if _sessions.get(sid) is not session:+ return _err(rid, 4007, "session no longer live; retry resume")+ if session.get("_client_gone_interrupt_requested"):+ return _err(rid, 4009, "session disconnect interrupt settling")+ return None++def _ws_orphan_turn_activity_is_fresh(session: dict) -> bool:"""Whether a detached RUNNING turn's activity clock (``_touch_activity``) is still fresh — the reaper must NOTinterrupt healthy detached work (closed laptop). Conservative: disabled threshold, missing/opaque agent, unreadable