File size: 15,786 Bytes
473bd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80065ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473bd03
 
 
 
 
80065ea
 
 
 
 
 
473bd03
80065ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473bd03
 
 
80065ea
473bd03
 
 
 
80065ea
473bd03
 
 
 
80065ea
473bd03
 
80065ea
 
473bd03
 
 
 
 
 
 
 
80065ea
 
473bd03
 
 
 
 
 
 
 
80065ea
 
473bd03
 
80065ea
473bd03
80065ea
473bd03
 
80065ea
473bd03
80065ea
473bd03
 
80065ea
 
 
 
 
 
 
473bd03
 
80065ea
473bd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80065ea
 
473bd03
 
 
 
 
 
 
 
 
 
 
 
80065ea
 
473bd03
 
80065ea
473bd03
 
 
 
 
 
80065ea
 
 
 
 
 
 
473bd03
 
 
 
 
 
 
80065ea
 
 
 
 
 
 
 
 
 
473bd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""backend/api/benchmark_handler.py β€” Gestore benchmark via Telegram.

Versione v7 (GAP-BENCH-2): usa benchmark-extended.mjs (comprehensive, 2319 righe)
con flag --json. Mantiene compatibilitΓ  backward con report v6.2-stress per /riepilogo.

Sprint history:
  v6.2 β€” benchmark-ultra-v6.2-stress.mjs (208 righe, stress+Groq judge)
  v7   β€” benchmark-extended.mjs (2319 righe, 10+ categorie, HF datasets,
          ref vs Replit/Cursor/Devin/Manus)

Fix invarianti portati da v6.2:
  - FIX-1: process.kill() + wait() su TimeoutError β€” nessun zombie su Railway.
  - FIX-2: _load_gap_map() usa importlib isolato β€” nessuna sys.path pollution.
  - FIX-3: I/O su file in asyncio.to_thread β€” non blocca l'event loop.
