Skip to content

The agent that grows with you

fix(tui_gateway): keep the isolated child's lease borrow inert across compression rotation

teknium1ecefbfa6e1c7
diff --git a/tui_gateway/compute_host_bridge.py b/tui_gateway/compute_host_bridge.py
index 37050e90750..be26cbbe043 100644
--- a/tui_gateway/compute_host_bridge.py
+++ b/tui_gateway/compute_host_bridge.py
@@ -72,7 +72,19 @@ def _compute_host_turn_frame(
# the child adopts it as an inert token instead of re-claiming and being fenced out by
# our own entry ("Session ... already has a live owner"). No lease held = no vouch, and
# the child keeps its legacy self-claim path (fail-closed refusal on conflict).
- "parent_owns_active_session_lease": session.get("active_session_lease") is not None}
+ "active_session_lease": _active_session_lease_vouch(session)}
+
+
+def _active_session_lease_vouch(session: dict) -> dict | None:
+ """``{lease_id, session_id}`` of the REAL registry lease this process holds for the session's
+ current stored id, else None. Qualified, not a bare bool: after a compression rotation a lease
+ still keyed on the old id must not let a (replacement) child borrow the continuation."""
+ lease = session.get("active_session_lease")
+ if lease is None or getattr(lease, "released", False) or not getattr(lease, "enabled", False):
+ return None
+ if str(lease.session_id) != str(session.get("session_key") or ""):
+ return None
+ return {"lease_id": str(lease.lease_id), "session_id": str(lease.session_id)}
def _metadata_mirror(session: dict | None) -> dict:
@@ -85,9 +97,17 @@ def _compute_host_session_info(session: dict) -> dict:
def _compute_host_adopt_frame_meta(session: dict, frame: dict) -> None:
- """Adopt a host frame's session_key / history_version. Caller holds history_lock."""
- if frame.get("session_key"):
- session["session_key"] = str(frame.get("session_key"))
+ """Adopt a host frame's session_key / history_version. Caller holds history_lock.
+
+ A rotated ``session_key`` means the child compressed A->B. The child only holds an inert
+ borrowed token (``_install_borrowed_lease``), so the REAL registry lease is re-anchored here,
+ by its owner — never claimed from the child pid (authority stays singular, #103737 review)."""
+ new_key = str(frame.get("session_key") or "")
+ if new_key and new_key != str(session.get("session_key") or ""):
+ if not _transfer_active_session_slot(str(frame.get("sid") or ""), session, new_session_id=new_key):
+ logger.warning("Compression session lease did not re-anchor: sid=%s old_session_id=%s new_session_id=%s",
+ frame.get("sid"), session.get("session_key"), new_key)
+ session["session_key"] = new_key
if frame.get("history_version") is not None:
with contextlib.suppress(Exception):
session["history_version"] = max(int(session.get("history_version", 0)),
diff --git a/tui_gateway/session_lifecycle.py b/tui_gateway/session_lifecycle.py
index beb68206941..cffc5d233d9 100644
--- a/tui_gateway/session_lifecycle.py
+++ b/tui_gateway/session_lifecycle.py
@@ -95,20 +95,27 @@ def _install_borrowed_lease(sid: str, session: dict, frame: dict) -> None:
same pid AND same live_session_id): every isolated turn failed with "Session ... already
has a live owner" (#101416). The slot is real and owned upstream, so the child must
neither claim a second one nor be able to release/transfer the parent's: the token is
- ``enabled=False`` (release/transfer no-op on it) and ``released=True`` (already "done").
-
- Only claimed when the parent vouched via ``parent_owns_active_session_lease`` in the
- frame. Anything else keeps the legacy behaviour — the child claims for itself and any
- ownership conflict fails CLOSED with the visible refusal.
+ ``enabled=False`` — ``release()`` is a no-op and ``transfer_active_session`` only retargets
+ the token locally, so a compression rotation A->B inside the child never reaches the
+ registry (the parent re-anchors its real lease from the reported ``session_key``, see
+ ``_compute_host_adopt_frame_meta``). NOT ``released=True``: a released token makes the
+ transfer fall through to a real registry claim under the child pid.
+
+ Only installed when the frame's ``active_session_lease`` vouch names THIS stored session
+ id — a parent lease still keyed on a pre-rotation id must not authorize its continuation.
+ Anything else keeps the legacy behaviour — the child claims for itself and any ownership
+ conflict fails CLOSED with the visible refusal.
"""
- if frame.get("parent_owns_active_session_lease") is not True:
+ vouch = frame.get("active_session_lease")
+ if not isinstance(vouch, dict) or session.get("active_session_lease") is not None:
return
- if session.get("active_session_lease") is not None:
+ key = str(session.get("session_key") or "")
+ if not key or str(vouch.get("session_id") or "") != key:
return
from hermes_cli.active_sessions import ActiveSessionLease
session["active_session_lease"] = ActiveSessionLease(
- lease_id=f"borrowed:{sid}", session_id=str(session.get("session_key") or ""),
- surface=str(frame.get("source") or "desktop"), enabled=False, released=True)
+ lease_id=f"borrowed:{vouch.get('lease_id') or sid}", session_id=key,
+ surface=str(frame.get("source") or "desktop"), enabled=False)
def _ensure_active_session_slot(sid: str, session: dict) -> str | None: