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): preserve relay stream failure details (#2409)

Call White1231e7c04e9a
diff --git a/core/handlers/model_hub/classification.py b/core/handlers/model_hub/classification.py
index c504c583a..f08726f28 100644
--- a/core/handlers/model_hub/classification.py
+++ b/core/handlers/model_hub/classification.py
@@ -131,6 +131,10 @@ _MACHINE_ERROR_TAXONOMY: Final[Mapping[str, _MachineErrorRule]] = {
"server_error": _MachineErrorRule("server", True, 70, "fallback", "server_error", None, 30, None),
"internal_error": _MachineErrorRule("server", True, 70, "fallback", "server_error", None, 30, None),
"api_error": _MachineErrorRule("server", True, 10, "fallback", "server_error", None, 30, None),
+ "service_unavailable_error": _MachineErrorRule(
+ "server", True, 70, "fallback", "server_error", None, 30, None
+ ),
+ "upstream_error": _MachineErrorRule("server", True, 70, "fallback", "server_error", None, 30, None),
}
MACHINE_ERROR_CODES: Final = frozenset(_MACHINE_ERROR_TAXONOMY)
UPSTREAM_MACHINE_ERROR_CODES: Final = frozenset(
diff --git a/core/handlers/model_hub/provenance.py b/core/handlers/model_hub/provenance.py
index b544e6aaf..beba20678 100644
--- a/core/handlers/model_hub/provenance.py
+++ b/core/handlers/model_hub/provenance.py
@@ -150,10 +150,13 @@ class TurnUpstreamFailure:
"""The last upstream refusal or connection failure behind a failed turn."""
source: str
- # None when no upstream answered: the text is the connection failure.
+ # None when the transport did not expose an upstream HTTP status. A
+ # protocol-level failure can still have answered through an HTTP 2xx
+ # envelope, so this is not by itself a connection verdict.
http_status: int | None
# Redacted and bounded by the engine client; display only.
detail: str
+ connection_error: bool = False
@dataclass(frozen=True)
@@ -164,6 +167,7 @@ class TurnOutcomeRenderingRule:
# Appended to the summary when the turn carries its last upstream failure:
# an upstream refusal, or a connection that no upstream answered.
upstream_failure_key: str | None = None
+ upstream_failure_without_status_key: str | None = None
connection_failure_key: str | None = None
@@ -183,6 +187,7 @@ TURN_OUTCOME_RENDERING_AUTHORITY: dict[str, TurnOutcomeRenderingRule] = {
("interrupted", "modelHub.launch.interrupted"),
),
upstream_failure_key="modelHub.launch.last_upstream_failure",
+ upstream_failure_without_status_key="modelHub.launch.last_upstream_failure_without_status",
connection_failure_key="modelHub.launch.last_connection_failure",
),
"turn.request_nonfallback": TurnOutcomeRenderingRule(
@@ -211,6 +216,7 @@ TURN_OUTCOME_RENDERING_AUTHORITY: dict[str, TurnOutcomeRenderingRule] = {
("transition_unpersisted", "modelHub.errors.stream_interrupted"),
),
upstream_failure_key="modelHub.launch.last_upstream_failure",
+ upstream_failure_without_status_key="modelHub.launch.last_upstream_failure_without_status",
connection_failure_key="modelHub.launch.last_connection_failure",
),
"turn.no_candidate.unconfigured": TurnOutcomeRenderingRule(
@@ -232,6 +238,7 @@ TURN_OUTCOME_RENDERING_AUTHORITY: dict[str, TurnOutcomeRenderingRule] = {
("interrupted", "modelHub.launch.interrupted"),
),
upstream_failure_key="modelHub.launch.last_upstream_failure",
+ upstream_failure_without_status_key="modelHub.launch.last_upstream_failure_without_status",
connection_failure_key="modelHub.launch.last_connection_failure",
),
"turn.canceled": TurnOutcomeRenderingRule(
@@ -594,8 +601,13 @@ def render_turn_outcome_copy(
failure = projection.last_upstream_failure
rule = _turn_outcome_rule(projection)
suffix_key = (
- rule.upstream_failure_key if failure is not None and failure.http_status is not None
- else rule.connection_failure_key
+ rule.connection_failure_key
+ if failure is not None and failure.connection_error
+ else (
+ rule.upstream_failure_key
+ if failure is not None and failure.http_status is not None
+ else rule.upstream_failure_without_status_key
+ )
)
if failure is not None and suffix_key is not None:
text = " ".join((
diff --git a/core/handlers/model_hub/service.py b/core/handlers/model_hub/service.py
index 7e24b095d..e32490436 100644
--- a/core/handlers/model_hub/service.py
+++ b/core/handlers/model_hub/service.py
@@ -7921,11 +7921,22 @@ class ModelHubService:
return None
if outcome.kind in {RawOutcomeKind.NETWORK_ERROR, RawOutcomeKind.TIMEOUT}:
# The engine's own connection diagnostic: no upstream answered.
- return TurnUpstreamFailure(source=source_label, http_status=None, detail=outcome.upstream_detail)
+ return TurnUpstreamFailure(
+ source=source_label,
+ http_status=None,
+ detail=outcome.upstream_detail,
+ connection_error=True,
+ )
status = outcome.http_status
- if type(status) is not int or not 100 <= status <= 599:
- return None
- return TurnUpstreamFailure(source=source_label, http_status=status, detail=outcome.upstream_detail)
+ return TurnUpstreamFailure(
+ source=source_label,
+ http_status=(
+ status
+ if type(status) is int and 300 <= status <= 599
+ else None
+ ),
+ detail=outcome.upstream_detail,
+ )
@staticmethod
def _produce_no_candidate_terminal_outcome(
diff --git a/core/handlers/model_hub/stream_wire.py b/core/handlers/model_hub/stream_wire.py
index de9e8f61e..fd0f61da0 100644
--- a/core/handlers/model_hub/stream_wire.py
+++ b/core/handlers/model_hub/stream_wire.py
@@ -607,6 +607,17 @@ PROTOCOL_STREAM_TAXONOMY: Final[Mapping[str, ProtocolStreamTaxonomy]] = {
"failed_terminal",
(("response", "error"),),
),
+ ProtocolTerminalEnvelope(
+ # Some OpenAI-compatible relays preserve the SSE event name but
+ # omit the duplicated `type` member from the JSON data. The
+ # event name and the response error envelope are still enough
+ # to identify this terminal failure.
+ "response.failed",
+ ("response", "error"),
+ None,
+ "failed_terminal",
+ (("response", "error"),),
+ ),
ProtocolTerminalEnvelope(
# https://platform.openai.com/docs/api-reference/responses-streaming/response/incomplete
"response.incomplete",
diff --git a/docs/plans/model-hub-contracts/turn-provenance.schema.json b/docs/plans/model-hub-contracts/turn-provenance.schema.json
index e3cc35673..da408be75 100644
--- a/docs/plans/model-hub-contracts/turn-provenance.schema.json
+++ b/docs/plans/model-hub-contracts/turn-provenance.schema.json
@@ -208,6 +208,8 @@
"server_error",
"internal_error",
"api_error",
+ "service_unavailable_error",
+ "upstream_error",
null
],
"description": "Optional observed machine code from classification.UPSTREAM_MACHINE_ERROR_CODES. If model_not_found was observed and classification returned upstream_request_invalid, retain that specific code over a co-occurring generic invalid_request_error type; otherwise use existing specificity order. This diagnostic selection never reranks or changes classification. Never derive a code from invalid_parameter; unknown codes, bodies, headers and credentials are not retained. Absent evidence is null; historical records may omit the field."
diff --git a/docs/plans/model-hub.md b/docs/plans/model-hub.md
index f1e71fc51..ed7a4ef4c 100644
--- a/docs/plans/model-hub.md
+++ b/docs/plans/model-hub.md
@@ -1326,12 +1326,15 @@ refusal that only says "server error" cannot be diagnosed, so persisted provenan
it per attempt (`failed_attempts[].upstream_detail`, `terminal_error.upstream_detail`),
each failed Hub attempt logs it once, and the `turn.exhausted`,
`turn.no_candidate.blocked`, and `turn.streamed_fallback` rows append the last
-fallback-class failure of the pending request to their summary copy. An upstream
-refusal renders `modelHub.launch.last_upstream_failure` (Source, HTTP status, text).
-The engine's own transport envelope (`network_failure.transport_before_first_byte`)
-carries no upstream status and renders `modelHub.launch.last_connection_failure`
-(Source, text), and Failure details labels that text as a connection error, never as
-what the upstream said. The text is the same projection in every place:
+fallback-class failure of the pending request to their summary copy. A refusal with a
+valid upstream HTTP status (300–599) renders
+`modelHub.launch.last_upstream_failure` (Source, HTTP status, text). A protocol-level
+refusal carried inside an HTTP 2xx SSE envelope renders
+`modelHub.launch.last_upstream_failure_without_status` (Source, text); the envelope's
+HTTP 2xx status is not presented as the upstream status. The engine's own transport
+envelope (`network_failure.transport_before_first_byte`) carries no upstream status and
+renders `modelHub.launch.last_connection_failure` (Source, text), and Failure details
+labels that text as a connection error, never as what the upstream said. The text is the same projection in every place:
credential-redacted, C0/C1 and Unicode Bidi_Control characters removed,
whitespace-collapsed, and bounded to 400 characters.
diff --git a/tests/test_model_hub_failure_presentation.py b/tests/test_model_hub_failure_presentation.py
index 4aa2991b0..df1e3869f 100644
--- a/tests/test_model_hub_failure_presentation.py
+++ b/tests/test_model_hub_failure_presentation.py
@@ -14,6 +14,9 @@ from core.controller import Controller
from core.handlers.model_hub.adapter import RawOutcomeKind
from core.handlers.model_hub.provenance import (
ENGINE_DOWN_TURN_OUTCOME,
+ TurnOutcomeProjectionInput,
+ TurnSupplyFacts,
+ TurnUpstreamFailure,
render_turn_outcome_copy,
)
from core.handlers.model_hub.turn_gateway import ModelHubTurnGateway
@@ -299,6 +302,71 @@ async def test_upstream_refusal_text_reaches_reply_record_and_log(
await gateway.close()
[email protected]("language", ["en", "zh"])
+def test_request_incompatible_copy_preserves_nonfallback_guidance(language):
+ detail = "response protection is unavailable"
+ projection = TurnOutcomeProjectionInput(
+ outcome="failed_terminal",
+ discriminator="request_nonfallback",
+ upstream_detail=detail,
+ )
+
+ text = render_turn_outcome_copy(projection, language)
+
+ assert text == t(
+ "modelHub.launch.request_incompatible_detail",
+ language,
+ detail=detail,
+ )
+ assert (
+ "switching Sources will not help" in text
+ if language == "en"
+ else "切换模型供应商也无法解决" in text
+ )
+
+
[email protected]("language", ["en", "zh"])
+def test_http_200_sse_failure_is_presented_as_upstream_error(language):
+ detail = "response protection is unavailable"
+ projection = TurnOutcomeProjectionInput(
+ outcome="exhausted",
+ discriminator="final_supply_state",
+ supply_facts=TurnSupplyFacts(
+ backend="codex",
+ model="gpt-6-astra",
+ supply_state="waiting",
+ source="Relay 服务",
+ ),
+ last_upstream_failure=TurnUpstreamFailure(
+ source="Relay 服务",
+ http_status=None,
+ detail=detail,
+ ),
+ )
+
+ text = render_turn_outcome_copy(projection, language)
+
+ assert t(
+ "modelHub.launch.last_upstream_failure_without_status",
+ language,
+ source="Relay 服务",
+ detail=detail,
+ ) in text
+ assert t(
+ "modelHub.launch.last_connection_failure",
+ language,
+ source="Relay 服务",
+ detail=detail,
+ ) not in text
+ assert "HTTP 200" not in text
+ assert (
+ "switching Sources will not help" not in text
+ if language == "en"
+ else "切换模型供应商也无法解决" not in text
+ )
+ assert detail in text
+
+
@pytest.mark.parametrize("language", ["en", "zh"])
def test_terminal_copy_never_promises_automatic_recovery(language):
"""MH-RETRY-COPY-001: final failures never promise that a timer will recover a Source."""
diff --git a/tests/test_model_hub_l3.py b/tests/test_model_hub_l3.py
index cd00f8e07..d07f82bc4 100644
--- a/tests/test_model_hub_l3.py
+++ b/tests/test_model_hub_l3.py
@@ -196,6 +196,7 @@ def test_turn_outcome_rendering_authority_covers_matrix_and_locales() -> None:
for key in (
*(key for _variant, key in rule.copy_keys),
rule.upstream_failure_key,
+ rule.upstream_failure_without_status_key,
rule.connection_failure_key,
)
if key is not None
diff --git a/tests/test_model_hub_runtime.py b/tests/test_model_hub_runtime.py
index a84fc001d..b9952cacf 100644
--- a/tests/test_model_hub_runtime.py
+++ b/tests/test_model_hub_runtime.py
@@ -6330,6 +6330,45 @@ def test_documented_incomplete_output_is_served_without_source_failure(
assert classify_outcome(outcome).action == "return"
+def test_relay_response_failed_without_json_type_preserves_upstream_failure() -> None:
+ state = client_module.ProtocolSSEState("openai_responses")
+ state.observe(
+ b'event: response.failed\ndata: '
+ b'{"response":{"error":{"code":"upstream_error",'
+ b'"message":"response protection is unavailable"}}}\n\n'
+ )
+
+ observation = state.terminal_observation()
+ assert observation is not None
+ assert observation.outcome == "failed_terminal"
+ assert observation.error_message == "response protection is unavailable"
+
+ source = SourceRecord(
+ source_id="src_fixture123",
+ vendor="custom",
+ protocol="openai_responses",
+ base_url="https://api.example.test/v1",
+ credential_ref="cred_fixture123",
+ allowed_origins=(),
+ model_ids=("model-a",),
+ prefix="source-fixture123",
+ )
+ outcome = client_module._observed_stream_terminal_outcome(
+ state,
+ source,
+ "model-a",
+ 200,
+ )
+ assert outcome is not None
+ assert outcome.kind is RawOutcomeKind.HTTP_ERROR
+ assert outcome.http_status == 200
+ assert outcome.upstream_detail == "response protection is unavailable"
+
+ decision = classify_outcome(outcome)
+ assert decision.action == "fallback"
+ assert decision.reason == "server_error"
+
+
@pytest.mark.parametrize(
"finish_reason",
("stop", "length", "content_filter", "tool_calls", "function_call"),
diff --git a/tests/test_model_hub_structure_guards.py b/tests/test_model_hub_structure_guards.py
index 65584aa4c..8126e5734 100644
--- a/tests/test_model_hub_structure_guards.py
+++ b/tests/test_model_hub_structure_guards.py
@@ -132,6 +132,16 @@ STREAM_ENVELOPE_FIXTURES = (
"payload": b'{"type":"response.failed","response":{"error":{"code":"permission_error"}}}',
"source": "https://platform.openai.com/docs/api-reference/responses-streaming/response/failed",
},
+ {
+ "protocol": "openai_responses",
+ "terminal_fact": "failed_terminal",
+ "event_name": "response.failed",
+ "selector_path": ("response", "error"),
+ "selector_value": None,
+ "error_paths": (("response", "error"),),
+ "payload": b'{"response":{"error":{"code":"upstream_error"}}}',
+ "source": "https://platform.openai.com/docs/api-reference/responses-streaming/response/failed",
+ },
{
"protocol": "openai_responses",
"terminal_fact": "failed_terminal",
@@ -255,7 +265,13 @@ MACHINE_ERROR_FAMILY_FIXTURES = {
"model_not_found",
"not_found_error",
),
- "server": ("server_error", "internal_error", "api_error"),
+ "server": (
+ "server_error",
+ "internal_error",
+ "api_error",
+ "service_unavailable_error",
+ "upstream_error",
+ ),
"transient": (
"rate_limit_error",
"rate_limit_exceeded",
diff --git a/vibe/i18n/en.json b/vibe/i18n/en.json
index 0ddf43a9a..dd1cdd810 100644
--- a/vibe/i18n/en.json
+++ b/vibe/i18n/en.json
@@ -997,6 +997,7 @@
"interrupted": "Model {model} is unavailable: {blockers}. Open Models to resolve it.",
"route_unconfigured": "Model {model} has no configured route. Open Models to configure one.",
"last_upstream_failure": "Last upstream error ({source}, HTTP {status}): {detail}",
+ "last_upstream_failure_without_status": "Last upstream error ({source}): {detail}",
"last_connection_failure": "Last connection error ({source}): {detail}",
"model_unlisted": "Model {model} is not in the {backend} model list. Choose another model to continue.",
"request_incompatible": "This request is incompatible; switching Sources will not help.",
diff --git a/vibe/i18n/zh.json b/vibe/i18n/zh.json
index 994a51f87..31d9d80a9 100644
--- a/vibe/i18n/zh.json
+++ b/vibe/i18n/zh.json
@@ -997,6 +997,7 @@
"interrupted": "模型 {model} 当前不可用:{blockers}。请前往「模型」页处理。",
"route_unconfigured": "模型 {model} 尚未配置路由。请前往「模型」页配置。",
"last_upstream_failure": "上游最后一次报错({source},HTTP {status}):{detail}",
+ "last_upstream_failure_without_status": "上游最后一次报错({source}):{detail}",
"last_connection_failure": "最后一次连接错误({source}):{detail}",
"model_unlisted": "模型 {model} 不在 {backend} 的模型列表中。请选择其他模型后继续。",
"request_incompatible": "此请求不兼容;切换模型供应商也无法解决。",