The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tui): session_compression — collapse defensive try/except layers, flatten partial-compress split
Teknium765599a1a65f
diff --git a/tui_gateway/session_compression.py b/tui_gateway/session_compression.pyindex 61b933f64c9..f0fb53a2f89 100644--- a/tui_gateway/session_compression.py+++ b/tui_gateway/session_compression.py@@ -57,9 +57,7 @@ def _derived_default_threshold_percent(agent: Any, compression: dict) -> float:model = getattr(agent, "model", "") or ""provider = getattr(agent, "provider", "") or ""autoraise_enabled = str(compression.get("codex_gpt55_autoraise", True)).lower() in {"true", "1", "yes"}- model_cthresh = _compression_threshold_for_model(- model, provider, allow_codex_gpt55_autoraise=autoraise_enabled- )+ model_cthresh = _compression_threshold_for_model(model, provider, allow_codex_gpt55_autoraise=autoraise_enabled)pct, _notice = _resolve_compression_threshold(pct, model_cthresh, model=model,is_codex_autoraise=_is_codex_gpt54_or_gpt55(model, provider) or _is_codex_spark(model, provider),@@ -95,10 +93,7 @@ def _apply_live_compression_config(agent: Any, cfg: dict | None) -> None:model_cfg = cfg.get("model") if isinstance(cfg.get("model"), dict) else {}enabled_raw = compression.get("enabled", True)- if isinstance(enabled_raw, bool):- agent.compression_enabled = enabled_raw- else:- agent.compression_enabled = str(enabled_raw).lower() in {"true", "1", "yes"}+ agent.compression_enabled = enabled_raw if isinstance(enabled_raw, bool) else str(enabled_raw).lower() in {"true", "1", "yes"}agent.codex_responses_native_compaction = is_truthy_value(compression.get("codex_responses_native", False))native_threshold_raw = compression.get("codex_responses_compact_threshold", 200_000)@@ -114,11 +109,8 @@ def _apply_live_compression_config(agent: Any, cfg: dict | None) -> None:agent.codex_responses_compact_threshold = native_threshold# Absence restores the agent_init/config default (0 = disabled).- idle_raw = compression.get("idle_compact_after_seconds", 0)- try:- agent.compression_idle_compact_after_seconds = max(0, int(idle_raw or 0))- except (TypeError, ValueError):- pass+ with contextlib.suppress(TypeError, ValueError):+ agent.compression_idle_compact_after_seconds = max(0, int(compression.get("idle_compact_after_seconds", 0) or 0))cc = getattr(agent, "context_compressor", None)if cc is None:@@ -138,32 +130,25 @@ def _apply_live_compression_config(agent: Any, cfg: dict | None) -> None:continuesetattr(cc, key, max(min_value, value))- try:+ with contextlib.suppress(TypeError, ValueError):ratio_raw = compression.get("target_ratio", _compressor_ctor_default("summary_target_ratio", 0.20))cc.summary_target_ratio = max(0.10, min(float(ratio_raw), 0.80))- except (TypeError, ValueError):- pass+ # Absent or invalid shape (agent_init treats both as empty): stale overrides must stop steering.raw_thresholds = compression.get("model_thresholds")- if isinstance(raw_thresholds, dict):- cc.model_thresholds = {- str(k): float(v)- for k, v in raw_thresholds.items() if isinstance(v, (int, float)) and not isinstance(v, bool)- }- else:- # Absent or invalid shape (agent_init treats both as empty): stale overrides must stop steering.- cc.model_thresholds = {}+ cc.model_thresholds = {+ str(k): float(v)+ for k, v in raw_thresholds.items() if isinstance(v, (int, float)) and not isinstance(v, bool)+ } if isinstance(raw_thresholds, dict) else {}# threshold: present value wins; absence derives via the agent_init resolution (default + autoraise).pct: float | None = Noneif "threshold" in compression:- try:+ with contextlib.suppress(TypeError, ValueError):pct = float(compression["threshold"])- except (TypeError, ValueError):- pct = Noneif pct is None:pct = _derived_default_threshold_percent(agent, compression)- try:+ with contextlib.suppress(TypeError, ValueError):cc._config_threshold_percent = pctcc._configured_threshold_percent = pctbase = pct@@ -180,8 +165,6 @@ def _apply_live_compression_config(agent: Any, cfg: dict | None) -> None:cc.threshold_percent = pctelse:cc.threshold_percent = pct- except (TypeError, ValueError):- passraw_ctx = model_cfg.get("context_length")if raw_ctx is not None:@@ -191,10 +174,8 @@ def _apply_live_compression_config(agent: Any, cfg: dict | None) -> None:new_ctx = 0if new_ctx > 0:cc._config_context_length = new_ctx- try:+ with contextlib.suppress(Exception):cc.context_length = new_ctx- except Exception:- passelif getattr(cc, "_config_context_length", None) is not None:# model.context_length removed: drop the override and force re-inference from model metadata# on next access (construction's deferred resolution); re-applies the small-context floor too.@@ -222,9 +203,7 @@ def _apply_live_compression_config(agent: Any, cfg: dict | None) -> None:compute = getattr(cc, "_compute_threshold_tokens", None)if callable(compute):cc.threshold_tokens = compute(- getattr(cc, "context_length", 0) or 0,- getattr(cc, "threshold_percent", 0.5),- getattr(cc, "max_tokens", None),+ getattr(cc, "context_length", 0) or 0, getattr(cc, "threshold_percent", 0.5), getattr(cc, "max_tokens", None))cc._apply_threshold_tokens_cap()else:@@ -313,18 +292,14 @@ def _compress_session_history(partial, keep_last, focus_topic = parse_partial_compress_args(focus_topic or "")# Only the head is summarized; the last `keep_last` exchanges ride along verbatim. A degenerate# split (empty tail) falls back to full compression so the user still gets an action.- tail: list = []- head = history- if partial:- head, tail = split_history_for_partial_compress(history, keep_last)- if not tail:- partial = False- head = history+ head, tail = (split_history_for_partial_compress(history, keep_last) if partial else (history, []))+ if not tail:+ partial, head = False, historyif approx_tokens is None:# Include system prompt + tool schemas so the figure reflects real request pressure.- _sys_prompt = getattr(agent, "_cached_system_prompt", "") or ""- _tools = getattr(agent, "tools", None) or None- approx_tokens = estimate_request_tokens_rough(history, system_prompt=_sys_prompt, tools=_tools)+ approx_tokens = estimate_request_tokens_rough(+ history, system_prompt=getattr(agent, "_cached_system_prompt", "") or "", tools=getattr(agent, "tools", None) or None+ )# system_message=None: _compress_context rebuilds the system prompt via _build_system_prompt(None);# passing the cached prompt (already holding the identity block) appends the identity twice.# force=True: every caller is a manual /compress path, which bypasses the summary-failure@@ -346,7 +321,7 @@ def _compress_session_history(finalize_context_engine_compression_notification(agent, committed=False)raise CompressionLockHeld(_lock_skipped if isinstance(_lock_skipped, str) else None)- if partial and tail:+ if partial:compressed = rejoin_compressed_head_and_tail(compressed, tail)with session["history_lock"]:if int(session.get("history_version", 0)) != history_version:@@ -375,14 +350,15 @@ def _sync_session_key_after_compress(if not new_session_id or new_session_id == old_key:return- lease_reanchored = _transfer_active_session_slot(sid, session, new_session_id=new_session_id)- if not lease_reanchored:+ if not _transfer_active_session_slot(sid, session, new_session_id=new_session_id):logger.warning("Compression session lease did not re-anchor: sid=%s old_session_id=%s new_session_id=%s",sid, old_key, new_session_id,)- try:+ # Even if the approval module fails to import, anchor session_key on the continuation id.+ session["session_key"] = new_session_id+ with contextlib.suppress(Exception):from tools.approval import (disable_session_yolo, enable_session_yolo, is_session_yolo_enabled, register_gateway_notify,unregister_gateway_notify,@@ -390,7 +366,6 @@ def _sync_session_key_after_compress(with contextlib.suppress(Exception):unregister_gateway_notify(old_key)- session["session_key"] = new_session_idtry:yolo_was_on = is_session_yolo_enabled(old_key)except Exception:@@ -401,9 +376,6 @@ def _sync_session_key_after_compress(disable_session_yolo(old_key)with contextlib.suppress(Exception):register_gateway_notify(new_session_id, lambda data: _emit_approval_request(sid, data))- except Exception:- # Even if the approval module fails to import, anchor session_key on the continuation id.- session["session_key"] = new_session_id# Invalidate any in-flight ``_drain_queued_prompt`` claim taken under the pre-rotation key: a raced# drain must not dispatch on the continuation (its envelope is restored to the queue).diff --git a/tui_gateway/session_notifications.py b/tui_gateway/session_notifications.pyindex f03be675f41..b7829217351 100644--- a/tui_gateway/session_notifications.py+++ b/tui_gateway/session_notifications.py@@ -1,8 +1,6 @@-"""Per-session notification poller: kanban/loop/delegation events routed to the owning session, desktop UI wiring, HUD surface note.--Bodies are rebound onto server.py's globals at install time (see-method_ctx.bind_module), so they reference server.py globals bare.-"""+"""Per-session notification poller: kanban/loop/delegation events routed to the owning session,+desktop UI wiring, HUD surface note. Bodies are rebound onto server.py's globals at install time+(method_ctx.bind_module), so they reference server.py globals bare."""from __future__ import annotations@@ -15,8 +13,7 @@ _registry = HandlerRegistry()def _notif_locked_sessions(fn, default):- """Run ``fn(_sessions)`` under ``_sessions_lock``; ``default`` if that fails (the poller- thread must never crash on a lock/enumeration failure)."""+ """Run ``fn(_sessions)`` under ``_sessions_lock``; ``default`` on failure (poller must never crash)."""try:with _sessions_lock:return fn(_sessions)@@ -32,6 +29,13 @@ def _notif_session_matches(s: dict, keys) -> bool:return str(s.get("session_key") or "") in keys or _session_lookup_key(s, fallback="") in keys+def _notif_live_session_matches(keys) -> bool:+ """Any non-finalized live session matches ``keys`` (False if the registry can't be read)."""+ return _notif_locked_sessions(+ lambda ss: any(not s.get("_finalized") and _notif_session_matches(s, keys) for s in ss.values()), False+ )++def _notif_resolve_event_key(evt_key: str) -> str:"""Resolve a compression-rotated session key to its continuation tip (or itself)."""try:@@ -56,13 +60,10 @@ def _notification_event_belongs_elsewhere(sid: str, session: dict, evt: dict) ->return True# Exact UI tab gone: fall through to durable session_key routing so a# resumed continuation with the same key/lineage can still claim it.-evt_key = str(evt.get("session_key") or "")if not evt_key:return False-current_keys = _notif_current_keys(sid, session)-# Compression can rotate AIAgent.session_id while the detached child is still running: map# the event's original key to its continuation tip so it reaches the live session instead of# becoming an orphan any poller may consume. A live continuation wins over the compressed@@ -71,19 +72,13 @@ def _notification_event_belongs_elsewhere(sid: str, session: dict, evt: dict) ->if resolved_key != evt_key:if resolved_key in current_keys:return False- if _notif_locked_sessions(- lambda ss: any(not s.get("_finalized") and _notif_session_matches(s, {resolved_key}) for s in ss.values()),- False,- ):+ if _notif_live_session_matches({resolved_key}):return True-if evt_key in current_keys:return False-snapshot = _notif_locked_sessions(lambda ss: list(ss.values()), None)if snapshot is None:return False # can't enumerate: fail open rather than drop the event-keys = {evt_key, resolved_key}return any(s is not session and not s.get("_finalized") and _notif_session_matches(s, keys) for s in snapshot)@@ -129,9 +124,9 @@ def _notification_event_dedup_key(evt: dict) -> tuple:return (evt_sid, evt_type)-# Mirror gateway/kanban_watchers.py TERMINAL_KINDS: claim silent kinds too so-# the cursor advances past them and they can't wedge a later completed/blocked-# event behind an unclaimed row.+# Mirror gateway/kanban_watchers.py TERMINAL_KINDS: claim silent kinds (archived/unblocked) too so+# the cursor advances past them and they can't wedge a later completed/blocked event behind an+# unclaimed row._KANBAN_NOTIFY_KINDS = ("completed", "blocked", "gave_up", "crashed", "timed_out", "status", "archived", "unblocked")_KANBAN_POLL_SECONDS = 5.0_LOOP_POLL_SECONDS = 5.0@@ -155,6 +150,17 @@ def _notif_log_failure(what: str, exc: BaseException) -> None:print(f"[tui_gateway] {what}: {type(exc).__name__}: {exc}", file=sys.stderr)+def _notif_submit(rid: str, sid: str, session: dict, text: str, what: str, **kwargs) -> None:+ """message.start + _run_prompt_submit for a claimed (running=True) turn; releases on failure."""+ try:+ _emit("message.start", sid)+ _run_prompt_submit(rid, sid, session, text, **kwargs)+ except Exception as exc:+ _notif_log_failure(what, exc)+ _notif_release_turn(session)+ raise++def _maybe_fire_tui_loop_tick(sid: str, session: dict) -> None:"""Fire a due /loop wakeup for an idle TUI/Desktop/dashboard session (per-session poller,coarse cadence). Claims the session under history_lock (running=True) before dispatching so@@ -163,56 +169,50 @@ def _maybe_fire_tui_loop_tick(sid: str, session: dict) -> None:from hermes_cli.loops import LoopManager, goal_blocks_loop_tickexcept Exception:return-sid_key = session.get("session_key") or ""if not sid_key:returnmgr = LoopManager(session_id=sid_key)- if not mgr.is_due() or goal_blocks_loop_tick(sid_key):- return- if not _notif_claim_turn(session):+ if not mgr.is_due() or goal_blocks_loop_tick(sid_key) or not _notif_claim_turn(session):return # busy — stays due, next poll retries-wakeup = mgr.fire_tick()if not wakeup:_notif_release_turn(session)return-tick_no = mgr.state.ticks_fired if mgr.state else "?"rid = f"__loop__{int(time.time() * 1000)}"try:_emit("status.update", sid, {"kind": "loop", "text": f"↻ /loop wakeup #{tick_no} firing…"})- if wakeup.lstrip().startswith("/"):- # Slash-command loop: route through the slash pipeline, not the- # model. No model reply to evaluate — complete immediately.- _notif_release_turn(session)- try:- parts = wakeup.lstrip()[1:].split(None, 1)- resp = _methods["command.dispatch"](- rid,- {"name": parts[0] if parts else "", "arg": parts[1] if len(parts) > 1 else "", "session_id": sid},- )- payload = (resp or {}).get("result") or {}- out = str(payload.get("output") or "").strip()- if out:- _emit("status.update", sid, {"kind": "loop", "text": out})- if payload.get("type") == "send" and payload.get("message"):- # Command resolves to a prompt (skill command etc.) — run it- # as a normal turn; the post-turn hook completes the tick.- if not _notif_claim_turn(session):- mgr.abandon_tick()- return- _emit("message.start", sid)- _run_prompt_submit(rid, sid, session, payload["message"])- return- except Exception:- pass- decision = mgr.complete_tick("")- if decision.get("message"):- _emit("status.update", sid, {"kind": "loop", "text": decision["message"]})+ if not wakeup.lstrip().startswith("/"):+ _emit("message.start", sid)+ _run_prompt_submit(rid, sid, session, wakeup)return- _emit("message.start", sid)- _run_prompt_submit(rid, sid, session, wakeup)+ # Slash-command loop: route through the slash pipeline, not the model. No model reply+ # to evaluate — complete immediately.+ _notif_release_turn(session)+ try:+ parts = wakeup.lstrip()[1:].split(None, 1)+ resp = _methods["command.dispatch"](+ rid, {"name": parts[0] if parts else "", "arg": parts[1] if len(parts) > 1 else "", "session_id": sid}+ )+ payload = (resp or {}).get("result") or {}+ out = str(payload.get("output") or "").strip()+ if out:+ _emit("status.update", sid, {"kind": "loop", "text": out})+ if payload.get("type") == "send" and payload.get("message"):+ # Command resolves to a prompt (skill command etc.) — run it as a normal turn;+ # the post-turn hook completes the tick.+ if not _notif_claim_turn(session):+ mgr.abandon_tick()+ return+ _emit("message.start", sid)+ _run_prompt_submit(rid, sid, session, payload["message"])+ return+ except Exception:+ pass+ decision = mgr.complete_tick("")+ if decision.get("message"):+ _emit("status.update", sid, {"kind": "loop", "text": decision["message"]})except Exception as exc:_notif_log_failure("loop wakeup dispatch failed", exc)_notif_release_turn(session)@@ -228,23 +228,11 @@ def _kb_first_line(value: Any, limit: int) -> str:def _kb_completed(task, payload: dict, title: str) -> str:if payload.get("summary"):handoff = _kb_first_line(payload["summary"], 200)- elif getattr(task, "result", None):- handoff = _kb_first_line(task.result, 160)else:- handoff = ""+ handoff = _kb_first_line(task.result, 160) if getattr(task, "result", None) else ""return f" done — {title}{handoff}"-def _kb_blocked(task, payload: dict, title: str) -> str:- reason = f": {str(payload.get('reason'))[:160]}" if payload.get("reason") else ""- return f" blocked{reason}"---def _kb_gave_up(task, payload: dict, title: str) -> str:- err = f"\n{str(payload.get('error'))[:200]}" if payload.get("error") else ""- return f" gave up after repeated spawn failures{err}"--def _kb_timed_out(task, payload: dict, title: str) -> str:try:limit = int(payload.get("limit_seconds") or 0)@@ -253,20 +241,21 @@ def _kb_timed_out(task, payload: dict, title: str) -> str:return f" timed out (max_runtime={limit}s); will retry"-# kind -> (glyph, suffix formatter); silent kinds (archived/unblocked) are absent → None.+# kind -> (glyph, suffix after "Kanban <id>"); silent kinds (archived/unblocked) are absent → None._KANBAN_EVENT_FORMATTERS = {"completed": ("✔", _kb_completed),- "blocked": ("⏸", _kb_blocked),- "gave_up": ("✖", _kb_gave_up),- "crashed": ("✖", lambda task, payload, title: " worker crashed (pid gone); dispatcher will retry"),+ "blocked": ("⏸", lambda t, p, title: " blocked" + (f": {str(p.get('reason'))[:160]}" if p.get("reason") else "")),+ "gave_up": ("✖", lambda t, p, title: " gave up after repeated spawn failures"+ + (f"\n{str(p.get('error'))[:200]}" if p.get("error") else "")),+ "crashed": ("✖", lambda t, p, title: " worker crashed (pid gone); dispatcher will retry"),"timed_out": ("⏱", _kb_timed_out),- "status": ("🔄", lambda task, payload, title: f" → {payload.get('status') or ''}"),+ "status": ("🔄", lambda t, p, title: f" → {p.get('status') or ''}"),}def _format_kanban_event_text(sub: dict, task, ev, board_slug: str) -> Optional[str]:- """Single-line notification text for one kanban event; wording mirrors- gateway/kanban_watchers.py so it reads the same as on Telegram. None for silent kinds."""+ """Single-line notification text for one kanban event; wording mirrors gateway/kanban_watchers.py+ so it reads the same as on Telegram. None for silent kinds."""entry = _KANBAN_EVENT_FORMATTERS.get(getattr(ev, "kind", ""))if entry is None:return None@@ -276,8 +265,7 @@ def _format_kanban_event_text(sub: dict, task, ev, board_slug: str) -> Optional[board_tag = f"[{board_slug}] " if board_slug else ""who = getattr(task, "assignee", None) or ""tag = f"@{who} " if who else ""- payload = getattr(ev, "payload", None) or {}- return f"{glyph} {board_tag}{tag}Kanban {task_id}{fmt(task, payload, title)}"+ return f"{glyph} {board_tag}{tag}Kanban {task_id}{fmt(task, getattr(ev, 'payload', None) or {}, title)}"def _collect_kanban_notifications(session: dict) -> list:@@ -303,8 +291,8 @@ def _collect_kanban_notifications(session: dict) -> list:boards = [_kb.read_board_metadata(_kb.DEFAULT_BOARD)]except Exception:return []- # Poll each resolved DB path once — multiple slugs can point at the same- # DB when HERMES_KANBAN_DB pins the board path (same guard as the gateway).+ # Poll each resolved DB path once — multiple slugs can point at the same DB when+ # HERMES_KANBAN_DB pins the board path (same guard as the gateway).seen_db_paths: set = set()for board_meta in boards:slug = (board_meta or {}).get("slug") or _kb.DEFAULT_BOARD@@ -316,9 +304,9 @@ def _collect_kanban_notifications(session: dict) -> list:if resolved in seen_db_paths:continueseen_db_paths.add(resolved)- # One poller per live session: don't open this board writable unless it- # has a subscription owned by this exact session. If the read-only probe- # fails (locked/corrupt DB), preserve delivery and fall through.+ # One poller per live session: don't open this board writable unless it has a subscription+ # owned by this exact session. If the read-only probe fails (locked/corrupt DB), preserve+ # delivery and fall through.try:if _kb.count_notify_subs(board=slug, platform="tui", chat_id=session_key) == 0:continue@@ -345,9 +333,9 @@ def _collect_kanban_notifications(session: dict) -> list:continuetask = _kb.get_task(conn, sub["task_id"])texts.extend(t for t in (_format_kanban_event_text(sub, task, ev, slug) for ev in events) if t)- # Unsubscribe only on archive: ``done`` is reversible in review/controller flows,- # so keeping the sub lets a later reopen notify the same session. The claimed- # cursor prevents replay.+ # Unsubscribe only on archive: ``done`` is reversible in review/controller flows, so+ # keeping the sub lets a later reopen notify the same session. The claimed cursor+ # prevents replay.if task and getattr(task, "status", "") == "archived":with contextlib.suppress(Exception):_kb.remove_notify_sub(conn, **sub_ident)@@ -379,12 +367,11 @@ def _notif_poll_kanban(sid: str, session: dict) -> None:_batch = list(_pending)session["_kanban_pending"] = []if _batch:- try:- _emit("message.start", sid)- _run_prompt_submit(f"__notif__{int(time.time() * 1000)}", sid, session, "\n".join(_batch))- except Exception as exc:- _notif_log_failure("kanban notification dispatch failed", exc)- _notif_release_turn(session)+ with contextlib.suppress(Exception):+ _notif_submit(+ f"__notif__{int(time.time() * 1000)}", sid, session, "\n".join(_batch),+ "kanban notification dispatch failed",+ )def _notif_render_event(sid: str, evt: dict, emitted: set, process_registry, format_process_notification) -> Optional[str]:@@ -407,22 +394,21 @@ def _notif_dispatch_event(sid: str, session: dict, evt: dict, text: str) -> None"""Run the claimed (running=True) agent turn for one notification event."""from tools.async_delegation import claim_event_delivery, complete_event_delivery, release_event_delivery- rid = f"__notif__{int(time.time() * 1000)}"claim = claim_event_delivery(evt, "tui-poller")if claim is None:return+ kwargs = (+ {"display_kind": "async_delegation_complete", "display_metadata": _async_delegation_display_metadata(evt)}+ if evt.get("type") == "async_delegation" else {}+ )try:- _emit("message.start", sid)- kwargs = (- {"display_kind": "async_delegation_complete", "display_metadata": _async_delegation_display_metadata(evt)}- if evt.get("type") == "async_delegation" else {}+ _notif_submit(+ f"__notif__{int(time.time() * 1000)}", sid, session, text, "notification poller dispatch failed", **kwargs)- _run_prompt_submit(rid, sid, session, text, **kwargs)- complete_event_delivery(evt, claim)- except Exception as exc:+ except Exception:release_event_delivery(evt, claim)- _notif_log_failure("notification poller dispatch failed", exc)- _notif_release_turn(session)+ return+ complete_event_delivery(evt, claim)def _notif_classify_event(sid: str, session: dict, evt: dict) -> str:@@ -435,6 +421,52 @@ def _notif_classify_event(sid: str, session: dict, evt: dict) -> str:return "mine"+def _notif_handle_event(sid, session, evt, emitted, registry, fmt, deferred) -> bool:+ """Route one dequeued event. ``deferred`` is None in the live loop (foreign events go straight+ back on the queue) and the shutdown-drain list otherwise (foreign + orphaned delegation+ payloads are set aside for a resume). Returns False when the drain must stop (session busy)."""+ queue = registry.completion_queue+ owner = _notif_classify_event(sid, session, evt)+ if owner == "foreign":+ if deferred is None:+ # Leave foreign events for their owner — otherwise a process started in session A+ # surfaces its completion in whichever poller wakes first.+ queue.put(evt)+ time.sleep(0.1)+ else:+ deferred.append(evt)+ return True+ if owner == "unowned":+ is_delegation = evt.get("type") == "async_delegation"+ origin, key = str(evt.get("origin_ui_session_id") or ""), str(evt.get("session_key") or "")+ if deferred is None:+ (logger.warning if is_delegation else logger.debug)(+ "Dropping unowned %s notification (origin=%r key=%r) instead of delivering to session %s",+ evt.get("type", "completion"), origin, key, sid,+ )+ elif is_delegation:+ deferred.append(evt)+ else:+ logger.debug(+ "Dropping unowned %s notification during shutdown drain (origin=%r key=%r)",+ evt.get("type", "completion"), origin, key,+ )+ return True+ text = _notif_render_event(sid, evt, emitted, registry, fmt)+ if not text:+ return True+ if not _notif_claim_turn(session):+ queue.put(evt)+ if deferred is not None:+ return False+ # Back off: the re-queued event keeps the queue non-empty, so without a sleep this loop+ # spins at 100% CPU while busy.+ time.sleep(0.25)+ return True+ _notif_dispatch_event(sid, session, evt, text)+ return True++def _notification_poller_loop(stop_event: threading.Event, sid: str, session: dict) -> None:"""Poll completion_queue and dispatch notifications autonomously (daemon thread started by_init_session()): emit a status.update (kind=process), then chain an agent turn via@@ -446,8 +478,7 @@ def _notification_poller_loop(stop_event: threading.Event, sid: str, session: diqueue = process_registry.completion_queue_emitted = set() # dedup re-queued events so one completion isn't emitted 50 times while busy- _last_kanban_poll = 0.0- _last_loop_poll = 0.0+ _last_kanban_poll = _last_loop_poll = 0.0while not stop_event.is_set() and not session.get("_finalized"):_now = time.monotonic()# /loop wakeup driver: fire a due tick for THIS session while idle (same claim-under-lock@@ -465,63 +496,18 @@ def _notification_poller_loop(stop_event: threading.Event, sid: str, session: dievt = queue.get(timeout=0.5)except Exception:continue+ _notif_handle_event(sid, session, evt, _emitted, process_registry, format_process_notification, None)- owner = _notif_classify_event(sid, session, evt)- if owner == "foreign":- # Leave foreign events for their owner — otherwise a process started in- # session A surfaces its completion in whichever poller wakes first.- queue.put(evt)- time.sleep(0.1)- continue- if owner == "unowned":- log = logger.warning if evt.get("type") == "async_delegation" else logger.debug- log(- "Dropping unowned %s notification (origin=%r key=%r) instead of delivering to session %s",- evt.get("type", "completion"), str(evt.get("origin_ui_session_id") or ""),- str(evt.get("session_key") or ""), sid,- )- continue-- text = _notif_render_event(sid, evt, _emitted, process_registry, format_process_notification)- if not text:- continue- if not _notif_claim_turn(session):- # Back off: the re-queued event keeps the queue non-empty, so- # without a sleep this loop spins at 100% CPU while busy.- queue.put(evt)- time.sleep(0.25)- continue- _notif_dispatch_event(sid, session, evt, text)-- # Drain remaining events after the stop signal so nothing is lost on shutdown. Other live- # sessions' events are set aside and re-queued; orphaned events (owner gone) are dropped —- # same guard as the main loop, except orphaned delegation payloads are deferred for a resume.+ # Drain remaining events after the stop signal so nothing is lost on shutdown; foreign and+ # orphaned-delegation events are handed back to the shared queue afterwards.deferred: list = []while not queue.empty():try:evt = queue.get_nowait()except Exception:break- owner = _notif_classify_event(sid, session, evt)- if owner == "foreign" or (owner == "unowned" and evt.get("type") == "async_delegation"):- deferred.append(evt)- continue- if owner == "unowned":- logger.debug(- "Dropping unowned %s notification during shutdown drain (origin=%r key=%r)",- evt.get("type", "completion"), str(evt.get("origin_ui_session_id") or ""),- str(evt.get("session_key") or ""),- )- continue- text = _notif_render_event(sid, evt, _emitted, process_registry, format_process_notification)- if not text:- continue- if not _notif_claim_turn(session):- queue.put(evt)+ if not _notif_handle_event(sid, session, evt, _emitted, process_registry, format_process_notification, deferred):break- _notif_dispatch_event(sid, session, evt, text)-- # Hand any other sessions' events back to the shared queue.for evt in deferred:queue.put(evt)@@ -561,8 +547,8 @@ def _wire_agent_terminal_output() -> None:return ""def _emit_agent_terminal_close(session, process_id):- # session may be None (process already finished/pruned) — the tab can- # still linger and be closed; route to the owning window when we can.+ # session may be None (process already finished/pruned) — the tab can still linger and+ # be closed; route to the owning window when we can._emit("terminal.close", _owner_sid_for_process(session) if session is not None else "", {"process_id": process_id})if getattr(process_registry, "on_output", None) is None:@@ -587,7 +573,6 @@ def _wire_desktop_ui() -> None:from tools import desktop_uiexcept Exception:return-desktop_ui.set_emitter(lambda sid, event, payload: _emit(event, sid, payload))_desktop_ui_wired = True@@ -599,11 +584,10 @@ _notification_pollers: list = []def _start_notification_poller(sid: str, session: dict) -> threading.Event:- """Start the background notification poller for a TUI session."""+ """Start the background notification poller for a TUI session (thread name is greppable)."""_wire_agent_terminal_output()_wire_desktop_ui()stop = threading.Event()- # Thread name is greppable for debuggers/test teardowns.t = threading.Thread(target=_notification_poller_loop, args=(stop, sid, session), daemon=True, name=f"tui-notif-poller-{sid}")