okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1"""A missing wrapper closer ends before a following call, but prose alone does not confirm a call. 2 3A call's own </function> separates it from the next wrapper without merging their arguments. 4Parameter values may still contain call-format tags; recovery batches stay opt-in. 5 6python -m unittest serve.test_tool_call_unclosed -v 7""" 8import json 9import sys 10import unittest 11from pathlib import Path 12 13sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 14 15from serve .frontend import OutputParser # noqa: E402 16 17TOOLS = [{"name" :"read" ,"parameters" : {"properties" : {"path" : {"type" :"string" }}}}, 18 {"name" :"write" ,"parameters" : {"properties" : {"path" : {"type" :"string" },"content" : {"type" :"string" }}}}] 19READ = "<function=read>\n<parameter=path>\na.py\n</parameter>\n</function>" 20WRITE = "<function=write>\n<parameter=path>\nb.py\n</parameter>\n<parameter=content>\nx = 1\n</parameter>\n</function>" 21 22 23def run (text ,stream_tools ,recover ,step ): 24p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = stream_tools ,recover = recover ) 25evs = [] 26for i in range (0 ,len (text ),step ):# fed in small pieces, as a stream arrives 27evs += p .feed (text [i :i + step ]) 28evs += p .finish ("stop" ) 29calls = [(e .call .name ,e .call .arguments )for e in evs if e .kind == "tool_call" ] 30if stream_tools and not calls : 31arguments = "" .join (e .text for e in evs if e .kind == "tool_args" ) 32if arguments : 33try : 34json .loads (arguments ) 35except ValueError : 36pass 37else : 38raise AssertionError ("an unconfirmed call has complete streamed arguments" ) 39return calls ,"" .join (e .text for e in evs if e .kind == "content" ) 40 41 42class UnclosedCall (unittest .TestCase ): 43def check (self ,text ,want_calls ,want_content = "" ): 44for stream_tools in (False ,True ): 45for recover in (False ,True ): 46for step in (1 ,3 ,7 ,4096 ): 47with self .subTest (stream_tools = stream_tools ,recover = recover ,step = step ): 48calls ,content = run (text ,stream_tools ,recover ,step ) 49self .assertEqual (calls ,want_calls ) 50self .assertEqual (content .strip (),want_content ) 51 52def test_closed_calls_are_unchanged (self ): 53self .check ("<tool_call>\n" + READ + "\n</tool_call>\n<tool_call>\n" + WRITE + "\n</tool_call>" , 54 [("read" , {"path" :"a.py" }), ("write" , {"path" :"b.py" ,"content" :"x = 1" })]) 55 56def test_a_second_call_after_an_unclosed_one_is_its_own_call (self ): 57self .check ("<tool_call>\n" + READ + "\n<tool_call>\n" + WRITE + "\n</tool_call>" , 58 [("read" , {"path" :"a.py" }), ("write" , {"path" :"b.py" ,"content" :"x = 1" })]) 59 60def test_quoted_incomplete_wrapper_stays_literal (self ): 61text = "Format: <tool_call>\n" + READ + "\nand then I explain it." 62self .check (text , [],text ) 63 64def test_following_literal_marker_does_not_complete_an_unclosed_call (self ): 65text = "<tool_call>\n" + READ + "\n<tool_call> is a format marker." 66self .check (text , [],text ) 67 68def test_omitted_wrapper_recovers_only_on_a_natural_stop (self ): 69for closed in (True ,False ): 70text = "<tool_call>\n" + READ + ("\n</tool_call>" if closed else "" ) 71for reason in (None ,"stop" ,"length" ,"cancel" ,"disconnect" ): 72for step in (1 ,7 ,4096 ): 73with self .subTest (closed = closed ,reason = reason ,step = step ): 74p = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = True ) 75events = [] 76for offset in range (0 ,len (text ),step ): 77events += p .feed (text [offset :offset + step ]) 78events += p .finish (reason ) 79calls = [e for e in events if e .kind == "tool_call" ] 80complete = closed or reason in (None ,"stop" ) 81self .assertEqual (len (calls ),int (complete )) 82if not complete : 83with self .assertRaises (ValueError ): 84json .loads ("" .join (e .text for e in events if e .kind == "tool_args" )) 85 86def test_a_value_that_holds_the_tags_still_ends_at_the_calls_own_closer (self ): 87value = "a </function> and a <tool_call> in a string" 88call = ("<tool_call>\n<function=write>\n<parameter=path>\nc.py\n</parameter>\n<parameter=content>\n" + value + 89"\n</parameter>\n</function>\n</tool_call>" ) 90self .check (call , [("write" , {"path" :"c.py" ,"content" :value })]) 91 92 93if __name__ == "__main__" : 94unittest .main ()