Skip to content

The agent that grows with you

fix(tui_gateway): start no automatic turn after the user's Stop

Herman Bohdanov966737be6b28
diff --git a/tests/tui_gateway/test_tui_gateway_server.py b/tests/tui_gateway/test_tui_gateway_server.py
index cb923d8ace9..148b159cc90 100644
--- a/tests/tui_gateway/test_tui_gateway_server.py
+++ b/tests/tui_gateway/test_tui_gateway_server.py
@@ -18769,6 +18769,38 @@ def test_notification_poller_skips_consumed(monkeypatch):
process_registry.completion_queue.get_nowait()
+def test_notification_poller_starts_no_turn_after_stop(monkeypatch):
+ """After the user pressed Stop, a completion that arrives must not start an
+ automatic model turn; it waits (requeued) until the user submits again."""
+ import queue as _queue_mod
+
+ from tools.process_registry import process_registry
+
+ started = []
+ sess = _session(running=False, _turn_cancel_requested=True)
+ server._sessions["sid_stopped"] = sess
+ monkeypatch.setattr(server, "_emit", lambda *a, **kw: None)
+ monkeypatch.setattr(server, "_run_prompt_submit", lambda *a, **kw: started.append(a) or True)
+ isolated_queue: _queue_mod.Queue = _queue_mod.Queue()
+ monkeypatch.setattr(process_registry, "completion_queue", isolated_queue)
+ process_registry._completion_consumed.discard("proc_after_stop")
+ isolated_queue.put({
+ "type": "completion", "session_id": "proc_after_stop", "command": "make build",
+ "exit_code": 0, "output": "ok",
+ })
+ stop = threading.Event()
+ stop.set()
+
+ try:
+ server._notification_poller_loop(stop, "sid_stopped", sess)
+
+ assert started == []
+ assert sess["running"] is False
+ assert isolated_queue.get_nowait()["session_id"] == "proc_after_stop"
+ finally:
+ server._sessions.pop("sid_stopped", None)
+
+
def test_notification_poller_requeues_when_busy(monkeypatch):
"""When the agent is busy, the poller requeues the event."""
import queue as _queue_mod
diff --git a/tui_gateway/session_notifications.py b/tui_gateway/session_notifications.py
index 81d61d87e7e..5e4396a2a41 100644
--- a/tui_gateway/session_notifications.py
+++ b/tui_gateway/session_notifications.py
@@ -146,9 +146,11 @@ def _notif_release_turn(session: dict) -> None:
def _notif_claim_turn(session: dict) -> bool:
- """Claim the idle session (running=True) under history_lock; False if a turn is live."""
+ """Claim the idle session (running=True) under history_lock; False if a turn is live.
+ After the user's Stop no automatic turn starts: the cancel latch holds notifications
+ (requeued by the callers) until the next user prompt clears it."""
with _session_turn_admission(session) as admitted:
- if not admitted or session.get("running"):
+ if not admitted or session.get("running") or session.get("_turn_cancel_requested"):
return False
session["running"] = True
return True