Skip to content

Your lightweight open-source personal agent.

fix(web): report ambiguous ddgs failures as unavailable

chengyongrua8873bb5657d
diff --git a/nanobot/agent/tools/web.py b/nanobot/agent/tools/web.py
index 1643336fe..256771ca5 100644
--- a/nanobot/agent/tools/web.py
+++ b/nanobot/agent/tools/web.py
@@ -14,6 +14,8 @@ from typing import Any, cast
from urllib.parse import parse_qsl, quote, urljoin, urlparse
import httpx
+from ddgs.exceptions import DDGSException
+from ddgs.exceptions import TimeoutException as DDGSTimeoutException
from loguru import logger
from nanobot.agent.tools.base import Tool, ToolResult, tool_parameters
@@ -1191,7 +1193,7 @@ class WebSearchTool(Tool):
timeout=self.config.timeout,
)
if not raw:
- return f"No results for: {query}"
+ return f"No usable search entries returned for: {query}"
raw_items = cast(list[dict[str, Any]], raw)
items: list[dict[str, Any]] = [
{"title": r.get("title", ""), "url": r.get("href", ""), "content": r.get("body", "")}
@@ -1203,6 +1205,23 @@ class WebSearchTool(Tool):
"DuckDuckGo search failed: {}",
e if tool_log_content_allowed() else type(e).__name__,
)
+ if isinstance(e, TimeoutError):
+ return ToolResult.error(
+ "Error: Web search timed out while waiting for ddgs. "
+ "No usable search result was obtained before the tool deadline; "
+ "matching-page availability is unknown."
+ )
+ if isinstance(e, DDGSTimeoutException):
+ return ToolResult.error(
+ "Error: Web search unavailable (ddgs reported a timeout). "
+ "No usable search result was obtained; matching-page availability is unknown."
+ )
+ if isinstance(e, DDGSException):
+ return ToolResult.error(
+ "Error: Web search unavailable (ddgs; cause unknown). "
+ "No usable search result was obtained. The search library did not expose "
+ "a reliable cause; this does not establish that no matching pages exist."
+ )
return ToolResult.error(f"Error: DuckDuckGo search failed ({e})")
async def _search_bocha(self, query: str, n: int, freshness: str = "noLimit") -> str:
diff --git a/tests/agent/test_runner_web_search_errors.py b/tests/agent/test_runner_web_search_errors.py
new file mode 100644
index 000000000..423e2ba81
--- /dev/null
+++ b/tests/agent/test_runner_web_search_errors.py
@@ -0,0 +1,81 @@
+"""Model-facing search failures through real ddgs parsing and aggregation."""
+
+from __future__ import annotations
+
+import json
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+from ddgs.ddgs import DDGS
+from ddgs.engines.wikipedia import Wikipedia
+from ddgs.exceptions import TimeoutException
+from ddgs.http_client import HttpClient
+
+from agent.runner_helpers import make_run_spec
+from nanobot.agent.runner import AgentRunner
+from nanobot.agent.tools.registry import ToolRegistry
+from nanobot.agent.tools.web import WebSearchTool
+from nanobot.config.schema import AgentDefaults, WebSearchConfig
+from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest
+
+
+ ("outcome", "expected_header"),
+ [
+ pytest.param("empty", "Error: Web search unavailable (ddgs; cause unknown).", id="empty-parsing"),
+ pytest.param("forbidden", "Error: Web search unavailable (ddgs; cause unknown).", id="http-403"),
+ pytest.param("rate_limited", "Error: Web search unavailable (ddgs; cause unknown).", id="http-429"),
+ pytest.param("timeout", "Error: Web search unavailable (ddgs reported a timeout).", id="library-timeout"),
+ ],
+)
+async def test_ddgs_failure_reaches_model_with_observable_cause(monkeypatch, outcome, expected_header):
+ # Keep the released parser and aggregator, selecting one real engine offline.
+ engine = Wikipedia(timeout=1)
+ monkeypatch.setattr(DDGS, "_get_engines", lambda *_args: [engine])
+ requests: list[str] = []
+
+ def request(_self, method, url, **_kwargs):
+ requests.append(url)
+ if outcome == "timeout":
+ raise TimeoutException("request timed out")
+ status = {"empty": 200, "forbidden": 403, "rate_limited": 429}[outcome]
+ return SimpleNamespace(status_code=status, text=json.dumps(["nanobot", [], [], []]))
+
+ monkeypatch.setattr(HttpClient, "request", request)
+ provider = MagicMock(spec=LLMProvider)
+ provider.chat_stream_with_retry = AsyncMock(side_effect=[
+ LLMResponse(content=None, tool_calls=[ToolCallRequest(
+ id="search-1", name="web_search", arguments={"query": "nanobot"},
+ )]),
+ LLMResponse(content="done"),
+ ])
+ tools = ToolRegistry()
+ tools.register(WebSearchTool(config=WebSearchConfig(provider="duckduckgo")))
+
+ result = await AgentRunner().run(make_run_spec(
+ provider,
+ model="test-model",
+ initial_messages=[{"role": "user", "content": "Search for nanobot"}],
+ tools=tools,
+ max_iterations=2,
+ max_tool_result_chars=AgentDefaults().max_tool_result_chars,
+ ))
+
+ assert len(requests) == 1
+ assert provider.chat_stream_with_retry.await_count == 2
+ model_messages = provider.chat_stream_with_retry.await_args_list[1].kwargs["messages"]
+ tool_message = next(message for message in model_messages if message.get("role") == "tool")
+ assert tool_message["tool_call_id"] == "search-1"
+ assert tool_message["content"].startswith(expected_header)
+ assert "No usable search result was obtained" in tool_message["content"]
+ if outcome == "timeout":
+ assert "matching-page availability is unknown" in tool_message["content"]
+ else:
+ assert "did not expose a reliable cause" in tool_message["content"]
+ assert "does not establish that no matching pages exist" in tool_message["content"]
+ assert result.stop_reason == "completed"
+ assert result.tool_events[0]["name"] == "web_search"
+ assert result.tool_events[0]["status"] == "error"
+ assert result.tool_events[0]["detail"].startswith(expected_header)
diff --git a/tests/tools/test_web_search_tool.py b/tests/tools/test_web_search_tool.py
index 3ad6b2758..4dc768b23 100644
--- a/tests/tools/test_web_search_tool.py
+++ b/tests/tools/test_web_search_tool.py
@@ -1308,9 +1308,13 @@ async def test_duckduckgo_timeout_returns_error(monkeypatch):
monkeypatch.setattr("ddgs.DDGS", HangingDDGS)
tool = _tool(provider="duckduckgo")
tool.config.timeout = 0.2
- result = await tool.execute(query="test")
- gate.set()
- assert "Error" in result
+ try:
+ result = await tool.execute(query="test")
+ finally:
+ gate.set()
+ assert is_tool_error_result(result)
+ assert result.startswith("Error: Web search timed out while waiting for ddgs.")
+ assert "tool deadline" in result
@pytest.mark.asyncio