v0.13.0: the merge report says who, what, and where

The merge window kept reporting "Order kept from this machine (most recently
edited)" for playlists edited on the other machine. Three defects, confirmed
against the real snapshots in .resolved/:

* "this machine" was inferred from which copy held the plain filename. That is
  Syncthing's call, not a statement about authorship — it sets the local copy
  aside as readily as a remote one. In the 9:34 PM `* a fresh master` merge the
  copy labelled "the other machine" was this machine's own 3:15 PM merge output,
  so the label was exactly backwards. The 7-char device ID in the conflict
  filename — the only real evidence — was matched by a bare \w+ and deleted with
  the file. New sync_identity.py decodes it against Syncthing's config.xml and
  works out which device is us from cert.pem.

* The decision leaned local. date_modified was only consulted when *both* copies
  had one, and an iTunes playlist never reordered here has none — so the honest
  comparison was skipped exactly when one machine had edited and the other
  hadn't. A stamped copy now beats an unstamped one; mtime is the fallback only
  when neither side has ever been edited. And every merge used to rewrite the
  file it kept whether or not anything changed, freshening its mtime while the
  conflict file kept its origin's: a ratchet. No-op merges write nothing, and a
  merge whose result is a union neither copy had stamps date_modified, so the
  other machine adopts it instead of trading the same 19 tracks back and forth.

* Nothing was actionable. Re-inserted tracks are now named with their position
  ("Pola — Abeille -> position 24, after ..."), six in the window and all of
  them in what-changed.txt at the top of the backup snapshot, alongside the real
  conflict filename and its device. Tracks only this copy has are reported too
  rather than resurrected in silence.

Also fixed while in here: a rename or folder move made elsewhere was discarded
by every merge (only track_ids and settings were adopted); _reconcile_playlist
asserted the local edit was newer and never checked, so a reorder synced in from
the other machine was undone and flushed back to disk, and the branch reaching
it was gated on a dirty flag that a column drag sets; and _merge_metadata
decided the music folder from whichever copy an mtime coin flip had kept.

