The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): --replace no longer skips the host-lock refusal
John Paul Solivaae163160fee7
diff --git a/gateway/run.py b/gateway/run.pyindex 8ba08d15c52..2c07b36de47 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -5861,7 +5861,10 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =_planned_stop_watcher_thread.start()# PID file BEFORE adapters: of two concurrent `run --replace`, only the O_EXCL winner opens sockets.- if not _start_gateway_claim_pid_file(force=force or replace):+ # Only --force skips the host-lock refusal. Every generated unit carries --replace, so reading it as+ # --force there disabled the one arbiter of the two-units-at-once race; a replace that took the+ # owner over already freed the lock with that process.+ if not _start_gateway_claim_pid_file(force=force):return False# Right after the PID claim (which makes us authoritative); non-fatal — consumers fall back to scan.diff --git a/tests/gateway/test_host_gateway_lock_refusal.py b/tests/gateway/test_host_gateway_lock_refusal.pyindex 73bd8052d03..e1dc8d459cc 100644--- a/tests/gateway/test_host_gateway_lock_refusal.py+++ b/tests/gateway/test_host_gateway_lock_refusal.py@@ -149,3 +149,56 @@ def test_a_multiplexing_owner_is_still_refused(host_lock_dir, monkeypatch):finally:handle.close()assert exc.value.code == GATEWAY_SERVICE_RESTART_EXIT_CODE++[email protected](sys.platform == "win32", reason="flock-based contention setup")+async def test_a_replace_unit_that_replaced_nothing_is_still_refused_when_it_loses_the_lock(+ host_lock_dir, tmp_path, monkeypatch,+):+ """Every unit Hermes generates (launchd, systemd, s6) runs ``gateway run --replace``. When the+ attach check saw no owner yet (the record lands a moment after the owner's claim) nothing was+ replaced, and the lock is the only arbiter of the race. Reading ``--replace`` as ``--force``+ there started a second gateway beside the multiplexer, and the two fought for the same bot+ tokens: each --replace token handoff SIGTERMs the current holder."""+ from unittest.mock import AsyncMock++ from gateway import host_rendezvous as hr+ from gateway import status+ from gateway.config import GatewayConfig+ from gateway.restart import GATEWAY_SERVICE_RESTART_EXIT_CODE+ from gateway.run import start_gateway++ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))+ (tmp_path / "home").mkdir()++ class _RunnerMustNotStart:+ def __init__(self, config):+ self.config = config+ self.adapters = {}++ async def start(self):+ raise AssertionError("a second gateway started beside the host owner")++ async def stop(self):+ return None++ monkeypatch.setattr("gateway.run._host_attach_or_none", AsyncMock(return_value=None))+ monkeypatch.setattr("gateway.status.get_running_pid", lambda: None)+ monkeypatch.setattr("tools.skills_sync.sync_skills", lambda quiet=True: None)+ monkeypatch.setattr("hermes_logging.setup_logging", lambda hermes_home, mode: tmp_path)+ monkeypatch.setattr("hermes_logging._add_rotating_handler", lambda *args, **kwargs: None)+ monkeypatch.setattr("gateway.run.GatewayRunner", _RunnerMustNotStart)+ monkeypatch.setattr("gateway.host_attach.request_serve_profile",+ lambda profile, owner=None: None) # a multiplexer, not a standalone owner++ hr.publish_record(hr.ROLE_GATEWAY, profiles=("default",), home=str(host_lock_dir))+ handle = _hold_host_lock_from_another_description(hr)+ try:+ with pytest.raises(SystemExit) as exc:+ await start_gateway(config=GatewayConfig(), replace=True, verbosity=None)+ finally:+ handle.close()+ status.remove_pid_file()+ status.release_gateway_runtime_lock()+ assert exc.value.code == GATEWAY_SERVICE_RESTART_EXIT_CODEdiff --git a/website/docs/user-guide/multi-profile-gateways.md b/website/docs/user-guide/multi-profile-gateways.mdindex 210b36d4273..0c17e7928e0 100644--- a/website/docs/user-guide/multi-profile-gateways.md+++ b/website/docs/user-guide/multi-profile-gateways.md@@ -87,6 +87,10 @@ rather than "this profile's gateway":- Under a service supervisor the attach exits 75, not 0 — systemd, s6 andlaunchd all restart a 75 after a short delay, so the unit keeps retrying andtakes over by itself the moment the host process goes away.+- Two units started at once can both see no host process yet; the host lock+ decides which one runs, and the loser exits 75 and attaches on the retry.+ `--replace` does not skip that check (every generated unit carries it), only+ `--force` does.Multiplexing is **on by default** (`gateway.multiplex_profiles` defaults to`true`), with one safety rule: an *unset* flag is a request the default gateway