okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
311926416
main
1"""serve/test_tool_name_prefix.py - #5409: `<function=tools actio>` and the bare `<function=tools>` are a declared tool's call. 2 3Seen live (Qwen3.8-Flash-Next IQ3_S through Pi, after code-mode work that spells its tools `tools.actio(...)`): the model wrote 4the prefix of #4751 with a space for the dot, or alone. Neither named a declared tool, so the block came back as answer text and 5the turn ended. The space form is `tools.NAME` again; the bare prefix is the one declared tool that takes the call's parameters 6(only with tool_call_recovery). Anything else stays text as before, and an exactly declared name always wins. 7 8python -m unittest serve.test_tool_name_prefix -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 18ACTIO = {"name" :"actio" ,"parameters" : {"properties" : {"command" : {"type" :"string" }},"required" : ["command" ]}} 19READ = {"name" :"read" ,"parameters" : {"properties" : {"path" : {"type" :"string" }},"required" : ["path" ]}} 20BASH = {"name" :"bash" ,"parameters" : {"properties" : {"command" : {"type" :"string" },"timeout" : {"type" :"integer" }}}} 21 22 23def call (name ,param = "command" ,value = "ls" ): 24return f"<tool_call>\n<function= { name } >\n<parameter= { param } >\n { value } \n</parameter>\n</function>\n</tool_call>" 25 26 27def run (text ,tools ,stream_tools = False ,recover = True ,thinking = False ,step = 3 ): 28p = OutputParser (thinking = thinking ,tools = tools ,stream_tools = stream_tools ,recover = recover ) 29evs = [] 30for i in range (0 ,len (text ),step ): 31evs += p .feed (text [i :i + step ]) 32evs += p .finish ("stop" ) 33return evs 34 35 36def calls (evs ): 37return [(e .call .name ,e .call .arguments )for e in evs if e .kind == "tool_call" ] 38 39 40def text (evs ): 41return "" .join (e .text or "" for e in evs if e .kind in ("content" ,"reasoning" )) 42 43 44class SpaceForm (unittest .TestCase ): 45def test_every_prefix_and_mode (self ): 46for prefix in ("tools" ,"functions" ,"default_api" ): 47for stream in (False ,True ): 48for step in (1 ,3 ,1000 ): 49evs = run (call (f" { prefix } actio" ), [ACTIO ,READ ],stream_tools = stream ,step = step ) 50self .assertEqual (calls (evs ), [("actio" , {"command" :"ls" })], (prefix ,stream ,step )) 51self .assertEqual (text (evs ).strip (),"" ) 52 53def test_without_recovery_too (self ): 54evs = run (call ("tools actio" ), [ACTIO ],recover = False ) 55self .assertEqual (calls (evs ), [("actio" , {"command" :"ls" })]) 56 57def test_text_before_the_call_is_kept (self ): 58evs = run ("Let me look.\n" + call ("tools actio" ), [ACTIO ]) 59self .assertEqual (calls (evs ), [("actio" , {"command" :"ls" })]) 60self .assertEqual (text (evs ).strip (),"Let me look." ) 61 62def test_in_the_reasoning (self ): 63evs = run ("thinking...\n" + call ("tools actio" )+ "\n</think>\n" , [ACTIO ],thinking = True ) 64self .assertEqual (calls (evs ), [("actio" , {"command" :"ls" })]) 65 66def test_undeclared_name_stays_text (self ): 67for nm in ("tools nothing" ,"tools read now" ): 68evs = run (call (nm ), [ACTIO ,READ ]) 69self .assertEqual (calls (evs ), [],nm ) 70self .assertIn (f"<function= { nm } >" ,text (evs )) 71 72def test_cut_off_name_is_flushed_as_text (self ): 73t = "<tool_call>\n<function=tools ac" 74evs = run (t , [ACTIO ]) 75self .assertEqual (calls (evs ), []) 76self .assertEqual (text (evs ),t ) 77 78def test_two_calls (self ): 79evs = run (call ("tools actio" )+ "\n" + call ("tools read" ,"path" ,"/x" ), [ACTIO ,READ ],stream_tools = True ) 80self .assertEqual (calls (evs ), [("actio" , {"command" :"ls" }), ("read" , {"path" :"/x" })]) 81 82 83class BarePrefix (unittest .TestCase ): 84def test_one_tool_fits (self ): 85for prefix in ("tools" ,"functions" ): 86for stream in (False ,True ): 87for step in (1 ,3 ,1000 ): 88evs = run (call (prefix ,"path" ,"/x" ), [ACTIO ,READ ],stream_tools = stream ,step = step ) 89self .assertEqual (calls (evs ), [("read" , {"path" :"/x" })], (prefix ,stream ,step )) 90 91def test_two_tools_fit_stays_text (self ): 92both = {"name" :"also" ,"parameters" : {"properties" : {"path" : {"type" :"string" }}}} 93evs = run (call ("tools" ,"path" ,"/x" ), [READ ,both ]) 94self .assertEqual (calls (evs ), []) 95self .assertIn ("<function=tools>" ,text (evs )) 96 97def test_none_fits_stays_text (self ): 98evs = run (call ("tools" ,"nope" ,"1" ), [ACTIO ,READ ]) 99self .assertEqual (calls (evs ), []) 100self .assertIn ("<function=tools>" ,text (evs )) 101 102def test_required_parameter_missing (self ): 103evs = run (call ("tools" ,"timeout" ,"5" ), [ACTIO ,READ ])# `command` is required by actio 104self .assertEqual (calls (evs ), []) 105 106def test_optional_only_call_fits_one (self ): 107evs = run (call ("tools" ,"timeout" ,"5" ), [BASH ,READ ]) 108self .assertEqual (calls (evs ), [("bash" , {"timeout" :5 })]) 109 110def test_off_without_recovery (self ): 111evs = run (call ("tools" ,"path" ,"/x" ), [READ ],recover = False ) 112self .assertEqual (calls (evs ), []) 113self .assertIn ("<function=tools>" ,text (evs )) 114 115def test_a_declared_tool_named_tools_wins (self ): 116mine = {"name" :"tools" ,"parameters" : {"properties" : {"x" : {"type" :"string" }}}} 117evs = run (call ("tools" ,"x" ,"1" ), [mine ,READ ]) 118self .assertEqual (calls (evs ), [("tools" , {"x" :"1" })]) 119 120def test_unfinished_bare_call_is_flushed_as_text (self ): 121t = "<tool_call>\n<function=tools>\n<parameter=path>\n/x" 122evs = run (t , [READ ],stream_tools = True ) 123self .assertEqual (calls (evs ), []) 124self .assertEqual (text (evs ),t ) 125 126def test_no_tools_declared_is_unchanged (self ): 127evs = run (call ("tools actio" ), []) 128self .assertEqual (calls (evs ), []) 129 130 131if __name__ == "__main__" : 132unittest .main ()