Skip to content

The agent that grows with you

refactor(gateway): run_busy dispatch tables, overflow/steer/confirm helpers; compact adapter+busy docs

Teknium542757aaafd5
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.py
index 410ade239e4..06a56ee5bde 100644
--- a/gateway/run_adapters.py
+++ b/gateway/run_adapters.py
@@ -1,8 +1,8 @@
-"""Adapter connect/disconnect, fatal-error recovery, reconnect watcher and multiplex profile adapter methods for GatewayRunner.
+"""Adapter connect/disconnect, fatal-error recovery, reconnect watcher and multiplex profile
+adapters for GatewayRunner (mixin bound via the MRO).
-Split out of ``gateway/run.py``; bound onto ``GatewayRunner`` via the MRO.
-``gateway.run`` internals are imported lazily inside method bodies (import cycle),
-so ``patch("gateway.run.X")`` keeps intercepting them at call time.
+``gateway.run`` internals are imported lazily inside method bodies (import cycle), so
+``patch("gateway.run.X")`` keeps intercepting them at call time.
"""
from __future__ import annotations
@@ -32,7 +32,7 @@ logger = logging.getLogger("gateway.run")
class GatewayAdapterLifecycleMixin:
- """Adapter connect/disconnect, fatal-error recovery, reconnect watcher and multiplex profile adapter methods for GatewayRunner."""
+ """Adapter lifecycle: connect/teardown, fatal recovery, reconnect watcher, multiplex profiles."""
@staticmethod
async def _wait_or_detach(task: "asyncio.Future", timeout: float) -> bool:
@@ -69,10 +69,10 @@ class GatewayAdapterLifecycleMixin:
return False
async def _safe_adapter_disconnect(self, adapter, platform) -> None:
- """Call adapter.disconnect() defensively, swallowing any error.
+ """Call adapter.disconnect() defensively (bounded, never raises).
- For a failed/raised connect(): partial resources (aiohttp.ClientSession, poll tasks, child
- subprocesses) would otherwise leak. Must tolerate partial-init state and never raise.
+ After a failed connect() partial resources (ClientSession, poll tasks, subprocesses) would
+ otherwise leak; must tolerate partial-init state.
"""
timeout = self._adapter_disconnect_timeout_secs()
try:
@@ -95,13 +95,12 @@ class GatewayAdapterLifecycleMixin:
async def _bounded_adapter_teardown(
self, adapter, platform, *, profile: Optional[str] = None
) -> None:
- """Tear down one adapter on the shutdown path with bounded awaits.
+ """Tear down one adapter on the shutdown path with bounded awaits (never raises).
- ``cancel_background_tasks()`` and ``disconnect()`` can block forever on half-dead network
- state (e.g. a wedged WebSocket thread), stalling shutdown past systemd's ``TimeoutStopSec``;
- the SIGKILL skips ``atexit`` PID-file cleanup and the next start dies with "PID file race
- lost". Each await uses ``HERMES_GATEWAY_ADAPTER_DISCONNECT_TIMEOUT``; on timeout the task is
- cancelled and detached so a cancellation-swallowing adapter can't hang the loop. Never raises.
+ ``cancel_background_tasks()``/``disconnect()`` can block forever on half-dead network state,
+ stalling shutdown past systemd's ``TimeoutStopSec``; the SIGKILL then skips ``atexit``
+ PID-file cleanup and the next start dies with "PID file race lost". Each await is bounded
+ by ``HERMES_GATEWAY_ADAPTER_DISCONNECT_TIMEOUT`` and detached on timeout.
"""
timeout = self._adapter_disconnect_timeout_secs()
suffix = f" (profile: {profile})" if profile else ""
@@ -156,12 +155,11 @@ class GatewayAdapterLifecycleMixin:
return _ADAPTER_DISCONNECT_TIMEOUT_SECS_DEFAULT if override is None else override
def _platform_connect_timeout_secs(self, platform=None, *, initial: bool = False) -> float:
- """Return the per-platform connect timeout used during startup/retry.
+ """Per-platform connect timeout for startup/retry.
- Telegram's full 180s connect budget is deliberately NOT spent at cold start: an unreachable
- Telegram would hold the gateway out of ``running`` for the whole budget. The cold-start wait
- is capped and the platform handed to the reconnect watcher, which retries with the full
- budget and ``is_reconnect=True`` (preserving the offline update queue).
+ Telegram's full 180s budget is NOT spent at cold start (it would hold the gateway out of
+ ``running``): the initial wait is capped and the platform handed to the reconnect watcher,
+ which retries with the full budget and ``is_reconnect=True``.
"""
from gateway.run import (
_PLATFORM_CONNECT_TIMEOUT_SECS_DEFAULT,
@@ -180,12 +178,10 @@ class GatewayAdapterLifecycleMixin:
async def _connect_adapter_with_timeout(
self, adapter, platform, *, is_reconnect: bool = False, initial: bool = False
) -> bool:
- """Connect an adapter without allowing one platform to block others.
+ """Connect an adapter without letting one platform block others.
- ``is_reconnect`` lets adapters distinguish a cold first boot (drop any stale server-side
- queue) from a watcher reconnect (preserve the queue so interim messages aren't dropped).
- ``initial`` selects the capped cold-start budget for platforms whose full connect budget is
- too long to spend before the gateway reaches ``running`` (Telegram's 180s).
+ ``is_reconnect``: cold boot drops any stale server-side queue, a watcher reconnect keeps it.
+ ``initial`` selects the capped cold-start budget (see ``_platform_connect_timeout_secs``).
"""
timeout = self._platform_connect_timeout_secs(platform, initial=initial)
if timeout <= 0:
@@ -196,11 +192,10 @@ class GatewayAdapterLifecycleMixin:
raise TimeoutError(f"{platform.value} connect timed out after {timeout:g}s")
async def _connect_initial_adapter_with_timeout(self, adapter, platform) -> bool:
- """Connect one cold-start adapter with tightly scoped replace intent.
+ """Connect one cold-start adapter with replace intent visible ONLY during this await.
- The capability is visible only while this initial connect is awaited. Reconnects call
- ``_connect_adapter_with_timeout`` directly and adapters also default to deny, so a later
- network recovery can never evict a healthy token holder.
+ Reconnects bypass this (and adapters default to deny), so a later network recovery can
+ never evict a healthy token holder.
"""
adapter._platform_lock_takeover_allowed = bool(
self._platform_lock_takeover_on_start
@@ -213,11 +208,7 @@ class GatewayAdapterLifecycleMixin:
adapter._platform_lock_takeover_allowed = False
async def _handle_reaction_event(self, ctx: Dict[str, Any]) -> None:
- """Fan a normalised platform reaction event out to the HookRegistry.
-
- The adapter-supplied ``event_name`` ("reaction:added"/"reaction:removed") is the hook event,
- matching the ``agent:*`` naming scheme. Errors never block the adapter's event loop.
- """
+ """Fan a normalised reaction event out to the HookRegistry; errors never block the adapter."""
event_name = str(ctx.get("event_name") or "reaction:added")
try:
await self.hooks.emit(event_name, ctx)
@@ -225,13 +216,11 @@ class GatewayAdapterLifecycleMixin:
logger.debug("[Gateway] reaction hook emit failed", exc_info=True)
async def _handle_adapter_fatal_error(self, adapter: BasePlatformAdapter) -> None:
- """React to an adapter failure after startup.
+ """React to an adapter failure after startup (retryable → background reconnect queue).
- Retryable errors (network blip, DNS) queue the platform for background reconnection.
- The notification arrives on the failing adapter's own polling task, and the disconnect in
- the handler can cancel that task mid-flight (disconnect()'s current-task guard misses it
- because _safe_adapter_disconnect closes in a wrapper task), stranding the platform between
- the fatal log and the reconnect queue — so the real work runs in a detached task.
+ Runs in a detached task: the notification arrives on the failing adapter's own polling
+ task, which the handler's disconnect can cancel mid-flight, stranding the platform between
+ the fatal log and the reconnect queue.
"""
tasks = getattr(self, "_fatal_handler_tasks", None)
if tasks is None:
@@ -239,10 +228,8 @@ class GatewayAdapterLifecycleMixin:
task = asyncio.create_task(self._handle_adapter_fatal_error_detached(adapter))
tasks.add(task)
task.add_done_callback(tasks.discard)
- # Await so callers that expect completion still get it — but through shield(): Task.cancel()
- # on the caller also cancels the future it is awaiting (_fut_waiter), so a plain `await
- # task` would tunnel the cancellation straight into the "detached" task. shield() absorbs
- # it: the caller sees CancelledError, the handler runs to completion.
+ # shield(): a plain `await task` would tunnel the caller's cancellation into the detached
+ # task; with shield the caller sees CancelledError and the handler runs to completion.
await asyncio.shield(task)
def _queue_retryable_fatal_platform(self, adapter: BasePlatformAdapter) -> bool:
@@ -257,11 +244,9 @@ class GatewayAdapterLifecycleMixin:
if not platform_config:
return False
if adapter.platform in self._failed_platforms:
- # Nothing to enqueue — but "already queued" is exactly when the watcher may have died,
- # and the enqueue branch below holds the ONLY _ensure_reconnect_watcher_running() call.
- # _spawn_supervised gives up after _MAX_SUPERVISED_RESTARTS; without this backstop a
- # queued platform is a silent permanent outage (nothing retries, and the stranded check
- # treats a queued platform as safe so the process never restarts either).
+ # Already queued is exactly when the watcher may have died (_spawn_supervised gives up
+ # after _MAX_SUPERVISED_RESTARTS); without this backstop the platform is a silent
+ # permanent outage — nothing retries and the stranded check treats "queued" as safe.
self._ensure_reconnect_watcher_running()
return False
self._failed_platforms[adapter.platform] = {
@@ -280,27 +265,22 @@ class GatewayAdapterLifecycleMixin:
"%s queued for background reconnection",
adapter.platform.value,
)
- # Ensure the reconnect watcher is alive — respawn if it died (e.g. restart budget exhausted)
- # so queued platforms are not permanently stranded.
self._ensure_reconnect_watcher_running()
return True
async def _handle_adapter_fatal_error_detached(
self, adapter: BasePlatformAdapter
) -> None:
- """Run the fatal handler; if the platform still ends up stranded (not reconnected, not
- queued, not intentionally disabled), exit the gateway with failure so the service manager
- restarts it instead of leaving a silent partial outage."""
+ """Run the fatal handler; a platform left stranded (not reconnected, not queued, not
+ intentionally disabled) exits the gateway with failure so the service manager restarts it."""
try:
- # Outer hard deadline: even with queue-before-disconnect, a hang anywhere in the impl
- # (status write side effects, detach races, etc.) must not leave this task wedged
- # forever — the stranded check in ``finally`` only runs when we return.
+ # Outer hard deadline: a hang anywhere in the impl must not wedge this task — the
+ # stranded check in ``finally`` only runs when we return.
timeout = self._adapter_disconnect_timeout_secs()
if timeout <= 0:
await self._handle_adapter_fatal_error_impl(adapter)
else:
- # Disconnect budget plus a little queue/status bookkeeping overhead; keep the extra
- # proportional so tests that shrink the disconnect timeout still finish promptly.
+ # Disconnect budget + proportional bookkeeping overhead (tests shrink the timeout).
outer = timeout + min(2.0, max(0.05, timeout))
completed = await self._await_adapter_cleanup_with_timeout(
self._handle_adapter_fatal_error_impl(adapter),
@@ -355,8 +335,8 @@ class GatewayAdapterLifecycleMixin:
)
async def _handle_adapter_fatal_error_impl(self, adapter: BasePlatformAdapter) -> None:
- # Snapshot this platform slot's current owner first: acting on a stale notification would
- # overwrite a healthy platform's runtime status and wrongly re-queue it for reconnection.
+ # Snapshot the slot owner first: a stale notification must not overwrite a healthy
+ # platform's runtime status or re-queue it.
existing = self.adapters.get(adapter.platform)
if existing is not None and existing is not adapter:
logger.debug(
@@ -372,8 +352,7 @@ class GatewayAdapterLifecycleMixin:
adapter.fatal_error_code or "unknown",
adapter.fatal_error_message or "unknown error",
)
- # A relay credential revoked by opt-out is not an error to retry: render a clean "disabled"
- # state, not red "fatal"/"retrying" (non-retryable code, so it also leaves the queue below).
+ # relay_disabled (credential revoked by opt-out) renders "disabled", not red fatal/retrying.
self._update_platform_runtime_status(
adapter.platform.value,
platform_state=(
@@ -385,21 +364,17 @@ class GatewayAdapterLifecycleMixin:
)
if existing is adapter:
- # Claim this adapter for teardown before awaiting disconnect(): a second fatal-error
- # notification for the same adapter (e.g. a concurrent recovery path) would otherwise
- # still see itself as "existing" during the await and disconnect() the same object twice.
+ # Claim for teardown BEFORE awaiting disconnect(), else a concurrent second fatal
+ # notification for the same adapter would disconnect() it twice.
self.adapters.pop(adapter.platform, None)
self.delivery_router.adapters = self.adapters
- # Queue retryable failures BEFORE any disconnect await: a half-dead transport can wedge
- # native close() (or swallow CancelledError), so "disconnect then queue" left platforms
- # permanently deaf in a live process after the network recovered. Populate the queue first so the
- # reconnect watcher always has work; teardown is best-effort after.
+ # Queue BEFORE any disconnect await: a half-dead transport can wedge native close() (or
+ # swallow CancelledError), and "disconnect then queue" left platforms permanently deaf.
self._queue_retryable_fatal_platform(adapter)
if existing is adapter:
- # A half-closed transport can wedge native close() indefinitely; reuse the shutdown-path
- # timeout so this runtime fatal handler always returns to the stay-alive / stranded path.
+ # Bounded by the shutdown-path timeout so this always returns to the stranded check.
await self._safe_adapter_disconnect(adapter, adapter.platform)
if not self.adapters and not self._failed_platforms:
@@ -411,9 +386,8 @@ class GatewayAdapterLifecycleMixin:
logger.error("No connected messaging platforms remain. Shutting down gateway cleanly.")
await self.stop()
elif not self.adapters and self._failed_platforms:
- # All platforms are down and queued for reconnection. Keep the gateway alive so cron jobs
- # still run and the watcher can recover platforms when the problem clears; exiting for a
- # systemd restart would turn a transient outage into a state-killing restart loop.
+ # Everything is down but queued: stay alive (cron still runs, watcher recovers) rather
+ # than turn a transient outage into a state-killing systemd restart loop.
logger.warning(
"No connected messaging platforms remain, but %d platform(s) "
"queued for reconnection — gateway staying alive, watcher will "
@@ -437,10 +411,7 @@ class GatewayAdapterLifecycleMixin:
@staticmethod
def _supervised_backoff(attempt: int) -> float:
- """Delay before the supervisor's next respawn, in seconds (capped exponential).
-
- A method so tests can collapse the schedule instead of sleeping through the real curve.
- """
+ """Capped exponential respawn delay (a method so tests can collapse the schedule)."""
return min(60, 2 ** min(attempt, 6))
def _spawn_supervised(
@@ -449,29 +420,26 @@ class GatewayAdapterLifecycleMixin:
):
"""Launch a long-lived background task with task-level supervision.
- Catches what a per-iteration try/except cannot — exceptions in the OUTER loop or pre-try
- setup — which a bare ``asyncio.create_task`` drops silently. Restarts with capped backoff up
- to ``_MAX_SUPERVISED_RESTARTS`` rapid failures; the counter resets after a run healthy for
- ``_SUPERVISED_HEALTHY_SECS``. Each spawn uses a fresh ``Context``: an inherited
- delegated-child marker would make the Kanban dispatcher reject its own writes.
- ``on_spawn`` fires on EVERY spawn incl. respawns; callers tracking the handle elsewhere
- (e.g. ``_reconnect_watcher_task``) MUST pass it or a respawn leaves a stale handle and a
- SECOND watcher. ``on_give_up(name)`` fires when the restart budget is spent.
+ Catches outer-loop/pre-try exceptions a bare ``create_task`` drops silently. Restarts with
+ capped backoff up to ``_MAX_SUPERVISED_RESTARTS`` rapid failures; the counter resets after
+ ``_SUPERVISED_HEALTHY_SECS`` of healthy running. Each spawn uses a fresh ``Context`` (an
+ inherited delegated-child marker would make the Kanban dispatcher reject its own writes).
+ ``on_spawn`` fires on EVERY spawn incl. respawns — callers tracking the handle elsewhere
+ MUST pass it or a respawn leaves a stale handle and a SECOND watcher. ``on_give_up(name)``
+ fires when the restart budget is spent.
"""
# Spawn timestamp lets ``_done`` tell a rapid crash-loop from a healthy-run-then-crash.
_started = time.monotonic()
- # Deliberately no kwargs to create_task (some test doubles mock a narrow signature); calling
- # it from a fresh Context gives the same isolation as create_task(..., context=Context()).
+ # No create_task kwargs (test doubles mock a narrow signature); running it from a fresh
+ # Context gives the same isolation as create_task(..., context=Context()).
task = Context().run(lambda: asyncio.create_task(coro_factory()))
- # PERMANENT supervised watcher, not transient background WORK: the scale-to-zero idle check
- # must ignore process-lifetime watchers or the gateway counts itself busy forever. Transient
- # tasks added to _background_tasks elsewhere (startup-resume events etc.) stay counted.
+ # Marks a PERMANENT watcher so the scale-to-zero idle check ignores it (else the gateway
+ # counts itself busy forever); transient _background_tasks stay counted.
task._hermes_supervised_watcher = True # type: ignore[attr-defined]
self._retain_background_task(task)
if on_spawn is not None:
- # Record the live handle NOW so an external tracker (e.g. _reconnect_watcher_task)
- # points at the current task, not a dead one left by a prior supervised respawn.
+ # Record the live handle NOW so external trackers don't point at a dead prior task.
try:
on_spawn(task)
except Exception: # pragma: no cover - defensive; a tracker must never kill the spawn
@@ -483,18 +451,14 @@ class GatewayAdapterLifecycleMixin:
return
exc = t.exception()
if exc is None:
- # Clean return == deliberate shutdown or a self-disabling watcher (e.g. a gated
- # no-op returning at once); respawning would busy-spin it — NEVER restart on it.
+ # Clean return = deliberate shutdown or a self-disabling watcher; NEVER respawn.
return
logger.error("Supervised task %s died: %r", name, exc, exc_info=exc)
if restart and self._running:
ran_for = time.monotonic() - _started
- if ran_for >= self._SUPERVISED_HEALTHY_SECS:
- # Ran healthily before crashing — a FRESH failure, not a rapid crash-loop. Reset
- # the counter so a daemon crashing a few times over days is never abandoned.
- effective_attempt = 0
- else:
- effective_attempt = _attempt
+ # A healthy run before the crash is a FRESH failure, not a crash-loop: reset so a
+ # daemon crashing a few times over days is never abandoned.
+ effective_attempt = 0 if ran_for >= self._SUPERVISED_HEALTHY_SECS else _attempt
if effective_attempt >= self._MAX_SUPERVISED_RESTARTS:
logger.error(
"Supervised task %s died %d times in rapid succession "
@@ -523,13 +487,10 @@ class GatewayAdapterLifecycleMixin:
restart=restart,
_attempt=effective_attempt + 1,
on_spawn=on_spawn,
- # Threaded through the recursion like on_spawn: only the LAST respawn's give-up
- # matters, and dropping the callback leaves the exhaustion branch with no owner.
- on_give_up=on_give_up,
+ on_give_up=on_give_up, # only the LAST respawn's give-up matters
)
- # The done callback retains its registration context, so isolate the backoff task
- # too; otherwise a restart could reintroduce the original caller's turn scope.
+ # The done callback runs in its registration context; isolate the backoff task too.
self._retain_background_task(Context().run(lambda: asyncio.create_task(_respawn())))
task.add_done_callback(_done)
@@ -538,19 +499,16 @@ class GatewayAdapterLifecycleMixin:
async def _handoff_watcher(
self, interval: float = 2.0, drain_timeout: float = 30.0,
) -> None:
- """Background task that processes pending CLI→gateway session handoffs.
+ """Process pending CLI→gateway session handoffs.
Polls ``state.db`` for ``handoff_state='pending'`` rows: claim atomically (pending →
- running), re-bind the home channel's session_key to the CLI session_id via
- ``switch_session``, dispatch a synthetic ``MessageEvent``, mark ``completed``/``failed``.
+ running), re-bind the home channel's session_key to the CLI session_id, dispatch a
+ synthetic ``MessageEvent``, mark ``completed``/``failed``.
"""
from gateway.run import _async_profile_runtime_scope, _handoff_watch_scopes, _reclaim_stale
- # Initial delay so the gateway is fully connected to its platforms
- # before we try to dispatch handoffs through them.
- await asyncio.sleep(5)
+ await asyncio.sleep(5) # let platforms connect before dispatching through them
- # Does _process_handoff accept the profile argument? The real one does; test stand-ins bind
- # a one-parameter callable. Probed once, outside the loop.
+ # Does _process_handoff accept the profile argument? Test stand-ins bind a one-arg callable.
try:
import inspect as _inspect
_process_takes_profile = len(
@@ -559,9 +517,8 @@ class GatewayAdapterLifecycleMixin:
except Exception:
_process_takes_profile = False
- # In-flight dispatches keyed by session id. A handoff runs a FULL agent turn plus delivery
- # (far longer than the CLI's 60s wait); inline processing would let one slow handoff block
- # every other profile's poll and time them out. Fire-and-forget; the poll loop only claims.
+ # In-flight dispatches keyed by session id: a handoff runs a FULL agent turn, so inline
+ # processing would let one slow handoff time out every other profile's poll.
inflight: Dict[str, "asyncio.Task"] = {}
async def _dispatch(row, session_id, session_db, profile_name) -> None:
@@ -591,10 +548,10 @@ class GatewayAdapterLifecycleMixin:
async def _tick(profile_name: Optional[str] = None) -> None:
"""One poll of the CURRENTLY-SCOPED session store.
- A closure over ``self``, not a method: unit tests bind ``_handoff_watcher`` onto a
- ``SimpleNamespace`` exposing only ``_session_db``, ``_running`` and ``_process_handoff``;
- any other ``self.<attr>`` would raise, be swallowed by the loop, and silently no-op the
- watcher. ``profile_name`` (``None`` = root) makes delivery use that profile's OWN adapter.
+ A closure, not a method: tests bind ``_handoff_watcher`` onto a ``SimpleNamespace`` with
+ only ``_session_db``/``_running``/``_process_handoff``; any other ``self.<attr>`` would
+ raise, be swallowed, and silently no-op the watcher. ``profile_name`` (None = root)
+ makes delivery use that profile's OWN adapter.
"""
session_db = getattr(self, "_session_db", None)
if session_db is None:
@@ -607,8 +564,6 @@ class GatewayAdapterLifecycleMixin:
if not await session_db.claim_handoff(session_id):
# Another tick or another gateway already claimed it.
continue
- # Positional, not keyword: tests bind a one-arg ``_process_handoff(row)`` stand-in and a
- # keyword call would TypeError into the failure branch (arity probed above).
# INVARIANT (do not weaken): this task is created inside _profile_runtime_scope but
# typically RUNS after it exits; it sees the profile's home/secret scope only because
# those seams are ContextVar-based and ensure_future copies the Context.
@@ -616,8 +571,8 @@ class GatewayAdapterLifecycleMixin:
_dispatch(row, session_id, session_db, profile_name)
)
- # A row still 'running' at startup belongs to a gateway that died mid-dispatch: it can never
- # reach a terminal state, and request_handoff refuses new requests while it sits there.
+ # A row still 'running' at startup belongs to a gateway that died mid-dispatch; it blocks
+ # request_handoff until reclaimed.
def _scope(profile_home):
if profile_home is None:
return contextlib.nullcontext()
@@ -642,8 +597,7 @@ class GatewayAdapterLifecycleMixin:
logger.debug("Handoff watcher tick error: %s", exc, exc_info=True)
await asyncio.sleep(interval)
finally:
- # Drain in-flight dispatches before returning: cancelling would strand their rows in
- # 'running'; a bounded grace period lets an almost-done handoff record its own state.
+ # Bounded drain: cancelling would strand in-flight rows in 'running'.
pending_tasks = [t for t in inflight.values() if not t.done()]
if pending_tasks:
try:
@@ -657,17 +611,15 @@ class GatewayAdapterLifecycleMixin:
def _on_reconnect_watcher_gave_up(self, name: str = "") -> None:
"""Own the reconnect invariant once supervision has abandoned it.
- Invariant: while running and ``_failed_platforms`` is non-empty, a reconnect watcher is live
- or a bounded respawn is scheduled. Event-coupled recovery is not enough: the failed adapter
- is dropped from the live map, so no later event may ever arrive to notice a dead watcher.
- Deliberately NOT done here: requesting a process restart when the slow tier is exhausted —
- a blast-radius policy call; a single loud error names the still-queued platforms instead.
+ Invariant: while running with ``_failed_platforms`` non-empty, a watcher is live or a
+ bounded respawn is scheduled — no later event can arrive to notice a dead watcher because
+ the failed adapter is gone from the live map. Deliberately NOT a process restart when the
+ slow tier is exhausted (blast-radius policy); a loud error names the queued platforms.
"""
if not getattr(self, "_running", False):
return
if not getattr(self, "_failed_platforms", None):
- # No queued work depends on the watcher; leaving it dead is correct — the enqueue path
- # spawns a fresh one the moment a platform is queued again.
+ # Nothing depends on the watcher; the enqueue path spawns a fresh one when needed.
logger.warning(
"Reconnect watcher supervision exhausted with an empty retry "
"queue — leaving it down until a platform is queued."
@@ -693,9 +645,7 @@ class GatewayAdapterLifecycleMixin:
if not getattr(self, "_running", False):
return
if not getattr(self, "_failed_platforms", None):
- # The queue drained while we waited -- something else healed
- # it. Nothing to own any more.
- return
+ return # queue drained while we waited
task = getattr(self, "_reconnect_watcher_task", None)
if task is not None and not task.done():
return # a watcher came back on its own; stand down
@@ -715,11 +665,8 @@ class GatewayAdapterLifecycleMixin:
self._retain_background_task(asyncio.create_task(_slow_respawn()))
def _spawn_reconnect_watcher(self, *, on_give_up=None):
- """Single place that knows how to launch the reconnect watcher.
-
- ``on_spawn`` is load-bearing: without it the supervisor's own respawn leaves
- ``_reconnect_watcher_task`` at a dead handle and ``_ensure_...`` spawns a second watcher.
- """
+ """Launch the reconnect watcher. ``on_spawn`` is load-bearing: without it a supervised
+ respawn leaves ``_reconnect_watcher_task`` dead and ``_ensure_...`` spawns a second one."""
self._reconnect_watcher_task = self._spawn_supervised(
self._platform_reconnect_watcher,
"platform_reconnect_watcher",
@@ -729,11 +676,10 @@ class GatewayAdapterLifecycleMixin:
return self._reconnect_watcher_task
def _ensure_reconnect_watcher_running(self) -> None:
- """Ensure the platform reconnect watcher background task is alive.
+ """Respawn a dead reconnect watcher so queued platforms are not stranded.
- Respawns a dead watcher (exhausted restart budget, unrecoverable exception) so queued
- platforms are not stranded. Called on BOTH _queue_retryable_fatal_platform paths: the
- re-fatal of an already-queued platform is the only case where the budget can be exhausted.
+ Called on BOTH _queue_retryable_fatal_platform paths: the re-fatal of an already-queued
+ platform is the only case where the restart budget can be exhausted.
"""
if not getattr(self, "_running", False):
return
@@ -964,9 +910,9 @@ class GatewayAdapterLifecycleMixin:
async def _cancel_secondary_profile_reconnect_tasks(self) -> None:
"""Cancel profile-scoped reconnects before tearing down their registry.
- A reconnect can be waiting in adapter setup while shutdown begins. It must not republish
- an adapter after the secondary registry is drained. Waiting is bounded by the adapter-
- cleanup budget; a task that overruns is still blocked by the stopped runner state.
+ A reconnect mid-setup must not republish an adapter after the secondary registry drains.
+ Waiting is bounded by the adapter-cleanup budget; an overrunning task is still blocked by
+ the stopped runner state.
"""
pending = self._profile_failed_platforms
if not isinstance(pending, dict):
@@ -992,13 +938,11 @@ class GatewayAdapterLifecycleMixin:
pending.clear()
async def _start_secondary_profile_adapters(self) -> int:
- """Bring up adapters for every non-active profile this gateway serves.
+ """Bring up adapters for every non-active profile; returns the connected count.
- Returns the count of connected secondary adapters; 0 unless ``gateway.multiplex_profiles``.
- Each profile's adapters connect under its HERMES_HOME + secret scope, live in
- ``self._profile_adapters[profile]``, and get a handler stamping ``source.profile``. Same-
- platform credential collisions are refused here — the only point seeing every profile's
- resolved credentials together.
+ 0 unless ``gateway.multiplex_profiles``. Each profile connects under its own HERMES_HOME +
+ secret scope into ``self._profile_adapters[profile]``. Credential/listener collisions are
+ refused here — the only point seeing every profile's resolved credentials together.
"""
from gateway.run import (
MultiplexConfigError,
@@ -1015,8 +959,7 @@ class GatewayAdapterLifecycleMixin:
active = get_active_profile_name() or "default"
connected = 0
- # Resource claim -> owning profile. Credential claims stop two profiles polling the same
- # account; listener claims stop sidecars with distinct credentials binding one endpoint.
+ # Resource claim -> owning profile (credential: same account; listener: same bind+port).
claimed: Dict[tuple, str] = {}
for _plat, _ad in self.adapters.items():
fp = self._adapter_credential_fingerprint(_ad)
@@ -1025,8 +968,7 @@ class GatewayAdapterLifecycleMixin:
listener_claim = self._adapter_listener_claim(_plat, _ad)
if listener_claim is not None:
claimed[listener_claim] = active
- # A retryable primary still owns its credential and listener; reserve both while queued
- # so a secondary cannot take the endpoint before the reconnect watcher retries it.
+ # A queued retryable primary still owns its credential and listener; reserve both.
for retry_info in getattr(self, "_failed_platforms", {}).values():
for claim_name in ("credential_claim", "listener_claim"):
retry_claim = retry_info.get(claim_name)
@@ -1055,17 +997,15 @@ class GatewayAdapterLifecycleMixin:
profile_name, e, exc_info=True,
)
- # Record the authoritative served set in runtime status for `hermes status`. "Served"
- # means eligible for shared routing, HTTP prefixes, cron, and profile runtime scope —
- # intentionally broader than profiles with a connected (or any) secondary adapter.
+ # Record the served set for `hermes status`. "Served" = eligible for shared routing, HTTP
+ # prefixes, cron and runtime scope — broader than "has a connected secondary adapter".
try:
from gateway.status import write_runtime_status
from gateway.pairing import PairingStore
served = [active] + sorted(
name for name, _home in profile_homes if name != active
)
- # Per-profile PairingStores so authz_mixin routes pairing checks to the right whitelist;
- # the active profile's store is at its HERMES_HOME, other served profiles at their own.
+ # Per-profile PairingStores so authz routes pairing checks to the right whitelist.
for name in served:
if name and name not in self.pairing_stores:
self.pairing_stores[name] = (
@@ -1096,9 +1036,8 @@ class GatewayAdapterLifecycleMixin:
from gateway.config import load_gateway_config
from hermes_cli.env_loader import hydrate_profile_secret_sources
- # Hydrate external secret sources (1Password/vault/...) off-loop ONCE, then enter the scope
- # without re-hydrating: the sync hydration is network-bound and would otherwise stall every
- # other profile's heartbeat while this one boots (same class as the reconnect path).
+ # Hydrate external secret sources (1Password/vault/...) off-loop ONCE: the sync hydration is
+ # network-bound and would stall every other profile's heartbeat while this one boots.
await asyncio.to_thread(hydrate_profile_secret_sources, profile_home)
with _profile_runtime_scope(profile_home, hydrate_secrets=False):
@@ -1107,10 +1046,8 @@ class GatewayAdapterLifecycleMixin:
discover_plugins()
- # Register this profile's own declarative shell hooks and outbound webhooks. The
- # registration in start() runs before any profile scope exists and only sees the root
- # profile's config, so without this a secondary profile's `hooks:` block is silently
- # inert (its turns use a plugin manager keyed by resolved home).
+ # Register this profile's shell hooks / outbound webhooks: start() registers before any
+ # profile scope exists, so a secondary profile's `hooks:` block would be silently inert.
try:
from hermes_cli.config import load_config as _load_profile_config
from agent.shell_hooks import register_from_config as _register_shell_hooks
@@ -1217,10 +1154,8 @@ class GatewayAdapterLifecycleMixin:
for platform, platform_config in profile_cfg.platforms.items():
if not platform_config.enabled:
continue
- # A platform enabled in a secondary profile's config.yaml may have no credential in that
- # profile's secret scope — the shared YAML enables it for the default profile only.
- # Building an adapter anyway would fan one inbound message out across every
- # credential-less profile; mirror the primary loop's credential gate and skip.
+ # Enabled in the shared YAML but no credential in THIS profile's scope: building an
+ # adapter anyway would fan one inbound message out across every credential-less profile.
if multiplex and not _platform_has_bot_credential(platform, platform_config):
logger.info(
"[MULTIPLEX] Profile '%s': skipping %s - no bot credential "
@@ -1229,9 +1164,8 @@ class GatewayAdapterLifecycleMixin:
platform.value,
)
continue
- # Relay and WhatsApp are shared process-level ingress in multiplex mode (one connection
- # owned by the active profile, route-stamped source.profile fans out). WhatsApp is one
- # session per phone number; a secondary adapter would only retry-loop and stall startup.
+ # Relay/WhatsApp are shared process-level ingress under multiplex (the active profile's
+ # one connection fans out by route); a secondary would only retry-loop and stall startup.
if multiplex and platform in (Platform.RELAY, Platform.WHATSAPP):
continue
try:
@@ -1308,10 +1242,10 @@ class GatewayAdapterLifecycleMixin:
platform_event_handler=None,
busy_text_mode: Optional[str] = None,
) -> None:
- """Install the runner callbacks every adapter needs, in the shared order.
+ """Install the runner callbacks every adapter needs (defaults = primary handlers).
- Defaults are the primary-adapter handlers; secondary-profile wiring passes its
- profile-scoped variants. ``set_reaction_handler`` is optional on plugin adapters.
+ Secondary-profile wiring passes its profile-scoped variants. ``set_reaction_handler`` is
+ optional on plugin adapters.
"""
adapter.set_message_handler(message_handler or self._primary_message_handler())
adapter.set_fatal_error_handler(fatal_error_handler or self._handle_adapter_fatal_error)
@@ -1343,10 +1277,9 @@ class GatewayAdapterLifecycleMixin:
# Runtime status is process-scoped while message/config work is profile-scoped. Keep both
# dimensions in the key so dashboard/NAS health aggregation sees which secondary failed.
adapter._runtime_status_platform_key = f"{profile_name}:{platform.value}"
- # Declare credential ownership BEFORE any inbound event can be handled: adapter-level
- # session keys (batching, _active_sessions, busy guard) are derived at ingress, before the
- # handler stamps source.profile — without this every secondary bot would key into the
- # default profile's `agent:main:` lane (see BasePlatformAdapter._session_key_profile).
+ # Declare ownership BEFORE any inbound event: adapter-level session keys are derived at
+ # ingress, before the handler stamps source.profile — without this every secondary bot
+ # would key into the default profile's `agent:main:` lane.
_set_owner = getattr(adapter, "set_owner_profile", None)
if callable(_set_owner):
_set_owner(profile_name)
@@ -1394,8 +1327,7 @@ class GatewayAdapterLifecycleMixin:
profile_config = load_gateway_config().platforms.get(platform)
if profile_config is None or not profile_config.enabled:
return
- # Mirrors the startup credential gate: a credential removed from this
- # profile's scope must not rebuild an adapter that would fan out turns.
+ # Startup credential gate mirror: a removed credential must not rebuild.
if not _platform_has_bot_credential(platform, profile_config):
logger.info(
"Secondary %s reconnect skipped: no bot credential "
@@ -1483,17 +1415,14 @@ class GatewayAdapterLifecycleMixin:
) -> None:
"""Queue a cold-start reconnect for a secondary adapter.
- Startup failures happen BEFORE ``self._running`` flips True, so the regular scheduler's
- guard would drop the request. Park a task across startup and hand off to the scheduler once
- live (``_profile_failed_platforms`` dedupes); release it if shutdown begins first.
- Non-retryable failures are dropped as the regular scheduler would.
+ Startup failures happen BEFORE ``self._running`` flips True (the regular scheduler would
+ drop them): park a task and hand off once live; release it if shutdown begins first.
"""
if not getattr(adapter, "fatal_error_retryable", True):
return
if is_global_startup_conflict(getattr(adapter, "fatal_error_code", None)):
- # Same startup contract as the primary path: a live foreign holder of this profile's
- # token/identity is an ownership conflict, not a transient blip. Park it fatal (like
- # ``duplicate_credential``) instead of retry-storming the token every backoff.
+ # A live foreign holder of this profile's token is an ownership conflict, not a blip:
+ # park it fatal (like ``duplicate_credential``) instead of retry-storming.
logger.error(
"[MULTIPLEX] Profile '%s': %s credential is held by another "
"gateway (%s) — parked, not retried. %s",
@@ -1526,8 +1455,7 @@ class GatewayAdapterLifecycleMixin:
if self._running:
_handoff()
return
- # Modest poll: startup completion has no dedicated event, and the reconnect runner's own
- # backoff makes sub-100ms precision irrelevant. Bounded so a wedged startup cannot spin.
+ # Poll (startup completion has no event); bounded so a wedged startup cannot spin.
while not self._running and not self._shutdown_event.is_set():
await asyncio.sleep(0.1)
if self._running and not self._shutdown_event.is_set():
@@ -1571,11 +1499,7 @@ class GatewayAdapterLifecycleMixin:
platform: Platform,
adapter: BasePlatformAdapter,
) -> None:
- """Remove a failed multiplexed adapter without touching the primary slot.
-
- Secondaries live in ``_profile_adapters``, which the primary-only fatal handler ignores;
- without this route a fatal secondary Discord client stayed live forever.
- """
+ """Remove a failed multiplexed adapter (the primary-only fatal handler ignores them)."""
profile_map = getattr(self, "_profile_adapters", {}).get(profile_name)
if not isinstance(profile_map, dict) or profile_map.get(platform) is not adapter:
logger.debug(
@@ -1614,11 +1538,10 @@ class GatewayAdapterLifecycleMixin:
pass
def _make_profile_message_handler(self, profile_name: str):
- """Return a message handler that stamps source.profile then delegates.
+ """Message handler that stamps source.profile, then delegates under the profile scope.
- Auth runs inside ``_handle_message`` *before* the agent-turn scope is installed. For
- secondary profiles under multiplex, wrap the whole handler in ``_profile_runtime_scope``
- so allowlists/tokens from that profile's ``.env`` are visible to ``get_secret`` / authz.
+ Auth runs inside ``_handle_message`` BEFORE the agent-turn scope, so the whole handler
+ must run scoped for the profile's ``.env`` allowlists/tokens to be visible.
"""
from gateway.run import _async_profile_runtime_scope
profile_home = self._profile_home_or_none(profile_name)
@@ -1646,18 +1569,16 @@ class GatewayAdapterLifecycleMixin:
def _make_default_profile_message_handler(self):
"""Scope primary-adapter messages to their routed multiplex profile.
- Resolve the home per event so session lookup and transcript loading use the same profile
- store as the agent run. Authorization stays with the transport profile (a routed profile
- may intentionally have no bot credential/allowlist): the transport home is preserved on the
- live source and never re-checked against the routed scope. Unrouted events keep the default.
+ Authorization stays with the transport profile (a routed profile may have no credential/
+ allowlist): the transport home is preserved on the live source, never re-checked against
+ the routed scope. Unrouted events keep the default.
"""
from gateway.run import _async_profile_runtime_scope, get_hermes_home
default_home = Path(get_hermes_home())
async def _handler(event):
source = event.source
- # In-process only (SessionSource serialization ignores dynamic attrs). The route selects
- # agent/session state, not which bot admitted the message — separate trust domains.
+ # In-process only (serialization ignores dynamic attrs); route ≠ admitting bot.
source._authorization_profile_home = default_home
if (
not getattr(source, "profile", None)
@@ -1668,8 +1589,7 @@ class GatewayAdapterLifecycleMixin:
try:
source.profile = self._profile_name_for_source(source)
except ProfileRouteRejected:
- # NOT write-only: the ``_handle_message`` ingress gate reads this exact marker
- # and drops the message fail-closed (explicit route to an unserved profile).
+ # Read by the ``_handle_message`` ingress gate, which drops fail-closed.
source.profile_route_rejected = True
profile_home = (
@@ -1747,11 +1667,8 @@ class GatewayAdapterLifecycleMixin:
@staticmethod
def _adapter_listener_claim(platform: Platform, adapter: Any) -> Optional[tuple]:
- """Return the exclusive listener resource claimed by an adapter.
-
- Sidecars with different credentials still cannot share a bind+port; expose it as a claim so
- multiplex startup rejects the later adapter before connect()/disconnect() disturb the first.
- """
+ """Exclusive listener claim (Photon sidecar bind+port): distinct credentials still cannot
+ share a port, so the later adapter is rejected before connect() disturbs the first."""
if getattr(platform, "value", None) != "photon":
return None
bind = getattr(adapter, "_sidecar_bind", None)
@@ -1766,37 +1683,21 @@ class GatewayAdapterLifecycleMixin:
@staticmethod
def _adapter_credential_fingerprint(adapter: Any) -> Optional[str]:
- """Return a stable, log-safe fingerprint of an adapter's credential.
-
- Salted hash (never the credential) used to detect two profiles sharing one platform
- credential; None when no credential is discoverable (conflict detection is then skipped).
- """
+ """Salted, log-safe hash of an adapter's credential; None when none is discoverable
+ (conflict detection is then skipped)."""
token = None
for attr in (
- "token",
- "bot_token",
- "_token",
- "api_token",
- "_bot_token",
- # Photon/Spectrum authenticates with project credentials, not a bot token; including
- # its secret stops multiplexed profiles spawning rival sidecars for one account/port.
- "_project_secret",
- # Feishu/Lark authenticates with an app_id/app_secret pair (one WebSocket per app).
- # app_id is stable, log-safe and already the adapter's _app_lock_identity, so including
- # it lets the multiplex guard refuse cloned profiles competing for the same app.
- "_app_id",
- # Same class: Teams (client_id/client_secret) and WeCom
- # (bot_id/secret) authenticate with an app-style id pair too.
- "_client_id",
- "_bot_id",
+ "token", "bot_token", "_token", "api_token", "_bot_token",
+ "_project_secret", # Photon/Spectrum: project credentials, not a bot token
+ "_app_id", # Feishu/Lark app_id — stable, log-safe, already the _app_lock_identity
+ "_client_id", "_bot_id", # Teams / WeCom app-style id pairs
):
val = getattr(adapter, attr, None)
if isinstance(val, str) and val.strip():
token = val.strip()
break
- # Many adapters (e.g. Discord) store the token on their `config` sub-object. Without this
- # lookup they return None, the same-token check is silently skipped, and every profile's
- # adapter polls the same bot token — a per-message race over which one answers.
+ # Many adapters (e.g. Discord) keep the token on `config`; without this the same-token check
+ # is silently skipped and every profile polls the same bot — a race over which one answers.
if not token:
cfg = getattr(adapter, "config", None)
for attr in ("token", "bot_token"):
@@ -1814,11 +1715,8 @@ class GatewayAdapterLifecycleMixin:
platform: Platform,
config: Any,
) -> Optional[BasePlatformAdapter]:
- """Create an adapter and bind it to this gateway runner.
-
- Every lifecycle path (primary/secondary startup, reconnect) uses this method; keep runner
- binding here so adapters can resolve inbound profile routes before handlers or connect().
- """
+ """Create an adapter bound to this runner (every lifecycle path goes through here so
+ adapters can resolve inbound profile routes before handlers or connect())."""
adapter = self._instantiate_adapter(platform, config)
if adapter is not None:
adapter.gateway_runner = self
@@ -1829,10 +1727,7 @@ class GatewayAdapterLifecycleMixin:
platform: Platform,
config: Any,
) -> Optional[BasePlatformAdapter]:
- """Instantiate the appropriate adapter for a platform.
-
- Checks platform_registry (plugin adapters) first, then the built-in table of core platforms.
- """
+ """Instantiate the adapter for a platform: plugin registry first, then built-ins."""
from gateway.run import _instantiate_builtin_adapter
if hasattr(config, "extra") and isinstance(config.extra, dict):
config.extra.setdefault(
@@ -1844,15 +1739,13 @@ class GatewayAdapterLifecycleMixin:
getattr(self.config, "thread_sessions_per_user", False),
)
- # ── Plugin-registered platforms (checked first) ───────────────────
try:
from gateway.platform_registry import platform_registry
if platform_registry.is_registered(platform.value):
adapter = platform_registry.create_adapter(platform.value, config)
if adapter is not None:
return adapter
- # Registered but failed to instantiate — don't silently fall
- # through to built-ins (there are none for plugin platforms).
+ # Registered but failed — never fall through to built-ins.
logger.error(
"Platform '%s' is registered but adapter creation failed "
"(check dependencies and config)",
@@ -1861,8 +1754,6 @@ class GatewayAdapterLifecycleMixin:
return None
except Exception as e:
logger.debug("Platform registry lookup for '%s' failed: %s", platform.value, e)
- # Fall through to built-in adapters below
-
return _instantiate_builtin_adapter(platform, config)
def _make_adapter_auth_check(
@@ -1870,14 +1761,11 @@ class GatewayAdapterLifecycleMixin:
platform: Platform,
profile_name: Optional[str] = None,
) -> Callable[[str, Optional[str], Optional[str]], bool]:
- """Build a platform-bound auth callback for adapter use.
-
- Adapters fetching external context (e.g. Slack ``conversations.replies``) use it via
- ``_is_sender_authorized`` to mark non-allowlisted senders unverified (prompt-injection
- mitigation). Delegates to :meth:`_is_user_authorized` so the full auth chain stays the single
- source of truth. ``profile_name`` binds a secondary adapter to its own secret scope; for the
- shared primary (None) the ``profile_routes`` match is stamped on the source so the routed
- profile's pairing store is consulted while allowlist reads stay under the transport home.
+ """Build a platform-bound auth callback for adapters (prompt-injection mitigation for
+ externally fetched context). Delegates to :meth:`_is_user_authorized` — the single source
+ of truth. ``profile_name`` binds a secondary to its own secret scope; for the shared primary
+ (None) the ``profile_routes`` match is stamped so the routed profile's pairing store is
+ consulted while allowlist reads stay under the transport home.
"""
from gateway.run import get_hermes_home
multiplex = bool(getattr(self.config, "multiplex_profiles", False))
@@ -1904,9 +1792,8 @@ class GatewayAdapterLifecycleMixin:
is_bot=bool(is_bot),
profile=profile_name,
)
- # Same in-process transport provenance ``build_source`` retains, so adapter-level policy
- # reads (config.yaml group_allowed_chats, allow_from) resolve the receiving adapter even
- # once the routed profile is stamped below.
+ # Same transport provenance ``build_source`` retains, so adapter-level policy reads
+ # resolve the receiving adapter even once the routed profile is stamped below.
registry = (
(getattr(self, "_profile_adapters", None) or {}).get(profile_name)
if profile_name
@@ -1923,8 +1810,6 @@ class GatewayAdapterLifecycleMixin:
try:
source.profile = self._profile_name_for_source(source)
except ProfileRouteRejected:
- # Same fail-closed outcome as the ingress gate in
- # ``_handle_message`` for a route to an unserved profile.
- return False
+ return False # fail-closed, like the ``_handle_message`` ingress gate
return self._is_user_authorized_for_source(source)
return check
diff --git a/gateway/run_busy.py b/gateway/run_busy.py
index 53946c7587f..938aff4326e 100644
--- a/gateway/run_busy.py
+++ b/gateway/run_busy.py
@@ -1,8 +1,8 @@
-"""Busy-session queueing, slot claims, slash dispatch tables and destructive-slash confirmation for GatewayRunner.
+"""Busy-session queueing, slot claims, slash dispatch tables and destructive-slash confirmation
+for GatewayRunner (mixin bound via the MRO).
-Split out of ``gateway/run.py``; bound onto ``GatewayRunner`` via the MRO.
-``gateway.run`` internals are imported lazily inside method bodies (import cycle),
-so ``patch("gateway.run.X")`` keeps intercepting them at call time.
+``gateway.run`` internals are imported lazily inside method bodies (import cycle), so
+``patch("gateway.run.X")`` keeps intercepting them at call time.
"""
from __future__ import annotations
@@ -10,6 +10,7 @@ from __future__ import annotations
import logging
from typing import TYPE_CHECKING
import asyncio
+import contextlib
import json
import os
import time
@@ -27,16 +28,20 @@ logger = logging.getLogger("gateway.run")
class GatewayBusySessionMixin:
- """Busy-session queueing, slot claims, slash dispatch tables and destructive-slash confirmation for GatewayRunner."""
+ """Busy-session queueing, slot claims, slash dispatch tables, destructive-slash confirmation."""
def _queue_during_drain_enabled(
self, busy_input_mode: Optional[str] = None
) -> bool:
- # "queue" and "steer" both mean messages must not be lost across restart: queue them for
- # the newly-spawned gateway process to pick up. "interrupt" mode drops them.
+ # "queue"/"steer" mean messages survive a restart (queued for the new process); "interrupt" drops.
mode = busy_input_mode or self._busy_input_mode
return self._restart_requested and mode in {"queue", "steer"}
+ def _overflow_queue(self, session_key: str):
+ """The session's FIFO overflow list, or None when no session state exists yet."""
+ state = self._peek_session_state(session_key)
+ return state.conversation.queued_events if state else None
+
def _enqueue_fifo(self, session_key: str, queued_event: "MessageEvent", adapter: Any) -> None:
"""Append a /queue event to the FIFO chain for a session."""
if adapter is None:
@@ -57,14 +62,12 @@ class GatewayBusySessionMixin:
adapter: Any,
pending_event: Optional["MessageEvent"],
) -> Optional["MessageEvent"]:
- """Promote the next overflow item after the slot was drained.
+ """Promote the next overflow item after the slot drained.
- If pending_event is None, return the overflow head as the new pending_event; if the slot is
- already populated (interrupt follow-up etc.), stage the head there for the NEXT recursion.
- Returns the (possibly updated) pending_event.
+ ``pending_event`` None → the overflow head becomes the pending event; otherwise the head is
+ staged into the slot for the NEXT recursion. Returns the (possibly updated) pending_event.
"""
- _q_state = self._peek_session_state(session_key)
- overflow = _q_state.conversation.queued_events if _q_state else None
+ overflow = self._overflow_queue(session_key)
if not overflow:
return pending_event
next_queued = overflow.pop(0)
@@ -79,8 +82,7 @@ class GatewayBusySessionMixin:
def _queue_depth(self, session_key: str, *, adapter: Any = None) -> int:
"""Total pending /queue items for a session — slot + overflow."""
- _q_state = self._peek_session_state(session_key)
- depth = len(_q_state.conversation.queued_events) if _q_state else 0
+ depth = len(self._overflow_queue(session_key) or ())
if adapter is not None and session_key in getattr(adapter, "_pending_messages", {}):
depth += 1
return depth
@@ -88,30 +90,24 @@ class GatewayBusySessionMixin:
def _rescue_orphaned_overflow(
self, session_key: str, adapter: Any
) -> Optional["MessageEvent"]:
- """Pop the oldest orphaned FIFO overflow event for an idle session.
-
- ``queued_events`` drains only at the post-turn promotion site in ``_run_agent``; if a busy
- window ends without that drain (early recursion exit, exception/interrupt/generation-bump),
- the overflow is silently orphaned. Called when a NEW event arrives for a NON-busy session:
- the oldest orphan is returned to run as THIS turn, the next is staged into the slot so the
- chain continues in arrival order, and the caller enqueues the incoming event behind it. The
- returned event is REMOVED from both stores, else the post-turn dequeue would run it twice.
- Returns ``None`` when there is nothing to rescue (no overflow, slot occupied, or no slot).
+ """Pop the oldest orphaned FIFO overflow event for an idle session (None if nothing to rescue).
+
+ ``queued_events`` drains only at the post-turn promotion site; a busy window ending without
+ it (early exit, exception/interrupt/generation-bump) orphans the overflow. On a NEW event for
+ a NON-busy session the oldest orphan runs as THIS turn, the next is staged into the slot so
+ arrival order holds, and the caller enqueues the incoming event behind it. The returned
+ event is REMOVED from both stores, else the post-turn dequeue would run it twice.
"""
try:
- _q_state = self._peek_session_state(session_key)
- overflow = _q_state.conversation.queued_events if _q_state else None
+ overflow = self._overflow_queue(session_key)
if not overflow:
return None
pending_slot = getattr(adapter, "_pending_messages", None)
if not isinstance(pending_slot, dict) or pending_slot.get(session_key):
- # Slot occupied (busy) or no slot storage — promotion owns
- # this; do not fight it from the idle path.
- return None
+ return None # slot occupied (busy) or no slot storage — promotion owns this
head = overflow.pop(0)
- # Keep the slot occupied for the rest of the chain so the drain promotes in order and
- # any mid-chain arrival routes to overflow instead of jumping the queue (same invariant
- # as the drain's own _promote_queued_event). Only ONE event fits the slot.
+ # Keep the slot occupied so the drain promotes in order and a mid-chain arrival routes
+ # to overflow instead of jumping the queue (same invariant as _promote_queued_event).
if overflow:
pending_slot[session_key] = overflow.pop(0)
logger.warning(
@@ -134,20 +130,12 @@ class GatewayBusySessionMixin:
@staticmethod
def _is_goal_continuation_event(event_or_text: Any) -> bool:
- """Return True for synthetic /goal continuation turns.
-
- Goal continuations are normal queued user-role events, so pause/clear must distinguish
- them from real user /queue messages before removing or suppressing them.
- """
+ """True for synthetic /goal continuation turns (so pause/clear can spare real /queue items)."""
text = getattr(event_or_text, "text", event_or_text) or ""
return str(text).startswith("[Continuing toward your standing goal]\nGoal:")
def _clear_goal_pending_continuations(self, session_key: str, adapter: Any) -> int:
- """Remove queued synthetic /goal continuations for one session.
-
- User /goal pause/clear can race a judge-queued continuation; only synthetic goal
- continuations are removed, normal /queue and user follow-up events are preserved.
- """
+ """Remove queued synthetic /goal continuations for one session; real /queue items are kept."""
removed = 0
pending_slot = getattr(adapter, "_pending_messages", None) if adapter is not None else None
if isinstance(pending_slot, dict):
@@ -156,16 +144,11 @@ class GatewayBusySessionMixin:
pending_slot.pop(session_key, None)
removed += 1
- _q_state = self._peek_session_state(session_key)
- overflow = _q_state.conversation.queued_events if _q_state else []
+ overflow = self._overflow_queue(session_key)
if overflow:
- kept = []
- for queued_event in overflow:
- if self._is_goal_continuation_event(queued_event):
- removed += 1
- else:
- kept.append(queued_event)
- _q_state.conversation.queued_events = kept
+ kept = [e for e in overflow if not self._is_goal_continuation_event(e)]
+ removed += len(overflow) - len(kept)
+ self._peek_session_state(session_key).conversation.queued_events = kept
return removed
def _goal_still_active_for_session(self, session_id: str) -> bool:
@@ -225,9 +208,8 @@ class GatewayBusySessionMixin:
"platform": platform,
"chat_id": getattr(source, "chat_id", "") or "",
"user_id": getattr(source, "user_id", "") or "",
- # Writer identity for re-entrancy: if this process leaks a lease for this session
- # (exception path skipped release), the next turn re-acquires its own entry rather
- # than being fenced out forever — pruning only reclaims entries whose PROCESS died.
+ # Writer identity: a leaked lease from this process is re-acquired by the next
+ # turn rather than fencing it out forever (pruning only reclaims dead PROCESSES).
"live_session_id": str(session_key),
},
)
@@ -237,40 +219,26 @@ class GatewayBusySessionMixin:
@staticmethod
def _agent_has_active_subagents(running_agent: Any) -> bool:
- """Return True when *running_agent* is driving subagents via ``delegate_task``.
-
- ``AIAgent.interrupt()`` cascades through ``_active_children`` and aborts in-flight subagent
- work, so callers demote ``busy_input_mode='interrupt'`` to ``queue`` while this is True;
- explicit ``/stop`` is untouched. Fail-safe: returns False on any attribute/lock error.
- """
+ """True when *running_agent* is driving subagents (callers demote interrupt → queue;
+ ``interrupt()`` would cascade through ``_active_children``). Fail-safe False on any error."""
from gateway.run import _AGENT_PENDING_SENTINEL
if running_agent is None or running_agent is _AGENT_PENDING_SENTINEL:
return False
children = getattr(running_agent, "_active_children", None)
- # AIAgent always initialises this as a concrete list. Reject anything that isn't a real
- # collection — guards against ``MagicMock()._active_children`` auto-creating a truthy stub
- # in tests and triggering the demotion for an agent with no subagents.
- if not isinstance(children, (list, tuple, set)):
- return False
- if not children:
+ # Real collections only — a ``MagicMock()._active_children`` auto-attr must not demote.
+ if not isinstance(children, (list, tuple, set)) or not children:
return False
lock = getattr(running_agent, "_active_children_lock", None)
try:
- if lock is not None:
- with lock:
- return bool(children)
- return bool(children)
+ with lock if lock is not None else contextlib.nullcontext():
+ return bool(children)
except Exception:
return False
async def _session_has_compression_in_flight(self, session_key: str) -> bool:
- """Return True when a compression lock is held for this session's id.
-
- Gateway ``interrupt`` busy mode could start a follow-up against the pre-rotation parent while
- compression is mid-flight, producing orphaned compression siblings; callers demote interrupt
- to queue when True. Both blocking sources (``session_store`` lock + JSON load, SQLite lock
- holder SELECT) run in a worker thread so a large state.db never freezes the event loop.
- """
+ """True when a compression lock is held for this session's id (callers demote interrupt →
+ queue, else a follow-up against the pre-rotation parent orphans compression siblings).
+ Both blocking reads run in a worker thread so a large state.db never freezes the loop."""
session_store = getattr(self, "session_store", None)
if not session_key or session_store is None:
return False
@@ -289,10 +257,8 @@ class GatewayBusySessionMixin:
exc_info=True,
)
return True
- if not session_id:
- return False
session_db = getattr(self, "_session_db", None)
- if session_db is None:
+ if not session_id or session_db is None:
return False
raw_db = getattr(session_db, "_db", session_db)
try:
@@ -328,9 +294,8 @@ class GatewayBusySessionMixin:
adapter = self._adapter_for_source(event.source)
if not adapter:
return
- # Route through the ``/queue`` FIFO infrastructure so each follow-up gets its own turn in
- # arrival order (merge_text=False silently OVERWROTE the single pending slot). Photo bursts
- # still merge into the head slot (album semantics); everything else appends to the tail.
+ # FIFO so each follow-up gets its own turn in arrival order (the single pending slot used to
+ # be silently OVERWRITTEN). Photo bursts still merge into the head slot (album semantics).
pending_slot = getattr(adapter, "_pending_messages", None)
existing = pending_slot.get(session_key) if isinstance(pending_slot, dict) else None
security_metadata_keys = (
@@ -376,13 +341,11 @@ class GatewayBusySessionMixin:
self._enqueue_fifo(session_key, event, adapter)
async def _prepare_busy_steer_text(self, event: MessageEvent) -> str:
- """Return steerable text for a busy follow-up, transcribing voice first.
+ """Steerable text for a busy follow-up, transcribing voice-message media first.
- Successful steer messages bypass the inbound STT queue, so without this a media-only voice
- follow-up has empty text and steer silently degrades to queue mode. Only voice-message media
- (not audio file attachments) is transcribed; on failure keep any caption and let the steer
- fallback handle it. Goes through ``_transcribe_and_echo_pending_voice`` — the single
- out-of-band STT choke point — so STT runs at most once per message (cached on the event).
+ Steer bypasses the inbound STT queue, so a media-only voice follow-up would otherwise
+ silently degrade to queue mode. Uses the single out-of-band STT choke point, so STT runs at
+ most once per message; on failure the caption (if any) is kept.
"""
text = (event.text or "").strip()
if not self._pending_event_audio_paths(event):
@@ -432,43 +395,36 @@ class GatewayBusySessionMixin:
metadata=thread_meta,
)
+ # Bare-word approval replies → (verb, args) for the synthesized slash command.
+ _PLAINTEXT_APPROVAL_WORDS: Dict[str, tuple] = {
+ **{w: ("approve", "") for w in ("approve", "yes", "ok", "okay", "confirm", "y", "👍")},
+ **{w: ("deny", "") for w in ("deny", "no", "reject", "cancel", "n", "👎")},
+ **{w: ("approve", "always") for w in ("always", "approve always", "always approve")},
+ **{w: ("approve", "session") for w in ("session", "approve session", "session approve")},
+ }
+
async def _route_plaintext_approval_while_busy(self, event: MessageEvent, session_key: str) -> bool:
"""Route a bare "yes"/"no" to the approval handlers while a dangerous-command approval blocks.
Returns True when the message was consumed as an approval response.
"""
- # Approval routing: while blocked on a dangerous-command approval, a bare "yes" must reach the
- # approval handler, not be steered/queued/interrupted (else it queues behind a turn that can't
- # start until the approval resolves -> auto-deny deadlock). Slash forms already bypass at the
- # base-adapter guard. Gated on has_blocking_approval so a conversational "yes" never fires a
- # command. Reuse the /approve and /deny handlers; the busy path does not auto-send their return.
+ # A bare "yes" while blocked on a dangerous-command approval must reach the approval handler,
+ # not queue behind a turn that can't start until it resolves (auto-deny deadlock). Gated on
+ # has_blocking_approval so a conversational "yes" never fires a command.
try:
from tools.approval import has_blocking_approval
if event.allow_gateway_control and has_blocking_approval(session_key):
_raw_text = (event.text or "").strip().lower()
- _approve_words = {"approve", "yes", "ok", "okay", "confirm", "y", "👍"}
- _deny_words = {"deny", "no", "reject", "cancel", "n", "👎"}
- _approval_handler = None
- _normalized_args = ""
- if _raw_text in _approve_words:
- _approval_handler = self._handle_approve_command
- elif _raw_text in _deny_words:
- _approval_handler = self._handle_deny_command
- elif _raw_text in {"always", "approve always", "always approve"}:
- _approval_handler = self._handle_approve_command
- _normalized_args = "always"
- elif _raw_text in {"session", "approve session", "session approve"}:
- _approval_handler = self._handle_approve_command
- _normalized_args = "session"
- if _approval_handler is not None:
+ _match = self._PLAINTEXT_APPROVAL_WORDS.get(_raw_text)
+ if _match is not None:
+ _verb, _normalized_args = _match
+ _approval_handler = (
+ self._handle_approve_command if _verb == "approve" else self._handle_deny_command
+ )
# Synthesize "/approve [args]" / "/deny" so the slash handlers parse modifiers via
# event.get_command_args(). Always a literal "/": is_command()/get_command_args()
# don't recognize per-platform display prefixes ("!" on Slack/Matrix).
- _verb = "approve" if _approval_handler is self._handle_approve_command else "deny"
- _synth = f"/{_verb}"
- if _normalized_args:
- _synth = f"{_synth} {_normalized_args}"
- event.text = _synth
+ event.text = f"/{_verb} {_normalized_args}".rstrip()
_reply = await _approval_handler(event)
logger.info(
"Approval response via plain text: session=%s verb=%s args=%r",
@@ -503,10 +459,9 @@ class GatewayBusySessionMixin:
) -> "GatewayRunner._BusySteerOutcome":
"""Apply interrupt->queue demotions, then attempt steer (steer mode) or redirect (interrupt mode)."""
from gateway.run import _AGENT_PENDING_SENTINEL
- # Steer mode injects mid-run via running_agent.steer(); fall back to queue (nothing lost) if the
- # agent isn't running yet (sentinel), lacks steer(), or the payload is empty. interrupt()
- # cascades to ``_active_children`` and aborts delegate_task work, so demote ``interrupt`` to
- # ``queue`` while the parent drives subagents; explicit /stop and /new still force-cancel all.
+ # Steer injects mid-run via running_agent.steer(), falling back to queue (nothing lost) when
+ # the agent isn't running yet, lacks steer(), or the payload is empty. Interrupt is demoted
+ # to queue while subagents run (interrupt() would abort them); /stop and /new still cancel all.
demoted_for_subagents = (
effective_mode == "interrupt"
and self._agent_has_active_subagents(running_agent)
@@ -533,8 +488,7 @@ class GatewayBusySessionMixin:
redirected = False
if effective_mode == "steer":
steer_text = await self._prepare_busy_steer_text(event)
- # Steerable: plain text, OR every attachment is STT-eligible voice media whose transcript
- # was folded into steer_text — else a voice note in steer mode silently degrades to queue.
+ # Steerable: plain text, OR every attachment is voice media folded into steer_text.
_steer_media_urls = getattr(event, "media_urls", None) or []
_steer_all_voice = bool(_steer_media_urls) and (
len(self._pending_event_audio_paths(event)) == len(_steer_media_urls)
@@ -560,7 +514,6 @@ class GatewayBusySessionMixin:
logger.warning("Gateway steer failed for session %s: %s", session_key, exc)
steered = False
if not steered:
- # Fall back to queue (merge into pending messages, no interrupt)
effective_mode = "queue"
elif (
effective_mode == "interrupt"
@@ -606,8 +559,7 @@ class GatewayBusySessionMixin:
pass # don't let interrupt failure block the ack
def _busy_steer_ack_enabled(self, event: MessageEvent, session_key: str) -> bool:
- # Steer mode already injected the text; some mobile chat setups want silent steering (like STT
- # echo suppression) — keep the behavior, drop only the confirmation bubble.
+ # Some mobile chat setups want silent steering — keep the behavior, drop the bubble.
from gateway.run import _load_gateway_config, _platform_config_key
from gateway.display_config import resolve_display_setting
platform_key = _platform_config_key(event.source.platform)
@@ -627,6 +579,10 @@ class GatewayBusySessionMixin:
logger.debug("Busy steer ack suppressed for session %s", session_key)
return steer_ack_enabled
+ _BUSY_DEMOTED_TAIL = (
+ " — your message is queued for when it finishes (use /stop to cancel everything)."
+ )
+
def _compose_busy_ack_message(
self,
event: MessageEvent,
@@ -648,8 +604,7 @@ class GatewayBusySessionMixin:
)
from gateway.display_config import resolve_display_setting
- # Mobile chat defaults keep the ack terse; iteration/tool detail stays in logs and can be opted
- # in per platform via display.platforms.<platform>.busy_ack_detail.
+ # Terse by default; iteration/tool detail opts in via display.platforms.<p>.busy_ack_detail.
status_parts = []
busy_ack_detail_enabled = bool(
resolve_display_setting(
@@ -680,39 +635,21 @@ class GatewayBusySessionMixin:
status_detail = f" ({', '.join(status_parts)})" if status_parts else ""
if is_steer_mode:
- message = (
- f"⏩ Steered into current run{status_detail}. "
- f"Your message arrives after the next tool call."
- )
+ head, tail = "⏩ Steered into current run", ". Your message arrives after the next tool call."
elif is_redirect_mode:
- message = (
- f"↪ Redirected current run{status_detail}. "
- f"I'll adjust using your correction."
- )
+ head, tail = "↪ Redirected current run", ". I'll adjust using your correction."
elif is_queue_mode and demoted_for_subagents:
# Explain the demotion: the follow-up didn't kill the subagent; /stop is the escape hatch.
- message = (
- f"⏳ Subagent working{status_detail} — your message is queued for "
- f"when it finishes (use /stop to cancel everything)."
- )
+ head, tail = "⏳ Subagent working", self._BUSY_DEMOTED_TAIL
elif is_queue_mode and demoted_for_compression:
- message = (
- f"⏳ Compressing context{status_detail} — your message is queued for "
- f"when it finishes (use /stop to cancel everything)."
- )
+ head, tail = "⏳ Compressing context", self._BUSY_DEMOTED_TAIL
elif is_queue_mode:
- message = (
- f"⏳ Queued for the next turn{status_detail}. "
- f"I'll respond once the current task finishes."
- )
+ head, tail = "⏳ Queued for the next turn", ". I'll respond once the current task finishes."
else:
- message = (
- f"⚡ Interrupting current task{status_detail}. "
- f"I'll respond to your message shortly."
- )
+ head, tail = "⚡ Interrupting current task", ". I'll respond to your message shortly."
+ message = f"{head}{status_detail}{tail}"
- # First-touch onboarding: one-time hint about the queue/interrupt knob; the flag is persisted to
- # config.yaml so it never fires again on this install.
+ # One-time onboarding hint about the queue/interrupt knob (flag persisted to config.yaml).
try:
from agent.onboarding import (
BUSY_INPUT_FLAG,
@@ -720,20 +657,14 @@ class GatewayBusySessionMixin:
is_seen,
mark_seen,
)
- _user_cfg = _load_gateway_config()
- if not is_seen(_user_cfg, BUSY_INPUT_FLAG):
- if is_steer_mode:
- _hint_mode = "steer"
- elif is_queue_mode:
- _hint_mode = "queue"
- elif is_redirect_mode:
- _hint_mode = "redirect"
- else:
- _hint_mode = "interrupt"
- message = (
- f"{message}\n\n"
- f"{busy_input_hint_gateway(_hint_mode)}"
+ if not is_seen(_load_gateway_config(), BUSY_INPUT_FLAG):
+ _hint_mode = (
+ "steer" if is_steer_mode
+ else "queue" if is_queue_mode
+ else "redirect" if is_redirect_mode
+ else "interrupt"
)
+ message = f"{message}\n\n{busy_input_hint_gateway(_hint_mode)}"
mark_seen(_hermes_home / "config.yaml", BUSY_INPUT_FLAG)
except Exception as _onb_err:
logger.debug("Failed to apply busy-input onboarding hint: %s", _onb_err)
@@ -753,9 +684,8 @@ class GatewayBusySessionMixin:
logger.debug("Failed to send busy-ack: %s", e)
async def _handle_active_session_busy_message(self, event: MessageEvent, session_key: str) -> bool:
- # Authorization gate: the cold path (_handle_message) checks _is_user_authorized before
- # creating a session; the busy path must enforce the same check, else unauthorized users in
- # shared threads (Slack/Telegram/Discord) inject messages into a session they don't own.
+ # Same authorization gate as the cold path, else unauthorized users in shared threads
+ # inject messages into a session they don't own.
from gateway.run import _AGENT_PENDING_SENTINEL
if not self._is_user_authorized(event.source):
logger.warning(
@@ -783,15 +713,14 @@ class GatewayBusySessionMixin:
if not adapter:
return False # let default path handle it
- # Internal synthetic events (async-delegation / background-process completions) must never
- # interrupt/steer: treated as user TEXT while busy, interrupt mode would abort the active turn;
- # a completion surfaces as a NEW turn only when idle. Plugin events carry untrusted payload
- # text, so queue them through the gateway FIFO (security metadata kept apart).
- if getattr(event, "internal", False) and not event.allow_gateway_control:
+ # Internal synthetic events (delegation / background completions) must never interrupt or
+ # steer; they surface as a NEW turn when idle. Plugin events carry untrusted payload text, so
+ # queue them through the FIFO (security metadata kept apart).
+ if getattr(event, "internal", False):
+ if event.allow_gateway_control:
+ return False
self._queue_or_replace_pending_event(session_key, event)
return True
- if getattr(event, "internal", False):
- return False
_busy_state = self._peek_session_state(session_key)
running_agent = _busy_state.turn.agent if _busy_state else None
@@ -805,24 +734,17 @@ class GatewayBusySessionMixin:
return False
_steer = await self._resolve_busy_steer_or_redirect(event, session_key, effective_mode, running_agent)
- effective_mode = _steer.effective_mode
- demoted_for_subagents = _steer.demoted_for_subagents
- demoted_for_compression = _steer.demoted_for_compression
- steered = _steer.steered
- redirected = _steer.redirected
-
- # Queue as the next turn after the current run ends. Skip after a successful steer — the text
- # is already in the run and must NOT replay. Use the FIFO helper, not raw
- # merge_pending_message_event (merge_text=True newline-joins consecutive TEXT follow-ups into
- # ONE turn); FIFO gives each text its own turn while keeping photo-burst / album merge for media.
- if not steered and not redirected:
+ effective_mode, redirected = _steer.effective_mode, _steer.redirected
+
+ # Queue as the next turn — skipped after a successful steer/redirect (the text is already in
+ # the run and must NOT replay). FIFO gives each text its own turn (raw merge would join them).
+ if not _steer.steered and not redirected:
self._queue_or_replace_pending_event(session_key, event)
is_queue_mode = effective_mode == "queue"
is_steer_mode = effective_mode == "steer"
is_redirect_mode = effective_mode == "interrupt" and redirected
- # Interrupt mode: abort in-flight tool calls; the agent loop exits at its next check point.
if (
effective_mode == "interrupt"
and not redirected
@@ -831,15 +753,14 @@ class GatewayBusySessionMixin:
):
await self._interrupt_running_agent_for_busy_event(event, adapter, running_agent)
- # Disabled ack: skip sending, still process input. Checked before debounce so we never stamp a
- # "last ack" timestamp for an ack that was not delivered.
+ # Disabled ack: still process input. Checked before debounce so an undelivered ack never
+ # stamps the "last ack" timestamp.
busy_ack_enabled = os.environ.get("HERMES_GATEWAY_BUSY_ACK_ENABLED", "true").lower() == "true"
if not busy_ack_enabled:
logger.debug("Busy ack suppressed for session %s", session_key)
return True # input still processed, just no ack sent
- # Debounce before the config-heavy display lookup: rapid follow-ups are still processed but
- # shouldn't cost a config read just to learn no ack will be sent.
+ # Debounce before the config-heavy display lookup.
_BUSY_ACK_COOLDOWN = 30
now = time.time()
last_ack = _busy_state.turn.busy_ack_ts if _busy_state else 0
@@ -859,8 +780,8 @@ class GatewayBusySessionMixin:
is_steer_mode=is_steer_mode,
is_queue_mode=is_queue_mode,
is_redirect_mode=is_redirect_mode,
- demoted_for_subagents=demoted_for_subagents,
- demoted_for_compression=demoted_for_compression,
+ demoted_for_subagents=_steer.demoted_for_subagents,
+ demoted_for_compression=_steer.demoted_for_compression,
)
await self._send_busy_ack_reply(event, adapter, message)
return True
@@ -903,9 +824,7 @@ class GatewayBusySessionMixin:
logger.debug("%s ack send failed", label, exc_info=True)
def _gateway_idle_command_handlers(self):
- """Slash handlers dispatched only when no agent is running for the session (idle path).
-
- Busy dispatch keeps its own explicit allowlist (``_dispatch_busy_slash_command``)."""
+ """Slash handlers dispatched only on the idle path (busy dispatch has its own allowlist)."""
return {
"topic": self._handle_topic_command,
"whoami": self._handle_whoami_command,
@@ -944,33 +863,34 @@ class GatewayBusySessionMixin:
"voice": self._handle_voice_command,
}
+ # busy_handler key (hermes_cli/commands.py CommandDef) → mid-run variant method name.
+ _BUSY_SPECIAL_HANDLERS: Dict[str, str] = {
+ "start": "_busy_start_command",
+ "stop": "_busy_stop_command",
+ "new": "_busy_new_command",
+ "queue": "_busy_queue_command",
+ "steer": "_busy_steer_command",
+ "egress": "_busy_egress_command",
+ "goal": "_busy_goal_command",
+ "loop": "_busy_loop_command",
+ }
+
async def _dispatch_busy_slash_command(
self, event: MessageEvent, cmd_def, quick_key: str, source,
):
"""Dispatch a recognized slash command while an agent is running.
- Order: ``busy_handler`` (special mid-run variant) → ``busy_policy == "dispatch"`` (normal
- handler) → catch-all busy-reject text. Rejecting beats falling through to interrupt +
- discard: Discord-registered slash commands would interrupt the agent AND be discarded by
- the slash-command safety net, producing a zero-char response.
+ Order: ``busy_handler`` (mid-run variant) → ``busy_policy == "dispatch"`` (normal handler)
+ → catch-all reject text. Rejecting beats interrupt + discard (a zero-char response).
"""
name = cmd_def.name
policy = getattr(cmd_def, "busy_policy", "reject")
handler_key = getattr(cmd_def, "busy_handler", None)
if handler_key:
- special = {
- "start": self._busy_start_command,
- "stop": self._busy_stop_command,
- "new": self._busy_new_command,
- "queue": self._busy_queue_command,
- "steer": self._busy_steer_command,
- "egress": self._busy_egress_command,
- "goal": self._busy_goal_command,
- "loop": self._busy_loop_command,
- }.get(handler_key)
+ special = self._BUSY_SPECIAL_HANDLERS.get(handler_key)
if special is not None:
- return await special(event, quick_key, source)
+ return await getattr(self, special)(event, quick_key, source)
reject_text = self._BUSY_REJECT_TEXT.get(handler_key)
if reject_text is not None:
return reject_text
@@ -984,19 +904,14 @@ class GatewayBusySessionMixin:
"falling back to busy-reject", policy, name,
)
- # Catch-all: any other recognized slash command hit the running-agent guard — reject
- # gracefully rather than falling through to interrupt + discard.
return (
f"⏳ Agent is running — `/{name}` can't run "
f"mid-turn. Wait for the current response or `/stop` first."
)
async def _handle_pause_command(self, event: MessageEvent):
- """`/pause [reason]` engages the global emergency stop; `/pause off` (resume/stop) lifts it.
-
- In-band resume path for messaging-only operators — the estop gate lets recognized slash
- commands through while paused so a user without host-shell access is never locked out.
- """
+ """`/pause [reason]` engages the global emergency stop; `/pause off` lifts it (the estop gate
+ lets slash commands through while paused so messaging-only operators are never locked out)."""
from agent import estop
args = (event.get_command_args() or "").strip()
@@ -1020,8 +935,7 @@ class GatewayBusySessionMixin:
)
async def _busy_start_command(self, event: MessageEvent, quick_key: str, source):
- # Telegram sends /start for bot launches/deep-links — a platform ping, not a user command:
- # no help dump, no agent interrupt, no queued text.
+ # Telegram's /start is a platform ping (bot launch/deep-link), not a user command.
logger.info("Ignoring /start platform ping for active session %s", quick_key)
return ""
@@ -1031,9 +945,7 @@ class GatewayBusySessionMixin:
return format_status_text()
async def _busy_stop_command(self, event: MessageEvent, quick_key: str, source):
- # /stop must hard-kill the session when an agent is running. A soft interrupt
- # (agent.interrupt()) doesn't help when the agent is truly hung — the executor thread is
- # blocked and never checks _interrupt_requested.
+ # Hard-kill: a soft interrupt can't reach a truly hung executor thread.
from gateway.run import _INTERRUPT_REASON_STOP
await self._interrupt_and_clear_session(
quick_key,
@@ -1045,9 +957,8 @@ class GatewayBusySessionMixin:
return EphemeralReply(t("gateway.stop.stopped"))
async def _busy_new_command(self, event: MessageEvent, quick_key: str, source):
- # /reset and /new must bypass the running-agent guard so they actually dispatch as commands
- # instead of being queued as user text (which would be fed back to the agent with the same
- # broken history — #2170). Clear any pending messages so the old text doesn't replay
+ # /reset and /new bypass the running-agent guard (else they'd queue as user text and replay
+ # into the same broken history); clear pending messages so the old text doesn't replay.
from gateway.run import _INTERRUPT_REASON_RESET
await self._interrupt_and_clear_session(
quick_key,
@@ -1055,17 +966,12 @@ class GatewayBusySessionMixin:
interrupt_reason=_INTERRUPT_REASON_RESET,
invalidation_reason="new_command",
)
- # Clean up the running agent entry so the reset handler
- # doesn't think an agent is still active.
return await self._handle_reset_command(event)
async def _busy_queue_command(self, event: MessageEvent, quick_key: str, source):
- # /queue <prompt> — queue without interrupting. Each /queue is its own full agent turn, run
- # FIFO after the current run (and earlier /queue items) finish; messages are NOT merged.
+ # Each /queue is its own full agent turn, run FIFO after the current run; never merged.
queued_text = event.get_command_args().strip()
- # Preserve media/reply payloads: a /queue carrying a photo, document, or reply context is
- # valid even with no prompt text (e.g. "/queue" as the caption of an image). Dropping these
- # fields silently lost the attachment when the queued turn ran.
+ # A /queue carrying media or reply context is valid with no prompt text (image caption).
has_media = bool(getattr(event, "media_urls", None))
if not queued_text and not has_media:
return "Usage: /queue <prompt>"
@@ -1092,23 +998,23 @@ class GatewayBusySessionMixin:
timestamp=event.timestamp,
)
self._enqueue_fifo(quick_key, queued_event, adapter)
- depth = self._queue_depth(quick_key, adapter=self._adapter_for_source(source))
+ depth = self._queue_depth(quick_key, adapter=adapter)
if depth <= 1:
return "Queued for the next turn."
return f"Queued for the next turn. ({depth} queued)"
async def _busy_steer_command(self, event: MessageEvent, quick_key: str, source):
- # /steer <prompt> — inject mid-run after the next tool call. Unlike /queue (turn boundary),
- # /steer lands BETWEEN tool-call iterations inside the same agent run, by appending to the
- # last tool result's content. No interrupt, no new user turn, no role-alternation violation.
+ # /steer lands BETWEEN tool-call iterations of the same run (appended to the last tool
+ # result) — no interrupt, no new user turn, no role-alternation violation.
from gateway.run import _AGENT_PENDING_SENTINEL
steer_text = event.get_command_args().strip()
if not steer_text:
return "Usage: /steer <prompt>"
_steer_state = self._peek_session_state(quick_key)
running_agent = _steer_state.turn.agent if _steer_state else None
- if running_agent is _AGENT_PENDING_SENTINEL:
- # Agent hasn't started yet — queue as turn-boundary fallback.
+
+ def _queue_fallback(reply: str) -> str:
+ # Turn-boundary fallback: queue the steer text as its own follow-up turn.
adapter = self._adapter_for_source(source)
if adapter:
queued_event = MessageEvent(
@@ -1120,7 +1026,10 @@ class GatewayBusySessionMixin:
channel_context=event.channel_context,
)
self._enqueue_fifo(quick_key, queued_event, adapter)
- return "Agent still starting — /steer queued for the next turn."
+ return reply
+
+ if running_agent is _AGENT_PENDING_SENTINEL:
+ return _queue_fallback("Agent still starting — /steer queued for the next turn.")
if running_agent and hasattr(running_agent, "steer"):
try:
accepted = running_agent.steer(steer_text)
@@ -1131,28 +1040,14 @@ class GatewayBusySessionMixin:
preview = steer_text[:60] + ("..." if len(steer_text) > 60 else "")
return f"⏩ Steer queued — arrives after the next tool call: '{preview}'"
return "Steer rejected (empty payload)."
- # Running agent is missing or lacks steer() — fall back to queue.
- adapter = self._adapter_for_source(source)
- if adapter:
- queued_event = MessageEvent(
- text=steer_text,
- message_type=MessageType.TEXT,
- source=event.source,
- message_id=event.message_id,
- channel_prompt=event.channel_prompt,
- channel_context=event.channel_context,
- )
- self._enqueue_fifo(quick_key, queued_event, adapter)
- return "No active agent — /steer queued for the next turn."
+ # Running agent is missing or lacks steer().
+ return _queue_fallback("No active agent — /steer queued for the next turn.")
async def _busy_goal_command(self, event: MessageEvent, quick_key: str, source):
- # /goal is safe mid-run for status/pause/clear/wait (inspection and control-plane only —
- # doesn't interrupt the running turn). Setting new goal text mid-run is rejected like
- # /model so we don't race a second continuation prompt against the current turn.
+ # Control verbs are safe mid-run (state only); setting new goal text is rejected so we don't
+ # race a second continuation against the current turn. wait/gate take an argument.
_goal_arg = (event.get_command_args() or "").strip().lower()
_goal_verb = _goal_arg.split(None, 1)[0] if _goal_arg else ""
- # Exact-match control verbs, plus the wait/unwait barrier verbs (take a pid) and the gate
- # management verb (gates run at turn boundary, so editing the gate list mid-run is safe).
_is_control = (
not _goal_arg
or _goal_arg in {"status", "pause", "resume", "clear", "stop", "done", "unwait"}
@@ -1163,8 +1058,7 @@ class GatewayBusySessionMixin:
return "Agent is running — use /goal status / pause / clear / wait mid-run, or /stop before setting a new goal."
async def _busy_loop_command(self, event: MessageEvent, quick_key: str, source):
- # /loop mirrors /goal: control verbs are safe mid-run (state only — read at the next idle
- # boundary); setting a new loop mid-run is rejected so we don't race the current turn.
+ # Mirrors /goal: control verbs are safe mid-run; a new loop is rejected.
_loop_arg = (event.get_command_args() or "").strip().lower()
if not _loop_arg or _loop_arg in {"status", "pause", "resume", "stop", "clear", "cancel", "help", "--help", "-h"}:
return await self._handle_loop_command(event)
@@ -1173,12 +1067,8 @@ class GatewayBusySessionMixin:
def _check_slash_access(
self, source: SessionSource, canonical_cmd: str
) -> Optional[str]:
- """Return a denial message if ``source`` cannot run ``canonical_cmd``, else None.
-
- Used by the cold and running-agent dispatch paths in ``_handle_message`` so admin/user gating
- can't be bypassed by an in-flight agent. Backward-compat: without ``allow_admin_from`` for the
- scope, ``policy_for_source`` returns ``enabled=False`` and this always returns None.
- """
+ """Denial message if ``source`` cannot run ``canonical_cmd``, else None (both dispatch paths
+ use it so an in-flight agent can't bypass admin gating; no ``allow_admin_from`` → None)."""
from gateway.slash_access import policy_for_source as _policy_for_source
if not canonical_cmd:
@@ -1209,14 +1099,9 @@ class GatewayBusySessionMixin:
return f"⛔ /{canonical_cmd} is admin-only here. {suffix}"
def _sibling_thread_run_keys(self, source: SessionSource, own_key: str) -> list:
- """Find running-agent keys for OTHER participants in the same thread.
-
- In per-user thread mode each participant gets an isolated key
- (``...:{thread_id}:{user_id}``), so another user's run is invisible to the caller's own
- ``/stop``. Returns keys of *actually running* agents (not the pending sentinel, not the
- caller's own) sharing the caller's ``{chat_id}:{thread_id}`` prefix; empty when not in a
- thread or no sibling runs exist. Callers must still gate on authorization.
- """
+ """Running-agent keys of OTHER participants in the same thread (per-user thread mode keys
+ are ``...:{thread_id}:{user_id}``, so another user's run is invisible to the caller's own
+ ``/stop``). Excludes the pending sentinel and ``own_key``; callers still gate on authz."""
from gateway.run import _AGENT_PENDING_SENTINEL
thread_id = getattr(source, "thread_id", None)
chat_id = getattr(source, "chat_id", None)
@@ -1224,40 +1109,29 @@ class GatewayBusySessionMixin:
return []
platform = source.platform.value
chat_type = getattr(source, "chat_type", None) or ""
- # Prefix that every per-user key in this thread shares, up to and including the thread_id
- # segment. Match the exact key or prefix + ":" (a further user_id segment) so an unrelated
- # thread whose id merely starts with this one is not matched.
+ # Match the exact key or prefix + ":" so a thread id that merely starts with this one
+ # is not matched.
prefix = ":".join(
["agent:main", platform, chat_type, str(chat_id), str(thread_id)]
)
- matches = []
- for key, agent in self._running_agent_items():
- if key == own_key:
- continue
- if agent is _AGENT_PENDING_SENTINEL or not agent:
- continue
- if key == prefix or key.startswith(prefix + ":"):
- matches.append(key)
- return matches
+ return [
+ key
+ for key, agent in self._running_agent_items()
+ if key != own_key
+ and agent is not _AGENT_PENDING_SENTINEL and agent
+ and (key == prefix or key.startswith(prefix + ":"))
+ ]
def _is_stale_restart_redelivery(self, event: MessageEvent) -> bool:
- """Return True if this /restart is a Telegram re-delivery we already handled.
+ """True if this /restart is a Telegram re-delivery we already handled.
- The previous gateway wrote ``.restart_last_processed.json`` with the triggering platform
- + update_id when it processed the /restart. A /restart on the same platform with
- update_id <= that value is a redelivery when this process booted from that restart;
- otherwise the marker must still be recent (< 5 minutes). Telegram only (the only platform
- with a numeric cross-session update ordering); other platforms return False.
+ The previous gateway wrote ``.restart_last_processed.json`` (platform + update_id). A
+ /restart with update_id <= that value is a redelivery when this process booted from that
+ restart; otherwise the marker must be < 5 minutes old. Telegram only (numeric ordering).
"""
from gateway.run import _hermes_home
- if event is None or event.source is None:
- return False
- if event.platform_update_id is None:
- return False
- if event.source.platform is None:
+ if event is None or event.source is None or event.platform_update_id is None:
return False
- # Only Telegram populates platform_update_id currently; be explicit
- # so future platforms aren't accidentally gated by this check.
try:
platform_value = event.source.platform.value
except Exception:
@@ -1268,13 +1142,9 @@ class GatewayBusySessionMixin:
try:
marker_path = _hermes_home / ".restart_last_processed.json"
if not marker_path.exists():
- # Belt-and-suspenders for a missing dedup marker (cleaned up, or the previous write
- # failed): without it the update_id comparison can't run and a redelivered /restart
- # would re-restart the gateway forever. Suppress ONLY when a restart cycle is
- # independently confirmed: this process booted from a chat-originated /restart
- # (_booted_from_restart) AND is within a short post-boot window; a genuine first
- # /restart on a fresh boot is never swallowed (flag stays False). Consume the flag
- # one-shot so a later legitimate /restart in the same session is honored.
+ # Missing marker: a redelivered /restart would otherwise re-restart forever. Suppress
+ # ONLY when this process booted from a chat /restart AND is within a short post-boot
+ # window; consume the flag one-shot so a later legitimate /restart is honored.
if (
getattr(self, "_booted_from_restart", False)
and time.time() - getattr(self, "_startup_time", 0.0) < 60
@@ -1286,35 +1156,26 @@ class GatewayBusySessionMixin:
except Exception:
return False
- if data.get("platform") != platform_value:
- return False
recorded_uid = data.get("update_id")
- if not isinstance(recorded_uid, int):
- return False
- if event.platform_update_id > recorded_uid:
+ if (
+ data.get("platform") != platform_value
+ or not isinstance(recorded_uid, int)
+ or event.platform_update_id > recorded_uid
+ ):
return False
- # A service-managed restart can legitimately take longer than the marker's normal five-
- # minute trust window while adapters, cron, and in-flight deliveries drain. Consume the boot
- # signal one-shot so a later genuine command is evaluated normally.
+ # A service-managed restart can outlast the 5-minute trust window; consume the boot
+ # signal one-shot.
if getattr(self, "_booted_from_restart", False):
self._booted_from_restart = False
return True
- # Staleness guard: ignore markers older than 5 minutes so a legitimately old one (e.g. crash
- # recovery where notify never fired) doesn't swallow a fresh /restart.
+ # Staleness guard: an old marker (crash recovery) must not swallow a fresh /restart.
requested_at = data.get("requested_at")
- if isinstance(requested_at, (int, float)):
- if time.time() - requested_at > 300:
- return False
- return True
+ return not (isinstance(requested_at, (int, float)) and time.time() - requested_at > 300)
async def _handle_suggestions_command(self, event: MessageEvent) -> str:
- """Handle /suggestions in the gateway.
-
- Delegates to the shared handler so CLI and gateway never drift. The origin is built from
- the event source so an accepted suggestion's job delivers back to this chat/thread.
- """
+ """/suggestions via the shared handler (origin = event source so jobs deliver back here)."""
from gateway.run import _command_origin_for_source
args = (event.get_command_args() or "").strip()
origin = _command_origin_for_source(event.source)
@@ -1327,11 +1188,7 @@ class GatewayBusySessionMixin:
return f"Suggestions command failed: {e}"
async def _handle_blueprint_command(self, event: MessageEvent):
- """Handle /blueprint in the gateway.
-
- Delegates to the shared handler so CLI, TUI, and gateway never drift. Origin is built
- from the event source so a directly created blueprint job delivers back to this chat.
- """
+ """/blueprint via the shared handler (origin = event source so jobs deliver back here)."""
from gateway.run import _command_origin_for_source
args = (event.get_command_args() or "").strip()
origin = _command_origin_for_source(event.source)
@@ -1356,13 +1213,10 @@ class GatewayBusySessionMixin:
) -> Union[str, "EphemeralReply", None]:
"""Gate a destructive session slash command (/new, /reset, /undo).
- ``execute`` is an async ``execute() -> str | EphemeralReply`` performing the action. It
- runs immediately if ``approvals.destructive_slash_confirm`` is off; otherwise this routes
- through ``_request_slash_confirm`` (native buttons or text fallback): ``once`` runs it,
- ``always`` persists ``destructive_slash_confirm: false`` then runs it, ``cancel`` returns
- a "cancelled" message without running it.
+ ``execute()`` (async → str | EphemeralReply) runs immediately when
+ ``approvals.destructive_slash_confirm`` is off; otherwise via ``_request_slash_confirm``:
+ ``once`` runs it, ``always`` persists the opt-out then runs it, ``cancel`` skips it.
"""
- # Gate check.
confirm_required = True
try:
cfg = self._read_user_config()
@@ -1378,57 +1232,10 @@ class GatewayBusySessionMixin:
session_key = self._session_key_for_source(event.source)
async def _on_confirm(choice: str):
- if choice == "cancel":
- return f"🟡 /{command} cancelled. Conversation unchanged."
- persisted = False
- if choice == "always":
- try:
- from cli import save_config_value
- # save_config_value swallows its own errors and reports the
- # outcome in the return value, so the try block alone says
- # nothing about whether the write landed.
- persisted = bool(
- save_config_value("approvals.destructive_slash_confirm", False)
- )
- if persisted:
- logger.info(
- "User opted out of destructive slash confirm (session=%s)",
- session_key,
- )
- else:
- logger.warning(
- "Could not persist destructive_slash_confirm=false "
- "(session=%s); config.yaml is not writable",
- session_key,
- )
- except Exception as exc:
- logger.warning(
- "Failed to persist destructive_slash_confirm=false: %s", exc,
- )
- result = await execute()
- if choice == "always":
- if persisted:
- note = (
- "\n\nℹ️ Future /clear, /new, /reset, and /undo will run "
- "without confirmation. Re-enable via "
- "`approvals.destructive_slash_confirm: true` in config.yaml."
- )
- else:
- # The user did approve this run, so the action still goes ahead, but the
- # preference did not stick and the prompt will be back next time. Say so rather
- # than promising an opt-out that was never written.
- note = (
- "\n\n⚠️ Could not save that preference (config.yaml is not "
- "writable), so /clear, /new, /reset, and /undo will ask "
- "again next time. To silence it permanently, set "
- "`approvals.destructive_slash_confirm: false` in config.yaml."
- )
- if isinstance(result, str):
- return result + note
- # EphemeralReply or other: leave untouched, since the note would
- # mangle structured replies.
- return result
- return result
+ # Via the class, not ``self``: tests drive this gate on a bare SimpleNamespace runner.
+ return await GatewayBusySessionMixin._run_confirmed_destructive_slash(
+ choice, command, execute, session_key
+ )
_p = self._typed_command_prefix_for(event.source.platform)
prompt_message = (
@@ -1448,6 +1255,53 @@ class GatewayBusySessionMixin:
handler=_on_confirm,
)
+ _DESTRUCTIVE_OPTOUT_NOTE = {
+ True: (
+ "\n\nℹ️ Future /clear, /new, /reset, and /undo will run "
+ "without confirmation. Re-enable via "
+ "`approvals.destructive_slash_confirm: true` in config.yaml."
+ ),
+ # The user did approve this run, so the action still goes ahead, but the preference did
+ # not stick and the prompt will be back next time. Say so rather than promising an
+ # opt-out that was never written.
+ False: (
+ "\n\n⚠️ Could not save that preference (config.yaml is not "
+ "writable), so /clear, /new, /reset, and /undo will ask "
+ "again next time. To silence it permanently, set "
+ "`approvals.destructive_slash_confirm: false` in config.yaml."
+ ),
+ }
+
+ @staticmethod
+ async def _run_confirmed_destructive_slash(choice: str, command: str, execute, session_key: str):
+ """Confirm-callback body: ``cancel`` → message; ``always`` persists the opt-out, then runs."""
+ if choice == "cancel":
+ return f"🟡 /{command} cancelled. Conversation unchanged."
+ persisted = False
+ if choice == "always":
+ try:
+ from cli import save_config_value
+ # save_config_value swallows its own errors and reports the outcome in the return
+ # value, so the try block alone says nothing about whether the write landed.
+ persisted = bool(save_config_value("approvals.destructive_slash_confirm", False))
+ if persisted:
+ logger.info(
+ "User opted out of destructive slash confirm (session=%s)", session_key,
+ )
+ else:
+ logger.warning(
+ "Could not persist destructive_slash_confirm=false "
+ "(session=%s); config.yaml is not writable",
+ session_key,
+ )
+ except Exception as exc:
+ logger.warning("Failed to persist destructive_slash_confirm=false: %s", exc)
+ result = await execute()
+ # Only plain-string results get the note: it would mangle an EphemeralReply.
+ if choice == "always" and isinstance(result, str):
+ return result + GatewayBusySessionMixin._DESTRUCTIVE_OPTOUT_NOTE[persisted]
+ return result
+
async def _request_slash_confirm(
self,
*,
@@ -1457,19 +1311,14 @@ class GatewayBusySessionMixin:
message: str,
handler,
) -> Optional[str]:
- """Ask the user to confirm an expensive slash command.
-
- ``handler(choice: str) -> str`` runs on the event loop when the user responds with
- ``"once"``, ``"always"``, or ``"cancel"``; its return value is sent as a gateway message.
- Returns the immediate acknowledgment: ``None`` if buttons rendered (self-explanatory),
- otherwise the text-fallback message itself IS the ack.
- """
+ """Ask the user to confirm a slash command; ``handler(choice)`` runs on "once"/"always"/
+ "cancel" and its return is sent as a message. Returns None if buttons rendered, else the
+ text-fallback message (which IS the ack)."""
from tools import slash_confirm as _slash_confirm_mod
source = event.source
session_key = self._session_key_for_source(source)
- # Bare-runner test harnesses (object.__new__(GatewayRunner)) skip __init__ and lack the
- # counter attribute; fall back to a local counter. Real runs always have the attribute.
+ # object.__new__ test runners lack the counter; fall back to a local one.
counter = getattr(self, "_slash_confirm_counter", None)
if counter is None:
import itertools as _itertools
@@ -1477,14 +1326,12 @@ class GatewayBusySessionMixin:
self._slash_confirm_counter = counter
confirm_id = f"{next(counter)}"
- # Register the pending confirm FIRST so a super-fast button click
- # cannot race the send_slash_confirm return.
+ # Register FIRST so a fast button click cannot race the send_slash_confirm return.
_slash_confirm_mod.register(session_key, confirm_id, command, handler)
adapter = self._adapter_for_source(source)
metadata = self._thread_metadata_for_source(source, self._reply_anchor_for_event(event))
- used_buttons = False
if adapter is not None:
try:
button_result = await adapter.send_slash_confirm(
@@ -1496,25 +1343,17 @@ class GatewayBusySessionMixin:
metadata=metadata,
)
if button_result and getattr(button_result, "success", False):
- used_buttons = True
+ return None # buttons rendered — no redundant text ack
except Exception as exc:
logger.debug(
"send_slash_confirm failed for %s on %s: %s",
command, source.platform, exc,
)
-
- if used_buttons:
- # Buttons rendered — no redundant text ack.
- return None
- # Text fallback — return the prompt message as the direct reply.
+ # Text fallback — the prompt message itself is the direct reply.
return message
def _read_user_config(self) -> Dict[str, Any]:
- """Read the user's raw config.yaml (cached) for gate lookups.
-
- Used by slash-confirm gates that must reflect on-disk state changes
- (e.g. a prior "Always Approve" click) without a gateway restart.
- """
+ """Raw config.yaml for gate lookups that must see on-disk changes without a restart."""
try:
from hermes_cli.config import load_config
cfg = load_config()