Skip to content

The agent that grows with you

refactor(gateway): trim the --replace ownership prose and reuse _migrate_command

kshitijk4poorfa4714ca6a18
diff --git a/gateway/host_attach.py b/gateway/host_attach.py
index 070c970f778..faf2bf87f15 100644
--- a/gateway/host_attach.py
+++ b/gateway/host_attach.py
@@ -10,13 +10,10 @@ Five outcomes, in order:
* ``ATTACH`` — a live host gateway already serves this profile. Nothing to start; exit 0.
* ``RESCAN``→ATTACH — it does not serve it yet: ask it to reconcile ``profiles/`` now (control
socket ``rescan-profiles``) and attach once the answer includes us.
-* ``REPLACE_HOST`` — ``--replace`` names the host process as the target when it serves this
- profile, whichever home launched it. An owner that has not published its served set yet is
- also targeted, but the ownership guard (``run._replace_target_belongs_to_other_profile``) can
- then only prove it from THIS home's pid record, so a served-unknown owner launched from another
- home is refused (exit 1, one supervisor retry). An owner known not to serve us is another
- profile's gateway and falls through to REFUSE/START below; ``--force`` is the explicit
- takeover-anything switch.
+* ``REPLACE_HOST`` — ``--replace`` targets the host process when it serves this profile, whichever
+ home launched it. An owner that has not published its served set yet is targeted too, but the
+ ownership guard (``run._replace_target_belongs_to_other_profile``) can then only prove it from
+ THIS home's pid record, so from another home the replace is refused (exit 1).
* ``REFUSE`` — a live MULTIPLEXING gateway exists and cannot be made to serve this profile.
Never start a second one silently.
* ``START`` — no live owner, or the owner answers ``multiplex: False``: it is another
@@ -346,9 +343,7 @@ def decide(our_home: Path, *, replace: bool = False) -> HostAttachDecision:
if gateway is None or gateway.pid == os.getpid():
return standalone_attach_decision(our_home, None) or HostAttachDecision(START, "")
if replace and (gateway.serves(profile) or not gateway.served_known):
- # --replace is authority over the process SERVING THIS PROFILE (whichever home launched it;
- # a served-unknown owner is provable only from this home's pid record). An owner known not
- # to serve us is another profile's gateway: replacing it is always refused
+ # An owner known not to serve us is another profile's gateway: replacing it is always refused
# (_replace_target_belongs_to_other_profile fails closed) and the gateway exits, so on a
# one-process-per-profile fleet, whose generated units all carry --replace, every unit but
# the lock holder respawn-storms. Such an owner takes the non-replace path below instead.
@@ -384,10 +379,12 @@ def decide(our_home: Path, *, replace: bool = False) -> HostAttachDecision:
# every unit but the first to claim the host lock. Start beside it. decide() runs twice per
# start (CLI guard + start_gateway), so this is INFO; the host-lock claim in run.py logs the
# one WARNING with the `gateway migrate --multiplex` converge hint.
+ from hermes_cli.gateway_migrate import MIGRATE_COMMAND
+
logger.info(
"Another profile's standalone gateway owns this host (%s); starting profile '%s' beside it. "
- "Fold every profile onto one gateway with: hermes gateway migrate --multiplex",
- attached.describe(), profile)
+ "Fold every profile onto one gateway with: %s",
+ attached.describe(), profile, MIGRATE_COMMAND)
return HostAttachDecision(START, "")
if not gateway.served_known:
# The owner never answered, so we know only that it exists. ATTACH here (on the record's
diff --git a/gateway/run.py b/gateway/run.py
index 06dcb9d5e9c..20071c31644 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -5484,22 +5484,18 @@ def _owner_is_standalone() -> bool:
def _refuse_second_host_gateway(owner) -> None:
"""Print the named refusal and exit 75 so a supervisor retries instead of parking the unit.
- Reached only after losing the host lock to a live process, so ``--replace`` is not offered as
- the way past it: it only takes over an owner that serves this profile (which would have been
- handled before the claim), and it does not skip the lock check. A holder with no readable
- record (its publish failed, or it runs another HOST_PROTOCOL_VERSION) can only be stopped or
- bypassed with ``--force``.
+ Reached only after losing the host lock, so ``--replace`` is not offered: an owner that serves
+ this profile was already handled before the claim, and ``--replace`` does not skip the lock.
"""
from gateway import host_rendezvous as hr
from gateway.restart import GATEWAY_SERVICE_RESTART_EXIT_CODE
- from hermes_cli.gateway_migrate import MIGRATE_COMMAND
who = hr.describe(owner) if owner else "owner unknown (its record is gone)"
message = (
f"❌ Another gateway already owns this host: {who}\n"
f" Exactly one gateway per host serves every profile, so this process will not start a\n"
f" second one (it would double-bind this profile's platforms).\n"
- f" Fold every profile onto the owner: {MIGRATE_COMMAND}\n"
+ f" Fold every profile onto the owner: {_migrate_command()}\n"
f" Or stop the other gateway first, then start this one.\n"
f" Or start one anyway (skips the host-lock check): hermes gateway run --force\n"
f" (--replace does not skip this check; it only replaces an owner that serves this profile.)")
@@ -5581,8 +5577,7 @@ async def _host_attach_or_none(replace: bool, force: bool = False) -> Optional[b
print(decision.message)
return False
if decision.outcome == REPLACE_HOST and decision.owner is not None:
- # decide() already filtered: the owner serves this profile (whichever home launched it) or
- # has not published its served set yet (then only this home's pid record can prove it).
+ # decide() only targets an owner that serves this profile or has not published its served set.
if not await _start_gateway_replace_existing_instance(decision.owner.pid, True):
return False
return None
@@ -5885,10 +5880,9 @@ async def start_gateway(config: Optional[GatewayConfig] = None, replace: bool =
# PID file BEFORE adapters: of two concurrent `run --replace`, only the O_EXCL winner opens sockets.
# 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. Consequence: a live holder with no
- # readable record (publish_record failed after the claim, or a different HOST_PROTOCOL_VERSION
- # during a rolling upgrade) blocks every other unit with exit 75 until it exits; only --force
- # gets past it.
+ # owner over already freed the lock with that process. Consequence: a live holder this process
+ # cannot interrogate (its record never published, or it speaks another HOST_PROTOCOL_VERSION
+ # mid-upgrade) blocks every other unit with exit 75 until it exits; only --force gets past it.
if not _start_gateway_claim_pid_file(force=force):
return False