okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1"""Partial-tag parity, including overlapping prefixes and arbitrary chunk boundaries.""" 2import itertools 3import random 4import unittest 5 6from serve .frontend import OutputParser 7 8 9def reference_hold (text ,tags ): 10return max ((n for tag in tags for n in range (1 ,len (tag ))if text .endswith (tag [:n ])),default = 0 ) 11 12 13class ReferenceParser (OutputParser ): 14def _hold (self ,text ,tags ): 15return reference_hold (text ,tags ) 16 17 18class HoldTests (unittest .TestCase ): 19def test_overlapping_and_empty_tags (self ): 20parser = OutputParser () 21tags = ("" ,"a" ,"aaa" ,"abab" ,"baab" ,"<tool_call>" ,"</think>" ) 22for size in range (8 ): 23for chars in itertools .product ("ab<" ,repeat = size ): 24text = "" .join (chars ) 25self .assertEqual (parser ._hold (text ,tags ),reference_hold (text ,tags ),text ) 26 27def test_every_real_tag_split (self ): 28tags = ("<tool_call>" ,"</think>" ,"</parameter>" ) 29parser = OutputParser () 30for tag in tags : 31for n in range (len (tag )+ 1 ): 32for prefix in ("" ,"prose\n" ,"<" ,"<<<" ,"🙂日本語" ): 33text = prefix + tag [:n ] 34self .assertEqual (parser ._hold (text ,tags ),reference_hold (text ,tags ),text ) 35 36def test_streamed_events_match_reference (self ): 37texts = ["Reasoning.\n</think>\nAnswer café 🙂\n" , 38"Quoted `</think>` text and <tool_call> prose." , 39"\n```xml\n<tool_call>\n```\nAnswer\n<tool_" , 40"<tool_call><function=lookup><parameter=q>abc</parameter></function></tool_call>\nDone" , 41"<tool_call><function=lookup><parameter=q>abc</parameter></function></tool_call></think>Done" ] 42rng = random .Random (1028 ) 43for text ,thinking ,recover ,stream_tools in itertools .product (texts , (False ,True ), (False ,True ), (False ,True )): 44for _ in range (5 ): 45args = dict (thinking = thinking ,tools = [{"name" :"lookup" }],recover = recover ,stream_tools = stream_tools ) 46actual ,expected = OutputParser (** args ),ReferenceParser (** args ) 47def normalized (events ): 48# Tool call IDs are deliberately random; compare their semantic contents. 49return [(e .kind ,e .text , (e .call .name ,e .call .arguments )if e .call else None )for e in events ] 50pos = 0 51while pos < len (text ): 52end = pos + rng .randint (1 ,9 ) 53delta = text [pos :end ] 54self .assertEqual (normalized (actual .feed (delta )),normalized (expected .feed (delta ))) 55pos = end 56self .assertEqual (normalized (actual .finish ()),normalized (expected .finish ())) 57 58 59if __name__ == "__main__" : 60unittest .main ()