"""Selección y anotación explícita del asistente de 50 trazas publicadas en m23-l27."""
import json
from pathlib import Path
import random

# Orden fijado por el muestreo (25 por adaptador, semilla 2828). Etiquetas
# revisadas caso a caso por el asistente; no son anotaciones humanas ni independientes.
LABELS = [
 'final','final','formato','final','herramienta','herramienta','herramienta','final','formato','herramienta',
 'formato','formato','herramienta','formato','final','final','herramienta','herramienta','final','final',
 'final','herramienta','formato','final','formato',
 'formato','herramienta','formato','final','formato','final','observacion','formato','formato','formato',
 'formato','formato','formato','exito','exito','formato','final','formato','formato','formato',
 'formato','exito','final','formato','final',
]
EXPECTED = [
 ('db-06','react',4),('db-09','react',1),('file-00','react',0),('db-02','react',2),('fix-06','react',3),
 ('fix-02','react',2),('fix-04','react',3),('db-01','react',3),('file-13','react',1),('fix-02','react',4),
 ('file-05','react',0),('file-13','react',4),('fix-08','react',0),('file-06','react',3),('db-01','react',4),
 ('db-09','react',0),('fix-04','react',1),('file-03','react',0),('db-00','react',0),('db-14','react',2),
 ('file-02','react',0),('fix-06','react',4),('file-11','react',4),('db-13','react',4),('file-06','react',0),
 ('file-02','una_llamada',0),('fix-06','una_llamada',4),('fix-03','una_llamada',1),('db-06','una_llamada',1),('file-01','una_llamada',3),
 ('db-01','una_llamada',2),('fix-06','una_llamada',1),('fix-01','una_llamada',2),('fix-02','una_llamada',2),('fix-05','una_llamada',4),
 ('file-05','una_llamada',4),('fix-09','una_llamada',0),('file-02','una_llamada',4),('db-14','una_llamada',4),('db-10','una_llamada',2),
 ('file-01','una_llamada',2),('db-09','una_llamada',1),('file-03','una_llamada',2),('file-01','una_llamada',4),('file-03','una_llamada',4),
 ('fix-07','una_llamada',0),('db-06','una_llamada',0),('db-06','una_llamada',4),('file-08','una_llamada',4),('db-02','una_llamada',0),
]


def annotated(rows):
    rng=random.Random(2828); chosen=[]
    for variant in ('react','una_llamada'):
        chosen.extend(rng.sample([r for r in rows if r['variant']==variant],25))
    if [(r['task'],r['variant'],r['attempt']) for r in chosen] != EXPECTED:
        raise ValueError('published trajectory selection changed')
    return [{'task':r['task'],'variant':r['variant'],'attempt':r['attempt'],
             'label':label,'annotation_by':'assistant','success_checker':r['success'],
             'agent':r['agent']} for r,label in zip(chosen,LABELS)]


if __name__=='__main__':
    local=Path(__file__).with_name('agent-runs.json')
    path=local if local.exists() else next((p/'public/data/m23/l27/agent-runs.json' for p in Path(__file__).resolve().parents if (p/'public/data/m23/l27/agent-runs.json').exists()), None)
    if path is None: raise FileNotFoundError('download agent-runs.json alongside this script')
    rows=json.loads(path.read_text(encoding='utf-8'))
    output=annotated(rows)
    print('annotated',len(output),'trajectories by assistant; labels:', {x:sum(r['label']==x for r in output) for x in sorted(set(LABELS))})
