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
+11 -1
View File
@@ -225,7 +225,17 @@ def run_discovery(conn: psycopg.Connection, sources: list[SimilaritySource],
followed = _followed_mbids(conn)
live = _healthy_sources(sources)
affected: set[str] = set()
# Evict contributions from seeds no longer followed — unfollow/removal has no
# FK cascade, so without this a removed seed's score would linger and inflate
# the aggregate forever. Seed `affected` with the touched candidates so their
# suggestion scores are recomputed from the remaining live seeds this sweep.
with conn.cursor() as cur:
cur.execute('SELECT DISTINCT "candidateMbid" FROM "DiscoverySeedContribution" '
'WHERE "seedMbid" NOT IN (SELECT mbid FROM "WatchedArtist")')
affected = {r[0] for r in cur.fetchall()}
cur.execute('DELETE FROM "DiscoverySeedContribution" '
'WHERE "seedMbid" NOT IN (SELECT mbid FROM "WatchedArtist")')
for seed_mbid, _seed_name in seeds:
affected |= _record_contributions(conn, seed_mbid, live, followed, cfg)
+4 -1
View File
@@ -76,6 +76,7 @@ def maybe_run_scan(conn, resolver, probe, browser, config, dest_root: str = DEST
except Exception as e: # a scan error must never kill the worker
print(f"worker: library scan worklist build failed: {e}", flush=True)
conn.rollback()
_set_config(conn, "scan.id", "")
_set_config(conn, "scan.inProgress", "false")
_set_config(conn, "scan.requested", "false")
return
@@ -91,7 +92,9 @@ def maybe_run_scan(conn, resolver, probe, browser, config, dest_root: str = DEST
except Exception as e: # a scan error must never kill the worker
print(f"worker: library scan chunk failed: {e}", flush=True)
conn.rollback()
_set_config(conn, "scan.inProgress", "false")
clear_worklist(conn, scan_id)
_set_config(conn, "scan.id", "")
_set_config(conn, "scan.inProgress", "false") # abandon the scan; a new request restarts it
_set_config(conn, "scan.requested", "false")
return
imported_total = imported_so_far + imp