okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1"""#3441 (a call whose envelope drifted by a token) and #3075 (a call for a tool the request never declared).""" 2from __future__import annotations 3 4import sys 5import unittest 6from pathlib import Path 7 8sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 9import frontend # noqa: E402 10 11TOOLS = [{"name" :"functions.exec" ,"parameters" : {"properties" : {"input" : {"type" :"string" }}}}, 12 {"name" :"shell" ,"parameters" : {"properties" : {"cmd" : {"type" :"string" }}}}, 13 {"name" :"ns.web_search" ,"parameters" : {"properties" : {"q" : {"type" :"string" }}}}] 14 15 16def body (opener ,param = "input" ,value = "1+1" ,name = None ): 17return f"<tool_call>\n { opener } \n<parameter= { param } >\n { value } \n</parameter>\n</function>\n</tool_call>" 18 19 20def run (text ,width = 0 ,** kw ): 21"""-> (events, parser); `width` 0 feeds the text whole, else that many characters at a time.""" 22p = frontend .OutputParser (thinking = False ,stream_tools = True ,** kw ) 23evs = [] 24if width : 25for k in range (0 ,len (text ),width ): 26evs += p .feed (text [k :k + width ]) 27else : 28evs += p .feed (text ) 29evs += p .finish () 30return evs ,p 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 == "content" ) 39 40 41class DriftedEnvelope (unittest .TestCase ): 42def test_a_dropped_function_keyword_is_still_the_call (self ): 43for opener in ("<=functions.exec>" ,"<function:functions.exec>" ,"<function =functions.exec>" , 44"<functions.exec>" ,"<= functions.exec >" ): 45for width in (0 ,1 ,2 ,3 ,7 ): 46with self .subTest (opener = opener ,width = width ): 47evs ,p = run (body (opener ),width ,tools = TOOLS ) 48self .assertEqual (calls (evs ), [("functions.exec" , {"input" :"1+1" })]) 49self .assertEqual (text (evs ),"" ) 50self .assertEqual (p .unread ,0 ) 51 52def test_the_template_form_is_unchanged (self ): 53evs ,p = run (body ("<function=functions.exec>" ),0 ,tools = TOOLS ) 54self .assertEqual (calls (evs ), [("functions.exec" , {"input" :"1+1" })]) 55 56def test_an_undeclared_name_in_a_drifted_opener_stays_text (self ): 57for width in (0 ,1 ,4 ): 58with self .subTest (width = width ): 59raw = body ("<=missing>" ) 60evs ,p = run (raw ,width ,tools = TOOLS ) 61self .assertEqual (calls (evs ), []) 62self .assertEqual (text (evs ),raw ) 63self .assertEqual (p .unread ,1 ) 64self .assertFalse ([e for e in evs if e .kind == "tool_start" ]) 65 66def test_prose_about_the_tag_is_not_a_drift (self ): 67for raw in ("Use a <tool_call> block for that." ,"<tool_call> then the name" ): 68evs ,p = run (raw ,3 ,tools = TOOLS ) 69self .assertEqual (calls (evs ), []) 70self .assertEqual (text (evs ),raw ) 71self .assertEqual (p .unread ,0 ) 72 73def test_streamed_and_whole_agree (self ): 74raw = "Let me run it.\n" + body ("<=shell>" ,"cmd" ,"ls" )+ "\nDone." 75whole ,_ = run (raw ,0 ,tools = TOOLS ) 76for width in (1 ,2 ,5 ,11 ): 77streamed ,_ = run (raw ,width ,tools = TOOLS ) 78self .assertEqual (calls (streamed ),calls (whole )) 79self .assertEqual (text (streamed ),text (whole )) 80self .assertEqual (calls (whole ), [("shell" , {"cmd" :"ls" })]) 81self .assertEqual (text (whole ),"Let me run it.Done." ) 82 83 84class OnlyDeclaredTools (unittest .TestCase ): 85def test_a_call_for_another_tool_stays_text (self ): 86raw = body ("<function=get_indicators>" ,"symbol" ,"600076" ) 87for width in (0 ,1 ,3 ,9 ): 88with self .subTest (width = width ): 89evs ,p = run ("I will check.\n" + raw ,width ,tools = TOOLS ) 90self .assertEqual (calls (evs ), []) 91self .assertFalse ([e for e in evs if e .kind in ("tool_start" ,"tool_args" )]) 92self .assertEqual (text (evs ),"I will check.\n" + raw ) 93 94def test_an_unparsed_call_is_counted_and_named_for_the_log (self ): 95"""T1 rail: a call that went back as text is no longer silent; the server prints its count and the names the model wrote.""" 96raw = body ("<function=get_indicators>" ,"symbol" ,"600076" ) 97evs ,p = run ("I will check." + chr (10 )+ raw ,4 ,tools = TOOLS ) 98self .assertEqual ((p .unread ,p .unread_names ), (1 , ["get_indicators" ])) 99evs ,p = run (body ("<function=shell>" ,"cmd" ,"ls" ),4 ,tools = TOOLS )# a declared call is no unread call 100self .assertEqual ((p .unread ,p .unread_names ), (0 , [])) 101 102def test_a_declared_call_after_an_undeclared_one_is_still_a_call (self ): 103raw = body ("<function=get_indicators>" )+ "\n" + body ("<function=shell>" ,"cmd" ,"ls" ) 104evs ,p = run (raw ,5 ,tools = TOOLS ) 105self .assertEqual (calls (evs ), [("shell" , {"cmd" :"ls" })]) 106self .assertIn ("get_indicators" ,text (evs )) 107 108def test_the_responses_spelling_of_a_namespace_tool_counts (self ): 109evs ,p = run (body ("<function=ns__web_search>" ,"q" ,"x" ),4 ,tools = TOOLS ) 110# PR #3556: the call carries the declared spelling (ns.web_search), the one the client offered, not the model's flat one 111self .assertEqual (calls (evs ), [("ns.web_search" , {"q" :"x" })]) 112 113def test_no_tools_declared (self ): 114raw = body ("<function=search>" ,"q" ,"2+2" ) 115evs ,p = run (raw ,3 ,tools = None ,declared_only = True ) 116self .assertEqual ((calls (evs ),text (evs )), ([],raw )) 117evs ,p = run (raw ,3 ,tools = [],declared_only = True ) 118self .assertEqual ((calls (evs ),text (evs )), ([],raw )) 119# without the flag (the Responses route: a Codex compaction request keeps its tools in the prompt only) 120evs ,p = run (raw ,3 ,tools = None ) 121self .assertEqual (calls (evs ), [("search" , {"q" :"2+2" })]) 122 123def test_the_reasoning_state_is_as_before (self ): 124p = frontend .OutputParser (thinking = True ,tools = TOOLS ,stream_tools = True ) 125evs = p .feed ("think " + body ("<function=shell>" ,"cmd" ,"ls" )+ "</think>\n\nok" )+ p .finish () 126self .assertEqual (calls (evs ), []) 127 128 129if __name__ == "__main__" : 130unittest .main ()