Skip to content

The agent that grows with you

refactor(update): move the systemd unit timing budgets into update_cmd_fleet_unit_budget

Tekniume6d03aa06688
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 509bfc7c17f..2348c122c45 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -60,7 +60,7 @@ from hermes_cli.update_cmd_fleet import ( # noqa: F401
_restart_gateway_fleet_after_update, _restart_launchd_gateway_after_update,
_restart_macos_launchd_gateways, _restart_phase_failure_is_incomplete,
_restart_systemd_gateway_units,
- _run_pending_fleet_restart, _service_restart_sec,
+ _run_pending_fleet_restart,
_service_unit_supports_graceful_sigusr1_restart, _surviving_gateway_pids_after_failed_restart,
_systemctl, _systemctl_reset_and_restart,
_wait_for_service_active, _warn_gateway_restart_phase_aborted,
diff --git a/hermes_cli/update_cmd_fleet.py b/hermes_cli/update_cmd_fleet.py
index d7eff376bfa..c2e0be51785 100644
--- a/hermes_cli/update_cmd_fleet.py
+++ b/hermes_cli/update_cmd_fleet.py
@@ -732,51 +732,13 @@ def _systemctl(cmd: list, *, timeout: float):
return subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=timeout)
-# poll() takes signed 32-bit milliseconds; keep headroom for rounding in communicate().
-_SYSTEMCTL_RESTART_TIMEOUT_MAX = (2**31 - 1) // 1000 - 1
-
-
-def _systemd_restart_timeout(scope_cmd: list, svc_name: str, *, start_only: bool = False) -> float:
- """Outwait the unit's stop + start budgets, not just the systemctl client.
-
- A client timeout does not cancel the manager's queued restart. Unknown or
- infinite limits use systemd's usual 90s per phase so automation stays bounded.
- Custom ExecStop chains or EXTEND_TIMEOUT_USEC can still exceed this budget;
- genuine timeouts must continue through the existing per-unit failure path.
- """
- from gateway.shutdown_forensics import parse_systemd_duration_to_us
-
- budgets = {"TimeoutStartUSec": 90.0}
- if not start_only:
- budgets["TimeoutStopUSec"] = 90.0
- try:
- show = _systemctl(
- scope_cmd + ["show", svc_name, "--property=TimeoutStopUSec,TimeoutStartUSec"],
- timeout=5,
- )
- except (FileNotFoundError, subprocess.TimeoutExpired):
- return sum(budgets.values()) + 15.0
- if show.returncode == 0:
- for line in (show.stdout or "").splitlines():
- key, _, raw = line.partition("=")
- if key in budgets:
- # The shared parser returns None for infinity/unrecognized units.
- try:
- raw = raw.strip()
- duration = int(raw) if raw.isascii() and raw.isdigit() else parse_systemd_duration_to_us(raw)
- if duration is not None and duration > 0:
- budgets[key] = duration / 1_000_000
- except (ValueError, OverflowError):
- pass
- return min(sum(budgets.values()) + 15.0, _SYSTEMCTL_RESTART_TIMEOUT_MAX)
-
-
def _systemctl_reset_and_restart(manage_cmd: list, svc_name: str, *, scope_cmd: list | None = None):
"""``reset-failed`` then ``restart``: a unit parked in failed state by systemd's own
auto-restart can wedge a plain ``restart`` against RestartSec backoff and stay dead."""
# Property reads need no manage-units privileges: narrow sudoers may permit
# restart/reset-failed but deny show. Keep the same user/system manager scope.
- timeout = _systemd_restart_timeout(scope_cmd if scope_cmd is not None else manage_cmd, svc_name)
+ from hermes_cli.update_cmd_fleet_unit_budget import systemd_restart_timeout
+ timeout = systemd_restart_timeout(scope_cmd if scope_cmd is not None else manage_cmd, svc_name)
_systemctl(manage_cmd + ["reset-failed", svc_name], timeout=10)
return _systemctl(manage_cmd + ["restart", svc_name], timeout=timeout)
@@ -1332,6 +1294,7 @@ def _restart_one_systemd_gateway_unit(
_main_pid, drain_budget, svc_name, self_restart_pending=self_restart_pending)
if _graceful_ok:
+ from hermes_cli.update_cmd_fleet_unit_budget import service_restart_sec, systemd_restart_timeout
# ``Restart=always`` respawns only after RestartSec (60s in our unit; dead time for a
# voluntary restart). ``reset-failed`` + ``start`` skips it (~1-3s); if RestartSec already
# elapsed, ``start`` is a no-op and we fall through to the poll. Needs manage-units
@@ -1340,14 +1303,14 @@ def _restart_one_systemd_gateway_unit(
_systemctl(_manage_cmd + ["reset-failed", svc_name], timeout=10)
_systemctl(
_manage_cmd + ["start", svc_name],
- timeout=_systemd_restart_timeout(scope_cmd, svc_name, start_only=True),
+ timeout=systemd_restart_timeout(scope_cmd, svc_name, start_only=True),
)
if _wait_for_service_active(scope_cmd, svc_name, timeout=10.0):
restarted_services.append(svc_name)
return
# Passive poll: auto-restart fires after RestartSec regardless of
# privileges — primary when _manage_cmd is None, fallback otherwise.
- _restart_sec = _service_restart_sec(scope_cmd, svc_name, default=0.0)
+ _restart_sec = service_restart_sec(scope_cmd, svc_name, default=0.0)
if _manage_cmd is None and _restart_sec > 5.0:
print(
f" → {svc_name}: waiting for systemd "
@@ -1891,29 +1854,3 @@ def _wait_for_service_active(scope_cmd_: list, svc_name_: str, timeout: float =
if _time.monotonic() >= deadline:
return False
_time.sleep(0.5)
-
-
-_RESTART_SEC_UNITS = (("ms", 0.001), ("us", 0.000001), ("min", 60.0), ("s", 1.0))
-
-
-def _service_restart_sec(scope_cmd_: list, svc_name_: str, default: float = 0.0) -> float:
- """Read the unit's ``RestartUSec`` in seconds. ``is-active`` pollers must wait
- >= RestartSec + slack or they give up *during* the cooldown and misreport."""
- try:
- _show = _systemctl(scope_cmd_ + ["show", svc_name_, "--property=RestartUSec", "--value"], timeout=5)
- except (FileNotFoundError, subprocess.TimeoutExpired):
- return default
- raw = (_show.stdout or "").strip()
- # Values like "30s", "100ms", "1min 30s", "infinity"; on any miss return default.
- if not raw or raw == "infinity":
- return default
- total = 0.0
- matched = False
- for part in raw.split():
- for _suf, _mult in _RESTART_SEC_UNITS:
- if part.endswith(_suf):
- with suppress(ValueError):
- total += float(part[: -len(_suf)]) * _mult
- matched = True
- break
- return total if matched else default
diff --git a/hermes_cli/update_cmd_fleet_unit_budget.py b/hermes_cli/update_cmd_fleet_unit_budget.py
new file mode 100644
index 00000000000..24542213be3
--- /dev/null
+++ b/hermes_cli/update_cmd_fleet_unit_budget.py
@@ -0,0 +1,78 @@
+"""systemd unit timing budgets for the update's gateway restarts (``update_cmd_fleet`` sibling).
+
+How long a restart client must wait (the unit's stop + start budgets) and how long an ``is-active``
+poller must outwait the unit's ``RestartSec`` cooldown. Both read unit properties through
+``update_cmd_fleet._systemctl`` (late-imported: that is the seam tests patch).
+"""
+
+from __future__ import annotations
+
+import subprocess
+from contextlib import suppress
+
+# poll() takes signed 32-bit milliseconds; keep headroom for rounding in communicate().
+_SYSTEMCTL_RESTART_TIMEOUT_MAX = (2**31 - 1) // 1000 - 1
+
+
+def systemd_restart_timeout(scope_cmd: list, svc_name: str, *, start_only: bool = False) -> float:
+ """Outwait the unit's stop + start budgets, not just the systemctl client.
+
+ A client timeout does not cancel the manager's queued restart. Unknown or
+ infinite limits use systemd's usual 90s per phase so automation stays bounded.
+ Custom ExecStop chains or EXTEND_TIMEOUT_USEC can still exceed this budget;
+ genuine timeouts must continue through the existing per-unit failure path.
+ """
+ from gateway.shutdown_forensics import parse_systemd_duration_to_us
+ from hermes_cli.update_cmd_fleet import _systemctl
+
+ budgets = {"TimeoutStartUSec": 90.0}
+ if not start_only:
+ budgets["TimeoutStopUSec"] = 90.0
+ try:
+ show = _systemctl(
+ scope_cmd + ["show", svc_name, "--property=TimeoutStopUSec,TimeoutStartUSec"],
+ timeout=5,
+ )
+ except (FileNotFoundError, subprocess.TimeoutExpired):
+ return sum(budgets.values()) + 15.0
+ if show.returncode == 0:
+ for line in (show.stdout or "").splitlines():
+ key, _, raw = line.partition("=")
+ if key in budgets:
+ # The shared parser returns None for infinity/unrecognized units.
+ try:
+ raw = raw.strip()
+ duration = int(raw) if raw.isascii() and raw.isdigit() else parse_systemd_duration_to_us(raw)
+ if duration is not None and duration > 0:
+ budgets[key] = duration / 1_000_000
+ except (ValueError, OverflowError):
+ pass
+ return min(sum(budgets.values()) + 15.0, _SYSTEMCTL_RESTART_TIMEOUT_MAX)
+
+
+_RESTART_SEC_UNITS = (("ms", 0.001), ("us", 0.000001), ("min", 60.0), ("s", 1.0))
+
+
+def service_restart_sec(scope_cmd_: list, svc_name_: str, default: float = 0.0) -> float:
+ """Read the unit's ``RestartUSec`` in seconds. ``is-active`` pollers must wait
+ >= RestartSec + slack or they give up *during* the cooldown and misreport."""
+ from hermes_cli.update_cmd_fleet import _systemctl
+
+ try:
+ _show = _systemctl(scope_cmd_ + ["show", svc_name_, "--property=RestartUSec", "--value"], timeout=5)
+ except (FileNotFoundError, subprocess.TimeoutExpired):
+ return default
+ raw = (_show.stdout or "").strip()
+ # Values like "30s", "100ms", "1min 30s", "infinity"; on any miss return default.
+ if not raw or raw == "infinity":
+ return default
+ total = 0.0
+ matched = False
+ for part in raw.split():
+ for _suf, _mult in _RESTART_SEC_UNITS:
+ if part.endswith(_suf):
+ with suppress(ValueError):
+ total += float(part[: -len(_suf)]) * _mult
+ matched = True
+ break
+ return total if matched else default