openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

BookWorm harvest: a truncated crawl advances the feed cursor past pages it never fetched

Open MohitSAGAR11 opened this issue 1 month ago • 2 comments

Problem

iter_pages() detects two truncation cases and records them in state["truncated"], and _report() warns when it sees the flag. For the max_pages cap that is adequate — it is documented as a testing-only flag and the operator chose it.

The other case is not operator-chosen. A feed whose rel="next" chain returns to an already-seen URL trips the loop guard at harvest.py:148-151, and the cursor advances anyway:

# openlibrary/bookworm/harvest.py:425 — native modified_since path
FeedRegistry.advance(feed.id, last_updated=now.replace(tzinfo=None))

# openlibrary/bookworm/harvest.py:489 — full-crawl path
FeedRegistry.advance(feed.id, last_updated=new_cursor.replace(tzinfo=None))

Neither call consults page_state. The function's own docstring states the consequence:

a truncated crawl that silently advances the cursor skips every page it never fetched, permanently

Recovery today depends on a human reading the _report() warning and manually resetting last_updated. If that line is missed, the skipped publications sit below the cursor and are never harvested again — no error, zero exit code.

The impact scales with automation. Nothing invokes a harvest on a schedule today, so this is currently reachable only through manual runs. Once a scheduled tick exists, an unattended run that trips the loop guard loses a window with nobody watching.

Reproducing the bug

  1. Register a feed whose rel="next" chain returns to an already-seen URL (a provider-side pagination defect; docker/mockservices can emulate it).
  2. Run harvest_feed() against it.
  3. Inspect the result and the feed row.
  • Expected behavior: the cursor does not move past pages that were never fetched — either left unchanged, or advanced only to a watermark covering pages actually fetched.
  • Actual behavior: result["truncated"] is True, and feed.last_updated has advanced regardless. The unfetched pages are now below the cursor.

Context

  • Browser (Chrome, Safari, Firefox, etc): n/a
  • OS (Windows, Mac, etc): n/a
  • Logged in (Y/N): n/a
  • Environment (prod, dev, local): applies to all; currently reachable only via manual/CLI harvest runs

openlibrary/bookworm/tests/test_harvest.py already asserts result["truncated"] is True (lines 248 and 261), so the flag itself is covered. What is not covered is what the cursor does when that flag is set.

Breakdown

Implementation Details (for maintainers)

Requirements Checklist

  • [ ] harvest.py:425 and :489 consult page_state before advancing
  • [ ] Decide between leaving the cursor unchanged on truncation, or advancing to a watermark derived only from fetched pages
  • [ ] Keep the existing _report() warning either way — it is still the operator's signal
  • [ ] Test that a truncated crawl does not move last_updated past unfetched pages

Related files

  • openlibrary/bookworm/harvest.py — iter_pages, _harvest_native, _harvest_by_full_crawl
  • openlibrary/bookworm/registry.py — FeedRegistry.advance
  • openlibrary/bookworm/cli.py — _report
  • openlibrary/bookworm/tests/test_harvest.py

Open question, worth a ruling rather than a guess

The two paths may not want the same answer. The native modified_since path advances to now, so simply not advancing on truncation is clean. The full-crawl path advances to min(max_modified, now), and on a truncated crawl max_modified already reflects only the pages that were fetched — so it may be approximately correct already, or may still skip older records sitting in pages that were never retrieved.

Stakeholders

  • @mekarpeles — lead on #12655 / #13775. Found while tracing the ingest path for #14052.

Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

MohitSAGAR11 avatar Oct 24 '26 11:10 MohitSAGAR11

Labels landed automatically just after I posted that — disregard. Happy to implement this if it's wanted; the only thing holding a PR back is the open question above, whether the full-crawl path needs the same treatment as the native one.

MohitSAGAR11 avatar Oct 24 '26 11:10 MohitSAGAR11

How many issues are you proposing to work on simultaneously?

tfmorris avatar Oct 27 '26 05:10 tfmorris