Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
resource_plan.py: trust only a v2 SSD cache recorded on this volume (#379)
monotophic04e85d984184
diff --git a/c/resource_plan.py b/c/resource_plan.pyindex f35309bc..fa6ef49d 100644--- a/c/resource_plan.py+++ b/c/resource_plan.py@@ -143,17 +143,56 @@ def memory_available():return 0+# Strict .coli_ssd grammar -- the byte-for-byte mirror of colibri.c's+# coli_ssd_cache_parse() (keep the two in lockstep; test_resource_plan.py and+# test_ssd_probe.c chew the same vector file, tests/fixtures/ssd_cache_vectors.txt):+# v2: b"v2 <gbs> <st_dev>" single spaces, at most one trailing \n+# legacy: b"<gbs>" the pre-fix format, never trusted+# where <gbs> = digits["."digits] with 0 < gbs < 1000 and <st_dev> = 1..20+# digits fitting unsigned 64-bit; total length <= 64 bytes, no NULs, nothing+# else. float() permissiveness ("inf", "nan", "1e99", whitespace, signs) is+# deliberately out: it let corrupt caches surface as measurements, and "inf"+# reached doctor's JSON as the invalid literal Infinity.+_SSD_CACHE_V2 = re.compile(rb"\Av2 (\d+(?:\.\d+)?) (\d{1,20})\n?\Z")+_SSD_CACHE_LEGACY = re.compile(rb"\A(\d+(?:\.\d+)?)\n?\Z")+++def parse_ssd_cache(data):+ """Classify raw .coli_ssd bytes under the strict grammar above. Returns+ ("v2", gbs, st_dev), ("legacy", gbs, None), or (None, None, None) for+ garbage. Classification only -- trust (the st_dev match) is the caller's."""+ if not data or len(data) > 64 or b"\x00" in data:+ return (None, None, None)+ match = _SSD_CACHE_V2.match(data)+ if match:+ gbs, dev = float(match.group(1)), int(match.group(2))+ if 0 < gbs < 1000 and dev <= 0xFFFFFFFFFFFFFFFF:+ return ("v2", gbs, dev)+ return (None, None, None)+ match = _SSD_CACHE_LEGACY.match(data)+ if match:+ gbs = float(match.group(1))+ if 0 < gbs < 1000:+ return ("legacy", gbs, None)+ return (None, None, None)++def read_ssd_probe(model_dir):"""Read the cached F_NOCACHE storage probe the C engine writes to- <model>/.coli_ssd on its first Metal+darwin startup (colibri.c coli_ssd_probe_cached,- issue #379). Read-only: never re-measures, never guesses -- mirrors S4's- "read-and-display only" contract for `coli doctor`/`coli plan`. Returns the- measured GB/s as a float, or None if the file is absent or unparsable."""+ <model>/.coli_ssd on its first Metal+darwin startup (colibri.c+ coli_ssd_probe_cached, issue #379). Read-only: never re-measures, never+ guesses -- mirrors S4's "read-and-display only" contract for `coli+ doctor`/`coli plan`. Returns the measured GB/s as a float, or None.+ Trust matches the engine exactly: only a v2 cache whose recorded st_dev+ still matches the model dir's volume counts -- a legacy bare number or a+ cache carried to another drive is one the engine will re-measure, so+ surfacing it here would show a number the engine no longer believes."""try:- text = (Path(model_dir) / ".coli_ssd").read_text(encoding="utf-8").strip()- value = float(text.split()[0])- return value if value >= 0 else None- except (OSError, ValueError, IndexError):+ kind, gbs, dev = parse_ssd_cache((Path(model_dir) / ".coli_ssd").read_bytes())+ if kind == "v2" and dev == os.stat(model_dir).st_dev:+ return gbs+ return None+ except OSError:return Nonediff --git a/c/tests/test_doctor.py b/c/tests/test_doctor.pyindex 6c747a46..675336a5 100644--- a/c/tests/test_doctor.py+++ b/c/tests/test_doctor.py@@ -1,4 +1,5 @@import json+import osimport structimport subprocessimport sys@@ -156,11 +157,23 @@ class DoctorTest(unittest.TestCase):self.assertEqual(checks["storage.ssd_probe"]["status"], "skip")def test_ssd_probe_check_passes_and_reports_cached_value(self):- (self.model / ".coli_ssd").write_text("14.3\n")+ (self.model / ".coli_ssd").write_text(f"v2 14.3 {os.stat(self.model).st_dev}\n")checks = self.checks_by_id(self.report())self.assertEqual(checks["storage.ssd_probe"]["status"], "pass")self.assertEqual(checks["storage.ssd_probe"]["details"]["gbs"], 14.3)+ def test_ssd_probe_check_never_emits_json_infinity(self):+ # float("inf") used to sail through the old reader; json.dumps renders+ # it as the bare literal Infinity, which is not JSON -- machine+ # consumers of `coli doctor --json` would then fail to parse the whole+ # report. The strict v2 grammar bans inf/nan outright (#386 fix round).+ (self.model / ".coli_ssd").write_text("inf\n")+ report = self.report()+ checks = self.checks_by_id(report)+ self.assertEqual(checks["storage.ssd_probe"]["status"], "skip")+ encoded = json.dumps(report, indent=2, allow_nan=False) # raises on inf/nan+ json.loads(encoded)+def test_text_format_contains_checks_plan_and_result(self):output = format_doctor(self.report())diff --git a/c/tests/test_resource_plan.py b/c/tests/test_resource_plan.pyindex ee56c7da..608043c0 100644--- a/c/tests/test_resource_plan.py+++ b/c/tests/test_resource_plan.py@@ -1,4 +1,5 @@import json+import osimport structimport subprocessimport sys@@ -15,6 +16,7 @@ from resource_plan import (environment_for_plan,format_plan,memory_available,+ parse_ssd_cache,physical_cpu_count,read_ssd_probe,)@@ -76,14 +78,33 @@ class ResourcePlanTest(unittest.TestCase):# #379: read_ssd_probe reads the cached F_NOCACHE measurement colibri.c writes to# <model>/.coli_ssd on its first Metal+darwin startup. This is the read-only# Python-side half of the probe contract (S4) -- never re-measures, never- # guesses, so every case here is pure file parsing.+ # guesses, so every case here is pure file parsing. Trust mirrors the+ # engine: only a v2 cache recorded on THIS volume (matching st_dev) counts.+ def _write_v2_cache(self, gbs="14.322", dev=None):+ if dev is None:+ dev = os.stat(self.model).st_dev+ (self.model / ".coli_ssd").write_text(f"v2 {gbs} {dev}\n")+def test_ssd_probe_missing_file_returns_none(self):self.assertIsNone(read_ssd_probe(self.model))- def test_ssd_probe_reads_cached_value(self):- (self.model / ".coli_ssd").write_text("14.322\n")+ def test_ssd_probe_reads_cached_v2_value(self):+ self._write_v2_cache("14.322")self.assertEqual(read_ssd_probe(self.model), 14.322)+ def test_ssd_probe_foreign_volume_v2_returns_none(self):+ # A model dir rsync'd to another drive carries the OLD volume's+ # measurement; the engine re-probes it, so doctor/plan must not show it.+ self._write_v2_cache("14.322", dev=os.stat(self.model).st_dev + 1)+ self.assertIsNone(read_ssd_probe(self.model))++ def test_ssd_probe_legacy_bare_number_returns_none(self):+ # Pre-v2 caches were written before cold-range steering existed, so the+ # value may be page-cache contamination; the engine re-probes and+ # upgrades, and until then there is no number worth surfacing.+ (self.model / ".coli_ssd").write_text("14.322\n")+ self.assertIsNone(read_ssd_probe(self.model))+def test_ssd_probe_unparsable_file_returns_none(self):(self.model / ".coli_ssd").write_text("not-a-number\n")self.assertIsNone(read_ssd_probe(self.model))@@ -92,15 +113,46 @@ class ResourcePlanTest(unittest.TestCase):(self.model / ".coli_ssd").write_text("")self.assertIsNone(read_ssd_probe(self.model))- def test_ssd_probe_negative_value_returns_none(self):- # colibri.c never caches a negative number (a failed probe just doesn't- # write the file), but a hand-edited/corrupt file should still be- # rejected rather than surfaced as a bogus "fast" reading.- (self.model / ".coli_ssd").write_text("-1\n")- self.assertIsNone(read_ssd_probe(self.model))+ def test_ssd_probe_grammar_matches_c_reader_vectors(self):+ # THE parity pin (#386 fix round): parse_ssd_cache() must accept exactly+ # the grammar colibri.c's coli_ssd_cache_parse() accepts. Both suites+ # consume the same vector file; edit it and both sides re-judge.+ vectors = Path(__file__).parent / "fixtures" / "ssd_cache_vectors.txt"+ unescape = {"n": b"\n", "r": b"\r", "t": b"\t", "0": b"\x00",+ "s": b" ", "\\": b"\\"}+ checked = 0+ for raw in vectors.read_text(encoding="utf-8").splitlines():+ if not raw or raw.startswith("#"):+ continue+ fields = raw.split("\t")+ payload = b""+ escaped = fields[-1] if len(fields) > 1 else ""+ i = 0+ while i < len(escaped):+ if escaped[i] == "\\" and i + 1 < len(escaped):+ self.assertIn(escaped[i + 1], unescape, f"bad escape in: {raw!r}")+ payload += unescape[escaped[i + 1]]+ i += 2+ else:+ payload += escaped[i].encode("utf-8")+ i += 1+ kind, gbs, dev = parse_ssd_cache(payload)+ if fields[0] == "garbage":+ self.assertEqual((kind, gbs, dev), (None, None, None),+ f"garbage accepted: {payload!r} -> {(kind, gbs, dev)}")+ elif fields[0] == "legacy":+ self.assertEqual((kind, gbs, dev), ("legacy", float(fields[1]), None),+ f"legacy misread: {payload!r}")+ elif fields[0] == "v2":+ self.assertEqual((kind, gbs, dev), ("v2", float(fields[1]), int(fields[2])),+ f"v2 misread: {payload!r}")+ else:+ self.fail(f"unknown vector kind: {fields[0]}")+ checked += 1+ self.assertGreaterEqual(checked, 40, "vector file suspiciously short")def test_ssd_probe_surfaces_in_plan_and_format(self):- (self.model / ".coli_ssd").write_text("14.3\n")+ self._write_v2_cache("14.3")plan = build_plan(self.model, available_memory=16 * GB, available_disk=1)self.assertEqual(plan["ssd_probe_gbs"], 14.3)self.assertIn("14.3 GB/s", format_plan(plan))