okami.mom/strata

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

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

main
5.4 KiB107 linesraw
1"""A launch, a setup and a model download never update an engine by themselves (owner, 11-01; the same shape as #4823: "downloading a model changed my
2engine without asking").  An engine update is the person's choice, and never beside a loaded model.
3
4* Qwen: `strata launch` with an engine older than the model needs warns and goes on; it updates only on a yes (--yes, or the answer on the screens),
5  and never with a model loaded.  No engine installed at all is still fetched (nothing runs on it yet).
6* The app's model download for Qwen never fetches an engine (control_engine_step), as before.
7"""
8from __future__ import annotations
9
10import ast
11import json
12import sys
13import unittest
14from pathlib import Path
15from unittest import mock
16
17sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
18from strata_cli.launch import settings as S  # noqa: E402
19from strata_cli.launch.runner import Runner  # noqa: E402
20from strata_control import engine_component as ec, engine_update, updater  # noqa: E402
21from tests.terminal.test_launch_engine_first import FakeProc, qwen_catalog, update_events  # noqa: E402
22
23ROOT = Path(__file__).resolve().parents[2]
24OLD = {"usable": True, "engine": {"version": "0.1.45", "backend": "cuda"}, "notice": None}
25NEW = {"usable": True, "engine": {"version": "0.1.50", "backend": "cuda"}, "notice": None}
26NONE = {"usable": False, "engine": None, "notice": "No engine for this PC was found"}
27RUNNING = [{"profile": "launch-demo", "state": "ready"}]
28
29
30class QwenStaleEngine(unittest.TestCase):
31    def ensure(self, statuses, *, serving=(), confirm=None, assume_yes=False):
32        spawns, seen = [], []
33        r = Runner(S.LaunchSettings(family="qwen", model="Q2_0", context=32768), catalog=qwen_catalog(), emit=seen.append,
34                   confirm=confirm, assume_yes=assume_yes)
35        r.check_cancel = lambda: None
36        with mock.patch.object(ec, "status", side_effect=list(statuses)), \
37                mock.patch.object(Runner, "needed_engine", return_value=(0, 1, 50)), \
38                mock.patch.object(updater, "serving_now", return_value=list(serving)), \
39                mock.patch.object(Runner, "_spawn", side_effect=lambda argv, **kw: spawns.append(argv) or FakeProc(update_events("updated"), 0)):
40            r.ensure_engine()
41        return spawns, r, seen
42
43    def test_a_loaded_model_blocks_the_update_even_with_yes(self):
44        for kwargs in ({"assume_yes": True}, {"confirm": lambda *a: True}, {}):
45            spawns, r, seen = self.ensure([OLD], serving=RUNNING, **kwargs)
46            self.assertEqual(spawns, [], kwargs)
47            self.assertTrue(any("running, so Strata did not update the engine" in n for n in r.notices), r.notices)
48            self.assertTrue(any("strata launch --stop" in n for n in r.notices))
49
50    def test_no_yes_no_update(self):
51        spawns, r, _ = self.ensure([OLD])
52        self.assertEqual(spawns, [])
53        self.assertIn("Strata did not update it. Run `strata update` (or start with --yes)", r.notices[0])
54
55    def test_no_to_the_question_no_update(self):
56        asked = []
57        spawns, r, _ = self.ensure([OLD], confirm=lambda t, b: asked.append((t, b)) or False)
58        self.assertEqual(spawns, [])
59        self.assertEqual(len(asked), 1)
60        self.assertIn("older than this model needs", asked[0][1])
61
62    def test_yes_to_the_question_updates(self):
63        spawns, r, _ = self.ensure([OLD, NEW], confirm=lambda t, b: True)
64        self.assertEqual(len(spawns), 1)
65        self.assertEqual(r.notices, [])
66
67    def test_yes_flag_updates_when_nothing_is_loaded(self):
68        spawns, r, _ = self.ensure([OLD, NEW], assume_yes=True)
69        self.assertEqual(len(spawns), 1)
70
71    def test_no_engine_at_all_is_still_fetched_because_nothing_runs_on_it(self):
72        spawns, r, _ = self.ensure([NONE, NEW])
73        self.assertEqual(len(spawns), 1)
74
75    def test_an_engine_that_is_new_enough_asks_nothing(self):
76        asked = []
77        spawns, r, _ = self.ensure([NEW], confirm=lambda t, b: asked.append(t))
78        self.assertEqual((spawns, asked), ([], []))
79
80    def test_plan_says_it_will_ask(self):
81        r = Runner(S.LaunchSettings(family="qwen", model="Q2_0", context=32768), catalog=qwen_catalog(), emit=lambda e: None)
82        with mock.patch.object(ec, "status", return_value=OLD), mock.patch.object(Runner, "needed_engine", return_value=(0, 1, 50)), \
83                mock.patch("strata_cli.launch.runner.install_isolation_words", return_value=[]):
84            words = r.preflight_words()
85        self.assertTrue(any("will ask before updating it" in w for w in words), words)
86
87
88class SourceRules(unittest.TestCase):
89    def calls(self, path, function):
90        tree = ast.parse((ROOT / path).read_text(encoding="utf-8"))
91        names = set()
92        for node in ast.walk(tree):
93            if isinstance(node, ast.FunctionDef) and node.name == function:
94                for inner in ast.walk(node):
95                    if isinstance(inner, ast.Call):
96                        f = inner.func
97                        names.add(f.attr if isinstance(f, ast.Attribute) else f.id if isinstance(f, ast.Name) else "")
98        return names
99
100    def test_the_apps_qwen_model_download_never_fetches_an_engine(self):
101        names = self.calls("setup.py", "control_engine_step")
102        for forbidden in ("engine_from_feed", "get_prebuilt", "get_prebuilt_hip", "update_installed_engine", "run", "urlopen", "check_call", "Popen"):
103            self.assertNotIn(forbidden, names)
104
105
106if __name__ == "__main__":
107    unittest.main()