fix(providers): saving a key for an enabled source tries the gaps again (#8102)

A source that needs a key, such as Last.fm, answers "not found" without
one, and the enricher settles each row it tried that way at the current
version. Switching Last.fm on before entering its key settled every gap
without it, and saving the key didn't move the version, so Last.fm never
came back to them. The sources signature now marks each enabled source
that has a key, so a key arriving or leaving moves the version. Replacing
one key with another still doesn't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 23:50:29 -05:00
co-authored by Claude Opus 5.5
parent 3a7081be19
commit f3997b657c
5 changed files with 115 additions and 56 deletions
+23 -11
View File
@@ -72,8 +72,8 @@ type Config struct {
// Service reconciles a family's registry with its settings table, caches
// each provider's enabled flag and key, and owns the sources version: the
// stamp the enricher writes onto rows it has tried. The version moves
// whenever the enabled set changes, so rows that found nothing get another
// try through the new chain.
// whenever the enabled set changes, or an enabled source gains or loses its
// key, so rows that found nothing get another try through the new chain.
type Service struct {
cfg Config
@@ -168,7 +168,8 @@ func (s *Service) CurrentVersion() int32 {
}
// UpdateProvider applies an admin patch and re-Configures the provider.
// versionBumped is true when the enabled set changed; a key change alone
// versionBumped is true when the sources signature changed: the enabled set,
// or whether an enabled source has a key. Replacing one key with another
// doesn't move the version.
func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch UpdatePatch) (versionBumped bool, err error) {
p, err := s.cfg.Registry.ByID(providerID)
@@ -176,7 +177,7 @@ func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch U
return false, err
}
s.mu.RLock()
before := enabledSetSignature(s.enabledIDs)
before := s.sourcesSignature()
enabled := s.enabledIDs[providerID]
s.mu.RUnlock()
if patch.Enabled != nil {
@@ -200,7 +201,7 @@ func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch U
s.cache(r)
}
s.configure(p)
if enabledSetSignature(s.enabledIDs) == before {
if s.sourcesSignature() == before {
return false, nil
}
v, err := s.cfg.Store.BumpVersion(ctx)
@@ -211,13 +212,24 @@ func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch U
return true, nil
}
// enabledSetSignature is the sorted, comma-joined enabled provider IDs.
func enabledSetSignature(enabledIDs map[string]bool) string {
ids := make([]string, 0, len(enabledIDs))
for id, on := range enabledIDs {
if on {
ids = append(ids, id)
// sourcesSignature is the sorted, comma-joined enabled provider IDs, each
// marked "+key" when it has a key; the caller holds mu (read or write).
//
// The key belongs in it because a source that needs one, such as Last.fm,
// answers "not found" without it, and the enricher settles a row tried
// through it as found-nothing at the current version. Switching Last.fm on
// before entering its key would otherwise settle every gap without it, and
// the key, once saved, would never bring Last.fm back to them.
func (s *Service) sourcesSignature() string {
ids := make([]string, 0, len(s.enabledIDs))
for id, on := range s.enabledIDs {
if !on {
continue
}
if s.apiKeys[id] != "" {
id += "+key"
}
ids = append(ids, id)
}
sort.Strings(ids)
return strings.Join(ids, ",")