from __future__ import annotations


_GUARD_STREAK_COOL_CAP = 0.35
_GUARD_STREAK_COOLER_CAP = 0.15


def effective_llm_temperature_for_guard_streak(base: float, streak: int, fallback_temp: float) -> float:
    """Lower temperature on repeated guard-triggered retries."""
    try:
        b = float(base)
    except (TypeError, ValueError):
        b = float(fallback_temp)
    s = int(streak) if streak is not None else 0
    if s <= 1:
        return b
    if s == 2:
        return min(b, _GUARD_STREAK_COOL_CAP)
    return min(b, _GUARD_STREAK_COOLER_CAP)

