Skip to content

fix(nc-ocr-flow): combine A1-A3 + B1-B6 audit fixes on top of #65/#66/#68 - #69

Merged
Likivik merged 2 commits into
mainfrom
ocrfix-combined
Oct 17, 2026
Merged

Likivik merged 2 commits into
mainfrom
ocrfix-combined

Conversation

@Likivik

@Likivik Likivik commented Oct 17, 2026

Copy link
Copy Markdown
Owner

Summary

Rebase and combine the two audit change sets onto current main, with conflicts resolved and tests green.

A1-A3 (engine/text fixes)

  • A1: _strip_existing_text gains an optional page_idx parameter; _finalize now strips only the pages it is about to overwrite, so a page whose engine returned no blocks keeps its pre-existing text layer.
  • A2: google_engine._aggregate_json_results returns list[tuple[int, OcrResult]] with REAL 0-indexed source page indices and warns on duplicate pageNumber. vlm_pages is no longer list(range(n)).
  • A3: google_engine._page_dims local-PDF fallback renders at the new _PAGE_DIMS_FALLBACK_DPI = 200 and returns PIXELS, not PDF points.

B1-B6 (webhook/jobstore/orientation fixes)

  • B1: force=True no longer bypasses the per-path in-flight gate in _enqueue. Two concurrent force callers collapse to ONE jobs row, returning status='already_running' naming the existing job. Added jobstore.in_flight_job_for_path.
  • B2: retry counters no longer double-count. /stats now spreads retries_total + retries_queued into the payload the NC app reads.
  • B3: _webdav_exists treats HTTP 410 Gone like 404. Switched the call to requests.request("HEAD", ...) so the standard test pattern of patching ws.requests.request actually intercepts it (requests.head captures request at import time and bypasses attribute lookup on the parent module).
  • B4: jobstore._prune skips rows waiting on a backoff (COALESCE(next_run_at,0) <= now).
  • B5: _ENQUEUE_LOCKS got a bounded 1h idle TTL sweep via _ENQUEUE_LOCK_TTL_S = 3600.0 and _ENQUEUE_LOCKS_LAST_USED.
  • B6: the expensive orientation precheck was REMOVED. preorient_pages_safe calls preorient_pdf exactly once; _detect_all_pages_orientations does not exist; tests/test_orientation.py::TestNoExpensivePrecheck asserts its absence.

Conflicts resolved

  • jobstore.py (6 conflicts) — both sides added attempts / next_run_at columns and a retry-counting stats(). Kept main's _migrate_add_retry_columns helper (the PR fix(nc-ocr-flow): create the retry index after the ALTERs (jobstore init was dead on existing DBs) #68 ordering fix that puts the index AFTER the ALTERs and never in the schema script) and main's attempts_i / next_run_at_f typed normalization. Reconciled the schema column defs (INTEGER DEFAULT 0 / REAL DEFAULT 0) and dropped B's inline ALTER loop + redundant CREATE INDEX idx_jobs_pending (the ordering bug PR fix(nc-ocr-flow): create the retry index after the ALTERs (jobstore init was dead on existing DBs) #68 fixed). Adopted main's two-COUNT implementation of the retry counters; tests pass with both retries_total and retries_queued exposed.
  • webhook_server.py (2 conflicts) — adopted main's _webdav_exists structure (with timeout parameter) and B3's 410-as-gone behaviour. Adopted main's jobstore.stats() call site in /stats and added the B2 retries_total / retries_queued spread from the same js handle.
  • test_webhook_api.py (1 conflict) — both sides added disjoint test blocks (main's C1-C6 + B's B1-B6). Kept both and stripped the conflict markers.
  • orientation.py — removed the precheck call and the _detect_all_pages_orientations function (B6). Main's docstring already described the regression; preserved the audit contract.
  • Also dropped the duplicate _maybe_schedule_retry(job) 1-arg variant B introduced (it shadowed main's 3-arg signature that the tests depend on) and removed the now-redundant finally-block call (main's exception handlers already schedule retries for every transient failure).

Tests

