okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1"""Native tool parsing regressions; no model or GPU required.""" 2import unittest 3from serve .frontend import OutputParser 4 5CALL = "<tool_call>\n<function=mcp__files__read>\n<parameter=path>\n123\n</parameter>\n</function>\n</tool_call>" 6TOOLS = [{"name" :"mcp__files.read" ,"parameters" : {"properties" : {"path" : {"type" :"string" }}}}] 7 8class NativeTools (unittest .TestCase ): 9def test_unclosed_think_call_and_split_tags (self ): 10for streaming in (False ,True ): 11for step in (1 ,7 ,4096 ): 12parser = OutputParser (thinking = True ,tools = TOOLS ,stream_tools = streaming ) 13events = [] 14text = "Inspect the file.\n" + CALL 15for i in range (0 ,len (text ),step ): 16events += parser .feed (text [i :i + step ]) 17events += parser .finish () 18calls = [e .call for e in events if e .kind == "tool_call" ] 19self .assertEqual (len (calls ),1 ) 20self .assertEqual ((calls [0 ].name ,calls [0 ].arguments ), ("mcp__files.read" , {"path" :"123" })) 21self .assertEqual ("" .join (e .text for e in events if e .kind == "reasoning" ),"Inspect the file.\n" ) 22 23 24def test_whitespace_name_keeps_namespace_schema_and_stream_events (self ): 25for thinking ,prefix in ((True ,"Inspect the file.\n" ), (True ,"Ready.</think>\n\n" ), (False ,"" )): 26for whitespace in (" " ,"\t" ," \t" ): 27text = prefix + CALL .replace ("<function=mcp__files__read>" , 28"<function=" + whitespace + "mcp__files__read" + whitespace + ">" ) 29for streaming in (False ,True ): 30for step in (1 ,7 ,4096 ): 31with self .subTest (thinking = thinking ,prefix = prefix ,whitespace = whitespace , 32streaming = streaming ,step = step ): 33parser = OutputParser (thinking = thinking ,tools = TOOLS ,stream_tools = streaming ) 34events = [] 35for i in range (0 ,len (text ),step ): 36events += parser .feed (text [i :i + step ]) 37events += parser .finish () 38calls = [event .call for event in events if event .kind == "tool_call" ] 39self .assertEqual ([(c .name ,c .arguments )for c in calls ], 40 [("mcp__files.read" , {"path" :"123" })]) 41self .assertTrue (all (event .call .name == "mcp__files.read" 42for event in events if event .kind == "tool_start" )) 43 44 45def test_padded_unknown_wrapper_stays_text_and_unpadded_stays_native (self ): 46for recover in (False ,True ): 47for streaming in (False ,True ): 48for step in (1 ,7 ,4096 ): 49for name in (" missing " ,"\tmissing\t" ,"mcp__ files" ," " ): 50text = "<tool_call><function=" + name + "></function></tool_call>" 51parser = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = streaming ,recover = recover ) 52events = [] 53for i in range (0 ,len (text ),step ): 54events += parser .feed (text [i :i + step ]) 55events += parser .finish () 56self .assertFalse (any (event .kind .startswith ("tool" )for event in events )) 57self .assertEqual ("" .join (event .text for event in events if event .kind == "content" ),text ) 58# an unpadded name nobody declared: text where tools are declared (the 0.2.2 rule, #3075); the Responses 59# route of a request with no tools (Codex compaction, declared_only off) keeps it as a native call 60text = "<tool_call><function=missing></function></tool_call>" 61for tools ,declared_only ,expected in ((TOOLS ,False , []), (TOOLS ,True , []), (None ,False , ["missing" ])): 62parser = OutputParser (thinking = False ,tools = tools ,stream_tools = streaming ,recover = recover , 63declared_only = declared_only ) 64events = [] 65for i in range (0 ,len (text ),step ): 66events += parser .feed (text [i :i + step ]) 67events += parser .finish () 68self .assertEqual ([event .call .name for event in events if event .kind == "tool_call" ],expected ) 69 70 71def test_padded_unknown_second_call_is_not_announced (self ): 72text = CALL [:- len ("</tool_call>" )]+ "<function= missing ></function></tool_call>" 73for step in (1 ,7 ,4096 ): 74parser = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = True ,recover = True ) 75events = [] 76for i in range (0 ,len (text ),step ): 77events += parser .feed (text [i :i + step ]) 78events += parser .finish () 79self .assertEqual ([event .call .name for event in events if event .kind == "tool_call" ], ["mcp__files.read" ]) 80self .assertEqual ([event .call .name for event in events if event .kind == "tool_start" ], ["mcp__files.read" ]) 81self .assertIn ("<function= missing >" ,"" .join (event .text for event in events if event .kind == "content" )) 82 83 84def test_padded_bare_alias_is_declared_and_schema_typed (self ): 85text = CALL .replace ("<tool_call>\n" ,"" ).replace ("\n</tool_call>" ,"" ).replace ( 86"<function=mcp__files__read>" ,"<function= mcp__files__read >" ) 87for step in (1 ,7 ,4096 ): 88parser = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = True ,recover = True ) 89events = [] 90for i in range (0 ,len (text ),step ): 91events += parser .feed (text [i :i + step ]) 92events += parser .finish () 93self .assertEqual ([(event .call .name ,event .call .arguments )for event in events if event .kind == "tool_call" ], 94 [("mcp__files.read" , {"path" :"123" })]) 95starts = [event .call .name for event in events if event .kind == "tool_start" ] 96self .assertEqual (starts , ["mcp__files.read" ]) 97 98 99def test_literal_call_markers_remain_reasoning_prose (self ): 100for prefix in ("Quote <tool_call> as prose." ,"Use `<tool_call>` in docs." ,"Literal marker:\n<tool_call>\nContinue thinking." ,"Empty <tool_call><function=> is prose." ): 101for step in (1 ,7 ,4096 ): 102parser = OutputParser (thinking = True ,tools = TOOLS ,stream_tools = True ) 103text = prefix + "</think>\n\nVisible answer." 104events = [] 105for i in range (0 ,len (text ),step ):events += parser .feed (text [i :i + step ]) 106events += parser .finish () 107self .assertEqual ('' .join (e .text for e in events if e .kind == 'reasoning' ),prefix ) 108self .assertEqual ('' .join (e .text for e in events if e .kind == 'content' ),'Visible answer.' ) 109self .assertFalse (any (e .kind .startswith ('tool' )for e in events )) 110 111 112def test_literal_call_markers_in_content_before_genuine_call (self ): 113prefix = "Use `<tool_call>` in docs. Empty <tool_call><function=> stays text." 114for streaming in (False ,True ): 115for step in (1 ,7 ,4096 ): 116parser = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = streaming ) 117text = prefix + "\n" + CALL 118events = [] 119for i in range (0 ,len (text ),step ): 120events += parser .feed (text [i :i + step ]) 121events += parser .finish () 122self .assertEqual ('' .join (e .text for e in events if e .kind == 'content' ),prefix ) 123calls = [e .call for e in events if e .kind == 'tool_call' ] 124self .assertEqual (len (calls ),1 ) 125self .assertEqual (calls [0 ].arguments , {'path' :'123' }) 126 127 128def test_namespace_alias_schema_and_exact_name_precedence (self ): 129for streaming in (False ,True ): 130parser = OutputParser (thinking = False ,tools = TOOLS ,stream_tools = streaming ) 131events = parser .feed (CALL )+ parser .finish () 132call = next (e .call for e in events if e .kind == "tool_call" ) 133self .assertEqual ((call .name ,call .arguments ), ("mcp__files.read" , {"path" :"123" })) 134exact = {"name" :"mcp__files__read" ,"parameters" : {"properties" : {"path" : {"type" :"integer" }}}} 135parser = OutputParser (thinking = False ,tools = [* TOOLS ,exact ],stream_tools = streaming ) 136call = next (e .call for e in parser .feed (CALL )+ parser .finish ()if e .kind == "tool_call" ) 137self .assertEqual ((call .name ,call .arguments ), ("mcp__files__read" , {"path" :123 })) 138 139ambiguous = [{"name" :"a.b__c" }, {"name" :"a__b.c" }] 140parser = OutputParser (thinking = False ,tools = ambiguous ) 141text = CALL .replace ("mcp__files__read" ,"a__b__c" ) 142events = parser .feed (text )+ parser .finish () 143# an ambiguous alias is no declared tool: it does not route to the first namespace; it stays text 144self .assertFalse (any (e .kind == "tool_call" for e in events )) 145self .assertEqual ("" .join (e .text for e in events if e .kind == "content" ),text ) 146self .assertEqual (parser .unread ,1 )# and it is counted, so the server logs it 147 148 149if __name__ == "__main__" : 150unittest .main ()