Make test suite runnable from clean checkout + cross-platform vram_detect
CI / build (push) Has been cancelled

runnable-test-suite (parent) — complete. Three sub-tasks all complete:

- make-tests-runnable: add requirements.txt pinning pytest==7.4.4, sweep all
  docs/prompts from bare 'python' to 'python3' (stock macOS/Windows ships
  python3), add idempotent .venv install block to scripts/install.sh, and add
  'from __future__ import annotations' to 3 dashboard modules using PEP 604
  union syntax at definition time so they import on Python 3.9+. The
  PEP 604 bug was caught by the streak verifier itself during implementation.

- vram-detect-cross-platform: scripts/vram_detect.py now branches on
  platform.system() for Linux/Darwin/Windows. macOS path uses
  system_profiler SPDisplaysDataType (Apple Silicon unified memory via
  sysctl hw.memsize; Intel Macs via 'VRAM (Total):'). Windows uses wmic
  path win32_VideoController get AdapterRAM with PowerShell fallback. Linux
  /proc/meminfo and nvidia-smi/lspci paths unchanged (regression test locks
  them). Added 14 local-LLM context-window entries (llama-3.1, qwen2.5,
  mistral, deepseek-r1/v3, glm-4/4.5, gemma-2, phi-3/4) with source-cited
  model cards. Added _probe_ollama_model() that runs 'ollama list' as a
  last-resort fallback. run_command() now wraps PowerShell cmdlets on
  Windows (['powershell', '-NoProfile', '-NoLogo', '-Command', ...]).

- vram-detect-cross-platform-tests: 11 new monkeypatched tests in
  tests/test_vram_detect.py covering Linux/Darwin/Windows branches for
  detect_ram and detect_gpu_vram, prefix-match for unknown model names,
  ollama probe, Windows PowerShell wrapper, and a LOCKED regression test
  for _detect_ram_linux(). All external subprocess/sysctl/wmic calls are
  mocked; no live hardware probes. Suite total: 235 passed, 0 errors.

Verified on this box: gpu_vram_gb 0 -> 32 on Apple M5 (32GB unified memory),
target context correctly jumped 12k -> 42k.

Subtask-2 implementation was authored by local LLM (gemma-4-26B-A4B-it
via headroom proxy @ localhost:8787). The 10-consecutive-clean-pass streak
verifier ran as the independent checker model (article #2/#9/#13 in
'WTF Is a Loop? Part 2'). One anti-spin rail fired: local LLM produced
inline branches where subtask-3 tests expected private _detect_ram_linux()
helper; extracted helper to match the test contract without weakening tests.

