The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): bound recursive union ref traversal
Yuchen Wang8636ccc4c59e
diff --git a/tests/agent/test_tool_arg_coercion.py b/tests/agent/test_tool_arg_coercion.pyindex 8d43829438a..98ae10ccadb 100644--- a/tests/agent/test_tool_arg_coercion.py+++ b/tests/agent/test_tool_arg_coercion.py@@ -176,6 +176,17 @@ class TestSchemaAcceptsKind:def test_non_dict(self):assert _schema_accepts_kind(None, "array") is False+ def test_recursive_union_refs_keep_noncyclic_alternatives(self):+ for union_key in ("anyOf", "oneOf", "allOf"):+ root = {"$defs": {"Node": {+ union_key: [{"$ref": "#/$defs/Node"}, {"type": "object"}],+ }}}+ schema = {"type": "array", "items": {"$ref": "#/$defs/Node"}}+ assert _schema_accepts_kind(root["$defs"]["Node"], "array", root) is False+ assert _normalize_json_strings_for_schema(+ ['{"street": "Alpha Ave"}'], schema, root,+ ) == [{"street": "Alpha Ave"}]+class TestNormalizeJsonStringsForSchema:"""Unit tests for _normalize_json_strings_for_schema (the recursive pass)."""diff --git a/tools/arg_coercion.py b/tools/arg_coercion.pyindex de781c5dc06..3ff7a36f15c 100644--- a/tools/arg_coercion.py+++ b/tools/arg_coercion.py@@ -133,17 +133,22 @@ def _schema_accepts_kind(schema: Any,kind: str,root_schema: Any = None,+ seen_schema_ids: frozenset[int] = frozenset(),) -> bool:"""True when *schema* permits JSON type *kind* via ``type`` or any anyOf/oneOf/allOf branch."""if not isinstance(schema, dict):return False+ if id(schema) in seen_schema_ids:+ return False+ # Track this branch only: a recursive union must not hide its other alternatives.+ seen_schema_ids = seen_schema_ids | {id(schema)}if root_schema is None:root_schema = schemaschema = _resolve_local_schema_ref(schema, root_schema)t = schema.get("type")if t == kind or (isinstance(t, list) and kind in t):return True- return any(isinstance(branches := schema.get(union_key), list) and any(_schema_accepts_kind(b, kind, root_schema) for b in branches)+ return any(isinstance(branches := schema.get(union_key), list) and any(_schema_accepts_kind(b, kind, root_schema, seen_schema_ids) for b in branches)for union_key in ("anyOf", "oneOf", "allOf"))