2a95791992
run_agent.py: delete the `# noqa: F401` re-export block (agent.process_bootstrap
OpenAI/_SafeWriter/_get_proxy_*, model_tools get_tool_definitions/
handle_function_call/check_toolset_requirements, FailoverReason,
_qwen_portal_headers/_routermint_headers, session_persistence names,
estimate_request_tokens_rough, ContextCompressor + friends, jittered_backoff,
prompt_builder names, message_sanitization names, tool_dispatch_helpers
names) — 41 names run_agent never used itself — and the `_STREAM_DIAG_HEADERS`
back-compat class alias (no in-tree reader). run_agent now imports only what
it uses (get_toolset_for_tool, is_local_endpoint, coalesce/uniquify tool-call
ids, cleanup_vm/get_active_env from terminal_tool_lifecycle).
agent/*: `_ra().X` late-binds that only reached a re-export now import the
defining module directly (agent_runtime_helpers -> process_bootstrap.OpenAI,
model_tools.handle_function_call, session_persistence._safe_session_filename_component;
agent_init -> model_tools.get_tool_definitions/check_toolset_requirements,
_lazy_headers("agent.client_lifecycle", ...) for qwen/routermint;
system_prompt -> agent.prompt_builder / model_tools directly, dropping its
own _ra() shim and the `_r` parameter threading). `_ra()` stays for
run_agent-resident names (logger, AIAgent, _hermes_home, _set_interrupt, ...).
toolsets.py: remove resolve_multiple_toolsets (shim-only, restored by
34abf954bd); tests/test_toolsets.py pins the same union behavior via
resolve_toolset over each name.
providers/__init__.py: drop the OMIT_TEMPERATURE re-export (no callers via the
package); ProviderProfile stays because __init__ uses it for annotations —
2 tests repointed to providers.base.
agent/iteration_budget.py: drop the "run_agent re-exports the class"
docstring pointer; 4 tests import IterationBudget from its home.
model_tools.py (arg_coercion names), agent/tool_executor.py, and
hermes_cli/cli_session_mixin.py repoints landed via a sibling commit on this
shared worktree.
Callers repointed: gateway/run.py, hermes_cli/cli_chat_turn_mixin.py,
hermes_cli/cli_tui_mixin.py, tui_gateway/session_workdir.py,
agent/transports/codex.py (one-line imports) + comment pointers in
tools/file_state.py, tools/schema_sanitizer.py, scripts/tool_search_livetest.py.
Tests: patch("run_agent.X") / monkeypatch.setattr(run_agent, "X") /
`from run_agent import X` -> defining module across 99 test files.
249 lines
7.2 KiB
Python
249 lines
7.2 KiB
Python
"""Tests for tool argument type coercion.
|
|
|
|
When LLMs return tool call arguments, they frequently put numbers as strings
|
|
("42" instead of 42) and booleans as strings ("true" instead of true).
|
|
coerce_tool_args() fixes these type mismatches by comparing argument values
|
|
against the tool's JSON Schema before dispatch.
|
|
"""
|
|
|
|
from unittest.mock import patch
|
|
|
|
from tools.arg_coercion import (
|
|
coerce_tool_args,
|
|
_coerce_value,
|
|
_coerce_number,
|
|
_coerce_boolean,
|
|
_schema_accepts_kind,
|
|
_normalize_json_strings_for_schema,
|
|
)
|
|
|
|
|
|
# ── Low-level coercion helpers ────────────────────────────────────────────
|
|
|
|
|
|
class TestCoerceNumber:
|
|
"""Unit tests for _coerce_number."""
|
|
|
|
def test_integer_string(self):
|
|
assert _coerce_number("42") == 42
|
|
assert isinstance(_coerce_number("42"), int)
|
|
|
|
def test_negative_integer(self):
|
|
assert _coerce_number("-7") == -7
|
|
|
|
|
|
|
|
|
|
def test_integer_only_rejects_float(self):
|
|
"""When integer_only=True, "3.14" should stay as string."""
|
|
result = _coerce_number("3.14", integer_only=True)
|
|
assert result == "3.14"
|
|
assert isinstance(result, str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCoerceBoolean:
|
|
"""Unit tests for _coerce_boolean."""
|
|
|
|
def test_true_lowercase(self):
|
|
assert _coerce_boolean("true") is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_one_zero_not_coerced(self):
|
|
"""'1' and '0' are not boolean values."""
|
|
assert _coerce_boolean("1") == "1"
|
|
assert _coerce_boolean("0") == "0"
|
|
|
|
|
|
|
|
class TestCoerceValue:
|
|
"""Unit tests for _coerce_value."""
|
|
|
|
def test_integer_type(self):
|
|
assert _coerce_value("5", "integer") == 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_array_type_parsed_from_json_string(self):
|
|
"""Stringified JSON arrays are parsed into native lists."""
|
|
assert _coerce_value('["a", "b"]', "array") == ["a", "b"]
|
|
assert _coerce_value("[1, 2, 3]", "array") == [1, 2, 3]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── Full coerce_tool_args with registry ───────────────────────────────────
|
|
|
|
|
|
class TestCoerceToolArgs:
|
|
"""Integration tests for coerce_tool_args using the tool registry."""
|
|
|
|
def _mock_schema(self, properties):
|
|
"""Build a minimal tool schema with the given properties."""
|
|
return {
|
|
"name": "test_tool",
|
|
"description": "test",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": properties,
|
|
},
|
|
}
|
|
|
|
def test_coerces_integer_arg(self):
|
|
schema = self._mock_schema({"limit": {"type": "integer"}})
|
|
with patch("tools.arg_coercion.registry.get_schema", return_value=schema):
|
|
args = {"limit": "10"}
|
|
result = coerce_tool_args("test_tool", args)
|
|
assert result["limit"] == 10
|
|
assert isinstance(result["limit"], int)
|
|
|
|
|
|
|
|
|
|
def test_leaves_already_correct_types(self):
|
|
schema = self._mock_schema({"limit": {"type": "integer"}})
|
|
with patch("tools.arg_coercion.registry.get_schema", return_value=schema):
|
|
args = {"limit": 10}
|
|
result = coerce_tool_args("test_tool", args)
|
|
assert result["limit"] == 10
|
|
|
|
|
|
def test_empty_args(self):
|
|
assert coerce_tool_args("test_tool", {}) == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_real_read_file_schema(self):
|
|
"""Test against the actual read_file schema from the registry."""
|
|
# This uses the real registry — read_file should be registered
|
|
args = {"path": "foo.py", "offset": "10", "limit": "100"}
|
|
result = coerce_tool_args("read_file", args)
|
|
assert result["path"] == "foo.py"
|
|
assert result["offset"] == 10
|
|
assert isinstance(result["offset"], int)
|
|
assert result["limit"] == 100
|
|
assert isinstance(result["limit"], int)
|
|
|
|
|
|
# ── Schema-guided nested JSON-string normalization (cline/cline#11803) ─────
|
|
|
|
|
|
class TestSchemaAcceptsKind:
|
|
"""Unit tests for _schema_accepts_kind."""
|
|
|
|
def test_plain_type(self):
|
|
assert _schema_accepts_kind({"type": "array"}, "array") is True
|
|
assert _schema_accepts_kind({"type": "object"}, "object") is True
|
|
assert _schema_accepts_kind({"type": "string"}, "array") is False
|
|
|
|
|
|
|
|
def test_non_dict(self):
|
|
assert _schema_accepts_kind(None, "array") is False
|
|
|
|
|
|
class TestNormalizeJsonStringsForSchema:
|
|
"""Unit tests for _normalize_json_strings_for_schema (the recursive pass)."""
|
|
|
|
def test_parses_json_string_array_when_schema_expects_array(self):
|
|
schema = {"type": "array", "items": {"type": "string"}}
|
|
out = _normalize_json_strings_for_schema('["git status", "bun test"]', schema)
|
|
assert out == ["git status", "bun test"]
|
|
|
|
|
|
|
|
|
|
def test_native_list_preserved_identity(self):
|
|
schema = {"type": "array", "items": {"type": "object", "properties": {}}}
|
|
value = [{"id": "1"}]
|
|
# Nothing to change — same object back (no-op identity preserved).
|
|
assert _normalize_json_strings_for_schema(value, schema) is value
|
|
|
|
def test_non_dict_schema_returns_value(self):
|
|
assert _normalize_json_strings_for_schema("x", None) == "x"
|
|
|
|
|
|
class TestCoerceToolArgsNested:
|
|
"""Integration: nested JSON-string elements/fields are normalized via the
|
|
registry schema, while legitimate string fields are preserved."""
|
|
|
|
def _array_of_objects_schema(self):
|
|
return {
|
|
"name": "test_tool",
|
|
"description": "test",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"items": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "object",
|
|
"properties": {
|
|
"id": {"type": "string"},
|
|
"content": {"type": "string"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
def test_array_elements_as_json_strings_are_parsed(self):
|
|
schema = self._array_of_objects_schema()
|
|
with patch("tools.arg_coercion.registry.get_schema", return_value=schema):
|
|
args = {"items": ['{"id": "1", "content": "x"}']}
|
|
result = coerce_tool_args("test_tool", args)
|
|
assert result["items"] == [{"id": "1", "content": "x"}]
|
|
|
|
|
|
def test_string_subfield_with_json_content_preserved(self):
|
|
"""A string-typed sub-field whose value looks like JSON must NOT be parsed."""
|
|
schema = self._array_of_objects_schema()
|
|
with patch("tools.arg_coercion.registry.get_schema", return_value=schema):
|
|
args = {"items": [{"id": "1", "content": '{"not": "parsed"}'}]}
|
|
result = coerce_tool_args("test_tool", args)
|
|
assert result["items"][0]["content"] == '{"not": "parsed"}'
|
|
|
|
|
|
|
|
def test_real_todo_schema_element_strings(self):
|
|
"""Against the real todo schema from the registry."""
|
|
import json as _json
|
|
args = {"todos": [_json.dumps({"id": "1", "content": "x", "status": "pending"})]}
|
|
result = coerce_tool_args("todo_list", args)
|
|
assert result["todos"][0] == {"id": "1", "content": "x", "status": "pending"}
|