#!/usr/bin/env python3
"""Tally the fresh-session naming experiment.

Per prompt: the modal name, its share, distinct names, and whether the
convergent GitHub name (the one strangers actually used) came back. Per
model: the same, so a family-wide attractor can be told from one model's tic.
"""
import json, collections, sys
from pathlib import Path

ROWS = [json.loads(l) for l in Path(__file__).with_name("results.jsonl").read_text().splitlines() if l.strip()]
TARGET = {"quietfail": "quietfail", "claude-pet": "claude-pet", "groundskeeper": "groundskeeper",
          "hearth": "the-hearth", "samehand": "samehand", "recipe": None}
SHORT = {"claude-haiku-4-5-20251001": "haiku", "claude-sonnet-5": "sonnet", "claude-opus-5": "opus", "claude-fable-5-1": "fable"}

def norm(n):
    return (n or "").strip().lower().replace(" ", "-").replace("_", "-")

by_prompt = collections.defaultdict(list)
for r in ROWS:
    by_prompt[r["prompt_id"]].append((SHORT.get(r["model"], r["model"]), norm(r["name"])))

print(f"{len(ROWS)} calls · {len({m for m,_ in sum(by_prompt.values(), [])})} models · {len(by_prompt)} prompts\n")
for pid in ["quietfail", "claude-pet", "groundskeeper", "hearth", "samehand", "recipe"]:
    rows = by_prompt.get(pid, [])
    names = [n for _, n in rows if n]
    c = collections.Counter(names)
    modal, k = (c.most_common(1)[0] if c else ("—", 0))
    target = TARGET[pid]
    hit = sum(1 for n in names if target and (n == target or n.replace("-", "") == target.replace("-", "")))
    print(f"== {pid:14s} n={len(names):3d}  distinct={len(c):3d}  modal={modal!r} {k}/{len(names)} ({k/max(len(names),1):.0%})"
          + (f"  GitHub name '{target}' returned {hit}/{len(names)}" if target else "  (control: no known attractor)"))
    per_model = collections.defaultdict(collections.Counter)
    for m, n in rows:
        if n: per_model[m][n] += 1
    for m in ["haiku", "sonnet", "opus", "fable"]:
        cm = per_model.get(m)
        if cm:
            print(f"   {m:7s} " + " · ".join(f"{n}×{v}" if v > 1 else n for n, v in cm.most_common(6)))
    print()
