Skip to content

"Vibe-Trading: Your Personal Trading Agent"

test(search): cover CN fallback without network

Haozhe Wu760a490b878f
diff --git a/agent/src/reliability/__init__.py b/agent/src/reliability/__init__.py
index b21a30771..c01132bdb 100644
--- a/agent/src/reliability/__init__.py
+++ b/agent/src/reliability/__init__.py
@@ -3,4 +3,3 @@
from __future__ import annotations
__all__ = []
-
diff --git a/agent/src/reliability/artifacts/__init__.py b/agent/src/reliability/artifacts/__init__.py
index e939030ea..a7a1dac98 100644
--- a/agent/src/reliability/artifacts/__init__.py
+++ b/agent/src/reliability/artifacts/__init__.py
@@ -14,4 +14,3 @@ __all__ = [
"sha256_file",
"sha256_json",
]
-
diff --git a/agent/src/reliability/artifacts/hashing.py b/agent/src/reliability/artifacts/hashing.py
index 6c63ee519..6ee25bab3 100644
--- a/agent/src/reliability/artifacts/hashing.py
+++ b/agent/src/reliability/artifacts/hashing.py
@@ -80,4 +80,3 @@ def _looks_like_pandas(value: Any) -> bool:
module = type(value).__module__
name = type(value).__name__
return module.startswith("pandas.") and name in {"DataFrame", "Series"}
-
diff --git a/agent/src/reliability/artifacts/lineage.py b/agent/src/reliability/artifacts/lineage.py
index 0263994f7..fca19e967 100644
--- a/agent/src/reliability/artifacts/lineage.py
+++ b/agent/src/reliability/artifacts/lineage.py
@@ -26,4 +26,3 @@ def lineage_edges(record: ArtifactRecord) -> list[ArtifactLineageEdge]:
ArtifactLineageEdge(parent_artifact_id=parent, child_artifact_id=record.artifact_id)
for parent in record.parent_artifacts
]
-
diff --git a/agent/src/reliability/artifacts/model.py b/agent/src/reliability/artifacts/model.py
index 1a630c4f0..a2faecd01 100644
--- a/agent/src/reliability/artifacts/model.py
+++ b/agent/src/reliability/artifacts/model.py
@@ -64,4 +64,3 @@ class ArtifactRecord(BaseModel):
sha256=self.sha256,
uri=self.uri,
)
-
diff --git a/agent/src/reliability/artifacts/store.py b/agent/src/reliability/artifacts/store.py
index 4629463c2..f8875c432 100644
--- a/agent/src/reliability/artifacts/store.py
+++ b/agent/src/reliability/artifacts/store.py
@@ -207,4 +207,3 @@ def _atomic_write_bytes(path: Path, data: bytes) -> None:
tmp.unlink()
except FileNotFoundError:
pass
-
diff --git a/agent/src/reliability/config.py b/agent/src/reliability/config.py
index 89577c432..31931b5c9 100644
--- a/agent/src/reliability/config.py
+++ b/agent/src/reliability/config.py
@@ -36,4 +36,3 @@ def artifact_root() -> Path:
if raw:
return Path(os.path.expandvars(os.path.expanduser(raw))).resolve(strict=False)
return (Path.home() / ".vibe-trading" / "artifacts").resolve(strict=False)
-
diff --git a/agent/src/reliability/errors.py b/agent/src/reliability/errors.py
index a4293ebe5..6ebcdb016 100644
--- a/agent/src/reliability/errors.py
+++ b/agent/src/reliability/errors.py
@@ -17,4 +17,3 @@ class ArtifactStoreError(ReliabilityError):
class ArtifactPathError(ArtifactStoreError, ValueError):
"""Raised when an artifact path escapes the configured root."""
-
diff --git a/agent/src/reliability/redaction.py b/agent/src/reliability/redaction.py
index fc71f8414..4bead252c 100644
--- a/agent/src/reliability/redaction.py
+++ b/agent/src/reliability/redaction.py
@@ -56,4 +56,3 @@ def _key_is_secret_like(key: str) -> bool:
def _value_is_secret_like(value: str) -> bool:
return bool(_BEARER_RE.match(value) or _KEY_PREFIX_RE.match(value) or _LONG_RANDOM_RE.match(value))
-
diff --git a/agent/src/reliability/schema.py b/agent/src/reliability/schema.py
index 36d401ed7..4af733d17 100644
--- a/agent/src/reliability/schema.py
+++ b/agent/src/reliability/schema.py
@@ -31,4 +31,3 @@ ARTIFACT_TYPES: frozenset[str] = frozenset(
"research_card",
}
)
-
diff --git a/agent/src/tools/web_search_tool.py b/agent/src/tools/web_search_tool.py
index 376adab8b..ab65eb871 100644
--- a/agent/src/tools/web_search_tool.py
+++ b/agent/src/tools/web_search_tool.py
@@ -317,7 +317,9 @@ class WebSearchTool(BaseTool):
f"Web search failed after {_MAX_ATTEMPTS} attempts "
f"(backends: {backends if supports_backend else 'duckduckgo'}): {last_error}. "
f"CN fallbacks (sogou, bing_cn): {fb_err}. "
- "Read a known URL directly with read_url."
+ "Retry shortly, set VIBE_TRADING_SEARCH_BACKENDS to a different engine list, "
+ "set VIBE_TRADING_SEARCH_BING_FALLBACK=0 to disable CN fallback, or read a "
+ "known URL directly with read_url."
),
},
ensure_ascii=False,
diff --git a/agent/tests/reliability/test_artifact_concurrency.py b/agent/tests/reliability/test_artifact_concurrency.py
index 18864a6ec..068702b40 100644
--- a/agent/tests/reliability/test_artifact_concurrency.py
+++ b/agent/tests/reliability/test_artifact_concurrency.py
@@ -32,4 +32,3 @@ def test_artifact_store_concurrent_writes_no_metadata_loss(
assert count == len(payloads)
assert len(set(artifact_ids)) == len(payloads)
-
diff --git a/agent/tests/reliability/test_artifact_hashing.py b/agent/tests/reliability/test_artifact_hashing.py
index 09dd18789..b143fdeee 100644
--- a/agent/tests/reliability/test_artifact_hashing.py
+++ b/agent/tests/reliability/test_artifact_hashing.py
@@ -70,4 +70,3 @@ def test_pydantic_model_hash_requires_model_dump_json() -> None:
assert isinstance(digest, str)
assert len(digest) == 64
-
diff --git a/agent/tests/reliability/test_artifact_models.py b/agent/tests/reliability/test_artifact_models.py
index 86bce04ae..8229bf80b 100644
--- a/agent/tests/reliability/test_artifact_models.py
+++ b/agent/tests/reliability/test_artifact_models.py
@@ -71,4 +71,3 @@ def test_artifact_ref_is_lightweight() -> None:
"sha256": "d" * 64,
"uri": "artifact://sha256/" + "d" * 64,
}
-
diff --git a/agent/tests/reliability/test_artifact_store.py b/agent/tests/reliability/test_artifact_store.py
index 6f970d646..2bde07c9d 100644
--- a/agent/tests/reliability/test_artifact_store.py
+++ b/agent/tests/reliability/test_artifact_store.py
@@ -104,4 +104,3 @@ def test_reliability_mode_off_does_not_require_artifact_write(
assert record is None
assert not (tmp_path / "artifact_index.sqlite").exists()
assert not (tmp_path / "objects").exists()
-
diff --git a/agent/tests/reliability/test_artifact_trace_compat.py b/agent/tests/reliability/test_artifact_trace_compat.py
index 25b66e8fb..33e3f8705 100644
--- a/agent/tests/reliability/test_artifact_trace_compat.py
+++ b/agent/tests/reliability/test_artifact_trace_compat.py
@@ -85,4 +85,3 @@ def test_run_card_can_include_optional_irr_artifact_refs(tmp_path: Path) -> None
assert loaded["artifacts"] == card["artifacts"]
assert loaded["artifact_refs"] == artifact_refs
assert "art_scorecard" in (tmp_path / "run_card.md").read_text(encoding="utf-8")
-
diff --git a/agent/tests/test_web_search_tool.py b/agent/tests/test_web_search_tool.py
index 458b4fc76..a1f7fccdf 100644
--- a/agent/tests/test_web_search_tool.py
+++ b/agent/tests/test_web_search_tool.py
@@ -41,6 +41,8 @@ def _patch_ddgs(monkeypatch, text_impl):
@pytest.fixture(autouse=True)
def _clear_backend_env(monkeypatch):
monkeypatch.delenv("VIBE_TRADING_SEARCH_BACKENDS", raising=False)
+ monkeypatch.delenv("VIBE_TRADING_SEARCH_BING_FALLBACK", raising=False)
+ monkeypatch.delenv("ALIYUN_IQS_API_KEY", raising=False)
def test_returns_results_and_passes_backend_list(monkeypatch):
@@ -111,6 +113,7 @@ def test_no_results_is_ok_empty_not_error(monkeypatch):
def test_persistent_failure_returns_actionable_error(monkeypatch):
"""When every attempt fails, the error names the retry/env/read_url remedies."""
+ monkeypatch.setenv("VIBE_TRADING_SEARCH_BING_FALLBACK", "0")
monkeypatch.setattr("src.tools.web_search_tool.time.sleep", lambda *_: None)
calls = {"n": 0}
@@ -127,6 +130,60 @@ def test_persistent_failure_returns_actionable_error(monkeypatch):
assert "read_url" in out["error"]
+def test_persistent_failure_uses_cn_fallback_without_network(monkeypatch):
+ """Persistent ddgs failures can fall back to CN search without touching real network in tests."""
+ monkeypatch.setattr("src.tools.web_search_tool.time.sleep", lambda *_: None)
+ calls = {"ddgs": 0, "sogou": 0, "bing_cn": 0}
+
+ def text_impl(query, max_results, **kwargs):
+ calls["ddgs"] += 1
+ raise RuntimeError("Ratelimit 429")
+
+ def fake_sogou(query, max_results=5):
+ calls["sogou"] += 1
+ return [{"title": "fallback", "href": "https://example.test/f", "body": "ok"}]
+
+ def fake_bing_cn(query, max_results=5):
+ calls["bing_cn"] += 1
+ return [{"title": "backup", "href": "https://example.test/b", "body": "ok"}]
+
+ monkeypatch.setattr("src.tools.web_search_tool._sogou_search", fake_sogou)
+ monkeypatch.setattr("src.tools.web_search_tool._bing_cn_search", fake_bing_cn)
+
+ with _patch_ddgs(monkeypatch, text_impl):
+ out = json.loads(WebSearchTool().execute(query="tsla"))
+
+ assert out["status"] == "ok"
+ assert out["backends"] == "sogou_fallback"
+ assert out["results"][0]["url"] == "https://example.test/f"
+ assert calls == {"ddgs": 3, "sogou": 1, "bing_cn": 0}
+
+
+def test_network_failure_fast_fails_to_cn_fallback(monkeypatch):
+ """Network-style ddgs failures skip extra retries and go straight to fallback."""
+ monkeypatch.setattr("src.tools.web_search_tool.time.sleep", lambda *_: None)
+ calls = {"ddgs": 0}
+
+ def text_impl(query, max_results, **kwargs):
+ calls["ddgs"] += 1
+ raise RuntimeError("Connection timed out")
+
+ monkeypatch.setattr(
+ "src.tools.web_search_tool._sogou_search",
+ lambda query, max_results=5: [
+ {"title": "fallback", "href": "https://example.test/f", "body": "ok"}
+ ],
+ )
+ monkeypatch.setattr("src.tools.web_search_tool._bing_cn_search", lambda *_args, **_kwargs: [])
+
+ with _patch_ddgs(monkeypatch, text_impl):
+ out = json.loads(WebSearchTool().execute(query="maotai"))
+
+ assert out["status"] == "ok"
+ assert out["backends"] == "sogou_fallback"
+ assert calls["ddgs"] == 1
+
+
def test_max_results_capped_at_10(monkeypatch):
"""max_results is clamped to 10."""
seen = {}