Skip to content

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

Reject nested doclib watches

jinzhenj32114db5149d
diff --git a/mineru/doclib/services/config_svc.py b/mineru/doclib/services/config_svc.py
index 57e8eadb..e6080ffa 100644
--- a/mineru/doclib/services/config_svc.py
+++ b/mineru/doclib/services/config_svc.py
@@ -84,6 +84,16 @@ class ConfigService:
digest = hashlib.blake2b(path.encode("utf-8"), digest_size=8).digest()
return int.from_bytes(digest, "big") & ((1 << 63) - 1)
+ @staticmethod
+ def _watch_paths_are_nested(first: str, second: str) -> bool:
+ if first == second:
+ return False
+ try:
+ common = os.path.commonpath([first, second])
+ except ValueError:
+ return False
+ return common == first or common == second
+
async def add_watch(
self, path: str, removable: bool = False, label: str | None = None
) -> WatchTargetRow:
@@ -95,6 +105,15 @@ class ConfigService:
if not os.path.isdir(path):
raise InvalidRequestError("invalid_request", f"Watch path does not exist or is not a directory: {path}", "path")
+ watches = cast(list[WatchTargetRow], await self.db.fetchall("SELECT * FROM watches WHERE enabled=1"))
+ for watch in watches:
+ if self._watch_paths_are_nested(path, watch["path"]):
+ raise InvalidRequestError(
+ "invalid_request",
+ f"Watch path overlaps existing watch target id={watch['id']} path={watch['path']}: {path}",
+ "path",
+ )
+
wid = self._watch_id_for_path(path)
now = int(time.time() * 1000)
diff --git a/tests/unittest/test_doclib_interface_contract.py b/tests/unittest/test_doclib_interface_contract.py
index fe3e9bf8..d66cf7d5 100644
--- a/tests/unittest/test_doclib_interface_contract.py
+++ b/tests/unittest/test_doclib_interface_contract.py
@@ -1,5 +1,6 @@
import asyncio
import inspect
+from pathlib import Path
from typing import Any, cast, get_type_hints
import pytest
@@ -247,6 +248,71 @@ def test_add_watch_normalizes_user_path_before_storing(tmp_path) -> None:
asyncio.run(_run())
+def test_add_watch_rejects_child_of_existing_watch(tmp_path: Path) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+ await db.initialize()
+ service = ConfigService(db)
+
+ root = tmp_path / "root"
+ child = root / "child"
+ child.mkdir(parents=True)
+
+ existing = await service.add_watch(str(root))
+
+ with pytest.raises(InvalidRequestError) as exc_info:
+ await service.add_watch(str(child))
+
+ assert exc_info.value.code == "invalid_request"
+ assert exc_info.value.param == "path"
+ assert str(existing["id"]) in exc_info.value.message
+ assert str(root) in exc_info.value.message
+
+ asyncio.run(_run())
+
+
+def test_add_watch_rejects_parent_of_existing_watch(tmp_path: Path) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+ await db.initialize()
+ service = ConfigService(db)
+
+ root = tmp_path / "root"
+ child = root / "child"
+ child.mkdir(parents=True)
+
+ existing = await service.add_watch(str(child))
+
+ with pytest.raises(InvalidRequestError) as exc_info:
+ await service.add_watch(str(root))
+
+ assert exc_info.value.code == "invalid_request"
+ assert exc_info.value.param == "path"
+ assert str(existing["id"]) in exc_info.value.message
+ assert str(child) in exc_info.value.message
+
+ asyncio.run(_run())
+
+
+def test_add_watch_allows_same_path_upsert(tmp_path: Path) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.db"))
+ await db.initialize()
+ service = ConfigService(db)
+
+ root = tmp_path / "root"
+ root.mkdir()
+
+ first = await service.add_watch(str(root), label="First")
+ second = await service.add_watch(str(root), label="Second")
+
+ assert second["id"] == first["id"]
+ assert second["path"] == first["path"]
+ assert second["label"] == "Second"
+
+ asyncio.run(_run())
+
+
def test_config_service_rejects_unsupported_rule_type_with_structured_error(tmp_path) -> None:
async def _run() -> None:
db = DatabaseManager(str(tmp_path / "doclib.db"))