#!/usr/bin/env python3
"""Sends each prompt of a fixture through four answer routes N times, round by round, so a tracker can see
how much a brand list or citation list moves between identical calls: ChatGPT web, Google AI Mode and Gemini
(Cloro interface routes) and Perplexity's own answer API (fast preset). One raw response per call, resumable. Polls the Treg ledger every 30 s because the CLI caps a read
at the last 200 rows, and stops starting calls once the balance has fallen by the cap. Deterministic; no LLM.
Usage: scripts/run-ai-answer-repeats.py evidence/fixtures/ai-answer-repeats-v1.json <out-dir> [--runs N] [--prompts P21,P23] [--cap 4.5]
<out-dir>/raw/ holds raw responses and <out-dir>/raw/ledger.json the ledger entries for the window."""
import json, sys, os, subprocess, time, threading, argparse, concurrent.futures as cf
a = argparse.ArgumentParser(); a.add_argument('fixture'); a.add_argument('out'); a.add_argument('--runs', type=int); a.add_argument('--prompts'); a.add_argument('--cap', type=float, default=4.5); a.add_argument('--workers', type=int, default=3)
args = a.parse_args()
fx = json.load(open(args.fixture)); raw = os.path.join(args.out, 'raw'); os.makedirs(raw, exist_ok=True)
runs = args.runs or fx['runs']; only = set(args.prompts.split(',')) if args.prompts else None
prompts = [p for p in fx['prompts'] if not only or p['id'] in only]
ROUTES = {
    'chatgpt-web': lambda q: ('cloro.ai-search.chatgpt.scrape', {'prompt': q, 'country': 'US'}),
    'aimode': lambda q: ('cloro.google.serp.ai_mode', {'prompt': q, 'gl': 'US'}),
    'gemini': lambda q: ('cloro.ai-search.gemini.scrape', {'prompt': q, 'country': 'US'}),
    'perplexity-api': lambda q: ('perplexity.web.answer.fast', {'preset': 'fast', 'input': q, 'max_steps': 1, 'max_output_tokens': 400}),
}
def treg(argv, t):
    try:
        p = subprocess.run(['treg', *argv], capture_output=True, text=True, timeout=t)
        return p.returncode, p.stdout if p.stdout.strip() else p.stderr
    except subprocess.TimeoutExpired:
        return 124, json.dumps({'_timeout': t})
def balance():
    rc, s = treg(['balance', '--json'], 60)
    try: return json.loads(s)['balance_usd']
    except Exception: return None
stop = threading.Event(); done_polling = threading.Event(); entries = {}; b0 = balance(); t_start = time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime())
led = os.path.join(raw, 'ledger.json')
if os.path.exists(led):
    prev = json.load(open(led)); entries = {e['id']: e for e in prev['entries']}; b0 = prev['balance_before']; t_start = prev['started']
print('balance before', b0, t_start, flush=True)
def save(b1=None):
    json.dump({'started': t_start, 'balance_before': b0, 'balance_after': b1, 'ended': time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime()), 'entries': sorted(entries.values(), key=lambda e: e.get('created_at', ''))}, open(led, 'w'), indent=1)
def poll():
    while not done_polling.is_set():
        rc, s = treg(['balance', '--limit', '200', '--json'], 90)
        try:
            d = json.loads(s)
            for e in (d.get('entries') or {}).get('items') or []: entries[e['id']] = e
            if d.get('balance_usd') is not None and b0 is not None and b0 - d['balance_usd'] > args.cap:
                print('cap reached, stopping new calls', d['balance_usd'], flush=True); stop.set()
            save()
        except Exception as ex: print('poll error', str(ex)[:120], flush=True)
        done_polling.wait(30)
threading.Thread(target=poll, daemon=True).start()
def call(job):
    route, pid, q, n = job; label = f'{route}-{pid}-r{n:02d}'; f = os.path.join(raw, label + '.json')
    if os.path.exists(f) or stop.is_set(): return
    slug, body = ROUTES[route](q); t0 = time.time()
    rc, s = treg(['call', slug, '--method', 'POST', '--data', json.dumps(body)], 240)
    try: resp = json.loads(s)
    except Exception: resp = {'_nonjson': s[:2000]}
    json.dump({'_run': {'route': route, 'id': pid, 'run': n, 'label': label, 'latency_ms': int((time.time() - t0) * 1000), 'exit': rc, 'ts': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())}, 'response': resp}, open(f, 'w'))
    print(label, int(time.time() - t0), 's exit', rc, flush=True)
def run_route(route):
    # one route, a small pool, round by round: every prompt's run 1 before any prompt's run 2
    jobs = [(route, p['id'], p['prompt'], n) for n in range(1, runs + 1) for p in prompts]
    with cf.ThreadPoolExecutor(max_workers=args.workers) as ex: list(ex.map(call, jobs))
with cf.ThreadPoolExecutor(max_workers=len(ROUTES)) as ex: list(ex.map(run_route, ROUTES))
time.sleep(35); done_polling.set(); b1 = balance(); save(b1); print('balance after', b1, 'done', flush=True)
