okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
311926416
main
1"""serve/test_tool_opener_misspelt.py - #5276: `<toolcall>` (no underscore) before `<function=NAME>` of a declared tool opens the call. 2 3Seen live (Qwen3.8-Flash-Next UD-Q4_K_XL, OpenMinis on /v1/messages): the model wrote the opener as ordinary text, misspelled 4`<toolcall>`, then a whole call of a declared tool and the real `</tool_call>`. The block came back as answer text, the turn 5ended and the agent waited for a result that never came. The misspelling counts only where a `<tool_call>` would open a call 6(start of a line, outside code) and only when a declared tool's `<function=NAME>` follows; prose never triggers it. 7 8python -m unittest serve.test_tool_opener_misspelt -v 9""" 10import sys 11import unittest 12from pathlib import Path 13 14sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 15 16from serve .frontend import OutputParser # noqa: E402 17 18BASH = {"name" :"bash" ,"parameters" : {"properties" : {"command" : {"type" :"string" },"tooltitle" : {"type" :"string" }}}} 19BODY = ("<function=bash>\n<parameter=command>\ncd proj && rg -o 'x' landing.html | sort | uniq -c\n</parameter>\n" 20"<parameter=tooltitle>\nCount\n</parameter>\n</function>\n" ) 21ARGS = {"command" :"cd proj && rg -o 'x' landing.html | sort | uniq -c" ,"tooltitle" :"Count" } 22 23 24def run (text ,stream_tools = False ,recover = True ,thinking = False ,step = 3 ,tools = (BASH ,)): 25p = OutputParser (thinking = thinking ,tools = list (tools ),stream_tools = stream_tools ,recover = recover ) 26evs = [] 27for i in range (0 ,len (text ),step ): 28evs += p .feed (text [i :i + step ]) 29evs += p .finish ("stop" ) 30return evs 31 32 33def calls (evs ): 34return [(e .call .name ,e .call .arguments )for e in evs if e .kind == "tool_call" ] 35 36 37def text (evs ): 38return "" .join (e .text or "" for e in evs if e .kind in ("content" ,"reasoning" )) 39 40 41class Misspelt (unittest .TestCase ): 42def test_issue_block (self ): 43for opener in ("<toolcall>" ,"<tool-call>" ): 44for stream in (False ,True ): 45for step in (1 ,3 ,1000 ): 46for recover in (False ,True ): 47evs = run (opener + "\n" + BODY + "</tool_call>" ,stream ,recover ,step = step ) 48self .assertEqual (calls (evs ), [("bash" ,ARGS )], (opener ,stream ,step ,recover )) 49self .assertEqual (text (evs ).strip (),"" ) 50 51def test_without_the_closing_tag_as_the_real_opener (self ): 52self .assertEqual (calls (run ("<toolcall>\n" + BODY )),calls (run ("<tool_call>\n" + BODY ))) 53 54def test_mid_line_as_the_real_opener (self ): 55t = "ok <toolcall>\n" + BODY + "</tool_call>" 56self .assertEqual (calls (run (t )),calls (run (t .replace ("<toolcall>" ,"<tool_call>" )))) 57self .assertEqual (calls (run (t )), [("bash" ,ARGS )]) 58 59def test_in_the_reasoning_mid_line_stays_text (self ): 60evs = run ("hm <toolcall>\n" + BODY + "</tool_call>\n</think>\n" ,thinking = True ) 61self .assertEqual (calls (evs ), []) 62 63def test_text_before_is_kept (self ): 64evs = run ("I will count.\n<toolcall>\n" + BODY + "</tool_call>" ) 65self .assertEqual (calls (evs ), [("bash" ,ARGS )]) 66self .assertEqual (text (evs ).strip (),"I will count." ) 67 68def test_in_the_reasoning (self ): 69evs = run ("hm\n<toolcall>\n" + BODY + "</tool_call>\n</think>\n" ,thinking = True ) 70self .assertEqual (calls (evs ), [("bash" ,ARGS )]) 71 72def test_prose_naming_the_tag_stays_text (self ): 73for t in ("The model may write <toolcall> by mistake." , 74"Write <toolcall> and then a call.\n" , 75"<toolcall>\nnot a call\n</tool_call>" , 76"<toolcall>" , 77"<toolcall>\n<function=nothing>\n<parameter=a>\n1\n</parameter>\n</function>\n</tool_call>" ): 78for stream in (False ,True ): 79evs = run (t ,stream ) 80self .assertEqual (calls (evs ), [],t ) 81self .assertEqual (text (evs ),t ) 82 83def test_code_fence_stays_text (self ): 84t = "Example:\n```\n<toolcall>\n" + BODY + "</tool_call>\n```\n" 85evs = run (t ) 86self .assertEqual (calls (evs ), []) 87self .assertEqual (text (evs ),t ) 88 89def test_indented_code_stays_text (self ): 90t = "Example:\n\n <toolcall>\n " + BODY .replace ("\n" ,"\n " )+ "</tool_call>\n" 91self .assertEqual (calls (run (t )), []) 92 93def test_no_tools_declared (self ): 94t = "<toolcall>\n" + BODY + "</tool_call>" 95self .assertEqual (calls (run (t ,tools = ())), []) 96self .assertEqual (text (run (t ,tools = ())),t ) 97 98def test_the_real_opener_is_unchanged (self ): 99evs = run ("<tool_call>\n" + BODY + "</tool_call>" ) 100self .assertEqual (calls (evs ), [("bash" ,ARGS )]) 101 102 103if __name__ == "__main__" : 104unittest .main ()