File size: 3,506 Bytes
111ee96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Relanza en orden los lotes que llaman a Claude, reanudando donde quedaron, y tras cada lote
regenera REVISAR.md, RESULTADOS.md y las cifras del README y hace commit.

Si un lote se detiene por acceso o límite de uso ("ABORTADO" o "LOTE DETENIDO" en su salida),
la cadena para ahí: no se reintenta en bucle. Relanzar este mismo script más tarde continúa.

    .venv/bin/python scripts/relanzar.py [--trabajadores 3]
"""
from __future__ import annotations

import argparse
import subprocess
import sys
import time
from pathlib import Path

RAIZ = Path(__file__).resolve().parent.parent
PY = str(RAIZ / ".venv/bin/python")


def correr(nombre: str, cmd: list, log) -> str:
    print(f"\n===== {nombre} =====  {time.strftime('%H:%M:%S')}", flush=True)
    log.write(f"\n===== {nombre} ===== {time.strftime('%Y-%m-%d %H:%M:%S')}\n{' '.join(cmd)}\n"); log.flush()
    proc = subprocess.run(cmd, cwd=RAIZ, capture_output=True, text=True)
    salida = (proc.stdout or "") + "\n" + (proc.stderr or "")
    log.write(salida); log.flush()
    for linea in salida.splitlines():
        if any(k in linea for k in ("INFO", "ERROR", "WARNING", "ABORTADO", "nuevos", "n_items")):
            print(linea[:220], flush=True)
    if proc.returncode != 0 or "ABORTADO" in salida or "LOTE DETENIDO" in salida:
        return "detenido"
    return "ok"


def regenerar_y_commit(nombre: str, log) -> None:
    for cmd in ([PY, "scripts/cifras_readme.py"], [PY, "scripts/revisar.py"], [PY, "-m", "licitabench.evaluate", "--tabla"]):
        r = subprocess.run(cmd, cwd=RAIZ, capture_output=True, text=True)
        log.write(f"$ {' '.join(cmd)}\n{r.stdout[-400:]}\n{r.stderr[-400:]}\n")
    subprocess.run(["git", "add", "-A"], cwd=RAIZ, check=False)
    cambios = subprocess.run(["git", "status", "--porcelain"], cwd=RAIZ, capture_output=True, text=True).stdout.strip()
    if cambios:
        subprocess.run(["git", "commit", "-q", "-m",
                        f"Lote {nombre} con la suscripción: resultados, cola y documentos regenerados\n\nCo-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>"],
                       cwd=RAIZ, check=False)
        print(f"commit hecho tras {nombre}", flush=True)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--trabajadores", type=int, default=3)
    a = ap.parse_args()
    w = str(a.trabajadores)
    pasos = [
        ("T1 línea base", [PY, "-m", "licitabench.evaluate", "--tarea", "T1", "--modelo", "claude:claude-opus-5-5",
                           "--n", "100", "--idioma", "es", "--semilla", "42", "--trabajadores", w]),
        ("T2 etiquetado", [PY, "-m", "licitabench.etiquetar", "--tarea", "T2", "--n", "60", "--trabajadores", w]),
        ("T3 etiquetado", [PY, "-m", "licitabench.etiquetar", "--tarea", "T3", "--n", "60", "--trabajadores", w]),
        ("T4 etiquetado", [PY, "-m", "licitabench.etiquetar", "--tarea", "T4", "--n", "30", "--trabajadores", w]),
    ]
    (RAIZ / "results/raw").mkdir(parents=True, exist_ok=True)
    with (RAIZ / "results/raw/relanzar.log").open("a", encoding="utf-8") as log:
        for nombre, cmd in pasos:
            estado = correr(nombre, cmd, log)
            regenerar_y_commit(nombre, log)
            if estado == "detenido":
                print(f"\nCADENA DETENIDA en {nombre}: ver results/raw/relanzar.log. Relanzar este script más tarde continúa donde quedó.", flush=True)
                sys.exit(2)
    print("\nCADENA COMPLETA", flush=True)


if __name__ == "__main__":
    main()