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
+34
View File
@@ -165,6 +165,40 @@ def test_seed_dropoff_lowers_seedcount(conn):
assert _artist_score(conn, "c1") == (0.5, 1) # only s2 remains
def test_evicts_contributions_from_removed_seed(conn):
# Two live seeds contribute to c1 -> 1.1/2. Then s1 is unfollowed (deleted from
# WatchedArtist, simulating DELETE /api/artists/[id]) without any FK cascade on
# DiscoverySeedContribution. The next sweep must evict s1's orphaned contribution
# rather than leaving it to inflate c1's score forever.
insert_watched_artist(conn, mbid="s1", name="Seed One",
last_discovered_sql="now() - interval '200 hours'")
insert_watched_artist(conn, mbid="s2", name="Seed Two",
last_discovered_sql="now() - interval '200 hours'")
src = FakeSimilaritySource(similar={
"s1": [SimilarArtist("c1", "Shared", 0.6)],
"s2": [SimilarArtist("c1", "Shared", 0.5)],
})
run_discovery(conn, [src], FakeMbBrowser(), DiscoveryConfig())
assert _artist_score(conn, "c1") == (1.1, 2)
# Unfollow s1 (no FK cascade on DiscoverySeedContribution).
with conn.cursor() as cur:
cur.execute('DELETE FROM "WatchedArtist" WHERE mbid = %s', ("s1",))
conn.commit()
# Make s2 due again and sweep; s1 can no longer be a seed since it's gone.
with conn.cursor() as cur:
cur.execute('UPDATE "WatchedArtist" SET "lastDiscoveredAt" = now() - interval \'200 hours\' WHERE mbid = %s', ("s2",))
conn.commit()
src2 = FakeSimilaritySource(similar={"s2": [SimilarArtist("c1", "Shared", 0.5)]})
run_discovery(conn, [src2], FakeMbBrowser(), DiscoveryConfig())
assert _artist_score(conn, "c1") == (0.5, 1) # s1's stale 0.6 evicted, not summed
with conn.cursor() as cur:
cur.execute('SELECT count(*) FROM "DiscoverySeedContribution" WHERE "seedMbid" = %s', ("s1",))
assert cur.fetchone()[0] == 0
def test_run_discovery_processes_at_most_chunk_size_seeds(conn):
from lyra_worker.discovery import DiscoveryConfig, run_discovery
from lyra_worker.adapters.fakes import FakeMbBrowser, FakeSimilaritySource