pause.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """``hermes pause`` / ``hermes resume`` — the global emergency stop.
  2. ``pause`` writes the ESTOP sentinel at ``$HERMES_HOME/ESTOP``; cron, kanban and new gateway
  3. turns halt on their next check (in-flight work is never killed). Every local turn is a gateway
  4. turn, so interactive CLI, TUI and Desktop turns are held too, with ``agent.estop.paused_reply``'s
  5. notice. ``resume`` removes it and operation resumes on the next tick — no restart. Ported from gastownhall/gastown estop.go (MIT).
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. def cmd_pause(args: argparse.Namespace) -> int:
  10. """Engage the global emergency stop."""
  11. from agent.estop import engage, get_state, is_engaged
  12. reason = getattr(args, "reason", None)
  13. already = is_engaged()
  14. path = engage(reason=reason)
  15. state = get_state() or {}
  16. verb = "Still paused" if already else "Hermes paused"
  17. detail = f" — reason: {state['reason']}" if state.get("reason") else ""
  18. print(f"⏸️ {verb}{detail}")
  19. print(f" sentinel: {path}")
  20. print(
  21. " Cron dispatch, kanban dispatch, and new gateway turns (messaging and interactive\n"
  22. " CLI, TUI and Desktop turns alike) are on hold. In-flight work keeps running.\n"
  23. " Run `hermes resume` to lift the pause.")
  24. return 0
  25. def cmd_resume(args: argparse.Namespace) -> int:
  26. """Disengage the global emergency stop."""
  27. from agent.estop import disengage, sentinel_path
  28. if disengage():
  29. print("▶️ Hermes resumed — dispatch picks up on the next tick.")
  30. else:
  31. print(f"Hermes is not paused (no sentinel at {sentinel_path()}).")
  32. return 0
  33. def build_pause_parser(subparsers) -> None:
  34. """Attach the ``pause`` and ``resume`` subcommands to ``subparsers``."""
  35. pause_parser = subparsers.add_parser(
  36. "pause", help="Emergency stop: pause cron/kanban dispatch and new gateway turns",
  37. description="Engage the global emergency stop. Halts NEW work only — cron "
  38. "dispatch, kanban dispatch, and new gateway turns — until "
  39. "`hermes resume`. Interactive CLI, TUI and Desktop turns are gateway turns, so they "
  40. "are held too: each gets \"Hermes is paused\" with the reason. Turns already "
  41. "running finish; in-flight work is never killed.")
  42. pause_parser.add_argument(
  43. "--reason", default=None, help="Optional reason stored in the sentinel and shown to users")
  44. pause_parser.set_defaults(func=cmd_pause)
  45. resume_parser = subparsers.add_parser(
  46. "resume", help="Lift the emergency stop set by `hermes pause`",
  47. description="Remove the ESTOP sentinel; dispatch resumes on the next tick.")
  48. resume_parser.set_defaults(func=cmd_resume)