forked from trav/lintunes
v0.14.0: a removal you make sticks
Round 43 made the merge report honest, and in doing so made its one real
limitation impossible to miss: every merge was a union, so a song removed from a
playlist on one machine was handed straight back by the other on the next sync,
and a track deleted from the library came back with it. A deletion you cannot
make stick is not a deletion.
The union was there for a real reason — two copies with no common ancestor
cannot tell "A added this" from "B removed it" — so the missing evidence is
written down instead of inferred. New lintunes/tombstones.py: a playlist keeps
track_events {track id: [when, add|remove]}, the library keeps deleted_tracks
{track id: when} in library_metadata.json, and a merge applies the newest event
per track across both copies. A removal beats a copy that merely still had the
song; a deliberate re-add afterwards beats the removal; a track nobody touched
still merges as a union, which stays the safe behavior where there is no
evidence either way.
Deliberately not "the newer copy wins wholesale": that one-liner silently drops
a song the other machine added while you were removing one, which
test_an_unrelated_addition_is_not_lost pins.
Events are recorded in the funnels that already exist — _set_track_ids diffs
before/after so a reorder records nothing, _remove_tracks stamps the library,
_restore_tracks clears it so Ctrl+Z takes the tombstone back — and pruned after
30 days at the save boundary.
Three edges worth naming:
* Track ids are never reused. The next id came from max(library.tracks), so
deleting the highest-numbered track freed its id, and the next import would be
dropped on sight by the dead id's own tombstone on every machine.
* library_metadata.json is merged before library.json, since it carries the
record the library merge is filtered against and rglob order is not a plan.
* A merge applying a deletion never touches a music file — it drops the library
entry only, and test_a_merge_never_touches_a_music_file fails the run if
send_to_trash is so much as called. Applied removals grade WARNING and name
the song.
Claude-Session: https://claude.ai/code/session_01Y9ZEFi4qNJ39FMiBtiAxy2
This commit is contained in:
@@ -8,7 +8,7 @@ from PyQt6.QtCore import QObject, QTimer, pyqtSignal
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
from lintunes import music_folder as music_folder_mod
|
||||
from lintunes import smart, tagging, trash
|
||||
from lintunes import smart, tagging, tombstones, trash
|
||||
from lintunes.importers.file_importer import organized_destination, unique_path
|
||||
from lintunes.models import Library, Playlist, PlaylistType
|
||||
from lintunes.paths import to_relative
|
||||
@@ -96,13 +96,19 @@ class LibraryManager(QObject):
|
||||
self._own_sigs: dict[str, tuple | None] = {}
|
||||
self.snapshot_sync_state()
|
||||
|
||||
self._max_track_id = max(self.library.tracks.keys(), default=0)
|
||||
self._max_track_id = self._highest_track_id()
|
||||
|
||||
# ---- id generation ----
|
||||
|
||||
def new_track_id(self) -> int:
|
||||
return self._max_track_id + 1
|
||||
|
||||
def _highest_track_id(self) -> int:
|
||||
"""The highest id ever used, deletions included. Ids must never be
|
||||
reused: a new track handed a deleted one's id would be dropped on sight
|
||||
by that id's own tombstone (and on every other machine too)."""
|
||||
return max([*self.library.tracks, *self.library.deleted_tracks], default=0)
|
||||
|
||||
def new_persistent_id(self) -> str:
|
||||
while True:
|
||||
pid = secrets.token_hex(8).upper()
|
||||
@@ -336,6 +342,7 @@ class LibraryManager(QObject):
|
||||
playlist = self.library.playlists.get(pid)
|
||||
if playlist is None:
|
||||
return
|
||||
before = list(playlist.track_ids)
|
||||
playlist.track_ids = list(ids)
|
||||
# The single funnel for every content change (add / remove / reorder /
|
||||
# undo). date_modified moves only where content genuinely changed: here,
|
||||
@@ -347,6 +354,10 @@ class LibraryManager(QObject):
|
||||
# conflict_resolver._playlist_newer.
|
||||
if touch:
|
||||
playlist.date_modified = _utc_now_iso()
|
||||
# Record *what* changed, not just that something did. A merge has no
|
||||
# common ancestor, so without this a removal is indistinguishable
|
||||
# from the other copy's addition and the union hands the song back.
|
||||
tombstones.record(playlist.track_events, before, playlist.track_ids)
|
||||
self._mark_playlist(pid, content=True)
|
||||
self.playlist_content_changed.emit(pid)
|
||||
|
||||
@@ -465,8 +476,13 @@ class LibraryManager(QObject):
|
||||
|
||||
def _remove_tracks(self, tracks: list, snapshots: dict[str, list[int]]):
|
||||
doomed = {t.track_id for t in tracks}
|
||||
when = _utc_now_iso()
|
||||
for track in tracks:
|
||||
self.library.tracks.pop(track.track_id, None)
|
||||
# Same reason as the playlist events: library.json merges as a
|
||||
# union, so a deletion the other copy hasn't heard about comes
|
||||
# straight back without a record that it was deliberate.
|
||||
self.library.deleted_tracks[track.track_id] = when
|
||||
self._invalidate_artwork(track)
|
||||
for pid in snapshots:
|
||||
playlist = self.library.playlists.get(pid)
|
||||
@@ -482,6 +498,7 @@ class LibraryManager(QObject):
|
||||
def _restore_tracks(self, tracks: list, snapshots: dict[str, list[int]]):
|
||||
for track in tracks:
|
||||
self.library.tracks[track.track_id] = track
|
||||
self.library.deleted_tracks.pop(track.track_id, None)
|
||||
self._max_track_id = max(self._max_track_id, track.track_id)
|
||||
for pid, ids in snapshots.items():
|
||||
if pid in self.library.playlists:
|
||||
@@ -1016,6 +1033,15 @@ class LibraryManager(QObject):
|
||||
# amount equal to this machine's journal.
|
||||
self.play_journal.load(self.data_dir, disk)
|
||||
|
||||
# Deletion records are additive on both sides: ours are not on disk yet
|
||||
# if we haven't flushed, and theirs are news to us. Union first, then
|
||||
# apply, so a track deleted on either machine stays deleted on both.
|
||||
self.library.deleted_tracks = tombstones.merge_events(
|
||||
self.library.deleted_tracks, disk.deleted_tracks)
|
||||
for tid in self.library.deleted_tracks:
|
||||
self.library.tracks.pop(tid, None)
|
||||
disk.tracks.pop(tid, None)
|
||||
|
||||
for tid, disk_track in disk.tracks.items():
|
||||
mem_track = self.library.tracks.get(tid)
|
||||
if mem_track is None:
|
||||
@@ -1047,7 +1073,7 @@ class LibraryManager(QObject):
|
||||
self.library.library_settings = disk.library_settings
|
||||
self.refresh_music_folder()
|
||||
|
||||
self._max_track_id = max(self.library.tracks.keys(), default=0)
|
||||
self._max_track_id = self._highest_track_id()
|
||||
|
||||
self._recomputing = True
|
||||
try:
|
||||
@@ -1098,6 +1124,8 @@ def _reconcile_playlist(mem_pl, disk_pl):
|
||||
other machine was quietly undone. Same rule as
|
||||
conflict_resolver._playlist_newer, one layer up."""
|
||||
from lintunes.storage.conflict_resolver import merge_track_order
|
||||
mem_pl.track_events = tombstones.merge_events(mem_pl.track_events,
|
||||
disk_pl.track_events)
|
||||
ours, theirs = mem_pl.date_modified, disk_pl.date_modified
|
||||
disk_newer = bool(theirs) and (not ours or theirs > ours)
|
||||
mine, other = list(mem_pl.track_ids), list(disk_pl.track_ids)
|
||||
@@ -1116,7 +1144,9 @@ def _reconcile_playlist(mem_pl, disk_pl):
|
||||
# so is what lets the other machine adopt it instead of the two trading
|
||||
# the same tracks back and forth.
|
||||
mem_pl.date_modified = _utc_now_iso()
|
||||
mem_pl.track_ids = merged
|
||||
# A union would hand back anything the newest event says was removed.
|
||||
gone = tombstones.removed_ids(mem_pl.track_events)
|
||||
mem_pl.track_ids = [tid for tid in merged if tid not in gone]
|
||||
|
||||
|
||||
def _utc_now_iso() -> str:
|
||||
|
||||
Reference in New Issue
Block a user