okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
1"""Offline checks for image/steering isolation evidence and generated fixtures.""" 2import copy 3from pathlib import Path 4import struct 5import subprocess 6import sys 7import tempfile 8import unittest 9 10sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ]))# runnable as tools/test_x.py from the repo root too 11 12from tools .conversation_cache_isolation import STATE_KEYS ,fixtures ,verify 13from tools .gguf_reader import GGUFFile 14 15 16def evidence (scenario ): 17image = scenario == 'image' 18names = (['image-A' ,'text-B' ,'image-A-return' ,'image-B' ,'image-A-again' ,'grid-changed' ,'image-A-final' ] 19if image else ['off' ,'on' ,'text-B' ,'off-return' ,'on-return' ,'off-again' ]) 20hits = [2 ,4 ,6 ]if image else [3 ,4 ,5 ] 21records = [{'name' :name ,'ids' : [123 ],'finish' :'length' ,'reused' :0 , 22'state' : {k :str (i )for k in STATE_KEYS }}for i ,name in enumerate (names )] 23candidate = copy .deepcopy (records ) 24for i in hits : 25candidate [i ]['reused' ]= 100 26info = dict (expert_slots = 8000 ,kv = 'int8' ,kv_resident = 32768 ,context = 262144 , 27spec = 2 ,mtp_max = 1 ,lookup = 0 ,cvec = 0 if image else 'add:1-47' ) 28return {'baseline' :records ,'candidate' :candidate , 29'engine_info' : {'baseline' :dict (info ),'candidate' :dict (info )}} 30 31 32class IsolationGate (unittest .TestCase ): 33def test_complete_evidence (self ): 34for scenario in ('image' ,'add' ,'project' ): 35verify (evidence (scenario ),scenario ) 36 37def test_incomplete_or_wrong_evidence_rejected (self ): 38for scenario in ('image' ,'add' ): 39cases = { 40'incompatible reuse' :lambda d :d ['candidate' ][1 ].update (reused = 5 ), 41'no parked hit' :lambda d :d ['candidate' ][- 1 ].update (reused = 0 ), 42'state mismatch' :lambda d :d ['candidate' ][- 1 ]['state' ].update (gdn = 'ffff' ), 43'missing state' :lambda d :d ['candidate' ][- 1 ]['state' ].clear (), 44'empty output' :lambda d :d ['candidate' ][- 1 ]['ids' ].clear (), 45'missing request' :lambda d :d ['candidate' ].pop (), 46'different residency' :lambda d :d ['engine_info' ]['candidate' ].update (expert_slots = 1 ), 47 } 48for name ,mutate in cases .items (): 49with self .subTest (scenario = scenario ,case = name ): 50data = evidence (scenario ) 51mutate (data ) 52with self .assertRaises (AssertionError ): 53verify (data ,scenario ) 54 55def test_generated_fixtures (self ): 56with tempfile .TemporaryDirectory (prefix = 'strata-isolation-' )as directory : 57p = Path (directory ) 58fixtures (p ,2560 ) 59cv = GGUFFile (p / 'control.gguf' ) 60self .assertEqual (cv .metadata ['general.architecture' ],'controlvector' ) 61self .assertEqual ([t .name for t in cv .tensors ], ['direction.1' ,'direction.20' ]) 62self .assertTrue (all (t .elements == 2560 for t in cv .tensors )) 63a ,b ,grid = [(p / (name + '.sve' )).read_bytes ()for name in ('image-a' ,'image-b' ,'image-grid' )] 64self .assertEqual (struct .unpack ('<5i' ,a [:20 ]), (0x31455653 ,4 ,2 ,2 ,2560 )) 65self .assertEqual (len (a ),20 + 4 * 2560 * 4 ) 66self .assertEqual (a [:20 ],b [:20 ]) 67self .assertNotEqual (a [20 :],b [20 :]) 68self .assertNotEqual (a [:20 ],grid [:20 ]) 69self .assertEqual (a [20 :],grid [20 :]) 70 71def test_dry_run_does_not_read_missing_config (self ): 72with tempfile .TemporaryDirectory (prefix = 'strata-isolation-dry-' )as directory : 73p = Path (directory ) 74result = subprocess .run ([sys .executable ,str (Path (__file__ ).with_name ('conversation_cache_isolation.py' )), 75'--config' ,str (p / 'missing.json' ),'--engine' ,str (p / 'missing' ), 76'--output' ,str (p / 'not-created' ),'--scenario' ,'image' ], 77capture_output = True ,text = True ,timeout = 10 ) 78self .assertEqual (result .returncode ,0 ,result .stderr ) 79self .assertIn ('no model loaded' ,result .stdout ) 80self .assertFalse ((p / 'not-created' ).exists ()) 81 82 83if __name__ == '__main__' : 84unittest .main ()