okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""Stop, edit and resend, over and over (#879, #606): every answer must be real text, never a repeated token. 3 4The reports are an all-logits NaN (one token repeated, "!!!!") after a request was stopped and the next one, an edit 5or a resend of the same prompt, started while the engine was still winding the first down. This drives that loop 6against a running server: stream an answer, read a few lines, close the connection (the client's Stop), wait 0 to 7~0.4 s (sometimes not at all, sometimes a second request already running), send the edited prompt, and check the 8answer. Run the engine with STRATA_DBG_NAN=1 to get the first non-finite layer named in its log. 9 10STRATA_KEY=... python3 research/tools/live-probes-2026-10/stop_resend_test.py http://127.0.0.1:8090 [rounds=40] [seed=1] 11 12Exit 0 when every answer was real text, 1 otherwise. 13""" 14import json 15import os 16import random 17import re 18import sys 19import threading 20import time 21import urllib .request 22 23BASE = sys .argv [1 ].rstrip ("/" )if len (sys .argv )> 1 else "http://127.0.0.1:8080" 24ROUNDS = int (sys .argv [2 ])if len (sys .argv )> 2 else 40 25SEED = int (sys .argv [3 ])if len (sys .argv )> 3 else 1 26KEY = os .environ .get ("STRATA_KEY" ,"" ) 27URL = BASE + "/v1/chat/completions" 28 29LONG = ["Describe the weather in Montpellier in great detail, at length." , 30"Write a long story about a lighthouse keeper." , 31"Explain how a bicycle works, in as much detail as you can." ] 32SHORT = [("Reply with exactly one word: the color of the sky on a clear day." ,"blue" ), 33 ("Reply with exactly one word: the opposite of cold." ,"hot|warm" ), 34 ("What is 2 plus 3? Reply with the number only." ,"5|five" )] 35 36 37def req (content ,stream ,max_tokens ): 38body = json .dumps ({"model" :"m" ,"max_tokens" :max_tokens ,"temperature" :0 ,"stream" :stream , 39"messages" : [{"role" :"user" ,"content" :content }], 40"chat_template_kwargs" : {"enable_thinking" :False }}).encode () 41return urllib .request .Request (URL ,data = body ,headers = {"Content-Type" :"application/json" , 42"Authorization" :f"Bearer { KEY } " }) 43 44 45def stop_after (content ,lines ): 46r = urllib .request .urlopen (req (content ,True ,400 ),timeout = 600 ) 47for _ in range (lines ): 48r .readline () 49r .close () 50 51 52def answer (content ,max_tokens = 40 ): 53with urllib .request .urlopen (req (content ,False ,max_tokens ),timeout = 600 )as r : 54m = json .load (r )["choices" ][0 ]["message" ] 55return ((m .get ("content" )or "" )+ " " + (m .get ("reasoning_content" )or m .get ("reasoning" )or "" )).strip () 56 57 58def garbage (text ): 59"""Empty, one character or one token repeated (the NaN signature: '!!!!!!', 'the the the the').""" 60t = text .strip () 61if len (t )< 1 : 62return True 63if len (t )>= 8 and len (set (t .replace (" " ,"" )))<= 2 : 64return True 65words = re .findall (r"\S+" ,t ) 66return len (words )>= 6 and len (set (words ))== 1 67 68 69def main (): 70rnd = random .Random (SEED ) 71bad = 0 72for i in range (ROUNDS ): 73long_p = rnd .choice (LONG ) 74q ,want = rnd .choice (SHORT ) 75mode = rnd .choice (("stop-resend" ,"stop-edit" ,"overlap" )) 76bg = None 77try : 78if mode == "overlap" :# the next request is already running while the first one is stopped 79bg = threading .Thread (target = lambda :answer (rnd .choice (LONG ),60 )) 80bg .start () 81time .sleep (rnd .uniform (0 ,0.3 )) 82stop_after (long_p ,rnd .randint (1 ,8 )) 83time .sleep (rnd .choice ((0 ,0 ,0.05 ,0.2 ,0.4 ))) 84text = answer (long_p if mode == "stop-resend" else q ,40 if mode == "stop-resend" else 30 ) 85except Exception as e :# a refused or dropped request is a failure of its own 86f" { i :3d } { mode } : ERROR { e !r } " ,flush = True ) 87bad += 1 88continue 89finally : 90if bg : 91bg .join () 92ok = not garbage (text )and (mode == "stop-resend" or re .search (want ,text ,re .I )is not None ) 93bad += not ok 94f" { i :3d } { mode } : { 'ok' if ok else 'BAD' } { text [: 70 ]!r } " ,flush = True ) 95f" { ROUNDS } rounds, { bad } bad" ) 96return 1 if bad else 0 97 98 99if __name__ == "__main__" : 100sys .exit (main ())