// Degeneracy guard — exact port of hermes-router.py bad_output().
// Catches empty / too-long / repetitive / gibberish model output so we never
// auto-send garbage (Legea 4: design for what breaks).
export function badOutput(t: string): boolean {
  if (!t || t.trim().length < 2) return true;
  if (t.length > 700) return true;

  const words = t.split(/\s+/).filter(Boolean);
  if (words.length >= 8) {
    const counts = new Map<string, number>();
    for (const w of words) counts.set(w, (counts.get(w) ?? 0) + 1);
    const max = Math.max(...counts.values());
    if (max / words.length > 0.30) return true; // one word > 30% of text
  }

  // same word repeated 5+ times in a row
  if (/(\b\w+\b)([\s.,!]+\1){4,}/i.test(t)) return true;

  return false;
}
