The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a held gateway.lock is never unlinked on an identity mismatch (#125610, salvage #125679)
JoaoMarcos447350426b1793
diff --git a/gateway/status.py b/gateway/status.pyindex af85ad0548b..9094b4ae6b7 100644--- a/gateway/status.py+++ b/gateway/status.py@@ -2057,10 +2057,11 @@ def get_running_pid() -> Optional[int]:"""PID of a running gateway (lock + PID file verified against the live process), or None.An explicit ``pid_path`` is a scoped query into that home's identity files: records are- validated against the probed home (not the serve process's), and a live record is never- cleanup-unlinked, so polling another profile must not delete its gateway.pid/gateway.lock- (#106406). The unscoped path keeps main's poison-file housekeeping: a live record owned by- another home inside this home's gateway.pid is unlinked on refusal (#89315)."""+ validated against the probed home (not the serve process's) (#106406). While the runtime lock+ is HELD, a live recorded PID is never cleanup-unlinked, scoped or not: the holder is a gateway+ whatever its command line reads as, and unlinking a held ``gateway.lock`` leaves it locking a+ deleted inode so the next starter double-runs (#125610, #123109). Dead records and inactive-lock+ metadata still take the poison-file cleanup (#89315)."""resolved_pid_path = pid_path or _get_pid_path()resolved_lock_path = _get_gateway_lock_path(resolved_pid_path)if is_gateway_runtime_lock_active(resolved_lock_path):@@ -2081,11 +2082,11 @@ def get_running_pid(record, pid, expected_home=expected_home):return pid- # Scoped only: a live record we could not adopt may still be a real gateway;- # unlinking its identity files would break that home's double-run protection- # while the PID is alive. Unscoped keeps the #89315 poison-file cleanup.+ # A live record we could not adopt may still be a real gateway (an identity matcher+ # that lags a new launcher shape, a record written by an older version): an identity+ # rejection is not stale-file authority while the PID is alive and the lock held.saw_live_pid = True- if expected_home is None or not saw_live_pid:+ if not saw_live_pid:_cleanup_invalid_pid_path(resolved_pid_path, cleanup_stale=cleanup_stale)return get_runtime_status_running_pid() if pid_path is None else None# Lock inactive: the runtime-status fallback runs BEFORE cleanup here.diff --git a/tests/gateway/test_status.py b/tests/gateway/test_status.pyindex e74c078901d..0ebb171abdc 100644--- a/tests/gateway/test_status.py+++ b/tests/gateway/test_status.py@@ -169,6 +169,31 @@ class TestGatewayPidState:monkeypatch.setenv("HERMES_HOME", str(process_home))(process_home / "gateway.pid").unlink(missing_ok=True)+ def test_unscoped_live_identity_mismatch_never_unlinks_active_files(+ self, tmp_path, monkeypatch+ ):+ """A live PID behind a HELD lock is not stale-cleanup authority, even unscoped: an+ identity rejection (a matcher lagging a launcher shape) must not unlink the live+ gateway's gateway.pid/gateway.lock (#125610, #123109)."""+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ record = {+ "pid": 4242, "kind": "hermes-gateway", "start_time": 123,+ "argv": ["python", "-m", "hermes_cli.main", "gateway", "run"],+ "hermes_home": str(tmp_path.resolve()),+ }+ pid_path = tmp_path / "gateway.pid"+ lock_path = tmp_path / "gateway.lock"+ pid_path.write_text(json.dumps(record), encoding="utf-8")+ lock_path.write_text(json.dumps(record), encoding="utf-8")+ monkeypatch.setattr(status, "is_gateway_runtime_lock_active", lambda _path=None: True)+ monkeypatch.setattr(status, "_pid_exists", lambda _pid: True)+ monkeypatch.setattr(status, "_get_process_start_time", lambda _pid: 123)+ monkeypatch.setattr(status, "_read_process_cmdline", lambda _pid: "python -m hermes_cli.main chat")+ monkeypatch.setattr(status, "get_runtime_status_running_pid", lambda: None)++ assert status.get_running_pid() is None+ assert pid_path.exists() and lock_path.exists()+class TestScopedGatewayPidQuery:"""get_running_pid(pid_path) is a scoped query into another home's identity files (#106406):