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++

setup: an earlier config that does not parse is skipped, not a crash; configs are written atomically (#459)

Niko1221ce2d83b62daa
diff --git a/setup.py b/setup.py
index 67fdd4f5a..c8aba719c 100644
--- a/setup.py
+++ b/setup.py
@@ -607,7 +607,7 @@ def offer_together(cfg_path: Path, cfg: dict, yes: bool) -> dict:
ok("from now on this model runs on " + " + ".join(gpu_name(g) for g in pair))
else:
ok("staying on one GPU (START-HERE.bat --gpus " + ",".join(str(g["index"]) for g in pair) + " switches)")
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
return cfg
@@ -2105,7 +2105,7 @@ def repoint_config(cfg_file: Path, old: Path, new: Path) -> None:
new_cfg = fix(cfg)
if new_cfg != cfg:
- cfg_file.write_text(json.dumps(new_cfg, indent=1), encoding="utf-8")
+ write_config(cfg_file, new_cfg)
def data_folder(requested: str | None) -> tuple:
@@ -2162,13 +2162,39 @@ def data_folder(requested: str | None) -> tuple:
return dest, elsewhere
+def write_config(path: Path, cfg: dict):
+ """A run config, written whole or not at all (#459): to a temporary file first, then moved over the old one, so
+ a setup stopped half-way (a closed window, a full disk) never leaves an empty strata-*.json behind."""
+ tmp = path.with_name(path.name + ".tmp")
+ tmp.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ os.replace(tmp, path)
+
+
+def readable_config(path: Path) -> bool:
+ """#459: a config that parses as a JSON object; any other gets a one-line warning naming it."""
+ text = None
+ try:
+ text = path.read_text(encoding="utf-8-sig")
+ if isinstance(json.loads(text), dict):
+ return True
+ why = "not a JSON object"
+ except OSError as e:
+ why = e.strerror or str(e)
+ except ValueError: # JSONDecodeError, or bytes that are not UTF-8
+ why = "the file is empty" if text is not None and not text.strip() else "not valid JSON"
+ warn(f"skipped the earlier config {path} ({why}): setting this copy up without it")
+ return False
+
+
def previous_config(elsewhere_first: list, settings: dict):
- """The most recently used model config of another Strata folder on this PC, for a folder that has none yet."""
+ """The most recently used model config of another Strata folder on this PC, for a folder that has none yet. One
+ that does not parse (an empty or cut-off file, #459) is skipped with a warning: the newest readable one is used,
+ and with none this copy is set up as a fresh install."""
cands = []
for folder in [*elsewhere_first, *other_installs(settings)]:
cands += list(folder.glob("strata-*.json"))
cands = [c for c in dict.fromkeys(cands) if c.is_file()]
- return max(cands, key=lambda p: p.stat().st_mtime) if cands else None
+ return next((c for c in sorted(cands, key=lambda p: p.stat().st_mtime, reverse=True) if readable_config(c)), None)
def choices_from_config(cfg_path: Path) -> dict:
@@ -2261,7 +2287,7 @@ def calibrate_config(cfg_path: Path) -> bool:
warn(f"the tuning did not finish ({e}): the default settings stay")
return False
cfg["args"] = CAL.apply(cfg["args"], res["settings"])
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
st = load_settings()
st.setdefault("calibration", {})[hardware_key(cfg)] = {"settings": res["settings"], "tok_s": res["report"].get("tok_s"),
"date": time.strftime("%Y-%m-%d")}
@@ -2301,7 +2327,7 @@ def upgrade_config(cfg_path: Path, cfg: dict) -> dict:
changed = True
ok("WSL: KV streaming off (the driver pins only about 1 GB of RAM); the KV cache stays in VRAM")
if changed:
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
return cfg
@@ -2315,7 +2341,7 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_
keep = {k: v for k, v in (keep or {}).items() if v is not None}
if keep and any(cfg.get(k) != v for k, v in keep.items()): # #179: a --host/--api-key on a start was ignored
cfg.update(keep)
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
ok("saved for this model: " + ", ".join("api key" if k == "api_key" else f"{k.replace('_', ' ')} {v}"
for k, v in keep.items()))
cfg_path.touch() # the most recently used model
@@ -2334,7 +2360,7 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_
"set it up for these cards: ./setup.sh --setup --backend hip --gpus " + ",".join(map(str, gpu)))
cfg["gpu"], cfg["gpus_asked"] = gpu, True
cfg["layer_split"] = layer_split or cfg.get("layer_split") or "auto"
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
gpu = None
elif gpu is not None:
cmd += ["--gpu", str(gpu)]
@@ -2357,7 +2383,7 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_
check_gpus(gpu, found, yes=yes, named=True)
cfg["gpu"], cfg["gpus_asked"] = gpu, True
cfg["layer_split"] = layer_split or cfg.get("layer_split") or "auto"
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
gpu = None
elif gpu is not None: # --gpu N: this start only, on that card
check_gpus([gpu], found)
@@ -2366,7 +2392,7 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_
cfg = offer_together(cfg_path, cfg, yes)
use = gpu if gpu is not None else cfg.get("gpu")
if isinstance(use, list) and split_mmap(cfg): # #364 #384: a resident low-RAM config on several GPUs
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
if cfg.get("backend") == "hip":
pass
elif isinstance(use, list):
@@ -2471,7 +2497,7 @@ def ensure_engine_for(cards, cfg_path: Path, cfg: dict, yes: bool) -> dict:
build_engine({**main, "archs": archs}, vision, yes, get_llama_cpp())
dirs = json.loads(info.read_text()).get("cuda_dirs") or []
cfg["lib_dirs"] = dirs + [d for d in cfg.get("lib_dirs") or [] if d not in dirs]
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
return cfg
@@ -3217,7 +3243,7 @@ def main() -> int:
import calibrate as CAL
cfg["args"] = CAL.apply(cfg["args"], cal.get("settings") or {})
ok("the settings tuned for this PC earlier are used" + (f" ({cal['date']})" if cal.get("date") else ""))
- cfg_path.write_text(json.dumps(cfg, indent=1), encoding="utf-8")
+ write_config(cfg_path, cfg)
script = write_run_script(tag, cfg_path, port)
# offered only when someone answers: --yes installs and adopted earlier installs are not held up by it
if cal is None and not hip and not a.no_start and not a.yes and ask(
diff --git a/tools/test_setup_risk.py b/tools/test_setup_risk.py
index 05aa399ec..39e0defc2 100644
--- a/tools/test_setup_risk.py
+++ b/tools/test_setup_risk.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import contextlib
import io
import json
+import os
import sys
import tempfile
import unittest
@@ -135,6 +136,53 @@ class Context(unittest.TestCase):
self.assertTrue(started.called)
+class BrokenEarlierConfig(unittest.TestCase):
+ """#459: a new copy of Strata set up like an earlier install skips an earlier config that does not parse (an
+ empty strata-*.json crashed START-HERE with JSONDecodeError) and goes on as a fresh install; configs are written
+ whole (a temporary file moved over the old one)."""
+ RAM64, GPU32 = PROFILES["64GB-1x32GB"]
+
+ def setup_with(self, files):
+ with tempfile.TemporaryDirectory() as d:
+ for i, (name, text) in enumerate(files): # in order, oldest first
+ (Path(d) / name).write_text(text, encoding="utf-8")
+ os.utime(Path(d) / name, (1_700_000_000 + i, 1_700_000_000 + i))
+ return install(self.RAM64, self.GPU32, [], extra=[
+ mock.patch.object(setup, "other_installs", lambda settings: [Path(d)]),
+ mock.patch.object(setup, "start", mock.Mock(return_value=0))])
+
+ def test_an_empty_config_is_skipped(self):
+ for text, why in (("", "the file is empty"), ("{\"args\": [", "not valid JSON"), ("[1]", "not a JSON object")):
+ with self.subTest(text=text):
+ code, out, cfg, _ = self.setup_with([("strata-iq3_s.json", text)])
+ self.assertEqual(code, 0, out)
+ self.assertIn("skipped the earlier config ", out)
+ self.assertIn(f"strata-iq3_s.json ({why}): setting this copy up without it", out)
+ self.assertNotIn("Found your earlier install", out)
+ self.assertIsNotNone(cfg) # the fresh install's config
+
+ def test_the_newest_readable_config_is_used(self):
+ good = json.dumps({"args": ["--max-context", "262144", "--kv", "int8"], "port": 8080})
+ code, out, cfg, _ = self.setup_with([("strata-iq3_s.json", good), ("strata-q2_0.json", "")])
+ self.assertEqual(code, 0, out)
+ self.assertIn("strata-q2_0.json (the file is empty)", out)
+ self.assertIn("Found your earlier install", out)
+ self.assertIn("(iq3_s)", out)
+ self.assertEqual(arg(cfg, "--max-context"), "262144")
+
+ def test_write_config_leaves_no_partial_file(self):
+ with tempfile.TemporaryDirectory() as d:
+ p = Path(d) / "strata-iq3_s.json"
+ p.write_text("old", encoding="utf-8")
+ setup.write_config(p, {"args": ["--kv", "int8"]})
+ self.assertEqual(p.read_text(encoding="utf-8"), json.dumps({"args": ["--kv", "int8"]}, indent=1))
+ self.assertEqual([f.name for f in Path(d).iterdir()], ["strata-iq3_s.json"]) # no .tmp left
+ with mock.patch.object(Path, "write_text", side_effect=OSError(28, "No space left on device")):
+ with self.assertRaises(OSError):
+ setup.write_config(p, {"args": []})
+ self.assertEqual(json.loads(p.read_text(encoding="utf-8")), {"args": ["--kv", "int8"]}) # kept whole
+
+
class LowRamGpus(unittest.TestCase):
"""S2/S3 (#364 #384): one GPU recommended in the low-RAM mode, all of them when asked for."""