@@ -67,6 +67,19 @@ def _parse_frontend_registry_keys(path: Path, marker: str) -> frozenset[str]:
6767 return frozenset (keys )
6868
6969
70+ @pytest .fixture
71+ def adjacent_top_level_defs_source () -> str :
72+ """Module text with no blank line between two top-level ``def`` blocks."""
73+ return (
74+ 'def _render_tool_result(parsed):\n '
75+ ' rt = "bash"\n '
76+ ' if rt == "bash":\n '
77+ ' pass\n '
78+ "def _render_system(msg):\n "
79+ ' return "next"\n '
80+ )
81+
82+
7083def _parse_frontend_tool_use_renderers (path : Path ) -> frozenset [str ]:
7184 """Extract ``TOOL_USE_RENDERERS`` keys."""
7285 return _parse_frontend_registry_keys (path , "export const TOOL_USE_RENDERERS = {" )
@@ -80,7 +93,7 @@ def _parse_frontend_tool_result_renderers(path: Path) -> frozenset[str]:
8093def _parse_top_level_function_body (text : str , func_name : str ) -> str :
8194 """Slice a module-level ``def`` through the next top-level ``def`` or EOF."""
8295 match = re .search (
83- rf"^def { re .escape (func_name )} \(.*?(?=^\ndef |\Z)" ,
96+ rf"^def { re .escape (func_name )} \(.*?(?=^def |\Z)" ,
8497 text ,
8598 re .DOTALL | re .MULTILINE ,
8699 )
@@ -90,6 +103,29 @@ def _parse_top_level_function_body(text: str, func_name: str) -> str:
90103 return match .group (0 )
91104
92105
106+ def test_parse_top_level_function_body_stops_at_adjacent_def (
107+ adjacent_top_level_defs_source : str ,
108+ ) -> None :
109+ body = _parse_top_level_function_body (
110+ adjacent_top_level_defs_source ,
111+ "_render_tool_result" ,
112+ )
113+ handlers = frozenset (re .findall (r'(?:if|elif) rt == "([^"]+)"' , body ))
114+ assert handlers == frozenset ({"bash" })
115+ assert "_render_system" not in body
116+
117+
118+ def test_parse_top_level_function_body_stops_at_eof () -> None :
119+ source = (
120+ "def _render_tool_result(parsed):\n "
121+ ' if rt == "grep":\n '
122+ " pass\n "
123+ )
124+ body = _parse_top_level_function_body (source , "_render_tool_result" )
125+ handlers = frozenset (re .findall (r'(?:if|elif) rt == "([^"]+)"' , body ))
126+ assert handlers == frozenset ({"grep" })
127+
128+
93129def _parse_dispatch_builder_result_types (path : Path ) -> frozenset [str ]:
94130 """Extract ``result_type`` literals from tool-result dispatch builders."""
95131 text = path .read_text (encoding = "utf-8" )
0 commit comments