Repository navigation
fix(nc-ocr-flow): fingerprint-verified skip guard for foreign OCR writers - #73
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Production has a second OCR pipeline (the third-party
workflow_ocrNextcloud 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 asstamped_good_by_usand the user's PDF silently contains broken OCR forever.What
Add a fingerprint-verified skip guard that catches this. The infrastructure (
_index_recordalready stores(size, etag)as the fingerprint of the file as we left it, and_propfind_size_etagdoes 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:
error/gone, or never recorded) → skip as today at debug level; not treated as a mismatchPROPFIND 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_rewritescolumn onfiles, idempotent migration following the C6 / PR #68 pattern —ALTER TABLE ADD COLUMN ... DEFAULT 0AFTER the schema script). AfterFOREIGN_REWRITE_LIMIT=3consecutive foreign rewrites the guard:/statsstays bounded)(False, REASON_FOREIGN_REWRITE)so the worker records the row without OCR'ingThe counter resets to 0 on the next fingerprint match, so the bound recovers once the competing writer is gone.
G3: surface it
/statsnow includesforeign_rewrites(sum across all rows) — additive, the NC app's StatsService silently ignores unknown keysfingerprint-verified skip guard enabledto confirm the build has itG4: 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_fingerprint_mismatch_bypasses_skip_and_warnstest_fingerprint_match_skips_as_todaytest_null_stored_fingerprint_skips_as_todaytest_consecutive_foreign_rewrites_bound_to_limittest_subsequent_mismatches_after_limit_are_silenttest_stats_contains_foreign_rewritestest_fingerprint_guard_disabled_reverts_to_silent_skipBaseline: 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
pkgs/nc-ocr-flow/