From 78469f388a108447ac4c37f2d79538e27e7bf7b3 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Fri, 14 Aug 2026 18:46:43 -0700 Subject: [PATCH] Fall back from unavailable default Sonic search --- archivebox/search/query.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/archivebox/search/query.py b/archivebox/search/query.py index fee1731f..5761de83 100644 --- a/archivebox/search/query.py +++ b/archivebox/search/query.py @@ -232,12 +232,21 @@ def iter_query_search_ids( backends = get_available_backends() configured_backend = normalize_search_backend_name(config.SEARCH_BACKEND_ENGINE) + fallback_enabled = search_mode_base == "deep" and (not forced_backend or forced_backend == configured_backend == "sonic") if forced_backend: if forced_backend not in backends: raise RuntimeError( f'Search backend "{forced_backend}" not found. Available backends: {list(backends) or "none"}', ) - backend_names = [forced_backend] + backend_names = ( + [ + forced_backend, + *(name for name in backends if name not in {forced_backend, "ripgrep"}), + *(["ripgrep"] if "ripgrep" in backends and forced_backend != "ripgrep" else []), + ] + if fallback_enabled + else [forced_backend] + ) elif search_mode_base == "deep": backend_names = [ *([configured_backend] if configured_backend in backends and configured_backend != "ripgrep" else []), @@ -252,18 +261,6 @@ def iter_query_search_ids( get_backend() return - if "sonic" in backend_names: - import sys - from contextlib import redirect_stdout - - from archivebox.core.takeover_util import ensure_daemon_stack - - # Search commands can produce structured stdout such as --csv/--json. - # Daemon bootstrap progress is diagnostic output and must not corrupt - # that user-facing data stream. - with redirect_stdout(sys.stderr): - ensure_daemon_stack(reason="search query") - errors: list[Exception] = [] successful_backends = 0 seen: set[str] = set() @@ -271,6 +268,16 @@ def iter_query_search_ids( for backend_name in backend_names: backend = backends[backend_name] try: + if backend_name == "sonic": + import sys + from contextlib import redirect_stdout + + from archivebox.core.takeover_util import ensure_daemon_stack + + # Search commands can produce structured stdout such as + # --csv/--json. Keep daemon diagnostics on stderr. + with redirect_stdout(sys.stderr): + ensure_daemon_stack(reason="search query") with search_backend_env(config=config): if backend_name == "ripgrep": ids = backend.iter_search(query, search_mode=search_mode_base) @@ -286,7 +293,7 @@ def iter_query_search_ids( successful_backends += 1 except Exception as err: errors.append(err) - if search_mode_base != "deep" or forced_backend: + if not fallback_enabled: raise except Exception as err: stderr()