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: add sentinel guard in download_service and replace .exists() with .is_file() (#2646)

LearningCircuitc62f9aa09694
diff --git a/src/local_deep_research/research_library/deletion/services/document_deletion.py b/src/local_deep_research/research_library/deletion/services/document_deletion.py
index 1d649f1025..1f37bd5776 100644
--- a/src/local_deep_research/research_library/deletion/services/document_deletion.py
+++ b/src/local_deep_research/research_library/deletion/services/document_deletion.py
@@ -203,7 +203,7 @@ class DocumentDeletionService:
file_path = get_absolute_path_from_settings(
document.file_path
)
- if file_path and file_path.exists():
+ if file_path and file_path.is_file():
result["bytes_freed"] = file_path.stat().st_size
CascadeHelper.delete_filesystem_file(
str(file_path)
diff --git a/src/local_deep_research/research_library/deletion/utils/cascade_helper.py b/src/local_deep_research/research_library/deletion/utils/cascade_helper.py
index 3bc981f6b2..8693db822c 100644
--- a/src/local_deep_research/research_library/deletion/utils/cascade_helper.py
+++ b/src/local_deep_research/research_library/deletion/utils/cascade_helper.py
@@ -157,7 +157,7 @@ class CascadeHelper:
try:
path = Path(file_path)
- if path.exists():
+ if path.is_file():
path.unlink()
logger.debug(f"Deleted filesystem file: {file_path}")
return True
@@ -187,14 +187,14 @@ class CascadeHelper:
# FAISS index file
faiss_file = path.with_suffix(".faiss")
- if faiss_file.exists():
+ if faiss_file.is_file():
faiss_file.unlink()
logger.debug(f"Deleted FAISS index file: {faiss_file}")
deleted_any = True
# Pickle file for metadata
pkl_file = path.with_suffix(".pkl")
- if pkl_file.exists():
+ if pkl_file.is_file():
pkl_file.unlink()
logger.debug(f"Deleted FAISS pkl file: {pkl_file}")
deleted_any = True
diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.py
index dfb1670dce..fc121d13de 100644
--- a/src/local_deep_research/research_library/services/download_service.py
+++ b/src/local_deep_research/research_library/services/download_service.py
@@ -23,7 +23,7 @@ from sqlalchemy.orm import Session
import pdfplumber
from pypdf import PdfReader
-from ...constants import FILE_PATH_TEXT_ONLY
+from ...constants import FILE_PATH_SENTINELS, FILE_PATH_TEXT_ONLY
from ...database.models.download_tracker import (
DownloadAttempt,
DownloadTracker,
@@ -1012,7 +1012,10 @@ class DownloadService:
return None
# Validate path to prevent path traversal attacks
- if not pdf_document.file_path:
+ if (
+ not pdf_document.file_path
+ or pdf_document.file_path in FILE_PATH_SENTINELS
+ ):
return None
try:
safe_path = PathValidator.validate_safe_path(
@@ -1024,7 +1027,7 @@ class DownloadService:
f"Path traversal blocked: {pdf_document.file_path} - {e}"
)
return None
- if not pdf_path.exists():
+ if not pdf_path.is_file():
return None
logger.info(f"Found existing PDF, extracting text from: {pdf_path}")
diff --git a/src/local_deep_research/research_library/services/library_service.py b/src/local_deep_research/research_library/services/library_service.py
index 2bebae94de..f9ceac7c4c 100644
--- a/src/local_deep_research/research_library/services/library_service.py
+++ b/src/local_deep_research/research_library/services/library_service.py
@@ -710,7 +710,7 @@ class LibraryService:
file_path = get_absolute_path_from_settings(
tracker.file_path
)
- if file_path and file_path.exists():
+ if file_path and file_path.is_file():
file_path.unlink()
logger.info(f"Deleted file: {file_path}")
except Exception:
@@ -754,7 +754,7 @@ class LibraryService:
validated_path = PathValidator.validate_safe_path(
tracker.file_path, library_root, allow_absolute=False
)
- if validated_path and validated_path.exists():
+ if validated_path and validated_path.is_file():
return open_file_location(str(validated_path))
except ValueError as e:
logger.warning(f"Path validation failed: {e}")
@@ -868,7 +868,7 @@ class LibraryService:
file_path = get_absolute_path_from_settings(
tracker.file_path
)
- if file_path and file_path.exists():
+ if file_path and file_path.is_file():
stats["files_found"] += 1
else:
# File missing or path invalid - mark for re-download
diff --git a/tests/research_library/deletion/utils/test_cascade_helper_edge_cases.py b/tests/research_library/deletion/utils/test_cascade_helper_edge_cases.py
index 1343f5835e..c2311861a4 100644
--- a/tests/research_library/deletion/utils/test_cascade_helper_edge_cases.py
+++ b/tests/research_library/deletion/utils/test_cascade_helper_edge_cases.py
@@ -52,7 +52,7 @@ class TestDeleteFilesystemFile:
def test_returns_false_on_unlink_exception(self, mock_path_cls):
"""Returns False when unlink raises OSError."""
mock_path = MagicMock()
- mock_path.exists.return_value = True
+ mock_path.is_file.return_value = True
mock_path.unlink.side_effect = OSError("permission denied")
mock_path_cls.return_value = mock_path
assert CascadeHelper.delete_filesystem_file("/some/path") is False
diff --git a/tests/research_library/services/test_library_service_collections_stats.py b/tests/research_library/services/test_library_service_collections_stats.py
index 57df7ef66e..67121120f8 100644
--- a/tests/research_library/services/test_library_service_collections_stats.py
+++ b/tests/research_library/services/test_library_service_collections_stats.py
@@ -503,7 +503,7 @@ class TestOpenFileLocation:
_mock_session_cm(mocker, mock_session)
mock_validated_path = MagicMock()
- mock_validated_path.exists.return_value = True
+ mock_validated_path.is_file.return_value = True
mocker.patch(
"local_deep_research.research_library.services.library_service.get_absolute_path_from_settings",
diff --git a/tests/research_library/services/test_library_service_crud_deep.py b/tests/research_library/services/test_library_service_crud_deep.py
index 3d203c699a..3ab32f4209 100644
--- a/tests/research_library/services/test_library_service_crud_deep.py
+++ b/tests/research_library/services/test_library_service_crud_deep.py
@@ -76,7 +76,7 @@ class TestDeleteDocumentHappyPaths:
# Mock file path resolution
mock_path = MagicMock()
- mock_path.exists.return_value = True
+ mock_path.is_file.return_value = True
mocker.patch(
"local_deep_research.research_library.services.library_service.get_absolute_path_from_settings",
return_value=mock_path,
@@ -139,7 +139,7 @@ class TestDeleteDocumentHappyPaths:
_mock_session_cm(mocker, mock_session)
mock_path = MagicMock()
- mock_path.exists.return_value = False
+ mock_path.is_file.return_value = False
mocker.patch(
"local_deep_research.research_library.services.library_service.get_absolute_path_from_settings",
return_value=mock_path,
@@ -215,7 +215,7 @@ class TestDeleteDocumentHappyPaths:
_mock_session_cm(mocker, mock_session)
mock_path = MagicMock()
- mock_path.exists.return_value = True
+ mock_path.is_file.return_value = True
mock_path.unlink.side_effect = OSError("Permission denied")
mocker.patch(
"local_deep_research.research_library.services.library_service.get_absolute_path_from_settings",
@@ -257,7 +257,7 @@ class TestDeleteDocumentHappyPaths:
_mock_session_cm(mocker, mock_session)
mock_path = MagicMock()
- mock_path.exists.return_value = False
+ mock_path.is_file.return_value = False
mocker.patch(
"local_deep_research.research_library.services.library_service.get_absolute_path_from_settings",
return_value=mock_path,
@@ -564,7 +564,7 @@ class TestSyncLibraryWithFilesystem:
_mock_session_cm(mocker, mock_session)
mock_path = MagicMock()
- mock_path.exists.return_value = True
+ mock_path.is_file.return_value = True
mocker.patch(
"local_deep_research.research_library.services.library_service.get_absolute_path_from_settings",
return_value=mock_path,
@@ -603,7 +603,7 @@ class TestSyncLibraryWithFilesystem:
_mock_session_cm(mocker, mock_session)
mock_path = MagicMock()
- mock_path.exists.return_value = False
+ mock_path.is_file.return_value = False
mocker.patch(
"local_deep_research.research_library.services.library_service.get_absolute_path_from_settings",
return_value=mock_path,
@@ -686,9 +686,9 @@ class TestSyncLibraryWithFilesystem:
# First 2 files exist, third missing
paths = [MagicMock(), MagicMock(), MagicMock()]
- paths[0].exists.return_value = True
- paths[1].exists.return_value = True
- paths[2].exists.return_value = False
+ paths[0].is_file.return_value = True
+ paths[1].is_file.return_value = True
+ paths[2].is_file.return_value = False
path_call_count = {"n": 0}