From c16870277cd224ebc53fdaf23cc31cb79a10acff Mon Sep 17 00:00:00 2001 From: teknium1 Date: Tue, 17 Mar 2026 01:35:02 -0700 Subject: [PATCH] test: add regression test for stale PID in gateway_state.json (#1631) Verifies that write_runtime_status() overwrites pid and start_time from a previous process rather than preserving them via setdefault(). Covers the fix from PR #1632. --- tests/gateway/test_status.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/gateway/test_status.py b/tests/gateway/test_status.py index 892c4cbdd..96dfa537c 100644 --- a/tests/gateway/test_status.py +++ b/tests/gateway/test_status.py @@ -44,6 +44,26 @@ class TestGatewayPidState: class TestGatewayRuntimeStatus: + def test_write_runtime_status_overwrites_stale_pid_on_restart(self, tmp_path, monkeypatch): + """Regression: setdefault() preserved stale PID from previous process (#1631).""" + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) + + # Simulate a previous gateway run that left a state file with a stale PID + state_path = tmp_path / "gateway_state.json" + state_path.write_text(json.dumps({ + "pid": 99999, + "start_time": 1000.0, + "kind": "hermes-gateway", + "platforms": {}, + "updated_at": "2025-01-01T00:00:00Z", + })) + + status.write_runtime_status(gateway_state="running") + + payload = status.read_runtime_status() + assert payload["pid"] == os.getpid(), "PID should be overwritten, not preserved via setdefault" + assert payload["start_time"] != 1000.0, "start_time should be overwritten on restart" + def test_write_runtime_status_records_platform_failure(self, tmp_path, monkeypatch): monkeypatch.setenv("HERMES_HOME", str(tmp_path))