Parent + all 3 subtasks complete. Prior opencode-subagent implementation
of subtask-2 preserved in git stash for reference.
This commit is contained in:
Lap Tran
2026-06-21 18:28:15 -04:00
parent 1d36c0e4ad
commit f32f98575b
46 changed files with 1499 additions and 84 deletions
+189
View File
@@ -108,3 +108,192 @@ def test_extract_model_from_file_respects_10kb_limit(tmp_path: Path) -> None:
env_file.write_text("x" * 11_000 + "\nMODEL=far-away-model\n")
model = vram._extract_model_from_file(env_file)
assert model is None
# --- Cross-platform fixtures (module-level multiline string constants) ---
MEMINFO_LINUX_16GB = "MemTotal: 16384000 kB\nMemAvailable: 8192000 kB\n"
MEMINFO_LINUX_32GB = "MemTotal: 32768000 kB\nMemAvailable: 16384000 kB\n"
APPLE_M2_PROFILER = """\
Graphics/Displays:
Apple M2:
Chipset Model: Apple M2
Type: GPU
Bus: Built-In
Total Number of Cores: 10
Vendor: Apple (0x106b)
Metal: Supported, version 2
"""
WMIC_VIDEOCONTROLLER = """\
AdapterRAM=8589934592
Name=NVIDIA GeForce RTX 3060
"""
WMIC_COMPUTERSYSTEM = """\
TotalPhysicalMemory=34359738368
"""
OLLAMA_LIST = """\
NAME ID SIZE MODIFIED
llama-3.1-8b abc 4.7GB 2 days ago
"""
class _FakeResult:
"""Minimal stand-in for subprocess.CompletedProcess."""
def __init__(self, stdout: str = "", returncode: int = 0) -> None:
self.stdout = stdout
self.returncode = returncode
def _make_fake_run(responses: dict[str, str]):
"""Build a subprocess.run fake that maps cmd[0] (or joined PS string) to stdout."""
def fake_run(cmd, *args, **kwargs):
name = cmd[0]
if name == "powershell":
joined = cmd[-1] if len(cmd) > 4 else ""
for key, out in responses.items():
if key in joined:
return _FakeResult(out)
return _FakeResult("", 1)
return _FakeResult(responses.get(name, ""))
return fake_run
def _patch_meminfo(monkeypatch, text: str) -> None:
"""Patch Path.exists/read_text to serve `text` for /proc/meminfo only."""
orig_exists = Path.exists
orig_read_text = Path.read_text
def fake_exists(self):
if self.as_posix() == "/proc/meminfo":
return True
return orig_exists(self)
def fake_read_text(self, *args, **kwargs):
if self.as_posix() == "/proc/meminfo":
return text
return orig_read_text(self, *args, **kwargs)
monkeypatch.setattr(Path, "exists", fake_exists)
monkeypatch.setattr(Path, "read_text", fake_read_text)
def test_detect_ram_linux(monkeypatch) -> None:
monkeypatch.setattr(vram.platform, "system", lambda: "Linux")
_patch_meminfo(monkeypatch, MEMINFO_LINUX_16GB)
total, available = vram.detect_ram()
assert (total, available) == (16_384_000, 8_192_000)
def test_detect_ram_macos(monkeypatch) -> None:
monkeypatch.setattr(vram.platform, "system", lambda: "Darwin")
monkeypatch.setattr(vram.shutil, "which", lambda name: name)
monkeypatch.setattr(
vram.subprocess, "run", _make_fake_run({"sysctl": "34359738368"})
)
total, available = vram.detect_ram()
assert total == 33_554_432
assert available == total
def test_detect_ram_windows(monkeypatch) -> None:
monkeypatch.setattr(vram.platform, "system", lambda: "Windows")
monkeypatch.setattr(vram.shutil, "which", lambda name: name)
monkeypatch.setattr(
vram.subprocess, "run", _make_fake_run({"wmic": WMIC_COMPUTERSYSTEM})
)
total, available = vram.detect_ram()
assert total == 33_554_432
def test_detect_gpu_vram_nvidia_linux(monkeypatch) -> None:
monkeypatch.setattr(vram.platform, "system", lambda: "Linux")
monkeypatch.setattr(vram.shutil, "which", lambda name: name)
monkeypatch.setattr(
vram.subprocess, "run", _make_fake_run({"nvidia-smi": "24576\n"})
)
total, per_gpu, num = vram.detect_gpu_vram()
assert (total, per_gpu, num) == (25_165_824, 25_165_824, 1)
def test_detect_gpu_vram_apple_silicon(monkeypatch) -> None:
monkeypatch.setattr(vram.platform, "system", lambda: "Darwin")
monkeypatch.setattr(vram.shutil, "which", lambda name: name)
monkeypatch.setattr(
vram.subprocess,
"run",
_make_fake_run(
{"system_profiler": APPLE_M2_PROFILER, "sysctl": "17179869184"}
),
)
total, per_gpu, num = vram.detect_gpu_vram()
assert total > 0
assert num >= 1
def test_detect_gpu_vram_windows_wmic(monkeypatch) -> None:
monkeypatch.setattr(vram.platform, "system", lambda: "Windows")
monkeypatch.setattr(vram.shutil, "which", lambda name: name)
monkeypatch.setattr(
vram.subprocess, "run", _make_fake_run({"wmic": WMIC_VIDEOCONTROLLER})
)
total, per_gpu, num = vram.detect_gpu_vram()
assert total == 8_388_608
def test_lookup_model_context_unknown_returns_zero() -> None:
assert vram._lookup_model_context("completely-unknown-model") == 0
def test_lookup_model_context_prefix_match() -> None:
assert vram._lookup_model_context("deepseek-r1:7b") == 64_000
assert vram._lookup_model_context("llama-3.1-8b-instruct") == 128_000
def test_detect_model_context_ollama_probe(monkeypatch, tmp_path: Path) -> None:
monkeypatch.setattr(vram.Path, "home", lambda: tmp_path)
monkeypatch.setattr(
vram.shutil, "which", lambda name: name if name == "ollama" else None
)
monkeypatch.setattr(
vram.subprocess, "run", _make_fake_run({"ollama": OLLAMA_LIST})
)
context = vram.detect_model_context(None, tmp_path)
assert context == 128_000
def test_run_command_windows_powershell_wrapper(monkeypatch) -> None:
monkeypatch.setattr(vram.platform, "system", lambda: "Windows")
monkeypatch.setattr(vram.shutil, "which", lambda name: name)
captured: dict = {}
def fake_run(cmd, *args, **kwargs):
captured["cmd"] = cmd
return _FakeResult("ok", 0)
monkeypatch.setattr(vram.subprocess, "run", fake_run)
result = vram.run_command(
["Get-CimInstance", "Win32_VideoController", "-Property", "AdapterRAM"]
)
assert result == "ok"
cmd = captured["cmd"]
assert cmd[0] == "powershell"
assert "-NoProfile" in cmd
assert "-NoLogo" in cmd
assert "-Command" in cmd
assert "Get-CimInstance" in cmd[-1]
assert "Win32_VideoController" in cmd[-1]
def test_detect_ram_linux_regression(monkeypatch) -> None:
"""LOCKED regression guard — exact match on (total_kb, available_kb)."""
monkeypatch.setattr(vram.platform, "system", lambda: "Linux")
_patch_meminfo(monkeypatch, MEMINFO_LINUX_32GB)
total, available = vram._detect_ram_linux()
assert (total, available) == (32_768_000, 16_384_000)