Skip to content

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

Fix invalid parse after cursor errors

jinzhenje653e2420b2c
diff --git a/mineru/doclib/server.py b/mineru/doclib/server.py
index 0b6215d4..7aadb17e 100644
--- a/mineru/doclib/server.py
+++ b/mineru/doclib/server.py
@@ -608,7 +608,7 @@ class DoclibServer(AsyncDoclibInterface):
doc = await self._doc_for_ref(doc_ref, param="doc_ref")
normalized_page_range = _normalize_content_page_range(page_range, after, doc)
- after_cursor = parse_content_cursor(after) if after else None
+ after_cursor = _parse_after_cursor(after)
if after_cursor:
_validate_cursor_for_doc(after_cursor, doc, tier, normalized_page_range)
return _ReadPlan(
@@ -1125,7 +1125,7 @@ class DoclibServer(AsyncDoclibInterface):
pages_for_render,
short_id=plan.short_id,
tier=plan.tier,
- after=parse_content_cursor(plan.after) if plan.after else None,
+ after=_parse_after_cursor(plan.after),
limit=plan.limit,
add_markers=not plan.no_marker,
target=plan.target,
@@ -1379,6 +1379,15 @@ def _locator_page_range(locator: _LocatorParts, doc: DocRow, context: int) -> st
return _range_str(start, end)
+def _parse_after_cursor(after: str | None) -> ContentCursor | None:
+ if not after:
+ return None
+ try:
+ return parse_content_cursor(after)
+ except ValueError as exc:
+ raise InvalidRequestError("invalid_locator", str(exc), "after") from None
+
+
def _validate_cursor_for_doc(cursor: ContentCursor, doc: DocRow, tier: Tier, page_range: str | None) -> None:
if cursor.short_id != doc["short_id"]:
raise InvalidRequestError("invalid_request", "after cursor does not belong to this document.", "after")
@@ -1505,7 +1514,8 @@ def _normalize_content_page_range(page_range: str | None, after: str | None, doc
if page_range:
return _normalize_page_range(page_range, page_count)
if after:
- cursor = parse_content_cursor(after)
+ cursor = _parse_after_cursor(after)
+ assert cursor is not None
end = min(page_count, cursor.page_no + 9)
return f"{cursor.page_no}~{end}" if cursor.page_no != end else str(cursor.page_no)
end = min(page_count, 10)
diff --git a/tests/unittest/test_cli_next_command_design.py b/tests/unittest/test_cli_next_command_design.py
index 42b1291c..b13f2297 100644
--- a/tests/unittest/test_cli_next_command_design.py
+++ b/tests/unittest/test_cli_next_command_design.py
@@ -1215,6 +1215,37 @@ def test_parse_json_error_output_is_machine_readable(monkeypatch: Any, tmp_path:
assert "No standard or pro engine available" in payload["error"]["message"]
+def test_parse_invalid_after_cursor_json_error_is_validation_error(monkeypatch: Any, tmp_path: Path) -> None:
+ source = tmp_path / "demo.pdf"
+ source.write_bytes(b"%PDF-1.7\n")
+
+ class _Client:
+ def __init__(self, *, timeout: int) -> None:
+ assert timeout == 31
+
+ def ensure_parse(self, request: Any) -> ParseResponse:
+ return ParseResponse(sha256="a" * 64, tier="standard", page_range="1", status="done")
+
+ def get_doc_content(self, *args: Any, **kwargs: Any) -> DocContentResponse:
+ raise RuntimeError(
+ "('invalid_locator', 'Invalid doclib content cursor: invalid-cursor-for-param-test', 'after')"
+ )
+
+ monkeypatch.setattr(parse, "DoclibClient", _Client)
+
+ result = runner.invoke(
+ app,
+ ["parse", str(source), "--after", "invalid-cursor-for-param-test", "--wait", "1", "--json"],
+ )
+
+ assert result.exit_code == 1
+ payload = json.loads(result.output)
+ assert payload["error"]["type"] == "invalid_request_error"
+ assert payload["error"]["code"] == "invalid_locator"
+ assert payload["error"]["param"] == "after"
+ assert "Invalid doclib content cursor" in payload["error"]["message"]
+
+
def test_parse_missing_file_json_error_is_machine_readable(tmp_path: Path) -> None:
missing = tmp_path / "missing.pdf"
diff --git a/tests/unittest/test_doclib_cache_semantics.py b/tests/unittest/test_doclib_cache_semantics.py
index 5bb4cc07..d6aa0628 100644
--- a/tests/unittest/test_doclib_cache_semantics.py
+++ b/tests/unittest/test_doclib_cache_semantics.py
@@ -2135,6 +2135,32 @@ def test_doclib_server_accepts_short_id_for_sha256_doc_inputs(tmp_path: Path) ->
asyncio.run(_run())
+def test_doc_content_invalid_after_cursor_returns_invalid_locator(tmp_path: Path) -> None:
+ async def _run() -> None:
+ db = DatabaseManager(str(tmp_path / "doclib.sqlite"))
+ await db.initialize()
+ server = DoclibServer(SimpleNamespace(db=db, data_dir=str(tmp_path)))
+ now = 1000
+ sha256 = "a" * 64
+ short_id = "aaaaaaa"
+ await db.execute(
+ "INSERT INTO docs (sha256, short_id, size_bytes, file_type, page_count, first_seen_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?)",
+ (sha256, short_id, 12, "pdf", 1, now, now),
+ )
+ try:
+ with pytest.raises(InvalidRequestError) as exc_info:
+ await server.get_doc_content(short_id, tier="standard", after="invalid-cursor-for-param-test")
+
+ assert exc_info.value.code == "invalid_locator"
+ assert exc_info.value.param == "after"
+ assert "Invalid doclib content cursor" in exc_info.value.message
+ finally:
+ await db.close()
+
+ asyncio.run(_run())
+
+
def test_get_file_by_path_missing_doclib_record_message_is_not_disk_file_not_found(tmp_path: Path) -> None:
class _ParseSvc:
async def ensure_ingested(self, path: str) -> None: