okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
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), 5and 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 ): 31def ensure (self ,statuses ,* ,serving = (),confirm = None ,assume_yes = False ): 32spawns ,seen = [], [] 33r = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ),catalog = qwen_catalog (),emit = seen .append , 34confirm = confirm ,assume_yes = assume_yes ) 35r .check_cancel = lambda :None 36with mock .patch .object (ec ,"status" ,side_effect = list (statuses )), \ 37mock .patch .object (Runner ,"needed_engine" ,return_value = (0 ,1 ,50 )), \ 38mock .patch .object (updater ,"serving_now" ,return_value = list (serving )), \ 39mock .patch .object (Runner ,"_spawn" ,side_effect = lambda argv ,** kw :spawns .append (argv )or FakeProc (update_events ("updated" ),0 )): 40r .ensure_engine () 41return spawns ,r ,seen 42 43def test_a_loaded_model_blocks_the_update_even_with_yes (self ): 44for kwargs in ({"assume_yes" :True }, {"confirm" :lambda * a :True }, {}): 45spawns ,r ,seen = self .ensure ([OLD ],serving = RUNNING ,** kwargs ) 46self .assertEqual (spawns , [],kwargs ) 47self .assertTrue (any ("running, so Strata did not update the engine" in n for n in r .notices ),r .notices ) 48self .assertTrue (any ("strata launch --stop" in n for n in r .notices )) 49 50def test_no_yes_no_update (self ): 51spawns ,r ,_ = self .ensure ([OLD ]) 52self .assertEqual (spawns , []) 53self .assertIn ("Strata did not update it. Run `strata update` (or start with --yes)" ,r .notices [0 ]) 54 55def test_no_to_the_question_no_update (self ): 56asked = [] 57spawns ,r ,_ = self .ensure ([OLD ],confirm = lambda t ,b :asked .append ((t ,b ))or False ) 58self .assertEqual (spawns , []) 59self .assertEqual (len (asked ),1 ) 60self .assertIn ("older than this model needs" ,asked [0 ][1 ]) 61 62def test_yes_to_the_question_updates (self ): 63spawns ,r ,_ = self .ensure ([OLD ,NEW ],confirm = lambda t ,b :True ) 64self .assertEqual (len (spawns ),1 ) 65self .assertEqual (r .notices , []) 66 67def test_yes_flag_updates_when_nothing_is_loaded (self ): 68spawns ,r ,_ = self .ensure ([OLD ,NEW ],assume_yes = True ) 69self .assertEqual (len (spawns ),1 ) 70 71def test_no_engine_at_all_is_still_fetched_because_nothing_runs_on_it (self ): 72spawns ,r ,_ = self .ensure ([NONE ,NEW ]) 73self .assertEqual (len (spawns ),1 ) 74 75def test_an_engine_that_is_new_enough_asks_nothing (self ): 76asked = [] 77spawns ,r ,_ = self .ensure ([NEW ],confirm = lambda t ,b :asked .append (t )) 78self .assertEqual ((spawns ,asked ), ([], [])) 79 80def test_plan_says_it_will_ask (self ): 81r = Runner (S .LaunchSettings (family = "qwen" ,model = "Q2_0" ,context = 32768 ),catalog = qwen_catalog (),emit = lambda e :None ) 82with mock .patch .object (ec ,"status" ,return_value = OLD ),mock .patch .object (Runner ,"needed_engine" ,return_value = (0 ,1 ,50 )), \ 83mock .patch ("strata_cli.launch.runner.install_isolation_words" ,return_value = []): 84words = r .preflight_words () 85self .assertTrue (any ("will ask before updating it" in w for w in words ),words ) 86 87 88class SourceRules (unittest .TestCase ): 89def calls (self ,path ,function ): 90tree = ast .parse ((ROOT / path ).read_text (encoding = "utf-8" )) 91names = set () 92for node in ast .walk (tree ): 93if isinstance (node ,ast .FunctionDef )and node .name == function : 94for inner in ast .walk (node ): 95if isinstance (inner ,ast .Call ): 96f = inner .func 97names .add (f .attr if isinstance (f ,ast .Attribute )else f .id if isinstance (f ,ast .Name )else "" ) 98return names 99 100def test_the_apps_qwen_model_download_never_fetches_an_engine (self ): 101names = self .calls ("setup.py" ,"control_engine_step" ) 102for forbidden in ("engine_from_feed" ,"get_prebuilt" ,"get_prebuilt_hip" ,"update_installed_engine" ,"run" ,"urlopen" ,"check_call" ,"Popen" ): 103self .assertNotIn (forbidden ,names ) 104 105 106if __name__ == "__main__" : 107unittest .main ()