The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): back a refused notice off exponentially, only where it is requeued
Teknium475d18ea5d87
diff --git a/tests/tui_gateway/test_notification_refusal_backoff.py b/tests/tui_gateway/test_notification_refusal_backoff.pyindex 90e9d0d4517..39bd252bcf8 100644--- a/tests/tui_gateway/test_notification_refusal_backoff.py+++ b/tests/tui_gateway/test_notification_refusal_backoff.py@@ -52,6 +52,13 @@ def test_canonical_busy_notification_backs_off_without_consuming_work(monkeypatcassert registry.completion_queue.get_nowait() is eventassert not completed+ # Still refused (the gateway keeps owning the chat): each retry waits twice as long, to a cap.+ for _ in range(8):+ dispatch()+ assert registry.completion_queue.get_nowait() is event+ assert waits == [0.25, 0.5, 1.0, 2.0, 4.0, 5.0, 5.0, 5.0, 5.0], waits+ assert waits[-1] == server._NOTIF_REFUSAL_BACKOFF_MAX_S+# Human submissions still report ownership refusals.assert server._run_prompt_submit("human", "ui", session, "hello") is Falseassert emitted[0][0] == "error"@@ -63,3 +70,15 @@ def test_canonical_busy_notification_backs_off_without_consuming_work(monkeypatcassert len(completed) == 1assert registry.completion_queue.empty()assert not waits+ assert "_notif_refusal_delay" not in session # the next refusal starts from the floor again+++def test_a_refusal_that_is_not_requeued_does_not_delay_the_poller(monkeypatch):+ """Kanban and /loop callers keep their notice themselves; the poller thread must not sleep for them+ (it also drives the kanban and loop polls)."""+ waits = []+ monkeypatch.setattr(server, "time", SimpleNamespace(sleep=waits.append, time=lambda: 1))+ monkeypatch.setattr(server, "_run_prompt_submit", lambda *a, **k: False)+ session = {"history_lock": threading.RLock(), "running": True}+ assert server._notif_submit("r", "ui", session, "tick", "loop wakeup send failed") is False+ assert session["running"] is False and not waitsdiff --git a/tui_gateway/session_notifications.py b/tui_gateway/session_notifications.pyindex e0bd82d0c4c..f369786c9fd 100644--- a/tui_gateway/session_notifications.py+++ b/tui_gateway/session_notifications.py@@ -171,15 +171,30 @@ def _notif_log_failure(what: str, exc: BaseException) -> None:print(f"[tui_gateway] {what}: {type(exc).__name__}: {exc}", file=sys.stderr)+# A retried notice the canonical ledger refuses (SESSION_NOT_OWNED persists while a gateway owns the+# chat) waits before its requeue, doubling per session up to the kanban/loop poll period so the+# poller thread never delays those polls by more than one period.+_NOTIF_REFUSAL_BACKOFF_S = 0.25+_NOTIF_REFUSAL_BACKOFF_MAX_S = _KANBAN_POLL_SECONDS+++def _notif_refusal_backoff(session: dict) -> None:+ """Wait before requeueing a refused notice: this session's next delay doubles, to a cap."""+ delay = session.get("_notif_refusal_delay") or _NOTIF_REFUSAL_BACKOFF_S+ session["_notif_refusal_delay"] = min(delay * 2, _NOTIF_REFUSAL_BACKOFF_MAX_S)+ time.sleep(delay)++def _notif_submit(rid: str, sid: str, session: dict, text: str, what: str, **kwargs) -> bool:"""Only the execution gate can acknowledge a claimed notification turn."""try:accepted = _run_prompt_submit(rid, sid, session, text, **kwargs)- if not accepted:+ if accepted:+ session.pop("_notif_refusal_delay", None)+ else:+ # Admission can refuse even after the local running flag was claimed. A caller that+ # requeues the notice backs off first (_notif_refusal_backoff); the rest return at once._notif_release_turn(session)- # Admission can refuse even after the local running flag was claimed.- # Callers requeue immediately; queue.get(timeout=...) then never waits.- time.sleep(0.25)return acceptedexcept Exception as exc:_notif_log_failure(what, exc)@@ -545,6 +560,7 @@ def _notif_dispatch_event(sid: str, session: dict, evt: dict, text: str) -> Noneif not _notif_submit(f"__notif__{int(time.time() * 1000)}", sid, session, text,"notification poller dispatch failed", report_ownership_refusal=False, **kwargs):_notif_defer_event(evt, claim)+ _notif_refusal_backoff(session) # requeued: queue.get(timeout=...) would never waitreturnexcept Exception:release_event_delivery(evt, claim)@@ -654,6 +670,8 @@ def _notif_dispatch_completions(sid, session, notifications, registry, deferred)for event, _text, claim in claimed:_notif_defer_event(event, claim,deferred.append if deferred is not None else registry.completion_queue.put)+ if deferred is None: # requeued onto the live queue (a shutdown drain hands them back)+ _notif_refusal_backoff(session)returnexcept Exception:for event, _text, claim in claimed: