Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Fix PDF image mapping, caption refresh, and Grok detection

Bingxi Zhao (Frank)1829ae23f0df
diff --git a/deeptutor/capabilities/reading/media_notes.py b/deeptutor/capabilities/reading/media_notes.py
index 5b55af621..430f582c3 100644
--- a/deeptutor/capabilities/reading/media_notes.py
+++ b/deeptutor/capabilities/reading/media_notes.py
@@ -40,9 +40,7 @@ def _render_image(name: str, caption: str) -> str:
return f"{name} — {clipped}"
-def render_media_note(
- store: Any, material_id: str, unit: str, locators: Sequence[int]
-) -> str:
+def render_media_note(store: Any, material_id: str, unit: str, locators: Sequence[int]) -> str:
"""List the embedded images that live in the units just read.
The reader pane displays these images next to their unit, and the turn
diff --git a/deeptutor/capabilities/reading/tools.py b/deeptutor/capabilities/reading/tools.py
index a35688c0f..98cd8fe63 100644
--- a/deeptutor/capabilities/reading/tools.py
+++ b/deeptutor/capabilities/reading/tools.py
@@ -421,7 +421,9 @@ class ReadMaterialTool(_ReadingToolBase):
for locator in rendered.locators
if locator in stamps
)
- media_note = await asyncio.to_thread(_media_note, store, material_id, manifest.unit, rendered.locators)
+ media_note = await asyncio.to_thread(
+ _media_note, store, material_id, manifest.unit, rendered.locators
+ )
followup = (
"\n\n→ Now call reader_goto with the verbatim sentence you are "
"about to cite, so the user sees it highlighted, and cite it in "
diff --git a/deeptutor/reading/captions.py b/deeptutor/reading/captions.py
index e0852437a..f24dcdb84 100644
--- a/deeptutor/reading/captions.py
+++ b/deeptutor/reading/captions.py
@@ -65,11 +65,7 @@ async def caption_material_media(
"""
store = store or ReadingStore()
rows = store.media_items(material_id)
- pending = [
- row
- for row in rows
- if force or not str(row.get("caption") or "").strip()
- ]
+ pending = [row for row in rows if force or not str(row.get("caption") or "").strip()]
if limit is not None:
pending = pending[: max(0, int(limit))]
if not pending:
@@ -81,9 +77,7 @@ async def caption_material_media(
logger.warning("Image captioning skipped: LLM client is unavailable (%s)", exc)
return 0
if not client.supports_multimodal_images():
- logger.warning(
- "Image captioning skipped: the configured LLM does not accept image input."
- )
+ logger.warning("Image captioning skipped: the configured LLM does not accept image input.")
return 0
concurrency, timeout_seconds = image_description_limits()
@@ -113,9 +107,7 @@ async def caption_material_media(
timeout=timeout_seconds,
)
except asyncio.TimeoutError:
- logger.warning(
- "Image captioning timed out after %ss: %s", timeout_seconds, name
- )
+ logger.warning("Image captioning timed out after %ss: %s", timeout_seconds, name)
return None
except Exception as exc: # noqa: BLE001 - one bad image must not sink the rest
logger.warning("Image captioning failed for %s: %s", name, exc)
@@ -126,9 +118,7 @@ async def caption_material_media(
return None
return name, caption
- results = await asyncio.gather(
- *(_caption_one(row) for row in pending), return_exceptions=True
- )
+ results = await asyncio.gather(*(_caption_one(row) for row in pending), return_exceptions=True)
captions: dict[str, str] = {}
for result in results:
if isinstance(result, BaseException):
diff --git a/deeptutor/reading/extract.py b/deeptutor/reading/extract.py
index d0c29da9e..97f0db482 100644
--- a/deeptutor/reading/extract.py
+++ b/deeptutor/reading/extract.py
@@ -231,6 +231,7 @@ def _pdf_pages_with_image_markers(
)
return targeted, _media_for_units(targeted, pdf_images.collection.images)
+
def _extract_epub(source: Path) -> Extraction:
"""Preserve EPUB spine order so browser and assistant locators agree."""
from deeptutor.utils.document_extractor import DocumentExtractionError, extract_epub_spine
diff --git a/deeptutor/reading/refresh.py b/deeptutor/reading/refresh.py
index 2150740b3..1450c6a06 100644
--- a/deeptutor/reading/refresh.py
+++ b/deeptutor/reading/refresh.py
@@ -87,9 +87,7 @@ def _refresh_document(store: ReadingStore, material_id: str) -> Any:
return refresh(material_id)
-async def _caption_material_media(
- store: ReadingStore, material_id: str, *, force: bool
-) -> int:
+async def _caption_material_media(store: ReadingStore, material_id: str, *, force: bool) -> int:
"""Caption one material's media, importing the captions module lazily.
Deferred so ``--dry-run`` keeps working before ``captions.py`` lands.
@@ -100,9 +98,7 @@ async def _caption_material_media(
raise ReadingError(
"captioning is unavailable: deeptutor.reading.captions is not importable"
) from exc
- return int(
- await caption_material_media(material_id, store=store, force=force, limit=None)
- )
+ return int(await caption_material_media(material_id, store=store, force=force, limit=None))
async def refresh_materials(
diff --git a/deeptutor/reading/store.py b/deeptutor/reading/store.py
index ac79bc2b2..2e1604bf6 100644
--- a/deeptutor/reading/store.py
+++ b/deeptutor/reading/store.py
@@ -289,7 +289,9 @@ class ReadingStore:
"bytes": len(item.data),
}
)
- _atomic_write(stage_dir / MEDIA_INDEX_NAME, json.dumps(media_rows, ensure_ascii=False))
+ _atomic_write(
+ stage_dir / MEDIA_INDEX_NAME, json.dumps(media_rows, ensure_ascii=False)
+ )
if extraction.render_mode != "text":
raw_dir = stage_dir / RAW_DIR
@@ -428,14 +430,35 @@ class ReadingStore:
f"{existing.unit_count}; refusing to shift locators."
)
- # Captions cost a vision-model call each, so a re-extraction that
- # does not re-caption (CLI ``--no-caption``) must carry the old
- # ones forward by image name: same figure in, same caption out.
- previous_captions = {
- str(row.get("name")): str(row.get("caption"))
- for row in (self.media_items(resolved_id) or [])
- if row.get("name") and str(row.get("caption") or "").strip()
- }
+ # Extraction order can rename image-01.png to a different figure.
+ # Carry captions forward only when the image bytes still match.
+ previous_captions: dict[str, str] = {}
+ conflicting_hashes: set[str] = set()
+ digest_by_name: dict[str, str | None] = {}
+ for row in self.media_items(resolved_id):
+ caption = str(row.get("caption") or "").strip()
+ if not caption:
+ continue
+ name = str(row.get("name") or "")
+ if name not in digest_by_name:
+ path = self.media_path(resolved_id, name)
+ try:
+ digest_by_name[name] = (
+ hashlib.sha256(path.read_bytes()).hexdigest()
+ if path is not None
+ else None
+ )
+ except OSError:
+ digest_by_name[name] = None
+ digest = digest_by_name[name]
+ if digest is None:
+ continue
+ if digest in previous_captions and previous_captions[digest] != caption:
+ conflicting_hashes.add(digest)
+ else:
+ previous_captions[digest] = caption
+ for digest in conflicting_hashes:
+ previous_captions.pop(digest, None)
stage_dir = self.root / f".{resolved_id}.{uuid.uuid4().hex[:8]}.staging"
backup_dir = self.root / f".{resolved_id}.{uuid.uuid4().hex[:8]}.backup"
@@ -455,7 +478,7 @@ class ReadingStore:
"mime": item.mime_type,
"bytes": len(item.data),
}
- caption = previous_captions.get(item.name)
+ caption = previous_captions.get(hashlib.sha256(item.data).hexdigest())
if caption:
row["caption"] = caption
media_rows.append(row)
@@ -515,9 +538,7 @@ class ReadingStore:
for state_dir in (ANNOTATIONS_DIR, POSITIONS_DIR, BOOKMARKS_DIR, REVISIONS_DIR):
source_state_dir = material_dir / state_dir
if source_state_dir.is_dir():
- shutil.copytree(
- source_state_dir, stage_dir / state_dir, dirs_exist_ok=True
- )
+ shutil.copytree(source_state_dir, stage_dir / state_dir, dirs_exist_ok=True)
for state_name in (ANNOTATIONS_NAME, POSITION_NAME):
source_state = material_dir / state_name
if source_state.is_file():
@@ -903,9 +924,7 @@ class ReadingStore:
path = self._dir(material_id) / MEDIA_DIR / clean
return path if path.is_file() else None
- def update_media_captions(
- self, material_id: str, captions: Mapping[str, str]
- ) -> int:
+ def update_media_captions(self, material_id: str, captions: Mapping[str, str]) -> int:
"""Write per-image captions into the media index. Returns rows changed.
*captions* maps an image name to its text. Names absent from the index
diff --git a/deeptutor/services/session/attachment_parsing.py b/deeptutor/services/session/attachment_parsing.py
index 8762d1096..201838938 100644
--- a/deeptutor/services/session/attachment_parsing.py
+++ b/deeptutor/services/session/attachment_parsing.py
@@ -5,13 +5,16 @@ from __future__ import annotations
import asyncio
from collections.abc import Callable, Iterable
import logging
+import re
from typing import Any
from deeptutor.services.config.runtime_settings import get_chat_attachment_limits
from deeptutor.services.parsing import get_parse_service
from deeptutor.services.storage import AttachmentStore
+from deeptutor.utils.document_images import find_markers
logger = logging.getLogger(__name__)
+_PDF_PAGE_HEADER = re.compile(r"--- Page (\d+) ---")
async def parse_chat_pdf_attachments(
@@ -72,7 +75,20 @@ async def parse_chat_pdf_attachments(
error = str(exc)
if text:
cap = min(limits.max_chars_per_doc, limits.max_chars_total - total_chars)
- if len(text) > cap:
+ image_locations = _pdf_image_locations(fallback_text, text)
+ if image_locations:
+ # Reserve room for the original page-to-image mapping even if
+ # the configured parser's Markdown fills the text quota.
+ note = "\n\n[Embedded image locations in original PDF]"
+ for location in image_locations:
+ if len(note) + 1 + len(location) > cap:
+ break
+ note += "\n" + location
+ if note != "\n\n[Embedded image locations in original PDF]":
+ text = text[: cap - len(note)] + note
+ else:
+ text = text[:cap]
+ elif len(text) > cap:
text = text[:cap] + f"... (truncated, {len(text)} chars total; chat quota hit)"
record["extracted_text"] = text
record["extracted_chars"] = len(text)
@@ -112,6 +128,23 @@ async def parse_chat_pdf_attachments(
return updated, contexts
+def _pdf_image_locations(native_text: str, parsed_text: str) -> list[str]:
+ """Keep native PDF page markers for images omitted by configured parsers."""
+ page_number: str | None = None
+ locations: list[str] = []
+ for line in native_text.splitlines():
+ stripped = line.strip()
+ page = _PDF_PAGE_HEADER.fullmatch(stripped)
+ if page:
+ page_number = page.group(1)
+ elif page_number:
+ for index, name in find_markers(stripped):
+ location = f"--- Page {page_number} ---\n[图片 {index}: {name}]"
+ if location not in parsed_text and location not in locations:
+ locations.append(location)
+ return locations
+
+
def _replace_context(contexts: list[str], filename: str, text: str, error: str) -> None:
replacement = (
f"[File: {filename}]\n{text}"
diff --git a/deeptutor/services/subagent/grok.py b/deeptutor/services/subagent/grok.py
index ddd4b6e84..d09dec68d 100644
--- a/deeptutor/services/subagent/grok.py
+++ b/deeptutor/services/subagent/grok.py
@@ -66,7 +66,15 @@ class GrokBackend(SubagentBackend):
help_ok, help_text = await probe_version([self.cli_command, "--help"])
ok = help_ok and all(
flag in help_text
- for flag in ("streaming-json", "--single", "--resume", "--permission-mode")
+ for flag in (
+ "--output-format",
+ "streaming-json",
+ "--single",
+ "--resume",
+ "--permission-mode",
+ "--no-memory",
+ "--verbatim",
+ )
)
if not ok:
detail = "Incompatible grok command. " + _NOT_FOUND_DETAIL
diff --git a/deeptutor/utils/document_extractor.py b/deeptutor/utils/document_extractor.py
index e774e9dd5..6f352276a 100644
--- a/deeptutor/utils/document_extractor.py
+++ b/deeptutor/utils/document_extractor.py
@@ -391,12 +391,7 @@ def _extract_pdf(data: bytes, filename: str) -> str:
build_marker(pdf_images.collection.images[index]) for index in indices
]
pages = [
- page_text
- + (
- "\n" + "\n".join(marker_by_page[i])
- if i in marker_by_page
- else ""
- )
+ page_text + ("\n" + "\n".join(marker_by_page[i]) if i in marker_by_page else "")
for i, page_text in enumerate(pages, 1)
]
note = pdf_images.collection.summary_note()
@@ -1333,9 +1328,7 @@ def extract_documents_from_records(
"base64": base64.b64encode(image.data).decode("ascii"),
"filename": f"{stem}-图{image_index_from_name(image.name)}{ext}",
"mime_type": image.mime_type,
- "id": f"{doc_id}-e{image_index_from_name(image.name):02d}"
- if doc_id
- else "",
+ "id": f"{doc_id}-e{image_index_from_name(image.name):02d}" if doc_id else "",
"embedded": True,
}
)
diff --git a/deeptutor/utils/document_images.py b/deeptutor/utils/document_images.py
index 4d8915d94..e3c6240d0 100644
--- a/deeptutor/utils/document_images.py
+++ b/deeptutor/utils/document_images.py
@@ -202,6 +202,7 @@ def build_marker(image: EmbeddedImage) -> str:
"""The canonical in-text marker for one image."""
return f"[图片 {image_index_from_name(image.name)}: {image.name}]"
+
_MARKER_PATTERN = re.compile(r"\[图片 (\d+): ([^\]\s]+)\]")
@@ -505,19 +506,17 @@ def _rels_for(member: str) -> str:
# ---------------------------------------------------------------------------
-def extract_pdf_images(
- data: bytes, *, budget: ImageBudget | None = None
-) -> PdfImages:
+def extract_pdf_images(data: bytes, *, budget: ImageBudget | None = None) -> PdfImages:
"""Raster images per page via PyMuPDF, deduplicated across the document.
- Running headers/footers embed the same logo xref on every page; only the
- first occurrence is kept so a 300-page PDF does not become 300 copies of
- the same banner.
+ Running headers/footers embed the same logo xref on every page. Store its
+ bytes once, but map that image to every page where it appears.
``budget`` defaults to the chat-attachment caps (unchanged behaviour). When
it carries a non-zero ``max_images_per_page``, a page stops admitting once
it hits that many images and the rest of that page's candidates are counted
- as page-budget skips — dedup hits are still free and never counted.
+ as page-budget skips. Reused xrefs still count toward the per-page cap,
+ but do not consume the document-wide image or byte budgets again.
"""
try:
import pymupdf
@@ -527,26 +526,41 @@ def extract_pdf_images(
budget = budget or ImageBudget()
images: list[EmbeddedImage] = []
page_map: list[tuple[int, tuple[int, ...]]] = []
- seen_xrefs: set[int] = set()
+ xref_to_index: dict[int, int] = {}
+ rejected_xrefs: set[int] = set()
try:
with pymupdf.open(stream=data, filetype="pdf") as doc:
for page_number, page in enumerate(doc, 1):
indices: list[int] = []
admitted_on_page = 0
+ seen_on_page: set[int] = set()
for info in page.get_images(full=True):
xref = info[0]
- if xref in seen_xrefs:
+ if xref in seen_on_page:
+ continue
+ seen_on_page.add(xref)
+ if budget.max_images_per_page and (
+ admitted_on_page >= budget.max_images_per_page
+ ):
+ budget.skipped_page_budget += 1
+ continue
+ if xref in xref_to_index:
+ indices.append(xref_to_index[xref])
+ admitted_on_page += 1
continue
- seen_xrefs.add(xref)
+ if xref in rejected_xrefs:
+ continue
+ rejected_xrefs.add(xref)
try:
extracted = doc.extract_image(xref)
except Exception:
continue
raw = extracted.get("image") or b""
- if int(extracted.get("width") or 0) < 64 or int(
- extracted.get("height") or 0
- ) < 64:
+ if (
+ int(extracted.get("width") or 0) < 64
+ or int(extracted.get("height") or 0) < 64
+ ):
continue
ext = _normalise_ext("." + (extracted.get("ext") or ""))
if ext in {".emf", ".wmf"}:
@@ -555,15 +569,11 @@ def extract_pdf_images(
if ext not in SUPPORTED_IMAGE_EXTENSIONS:
budget.register_other_skip()
continue
- if budget.max_images_per_page and (
- admitted_on_page >= budget.max_images_per_page
- ):
- budget.skipped_page_budget += 1
- continue
admitted = budget.try_admit(ext, raw)
if admitted is not None:
images.append(admitted)
- indices.append(len(images) - 1)
+ xref_to_index[xref] = len(images) - 1
+ indices.append(xref_to_index[xref])
admitted_on_page += 1
if indices:
page_map.append((page_number, tuple(indices)))
diff --git a/scripts/reading_refresh_figures.py b/scripts/reading_refresh_figures.py
index 7908f8986..a83ce8dfb 100644
--- a/scripts/reading_refresh_figures.py
+++ b/scripts/reading_refresh_figures.py
@@ -72,12 +72,8 @@ def _build_parser() -> argparse.ArgumentParser:
action="store_true",
help="caption even if a stored caption already exists (implies --caption)",
)
- parser.add_argument(
- "--limit", type=int, metavar="N", help="process at most N materials"
- )
- parser.add_argument(
- "--yes", action="store_true", help="skip the interactive confirmation"
- )
+ parser.add_argument("--limit", type=int, metavar="N", help="process at most N materials")
+ parser.add_argument("--yes", action="store_true", help="skip the interactive confirmation")
return parser
@@ -158,8 +154,7 @@ def main(argv: list[str] | None = None) -> int:
return 0
if not sys.stdin.isatty():
print(
- "refusing to run a non-dry refresh without --yes in a "
- "non-interactive shell",
+ "refusing to run a non-dry refresh without --yes in a non-interactive shell",
file=sys.stderr,
)
return 2
diff --git a/tests/reading/test_reading_captions.py b/tests/reading/test_reading_captions.py
index df6f98a88..c37756456 100644
--- a/tests/reading/test_reading_captions.py
+++ b/tests/reading/test_reading_captions.py
@@ -32,7 +32,9 @@ class _StubClient:
def supports_multimodal_images(self) -> bool:
return self._vision
- async def complete(self, prompt: str, system_prompt: str | None = None, **kwargs: object) -> str:
+ async def complete(
+ self, prompt: str, system_prompt: str | None = None, **kwargs: object
+ ) -> str:
name = str(kwargs.get("image_filename") or "")
self.calls.append(name)
if name in self._fail:
@@ -158,9 +160,7 @@ def _pdf_with_image(path: Path) -> None:
doc = pymupdf.open()
page = doc.new_page()
- prose = (
- "Figure one shows the measured throughput across three configurations. " * 8
- )
+ prose = "Figure one shows the measured throughput across three configurations. " * 8
page.insert_textbox(pymupdf.Rect(72, 72, 520, 280), prose, fontsize=9)
page.insert_image(pymupdf.Rect(72, 320, 300, 520), stream=_png_bytes(seed=11))
page.insert_image(pymupdf.Rect(320, 320, 540, 520), stream=_png_bytes(seed=23))
@@ -190,19 +190,40 @@ def test_refresh_document_preserves_state_and_captions(tmp_path: Path) -> None:
asset_file.parent.mkdir(parents=True, exist_ok=True)
asset_file.write_bytes(b"poster-bytes")
- # Old captions: keep one real figure's caption, drop another figure's row
- # so it re-appears uncaptioned, and leave a ghost row that the new
- # extraction will not reproduce.
+ # The same image gets a different old filename, while the other old
+ # filename now holds different bytes. Only the matching image may keep
+ # its caption when extraction assigns ordinal names again.
kept_name = names[0]
new_name = names[1]
+ legacy_name = "legacy-figure.png"
+ media_dir = content_dir / "media"
+ (media_dir / kept_name).rename(media_dir / legacy_name)
+ (media_dir / new_name).write_bytes(_png_bytes(seed=77))
media_index = content_dir / "media.json"
rows = [
- {"name": kept_name, "locator": 1, "mime": "image/png", "bytes": 9,
- "caption": f"kept caption for {kept_name}"},
+ {
+ "name": legacy_name,
+ "locator": 1,
+ "mime": "image/png",
+ "bytes": 9,
+ "caption": f"kept caption for {kept_name}",
+ },
+ {
+ "name": new_name,
+ "locator": 1,
+ "mime": "image/png",
+ "bytes": 9,
+ "caption": "stale caption for replaced bytes",
+ },
]
rows.append(
- {"name": "ghost.png", "locator": 1, "mime": "image/png", "bytes": 9,
- "caption": "ghost caption"}
+ {
+ "name": "ghost.png",
+ "locator": 1,
+ "mime": "image/png",
+ "bytes": 9,
+ "caption": "ghost caption",
+ }
)
media_index.write_text(json.dumps(rows), encoding="utf-8")
@@ -217,6 +238,7 @@ def test_refresh_document_preserves_state_and_captions(tmp_path: Path) -> None:
assert captions.get(kept_name) == f"kept caption for {kept_name}"
assert new_name in captions and not captions.get(new_name)
assert "ghost.png" not in captions
+ assert legacy_name not in captions
def test_refresh_document_requires_raw_bytes(tmp_path: Path) -> None:
diff --git a/tests/reading/test_reading_media_notes.py b/tests/reading/test_reading_media_notes.py
index b08676ee3..637c773a0 100644
--- a/tests/reading/test_reading_media_notes.py
+++ b/tests/reading/test_reading_media_notes.py
@@ -26,7 +26,6 @@ from deeptutor.capabilities.reading.media_notes import (
)
from deeptutor.capabilities.reading.tools import _media_note
from deeptutor.core.context import UnifiedContext
-
import deeptutor.reading as reading_pkg
from deeptutor.reading import page_render as page_render_module
@@ -74,7 +73,9 @@ def test_media_note_without_captions_is_byte_for_byte_the_legacy_output() -> Non
note = _media_note(_FakeStore(rows), "material", "page", [3, 5])
- assert note == _legacy_note("page", [(3, ["image-01.png", "image-02.png"]), (5, ["image-09.png"])])
+ assert note == _legacy_note(
+ "page", [(3, ["image-01.png", "image-02.png"]), (5, ["image-09.png"])]
+ )
def test_media_note_appends_each_caption_after_its_figure() -> None:
@@ -203,9 +204,7 @@ def test_pre_loop_seed_skips_media_work_without_a_locator(monkeypatch, locator)
def test_pre_loop_seed_announces_a_page_render(monkeypatch) -> None:
monkeypatch.setattr(reading_pkg, "ReadingStore", lambda: _FakeStore([]))
- monkeypatch.setattr(
- page_render_module, "page_has_render", lambda material_id, locator: True
- )
+ monkeypatch.setattr(page_render_module, "page_has_render", lambda material_id, locator: True)
seed = ReadingCapability().pre_loop_seed(_viewport_context(14))
@@ -215,9 +214,7 @@ def test_pre_loop_seed_announces_a_page_render(monkeypatch) -> None:
def test_pre_loop_seed_is_byte_for_byte_unchanged_without_a_page_render(monkeypatch) -> None:
monkeypatch.setattr(reading_pkg, "ReadingStore", lambda: _FakeStore([_row(4, "image-01.png")]))
- monkeypatch.setattr(
- page_render_module, "page_has_render", lambda material_id, locator: False
- )
+ monkeypatch.setattr(page_render_module, "page_has_render", lambda material_id, locator: False)
seed = ReadingCapability().pre_loop_seed(_viewport_context(4))
diff --git a/tests/reading/test_reading_page_render.py b/tests/reading/test_reading_page_render.py
index 464fed82d..a83d9fadb 100644
--- a/tests/reading/test_reading_page_render.py
+++ b/tests/reading/test_reading_page_render.py
@@ -64,9 +64,7 @@ def _prose(label: str) -> str:
def _draw_diagram(page) -> None:
"""Ten vector primitives — comfortably past the render gate of 8."""
for index in range(4):
- page.draw_line(
- pymupdf.Point(72, 120 + index * 12), pymupdf.Point(300, 120 + index * 12)
- )
+ page.draw_line(pymupdf.Point(72, 120 + index * 12), pymupdf.Point(300, 120 + index * 12))
for index in range(3):
page.draw_rect(pymupdf.Rect(72, 220 + index * 34, 220, 244 + index * 34))
for index in range(3):
@@ -82,9 +80,7 @@ def _build_pdf(path: Path, *, embedded_images: int = 0) -> None:
rect = pymupdf.Rect(72, 380 + index * 60, 260, 440 + index * 60)
page.insert_image(rect, stream=_png_bytes(seed=index + 1))
text_page = doc.new_page(width=612, height=792)
- text_page.insert_textbox(
- pymupdf.Rect(72, 72, 540, 600), _prose("Page two."), fontsize=11
- )
+ text_page.insert_textbox(pymupdf.Rect(72, 72, 540, 600), _prose("Page two."), fontsize=11)
doc.save(str(path))
doc.close()
@@ -157,9 +153,7 @@ def test_attachments_truncate_to_max_with_render_first(reading_home: Path) -> No
manifest = store.ingest(source)
_write_media_index(store, manifest.material_id, count=5)
- attachments = _reading_viewport_image_attachments(
- manifest.material_id, {"locator": 1}
- )
+ attachments = _reading_viewport_image_attachments(manifest.material_id, {"locator": 1})
assert len(attachments) == READING_VIEWPORT_MAX_IMAGES == 4
assert attachments[0]["id"].startswith("rp-")
diff --git a/tests/reading/test_reading_pdf_media.py b/tests/reading/test_reading_pdf_media.py
index b0feaec1e..38f9d54cc 100644
--- a/tests/reading/test_reading_pdf_media.py
+++ b/tests/reading/test_reading_pdf_media.py
@@ -76,6 +76,27 @@ def test_pdf_images_become_locator_pinned_media(tmp_path) -> None:
assert len(media_names) == locator_counts[locator]
+def test_reused_pdf_image_is_mapped_to_each_page(tmp_path) -> None:
+ pymupdf = pytest.importorskip("pymupdf")
+ pdf_path = tmp_path / "repeated-figure.pdf"
+ image = _png_bytes((150, 120), seed=7)
+ doc = pymupdf.open()
+ for page_index in range(1, 4):
+ page = doc.new_page(width=612, height=792)
+ page.insert_text((72, 60), _prose(f"Page {page_index}"))
+ page.insert_image(pymupdf.Rect(72, 100, 240, 180), stream=image)
+ doc.save(str(pdf_path))
+ doc.close()
+
+ extraction = extract_material(pdf_path)
+
+ assert len(extraction.units) == 3
+ assert [item.locator for item in extraction.media] == [1, 2, 3]
+ assert len({item.name for item in extraction.media}) == 1
+ for unit in extraction.units:
+ assert find_markers(unit) == [(1, extraction.media[0].name)]
+
+
def test_text_only_pdf_has_no_media_and_no_marker(tmp_path) -> None:
pdf_path = tmp_path / "plain.pdf"
_build_pdf(pdf_path, images_per_page=[0, 0])
diff --git a/tests/services/session/test_attachment_parsing.py b/tests/services/session/test_attachment_parsing.py
index 5a0a95a64..097d00065 100644
--- a/tests/services/session/test_attachment_parsing.py
+++ b/tests/services/session/test_attachment_parsing.py
@@ -4,6 +4,7 @@ from pathlib import Path
import pytest
+from deeptutor.services.config.runtime_settings import ChatAttachmentLimits
from deeptutor.services.parsing.types import ParsedDocument
from deeptutor.services.session.attachment_parsing import parse_chat_pdf_attachments
from deeptutor.services.storage.attachment_store import LocalDiskAttachmentStore
@@ -61,6 +62,75 @@ async def test_chat_pdf_always_uses_configured_parser(
assert contexts == ["[File: notes.pdf]\n# Parsed layout\n\n![figure](image.png)"]
+async def test_configured_parser_keeps_native_image_page_markers(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ store = LocalDiskAttachmentStore(root=tmp_path / "attachments")
+ await store.put(session_id="session", attachment_id="pdf", filename="notes.pdf", data=b"pdf")
+
+ class Parser:
+ def parse(self, path: Path, **_: object) -> ParsedDocument:
+ return ParsedDocument(markdown="# Parsed layout")
+
+ monkeypatch.setattr(
+ "deeptutor.services.session.attachment_parsing.get_parse_service", lambda: Parser()
+ )
+ native = (
+ "--- Page 1 ---\nNative text\n[图片 1: image-01.png]"
+ "\n\n--- Page 2 ---\nMore text\n[图片 1: image-01.png]"
+ )
+ records, contexts = await parse_chat_pdf_attachments(
+ [{"id": "pdf", "filename": "notes.pdf", "extracted_text": native}],
+ attachment_store=store,
+ session_id="session",
+ document_texts=[f"[File: notes.pdf]\n{native}"],
+ )
+
+ extracted = records[0]["extracted_text"]
+ assert extracted.startswith("# Parsed layout")
+ assert "--- Page 1 ---\n[图片 1: image-01.png]" in extracted
+ assert "--- Page 2 ---\n[图片 1: image-01.png]" in extracted
+ assert contexts == [f"[File: notes.pdf]\n{extracted}"]
+
+
+async def test_image_page_markers_survive_parser_text_quota(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ store = LocalDiskAttachmentStore(root=tmp_path / "attachments")
+ await store.put(session_id="session", attachment_id="pdf", filename="notes.pdf", data=b"pdf")
+
+ class Parser:
+ def parse(self, path: Path, **_: object) -> ParsedDocument:
+ return ParsedDocument(markdown="P" * 500)
+
+ monkeypatch.setattr(
+ "deeptutor.services.session.attachment_parsing.get_parse_service", lambda: Parser()
+ )
+ monkeypatch.setattr(
+ "deeptutor.services.session.attachment_parsing.get_chat_attachment_limits",
+ lambda: ChatAttachmentLimits(1000, 1000, 120, 120),
+ )
+ records, contexts = await parse_chat_pdf_attachments(
+ [
+ {
+ "id": "pdf",
+ "filename": "notes.pdf",
+ "extracted_text": "--- Page 1 ---\n[图片 1: image-01.png]",
+ }
+ ],
+ attachment_store=store,
+ session_id="session",
+ document_texts=[],
+ )
+
+ extracted = records[0]["extracted_text"]
+ assert len(extracted) == records[0]["extracted_chars"] == 120
+ assert extracted.endswith("--- Page 1 ---\n[图片 1: image-01.png]")
+ assert contexts == [f"[File: notes.pdf]\n{extracted}"]
+
+
@pytest.mark.asyncio
async def test_scan_pdf_uses_parser_and_reports_structured_progress(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
diff --git a/tests/services/test_grok_backend.py b/tests/services/test_grok_backend.py
index 53a766981..dfd6fce78 100644
--- a/tests/services/test_grok_backend.py
+++ b/tests/services/test_grok_backend.py
@@ -287,7 +287,12 @@ async def test_registry_detection_and_options_use_grok_protocol(monkeypatch, ins
return installed, "grok 1.0.3" if installed else "not installed"
return (
True,
- "--single --resume --permission-mode streaming-json" if compatible else "other CLI",
+ (
+ "--output-format streaming-json --single --resume "
+ "--permission-mode --no-memory --verbatim"
+ )
+ if compatible
+ else "other CLI",
)
monkeypatch.setattr("deeptutor.services.subagent.grok.probe_version", probe)
@@ -300,3 +305,17 @@ async def test_registry_detection_and_options_use_grok_protocol(monkeypatch, ins
assert options.allow_custom_model and options.models == [] and options.efforts == []
if not options.available:
assert "xAI" in options.detail and "grok login" in options.detail
+
+
[email protected]("missing_flag", ["--output-format", "--no-memory", "--verbatim"])
+async def test_grok_detection_requires_every_unconditional_flag(monkeypatch, missing_flag):
+ help_text = (
+ "--output-format streaming-json --single --resume --permission-mode --no-memory --verbatim"
+ ).replace(missing_flag, "")
+
+ async def probe(cmd):
+ return True, "grok 1.0.3" if cmd[-1] == "--version" else help_text
+
+ monkeypatch.setattr("deeptutor.services.subagent.grok.probe_version", probe)
+ assert not (await GrokBackend().detect()).available
diff --git a/tests/utils/test_document_images.py b/tests/utils/test_document_images.py
index faf735e4f..c909dcdf5 100644
--- a/tests/utils/test_document_images.py
+++ b/tests/utils/test_document_images.py
@@ -185,7 +185,7 @@ def test_pdf_images_deduplicates_repeated_xref(tmp_path) -> None:
result = extract_pdf_images(pdf_path.read_bytes())
assert len(result.collection.images) == 1
- assert [page for page, _ in result.page_map] == [1]
+ assert result.page_map == ((1, (0,)), (2, (0,)), (3, (0,)))
def _png_for_index(index: int) -> bytes: