Skip to content

Commit f5be923

Browse files
committed
refactor(xai): simplify _xai_prefers_native_web_search to use registry
Drop the manual web.search_backend / web.backend config-reading block that duplicated _read_config_key in web_search_registry.py. The function now delegates directly to get_active_search_provider() (which reads the same config keys via the registry's canonical resolver) and falls back to _get_search_backend() only when the registry has no providers loaded. Also updates the TestXaiWebSearchBackendPreference tests to monkeypatch the registry instead of load_config_readonly, and adds two new tests for the legacy fallback path (no provider registered -> _get_search_backend).
1 parent 29eba9c commit f5be923

2 files changed

Lines changed: 38 additions & 26 deletions

File tree

agent/transports/codex.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,14 @@ def _bounded_prompt_cache_key(value: Any) -> Optional[str]:
3939
def _xai_prefers_native_web_search() -> bool:
4040
"""True when xAI Responses should use Grok's native ``web_search`` built-in.
4141
42-
Honors explicit ``web.search_backend`` / ``web.backend`` first. Only falls
43-
back to native when the resolved active provider is ``xai`` (or resolution
44-
fails — preserve the incomplete-hang fix rather than risk reintroducing it).
42+
Delegates to the web-search registry's provider resolution (which reads
43+
``web.search_backend`` / ``web.backend`` from config) and checks whether
44+
the resolved provider is xAI. Falls back to the legacy ``_get_search_backend``
45+
probe when the registry has no providers loaded. On any resolution failure,
46+
returns True (fail-closed to native — preserves the #48108 incomplete-hang
47+
fix rather than risk reintroducing it).
4548
"""
4649
try:
47-
from hermes_cli.config import load_config_readonly
48-
49-
cfg = load_config_readonly() or {}
50-
web = cfg.get("web") if isinstance(cfg, dict) else None
51-
if isinstance(web, dict):
52-
explicit = (
53-
str(web.get("search_backend") or "").strip().lower()
54-
or str(web.get("backend") or "").strip().lower()
55-
)
56-
if explicit == "xai":
57-
return True
58-
if explicit:
59-
# User asked for Firecrawl / Tavily / etc. — keep Hermes dispatch.
60-
return False
61-
6250
from agent.web_search_registry import get_active_search_provider
6351

6452
provider = get_active_search_provider()
@@ -69,7 +57,7 @@ def _xai_prefers_native_web_search() -> bool:
6957

7058
return (_get_search_backend() or "").strip().lower() == "xai"
7159
except Exception:
72-
# Fail closed to native swap — same behavior as pre-fix main.
60+
# Fail closed to native — same behavior as pre-fix main.
7361
return True
7462

7563

tests/agent/transports/test_codex_transport.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,30 +438,54 @@ def test_explicit_firecrawl_prefers_client(self, monkeypatch):
438438
import agent.transports.codex as codex_mod
439439

440440
monkeypatch.setattr(
441-
"hermes_cli.config.load_config_readonly",
442-
lambda: {"web": {"backend": "firecrawl"}},
441+
"agent.web_search_registry.get_active_search_provider",
442+
lambda: SimpleNamespace(name="firecrawl"),
443443
)
444444
assert codex_mod._xai_prefers_native_web_search() is False
445445

446446
def test_explicit_search_backend_xai_prefers_native(self, monkeypatch):
447447
import agent.transports.codex as codex_mod
448448

449449
monkeypatch.setattr(
450-
"hermes_cli.config.load_config_readonly",
451-
lambda: {"web": {"search_backend": "xai"}},
450+
"agent.web_search_registry.get_active_search_provider",
451+
lambda: SimpleNamespace(name="xai"),
452452
)
453453
assert codex_mod._xai_prefers_native_web_search() is True
454454

455455
def test_resolved_non_xai_provider_prefers_client(self, monkeypatch):
456456
import agent.transports.codex as codex_mod
457457

458458
monkeypatch.setattr(
459-
"hermes_cli.config.load_config_readonly",
460-
lambda: {"web": {}},
459+
"agent.web_search_registry.get_active_search_provider",
460+
lambda: SimpleNamespace(name="firecrawl"),
461461
)
462+
assert codex_mod._xai_prefers_native_web_search() is False
463+
464+
def test_no_provider_legacy_fallback_xai(self, monkeypatch):
465+
"""When no provider is registered, fall back to _get_search_backend."""
466+
import agent.transports.codex as codex_mod
467+
462468
monkeypatch.setattr(
463469
"agent.web_search_registry.get_active_search_provider",
464-
lambda: SimpleNamespace(name="firecrawl"),
470+
lambda: None,
471+
)
472+
monkeypatch.setattr(
473+
"tools.web_tools._get_search_backend",
474+
lambda: "xai",
475+
)
476+
assert codex_mod._xai_prefers_native_web_search() is True
477+
478+
def test_no_provider_legacy_fallback_non_xai(self, monkeypatch):
479+
"""When no provider is registered and backend isn't xai, keep client."""
480+
import agent.transports.codex as codex_mod
481+
482+
monkeypatch.setattr(
483+
"agent.web_search_registry.get_active_search_provider",
484+
lambda: None,
485+
)
486+
monkeypatch.setattr(
487+
"tools.web_tools._get_search_backend",
488+
lambda: "firecrawl",
465489
)
466490
assert codex_mod._xai_prefers_native_web_search() is False
467491

0 commit comments

Comments
 (0)