Skip to content

Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.

fix(pdf): discard stale inspection metadata (#220)

Stamina90d538c8506f6
diff --git a/book_to_skill/pdf_inspector_integration.py b/book_to_skill/pdf_inspector_integration.py
index 8f9ce9d..f1e4616 100644
--- a/book_to_skill/pdf_inspector_integration.py
+++ b/book_to_skill/pdf_inspector_integration.py
@@ -191,12 +191,15 @@ def install_pdf_inspector_hook(utils_module: Any | None = None) -> None:
return
def wrapped(input_path: Path, extraction_mode: str, install_mode: str) -> dict[str, Any]:
+ inspection_key = str(input_path.resolve())
+ _INSPECTIONS.pop(inspection_key, None)
+
if not _looks_like_pdf(input_path):
return original(input_path, extraction_mode, install_mode)
markdown, inspection = inspect_pdf(input_path)
if inspection is not None:
- _INSPECTIONS[str(input_path.resolve())] = inspection
+ _INSPECTIONS[inspection_key] = inspection
if extraction_mode == "text" and markdown and inspection:
result = _result_from_inspector(utils_module, input_path, markdown, inspection)
diff --git a/tests/test_pdf_inspector_integration.py b/tests/test_pdf_inspector_integration.py
index a3adb59..e8cce59 100644
--- a/tests/test_pdf_inspector_integration.py
+++ b/tests/test_pdf_inspector_integration.py
@@ -102,6 +102,43 @@ def test_hook_uses_existing_pipeline_when_inspector_metadata_is_invalid(
assert integration._INSPECTIONS == {}
+def test_failed_reinspection_does_not_reuse_stale_metadata(tmp_path, monkeypatch):
+ integration._reset_state_for_tests()
+ pdf = tmp_path / "book.pdf"
+ pdf.write_bytes(b"%PDF-1.7\nfixture")
+ inspections = iter(
+ [
+ (None, {"engine": "pdf-inspector", "confidence": 0.5}),
+ (None, None),
+ ]
+ )
+ monkeypatch.setattr(integration, "inspect_pdf", lambda _path: next(inspections))
+
+ fake_utils = SimpleNamespace(
+ extract_single_file=lambda *_args: {"extraction_method": "legacy"}
+ )
+ integration.install_pdf_inspector_hook(fake_utils)
+
+ fake_utils.extract_single_file(pdf, "text", "no")
+ fake_utils.extract_single_file(pdf, "text", "no")
+
+ metadata_path = tmp_path / "metadata.json"
+ metadata_path.write_text(
+ json.dumps(
+ {
+ "total_sources": 1,
+ "sources": [{"source_file": str(pdf.resolve())}],
+ }
+ ),
+ encoding="utf-8",
+ )
+ integration.enrich_pdf_inspector_metadata(metadata_path)
+ metadata = json.loads(metadata_path.read_text(encoding="utf-8"))
+
+ assert "pdf_inspector" not in metadata
+ assert "pdf_inspector" not in metadata["sources"][0]
+
+
def test_hook_uses_inspector_for_clean_text_pdf(tmp_path, monkeypatch):
integration._reset_state_for_tests()
pdf = tmp_path / "book.pdf"