- deadline: _result()/_abandon() replace 7 BoundedResult constructions and 2 cancel+callback sites; timers handled as a list; dead raise_if_timed_out removed. - file_safety: retired classify_cross_profile_target (0 refs; get_cross_profile_warning stub kept for external callers), _home_and_resolved/_mirror_warning shared by the sandbox/container mirror guards; _find_sandbox_mirror_segments inlined. - estop: _hermes_home/_canonical_root now the file_safety helpers; _reset_log_state_for_tests inlined into its only test. - subagent_lifecycle: _validate_request driven by _UNSUPPORTED_REQUEST_FIELDS table. - turn_liveness: dead start()/_abort_message removed, _emit_warning shared. - process_bootstrap: _enable_happy_eyeballs reused by the client variant. - Comment/docstring compaction across the remaining leaf modules.
162 lines
5.3 KiB
Python
162 lines
5.3 KiB
Python
"""Global emergency stop (ESTOP) — a resumable pause for NEW work only.
|
|
|
|
``hermes pause`` writes a sentinel at ``$HERMES_HOME/ESTOP``; ``hermes resume``
|
|
removes it. While it exists the cron scheduler, kanban dispatcher and new
|
|
gateway turns skip work; in-flight work is never killed. The check is one or
|
|
two ``os.stat`` calls (process home + fleet root when they differ), uncached,
|
|
so engaging/disengaging binds on the next check. The sentinel body is optional
|
|
JSON ``{"reason", "engaged_at"}``; a corrupt/empty file still counts as engaged
|
|
(fail safe, e.g. ``touch ~/.hermes/ESTOP``). Ported from gastownhall/gastown
|
|
estop.go (MIT).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import threading
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
# Same profile-aware / fleet-root resolvers the file-safety guards use (fail-open to ~/.hermes).
|
|
from agent.file_safety import _hermes_home_path as _hermes_home, _hermes_root_path as _canonical_root
|
|
|
|
SENTINEL_NAME = "ESTOP"
|
|
|
|
# Per-component "logged already for this engagement" flags: log once per
|
|
# engagement, not once per tick.
|
|
_log_lock = threading.Lock()
|
|
_logged_components: set[str] = set()
|
|
|
|
|
|
def sentinel_path() -> Path:
|
|
"""Path of the ESTOP sentinel this process would write on `hermes pause`."""
|
|
return _hermes_home() / SENTINEL_NAME
|
|
|
|
|
|
def _candidate_sentinel_paths() -> list:
|
|
"""Profile home first, then the fleet root if it is a different directory: a profile
|
|
gateway (HERMES_HOME=~/.hermes/profiles/<n>) must still honor an operator's ~/.hermes/ESTOP."""
|
|
primary = sentinel_path()
|
|
paths = [primary]
|
|
try:
|
|
root = _canonical_root() / SENTINEL_NAME
|
|
except Exception:
|
|
return paths
|
|
try:
|
|
if root.resolve() != primary.resolve():
|
|
paths.append(root)
|
|
except Exception:
|
|
# Non-Path test doubles fail .resolve(); plain equality still dedupes.
|
|
if root != primary:
|
|
paths.append(root)
|
|
return paths
|
|
|
|
|
|
def is_engaged() -> bool:
|
|
"""True if ANY candidate sentinel exists; fail SAFE (True) on stat errors."""
|
|
saw_stat_error = False
|
|
for path in _candidate_sentinel_paths():
|
|
try:
|
|
if path.exists():
|
|
return True
|
|
except OSError:
|
|
saw_stat_error = True
|
|
return saw_stat_error
|
|
|
|
|
|
def engage(reason: Optional[str] = None) -> Path:
|
|
"""Create the ESTOP sentinel. Idempotent; re-engaging updates the file."""
|
|
path = sentinel_path()
|
|
payload = {"engaged_at": datetime.now(timezone.utc).isoformat(), "reason": reason or None}
|
|
try:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
|
|
except OSError:
|
|
# Best effort: an empty/partial sentinel still pauses (fail safe).
|
|
try:
|
|
path.touch(exist_ok=True)
|
|
except OSError:
|
|
pass
|
|
return path
|
|
|
|
|
|
def disengage() -> bool:
|
|
"""Remove every visible sentinel (process-local and fleet-root)."""
|
|
lifted = False
|
|
for path in _candidate_sentinel_paths():
|
|
try:
|
|
path.unlink()
|
|
lifted = True
|
|
except (OSError, AttributeError):
|
|
continue
|
|
return lifted
|
|
|
|
|
|
def get_state() -> Optional[dict]:
|
|
"""Return ``{"reason", "engaged_at"}`` or None when not engaged.
|
|
|
|
An unreadable/corrupt body still reports engaged with both fields None.
|
|
"""
|
|
if not is_engaged():
|
|
return None
|
|
reason = engaged_at = None
|
|
found = False
|
|
for path in _candidate_sentinel_paths():
|
|
try:
|
|
exists = path.exists()
|
|
except OSError:
|
|
return {"reason": None, "engaged_at": None}
|
|
except AttributeError:
|
|
continue
|
|
if not exists:
|
|
continue
|
|
found = True
|
|
try:
|
|
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
if isinstance(raw, dict):
|
|
reason = raw.get("reason") or None
|
|
engaged_at = raw.get("engaged_at") or None
|
|
break
|
|
except (OSError, ValueError, AttributeError):
|
|
continue
|
|
if not found:
|
|
return None
|
|
return {"reason": reason, "engaged_at": engaged_at}
|
|
|
|
|
|
def paused_reply() -> Optional[str]:
|
|
"""Short user-facing notice for new gateway turns, or None if not paused."""
|
|
state = get_state()
|
|
if state is None:
|
|
return None
|
|
reason = state.get("reason")
|
|
tag = f" ({reason})" if reason else ""
|
|
return f"⏸️ Hermes is paused{tag}. New work is on hold; run `hermes resume` to pick things back up."
|
|
|
|
|
|
def check_paused(component: str, logger: logging.Logger) -> bool:
|
|
"""Return True when engaged, logging once per engagement per component
|
|
(re-armed after a resume)."""
|
|
if not is_engaged():
|
|
with _log_lock:
|
|
_logged_components.discard(component)
|
|
return False
|
|
with _log_lock:
|
|
first = component not in _logged_components
|
|
if first:
|
|
_logged_components.add(component)
|
|
if first:
|
|
state = get_state() or {}
|
|
reason = state.get("reason")
|
|
suffix = f" (reason: {reason})" if reason else ""
|
|
logger.info(
|
|
"%s dispatch paused by global emergency stop%s — remove with "
|
|
"`hermes resume` (%s)",
|
|
component,
|
|
suffix,
|
|
sentinel_path(),
|
|
)
|
|
return True
|