Skip to content

Commit d6c9bbf

Browse files
author
Mateusz
committed
fix: guard composite leaf resolution
1 parent 20d66f8 commit d6c9bbf

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/core/services/backend_model_resolver.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from src.core.services.composite_routing_state import (
3838
COMPOSITE_LEAF_RESOLUTION_EXTRA_BODY_KEY,
3939
COMPOSITE_LEAF_RESOLUTION_FLAG,
40+
is_composite_selector,
4041
resolve_composite_routing_surface,
4142
)
4243
from src.core.services.replacement_compatibility_bridge import (
@@ -470,16 +471,20 @@ def _require_explicit_backend_for_surface(surface: RoutingSurface) -> bool:
470471
return surface is RoutingSurface.REPLACEMENT_BRIDGE
471472

472473
@staticmethod
473-
def _is_composite_leaf_resolution(
474-
*,
475-
context: RequestContext | None,
476-
request: ChatRequest,
477-
) -> bool:
478-
if context is None:
479-
extra_body = request.extra_body
480-
if isinstance(extra_body, dict):
481-
return bool(extra_body.get(COMPOSITE_LEAF_RESOLUTION_EXTRA_BODY_KEY))
482-
return False
474+
def _is_composite_leaf_resolution(
475+
*,
476+
context: RequestContext | None,
477+
request: ChatRequest,
478+
) -> bool:
479+
model = request.model
480+
if is_composite_selector(model):
481+
return False
482+
483+
if context is None:
484+
extra_body = request.extra_body
485+
if isinstance(extra_body, dict):
486+
return bool(extra_body.get(COMPOSITE_LEAF_RESOLUTION_EXTRA_BODY_KEY))
487+
return False
483488
if bool(context.extensions.get(COMPOSITE_LEAF_RESOLUTION_FLAG)):
484489
return True
485490
extra_body = request.extra_body

tests/unit/core/services/test_backend_model_resolver_composite_entrypoint.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,30 @@ async def test_composite_failover_across_numbered_backend_instances() -> None:
499499
assert result.uri_params == {"top_p": "0.9"}
500500

501501

502+
@pytest.mark.asyncio
503+
async def test_stale_leaf_resolution_flag_does_not_bypass_composite_failover() -> None:
504+
resolver = _build_resolver_with_real_composite(
505+
unavailable_backends={"openai-codex"}
506+
)
507+
request = ChatRequest(
508+
model=(
509+
"openai-codex:gpt-5.3-codex?reasoning_effort=low|"
510+
"anthropic:claude-3-5-sonnet?reasoning_effort=medium"
511+
),
512+
messages=[ChatMessage(role="user", content="hello")],
513+
extra_body={"_composite_leaf_resolution": True},
514+
)
515+
516+
result = await resolver.resolve_target(
517+
request,
518+
context=_context("main"),
519+
)
520+
521+
assert result.backend == "anthropic"
522+
assert result.model == "claude-3-5-sonnet"
523+
assert result.uri_params == {"reasoning_effort": "medium"}
524+
525+
502526
@pytest.mark.asyncio
503527
async def test_composite_weighted_numbered_instances_respects_deterministic_random() -> (
504528
None

0 commit comments

Comments
 (0)