fix(voice): attribute provider terminal failures safely

This commit is contained in:
amirhosein mazaheri
2026-10-13 14:59:48 +03:30
parent 1139b31219
commit 15558fd850
5 changed files with 95 additions and 3 deletions
+26
View File
@@ -118,6 +118,7 @@ NUMERIC_FIELDS = frozenset({
"generated_bytes", "queued_bytes", "reserved_bytes", "credit_bytes", "discarded_bytes",
"chunk_bytes", "lock_wait_ms", "backlog_bytes", "peak_backlog_bytes",
"received_elapsed_ms", "terminal_completed", "terminal_cancelled", "terminal_limited",
"terminal_failed", "terminal_incomplete", "terminal_tool_calls", "terminal_output_items",
"authority_ms", "emit_ms", "queued_controls",
"cancellation_type_match", "cancellation_code_match", "cancellation_param_match", "cancellation_profile_2_1_shimmer",
"transport_limit_bytes", "transport_limit_messages", "occupancy_count", "limit_count", "occupancy_bytes", "limit_bytes", "transport_bytes", "transport_messages",
@@ -162,3 +163,28 @@ def protocol_reason(exc):
def terminal_failed(code):
return code.startswith("LIVE_") or code in (
"SEARCH_TIMEOUT", "SEARCH_SATURATED", "DEPENDENCY_UNAVAILABLE", "SEARCH_CANCELLED")
def terminal_failure_reason(response):
"""Classify structured terminal details without retaining provider text."""
status = response.get("status")
details = response.get("status_details")
if isinstance(details, dict):
error = details.get("error")
if status == "failed" and isinstance(error, dict):
code = error.get("code")
if isinstance(code, str):
reason = {
"server_error": "PROVIDER_RESPONSE_SERVER_ERROR",
"rate_limit_exceeded": "PROVIDER_RESPONSE_RATE_LIMIT",
"insufficient_quota": "PROVIDER_RESPONSE_QUOTA",
}.get(code)
if reason:
return reason
if status == "incomplete" and details.get("reason") == "content_filter":
return "PROVIDER_RESPONSE_CONTENT_FILTER"
if status == "failed":
return "PROVIDER_RESPONSE_FAILED"
if status == "incomplete":
return "PROVIDER_RESPONSE_INCOMPLETE"
return "PROVIDER_RESPONSE_UNEXPECTED_STATUS"
+11 -1
View File
@@ -15,7 +15,7 @@ from app.modules.knowledge.langchain_tool import ToolContext, create_search_tool
from app.modules.knowledge.service import SearchInput, authority, transaction
from app.infrastructure.realtime import canonical_json, strict_json, ProviderProtocolError
from app.modules.voice.settings import RuntimeSettings
from app.modules.voice.diagnostics import diagnostic, protocol_reason, trace
from app.modules.voice.diagnostics import diagnostic, protocol_reason, terminal_failure_reason, trace
from app.modules.voice.serialization import bounded_json, json_size, bounded_json_digest
def identity():
@@ -721,6 +721,8 @@ class VoiceRuntime:
self.sync_failed("PROVIDER_TRANSPORT_LOST_DURING_SYNC",
phase="provider_receive", exc=exc, turn=self.cleanup_owner())
else:
diagnostic(self.session_id, "provider_receive", "PROVIDER_TRANSPORT_LOST",
exc=exc, code="LIVE_PROVIDER_UNAVAILABLE")
self.fail("LIVE_PROVIDER_UNAVAILABLE")
async def _sender(self):
@@ -1542,11 +1544,19 @@ class VoiceRuntime:
return
limited = bool(getattr(self.adapter, "token_limited", lambda _: False)(wire))
if wire["status"] not in ("completed", "cancelled", "waiting_tools") and not limited:
diagnostic(self.session_id, "provider_terminal", terminal_failure_reason(wire),
event_type=kind, code="LIVE_PROVIDER_UNAVAILABLE",
terminal_failed=int(wire["status"] == "failed"),
terminal_incomplete=int(wire["status"] == "incomplete"),
terminal_tool_calls=len(response.calls), terminal_output_items=len(wire["output"]))
self.fail("LIVE_PROVIDER_UNAVAILABLE", turn=response.turn)
return
# Unfinished tool context cannot safely be reused; fail before execution.
if limited and (response.calls or any(
item.get("type") != "message" for item in wire["output"])):
diagnostic(self.session_id, "provider_terminal", "TOKEN_LIMIT_WITH_UNFINISHED_TOOLS",
event_type=kind, code="LIVE_PROVIDER_UNAVAILABLE", terminal_limited=1,
terminal_tool_calls=len(response.calls), terminal_output_items=len(wire["output"]))
self.fail("LIVE_PROVIDER_UNAVAILABLE", turn=response.turn)
return
terminal_calls = set()