import json, urllib.request, collections
BASE={"temperature":0.7,"top_p":0.8,"presence_penalty":1.5,"top_k":20,"min_p":0.0,"repetition_penalty":1.0}
PROMPTS=[
 ("math","A farmer has 17 sheep. All but 9 run away. He buys twice as many as he has left, then sells 4. How many now? Explain."),
 ("tech","Compare NVMe raidz2 vs mirrored vdevs for a write-heavy Postgres workload. Which would you pick and why?"),
 ("prose","Write a short scene: two engineers argue about shipping a known-flaky feature."),
 ("openq","Why did the Bronze Age collapse happen? Give your best reasoning."),
]
CONVO=[{"role":"user","content":"Explain what a heat pump does."},
       {"role":"assistant","content":"A heat pump moves heat rather than generating it, using a refrigerant cycle to pull warmth from outside air into a house."},
       {"role":"user","content":"Why does its efficiency drop when it gets very cold out? Think it through."}]
def run(label, ctk, n=6):
    t=collections.Counter()
    for lbl,p in PROMPTS:
        for i in range(n):
            body={"model":"qwen3.8-27b-uncensored","messages":[{"role":"user","content":p}],
                  "max_tokens":3072,"chat_template_kwargs":ctk}; body.update(BASE)
            req=urllib.request.Request("http://10.250.50.54:8015/v1/chat/completions",
                data=json.dumps(body).encode(),headers={"Content-Type":"application/json"})
            d=json.loads(urllib.request.urlopen(req,timeout=300).read().decode(),strict=False)
            c=d["choices"][0]; cont=c["message"].get("content") or ""
            t["n"]+=1
            if "<think>" in cont: t["LEAK"]+=1; t["leak_"+lbl]+=1
            if not cont.strip(): t["EMPTY"]+=1
            if c.get("finish_reason")!="stop": t["finish_"+str(c.get("finish_reason"))]+=1
    # multi-turn
    for i in range(n):
        body={"model":"qwen3.8-27b-uncensored","messages":CONVO,"max_tokens":3072,"chat_template_kwargs":ctk}
        body.update(BASE)
        req=urllib.request.Request("http://10.250.50.54:8015/v1/chat/completions",
            data=json.dumps(body).encode(),headers={"Content-Type":"application/json"})
        d=json.loads(urllib.request.urlopen(req,timeout=300).read().decode(),strict=False)
        c=d["choices"][0]; cont=c["message"].get("content") or ""
        t["n"]+=1; t["mt"]+=1
        if "<think>" in cont: t["LEAK"]+=1; t["leak_multiturn"]+=1
        if not cont.strip(): t["EMPTY"]+=1
    extra=" ".join(f"{k}={v}" for k,v in sorted(t.items()) if k.startswith(("leak_","finish_")))
    print(f"  {label:<40} n={t['n']:<3} LEAK={t.get('LEAK',0):<3} EMPTY={t.get('EMPTY',0)}  {extra}")
run("CURRENT: enable_thinking=false", {"enable_thinking":False})
run("FIX B:   thinking=true, effort=low", {"enable_thinking":True,"reasoning_effort":"low"})
