"""Shared gateway restart constants and supervisor detection helpers.""" import math import os import re import subprocess import sys from collections.abc import Callable, Mapping from hermes_cli.config import DEFAULT_CONFIG # EX_TEMPFAIL (sysexits.h): ask the service manager to restart after a graceful drain/reload. GATEWAY_SERVICE_RESTART_EXIT_CODE = 75 # EX_CONFIG (sysexits.h): fatal configuration error (token collision, no platforms); # the s6 finish script maps it to exit 125 so the supervisor stops restarting. # See #51228. GATEWAY_FATAL_CONFIG_EXIT_CODE = 78 def map_fatal_config_exit_for_launchd(returncode: int) -> int: """Translate gateway EX_CONFIG so launchd can park the job. systemd uses ``RestartPreventExitStatus=78``; s6 maps 78→125. launchd cannot gate on a specific status, and unconditional ``KeepAlive=true`` respawns 78 forever (#89477). The generated plist uses ``KeepAlive.SuccessfulExit=false``; mapping 78→0 is a deliberate stop. Exit 75 (please restart) and other failures pass through so KeepAlive still relaunches them. Negative ``wait()`` codes (signals) are left to the caller. """ if returncode == GATEWAY_FATAL_CONFIG_EXIT_CODE: return 0 return returncode # Set by ``hermes gateway run --external-supervisor``. Unlike systemd's INVOCATION_ID # and launchd's XPC_SERVICE_NAME, this survives wrappers that replace the child # environment (e.g. ``sudo env -i``). EXTERNAL_GATEWAY_SUPERVISOR_ENV = "HERMES_GATEWAY_EXTERNAL_SUPERVISOR" # Forwarded by the stderr-timestamp launchd wrapper (hermes_cli/stderr_timestamp.py) to the gateway # grandchild, which sees ``XPC_SERVICE_NAME=0``. Read only via :func:`launchd_job_label`. LAUNCHD_LABEL_ENV = "HERMES_LAUNCHD_LABEL" DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT = float(DEFAULT_CONFIG["agent"]["restart_drain_timeout"]) DEFAULT_GATEWAY_SIGNAL_INTERRUPT_GRACE_TIMEOUT = float(DEFAULT_CONFIG["gateway"]["signal_interrupt_grace_timeout"]) DEFAULT_GATEWAY_POST_INTERRUPT_GRACE_TIMEOUT = 5.0 # In-band restart waits for active turns to finish *before* ``stop()`` begins; distinct from # ``restart_drain_timeout``, the force-interrupt budget once ``stop()`` runs (short under TimeoutStopSec). DEFAULT_GATEWAY_RESTART_AFTER_TURN_TIMEOUT = float(DEFAULT_CONFIG["agent"]["restart_after_turn_timeout"]) # Floor under the ``stop()`` drain for cron jobs and api_server (/v1) runs. ``restart_drain_timeout`` # defaults to 0 because interrupting a *chat* turn is cheap and recoverable (user told, session # resume_pending); an interrupted *cron* run is a permanent failure in jobs.json and an interrupted # /v1 run fails its waiting caller — a 0s drain silently destroys work. DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT = float(DEFAULT_CONFIG["agent"]["cron_drain_timeout"]) # Watchdog leash held back for post-drain work (interrupt agents, kill subprocesses, mark jobs, # disconnect adapters). Waiting for cron past that trades a job killed *and recorded* for one # SIGKILLed mid-write and wedged at ``last_status=running`` forever. CRON_DRAIN_CLEANUP_RESERVE_S = 10.0 # systemd TimeoutStopSec headroom after the stop-path drain budget, and the floor when that # budget is still the default immediate (0s) chat drain. Keep in lockstep with generate_systemd_unit(). # See #94759. SYSTEMD_STOP_HEADROOM_S = 30.0 SYSTEMD_TIMEOUT_STOP_SEC_FLOOR = 60.0 # launchd is the one supervisor whose stop budget the gateway cannot size: # ``ExitTimeOut`` lives in the plist, and the per-user (gui) domain CLAMPS # it — measured on macOS 26.6.1: plist 215 -> live 60, 90 -> 60, 60 -> 60, # 30 -> 30. The gateway can only *read* the live value (``launchctl print # gui//