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

@ -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)."""