Skip to content

The agent that grows with you

fix(gateway): gate compute-host interrupt forward on hosted activity

Teknium4396253aed6b
diff --git a/tests/test_tui_gateway_server.py b/tests/test_tui_gateway_server.py
index e9b0a196a46..78a3b82f4c8 100644
--- a/tests/test_tui_gateway_server.py
+++ b/tests/test_tui_gateway_server.py
@@ -594,6 +594,36 @@ def test_compute_host_interrupt_forwards_when_parent_running_mirror_is_stale(mon
server._sessions.pop(sid, None)
+def test_compute_host_interrupt_skips_lazy_session_with_no_hosted_turn(monkeypatch):
+ """A lazy session that never submitted a hosted turn must not spawn a host.
+
+ ``HostSupervisor.interrupt()`` calls ``start()``, so forwarding the
+ interrupt unconditionally would launch a compute-host child just to
+ deliver an interrupt for a session with no work in it.
+ """
+ class _Supervisor:
+ def interrupt(self, sid, *, request_id=None): # pragma: no cover - must not run
+ raise AssertionError("interrupt must not be forwarded for idle lazy sessions")
+
+ sid = "lazy-idle"
+ session = _session(
+ agent_ready=threading.Event(),
+ running=False,
+ )
+ session["agent"] = None # _session() substitutes a namespace for None
+ server._sessions[sid] = session
+ monkeypatch.setattr(server, "_load_cfg", lambda: {"dashboard": {"turn_isolation": True}})
+ monkeypatch.setattr(server, "_get_compute_host_supervisor", lambda _cfg=None: _Supervisor())
+
+ try:
+ response = server.handle_request(
+ {"id": "interrupt", "method": "session.interrupt", "params": {"session_id": sid}}
+ )
+ assert response["result"] == {"status": "interrupted", "turn_isolation": True}
+ finally:
+ server._sessions.pop(sid, None)
+
+
def test_slash_exec_compress_flag_on_applies_host_control_mirror(monkeypatch):
class _ExplodingWorker:
def __init__(self, *args, **kwargs):
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index 3af450540d7..4e846e36e33 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -1246,9 +1246,16 @@ def _interrupt_session_turn(
if use_compute_host:
# The host owns the live turn. Parent `running` is only a mirror and
- # can lag behind a blocked interactive tool, so let the host determine
- # whether there is work to interrupt.
- _get_compute_host_supervisor().interrupt(sid, request_id=request_id)
+ # can lag behind a blocked interactive tool (a clarify parked on its
+ # Event keeps the host turn alive after the parent flag went stale),
+ # so let the host decide whether there is work to interrupt.
+ # Gate on `_compute_host_active` too: `_session_uses_compute_host`
+ # is also true for lazy sessions that never ran a hosted turn, and
+ # `HostSupervisor.interrupt()` calls `start()` — forwarding
+ # unconditionally would spawn a compute-host child just to deliver
+ # an interrupt no session ever submitted work to.
+ if should_interrupt or session.get("_compute_host_active"):
+ _get_compute_host_supervisor().interrupt(sid, request_id=request_id)
else:
run_thread = session.get("_run_thread")
run_thread_alive = run_thread is not None and run_thread.is_alive()