Skip to content

fix(nc-ocr-flow): correctness fixes across the Vision engine and job pipeline - #64

Merged
Likivik merged 2 commits into
mainfrom
ocrfix-vision-pipeline
Oct 16, 2026
Merged

Likivik merged 2 commits into
mainfrom
ocrfix-vision-pipeline

Conversation

@Likivik

@Likivik Likivik commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Why

A static review of the OCR path (plus one live production symptom) turned up a batch of confirmed defects. The only user-visible one so far was pages_month stuck at 0 with 98 jobs, but several are latent hard failures.

Engine (Google Vision)

  • vlm_pages was always empty → pages_month/cost display reported 0 for every job while jobs_month grew. Both google_engine and tesseract_engine now populate it with real page indices.
  • 20 MB guard was wrong: it raised "use the GCS path" although GCS upload is the only path (the comment in the same function says so). Every legitimate scanned PDF >20 MB hard-failed. Replaced with a 200 MB ceiling.
  • _page_dims returned (0, 0) when Vision omitted pages[]; the normalized-vertex branch then dropped every Word → zero blocks → status=error. Now falls back to the local source PDF's page rect, and otherwise raises loudly instead of silently zeroing.
  • Async-batch LRO timeout 300 s → 1800 s, with elapsed time + operation name in the error.
  • nix: NC_OCR_GOOGLE_LOCATION and NC_OCR_GOOGLE_BATCH_TIMEOUT are now set explicitly (location must match the bucket's region).

Pipeline (webhook server)

  • _webdav_delete now calls raise_for_status() inside the retry closure (download/upload already did), so a 5xx on the final attempt is retried instead of leaving both the source image and the new PDF in Nextcloud.
  • Tesseract results are checked for an actual text layer before being stamped done — an empty layer used to overwrite the user's original indistinguishably.
  • cost_usd subtracts the 1000-page free tier.
  • Quota reading picks the dimensionsInfos entry for the configured region instead of dims[0] (which could be another region's limit), with a conservative 1800 fallback + warning.
  • /webhook returns 400 on malformed JSON instead of 500 (no more Nextcloud retry-storm).
  • Image branch checks for an existing target PDF before the paid Vision call.
  • /stats now states its contract: errors (unique paths), gone (file vanished from NC), errors_attempts (from the jobs table). Production showed errors: 36 vs 0 error rows in jobs — all 36 were stale rows for deleted test probes. A 404 on a queued path is now recorded as gone, not a permanent error.
  • _enqueue takes a per-path lock, closing the duplicate-enqueue race (in-process; a multi-process deployment still wants a jobstore-level transaction).
  • PROPFIND parsing accepts any namespace prefix instead of literal <d:href>.
  • _PROCESSED_TTL is tunable via the NC_OCR_PROCESSED_TTL setting/env.

Not fixed here

Bounded automatic retry of transient status='error' jobs — deferred, it needs a sweeper design rather than a patch.

Tests

cd pkgs/nc-ocr-flow && uv sync --group dev && .venv/bin/python -m pytest tests/ -q → 196 passed, 1 failed. The failure is test_orientation.py::TestDetectOrientation::test_takes_pp_lcnet_when_confident (local PP-LCNet model unavailable; same failure on unmodified main, unrelated to these files).

…ms, batch timeout

- google_engine/tesseract_engine: populate vlm_pages so the billable page
  counter (pages_month) and cost display stop reporting 0 for every job.
- google_engine: replace the 20 MB inline-content guard (a leftover from a
  removed inline design; the only path is GCS upload) with a 200 MB ceiling,
  so legitimate scanned PDFs above 20 MB no longer hard-fail.
- google_engine: _page_dims no longer silently returns (0,0) — it falls back
  to the local source PDF's page rect and otherwise raises, instead of
  dropping every Word and turning the job into status=error.
- google_engine: async-batch LRO timeout default 300s -> 1800s, with elapsed
  time and operation name in the TimeoutError.
- workflow-ocr/default.nix: expose NC_OCR_GOOGLE_LOCATION and
  NC_OCR_GOOGLE_BATCH_TIMEOUT.
…yer, enqueue race)

- _webdav_delete: raise_for_status now runs inside the retry closure, like
  download/upload, so a 5xx on the final attempt is actually retried.
- tesseract: assert the produced PDF really carries a text layer before
  stamping done — an empty layer used to overwrite the user's original.
- cost_usd: subtract the 1000-page monthly free tier.
- quota: pick the dimensionsInfos entry matching NC_OCR_GOOGLE_LOCATION
  instead of dims[0], falling back to 1800 with a warning.
- /webhook: malformed JSON now returns 400 instead of 500 (NC retry-storm).
- image branch: check for an existing target PDF before the paid Vision call.
- /stats: state the contract explicitly — errors (unique paths), gone
  (file vanished from NC), errors_attempts (from the jobs table). A 404 on a
  queued path is recorded as 'gone' rather than a permanent 'error'.
- _enqueue: per-path lock closes the duplicate-enqueue race.
- PROPFIND parsing: accept any namespace prefix instead of literal <d:href>.
- _PROCESSED_TTL: tunable via NC_OCR_PROCESSED_TTL setting/env.
@Likivik
Likivik merged commit 920eb20 into main Oct 16, 2026
7 checks passed
@Likivik
Likivik deleted the ocrfix-vision-pipeline branch October 16, 2026 15:44
Likivik added a commit that referenced this pull request Nov 12, 2026
* deploy(poweredge): bump ocrflow to ebaf690 (engine policy, scoped status API, claim guard)

* deploy(poweredge): ocrflow 5559437 — one engine option, readable Google key

* deploy(poweredge): ocrflow 1fba10e — seeding unit names the real nextcloud user

* deploy(poweredge): ocrflow 937db35 — Google batch project id

* poweredge(ocr): point google-ocr at the ocr-project-tmp bucket, not a bucket it may create

* poweredge(ocr): worker moves bytes over the app API, not WebDAV

* deploy(poweredge): ocrflow api-transport fixes (byte fetch + enqueue id)

* deploy(poweredge): ocrflow pages/chars reporting

* feat(nc-rag): enable cross-encoder reranking in nextcloud-mcp

SEARCH_RERANK_URL points at serenity's llama-reranker (:8082/v1/rerank)
via the direct Cohere-protocol path, so no gateway M2M token is needed.
Also opens 8082 on tailscale0 explicitly instead of relying on the port
happening to be reachable.

* fix(nc-rag): rerank top-50 with a 120s timeout

The upstream 30s default times out when 200 chunks of up to 2000 chars are
scored on the 1660, and nc-mcp degrades silently to retrieval order — so
reranking was on paper only. Measured ~0.09s/doc; 50 candidates keeps
latency ~8s and 120s absorbs a cold GPU.

* deploy(poweredge): four OCR failure-class fixes

* deploy(poweredge): real failure sites (#57)

* deploy(poweredge): pdfio, reason codes, empty-source skip, readiness, retry

* deploy(poweredge): reason codes, stage events, 422 classification (#64-#69)

* feat(hermes): bump to v0.23.1 with a local patchelf gate

v0.23.1's nix/pm-packages.nix backports NixOS/patchelf#672 onto patchelf
unconditionally. That patch is written for 0.19 and rejects 4/6 hunks
against the 0.15.2 that nixpkgs reverted to (61a15beb37, 2026-10-30,
tracking #577387), so nothing built.

- apply upstream PR NousResearch/hermes-agent#152542 (gates the backport
  to 0.19.0-0.19.2) over the flake source and callPackage from it
- use hermes-agent's OWN uv2nix/pyproject stack: its python.nix passes
  default-groups, which our older uv2nix rejects
- pin nix/pnpm-deps-hash to the store hash fetchPnpmDeps produces on our
  nixpkgs; upstream's was made with 02c087f

Drop both patches once a released tag carries the gate and we bump nixpkgs.

* test(smoke): read the expected hermes version from /etc

The probe hardcoded *v0.21.7*, so the 0.23.1 build reported HERMES_VERSION_BAD
and the gate failed on a stale literal rather than on the system.

The aspect now writes /etc/hermes-expected-version from the same hermesVersion
that builds the package; the probe compares against it and keeps a literal only
as a loud fallback.

VM smoke: PASS (28 running / 68 loaded services, both ZFS pool imports
allowlisted as guest-impossible).

---------

Co-authored-by: Likivik <[email protected]>
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