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))