Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(model-hub): let a timeout, limit or server status outrank a request-class code (#2757)

Call White75fce964b6bc
diff --git a/core/handlers/model_hub/classification.py b/core/handlers/model_hub/classification.py
index 24a927db7..68e560450 100644
--- a/core/handlers/model_hub/classification.py
+++ b/core/handlers/model_hub/classification.py
@@ -234,16 +234,32 @@ def machine_codes_name_model(outcome: RawCallOutcome) -> bool:
return bool(_MODEL_SURFACE_PATTERNS.search(_error_text(outcome)))
+def _status_clears_request(http_status: int | None) -> bool:
+ """Whether the status itself says the call failed for a reason other than the request.
+
+ A timeout (408), a rate or quota limit (429) and a server or gateway failure
+ (5xx) are the server's own account of why it did not serve the call. A
+ request-family code beside one contradicts it rather than refining it:
+ relays wrap their own timeouts and stalls in ``invalid_request_error``
+ (#2260), so that code cannot show that no Source could serve the request.
+ """
+
+ return http_status in (408, 429) or (http_status is not None and 500 <= http_status <= 599)
+
+
def _classify_unstreamed(
outcome: RawCallOutcome,
*,
refresh_attempted: bool = False,
) -> ResolutionDecision:
- # Signed machine-code rows precede every transport and status heuristic.
+ clears_request = _status_clears_request(outcome.http_status)
+ # Signed machine-code rows precede every transport and status heuristic,
+ # except that a status clearing the request outranks the request family.
machine_rows = (
(_MACHINE_ERROR_TAXONOMY[code], code)
for code in machine_error_codes(outcome)
if code in _MACHINE_ERROR_TAXONOMY
+ and not (clears_request and _MACHINE_ERROR_TAXONOMY[code].family == "request")
)
for machine_row, _machine_code in machine_rows:
if machine_row.action is not None:
@@ -279,7 +295,7 @@ def _classify_unstreamed(
"fallback",
reason="balance_exhausted",
)
- if _SURFACE_PATTERNS.search(error_text) or names_model_refusal(outcome):
+ if not clears_request and (_SURFACE_PATTERNS.search(error_text) or names_model_refusal(outcome)):
return ResolutionDecision("surface", error_code="upstream_request_invalid")
if _QUOTA_PATTERNS.search(error_text):
@@ -299,7 +315,7 @@ def _classify_unstreamed(
reason="rate_limited",
cooldown_seconds=60,
)
- if outcome.http_status is not None and 500 <= outcome.http_status < 600:
+ if outcome.http_status == 408 or (outcome.http_status is not None and 500 <= outcome.http_status < 600):
return ResolutionDecision(
"fallback",
reason="server_error",
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index 276732dd3..49f10013e 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -976,6 +976,16 @@ scenarios:
kind: compatibility
layer: e2e
test: tests/e2e/test_model_hub_routing.py::test_mh_routing_013_legacy_empty_inherits_matching_and_exact_passthrough_through_real_cpa
+ - id: MH-ROUTING-014
+ name: >-
+ A request-family machine code beside a timeout, rate-limit or server status (408, 429, 5xx) does not make the
+ call non-fallback; the turn moves to the next Source, which is cooled and recorded, while a 4xx keeps the code
+ status: covered
+ kind: recovery
+ layer: contract
+ test:
+ - tests/test_model_hub_resolution.py::test_a_stalled_relay_wrapped_in_a_request_class_code_falls_back_and_cools_its_source
+ - tests/test_model_hub_resolution.py::test_a_status_that_clears_the_request_outranks_a_request_family_code
- id: MH-REQUEST-BUDGET-001
name: Large multi-image requests retain exact model input through authenticated HTTP ingress
status: covered
diff --git a/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py b/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py
index db29f7b33..a8b866927 100644
--- a/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py
+++ b/tests/scenarios/model_hub/test_model_hub_live_resolution_scenarios.py
@@ -974,7 +974,8 @@ def test_mh_effort_001_unknown_capability_preserves_intent_and_turn_completes(
("upstream_status", "upstream_code", "wire_status", "wire_code", "terminal_reason"),
[
(429, "rate_limited", 429, "stream_interrupted", "stream_interrupted"),
- (503, "request_too_large", 400, "upstream_request_invalid", "invalid_parameter"),
+ # A request-family code is believed only beside a status that blames the request (#2260).
+ (413, "request_too_large", 400, "upstream_request_invalid", "invalid_parameter"),
],
)
def test_turn_gateway_nonstream_buffer_surfaces_terminal_failure(
diff --git a/tests/test_model_hub_resolution.py b/tests/test_model_hub_resolution.py
index fe227f511..468b361a3 100644
--- a/tests/test_model_hub_resolution.py
+++ b/tests/test_model_hub_resolution.py
@@ -649,6 +649,107 @@ def test_terminal_outcome_category_authority_has_a_behavior_consumer(
) == expected_category
+_REQUEST_FAMILY_CODES = (
+ "invalid_request_error", "invalid_parameter", "validation_error",
+ "context_length_exceeded", "request_too_large", "model_not_found", "not_found_error",
+)
+
+
[email protected]("code", _REQUEST_FAMILY_CODES)
+ ("status", "action", "reason", "cooldown_seconds"),
+ [
+ # The status says the request is at fault: the code is believed.
+ (400, "surface", None, 0),
+ (413, "surface", None, 0),
+ (422, "surface", None, 0),
+ # The status says something other than the request failed the call.
+ (408, "fallback", "server_error", 30),
+ (429, "fallback", "rate_limited", 60),
+ (500, "fallback", "server_error", 30),
+ (502, "fallback", "server_error", 30),
+ (503, "fallback", "server_error", 30),
+ (504, "fallback", "server_error", 30),
+ ],
+)
+def test_a_status_that_clears_the_request_outranks_a_request_family_code(
+ code, status, action, reason, cooldown_seconds,
+) -> None:
+ """Scenario: MH-ROUTING-014 -- relays wrap their own timeouts and stalls in a request-class code (#2260).
+
+ Such a code beside a timeout, limit or server status cannot show that no
+ Source could serve the request, so the call falls back and its Source cools.
+ """
+
+ decision = classify_outcome(
+ RawCallOutcome(
+ kind=RawOutcomeKind.HTTP_ERROR,
+ http_status=status,
+ error_code=None,
+ redacted_message=None,
+ stream_started=False,
+ model_id="upstream-first",
+ source_id="src_route040",
+ error_type=code,
+ )
+ )
+
+ assert (decision.action, decision.reason, decision.cooldown_seconds) == (action, reason, cooldown_seconds)
+ if action == "surface":
+ assert decision.error_code == "upstream_request_invalid"
+
+
+def test_a_stalled_relay_wrapped_in_a_request_class_code_falls_back_and_cools_its_source(tmp_path):
+ """Scenario: MH-ROUTING-014 -- the reported shape: a relay's idle timeout typed ``invalid_request_error`` on a 504 (#2260)."""
+
+ first = _source("src_route041", ("upstream-first",))
+ second = _source("src_route042", ("upstream-second",))
+ config = _config([first, second])
+ config.agents["claude"].routes["claude-opus-4-6"] = ModelHubRouteConfig(
+ hops=(
+ ModelHubRouteHopConfig(first.id, "upstream-first"),
+ ModelHubRouteHopConfig(second.id, "upstream-second"),
+ )
+ )
+ adapter = FakeAdapter()
+ adapter.outcomes.extend(
+ (
+ RawCallOutcome(
+ kind=RawOutcomeKind.HTTP_ERROR,
+ http_status=504,
+ error_code=None,
+ redacted_message=None,
+ stream_started=False,
+ model_id="upstream-first",
+ source_id=first.id,
+ error_type="invalid_request_error",
+ upstream_detail="upstream stream idle for 3m0s",
+ ),
+ RawCallOutcome(
+ kind=RawOutcomeKind.SUCCESS,
+ http_status=200,
+ error_code=None,
+ redacted_message=None,
+ stream_started=False,
+ model_id="upstream-second",
+ source_id=second.id,
+ ),
+ )
+ )
+ service, store, _ = _service(tmp_path, config, adapter)
+
+ resolved = asyncio.run(service.resolve(backend="claude", model_id="claude-opus-4-6", request={}))
+
+ assert resolved.source_id == second.id
+ assert adapter.invocations == [(first.id, "upstream-first"), (second.id, "upstream-second")]
+ cooled = store.load().sources[0].state
+ assert cooled.status == "cooldown" and cooled.retry_at is not None
+ assert sorted((event["kind"], event["from_source"], event["to_source"], event["reason"]) for event in service.events.list()) == [
+ ("cooldown", first.id, None, "server_error"),
+ ("switch", first.id, second.id, "server_error"),
+ ]
+
+
@pytest.mark.parametrize("status", [401, 402, 403, 429, 500])
def test_machine_permission_denial_precedes_every_status_heuristic(
tmp_path,