Claude-Session: https://claude.ai/code/session_01Y9ZEFi4qNJ39FMiBtiAxy2
This commit is contained in:
2026-08-27 15:16:49 -04:00
parent 3124dc8a07
commit e6bd0de4e3
10 changed files with 978 additions and 71 deletions
+65 -19
View File
@@ -61,6 +61,11 @@ class LibraryManager(QObject):
self._dirty_tracks = False
self._dirty_metadata = False
self._dirty_playlists: set[str] = set()
# Which of those are dirty because their *contents* changed, as opposed
# to a column drag or a sort click. A sync that arrives while a playlist
# is merely cosmetically dirty must adopt the disk copy wholesale, not
# treat this machine as the one holding the edits.
self._dirty_playlist_content: set[str] = set()
self._deleted_playlists: set[str] = set()
# Play counts live in per-machine journals, not library.json — see
@@ -238,6 +243,7 @@ class LibraryManager(QObject):
for playlist in playlists:
self.library.playlists[playlist.persistent_id] = playlist
self._dirty_playlists.add(playlist.persistent_id)
self._dirty_playlist_content.add(playlist.persistent_id)
self._deleted_playlists.discard(playlist.persistent_id)
self._dirty_metadata = True
self._schedule_save()
@@ -248,6 +254,7 @@ class LibraryManager(QObject):
pid = playlist.persistent_id
self.library.playlists.pop(pid, None)
self._dirty_playlists.discard(pid)
self._dirty_playlist_content.discard(pid)
self._deleted_playlists.add(pid)
self._dirty_metadata = True
self._schedule_save()
@@ -257,14 +264,19 @@ class LibraryManager(QObject):
playlist = self.library.playlists.get(pid)
if playlist:
playlist.name = name
self._mark_playlist(pid)
# A rename is an edit like any other: without a stamp it is
# invisible to every merge, and the other machine's older name wins
# by default forever.
playlist.date_modified = _utc_now_iso()
self._mark_playlist(pid, content=True)
self.playlists_changed.emit()
def _apply_reparent(self, pid: str, parent_pid: str):
playlist = self.library.playlists.get(pid)
if playlist:
playlist.parent_persistent_id = parent_pid
self._mark_playlist(pid)
playlist.date_modified = _utc_now_iso()
self._mark_playlist(pid, content=True)
self.playlists_changed.emit()
# ---- playlist contents ----
@@ -326,14 +338,16 @@ class LibraryManager(QObject):
return
playlist.track_ids = list(ids)
# The single funnel for every content change (add / remove / reorder /
# undo), and deliberately the *only* place date_modified moves — a
# column resize must not look like an edit to the merge. Nor may a smart
# recompute: it fires every time a play count moves, and bumping the
# timestamp there made "most recently edited" meaningless for smart
# playlists. See conflict_resolver._playlist_newer.
# undo). date_modified moves only where content genuinely changed: here,
# a rename or reparent, and a reconcile whose union is content neither
# copy had — never for a column resize, which must not look like an edit
# to the merge. Nor for a smart recompute: it fires every time a play
# count moves, and bumping the timestamp there made "most recently
# edited" meaningless for smart playlists. See
# conflict_resolver._playlist_newer.
if touch:
playlist.date_modified = _utc_now_iso()
self._mark_playlist(pid)
self._mark_playlist(pid, content=True)
self.playlist_content_changed.emit(pid)
def _push_track_ids(self, label, pid, before, after):
@@ -892,8 +906,10 @@ class LibraryManager(QObject):
# ---- saving ----
def _mark_playlist(self, pid: str):
def _mark_playlist(self, pid: str, content: bool = False):
self._dirty_playlists.add(pid)
if content:
self._dirty_playlist_content.add(pid)
self._dirty_metadata = True
self._schedule_save()
@@ -921,6 +937,7 @@ class LibraryManager(QObject):
json_storage.save_playlist(playlist, self.data_dir)
self._record_sig(self.data_dir / "playlists" / f"{pid}.json")
self._dirty_playlists.clear()
self._dirty_playlist_content.clear()
for pid in list(self._deleted_playlists):
json_storage.delete_playlist_file(pid, self.data_dir)
self._own_sigs.pop(str(self.data_dir / "playlists" / f"{pid}.json"), None)
@@ -985,7 +1002,13 @@ class LibraryManager(QObject):
touches the player."""
was_dirty = (self._dirty_tracks or bool(self._dirty_playlists)
or self._dirty_metadata)
dirty_pids = set(self._dirty_playlists)
# Only a *content* edit makes this machine's copy the one to reconcile
# against. A playlist whose local dirt is a column width adopts the disk
# copy — otherwise a sync that lands in the three seconds after a sort
# click reverted the other machine's reorder and then flushed the
# revert back to disk.
content_pids = set(self._dirty_playlist_content)
pending_pids = set(self._dirty_playlists)
disk = json_storage.load_library(self.data_dir)
# Fold the journals onto the disk copy *before* reconciling. The tracks
# in memory already carry effective counts, so without this the merge
@@ -1007,12 +1030,12 @@ class LibraryManager(QObject):
mem_pl = self.library.playlists.get(pid)
if mem_pl is None:
self.library.playlists[pid] = disk_pl
elif pid in dirty_pids:
_reconcile_playlist(mem_pl, disk_pl) # keep local edits, union in
elif pid in content_pids:
_reconcile_playlist(mem_pl, disk_pl) # merge the two edits
else:
mem_pl.__dict__.update(disk_pl.__dict__) # adopt disk (identity kept)
for pid in list(self.library.playlists):
if pid not in disk.playlists and pid not in dirty_pids:
if pid not in disk.playlists and pid not in pending_pids:
self.library.playlists.pop(pid, None)
if not self._dirty_metadata:
@@ -1064,13 +1087,36 @@ def _reconcile_track(mem_track, disk_track):
def _reconcile_playlist(mem_pl, disk_pl):
"""Reconcile a playlist we were locally editing with the disk copy: our order
wins (the local edit is the more recent one), and tracks that exist only on
the other machine are merged back into position rather than appended (a merge
never drops tracks)."""
"""Reconcile a playlist we were locally editing with the copy that just
synced in. Whichever side recorded the more recent edit sets the order, and
tracks that exist only on the other side are merged back into position
rather than appended (a merge never drops tracks).
The stamps used to go unread here: the local order simply won, on the
strength of a docstring asserting it was the newer one. It often wasn't —
and the result was flushed straight back to disk, so a reorder made on the
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_ids = merge_track_order(list(mem_pl.track_ids),
list(disk_pl.track_ids))
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)
merged = (merge_track_order(other, mine) if disk_newer
else merge_track_order(mine, other))
if disk_newer:
# The disk copy is the edited one, so its name/folder/columns are the
# edited ones too — all four used to be dropped on the floor here.
mem_pl.name = disk_pl.name
mem_pl.parent_persistent_id = disk_pl.parent_persistent_id
mem_pl.settings = disk_pl.settings
if disk_newer and merged == other and theirs:
mem_pl.date_modified = theirs
elif merged != mine:
# A union is content neither copy had, so it is newer than both; saying
# 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
def _utc_now_iso() -> str: