Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(agent): preserve streamed replies after model failures (#6494)
chengyongru219c8bb2bcaa
diff --git a/docs/webui.md b/docs/webui.mdindex 8865ba183..529404681 100644--- a/docs/webui.md+++ b/docs/webui.md@@ -133,6 +133,8 @@ File previews follow the active topic's access mode. Restricted workspace access### Model failures+If a model stream fails after producing reply text, the text is retained for reading and follow-up questions. A reply can look complete even when the request ended with a connection error.+After a model request fails and automatic retries stop, the WebUI shows the failure category. Use **Continue** to resume from the saved conversation, or open model settings to change the model or correct its authorization. Review completed work before continuing: saved tool results are retained, but the model can choose to call tools again. Continuing does not resend the original message. Sending a new message or dismissing the notice clears the pending decision. Saved recovery decisions remain available after a refresh or gateway restart; temporary chats still follow their normal lifetime.### Delegated workdiff --git a/nanobot/agent/hook.py b/nanobot/agent/hook.pyindex fa6dab3f1..a20bbec41 100644--- a/nanobot/agent/hook.py+++ b/nanobot/agent/hook.py@@ -26,6 +26,8 @@ class AgentHookContext:tool_results: list[Any] = field(default_factory=list)tool_events: list[dict[str, str]] = field(default_factory=list)streamed_content: bool = False+ # Raw segments observed this round, retained only if its terminal response fails.+ streamed_content_parts: list[str] = field(default_factory=list, repr=False)streamed_reasoning: bool = Falsestream_continues_current_message: bool = Falsefinal_content: str | None = Nonediff --git a/nanobot/agent/runner.py b/nanobot/agent/runner.pyindex bdac12f70..fa8782ecd 100644--- a/nanobot/agent/runner.py+++ b/nanobot/agent/runner.py@@ -726,6 +726,23 @@ class AgentRunner:response,)+ if response.finish_reason == "error":+ interrupted_parts = [+ content+ for part in context.streamed_content_parts+ if (content := extract_reasoning(None, None, part)[1])+ ]+ interrupted_content = hook.finalize_content(+ context, "\n\n".join(interrupted_parts),+ )+ if not is_blank_text(interrupted_content):+ # This is unsynchronized output, not a completed response or+ # provider-owned projection. Keep it in the next native delta.+ messages.append({+ **build_assistant_message(interrupted_content),+ "_interrupted": True,+ })+# Check for mid-turn injections BEFORE signaling stream end.# If injections are found we keep the stream alive (resuming=True)# so streaming channels don't prematurely finalize the card.@@ -934,6 +951,13 @@ class AgentRunner:first_output_at: float | None = Nonegeneration_started_at: float | None = Nonegeneration_elapsed_s = 0.0+ stream_buf = ""++ def _end_stream_segment() -> None:+ nonlocal stream_buf+ if stream_buf:+ context.streamed_content_parts.append(stream_buf)+ stream_buf = ""def _generation_delta(delta: str) -> None:nonlocal first_output_at, generation_started_at@@ -981,6 +1005,8 @@ class AgentRunner:if event.get("kind") != "hosted_tool":returnawait _close_native_reasoning()+ if event.get("phase") == "start":+ _end_stream_segment()await hook.on_provider_tool_event(context, event)call_id = event.get("call_id")if not call_id:@@ -995,6 +1021,8 @@ class AgentRunner:thinking_buf = ""async def _stream(delta: str) -> None:+ nonlocal stream_buf+ stream_buf += delta_generation_delta(delta)if delta:context.streamed_content = True@@ -1018,6 +1046,7 @@ class AgentRunner:async def _stream_recover() -> None:_pause_generation()await _close_native_reasoning()+ _end_stream_segment()await hook.on_stream_end(context, resuming=True)kwargs.update(@@ -1039,6 +1068,7 @@ class AgentRunner:raise_pause_generation()await _close_native_reasoning()+ _end_stream_segment()if first_output_at is not None:response.ttft_ms = max(0, round((first_output_at - request_started_at) * 1000))if generation_elapsed_s > 0:diff --git a/nanobot/events.py b/nanobot/events.pyindex 39dfd8d75..c4be93f93 100644--- a/nanobot/events.py+++ b/nanobot/events.py@@ -70,6 +70,7 @@ class RecoveryStateEvent(AgentEvent):attempts: int = 0can_continue: bool | None = Nonefailure_error_kind: str | None = None+ reply_preserved: bool | None = None@dataclass(frozen=True)diff --git a/nanobot/providers/base.py b/nanobot/providers/base.pyindex de886f772..429dadbd3 100644--- a/nanobot/providers/base.py+++ b/nanobot/providers/base.py@@ -1120,7 +1120,7 @@ class LLMProvider(ABC):),timeout=resolve_stream_idle_timeout_s(),)- if on_content_delta and response.content:+ if on_content_delta and response.content and response.finish_reason != "error":await on_content_delta(response.content)return responsediff --git a/nanobot/session/recovery.py b/nanobot/session/recovery.pyindex bea08000a..7c2c5429e 100644--- a/nanobot/session/recovery.py+++ b/nanobot/session/recovery.py@@ -452,4 +452,6 @@ def recovery_state_from_metadata(metadata: Mapping[str, Any] | None) -> dict[str"authentication", "billing", "connection", "timeout", "rate_limit", "server", "unknown",}:payload["failure_error_kind"] = failure_error_kind+ if state.get("reply_preserved") is True:+ payload["reply_preserved"] = Truereturn payloaddiff --git a/nanobot/webui/outbound_wire.py b/nanobot/webui/outbound_wire.pyindex cf89fd1a3..316f7c968 100644--- a/nanobot/webui/outbound_wire.py+++ b/nanobot/webui/outbound_wire.py@@ -73,6 +73,7 @@ class RecoveryStateWirePayload(_ChatWirePayload):reason: NotRequired[str]can_continue: NotRequired[bool]failure_error_kind: NotRequired[str]+ reply_preserved: NotRequired[bool]class RetryStatusWirePayload(_ChatWirePayload):@@ -139,6 +140,8 @@ def encode_recovery_state(payload["can_continue"] = event.can_continueif event.failure_error_kind is not None:payload["failure_error_kind"] = event.failure_error_kind+ if event.reply_preserved is not None:+ payload["reply_preserved"] = event.reply_preservedreturn payloaddiff --git a/nanobot/webui/recovery.py b/nanobot/webui/recovery.pyindex dca69e7e1..15c246d3a 100644--- a/nanobot/webui/recovery.py+++ b/nanobot/webui/recovery.py@@ -187,6 +187,8 @@ class RecoveryCoordinator:can_continue=any(message.get("role") == "user" for message in session.messages),)waiting["failure_error_kind"] = failure_error_kind or "unknown"+ if session.messages and session.messages[-1].get("_interrupted") is True:+ waiting["reply_preserved"] = Trueawait self.sessions.state.restore_interruption(session)await self._publish(route[1], waiting)return@@ -449,6 +451,7 @@ class RecoveryCoordinator:attempts=cast(int, state.get("attempts", 0)),can_continue=cast(bool | None, state.get("can_continue")),failure_error_kind=cast(str | None, state.get("failure_error_kind")),+ reply_preserved=cast(bool | None, state.get("reply_preserved")),),))diff --git a/packages/client-events/fixtures.json b/packages/client-events/fixtures.jsonindex d8fb4d418..611a0ce97 100644--- a/packages/client-events/fixtures.json+++ b/packages/client-events/fixtures.json@@ -5,7 +5,7 @@{"event":"context_compaction","chat_id":"chat","compaction_id":"c1","phase":"cancelled"},{"event":"recovery_state","chat_id":"chat","status":"resuming","recovery_id":"r1","attempts":0},{"event":"recovery_state","chat_id":"chat","status":"awaiting_user","recovery_id":"r1","attempts":1,"reason":"tool_state_unknown","can_continue":false},- {"event":"recovery_state","chat_id":"chat","status":"awaiting_user","recovery_id":"r2","attempts":0,"reason":"model_request_failed","failure_error_kind":"connection"},+ {"event":"recovery_state","chat_id":"chat","status":"awaiting_user","recovery_id":"r2","attempts":0,"reason":"model_request_failed","failure_error_kind":"connection","reply_preserved":true},{"event":"recovery_state","chat_id":"chat","status":"recovered","recovery_id":"r1","attempts":1},{"event":"recovery_state","chat_id":"chat","status":"failed","recovery_id":"r1","attempts":2},{"event":"retry_status","chat_id":"chat","state":"waiting","attempt":1,"max_attempts":4,"error_kind":"connection","retry_after_s":2},diff --git a/packages/client-events/notifications.ts b/packages/client-events/notifications.tsindex d5312fa6d..ea8ba6f8a 100644--- a/packages/client-events/notifications.ts+++ b/packages/client-events/notifications.ts@@ -8,6 +8,7 @@ export interface RecoveryState {attempts?: numbercan_continue?: booleanfailure_error_kind?: string+ reply_preserved?: boolean}export type CompactionReason = "idle" | "context_pressure" | "manual" | "provider_native"@@ -63,6 +64,7 @@ export function isRecoveryState(value: unknown): value is RecoveryState {&& optional(value.attempts, "number")&& optional(value.can_continue, "boolean")&& optional(value.failure_error_kind, "string")+ && optional(value.reply_preserved, "boolean")}/** Undefined means another protocol family; null means a malformed notification. */diff --git a/tests/agent/test_prompt_responsiveness.py b/tests/agent/test_prompt_responsiveness.pyindex 3e53e5735..e012ba435 100644--- a/tests/agent/test_prompt_responsiveness.py+++ b/tests/agent/test_prompt_responsiveness.py@@ -454,23 +454,35 @@ async def test_local_token_estimation_leaves_loop_available(loop._extra_hooks.append(Lifecycle())- def count(*_args, **_kwargs):- captured.append(current_request_context())+ from nanobot.utils.helpers import estimate_prompt_tokens_chain++ def count(*args, **kwargs):+ captured.append((current_request_context(), threading.get_ident()))runtime_loop.call_soon_threadsafe(entered.set)try:assert release.wait(5), "test did not release local token estimation"- return 10, "test-counter"+ return estimate_prompt_tokens_chain(*args, **kwargs)finally:runtime_loop.call_soon_threadsafe(completed.set)module = "context_governance" if phase == "request" else "runner"monkeypatch.setattr(f"nanobot.agent.{module}.estimate_prompt_tokens_chain", count)task = asyncio.create_task(loop.process_direct("Hello", session_key="cli:token-work"))+ entry = asyncio.create_task(entered.wait())try:- await asyncio.wait_for(entered.wait(), timeout=5)- assert captured[0] is not None- assert captured[0].session_key == "cli:token-work"- assert captured[0].workspace_scope.project_path == tmp_path.resolve()+ # Admission and prompt I/O precede estimation. Its worker gate, not turn+ # startup, owns the five-second responsiveness deadline.+ done, _ = await asyncio.wait((task, entry), return_when=asyncio.FIRST_COMPLETED)+ if task in done:+ task.result()+ assert entry in done, "turn ended before local token estimation started"+ request, worker_thread = captured[0]+ assert request is not None+ assert request.session_key == "cli:token-work"+ assert request.workspace_scope.project_path == tmp_path.resolve()+ assert worker_thread != threading.get_ident()+ assert not completed.is_set()+ assert not release.is_set()assert current_request_context() is Noneif phase == "request":loop.provider.chat_stream_with_retry.assert_not_awaited()@@ -493,9 +505,10 @@ async def test_local_token_estimation_leaves_loop_available(assert [public_history_message(message)["content"] for message in saved.messages] == ["Hello", "Done"]finally:release.set()- if not task.done():- task.cancel()- await asyncio.gather(task, return_exceptions=True)+ for pending in (task, entry):+ if not pending.done():+ pending.cancel()+ await asyncio.gather(task, entry, return_exceptions=True)if captured:await asyncio.wait_for(completed.wait(), timeout=5)await loop.aclose()diff --git a/tests/agent/test_runner_interrupted_output.py b/tests/agent/test_runner_interrupted_output.pynew file mode 100644index 000000000..38bce9d42--- /dev/null+++ b/tests/agent/test_runner_interrupted_output.py@@ -0,0 +1,287 @@+"""Model failures retain the assistant text already delivered to the user."""++from __future__ import annotations++from copy import deepcopy+from unittest.mock import AsyncMock, MagicMock++import pytest++from agent.runner_helpers import make_run_spec+from nanobot.agent.progress_hook import AgentProgressHook+from nanobot.agent.runner import _PERSISTED_MODEL_ERROR_PLACEHOLDER, AgentRunner+from nanobot.config.schema import ModelPresetConfig+from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest+from nanobot.providers.fallback_provider import FallbackProvider+from nanobot.providers.model_api import ModelAPICapabilities+from nanobot.providers.openai_compat_provider import OpenAICompatProvider+from nanobot.providers.openai_responses import parse_response_output+from nanobot.providers.unconfigured_provider import UnconfiguredProvider+from nanobot.utils.progress_events import output_events++[email protected]("text", ["The answer is 42.", "The answer starts with"])+async def test_failed_stream_retains_real_reply_for_the_next_request(text):+ provider = MagicMock(spec=LLMProvider)+ tools = MagicMock()+ tools.get_definitions.return_value = []+ tools.execute = AsyncMock()++ async def fail_after_output(**kwargs):+ await kwargs["on_thinking_delta"]("private reasoning")+ await kwargs["on_content_delta"]("<think>hidden thought</think>" + text)+ return LLMResponse(+ content="network diagnostic, not assistant text",+ finish_reason="error",+ error_kind="connection",+ tool_calls=[ToolCallRequest(id="unfinished", name="exec", arguments={})],+ )++ provider.chat_stream_with_retry = AsyncMock(side_effect=fail_after_output)+ continue_goal = AsyncMock(return_value="continue the goal")+ checkpoints = []+ result = await AgentRunner().run(make_run_spec(+ provider, initial_messages=[{"role": "user", "content": "question"}],+ tools=tools, model="test", max_iterations=2, max_tool_result_chars=1000,+ hook=AgentProgressHook(streaming=True),+ continuation_callback=continue_goal,+ checkpoint_callback=AsyncMock(side_effect=checkpoints.append),+ ))++ assert result.stop_reason == "error"+ assert result.failure_error_kind == "connection"+ assert result.messages[-1]["content"] == text+ assert result.messages[-1]["_interrupted"] is True+ assert result.final_content == result.error == "network diagnostic, not assistant text"+ assert checkpoints == []+ continue_goal.assert_not_awaited()+ tools.execute.assert_not_awaited()++ provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="follow-up"))+ await AgentRunner().run(make_run_spec(+ provider,+ initial_messages=[*result.messages, {"role": "user", "content": "explain that"}],+ tools=tools, model="test", max_iterations=1, max_tool_result_chars=1000,+ ))+ sent = provider.chat_stream_with_retry.await_args.kwargs["messages"]+ assert [message["content"] for message in sent if message.get("role") == "assistant"] == [text]+++async def test_failed_stream_keeps_visible_segments_across_hosted_tools_and_recovery():+ provider = MagicMock(spec=LLMProvider)+ tools = MagicMock()+ tools.get_definitions.return_value = []+ visible = []++ async def show(delta):+ visible.append(delta)++ async def fail_after_segments(**kwargs):+ await kwargs["on_content_delta"]("<thi")+ await kwargs["on_content_delta"]("nk>hidden</think>Before search.")+ await kwargs["on_tool_call_delta"]({+ "kind": "hosted_tool", "phase": "start", "call_id": "search-1",+ "name": "web_search", "arguments": {},+ })+ await kwargs["on_content_delta"]("<think>unfinished hidden thought")+ await kwargs["on_stream_recover"]()+ await kwargs["on_content_delta"]("<thinking>new hidden thought</thinking>After search.")+ return LLMResponse(content="connection lost", finish_reason="error", error_kind="connection")++ provider.chat_stream_with_retry = AsyncMock(side_effect=fail_after_segments)+ result = await AgentRunner().run(make_run_spec(+ provider, initial_messages=[{"role": "user", "content": "search"}],+ tools=tools, model="test", max_iterations=1, max_tool_result_chars=1000,+ hook=AgentProgressHook(output_events(on_stream=show), streaming=True),+ ))++ assert visible == ["Before search.", "After search."]+ assert result.messages[-1]["content"] == "Before search.\n\nAfter search."+ assert result.messages[-1]["_interrupted"] is True+ assert result.stop_reason == "error"+++async def test_reasoning_only_failure_does_not_claim_a_preserved_reply():+ provider = MagicMock(spec=LLMProvider)+ tools = MagicMock()+ tools.get_definitions.return_value = []++ async def fail_after_reasoning(**kwargs):+ await kwargs["on_thinking_delta"]("native private reasoning")+ await kwargs["on_content_delta"]("<think>unfinished private reasoning")+ return LLMResponse(content="connection lost", finish_reason="error", error_kind="connection")++ provider.chat_stream_with_retry = AsyncMock(side_effect=fail_after_reasoning)+ result = await AgentRunner().run(make_run_spec(+ provider, initial_messages=[{"role": "user", "content": "question"}],+ tools=tools, model="test", max_iterations=1, max_tool_result_chars=1000,+ hook=AgentProgressHook(streaming=True),+ ))++ assert result.messages[-1]["content"] == _PERSISTED_MODEL_ERROR_PLACEHOLDER+ assert result.messages[-1].get("_interrupted") is not True+ assert result.stop_reason == "error"+++async def test_nonstreaming_provider_error_is_not_observed_as_assistant_body():+ provider = UnconfiguredProvider("test")+ tools = MagicMock()+ tools.get_definitions.return_value = []+ show = AsyncMock()+ result = await AgentRunner().run(make_run_spec(+ provider, initial_messages=[{"role": "user", "content": "question"}],+ tools=tools, model="test", max_iterations=1, max_tool_result_chars=1000,+ hook=AgentProgressHook(output_events(on_stream=show), streaming=True),+ ))++ assert result.stop_reason == "error"+ assert result.failure_error_kind == "unknown"+ assert result.error == result.final_content+ assert "Open Settings → Models" in result.final_content+ assert result.messages[-1]["content"] == _PERSISTED_MODEL_ERROR_PLACEHOLDER+ assert result.messages[-1].get("_interrupted") is not True+ show.assert_not_awaited()+++class _StreamingProvider(LLMProvider):+ def __init__(self, steps):+ super().__init__(provider_name="test")+ self.steps = iter(steps)+ self.calls = 0++ def get_default_model(self):+ return "test"++ async def chat(self, **kwargs):+ raise AssertionError("expected a streaming request")++ async def chat_stream(self, **kwargs):+ self.calls += 1+ text, response = next(self.steps)+ await kwargs["on_content_delta"](text)+ return response++[email protected]("fallback_fails", [False, True])+async def test_retry_and_fallback_preserve_failed_segments_only_on_terminal_error(+ monkeypatch, fallback_fails,+):+ monkeypatch.setattr(LLMProvider, "_CHAT_RETRY_DELAYS", (0,))+ timeout = LLMResponse(content="stream timeout diagnostic", finish_reason="error", error_kind="timeout")+ primary = _StreamingProvider([+ ("First attempt.", timeout), ("Second attempt.", timeout),+ ])+ final = LLMResponse(+ content="fallback connection diagnostic" if fallback_fails else "Backup answer.",+ finish_reason="error" if fallback_fails else "stop",+ error_kind="connection" if fallback_fails else None,+ )+ fallback = _StreamingProvider([("Backup answer.", final)])+ provider = FallbackProvider(+ primary, [ModelPresetConfig(model="backup", provider="custom")],+ lambda _preset: fallback,+ )+ tools = MagicMock()+ tools.get_definitions.return_value = []+ result = await AgentRunner().run(make_run_spec(+ provider, initial_messages=[{"role": "user", "content": "question"}],+ tools=tools, model="test", max_iterations=1, max_tool_result_chars=1000,+ hook=AgentProgressHook(streaming=True),+ ))++ assert primary.calls == 2+ assert fallback.calls == 1+ assistant = [message for message in result.messages if message["role"] == "assistant"]+ assert len(assistant) == 1+ if fallback_fails:+ assert assistant[0]["content"] == "First attempt.\n\nSecond attempt.\n\nBackup answer."+ assert assistant[0]["_interrupted"] is True+ assert result.stop_reason == "error"+ assert result.final_content == "fallback connection diagnostic"+ else:+ assert assistant[0]["content"] == "Backup answer."+ assert assistant[0].get("_interrupted") is not True+ assert result.stop_reason == "completed"+ assert result.final_content == "Backup answer."++[email protected]("midturn", [False, True])+async def test_native_next_request_contains_preserved_reply_without_repeating_completed_tool(+ monkeypatch, midturn,+):+ provider = OpenAICompatProvider(+ api_key="test", default_model="test", model_api=ModelAPICapabilities(+ supported_apis=("responses",), preferred_api="responses",+ ),+ )+ monkeypatch.setattr(provider, "_ensure_client", AsyncMock(return_value=MagicMock()))+ requests = []+ injections = []+ checkpoints = []+ followup = {"role": "user", "content": "explain that"}++ async def send(_client, body, **kwargs):+ requests.append(deepcopy(body["input"]))+ if len(requests) == 1:+ return parse_response_output({+ "status": "completed", "output": [{+ "type": "function_call", "id": "fc_1", "call_id": "call_1",+ "name": "read_file", "arguments": "{}",+ }],+ }, state_provider=kwargs["provider"], state_model=body["model"],+ state_input_items=body["input"])+ if len(requests) == 2:+ await kwargs["on_content_delta"]("Preserved conclusion.")+ if midturn:+ injections.append(followup)+ raise ConnectionError("connection lost after reply")+ return LLMResponse(content="follow-up answer")++ async def drain():+ batch = list(injections)+ injections.clear()+ return batch++ monkeypatch.setattr(provider._responses, "sdk_request", send)+ tools = MagicMock()+ tools.get_definitions.return_value = []+ tools.execute = AsyncMock(return_value="completed tool output")+ result = await AgentRunner().run(make_run_spec(+ provider, initial_messages=[{"role": "user", "content": "question"}],+ tools=tools, model="test", max_iterations=3, max_tool_result_chars=1000,+ hook=AgentProgressHook(streaming=True), injection_callback=drain,+ checkpoint_callback=AsyncMock(side_effect=checkpoints.append),+ ))+ if not midturn:+ assert result.stop_reason == "error"+ assert result.messages[-1]["_interrupted"] is True+ assert result.provider_state is not None+ await AgentRunner().run(make_run_spec(+ provider, initial_messages=[*result.messages, followup], tools=tools,+ model="test", max_iterations=1, max_tool_result_chars=1000,+ provider_state=result.provider_state.with_pending_messages([+ *result.provider_state.pending_messages, followup,+ ]),+ ))+ else:+ assert result.stop_reason == "completed"++ assert len(requests) == 3+ assert requests[2][-2:] == [{+ "type": "message", "role": "assistant", "status": "completed",+ "content": [{"type": "output_text", "text": "Preserved conclusion."}],+ }, {+ "role": "user", "content": [{"type": "input_text", "text": "explain that"}],+ }]+ assert [item for item in requests[2] if item.get("type") == "function_call_output"] == [{+ "type": "function_call_output", "call_id": "call_1", "output": "completed tool output",+ }]+ assert not any(checkpoint["assistant_message"].get("_interrupted") for checkpoint in checkpoints)+ tools.execute.assert_awaited_once_with("read_file", {})+ await provider.aclose()diff --git a/tests/session/test_recovery.py b/tests/session/test_recovery.pyindex f12238aba..a715c7659 100644--- a/tests/session/test_recovery.py+++ b/tests/session/test_recovery.py@@ -739,10 +739,15 @@ async def test_dismiss_does_not_queue_work(tmp_path: Path) -> None:@pytest.mark.asyncio-async def test_model_failure_waits_across_restart_and_rejects_duplicate_continue(tmp_path: Path) -> None:[email protected]("reply_preserved", [False, True])+async def test_model_failure_waits_across_restart_and_rejects_duplicate_continue(+ tmp_path: Path, reply_preserved: bool,+) -> None:coordinator, bus, sessions = _coordinator(tmp_path)session = sessions.get_or_create("websocket:chat")session.messages = [{"role": "user", "content": "finish this"}]+ if reply_preserved:+ session.messages.append({"role": "assistant", "content": "saved reply", "_interrupted": True})_persist(sessions, session)await coordinator.turn_completed("websocket:chat", failure_kind="model", failure_error_kind="connection")@@ -752,6 +757,11 @@ async def test_model_failure_waits_across_restart_and_rejects_duplicate_continueassert bus.inbound.empty()state = sessions.get_or_create("websocket:chat").metadata[RECOVERY_METADATA_KEY]assert state["failure_error_kind"] == "connection"+ assert state.get("reply_preserved", False) is reply_preserved+ from nanobot.session.recovery import recovery_state_from_metadata+ public_state = recovery_state_from_metadata({RECOVERY_METADATA_KEY: state})+ assert public_state is not None+ assert public_state.get("reply_preserved", False) is reply_preservedassert state["status"] == "awaiting_user"payload = {"chat_id": "chat", "recovery_id": state["recovery_id"]}await coordinator.handle_action("continue", payload)diff --git a/tests/webui/test_outbound_wire.py b/tests/webui/test_outbound_wire.pyindex 09c783703..a5910c264 100644--- a/tests/webui/test_outbound_wire.py+++ b/tests/webui/test_outbound_wire.py@@ -161,3 +161,18 @@ def test_encode_turn_end_projects_complete_wire_contract() -> None:"failure_attempts": 4,"failure_message": "Connection refused",}++[email protected]("preserved", [False, True])+def test_encode_recovery_state_reports_reply_preservation(preserved: bool) -> None:+ payload = encode_recovery_state(+ "chat-1",+ RecoveryStateEvent(+ status="awaiting_user", recovery_id="recovery-1",+ reason="model_request_failed", failure_error_kind="connection",+ reply_preserved=preserved,+ ),+ )+ assert payload["reply_preserved"] is preserved+ assert payload["status"] == "awaiting_user"+ assert payload["failure_error_kind"] == "connection"diff --git a/webui/src/components/thread/RecoveryNotice.tsx b/webui/src/components/thread/RecoveryNotice.tsxindex f7ad9af2f..b3824b5fa 100644--- a/webui/src/components/thread/RecoveryNotice.tsx+++ b/webui/src/components/thread/RecoveryNotice.tsx@@ -45,7 +45,9 @@ export function RecoveryNotice({ state, onContinue, onDismiss, onOpenSettings }:? t("recovery.failedHelp", {defaultValue: "The saved task could not be restored safely. Review it before continuing.",})- : modelFailure?.body ?? t("recovery.review", { defaultValue: "Review the task before continuing. Tools will not be replayed automatically." });+ : modelFailure && state.reply_preserved === true+ ? t("recovery.replyPreserved", { defaultValue: "Reply saved. You can continue." })+ : modelFailure?.body ?? t("recovery.review", { defaultValue: "Review the task before continuing. Tools will not be replayed automatically." });const run = (action: "continue" | "dismiss") => {setPending(action);setError(null);diff --git a/webui/src/hooks/useNanobotStream.ts b/webui/src/hooks/useNanobotStream.tsindex 6b1c176dd..8a749eb98 100644--- a/webui/src/hooks/useNanobotStream.ts+++ b/webui/src/hooks/useNanobotStream.ts@@ -870,6 +870,9 @@ export function useNanobotStream(...(typeof ev.failure_error_kind === "string"? { failure_error_kind: ev.failure_error_kind }: {}),+ ...(typeof ev.reply_preserved === "boolean"+ ? { reply_preserved: ev.reply_preserved }+ : {}),};setRecoveryState(next);if (ev.reason === "model_request_failed" || ev.status === "resuming") {diff --git a/webui/src/i18n/locales/en/common.json b/webui/src/i18n/locales/en/common.jsonindex 7989aaba2..7a50e6019 100644--- a/webui/src/i18n/locales/en/common.json+++ b/webui/src/i18n/locales/en/common.json@@ -2546,6 +2546,7 @@"failedHelp": "The saved task could not be restored safely. Review it before continuing.","resuming": "Restoring interrupted task…","review": "Review the task before continuing. Tools will not be replayed automatically.",+ "replyPreserved": "Reply saved. You can continue.","safeResume": "Continuing from saved conversation context.","dismiss": "Dismiss","continue": "Continue"@@ -2670,7 +2671,7 @@"authenticationBody": "Sign in again or check the provider’s API key in model settings.","billingTitle": "Model provider quota is unavailable","billingBody": "Add credit or check billing for the provider account, then try again.",- "connectionTitle": "Could not connect to the model provider",+ "connectionTitle": "Model connection failed","timeoutTitle": "Model provider request timed out","rateLimitTitle": "Model provider rate limit reached","serverTitle": "Model provider service error",diff --git a/webui/src/i18n/locales/es/common.json b/webui/src/i18n/locales/es/common.jsonindex 99988a5f5..e6a0d6bf3 100644--- a/webui/src/i18n/locales/es/common.json+++ b/webui/src/i18n/locales/es/common.json@@ -2341,6 +2341,7 @@"failedHelp": "La tarea guardada no se pudo restaurar de forma segura. Revísala antes de continuar.","resuming": "Restaurando la tarea interrumpida…","review": "Revisa la tarea antes de continuar. Las herramientas no se repetirán automáticamente.",+ "replyPreserved": "La respuesta está guardada. Puedes continuar.","safeResume": "Continuando desde el contexto guardado de la conversación.","dismiss": "Descartar","continue": "Continuar"@@ -2465,7 +2466,7 @@"authenticationBody": "Vuelve a iniciar sesión o revisa la clave API en los ajustes del modelo.","billingTitle": "La cuota del proveedor del modelo no está disponible","billingBody": "Añade crédito o revisa la facturación de la cuenta del proveedor y vuelve a intentarlo.",- "connectionTitle": "No se pudo conectar con el proveedor del modelo",+ "connectionTitle": "Error de conexión con el modelo","timeoutTitle": "La solicitud al proveedor del modelo agotó el tiempo de espera","rateLimitTitle": "Se alcanzó el límite de solicitudes del proveedor del modelo","serverTitle": "Error del servicio del proveedor del modelo",diff --git a/webui/src/i18n/locales/fr/common.json b/webui/src/i18n/locales/fr/common.jsonindex 49203f74c..e45998b45 100644--- a/webui/src/i18n/locales/fr/common.json+++ b/webui/src/i18n/locales/fr/common.json@@ -2340,6 +2340,7 @@"failedHelp": "La tâche enregistrée n’a pas pu être restaurée en toute sécurité. Vérifiez-la avant de continuer.","resuming": "Restauration de la tâche interrompue…","review": "Vérifiez la tâche avant de continuer. Les outils ne seront pas relancés automatiquement.",+ "replyPreserved": "La réponse est enregistrée. Vous pouvez continuer.","safeResume": "Reprise depuis le contexte de conversation enregistré.","dismiss": "Ignorer","continue": "Continuer"@@ -2464,7 +2465,7 @@"authenticationBody": "Reconnectez-vous ou vérifiez la clé API dans les paramètres du modèle.","billingTitle": "Quota du fournisseur du modèle indisponible","billingBody": "Ajoutez du crédit ou vérifiez la facturation du compte fournisseur, puis réessayez.",- "connectionTitle": "Impossible de se connecter au fournisseur du modèle",+ "connectionTitle": "Échec de connexion au modèle","timeoutTitle": "La requête au fournisseur du modèle a expiré","rateLimitTitle": "Limite de requêtes du fournisseur du modèle atteinte","serverTitle": "Erreur du service du fournisseur du modèle",diff --git a/webui/src/i18n/locales/id/common.json b/webui/src/i18n/locales/id/common.jsonindex 3d51359fd..9191dfad6 100644--- a/webui/src/i18n/locales/id/common.json+++ b/webui/src/i18n/locales/id/common.json@@ -2340,6 +2340,7 @@"failedHelp": "Tugas tersimpan tidak dapat dipulihkan dengan aman. Tinjau sebelum melanjutkan.","resuming": "Memulihkan tugas yang terputus…","review": "Tinjau tugas sebelum melanjutkan. Alat tidak akan dijalankan ulang secara otomatis.",+ "replyPreserved": "Balasan tersimpan. Anda dapat melanjutkan.","safeResume": "Melanjutkan dari konteks percakapan yang tersimpan.","dismiss": "Abaikan","continue": "Lanjutkan"@@ -2464,7 +2465,7 @@"authenticationBody": "Masuk kembali atau periksa kunci API di pengaturan model.","billingTitle": "Kuota penyedia model tidak tersedia","billingBody": "Tambahkan kredit atau periksa tagihan akun penyedia, lalu coba lagi.",- "connectionTitle": "Tidak dapat terhubung ke penyedia model",+ "connectionTitle": "Koneksi model gagal","timeoutTitle": "Permintaan ke penyedia model kehabisan waktu","rateLimitTitle": "Batas permintaan penyedia model tercapai","serverTitle": "Kesalahan layanan penyedia model",diff --git a/webui/src/i18n/locales/ja/common.json b/webui/src/i18n/locales/ja/common.jsonindex cb798fff1..16588a142 100644--- a/webui/src/i18n/locales/ja/common.json+++ b/webui/src/i18n/locales/ja/common.json@@ -2340,6 +2340,7 @@"failedHelp": "保存されたタスクを安全に復元できませんでした。続行前に確認してください。","resuming": "中断されたタスクを復元しています…","review": "続行する前にタスクを確認してください。ツールは自動的に再実行されません。",+ "replyPreserved": "返信は保存されています。生成を続けられます。","safeResume": "保存された会話コンテキストから続行しています。","dismiss": "閉じる","continue": "続行"@@ -2464,7 +2465,7 @@"authenticationBody": "モデル設定で再度ログインするか、API キーを確認してください。","billingTitle": "モデルプロバイダーの利用枠を使用できません","billingBody": "プロバイダーアカウントにクレジットを追加するか、請求状況を確認してから、もう一度お試しください。",- "connectionTitle": "モデルプロバイダーに接続できません",+ "connectionTitle": "モデル接続エラー","timeoutTitle": "モデルプロバイダーへのリクエストがタイムアウトしました","rateLimitTitle": "モデルプロバイダーのリクエスト上限に達しました","serverTitle": "モデルプロバイダーでサービスエラーが発生しました",diff --git a/webui/src/i18n/locales/ko/common.json b/webui/src/i18n/locales/ko/common.jsonindex 79e304dbf..934193efd 100644--- a/webui/src/i18n/locales/ko/common.json+++ b/webui/src/i18n/locales/ko/common.json@@ -2340,6 +2340,7 @@"failedHelp": "저장된 작업을 안전하게 복구할 수 없습니다. 계속하기 전에 검토하세요.","resuming": "중단된 작업을 복구하는 중…","review": "계속하기 전에 작업을 검토하세요. 도구는 자동으로 다시 실행되지 않습니다.",+ "replyPreserved": "답변이 저장되었습니다. 계속 생성할 수 있습니다.","safeResume": "저장된 대화 컨텍스트에서 계속합니다.","dismiss": "닫기","continue": "계속"@@ -2464,7 +2465,7 @@"authenticationBody": "모델 설정에서 다시 로그인하거나 API 키를 확인하세요.","billingTitle": "모델 제공업체 할당량을 사용할 수 없음","billingBody": "제공업체 계정에 크레딧을 추가하거나 결제 상태를 확인한 후 다시 시도하세요.",- "connectionTitle": "모델 제공업체에 연결할 수 없음",+ "connectionTitle": "모델 연결 오류","timeoutTitle": "모델 제공업체 요청 시간 초과","rateLimitTitle": "모델 제공업체 요청 한도 초과","serverTitle": "모델 제공업체 서비스 오류",diff --git a/webui/src/i18n/locales/pt-BR/common.json b/webui/src/i18n/locales/pt-BR/common.jsonindex b45fd39ef..29a423da0 100644--- a/webui/src/i18n/locales/pt-BR/common.json+++ b/webui/src/i18n/locales/pt-BR/common.json@@ -2354,6 +2354,7 @@"failedHelp": "Não foi possível restaurar a tarefa salva com segurança. Revise-a antes de continuar.","resuming": "Restaurando a tarefa interrompida…","review": "Revise a tarefa antes de continuar. As ferramentas não serão executadas novamente de forma automática.",+ "replyPreserved": "A resposta foi salva. Você pode continuar.","safeResume": "Continuando a partir do contexto de conversa salvo.","dismiss": "Descartar","continue": "Continuar"@@ -2478,7 +2479,7 @@"authenticationBody": "Entre novamente ou verifique a chave API nas configurações do modelo.","billingTitle": "A cota do provedor do modelo está indisponível","billingBody": "Adicione créditos ou verifique o faturamento da conta do provedor e tente novamente.",- "connectionTitle": "Não foi possível conectar ao provedor do modelo",+ "connectionTitle": "Falha na conexão com o modelo","timeoutTitle": "A solicitação ao provedor do modelo expirou","rateLimitTitle": "Limite de solicitações do provedor do modelo atingido","serverTitle": "Erro no serviço do provedor do modelo",diff --git a/webui/src/i18n/locales/vi/common.json b/webui/src/i18n/locales/vi/common.jsonindex a2e4598d9..c0ead703c 100644--- a/webui/src/i18n/locales/vi/common.json+++ b/webui/src/i18n/locales/vi/common.json@@ -2340,6 +2340,7 @@"failedHelp": "Không thể khôi phục an toàn tác vụ đã lưu. Hãy xem lại trước khi tiếp tục.","resuming": "Đang khôi phục tác vụ bị gián đoạn…","review": "Hãy xem lại tác vụ trước khi tiếp tục. Công cụ sẽ không tự động chạy lại.",+ "replyPreserved": "Câu trả lời đã được lưu. Bạn có thể tiếp tục.","safeResume": "Đang tiếp tục từ ngữ cảnh hội thoại đã lưu.","dismiss": "Bỏ qua","continue": "Tiếp tục"@@ -2464,7 +2465,7 @@"authenticationBody": "Đăng nhập lại hoặc kiểm tra khóa API trong cài đặt mô hình.","billingTitle": "Hạn mức của nhà cung cấp mô hình không khả dụng","billingBody": "Hãy nạp thêm tín dụng hoặc kiểm tra thanh toán của tài khoản nhà cung cấp rồi thử lại.",- "connectionTitle": "Không thể kết nối với nhà cung cấp mô hình",+ "connectionTitle": "Lỗi kết nối mô hình","timeoutTitle": "Yêu cầu tới nhà cung cấp mô hình đã hết thời gian chờ","rateLimitTitle": "Đã đạt giới hạn yêu cầu của nhà cung cấp mô hình","serverTitle": "Lỗi dịch vụ nhà cung cấp mô hình",diff --git a/webui/src/i18n/locales/zh-CN/common.json b/webui/src/i18n/locales/zh-CN/common.jsonindex 35c4a5856..3b0e003da 100644--- a/webui/src/i18n/locales/zh-CN/common.json+++ b/webui/src/i18n/locales/zh-CN/common.json@@ -2546,6 +2546,7 @@"failedHelp": "无法安全恢复已保存的任务,继续前请先检查。","resuming": "正在恢复中断的任务…","review": "继续前请检查任务。工具不会被自动重放。",+ "replyPreserved": "回复已保留,可继续生成。","safeResume": "正在从已保存的对话上下文继续。","dismiss": "忽略","continue": "继续"@@ -2670,7 +2671,7 @@"authenticationBody": "请在模型设置中重新登录或检查 API 密钥。","billingTitle": "模型提供商账户额度不足","billingBody": "请为提供商账户充值或检查账单状态后重试。",- "connectionTitle": "无法连接模型提供商",+ "connectionTitle": "模型连接异常","timeoutTitle": "模型提供商请求超时","rateLimitTitle": "模型提供商请求受限","serverTitle": "模型提供商服务异常",diff --git a/webui/src/i18n/locales/zh-TW/common.json b/webui/src/i18n/locales/zh-TW/common.jsonindex 48027b8b9..60a03ac2c 100644--- a/webui/src/i18n/locales/zh-TW/common.json+++ b/webui/src/i18n/locales/zh-TW/common.json@@ -2340,6 +2340,7 @@"failedHelp": "無法安全復原已儲存的任務,繼續前請先檢查。","resuming": "正在復原中斷的任務…","review": "繼續前請檢查任務。工具不會自動重播。",+ "replyPreserved": "回覆已保留,可繼續生成。","safeResume": "正在從已儲存的對話上下文繼續。","dismiss": "略過","continue": "繼續"@@ -2464,7 +2465,7 @@"authenticationBody": "請在模型設定中重新登入或檢查 API 金鑰。","billingTitle": "模型供應商額度無法使用","billingBody": "請為供應商帳戶儲值或檢查帳單狀態後再試。",- "connectionTitle": "無法連線至模型供應商",+ "connectionTitle": "模型連線異常","timeoutTitle": "模型供應商請求逾時","rateLimitTitle": "模型供應商請求受到限制","serverTitle": "模型供應商服務發生錯誤",diff --git a/webui/src/tests/model-request-failure.test.ts b/webui/src/tests/model-request-failure.test.tsindex a940cf4b8..0c76d2f82 100644--- a/webui/src/tests/model-request-failure.test.ts+++ b/webui/src/tests/model-request-failure.test.ts@@ -12,7 +12,7 @@ describe("model request failure copy", () => {expect(titles).toEqual(["Model provider authorization failed","Model provider quota is unavailable",- "Could not connect to the model provider",+ "Model connection failed","Model provider request timed out","Model provider rate limit reached","Model provider service error",diff --git a/webui/src/tests/recovery-notice.test.tsx b/webui/src/tests/recovery-notice.test.tsxindex 7c2722819..d697b096a 100644--- a/webui/src/tests/recovery-notice.test.tsx+++ b/webui/src/tests/recovery-notice.test.tsx@@ -154,3 +154,32 @@ describe("RecoveryNotice", () => {expect(screen.getByRole("button", { name: "Continue" })).toBeEnabled();});});+++it("shows a saved reply without treating the failed request as completed", async () => {+ const onContinue = vi.fn().mockResolvedValue(undefined);+ const onDismiss = vi.fn().mockResolvedValue(undefined);+ const { rerender } = render(+ <RecoveryNotice+ state={{ ...INTERRUPTED, reason: "model_request_failed", failure_error_kind: "connection", reply_preserved: true }}+ onContinue={onContinue}+ onDismiss={onDismiss}+ />,+ );++ expect(screen.getByRole("alert")).toHaveTextContent("Model connection failed");+ expect(screen.getByRole("alert")).toHaveTextContent("Reply saved. You can continue.");+ fireEvent.click(screen.getByRole("button", { name: "Continue" }));+ await waitFor(() => expect(screen.queryByRole("alert")).not.toBeInTheDocument());+ expect(onContinue).toHaveBeenCalledOnce();++ rerender(+ <RecoveryNotice+ state={{ ...INTERRUPTED, recovery_id: "no-output", reason: "model_request_failed", failure_error_kind: "connection" }}+ onContinue={onContinue}+ onDismiss={onDismiss}+ />,+ );+ expect(screen.getByRole("alert")).toHaveTextContent("Model connection failed");+ expect(screen.getByRole("alert")).toHaveTextContent("Try again later or choose another model.");+});diff --git a/webui/src/tests/shared-proxy-setup.test.tsx b/webui/src/tests/shared-proxy-setup.test.tsxindex ae16dc5e7..1705f1c6b 100644--- a/webui/src/tests/shared-proxy-setup.test.tsx+++ b/webui/src/tests/shared-proxy-setup.test.tsx@@ -140,6 +140,7 @@ describe("Inline shared proxy setup", () => {expect(input).toBeDisabled();fireEvent.keyDown(setup, { key: "Escape" });expect(setup).toBeInTheDocument();+ await waitFor(() => expect(requestMutationMock).toHaveBeenCalledTimes(2));await act(async () => { resolveSave(saved); });await waitFor(() => expect(screen.queryByRole("dialog", { name: "Shared proxy" })).not.toBeInTheDocument());expect(screen.getByRole("dialog", { name: "Docs MCP" })).toBe(parent);diff --git a/webui/src/tests/thread-shell.test.tsx b/webui/src/tests/thread-shell.test.tsxindex 5478d5a34..7d837c512 100644--- a/webui/src/tests/thread-shell.test.tsx+++ b/webui/src/tests/thread-shell.test.tsx@@ -5207,7 +5207,7 @@ describe("ThreadShell", () => {});const banner = await screen.findByRole("alert");- expect(banner).toHaveTextContent("无法连接模型提供商");+ expect(banner).toHaveTextContent("模型连接异常");expect(banner).toHaveTextContent("已尝试 4 次,仍未成功。可稍后继续或切换模型。",);@@ -5224,7 +5224,7 @@ describe("ThreadShell", () => {});});expect(screen.getAllByRole("alert")).toHaveLength(1);- expect(screen.getByRole("alert")).toHaveTextContent("无法连接模型提供商");+ expect(screen.getByRole("alert")).toHaveTextContent("模型连接异常");expect(screen.getByRole("button", { name: "继续" })).toBeEnabled();}diff --git a/webui/src/tests/useNanobotStream.test.tsx b/webui/src/tests/useNanobotStream.test.tsxindex d22285351..bdbc92a4b 100644--- a/webui/src/tests/useNanobotStream.test.tsx+++ b/webui/src/tests/useNanobotStream.test.tsx@@ -822,6 +822,26 @@ describe("useNanobotStream", () => {},);+ it.each([true, false, undefined])("keeps saved-reply facts consistent live and on attach (%s)", (preserved) => {+ const fake = fakeClient();+ const { result } = renderHook(() => useNanobotStream("chat-preserved", EMPTY_MESSAGES), {+ wrapper: wrap(fake.client),+ });+ const state = {+ recovery_id: "preserved-1", status: "awaiting_user" as const,+ reason: "model_request_failed", failure_error_kind: "connection",+ ...(preserved === undefined ? {} : { reply_preserved: preserved }),+ };+ act(() => fake.emit("chat-preserved", {+ event: "recovery_state", chat_id: "chat-preserved", ...state,+ }));+ expect(result.current.recoveryState).toEqual(state);+ act(() => fake.emit("chat-preserved", {+ event: "attached", chat_id: "chat-preserved", recovery_state: state,+ }));+ expect(result.current.recoveryState).toEqual(state);+ });+it("does not let historical recovered state clear a later active turn", () => {const fake = fakeClient();const { result } = renderHook(