~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(library): stream get_unique_domains scan to bound memory (#4560) (#4720)
LearningCircuit71b568fb9b28
diff --git a/changelog.d/+bound-unique-domains-scan.bugfix.md b/changelog.d/+bound-unique-domains-scan.bugfix.mdnew file mode 100644index 0000000000..dfe84b483c--- /dev/null+++ b/changelog.d/+bound-unique-domains-scan.bugfix.md@@ -0,0 +1 @@+The library domain-filter scan (`get_unique_domains`) now streams document URLs in batches instead of loading every row at once, avoiding excessive memory use on very large libraries (#4560).diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.pyindex 6a712059f3..8e0e5a1e2b 100644--- a/src/local_deep_research/research_library/services/library_service.py+++ b/src/local_deep_research/research_library/services/library_service.py@@ -44,6 +44,12 @@ from ..utils import (# comprehensive large-scale filtering is a server-side-search follow-up._DROPDOWN_RESEARCH_LIMIT = 1000+# ``get_unique_domains`` scans one URL row per downloaded document to build the+# domain filter. Stream it in batches (``yield_per``) rather than materializing+# every row at once, so a very large library cannot exhaust memory (#4560). The+# distinct-domain set it accumulates is small regardless of library size.+_DOMAIN_SCAN_BATCH_SIZE = 1000+class LibraryService:"""Service for managing and querying the document library."""@@ -1078,15 +1084,25 @@ class LibraryService:return Falsedef get_unique_domains(self) -> List[str]:- """Get sorted list of unique netlocs from all document URLs."""+ """Get sorted list of unique netlocs from all document URLs.++ Streams the URL column in batches (``yield_per``) and accumulates+ netlocs into a set, so a very large library is never fully+ materialized in memory at once (#4560). The query already projects to+ the single ``original_url`` column.+ """with get_user_db_session(self.username) as session:- urls = (+ netlocs = set()+ rows = (session.query(Document.original_url).filter(Document.original_url.isnot(None))- .all()+ .yield_per(_DOMAIN_SCAN_BATCH_SIZE))- netlocs = {self._extract_domain(row[0]) for row in urls}- return sorted(n for n in netlocs if n)+ for (url,) in rows:+ domain = self._extract_domain(url)+ if domain:+ netlocs.add(domain)+ return sorted(netlocs)def _extract_domain(self, url: str) -> str:"""Extract domain from URL."""diff --git a/tests/research_library/services/test_library_service.py b/tests/research_library/services/test_library_service.pyindex 9078062fdf..cc57bb355c 100644--- a/tests/research_library/services/test_library_service.py+++ b/tests/research_library/services/test_library_service.py@@ -322,7 +322,7 @@ class TestLibraryServiceGetUniqueDomains:mock_session.__enter__ = Mock(return_value=mock_session)mock_session.__exit__ = Mock(return_value=False)- mock_session.query.return_value.filter.return_value.all.return_value = [+ mock_session.query.return_value.filter.return_value.yield_per.return_value = [("https://arxiv.org/abs/1234",),("https://nature.com/paper1",),]diff --git a/tests/research_library/services/test_library_service_collections_stats.py b/tests/research_library/services/test_library_service_collections_stats.pyindex 25aa3c21d9..b48c98121b 100644--- a/tests/research_library/services/test_library_service_collections_stats.py+++ b/tests/research_library/services/test_library_service_collections_stats.py@@ -166,7 +166,7 @@ class TestGetUniqueDomainsEdgeCases:"""None URLs are skipped when extracting netlocs."""service = _make_service()mock_session = MagicMock()- mock_session.query.return_value.filter.return_value.all.return_value = [+ mock_session.query.return_value.filter.return_value.yield_per.return_value = [(None,),("https://nature.com/paper",),]@@ -180,7 +180,7 @@ class TestGetUniqueDomainsEdgeCases:"""No documents → empty domain list."""service = _make_service()mock_session = MagicMock()- mock_session.query.return_value.filter.return_value.all.return_value = []+ mock_session.query.return_value.filter.return_value.yield_per.return_value = []_mock_session_cm(mocker, mock_session)result = service.get_unique_domains()@@ -191,7 +191,7 @@ class TestGetUniqueDomainsEdgeCases:"""Multiple URLs with same netloc deduplicated; result is sorted."""service = _make_service()mock_session = MagicMock()- mock_session.query.return_value.filter.return_value.all.return_value = [+ mock_session.query.return_value.filter.return_value.yield_per.return_value = [("https://nature.com/a",),("https://nature.com/b",),("https://arxiv.org/abs/1234",),@@ -203,6 +203,25 @@ class TestGetUniqueDomainsEdgeCases:assert result == ["arxiv.org", "nature.com", "sciencedirect.com"]+ def test_scans_urls_in_bounded_batches(self, mocker):+ """The URL scan streams via ``yield_per`` rather than ``.all()`` so a+ very large library is never fully materialized (#4560). Fails if the+ query reverts to an unbounded load."""+ from local_deep_research.research_library.services.library_service import (+ _DOMAIN_SCAN_BATCH_SIZE,+ )++ service = _make_service()+ mock_session = MagicMock()+ mock_filter = mock_session.query.return_value.filter.return_value+ mock_filter.yield_per.return_value = []+ _mock_session_cm(mocker, mock_session)++ service.get_unique_domains()++ mock_filter.yield_per.assert_called_once_with(_DOMAIN_SCAN_BATCH_SIZE)+ mock_filter.all.assert_not_called()+# ============== get_research_list_for_dropdown ==============