gateway_runtime_cli.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """Machine-readable ensure boundary. No bootstrap credentials leave this command."""
  2. from dataclasses import asdict
  3. import json
  4. import sys
  5. from contextlib import redirect_stdout
  6. def ensure_exit_code(result) -> int:
  7. if result.reason_code == "deadline":
  8. return 5
  9. if result.reason_code in {"authorization", "profile_mismatch", "gateway_elevated", "gateway_other_session"}:
  10. return 4
  11. return {"ready": 0, "incompatible": 3, "draining": 6,
  12. "inaccessible": 7, "conflict": 7, "starting": 5, "absent": 5}[result.state]
  13. def cmd_gateway_ensure(args) -> None:
  14. from hermes_constants import get_hermes_home
  15. from hermes_cli.gateway_runtime import ensure_gateway_runtime
  16. try:
  17. # Legacy imported utilities can print; protocol stdout belongs only to us.
  18. with redirect_stdout(sys.stderr):
  19. result = ensure_gateway_runtime(get_hermes_home(), timeout=float(args.timeout))
  20. payload = asdict(result)
  21. if result.endpoint:
  22. payload["endpoint"]["capabilities"] = sorted(result.endpoint.capabilities)
  23. # The code THIS client (and any gateway it starts) runs, resolved exactly like the owner's
  24. # ``endpoint.code_sha``: a client that sees them differ is attached to a gateway that
  25. # outlived ``hermes update`` and must restart it rather than re-attach.
  26. from hermes_cli.version_info import get_code_identity
  27. payload["client_code_sha"] = get_code_identity().get("sha")
  28. code = ensure_exit_code(result)
  29. except (ValueError, TypeError):
  30. payload, code = {"state": "inaccessible", "reason_code": "invalid_invocation", "endpoint": None}, 2
  31. print(json.dumps(payload, separators=(",", ":"), sort_keys=True))
  32. raise SystemExit(code)