okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-ds-out ac306afb2 into f-own-intaff3a132f

main
12.1 KiB240 linesraw
1"""Every place a DeepSeek model can still come in (DeepSeek removed in release B; strata_control/retired.py) refuses it in plain words, at
2once, and leaves the person's config and files exactly as they were.  No model is needed: fake configs, folders and GGUF headers."""
3from __future__ import annotations
4
5import argparse
6import contextlib
7import hashlib
8import io
9import json
10import struct
11import sys
12import tempfile
13import unittest
14from pathlib import Path
15from unittest import mock
16
17ROOT = Path(__file__).resolve().parents[2]
18sys.path.insert(0, str(ROOT))
19
20from strata_control import cfg_paths, models, retired, runtime  # noqa: E402
21from strata_control.profiles import ProfileError  # noqa: E402
22
23D1_HEAD = "is a DeepSeek model. This version of Strata no longer runs DeepSeek models, so it was not started."
24
25
26def sha(p: Path) -> str:
27    return hashlib.sha256(p.read_bytes()).hexdigest()
28
29
30def ds_install(d: Path, *, model_bytes: int = 3_000_000) -> tuple[Path, Path, Path]:
31    """(config, model folder, pack) of a fake DeepSeek install as setup 0.2.13 wrote it."""
32    models_dir = d / "data" / "models" / "deepseek-v4.1-flash-ist-das-2.00bpw"
33    models_dir.mkdir(parents=True)
34    (models_dir / "DeepSeek-V4.1-Flash-1017-GSQ-RCO-IQ2_XXS-00001-of-00004.gguf").write_bytes(b"\0" * model_bytes)
35    pack = d / "data" / "packs" / "deepseek"
36    pack.mkdir(parents=True)
37    (pack / "ds4.txt").write_text(f"format gguf\nmodel_dir {models_dir}\n", encoding="utf-8")
38    eng = d / "engine-dsv41" / "0.1.59-linux-x86_64-cuda-cuda13"
39    eng.mkdir(parents=True)
40    exe = eng / "strata-dsv41"
41    exe.write_text("#!/bin/sh\necho started > \"$0.ran\"\n", encoding="utf-8")
42    cfg = {"exe": str(exe), "args": ["--pack", str(pack), "--max-context", "131072", "--eos", "1"], "cwd": str(d),
43           "tokenizer": str(pack / "tokenizer"), "model_name": "deepseek-v4.1-flash", "arch": "deepseek41", "port": 8080,
44           "lib_dirs": [str(d / "components" / "engine" / "0.1.50-linux-x86_64-cuda" / "lib")]}
45    path = d / "strata-deepseek-v4.1-flash-ist-das-2.00bpw.json"
46    path.write_text(json.dumps(cfg, indent=2), encoding="utf-8")
47    return path, models_dir, pack
48
49
50class StartRefused(unittest.TestCase):
51    """The app and `strata start` (and a saved `strata launch` profile) start through runtime.start."""
52
53    def test_runtime_start_refuses_before_anything_is_written(self):
54        with tempfile.TemporaryDirectory() as d:
55            path, models_dir, pack = ds_install(Path(d))
56            before = sha(path)
57            profile = {"schema_version": 1, "id": "launch-ds", "source_config": str(path), "overrides": {}}
58            with mock.patch.object(runtime.profiles, "load", return_value=(profile, "0" * 16)), \
59                    mock.patch.object(runtime, "_repair_config_paths", side_effect=AssertionError("wrote configs")), \
60                    mock.patch.object(runtime.subprocess, "Popen", side_effect=AssertionError("started a process")):
61                with self.assertRaises(ProfileError) as caught:
62                    runtime.start("launch-ds", wait=0)
63            text = str(caught.exception)
64            self.assertTrue(text.startswith("DeepSeek-V4.1-Flash ISTA-DAS 2.00 bpw " + D1_HEAD), text)
65            self.assertIn(f"Its files are still on this computer: 0.0 GB in {models_dir} and {pack}. Nothing was changed or deleted.", text)
66            self.assertTrue(text.endswith("you can delete that folder to free the space."), text)
67            self.assertEqual(sha(path), before)
68            self.assertFalse(Path(json.loads(path.read_text())["exe"] + ".ran").exists())
69
70    def test_a_qwen_profile_is_not_stopped_here(self):
71        with tempfile.TemporaryDirectory() as d:
72            path = Path(d) / "strata-iq3_s.json"
73            path.write_text(json.dumps({"exe": "/x/strata", "args": [], "arch": "qwen3next", "model_name": "q"}), encoding="utf-8")
74            profile = {"schema_version": 1, "id": "app-1", "source_config": str(path), "overrides": {}}
75            with mock.patch.object(runtime.profiles, "load", return_value=(profile, "0" * 16)):
76                runtime._refuse_deepseek("app-1")        # no error
77
78
79class ConfigsNeverWritten(unittest.TestCase):
80    def test_path_repair_leaves_a_deepseek_config_alone(self):
81        with tempfile.TemporaryDirectory() as d:
82            path, _m, _p = ds_install(Path(d))
83            before = sha(path)
84            notes, problems = cfg_paths.repair([path])
85            self.assertEqual((notes, problems), ([], []))
86            self.assertEqual(sha(path), before)
87
88
89class ModelList(unittest.TestCase):
90    """The app's model list and `strata models` read strata_control/models.py."""
91
92    def test_listed_as_retired_without_inspection(self):
93        with tempfile.TemporaryDirectory() as d:
94            path, models_dir, _p = ds_install(Path(d))
95            before = sha(path)
96            with mock.patch.object(models, "inspect_components", side_effect=AssertionError("inspected")):
97                out = models.inspect("deepseek-v4.1-flash-ist-das-2.00bpw", override=d)["model"]
98            self.assertEqual(out["component_status"], "retired")
99            self.assertEqual(out["retired"], "deepseek")
100            self.assertEqual(out["name"], "DeepSeek-V4.1-Flash ISTA-DAS 2.00 bpw")
101            self.assertTrue(out["refusal"].startswith("DeepSeek-V4.1-Flash ISTA-DAS 2.00 bpw " + D1_HEAD))
102            self.assertIn(str(models_dir), out["retired_line"])
103            self.assertEqual(sha(path), before)
104
105    def test_import_of_deepseek_files_is_named(self):
106        with tempfile.TemporaryDirectory() as d:
107            _c, models_dir, _p = ds_install(Path(d))
108            with self.assertRaises(ProfileError) as caught:
109                models.plan_import(str(models_dir))
110            self.assertIn("These are DeepSeek model files (0.0 GB in", str(caught.exception))
111            self.assertIn("nothing in the folder was changed", str(caught.exception))
112
113
114class Server(unittest.TestCase):
115    """serve/server.py started directly (a script, Docker, a gateway) with a DeepSeek config: stops before the engine starts."""
116
117    def run_server(self, cfg: dict) -> tuple[int, str]:
118        from serve import server
119        with tempfile.TemporaryDirectory() as d:
120            p = Path(d) / "c.json"
121            p.write_text(json.dumps(cfg), encoding="utf-8")
122            out = io.StringIO()
123            with contextlib.redirect_stdout(out):
124                code = server.main(["--config", str(p), "--port", "0"])
125            return code, out.getvalue()
126
127    def test_arch(self):
128        code, out = self.run_server({"exe": "/nowhere/strata-dsv41", "args": [], "arch": "deepseek41"})
129        self.assertEqual(code, 2)
130        self.assertIn("this config runs a DeepSeek model (arch deepseek41)", out)
131        self.assertIn("so the server stops here. The model files were not changed.", out)
132
133    def test_tokenizer_without_arch(self):
134        with tempfile.TemporaryDirectory() as d:
135            tok = Path(d) / "tok"
136            tok.mkdir()
137            (tok / "tokenizer.json").write_text(json.dumps({"pre": "deepseek3"}), encoding="utf-8")
138            code, out = self.run_server({"exe": "/nowhere/strata", "args": [], "tokenizer": str(tok)})
139        self.assertEqual(code, 2)
140        self.assertIn("(DeepSeek tokenizer)", out)
141
142
143class Setup(unittest.TestCase):
144    def run_setup(self, *argv: str) -> tuple[int, str]:
145        import setup
146        out = io.StringIO()
147        with mock.patch.object(sys, "argv", ["setup.py", *argv]), contextlib.redirect_stdout(out):
148            code = setup.main()
149        return code, out.getvalue()
150
151    def test_family_and_model_names(self):
152        for argv in (("--family", "deepseek41"), ("--family", "deepseek41", "--model", "IST-DAS-2.00bpw"), ("--model", "IST-DAS-2.25bpw"),
153                     ("--model=ISTA-DAS-Coder",)):
154            code, out = self.run_setup(*argv, "--yes", "--no-start")
155            self.assertEqual(code, 2, argv)
156            self.assertIn("Strata no longer offers DeepSeek models. Run `strata launch` to choose one of the Qwen models.", out)
157
158    def test_config_lists_leave_deepseek_out(self):
159        import setup
160        with tempfile.TemporaryDirectory() as d:
161            path, _m, _p = ds_install(Path(d))
162            qwen = Path(d) / "strata-iq3_s.json"
163            qwen.write_text(json.dumps({"exe": "/x/strata", "args": ["--model", "/m/q.gguf"], "model_name": "q"}), encoding="utf-8")
164            before = sha(path)
165            with mock.patch.object(setup, "home", return_value=Path(d)):
166                self.assertEqual(setup.installed_configs(), [qwen])
167                self.assertEqual(setup.retired_configs(), [path])
168                qwen.unlink()
169                self.assertIsNone(setup.previous_config([Path(d)], {"installs": []}))     # never set up "like" the DeepSeek model
170            self.assertEqual(sha(path), before)
171            self.assertEqual(retired.left_as_is(retired.title(None, path)),
172                             "DeepSeek-V4.1-Flash ISTA-DAS 2.00 bpw: a DeepSeek model, left as it is (this version of Strata no longer "
173                             "runs DeepSeek models).")
174
175
176class Launch(unittest.TestCase):
177    def parse(self, *argv: str) -> argparse.Namespace:
178        from strata_cli.launch import cmd
179        ap = argparse.ArgumentParser()
180        cmd.add_parsers(ap.add_subparsers(dest="command"))
181        return ap.parse_args(["launch", *argv])
182
183    def test_typed_names(self):
184        from strata_cli.launch import cmd
185        for argv in (("--model", "IST-DAS-2.00bpw"), ("--family", "deepseek41", "--model", "UD-Q8_K_XL"), ("--model", "ISTA-DAS-Coder")):
186            with self.assertRaises(ValueError) as caught:
187                cmd.settings_from_args(self.parse(*argv), catalog=object())
188            self.assertEqual(str(caught.exception), retired.NOT_OFFERED, argv)
189
190    def test_saved_preset(self):
191        from strata_cli.launch import cmd, store
192        from strata_cli.launch.settings import LaunchSettings
193        saved = LaunchSettings()
194        saved.family, saved.model = "deepseek41", "IST-DAS-2.00bpw"
195        with mock.patch.object(store, "load", return_value=saved):
196            with self.assertRaises(ValueError) as caught:
197                cmd.settings_from_args(self.parse("--preset", "big"), catalog=object())
198        self.assertEqual(str(caught.exception), retired.NOT_OFFERED)
199
200    def test_each_dropped_deepseek_flag(self):
201        """--dsv41-package, --speed, --full-quality and --dspark stay accepted (an old script, a saved shortcut) and answer D3."""
202        from strata_cli.launch import cmd
203        for argv in (("--model", "IQ3_S", "--dsv41-package", "x.tar.gz"), ("--model", "IQ3_S", "--speed", "faster"),
204                     ("--model", "IQ3_S", "--full-quality"), ("--model", "IQ3_S", "--dspark", "on")):
205            with self.assertRaises(ValueError) as caught:
206                cmd.settings_from_args(self.parse(*argv), catalog=object())
207            self.assertEqual(str(caught.exception), retired.NOT_OFFERED, argv)
208
209    def test_the_dropped_flags_are_not_in_the_help(self):
210        from strata_cli.launch import cmd
211        ap = argparse.ArgumentParser()
212        sub = ap.add_subparsers(dest="command")
213        cmd.add_parsers(sub)
214        text = sub.choices["launch"].format_help()
215        for flag in ("--dsv41-package", "--speed", "--full-quality", "--dspark"):
216            self.assertNotIn(flag, text)
217
218    def test_own_files(self):
219        from strata_cli.launch import runner
220        with tempfile.TemporaryDirectory() as d:
221            _c, models_dir, _p = ds_install(Path(d))
222            fake = mock.Mock()
223            fake.s = mock.Mock(model_path=str(models_dir))
224            with self.assertRaises(runner.LaunchError) as caught:
225                runner.Runner.resolve_own_files(fake)
226            self.assertIn("These are DeepSeek model files", caught.exception.text)
227            self.assertEqual(caught.exception.code, 2)
228
229    def test_qwen_names_pass(self):
230        from strata_cli.launch import cmd
231
232        class Cat:
233            def resolve(self, model, family):
234                return ("qwen", model)
235        s, _ = cmd.settings_from_args(self.parse("--model", "IQ3_S"), catalog=Cat())
236        self.assertEqual((s.family, s.model), ("qwen", "IQ3_S"))
237
238
239if __name__ == "__main__":
240    unittest.main()