fix(worker): evict removed-seed contributions + clear scan worklist on abandon

Two row-leak bugs found in whole-branch review:

- discovery.run_discovery now prunes DiscoverySeedContribution rows whose
  seedMbid is no longer a WatchedArtist (unfollow/removal has no FK cascade)
  and recomputes the touched candidates' scores this sweep, instead of
  letting a dead seed's contribution inflate the aggregate forever.

- maybe_run_scan's exception handler around scan_chunk now calls
  clear_worklist and clears scan.id, matching the drain path. Previously
  an exception mid-chunk left the already-done rows plus the remaining
  worklist for that scan_id orphaned forever, since the next scan mints a
  fresh uuid. Also clear scan.id in the build_worklist failure handler for
  consistency.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-13 15:39:38 +02:00
parent 0115b82867
commit 6f325d30b9
4 changed files with 69 additions and 2 deletions
+20
View File
@@ -14,6 +14,11 @@ class FakeResolver:
return MBTarget(artist=artist, album=album, rg_mbid=f"rg-{album}", artist_mbid="a1")
class RaisingProbe:
def probe(self, path):
raise RuntimeError("probe boom")
def _set(conn, key, value):
with conn.cursor() as cur:
cur.execute(
@@ -78,6 +83,21 @@ def test_scan_chunks_across_iterations_and_totals_correctly(conn, tmp_path):
assert cur.fetchone()[0] == 3
def test_scan_chunk_exception_clears_worklist_not_just_flags(conn, tmp_path):
# scan_chunk commits per item, so if it throws mid-chunk the except block must
# still clear the worklist — otherwise the done + not-yet-processed ScanWorkItem
# rows for this scan_id are orphaned forever (the next scan mints a fresh uuid).
_album(tmp_path, "Artist", "Album")
_set(conn, "scan.requested", "true")
maybe_run_scan(conn, FakeResolver(), RaisingProbe(), FakeMbBrowser(), _cfg(conn), dest_root=str(tmp_path))
assert _get(conn, "scan.inProgress") == "false"
with conn.cursor() as cur:
cur.execute('SELECT count(*) FROM "ScanWorkItem"')
assert cur.fetchone()[0] == 0
def test_maybe_run_scan_idle_when_not_requested_or_in_progress(conn, tmp_path):
# no flags set -> no-op, no result written
maybe_run_scan(conn, FakeResolver(), FakeProbe(), FakeMbBrowser(), _cfg(conn), dest_root=str(tmp_path))