~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(scheduler): gate resource prefetch on extract_text and contain prefetch failures (#8091)
Sebastian Cao6e2430884cc0
diff --git a/changelog.d/8091.bugfix.md b/changelog.d/8091.bugfix.mdnew file mode 100644index 0000000000..9d9213a406--- /dev/null+++ b/changelog.d/8091.bugfix.md@@ -0,0 +1 @@+The document scheduler no longer queries research resources when text extraction is disabled, and a failed resource-prefetch query (e.g. a locked database) for one research no longer blocks PDF download queueing for every research or the scheduler's `last_run` update.diff --git a/src/local_deep_research/scheduler/background.py b/src/local_deep_research/scheduler/background.pyindex dd338c08d2..ff87c75f9d 100644--- a/src/local_deep_research/scheduler/background.py+++ b/src/local_deep_research/scheduler/background.py@@ -1373,18 +1373,39 @@ class BackgroundJobScheduler:# Materialise each research's downloadable resources as plain# ``(id, url)`` tuples. ``research_id`` / ``url`` are selected# as columns (not ORM entities) and filtered here, so the loop- # below needs no database access at all.- for research in research_to_process:- resource_rows = (- db.query(ResearchResource.id, ResearchResource.url)- .filter_by(research_id=research["id"])- .all()- )- resources_by_research[research["id"]] = [- (row.id, row.url)- for row in resource_rows- if is_downloadable_url(row.url)- ]+ # below needs no database access at all. Only the extract_text+ # pass reads these, so skip the queries entirely when it is+ # off (#7525).+ if settings.extract_text:+ for research in research_to_process:+ try:+ resource_rows = (+ db.query(+ ResearchResource.id,+ ResearchResource.url,+ )+ .filter_by(research_id=research["id"])+ .all()+ )+ resources_by_research[research["id"]] = [+ (row.id, row.url)+ for row in resource_rows+ if is_downloadable_url(row.url)+ ]+ except Exception as e:+ # A failed prefetch (e.g. locked DB) must not+ # abort the whole pass: download_pdfs queueing+ # and the last_run update still apply, and the+ # stored empty list means the extract_text+ # pass simply has nothing to do here.+ # Exception text can carry the SQLCipher+ # master password, so log its type only+ # (#7525).+ logger.warning(+ f"[DOC_SCHEDULER] Failed to prefetch resources for research {research['id']}: "+ f"{type(e).__name__}"+ )+ resources_by_research[research["id"]] = []# ---- read session closed; loop below holds no SQLite transaction ----diff --git a/tests/research_library/services/test_download_service_3827_fix.py b/tests/research_library/services/test_download_service_3827_fix.pyindex ad96ba15f9..7ce846a849 100644--- a/tests/research_library/services/test_download_service_3827_fix.py+++ b/tests/research_library/services/test_download_service_3827_fix.py@@ -902,15 +902,29 @@ def test_process_user_documents_closes_read_session_before_downloading():)-def test_process_user_documents_resource_prefetch_failure_stays_fail_closed():- """A database failure while materialising the resource list must not- advance ``last_run``.-- ``last_run`` advancing on a failed pass would silently skip that- research on every future tick. This pins the existing fail-closed- policy (see the ``Intentionally NOT wrapped in try/finally`` comment in- the implementation) for the prefetch failure path introduced by the- scoped-session refactor.+# ---------------------------------------------------------------------------+# Issue #7525: the resource prefetch introduced by the scoped-session refactor+# (#6720) ran even with extract_text off, and a prefetch query failure escaped+# the read session — skipping download_pdfs queueing for every research and+# the last_run update. The prefetch is now gated on extract_text and contained+# per research (log the exception type, store an empty list).+# ---------------------------------------------------------------------------+++def test_process_user_documents_resource_prefetch_failure_is_contained():+ """A database failure while materialising one research's resource list+ must stay contained to that research's extract_text work (#7525).++ Before the fix the prefetch had no try/except, so one failed query+ (e.g. a locked DB) escaped the read session, skipped ``download_pdfs``+ queueing for EVERY research and left ``last_run`` unset. This test+ replaces the previous fail-closed pin (prefetch failure must not+ advance ``last_run``) per #7525: the failure is+ now logged (type name only), the research gets an empty resource list,+ PDF queueing still runs for every research, and ``last_run`` advances.+ The fail-closed policy for UPSTREAM setup failures (DB open, settings+ manager init, the initial research query — see the ``Intentionally NOT+ wrapped in try/finally`` comment in the implementation) is unchanged."""from datetime import datetime, UTC@@ -930,12 +944,131 @@ def test_process_user_documents_resource_prefetch_failure_stays_fail_closed():scheduler._credential_store.store("testuser", "testpass")settings = DocumentSchedulerSettings(- download_pdfs=False,+ download_pdfs=True,extract_text=True,generate_rag=False,last_run="",)+ research_rows = [+ MagicMock(id="research-a", title="A", completed_at=None),+ MagicMock(id="research-b", title="B", completed_at=None),+ ]++ db = MagicMock()+ research_query = MagicMock()+ research_query.filter.return_value = research_query+ research_query.order_by.return_value = research_query+ research_query.limit.return_value = research_query+ research_query.all.return_value = research_rows+ # Both per-research ResearchResource queries raise: the failure must be+ # caught per research instead of escaping the read session.+ db.query.side_effect = [+ research_query,+ RuntimeError("simulated DB query failure"),+ RuntimeError("simulated DB query failure"),+ ]++ from contextlib import contextmanager as _cm++ @_cm+ def fake_get_user_db_session(*a, **kw):+ yield db++ download_service = MagicMock()+ download_service.__enter__ = MagicMock(return_value=download_service)+ download_service.__exit__ = MagicMock(return_value=False)+ download_service.queue_research_downloads.return_value = 0++ settings_manager = MagicMock()+ settings_manager.get_settings_snapshot.return_value = {+ "search.tool": "searxng"+ }++ with (+ patch.object(+ scheduler,+ "_get_document_scheduler_settings",+ return_value=settings,+ ),+ patch(+ "local_deep_research.database.session_context.get_user_db_session",+ side_effect=fake_get_user_db_session,+ ),+ patch(+ "local_deep_research.settings.manager.SettingsManager",+ return_value=settings_manager,+ ),+ patch(+ "local_deep_research.research_library.services.download_service.DownloadService",+ return_value=download_service,+ ),+ ):+ scheduler._process_user_documents("testuser")++ # The first research's failure did not stop the second's prefetch from+ # being attempted: ResearchHistory + one ResearchResource query each.+ assert db.query.call_count == 3, (+ "A prefetch failure must not abort the prefetch loop; "+ f"saw {db.query.call_count} queries."+ )++ # PDF queueing still ran for EVERY research.+ queued_ids = [+ call.args[0]+ for call in download_service.queue_research_downloads.call_args_list+ ]+ assert queued_ids == ["research-a", "research-b"], (+ "A prefetch failure must not block download_pdfs queueing; "+ f"queued {queued_ids!r}."+ )++ # The failed researches have no materialised resources, so the+ # extract_text pass had nothing to do.+ download_service.download_as_text.assert_not_called()++ # last_run advances: the pass completed, so the next tick must not+ # re-process these researches forever.+ last_run_calls = [+ call+ for call in settings_manager.set_setting.call_args_list+ if call.args and call.args[0] == "document_scheduler.last_run"+ ]+ assert last_run_calls, (+ "last_run must advance when the pass completes despite a contained "+ "prefetch failure."+ )+++def test_process_user_documents_extract_text_off_skips_resource_prefetch():+ """With ``extract_text`` off, no ResearchResource query runs at all —+ only the extract_text pass reads the prefetched lists — and PDF+ queueing proceeds as normal (#7525).+ """+ from datetime import datetime, UTC++ from local_deep_research.scheduler.background import (+ BackgroundJobScheduler,+ DocumentSchedulerSettings,+ )++ BackgroundJobScheduler._instance = None+ with patch("local_deep_research.scheduler.background.BackgroundScheduler"):+ scheduler = BackgroundJobScheduler()++ scheduler.user_sessions["testuser"] = {+ "scheduled_jobs": set(),+ "last_activity": datetime.now(UTC),+ }+ scheduler._credential_store.store("testuser", "testpass")++ settings = DocumentSchedulerSettings(+ download_pdfs=True,+ extract_text=False,+ generate_rag=False,+ last_run="",+ )+research_row = MagicMock(id="research-x", title="T", completed_at=None)db = MagicMock()@@ -944,10 +1077,10 @@ def test_process_user_documents_resource_prefetch_failure_stays_fail_closed():research_query.order_by.return_value = research_queryresearch_query.limit.return_value = research_queryresearch_query.all.return_value = [research_row]- # The second query (ResearchResource columns) raises, so the failure- # escapes the read session and lands in the method-level except.- resource_query_failure = RuntimeError("simulated DB query failure")- db.query.side_effect = [research_query, resource_query_failure]+ # Only the ResearchHistory query may run. A second db.query call (the+ # prefetch) would exhaust this side_effect and fail the pass, exactly+ # as the ungated prefetch did before the fix.+ db.query.side_effect = [research_query]from contextlib import contextmanager as _cm@@ -958,6 +1091,7 @@ def test_process_user_documents_resource_prefetch_failure_stays_fail_closed():download_service = MagicMock()download_service.__enter__ = MagicMock(return_value=download_service)download_service.__exit__ = MagicMock(return_value=False)+ download_service.queue_research_downloads.return_value = 0settings_manager = MagicMock()settings_manager.get_settings_snapshot.return_value = {@@ -983,22 +1117,28 @@ def test_process_user_documents_resource_prefetch_failure_stays_fail_closed():return_value=download_service,),):- # Must not raise: the method-level except contains the failure.scheduler._process_user_documents("testuser")- # No download was attempted because the resource list never materialised.+ # No ResearchResource prefetch ran with extract_text off.+ assert db.query.call_count == 1, (+ "extract_text=False must skip the resource prefetch; "+ f"saw {db.query.call_count} queries."+ )++ # Downloads were still queued, and no text extraction was attempted.+ download_service.queue_research_downloads.assert_called_once_with(+ "research-x"+ )download_service.download_as_text.assert_not_called()- # Fail closed: last_run must NOT advance, so the next tick retries this- # user rather than silently skipping the research forever.+ # The pass completed, so last_run advances.last_run_calls = [callfor call in settings_manager.set_setting.call_args_listif call.args and call.args[0] == "document_scheduler.last_run"]- assert not last_run_calls, (- "last_run must not advance when the resource prefetch failed; "- f"saw {last_run_calls!r}."+ assert last_run_calls, (+ "last_run must advance on a completed extract_text=False pass.")