Skip to content

The agent that grows with you

fix(tui_gateway): a closing refusal releases only the lease its own admission claimed

Brooklyn Nicholson99d726905420
diff --git a/tests/tui_gateway/test_closed_session_turn_lease.py b/tests/tui_gateway/test_closed_session_turn_lease.py
index ef9f772b1a9..28ece18dbef 100644
--- a/tests/tui_gateway/test_closed_session_turn_lease.py
+++ b/tests/tui_gateway/test_closed_session_turn_lease.py
@@ -125,3 +125,18 @@ def test_close_landing_while_turn_claims_lease_leaves_no_lease(turn_env, monkeyp
"leases": _registry_session_ids(), "reopen_refusal": _reopen_refusal(),
"running": session["running"], "turns_run": turns_run,
} == {"leases": [], "reopen_refusal": None, "running": False, "turns_run": []}
+
+
+def test_closing_refusal_keeps_a_lease_the_session_already_held(turn_env):
+ """Only a lease this admission claimed is released: one held from before the close is the close's
+ own to hand off (an isolated compute-host turn defers it in _settle_isolated_turn_before_close)."""
+ session = _session()
+ turn_env.append(session)
+ assert server._ensure_active_session_slot(SID, session) is None
+ held = session["active_session_lease"]
+ session["_closing"] = True
+
+ assert server._admit_prompt_turn(SID, session, "hello", None, None, None, None) is None
+
+ assert session.get("active_session_lease") is held
+ assert _registry_session_ids() == ["closing-session-key"]
diff --git a/tui_gateway/prompt_turn.py b/tui_gateway/prompt_turn.py
index afaf4050cfc..9a811ff756a 100644
--- a/tui_gateway/prompt_turn.py
+++ b/tui_gateway/prompt_turn.py
@@ -120,6 +120,7 @@ def _admit_prompt_turn(
"""Ownership + liveness gate every turn source must cross; ``(images, agent)`` or None.
Synthesized turns (auto-continue, wake-ups) call ``_run_prompt_submit`` directly — the
bypass that once let a second backend run a duplicate turn."""
+ held_lease = session.get("active_session_lease")
# When the session already holds its lease this is a cheap dict check. See #94778.
if not session.get("_closing") and (ownership_refusal := _ensure_active_session_slot(sid, session)) is not None:
logger.info(
@@ -137,9 +138,10 @@ def _admit_prompt_turn(
and int(session.get("_queued_prompt_generation", 0)) != queued_prompt_generation):
session["running"] = False
session.pop("_submit_user_row", None)
- if session.get("_closing"):
- # Close stops waiting for this thread after a grace and then finalizes. A lease claimed
- # after that finalize has no other code path that releases it.
+ if session.get("_closing") and session.get("active_session_lease") is not held_lease:
+ # Close stops waiting for this thread after a grace and then finalizes. A lease this
+ # admission claimed after that finalize has no other code path that releases it; one
+ # the session already held stays for close's own handoff (_settle_isolated_turn_before_close).
_release_active_session_slot(session)
return None
images = list(session.get("attached_images", []) if image_paths is None else image_paths)