From 2806e679ac47e091cd4372a0e507ff40af0899f2 Mon Sep 17 00:00:00 2001 From: Sanapach Date: Thu, 19 Nov 2026 22:15:09 +0200 Subject: [PATCH] Leave out placeholders an older device echoes back as music A device below sync v4 mirrors the redacted queue it gets with its own types, which drop PlaybackTrack::source, so each "Private item" becomes a bare music item. When it takes playback over (Play on it, a paused-startup claim, automatic failover) or sends a command, it announces those items, and this device adopted unplayable fake tracks and passed them on to every v4 peer. States from such devices are now cleaned on receipt, with a status message once per change. Co-Authored-By: Claude Opus 5.5 --- src/devices.rs | 112 ++++++++++++- src/devices/podcast_sync_tests.rs | 266 ++++++++++++++++++++++++++++++ 2 files changed, 374 insertions(+), 4 deletions(-) diff --git a/src/devices.rs b/src/devices.rs index 0e964f8..9e35cff 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -12,7 +12,9 @@ //! into that store. Everything sent to a peer is gated by the version that //! peer reported itself (`op_allowed_for_peer`, `SyncSnapshot::for_peer`, //! `playback_allowed_for_peer`; a queue with podcast items reaches an older -//! peer only redacted, for its ownership engine). +//! peer only redacted, for its ownership engine, and the placeholders such a +//! peer echoes back as music are left out of what it sends: +//! `drop_echoed_private_items`). use music_dht::playback::{Checkpoint, CommandStamp, Config as PlaybackConfig, Engine}; use std::collections::{BTreeMap, BTreeSet}; @@ -205,6 +207,74 @@ pub fn is_private_track_item(track: &TrackItem) -> bool { && track.title == music_dht::device_sync::PRIVATE_ITEM_TITLE } +/// Whether a music item received from a device below protocol v4 is a +/// "Private item" placeholder that it echoes back. +/// +/// Such a device gets this device's queue redacted +/// ([`DeviceSync::local_playback_snapshot_for`]) and, while it controls this +/// device, mirrors it with its own types, which have no +/// `PlaybackTrack::source`. Each placeholder then turns into a music item +/// titled "Private item" with nothing else: no content id, federated +/// source, artist, release or duration. When that device takes playback +/// over (Play on it, a paused-startup claim, automatic failover) or sends a +/// command, it announces those items as its queue. +fn is_echoed_private_item(track: &PlaybackTrack) -> bool { + track.source.is_none() + && track.title == music_dht::device_sync::PRIVATE_ITEM_TITLE + && track.content_id.is_none() + && track.fed.is_none() + && track.artist_names.is_empty() + && track.featured_artist_names.is_empty() + && track.release_title.is_empty() + && track.duration_seconds == 0.0 +} + +/// Removes the "Private item" placeholders that a device below protocol v4 +/// echoed back ([`is_echoed_private_item`]) from a state it sent, and +/// returns how many there were. They stand for episodes this device keeps +/// private; as music items they could never play, and they would spread to +/// every queue as such. The position moves with the queue; when the +/// current item was one of them, the next item is current from its start. +pub fn drop_echoed_private_items(state: &mut PlaybackStateWire) -> usize { + let current = state.queue_pos; + let mut dropped = 0; + let mut kept_before = 0; + let mut current_dropped = false; + let mut index = 0; + state.queue.retain(|track| { + let echoed = is_echoed_private_item(track); + if echoed { + dropped += 1; + current_dropped |= index == current; + } else if index < current { + kept_before += 1; + } + index += 1; + !echoed + }); + if dropped == 0 { + return 0; + } + state.queue_pos = kept_before.min(state.queue.len().saturating_sub(1)); + if current_dropped { + state.position_secs = 0.0; + } + if state.queue.is_empty() { + state.playing = false; + } + dropped +} + +/// Status for placeholders left out of a state from an older device. +fn echoed_private_items_message(device: &str, dropped: usize) -> String { + let items = if dropped == 1 { "entry" } else { "entries" }; + format!( + "left out {dropped} \"{}\" {items} of {device}'s queue: they stand for podcast \ + episodes, and {device} needs a Furumi update to play them", + music_dht::device_sync::PRIVATE_ITEM_TITLE + ) +} + /// Queue entries for a received queue, with `resolve_music` deciding music /// items (a library copy, for example). Podcast episodes keep their typed /// reference; Jam private placeholders are numbered in queue order. @@ -396,6 +466,9 @@ struct PlaybackShared { engine_group: String, local: Option, remote: BTreeMap, + /// Echoed placeholders last left out of each older device's snapshot + /// (`drop_echoed_private_items`), to report a change only once. + echoed: BTreeMap, } #[derive(Debug, Clone)] @@ -1957,6 +2030,18 @@ impl DeviceSync { } return Ok(()); } + // Its state may hold the placeholders it was sent, echoed back as + // music items: they are left out, never adopted. + let mut command = command.clone(); + let echoed = if music_dht::device_sync::supports_podcasts(peer_version) { + 0 + } else { + match &mut command { + PlaybackCommand::SetState { state, .. } + | PlaybackCommand::ActiveChanged { state, .. } => drop_echoed_private_items(state), + } + }; + let command = &command; let handoff = matches!(command, PlaybackCommand::ActiveChanged { .. }); if !handoff && target_device_id != identity.device_id { return Ok(()); @@ -1992,6 +2077,12 @@ impl DeviceSync { authority: authority.clone(), origin: origin.to_string(), }); + if echoed > 0 { + let _ = tx.send(AppEvent::StatusMessage(echoed_private_items_message( + &self.device_label(origin), + echoed, + ))); + } } Ok(()) } @@ -3056,7 +3147,7 @@ impl DeviceSync { Some(snapshot) } - fn apply_playback_snapshot(&self, sender: &str, snapshot: PlaybackSnapshot) -> Result<()> { + fn apply_playback_snapshot(&self, sender: &str, mut snapshot: PlaybackSnapshot) -> Result<()> { let identity = self.ensure_identity()?; if snapshot.device_id != sender || snapshot.device_id == identity.device_id { return Ok(()); @@ -3064,12 +3155,18 @@ impl DeviceSync { let Some(coordination) = &snapshot.coordination else { return Ok(()); }; + let echoed = + if music_dht::device_sync::supports_podcasts(self.peer_protocol_version(sender)?) { + 0 + } else { + drop_echoed_private_items(&mut snapshot.state) + }; if !self.with_playback_engine(|engine| { engine.observe(&snapshot.device_id, coordination, playback_clock()) })? { return Ok(()); } - let should_send = { + let (should_send, report_echoed) = { let mut shared = lock(&self.playback); let changed = shared.remote.get(&snapshot.device_id).is_none_or(|old| { old.coordination.as_ref().is_none_or(|c| { @@ -3082,10 +3179,17 @@ impl DeviceSync { .remote .insert(snapshot.device_id.clone(), snapshot.clone()); } - changed + let reported = shared.echoed.insert(snapshot.device_id.clone(), echoed); + (changed, changed && echoed > 0 && reported != Some(echoed)) }; if should_send && let Some(tx) = lock(&self.event_tx).as_ref() { + let device = snapshot.device_name.clone(); let _ = tx.send(AppEvent::DevicePlayback(snapshot)); + if report_echoed { + let _ = tx.send(AppEvent::StatusMessage(echoed_private_items_message( + &device, echoed, + ))); + } } Ok(()) } diff --git a/src/devices/podcast_sync_tests.rs b/src/devices/podcast_sync_tests.rs index a106b1a..e17e718 100644 --- a/src/devices/podcast_sync_tests.rs +++ b/src/devices/podcast_sync_tests.rs @@ -891,6 +891,272 @@ fn an_old_device_keeps_hearing_from_a_podcast_queue_owner_and_never_fails_over() assert!(local.state.queue[1].podcast_episode_ref().is_some()); } +/// What a 0.3.x device makes of a queue item it received and announces +/// again: it mirrors the item with its own types, which have no +/// `PlaybackTrack::source`, and serializes the queue entry back (the same +/// music path as `to_track_item` / `from_track` here). +fn republished_by_old_device( + received: &super::wire_compat_tests::legacy::PlaybackTrack, +) -> PlaybackTrack { + let parsed: PlaybackTrack = + serde_json::from_value(serde_json::to_value(received).unwrap()).unwrap(); + assert!(parsed.source.is_none(), "the old types carry no source"); + PlaybackTrack::from_track(&parsed.to_track_item()) +} + +fn other_music_wire() -> PlaybackTrack { + let mut track = music_wire(); + track.title = "Other song".into(); + track.content_id = Some(format!("b3:{}", "e".repeat(64))); + track +} + +fn queue_titles(state: &PlaybackStateWire) -> Vec<&str> { + state + .queue + .iter() + .map(|track| track.title.as_str()) + .collect() +} + +/// An old device mirrors the redacted queue it is sent while this device +/// owns playback. When it then takes over (Play on it, a paused-startup +/// claim, or automatic failover once this device was gone for two +/// minutes), it announces that queue as its own, each "Private item" now an +/// ordinary music item that nothing can play. This device leaves those out +/// instead of adopting them, and says why. +#[test] +fn placeholders_an_old_device_echoes_back_are_left_out() { + use super::wire_compat_tests::legacy; + + let mut a = device(); + trust_old_device(&a); + a.sync.claim_playback(&a.id).unwrap(); + let episode = episode_ref(9); + let mut state = state_with( + vec![ + music_wire(), + PlaybackTrack::podcast_episode(episode.clone()), + other_music_wire(), + ], + 42.0, + ); + state.queue_pos = 1; + state.paused = false; + a.sync.playback_tick(true, true).unwrap(); + publish(&a, state); + + // The old device receives the redacted queue and mirrors it. + let legacy::WireMessage::Hello { + playback: Some(received), + .. + } = serde_json::from_str(&hello_json(&a, "dev_old")).unwrap() + else { + panic!("the old device gets a playback snapshot"); + }; + let mirrored: Vec = received + .state + .queue + .iter() + .map(republished_by_old_device) + .collect(); + let echoed = mirrored[1].clone(); + assert_eq!(echoed.title, music_dht::device_sync::PRIVATE_ITEM_TITLE); + assert!(echoed.source.is_none(), "a music item now"); + assert!(echoed.id < 0, "the old device's portable id"); + assert!( + !is_private_track_item(&echoed.to_track_item()), + "not a placeholder of this device" + ); + + // It takes over and announces the mirrored queue, with a higher claim. + let mut old = Engine::new( + "dev_old".into(), + PlaybackConfig::default(), + Default::default(), + 0, + ); + assert!(old.observe(&a.id, received.coordination.as_ref().unwrap(), 0)); + assert!(old.transfer("dev_old", 1)); + let announced: legacy::PlaybackStateWire = serde_json::from_value( + serde_json::to_value(PlaybackStateWire { + queue: mirrored, + ..received_state_v4(&received.state) + }) + .unwrap(), + ) + .unwrap(); + let old_profile = legacy::DeviceProfileWire { + device_id: "dev_old".into(), + name: "Old laptop".into(), + client_version: "0.3.5".into(), + protocol_version: 3, + endpoint_id: String::new(), + endpoint_ticket: String::new(), + revoked: false, + revoke_cutoff_seq: None, + updated_at_ms: 1, + }; + let hello = legacy::WireMessage::Hello { + group_id: a.sync.ensure_identity().unwrap().group_id, + profile: old_profile.clone(), + devices: vec![old_profile], + vector: BTreeMap::new(), + ops: Vec::new(), + snapshot: legacy::SyncSnapshot::default(), + playback: Some(legacy::PlaybackSnapshot { + device_id: "dev_old".into(), + device_name: "Old laptop".into(), + active: true, + updated_at_ms: now_ms(), + state: announced, + coordination: Some(old.announcement()), + }), + }; + while a.events.try_recv().is_ok() {} + receive(&a.sync, &serde_json::to_string(&hello).unwrap()); + + // This device follows the takeover with the music items only. + let mut adopted = None; + let mut messages = Vec::new(); + while let Ok(event) = a.events.try_recv() { + match event { + AppEvent::DevicePlayback(snapshot) => adopted = Some(snapshot), + AppEvent::StatusMessage(message) => messages.push(message), + _ => {} + } + } + let adopted = adopted.expect("the takeover reaches the UI"); + assert_eq!( + a.sync + .with_playback_engine(|engine| engine.owner().map(str::to_owned)) + .unwrap() + .as_deref(), + Some("dev_old") + ); + assert_eq!(queue_titles(&adopted.state), ["Song", "Other song"]); + // The echoed item was current: the next one is, from its start. + assert_eq!(adopted.state.queue_pos, 1); + assert_eq!(adopted.state.position_secs, 0.0); + let items = track_items_from_wire(&adopted.state.queue, PlaybackTrack::to_track_item); + assert!( + items + .iter() + .all(|item| item.title != music_dht::device_sync::PRIVATE_ITEM_TITLE), + "{items:?}" + ); + assert!( + messages + .iter() + .any(|message| message.contains("Old laptop") && message.contains("Private item")), + "{messages:?}" + ); + // Its next announcements say nothing new. + let mut again = adopted.clone(); + again.state.queue.insert(1, echoed.clone()); + old.heartbeat(5_000); + again.coordination = Some(old.announcement()); + a.sync.apply_playback_snapshot("dev_old", again).unwrap(); + assert!( + std::iter::from_fn(|| a.events.try_recv().ok()) + .all(|event| !matches!(event, AppEvent::StatusMessage(_))) + ); + + // A command it builds from its stale mirror, once this device's queue + // has no episode left (so commands from it are accepted), is cleaned + // the same way. + let mut b = device(); + trust_old_device(&b); + b.sync.claim_playback(&b.id).unwrap(); + publish(&b, state_with(vec![music_wire()], 0.0)); + let mut old = Engine::new( + "dev_old".into(), + PlaybackConfig::default(), + Default::default(), + 0, + ); + old.transfer("dev_old", 0); + let stamp = old.stamp().unwrap(); + let mut stale = state_with(vec![music_wire(), echoed, other_music_wire()], 7.0); + stale.queue_pos = 2; + let take_over = PlaybackCommand::ActiveChanged { + active_device_id: "dev_old".into(), + active_device_name: "Old laptop".into(), + state: stale, + }; + b.sync + .apply_playback_command(&b.id, &take_over, Some(&stamp), "dev_old", "dev_old:7") + .unwrap(); + let mut applied = None; + let mut messages = Vec::new(); + while let Ok(event) = b.events.try_recv() { + match event { + AppEvent::PlaybackCommand { + command: PlaybackCommand::ActiveChanged { state, .. }, + .. + } => applied = Some(state), + AppEvent::StatusMessage(message) => messages.push(message), + _ => {} + } + } + let applied = applied.expect("an ordinary handoff"); + assert_eq!(queue_titles(&applied), ["Song", "Other song"]); + assert_eq!(applied.queue_pos, 1); + assert_eq!(applied.position_secs, 7.0, "the current item was kept"); + assert!( + messages + .iter() + .any(|message| message.contains("Old laptop")), + "{messages:?}" + ); +} + +/// The v4 form of a state an old device received (its queue is replaced). +fn received_state_v4( + state: &super::wire_compat_tests::legacy::PlaybackStateWire, +) -> PlaybackStateWire { + serde_json::from_value(serde_json::to_value(state).unwrap()).unwrap() +} + +/// Only a bare music item titled "Private item" counts as an echoed +/// placeholder: a song with that title and an artist or a content id stays, +/// and so does a real placeholder (its source says so). +#[test] +fn only_bare_music_items_from_old_devices_count_as_echoed_placeholders() { + let echoed = { + let mut track = PlaybackTrack::private_placeholder(); + track.source = None; + track.id = -42; + track + }; + let mut named_song = echoed.clone(); + named_song.artist_names = vec!["Band".into()]; + let mut with_content = echoed.clone(); + with_content.content_id = Some(format!("b3:{}", "f".repeat(64))); + let mut state = state_with( + vec![ + echoed.clone(), + named_song, + with_content, + PlaybackTrack::private_placeholder(), + echoed, + ], + 3.0, + ); + state.queue_pos = 2; + assert_eq!(drop_echoed_private_items(&mut state), 2); + assert_eq!(state.queue.len(), 3); + assert_eq!(state.queue_pos, 1, "the same current item"); + assert_eq!(state.position_secs, 3.0); + assert!(state.queue[2].is_private_placeholder()); + + let mut only_echoes = state_with(vec![state.queue[0].clone()], 1.0); + only_echoes.queue[0].artist_names.clear(); + assert_eq!(drop_echoed_private_items(&mut only_echoes), 1); + assert!(only_echoes.queue.is_empty()); + assert!(!only_echoes.playing); +} + /// Feed and audio URLs may carry private tokens; the debug log (shown in /// the Logs tab) never prints them. #[test]