BookWorm harvest: a truncated crawl advances the feed cursor past pages it never fetched
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
- Register a feed whose
rel="next"chain returns to an already-seen URL (a provider-side pagination defect;docker/mockservicescan emulate it). - Run
harvest_feed()against it. - 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, andfeed.last_updatedhas 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:425and:489consultpage_statebefore 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_updatedpast unfetched pages
Related files
openlibrary/bookworm/harvest.py—iter_pages,_harvest_native,_harvest_by_full_crawlopenlibrary/bookworm/registry.py—FeedRegistry.advanceopenlibrary/bookworm/cli.py—_reportopenlibrary/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.
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.
How many issues are you proposing to work on simultaneously?