The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): compact slash_access/goals/status mixins; golden-verified output parity
Teknium93dc943151fb
diff --git a/gateway/slash_access.py b/gateway/slash_access.pyindex ac80c6338c5..ed7b096f111 100644--- a/gateway/slash_access.py+++ b/gateway/slash_access.py@@ -1,73 +1,62 @@"""Per-platform slash command access control.-A second axis beside ``allow_from``: of the users allowed to talk to the-gateway, which ones may run which slash commands. Two lists per scope (DM vs-group, mirroring ``allow_from`` / ``group_allow_from``):-- - ``allow_admin_from`` — user IDs that get every registered slash- command (built-in + plugin-registered).- - ``user_allowed_commands`` — command names non-admins may run. Empty /- unset → non-admins get no slash commands- (beyond the ``_ALWAYS_ALLOWED_FOR_USERS`` floor).--Backward compatibility: if ``allow_admin_from`` is not set for a scope,-gating is disabled for that scope and every allowed user can run every-command, so existing installs are unaffected until an operator lists an admin.--The gate is applied at the dispatch site in ``gateway/run.py`` so it covers-both built-in and plugin commands via the live registry. It never affects-plain chat — non-admins still talk to the agent normally.+A second axis beside ``allow_from``: of the users allowed to talk to the gateway, which may run+which slash commands. Two lists per scope (DM vs group, mirroring ``allow_from`` /+``group_allow_from``): ``allow_admin_from`` (user IDs that get every registered command, built-in+and plugin) and ``user_allowed_commands`` (names non-admins may run; empty/unset -> only the+``_ALWAYS_ALLOWED_FOR_USERS`` floor). No ``allow_admin_from`` for a scope => gating disabled there,+so existing installs are unaffected until an operator lists an admin. Applied at the dispatch+site in ``gateway/run.py`` via the live registry; never affects plain chat."""from __future__ import annotationsfrom dataclasses import dataclass-from typing import Any, Callable, FrozenSet, Iterable, Optional, Tuple+from typing import Any, Callable, FrozenSet, Iterable, Optional+# Read-only floor every allowed user keeps under gating, so a non-admin can still discover what+# they can do. ``user_allowed_commands`` only adds to this set, never restricts it.+_ALWAYS_ALLOWED_FOR_USERS: FrozenSet[str] = frozenset({"help", "whoami"})-# Floor of read-only commands every allowed user keeps even under gating, so a-# non-admin can still discover what they can do. ``user_allowed_commands`` only-# adds to this set, never restricts it.-_ALWAYS_ALLOWED_FOR_USERS: FrozenSet[str] = frozenset({- "help",- "whoami",-})+_DM_CHAT_TYPES = frozenset({"dm", "direct", "private", ""})++# scope -> (admin list key, user command list key)+_SCOPE_KEYS = {+ "dm": ("allow_admin_from", "user_allowed_commands"),+ "group": ("group_allow_admin_from", "group_user_allowed_commands"),+}@dataclass(frozen=True)class SlashAccessPolicy:- """Resolved access policy for a single (platform, scope) pair.+ """Resolved access policy for one (platform, scope) pair; scope is ``"dm"`` or ``"group"``."""- ``scope`` is ``"dm"`` for direct messages and ``"group"`` for every other- multi-user context; ``policy_for_source`` maps chat_type → scope.- """-- enabled: bool # gating active for this scope?+ enabled: bool # gating active for this scope?admin_user_ids: FrozenSet[str]user_allowed_commands: FrozenSet[str]def is_admin(self, user_id: Optional[str]) -> bool:- # Gating disabled → everyone is admin so callers can use is_admin/can_run uniformly.+ # Gating disabled -> everyone is admin so callers can use is_admin/can_run uniformly.if not self.enabled:return True- if not user_id:- return False- return str(user_id) in self.admin_user_ids+ return bool(user_id) and str(user_id) in self.admin_user_idsdef can_run(self, user_id: Optional[str], canonical_cmd: str) -> bool:if self.is_admin(user_id):return True- if not canonical_cmd:- return False- return canonical_cmd in _ALWAYS_ALLOWED_FOR_USERS or canonical_cmd in self.user_allowed_commands+ return bool(canonical_cmd) and (+ canonical_cmd in _ALWAYS_ALLOWED_FOR_USERS or canonical_cmd in self.user_allowed_commands+ )-_DM_CHAT_TYPES = frozenset({"dm", "direct", "private", ""})+_DISABLED_POLICY = SlashAccessPolicy(+ enabled=False, admin_user_ids=frozenset(), user_allowed_commands=frozenset()+)-def _coerce_list(raw: Any, normalize: Callable[[str], str]) -> FrozenSet[str]:- """Normalize a YAML-loaded value (None, list/tuple/set, comma string, or scalar)- into a frozenset of stripped, non-empty strings, applying ``normalize`` to each."""+def _coerce_list(raw: Any, normalize: Callable[[str], str] = str) -> FrozenSet[str]:+ """Normalize a YAML-loaded value (None, list/tuple/set, comma string, or scalar) into a+ frozenset of stripped, non-empty strings, applying ``normalize`` to each."""if raw is None:return frozenset()if isinstance(raw, (list, tuple, set, frozenset)):@@ -81,75 +70,37 @@ def _coerce_list(raw: Any, normalize: Callable[[str], str]) -> FrozenSet[str]:def _coerce_id_list(raw: Any) -> FrozenSet[str]:"""Normalize an admin/user id list into a frozenset of strings."""- return _coerce_list(raw, lambda s: s)+ return _coerce_list(raw)def _coerce_command_list(raw: Any) -> FrozenSet[str]:- """Normalize a command allowlist: strip leading slashes (accepts ``/help`` or- ``help``) and lowercase to match how ``resolve_command()`` stores names."""+ """Command allowlist: strip leading slashes (``/help`` or ``help``) and lowercase to match+ how ``resolve_command()`` stores names."""return _coerce_list(raw, lambda s: s.lstrip("/").lower())-def _scope_for_chat_type(chat_type: Optional[str]) -> str:- if chat_type and chat_type.lower() in _DM_CHAT_TYPES:- return "dm"- return "group"---def _platform_extra(platform_config: Any) -> dict:- """Return the ``extra`` dict from a PlatformConfig-like object (or a bare- dict, as some test harnesses pass); {} for None/unknown shapes."""- if platform_config is None:- return {}- extra = getattr(platform_config, "extra", None)- if isinstance(extra, dict):- return extra- if isinstance(platform_config, dict):- return platform_config- return {}---def _keys_for_scope(scope: str) -> Tuple[str, str]:- """Return (admin_key, user_cmd_key) names for a scope."""- if scope == "group":- return ("group_allow_admin_from", "group_user_allowed_commands")- return ("allow_admin_from", "user_allowed_commands")--def policy_from_extra(extra: dict, scope: str) -> SlashAccessPolicy:"""Build a policy from a platform's ``extra`` dict for one scope.- DM scope falls back to ``group_user_allowed_commands`` ONLY for the command- list, and only when DM didn't set its own, so operators list a shared set- once. Admin lists are NOT cross-scope: a DM admin is not a group admin.+ DM scope falls back to ``group_user_allowed_commands`` ONLY for the command list, and only+ when DM didn't set its own. Admin lists are NOT cross-scope: a DM admin is not a group admin."""- admin_key, cmd_key = _keys_for_scope(scope)+ admin_key, cmd_key = _SCOPE_KEYS.get(scope, _SCOPE_KEYS["dm"])admin_ids = _coerce_id_list(extra.get(admin_key))cmds = _coerce_command_list(extra.get(cmd_key))-if scope == "dm" and not cmds:cmds = _coerce_command_list(extra.get("group_user_allowed_commands"))-- return SlashAccessPolicy(- enabled=bool(admin_ids),- admin_user_ids=admin_ids,- user_allowed_commands=cmds,- )+ return SlashAccessPolicy(enabled=bool(admin_ids), admin_user_ids=admin_ids, user_allowed_commands=cmds)def policy_for_source(gateway_config: Any, source: Any) -> SlashAccessPolicy:"""Resolve the slash-gating policy for a SessionSource.- Returns a disabled (allow-everything) policy when gateway_config/source is- None, the platform has no PlatformConfig, or no admin list is set for the- scope. Gates slash commands only, never plain chat.+ Disabled (allow-everything) when gateway_config/source is None, the platform has no+ PlatformConfig, or no admin list is set for the scope. Gates slash commands only, never chat."""if gateway_config is None or source is None:- return SlashAccessPolicy(- enabled=False,- admin_user_ids=frozenset(),- user_allowed_commands=frozenset(),- )+ return _DISABLED_POLICYplatforms = getattr(gateway_config, "platforms", None)platform_config = Noneif platforms is not None:@@ -157,13 +108,13 @@ def policy_for_source(gateway_config: Any, source: Any) -> SlashAccessPolicy:platform_config = platforms.get(source.platform)except Exception:platform_config = None- extra = _platform_extra(platform_config)- scope = _scope_for_chat_type(getattr(source, "chat_type", None))+ # ``extra`` from a PlatformConfig-like object, or a bare dict as some test harnesses pass.+ extra = getattr(platform_config, "extra", None)+ if not isinstance(extra, dict):+ extra = platform_config if isinstance(platform_config, dict) else {}+ chat_type = getattr(source, "chat_type", None)+ scope = "dm" if chat_type and chat_type.lower() in _DM_CHAT_TYPES else "group"return policy_from_extra(extra, scope)-__all__ = [- "SlashAccessPolicy",- "policy_from_extra",- "policy_for_source",-]+__all__ = ["SlashAccessPolicy", "policy_from_extra", "policy_for_source"]diff --git a/gateway/slash_commands_goals.py b/gateway/slash_commands_goals.pyindex 9e6e2fa1100..ba1c7d86ac2 100644--- a/gateway/slash_commands_goals.py+++ b/gateway/slash_commands_goals.py@@ -1,8 +1,7 @@"""Autonomy-loop gateway commands: /goal, /subgoal, /heartbeat, /loop, /refine, /review.Split out of ``gateway/slash_commands.py``; bound onto ``GatewayRunner`` through-``GatewaySlashCommandsMixin``. Origin internals are imported lazily (``from gateway.slash_commands-import ...``) inside the bodies to avoid the import cycle.+``GatewaySlashCommandsMixin``."""from __future__ import annotations@@ -16,19 +15,23 @@ from gateway.platforms.base import MessageEvent, MessageTypelogger = logging.getLogger("gateway.run")+def _plural(n: int, noun: str) -> str:+ return f"{n} {noun}{'s' if n != 1 else ''}"++class GatewayGoalCommandsMixin:"""Autonomy-loop gateway commands: /goal, /subgoal, /heartbeat, /loop, /refine, /review."""- async def _handle_goal_command(self, event: "MessageEvent") -> str:- """Handle /goal for gateway platforms.+ async def _handle_goal_command(self, event: MessageEvent) -> str:+ """Handle /goal: status / show / unwait / clear / pause / resume / wait / gate / <new goal>.- Subcommands: status / pause / resume / clear. Setting a new goal queues the goal text as the- next turn so the agent starts immediately; the post-turn continuation hook takes over after.+ Setting a new goal queues the goal text as the next turn so the agent starts immediately;+ the post-turn continuation hook takes over after."""args = (event.get_command_args() or "").strip()lower = args.lower()- mgr, session_entry = await self._get_goal_manager_for_event(event)+ mgr, _session_entry = await self._get_goal_manager_for_event(event)if mgr is None:return t("gateway.goal.unavailable")@@ -52,40 +55,51 @@ class GatewayGoalCommandsMixin:if lower == "resume":return self._goal_resume(mgr, event)# Verb-prefixed forms take the remainder as their argument.- for verb, handler in (("wait ", self._goal_wait), ("gate ", self._goal_gate)):- if lower == verb.strip() or lower.startswith(verb):- return handler(mgr, args[len(verb) - 1:].strip(), event)+ for verb, handler in (("wait", self._goal_wait), ("gate", self._goal_gate)):+ if lower == verb or lower.startswith(verb + " "):+ return handler(mgr, args[len(verb):].strip(), event)return await self._goal_set(mgr, args, lower, event)def _clear_goal_continuations(self, event: MessageEvent, verb: str) -> None:try:- adapter, _quick_key = self._adapter_and_key_for(event)- if adapter and _quick_key:- self._clear_goal_pending_continuations(_quick_key, adapter)+ adapter, quick_key = self._adapter_and_key_for(event)+ if adapter and quick_key:+ self._clear_goal_pending_continuations(quick_key, adapter)except Exception as exc:logger.debug("goal %s: pending continuation cleanup failed: %s", verb, exc)- def _goal_resume(self, mgr, event: MessageEvent) -> str:- state = mgr.resume()- if state is None:- return t("gateway.goal.no_resume")- # Resume must restart work, not just flip persisted state: enqueue the canonical- # continuation through the adapter FIFO — the same path the post-turn judge uses — so- # the next turn fires as soon as this reply is delivered.- prompt = mgr.next_continuation_prompt()+ def _enqueue_goal_turn(+ self, event: MessageEvent, text: str, *, label: str, kickoff: bool, route=None+ ) -> None:+ """Enqueue *text* as the next turn through the adapter FIFO (the post-turn judge's path).++ A kickoff keeps the triggering message id / channel prompt; a resume continuation carries+ none. *route* is a pre-resolved ``(adapter, quick_key)``; otherwise resolved here.+ Best-effort: enqueue failures are logged, never surfaced.+ """try:- adapter, _quick_key = self._adapter_and_key_for(event)- if prompt and adapter and _quick_key:- cont_event = MessageEvent(- text=prompt,+ adapter, quick_key = route or self._adapter_and_key_for(event)+ if text and adapter and quick_key:+ turn = MessageEvent(+ text=text,message_type=MessageType.TEXT,source=event.source,- message_id=None,- channel_prompt=None,+ message_id=event.message_id if kickoff else None,+ channel_prompt=event.channel_prompt if kickoff else None,)- self._enqueue_fifo(_quick_key, cont_event, adapter)+ self._enqueue_fifo(quick_key, turn, adapter)except Exception as exc:- logger.debug("goal resume: continuation enqueue failed: %s", exc)+ logger.debug("goal %s failed: %s", label, exc)++ def _goal_resume(self, mgr, event: MessageEvent) -> str:+ state = mgr.resume()+ if state is None:+ return t("gateway.goal.no_resume")+ # Resume must restart work, not just flip persisted state: enqueue the canonical+ # continuation so the next turn fires as soon as this reply is delivered.+ self._enqueue_goal_turn(+ event, mgr.next_continuation_prompt(), label="resume: continuation enqueue", kickoff=False+ )return t("gateway.goal.resumed", goal=state.goal)@staticmethod@@ -113,12 +127,12 @@ class GatewayGoalCommandsMixin:return mgr.render_gates()if gate_lower.startswith("add "):# SECURITY: a gate is persisted and later executed with shell=True at every goal turn- # boundary (run_gate), with no approval prompt. Letting an allowed but non-admin gateway- # sender choose that string is authenticated RCE under the Hermes process account — and- # with no admin list configured (the backward-compatible default) every allowed sender- # is treated as unrestricted. Gate ONLY this shell-creating operation behind a real,- # explicitly-configured admin (the same fail-closed check that guards cross-origin- # /resume); list/remove/clear stay open so a non-admin can still recover.+ # boundary (run_gate), with no approval prompt. Letting an allowed but non-admin sender+ # choose that string is authenticated RCE under the Hermes process account — and with+ # no admin list configured (the default) every allowed sender is unrestricted. Gate ONLY+ # this shell-creating operation behind a real, explicitly-configured admin (the same+ # fail-closed check that guards cross-origin /resume); list/remove/clear stay open so+ # a non-admin can still recover.if not self._resume_caller_is_admin(event.source):return ("⛔ /goal gate add requires an explicitly configured "@@ -134,7 +148,7 @@ class GatewayGoalCommandsMixin:f"({gate.max_retries} retries, {gate.timeout_seconds}s timeout). "f"It must pass before the goal can complete.")- if gate_lower.startswith("remove ") or gate_lower.startswith("rm "):+ if gate_lower.startswith(("remove ", "rm ")):try:removed = mgr.remove_gate(int(gate_arg.split(None, 1)[1].strip()))except (RuntimeError, ValueError, IndexError) as exc:@@ -145,23 +159,22 @@ class GatewayGoalCommandsMixin:prev = mgr.clear_gates()except RuntimeError as exc:return f"/goal gate clear: {exc}"- return f"✓ Cleared {prev} gate{'s' if prev != 1 else ''}."+ return f"✓ Cleared {_plural(prev, 'gate')}."return "Usage: /goal gate [list | add <command> | remove <N> | clear]"async def _goal_set(self, mgr, args: str, lower: str, event: MessageEvent) -> str:"""Set a new goal from free text, inline ``field: value`` contract lines, or ``draft <objective>``."""- if lower.startswith("draft"):- # Draft a structured completion contract, then set it. The aux LLM call is sync;- # run it off the event loop.+ drafting = lower.startswith("draft")+ if drafting:objective = args[len("draft"):].strip()if not objective:return "Usage: /goal draft <objective in plain language>"try:from hermes_cli.goals import draft_contract- # _run_in_executor_with_context, not a bare hop: drafting a contract calls the- # auxiliary LLM, whose provider/credential resolution reads the profile secret scope- # — a contextvar that a default-executor hop drops, leaving it unscoped.+ # _run_in_executor_with_context, not a bare hop: drafting calls the auxiliary LLM,+ # whose provider/credential resolution reads the profile secret scope — a+ # contextvar a default-executor hop drops.contract = await self._run_in_executor_with_context(draft_contract, objective)except Exception as exc:logger.debug("goal draft failed: %s", exc)@@ -181,62 +194,48 @@ class GatewayGoalCommandsMixin:except ValueError as exc:return t("gateway.goal.invalid", error=str(exc))- # Queue the goal text as an immediate first turn so the agent starts making progress. The- # post-turn hook takes over after.- adapter, _quick_key = self._adapter_and_key_for(event)- if adapter and _quick_key:- try:- kickoff_event = MessageEvent(- text=state.goal,- message_type=MessageType.TEXT,- source=event.source,- message_id=event.message_id,- channel_prompt=event.channel_prompt,- )- self._enqueue_fifo(_quick_key, kickoff_event, adapter)- except Exception as exc:- logger.debug("goal kickoff enqueue failed: %s", exc)+ # Queue the goal text as an immediate first turn; the post-turn hook takes over after.+ self._enqueue_goal_turn(+ event, state.goal, label="kickoff enqueue", kickoff=True, route=self._adapter_and_key_for(event)+ )base = t("gateway.goal.set", budget=state.max_turns, goal=state.goal)if state.has_contract():return f"{base}\nCompletion contract:\n{state.contract.render_block()}"- if lower.startswith("draft"):- # Drafting was requested but the aux model couldn't produce one.+ if drafting:return f"{base}\n(Couldn't draft a contract — running as a free-form goal.)"return base- async def _handle_heartbeat_command(self, event: "MessageEvent") -> str:- """Handle /heartbeat for gateway platforms (mirror of CLI handler).-- Manages the session's one recurring re-entry prompt. The gateway-wide poller injects due- heartbeats through the adapter FIFO as ordinary user turns, so alternation and caching hold.- """+ async def _handle_heartbeat_command(self, event: MessageEvent) -> str:+ """Handle /heartbeat (mirror of the CLI handler): the session's one recurring re-entry+ prompt. The gateway-wide poller injects due heartbeats through the adapter FIFO as+ ordinary user turns, so alternation and caching hold."""from hermes_cli.heartbeat import parse_interval, format_interval, MIN_INTERVAL_SECONDSargs = (event.get_command_args() or "").strip()lower = args.lower()- mgr, session_entry = await self._get_heartbeat_manager_for_event(event)+ mgr, _session_entry = await self._get_heartbeat_manager_for_event(event)if mgr is None:return "Heartbeats unavailable (no session)."quick_key = self._session_key_for_source(event.source) if event.source else None+ def _watch():+ if quick_key and event.source is not None:+ self._register_heartbeat_watch(quick_key, event.source, mgr.session_id)+if not args or lower == "status":return mgr.status_line()-if lower == "pause":state = mgr.pause()return f"⏸ Heartbeat paused: {state.prompt}" if state else "No heartbeat set."-if lower == "resume":state = mgr.resume()if state is None:return "No heartbeat to resume."- if quick_key and event.source is not None:- self._register_heartbeat_watch(quick_key, event.source, mgr.session_id)+ _watch()return f"▶ Heartbeat resumed (every {format_interval(state.interval_seconds)}): {state.prompt}"-if lower in {"clear", "stop", "off"}:had = mgr.clear()if quick_key:@@ -268,8 +267,7 @@ class GatewayGoalCommandsMixin:state = mgr.set(prompt, interval)except ValueError as exc:return f"Invalid heartbeat: {exc}"- if quick_key and event.source is not None:- self._register_heartbeat_watch(quick_key, event.source, mgr.session_id)+ _watch()return (f"♥ Heartbeat set (every {format_interval(state.interval_seconds)}): {state.prompt}\n""Fires as a normal turn whenever this session is idle and the interval has "@@ -291,14 +289,14 @@ class GatewayGoalCommandsMixin:return quick_key, None, f"Nothing to {verb} yet — send a message first."return quick_key, agent, None- async def _handle_refine_command(self, event: "MessageEvent") -> str:+ async def _handle_refine_command(self, event: MessageEvent) -> str:"""Handle /refine — run the memory/skill review fork on demand.Runs in a daemon thread against a snapshot of the cached AIAgent's conversation; the livesession and prompt cache are untouched. Requires at least one completed turn."""args = (event.get_command_args() or "").strip()- quick_key, agent, error = self._idle_cached_agent_or_error(event, "refine")+ _quick_key, agent, error = self._idle_cached_agent_or_error(event, "refine")if error:return error@@ -306,12 +304,11 @@ class GatewayGoalCommandsMixin:if not snapshot:return "Nothing to refine yet — the conversation is empty."- review_skills = "skill_manage" in getattr(agent, "valid_tool_names", set())try:agent._spawn_background_review(messages_snapshot=snapshot,review_memory=True,- review_skills=review_skills,+ review_skills="skill_manage" in getattr(agent, "valid_tool_names", set()),focus=args or None,)except Exception as exc:@@ -322,7 +319,7 @@ class GatewayGoalCommandsMixin:f"any memory/skill updates will be reported when done.")- async def _handle_review_command(self, event: "MessageEvent") -> str:+ async def _handle_review_command(self, event: MessageEvent) -> str:"""Handle /review — spawn an independent reviewer subagent.The approval session-key contextvar is only bound during agent turns, so bind it explicitly@@ -335,10 +332,7 @@ class GatewayGoalCommandsMixin:snapshot = list(getattr(agent, "_session_messages", None) or [])- from tools.approval import (- reset_current_session_key,- set_current_session_key,- )+ from tools.approval import reset_current_session_key, set_current_session_keydef _dispatch():token = set_current_session_key(quick_key)@@ -350,9 +344,8 @@ class GatewayGoalCommandsMixin:reset_current_session_key(token)try:- # _run_in_executor_with_context, not a bare hop: the reviewer- # subagent is spawned from the worker and inherits its context,- # so a bare hop would run it under the launch home / no secret scope.+ # _run_in_executor_with_context, not a bare hop: the reviewer subagent is spawned from+ # the worker and inherits its context; a bare hop would run it under the launch home.result = await self._run_in_executor_with_context(_dispatch)except ValueError as exc:return str(exc)@@ -363,20 +356,16 @@ class GatewayGoalCommandsMixin:return format_dispatch_note(result, args)- async def _handle_subgoal_command(self, event: "MessageEvent") -> str:- """Handle /subgoal for gateway platforms (mirror of CLI handler).-- Subgoals are extra criteria appended to the active goal mid-loop. They modify state read- at the next turn boundary, so this is safe to invoke while the agent is running.- """+ async def _handle_subgoal_command(self, event: MessageEvent) -> str:+ """Handle /subgoal (mirror of the CLI handler): extra criteria appended to the active goal+ mid-loop. They modify state read at the next turn boundary, so this is safe while the+ agent is running."""args = (event.get_command_args() or "").strip()mgr, _session_entry = await self._get_goal_manager_for_event(event)if mgr is None:return t("gateway.goal.unavailable")if not mgr.has_goal():return "No active goal. Set one with /goal <text>."-- # No args → list current subgoals.if not args:return f"{mgr.status_line()}\n{mgr.render_subgoals()}"@@ -402,9 +391,7 @@ class GatewayGoalCommandsMixin:prev = mgr.clear_subgoals()except RuntimeError as exc:return f"/subgoal clear: {exc}"- if prev:- return f"✓ Cleared {prev} subgoal{'s' if prev != 1 else ''}."- return "No subgoals to clear."+ return f"✓ Cleared {_plural(prev, 'subgoal')}." if prev else "No subgoals to clear."try:text = mgr.add_subgoal(args)@@ -413,45 +400,29 @@ class GatewayGoalCommandsMixin:idx = len(mgr.state.subgoals) if mgr.state else 0return f"✓ Added subgoal {idx}: {text}"- async def _get_loop_manager_for_event(self, event: "MessageEvent"):- """Return a LoopManager bound to the session for this gateway event.+ async def _handle_loop_command(self, event: MessageEvent) -> str:+ """Handle /loop — recurring in-session wakeups, via ``dispatch_loop_command`` (CLI mirror).- Returns ``(manager, session_entry)``, or ``(None, None)`` when the loops module or session- can't be loaded. Mirrors ``_get_goal_manager_for_event``.+ New loops capture the event's routing (platform/chat/thread) so the idle loop-wakeup+ watcher can inject ticks here after a restart."""try:- from hermes_cli.loops import LoopManager+ from hermes_cli.loops import LoopManager, dispatch_loop_command, goal_blocks_loop_tickexcept Exception as exc:- logger.debug("loop manager unavailable: %s", exc)- return None, None- # Warm the SessionDB cache off-loop. A cold cache drops the first- # /loop write while the reply claims the loop was set (same class- # as the /goal false-ack fix).+ logger.debug("loops module unavailable: %s", exc)+ return "Loops unavailable."++ # Warm the SessionDB cache off-loop: a cold cache drops the first /loop write while the+ # reply claims the loop was set (same class as the /goal false-ack fix).await self._warm_goals_session_db("loop manager")try:session_entry = await self.async_session_store.get_or_create_session(event.source)except Exception:- return None, None+ session_entry = Nonesid = getattr(session_entry, "session_id", None) or ""if not sid:- return None, None- return LoopManager(session_id=sid), session_entry-- async def _handle_loop_command(self, event: "MessageEvent") -> str:- """Handle /loop for gateway platforms — recurring in-session wakeups.-- Mirrors the CLI handler via ``dispatch_loop_command``. New loops capture the event's routing- (platform/chat/thread) so the idle loop-wakeup watcher can inject ticks here after a restart.- """- try:- from hermes_cli.loops import dispatch_loop_command, goal_blocks_loop_tick- except Exception as exc:- logger.debug("loops module unavailable: %s", exc)- return "Loops unavailable."-- mgr, _session_entry = await self._get_loop_manager_for_event(event)- if mgr is None:return "Loops unavailable (no active session)."+ mgr = LoopManager(session_id=sid)route: dict = {}try:diff --git a/gateway/slash_commands_status.py b/gateway/slash_commands_status.pyindex 33c42853679..e6320243f73 100644--- a/gateway/slash_commands_status.py+++ b/gateway/slash_commands_status.py@@ -1,8 +1,7 @@"""Read-only gateway introspection commands: /status, /context, /usage, /agents, /insights, /topup.Split out of ``gateway/slash_commands.py``; bound onto ``GatewayRunner`` through-``GatewaySlashCommandsMixin``. Origin internals are imported lazily (``from gateway.slash_commands-import ...``) inside the bodies to avoid the import cycle.+``GatewaySlashCommandsMixin``."""from __future__ import annotations@@ -23,6 +22,8 @@ from gateway.platforms.base import MessageEvent# Log-record parity with gateway/run.py and the origin module.logger = logging.getLogger("gateway.run")+_LIST_CAP = 12 # /agents shows at most this many rows per section+def _clean_str(value: Any) -> str:"""Strip and return a non-empty string value, or empty string."""@@ -37,6 +38,26 @@ def _int_value(value: Any) -> int:return 0+def _n(obj, attr: str):+ return getattr(obj, attr, 0) or 0+++def _fmt(n) -> str:+ return f"{n:,}"+++def _pct(used, total) -> float:+ return min(100, used / total * 100) if total else 0+++def _clip(text: str, limit: int) -> str:+ return text[: limit - 3] + "..." if len(text) > limit else text+++def _chat_msgs(history) -> list[dict]:+ return [m for m in history if m.get("role") in {"user", "assistant"} and m.get("content")]++def _status_model_route(status_agent, persisted_route: dict, session_row: dict, session_entry):"""``(model, provider, context_used, context_total)`` for /status.@@ -46,23 +67,18 @@ def _status_model_route(status_agent, persisted_route: dict, session_row: dict,from gateway.run import _AGENT_PENDING_SENTINEL, _load_gateway_config, _resolve_gateway_modelmodel_name = provider_name = ""- route_resolved = Falsecontext_used = context_total = 0if status_agent is not None and status_agent is not _AGENT_PENDING_SENTINEL:- live_model = _clean_str(getattr(status_agent, "model", ""))- live_provider = _clean_str(getattr(status_agent, "provider", ""))- if live_model and live_provider:- model_name, provider_name, route_resolved = live_model, live_provider, True+ model_name = _clean_str(getattr(status_agent, "model", ""))+ provider_name = _clean_str(getattr(status_agent, "provider", ""))ctx = getattr(status_agent, "context_compressor", None)if ctx is not None:context_used = _int_value(getattr(ctx, "last_prompt_tokens", 0))context_total = _int_value(getattr(ctx, "context_length", 0))-- persisted_model = _clean_str(persisted_route.get("model"))- persisted_provider = _clean_str(persisted_route.get("billing_provider"))- if not route_resolved and persisted_model and persisted_provider:- model_name, provider_name, route_resolved = persisted_model, persisted_provider, True- if not route_resolved:+ if not (model_name and provider_name):+ model_name = _clean_str(persisted_route.get("model"))+ provider_name = _clean_str(persisted_route.get("billing_provider"))+ if not (model_name and provider_name):model_name = _clean_str(session_row.get("model"))provider_name = _clean_str(session_row.get("billing_provider"))context_used = context_used or _int_value(getattr(session_entry, "last_prompt_tokens", 0))@@ -91,53 +107,37 @@ def _context_compressor_lines(agent, ctx, used: int) -> list[str]:"""/context full view: auto-compression threshold/headroom, compression count + last savings,and cumulative throughput (labelled as throughput, NOT context size)."""lines: list[str] = []- threshold = getattr(ctx, "threshold_tokens", 0) or 0- threshold_pct = (getattr(ctx, "threshold_percent", 0) or 0) * 100+ threshold = _n(ctx, "threshold_tokens")+ threshold_pct = f"{_n(ctx, 'threshold_percent') * 100:.0f}"if threshold > 0:if used >= threshold:- lines.append(- t("gateway.context.over_threshold", threshold=f"{threshold:,}", threshold_pct=f"{threshold_pct:.0f}")- )+ lines.append(t("gateway.context.over_threshold", threshold=_fmt(threshold), threshold_pct=threshold_pct))else:- lines.append(- t(- "gateway.context.threshold",- threshold=f"{threshold:,}",- threshold_pct=f"{threshold_pct:.0f}",- to_go=f"{threshold - used:,}",- )- )- compressions = getattr(ctx, "compression_count", 0) or 0+ lines.append(t("gateway.context.threshold", threshold=_fmt(threshold),+ threshold_pct=threshold_pct, to_go=_fmt(threshold - used)))+ compressions = _n(ctx, "compression_count")lines.append(t("gateway.context.compressions", count=compressions))if compressions:savings = getattr(ctx, "_last_compression_savings_pct", None)if savings is not None:lines.append(t("gateway.context.last_savings", savings=f"{savings:.0f}"))-- def _n(attr):- return getattr(agent, attr, 0) or 0-- lines.append("")- lines.append(t("gateway.context.totals_header", calls=_n("session_api_calls")))- lines.append(- t(- "gateway.context.totals_line",- input=f"{_n('session_input_tokens'):,}",- output=f"{_n('session_output_tokens'):,}",- reasoning=f"{_n('session_reasoning_tokens'):,}",- )- )- lines.append(t("gateway.context.total_billed", total=f"{_n('session_total_tokens'):,}"))- lines.append(t("gateway.context.throughput_note"))+ lines += [+ "",+ t("gateway.context.totals_header", calls=_n(agent, "session_api_calls")),+ t("gateway.context.totals_line",+ input=_fmt(_n(agent, "session_input_tokens")),+ output=_fmt(_n(agent, "session_output_tokens")),+ reasoning=_fmt(_n(agent, "session_reasoning_tokens"))),+ t("gateway.context.total_billed", total=_fmt(_n(agent, "session_total_tokens"))),+ t("gateway.context.throughput_note"),+ ]return linesdef _agents_delegation_lines(d: dict) -> list[str]:"""/agents rows for one background delegation. Live per-child activity comes from theregistry's progress sampler: api calls, current tool, seconds since last activity."""- goal = " ".join(str(d.get("goal") or "").split())- if len(goal) > 70:- goal = goal[:67] + "..."+ goal = _clip(" ".join(str(d.get("goal") or "").split()), 70)status = d.get("status", "?")row = f"- `{d.get('delegation_id', '?')}` · {status}"if status == "stalling":@@ -169,26 +169,35 @@ def _usage_agent_stats_lines(agent) -> list[str]:rl_state = agent.get_rate_limit_state()if rl_state and rl_state.has_data:from agent.rate_limit_tracker import format_rate_limit_compact- lines.append(t("gateway.usage.rate_limits", state=format_rate_limit_compact(rl_state)))- lines.append("")- input_tokens = getattr(agent, "session_input_tokens", 0) or 0- output_tokens = getattr(agent, "session_output_tokens", 0) or 0- lines.append(t("gateway.usage.header_session"))- lines.append(t("gateway.usage.label_model", model=agent.model))- lines.append(t("gateway.usage.label_input_tokens", count=f"{input_tokens:,}"))- lines.append(t("gateway.usage.label_output_tokens", count=f"{output_tokens:,}"))- lines.append(t("gateway.usage.label_total", count=f"{agent.session_total_tokens:,}"))- lines.append(t("gateway.usage.label_api_calls", count=agent.session_api_calls))+ lines += [t("gateway.usage.rate_limits", state=format_rate_limit_compact(rl_state)), ""]+ lines += [+ t("gateway.usage.header_session"),+ t("gateway.usage.label_model", model=agent.model),+ t("gateway.usage.label_input_tokens", count=_fmt(_n(agent, "session_input_tokens"))),+ t("gateway.usage.label_output_tokens", count=_fmt(_n(agent, "session_output_tokens"))),+ t("gateway.usage.label_total", count=_fmt(agent.session_total_tokens)),+ t("gateway.usage.label_api_calls", count=agent.session_api_calls),+ ]ctx = agent.context_compressor- _lpt = ctx.last_prompt_tokens if ctx.last_prompt_tokens > 0 else 0- if _lpt:- pct = min(100, _lpt / ctx.context_length * 100) if ctx.context_length else 0- lines.append(t("gateway.usage.label_context", used=f"{_lpt:,}", total=f"{ctx.context_length:,}", pct=f"{pct:.0f}"))+ if ctx.last_prompt_tokens > 0:+ pct = _pct(ctx.last_prompt_tokens, ctx.context_length)+ lines.append(t("gateway.usage.label_context", used=_fmt(ctx.last_prompt_tokens),+ total=_fmt(ctx.context_length), pct=f"{pct:.0f}"))if ctx.compression_count:lines.append(t("gateway.usage.label_compressions", count=ctx.compression_count))return lines+def _capped_rows(items: list, render) -> list[str]:+ """Render up to ``_LIST_CAP`` items via *render* (list of lines each) plus an overflow line."""+ lines: list[str] = []+ for item in items[:_LIST_CAP]:+ lines.extend(render(item))+ if len(items) > _LIST_CAP:+ lines.append(t("gateway.agents.more", count=len(items) - _LIST_CAP))+ return lines++class GatewayStatusCommandsMixin:"""Read-only gateway introspection commands: /status, /context, /usage, /agents, /insights, /topup."""@@ -198,91 +207,62 @@ class GatewayStatusCommandsMixin:source = event.sourcesession_entry = await self.async_session_store.get_or_create_session(source)-- connected_platforms = [p.value for p in self.adapters]-- # Check if there's an active agent. Keep the sentinel distinct: a- # starting/pending run should not be treated as a fully usable agent for- # model/context display, but it still occupies the session slot.session_key = session_entry.session_key+ # Keep the sentinel distinct: a starting/pending run is not a usable agent for+ # model/context display, but it still occupies the session slot.agent = self._running_agents.get(session_key)is_running = agent is not None and agent is not _AGENT_PENDING_SENTINEL- # Count pending /queue follow-ups (slot + overflow).+ # Pending /queue follow-ups (slot + overflow).adapter = self.adapters.get(source.platform) if source else Nonequeue_depth = self._queue_depth(session_key, adapter=adapter)title, session_row, db_total_tokens, persisted_route = await self._status_session_db_facts(session_entry.session_id)- # Resolve model/context for cockpit-style status. Prefer the live or cached agent because it- # carries the actual runtime route and context compressor; fall back to SessionDB metadata +- # last_prompt_tokens so /status stays useful between turns without billing/account calls.+ # Prefer the live or cached agent (actual runtime route + context compressor); fall back+ # to SessionDB metadata + last_prompt_tokens so /status stays useful between turns.status_agent = agent if is_running else self._cached_agent_for(session_key)model_name, provider_name, context_used, context_total = _status_model_route(status_agent, persisted_route, session_row, session_entry)- model_line = ""- if model_name:- if provider_name:- model_line = t("gateway.status.model_provider", model=model_name, provider=provider_name)- else:- model_line = t("gateway.status.model", model=model_name)-- context_line = ""- if context_total:- pct = min(100, round((context_used / context_total) * 100)) if context_total else 0- context_line = t(- "gateway.status.context",- used=f"{context_used:,}",- total=f"{context_total:,}",- pct=f"{pct}",- )- elif context_used:- context_line = t("gateway.status.context_used", used=f"{context_used:,}")-- lines = [- t("gateway.status.header"),- "",- t("gateway.status.session_id", session_id=session_entry.session_id),- ]+ lines = [t("gateway.status.header"), "", t("gateway.status.session_id", session_id=session_entry.session_id)]if title:lines.append(t("gateway.status.title", title=title))- lines.extend([+ lines += [t("gateway.status.created", timestamp=session_entry.created_at.strftime('%Y-%m-%d %H:%M')),t("gateway.status.last_activity", timestamp=session_entry.updated_at.strftime('%Y-%m-%d %H:%M')),- ])- if model_line:- lines.append(model_line)- if context_line:- lines.append(context_line)- lines.extend([- t("gateway.status.tokens", tokens=f"{db_total_tokens:,}"),- t("gateway.status.agent_running", state=t("gateway.status.state_yes") if is_running else t("gateway.status.state_no")),- ])+ ]+ if model_name and provider_name:+ lines.append(t("gateway.status.model_provider", model=model_name, provider=provider_name))+ elif model_name:+ lines.append(t("gateway.status.model", model=model_name))+ if context_total:+ pct = min(100, round((context_used / context_total) * 100))+ lines.append(t("gateway.status.context", used=_fmt(context_used), total=_fmt(context_total), pct=f"{pct}"))+ elif context_used:+ lines.append(t("gateway.status.context_used", used=_fmt(context_used)))+ state = t("gateway.status.state_yes") if is_running else t("gateway.status.state_no")+ lines += [+ t("gateway.status.tokens", tokens=_fmt(db_total_tokens)),+ t("gateway.status.agent_running", state=state),+ ]if queue_depth:lines.append(t("gateway.status.queued", count=queue_depth))if source.platform == Platform.MATRIX:- scope = getattr(self.adapters.get(Platform.MATRIX), "_matrix_session_scope", os.getenv("MATRIX_SESSION_SCOPE", "auto"))- thread = source.thread_id or "none"- lines.extend([+ scope = getattr(self.adapters.get(Platform.MATRIX), "_matrix_session_scope",+ os.getenv("MATRIX_SESSION_SCOPE", "auto"))+ lines += ["",t("gateway.status.matrix_scope_header"),t("gateway.status.matrix_scope_room", room=source.chat_name or source.chat_id),t("gateway.status.matrix_scope_room_id", room_id=source.chat_id),- t("gateway.status.matrix_scope_thread", thread_id=thread),+ t("gateway.status.matrix_scope_thread", thread_id=source.thread_id or "none"),t("gateway.status.matrix_scope_mode", scope=scope),- t(- "gateway.status.matrix_scope_key",- session_key=self._redact_matrix_session_key(session_key),- ),- ])- lines.extend([- "",- t("gateway.status.platforms", platforms=', '.join(connected_platforms)),- ])-+ t("gateway.status.matrix_scope_key", session_key=self._redact_matrix_session_key(session_key)),+ ]+ lines += ["", t("gateway.status.platforms", platforms=', '.join(p.value for p in self.adapters))]return "\n".join(lines)async def _status_session_db_facts(self, session_id: str):@@ -323,54 +303,40 @@ class GatewayStatusCommandsMixin:@staticmethoddef _redact_matrix_session_key(session_key: str) -> str:"""Return a stable Matrix session-key fingerprint for shared room status."""- text = str(session_key or "")- digest = hashlib.sha256(text.encode("utf-8")).hexdigest()[:12]+ digest = hashlib.sha256(str(session_key or "").encode("utf-8")).hexdigest()[:12]return f"sha256:{digest}"async def _handle_context_command(self, event: MessageEvent) -> str:- """Handle /context — the dedicated context-window view.+ """Handle /context — the deep context-window view (/status has the one-line summary).- /status shows a one-line ``used / total`` summary; this command is the deep view: a usage- gauge, auto-compression threshold and headroom, compression count and last savings, and- cumulative throughput — the last clearly labelled as throughput, NOT context size.- Resolution order: running agent, cached agent, SessionStore/SessionDB metadata, and a- transcript estimate only as last resort. ``/context all`` adds per-skill/toolset listings.+ Usage gauge, auto-compression threshold and headroom, compression count and last savings,+ and cumulative throughput (clearly labelled as throughput, NOT context size). Resolution+ order: running agent, cached agent, SessionStore/SessionDB metadata, transcript estimate as+ last resort. ``/context all`` adds per-skill/toolset listings."""source = event.source- session_key = self._session_key_for_source(source)session_entry = await self.async_session_store.get_or_create_session(source)expanded = event.get_command_args().strip().lower() in {"all", "full", "details"}# Running agent first (mid-turn), then cached agent (between turns).- agent = self._resident_agent_for(session_key)- has_agent = bool(agent)-- ctx = getattr(agent, "context_compressor", None) if has_agent else None- used, context_length, model_name = await self._resolve_context_figures(- agent if has_agent else None, ctx, session_entry, source- )+ agent = self._resident_agent_for(self._session_key_for_source(source)) or None+ ctx = getattr(agent, "context_compressor", None) if agent else None+ used, context_length, model_name = await self._resolve_context_figures(agent, ctx, session_entry, source)# Gauge path: real current-context figureif used > 0 and context_length > 0:- pct = min(100.0, used / context_length * 100)- headroom = max(0, context_length - used)- BAR_WIDTH = 24- filled = int(round(pct / 100 * BAR_WIDTH))- bar = "█" * max(0, filled) + "░" * max(0, BAR_WIDTH - filled)-+ pct = _pct(used, context_length)+ bar_width = 24+ filled = int(round(pct / 100 * bar_width))+ bar = "█" * max(0, filled) + "░" * max(0, bar_width - filled)lines = [t("gateway.context.header"),"",t("gateway.context.model", model=model_name or "?"),- t("gateway.context.window", total=f"{context_length:,}"),- t(- "gateway.context.in_use",- used=f"{used:,}",- total=f"{context_length:,}",- pct=f"{pct:.0f}",- ),+ t("gateway.context.window", total=_fmt(context_length)),+ t("gateway.context.in_use", used=_fmt(used), total=_fmt(context_length), pct=f"{pct:.0f}"),t("gateway.context.bar", bar=bar),- t("gateway.context.headroom", headroom=f"{headroom:,}"),+ t("gateway.context.headroom", headroom=_fmt(max(0, context_length - used))),"",]# Full view — compression / throughput need the live agent.@@ -378,18 +344,13 @@ class GatewayStatusCommandsMixin:lines.extend(_context_compressor_lines(agent, ctx, used))else:lines.append(t("gateway.context.detail_after_first"))-# Per-category estimated breakdown (+ optional expanded listings). Same chars/4 engine- # the desktop popover and /usage use; plain text (no glyph grid — monospace isn't- # guaranteed on messaging platforms). Fail-open: rendering errors never break /context.- if has_agent:- breakdown = await asyncio.to_thread(- self._context_breakdown_block, agent, source, expanded- )+ # the desktop popover and /usage use; plain text (monospace isn't guaranteed on+ # messaging platforms). Fail-open: rendering errors never break /context.+ if agent:+ breakdown = await asyncio.to_thread(self._context_breakdown_block, agent, source, expanded)if breakdown:- lines.append("")- lines.extend(breakdown)-+ lines += [""] + breakdownreturn "\n".join(lines)# Last resort: rough estimate from transcript@@ -397,24 +358,13 @@ class GatewayStatusCommandsMixin:if history:from agent.model_metadata import estimate_messages_tokens_rough- msgs = [- m- for m in history- if m.get("role") in {"user", "assistant"} and m.get("content")- ]- approx = estimate_messages_tokens_rough(msgs)- return "\n".join(- [- t("gateway.context.header"),- "",- t(- "gateway.context.estimated",- count=f"{approx:,}",- messages=len(msgs),- ),- t("gateway.context.detail_after_first"),- ]- )+ msgs = _chat_msgs(history)+ return "\n".join([+ t("gateway.context.header"),+ "",+ t("gateway.context.estimated", count=_fmt(estimate_messages_tokens_rough(msgs)), messages=len(msgs)),+ t("gateway.context.detail_after_first"),+ ])return t("gateway.context.no_data")async def _resolve_context_figures(self, agent, ctx, session_entry, source):@@ -424,10 +374,8 @@ class GatewayStatusCommandsMixin:model : agent.model -> SessionDB row modelwindow: compressor.context_length -> effective gateway model route -> model metadata"""- used = context_length = 0- if ctx is not None:- used = getattr(ctx, "last_prompt_tokens", 0) or 0- context_length = getattr(ctx, "context_length", 0) or 0+ used = _n(ctx, "last_prompt_tokens") if ctx is not None else 0+ context_length = _n(ctx, "context_length") if ctx is not None else 0model_name = _clean_str(getattr(agent, "model", "")) if agent is not None else ""if not used:used = _int_value(getattr(session_entry, "last_prompt_tokens", 0))@@ -444,8 +392,7 @@ class GatewayStatusCommandsMixin:def _resolve_nonresident_context():if getattr(getattr(self, "config", None), "multiplex_profiles", False):- profile_home = self._resolve_profile_home_for_source(source)- with _profile_runtime_scope(profile_home):+ with _profile_runtime_scope(self._resolve_profile_home_for_source(source)):return _resolve_gateway_model_context(model_name or None)return _resolve_gateway_model_context(model_name or None)@@ -470,84 +417,30 @@ class GatewayStatusCommandsMixin:now = time.time()current_session_key = self._session_key_for_source(event.source)-- running_agents: dict = getattr(self, "_running_agents", {}) or {}running_started: dict = getattr(self, "_running_agents_ts", {}) or {}agent_rows: list[dict] = []- for session_key, agent in running_agents.items():- started = float(running_started.get(session_key, now))- elapsed = max(0, int(now - started))+ for session_key, agent in (getattr(self, "_running_agents", {}) or {}).items():is_pending = agent is _AGENT_PENDING_SENTINEL- agent_rows.append(- {- "session_key": session_key,- "elapsed": elapsed,- "state": t("gateway.agents.state_starting") if is_pending else t("gateway.agents.state_running"),- "session_id": "" if is_pending else str(getattr(agent, "session_id", "") or ""),- "model": "" if is_pending else str(getattr(agent, "model", "") or ""),- }- )-+ agent_rows.append({+ "session_key": session_key,+ "elapsed": max(0, int(now - float(running_started.get(session_key, now)))),+ "state": t("gateway.agents.state_starting") if is_pending else t("gateway.agents.state_running"),+ "session_id": "" if is_pending else str(getattr(agent, "session_id", "") or ""),+ "model": "" if is_pending else str(getattr(agent, "model", "") or ""),+ })agent_rows.sort(key=lambda row: row["elapsed"], reverse=True)- running_processes: list[dict] = []try:- running_processes = [- p for p in process_registry.list_sessions()- if p.get("status") == "running"- ]+ running_processes = [p for p in process_registry.list_sessions() if p.get("status") == "running"]except Exception:running_processes = []background_tasks = [- t for t in (getattr(self, "_background_tasks", set()) or set())- if hasattr(t, "done") and not t.done()- ]-- lines = [- t("gateway.agents.header"),- "",- t("gateway.agents.active_agents", count=len(agent_rows)),+ task for task in (getattr(self, "_background_tasks", set()) or set())+ if hasattr(task, "done") and not task.done()]- if agent_rows:- for idx, row in enumerate(agent_rows[:12], 1):- current = t("gateway.agents.this_chat") if row["session_key"] == current_session_key else ""- sid = f" · `{row['session_id']}`" if row["session_id"] else ""- model = f" · `{row['model']}`" if row["model"] else ""- lines.append(- f"{idx}. `{row['session_key']}` · {row['state']} · "- f"{format_uptime_short(row['elapsed'])}{sid}{model}{current}"- )- if len(agent_rows) > 12:- lines.append(t("gateway.agents.more", count=len(agent_rows) - 12))-- lines.extend(- [- "",- t("gateway.agents.running_processes", count=len(running_processes)),- ]- )- if running_processes:- for proc in running_processes[:12]:- cmd = " ".join(str(proc.get("command", "")).split())- if len(cmd) > 90:- cmd = cmd[:87] + "..."- lines.append(- f"- `{proc.get('session_id', '?')}` · "- f"{format_uptime_short(int(proc.get('uptime_seconds', 0)))} · `{cmd}`"- )- if len(running_processes) > 12:- lines.append(t("gateway.agents.more", count=len(running_processes) - 12))-- lines.extend(- [- "",- t("gateway.agents.async_jobs", count=len(background_tasks)),- ]- )-# Background (async) delegations — delegate_task(background=true).try:from tools.async_delegation import list_async_delegations@@ -557,22 +450,30 @@ class GatewayStatusCommandsMixin:]except Exception:delegations = []- if delegations:- lines.extend(["", t("gateway.agents.background_delegations", count=len(delegations))])- for d in delegations[:12]:- lines.extend(_agents_delegation_lines(d))- if len(delegations) > 12:- lines.append(t("gateway.agents.more", count=len(delegations) - 12))-- if (- not agent_rows- and not running_processes- and not background_tasks- and not delegations- ):- lines.append("")- lines.append(t("gateway.agents.none"))+ def _agent_row(idx_row):+ idx, row = idx_row+ current = t("gateway.agents.this_chat") if row["session_key"] == current_session_key else ""+ sid = f" · `{row['session_id']}`" if row["session_id"] else ""+ model = f" · `{row['model']}`" if row["model"] else ""+ return [f"{idx}. `{row['session_key']}` · {row['state']} · "+ f"{format_uptime_short(row['elapsed'])}{sid}{model}{current}"]++ def _proc_row(proc):+ cmd = _clip(" ".join(str(proc.get("command", "")).split()), 90)+ return [f"- `{proc.get('session_id', '?')}` · "+ f"{format_uptime_short(int(proc.get('uptime_seconds', 0)))} · `{cmd}`"]++ lines = [t("gateway.agents.header"), "", t("gateway.agents.active_agents", count=len(agent_rows))]+ lines += _capped_rows(list(enumerate(agent_rows, 1)), _agent_row)+ lines += ["", t("gateway.agents.running_processes", count=len(running_processes))]+ lines += _capped_rows(running_processes, _proc_row)+ lines += ["", t("gateway.agents.async_jobs", count=len(background_tasks))]+ if delegations:+ lines += ["", t("gateway.agents.background_delegations", count=len(delegations))]+ lines += _capped_rows(delegations, _agents_delegation_lines)+ if not (agent_rows or running_processes or background_tasks or delegations):+ lines += ["", t("gateway.agents.none")]return "\n".join(lines)async def _handle_topup_command(self, event: MessageEvent) -> str:@@ -591,18 +492,16 @@ class GatewayStatusCommandsMixin:if view is None or not view.logged_in:return t("gateway.credits.not_logged_in")- lines: list[str] = ["💳 **Nous balance**"]- for line in view.balance_lines:- if line.lstrip().startswith("📈"):- continue # drop the helper's header; we print our own- lines.append(line)+ # Drop the helper's 📈 header; we print our own.+ lines = ["💳 **Nous balance**"] + [ln for ln in view.balance_lines if not ln.lstrip().startswith("📈")]if view.identity_line:- lines.append("")- lines.append(view.identity_line)+ lines += ["", view.identity_line]if view.topup_url:- lines.append("")- lines.append(f"Manage billing on the portal: {view.topup_url}")- lines.append("Top up and manage billing in the browser — your balance updates here after.")+ lines += [+ "",+ f"Manage billing on the portal: {view.topup_url}",+ "Top up and manage billing in the browser — your balance updates here after.",+ ]return "\n".join(lines)def _context_breakdown_block(self, agent, source, expanded: bool) -> list[str]:@@ -616,14 +515,12 @@ class GatewayStatusCommandsMixin:payload = self._session_context_breakdown(agent, source)if not (payload.get("categories") or []):return []-details = Noneif expanded:try:details = compute_context_details(agent)except Exception:details = {"skills": [], "toolsets": []}-return render_context_breakdown_lines(payload, details=details, grid=False)except Exception:return []@@ -632,7 +529,6 @@ class GatewayStatusCommandsMixin:"""Per-category context estimate (chars/4) for *agent* over the session transcript (sync)."""from agent.context_breakdown import compute_session_context_breakdown- history: list[dict] = []try:entry = self.session_store.get_or_create_session(source)history = self.session_store.load_transcript(entry.session_id) or []@@ -650,7 +546,6 @@ class GatewayStatusCommandsMixin:categories = payload.get("categories") or []if not categories:return []-total = payload.get("estimated_total") or 0out = [t("gateway.usage.breakdown_header")]for cat in categories:@@ -659,30 +554,21 @@ class GatewayStatusCommandsMixin:continuecat_id = str(cat.get("id") or "")label = t(f"gateway.usage.breakdown_cat_{cat_id}")- # Missing key → t() echoes the key back; fall back to the- # English label the engine already provides.+ # Missing key -> t() echoes the key back; fall back to the engine's English label.if label.endswith(f"breakdown_cat_{cat_id}"):label = str(cat.get("label") or cat_id)pct = round(tokens / total * 100) if total else 0- out.append(- t("gateway.usage.breakdown_line", label=label, count=f"{tokens:,}", pct=pct)- )+ out.append(t("gateway.usage.breakdown_line", label=label, count=_fmt(tokens), pct=pct))return out if len(out) > 1 else []except Exception:return []async def _handle_usage_command(self, event: MessageEvent) -> str:- """Handle /usage command -- show token usage for the current session.-- Checks both _running_agents (mid-turn) and _agent_cache (between turns) so details are- available whenever the user asks.- """+ """Handle /usage -- token usage for the current session (live or cached agent) plus+ account/credit blocks; ``/usage reset [--force]`` redeems a banked Codex reset credit."""source = event.sourcesession_key = self._session_key_for_source(source)- # `/usage reset [--force]` — redeem one banked Codex rate-limit reset- # credit. Parsed before the display path so it never mixes with the- # stats rendering below.raw_args = event.get_command_args().strip()args = [a.lower() for a in raw_args.split()] if raw_args else []wants_reset = bool(args) and args[0] == "reset"@@ -692,9 +578,8 @@ class GatewayStatusCommandsMixin:# Running agent first (mid-turn), then cached agent (between turns).agent = self._resident_agent_for(session_key)- # Resolve provider/base_url/api_key for the account-usage fetch. Prefer the live agent; fall- # back to persisted billing data on the SessionDB row so `/usage` still returns account info- # between turns when no agent is resident.+ # Provider/base_url/api_key for the account-usage fetch: live agent first, else persisted+ # billing data on the SessionDB row so `/usage` still returns account info between turns.provider = getattr(agent, "provider", None) if agent else Nonebase_url = getattr(agent, "base_url", None) if agent else Noneapi_key = getattr(agent, "api_key", None) if agent else None@@ -702,47 +587,37 @@ class GatewayStatusCommandsMixin:provider, base_url = await self._persisted_billing_route(source)if wants_reset:- normalized_provider = str(provider or "").strip().lower()- if normalized_provider != "openai-codex":+ if str(provider or "").strip().lower() != "openai-codex":return t("gateway.usage.reset_wrong_provider")- force = "--force" in args[1:]from agent.account_usage import redeem_codex_reset_creditresult = await asyncio.to_thread(- redeem_codex_reset_credit,- base_url=base_url,- api_key=api_key,- force=force,+ redeem_codex_reset_credit, base_url=base_url, api_key=api_key, force="--force" in args[1:],)return result.message- # Fetch account usage off the event loop so slow provider APIs don't- # block the gateway. Failures are non-fatal -- account_lines stays [].+ # Account usage off the event loop so slow provider APIs don't block the gateway;+ # failures are non-fatal (account_lines stays []).account_lines: list[str] = []- credits_lines: list[str] = []if provider:try:account_snapshot = await asyncio.to_thread(- fetch_account_usage,- provider,- base_url=base_url,- api_key=api_key,+ fetch_account_usage, provider, base_url=base_url, api_key=api_key,)except Exception:account_snapshot = Noneif account_snapshot:account_lines = render_account_usage_lines(account_snapshot, markdown=True)- # ── Nous credits magnitudes + monthly-grant % gauge ─────────────- # Shared with CLI/TUI via nous_credits_lines(); run off the event loop. Gates on "a Nous- # account is logged in" — NOT the inference provider, NOT under `if provider:` — so a Nous- # user inferring elsewhere still sees a balance. No recovery trigger; fail-open.+ # Nous credits + monthly-grant gauge (shared with CLI/TUI). Gates on "a Nous account is+ # logged in" — NOT the inference provider — so a Nous user inferring elsewhere still sees+ # a balance. Fail-open: never break /usage.try:from agent.account_usage import nous_credits_linescredits_lines = await asyncio.to_thread(nous_credits_lines, markdown=True)except Exception:- credits_lines = [] # fail-open: never break /usage+ credits_lines = []def _with_account_blocks(lines: list[str]) -> str:# Each block is preceded by a blank divider only when something precedes it.@@ -755,26 +630,23 @@ class GatewayStatusCommandsMixin:if agent and hasattr(agent, "session_total_tokens") and agent.session_api_calls > 0:lines = _usage_agent_stats_lines(agent)- # Per-category context breakdown (estimated — chars/4 heuristic). Same engine the- # desktop popover uses. The system prompt / tools / skills / memory slices read off the- # live agent; the conversation slice is estimated from the session transcript.+ # Per-category breakdown (chars/4 estimate, same engine as the desktop popover): prompt+ # / tools / skills / memory off the live agent, conversation from the transcript.breakdown_lines = await asyncio.to_thread(self._context_breakdown_lines, agent, source)if breakdown_lines:- lines.append("")- lines.extend(breakdown_lines)+ lines += [""] + breakdown_linesreturn _with_account_blocks(lines)- # No agent at all -- check session history for a rough count+ # No agent at all -- rough count from session historysession_entry = await self.async_session_store.get_or_create_session(source)history = await self.async_session_store.load_transcript(session_entry.session_id)if history:from agent.model_metadata import estimate_messages_tokens_rough- msgs = [m for m in history if m.get("role") in {"user", "assistant"} and m.get("content")]- approx = estimate_messages_tokens_rough(msgs)+ msgs = _chat_msgs(history)return _with_account_blocks([t("gateway.usage.header_session_info"),t("gateway.usage.label_messages", count=len(msgs)),- t("gateway.usage.label_estimated_context", count=f"{approx:,}"),+ t("gateway.usage.label_estimated_context", count=_fmt(estimate_messages_tokens_rough(msgs))),t("gateway.usage.detailed_after_first"),])if account_lines or credits_lines:@@ -796,34 +668,28 @@ class GatewayStatusCommandsMixin:return persisted.get("billing_provider"), persisted.get("billing_base_url")async def _handle_insights_command(self, event: MessageEvent) -> str:- """Handle /insights command -- show usage insights and analytics."""- args = event.get_command_args().strip()-+ """Handle /insights [N | --days N] [--source S] -- usage insights and analytics."""# Normalize Unicode dashes (Telegram/iOS auto-converts -- to em/en dash)- args = re.sub(r'[\u2012\u2013\u2014\u2015](days|source)', r'--\1', args)+ args = re.sub(r'[\u2012\u2013\u2014\u2015](days|source)', r'--\1', event.get_command_args().strip())days = 30source = None-- # Parse simple args: /insights 7 or /insights --days 7- if args:- parts = args.split()- i = 0- while i < len(parts):- if parts[i] == "--days" and i + 1 < len(parts):- try:- days = int(parts[i + 1])- except ValueError:- return t("gateway.insights.invalid_days", value=parts[i + 1])- i += 2- elif parts[i] == "--source" and i + 1 < len(parts):- source = parts[i + 1]- i += 2- elif parts[i].isdigit():+ parts = args.split()+ i = 0+ while i < len(parts):+ if parts[i] == "--days" and i + 1 < len(parts):+ try:+ days = int(parts[i + 1])+ except ValueError:+ return t("gateway.insights.invalid_days", value=parts[i + 1])+ i += 2+ elif parts[i] == "--source" and i + 1 < len(parts):+ source = parts[i + 1]+ i += 2+ else:+ if parts[i].isdigit():days = int(parts[i])- i += 1- else:- i += 1+ i += 1try:from hermes_state import get_shared_session_db@@ -833,15 +699,13 @@ class GatewayStatusCommandsMixin:db = get_shared_session_db()try:engine = InsightsEngine(db)- report = engine.generate(days=days, source=source)- result = engine.format_gateway(report)- return result+ return engine.format_gateway(engine.generate(days=days, source=source))finally:from hermes_state import release_or_closerelease_or_close(db)- # Not a bare hop: ``SessionDB()`` resolves ``get_hermes_home()`` at call time, which is- # a contextvar set by ``_profile_runtime_scope``; a default-executor hop starts with an+ # Not a bare hop: ``SessionDB()`` resolves ``get_hermes_home()`` at call time, a+ # contextvar set by ``_profile_runtime_scope``; a default-executor hop starts with an# EMPTY context and would read the DEFAULT profile's state.db.return await self._run_in_executor_with_context(_run_insights)except Exception as e: