Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

strata_pack.py build: refuse an output directory that already holds a pack, naming the file

Léon Avic Simmonsf0b083efeda6
diff --git a/tools/strata_pack.py b/tools/strata_pack.py
index 31fbe226e..ec8aa4547 100644
--- a/tools/strata_pack.py
+++ b/tools/strata_pack.py
@@ -203,7 +203,28 @@ def sha256(path: pathlib.Path, chunk: int = 1 << 24) -> str:
return h.hexdigest()
+# The files the engine reads a pack's identity from (src/core/expert_source.cpp, the fingerprint): one of them in
+# --out means the directory already holds a pack, this tool's or iq_pack.py's. experts.bin on its own does not:
+# build writes manifest.json last, so experts.bin without it is a build that did not finish, and is rebuilt.
+PACK_MARKERS = ("manifest.json", "index.txt", "native_experts.txt")
+
+
+def occupied(out_dir: pathlib.Path) -> pathlib.Path | None:
+ """The pack marker already in `out_dir`, or None when the directory is empty, absent or holds no pack."""
+ for name in PACK_MARKERS:
+ if (out_dir / name).exists():
+ return out_dir / name
+ return None
+
+
def build(gguf: pathlib.Path, out_dir: pathlib.Path, n_layers: int | None, skip_hash: bool) -> int:
+ # refused before anything is opened or written: build used to mkdir(exist_ok=True) and overwrite experts.bin,
+ # dense.bin, embd.bin and manifest.json of whatever pack was there, with nothing said
+ marker = occupied(out_dir)
+ if marker is not None:
+ print("%s already holds a pack (%s is there) - refusing to build; delete the directory or pass another "
+ "--out" % (out_dir, marker.name))
+ return 1
out_dir.mkdir(parents=True, exist_ok=True)
g = G.GGUFFile(gguf)
head, flen = open_shard(gguf)
diff --git a/tools/test_shards.py b/tools/test_shards.py
index 6d8ba397a..afe592c99 100644
--- a/tools/test_shards.py
+++ b/tools/test_shards.py
@@ -1,5 +1,6 @@
-"""A missing or short model shard is named with its numbers, and a pack's verify reads back the source hash
-its manifest recorded - over a minimal GGUF written here (no download, no model, no GPU).
+"""A missing or short model shard is named with its numbers, a pack's verify reads back the source hash its
+manifest recorded, and a pack's build refuses an output directory that already holds one - over a minimal GGUF
+written here (no download, no model, no GPU).
python -m unittest tools.test_shards
"""
@@ -115,5 +116,58 @@ class PackVerifyHash(unittest.TestCase):
self.assertEqual(self.run_verify({"shard1_sha256": "0" * 64}, limit=1)[0], 0) # --limit stays quick
+class PackBuildOccupied(unittest.TestCase):
+ """build refuses an --out that already holds a pack before it opens the GGUF or writes a byte; an empty or absent
+ directory goes on into the build. The GGUF reader is replaced by a sentinel, so "went on" is the sentinel
+ raised at the first thing build does after the check (a real build needs the model's shards)."""
+
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.dir = Path(self.tmp.name)
+ self.saved = strata_pack.G.GGUFFile
+ strata_pack.G.GGUFFile = lambda path: (_ for _ in ()).throw(Stop(str(path)))
+
+ def tearDown(self):
+ strata_pack.G.GGUFFile = self.saved
+ self.tmp.cleanup()
+
+ def run_build(self, out):
+ buf = io.StringIO()
+ with contextlib.redirect_stdout(buf):
+ rc = strata_pack.build(self.dir / "m-00001-of-00002.gguf", out, None, True)
+ return rc, buf.getvalue()
+
+ def test_occupied_dir_is_refused_naming_it(self):
+ for marker in ("manifest.json", "index.txt", "native_experts.txt"): # strata_pack.PACK_MARKERS
+ with self.subTest(marker=marker):
+ out = self.dir / marker.replace(".", "-")
+ out.mkdir()
+ (out / marker).write_text("the pack that was there", encoding="utf-8")
+ (out / "experts.bin").write_bytes(b"its experts")
+ rc, text = self.run_build(out)
+ self.assertEqual(rc, 1)
+ self.assertIn(str(out), text)
+ self.assertIn(marker, text)
+ self.assertEqual(sorted(p.name for p in out.iterdir()), sorted(["experts.bin", marker]))
+ self.assertEqual((out / marker).read_text(encoding="utf-8"), "the pack that was there")
+ self.assertEqual((out / "experts.bin").read_bytes(), b"its experts")
+
+ def test_empty_or_absent_dir_proceeds(self):
+ empty = self.dir / "empty"
+ empty.mkdir()
+ for out in (empty, self.dir / "absent" / "pack"):
+ with self.subTest(out=out.name), self.assertRaises(Stop):
+ self.run_build(out)
+ self.assertTrue(out.is_dir())
+
+ def test_unfinished_build_is_rebuilt(self):
+ # experts.bin without manifest.json is a build that stopped before its last file: not a pack, rebuilt as before
+ out = self.dir / "unfinished"
+ out.mkdir()
+ (out / "experts.bin").write_bytes(b"part")
+ with self.assertRaises(Stop):
+ self.run_build(out)
+
+
if __name__ == "__main__":
unittest.main()