okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fefaa985c
main
1"""Lane NET item 7: setup may use an Unsloth mmproj-F16.gguf that the person already has as the Flash-Next picture reader, and the plan says the same. 2 3The rule (setup.ALT_READERS, setup.accepted_alt_reader, setup.resolve_reader; the plan asks the same function through modelstate.reader_present): 4* only a file named mmproj-F16.gguf with the pinned size AND SHA-256, in a place where setup already looks for the pinned reader; 5* only when no pinned BF16 file is found anywhere (the pinned file always wins); anything else with that name is ignored, as before; 6* never downloaded, never deleted, never given a mark beside it: the SHA-256 is remembered in Strata's marks/ (keyed by real path, size, 7modification time and change time, so a replaced file is hashed again, also one rewritten in place with size and time kept); 8* the plan hashes without writing a mark and says nothing. 9The tests use a small stand-in file and patch the pin; one test holds the real pin.""" 10 11from __future__import annotations 12 13import contextlib 14import hashlib 15import io 16import os 17import tempfile 18import time 19import unittest 20from pathlib import Path 21from unittest import mock 22 23from strata_cli .launch import modelstate 24from tests .terminal .launch_support import HW_12 ,Home ,catalog 25 26ALT = "mmproj-F16.gguf" 27GOOD = b"unsloth-reader " * 100 28BAD = b"another-reader " * 100 # the same size, another content 29assert len (GOOD )== len (BAD ) 30NOTE = (" note: {path} has the name of Unsloth's picture reader but is a different file, so it is not used. " 31"The picture reader is downloaded (0.9 GB)." ) 32 33 34class Base (unittest .TestCase ): 35def setUp (self ): 36home = Home ().__enter__ () 37self .addCleanup (home .__exit__ ,None ,None ,None ) 38t = tempfile .TemporaryDirectory (prefix = "strata-altreader-" ) 39self .addCleanup (t .cleanup ) 40self .base = Path (t .name ) 41self .cat = catalog (HW_12 ) 42self .mod = self .cat .strata .setup_module ()# the module the plan asks too 43self .pinned = self .mod .MMPROJ 44for p in (mock .patch .dict (self .mod .ALT_READERS , {self .pinned : (ALT ,len (GOOD ),hashlib .sha256 (GOOD ).hexdigest ())}), 45mock .patch .object (self .mod ,"ROOT" ,self .base / "strata-root" )): 46p .start () 47self .addCleanup (p .stop ) 48self .own = self .base / "mine" / "IQ3_XXS" # a person's own folder of shards 49self .own .mkdir (parents = True ) 50self .models = self .base / "data" / "models" # Strata's own models folder (the pinned file would be here) 51self .models .mkdir (parents = True ) 52 53def start (self ,gguf_dir = "own" ): 54"""What setup does for the reader: (kind, path, printed text).""" 55out = io .StringIO () 56gd = self .own if gguf_dir == "own" else gguf_dir 57with contextlib .redirect_stdout (out ): 58kind ,path = self .mod .resolve_reader (self .pinned ,self .models / self .pinned ,self .models , [],gd ,907543008 ) 59return kind ,path ,out .getvalue () 60 61def plan (self ): 62"""What the plan believes: does setup find a reader without a download (and what it printed).""" 63out = io .StringIO () 64with contextlib .redirect_stdout (out ): 65present = modelstate .reader_present (self .cat ,"qwen" , [self .base / "data" ],own = self .own ) 66return present ,out .getvalue () 67 68def marks (self ): 69d = self .mod .ROOT / "marks" 70return sorted (d .iterdir ())if d .is_dir ()else [] 71 72def hashes (self ): 73"""Count the files setup hashes from here on.""" 74real = self .mod ._hash_file 75calls = [] 76 77def counting (path ,size ): 78calls .append (Path (path ).name ) 79return real (path ,size ) 80p = mock .patch .object (self .mod ,"_hash_file" ,counting ) 81p .start () 82self .addCleanup (p .stop ) 83return calls 84 85 86class TheRealPin (unittest .TestCase ): 87def test_the_pinned_values_are_the_owners (self ): 88import setup 89self .assertEqual (setup .ALT_READERS [setup .MMPROJ ], ("mmproj-F16.gguf" ,904004000 , 90"1f7b7f0b984cf065c604360c29c8098362ed61b290db0ff12c6f360bb1a8a980" )) 91self .assertEqual (setup .MMPROJ ,"mmproj-Qwen3.8-Flash-Next-BF16.gguf" ) 92self .assertEqual (list (setup .ALT_READERS ), [setup .MMPROJ ],"Flash-Next's pinned file only, no other family" ) 93 94 95class Accepted (Base ): 96def test_a_file_with_the_pinned_size_and_hash_is_used_and_checked_once (self ): 97 (self .own / ALT ).write_bytes (GOOD ) 98hashed = self .hashes () 99present ,said = self .plan ()# the plan first: hashes, says nothing, writes nothing 100self .assertTrue (present ) 101self .assertEqual (said ,"" ) 102self .assertEqual (self .marks (), [],"the plan writes no mark" ) 103kind ,path ,said = self .start () 104self .assertEqual ((kind ,path ), ("alt" ,self .own / ALT )) 105self .assertEqual (said ,f" checking { ALT } (SHA-256, 0.0 GB) ...\n" ) 106self .assertEqual (len (self .marks ()),1 ,"one mark in Strata's own folder" ) 107self .assertEqual (sorted (p .name for p in self .own .iterdir ()), [ALT ],"nothing was written beside the person's file" ) 108before = len (hashed ) 109kind ,_path ,said = self .start ()# the next run: the mark answers 110self .assertEqual ((kind ,said ), ("alt" ,"" )) 111self .assertTrue (self .plan ()[0 ]) 112self .assertEqual (len (hashed ),before ,"no second hash" ) 113 114def test_the_folder_above_and_its_models_folder_and_the_data_models_folder_count_too (self ): 115for where in (self .own .parent ,self .own .parent / "models" ,self .models ): 116where .mkdir (parents = True ,exist_ok = True ) 117f = where / ALT 118f .write_bytes (GOOD ) 119self .assertEqual (self .start ()[:2 ], ("alt" ,f ),str (where )) 120self .assertTrue (self .plan ()[0 ],str (where )) 121f .unlink () 122self .assertEqual (self .start ()[0 ],"fetch" ) 123self .assertFalse (self .plan ()[0 ]) 124 125 126class NotAccepted (Base ): 127def test_a_wrong_size_is_ignored_exactly_as_before (self ): 128 (self .own / ALT ).write_bytes (GOOD [:- 1 ]) 129hashed = self .hashes () 130kind ,path ,said = self .start () 131self .assertEqual ((kind ,path ,said ), ("fetch" ,self .models / self .pinned ,"" )) 132self .assertEqual (hashed , [],"not even hashed" ) 133self .assertEqual (self .plan (), (False ,"" )) 134self .assertEqual ((self .own / ALT ).read_bytes (),GOOD [:- 1 ]) 135 136def test_a_wrong_hash_is_ignored_with_the_note_and_the_pinned_file_is_downloaded (self ): 137 (self .own / ALT ).write_bytes (BAD ) 138hashed = self .hashes () 139kind ,path ,said = self .start () 140self .assertEqual ((kind ,path ), ("fetch" ,self .models / self .pinned )) 141self .assertEqual (said ,f" checking { ALT } (SHA-256, 0.0 GB) ...\n" + NOTE .format (path = self .own / ALT )+ "\n" ) 142self .assertEqual (self .plan (), (False ,"" ),"the plan agrees with the start: the reader is downloaded" ) 143self .assertEqual ((self .own / ALT ).read_bytes (),BAD ,"the person's file is never touched" ) 144self .assertEqual (sorted (p .name for p in self .own .iterdir ()), [ALT ]) 145before = len (hashed ) 146kind ,_path ,said = self .start ()# remembered: not hashed again, the note says it again 147self .assertEqual (kind ,"fetch" ) 148self .assertEqual (said ,NOTE .format (path = self .own / ALT )+ "\n" ) 149self .assertEqual (len (hashed ),before ) 150 151def test_another_name_is_never_looked_at (self ): 152 (self .own / "mmproj-BF16.gguf" ).write_bytes (GOOD ) 153 (self .own / "mmproj-Q8_0.gguf" ).write_bytes (GOOD ) 154self .assertEqual (self .start ()[0 ],"fetch" ) 155self .assertFalse (self .plan ()[0 ]) 156 157def test_the_pinned_file_always_wins (self ): 158 (self .own / ALT ).write_bytes (GOOD ) 159pinned = self .own / self .pinned 160pinned .write_bytes (b"x" * 10 )# beside the files: by name, any size (setup's mmproj_beside) 161hashed = self .hashes () 162kind ,path ,said = self .start () 163self .assertEqual ((kind ,path ,said ), ("near" ,pinned ,"" )) 164self .assertEqual (hashed , [],"the alternative is not even hashed" ) 165self .assertTrue (self .plan ()[0 ]) 166pinned .unlink () 167 (self .models / self .pinned ).write_bytes (b"y" )# Strata's own models folder 168kind ,path ,said = self .start () 169self .assertEqual ((kind ,path ,said ), ("fetch" ,self .models / self .pinned ,"" ),"the pinned file is used (and verified later by setup)" ) 170self .assertTrue (self .plan ()[0 ]) 171self .assertEqual (hashed , []) 172 173 174class Replaced (Base ): 175def test_a_file_accepted_once_and_then_replaced_by_another_of_the_same_size_is_hashed_again_and_refused (self ): 176f = self .own / ALT 177f .write_bytes (GOOD ) 178self .assertEqual (self .start ()[0 ],"alt" ) 179self .assertEqual (len (self .marks ()),1 ) 180hashed = self .hashes () 181stat = f .stat () 182f .write_bytes (BAD )# the same size and place, other bytes 183os .utime (f ,ns = (stat .st_atime_ns ,stat .st_mtime_ns + 5_000_000_000 ))# (a new modification time, as any real replacement has) 184kind ,_path ,said = self .start () 185self .assertEqual (kind ,"fetch" ) 186self .assertIn ("is a different file" ,said ) 187self .assertEqual (hashed , [ALT ],"hashed again" ) 188self .assertEqual (self .plan (), (False ,"" )) 189 190def test_a_rewrite_in_place_that_keeps_size_and_modification_time_is_hashed_again_and_refused (self ): 191f = self .own / ALT 192f .write_bytes (GOOD ) 193self .assertEqual (self .start ()[0 ],"alt" ) 194self .assertEqual (len (self .marks ()),1 ) 195hashed = self .hashes () 196stat = f .stat () 197time .sleep (0.05 )# past the file system clock step (a kernel tick on Linux) 198with open (f ,"r+b" )as fh :# the same file, rewritten in place (no new file, no rename) 199fh .write (BAD ) 200os .utime (f ,ns = (stat .st_atime_ns ,stat .st_mtime_ns ))# the old modification time put back 201self .assertEqual ((f .stat ().st_size ,f .stat ().st_mtime_ns ), (stat .st_size ,stat .st_mtime_ns ),"size and modification time as before" ) 202kind ,_path ,said = self .start () 203self .assertEqual (kind ,"fetch" ) 204self .assertIn ("is a different file" ,said ) 205self .assertEqual (hashed , [ALT ],"hashed again" ) 206self .assertEqual (self .plan (), (False ,"" )) 207 208def test_the_change_time_moves_on_such_a_rewrite_on_this_system (self ): 209f = self .own / ALT 210f .write_bytes (GOOD ) 211st = f .stat () 212before = self .mod ._change_time_ns (f ,st ) 213time .sleep (0.05 )# past the file system clock step (a kernel tick on Linux) 214with open (f ,"r+b" )as fh : 215fh .write (BAD ) 216os .utime (f ,ns = (st .st_atime_ns ,st .st_mtime_ns )) 217self .assertNotEqual (self .mod ._change_time_ns (f ,f .stat ()),before ) 218if os .name == "nt" :# Windows: the file system's ChangeTime, not os.stat's ctime (the creation time) 219self .assertNotEqual (self .mod ._change_time_ns (f ,f .stat ()),f .stat ().st_ctime_ns ) 220 221def test_other_kept_marks_keep_their_key (self ): 222f = self .own / "model-00001-of-00002.gguf" 223f .write_bytes (b"shard" ) 224st = f .stat () 225key = f" { f . resolve () } | { st . st_size } | { st . st_mtime_ns } " 226self .assertEqual (self .mod ._kept_mark (f ).name ,hashlib .sha1 (key .encode ()).hexdigest ()[:24 ]+ ".done" , 227"the key of every other kept mark (an Engram's too) is unchanged, so no mark made before is lost" ) 228self .assertNotEqual (self .mod ._alt_reader_mark (f ),self .mod ._kept_mark (f )) 229 230def test_a_changed_size_has_no_mark_either (self ): 231f = self .own / ALT 232f .write_bytes (GOOD ) 233self .assertEqual (self .start ()[0 ],"alt" ) 234f .write_bytes (GOOD + b"!" ) 235self .assertEqual (self .start (), ("fetch" ,self .models / self .pinned ,"" )) 236 237def test_the_mark_is_keyed_by_the_real_path_so_a_linked_folder_shares_it_and_cannot_hide_a_replacement (self ): 238real = self .base / "real" 239real .mkdir () 240 (real / ALT ).write_bytes (GOOD ) 241link = self .base / "link" 242try : 243os .symlink (real ,link ,target_is_directory = True ) 244except (OSError ,NotImplementedError ,AttributeError ): 245self .skipTest ("this account cannot make symbolic links" ) 246self .assertEqual (self .start (gguf_dir = real )[0 ],"alt" ) 247hashed = self .hashes () 248self .assertEqual (self .start (gguf_dir = link )[0 ],"alt" ) 249self .assertEqual (hashed , [],"the same file through the link: the same mark" ) 250stat = (real / ALT ).stat () 251 (real / ALT ).write_bytes (BAD ) 252os .utime (real / ALT ,ns = (stat .st_atime_ns ,stat .st_mtime_ns + 5_000_000_000 )) 253self .assertEqual (self .start (gguf_dir = link )[0 ],"fetch" ,"replaced behind the link: hashed again, refused" ) 254self .assertEqual (hashed , [ALT ]) 255 256 257if __name__ == "__main__" : 258unittest .main ()