diff --git a/docs/kakeyainferenceenginebuildskill.md b/docs/kakeyainferenceenginebuildskill.md index 38a40c82..afab3e40 100644 --- a/docs/kakeyainferenceenginebuildskill.md +++ b/docs/kakeyainferenceenginebuildskill.md @@ -351,5 +351,6 @@ If any answer is "no", write the weaker, true claim. - v0.5-cuda scorecard (+ honest §5): `docs/reports/kakeya-inference-engine-v0.5-cuda.md` - Engine vs vLLM long-context journey: `docs/reports/kakeya-engine-vs-vllm-h200.md`, `docs/reports/kakeya-vs-vllm-longcontext-h200.md` - MLX port lessons: `docs/mlx-port-lessons.md` +- Self-hosted runner Python pinning (reboot-proof mlx_lm/torch/transformers): `docs/skills/pin-selfhosted-runner-python-env-skill.md` - f_θ training pipeline: `docs/design/k3-f-theta-training-pipeline.md` - Session capacity / cross-host: `docs/adr/0014-agent-connection-capacity-and-cross-host-topology-tests.md` diff --git a/docs/skills/pin-selfhosted-runner-python-env-skill.md b/docs/skills/pin-selfhosted-runner-python-env-skill.md new file mode 100644 index 00000000..2f348fd0 --- /dev/null +++ b/docs/skills/pin-selfhosted-runner-python-env-skill.md @@ -0,0 +1,193 @@ +# Skill: Pin a self-hosted runner's Python env (survive reboots, reproducible heavy ML deps) + +**Reusable across agents (Claude / Codex / Cursor).** Copy this file or paste the +prompt in the appendix. It is written to be repo-agnostic; the concrete examples +use a GitHub Actions self-hosted Mac runner driving MLX (`mlx_lm`/`torch`/ +`transformers`), but the pattern applies to any self-hosted runner (Mac or Linux) +that runs heavy ML/native deps from a virtualenv. + +--- + +## 1. When to use this skill + +Trigger it when **a self-hosted runner job fails on a missing module that "used to +work"**, especially after a host **reboot / OS or Python upgrade / runner +re-register**. Classic signatures: + +- `ModuleNotFoundError: No module named 'mlx_lm'` (or `torch`, `transformers`, …) + in a job that previously passed. +- The failure is **fast** (seconds) — it dies at `import`, before any real work. +- A **lightweight probe** (one that only needs stdlib + a base package) still + passes, proving the runner is *online* but pointing at the **wrong interpreter**. +- The interpreter version changed (e.g. `python=3.14.3` where it used to be + `3.13.x`), or `pkg=None` for a package that should be installed. + +Root cause is almost always: the workflow invokes a **bare `python3`**, and after +the reboot the default `python3` on `PATH` is no longer the venv that has the +deps. The venv still exists; nothing points at it. + +--- + +## 2. Diagnose first (don't guess) + +Run the **cheapest possible probe** through the same runner path to read the +interpreter + module state, instead of assuming. Example (adapt the import list): + +```bash +python3 - <<'PY' +import sys +def v(m): + try: + mod = __import__(m); return getattr(mod, "__version__", "ok") + except Exception as e: + return f"MISSING ({e.__class__.__name__})" +print("python =", sys.version.split()[0], "| exe =", sys.executable) +for m in ("mlx", "mlx_lm", "torch", "transformers"): + print(f"{m} = {v(m)}") +PY +``` + +Decision rule: +- **Runner online + probe shows wrong `python`/`exe` or `MISSING` deps** → this skill (interpreter pinning). +- **Probe itself never starts (job stuck `queued`/`pending`)** → the runner *agent* + is down; restart the agent first (different problem). + +> In CI-driven runners, route the probe through the same executor the real jobs +> use (so `PATH`/env match). A one-liner like the above, committed as a tiny +> "env-probe" job/preset, is worth keeping permanently. + +--- + +## 3. Fix — three layers (do all three; they are defense-in-depth) + +### Layer A — Pin the interpreter the runner *agent* sees (host side, durable) + +Make the venv's `bin` the first thing on the **runner agent's** `PATH`, so a bare +`python3` resolves to the venv even across reboots. Pick the mechanism for how the +agent is launched: + +- **GitHub Actions runner as a service (recommended).** The runner reads a + `.env` and a `.path` file in its install dir at start: + ```bash + cd ~/actions-runner + echo "$HOME/kakeya-venv/bin" > .path # prepended to PATH + echo "VIRTUAL_ENV=$HOME/kakeya-venv" >> .env + ./svc.sh stop && ./svc.sh start # reload + ``` + (`.path` is concatenated ahead of the system PATH for every job; `.env` injects + process env. Both persist across reboots because the service re-reads them.) +- **launchd plist (macOS), if not using `svc.sh`.** In the runner's + `~/Library/LaunchAgents/.plist`, set: + ```xml + EnvironmentVariables + + PATH/Users/<you>/kakeya-venv/bin:/usr/bin:/bin:/usr/sbin:/sbin + + ``` + then `launchctl unload/load` the plist. +- **systemd (Linux self-hosted).** In the runner unit: + `Environment="PATH=/opt/kakeya-venv/bin:%h/.local/bin:/usr/bin:/bin"`, then + `systemctl daemon-reload && systemctl restart `. + +Verify: `python3 -c "import mlx_lm, torch, transformers; print('ok')"` from a job. + +### Layer B — Make the workflow/executor resolve a *pinned* interpreter (repo side, robust) + +Never call a bare `python3` for the heavy job. Resolve an explicit interpreter so +the repo is robust even if Layer A drifts: + +1. Add a repo/runner variable, e.g. `KAKEYA_MAC_PYTHON`, pointing at the venv + python (`/Users//kakeya-venv/bin/python`). Default-discover if unset: + ```bash + PYBIN="${KAKEYA_MAC_PYTHON:-}" + for c in "$PYBIN" "$HOME/kakeya-venv/bin/python" "$(command -v python3.13)" "$(command -v python3)"; do + [ -n "$c" ] && [ -x "$c" ] && "$c" -c 'import mlx_lm' 2>/dev/null && { PYBIN="$c"; break; } + done + ``` +2. Use `$PYBIN` (or substitute a `${PYTHON}` token in your command templates) + instead of `python3` for the actual workload. If your executor spawns argv + lists (no shell), resolve the token to `$PYBIN` before `subprocess.run`. + +### Layer C — Fail fast with a clear message (repo side, observability) + +Before the expensive step, assert the deps and **print a fix hint** so the next +failure is self-explanatory instead of a deep `ModuleNotFoundError`: + +```bash +"$PYBIN" - <<'PY' || { echo "::error::runner python missing ML deps — see pin-selfhosted-runner-python-env-skill.md (Layer A)"; exit 90; } +import mlx_lm, torch, transformers # noqa +PY +``` + +--- + +## 4. Verify the fix + +1. Re-run the lightweight env-probe → correct `python`/`exe`, all deps present. +2. Re-run one **real** (heavy) job → no `ModuleNotFoundError`, completes. +3. **Reboot the host and re-run** (the actual regression you are fixing) → still + green. This step is the whole point; do not skip it. + +--- + +## 5. Generalizing to a *Cloud Agent* VM env setup (different machine!) + +Do **not** confuse the self-hosted runner with the Cloud Agent VM: +- The **Cloud Agent VM** is typically Linux; it runs the *client* that dispatches + jobs and the unit-test gate. **Mac-only deps (MLX) do not belong there.** Put + only what the client/tests need into the Cloud Agent env setup (base image + + startup script), and pin versions. +- The **self-hosted runner** is where the heavy/native/Mac deps live. Pin them + there (Layers A–C above), not in the Cloud VM env setup. + +For the Cloud Agent VM specifically: bake stable deps into the **base image**, do +slow-changing installs in the **startup script**, and pin versions so a new VM is +reproducible. (In Cursor, this is the "env setup agent" config.) + +--- + +## 6. Anti-patterns + +- ❌ `pip install` the missing dep into whatever `python3` happens to be active + (often a too-new system Python with no wheels for `torch`/`mlx_lm`). Pin to the + known-good venv instead. +- ❌ Hardcoding an absolute interpreter path in many places. Resolve once + (variable + discovery) and reuse. +- ❌ "It works now" without a reboot test — the regression is reboot-triggered. +- ❌ Relying on an interactive shell's `source venv/bin/activate`; CI jobs and + services don't run your `.zshrc`. + +--- + +## Appendix — ready-to-paste prompt for a setup agent + +> **Task: make our self-hosted CI runner's Python environment reboot-proof.** +> +> Symptom: jobs on our self-hosted runner fail fast with +> `ModuleNotFoundError: No module named 'mlx_lm'` after the host rebooted; a +> lightweight env-probe shows the runner's default `python3` switched to a newer +> interpreter that lacks our ML stack (`mlx_lm`/`torch`/`transformers`), while the +> known-good venv still exists but is no longer on `PATH`. +> +> Do all of the following, smallest-diff first, and verify each: +> 1. **Diagnose:** run a tiny probe that prints `sys.version`, `sys.executable`, +> and import status of `mlx_lm, torch, transformers` through the same path the +> real jobs use. Confirm the wrong interpreter / missing modules. +> 2. **Host (runner agent):** pin the venv's `bin` ahead of system `PATH` for the +> runner service so a bare `python3` resolves to the venv across reboots — via +> the runner's `.path`/`.env` files (GitHub Actions `svc.sh`), or the +> launchd/systemd unit's `PATH` env. Reload the service. +> 3. **Repo (workflow/executor):** stop calling bare `python3` for the heavy job. +> Resolve a pinned interpreter from a `*_PYTHON` repo/runner variable, with a +> discovery fallback that picks the first candidate where `import mlx_lm` +> succeeds; use it for the workload commands. +> 4. **Repo (fail-fast):** before the expensive step, assert +> `import mlx_lm, torch, transformers` and emit a clear `::error::` with a link +> to this skill if missing (exit non-zero). +> 5. **Verify, including a reboot:** env-probe green, one real heavy job green, +> then reboot the host and re-run the same job — must still be green. +> 6. **Pin versions** in the venv (freeze a lockfile) and document the venv path + +> rebuild steps so the environment is reproducible, not just patched. +> +> Keep the heavy/native deps on the self-hosted runner only; do NOT add Mac-only +> deps to the Cloud Agent (Linux) VM env setup. diff --git a/inference_engine/backends/mlx/fused_specdecode.py b/inference_engine/backends/mlx/fused_specdecode.py index 9b6b1cfd..9318b6fb 100644 --- a/inference_engine/backends/mlx/fused_specdecode.py +++ b/inference_engine/backends/mlx/fused_specdecode.py @@ -35,7 +35,6 @@ restored_prefill_cache, ) - # --------------------------------------------------------------------------- # # Component A: capture verifier aux-layer hidden states (no transformers # `output_hidden_states` on MLX → patch the decoder-layer __call__). @@ -387,6 +386,7 @@ def fused_specdecode_generate_mlx_trim( eos_ids: Sequence[int] = (), single_fused: bool = False, on_commit: Optional[Callable[[List[int]], None]] = None, + stop_on_runaway: bool = True, ) -> Dict[str, Any]: """CUDA-parity fused spec decode: KEEP accepted K/V, TRIM only the rejected tail (no rollback, no carry re-forward). Requires the adapter to be @@ -412,6 +412,7 @@ def fused_specdecode_generate_mlx_trim( generated: List[int] = [] accepts: List[int] = [] block_evals: List[float] = [] + stopped_on_runaway = False ctx_len = C try: while len(generated) < gen_tokens: @@ -474,6 +475,12 @@ def fused_specdecode_generate_mlx_trim( timing["extend_s"] += time.perf_counter() - t_extend if any(t in eos for t in commit): break + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break finally: adapter._capture_aux = False generated = generated[:gen_tokens] @@ -483,6 +490,7 @@ def fused_specdecode_generate_mlx_trim( "mean_accept_len": (round(sum(accepts) / len(accepts), 3) if accepts else 0.0), "decode_tokens": len(generated), + "stopped_on_runaway": stopped_on_runaway, "loop": ("mlx_trim_single_fused_probe" if single_fused else "mlx_trim_keep_accepted_cuda_parity"), "single_fused": bool(single_fused), @@ -505,6 +513,7 @@ def fused_specdecode_generate_mlx( block_size: int, eos_ids: Sequence[int] = (), on_commit: Optional[Callable[[List[int]], None]] = None, + stop_on_runaway: bool = True, ) -> Dict[str, Any]: """All-MLX fused spec decode with ONE host sync per block. @@ -546,6 +555,7 @@ def fused_specdecode_generate_mlx( generated: List[int] = [] accepts: List[int] = [] + stopped_on_runaway = False # Rollback-carry state: rejected blocks roll the WHOLE forward back # (rollback_block — see its docstring for why trim is unsound on the # wrapped sliding ring) and carry the stream-committed-but-not-cached @@ -630,6 +640,12 @@ def fused_specdecode_generate_mlx( timing["extend_s"] += time.perf_counter() - t_extend if any(t in eos for t in commit): break + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break finally: adapter._capture_aux = False generated = generated[:gen_tokens] @@ -639,6 +655,7 @@ def fused_specdecode_generate_mlx( "mean_accept_len": (round(sum(accepts) / len(accepts), 3) if accepts else 0.0), "decode_tokens": len(generated), + "stopped_on_runaway": stopped_on_runaway, "loop": "mlx_rollback_carry_v3", "time_breakdown_s": {k: round(v, 3) for k, v in timing.items()}, } @@ -671,6 +688,40 @@ def _sliding_ring_would_wrap(cache: Any, n_new: int) -> bool: return False +def _trailing_runaway_drop( + ids: Sequence[int], + *, + max_period: int = 8, + min_reps: int = 12, + keep_reps: int = 3, +) -> int: + """Return how many TRAILING tokens to drop if ``ids`` ends in a runaway + short-period loop, else 0. + + A runaway loop is a unit of ``1..max_period`` tokens repeated ``>= min_reps`` + times back-to-back at the tail (e.g. the ``**``/``.2``/``*`` markdown-marker + collapse greedy decoding falls into on code prompts). When found, we keep + ``keep_reps`` instances and drop the rest, so callers can stop generation + with a clean tail instead of emitting an unbounded wall of repeats. + + Deliberately CONSERVATIVE (>= 12 back-to-back repeats of a <= 8-token unit) + so legitimately repetitive text — numbered lists, ``矿工 A/B/C`` enumerations, + structured code — is never trimmed. Returns 0 when no runaway is present.""" + n = len(ids) + for p in range(1, max_period + 1): + if n < p * min_reps: + continue + unit = list(ids[n - p:]) + reps = 0 + i = n + while i - p >= 0 and list(ids[i - p:i]) == unit: + reps += 1 + i -= p + if reps >= min_reps: + return max((reps - keep_reps) * p, 0) + return 0 + + # --------------------------------------------------------------------------- # # The fused spec-decode loop (control flow; MLX/torch ops via injected fns). # --------------------------------------------------------------------------- # @@ -689,6 +740,7 @@ def fused_specdecode_generate( cat_aux_fn: Callable[[Sequence[Any]], Any], allow_greedy_fallback: bool = True, on_commit: Optional[Callable[[List[int]], None]] = None, + stop_on_runaway: bool = True, ) -> Dict[str, Any]: """Run the fused engine. ``adapter`` must already be prefilled. Per block: draft from the cached drafter context (B), verify+capture-aux incrementally @@ -717,6 +769,7 @@ def fused_specdecode_generate( generated: List[int] = [] accepts: List[int] = [] fallback_to_greedy = False + stopped_on_runaway = False try: while len(generated) < gen_tokens: L = min(block_size, gen_tokens - len(generated)) @@ -792,6 +845,17 @@ def fused_specdecode_generate( _emit(on_commit, generated) if any(t in eos for t in commit): break + # Greedy decoding can collapse into a runaway short-period loop (e.g. + # the **/.2/* markdown-marker wall on code prompts); the drafter then + # trivially predicts the repeats and the greedy verifier accepts them, + # so acceptance stays HIGH while the output is garbage. Stop on it + # instead of emitting an unbounded wall (keeps a short clean tail). + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break if (allow_greedy_fallback and len(accepts) >= 2 and (sum(accepts) / len(accepts)) < 1.5): fallback_to_greedy = True @@ -810,6 +874,12 @@ def fused_specdecode_generate( _emit(on_commit, generated) if tok in eos: break + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break timing["fallback_greedy_s"] += time.perf_counter() - t_fb finally: adapter._capture_aux = False @@ -820,5 +890,6 @@ def fused_specdecode_generate( "mean_accept_len": (round(sum(accepts) / len(accepts), 3) if accepts else 0.0), "decode_tokens": len(generated), + "stopped_on_runaway": stopped_on_runaway, "time_breakdown_s": {k: round(v, 3) for k, v in timing.items()}, } diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 8a27bbb9..a404c423 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -798,6 +798,100 @@ def _harness_preset( params={"max_new_tokens": ("int:max_new_tokens", "64")}, validate_reports=True, # §4 liveness gate on-device ), + Preset( + name="mlx-kakeya-launcher-dryrun-bash32", + description="Guard the launcher against the macOS bash-3.2 " + "'unbound variable' bug: run scripts/run_kakeya_mac.sh " + "--dry-run under /bin/bash (Apple's frozen bash 3.2) with " + "NO pass-through args, so the empty EXTRA array is expanded " + "under set -u. Must exit 0 and print the command (pre-fix it " + "died with 'EXTRA[@]: unbound variable'). Fast; no model load.", + command_templates=( + ("/bin/bash", "scripts/run_kakeya_mac.sh", "--dry-run"), + ), + timeout_minutes=10, + validate_reports=False, + ), + Preset( + name="mlx-kakeya-launcher-full", + description="Validate scripts/run_kakeya_mac.sh in FULL mode (f_θ " + "verifier+proposer+f_θ, default path) on a LONG scripted " + "answer that crosses the ~1024 native-cache ring wrap. " + "Guards the launcher's full pipeline + the PR #146 " + "wrapped-ring fix end-to-end: the report must pass the §4 " + "liveness gate AND the quality gate (coherent, no runaway " + "repeat) past the wrap.", + command_templates=( + ( + "bash", "scripts/run_kakeya_mac.sh", + "--max-new-tokens", "{max_new_tokens}", + "--ignore-turn-stop", + "--chat-scripted", "请详细解释POW的工作原理", + "--output", + "results/research/k3_mac_bridge_launcher_full.json", + ), + ), + timeout_minutes=90, + params={"max_new_tokens": ("int:max_new_tokens", "1300")}, + validate_reports=True, # §4 liveness + §2.4 quality gate on-device + ), + Preset( + name="mlx-kakeya-codegen-degen-probe", + description="Regression probe (guard DISABLED): full f_θ fused engine " + "on the multi-turn 'explain PoW || write PoW in C' chat " + "that originally degenerated, with --fused-no-loop-guard so " + "any greedy markdown-marker collapse is observable. Pairs " + "with mlx-kakeya-codegen-guard-validate (guard ENABLED) to " + "show the guard is what keeps the answer clean. On current " + "code (post wrap-fix) both turns stay coherent.", + command_templates=( + ( + "python3", "scripts/research/k3_integrated_niah_eval_mac.py", + "--verifier-path", "${ENV:KAKEYA_MAC_VERIFIER_PATH}", + "--drafter-id", "${ENV:KAKEYA_MAC_DRAFTER_ID}", + "--f-theta-dir", "${ENV:KAKEYA_MAC_FTHETA_DIR}", + "--s5-exact-full-attn", "--fused-specdecode", "--force-f-theta", + "--sink-size", "4", "--window-size", "64", "--block-size", "4", + "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", + "--chat", "--fused-no-loop-guard", + "--chat-scripted", + "请详细解释POW的工作原理||实现一个PoW的代码,用c语言完成", + "--output", "results/research/codegen_degen_2815_longprompt.json", + ), + ), + timeout_minutes=120, + params={"max_new_tokens": ("int:max_new_tokens", "900")}, + validate_reports=False, + ), + Preset( + name="mlx-kakeya-codegen-guard-validate", + description="Validate the runaway-loop guard end-to-end: full f_θ fused " + "engine on the multi-turn 'explain PoW || write PoW in C' " + "chat with the guard ENABLED (production default). The " + "answer must stay coherent and never collapse into a marker " + "wall — if a runaway starts, the guard stops it " + "(stopped_on_runaway) leaving a clean tail. Confirmed " + "coherent on current code; byte-identical to the guard-off " + "probe (the guard is inert on healthy output).", + command_templates=( + ( + "python3", "scripts/research/k3_integrated_niah_eval_mac.py", + "--verifier-path", "${ENV:KAKEYA_MAC_VERIFIER_PATH}", + "--drafter-id", "${ENV:KAKEYA_MAC_DRAFTER_ID}", + "--f-theta-dir", "${ENV:KAKEYA_MAC_FTHETA_DIR}", + "--s5-exact-full-attn", "--fused-specdecode", "--force-f-theta", + "--sink-size", "4", "--window-size", "64", "--block-size", "4", + "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", + "--chat", + "--chat-scripted", + "请详细解释POW的工作原理||实现一个PoW的代码,用c语言完成", + "--output", "results/research/codegen_guard_validate_2815.json", + ), + ), + timeout_minutes=120, + params={"max_new_tokens": ("int:max_new_tokens", "900")}, + validate_reports=False, + ), Preset( name="mlx-kakeya-degen-probe", description="Long-decode regression probe: full f_θ fused engine on a " diff --git a/inference_engine/bridge/runner_python.py b/inference_engine/bridge/runner_python.py new file mode 100644 index 00000000..1b0c27b4 --- /dev/null +++ b/inference_engine/bridge/runner_python.py @@ -0,0 +1,123 @@ +"""Pin the Mac-bridge workload interpreter (Layer B) + import self-check (Layer C). + +A self-hosted runner's default ``python3`` can silently change across reboots / +OS upgrades (observed 2026-06-18: it flipped to a Python 3.14 without ``mlx_lm``, +breaking every full-engine preset with a deep ``ModuleNotFoundError``). The +mac-bridge executor used to invoke a bare ``python3`` for the workload, so it +inherited whatever interpreter happened to be first on ``PATH``. + +This module makes the workload interpreter **explicit and verified**: + +* **Layer B — resolution.** Build an ordered candidate list (a pinned + ``KAKEYA_MAC_PYTHON``, common venv paths, then ``PATH`` pythons) and pick the + first one that can import the gate module (``mlx_lm``); fall back to the first + existing candidate otherwise. +* **Layer C — gate.** For presets whose workload needs ``mlx_lm`` (the ``mlx-`` / + ``k3-`` engine families, minus the env-probe / upgrade tools that exist to + diagnose/repair the env), fail fast with a clear message instead of a deep + import error when no capable interpreter exists. + +All functions here are pure / dependency-injected so they are unit-tested on the +Linux gate (the CLI ``scripts/mac_bridge/run_preset.py`` is a thin caller). See +``docs/skills/pin-selfhosted-runner-python-env-skill.md``. +""" + +from __future__ import annotations + +import os +import shutil +from dataclasses import dataclass +from typing import Callable, List, Mapping, Optional, Sequence + +# The single module whose absence broke the runner; importing it implies the +# full MLX-LM stack is wired for the interpreter. +GATE_MODULE = "mlx_lm" + +# ``mlx-``/``k3-`` presets that must NOT be import-gated: these exist precisely +# to probe or repair the environment, so they must run even when mlx_lm is gone. +_IMPORT_GATE_SKIP = frozenset({"mlx-env-probe", "mlx-upgrade"}) + +SKILL_DOC = "docs/skills/pin-selfhosted-runner-python-env-skill.md" + + +def workload_python_candidates( + environ: Mapping[str, str], + *, + which: Callable[[str], Optional[str]] = shutil.which, + expanduser: Callable[[str], str] = os.path.expanduser, +) -> List[str]: + """Ordered, de-duplicated interpreter candidates for the heavy workload. + + Priority: the explicit pin (``KAKEYA_MAC_PYTHON``), then conventional venv + locations, then ``PATH`` pythons (a pinned minor version before the bare + ``python3`` that a reboot may have repointed).""" + raw = [ + environ.get("KAKEYA_MAC_PYTHON"), + expanduser("~/kakeya-venv/bin/python"), + expanduser("~/.venv/bin/python"), + which("python3.13"), + which("python3"), + ] + out: List[str] = [] + for c in raw: + if c and c not in out: + out.append(c) + return out + + +@dataclass(frozen=True) +class ResolvedPython: + """The interpreter chosen for the workload.""" + + path: str + gate_module_ok: bool # whether ``path`` can import GATE_MODULE + from_pin: bool # whether it came from ``KAKEYA_MAC_PYTHON`` + + +def resolve_workload_python( + candidates: Sequence[str], + can_import: Callable[[str], bool], + *, + pinned: Optional[str] = None, +) -> Optional[ResolvedPython]: + """Pick the first candidate that can import :data:`GATE_MODULE`; otherwise + the first candidate (a fallback whose ``gate_module_ok`` is ``False``). + Returns ``None`` only when there are no candidates at all.""" + first: Optional[str] = None + for c in candidates: + if first is None: + first = c + if can_import(c): + return ResolvedPython(c, True, c == pinned) + if first is None: + return None + return ResolvedPython(first, False, first == pinned) + + +def preset_requires_gate(preset_name: str) -> bool: + """True iff a preset's workload needs :data:`GATE_MODULE` (so a missing + import must fail fast). The ``mlx-`` / ``k3-`` engine presets do; the + env-probe and upgrade tools (which diagnose/repair the env) are exempt.""" + if preset_name in _IMPORT_GATE_SKIP: + return False + return preset_name.startswith(("mlx-", "k3-")) + + +def substitute_python(argv: Sequence[str], pybin: str) -> List[str]: + """Rewrite a leading bare ``python3`` to the resolved interpreter ``pybin``. + Non-``python3`` argv (e.g. ``bash run_kakeya_mac.sh``, which reads + ``KAKEYA_MAC_PYTHON`` itself) is returned unchanged.""" + a = list(argv) + if a and a[0] == "python3": + a[0] = pybin + return a + + +def gate_error_message(preset_name: str, pybin: str) -> str: + """The fail-fast message when a gated preset has no mlx_lm-capable python.""" + return ( + f"runner python '{pybin}' cannot import {GATE_MODULE!r}, which preset " + f"'{preset_name}' requires. The runner's default python likely changed " + f"(e.g. after a reboot). Pin the venv via KAKEYA_MAC_PYTHON or the runner " + f"agent PATH and reinstall the ML stack — see {SKILL_DOC}." + ) diff --git a/scripts/mac_bridge/run_preset.py b/scripts/mac_bridge/run_preset.py index a95122ad..4a63c00e 100644 --- a/scripts/mac_bridge/run_preset.py +++ b/scripts/mac_bridge/run_preset.py @@ -32,10 +32,29 @@ build_commands, parse_manifest_text, ) +from inference_engine.bridge.runner_python import ( + GATE_MODULE, + gate_error_message, + preset_requires_gate, + resolve_workload_python, + substitute_python, + workload_python_candidates, +) LOG_DIR = Path(".mac-bridge/logs") +def _can_import_gate_module(pybin: str) -> bool: + """True iff interpreter ``pybin`` can import the gate module (mlx_lm).""" + try: + return subprocess.run( + [pybin, "-c", f"import {GATE_MODULE}"], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + ).returncode == 0 + except OSError: + return False + + def main() -> int: ap = argparse.ArgumentParser(description=__doc__) ap.add_argument("--manifest", default=".mac-bridge/request.json") @@ -59,6 +78,23 @@ def main() -> int: print(json.dumps(argv)) return 0 + # Layer B — resolve a PINNED workload interpreter instead of trusting the + # bare ``python3`` on PATH (which a reboot can repoint to a python without + # mlx_lm). Layer C — gate: mlx-/k3- engine presets fail fast with a clear + # message when no mlx_lm-capable interpreter exists. + pinned = os.environ.get("KAKEYA_MAC_PYTHON") + candidates = workload_python_candidates(os.environ) + resolved = resolve_workload_python( + candidates, _can_import_gate_module, pinned=pinned) + pybin = resolved.path if resolved else "python3" + gate_ok = bool(resolved and resolved.gate_module_ok) + print(f"[mac-bridge] workload python={pybin} {GATE_MODULE}_ok={gate_ok} " + f"pinned={pinned!r} candidates={candidates}", file=sys.stderr) + if preset_requires_gate(request.preset.name) and not gate_ok: + print(f"::error::{gate_error_message(request.preset.name, pybin)}", + file=sys.stderr) + return 90 + LOG_DIR.mkdir(parents=True, exist_ok=True) summary = { "preset": request.preset.name, @@ -66,13 +102,19 @@ def main() -> int: "nonce": request.nonce, "commands": [], } + # Make the resolved interpreter authoritative for BOTH bare-``python3`` + # commands (rewritten here) and the launcher (which reads KAKEYA_MAC_PYTHON). + sub_env = dict(os.environ) + sub_env["KAKEYA_MAC_PYTHON"] = pybin rc = 0 for idx, argv in enumerate(commands): + argv = substitute_python(argv, pybin) log_path = LOG_DIR / f"{request.preset.name}-{idx}.log" print(f"[mac-bridge] exec[{idx}]: {argv}", file=sys.stderr) t0 = time.perf_counter() with log_path.open("wb") as log: - proc = subprocess.run(argv, stdout=log, stderr=subprocess.STDOUT) + proc = subprocess.run(argv, stdout=log, stderr=subprocess.STDOUT, + env=sub_env) elapsed = time.perf_counter() - t0 summary["commands"].append({ "argv": argv, diff --git a/scripts/research/k3_integrated_niah_eval_mac.py b/scripts/research/k3_integrated_niah_eval_mac.py index f00c151c..921d8fd0 100644 --- a/scripts/research/k3_integrated_niah_eval_mac.py +++ b/scripts/research/k3_integrated_niah_eval_mac.py @@ -185,6 +185,15 @@ def parse_args() -> argparse.Namespace: "stdout (as the interactive CLI does) instead of the " "per-block [stream] timing lines — lets a non-tty bridge " "run capture the exact live output format.") + ap.add_argument("--chat-scripted-file", default=None, + help="Like --chat-scripted but reads the (possibly long, " + "'||'-separated) scripted prompt from a UTF-8 file. Lets " + "a long context be a committed fixture instead of a giant " + "manifest argv. Overrides --chat-scripted when set.") + ap.add_argument("--fused-no-loop-guard", action="store_true", + help="DIAGNOSTIC: disable the fused engine's runaway-loop stop " + "(default ON) so a degeneration probe can observe the full " + "collapse. Production chat keeps the guard enabled.") ap.add_argument("--chat-native-ref", action="store_true", help="DIAGNOSTIC opt-in: before each chat turn, also run a " "plain NATIVE greedy AR decode of the SAME prompt for " @@ -815,19 +824,21 @@ def _gen_turn(pid: List[int], on_commit=None) -> Dict[str, Any]: evicted_positions=evicted, prefill_chunk_size=args.prefill_chunk_size, full_kv=args.cuda_trim) t0 = time.perf_counter() + _guard = not args.fused_no_loop_guard if mlx_drafter is not None and args.cuda_trim: res = fused_specdecode_generate_mlx_trim( adapter, active_drafter, aux_prompt=aux_prompt, embed_fn=embed_fn, lm_head_fn=lm_head_fn, gen_tokens=args.max_new_tokens, block_size=args.block_size, eos_ids=chat_eos, single_fused=args.single_fused, - on_commit=on_commit) + on_commit=on_commit, stop_on_runaway=_guard) elif mlx_drafter is not None: res = fused_specdecode_generate_mlx( adapter, active_drafter, aux_prompt=aux_prompt, embed_fn=embed_fn, lm_head_fn=lm_head_fn, gen_tokens=args.max_new_tokens, block_size=args.block_size, - eos_ids=chat_eos, on_commit=on_commit) + eos_ids=chat_eos, on_commit=on_commit, + stop_on_runaway=_guard) else: res = fused_specdecode_generate( adapter, active_drafter, aux_prompt=aux_prompt, @@ -835,7 +846,7 @@ def _gen_turn(pid: List[int], on_commit=None) -> Dict[str, Any]: gen_tokens=args.max_new_tokens, block_size=args.block_size, eos_ids=chat_eos, argmax_fn=argmax_fn, arange_fn=arange_fn, cat_aux_fn=cat_aux_fn, allow_greedy_fallback=False, - on_commit=on_commit) + on_commit=on_commit, stop_on_runaway=_guard) res["decode_s"] = round(time.perf_counter() - t0, 3) res["f_theta_ran"] = f_theta_ran res["f_theta_layers"] = sorted(rk.keys()) if rk else [] @@ -899,8 +910,12 @@ def cb(toks: List[int]) -> None: file=sys.stderr, flush=True) history: List[Dict[str, str]] = [] - if args.chat_scripted is not None: - turns = [t for t in args.chat_scripted.split("||") if t.strip()] + scripted = args.chat_scripted + if args.chat_scripted_file is not None: + with open(args.chat_scripted_file, encoding="utf-8") as _f: + scripted = _f.read() + if scripted is not None: + turns = [t for t in scripted.split("||") if t.strip()] transcript = [] for u in turns: history.append({"role": "user", "content": u}) diff --git a/scripts/run_kakeya_mac.sh b/scripts/run_kakeya_mac.sh index 4bf3308d..788f9e96 100755 --- a/scripts/run_kakeya_mac.sh +++ b/scripts/run_kakeya_mac.sh @@ -8,6 +8,13 @@ # the all-MLX proposer path (f_θ bypassed via S5 native prefill — much faster on # Mac, but the f_θ projection does not execute). # +# LONG ANSWERS ARE SAFE (PR #146). The full path runs on gemma-4's native hybrid +# cache (sliding RotatingKVCache, max_size≈1024). Past that ring wrap the engine +# automatically commits single tokens (no speculative rollback to mis-trim on the +# wrapped ring), so generations stay coherent well beyond ~1024 tokens — they +# just lose the spec-decode speedup past the wrap. So the default budget below is +# generous; you no longer need to keep answers under the window. +# # Model facts come from env vars (set on the kakeya-mac-m4 runner), with sane # fallbacks; override on the CLI if needed: # KAKEYA_MAC_VERIFIER_PATH local MLX gemma-4 dir @@ -17,7 +24,7 @@ # Usage: # bash scripts/run_kakeya_mac.sh # full engine (f_θ on), interactive # bash scripts/run_kakeya_mac.sh --fast # proposer-only (f_θ bypassed), faster -# bash scripts/run_kakeya_mac.sh --max-new-tokens 2048 --window 128 +# bash scripts/run_kakeya_mac.sh --max-new-tokens 4096 --window 128 # bash scripts/run_kakeya_mac.sh --dry-run # print the command, run nothing # echo 'Explain proof-of-work.' | bash scripts/run_kakeya_mac.sh # one-shot via stdin set -euo pipefail @@ -31,7 +38,9 @@ FTHETA="${KAKEYA_MAC_FTHETA_DIR:-results/research/f_theta_v5_s5_sliding}" SINK="${KAKEYA_SINK:-4}" WINDOW="${KAKEYA_WINDOW:-64}" BLOCK="${KAKEYA_BLOCK_SIZE:-4}" -MAX_NEW="${KAKEYA_MAX_NEW_TOKENS:-1024}" +# Default budget reaches past the ~1024 native-cache wrap; coherent there since +# PR #146 (single-token commits past the wrap). Raise/lower freely. +MAX_NEW="${KAKEYA_MAX_NEW_TOKENS:-2048}" FAST=0 DRY_RUN=0 @@ -47,7 +56,7 @@ while [[ $# -gt 0 ]]; do --window) shift; WINDOW="${1:?}" ;; --sink) shift; SINK="${1:?}" ;; --block-size) shift; BLOCK="${1:?}" ;; - -h|--help) sed -n '2,28p' "$0"; exit 0 ;; + -h|--help) sed -n '2,29p' "$0"; exit 0 ;; *) EXTRA+=("$1") ;; # pass-through (e.g. --chat-scripted ...) esac shift @@ -55,6 +64,11 @@ done log() { echo "[run-kakeya-mac] $*" >&2; } +# Pinned interpreter (Layer B): prefer KAKEYA_MAC_PYTHON (the venv python with +# mlx_lm/torch/transformers) over a bare python3 that a host reboot may have +# repointed. See docs/skills/pin-selfhosted-runner-python-env-skill.md. +PYBIN="${KAKEYA_MAC_PYTHON:-python3}" + # ---- argv for the full-engine harness chat ---- args=( --verifier-path "$VERIFIER" @@ -70,8 +84,9 @@ if [[ "$FAST" == "1" ]]; then MODE="FAST (verifier + proposer + S5 bounded KV; f_θ BYPASSED)" else # torch drafter + f_θ: the harness auto-enables --force-f-theta in --chat, so - # f_θ projection ACTUALLY RUNS each turn (the full pipeline). - MODE="FULL (verifier + proposer + f_θ + S5 bounded KV; f_θ runs)" + # f_θ projection ACTUALLY RUNS each turn (the full pipeline). Coherent past the + # ~1024 native-cache wrap (PR #146: single-token commits once the ring wraps). + MODE="FULL (verifier + proposer + f_θ + S5 bounded KV; f_θ runs; long-answer safe)" fi log "mode : $MODE" @@ -80,7 +95,11 @@ log "drafter : $DRAFTER" log "f_theta : $FTHETA" log "params : sink=$SINK window=$WINDOW block=$BLOCK max_new=$MAX_NEW" -cmd=( python3 scripts/research/k3_integrated_niah_eval_mac.py "${args[@]}" "${EXTRA[@]}" ) +# NOTE: ``${EXTRA[@]+"${EXTRA[@]}"}`` (not a bare ``"${EXTRA[@]}"``) — under +# ``set -u`` macOS's default bash 3.2 treats expanding an EMPTY array as an +# "unbound variable" error; the ``+`` form expands to nothing when EXTRA is +# empty and to the quoted elements otherwise. +cmd=( "$PYBIN" scripts/research/k3_integrated_niah_eval_mac.py "${args[@]}" ${EXTRA[@]+"${EXTRA[@]}"} ) if [[ "$DRY_RUN" == "1" ]]; then echo "PYTHONPATH=.:sdks/python ${cmd[*]}" @@ -88,9 +107,9 @@ if [[ "$DRY_RUN" == "1" ]]; then fi # ---- preflight (Apple Silicon + MLX + model) ---- -command -v python3 >/dev/null || { log "python3 not found"; exit 1; } -python3 -c "import mlx.core" 2>/dev/null \ - || { log "MLX not importable — this needs Apple Silicon + 'pip install mlx mlx-lm'"; exit 2; } +command -v "$PYBIN" >/dev/null 2>&1 || { log "interpreter not found: $PYBIN (set KAKEYA_MAC_PYTHON)"; exit 1; } +"$PYBIN" -c "import mlx.core, mlx_lm" 2>/dev/null \ + || { log "mlx/mlx_lm not importable by $PYBIN — Apple Silicon + a venv with 'mlx mlx-lm'; set KAKEYA_MAC_PYTHON. See docs/skills/pin-selfhosted-runner-python-env-skill.md"; exit 2; } [[ -d "$VERIFIER" ]] \ || { log "verifier model dir not found: $VERIFIER (set KAKEYA_MAC_VERIFIER_PATH)"; exit 3; } if [[ "$FAST" != "1" && ! -e "$FTHETA" ]]; then diff --git a/tests/backends/mlx/test_fused_specdecode.py b/tests/backends/mlx/test_fused_specdecode.py index ddf099b2..f9c37a47 100644 --- a/tests/backends/mlx/test_fused_specdecode.py +++ b/tests/backends/mlx/test_fused_specdecode.py @@ -170,6 +170,68 @@ def __init__(self, offset): self.max_size = None +def test_trailing_runaway_drop_detects_and_trims_loops(): + # 1-token unit repeated 20x -> drop all but keep_reps (default 3). + ids = [1, 2, 3] + [9] * 20 + drop = fsd._trailing_runaway_drop(ids) + assert drop == 17 # 20 - 3 kept + # multi-token unit (period 3) repeated 12x -> drop (12-3)*3 = 27. + ids2 = [5, 6] + [7, 8, 9] * 12 + assert fsd._trailing_runaway_drop(ids2) == 27 + + +def test_trailing_runaway_drop_is_conservative(): + # fewer than min_reps (12) back-to-back -> no trim. + assert fsd._trailing_runaway_drop([9] * 11) == 0 + # legitimate non-repeating tail -> no trim. + assert fsd._trailing_runaway_drop(list(range(40))) == 0 + # a period that does not tile the very tail -> no trim. + assert fsd._trailing_runaway_drop([1, 2] * 10 + [3]) == 0 + # empty / short -> no trim. + assert fsd._trailing_runaway_drop([]) == 0 + + +def test_fused_loop_stops_on_runaway_repeat(): + # Drafter keeps proposing the same token; the fake verifier's "+1" truth is + # defeated by making the bonus re-loop: we feed a drafter that always drafts + # the marker token and a verifier that greedily agrees, so the committed + # stream becomes a runaway single-token loop the guard must cut. + class _LoopAdapter(_FakeAdapter): + def forward_block(self, candidate): + # verifier greedily predicts the SAME marker token (42) forever. + if self._capture_aux: + L = len(candidate) + self._last_aux = [torch.zeros(L, self.hidden)] + return [42 for _ in candidate] + + adapter = _LoopAdapter(prompt_len=5, first_token=42) + drafter = _FakeDrafter(drafts=[[42, 42, 42]] * 60) + res = fsd.fused_specdecode_generate( + adapter, drafter, gen_tokens=400, block_size=4, eos_ids=(), + allow_greedy_fallback=False, **_loop_kwargs(drafter)) + assert res["stopped_on_runaway"] is True + # stopped early with a short clean tail, nowhere near the 400 budget. + assert len(res["tokens"]) < 40 + assert set(res["tokens"]) == {42} + + +def test_fused_loop_runaway_guard_can_be_disabled(): + class _LoopAdapter(_FakeAdapter): + def forward_block(self, candidate): + if self._capture_aux: + self._last_aux = [torch.zeros(len(candidate), self.hidden)] + return [42 for _ in candidate] + + adapter = _LoopAdapter(prompt_len=5, first_token=42) + drafter = _FakeDrafter(drafts=[[42, 42, 42]] * 200) + res = fsd.fused_specdecode_generate( + adapter, drafter, gen_tokens=120, block_size=4, eos_ids=(), + allow_greedy_fallback=False, stop_on_runaway=False, + **_loop_kwargs(drafter)) + assert res["stopped_on_runaway"] is False + assert len(res["tokens"]) == 120 # ran to the full budget + + def test_sliding_ring_would_wrap_detects_wrap(): # offset + n_new >= max_size -> the rotating ring becomes non-trimmable. cache = [_FakeRotating(offset=1022, max_size=1024)] diff --git a/tests/inference_engine/bridge/test_manifest.py b/tests/inference_engine/bridge/test_manifest.py index 090f189e..4d29a30e 100644 --- a/tests/inference_engine/bridge/test_manifest.py +++ b/tests/inference_engine/bridge/test_manifest.py @@ -82,9 +82,13 @@ def test_allowlist_contains_exactly_the_documented_presets(): "mlx-env-probe", "mlx-kakeya-chat-smoke", "mlx-kakeya-chat-stream-probe", + "mlx-kakeya-codegen-degen-probe", + "mlx-kakeya-codegen-guard-validate", "mlx-kakeya-degen-probe", "mlx-kakeya-fused-chat-ftheta", "mlx-kakeya-fused-chat-smoke", + "mlx-kakeya-launcher-dryrun-bash32", + "mlx-kakeya-launcher-full", "mlx-kakeya-launcher-smoke", "mlx-multitenant-pressure", "mlx-upgrade", @@ -107,7 +111,7 @@ def test_harness_presets_validate_reports_others_do_not(): "k3-step2-fused-allmlx", # §4 liveness gate runs on-device for the fused-chat presets too: "mlx-kakeya-fused-chat-smoke", "mlx-kakeya-fused-chat-ftheta", - "mlx-kakeya-launcher-smoke", + "mlx-kakeya-launcher-smoke", "mlx-kakeya-launcher-full", } @@ -167,6 +171,20 @@ def test_mlx_kakeya_launcher_smoke_preset_invokes_launcher(): assert argv[argv.index("--max-new-tokens") + 1] == "64" +def test_mlx_kakeya_launcher_full_preset_runs_full_mode_past_wrap(): + request = parse_manifest(_manifest( + preset="mlx-kakeya-launcher-full", params={"max_new_tokens": "1300"})) + (argv,) = build_commands(request, {}) + assert argv[0] == "bash" + assert argv[1].endswith("run_kakeya_mac.sh") + # FULL mode: NO --fast (f_θ verifier+proposer+f_θ path). + assert "--fast" not in argv + assert "--chat-scripted" in argv + assert "--ignore-turn-stop" in argv + # budget crosses the ~1024 native-cache ring wrap. + assert int(argv[argv.index("--max-new-tokens") + 1]) > 1024 + + def test_mlx_kakeya_fused_chat_ftheta_preset_runs_f_theta_path(): request = parse_manifest(_manifest( preset="mlx-kakeya-fused-chat-ftheta", diff --git a/tests/inference_engine/bridge/test_runner_python.py b/tests/inference_engine/bridge/test_runner_python.py new file mode 100644 index 00000000..8a0a6bb5 --- /dev/null +++ b/tests/inference_engine/bridge/test_runner_python.py @@ -0,0 +1,108 @@ +"""Unit tests for the mac-bridge workload interpreter pinning (Layers B/C). + +Pure / dependency-injected logic from ``inference_engine.bridge.runner_python``; +the CLI ``scripts/mac_bridge/run_preset.py`` is a thin caller (coverage-exempt). +""" + +from __future__ import annotations + +from inference_engine.bridge.runner_python import ( + GATE_MODULE, + SKILL_DOC, + ResolvedPython, + gate_error_message, + preset_requires_gate, + resolve_workload_python, + substitute_python, + workload_python_candidates, +) + + +# --------------------------------------------------------------------------- # +# workload_python_candidates +# --------------------------------------------------------------------------- # +def test_candidates_prioritise_pin_then_venvs_then_path(): + env = {"KAKEYA_MAC_PYTHON": "/pin/bin/python"} + which = {"python3.13": "/usr/bin/python3.13", "python3": "/usr/bin/python3"}.get + cands = workload_python_candidates( + env, which=which, expanduser=lambda p: p.replace("~", "/home/me")) + assert cands == [ + "/pin/bin/python", + "/home/me/kakeya-venv/bin/python", + "/home/me/.venv/bin/python", + "/usr/bin/python3.13", + "/usr/bin/python3", + ] + + +def test_candidates_drop_empty_and_dedupe(): + # no pin, python3.13 missing, and python3 == an expanded venv path (dedupe). + env: dict = {} + which = {"python3.13": None, "python3": "/home/me/.venv/bin/python"}.get + cands = workload_python_candidates( + env, which=which, expanduser=lambda p: p.replace("~", "/home/me")) + assert cands == [ + "/home/me/kakeya-venv/bin/python", + "/home/me/.venv/bin/python", + ] + assert None not in cands + + +# --------------------------------------------------------------------------- # +# resolve_workload_python +# --------------------------------------------------------------------------- # +def test_resolve_picks_first_importable(): + cands = ["/a/py", "/b/py", "/c/py"] + r = resolve_workload_python(cands, lambda p: p == "/b/py", pinned="/a/py") + assert r == ResolvedPython(path="/b/py", gate_module_ok=True, from_pin=False) + + +def test_resolve_marks_from_pin_when_pinned_is_importable(): + r = resolve_workload_python(["/pin/py", "/x/py"], lambda p: True, + pinned="/pin/py") + assert r.path == "/pin/py" and r.gate_module_ok is True and r.from_pin is True + + +def test_resolve_falls_back_to_first_when_none_importable(): + r = resolve_workload_python(["/a/py", "/b/py"], lambda p: False, + pinned="/a/py") + assert r == ResolvedPython(path="/a/py", gate_module_ok=False, from_pin=True) + + +def test_resolve_returns_none_without_candidates(): + assert resolve_workload_python([], lambda p: True) is None + + +# --------------------------------------------------------------------------- # +# preset_requires_gate +# --------------------------------------------------------------------------- # +def test_gate_required_for_mlx_and_k3_engine_presets(): + assert preset_requires_gate("mlx-kakeya-launcher-full") is True + assert preset_requires_gate("k3-step2-fused") is True + + +def test_gate_skips_diagnostic_and_installer_and_non_engine(): + assert preset_requires_gate("mlx-env-probe") is False # diagnostic + assert preset_requires_gate("mlx-upgrade") is False # installer + assert preset_requires_gate("integration-tests") is False + assert preset_requires_gate("agent-capacity-stress") is False + + +# --------------------------------------------------------------------------- # +# substitute_python / gate_error_message +# --------------------------------------------------------------------------- # +def test_substitute_rewrites_only_leading_bare_python3(): + assert substitute_python(["python3", "a.py", "--x"], "/v/py") == [ + "/v/py", "a.py", "--x"] + # non-python3 argv0 (e.g. the launcher) is untouched. + assert substitute_python(["bash", "run.sh"], "/v/py") == ["bash", "run.sh"] + # empty argv is safe. + assert substitute_python([], "/v/py") == [] + + +def test_gate_error_message_names_module_preset_and_skill(): + msg = gate_error_message("mlx-kakeya-launcher-full", "/usr/bin/python3") + assert GATE_MODULE in msg + assert "mlx-kakeya-launcher-full" in msg + assert "/usr/bin/python3" in msg + assert SKILL_DOC in msg