File size: 12,984 Bytes
a2d6a0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python3
# SPDX-License-Identifier: BUSL-1.1
"""

test_inference_integration.py — Prueba fehaciente del servidor pampar.inference.



Lanza el servidor como subprocess real, carga el checkpoint, y verifica que:

  1. El proceso arranca y emite READY

  2. Responde a una petición de inferencia con código Python real

  3. El código generado al menos tiene sintaxis válida

  4. Responde a una petición de boot con AGENTS.md bien formado

  5. Maneja JSON inválido sin caer



Uso:

  python scripts/test_inference_integration.py

  python scripts/test_inference_integration.py --checkpoint checkpoints/v3_sft_v8.pt

  python scripts/test_inference_integration.py --device cpu --timeout 60

"""

from __future__ import annotations

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

# ---------------------------------------------------------------------------

PROJECT_ROOT = Path(__file__).parent.parent
DEFAULT_CHECKPOINT = PROJECT_ROOT / "checkpoints" / "v3_sft_v8.pt"
VENV_PYTHON = PROJECT_ROOT.parent / ".venv" / "Scripts" / "python.exe"


def _python_has_torch(python_path: str) -> bool:
    """Verifica rápido si ese intérprete tiene torch disponible."""
    try:
        result = subprocess.run(
            [python_path, "-c", "import torch"],
            capture_output=True, timeout=10,
        )
        return result.returncode == 0
    except Exception:
        return False


def find_python() -> str:
    """Devuelve el primer intérprete Python que tenga torch instalado."""
    candidates = [
        str(VENV_PYTHON) if VENV_PYTHON.exists() else None,
        sys.executable,
    ]
    for candidate in candidates:
        if candidate and _python_has_torch(candidate):
            return candidate
    # Último recurso: sys.executable aunque no tenga torch (el servidor fallará con mensaje claro)
    return sys.executable


# ---------------------------------------------------------------------------
# Color helpers
# ---------------------------------------------------------------------------

def ok(msg: str) -> None:
    print(f"  ✅  {msg}")

def fail(msg: str) -> None:
    print(f"  ❌  {msg}")

def section(title: str) -> None:
    print(f"\n{'─'*50}")
    print(f"  {title}")
    print(f"{'─'*50}")


# ---------------------------------------------------------------------------
# Server wrapper
# ---------------------------------------------------------------------------

class InferenceServer:
    """Prozess-wrapper para pampar.inference."""

    def __init__(self, checkpoint: str, device: str, timeout_ready: int = 90):
        python = find_python()
        cmd = [python, "-m", "pampar.inference", "--checkpoint", checkpoint, "--device", device]
        import os as _os
        _env = _os.environ.copy()
        _env["PYTHONIOENCODING"] = "utf-8"
        self.proc = subprocess.Popen(
            cmd,
            cwd=str(PROJECT_ROOT),
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            encoding="utf-8",
            bufsize=1,
            env=_env,
        )
        self._wait_ready(timeout_ready)

    def _wait_ready(self, timeout: int) -> None:
        print(f"  Esperando READY (timeout={timeout}s)…", end="", flush=True)
        deadline = time.time() + timeout
        stderr_lines: list[str] = []
        while time.time() < deadline:
            line = self.proc.stderr.readline()
            if not line:
                if self.proc.poll() is not None:
                    raise RuntimeError(
                        f"Proceso terminó antes de READY (código {self.proc.returncode}).\n"
                        + "".join(stderr_lines)
                    )
                time.sleep(0.1)
                continue
            stderr_lines.append(line)
            print(".", end="", flush=True)
            if "READY" in line:
                print(" listo.")
                # Leer también la línea {"type":"ready"} de stdout
                self.proc.stdout.readline()
                return
        raise TimeoutError("El servidor no emitió READY a tiempo.\n" + "".join(stderr_lines[-20:]))

    def send(self, msg: dict) -> dict:
        line = json.dumps(msg, ensure_ascii=False) + "\n"
        self.proc.stdin.write(line)
        self.proc.stdin.flush()
        resp_line = self.proc.stdout.readline()
        if not resp_line:
            raise EOFError("El servidor cerró stdout inesperadamente.")
        return json.loads(resp_line)

    def send_raw(self, raw: str) -> str:
        self.proc.stdin.write(raw + "\n")
        self.proc.stdin.flush()
        return self.proc.stdout.readline()

    def close(self) -> None:
        try:
            self.proc.stdin.close()
        except Exception:
            pass
        self.proc.wait(timeout=5)


