fix(s31): close 1 review MAJOR — VECLIB_MAXIMUM_THREADS + honest determinism claim + the missing pin guard

This commit is contained in:
Kjell Tore Guttormsen 2026-07-25 12:52:52 +02:00
commit fb4c593924
2 changed files with 83 additions and 9 deletions

View file

@ -26,6 +26,7 @@ system's own echo of the query can no longer outrank genuine expert prose.
from __future__ import annotations
import ast
import os
import subprocess
import sys
from pathlib import Path
@ -106,6 +107,54 @@ def test_semretrieval_import_and_rank_pull_in_no_maf_and_no_verdicts() -> None:
assert result.returncode == 0, result.stderr
def test_blas_thread_pins_are_set_before_numpy_is_imported() -> None:
"""SC3 GUARD — the determinism claim's one untested half, until now.
Two properties, both checked in a FRESH subprocess that execs the module standalone:
1. all four thread variables are set to ``"1"`` after the module body runs;
2. ``numpy`` is absent from ``sys.modules`` BEFORE the module's env writes execute — the
ordering the whole mechanism depends on, since every backend latches its variable by the
time the numpy import completes.
``VECLIB_MAXIMUM_THREADS`` is the one that BITES on this build (numpy links Accelerate); the
other three cover OpenBLAS/MKL/OpenMP deployments. Before it was added this block pinned
nothing at all here, and no test could tell ``grep -rn 'OPENBLAS\\|OMP_NUM\\|MKL_NUM' tests/``
returned zero hits.
Detach point: move any ``os.environ.setdefault`` below the ``import numpy`` line RED."""
check = (
"import importlib.util, sys\n"
"assert 'numpy' not in sys.modules, 'numpy was imported before the probe started'\n"
f"spec = importlib.util.spec_from_file_location('semretrieval_pins', {str(_SEMRETRIEVAL)!r})\n"
"mod = importlib.util.module_from_spec(spec)\n"
"\n"
"# Trip-wire: record whether numpy was already imported at the moment the env writes ran.\n"
"import os\n"
"seen = {}\n"
"real_setdefault = os.environ.setdefault\n"
"def spy(key, value):\n"
" seen.setdefault(key, 'numpy' in sys.modules)\n"
" return real_setdefault(key, value)\n"
"os.environ.setdefault = spy\n"
"try:\n"
" spec.loader.exec_module(mod)\n"
"finally:\n"
" os.environ.setdefault = real_setdefault\n"
"\n"
"for var in ('VECLIB_MAXIMUM_THREADS', 'OPENBLAS_NUM_THREADS', 'OMP_NUM_THREADS',\n"
" 'MKL_NUM_THREADS'):\n"
" assert os.environ.get(var) == '1', var + ' is ' + repr(os.environ.get(var))\n"
" assert var in seen, var + ' was never pinned by the module body'\n"
" assert seen[var] is False, var + ' was pinned AFTER numpy was imported'\n"
"assert 'numpy' in sys.modules, 'the module never imported numpy — probe is not exercising it'\n"
)
env = {
k: v for k, v in os.environ.items() if not k.endswith(("_NUM_THREADS", "_MAXIMUM_THREADS"))
}
result = subprocess.run([sys.executable, "-c", check], capture_output=True, text=True, env=env)
assert result.returncode == 0, result.stderr
def test_semretrieval_imports_no_network_modules() -> None:
"""SC8 — the offline default must not be able to reach the network. Detach point: add
``import urllib.request`` (or any ``_NETWORK_ROOTS`` member) to semretrieval.py RED."""