from __future__ import annotations

import threading

_MLX_XML_ONLY_WARNING_THRESHOLD = 2
_mlx_xml_only_counts: dict[str, int] = {}
_mlx_xml_only_warned_threads: set[str] = set()
_mlx_xml_only_lock = threading.Lock()


def record_xml_salvage_round(thread_id: str | None) -> tuple[bool, int]:
    """Track XML-salvage rounds and return (should_warn_now, round_count)."""
    with _mlx_xml_only_lock:
        key = thread_id or "__unknown__"
        _mlx_xml_only_counts[key] = _mlx_xml_only_counts.get(key, 0) + 1
        count = _mlx_xml_only_counts[key]
        if count >= _MLX_XML_ONLY_WARNING_THRESHOLD and key not in _mlx_xml_only_warned_threads:
            _mlx_xml_only_warned_threads.add(key)
            return True, count
        return False, count

