From 2da1a3cd2104b7acf29c9c2c04a464bfd0c07028 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 19:25:50 -0700 Subject: [PATCH] refactor(gateway): compact kanban watcher/notifier call sites and state init --- gateway/kanban_watchers.py | 39 ++++++--------- gateway/kanban_watchers_common.py | 15 ++---- gateway/kanban_watchers_notifier.py | 75 +++++++++-------------------- 3 files changed, 44 insertions(+), 85 deletions(-) diff --git a/gateway/kanban_watchers.py b/gateway/kanban_watchers.py index ce081bd000..f38e6ee422 100644 --- a/gateway/kanban_watchers.py +++ b/gateway/kanban_watchers.py @@ -81,10 +81,8 @@ class GatewayKanbanWatchersMixin: sub_fail_counts: dict[tuple, int] = getattr(self, "_kanban_sub_fail_counts", {}) self._kanban_sub_fail_counts = sub_fail_counts - notifier_profile = getattr(self, "_kanban_notifier_profile", None) - if not notifier_profile: - notifier_profile = self._active_profile_name() - self._kanban_notifier_profile = notifier_profile + notifier_profile = getattr(self, "_kanban_notifier_profile", None) or self._active_profile_name() + self._kanban_notifier_profile = notifier_profile # Initial delay so the gateway can finish wiring adapters. await asyncio.sleep(5) @@ -224,19 +222,15 @@ class GatewayKanbanWatchersMixin: _lock_path = _kb.kanban_home() / "kanban" / ".dispatcher.lock" _lock_handle, _lock_state = _acquire_singleton_lock(_lock_path) if _lock_state == "contended": - logger.info( - "kanban dispatcher: another gateway already holds the dispatcher " - "lock (%s); this gateway will NOT dispatch.", _lock_path, - ) + logger.info("kanban dispatcher: another gateway already holds the dispatcher " + "lock (%s); this gateway will NOT dispatch.", _lock_path) return None if _lock_state == "held": self._kanban_dispatcher_lock_handle = _lock_handle # hold for process lifetime logger.info("kanban dispatcher: holding singleton dispatcher lock (%s)", _lock_path) else: - logger.warning( - "kanban dispatcher: advisory lock unavailable at %s; proceeding " - "on config control alone.", _lock_path, - ) + logger.warning("kanban dispatcher: advisory lock unavailable at %s; proceeding " + "on config control alone.", _lock_path) return _load_config, _kb, kanban_cfg async def _kanban_dispatcher_watcher(self) -> None: @@ -291,17 +285,16 @@ class GatewayKanbanWatchersMixin: any_spawned = _log_spawn_results(results) ready_pending = await _to_thread_process_service(dispatcher.ready_nonempty) bad_ticks = bad_ticks + 1 if ready_pending and not any_spawned else 0 - if bad_ticks >= _HEALTH_WINDOW: - now = int(time.time()) - if now - last_warn_at >= 300: - logger.warning( - "kanban dispatcher stuck: ready queue non-empty for " - "%d consecutive ticks but 0 workers spawned. Check " - "profile health (venv, PATH, credentials) and " - "`hermes kanban list --status ready`.", - bad_ticks, - ) - last_warn_at = now + now = int(time.time()) + if bad_ticks >= _HEALTH_WINDOW and now - last_warn_at >= 300: + logger.warning( + "kanban dispatcher stuck: ready queue non-empty for " + "%d consecutive ticks but 0 workers spawned. Check " + "profile health (venv, PATH, credentials) and " + "`hermes kanban list --status ready`.", + bad_ticks, + ) + last_warn_at = now except asyncio.CancelledError: logger.debug("kanban dispatcher: cancelled") self._release_kanban_dispatcher_lock() diff --git a/gateway/kanban_watchers_common.py b/gateway/kanban_watchers_common.py index e9e9fdb117..167e24d49b 100644 --- a/gateway/kanban_watchers_common.py +++ b/gateway/kanban_watchers_common.py @@ -46,15 +46,6 @@ def _board_slugs(kb: Any) -> list: return [b.get("slug") or kb.DEFAULT_BOARD for b in _list_boards(kb)] -def _kanban_cfg(load_config: Callable[[], Any]) -> Optional[dict]: - """``kanban`` section of the live config; ``None`` when the loader raises.""" - try: - cfg = load_config() - except Exception: - return None - return cfg.get("kanban", {}) if isinstance(cfg, dict) else {} - - def _positive_int_setting(kanban_cfg: dict, key: str) -> Optional[int]: """Parse an optional ``kanban.`` int cap; None when unset or invalid (< 1 is invalid).""" raw = kanban_cfg.get(key) @@ -78,9 +69,11 @@ def _resolve_auto_decompose_settings(load_config: Callable[[], Any]) -> "tuple[b Fails safe: a config read error returns ``(False, 3)`` rather than re-enabling a feature the user turned off. ``per_tick`` is clamped to ``>= 1``. """ - kcfg = _kanban_cfg(load_config) - if kcfg is None: + try: + cfg = load_config() + except Exception: return False, 3 + kcfg = cfg.get("kanban", {}) if isinstance(cfg, dict) else {} try: per_tick = int(kcfg.get("auto_decompose_per_tick", 3) or 3) except (TypeError, ValueError): diff --git a/gateway/kanban_watchers_notifier.py b/gateway/kanban_watchers_notifier.py index ac50b1ea34..2ae4f2f7fd 100644 --- a/gateway/kanban_watchers_notifier.py +++ b/gateway/kanban_watchers_notifier.py @@ -21,20 +21,13 @@ from gateway.kanban_watchers_common import _list_boards, _to_thread_process_serv TERMINAL_KINDS = ("completed", "blocked", "gave_up", "crashed", "timed_out", "status", "archived", "unblocked", "block_loop_detected", "review_requested", "changes_requested") # Kinds that hand a decision back to the origin, which must take a turn. # status/archived/unblocked are bookkeeping. -_WAKE_KINDS = ( - "completed", "gave_up", "crashed", "timed_out", - "blocked", "review_requested", "changes_requested", - "block_loop_detected", -) +_WAKE_KINDS = ("completed", "gave_up", "crashed", "timed_out", "blocked", "review_requested", "changes_requested", "block_loop_detected") # Consecutive send failures (adapter raised OR reported SendResult(success=False)) # before a sub is dropped as a dead chat. 12 ≈ 60s at the 5s cadence: a transient # API outage must not permanently unsubscribe a live review-gate channel. MAX_SEND_FAILURES = 12 -_LOCAL_PATH_RE = re.compile( - r"(? str: @@ -64,18 +57,15 @@ def _wake_scope_id(adapter: Any, sub: dict) -> Optional[str]: if value: return str(value) resolver = getattr(adapter, "scope_id_for_chat", None) - if callable(resolver): - try: - resolved = resolver(str(sub.get("chat_id") or "")) - except Exception as exc: - # An adapter-side lookup failure yields no scope, never an error. - logger.debug( - "kanban notifier: scope lookup failed for chat %s: %s", sub.get("chat_id"), exc, exc_info=True, - ) - return None - if resolved: - return str(resolved) - return None + if not callable(resolver): + return None + try: + resolved = resolver(str(sub.get("chat_id") or "")) + except Exception as exc: + # An adapter-side lookup failure yields no scope, never an error. + logger.debug("kanban notifier: scope lookup failed for chat %s: %s", sub.get("chat_id"), exc, exc_info=True) + return None + return str(resolved) if resolved else None def _platform_names(mapping: Any) -> set[str]: @@ -352,9 +342,8 @@ class _KanbanNotification: self.title = (task.title if task else sub["task_id"])[:120] self.board_tag = f"[{self.board_slug}] " if self.board_slug else "" # Attribute the ping to the worker that did the work. - who = task.assignee if task and task.assignee else None - self.tag = f"@{who} " if who else "" - self.head = f"{self.board_tag}{self.tag}Kanban {self.task_id}" + tag = f"@{task.assignee} " if task and task.assignee else "" + self.head = f"{self.board_tag}{tag}Kanban {self.task_id}" # The wake self-post path needs the key even when every event was skipped. self.sub_key = (sub["task_id"], sub["platform"], sub["chat_id"], sub.get("thread_id") or "") mode = sub.get("delivery_mode") or "notify" @@ -362,14 +351,11 @@ class _KanbanNotification: self.send_passive = mode != "wake" # Worker handoff carried into the synthetic wake turn so the woken # creator doesn't re-decompose work already on the board. - self.wake_handoff = "" - self.wake_review_detail = "" + self.wake_handoff = self.wake_review_detail = self.session_key = self.synth = "" self.plat: Any = None self.adapter: Any = None self.is_push_adapter = True self.wake_kinds: set = set() - self.session_key = "" - self.synth = "" # -- cursor / subscription ops (blocking, run in a fresh-context thread) -- @@ -400,11 +386,8 @@ class _KanbanNotification: await self.rewind() async def _wake_failed(self, fmt: str, exc: Exception) -> None: - await self.delivery_failed( - fmt, (self.task_id,), - "kanban notifier: dropping subscription %s on %s after %d consecutive wake failures", - exc, True, - ) + drop_fmt = "kanban notifier: dropping subscription %s on %s after %d consecutive wake failures" + await self.delivery_failed(fmt, (self.task_id,), drop_fmt, exc, True) # -- formatting -- @@ -494,10 +477,7 @@ class _KanbanNotification: # (else the event is lost); None / non-SendResult keeps the # "no exception == delivered" contract. if getattr(_send_res, "success", True) is False: - raise RuntimeError( - "adapter send() reported failure: " - f"{getattr(_send_res, 'error', None) or 'unknown error'}" - ) + raise RuntimeError(f"adapter send() reported failure: {getattr(_send_res, 'error', None) or 'unknown error'}") logger.debug( "kanban notifier: delivered %s event for %s to %s/%s on board %s", ev.kind, self.task_id, self.platform_str, sub["chat_id"], self.board_slug, @@ -525,10 +505,8 @@ class _KanbanNotification: # IS the delivery and resolves the failure counter. if not self.is_push_adapter and self.wake_agent: logger.debug( - "kanban notifier: adapter %s has no push " - "channel; skipping text ping for %s, relying " - "on wake self-post instead", - self.platform_str, self.task_id, + "kanban notifier: adapter %s has no push channel; skipping text ping for %s, relying " + "on wake self-post instead", self.platform_str, self.task_id, ) continue if not self.send_passive: @@ -539,10 +517,8 @@ class _KanbanNotification: self.clear_failures() except Exception as exc: await self.delivery_failed( - "kanban notifier: send failed for %s on %s (attempt %d/%d): %s", - (self.task_id, self.platform_str), - "kanban notifier: dropping subscription %s on %s after %d consecutive send failures", - exc, False, + "kanban notifier: send failed for %s on %s (attempt %d/%d): %s", (self.task_id, self.platform_str), + "kanban notifier: dropping subscription %s on %s after %d consecutive send failures", exc, False, ) return False return True @@ -559,10 +535,8 @@ class _KanbanNotification: # default) has no adapter. adapter = self.runner._authorization_adapter(self.plat, self.sub_profile or None) if adapter is None: - logger.debug( - "kanban notifier: adapter %s disconnected before delivery for %s; rewinding claim", - self.platform_str, self.task_id, - ) + logger.debug("kanban notifier: adapter %s disconnected before delivery for %s; rewinding claim", + self.platform_str, self.task_id) await self.rewind() return self.adapter = adapter @@ -572,7 +546,6 @@ class _KanbanNotification: if not await self._send_pings(): return # All text pings delivered (or skipped for non-push / wake-only). - task_terminal = self.task and self.task.status == "archived" self.build_wake_text() wake_kinds, is_push = self.wake_kinds, self.is_push_adapter @@ -603,5 +576,5 @@ class _KanbanNotification: except Exception as _wk_err: logger.warning("kanban notifier: wakeup injection failed for %s: %s", self.task_id, _wk_err, exc_info=True) # Unsubscribe only on archive; ``done`` is reversible. - if task_terminal: + if self.task and self.task.status == "archived": await self.unsub()