From 8ea27e05d070264505d0e3e32bc12f1a4b818476 Mon Sep 17 00:00:00 2001 From: Stephen Masters Date: Mon, 2 Nov 2026 23:38:43 +0000 Subject: [PATCH] fix(batch): hold a refused file in the triage queue with its reason A file sillybos refuses before any decoder reads it fell into the generic error handler: printed, counted as a failed run, and left in scans/ for the next run to refuse again. It now goes to the triage queue like a poor transcript does, with the refusal as its reason, a manifest and a record with no candidates, and the exit code stays 0. Brief 022's test of the arrangement is that a batch seeded with hostile files holds each with a readable reason and processes the rest. Co-authored-by: Yvette Mankerian Co-Authored-By: Claude Fable 5.1 --- AGENTS.md | 4 ++- docs/guide/command-line.md | 9 ++++++ src/tetrak_ocr/batch.py | 58 +++++++++++++++++++++++++++++++++++++- tests/ocr/test_batch.py | 55 ++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 6bfb677..c50b68a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -119,7 +119,9 @@ list — add a backend there and the CLI, the harness and the docs all pick it u `workspace/triage/`. It judges a transcript, not an engine, so it is a flag rather than a backend (an `auto-local-fast` backend was tried and withdrawn). It is opt-in because it changes which files reach `workspace/processed/`, and -needs the `qa` extra, checked up front rather than on the first file. +needs the `qa` extra, checked up front rather than on the first file. A file +sillybos refuses before reading (`InputRefusedError`) goes to the same queue +with the refusal as its reason, not into the run's error count. ## Conventions that matter diff --git a/docs/guide/command-line.md b/docs/guide/command-line.md index ff48b9e..a061486 100644 --- a/docs/guide/command-line.md +++ b/docs/guide/command-line.md @@ -120,6 +120,15 @@ by hand: [`tetrak-ocr triage`](#tetrak-ocr-triage) lists the queue from the records. +A file that is refused before any decoder reads it goes to the same queue: +a PostScript file called `.png`, a page over the pixel limit, a PDF with +JavaScript, each decided by [sillybos](https://github.com/scattercode/sillybos) +before a decoder opens the file. Its manifest and record +carry the refusal as the reason ("says PNG, is PostScript"), with no +transcript and no scores, and it counts as triaged rather than as an error, +so the exit code stays 0 and the file is not left in `workspace/scans/` for +the next run to refuse again. + `--quality-gate` applies the same threshold to any single named backend: ```bash diff --git a/src/tetrak_ocr/batch.py b/src/tetrak_ocr/batch.py index 0372654..dd48532 100644 --- a/src/tetrak_ocr/batch.py +++ b/src/tetrak_ocr/batch.py @@ -30,7 +30,7 @@ from .qa_score import LowQualityError from . import orientation, outputs, telemetry, triage -from .errors import MissingFeatureError +from .errors import InputRefusedError, MissingFeatureError from .imaging import page_documents from .layout import TextSpan, spans_of from .manifest import DEFAULT_NAME as MANIFEST_NAME @@ -192,6 +192,33 @@ def _send_to_triage(image_path: Path, error: "LowQualityError", backend: str | N print(f" → TRIAGE: {record.name} (+ {len(transcripts)} full transcript(s))") +def _hold_refused(image_path: Path, error: InputRefusedError, backend: str | None = None) -> Path: + """Move a file refused before reading to the triage queue, with its reason. + + A refusal is not a poor transcript: there are no scores and nothing to + excerpt, so the manifest says only what sillybos found ("says PNG, is + PostScript"; "412 pages; the limit is 500") and the record carries the + same sentence with no candidates. The file joins the queue rather than + staying in scans/, where the next run would only refuse it again, so the + person running the batch hears why once, in the place they look for + everything else that needs a decision. Returns the manifest's path. + """ + TRIAGE_DIR.mkdir(parents=True, exist_ok=True) + manifest_path = TRIAGE_DIR / f"{image_path.stem}.md" + manifest_path.write_text( + f"# {image_path.stem}\n\n## Refused before reading\n\n{error.reason}\n", encoding="utf-8" + ) + record, *_ = triage.hold(TRIAGE_DIR, image_path, {}, {}, reason=error.reason, backend=backend) + + dest = TRIAGE_DIR / image_path.name + shutil.move(str(image_path), dest) + + print(f" → TRIAGE: {dest.name} (refused before reading: {error.reason})") + print(f" → TRIAGE: {manifest_path.name}") + print(f" → TRIAGE: {record.name}") + return manifest_path + + def _relative(path: Path) -> str: """*path* relative to the workspace, for the manifest; as given if outside it.""" try: @@ -257,6 +284,13 @@ def process_file( *quality_gate* is set, this function scores that transcript and applies the same threshold. + A file that sillybos refuses before any decoder reads it (a PostScript + file called .png, a pixel bomb, a PDF with JavaScript) goes to the same + queue, with the refusal as its reason and no transcript, rather than into + the run's error count: it is a decision for a person, not a failure of + the pipeline. The orientation check catches its own refusals and leaves + the reading to refuse the file, so every refusal arrives here. + The gate is opt-in for single backends rather than always on: it changes which files reach workspace/processed/, and turning it on silently would start diverting output for anyone already running `--backend tesseract`. @@ -378,6 +412,25 @@ def _triage(exc: "LowQualityError") -> None: **file_info, ) + def _refuse(exc: InputRefusedError) -> None: + manifest_path = _hold_refused(image_path, exc, backend) + if triaged is not None: + triaged.append(image_path.name) + _record( + "triage", + outputs=[_relative(manifest_path)], + location=_relative(TRIAGE_DIR / image_path.name), + note=exc.reason, + ) + telemetry.record( + "file_finish", + backend=backend, + outcome="refused", + seconds=_elapsed(), + reason=exc.reason, + **file_info, + ) + # A multi-page searchable PDF needs one transcript per page, so that case # transcribes page by page. Everything else keeps the single # whole-document call, which is about twice as fast -- nobody should pay @@ -406,6 +459,9 @@ def _triage(exc: "LowQualityError") -> None: reading.update(_auto_local_choice()) _triage(exc) return + except InputRefusedError as exc: + _refuse(exc) + return except Exception as exc: telemetry.record( "file_finish", diff --git a/tests/ocr/test_batch.py b/tests/ocr/test_batch.py index 4aea904..36fcc98 100644 --- a/tests/ocr/test_batch.py +++ b/tests/ocr/test_batch.py @@ -15,6 +15,7 @@ from __future__ import annotations +import json import shutil from pathlib import Path @@ -113,6 +114,60 @@ def test_every_corpus_image_is_processed(self, working_dirs, corpus_images): assert f"{image.stem}.md" in produced, f"no transcript for {image.name}" +class TestRefusedInput: + """A file refused before reading goes to triage, not into the error count. + + sillybos refuses a file before any decoder opens it: a PostScript file + called .png, a pixel bomb, a PDF carrying JavaScript. The refusal used to + fall into the generic error handler, which printed it, counted it as a + failed run, and left the file in scans/ for the next run to refuse + again. The test of the whole arrangement is that a batch seeded with a + hostile file holds it with a readable reason and processes the rest. + """ + + @staticmethod + def _read_like_a_backend(monkeypatch): + """An OCR function that opens the file the way every backend does.""" + from tetrak_ocr import batch + from tetrak_ocr.imaging import open_image + + def ocr(path: Path) -> str: + with open_image(path): + return "a transcript" + + monkeypatch.setattr(batch, "build_ocr_fn", lambda *a, **k: ocr) + + def test_a_refused_file_is_held_with_its_reason(self, working_dirs, monkeypatch, capsys): + (working_dirs["scans"] / "scroll.png").write_bytes(b"%!PS-Adobe-3.0\n0 0 moveto\n") + _write_legible_image(working_dirs["scans"] / "page.png") + self._read_like_a_backend(monkeypatch) + + assert run_batch(["--backend", "tesseract"]) == 0 + + held = working_dirs["triage"] + assert (held / "scroll.png").exists() + assert not (working_dirs["scans"] / "scroll.png").exists() + assert not (working_dirs["processed"] / "scroll.png").exists() + assert "says PNG, is PostScript" in (held / "scroll.md").read_text() + record = json.loads((held / "scroll.json").read_text()) + assert record["reason"] == "says PNG, is PostScript." + assert record["candidates"] == [] and record["best"] is None + assert (working_dirs["processed"] / "page.png").exists() + assert "Triaged (1): scroll.png" in capsys.readouterr().out + + def test_the_queue_lists_a_refused_file(self, working_dirs, monkeypatch): + from tetrak_ocr import triage + + (working_dirs["scans"] / "scroll.png").write_bytes(b"%!PS-Adobe-3.0\n") + self._read_like_a_backend(monkeypatch) + run_batch(["--backend", "tesseract"]) + + [item] = triage.queue(working_dirs["triage"]) + + assert item.file == "scroll.png" and item.best_score is None + assert "says PNG, is PostScript" in triage.describe([item], working_dirs["triage"]) + + class TestQualityGate: """`--quality-gate` extends triage to any single backend.