okami.mom/strata

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

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

main
7.8 KiB132 linesraw
1"""Settings > Engine and `strata components engines | use | remove` for the engine of a Qwen4 model shape (APP-WIN item 6, owner 11-08 "later, after 0.2.13").
2
3A shape's engine lives in <components>/shapes/<shape>/engine/ (strata_control/shape_engine.py). The list shows one family per installed shape ("shape:<shape>",
4an id the app never shows; the title is None and the app names the group with the model's catalog title), after the Qwen and DeepSeek families. Switching
5pins within the shape's own folder (the same ``.updates/state.json`` engine.pinned the Qwen engine uses, which engine_component.resolve reads at every start of
6a model of that shape); the newest goes back to no pin. Deleting refuses the one in use and one a running program uses with the existing lines. The Strata
7engine's files and state are never touched, and a PC with no shape engine lists exactly what it did before.
8"""
9from __future__ import annotations
10
11import io
12import json
13import contextlib
14from pathlib import Path
15from unittest import mock
16
17from strata_control import engine_component as ec, engine_library as lib, shape_engine, signing, updater
18from tests.terminal.test_shape_engine import SHAPE, Base, tree
19
20FAMILY = "shape:" + SHAPE
21
22
23class ShapeEngines(Base):
24    def setUp(self):
25        super().setUp()
26        for p in (mock.patch.object(lib, "_process_uses", return_value=False),):
27            p.start()
28            self.addCleanup(p.stop)
29
30    def ctx(self):
31        return updater.Context(components=self.comp, source=self.url, trusted=signing.load_trusted_keys(), hardware=self.hw, serving=lambda: [], poll=0.05)
32
33    def install(self, *versions):
34        for i in range(len(versions)):
35            self.publish(*versions[: i + 1], sequence=i + 1)
36            with mock.patch.object(shape_engine, "_test_start", return_value=(True, "x")):
37                self.assertIsNotNone(self.ensure())
38
39    def rows(self):
40        return {e["version"]: e for e in lib.rows_of(lib.list_engines(self.ctx()), FAMILY)}
41
42    def picked(self):
43        return ec.resolve(shape_engine.root(SHAPE, self.comp), backend="cuda", component_id=shape_engine.component_id(SHAPE)).version
44
45    def test_no_shape_engine_lists_what_it_did_before(self):
46        listing = lib.list_engines(self.ctx())
47        self.assertEqual([f["family"] for f in listing["families"]], ["qwen"])
48        self.assertEqual(set(listing["families"][0]), {"family", "title", "engines"})
49
50    def test_an_installed_shape_engine_is_its_own_family_after_the_strata_engine(self):
51        self.install("0.1.59", "0.1.60")
52        listing = lib.list_engines(self.ctx())
53        self.assertEqual([f["family"] for f in listing["families"]], ["qwen", FAMILY])
54        fam = listing["families"][1]
55        self.assertEqual((fam["shape"], fam["title"]), (SHAPE, None), "the app names the group from the catalog; the family id is not a title")
56        rows = self.rows()
57        self.assertEqual(sorted(rows), ["0.1.59", "0.1.60"])
58        self.assertTrue(rows["0.1.60"]["active"] and rows["0.1.60"]["newest"])
59        self.assertTrue(rows["0.1.59"]["can_use"] and rows["0.1.59"]["can_delete"])
60        self.assertEqual(rows["0.1.60"]["delete_blocked"], "This is the engine in use. Switch to another one first.")
61        self.assertEqual({r["family"] for r in rows.values()}, {FAMILY})
62        self.assertEqual(listing["total_bytes"], sum(r["bytes"] for f in listing["families"] for r in f["engines"]))
63        qwen = lib.rows_of(listing, lib.QWEN)
64        self.assertEqual([r["version"] for r in qwen], ["0.1.58"], "the Strata engine is listed as before, alone in its family")
65
66    def test_switch_to_the_older_one_and_back_to_the_newest(self):
67        self.install("0.1.59", "0.1.60")
68        old = self.rows()["0.1.59"]["folder"]
69        out = lib.use_engine(self.ctx(), FAMILY, old)
70        self.assertEqual((out["status"], out["from"], out["to"], out["automatic"]), ("switched", "0.1.60", "0.1.59", False))
71        self.assertEqual(self.picked(), "0.1.59")
72        self.assertEqual(ec.pinned_folder(shape_engine.root(SHAPE, self.comp)), old)
73        self.assertIsNone(ec.pinned_folder(self.comp), "the Strata engine's pin is not touched")
74        self.assertTrue(self.rows()["0.1.59"]["active"], "a fresh read (a restart) sees it")
75        back = lib.use_engine(self.ctx(), FAMILY, self.rows()["0.1.60"]["folder"])
76        self.assertEqual((back["status"], back["automatic"]), ("switched", True))
77        self.assertIsNone(ec.pinned_folder(shape_engine.root(SHAPE, self.comp)), "the newest follows updates again: no pin")
78        self.assertEqual(lib.use_engine(self.ctx(), FAMILY, self.rows()["0.1.60"]["folder"])["status"], "already")
79        self.assertEqual(self.strata_before, tree(self.comp / "engine"))
80
81    def test_delete_the_older_one_and_never_the_one_in_use_or_one_a_program_uses(self):
82        self.install("0.1.59", "0.1.60")
83        rows = self.rows()
84        with self.assertRaises(updater.UpdateError) as e:
85            lib.delete_engine(self.ctx(), FAMILY, rows["0.1.60"]["folder"])
86        self.assertEqual(str(e.exception), "This is the engine in use. Switch to another one first.")
87        with mock.patch.object(lib, "_process_uses", side_effect=lambda folder: folder.name == rows["0.1.59"]["folder"]):
88            with self.assertRaises(updater.UpdateError) as e:
89                lib.delete_engine(self.ctx(), FAMILY, rows["0.1.59"]["folder"])
90        self.assertEqual(str(e.exception), "A running program is using it. Stop the model first.")
91        out = lib.delete_engine(self.ctx(), FAMILY, rows["0.1.59"]["folder"])
92        self.assertEqual((out["status"], out["version"], out["component"]), ("deleted", "0.1.59", shape_engine.component_id(SHAPE)))
93        self.assertEqual(sorted(self.rows()), ["0.1.60"])
94        self.assertEqual(self.picked(), "0.1.60")
95        self.assertEqual(self.strata_before, tree(self.comp / "engine"))
96
97    def test_a_deleted_pinned_engine_leaves_no_pin(self):
98        self.install("0.1.59", "0.1.60")
99        old = self.rows()["0.1.59"]["folder"]
100        lib.use_engine(self.ctx(), FAMILY, old)
101        lib.use_engine(self.ctx(), FAMILY, self.rows()["0.1.60"]["folder"])
102        state = updater.State(shape_engine.root(SHAPE, self.comp))
103        state.section("engine")["pinned"] = old                 # as if a pin was left behind
104        state.save()
105        self.assertEqual(self.picked(), "0.1.59")
106        lib.use_engine(self.ctx(), FAMILY, self.rows()["0.1.60"]["folder"])
107        lib.delete_engine(self.ctx(), FAMILY, old)
108        self.assertIsNone(ec.pinned_folder(shape_engine.root(SHAPE, self.comp)))
109
110    def test_a_family_that_is_not_a_shape_is_refused(self):
111        for bad in ("shape:", "shape:X", "shape:engine", "shapes:q4_lite", "strata-q4-lite"):
112            with self.assertRaises(updater.UpdateError):
113                lib.use_engine(self.ctx(), bad, "0.1.59-x")
114        self.assertEqual(lib.shape_of_family(FAMILY), SHAPE)
115        self.assertIsNone(lib.shape_of_family("qwen"))
116
117    def test_the_terminal_lists_the_shape_by_name_and_takes_the_family_the_app_passes(self):
118        from strata_cli import update_cmd
119        self.assertIn(FAMILY, update_cmd._ENGINE_FAMILIES)
120        self.assertIn("qwen", update_cmd._ENGINE_FAMILIES)
121        self.assertNotIn("shape:Nope", update_cmd._ENGINE_FAMILIES)
122        self.assertEqual(list(update_cmd._ENGINE_FAMILIES), ["qwen"], "help and errors list the Qwen family")
123        self.assertIn("deepseek", update_cmd._ENGINE_FAMILIES)   # accepted, so engine_library answers it in plain words (strata_control/retired.py)
124        self.install("0.1.59")
125        a = mock.Mock(components_command="engines")
126        out = io.StringIO()
127        with contextlib.redirect_stdout(out):
128            update_cmd._engines_command(a, self.ctx(), False, None)
129        text = out.getvalue()
130        self.assertIn("\n" + SHAPE + "\n", text)
131        self.assertNotIn(FAMILY, text)
132        self.assertNotIn("None", text)