Fix doc_modeler skipping already-indexed docs

The doc_modeler gated its query on `FileGroup.indexed == False`, but
`indexed` is a full-text-search flag that `apply_indexers` sets on every
FileGroup during a refresh. Because the refresh pipeline runs
apply_modelers -> apply_indexers, any doc file that reached apply_indexers
without first receiving a Doc row (modeler errored, was interrupted, or
files imported already-indexed) got `indexed=1` and was then excluded by
the `indexed == False` gate forever -- leaving it with model=NULL, no Doc
row, and invisible in /api/docs.

On one library this stranded 20,795 PDFs/epubs/mobis (only the 10
individually-tagged docs, modeled via the per-file do_model path, showed
up). No doc-mimetype files were left with indexed=0, so nothing would
ever recover them.

Gate the modeler on "no Doc row yet OR explicitly flagged for
re-indexing" instead, so already-indexed docs are picked up on the next
refresh while preserving re-index behavior and skipping already-modeled
docs. Self-healing: a global refresh now models the backlog.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-20 12:46:46 -06:00 • committed by lrnselfreliance
parent cdcf771ffa
commit 56c3a16839
2 changed files with 38 additions and 4 deletions

View file

@ -27,14 +27,22 @@ async def doc_modeler(progress_callback: Callable[[int], None] = None):
total_processed = 0
while True:
with get_db_session(commit=True) as session:
file_groups = session.query(FileGroup, Doc).filter(
file_groups = session.query(FileGroup, Doc) \
.outerjoin(Doc, Doc.file_group_id == FileGroup.id) \
.filter(
or_(
*[FileGroup.mimetype == mt for mt in DOC_MIMETYPES],
*[FileGroup.primary_path.like(f'%{suffix}') for suffix in COMIC_BOOK_SUFFIXES],
),
FileGroup.indexed == False,
).outerjoin(Doc, Doc.file_group_id == FileGroup.id) \
.limit(DOC_PROCESSING_LIMIT)
# Model any doc that has no Doc row yet (even if `apply_indexers` already set
# `indexed=True` before this modeler ran), or any doc explicitly flagged for
# re-indexing. Gating solely on `indexed == False` left already-indexed docs
# permanently unmodeled and invisible in /api/docs.
or_(
Doc.id.is_(None),
FileGroup.indexed == False,
),
).limit(DOC_PROCESSING_LIMIT)
file_groups: List[Tuple[FileGroup, Doc]] = list(file_groups)
processed = 0

View file

@ -116,6 +116,32 @@ async def test_doc_do_model_sets_model(test_session, example_pdf):
assert file_group.indexed is True
@pytest.mark.asyncio
async def test_doc_modeler_models_indexed_filegroup_without_doc(async_client, test_session, test_directory,
example_pdf):
"""A doc-mimetype FileGroup that was already indexed (indexed=True) but never modeled (no Doc
row) must still be picked up by the doc_modeler.
Regression: apply_indexers marks every FileGroup indexed=True. If a doc file gets indexed
before it is modeled (e.g. the modeler errored, or the file was imported already-indexed), the
old `indexed == False` gate excluded it forever -- leaving thousands of docs invisible in
/api/docs even though they exist on disk.
"""
# Simulate the stuck production state: the FileGroup has been swept by apply_indexers
# (indexed=True) but has no Doc row and no model.
file_group = FileGroup.from_paths(test_session, example_pdf)
file_group.indexed = True
file_group.model = None
test_session.commit()
assert test_session.query(Doc).count() == 0, 'Precondition: no Doc row exists yet'
await doc_modeler()
doc: Doc = test_session.query(Doc).one()
assert doc.file_group_id == file_group.id
assert doc.file_group.model == 'doc'
@pytest.mark.asyncio
async def test_doc_modeler_processes_more_than_batch_limit(async_client, test_session, test_directory):
"""Doc modeler processes more than one batch of files (limit=10)."""