feat(discovery): build_similarity_sources + worker sweep/trigger wiring

Adds registry.build_similarity_sources() (always constructs
ListenBrainzSource; reachability is a per-run health() concern, not a
startup gate) and wires discovery into the worker's main loop: a
scheduled sweep gated on discover.enabled + DISCOVER_TICK_SECONDS, and
a one-shot discover.requested trigger mirroring the existing
scan.requested pattern.

Also hardens run_discovery's health-check pass: a source whose
health() raises is now logged and skipped instead of aborting the
whole sweep (accepted finding from the Task-3 review).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonathan
2026-07-12 00:22:34 +02:00
parent 6f504a7c04
commit a15f4ae8bb
6 changed files with 119 additions and 2 deletions
+15
View File
@@ -72,6 +72,21 @@ def test_unhealthy_source_contributes_nothing(conn):
assert _artist_rows(conn) == []
def test_source_with_raising_health_is_skipped_not_fatal(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
class BoomSource:
name = "boom"
def health(self):
raise RuntimeError("boom")
def similar_artists(self, mbid):
return []
good = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "X", 0.9)]})
result = run_discovery(conn, [BoomSource(), good], FakeMbBrowser(), DiscoveryConfig())
assert result.artists == 1 # good source still processed; boom skipped, no crash
def test_config_from_config_parses_and_defaults():
cfg = DiscoveryConfig.from_config({"discover.enabled": "true", "discover.maxSeeds": "10",
"discover.minScore": "0.3"})
+35
View File
@@ -0,0 +1,35 @@
from lyra_worker.adapters.fakes import FakeMbBrowser, FakeSimilaritySource
from lyra_worker.main import _run_discovery
from lyra_worker.similarity.base import SimilarArtist
from tests.conftest import insert_watched_artist
def _set(conn, key, value):
with conn.cursor() as cur:
cur.execute(
'INSERT INTO "Config"(key, value, secret, "updatedAt") VALUES (%s, %s, false, now()) '
'ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, "updatedAt" = now()',
(key, value),
)
conn.commit()
def _get(conn, key):
with conn.cursor() as cur:
cur.execute('SELECT value FROM "Config" WHERE key = %s', (key,))
row = cur.fetchone()
return row[0] if row else None
def test_run_discovery_writes_result_and_clears_flag(conn):
insert_watched_artist(conn, mbid="s1", name="Seed")
_set(conn, "discover.requested", "true")
src = FakeSimilaritySource(similar={"s1": [SimilarArtist("c1", "Cand", 0.9)]})
_run_discovery(conn, [src], FakeMbBrowser())
assert _get(conn, "discover.requested") == "false" # flag cleared
assert "artists" in (_get(conn, "discover.result") or "") # summary written
with conn.cursor() as cur:
cur.execute('SELECT count(*) FROM "DiscoverySuggestion" WHERE kind = \'artist\'')
assert cur.fetchone()[0] == 1
+14
View File
@@ -0,0 +1,14 @@
from lyra_worker.registry import build_similarity_sources
from lyra_worker.similarity._listenbrainz import ListenBrainzSource
def test_build_returns_listenbrainz_by_default():
sources = build_similarity_sources({})
assert len(sources) == 1
assert isinstance(sources[0], ListenBrainzSource)
assert sources[0].name == "listenbrainz"
def test_base_url_override_from_config():
src = build_similarity_sources({"discover.listenBrainzUrl": "http://lb.local"})[0]
assert src._base == "http://lb.local"