fix(ui): surface model retry status (NAN-34) (#5504)

This commit is contained in:
chengyongru authored and GitHub committed 2026-09-07 15:12:39 +08:00
1 parent 0b1fa0c3e4
commit b581a496a6
52 files changed
+1697 -55

No files matched your search

+6
View File
@@ -155,6 +155,7 @@ class TurnContext:
final_content: str | None = None
all_messages: list[dict[str, Any]] = field(default_factory=list)
stop_reason: str = ""
failure_error_kind: str | None = None
streamed_content: bool = False
input_persisted_early: bool = False
@@ -2012,6 +2013,7 @@ class AgentLoop:
ctx.summary_checkpoint = result.summary_checkpoint
ctx.provider_compaction_applied = result.provider_compaction_applied
ctx.stop_reason = result.stop_reason
ctx.failure_error_kind = result.failure_error_kind
if (
ctx.kind is TurnKind.USER
and (ctx.delivery.route.channel, ctx.delivery.route.chat_id) in message_sends
@@ -2073,6 +2075,10 @@ class AgentLoop:
)
async def _prepare_outbound(self, ctx: TurnContext) -> None:
ctx.delivery.record_stop_reason(
ctx.stop_reason,
failure_error_kind=ctx.failure_error_kind,
)
if ctx.suppress_response:
ctx.outbound = None
return
+4
View File
@@ -130,6 +130,7 @@ class AgentRunResult:
round_usages: list[LLMUsage] = field(default_factory=list)
stop_reason: str = "completed"
error: str | None = None
failure_error_kind: str | None = None
tool_events: list[dict[str, str]] = field(default_factory=list)
had_injections: bool = False
# Terminal tail to emit when the preceding final-content prefix was already streamed.
@@ -396,6 +397,7 @@ class AgentRunner:
usage: LLMUsage | None = None
round_usages: list[LLMUsage] = []
error: str | None = None
failure_error_kind: str | None = None
stop_reason = "completed"
tool_events: list[dict[str, str]] = []
external_lookup_counts: dict[str, int] = {}
@@ -730,6 +732,7 @@ class AgentRunner:
had_injections = True
length_recovery_parts.clear()
continue
failure_error_kind = LLMProvider.public_error_kind(response)
break
if is_blank_text(clean):
final_content = EMPTY_FINAL_RESPONSE_MESSAGE
@@ -827,6 +830,7 @@ class AgentRunner:
round_usages=round_usages,
stop_reason=stop_reason,
error=error,
failure_error_kind=failure_error_kind,
tool_events=tool_events,
had_injections=had_injections,
pending_stream_content=pending_stream_content,
+39 -1
View File
@@ -12,6 +12,7 @@ from typing import TYPE_CHECKING, Any, cast
from nanobot.bus.events import InboundMessage, OutboundMessage
from nanobot.bus.notification_delivery import notification_is_deliverable
from nanobot.bus.outbound_events import (
RetryStatusEvent,
StreamDeltaEvent,
StreamedResponseEvent,
StreamEndEvent,
@@ -61,6 +62,14 @@ def _bind_events(
return EventSink(publish, accepts)
def _turn_outcome(stop_reason: object) -> tuple[str, str | None]:
if stop_reason == "error":
return "failed", "model"
if stop_reason == "tool_error":
return "failed", "tool"
return "completed", None
class TurnDeliveryFactory:
"""Route turn delivery and session-level notifications."""
@@ -182,6 +191,9 @@ class TurnDelivery:
_stream_open: bool = field(init=False, default=False)
events: EventSink = field(init=False)
_routed_events: EventSink = field(init=False)
_stop_reason: str | None = field(init=False, default=None)
_failure_error_kind: str | None = field(init=False, default=None)
_retry_status: RetryStatusEvent | None = field(init=False, default=None)
def __post_init__(self) -> None:
self._routed_events = _bind_events(self.bus, self.route)
@@ -244,6 +256,15 @@ class TurnDelivery:
def record_usage(self, round_usages: list[LLMUsage]) -> None:
self.runtime_event_publisher.record_turn_usage(self.session_key, round_usages)
def record_stop_reason(
self,
stop_reason: str,
*,
failure_error_kind: str | None = None,
) -> None:
self._stop_reason = stop_reason
self._failure_error_kind = failure_error_kind
def background_response(
self,
content: str | None,
@@ -276,12 +297,16 @@ class TurnDelivery:
*,
publish_completion: bool,
) -> None:
stop_reason = self._stop_reason
if stop_reason is None and response is not None:
stop_reason = cast(str | None, response.metadata.get("_stop_reason"))
completed_channel = self.lifecycle_message.channel
completed_chat_id = self.lifecycle_message.chat_id
if response is not None:
await self.bus.publish_outbound(response)
completed_channel = response.channel
completed_chat_id = response.chat_id
if response.channel != "websocket" or stop_reason != "error":
await self.bus.publish_outbound(response)
elif self.lifecycle_message.channel == "cli":
await self.bus.publish_outbound(
OutboundMessage(
@@ -292,11 +317,20 @@ class TurnDelivery:
)
)
if publish_completion:
outcome, failure_kind = _turn_outcome(stop_reason)
await self.runtime_event_publisher.turn_completed(
channel=completed_channel,
chat_id=completed_chat_id,
session_key=self.session_key,
metadata=self.lifecycle_message.metadata,
outcome=outcome,
failure_kind=failure_kind,
failure_error_kind=(self._failure_error_kind or (
self._retry_status.error_kind if failure_kind == "model" and self._retry_status else None
)),
failure_attempts=(
self._retry_status.attempt if failure_kind == "model" and self._retry_status else None
),
)
async def fail(self, *, publish_completion: bool) -> None:
@@ -314,6 +348,8 @@ class TurnDelivery:
chat_id=self.lifecycle_message.chat_id,
session_key=self.session_key,
metadata=self.lifecycle_message.metadata,
outcome="failed",
failure_kind="internal",
)
async def idle(self) -> None:
@@ -329,6 +365,8 @@ class TurnDelivery:
return f"{self._stream_base_id}:{self._stream_segment}"
async def _publish_event(self, event: AgentEvent) -> None:
if isinstance(event, RetryStatusEvent):
self._retry_status = event if event.state == "exhausted" else None
if isinstance(event, StreamDeltaEvent | StreamEndEvent):
if not self.streaming:
return