"""
from __future__ import annotations
import asyncio, importlib, importlib.util, json, logging, os
from typing import Any

logger = logging.getLogger("agente_ai.benchmark_handler")

# ── Percorsi server Railway ────────────────────────────────────────────────────
# Lo Space HF esegue il backend in /app; Railway puΓ² impostare REPO_ROOT.
_REPO_ROOT     = os.getenv("REPO_ROOT", "/app")

# Extended v5: 20 categorie. Gli Space possono montare il repository in
# /home/user/app anche quando il Dockerfile dichiara WORKDIR=/app.
_BENCH_SCRIPT_CANDIDATES = (
    os.getenv("BENCHMARK_RUNNER_PATH", "").strip(),
    os.path.join(_REPO_ROOT, "benchmark-extended.mjs"),
    "/home/user/app/benchmark-extended.mjs",
    "/app/benchmark-extended.mjs",
)
_BENCH_SCRIPT = next(
    (candidate for candidate in _BENCH_SCRIPT_CANDIDATES if candidate and os.path.isfile(candidate)),
    os.path.join(_REPO_ROOT, "benchmark-extended.mjs"),
)
_REPORT_V7     = "/tmp/agente-ai/benchmark-v5-latest.json"
_REPORT_V7_WEAK = "/tmp/agente-ai/benchmark-v5-weak-latest.json"
_WEAK_CATEGORIES = (
    "sql", "context_window", "reasoning", "data_analysis", "research_synthesis",
    "mmlu", "technical_writing", "code_correct", "feature", "security",
)
# 20 task seriali possono richiedere piΓΉ di 12 minuti con provider gratuiti.
_BENCH_TIMEOUT = float(os.getenv("BENCH_TIMEOUT_SECS", "3600"))

# v6.2 β€” usato come fallback in get_smart_summary per compatibilitΓ 
_REPORT_V6     = os.path.join(_REPO_ROOT, "benchmark-stress-report.json")


async def run_benchmark_task(chat_id: int, send_reply_fn, mode: str = "full") -> None:
    """Esegue il benchmark Extended v5 su tutte le 20 categorie via API task moderna."""
    if not await asyncio.to_thread(os.path.isfile, _BENCH_SCRIPT):
        await send_reply_fn(chat_id, "❌ <b>Runner benchmark esteso non disponibile.</b>\n"
                            "Il deployment non ha incluso <code>benchmark-extended.mjs</code>.")
        return

    is_weak_run = mode == "weak"
    if is_weak_run:
        report_path = _REPORT_V7_WEAK
        flags = [
            f"--categories={','.join(_WEAK_CATEGORIES)}", "--json",
            f"--output={report_path}", "--gap-analysis",
        ]
        await send_reply_fn(
            chat_id,
            "🎯 <b>Benchmark Extended v5 mirato avviato</b>\n"
            "<i>10 categorie piΓΉ deboli della baseline 39,1 Β· seed 1337 Β· task API moderna.</i>",
        )
    else:
        report_path = _REPORT_V7
        flags = ["--full", "--json", f"--output={report_path}", "--gap-analysis"]
        await send_reply_fn(
            chat_id,
            "πŸš€ <b>Benchmark Extended v5 avviato</b>\n"
            "<i>20/20 categorie Β· seed 1337 Β· task API moderna Β· durata variabile fino a ~60 min.</i>",
        )
    env = {
        **os.environ,
        "INTERNAL_TOKEN": os.getenv("INTERNAL_TOKEN", ""),
        "BENCHMARK_BASE_URL": os.getenv("BENCHMARK_BASE_URL", "http://127.0.0.1:7860"),
    }
    process: asyncio.subprocess.Process | None = None
    try:
        process = await asyncio.create_subprocess_exec(
            "node", _BENCH_SCRIPT, *flags,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
            env=env,
        )
        _stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=_BENCH_TIMEOUT)
        if process.returncode != 0:
            err = stderr.decode(errors="replace")[:400]
            logger.error("Extended benchmark failed rc=%d: %s", process.returncode, err)
            await send_reply_fn(chat_id, f"❌ <b>Errore benchmark Extended:</b>\n<code>{err}</code>")
            return
    except asyncio.TimeoutError:
        if process is not None:
            try:
                process.kill()
                await process.wait()
            except Exception:
                pass
        logger.warning("Extended benchmark timeout (>%.0fs) β€” process killed", _BENCH_TIMEOUT)
        await send_reply_fn(chat_id, f"⏱ <b>Timeout benchmark Extended</b> (>{int(_BENCH_TIMEOUT // 60)} min) β€” processo terminato.")
        return
    except Exception as exc:
        if process is not None:
            try:
                process.kill()
                await process.wait()
            except Exception:
                pass
        logger.exception("run_benchmark_task extended error")
        await send_reply_fn(chat_id, f"πŸ’₯ <b>Errore critico benchmark:</b> <code>{exc}</code>")
        return

    report_exists = await asyncio.to_thread(os.path.exists, report_path)
    if not report_exists:
        await send_reply_fn(chat_id, "⚠️ <b>Benchmark Extended terminato ma report non trovato.</b>")
        return
    try:
        report: dict[str, Any] = await asyncio.to_thread(_read_json, report_path)
    except Exception as exc:
        await send_reply_fn(chat_id, f"⚠️ <b>Report Extended non leggibile:</b> <code>{exc}</code>")
        return

    categories = {str(task.get("cat", "")) for task in report.get("tasks", []) if task.get("cat")}
    expected_categories = len(_WEAK_CATEGORIES) if is_weak_run else 20
    if len(categories) != expected_categories:
        await send_reply_fn(chat_id, f"⚠️ <b>Run incompleta:</b> <code>{len(categories)}/{expected_categories}</code> categorie nel report."
                            " Nessun risultato incompleto viene presentato come benchmark completo.")
        return
    await send_reply_fn(chat_id, _format_v7_report(report, expected_categories=expected_categories, run_label="mirato Β· categorie deboli" if is_weak_run else None))


def _format_v7_report(report: dict[str, Any], *, expected_categories: int = 20, run_label: str | None = None) -> str:
    """Formatta il report v7 per Telegram HTML."""
    s   = report.get("summary", {})
    ts  = (report.get("timestamp") or "")[:16].replace("T", " ")
    ver = report.get("version", "extended-v7")

    avg   = s.get("avgScore",  "N/A")
    repl  = s.get("avgReplit", "N/A")
    curs  = s.get("avgCursor", "N/A")
    devi  = s.get("avgDevin",  "N/A")
    manu  = s.get("avgManus",  "N/A")
    gaps  = s.get("gapCount",  0)
    verd  = s.get("verdict",   "")
    canary = s.get("canaryLeaks", 0)

    lines: list[str] = [
        f"πŸ† <b>Benchmark {ver} completato!</b>\n\n"
        f"πŸ“Š <b>Score agente:</b> <code>{avg}/100</code>\n"
        f"πŸ“… <b>Run:</b> <code>{ts}</code>\n"
        + (f"🎯 <b>Modalità:</b> <code>{run_label}</code>\n" if run_label else "") + "\n"
        "πŸ“ˆ <b>Confronto vs riferimenti:</b>\n"
        f"  β€’ Replit:  <code>{repl}/100</code>\n"
        f"  β€’ Cursor:  <code>{curs}/100</code>\n"
        f"  β€’ Devin:   <code>{devi}/100</code>\n"
        f"  β€’ Manus:   <code>{manu}/100</code>\n"
    ]

    if verd:
        lines.append(f"\nπŸ“ <b>Verdetto:</b> {verd}\n")
    if canary:
        lines.append(f"⚠️ <b>Canary leak:</b> {canary} task\n")

    # Score per categoria. Una categoria in timeout resta tentata ma non entra
    # nella media: non va trasformata silenziosamente in uno score pari a zero.
    tasks = report.get("tasks", [])
    if tasks:
        attempted_categories = {str(t.get("cat")) for t in tasks if t.get("cat")}
        by_cat: dict[str, list[float]] = {}
        for t in tasks:
            cat = t.get("cat", "?")
            sc  = t.get("score")
            if isinstance(sc, (int, float)):
                by_cat.setdefault(cat, []).append(float(sc))
        attempted = s.get("attemptedTaskCount", len(tasks))
        scored = s.get("scoredTaskCount", sum(len(v) for v in by_cat.values()))
        skipped = s.get("skippedTaskCount", max(0, attempted - scored))
        lines.append(
            f"πŸ§ͺ <b>Copertura:</b> <code>{len(attempted_categories)}/{expected_categories} categorie tentate Β· "
            f"{scored} valutabili Β· {skipped} non valutabili</code>\n"
        )
        if by_cat:
            lines.append("\nπŸ“‚ <b>Per categoria:</b>\n")
            for cat, scores in sorted(by_cat.items()):
                avg_cat = sum(scores) / len(scores)
                icon = "🟒" if avg_cat >= 70 else "🟑" if avg_cat >= 50 else "πŸ”΄"
                lines.append(f"  {icon} <code>{avg_cat:5.1f}</code>  {cat}\n")

    failures = report.get("taskFailures", [])
    if failures:
        lines.append("\n⚠️ <b>Categorie non valutabili:</b>\n")
        for failure in failures[:3]:
            cat = failure.get("cat", "?")
            reason = str(failure.get("reason", "errore non specificato"))[:100]
            lines.append(f"  β€’ <code>{cat}</code> β€” {reason}\n")
        if len(failures) > 3:
            lines.append(f"  <i>...e altre {len(failures) - 3}.</i>\n")

    # Gap cards (prime 3)
    gap_cards = report.get("gapCards", [])
    if gap_cards:
        lines.append(f"\nπŸ’‘ <b>Gap ({gaps} totali):</b>\n")
        for gc in gap_cards[:3]:
            gid   = gc.get("id",  "?")
            gtit  = gc.get("title", gc.get("name", ""))
            gsev  = gc.get("severity", "")
            lines.append(f"  β€’ <code>{gid}</code> {gtit}" + (f" [{gsev}]" if gsev else "") + "\n")
        if gaps > 3:
            lines.append(f"  <i>...e altri {gaps - 3} gap.</i>\n")

    lines.append("\nπŸ” <i>Usa /riepilogo per analisi approfondita.</i>")
    return "".join(lines)


# ── Helpers ───────────────────────────────────────────────────────────────────

def _read_json(path: str) -> dict[str, Any]:
    """Lettura JSON sincrona β€” da eseguire sempre in asyncio.to_thread."""
    with open(path, encoding="utf-8") as f:
        return json.load(f)


def _load_gap_map() -> tuple[list, list]:
    """Importa GAPS e CATS da scripts/gap_map.py senza inquinare sys.path.

    FIX-2: usa importlib.util.spec_from_file_location per un import isolato.
    """
    gap_map_path = os.path.join(_REPO_ROOT, "scripts", "gap_map.py")
    if not os.path.exists(gap_map_path):
        logger.debug("gap_map.py not found at %s", gap_map_path)
        return [], []
    try:
        spec = importlib.util.spec_from_file_location("_gap_map_isolated", gap_map_path)
        if spec is None or spec.loader is None:
            return [], []
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)  # type: ignore[union-attr]
        return getattr(mod, "GAPS", []), getattr(mod, "CATS", [])
    except Exception as exc:
        logger.warning("_load_gap_map failed: %s", exc)
        return [], []


async def get_smart_summary(chat_id: int) -> str:
    """Genera riepilogo strutturato: stato sistema + ultimo score + gap priority.

    GAP-BENCH-2: prova prima report v7, fallback a v6.2 per compatibilitΓ .
    """
    GAPS, _ = await asyncio.to_thread(_load_gap_map)
    lines: list[str] = ["πŸ“‹ <b>Briefing Assistente Proattivo</b>\n\n"]
    lines.append("🟒 <b>Stato Sistema:</b> Railway operativo\n")

    # Prova v7 per prima
    v7_exists = await asyncio.to_thread(os.path.exists, _REPORT_V7)
    v6_exists = await asyncio.to_thread(os.path.exists, _REPORT_V6)

    if v7_exists:
        try:
            report: dict[str, Any] = await asyncio.to_thread(_read_json, _REPORT_V7)
            s   = report.get("summary", {})
            avg = s.get("avgScore", "N/A")
            ts  = (report.get("timestamp") or "")[:16].replace("T", " ")
            ver = report.get("version", "v7")
            lines.append(f"πŸ“Š <b>Ultimo Score ({ver}):</b> <code>{avg}/100</code>\n")
            lines.append(f"πŸ“… <b>Run:</b> <code>{ts}</code>\n\n")

            # Categorie con score < 50 β†’ alimenta gap priority
            failed_cats: set[str] = set()
            for t in report.get("tasks", []):
                sc = t.get("score")
                if isinstance(sc, (int, float)) and sc < 50:
                    failed_cats.add(t.get("cat", "").lower())

            if failed_cats and GAPS:
                lines.append("πŸ’‘ <b>Aree prioritarie:</b>\n")
                for gap in GAPS:
                    if any(c in failed_cats for c in gap.get("categories", [])):
                        slug = (
                            f"feature/{gap['id'].lower()}"
                            f"-{gap['name'].lower().replace(' ', '-')}"
                        )
                        lines.append(f"  β€’ <code>{gap['name']}</code> β†’ <code>{slug}</code>\n")
            else:
                lines.append("✨ <b>Nessun gap critico nell'ultimo run.</b>\n")
        except Exception as exc:
            logger.warning("get_smart_summary v7 error: %s", exc)
            lines.append("⚠️ Errore lettura report v7 β€” esegui /bench per aggiornare.\n")

    elif v6_exists:
        # Fallback v6.2 β€” compatibilitΓ 
        try:
            report = await asyncio.to_thread(_read_json, _REPORT_V6)
            score = report.get("finalScore", "N/A")
            ts    = (report.get("timestamp") or "")[:16].replace("T", " ")
            judge = report.get("judge", "heuristic")
            lines.append(f"πŸ“Š <b>Ultimo Score (v6.2):</b> <code>{score}/100</code> [{judge}]\n")
            lines.append(f"πŸ“… <b>Run:</b> <code>{ts}</code>\n\n")
            failed_cats: set[str] = set()
            for res in report.get("results", []):
                if res.get("score", {}).get("total", 100) < 50:
                    rid = res.get("id", "")
                    if rid.startswith("STRESS_AMB"): failed_cats.add("recovery")
                    if rid.startswith("STRESS_REC"): failed_cats.add("recovery")
                    if rid.startswith("STRESS_MEM"): failed_cats.add("memory_context")
                    if rid.startswith("WEB"):        failed_cats.add("orchestration")
                    if rid.startswith("CODE"):       failed_cats.add("bug_fix")
                    if rid.startswith("REASON"):     failed_cats.add("reasoning")
            if failed_cats and GAPS:
                lines.append("πŸ’‘ <b>Aree prioritarie:</b>\n")
                for gap in GAPS:
                    if any(c in failed_cats for c in gap.get("categories", [])):
                        slug = (
                            f"feature/{gap['id'].lower()}"
                            f"-{gap['name'].lower().replace(' ', '-')}"
                        )
                        lines.append(f"  β€’ <code>{gap['name']}</code> β†’ <code>{slug}</code>\n")
            else:
                lines.append("✨ <b>Nessun gap critico nell'ultimo run.</b>\n")
        except Exception as exc:
            logger.warning("get_smart_summary v6 error: %s", exc)
            lines.append("⚠️ Errore lettura report v6.2 β€” esegui /bench per aggiornare.\n")
    else:
        lines.append("πŸ“Š <b>Nessun report</b> β€” esegui <code>/bench</code> per generare dati.\n")

    lines.append("\nπŸš€ <b>Task in corso:</b> controlla /status per stato live.")
    return "".join(lines)