feat(library): AcoustID lookup worker fills the MBIDs tags leave empty (M401 #3920 #3921)
release / web (push) Successful in 1m44s
release / go (push) Successful in 2m1s
release / govulncheck (push) Successful in 17s
release / integration (push) Successful in 5m22s
release / android (push) Successful in 5m48s
release / Build signed APK (releases and dev) (push) Successful in 5m53s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 2m6s
release / Verify release artifacts (tag releases only) (push) Skipped

Migration 0069 adds tracks.mbid_source (tag | acoustid), a lookup state per
track (matched | ambiguous | no_match | failed) and the acoustid_settings
row (off, no key, min score 0.85).

The file's tag outranks a lookup (D4). UpsertTrack keeps a looked-up id
through a re-read that finds no tag id and replaces it as soon as one
appears. SetTrackMbidFromAcoustID refuses to write over a tag id.

The worker fingerprints each untagged track with fpcalc's compressed
print, looks it up and writes an id only when D5 settles it: one
recording at or above the threshold, or one left after matching title and
length. Ambiguous and no-match results write nothing. A key AcoustID
refuses, or the service being unreachable, stops the pass and is reported
in the worker's status. It never counts as a verdict on a track.

A changed file drops its lookup in the scan. The re-lookup takes back an
id that no longer matches.

Admin API: GET /api/admin/library/acoustid (settings, status, coverage by
source), PUT …/acoustid-settings (write-only key), POST …/acoustid/run,
GET …/acoustid/unsettled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 23:26:03 -04:00
co-authored by Claude Opus 5.5
parent f13da62797
commit 3c575b137c
24 changed files with 1743 additions and 29 deletions
+99
View File
@@ -0,0 +1,99 @@
-- AcoustID recording-id lookup (M401). Migration 0069 says what each
-- lookup state means.
-- name: ListTracksNeedingAcoustIDLookup :many
-- The lookup worker's queue: present tracks with no tag MBID and no lookup
-- since their file last changed. A looked-up id is included once its row is
-- gone (the file changed), so the new bytes are checked rather than trusted.
-- Keyset-paged on id, as the loudness queue is, so a pass ends even when
-- every attempt is inconclusive and writes nothing.
SELECT t.id, t.file_path, t.title, t.duration_ms
FROM tracks t
WHERE (t.mbid IS NULL OR t.mbid_source = 'acoustid')
AND t.missing_since IS NULL
AND t.id > sqlc.arg(after_id)::uuid
AND NOT EXISTS (SELECT 1 FROM track_acoustid_lookups l WHERE l.track_id = t.id)
ORDER BY t.id
LIMIT sqlc.arg(batch_limit);
-- name: RecordAcoustIDLookup :exec
INSERT INTO track_acoustid_lookups (track_id, state, best_score, candidates, recording_mbid, detail)
VALUES (sqlc.arg(track_id), sqlc.arg(state), sqlc.narg(best_score), sqlc.arg(candidates),
sqlc.narg(recording_mbid), sqlc.narg(detail))
ON CONFLICT (track_id) DO UPDATE SET
state = EXCLUDED.state,
best_score = EXCLUDED.best_score,
candidates = EXCLUDED.candidates,
recording_mbid = EXCLUDED.recording_mbid,
detail = EXCLUDED.detail,
looked_up_at = now();
-- name: SetTrackMbidFromAcoustID :execrows
-- Never over a tag id (D4): the guard is here, not only in the worker, so a
-- tag the scan read between the worker's list and this write still wins.
-- updated_at is left alone: it gates the scan's mtime skip, and moving it
-- past a file's mtime would hide a change made just before the lookup.
UPDATE tracks
SET mbid = sqlc.arg(mbid), mbid_source = 'acoustid'
WHERE id = sqlc.arg(id)
AND (mbid IS NULL OR mbid_source = 'acoustid');
-- name: ClearTrackAcoustIDMbid :exec
-- A re-lookup of changed bytes that no longer matches takes back the id an
-- earlier lookup wrote. A tag id is never touched.
UPDATE tracks
SET mbid = NULL, mbid_source = NULL
WHERE id = sqlc.arg(id)
AND mbid_source = 'acoustid';
-- name: DeleteAcoustIDLookup :exec
-- The scan drops the lookup of bytes that are gone, so the worker checks the
-- new ones.
DELETE FROM track_acoustid_lookups WHERE track_id = $1;
-- name: GetMbidCoverage :one
-- The admin gauge: present tracks by where their MBID came from, and the
-- lookup states of those a lookup has seen. pending is what the worker's
-- queue holds.
SELECT count(*)::bigint AS total,
count(*) FILTER (WHERE t.mbid_source = 'tag')::bigint AS from_tag,
count(*) FILTER (WHERE t.mbid_source = 'acoustid')::bigint AS from_acoustid,
count(*) FILTER (WHERE l.state = 'ambiguous')::bigint AS ambiguous,
count(*) FILTER (WHERE l.state = 'no_match')::bigint AS no_match,
count(*) FILTER (WHERE l.state = 'failed')::bigint AS failed,
count(*) FILTER (
WHERE l.track_id IS NULL AND (t.mbid IS NULL OR t.mbid_source = 'acoustid')
)::bigint AS pending
FROM tracks t
LEFT JOIN track_acoustid_lookups l ON l.track_id = t.id
WHERE t.missing_since IS NULL;
-- name: ListUnsettledAcoustIDLookups :many
-- The admin list of tracks a lookup could not settle (ambiguous or failed),
-- so they are findable rather than silently empty.
SELECT t.id, t.title, t.file_path, ar.name AS artist_name, al.title AS album_title,
l.state, l.best_score, l.candidates, l.detail, l.looked_up_at
FROM track_acoustid_lookups l
JOIN tracks t ON t.id = l.track_id
JOIN artists ar ON ar.id = t.artist_id
JOIN albums al ON al.id = t.album_id
WHERE l.state IN ('ambiguous', 'failed')
AND t.missing_since IS NULL
ORDER BY ar.name, al.title, t.disc_number NULLS FIRST, t.track_number NULLS FIRST, t.title
LIMIT sqlc.arg(row_limit);
-- name: GetAcoustIDSettings :one
SELECT * FROM acoustid_settings WHERE id = true;
-- name: UpdateAcoustIDSettings :one
-- The key is write-only from the admin card: set_api_key false keeps the
-- stored one, and an empty api_key with set_api_key true clears it.
UPDATE acoustid_settings
SET enabled = sqlc.arg(enabled),
min_score = sqlc.arg(min_score),
api_key = CASE WHEN sqlc.arg(set_api_key)::boolean
THEN NULLIF(sqlc.arg(api_key)::text, '')
ELSE api_key END,
updated_at = now()
WHERE id = true
RETURNING *;