fix(library): the duplicate resolver never removes a file on its own (#8252)
release / go (push) Successful in 2m28s
release / web (push) Successful in 1m42s
release / govulncheck (push) Successful in 23s
release / integration (push) Successful in 5m9s
release / android (push) Successful in 5m28s
release / Build signed APK (push) Successful in 5m54s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 34s
release / Verify release artifacts (tag releases only) (push) Skipped

Other programs file music into the library now, and a startup pass
deleted a whole album a download manager had just filed, keeping older
loose copies of the same songs. The resolver assumed Minstrel was the
only program writing the library; it is not.

An automatic pass now changes only Minstrel's database:
- copies of one song, on one album or across releases, are linked as one
  song, so likes, plays and mixes count it once, and any group an admin
  may want to thin stays pending for review (and is announced);
- distinct versions are still dismissed, and rips still held back;
- a one-album wrong slot gets a note naming the song MusicBrainz or
  AcoustID says the recording is, for the admin who settles it.

Removing a copy is an admin's merge or wrong-slot settle, as before. When
an admin merges, a copy in a folder named for its album ("Album (Year)",
or its "CD nn" folder) now outranks a loose copy of the same audio.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 21:09:11 -05:00
co-authored by Claude Opus 5.5
parent 63af193b13
commit 677a421675
16 changed files with 330 additions and 462 deletions
+35 -38
View File
@@ -78,8 +78,7 @@ type slotCopy struct {
// Several copies may fit when they carry the same title ("Killjoy" twice
// beside "Dreamland"): they are one song held twice, not a wrong slot, so they
// are neither kept nor lost here. Once the wrong slot is emptied, the next
// sweep finds the pair on its own, and the resolver merges it as any song held
// twice on one album.
// sweep finds the pair on its own, as any song held twice on one album.
func wrongSlots(copies []slotCopy, tracks []dbq.MbReleaseTrack) (keep int, losers []int, ok bool) {
keep = -1
keepKey := ""
@@ -164,14 +163,12 @@ func dismissDistinct(
return n
}
// settleWrongSlots removes the copies that sit in another song's slot, for
// every one-album mismatch the track list, or AcoustID with it, decides. It
// returns how many copies went and the songs whose slots were emptied, and
// adds the groups it settled to settled.
func settleWrongSlots(
ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, groups []*resolveGroup, settled map[pgtype.UUID]bool,
src ResolveSources,
) (int, []string) {
// nameWrongSlots says, in the note of every one-album mismatch the track list
// or AcoustID decides, which song the recording is, so the admin settling it
// (SettleWrongSlot) need not work it out. It returns how many it named.
func nameWrongSlots(
ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, groups []*resolveGroup, src ResolveSources,
) int {
q := dbq.New(pool)
tracksByGroup := map[string][]dbq.MbReleaseTrack{}
fetches, identifies := trackListFetchesPerPass, identifiesPerPass
@@ -181,10 +178,9 @@ func settleWrongSlots(
if src.Identify == nil {
identifies = 0
}
removed := 0
var names []string
named := 0
for _, g := range groups {
if g.class != ClassMismatch || settled[g.id] || ctx.Err() != nil {
if g.class != ClassMismatch || ctx.Err() != nil {
continue
}
rg := g.members[0].ReleaseGroupMbid
@@ -218,13 +214,15 @@ func settleWrongSlots(
for i, m := range g.members {
copies[i] = slotCopy{Title: m.Title, Artist: m.ArtistName, DurationMs: m.DurationMs}
}
keep, losers, ok := wrongSlots(copies, tracks)
source := "MusicBrainz's track lengths say"
keep, _, ok := wrongSlots(copies, tracks)
if !ok {
recordings, known := groupRecordings(ctx, logger, g, src.Identify, &identifies)
if !known {
continue
}
if keep, losers, ok = namedCopies(copies, tracks, recordings); !ok {
source = "AcoustID says"
if keep, _, ok = namedCopies(copies, tracks, recordings); !ok {
logger.Info("duplicate resolve: AcoustID's answer does not place the recording in one slot; left for review",
"group_id", syncpkg.FormatUUID(g.id), "recordings", recordings)
}
@@ -232,19 +230,20 @@ func settleWrongSlots(
if !ok {
continue
}
emptied := emptiedSlots(ctx, q, logger, g, losers, tracks)
n, err := removeWrongSlots(ctx, pool, logger, g, keep, losers, emptied, pgtype.UUID{})
removed += n
if err != nil {
logger.Warn("duplicate resolve: removing wrong-slot copies failed", "group_id", syncpkg.FormatUUID(g.id), "err", err)
continue
}
settled[g.id] = true
for _, i := range losers {
names = append(names, notifications.TrackLabel(g.members[i].ArtistName, g.members[i].Title))
}
g.note = wrongSlotNote(source, g, keep)
named++
}
return removed, names
return named
}
// wrongSlotNote is what a mismatch's note says once the pass knows which song
// the recording is: the copy at keep, or none of them when keep is -1.
func wrongSlotNote(source string, g *resolveGroup, keep int) string {
if keep < 0 {
return source + " this recording is none of these songs. Settle it to empty the slots it fills."
}
return source + " this recording is \u201c" + g.members[keep].Title +
"\u201d. Settle it with that copy to empty the slots that are not its own."
}
// ErrNotWrongSlot means a group is not one recording filling several of an
@@ -261,13 +260,13 @@ type WrongSlotResult struct {
// SettleWrongSlot settles a one-album mismatch the resolver left for the
// admin, with their answer in place of MusicBrainz's lengths and
// AcoustID's. keepID is the copy whose title they say the recording is; an
// invalid id means they cannot say, and every copy goes. Either way it is the
// resolver's own path: the copies removed are marked missing, and their slots
// wait for a good copy. A merge does none of that, and keeps one copy's title
// whether or not it is the audio's.
// invalid id means they cannot say, and every copy goes. Either way the copies
// removed are marked missing, and their slots wait for a good copy. A merge
// does none of that, and keeps one copy's title whether or not it is the
// audio's.
//
// Copies sharing the kept copy's title are one song held twice and stay, as in
// wrongSlots: the next sweep finds the pair and the resolver merges it.
// wrongSlots: the next sweep finds the pair, for an admin to merge.
func SettleWrongSlot(
ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, groupID, keepID, actorID pgtype.UUID,
) (WrongSlotResult, error) {
@@ -548,16 +547,14 @@ func removeWrongSlots(
}
// notifyNewForReview tells admins about groups this pass classified for the
// first time and left for them. A group the resolver settled, linked or will
// link is not news, and would bury the groups that need them.
// first time and left for them. A group that needs no one (distinct versions
// dismissed, or copies on separate releases that count as one song) is not
// news, and would bury the groups that need them.
func notifyNewForReview(ctx context.Context, groups []*resolveGroup, settled map[pgtype.UUID]bool) {
var count int64
var items []string
for _, g := range groups {
if g.members[0].PriorClass != nil || settled[g.id] {
continue
}
if g.class != ClassReview && g.class != ClassMismatch {
if g.members[0].PriorClass != nil || settled[g.id] || !g.wantsAdmin() {
continue
}
count++