feat(jobs): runtime Python embebido + cadena de fallback (issue 0033 fase B)

Permite distribuir graph_explorer.exe Windows sin dependencia de WSL
ni del .venv del registry. Tambien funciona en Linux como bundle
autocontenido portable.

Cambios:

1. tools/freeze_python_runtime.sh
   - Linux: copia python-build-standalone (uv) ~87 MB,
     elimina marker EXTERNALLY-MANAGED, instala wheels.
   - Windows: descarga python-3.12.7-embed-amd64.zip oficial
     (~12 MB), habilita site-packages, instala wheels via
     pip install --target --platform win_amd64.
   - Idempotente via runtime/.lock con SHA256 del estado.
   - Lee python_runtime_deps del frontmatter de app.md.

2. jobs.cpp::cached_python_runtime() — resolver con cadena:
     1. <exe_dir>/runtime/python/{python.exe|bin/python3}  (embedded)
     2. $FN_PYTHON                                         (env)
     3. <registry_root>/python/.venv/bin/python3           (registry_venv)
     4. python3 del PATH                                   (system)
   Loggea procedencia al iniciar jobs_init.

3. POSIX run_subprocess: usa el runtime resuelto en lugar del
   path hardcodeado.

4. Windows run_subprocess: ramifica por needs_wsl. Si embedded
   o env, lanza Python Windows nativo via CreateProcessW
   directamente (run_path tambien Windows nativo). Solo el
   legacy registry_venv sigue por wsl.exe.

5. app.md: nuevos campos python_runtime: true y
   python_runtime_deps: [requests, certifi, urllib3].

6. .gitignore extendido con runtime/, projects/, _vendored/,
   .vendor.lock, binarios Go de enrichers.

Tests: 26/26 verde — 16 originales + 6 dispatcher fase A + 4
nuevos del resolver fase B (con/sin embed, FN_PYTHON, idempotencia
del freeze script).

