From 5f6e8a6baab8487ff5249993cb54a2c05bf0f587 Mon Sep 17 00:00:00 2001 From: takiAA <15012649371@163.com> Date: Wed, 7 Oct 2026 20:23:42 +0800 Subject: [PATCH] fix(documents): preserve complete PDF pages across reads --- nanobot/agent/tools/filesystem.py | 5 +++ nanobot/utils/document.py | 12 ++++- tests/tools/test_read_enhancements.py | 63 ++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index 0510b72d0..60eedd7f7 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -446,6 +446,11 @@ class ReadFileTool(_FsTool): return f"(PDF has no extractable text: {fp})" result = extraction.text + if extraction.truncated: + result += ( + f"\n\n(Page {extraction.end_page + 1} was truncated; " + "the remaining text on this page is not shown.)" + ) if extraction.end_page < extraction.total_pages - 1: next_start = extraction.end_page + 2 next_end = min(extraction.end_page + 1 + self._MAX_PDF_PAGES, extraction.total_pages) diff --git a/nanobot/utils/document.py b/nanobot/utils/document.py index 34cfd9f78..7039a0ac3 100644 --- a/nanobot/utils/document.py +++ b/nanobot/utils/document.py @@ -77,6 +77,7 @@ class PdfExtraction: total_pages: int start_page: int end_page: int + truncated: bool = False @dataclass(frozen=True, slots=True) @@ -485,10 +486,17 @@ def extract_pdf_pages( f"{_MAX_PDF_CONTENT_STREAM_SIZE // (1024 * 1024)} MB limit" ) text = (page.extract_text() or "").strip() - if text and not collector.add(f"--- Page {index + 1} ---\n{text}", separator="\n\n"): + if not text: + continue + page_text = f"--- Page {index + 1} ---\n{text}" + if collector.parts and collector.length + 2 + len(page_text) > max_chars: + # Leave this page for the next read instead of returning only its prefix. + end = index - 1 + break + if not collector.add(page_text, separator="\n\n"): end = index break - return PdfExtraction(collector.render(), total_pages, start, end) + return PdfExtraction(collector.render(), total_pages, start, end, truncated=collector.truncated) def _parse_pdf_page_range(pages: str | None, total_pages: int) -> tuple[int, int]: diff --git a/tests/tools/test_read_enhancements.py b/tests/tools/test_read_enhancements.py index 3445b42b2..bb809ad0d 100644 --- a/tests/tools/test_read_enhancements.py +++ b/tests/tools/test_read_enhancements.py @@ -212,9 +212,70 @@ class TestReadDedupSessionIsolation: class TestReadPdf: @pytest.fixture() - def tool(self, tmp_path): + def tool(self, tmp_path, monkeypatch): + monkeypatch.setattr("nanobot.config.loader._current_config_path", tmp_path / "config.json") return ReadFileTool(workspace=tmp_path) + @pytest.mark.asyncio + @pytest.mark.parametrize("page_count", [2, 3]) + async def test_pdf_character_limit_preserves_pages_across_continuation( + self, tool, tmp_path, monkeypatch, page_count, + ): + import re + + fitz = pytest.importorskip("pymupdf") + pdf_path = tmp_path / "report.pdf" + doc = fitz.open() + markers = [] + for number in range(1, page_count + 1): + marker = f"END_OF_PAGE_{number}" + markers.append(marker) + page = doc.new_page() + page.insert_text((72, 72), "Report details " * 4 + "\n" + marker) + doc.save(pdf_path) + doc.close() + monkeypatch.setattr(tool, "_MAX_CHARS", 130) + + outputs = [] + pages = None + for _ in range(page_count): + result = await tool.execute(path=str(pdf_path), pages=pages) + outputs.append(result) + continuation = re.search(r"Use pages='([^']+)'", result) + if continuation is None: + break + pages = continuation[1] + + combined = "\n".join(outputs) + for marker in markers: + assert combined.count(marker) == 1 + assert "truncated" not in combined + assert "Use pages=" not in outputs[-1] + + @pytest.mark.asyncio + async def test_pdf_oversized_page_reports_unreturned_text( + self, tool, tmp_path, monkeypatch, + ): + fitz = pytest.importorskip("pymupdf") + pdf_path = tmp_path / "dense.pdf" + doc = fitz.open() + page = doc.new_page() + page.insert_text((72, 72), "\n".join(["Dense page content " * 3] * 8)) + page = doc.new_page() + page.insert_text((72, 72), "Second page complete") + doc.save(pdf_path) + doc.close() + monkeypatch.setattr(tool, "_MAX_CHARS", 130) + + result = await tool.execute(path=str(pdf_path)) + + assert "Page 1 was truncated" in result + assert "remaining text on this page is not shown" in result + assert "Use pages='2-2'" in result + second = await tool.execute(path=str(pdf_path), pages="2") + assert "Second page complete" in second + assert "truncated" not in second + @pytest.mark.asyncio async def test_pdf_returns_text_content(self, tool, tmp_path): fitz = pytest.importorskip("fitz")