"""Passive output-quality tripwires (ADR-0018 §7).

Cheap heuristics over every completed request's text -- no model access, no
extra inference -- that flag likely-degenerate output (repetition loops,
truncation) so ``AdaptiveAuditSampler`` can bump that single request's audit
probability. A flag never strikes or bans a wallet by itself; it only raises
the odds that request gets a real audit (research §8 layer 5).

True perplexity scoring needs the serving model's logits, which the validator
does not have outside an audit call, so it stays roadmap-only (ADR-0018 §8);
this covers the repetition/truncation half of the passive-tripwire layer.
"""

from __future__ import annotations


def detect_output_tripwire(
    text: str,
    *,
    min_words: int = 4,
    repetition_threshold: float = 0.4,
) -> bool:
    """Flag empty output or a single word/token dominating the response."""
    if not text or not text.strip():
        return True
    words = text.split()
    if len(words) < min_words:
        return False
    counts: dict[str, int] = {}
    for word in words:
        counts[word] = counts.get(word, 0) + 1
    most_common = max(counts.values())
    return (most_common / len(words)) >= repetition_threshold


__all__ = ["detect_output_tripwire"]
