The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway, pm): a replaced gateway's PID file is judged by its own instance; live-hold test
Tekniumbb84e4b4e557
diff --git a/gateway/status.py b/gateway/status.pyindex 10ee84c013e..be08be0b5ff 100644--- a/gateway/status.py+++ b/gateway/status.py@@ -2040,22 +2040,10 @@ def get_running_pid_identity_strict(pid_path: Path) -> Optional[tuple[int, floatreturn Nonelock_record = _read_gateway_lock_record(resolved_lock_path)# The PID file is advisory beside a HELD lock: a launch-service gateway keeps serving after its- # gateway.pid was unlinked (#110166) and --replace force-unlinks the old one (#123430). The- # holder wrote its own identity into the lock at acquisition, so that record validated against- # the live process below is the same proof — raising here blocked every following update.- # A PID file whose process is gone is the same kind of noise when --replace rewrote the lock- # but left the previous gateway's file behind (#143331): the dead record used to abort every- # following `hermes update` at the pause stage. It drops out here; a record naming a LIVE- # process still has to agree with the lock below, so two live identities stay ambiguous.- pid_record = None- if pid_exists:- pid_record = _read_pid_record(resolved_pid_path)- if pid_record is None:- raise RuntimeError("gateway PID or lock metadata is malformed")- recorded_pid = _pid_from_record(pid_record)- if isinstance(recorded_pid, int) and recorded_pid > 0 and not _pid_exists(recorded_pid):- pid_record = None- records = (pid_record, lock_record) if pid_record is not None else (lock_record,)+ # gateway.pid was unlinked (#110166) and --replace force-unlinks the old one (#123430) or leaves+ # it behind (#143331); the lock record validated against the live process below is the proof.+ from gateway.status_pid_records import strict_identity_records+ records = strict_identity_records(resolved_pid_path, lock_record, pid_exists)if not all(records):raise RuntimeError("gateway PID or lock metadata is malformed")pid = _pid_from_record(records[0])diff --git a/gateway/status_pid_records.py b/gateway/status_pid_records.pynew file mode 100644index 00000000000..ec3bce48d9f--- /dev/null+++ b/gateway/status_pid_records.py@@ -0,0 +1,52 @@+"""Which runtime records name the gateway that holds ``gateway.lock`` right now.++``gateway.status.get_running_pid_identity_strict`` validates the PID file and the+lock record against one live process. The PID file is advisory beside a HELD+lock: a launch-service gateway keeps serving after ``gateway.pid`` was unlinked+(#110166), and ``gateway run --replace`` rewrites the lock but can leave the+previous gateway's PID file behind (#143331). That leftover names a different+process instance (its own start time) that is gone or whose PID was recycled,+and comparing it with the lock aborted every following ``hermes update`` at the+Windows gateway pause stage until the file was removed by hand.++A PID file claiming the lock holder's own start time under another PID is not a+leftover but garbled state, so it still reaches the comparison and fails closed;+so does a PID file naming another LIVE instance.+"""++from __future__ import annotations++from pathlib import Path+from typing import Any, Optional+++def strict_identity_records(pid_path: Path, lock_record: Optional[dict[str, Any]],+ pid_exists: bool) -> tuple:+ """Records the strict identity check must agree on; a previous gateway's PID file drops out."""+ from gateway import status++ if not pid_exists:+ return (lock_record,)+ pid_record = status._read_pid_record(pid_path)+ if pid_record and lock_record and names_a_previous_gateway(pid_record, lock_record):+ return (lock_record,)+ return (pid_record, lock_record)+++def names_a_previous_gateway(pid_record: dict[str, Any], lock_record: dict[str, Any]) -> bool:+ """True when *pid_record* is another instance than the lock holder and that instance is gone."""+ from gateway import status++ pid = status._pid_from_record(pid_record)+ recorded, held = pid_record.get("start_time"), lock_record.get("start_time")+ if pid is None or pid <= 0 or recorded is None or held is None:+ return False+ try:+ if status._start_times_agree(held, recorded):+ return False+ if not status._pid_exists(pid):+ return True+ live = status._get_process_start_time(pid)+ return live is not None and not status._start_times_agree(live, recorded)+ except (TypeError, ValueError):+ return Falsediff --git a/tests/gateway/test_status_strict_identity_live.py b/tests/gateway/test_status_strict_identity_live.pyindex a69c9318df0..7af0b1687e8 100644--- a/tests/gateway/test_status_strict_identity_live.py+++ b/tests/gateway/test_status_strict_identity_live.py@@ -112,6 +112,14 @@ def _exited_pid() -> int:return pid+def _write_previous_gateway_pid_file(home: Path, pid: int) -> None:+ """The PID file a replaced gateway left behind (#143331): its own PID and an earlier start."""+ record = json.loads((home / "gateway.pid").read_text(encoding="utf-8-sig"))+ record["pid"] = pid+ record["start_time"] = max(1, int(record["start_time"]) - 500)+ (home / "gateway.pid").write_text(json.dumps(record), encoding="utf-8")++@pytest.fixture(scope="class")def gateway_holder(tmp_path_factory):"""One live gateway-shaped lock holder for the class; each spawn costs about a second."""@@ -165,8 +173,7 @@ class TestStrictIdentityWithLiveLock:def test_pid_file_that_disagrees_with_the_lock_still_aborts(self, home, gateway_holder):record = json.loads((home / "gateway.pid").read_text(encoding="utf-8-sig"))- # A LIVE second identity: the disagreement is real, not a leftover file.- record["pid"] = os.getpid()+ record["pid"] = gateway_holder[1] + 1(home / "gateway.pid").write_text(json.dumps(record), encoding="utf-8")with pytest.raises(RuntimeError, match="disagree"):@@ -175,9 +182,7 @@ class TestStrictIdentityWithLiveLock:def test_stale_pid_file_beside_a_held_lock_resolves_from_the_lock_record(self, home, gateway_holder,):- record = json.loads((home / "gateway.pid").read_text(encoding="utf-8-sig"))- record["pid"] = _exited_pid()- (home / "gateway.pid").write_text(json.dumps(record), encoding="utf-8")+ _write_previous_gateway_pid_file(home, _exited_pid())identity = status.get_running_pid_identity_strict(home / "gateway.pid")@@ -191,14 +196,21 @@ class TestStrictIdentityWithLiveLock:monkeypatch.setattr("hermes_cli.profiles._get_default_hermes_home", lambda: home)monkeypatch.setattr("hermes_cli.profiles._get_profiles_root", lambda: home / "no-profiles")- record = json.loads((home / "gateway.pid").read_text(encoding="utf-8-sig"))- record["pid"] = _exited_pid()- (home / "gateway.pid").write_text(json.dumps(record), encoding="utf-8")+ _write_previous_gateway_pid_file(home, _exited_pid())procs = find_profile_gateway_processes(strict=True)assert [(Path(p.path).resolve(), p.pid) for p in procs] == [(home.resolve(), gateway_holder[1])]+ def test_previous_gateway_pid_recycled_by_a_live_process_resolves_from_the_lock(+ self, home, gateway_holder,+ ):+ _write_previous_gateway_pid_file(home, os.getpid())++ identity = status.get_running_pid_identity_strict(home / "gateway.pid")++ assert identity is not None and identity[0] == gateway_holder[1]+def test_malformed_pid_file_beside_a_held_lock_still_aborts(self, home):(home / "gateway.pid").write_text("not a record", encoding="utf-8")diff --git a/tests/pm/test_held_identical_entry_windows_live.py b/tests/pm/test_held_identical_entry_windows_live.pynew file mode 100644index 00000000000..ebd355724d4--- /dev/null+++ b/tests/pm/test_held_identical_entry_windows_live.py@@ -0,0 +1,58 @@+"""A live process holding a tools entry that already matches the update's bytes (#139088).++Windows refuses to rename a directory while any process keeps a file inside it+open without delete sharing, which is what a running gateway or Desktop does to+``tools\\git-*`` and ``tools\\python-*``. When an explicit install re-realizes the+SAME version (the recorded digest no longer matches the tree), the rename of the+live entry to ``.previous-*`` used to fail the whole run with WinError 5/32, so+every update ended "Desktop app build owed". The OS produces the hold here.+"""++from __future__ import annotations++import pytest++from pm.lock import Facts+from pm.store import tree_digest+from tests.pm.test_pm_core import pm_env as pm_env # noqa: PLC0414 -- the self-alias is load-bearing: it suppresses F811 for the pytest fixture parameter shadowing this import+from tests.pm._fixtures import served as served # noqa: PLC0414 -- the self-alias is load-bearing: it suppresses F811 for the pytest fixture parameter shadowing this import+++def _installed_with_drifted_digest(pm_env):+ """Install faketool, then make its recorded digest disagree with the realized tree."""+ from pm.install import ensure++ _, runtime, *_ = pm_env+ ensure("faketool", base_env={})+ facts = Facts(runtime / "facts.json")+ fact = facts.get("faketool")+ entry = runtime / fact["entry"]+ facts.record("faketool", fact["version"], fact["entry"], {}, runtime, target=fact.get("target"),+ artifacts=fact.get("artifacts"), digest="0" * 64)+ return runtime, entry++[email protected]("windows")+def test_held_entry_matching_the_pin_keeps_the_explicit_install_working(pm_env):+ from pm.install import ensure++ runtime, entry = _installed_with_drifted_digest(pm_env)+ with open(entry / "bin" / "faketool", "rb"):+ runner = ensure("faketool", base_env={}, explicit=True)+ assert entry.name in runner.env["PATH"]+ assert (entry / "bin" / "faketool").read_bytes() == b"#!x"+ assert not (runtime / f".previous-{entry.name}").exists()+ assert Facts(runtime / "facts.json").get("faketool")["digest"] == tree_digest(entry)++[email protected]("windows")+def test_held_entry_with_different_bytes_still_fails_the_install(pm_env):+ from pm.install import ensure+ from pm.package import InstallError++ runtime, entry = _installed_with_drifted_digest(pm_env)+ (entry / "bin" / "extra").write_bytes(b"not in the pin")+ with open(entry / "bin" / "faketool", "rb"), pytest.raises(InstallError):+ ensure("faketool", base_env={}, explicit=True)+ assert (entry / "bin" / "extra").exists()+ assert not (runtime / f".previous-{entry.name}").exists()