Skip to content

fix(nc-ocr-flow): fingerprint-verified skip guard for foreign OCR writers - #73

Merged
Likivik merged 1 commit into
mainfrom
ocr-foreign-rewrite-guard
Oct 17, 2026
Merged

Likivik merged 1 commit into
mainfrom
ocr-foreign-rewrite-guard

Conversation

@Likivik

@Likivik Likivik commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Why

Production has a second OCR pipeline (the third-party workflow_ocr Nextcloud app) installed on the same Nextcloud. It runs OCRmyPDF + Tesseract on the same postCreate/postWrite events, rewrites our output ~2 minutes after we upload, and replaces our readable Google text layer with a broken one that extracts 0 Cyrillic and ~4k orphan combining marks. The XMP stamp we write survives that rewrite, so the stamp-only skip logic records the file as stamped_good_by_us and the user's PDF silently contains broken OCR forever.

What

Add a fingerprint-verified skip guard that catches this. The infrastructure (_index_record already stores (size, etag) as the fingerprint of the file as we left it, and _propfind_size_etag does a Depth:0 PROPFIND) was already in place — only the comparison was missing.

G1: fingerprint-verified stamp skip

Before trusting the stamp we PROPFIND the live file on WebDAV and compare against the stored fingerprint:

  • match → skip as today (no extra work, counter reset for good measure)
  • mismatch → log a WARNING naming the path and BOTH fingerprints (stored vs live), bump the per-path counter, force re-OCR
  • stored NULL (row ended in error/gone, or never recorded) → skip as today at debug level; not treated as a mismatch

PROPFIND is paid only on the skip path. The stamp branch is rare in steady state; one PROPFIND is ~50–200 ms over a tailnet.

G2: bounded foreign-rewrite detection

Per-path counter (foreign_rewrites column on files, idempotent migration following the C6 / PR #68 pattern — ALTER TABLE ADD COLUMN ... DEFAULT 0 AFTER the schema script). After FOREIGN_REWRITE_LIMIT=3 consecutive foreign rewrites the guard:

  • logs ERROR once with the operator workflow ("remove the competing writer from the Nextcloud app store, or POST /rescan with force=true to clear the row")
  • stops bumping the counter (cap stays at 3; /stats stays bounded)
  • returns (False, REASON_FOREIGN_REWRITE) so the worker records the row without OCR'ing

The counter resets to 0 on the next fingerprint match, so the bound recovers once the competing writer is gone.

G3: surface it

  • /stats now includes foreign_rewrites (sum across all rows) — additive, the NC app's StatsService silently ignores unknown keys
  • One-line INFO at startup so an operator can grep for fingerprint-verified skip guard enabled to confirm the build has it

G4: tests

pkgs/nc-ocr-flow/tests/test_fingerprint_guard.py, 7 tests. Each one verified to FAIL when the corresponding piece of the fix is reverted (and PASS with the fix in place):

test guards verified fail without fix
test_fingerprint_mismatch_bypasses_skip_and_warns G1 mismatch yes (bypassed guard)
test_fingerprint_match_skips_as_today G1+G2 match reset yes (counter stays at 1)
test_null_stored_fingerprint_skips_as_today G1 NULL branch n/a (regression guard)
test_consecutive_foreign_rewrites_bound_to_limit G2 bound yes (no counter)
test_subsequent_mismatches_after_limit_are_silent G2 cap silence yes (counter grows)
test_stats_contains_foreign_rewrites G3 stats key yes (key missing)
test_fingerprint_guard_disabled_reverts_to_silent_skip self-test n/a (asserts the BAD path)

Baseline: 234 passed, 1 failed (test_orientation.py::test_takes_pp_lcnet_when_confident — pre-existing PP-LCNet ONNX model absence, unrelated). After this PR: 241 passed, same 1 failed.

Constraints respected

…ters

G1: stamp-skip now compares live WebDAV (size, etag) against the
stored fingerprint before trusting the stamp. Match → skip as today;
mismatch → log a WARNING naming BOTH fingerprints and re-OCR the
file. Stored fingerprint NULL (row ended in error/gone or never
recorded) → fall back to stamp-only skip at debug level.

G2: per-path counter (foreign_rewrites column, idempotent migration
following the C6 / PR #68 pattern) bounds the loop. After
FOREIGN_REWRITE_LIMIT=3 consecutive foreign rewrites the guard stops
auto-re-OCRing and records reason='foreign_rewrite' so the operator
knows the competing writer has won. ERROR logged exactly once on the
crossing; subsequent mismatches at the cap are silent.

G3: /stats.foreign_rewrites (sum across rows) added so the NC panel
can surface the cumulative count. Also a one-line INFO at startup
so an operator can confirm the guard is compiled in.

G4: tests/test_fingerprint_guard.py pins every behaviour:
  (a) stamp + mismatch → re-OCR, WARNING names stored+live,
  (b) stamp + match → skipped, counter reset,
  (c) NULL stored fingerprint → skipped as today, PROPFIND skipped,
  (d) 3 mismatches → reason='foreign_rewrite', cap stops the bump,
  (e) /stats.foreign_rewrites == SUM of per-path counters,
  (f) self-test that a no-op guard reverts to silent skip.
@Likivik
Likivik merged commit 9d7c4b4 into main Oct 17, 2026
7 checks passed
@Likivik
Likivik deleted the ocr-foreign-rewrite-guard branch October 17, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant