Skip to content

The agent that grows with you

fix(gateway): let the startup_failed stamp clear the way instead of parking the unit

salch-credff9e252d0027
diff --git a/gateway/run_bootstrap.py b/gateway/run_bootstrap.py
index 818af836e79..cc5caf5b4ba 100644
--- a/gateway/run_bootstrap.py
+++ b/gateway/run_bootstrap.py
@@ -29,9 +29,7 @@ logger = logging.getLogger("gateway.run")
async def _start_gateway_replace_existing_instance(existing_pid: int, replace: bool) -> bool:
"""Handle a live gateway PID under this HERMES_HOME: replace it (``--replace``) or refuse.
- Returns False when startup must abort (refused, permission denied, target still alive).
- Raises ``SystemExit(GATEWAY_FATAL_CONFIG_EXIT_CODE)`` when ownership is unprovable: that verdict
- is decided by the persisted identity record alone, so a retry can never change it."""
+ Returns False when startup must abort (refused, permission denied, target still alive)."""
from gateway.run import (_clear_takeover_marker_quiet, _replace_target_belongs_to_other_profile, _wait_for_pid_exit, get_hermes_home, suppress)
from gateway.status import get_process_start_time, remove_pid_file, terminate_pid
if not replace:
@@ -51,7 +49,6 @@ async def _start_gateway_replace_existing_instance(existing_pid: int, replace: b
# Never signal a process not provably ours (a poisoned PID record → cross-profile restart loop).
if _replace_target_belongs_to_other_profile(existing_pid):
- from gateway.restart import GATEWAY_FATAL_CONFIG_EXIT_CODE
from gateway.run import _write_runtime_status_quiet
from gateway.status import _get_process_hermes_home
logger.error(
@@ -59,14 +56,14 @@ async def _start_gateway_replace_existing_instance(existing_pid: int, replace: b
"to this profile's gateway (HERMES_HOME %s). Remove the "
"stale PID record or stop the owning profile explicitly.",
existing_pid, _get_process_hermes_home())
- # The verdict is decided by the persisted identity record alone, so no supervisor retry can
- # change it: a bare exit 1 left a Restart=always unit looping on a PID record that will never
- # prove ours (#135392). Stamp the fatal state and exit EX_CONFIG so systemd's
- # RestartPreventExitStatus=78 parks the unit and s6 maps 78 → 125 (permanent failure), and
- # /api/status reports startup_failed instead of a stale heartbeat.
+ # Stamp the retained state so the stale PID record stops being trusted BEFORE the
+ # next attempt: get_runtime_status_running_pid() returns None for startup_failed,
+ # so the retry either finds the target gone or a fresh PID instead of looping on
+ # the same record (#135392). Keep exit 1 so a supervisor's restart can win; a
+ # parked unit would hold WhatsApp and cron down on an unattended host.
_write_runtime_status_quiet(
gateway_state="startup_failed", exit_reason="gateway_replace_ownership_unproven")
- raise SystemExit(GATEWAY_FATAL_CONFIG_EXIT_CODE)
+ return False
existing_start_time = get_process_start_time(existing_pid)
logger.info("Replacing existing gateway instance (PID %d) with --replace.", existing_pid)
# Takeover marker: target exits 0 on our SIGTERM (exit 1 → systemd Restart=on-failure flap loop).
diff --git a/tests/gateway/test_replace_ownership_guard.py b/tests/gateway/test_replace_ownership_guard.py
index e4f1d24bf3d..473bf39959f 100644
--- a/tests/gateway/test_replace_ownership_guard.py
+++ b/tests/gateway/test_replace_ownership_guard.py
@@ -17,11 +17,11 @@ Pinned surfaces:
* signal boundary — ``start_gateway(replace=True)`` on unprovable ownership
refuses without calling ``terminate_pid`` or writing a takeover marker; the
legitimate bound same-home target still reaches the replace flow;
-* fatal refusal (#135392) — the unprovable-ownership refusal is decided by the
- persisted identity record alone, so it stamps ``gateway_state: startup_failed``
- (with an ``exit_reason`` code) and exits ``GATEWAY_FATAL_CONFIG_EXIT_CODE``
- (78) instead of a bare exit 1, so a Restart=always unit parks rather than
- restart-looping on the same record.
+* retained stamp (#135392) — the unprovable-ownership refusal stamps
+ ``gateway_state: startup_failed`` (with an ``exit_reason`` code) so the
+ stale PID record stops being trusted (``get_runtime_status_running_pid()``
+ returns None for startup_failed) and /api/status shows reality, while the
+ exit stays 1 so a supervisor's next attempt can win.
"""
from __future__ import annotations
@@ -332,16 +332,13 @@ class TestSignalBoundary:
agent_patches(stack)
try:
result = asyncio_run(gateway_run.start_gateway(replace=True))
- except SystemExit as exc:
- result = exc.code
except Exception:
result = "raised"
return result, calls
def test_unprovable_ownership_never_signals(self, profile_env):
- """Unprovable ownership → start_gateway exits fatally (EX_CONFIG) WITHOUT
- calling terminate_pid or writing a takeover marker."""
- from gateway.restart import GATEWAY_FATAL_CONFIG_EXIT_CODE
+ """Unprovable ownership → start_gateway returns False WITHOUT calling
+ terminate_pid or writing a takeover marker."""
def configure(stack):
guard = stack.enter_context(
@@ -354,9 +351,9 @@ class TestSignalBoundary:
result, calls = self._run_replace(lambda s: configure(s))
- assert result == GATEWAY_FATAL_CONFIG_EXIT_CODE, (
- "an unprovable-ownership refusal is permanent: exit 78 to park the unit, "
- "never a bare exit 1 that restarts into the same refusal"
+ assert result is False, (
+ "the refusal must stay exit-1-shaped so a supervisor's next attempt "
+ "can succeed once the stale record stops being trusted"
)
assert calls["terminate"] == 0, (
"--replace must not signal a target whose ownership is unproven"
@@ -366,8 +363,9 @@ class TestSignalBoundary:
)
def test_unprovable_ownership_stamps_startup_failed(self, profile_env):
- """The fatal refusal publishes the retained fatal state + an exit reason,
- so /api/status reports startup_failed instead of a stale heartbeat."""
+ """The refusal publishes the retained fatal state + an exit reason, so
+ /api/status reports startup_failed instead of a stale heartbeat and the
+ next start no longer trusts the stale PID record."""
def configure(stack):
stack.enter_context(
patch(