okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
abbf12caf
main
1"""Destructive-to-cache HTTP smoke test for an EXCLUSIVE, idle Strata test server. 2 3Sends synthetic conversations, deliberately evicts cached conversations, and 4disconnects a streaming response. Never run while other clients are using it. 5Dry-run by default; --run explicitly enables network traffic. Does not restart 6or reconfigure the server. Use the private-engine parity gate for state hashes. 7""" 8import argparse 9import json 10from pathlib import Path 11import time 12import urllib .request 13import uuid 14 15 16def require (condition ,message ): 17if not condition : 18raise RuntimeError (message ) 19 20 21class Client : 22def __init__ (self ,url ): 23self .url = url .rstrip ('/' ) 24 25def metrics (self ): 26with urllib .request .urlopen (self .url + '/metrics' ,timeout = 10 )as response : 27return json .load (response ) 28 29def request (self ,model ,content ,stream = False ,count = 8 ): 30body = {'model' :model ,'messages' : [{'role' :'user' ,'content' :content }], 31'max_tokens' :count ,'temperature' :0 ,'stream' :stream , 32'chat_template_kwargs' : {'enable_thinking' :False }} 33request = urllib .request .Request (self .url + '/v1/chat/completions' , 34data = json .dumps (body ).encode (), 35headers = {'Content-Type' :'application/json' }) 36return urllib .request .urlopen (request ,timeout = 120 ) 37 38 39def run (client ): 40initial = client .metrics () 41require (initial ['live' ]['state' ]== 'idle' and initial ['live' ]['queued' ]== 0 ,'server is busy' ) 42info = initial ['engine' ] 43slots = info .get ('conversation_cache_slots' ,0 ) 44require (info .get ('conversation_cache_mib' ,0 )> 0 and 1 <= slots <= 16 , 45'requires an enabled cache with 1–16 slots' ) 46require (not info .get ('api_key' ),'use an unauthenticated private test endpoint' ) 47marker = 'cache-smoke-' + uuid .uuid4 ().hex 48records = [] 49 50def before (): 51metrics = client .metrics () 52require (metrics ['live' ]['state' ]== 'idle' and metrics ['live' ]['queued' ]== 0 , 53'concurrent client detected' ) 54return max ((r ['time' ]for r in metrics ['requests' ]),default = 0 ) 55 56def after (stamp ,label ): 57for _ in range (50 ): 58metrics = client .metrics () 59if metrics ['live' ]['state' ]== 'idle' : 60break 61time .sleep (.2 ) 62require (metrics ['live' ]['state' ]== 'idle' ,'request did not drain' ) 63fresh = [r for r in metrics ['requests' ]if r ['time' ]> stamp ] 64require (len (fresh )== 1 and metrics ['live' ]['queued' ]== 0 , 65'missing request evidence or concurrent traffic' ) 66record = {'name' :label ,** fresh [0 ]} 67records .append (record ) 68return record 69 70def prompt (label ): 71return marker + ' ' + label + '\n' + '\n' .join ( 72f'Record { i } : blue square, green triangle, red circle.' for i in range (128 ) 73 )+ '\nName one color. Answer with one word.' 74 75def ask (label ,content ): 76stamp = before () 77with client .request (info ['model' ],content )as response : 78result = json .load (response ) 79require (bool (result .get ('choices' )),'missing completion' ) 80record = after (stamp ,label ) 81require (record ['finish' ]in ('length' ,'stop' ),'request did not finish normally' ) 82return result ['choices' ][0 ]['message' ],record 83 84a ,_ = ask ('A' ,prompt ('A' )) 85ask ('B' ,prompt ('B' )) 86restored ,record = ask ('A-restored' ,prompt ('A' )) 87require (record ['reused' ]> 0 and a == restored ,'A/B/A reuse or output mismatch' ) 88for i in range (slots + 1 ): 89ask (f'evict- { i } ' ,prompt (f'evict- { i } ' )) 90_ ,record = ask ('A-evicted' ,prompt ('A' )) 91require (record ['reused' ]== 0 ,'old A survived slot eviction' ) 92 93text = marker + ' cancel: ' + 'blue green red. ' * 256 94text += '\nCount from 1 to 1000, one number per line. Do not summarize.' 95stamp = before () 96with client .request (info ['model' ],text ,stream = True ,count = 4096 )as response : 97for line in response : 98if not line .startswith (b'data: ' )or line .strip ()== b'data: [DONE]' : 99continue 100choices = json .loads (line [6 :]).get ('choices' , []) 101if choices and choices [0 ].get ('delta' , {}).get ('content' ): 102break 103else : 104raise RuntimeError ('no streamed content to cancel' ) 105record = after (stamp ,'cancelled' ) 106require (record ['finish' ]in ('disconnect' ,'cancel' )and 0 < record ['output_tokens' ]< 4096 , 107'stream cancellation not observed' ) 108ask ('after-cancel-B' ,marker + ' Unrelated B: say hello.' ) 109_ ,record = ask ('cancelled-A-restored' ,text ) 110require (record ['reused' ]> 0 ,'cancelled conversation was not restored' ) 111return {'engine_info' :info ,'records' :records } 112 113 114def main (): 115parser = argparse .ArgumentParser (description = __doc__ ) 116parser .add_argument ('--url' ,default = 'http://127.0.0.1:11434' ) 117parser .add_argument ('--output' ,type = Path ,required = True ,help = 'new results file; existing paths refused' ) 118parser .add_argument ('--run' ,action = 'store_true' ) 119args = parser .parse_args () 120if not args .run : 121'Dry run: HTTP A/B/A, slot eviction, cancellation and recovery. No network traffic.' ) 122return 123# Refuse accidental overwrites before generating traffic. 124with args .output .open ('x' ,encoding = 'utf-8' )as output : 125try : 126result = run (Client (args .url )) 127except Exception as error : 128json .dump ({'passed' :False ,'error' :str (error )},output ,indent = 2 ) 129output .write ('\n' ) 130raise 131json .dump ({'passed' :True ,** result },output ,indent = 2 ) 132output .write ('\n' ) 133f'PASS: HTTP reuse, slot eviction, cancellation/recovery. Results: { args . output } ' ) 134 135 136if __name__ == '__main__' : 137main ()