feat(series): assisted-continuation matcher + suggestion queue — backend (FC-6.3)
Confirm-only "this post may continue this series" matcher. - series_suggestion table (post_id, series_tag_id, score, signals jsonb, status pending|added|dismissed, UNIQUE(post,series)); migration 0041 + two settings knobs (series_suggest_enabled, series_suggest_threshold). - series_match_service: weighted additive score (title-stem / same-artist / page-continuity / shared-distinctive-tags), no single signal gating. The title "pattern" is derived on the fly from the post titles already in a series, so it sharpens as more are confirmed (no persisted state to drift). Candidates are bounded to the post's artist. match_post upserts pending suggestions (UNIQUE + on-conflict, respecting prior added/dismissed decisions). - accept reuses add_post_as_chapter then marks 'added'; dismiss marks 'dismissed'. - rescan_series_suggestions_task: settings-gated, time-boxed + self-resuming from a post-id cursor (maintenance_long lane), like normalize_tags_task. - API: GET /series/suggestions, POST .../<id>/accept|dismiss, POST .../rescan. - Settings: enabled + threshold exposed via /settings/import. - Tests: pure scoring helpers + matcher/accept/dismiss/rescan lifecycle + UNIQUE dedup. Frontend (Suggestions tab + settings card) lands next. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -147,3 +147,54 @@ def normalize_tags_task(self) -> dict:
|
||||
)
|
||||
normalize_tags_task.delay()
|
||||
return summary
|
||||
|
||||
|
||||
# Time-box one rescan chunk well under the soft limit and re-enqueue from the
|
||||
# cursor — scoring every post against its artist's series is O(posts) and grows
|
||||
# with the library (FC-6.3). Mirrors normalize_tags_task.
|
||||
_SERIES_RESCAN_CHUNK_SECONDS = 600
|
||||
|
||||
|
||||
@celery.task(
|
||||
name="backend.app.tasks.admin.rescan_series_suggestions_task",
|
||||
bind=True,
|
||||
autoretry_for=(OperationalError, DBAPIError),
|
||||
retry_backoff=15, retry_backoff_max=180, max_retries=1,
|
||||
soft_time_limit=1800, time_limit=2400, # 30 min / 40 min
|
||||
)
|
||||
def rescan_series_suggestions_task(self, after_post_id: int = 0) -> dict:
|
||||
"""Score posts against their artist's series and write pending suggestions
|
||||
(FC-6.3). Settings-gated; time-boxed + self-resuming from a post-id cursor.
|
||||
Per-task async engine (NullPool) under its own asyncio loop, like normalize."""
|
||||
import asyncio
|
||||
|
||||
from ..models import ImportSettings
|
||||
from ..services.series_match_service import SeriesMatchService
|
||||
from ._async_session import async_session_factory
|
||||
|
||||
async def _run() -> dict:
|
||||
async_factory, async_engine = async_session_factory()
|
||||
try:
|
||||
async with async_factory() as session:
|
||||
settings = await ImportSettings.load(session)
|
||||
if not settings.series_suggest_enabled:
|
||||
return {"skipped": "series suggestions disabled"}
|
||||
threshold = settings.series_suggest_threshold
|
||||
return await SeriesMatchService(session).rescan(
|
||||
threshold=threshold,
|
||||
time_budget_seconds=_SERIES_RESCAN_CHUNK_SECONDS,
|
||||
after_post_id=after_post_id,
|
||||
)
|
||||
finally:
|
||||
await async_engine.dispose()
|
||||
|
||||
summary = asyncio.run(_run())
|
||||
if summary.get("partial") and summary.get("resume_after_id", 0) > after_post_id:
|
||||
log.info(
|
||||
"rescan_series_suggestions chunk done (%d scanned, %d suggested, "
|
||||
"resume after %s) — re-enqueuing",
|
||||
summary.get("scanned", 0), summary.get("suggested", 0),
|
||||
summary["resume_after_id"],
|
||||
)
|
||||
rescan_series_suggestions_task.delay(summary["resume_after_id"])
|
||||
return summary
|
||||
|
||||
Reference in New Issue
Block a user