okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
1"""The upstream comparison must reject mismatches and incomplete evidence.""" 2import copy 3import hashlib 4import json 5from pathlib import Path 6import subprocess 7import sys 8import tempfile 9import unittest 10 11sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ]))# runnable as tools/test_x.py from the repo root too 12 13from tools .conversation_cache_disabled import NAMES ,SETTINGS ,STATE_KEYS ,common_args ,state_hashes ,verify 14 15 16def fixture (): 17requests = [dict (name = n ,ids = [1 ,2 ],max_new = 16 ,expected = '4' if n == 'short' else None )for n in NAMES ] 18digest = hashlib .sha256 (json .dumps (requests ,sort_keys = True ).encode ()).hexdigest () 19baseline = dict (name = 'upstream' ,args = ['--kv' ,'int8' ],info = {key :1 for key in SETTINGS }, 20request_digest = digest ,cache_events = [],records = []) 21for name in NAMES : 22baseline ['records' ].append (dict (name = name ,ids = [4 ],text = '4' ,finish = 'stop' ,prompt_tokens = 2 , 23reused = 2 if name in ('A-live' ,'A-checkpoint' )else 0 , 24state = {key :'abc' for key in STATE_KEYS })) 25candidate = copy .deepcopy (baseline ) 26candidate ['name' ]= 'candidate-1' 27candidate ['info' ]['conversation_cache_mib' ]= 0 28candidate ['info' ]['conversation_disk_mib' ]= 0 29return dict (arms = [baseline ,candidate ],requests = requests ,request_digest = digest ) 30 31 32class DisabledCacheGateTest (unittest .TestCase ): 33def test_reference_fixture (self ): 34verify (fixture ()) 35 36def test_upstream_hash_schema (self ): 37line = 'strata serve: STATE_HASH L=2 gdn=ab ple=cd tail=ef pooled=12 kv=34 mtp=56 stale=78 ple_prev=1,2' 38self .assertEqual (set (state_hashes (line )[0 ]),set (STATE_KEYS )) 39self .assertEqual (state_hashes (line ),state_hashes (line + ' dead=99' )) 40# the candidate's pooled= is the upstream extent; pooled_full= (with the spare row) is not compared 41self .assertEqual (state_hashes (line ),state_hashes (line + ' dead=99 pooled_full=ff' ,candidate = True )) 42self .assertNotEqual (state_hashes (line ),state_hashes (line .replace ('pooled=12' ,'pooled=ff' )+ 43' pooled_full=12' ,candidate = True )) 44with self .assertRaises (AssertionError ): 45state_hashes (line ,candidate = True ) 46with self .assertRaises (AssertionError ): 47state_hashes (line .replace ('kv=34 ' ,'' )) 48 49def test_rejects_bad_evidence (self ): 50mutations = [ 51lambda r :r ['arms' ].pop (), 52lambda r :r ['requests' ].pop (), 53lambda r :r ['requests' ][0 ]['ids' ].append (3 ), 54lambda r :r ['arms' ][1 ]['args' ].append ('--different' ), 55lambda r :r ['arms' ][1 ].update (request_digest = 'different' ), 56lambda r :r ['arms' ][1 ]['records' ].pop (), 57lambda r :r ['arms' ][1 ]['info' ].update (expert_slots = 2 ), 58lambda r :r ['arms' ][1 ]['info' ].pop ('kv' ), 59lambda r :r ['arms' ][1 ]['info' ].update (conversation_cache_mib = 8192 ), 60lambda r :r ['arms' ][1 ]['info' ].pop ('conversation_cache_mib' ), 61lambda r :r ['arms' ][1 ]['info' ].update (conversation_disk_mib = 16384 ), 62lambda r :r ['arms' ][1 ]['cache_events' ].append ('disk cache: spilled' ), 63lambda r :r ['arms' ][1 ]['records' ][0 ].update (ids = []), 64lambda r :r ['arms' ][1 ]['records' ][0 ].update (ids = [5 ]), 65lambda r :r ['arms' ][1 ]['records' ][0 ].update (finish = 'disconnect' ), 66lambda r :r ['arms' ][1 ]['records' ][0 ].update (prompt_tokens = 1 ), 67lambda r :r ['arms' ][1 ]['records' ][0 ]['state' ].pop ('gdn' ), 68lambda r :r ['arms' ][1 ]['records' ][0 ]['state' ].update (kv = 'bad' ), 69lambda r :r ['arms' ][1 ]['records' ][0 ].update (reused = 1 ), 70lambda r : [a ['records' ][0 ].update (text = '5' )for a in r ['arms' ]], 71lambda r : [a ['records' ][2 ].update (reused = 0 )for a in r ['arms' ]], 72lambda r : [a ['records' ][5 ].update (reused = 2 )for a in r ['arms' ]], 73 ] 74for mutation in mutations : 75with self .subTest (mutation = mutations .index (mutation )): 76result = fixture () 77mutation (result ) 78with self .assertRaises (AssertionError ): 79verify (result ) 80 81def test_optimized_python_still_rejects (self ): 82code = ('from tools.test_conversation_cache_disabled import fixture, verify; ' 83'r=fixture(); r["arms"][1]["records"][0]["ids"]=[5]; verify(r)' ) 84p = subprocess .run ([sys .executable ,'-O' ,'-c' ,code ],capture_output = True ,text = True ) 85self .assertNotEqual (p .returncode ,0 ) 86self .assertIn ('output differs' ,p .stderr ) 87 88def test_cache_options_omitted_from_shared_arguments (self ): 89cfg = {'args' : ['--pack' ,'/model' ,'--conversation-cache-mib' ,'8192' , 90'--conversation-cache-disk' ,'/cache' ,'--kv' ,'int8' ]} 91args = common_args (cfg ,4 ) 92self .assertEqual (args [:4 ], ['--pack' ,'/model' ,'--kv' ,'int8' ]) 93self .assertFalse (any (x .startswith ('--conversation-cache-' )for x in args )) 94 95def test_dry_run_does_not_load_or_create_output (self ): 96with tempfile .TemporaryDirectory ()as tmp : 97out = Path (tmp )/ 'absent' 98p = subprocess .run ([sys .executable ,'tools/conversation_cache_disabled.py' ,'--config' ,'/absent' , 99'--upstream' ,'/absent' ,'--candidate' ,'/absent' ,'--output' ,str (out )], 100capture_output = True ,text = True ) 101self .assertEqual (p .returncode ,0 ,p .stderr ) 102self .assertFalse (out .exists ()) 103 104 105if __name__ == '__main__' : 106unittest .main ()