The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): compact kanban watcher/notifier call sites and state init
Teknium2da1a3cd2104
diff --git a/gateway/kanban_watchers.py b/gateway/kanban_watchers.pyindex ce081bd0007..f38e6ee4229 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 Noneif _lock_state == "held":self._kanban_dispatcher_lock_handle = _lock_handle # hold for process lifetimelogger.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_cfgasync 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 = nowexcept 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.pyindex e9e9fdb1179..167e24d49b6 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.<key>`` 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[bFails safe: a config read error returns ``(False, 3)`` rather thanre-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.pyindex ac50b1ea342..2ae4f2f7fd3 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_servTERMINAL_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"(?<![\w:/])(?:/(?:Users|home|private|tmp|var|etc|workspace)/[^\s,;]+|"- r"[A-Za-z]:\\[^\s,;]+)"-)+_LOCAL_PATH_RE = re.compile(r"(?<![\w:/])(?:/(?:Users|home|private|tmp|var|etc|workspace)/[^\s,;]+|" r"[A-Za-z]:\\[^\s,;]+)")def _safe_review_reason(value: Any, limit: int = 160) -> 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 Nonedef _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 = Noneself.adapter: Any = Noneself.is_push_adapter = Trueself.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,)continueif 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 Falsereturn 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()returnself.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()