Skip to content

The agent that grows with you

test(gateway): the default profile arriving second starts beside a standalone named owner

teknium19ead09cfd104
diff --git a/tests/gateway/test_host_attach_lifecycle.py b/tests/gateway/test_host_attach_lifecycle.py
index 933bb08fe49..903e51f582f 100644
--- a/tests/gateway/test_host_attach_lifecycle.py
+++ b/tests/gateway/test_host_attach_lifecycle.py
@@ -241,3 +241,21 @@ def test_a_foreign_record_is_not_a_record(tmp_path, monkeypatch, owner_pid):
assert hr.read_record(hr.ROLE_GATEWAY) is None
assert host_attach.host_gateway() is None
+
+
+def test_the_default_profile_arriving_second_starts_beside_a_standalone_named_owner(tmp_path, monkeypatch, owner_pid):
+ """The field shape of #118282: after a fleet restart a NAMED standalone unit claimed the host first and
+ the DEFAULT gateway arrived second. Refusing it exited 78 and its system unit crash-looped; the default
+ profile is a peer in a per-profile fleet, not a latecomer to a multiplexer."""
+ root = tmp_path / "root"
+ owner_home = root / "profiles" / "agent-ops"
+ _publish(owner_pid, owner_home, ("agent-ops",))
+ _answer_identify(monkeypatch, owner_pid, owner_home, ["agent-ops"])
+ monkeypatch.setattr(gateway_run, "get_hermes_home", lambda: root)
+ monkeypatch.setattr("gateway.control_socket.rescan_gateway_profiles",
+ lambda home, timeout=8.0: {"multiplex": False, "served_profiles": ["agent-ops"]})
+
+ decision = host_attach.decide(root)
+ assert host_attach.profile_name_for_home(root) == "default"
+ assert decision.outcome == host_attach.START
+ assert asyncio.run(gateway_run._host_attach_or_none(replace=False)) is None
diff --git a/tests/hermes_cli/test_update_unit_client_budget.py b/tests/hermes_cli/test_update_unit_client_budget.py
index c9db5169f6c..4ba823a52c5 100644
--- a/tests/hermes_cli/test_update_unit_client_budget.py
+++ b/tests/hermes_cli/test_update_unit_client_budget.py
@@ -69,3 +69,41 @@ def test_budget_fallback_keeps_real_errors(monkeypatch, limits, outcome):
result = fleet._systemctl_reset_and_restart(["systemctl"], "hermes-serve-test")
assert result.returncode == outcome
assert result.stderr == "manager diagnostic"
+
+
[email protected]("root", [True, False])
+def test_fleet_restart_repairs_a_system_unit_that_cannot_park_on_exit_78(monkeypatch, tmp_path, capsys, root):
+ """A ``Restart=on-failure`` system unit predating ``RestartPreventExitStatus=78`` crash-looped ~180x on
+ a permanent refusal while the user units parked (#118282). The update-time fleet restart is the only
+ contact with that unit: as root it rewrites it, otherwise it names the repair. A parked unit is left alone."""
+ from hermes_cli import gateway as gateway_cli
+
+ unit_dir = tmp_path / "system"
+ unit_dir.mkdir()
+ stale = unit_dir / "hermes-gateway.service"
+ stale.write_text("[Service]\nRestart=on-failure\nRestartSec=10\n", encoding="utf-8")
+ current = unit_dir / "hermes-gateway-ops.service"
+ current.write_text(f"[Service]\nRestart=always\nRestartPreventExitStatus={gateway_cli.GATEWAY_FATAL_CONFIG_EXIT_CODE}\n", encoding="utf-8")
+ monkeypatch.setattr(gateway_cli, "_SYSTEM_UNIT_DIR", unit_dir)
+ monkeypatch.setattr(gateway_cli, "get_service_name", lambda: "hermes-gateway")
+ monkeypatch.setattr(fleet, "_needs_sudo", lambda scope: not root)
+ refreshed = []
+ monkeypatch.setattr(gateway_cli, "refresh_systemd_unit_if_needed", lambda system=False: refreshed.append(system))
+ monkeypatch.setattr(fleet, "_systemctl", lambda cmd, *, timeout: subprocess.CompletedProcess(cmd, 0, "active", ""))
+ monkeypatch.setattr(fleet, "_drain_or_signal_gateway_for_update", lambda *a: True)
+ monkeypatch.setattr(fleet, "_wait_for_service_active", lambda *a, **kw: True)
+
+ for name in ("hermes-gateway", "hermes-gateway-ops"):
+ fleet._restart_one_systemd_gateway_unit(
+ name, scope="system", scope_cmd=["systemctl"], drain_budget=5,
+ _manage_cmd_cache={"system": ["systemctl"]}, restarted_services=[], failed_or_stale_units=[],
+ )
+
+ out = capsys.readouterr().out
+ if root:
+ assert refreshed == [True]
+ assert "RestartPreventExitStatus" not in out
+ else:
+ assert refreshed == []
+ assert out.count("RestartPreventExitStatus=78") == 1 and "hermes-gateway lacks" in out
+ assert "sudo hermes gateway install --system" in out