Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(search): record control-error searches as failed; race-free temp cleanup (#8130)

LearningCircuit98d6967191b7
diff --git a/changelog.d/8126.bugfix.md b/changelog.d/8126.bugfix.md
new file mode 100644
index 0000000000..d903fa7a36
--- /dev/null
+++ b/changelog.d/8126.bugfix.md
@@ -0,0 +1 @@
+A search stopped by the egress policy or by an unreadable setting while it runs is now recorded in the search metrics as a failed search, with the exception class as its error type, instead of as a successful one (the engine-level scope check before a search starts still writes no metrics row). The exception still propagates unchanged. Removing a search's temporary result attributes no longer raises `AttributeError` when two searches on the same engine instance finish at the same time. Fixes #8126.
diff --git a/src/local_deep_research/web_search_engines/search_engine_base.py b/src/local_deep_research/web_search_engines/search_engine_base.py
index d68698bb6f..e77ac2b79b 100644
--- a/src/local_deep_research/web_search_engines/search_engine_base.py
+++ b/src/local_deep_research/web_search_engines/search_engine_base.py
@@ -1559,10 +1559,14 @@ class BaseSearchEngine(ABC):
except Exception as e:
# Includes the last RateLimitError once retries are exhausted:
# the decorator re-raises it (reraise=True), never RetryError.
- self._record_search_failure(e)
+ # Mark the outcome first: ``_record_search_failure`` re-raises
+ # control errors (egress policy denial, unreadable setting) and
+ # the metrics row written below must still record a failure.
success = False
error_message = safe_msg = self._scrub_error(e)
error_type = type(e).__name__
+ results_count = 0
+ self._record_search_failure(e)
logger.warning(
f"Search engine {self.__class__.__name__} error: {safe_msg}"
)
@@ -1582,10 +1586,13 @@ class BaseSearchEngine(ABC):
error_type=error_type,
)
finally:
- # Clean up temporary search result storage
+ # Clean up temporary search result storage. One dict pop:
+ # a check-then-delete raises AttributeError when another
+ # search on this instance removes the attribute in between.
+ # Temp attributes are plain instance attributes (no slots
+ # or properties).
for attr in self._temp_attributes():
- if hasattr(self, attr):
- delattr(self, attr)
+ self.__dict__.pop(attr, None)
# ALWAYS clean up search context, even if recording fails
if context_was_set:
clear_search_context()
diff --git a/tests/web_search_engines/test_search_engine_base_run_outcome.py b/tests/web_search_engines/test_search_engine_base_run_outcome.py
new file mode 100644
index 0000000000..b36d5164c3
--- /dev/null
+++ b/tests/web_search_engines/test_search_engine_base_run_outcome.py
@@ -0,0 +1,182 @@
+"""Outcome bookkeeping in ``BaseSearchEngine.run()`` (issue #8126).
+
+Two behaviours of the shared search boundary:
+
+* A control error (egress policy denial, unreadable setting) still
+ propagates unchanged, but the ``SearchTracker`` row written in the
+ ``finally`` block records the search as failed with the exception class
+ as ``error_type``. Research termination is not a search failure and is
+ not recorded as one.
+* The per-search temporary attributes are removed without a
+ check-then-delete, so a search whose attribute was already removed by
+ another search on the same instance does not raise from ``run()``.
+"""
+
+from unittest.mock import Mock, patch
+
+import pytest
+
+from local_deep_research.exceptions import ResearchTerminatedException
+from local_deep_research.security.egress.policy import (
+ Decision,
+ PolicyDeniedError,
+)
+from local_deep_research.settings.exceptions import SettingsValueError
+from local_deep_research.web_search_engines.search_engine_base import (
+ BaseSearchEngine,
+)
+
+RECORD_SEARCH = (
+ "local_deep_research.metrics.search_tracker.SearchTracker.record_search"
+)
+
+PREVIEW = {"title": "T", "snippet": "S", "url": "https://example.com/a"}
+
+
+class _Engine(BaseSearchEngine):
+ """Raises ``preview_error`` or ``content_error`` when set."""
+
+ preview_error: BaseException | None = None
+ content_error: BaseException | None = None
+
+ def _get_previews(self, query):
+ self._search_results = [PREVIEW]
+ if self.preview_error is not None:
+ raise self.preview_error
+ return [dict(PREVIEW)]
+
+ def _get_full_content(self, relevant_items):
+ if self.content_error is not None:
+ raise self.content_error
+ return relevant_items
+
+
+def _tracked_engine(rate_limited=False, **errors):
+ engine = _Engine(programmatic_mode=True, search_snippets_only=False)
+ # Construct in programmatic mode (no global tracker), then enable the
+ # SearchTracker path, which run() takes only outside programmatic mode.
+ engine.programmatic_mode = False
+ engine.rate_tracker = Mock(enabled=rate_limited)
+ engine.rate_tracker.get_wait_time.return_value = 0
+ engine._verify_egress_scope = Mock()
+ for name, error in errors.items():
+ setattr(engine, name, error)
+ return engine
+
+
+def _policy_denied():
+ return PolicyDeniedError(
+ Decision(allowed=False, reason="scope_mismatch"), target="test"
+ )
+
+
+def _settings_error():
+ return SettingsValueError(
+ "search.engine.test.api_key", source="snapshot", reason="invalid"
+ )
+
+
[email protected]("rate_limited", [False, True])
[email protected]("stage", ["preview_error", "content_error"])
+ "make_error, error_type",
+ [
+ (_policy_denied, "PolicyDeniedError"),
+ (_settings_error, "SettingsValueError"),
+ ],
+)
+def test_control_error_propagates_and_is_recorded_as_failure(
+ make_error, error_type, stage, rate_limited
+):
+ error = make_error()
+ engine = _tracked_engine(rate_limited=rate_limited, **{stage: error})
+
+ with patch(RECORD_SEARCH) as record_search:
+ with pytest.raises(type(error)) as raised:
+ engine.run("query")
+
+ # The exception itself is unchanged.
+ assert raised.value is error
+ record_search.assert_called_once()
+ recorded = record_search.call_args.kwargs
+ assert recorded["success"] is False
+ assert recorded["error_type"] == error_type
+ assert recorded["error_message"]
+ assert recorded["results_count"] == 0
+ # The cleanup in the same finally block still ran.
+ assert "_search_results" not in vars(engine)
+
+
+def test_successful_search_is_still_recorded_as_success():
+ """Positive control for the parametrised failure test above."""
+ engine = _tracked_engine()
+
+ with patch(RECORD_SEARCH) as record_search:
+ results = engine.run("query")
+
+ assert len(results) == 1
+ recorded = record_search.call_args.kwargs
+ assert recorded["success"] is True
+ assert recorded["error_type"] is None
+ assert recorded["results_count"] == 1
+
+
+def test_research_termination_is_not_recorded_as_a_search_failure():
+ """Termination is a user stop, not an engine failure.
+
+ ``ResearchTerminatedException`` derives from ``BaseException`` and
+ bypasses the failure handling, so engine error counts are unaffected.
+ """
+ error = ResearchTerminatedException("stopped")
+ engine = _tracked_engine(preview_error=error)
+
+ with patch(RECORD_SEARCH) as record_search:
+ with pytest.raises(ResearchTerminatedException) as raised:
+ engine.run("query")
+
+ assert raised.value is error
+ recorded = record_search.call_args.kwargs
+ assert recorded["success"] is True
+ assert recorded["error_type"] is None
+
+
+class _ConcurrentlyCleanedEngine(_Engine):
+ """Another search on this instance removes ``_search_results`` the
+ moment this search has seen it, i.e. between a presence check and the
+ delete that follows it."""
+
+ def __getattribute__(self, name):
+ value = super().__getattribute__(name)
+ if name == "_search_results":
+ vars(self).pop(name, None)
+ return value
+
+
+def test_cleanup_tolerates_temp_attribute_removed_concurrently():
+ engine = _ConcurrentlyCleanedEngine(programmatic_mode=True)
+ engine.rate_tracker = Mock(enabled=False)
+ engine._verify_egress_scope = Mock()
+
+ results = engine.run("query")
+
+ assert len(results) == 1
+ assert "_search_results" not in vars(engine)
+
+
+def test_cleanup_removes_every_temp_attribute():
+ class _TwoAttrs(_Engine):
+ def _get_previews(self, query):
+ self._extra = "x"
+ return super()._get_previews(query)
+
+ def _temp_attributes(self):
+ return super()._temp_attributes() + ["_extra", "_never_set"]
+
+ engine = _TwoAttrs(programmatic_mode=True)
+ engine.rate_tracker = Mock(enabled=False)
+ engine._verify_egress_scope = Mock()
+
+ engine.run("query")
+
+ assert "_search_results" not in vars(engine)
+ assert "_extra" not in vars(engine)