okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""#1620: a request cancelled right after it restored a parked conversation must not lose that conversation. 3 4A (long answer) -> B (an unrelated chat parks A) -> A2 = A + a short next turn, STOPped as soon as the engine 5says RESUME (the parked A is restored, nothing or a few windows read) -> A3 = A + another next turn. 6 7A3 must reuse the whole of A and its answer from the live session; without the fix the cancelled A2 had taken 8the parked entry and A3 fell back to the turn checkpoint (the prompt, not the answer). 9 10python3 research/tools/live-probes-2026-10/cancel_park_test.py CFG # CFG: a run config json (as tools used by smoke); needs a conversation cache: 11STRATA_EXTRA_ARGS="--prompt-cache 4 --conversation-cache-mib 2048 --conversation-cache-slots 3" 12Prints PASS / FAIL and the reused token counts. Exit status 0 on PASS. 13""" 14import json ,os ,queue ,sys ,threading ,time 15from pathlib import Path 16 17REPO = Path (os .environ .get ("STRATA_REPO" ,Path (__file__ ).resolve ().parents [3 ])) 18sys .path .insert (0 ,str (REPO / "tools" ));sys .path .insert (0 ,str (REPO )) 19import strata_tokenizer as ST 20from serve .server import StrataEngine ,child_env 21 22cfg = json .load (open (sys .argv [1 ])) 23tp = Path (cfg ["tokenizer" ]) 24vocab = json .loads ((tp / "vocab.json" ).read_text ()) 25toks = [None ]* len (vocab ) 26for t ,i in vocab .items (): 27toks [i ]= t 28tok = ST .Tokenizer (toks , (tp / "merges.txt" ).read_text ().split ("\n" ),json .loads ((tp / "token_type.json" ).read_text ())) 29args = cfg ["args" ]+ os .environ .get ("STRATA_EXTRA_ARGS" ,"" ).split () 30eng = StrataEngine (os .environ .get ("STRATA_EXE" ,cfg ["exe" ]),args ,cwd = cfg .get ("cwd" ), 31log = os .environ .get ("SMOKE_LOG" ,cfg ["log" ]),env = child_env (cfg )) 32 33HEAD = "<|im_start|>user\n" 34GEN = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" 35 36 37def enc (text ): 38return tok .encode (text ,parse_special = True ) 39 40 41def run (ids ,max_new = 200 ): 42out = [t for t in eng .generate (ids ,max_new , {"temperature" :0 },threading .Event ())if t is not None ] 43return out ,int (eng .last .get ("reused" )or 0 ) 44 45 46def run_cancelled (ids ): 47"""GEN, then STOP once the engine has said RESUME (the restore is done, the prompt is not read yet).""" 48eng .proc .stdin .write ("GEN 200 " + "," .join (str (int (t ))for t in ids )+ "\n" ) 49eng .proc .stdin .flush () 50stopped ,reused ,fin = False ,None ,None 51deadline = time .time ()+ 600 52while time .time ()< deadline : 53try : 54line = eng .lines .get (timeout = 1 ) 55except queue .Empty : 56continue 57if line is None : 58raise SystemExit ("FAIL: the engine died" ) 59if line .startswith ("RESUME " ): 60reused = int (line .split ()[1 ]) 61eng .proc .stdin .write ("STOP\n" );eng .proc .stdin .flush ();stopped = True 62elif line .startswith ("DONE " ): 63fin = line .split ()[5 ]if len (line .split ())> 5 else "?" 64break 65return reused ,fin ,stopped 66 67 68story = " " .join (f"Item { i } : the quick brown fox number { i } jumps over lazy dog { i * 7 } ." for i in range (70 )) 69A1 = enc (HEAD + story + "\nNow count from 1 to 120 separated by commas." + GEN ) 70outA ,r0 = run (A1 ,300 ) 71f"A: prompt { len ( A1 ) } tokens, { len ( outA ) } generated, reused { r0 } " ,flush = True ) 72B1 = enc (HEAD + "Say hello in one short sentence." + GEN ) 73outB ,rb = run (B1 ,20 ) 74f"B: prompt { len ( B1 ) } tokens, reused { rb } (A is parked now)" ,flush = True ) 75cont = enc ("<|im_end|>\n<|im_start|>user\nNow say only the word done." + GEN ) 76A2 = A1 + outA + cont 77reused2 ,fin2 ,stopped = run_cancelled (A2 ) 78f"A2 (cancelled): restored/reused at RESUME { reused2 } , finish { fin2 } , STOP sent { stopped } " ,flush = True ) 79cont3 = enc ("<|im_end|>\n<|im_start|>user\nNow say only the word finished." + GEN ) 80A3 = A1 + outA + cont3 81outA3 ,reused3 = run (A3 ,20 ) 82want = len (A1 )+ len (outA )- 3 83f"A3: prompt { len ( A3 ) } tokens, reused { reused3 } (A + its answer = { len ( A1 ) + len ( outA ) } ; need >= { want } )" ,flush = True ) 84eng .proc .stdin .close () 85try : 86eng .proc .wait (60 ) 87except Exception : 88eng .proc .kill () 89ok = fin2 == "cancel" and reused3 >= want 90"PASS" if ok else "FAIL" ) 91sys .exit (0 if ok else 1 )