refactor(gateway): reuse recorded_gateway_home_conflicts for the scoped PID home check
The salvaged fix added `_pid_record_matches_home`, a near-copy of `recorded_gateway_home_conflicts(record, expected_home=...)` (already the scoped-home predicate used by the #89315 stop guard). Reuse it instead of carrying a second helper with the same semantics (legacy records without `hermes_home` are accepted by both). Also trims the salvaged tests to the two invariants that prove the symptom (scoped probe reports a live foreign profile's PID and leaves its identity files intact; a dead-PID scoped record is still cleaned). The third-home-claim variant exercised the same `saw_live_pid` branch.
This commit is contained in:
@@ -450,15 +450,6 @@ def _pid_record_belongs_to_current_profile(record: Optional[dict[str, Any]]) ->
|
||||
return not record_home or _same_hermes_home(record_home, _get_process_hermes_home())
|
||||
|
||||
|
||||
def _pid_record_matches_home(record: Optional[dict[str, Any]], expected_home: Path) -> bool:
|
||||
"""True when the record's ``hermes_home`` matches ``expected_home`` (legacy records: True).
|
||||
Scoped queries validate against the probed home, not the serve process's."""
|
||||
if not isinstance(record, dict):
|
||||
return False
|
||||
record_home = record.get("hermes_home")
|
||||
return not record_home or _same_hermes_home(record_home, expected_home)
|
||||
|
||||
|
||||
def _build_runtime_status_record() -> dict[str, Any]:
|
||||
return {
|
||||
**_build_pid_record(), "gateway_state": "starting", "exit_reason": None,
|
||||
@@ -1473,7 +1464,7 @@ def get_running_pid(
|
||||
continue
|
||||
home_ok = (
|
||||
_pid_record_belongs_to_current_profile(record) if expected_home is None
|
||||
else _pid_record_matches_home(record, expected_home)
|
||||
else not recorded_gateway_home_conflicts(record, expected_home=expected_home)
|
||||
)
|
||||
if home_ok and _record_matches_live_gateway_pid(
|
||||
record, pid, expected_home=expected_home
|
||||
|
||||
@@ -206,23 +206,6 @@ class TestScopedGatewayPidQuery:
|
||||
assert pid_path.exists()
|
||||
assert (profile_dir / "gateway.lock").exists()
|
||||
|
||||
def test_scoped_query_never_unlinks_live_foreign_identity(self, tmp_path, monkeypatch):
|
||||
# Live PID whose record claims a THIRD home: not adoptable, but its identity files must
|
||||
# survive the poll (double-run/takeover protection).
|
||||
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "default-home"))
|
||||
profile_dir, pid_path, record = self._write_scoped_profile(tmp_path)
|
||||
# Both identity files migrate together in the real world; repoint both homes.
|
||||
record["hermes_home"] = str((tmp_path / "elsewhere").resolve())
|
||||
pid_path.write_text(json.dumps(record))
|
||||
(profile_dir / "gateway.lock").write_text(json.dumps(record))
|
||||
monkeypatch.setattr(status, "is_gateway_runtime_lock_active", lambda lock: 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: None)
|
||||
assert status.get_running_pid(pid_path) is None
|
||||
assert pid_path.exists()
|
||||
assert (profile_dir / "gateway.lock").exists()
|
||||
|
||||
def test_scoped_query_still_cleans_dead_pid_record(self, tmp_path, monkeypatch):
|
||||
# A dead PID's stale record is still cleanup-unlinked, scoped or not.
|
||||
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "default-home"))
|
||||
|
||||
Reference in New Issue
Block a user