"""Heartbeat loop stop conditions — shared by agent.py and unit tests."""

from __future__ import annotations

from typing import Any, Mapping, Optional


def heartbeat_should_stop(
    status_code: int,
    body: Optional[Mapping[str, Any]] = None,
) -> bool:
    """True when central rejected heartbeat permanently (do not retry)."""
    if status_code == 401:
        return True
    if status_code == 200 and isinstance(body, Mapping):
        if body.get("status") == "discarded":
            return True
    return False