# ---------------------------------------------------------------------------
# Casos de prueba
# ---------------------------------------------------------------------------

PRUEBA_INFER = {
    "type": "infer",
    "prompt": (
        "### Problem:\n"
        "Write a Python function `suma(a, b)` that returns the sum of two numbers.\n"
        "### Solution:\n"
    ),
    "max_tokens": 80,
    "temperature": 0.1,
}

PRUEBA_INFER_ALGO_REAL = {
    "type": "infer",
    "prompt": (
        "### Problem:\n"
        "Write a Python function `es_par(n)` that returns True if n is even, False otherwise.\n"
        "### Solution:\n"
    ),
    "max_tokens": 256,
    "temperature": 0.1,
}


def run_tests(checkpoint: str, device: str, timeout: int) -> int:
    """Ejecuta todos los tests. Devuelve número de fallos."""
    fallos = 0

    section("Iniciando servidor de inferencia")
    try:
        server = InferenceServer(checkpoint=checkpoint, device=device, timeout_ready=timeout)
    except Exception as exc:
        fail(f"No se pudo iniciar el servidor: {exc}")
        return 1

    # ------------------------------------------------------------------
    # Test 1: responde a inferencia básica
    # ------------------------------------------------------------------
    section("Test 1: Respuesta a petición de inferencia")
    try:
        resp = server.send(PRUEBA_INFER)
        if resp.get("type") == "infer_ok":
            texto = resp.get("text", "").strip()
            if texto:
                ok(f"Texto generado ({len(texto)} chars): {repr(texto[:80])}")
            else:
                fail("infer_ok pero text está vacío")
                fallos += 1
        else:
            fail(f"Respuesta inesperada: {resp}")
            fallos += 1
    except Exception as exc:
        fail(f"Excepción: {exc}")
        fallos += 1

    # ------------------------------------------------------------------
    # Test 2: sintaxis válida en el código generado
    # ------------------------------------------------------------------
    section("Test 2: Código generado tiene sintaxis Python válida")
    try:
        resp = server.send(PRUEBA_INFER_ALGO_REAL)
        texto = resp.get("text", "")
        # Limpiamos markdown y truncamos en stop markers
        texto_limpio = texto.replace("```python", "").replace("```", "").strip()
        if "###" in texto_limpio:
            texto_limpio = texto_limpio[:texto_limpio.index("###")].rstrip()
        # Intentar parsear progresivamente — quitar líneas del final hasta que sea válido
        lines = texto_limpio.split("\n")
        parsed = False
        while lines:
            candidate = "\n".join(lines).rstrip()
            if candidate:
                try:
                    ast.parse(candidate)
                    texto_limpio = candidate
                    parsed = True
                    break
                except SyntaxError:
                    lines.pop()
            else:
                break
        if parsed:
            ok(f"Sintaxis válida: {repr(texto_limpio[:100])}")
        else:
            try:
                ast.parse(texto_limpio)
                ok(f"Sintaxis válida: {repr(texto_limpio[:100])}")
            except SyntaxError as se:
                fail(f"SyntaxError en código generado: {se}\nCódigo: {repr(texto_limpio[:200])}")
                fallos += 1
    except Exception as exc:
        fail(f"Excepción: {exc}")
        fallos += 1

    # ------------------------------------------------------------------
    # Test 3: boot genera AGENTS.md
    # ------------------------------------------------------------------
    section("Test 3: Boot genera AGENTS.md")
    try:
        resp = server.send({"type": "boot", "workspace": str(PROJECT_ROOT)})
        if resp.get("type") == "boot_ok":
            md = resp.get("agents_md", "")
            checks = [
                ("## Quick Reference" in md, "Sección Quick Reference"),
                ("## Boot protocol"    in md, "Sección Boot protocol"),
                (len(md) > 200,              "Contenido suficiente (>200 chars)"),
            ]
            for passed, label in checks:
                if passed:
                    ok(label)
                else:
                    fail(label)
                    fallos += 1
        else:
            fail(f"Respuesta inesperada: {resp}")
            fallos += 1
    except Exception as exc:
        fail(f"Excepción: {exc}")
        fallos += 1

    # ------------------------------------------------------------------
    # Test 4: prompt vacío → error (no crash)
    # ------------------------------------------------------------------
    section("Test 4: Prompt vacío → error controlado")
    try:
        resp = server.send({"type": "infer", "prompt": ""})
        if resp.get("type") == "error":
            ok(f"Error controlado: {resp.get('message', '')[:60]}")
        else:
            fail(f"Se esperaba error, se recibió: {resp}")
            fallos += 1
    except Exception as exc:
        fail(f"Excepción: {exc}")
        fallos += 1

    # ------------------------------------------------------------------
    # Test 5: JSON inválido no baja el servidor
    # ------------------------------------------------------------------
    section("Test 5: JSON inválido no mata el servidor")
    try:
        raw = server.send_raw("esto no es json {{{{")
        if raw.strip():
            resp = json.loads(raw)
            if resp.get("type") == "error":
                ok("El servidor respondió error y sigue vivo")
            else:
                ok(f"El servidor respondió (tipo={resp.get('type')}) y sigue vivo")
        else:
            fail("No hubo respuesta al JSON inválido")
            fallos += 1
        # Verificar que el servidor sigue respondiendo
        resp2 = server.send({"type": "infer", "prompt": "### Problem:\nhi\n### Solution:\n", "max_tokens": 20})
        if resp2.get("type") == "infer_ok":
            ok("Servidor sigue respondiendo después del JSON inválido")
        else:
            fail(f"El servidor dejó de responder: {resp2}")
            fallos += 1
    except Exception as exc:
        fail(f"Excepción: {exc}")
        fallos += 1

    # ------------------------------------------------------------------
    # Resumen
    # ------------------------------------------------------------------
    server.close()
    section("RESUMEN")
    if fallos == 0:
        print(f"  ✅  TODOS LOS TESTS PASARON (5/5)")
    else:
        print(f"  ❌  {fallos} test(s) fallaron")

    return fallos


# ---------------------------------------------------------------------------

def main() -> None:
    parser = argparse.ArgumentParser(description="Test de integración del servidor pampar.inference")
    parser.add_argument(
        "--checkpoint",
        default=str(DEFAULT_CHECKPOINT),
        help=f"Ruta al checkpoint .pt (default: {DEFAULT_CHECKPOINT})",
    )
    parser.add_argument(
        "--device", default="auto", choices=["auto", "cpu", "cuda"],
        help="Dispositivo de inferencia"
    )
    parser.add_argument(
        "--timeout", type=int, default=90,
        help="Segundos máximos para esperar READY del servidor"
    )
    args = parser.parse_args()

    if not Path(args.checkpoint).exists():
        print(f"ERROR: Checkpoint no encontrado: {args.checkpoint}")
        print(f"       Checkpoints disponibles:")
        for pt in sorted((PROJECT_ROOT / "checkpoints").glob("*.pt")):
            print(f"         {pt.name}")
        sys.exit(1)

    fallos = run_tests(args.checkpoint, args.device, args.timeout)
    sys.exit(0 if fallos == 0 else 1)


if __name__ == "__main__":
    main()