# retoor from devplacepy.services.devii.llm import _sanitize_tool_calls, _sanitize_tool_name def test_sanitize_tool_name_leaves_clean_names_untouched(): assert _sanitize_tool_name("plan") == "plan" assert _sanitize_tool_name("project_write_file") == "project_write_file" def test_sanitize_tool_name_strips_a_harmony_channel_leak(): assert _sanitize_tool_name("plan<|channel|>commentary") == "plan" def test_sanitize_tool_name_strips_any_special_token_style_suffix(): assert _sanitize_tool_name("verify<|message|>") == "verify" assert _sanitize_tool_name("recall<|end|>") == "recall" def test_sanitize_tool_name_falls_back_to_original_if_nothing_survives(): assert _sanitize_tool_name("<|channel|>commentary") == "<|channel|>commentary" def test_sanitize_tool_calls_mutates_leaked_names_in_place(): message = { "role": "assistant", "tool_calls": [ {"id": "1", "function": {"name": "plan<|channel|>commentary", "arguments": "{}"}}, {"id": "2", "function": {"name": "verify", "arguments": "{}"}}, ], } _sanitize_tool_calls(message) assert message["tool_calls"][0]["function"]["name"] == "plan" assert message["tool_calls"][1]["function"]["name"] == "verify" def test_sanitize_tool_calls_handles_no_tool_calls(): message = {"role": "assistant", "content": "hi"} _sanitize_tool_calls(message) assert message == {"role": "assistant", "content": "hi"} def test_sanitize_tool_calls_ignores_malformed_function_entries(): message = {"tool_calls": [{"id": "1", "function": "not-a-dict"}]} _sanitize_tool_calls(message) assert message["tool_calls"][0]["function"] == "not-a-dict"