fix(valuation): reuse the canonical artist for a Spotify id, never mint a second (chat#1889 row 8)
Matrix row 8 in chat#1889. Blocks chat#1900 (row 9), which is in draft until this lands.
Why
Adding one artist through /setup/artists produced two roster entries. Caught on a brand-new account during chat#1900's preview verification:
[
{ "name": "Del Water Gap", "account_id": "6e4d3f5f-…",
"socials": ["open.spotify.com/artist/0xPoVNPnxIIUS1vrxAYV00"] },
{ "name": "Del Water Gap", "account_id": "7647f901-…",
"socials": ["open.spotify.com/artist/0xPoVNPnxIIUS1vrxAYV00"] }
]
Same name, same avatar, same Spotify id. Client-side instrumentation showed exactly one POST /api/artists, so the second was created server-side by POST /api/valuation.
Root cause. runValuationHandler falls back to linkSearchedArtistToAccount when attachCanonicalArtistToAccount resolves nothing — which is always the case for a freshly seeded catalog, because its songs are not in song_artists yet. That fallback called createArtistInDb unconditionally.
attachCanonicalArtistToAccount's own docstring names the failure mode it was built to end:
This replaces the marketing funnel's per-signup artist creation — which minted duplicate, song-less roster artists — as the roster source of truth for funnel signups.
The fallback had quietly reintroduced it.
What changed
The lookup is global, not per-account. Artists are canonical and shared, and account_artist_ids is the join that lets many accounts roster one artist (chat#1866 exists precisely because of that sharing). Scoping the dedup per-account would still give every account its own copy of the same Spotify artist — it would only stop one account duplicating itself. So:
selectSocialsBySpotifyArtistId(lib/supabase/socials/) — finds socials by Spotify id with anilike, becauseprofile_urlis stored inconsistently: with and without a scheme, sometimes with?si=. The existingselectSocials({ profile_url })is an exacteqand misses real matches.findCanonicalArtistBySpotifyId(lib/valuation/) — resolves that social to its artist account. Best-effort: returnsnullon any error so a dedup lookup can never fail a valuation.linkSearchedArtistToAccount— reuses the canonical artist when one exists, linking it viainsertAccountArtistIdonly when the account doesn't already roster it. Creates only when no artist carries that Spotify id yet.
Verification
TDD, red → green, two units:
# RED 1 — module not found
FAIL lib/valuation/__tests__/findCanonicalArtistBySpotifyId.test.ts
# RED 2 — still creating a duplicate
FAIL lib/valuation/__tests__/linkSearchedArtistToAccount.test.ts (2 failed)
> rosters the existing canonical artist instead of creating a duplicate
> does not re-link a canonical artist the account already rosters
# GREEN — full api suite
pnpm exec vitest run
Test Files 785 passed (785)
Tests 4253 passed (4253)
Six new tests. The lookup covers the global-not-scoped assertion, scheme/query-string variance, no-match, no-linked-artist, and the throw path. The link covers reuse-instead-of-create and don't-re-link-when-already-rostered — plus the four pre-existing tests still pass, so the create path a genuine first-time artist depends on is unchanged.
pnpm exec tsc --noEmit → 236, identical to main's baseline, none in the touched files. eslint clean.
No docs change: POST /api/valuation's request/response contract is unchanged.
Reviewer notes
- Scope beyond the seeding path. This also fixes the
/artistsdialog, which uses the sameuseAddSpotifyArtistcreate-then-seed flow — so any account whose first artist was added there has been getting a duplicate too. Pre-existing, now fixed. - Existing duplicates are not cleaned up. This stops new ones; it does not merge rows already created. Worth a follow-up if the count is material — happy to open one.
- Sharing has a known edge: once accounts genuinely share one artist row, artist-level metadata is shared too. That is exactly what chat#1866 tracks (
PATCH /api/artistsauthorizing shared-metadata writes off the flat roster edge). This PR makes sharing work as designed rather than routing around it, so #1866 becomes more worth landing.
Preview verification to follow on this PR.
Tracked in chat#1889 (matrix row 8).
Summary by cubic
Fixes duplicate artist creation when adding by Spotify ID by reusing the existing canonical artist and linking it to the account. Addresses chat#1889 (row 8) and unblocks chat#1900.
- Bug Fixes
- Updated
linkSearchedArtistToAccountto reuse a canonical artist if it exists, only create when none exists, and skip re-linking if already rostered. - Added
findCanonicalArtistBySpotifyIdfor a global lookup by Spotify ID and extendedselectSocialswithprofileUrlContains(ilike onprofile_url) to handle inconsistent storage; removed the one-off Spotify lookup. - Applies to both the valuation flow and the
/artistsadd dialog.
- Updated
Summary by CodeRabbit
-
New Features
- Existing artist records are now reused when linking searched Spotify artists, reducing duplicate artist entries.
- Artists can be matched using partial profile URL searches.
-
Bug Fixes
- Improved artist linking to avoid duplicate account associations.
- Artist lookup failures are handled gracefully without interrupting the process.