Smoke E2E manual: runtime/python/bin/python3 ejecuta web_search
con cwd /tmp y registry_root pasado en ctx, sin tocar el .venv del
registry.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-02 16:51:02 +02:00
parent 2238355f40
commit 30f6f3758f
6 changed files with 521 additions and 27 deletions
+141
View File
@@ -0,0 +1,141 @@
"""Tests del resolver de Python runtime (issue 0033 fase B).
El resolver vive en C++ (jobs.cpp::cached_python_runtime). Como
pytest no puede llamarlo directo, los tests verifican el
comportamiento via efectos observables:
1. La estructura del freeze script y su lock.
2. Que el binario, al arrancar, loggea la procedencia del runtime
(kind=embedded|registry_venv|env|system).
El test del binario solo corre si el .exe esta compilado para Linux
en el path esperado — si no, se skipea.
"""
from __future__ import annotations
import os
import shutil
import subprocess
from pathlib import Path
import pytest
from conftest import REGISTRY_ROOT, APP_DIR_SRC
GRAPH_EXPLORER_BIN = (
REGISTRY_ROOT / "cpp" / "build" / "linux" / "apps"
/ "graph_explorer" / "graph_explorer"
)
def _runtime_log_line(stdout: str) -> str | None:
for line in stdout.splitlines():
if line.startswith("[jobs] python runtime:"):
return line
return None
@pytest.mark.skipif(not GRAPH_EXPLORER_BIN.exists(),
reason="binario graph_explorer no compilado")
def test_resolver_falls_back_to_registry_venv_when_no_embed(tmp_path):
"""Sin runtime/ junto al exe, debe caer a registry_venv."""
# Copiamos el binario a tmp_path (sin runtime/) y lo arrancamos
# desde el repo root (donde existe projects/default).
bin_copy = tmp_path / "graph_explorer"
shutil.copy(GRAPH_EXPLORER_BIN, bin_copy)
bin_copy.chmod(0o755)
proc = subprocess.run(
[str(bin_copy)],
cwd=str(REGISTRY_ROOT / "cpp"),
capture_output=True, text=True, timeout=4,
env={**os.environ, "DISPLAY": ""}, # forzamos GLFW fail rapido
)
line = _runtime_log_line(proc.stdout)
assert line is not None, f"no se logueo runtime: {proc.stdout[-500:]}"
assert "kind=registry_venv" in line, line
assert "wsl=0" in line, line
@pytest.mark.skipif(not GRAPH_EXPLORER_BIN.exists(),
reason="binario graph_explorer no compilado")
def test_resolver_picks_embedded_when_runtime_present(tmp_path):
"""Con runtime/ junto al exe, debe elegir embedded."""
bin_copy = tmp_path / "graph_explorer"
shutil.copy(GRAPH_EXPLORER_BIN, bin_copy)
bin_copy.chmod(0o755)
# Faux runtime: solo necesita un ejecutable en bin/python3.
rt = tmp_path / "runtime" / "python" / "bin"
rt.mkdir(parents=True)
fake_py = rt / "python3"
fake_py.write_text("#!/bin/bash\necho fake\n")
fake_py.chmod(0o755)
proc = subprocess.run(
[str(bin_copy)],
cwd=str(REGISTRY_ROOT / "cpp"),
capture_output=True, text=True, timeout=4,
env={**os.environ, "DISPLAY": ""},
)
line = _runtime_log_line(proc.stdout)
assert line is not None, f"no se logueo runtime: {proc.stdout[-500:]}"
assert "kind=embedded" in line, line
assert str(fake_py) in line, line
@pytest.mark.skipif(not GRAPH_EXPLORER_BIN.exists(),
reason="binario graph_explorer no compilado")
def test_resolver_uses_fn_python_env_var(tmp_path):
"""FN_PYTHON tiene prioridad sobre el venv del registry."""
bin_copy = tmp_path / "graph_explorer"
shutil.copy(GRAPH_EXPLORER_BIN, bin_copy)
bin_copy.chmod(0o755)
fake = tmp_path / "my_python"
fake.write_text("#!/bin/bash\necho fake\n")
fake.chmod(0o755)
proc = subprocess.run(
[str(bin_copy)],
cwd=str(REGISTRY_ROOT / "cpp"),
capture_output=True, text=True, timeout=4,
env={**os.environ, "DISPLAY": "", "FN_PYTHON": str(fake)},
)
line = _runtime_log_line(proc.stdout)
assert line is not None, f"no se logueo runtime: {proc.stdout[-500:]}"
assert "kind=env" in line, line
assert str(fake) in line, line
def test_freeze_script_is_idempotent(tmp_path):
"""Llamadas consecutivas con mismas deps no rehacen el runtime."""
fake_app = tmp_path / "app"
fake_app.mkdir()
(fake_app / "app.md").write_text(
"---\nname: x\npython_runtime: true\n"
"python_runtime_deps:\n - requests\n---\n",
encoding="utf-8")
script = APP_DIR_SRC / "tools" / "freeze_python_runtime.sh"
# 1ra ejecucion: deberia hacer todo el trabajo.
proc1 = subprocess.run(
[str(script), str(fake_app), "linux"],
capture_output=True, text=True, timeout=120,
)
if proc1.returncode != 0:
pytest.skip(f"freeze fallo (sin uv?): {proc1.stderr[:200]}")
assert (fake_app / "runtime" / ".lock").exists()
lock1 = (fake_app / "runtime" / ".lock").read_text()
# 2da: con el .lock ya escrito, debe reportar "sin cambios".
proc2 = subprocess.run(
[str(script), str(fake_app), "linux"],
capture_output=True, text=True, timeout=10,
)
assert proc2.returncode == 0, proc2.stderr
assert "sin cambios" in proc2.stdout, proc2.stdout
lock2 = (fake_app / "runtime" / ".lock").read_text()
assert lock1 == lock2