okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1"""Wrapped-call precheck parity against the original structural walk.""" 2import itertools 3import random 4import unittest 5from unittest .mock import patch 6 7from serve import frontend 8 9 10class ReferenceParser (frontend .OutputParser ): 11def _wrapped_call_end (self ): 12return frontend .call_end (self .buf ,self ._final ) 13 14 15def events_without_ids (events ): 16return [(e .kind ,e .text , (e .call .name ,e .call .arguments )if e .call else None )for e in events ] 17 18 19class CallScanTests (unittest .TestCase ): 20def test_chunk_boundaries_literal_tags_multiple_calls_and_recovery (self ): 21def call (value ): 22return "<tool_call><function=write><parameter=text>" + value + "</parameter></function></tool_call>" 23bodies = [call ("ordinary" ),call ("literal </tool_call> text </function> here" ), 24call ("literal </parameter> prose" ),call ("a" )+ call ("b" ), 25call ("unfinished" )[:- 5 ],call ("incomplete </tool_call> prose" )[:- 30 ], 26"<tool_call><function=write></function></tool_call>" , 27"<function=write><parameter=text>bare</parameter></function>" , 28"<tool_call><function=write><parameter=text>a</parameter></function>" 29"<parameter=write><parameter=text>b</parameter></function></tool_call>" ] 30tools = [{"name" :"write" ,"parameters" : {"properties" : {"text" : {"type" :"string" }}}}] 31rng = random .Random (1030 ) 32for text ,stream ,recover in itertools .product (bodies , (False ,True ), (False ,True )): 33# Every fixed chunk width, plus random partitions; compare after EACH feed. 34for width in range (1 ,16 ): 35a = frontend .OutputParser (thinking = False ,tools = tools ,stream_tools = stream ,recover = recover ) 36b = ReferenceParser (thinking = False ,tools = tools ,stream_tools = stream ,recover = recover ) 37pos = 0 38while pos < len (text ): 39end = pos + (rng .randint (1 ,20 )if width == 15 else width ) 40delta = text [pos :end ] 41self .assertEqual (events_without_ids (a .feed (delta )),events_without_ids (b .feed (delta ))) 42pos = end 43self .assertEqual (events_without_ids (a .finish ()),events_without_ids (b .finish ())) 44 45def test_no_structural_rescans_until_closer_and_reset_for_next_call (self ): 46parser = frontend .OutputParser (thinking = False ) 47with patch .object (frontend ,"call_end" ,wraps = frontend .call_end )as walk : 48for _ in range (2 ): 49parser .feed ("<tool_call><function=write><parameter=text>" ) 50before = walk .call_count 51for _ in range (100 ): 52parser .feed ("abcdefgh" ) 53self .assertEqual (walk .call_count ,before ) 54for char in "</parameter></function></tool_call>" : 55parser .feed (char ) 56self .assertEqual (walk .call_count ,before + 1 ) 57parser .finish ()# #4701: a closed call waits for what might follow it; the end of the turn settles it 58self .assertEqual (parser ._call_end_checked ,0 ) 59 60 61if __name__ == "__main__" : 62unittest .main ()