okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""#4891 ("Qwen breaks each time Strata is updated"): a saved run config names folders of one version of Strata's own components (the Control bundle that 2ran setup: cwd, --expert-profile; an engine package: the picture reader, library folders). The update keeps the new Control and 3the one before it, so the SECOND update after setup removes the folder the config names; the model then showed as "not installed" and a start failed. 4 5The updates here are real (`updater.run_update(control=True)` unpacks and prunes Control folders; `prune_engines` removes the 6older engine packages); the starts are real `runtime.start` calls with a fake engine, the way the app (with --die-with-pid) and the Terminal start. 7""" 8from __future__import annotations 9 10import json 11import os 12import shutil 13import sys 14import unittest 15from pathlib import Path 16 17sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [2 ])) 18from strata_control import cfg_paths ,doctor ,engine_component as ec ,profiles ,runtime ,updater # noqa: E402 19from tests .terminal .test_updater import PLATFORM ,UpdaterCase # noqa: E402 20 21WINDOWS = os .name == "nt" 22FAKE_ENGINE = ("#!" + sys .executable + "\nimport sys\n" 23"print('READY 4096 stop', flush=True)\n" 24"for line in sys.stdin:\n" 25" if line.strip() == 'QUIT': break\n" 26" if line.startswith(('GEN ', 'BGEN ')):\n" 27" print('T 97', flush=True)\n" 28" print('DONE 1 1 1.0 1.0 length 0 0 0', flush=True)\n" ) 29 30 31class StaleConfigPaths (UpdaterCase ): 32# (UpdaterCase's own tests are not repeated) 33def setUp (self ): 34super ().setUp () 35self .control = self .components / "control" 36self .engine_root = self .components / "engine" 37self .sequence = 0 38# Control 0.1.44 is what ran setup: its data/ holds the shipped expert profile 39self .install_control ("0.1.44" ) 40 (self .control / "active.json" ).write_text (json .dumps ({"version" :"0.1.44" ,"folder" :"0.1.44" ,"sha256" :"0" * 64 })) 41# the engine package that setup (or an earlier update) wrote into the config 42self .engine_kind = f" { PLATFORM } -cuda-cuda13" 43 (self .root / "packs" / "iq3_s" ).mkdir (parents = True ) 44self .old_engine = self .make_engine_folder (self .engine_root ,"0.1.44" ) 45self .tokenizer = str (self .root / "tokenizer" ) 46self .mine = self .root / "mine" / "my-profile.bin" # a path the person typed themselves 47self .configs = {} 48 49# ------------------------------------------------------------------ the world 50 51def install_control (self ,version ): 52folder = self .control / version 53 (folder / "data" ).mkdir (parents = True ) 54 (folder / "strata.py" ).write_text (f"print('strata { version } (python stub)')\n" ) 55 (folder / "data" / "expert-profile.bin" ).write_bytes (b"profile-" + version .encode ()) 56return folder 57 58def make_engine_folder (self ,root ,version ,script_name = None ): 59folder = root / f" { version } - { self . engine_kind } " 60folder .mkdir (parents = True ) 61name = script_name or ("engine.cmd" if WINDOWS else "engine.py" ) 62script = folder / "engine.py" 63script .write_text (FAKE_ENGINE ,encoding = "utf-8" ) 64script .chmod (0o700 ) 65if WINDOWS : 66 (folder / "engine.cmd" ).write_bytes (f'@echo off\r\n" { sys . executable } " "%~dp0engine.py" %*\r\n' .encode ()) 67 (folder / "strata-vision" ).write_bytes (b"reader " + version .encode ()) 68return folder 69 70def engine_exe (self ,folder ): 71return folder / ("engine.cmd" if WINDOWS else "engine.py" ) 72 73def publish_control (self ,version ): 74bundle = self .root / f"bundle- { version } " 75 (bundle / "strata_control" ).mkdir (parents = True ) 76 (bundle / "data" ).mkdir () 77 (bundle / "data" / "expert-profile.bin" ).write_bytes (b"profile-" + version .encode ()) 78 (bundle / "strata.py" ).write_text (f"print('strata { version } (python stub)')\n" ) 79 (bundle / "strata_control" / "__init__.py" ).write_text ("" ) 80from feed_tool import build_feed ,pack_control 81shutil .rmtree (self .release ,ignore_errors = True ) 82 (self .release / "control" ).mkdir (parents = True ) 83pack_control (bundle ,self .release / "control" ,version = version ,platform = PLATFORM ) 84self .sequence += 1 85build_feed (self .release ,key = self .key ,sequence = self .sequence ,control_dir = self .release / "control" ) 86 87def update_to (self ,version ): 88self .publish_control (version ) 89out = updater .run_update (self .ctx (),engine = False ,control = True ) 90self .assertEqual (out ["results" ][0 ]["status" ],"updated" ,out ) 91 92def two_updates (self ): 93"""Setup ran on Control 0.1.44. Two updates later (0.1.45, then 0.1.46) Control keeps 0.1.46 and 0.1.45, and the engine package 94is replaced the way its update does it.""" 95self .update_to ("0.1.45" ) 96self .assertTrue ((self .control / "0.1.44" ).is_dir (),"one update keeps the folder before it" ) 97self .update_to ("0.1.46" ) 98self .assertFalse ((self .control / "0.1.44" ).exists (),"the second update removes the folder setup wrote into the config" ) 99new_engine = self .make_engine_folder (self .engine_root ,"0.1.46" ) 100updater .prune_engines (self .components , {new_engine .name }) 101self .assertFalse (self .old_engine .exists ()) 102return new_engine ,None 103 104def qwen_config (self ,name ,vision = False ,** extra ): 105cfg = {"exe" :str (self .engine_exe (self .old_engine )),"backend" :"cuda" ,"tokenizer" :self .tokenizer , 106"cwd" :str (self .control / "0.1.44" ),"model_name" :"fake" ,"host" :"127.0.0.1" ,"port" :0 , 107"lib_dirs" : [str (self .old_engine )], 108"args" : ["--pack" ,str (self .root / "packs" / "iq3_s" ), 109"--expert-profile" ,str (self .control / "0.1.44" / "data" / "expert-profile.bin" ), 110"--expert-profile-save" ,str (self .control / "0.1.44" / "data" / "learned.bin" ), 111"--mtp" ,"2" ,"--lookup-file" ,str (self .mine )]} 112if vision :# (a real start would run the picture reader: only the read-side tests ask for it) 113cfg ["vision" ]= {"exe" :str (self .old_engine / "strata-vision" )} 114cfg .update (extra ) 115path = self .root / "run" / f" { name } .json" 116path .parent .mkdir (exist_ok = True ) 117path .write_text (json .dumps (cfg ,indent = 2 ),encoding = "utf-8" ) 118profiles .create (name ,str (path )) 119self .configs [name ]= path 120return path 121 122def deepseek_config (self ,name ): 123"""A DeepSeek config of Strata 0.2.13: its engine in engine-dsv41/, its cwd in the Control that ran setup.""" 124old_ds = self .root / "data" / "engine-dsv41" / f"0.1.49- { self . engine_kind } " 125exe = old_ds / "strata-dsv41" 126cfg = {"exe" :str (exe ),"arch" :"deepseek41" ,"backend" :"cuda" ,"tokenizer" :self .tokenizer ,"cwd" :str (self .control / "0.1.44" ), 127"model_name" :"deepseek" ,"host" :"127.0.0.1" ,"port" :0 ,"lib_dirs" : [str (old_ds )], 128"args" : ["--pack" ,str (self .root / "packs" / "ds" ),"--max-context" ,"8192" ]} 129path = self .root / "run" / f" { name } .json" 130path .parent .mkdir (exist_ok = True ) 131path .write_text (json .dumps (cfg ,indent = 2 ),encoding = "utf-8" ) 132profiles .create (name ,str (path )) 133self .configs [name ]= path 134return path 135 136def cfg_of (self ,path ): 137return json .loads (Path (path ).read_text (encoding = "utf-8" )) 138 139def without_output_path (self ,path ): 140"""The config's text without the one path the engine WRITES (--expert-profile-save), which is deliberately not looked for.""" 141return Path (path ).read_text (encoding = "utf-8" ).replace (json .dumps (str (self .control / "0.1.44" / "data" / "learned.bin" ))[1 :- 1 ],"" ) 142 143def host_log_lines (self ,name ,word = "config:" ): 144_ ,_ ,log_path = runtime ._paths (name ) 145return [l for l in log_path .read_text (encoding = "utf-8" ).splitlines ()if word in l ]if log_path .exists ()else [] 146 147def stop (self ,name ): 148try : 149if runtime ._read_state (name )is not None : 150runtime .stop (name ,timeout = 12 ) 151except Exception :# noqa: BLE001 152pass 153 154# ------------------------------------------------------------------ what an update does 155 156def test_the_folders_the_config_names_are_gone_after_two_updates_and_the_model_looks_missing (self ): 157path = self .qwen_config ("qwen" ,vision = True ) 158self .two_updates () 159cfg = self .cfg_of (path ) 160self .assertFalse (Path (cfg ["cwd" ]).exists ()) 161self .assertFalse (Path (cfg ["args" ][cfg ["args" ].index ("--expert-profile" )+ 1 ]).exists ()) 162report = doctor .inspect (path ) 163stale = [c for c in report ["components" ]if c ["status" ]!= "ok" and c ["name" ]in ("expert-profile" ,"vision/exe" ,"engine" )] 164self .assertEqual (stale , [],"the doctor (and so the model list) must follow the current folders: " + json .dumps (stale )) 165 166def test_reading_the_config_follows_the_current_version_in_memory_and_writes_nothing (self ): 167path = self .qwen_config ("qwen" ,vision = True ) 168new_engine ,_ = self .two_updates () 169before = path .read_bytes () 170cfg = profiles .read_run_config (path ) 171active = self .control / "0.1.46" 172self .assertEqual (Path (cfg ["cwd" ]),active ) 173self .assertEqual (Path (cfg ["args" ][cfg ["args" ].index ("--expert-profile" )+ 1 ]),active / "data" / "expert-profile.bin" ) 174self .assertEqual (Path (cfg ["vision" ]["exe" ]),new_engine / "strata-vision" ) 175self .assertEqual ([Path (d )for d in cfg ["lib_dirs" ]], [new_engine ]) 176self .assertEqual (Path (cfg ["exe" ]),self .engine_exe (new_engine )) 177self .assertEqual (path .read_bytes (),before ) 178 179def test_the_paths_the_person_typed_and_the_data_are_left_alone (self ): 180path = self .qwen_config ("qwen" ) 181self .two_updates () 182cfg = profiles .read_run_config (path ) 183args = cfg ["args" ] 184self .assertEqual (args [args .index ("--pack" )+ 1 ],str (self .root / "packs" / "iq3_s" )) 185self .assertEqual (args [args .index ("--lookup-file" )+ 1 ],str (self .mine ))# (typed, outside Strata's folders, does not exist: not touched) 186self .assertEqual (args [args .index ("--mtp" )+ 1 ],"2" ) 187self .assertEqual (args [args .index ("--expert-profile-save" )+ 1 ],str (self .control / "0.1.44" / "data" / "learned.bin" ), 188"a path the engine writes is not a file to look for" ) 189self .assertEqual (cfg ["tokenizer" ],self .tokenizer ) 190self .assertEqual (cfg ["model_name" ],"fake" ) 191 192def test_a_folder_that_still_exists_is_not_replaced_by_a_newer_one (self ): 193path = self .qwen_config ("qwen" ) 194self .update_to ("0.1.45" )# 0.1.44 is still there (the version before the newest) 195before = path .read_bytes () 196cfg = profiles .read_run_config (path ) 197self .assertEqual (cfg ["cwd" ],str (self .control / "0.1.44" )) 198self .assertEqual (cfg_paths .repair ([path ]), ([], [])) 199self .assertEqual (path .read_bytes (),before ) 200 201# ------------------------------------------------------------------ starts 202 203def test_a_terminal_start_rewrites_the_config_once_and_says_so_in_one_line (self ): 204path = self .qwen_config ("term" ) 205new_engine ,_ = self .two_updates () 206self .addCleanup (self .stop ,"term" ) 207started = runtime .start ("term" ,wait = 60 ) 208self .assertTrue (started ["ok" ],started ) 209cfg = self .cfg_of (path ) 210self .assertEqual (cfg ["cwd" ],str (self .control / "0.1.46" )) 211self .assertEqual (cfg ["exe" ],str (self .engine_exe (new_engine ))) 212self .assertNotIn ("0.1.44" ,self .without_output_path (path )) 213self .assertEqual (len (self .host_log_lines ("term" )),1 ,self .host_log_lines ("term" )) 214self .assertIn ("earlier Strata version" ,self .host_log_lines ("term" )[0 ]) 215runtime .stop ("term" ,timeout = 12 ) 216again = runtime .start ("term" ,wait = 60 ) 217self .assertTrue (again ["ok" ],again ) 218self .assertEqual (len (self .host_log_lines ("term" )),1 ,"nothing is rewritten the second time" ) 219 220def test_an_app_start_with_die_with_pid_does_the_same (self ): 221path = self .qwen_config ("app-1" ) 222self .two_updates () 223self .addCleanup (self .stop ,"app-1" ) 224started = runtime .start ("app-1" ,wait = 60 ,die_with_pid = os .getpid ()) 225self .assertTrue (started ["ok" ],started ) 226self .assertEqual (self .cfg_of (path )["cwd" ],str (self .control / "0.1.46" )) 227 228def test_a_start_planned_on_the_old_file_is_not_refused_because_strata_rewrote_it (self ): 229import hashlib 230path = self .qwen_config ("app-2" ) 231self .two_updates () 232self .addCleanup (self .stop ,"app-2" ) 233planned = hashlib .sha256 (path .read_bytes ()).hexdigest () 234started = runtime .start ("app-2" ,wait = 60 ,expected_source_sha256 = planned ) 235self .assertTrue (started ["ok" ],started ) 236 237def test_every_saved_profile_is_repaired_by_one_start (self ): 238a ,b = self .qwen_config ("one" ),self .qwen_config ("two" ) 239self .two_updates () 240self .addCleanup (self .stop ,"one" ) 241self .assertTrue (runtime .start ("one" ,wait = 60 )["ok" ]) 242self .assertNotIn ("0.1.44" ,self .without_output_path (b ),"the Models page reads the other profiles' configs too" ) 243 244def test_a_deepseek_config_is_left_exactly_as_it_was (self ): 245"""This Strata runs no DeepSeek model (strata_control/retired.py): neither the repair nor a start writes its config, which may still 246serve the Strata it came from; the start says why it stops.""" 247path = self .deepseek_config ("ds" ) 248self .two_updates () 249before = Path (path ).read_bytes () 250self .assertEqual (cfg_paths .repair ([path ]), ([], [])) 251with self .assertRaises (profiles .ProfileError )as caught : 252runtime .start ("ds" ,wait = 1 ,die_with_pid = os .getpid ()) 253self .assertIn ("is a DeepSeek model. This version of Strata no longer runs DeepSeek models, so it was not started." ,str (caught .exception )) 254self .assertEqual (Path (path ).read_bytes (),before ) 255 256# ------------------------------------------------------------------ a file that is nowhere 257 258def test_a_file_the_new_version_no_longer_has_stops_the_start_with_its_name (self ): 259path = self .qwen_config ("lost" ) 260self .two_updates () 261 (self .control / "0.1.46" / "data" / "expert-profile.bin" ).unlink () 262 (self .control / "0.1.45" / "data" / "expert-profile.bin" ).unlink () 263with self .assertRaises (profiles .ProfileError )as caught : 264runtime .start ("lost" ,wait = 5 ) 265text = str (caught .exception ) 266self .assertIn ("expert-profile.bin" ,text ) 267self .assertIn ("removed by an update" ,text ) 268self .assertIn ("run setup again" ,text ) 269self .assertIn ("0.1.44" ,path .read_text (encoding = "utf-8" ),"nothing is guessed: the file is left as it was" ) 270 271 272if __name__ == "__main__" : 273unittest .main ()