okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
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 ): 24def setUp (self ): 25super ().setUp () 26for p in (mock .patch .object (lib ,"_process_uses" ,return_value = False ),): 27p .start () 28self .addCleanup (p .stop ) 29 30def ctx (self ): 31return updater .Context (components = self .comp ,source = self .url ,trusted = signing .load_trusted_keys (),hardware = self .hw ,serving = lambda : [],poll = 0.05 ) 32 33def install (self ,* versions ): 34for i in range (len (versions )): 35self .publish (* versions [:i + 1 ],sequence = i + 1 ) 36with mock .patch .object (shape_engine ,"_test_start" ,return_value = (True ,"x" )): 37self .assertIsNotNone (self .ensure ()) 38 39def rows (self ): 40return {e ["version" ]:e for e in lib .rows_of (lib .list_engines (self .ctx ()),FAMILY )} 41 42def picked (self ): 43return ec .resolve (shape_engine .root (SHAPE ,self .comp ),backend = "cuda" ,component_id = shape_engine .component_id (SHAPE )).version 44 45def test_no_shape_engine_lists_what_it_did_before (self ): 46listing = lib .list_engines (self .ctx ()) 47self .assertEqual ([f ["family" ]for f in listing ["families" ]], ["qwen" ]) 48self .assertEqual (set (listing ["families" ][0 ]), {"family" ,"title" ,"engines" }) 49 50def test_an_installed_shape_engine_is_its_own_family_after_the_strata_engine (self ): 51self .install ("0.1.59" ,"0.1.60" ) 52listing = lib .list_engines (self .ctx ()) 53self .assertEqual ([f ["family" ]for f in listing ["families" ]], ["qwen" ,FAMILY ]) 54fam = listing ["families" ][1 ] 55self .assertEqual ((fam ["shape" ],fam ["title" ]), (SHAPE ,None ),"the app names the group from the catalog; the family id is not a title" ) 56rows = self .rows () 57self .assertEqual (sorted (rows ), ["0.1.59" ,"0.1.60" ]) 58self .assertTrue (rows ["0.1.60" ]["active" ]and rows ["0.1.60" ]["newest" ]) 59self .assertTrue (rows ["0.1.59" ]["can_use" ]and rows ["0.1.59" ]["can_delete" ]) 60self .assertEqual (rows ["0.1.60" ]["delete_blocked" ],"This is the engine in use. Switch to another one first." ) 61self .assertEqual ({r ["family" ]for r in rows .values ()}, {FAMILY }) 62self .assertEqual (listing ["total_bytes" ],sum (r ["bytes" ]for f in listing ["families" ]for r in f ["engines" ])) 63qwen = lib .rows_of (listing ,lib .QWEN ) 64self .assertEqual ([r ["version" ]for r in qwen ], ["0.1.58" ],"the Strata engine is listed as before, alone in its family" ) 65 66def test_switch_to_the_older_one_and_back_to_the_newest (self ): 67self .install ("0.1.59" ,"0.1.60" ) 68old = self .rows ()["0.1.59" ]["folder" ] 69out = lib .use_engine (self .ctx (),FAMILY ,old ) 70self .assertEqual ((out ["status" ],out ["from" ],out ["to" ],out ["automatic" ]), ("switched" ,"0.1.60" ,"0.1.59" ,False )) 71self .assertEqual (self .picked (),"0.1.59" ) 72self .assertEqual (ec .pinned_folder (shape_engine .root (SHAPE ,self .comp )),old ) 73self .assertIsNone (ec .pinned_folder (self .comp ),"the Strata engine's pin is not touched" ) 74self .assertTrue (self .rows ()["0.1.59" ]["active" ],"a fresh read (a restart) sees it" ) 75back = lib .use_engine (self .ctx (),FAMILY ,self .rows ()["0.1.60" ]["folder" ]) 76self .assertEqual ((back ["status" ],back ["automatic" ]), ("switched" ,True )) 77self .assertIsNone (ec .pinned_folder (shape_engine .root (SHAPE ,self .comp )),"the newest follows updates again: no pin" ) 78self .assertEqual (lib .use_engine (self .ctx (),FAMILY ,self .rows ()["0.1.60" ]["folder" ])["status" ],"already" ) 79self .assertEqual (self .strata_before ,tree (self .comp / "engine" )) 80 81def test_delete_the_older_one_and_never_the_one_in_use_or_one_a_program_uses (self ): 82self .install ("0.1.59" ,"0.1.60" ) 83rows = self .rows () 84with self .assertRaises (updater .UpdateError )as e : 85lib .delete_engine (self .ctx (),FAMILY ,rows ["0.1.60" ]["folder" ]) 86self .assertEqual (str (e .exception ),"This is the engine in use. Switch to another one first." ) 87with mock .patch .object (lib ,"_process_uses" ,side_effect = lambda folder :folder .name == rows ["0.1.59" ]["folder" ]): 88with self .assertRaises (updater .UpdateError )as e : 89lib .delete_engine (self .ctx (),FAMILY ,rows ["0.1.59" ]["folder" ]) 90self .assertEqual (str (e .exception ),"A running program is using it. Stop the model first." ) 91out = lib .delete_engine (self .ctx (),FAMILY ,rows ["0.1.59" ]["folder" ]) 92self .assertEqual ((out ["status" ],out ["version" ],out ["component" ]), ("deleted" ,"0.1.59" ,shape_engine .component_id (SHAPE ))) 93self .assertEqual (sorted (self .rows ()), ["0.1.60" ]) 94self .assertEqual (self .picked (),"0.1.60" ) 95self .assertEqual (self .strata_before ,tree (self .comp / "engine" )) 96 97def test_a_deleted_pinned_engine_leaves_no_pin (self ): 98self .install ("0.1.59" ,"0.1.60" ) 99old = self .rows ()["0.1.59" ]["folder" ] 100lib .use_engine (self .ctx (),FAMILY ,old ) 101lib .use_engine (self .ctx (),FAMILY ,self .rows ()["0.1.60" ]["folder" ]) 102state = updater .State (shape_engine .root (SHAPE ,self .comp )) 103state .section ("engine" )["pinned" ]= old # as if a pin was left behind 104state .save () 105self .assertEqual (self .picked (),"0.1.59" ) 106lib .use_engine (self .ctx (),FAMILY ,self .rows ()["0.1.60" ]["folder" ]) 107lib .delete_engine (self .ctx (),FAMILY ,old ) 108self .assertIsNone (ec .pinned_folder (shape_engine .root (SHAPE ,self .comp ))) 109 110def test_a_family_that_is_not_a_shape_is_refused (self ): 111for bad in ("shape:" ,"shape:X" ,"shape:engine" ,"shapes:q4_lite" ,"strata-q4-lite" ): 112with self .assertRaises (updater .UpdateError ): 113lib .use_engine (self .ctx (),bad ,"0.1.59-x" ) 114self .assertEqual (lib .shape_of_family (FAMILY ),SHAPE ) 115self .assertIsNone (lib .shape_of_family ("qwen" )) 116 117def test_the_terminal_lists_the_shape_by_name_and_takes_the_family_the_app_passes (self ): 118from strata_cli import update_cmd 119self .assertIn (FAMILY ,update_cmd ._ENGINE_FAMILIES ) 120self .assertIn ("qwen" ,update_cmd ._ENGINE_FAMILIES ) 121self .assertNotIn ("shape:Nope" ,update_cmd ._ENGINE_FAMILIES ) 122self .assertEqual (list (update_cmd ._ENGINE_FAMILIES ), ["qwen" ],"help and errors list the Qwen family" ) 123self .assertIn ("deepseek" ,update_cmd ._ENGINE_FAMILIES )# accepted, so engine_library answers it in plain words (strata_control/retired.py) 124self .install ("0.1.59" ) 125a = mock .Mock (components_command = "engines" ) 126out = io .StringIO () 127with contextlib .redirect_stdout (out ): 128update_cmd ._engines_command (a ,self .ctx (),False ,None ) 129text = out .getvalue () 130self .assertIn ("\n" + SHAPE + "\n" ,text ) 131self .assertNotIn (FAMILY ,text ) 132self .assertNotIn ("None" ,text )