Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

Preserve permission denied errors in ingest paths

jinzhenj0d1c15ceac92
diff --git a/mineru/doclib/background/ingest.py b/mineru/doclib/background/ingest.py
index 34e8f8d7..f601d9c2 100644
--- a/mineru/doclib/background/ingest.py
+++ b/mineru/doclib/background/ingest.py
@@ -7,6 +7,7 @@ import logging
import time
from typing import cast
+from ...errors import MineruError
from ..rows import CountRow, FileRow, IngestTaskRow
from ..services.parse_svc import ParseService
from ..types import FILE_STATUS_ACTIVE
@@ -107,7 +108,11 @@ class IngestWorkerPool:
return
error_code = "ingest_failed"
- if isinstance(exc, PermissionError):
+ error_msg = str(exc)[:500]
+ if isinstance(exc, MineruError):
+ error_code = exc.code
+ error_msg = (exc.message or str(exc))[:500]
+ elif isinstance(exc, PermissionError):
error_code = "file_permission_denied"
elif isinstance(exc, OSError):
error_code = "stat_failed"
@@ -115,7 +120,7 @@ class IngestWorkerPool:
now = int(time.time() * 1000)
await self.parse_svc.db.execute(
"UPDATE files SET sha256=NULL, locked_at=NULL, error_code=?, error_msg=?, updated_at=? WHERE id=?",
- (error_code, str(exc)[:500], now, task["id"]),
+ (error_code, error_msg, now, task["id"]),
)
except Exception:
pass
diff --git a/mineru/doclib/services/parse_svc.py b/mineru/doclib/services/parse_svc.py
index 843892ed..041e14a5 100644
--- a/mineru/doclib/services/parse_svc.py
+++ b/mineru/doclib/services/parse_svc.py
@@ -80,6 +80,8 @@ FileRefreshStatus = Literal["known", "new", "changed", "missing", "deleted", "un
class FileRefreshResult:
file: FileInfo | None
status: FileRefreshStatus
+ error_code: str | None = None
+ error_msg: str | None = None
@property
def needs_ingest(self) -> bool:
@@ -420,7 +422,7 @@ class ParseService:
async def _refresh_stat_error(self, existing: FileRow | None, error_code: str, error_msg: str) -> FileRefreshResult:
if existing is None:
- return FileRefreshResult(file=None, status="error")
+ return FileRefreshResult(file=None, status="error", error_code=error_code, error_msg=error_msg)
now = _now_ms()
await self.db.execute(
@@ -428,7 +430,7 @@ class ParseService:
(error_code, error_msg[:500], now, existing["id"]),
)
row = cast(FileRow | None, await self.db.fetchone("SELECT * FROM files WHERE id=?", (existing["id"],)))
- return FileRefreshResult(file=_file_info(row), status="error")
+ return FileRefreshResult(file=_file_info(row), status="error", error_code=error_code, error_msg=error_msg)
async def ensure_ingested(self, path: str, watch_id: int | None = None, *, allow_images: bool = False) -> FileRow | None:
"""Synchronously discover and ingest a source path when needed."""
@@ -672,8 +674,18 @@ class ParseService:
if refreshed.status in {"missing", "deleted", "unreachable"}:
raise InvalidRequestError("file_not_found", f"File {path} not found.", "path")
if refreshed.status == "error":
- code = refreshed.file.error_code if refreshed.file and refreshed.file.error_code else "ingest_failed"
- message = refreshed.file.error_msg if refreshed.file and refreshed.file.error_msg else "File could not be ingested."
+ code = (
+ (refreshed.file.error_code if refreshed.file and refreshed.file.error_code else None)
+ or refreshed.error_code
+ or "ingest_failed"
+ )
+ message = (
+ (refreshed.file.error_msg if refreshed.file and refreshed.file.error_msg else None)
+ or refreshed.error_msg
+ or "File could not be ingested."
+ )
+ if code == "file_permission_denied":
+ raise InvalidRequestError(code, message, "path")
raise MineruError(code, message, "path")
if refreshed.file is None:
raise MineruError("ingest_failed", "File could not be ingested.", "path")
diff --git a/tests/unittest/test_doclib_cache_semantics.py b/tests/unittest/test_doclib_cache_semantics.py
index c17d6399..93a160b1 100644
--- a/tests/unittest/test_doclib_cache_semantics.py
+++ b/tests/unittest/test_doclib_cache_semantics.py
@@ -1527,6 +1527,32 @@ def test_refresh_file_records_stat_error_without_marking_deleted(tmp_path: Path,
asyncio.run(_run())
+def test_request_parse_maps_stat_permission_error_without_existing_file_row(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+ await db.initialize()
+ service = ParseService(
+ db=db, fts=FTSManager(db), config_svc=None, data_dir=str(tmp_path / "data"), parse_lock_timeout_sec=1800
+ )
+ source = tmp_path / "no_exec_dir" / "inside.pdf"
+
+ async def _permission_denied(path: str) -> dict[str, Any]:
+ raise PermissionError(f"permission denied: {path}")
+
+ monkeypatch.setattr(parse_svc_module, "get_file_stat", _permission_denied)
+
+ with pytest.raises(InvalidRequestError) as exc_info:
+ await service.request_parse(str(source), tier="flash")
+
+ assert exc_info.value.code == "file_permission_denied"
+ assert exc_info.value.param == "path"
+ assert str(source) in exc_info.value.message
+
+ asyncio.run(_run())
+
+
def test_ingest_worker_skips_files_with_blocking_stat_errors(tmp_path: Path, monkeypatch) -> None:
async def _run() -> None:
db = DatabaseManager(str(tmp_path / "doclib.db"))
@@ -1558,6 +1584,37 @@ def test_ingest_worker_skips_files_with_blocking_stat_errors(tmp_path: Path, mon
asyncio.run(_run())
+def test_ingest_worker_preserves_mineru_permission_errors(tmp_path: Path) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+ await db.initialize()
+ service = ParseService(
+ db=db, fts=FTSManager(db), config_svc=None, data_dir=str(tmp_path / "data"), parse_lock_timeout_sec=1800
+ )
+ source = tmp_path / "note.txt"
+ now = 1000
+ file_id = await db.execute_insert(
+ "INSERT INTO files (path, filename, ext, size_bytes, mtime_ms, first_seen_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?)",
+ (str(source), source.name, "txt", 10, now, now, now),
+ )
+
+ worker = IngestWorkerPool(service, num_workers=1, lock_timeout_sec=60)
+ await worker._handle_ingest_error(
+ {"id": file_id, "path": str(source), "watch_id": None},
+ InvalidRequestError("file_permission_denied", "permission denied", "path"),
+ )
+
+ row = await db.fetchone("SELECT error_code, error_msg, locked_at FROM files WHERE id=?", (file_id,))
+ assert row is not None
+ assert row["error_code"] == "file_permission_denied"
+ assert row["error_msg"] == "permission denied"
+ assert row["locked_at"] is None
+ assert await worker._acquire_task() is None
+
+ asyncio.run(_run())
+
+
def test_ingest_worker_preserves_precise_file_access_errors(tmp_path: Path) -> None:
async def _run() -> None:
db = DatabaseManager(str(tmp_path / "doclib.db"))