nanobot/tests/providers/test_provider_tool_arguments.py
chengyongru 0a396aa6e2
Improve tool call validation strictness (#4190)
* Improve tool call validation strictness

Reject near-miss tool names without executing suggested tools. Require object-shaped tool parameters while preserving only lossless JSON wire-shape normalization.

* Tighten tool call argument validation

* Simplify tool argument validation tests

* Improve tool name suggestions

* Simplify tool suggestion helpers

* Limit tool suggestions to canonical matches

* Allow repair only for tool history replay

* Clarify non-object tool argument errors

* Inline replay tool argument normalization

* Track only successful tool executions

* Reject JSON null tool arguments
2026-06-09 14:50:40 +08:00

31 lines
1.1 KiB
Python

"""Shared tool-argument parsing policy tests."""
from nanobot.providers.base import (
parse_tool_arguments,
tool_arguments_json_for_replay,
tool_arguments_object_for_replay,
)
def test_parse_tool_arguments_preserves_malformed_executable_arguments() -> None:
assert parse_tool_arguments('{path:"foo.txt"}') == '{path:"foo.txt"}'
def test_parse_tool_arguments_preserves_non_object_executable_arguments() -> None:
assert parse_tool_arguments('["foo.txt"]') == ["foo.txt"]
assert parse_tool_arguments("false") is False
assert parse_tool_arguments("null") == "null"
def test_tool_arguments_object_for_replay_repairs_object_like_history_arguments() -> None:
assert tool_arguments_object_for_replay('{path:"foo.txt"}') == {"path": "foo.txt"}
def test_tool_arguments_object_for_replay_keeps_history_object_shaped() -> None:
for arguments in ['["foo.txt"]', "false", "null", "0", ["foo.txt"], False, None, 0]:
assert tool_arguments_object_for_replay(arguments) == {}
def test_tool_arguments_json_for_replay_returns_object_string() -> None:
assert tool_arguments_json_for_replay('{path:"foo.txt"}') == '{"path": "foo.txt"}'