The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): compact heartbeat/refine/review/loop handlers; restart dedup payload helper
Teknium6847c6dc4b73
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.pyindex c5045fb3fbf..75f32ea7e63 100644--- a/gateway/slash_commands.py+++ b/gateway/slash_commands.py@@ -136,6 +136,14 @@ def _restart_notify_payload(event: MessageEvent) -> dict:return data+def _restart_dedup_payload(event: MessageEvent) -> dict:+ """Platform + update_id of the triggering /restart, for redelivery detection."""+ data = {"platform": event.source.platform.value if event.source.platform else None, "requested_at": time.time()}+ if event.platform_update_id is not None:+ data["update_id"] = event.platform_update_id+ return data++def _spawn_detached_update(hermes_cmd, output_path, exit_code_path) -> None:"""Spawn ``hermes update --gateway`` detached so it survives the gateway restart it may trigger.@@ -598,21 +606,12 @@ class GatewaySlashCommandsMixin(self._restart_command_source = event.sourcereturn data- def _dedup_payload() -> dict:- data = {- "platform": event.source.platform.value if event.source.platform else None,- "requested_at": time.time(),- }- if event.platform_update_id is not None:- data["update_id"] = event.platform_update_id- return data-# Save the requester's routing info so the new gateway process can notify them once back.await _write_marker(".restart_notify.json", _notify_payload, "notify file")# Record the triggering platform + update_id in a dedicated dedup marker. Unlike# .restart_notify.json (unlinked once the new gateway sends its notification) this persists# so a delayed Telegram redelivery is still detectable. Overwritten on every /restart.- await _write_marker(".restart_last_processed.json", _dedup_payload, "dedup marker")+ await _write_marker(".restart_last_processed.json", lambda: _restart_dedup_payload(event), "dedup marker")active_agents = self._running_agent_count()# Under a service manager (systemd/launchd) or Docker/Podman, exit 75 so the supervisor /diff --git a/gateway/slash_commands_goals.py b/gateway/slash_commands_goals.pyindex 278801651a5..8fc7f1a77b2 100644--- a/gateway/slash_commands_goals.py+++ b/gateway/slash_commands_goals.py@@ -19,6 +19,13 @@ def _plural(n: int, noun: str) -> str:return f"{n} {noun}{'s' if n != 1 else ''}"+def _quiet_bool(fn) -> bool:+ try:+ return bool(fn())+ except Exception:+ return False++def _mgr_call(prefix: str, fn, *args, errors=(RuntimeError, ValueError)):"""``(result, None)`` from ``fn(*args)``, or ``(None, "<prefix>: <exc>")`` on a manager error."""try:@@ -245,15 +252,13 @@ class GatewayGoalCommandsMixin:# Set: `/heartbeat every 10m <prompt>` (also accepts `10m <prompt>`).tokens = args.split(None, 2)- interval = None- prompt = ""- if tokens and tokens[0].lower() == "every" and len(tokens) >= 2:+ interval, prompt = None, ""+ if tokens[0].lower() == "every" and len(tokens) >= 2:interval = parse_interval(f"every {tokens[1]}")prompt = tokens[2] if len(tokens) > 2 else ""- elif tokens:+ else:interval = parse_interval(tokens[0])prompt = args[len(tokens[0]):].strip() if interval and interval > 0 else ""-if interval is None:return ("Usage: /heartbeat every <interval> <prompt> (e.g. /heartbeat every 10m Check CI)\n"@@ -263,11 +268,9 @@ class GatewayGoalCommandsMixin:return f"Interval too small — minimum is {MIN_INTERVAL_SECONDS}s."if not prompt.strip():return "Usage: /heartbeat every <interval> <prompt> — the prompt is required."-- try:- state = mgr.set(prompt, interval)- except ValueError as exc:- return f"Invalid heartbeat: {exc}"+ state, err = _mgr_call("Invalid heartbeat", mgr.set, prompt, interval, errors=(ValueError,))+ if err:+ return err_watch()return (f"♥ Heartbeat set (every {format_interval(state.interval_seconds)}): {state.prompt}\n"@@ -304,13 +307,10 @@ class GatewayGoalCommandsMixin:snapshot = list(getattr(agent, "_session_messages", None) or [])if not snapshot:return "Nothing to refine yet — the conversation is empty."-try:agent._spawn_background_review(- messages_snapshot=snapshot,- review_memory=True,- review_skills="skill_manage" in getattr(agent, "valid_tool_names", set()),- focus=args or None,+ messages_snapshot=snapshot, review_memory=True,+ review_skills="skill_manage" in getattr(agent, "valid_tool_names", set()), focus=args or None,)except Exception as exc:return f"/refine failed to start: {exc}"@@ -330,9 +330,7 @@ class GatewayGoalCommandsMixin:quick_key, agent, error = self._idle_cached_agent_or_error(event, "review")if error:return error-snapshot = list(getattr(agent, "_session_messages", None) or [])-from tools.approval import reset_current_session_key, set_current_session_keydef _dispatch():@@ -351,7 +349,6 @@ class GatewayGoalCommandsMixin:return str(exc)except Exception as exc:return f"/review failed to start: {exc}"-from agent.review_engine import format_dispatch_notereturn format_dispatch_note(result, args)@@ -397,11 +394,7 @@ class GatewayGoalCommandsMixin:return f"✓ Added subgoal {idx}: {text}"async def _handle_loop_command(self, event: MessageEvent) -> str:- """Handle /loop — recurring in-session wakeups, via ``dispatch_loop_command`` (CLI mirror).-- New loops capture the event's routing (platform/chat/thread) so the idle loop-wakeup- watcher can inject ticks here after a restart.- """+ """Handle /loop — recurring in-session wakeups, via ``dispatch_loop_command`` (CLI mirror)."""try:from hermes_cli.loops import LoopManager, dispatch_loop_command, goal_blocks_loop_tickexcept Exception as exc:@@ -420,6 +413,8 @@ class GatewayGoalCommandsMixin:return "Loops unavailable (no active session)."mgr = LoopManager(session_id=sid)+ # New loops capture the event's routing so the idle loop-wakeup watcher can inject ticks+ # here after a restart; best-effort, empty fields dropped.route: dict = {}try:src = event.source@@ -432,16 +427,11 @@ class GatewayGoalCommandsMixin:except Exception:route = {}- args = (event.get_command_args() or "").strip()- result = dispatch_loop_command(mgr, args, route=route)+ result = dispatch_loop_command(mgr, (event.get_command_args() or "").strip(), route=route)output = result.get("output") or ""- if result.get("created"):- try:- if goal_blocks_loop_tick(mgr.session_id):- output += (- "\nNote: an active /goal is driving this session — loop "- "wakeups defer until the goal finishes, pauses, or parks."- )- except Exception:- pass+ if result.get("created") and _quiet_bool(lambda: goal_blocks_loop_tick(mgr.session_id)):+ output += (+ "\nNote: an active /goal is driving this session — loop "+ "wakeups defer until the goal finishes, pauses, or parks."+ )return output