cd pkgs/nc-ocr-flow && uv sync && uv pip install httpx2 google-cloud-storage google-cloud-vision
LD_LIBRARY_PATH="$(nix eval nixpkgs#gcc.cc.lib --raw)/lib:$LD_LIBRARY_PATH" .venv/bin/python -m pytest tests/ -q
  • 234 passed, 1 failed (the known local-only test_takes_pp_lcnet_when_confident that needs the ONNX PP-LCNet model file absent in this environment; passes in CI).

Notes

  • Did NOT bump package version again — A already took 0.3.0 → 0.4.0 in uv.lock; preserved.
  • The PR-branch uv.lock is unchanged from main (only test-runner-only packages installed locally are not part of the project's lockfile).

A1 _strip_existing_text now has an optional page_idx parameter;
  callers passing one strip only that page, callers passing None
  (or omitting it) keep the old whole-doc behaviour. _finalize
  uses the per-page form so a page whose engine returned no blocks
  retains its pre-existing text layer.

A2 google_engine._aggregate_json_results returns
  list[tuple[int, OcrResult]] with REAL 0-indexed source page
  indices and warns on duplicate pageNumber; vlm_pages is no longer
  list(range(n)).

A3 google_engine._page_dims local-PDF fallback renders at the new
  _PAGE_DIMS_FALLBACK_DPI = 200 and returns PIXELS, not PDF points.
…obstore/orientation fixes

A1 per-page text strip in _finalize: pages whose engine result has no
  blocks keep their pre-existing text layer; _strip_existing_text gained
  an optional page_idx=None parameter (None = old whole-doc behaviour).

A2 google_engine._aggregate_json_results returns
  list[tuple[int, OcrResult]] with REAL 0-indexed source page indices
  and warns on duplicate pageNumber, so vlm_pages is no longer
  list(range(n)).

A3 google_engine._page_dims local-PDF fallback now renders at a new
  _PAGE_DIMS_FALLBACK_DPI = 200 and returns PIXELS, not PDF points.

B1 force=True no longer bypasses the per-path in-flight gate in _enqueue.
  Two concurrent force callers collapse to ONE jobs row, returning
  status 'already_running' naming the existing job.
  Added jobstore.in_flight_job_for_path.

B2 retry counters no longer double-count and /stats now spreads
  retries_total + retries_queued into the payload the NC app reads.

B3 _webdav_exists treats HTTP 410 Gone like 404.

B4 jobstore._prune skips rows waiting on a backoff
  (COALESCE(next_run_at,0) <= now).

B5 _ENQUEUE_LOCKS got a bounded 1h idle TTL sweep.

B6 the expensive orientation precheck was REMOVED:
  preorient_pages_safe calls preorient_pdf exactly once;
  _detect_all_pages_orientations does not exist;
  a regression-guard test asserts its absence.

Conflicts resolved:
  - jobstore.py:6 conflicts around the C6 retry migration and the
    stats() retry counters. Kept main's _migrate_add_retry_columns
    helper (PR #68 ordering fix) and main's typed-docstring style for
    attempts_i/next_run_at_f. Reconciled the schema column defs and
    the inline ALTER loop into one migration path with the index
    AFTER the ALTERs (PR #68 invariant).
  - webhook_server.py:2 conflicts. Adopted main's _webdav_exists
    structure (with timeout parameter) and B3's 410-as-gone behaviour
    (now uses requests.request("HEAD", ...) so the standard test
    monkeypatching ws.requests.request intercepts the call). Adopted
    main's jobstore.stats() call site in /stats and added the B2
    retries_total/retries_queued spread from the same js handle.
  - test_webhook_api.py:1 conflict. Both sides added disjoint test
    blocks (main's C1-C6 and B's B1-B6); kept both, stripped the
    conflict markers.
  - orientation.py: removed the precheck call and the
    _detect_all_pages_orientations function (B6). Main's docstring
    already described the regression; preserved the audit contract.

Kept the A-side 0.3.0 -> 0.4.0 uv.lock version bump (package version
was already bumped in A; no further change).
@Likivik
Likivik merged commit bf35f41 into main Oct 17, 2026
7 checks passed
@Likivik
Likivik deleted the ocrfix-combined branch October 17, 2026 18:19
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