|
| 1 | +"""Pin the Mac-bridge workload interpreter (Layer B) + import self-check (Layer C). |
| 2 | +
|
| 3 | +A self-hosted runner's default ``python3`` can silently change across reboots / |
| 4 | +OS upgrades (observed 2026-06-18: it flipped to a Python 3.14 without ``mlx_lm``, |
| 5 | +breaking every full-engine preset with a deep ``ModuleNotFoundError``). The |
| 6 | +mac-bridge executor used to invoke a bare ``python3`` for the workload, so it |
| 7 | +inherited whatever interpreter happened to be first on ``PATH``. |
| 8 | +
|
| 9 | +This module makes the workload interpreter **explicit and verified**: |
| 10 | +
|
| 11 | +* **Layer B — resolution.** Build an ordered candidate list (a pinned |
| 12 | + ``KAKEYA_MAC_PYTHON``, common venv paths, then ``PATH`` pythons) and pick the |
| 13 | + first one that can import the gate module (``mlx_lm``); fall back to the first |
| 14 | + existing candidate otherwise. |
| 15 | +* **Layer C — gate.** For presets whose workload needs ``mlx_lm`` (the ``mlx-`` / |
| 16 | + ``k3-`` engine families, minus the env-probe / upgrade tools that exist to |
| 17 | + diagnose/repair the env), fail fast with a clear message instead of a deep |
| 18 | + import error when no capable interpreter exists. |
| 19 | +
|
| 20 | +All functions here are pure / dependency-injected so they are unit-tested on the |
| 21 | +Linux gate (the CLI ``scripts/mac_bridge/run_preset.py`` is a thin caller). See |
| 22 | +``docs/skills/pin-selfhosted-runner-python-env-skill.md``. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +import os |
| 28 | +import shutil |
| 29 | +from dataclasses import dataclass |
| 30 | +from typing import Callable, List, Mapping, Optional, Sequence |
| 31 | + |
| 32 | +# The single module whose absence broke the runner; importing it implies the |
| 33 | +# full MLX-LM stack is wired for the interpreter. |
| 34 | +GATE_MODULE = "mlx_lm" |
| 35 | + |
| 36 | +# ``mlx-``/``k3-`` presets that must NOT be import-gated: these exist precisely |
| 37 | +# to probe or repair the environment, so they must run even when mlx_lm is gone. |
| 38 | +_IMPORT_GATE_SKIP = frozenset({"mlx-env-probe", "mlx-upgrade"}) |
| 39 | + |
| 40 | +SKILL_DOC = "docs/skills/pin-selfhosted-runner-python-env-skill.md" |
| 41 | + |
| 42 | + |
| 43 | +def workload_python_candidates( |
| 44 | + environ: Mapping[str, str], |
| 45 | + *, |
| 46 | + which: Callable[[str], Optional[str]] = shutil.which, |
| 47 | + expanduser: Callable[[str], str] = os.path.expanduser, |
| 48 | +) -> List[str]: |
| 49 | + """Ordered, de-duplicated interpreter candidates for the heavy workload. |
| 50 | +
|
| 51 | + Priority: the explicit pin (``KAKEYA_MAC_PYTHON``), then conventional venv |
| 52 | + locations, then ``PATH`` pythons (a pinned minor version before the bare |
| 53 | + ``python3`` that a reboot may have repointed).""" |
| 54 | + raw = [ |
| 55 | + environ.get("KAKEYA_MAC_PYTHON"), |
| 56 | + expanduser("~/kakeya-venv/bin/python"), |
| 57 | + expanduser("~/.venv/bin/python"), |
| 58 | + which("python3.13"), |
| 59 | + which("python3"), |
| 60 | + ] |
| 61 | + out: List[str] = [] |
| 62 | + for c in raw: |
| 63 | + if c and c not in out: |
| 64 | + out.append(c) |
| 65 | + return out |
| 66 | + |
| 67 | + |
| 68 | +@dataclass(frozen=True) |
| 69 | +class ResolvedPython: |
| 70 | + """The interpreter chosen for the workload.""" |
| 71 | + |
| 72 | + path: str |
| 73 | + gate_module_ok: bool # whether ``path`` can import GATE_MODULE |
| 74 | + from_pin: bool # whether it came from ``KAKEYA_MAC_PYTHON`` |
| 75 | + |
| 76 | + |
| 77 | +def resolve_workload_python( |
| 78 | + candidates: Sequence[str], |
| 79 | + can_import: Callable[[str], bool], |
| 80 | + *, |
| 81 | + pinned: Optional[str] = None, |
| 82 | +) -> Optional[ResolvedPython]: |
| 83 | + """Pick the first candidate that can import :data:`GATE_MODULE`; otherwise |
| 84 | + the first candidate (a fallback whose ``gate_module_ok`` is ``False``). |
| 85 | + Returns ``None`` only when there are no candidates at all.""" |
| 86 | + first: Optional[str] = None |
| 87 | + for c in candidates: |
| 88 | + if first is None: |
| 89 | + first = c |
| 90 | + if can_import(c): |
| 91 | + return ResolvedPython(c, True, c == pinned) |
| 92 | + if first is None: |
| 93 | + return None |
| 94 | + return ResolvedPython(first, False, first == pinned) |
| 95 | + |
| 96 | + |
| 97 | +def preset_requires_gate(preset_name: str) -> bool: |
| 98 | + """True iff a preset's workload needs :data:`GATE_MODULE` (so a missing |
| 99 | + import must fail fast). The ``mlx-`` / ``k3-`` engine presets do; the |
| 100 | + env-probe and upgrade tools (which diagnose/repair the env) are exempt.""" |
| 101 | + if preset_name in _IMPORT_GATE_SKIP: |
| 102 | + return False |
| 103 | + return preset_name.startswith(("mlx-", "k3-")) |
| 104 | + |
| 105 | + |
| 106 | +def substitute_python(argv: Sequence[str], pybin: str) -> List[str]: |
| 107 | + """Rewrite a leading bare ``python3`` to the resolved interpreter ``pybin``. |
| 108 | + Non-``python3`` argv (e.g. ``bash run_kakeya_mac.sh``, which reads |
| 109 | + ``KAKEYA_MAC_PYTHON`` itself) is returned unchanged.""" |
| 110 | + a = list(argv) |
| 111 | + if a and a[0] == "python3": |
| 112 | + a[0] = pybin |
| 113 | + return a |
| 114 | + |
| 115 | + |
| 116 | +def gate_error_message(preset_name: str, pybin: str) -> str: |
| 117 | + """The fail-fast message when a gated preset has no mlx_lm-capable python.""" |
| 118 | + return ( |
| 119 | + f"runner python '{pybin}' cannot import {GATE_MODULE!r}, which preset " |
| 120 | + f"'{preset_name}' requires. The runner's default python likely changed " |
| 121 | + f"(e.g. after a reboot). Pin the venv via KAKEYA_MAC_PYTHON or the runner " |
| 122 | + f"agent PATH and reinstall the ML stack — see {SKILL_DOC}." |
| 123 | + ) |
0 commit comments