"""Last-resort acceptance of a version-mismatched download.
Some tracks simply don't exist on the configured sources in the wanted cut —
every copy is, say, the instrumental. The retry engine correctly rejects each
one (version mismatch) and eventually gives up, leaving the track missing.
This module provides an OPT-IN fallback: once a track's retries are fully
exhausted, if every quarantined candidate for it failed the *same* way (same
matched version, e.g. all ``instrumental``) and there are at least ``min_count``
of them, accept the best (first-tried) one rather than failing outright.
Hard safety rules:
- Only ``Version mismatch`` quarantines qualify. Audio/artist mismatches
(a genuinely different recording) and integrity/duration failures
(truncated or wrong file) never participate.
- All qualifying entries must share the same matched version. A mix
(instrumental + live) is ambiguous → no acceptance.
- The chosen candidate is re-imported with only the AcoustID gate bypassed;
the integrity / duration / bit-depth gates still run, so a truncated or
corrupt file is never let through by this path.
``select_version_mismatch_fallback`` is the pure decision core (no I/O) so it
can be tested directly. ``try_accept_version_mismatch_fallback`` wires it to the
quarantine store + re-import dispatch via injected callables.
"""
from __future__ import annotations
import re
from typing import Any, Callable, Dict, List, Optional
from utils.logging_config import get_logger
# Must live under the soulsync.* namespace — handlers only attach there, so a
# bare getLogger(__name__) sent every line (including the critical "accepting
# best quarantined candidate as last resort" warning) into the void instead of
# app.log. Same bug class as the prepare.py fix.
logger = get_logger("imports.version_fallback")
# Matches the reason string written by acoustid_verification's version gate:
# "Version mismatch: expected '
' () but file is '' ()"
# We only need the matched () version to test cross-entry consistency.
_VERSION_MISMATCH_RE = re.compile(
r"^Version mismatch:.*\bbut file is\b.*\(([^()]+)\)\s*$"
)
def _norm(text: Optional[str]) -> str:
return (text or "").strip().casefold()
def matched_version(reason: Optional[str]) -> Optional[str]:
"""Return the matched version token (e.g. ``'instrumental'``) for a
Version-mismatch reason string, or None if the reason isn't a version
mismatch / can't be parsed."""
if not reason:
return None
m = _VERSION_MISMATCH_RE.match(reason.strip())
if not m:
return None
return m.group(1).strip().casefold()
def select_version_mismatch_fallback(
entries: List[Dict[str, Any]],
expected_title: str,
expected_artist: str,
min_count: int,
) -> Optional[Dict[str, Any]]:
"""Pick the quarantine entry to accept as a last resort, or None.
``entries`` are dicts as produced by
:func:`core.imports.quarantine.list_quarantine_entries` (needs ``id``,
``reason``, ``expected_track``, ``expected_artist``, ``has_full_context``).
Returns the chosen entry (the first-tried = oldest = best, by ascending
``id`` whose timestamp prefix sorts chronologically) when, for this track,
there are at least ``min_count`` version-mismatch entries that all share the
same matched version and carry full context. Otherwise None.
"""
title = _norm(expected_title)
artist = _norm(expected_artist)
candidates = []
for e in entries:
if not e.get("has_full_context"):
continue
if _norm(e.get("expected_track")) != title:
continue
if _norm(e.get("expected_artist")) != artist:
continue
version = matched_version(e.get("reason"))
if version is None:
continue
candidates.append((version, e))
if len(candidates) < max(1, int(min_count or 1)):
return None
versions = {v for v, _ in candidates}
if len(versions) != 1:
# Inconsistent wrong versions (e.g. instrumental + live) — ambiguous,
# don't guess which the user wants.
return None
# First tried = oldest = highest-confidence (the retry walks candidates
# best-first). The id is a "_