fix(vertex-ai): resolve a root $ref chain in linear time
Some checks are pending
Cost map pins recheck / rerun-cost-map-guard (push) Waiting to run
LiteLLM Rust DB / db-queries (push) Waiting to run
LiteLLM Rust / rust-lint (push) Waiting to run
LiteLLM Rust / rust-test (push) Waiting to run
LiteLLM Rust / rust-wheel (push) Waiting to run
Terraform Modules / fmt, validate, test (aws) (push) Waiting to run
Terraform Modules / fmt, validate, test (gcp) (push) Waiting to run
Terraform Provider / gofmt, vet, build, test (push) Waiting to run
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Waiting to run

Walk $defs by name once with cycle detection, so a long missing ref or a
long-named ref cycle beside many defs no longer costs quadratic CPU
This commit is contained in:
mateo-berri 2026-11-14 09:13:28 -05:00
parent 56bbef4093
commit f47bc67c40
2 changed files with 44 additions and 7 deletions

View file

@ -940,18 +940,24 @@ def _root_cannot_be_vertex_parameters(root: Mapping[str, object]) -> bool:
) or not _is_absent_or_object_type(root.get("type"))
def _root_ref_target(schema: Mapping[str, JsonValue], defs: Mapping[str, JsonValue]) -> Mapping[str, JsonValue]:
def _root_ref_def_name(schema: Mapping[str, JsonValue], def_names: Mapping[str, str]) -> str | None:
match schema.get("$ref"):
case str() as ref if ref.startswith(_ROOT_DEFS_REF_PREFIX):
target: Final = defs.get(ref.removeprefix(_ROOT_DEFS_REF_PREFIX))
return target if isinstance(target, dict) else schema
return def_names.get(ref.removeprefix(_ROOT_DEFS_REF_PREFIX))
case _:
return schema
return None
def _resolve_root_ref(schema: Mapping[str, JsonValue], defs: Mapping[str, JsonValue]) -> Mapping[str, JsonValue]:
resolved: Final = reduce(lambda current, _: _root_ref_target(current, defs), range(len(defs)), schema)
return resolved if _root_ref_target(resolved, defs) is resolved else schema
object_defs: Final = MappingProxyType({name: value for name, value in defs.items() if isinstance(value, dict)})
def_names: Final = MappingProxyType({name: name for name in object_defs})
next_names: Final = ((name, _root_ref_def_name(value, def_names)) for name, value in object_defs.items())
next_def: Final = MappingProxyType({name: target for name, target in next_names if target is not None})
first: Final = _root_ref_def_name(schema, def_names)
if first is None:
return schema
last: Final = reduce(lambda name, _: next_def.get(name, name), range(len(next_def)), first)
return schema if last in next_def else object_defs[last]
def needs_parameters_json_schema(parameters: Mapping[str, JsonValue]) -> bool:

View file

@ -1,7 +1,7 @@
import copy
import threading
import time
from collections.abc import Iterator, Mapping
from collections.abc import Callable, Iterator, Mapping
from contextlib import contextmanager
from enum import Enum
from typing import Final
@ -1726,6 +1726,37 @@ def test_needs_parameters_json_schema(parameters: Mapping[str, JsonValue], expec
assert needs_parameters_json_schema(parameters) is expected
def _missing_root_ref(size: int) -> Mapping[str, JsonValue]:
return {"$ref": "#/$defs/" + "m" * size, "$defs": {f"d{i}": {} for i in range(size // 16)}}
def _long_named_root_ref_cycle(size: int) -> Mapping[str, JsonValue]:
first, second = "a" * size, "b" * size
fillers: Final = {f"d{i}": {"type": "string"} for i in range(size // 16)}
cycle: Final = {first: {"$ref": f"#/$defs/{second}"}, second: {"$ref": f"#/$defs/{first}"}}
return {"$ref": f"#/$defs/{first}", "$defs": {**cycle, **fillers}}
def _root_ref_cost(parameters: Mapping[str, JsonValue]) -> float:
def once() -> float:
started: Final = time.perf_counter()
needs_parameters_json_schema(parameters)
return time.perf_counter() - started
return min(once() for _ in range(3))
@pytest.mark.parametrize("build", [_missing_root_ref, _long_named_root_ref_cycle], ids=["missing-ref", "ref-cycle"])
def test_needs_parameters_json_schema_stays_linear_on_long_refs_and_many_defs(
build: Callable[[int], Mapping[str, JsonValue]],
) -> None:
"""Tool parameters are caller-controlled, so 4x longer refs with 4x more $defs must not cost 16x.
Comparing two sizes keeps this honest on a slow box, and the quadratic walk this replaced grew 13x to 16x."""
growth: Final = _root_ref_cost(build(256 * 1024)) / _root_ref_cost(build(64 * 1024))
assert growth < 10.0, f"cost grew {growth:.1f}x for 4x the input"
class _Priority(int, Enum):
LOW = 1
HIGH = 3