diff --git a/architecture.md b/architecture.md index 1c14505..1eccf8b 100644 --- a/architecture.md +++ b/architecture.md @@ -401,8 +401,9 @@ work without waiting for a socket response. Pending volume work is latest-value-per-object; mute presses remain a bounded FIFO. Confirmed snapshots replace the local base and retain optimistic overlays until observation settles them. Poll quickly while writes are pending, slowly while idle, and resnapshot -then retry once after a stale edit. Start immediately with a disconnected -placeholder when the socket is absent, retain the last confirmed items across +then retry a bounded number of times after stale mutations so consecutive +server revisions cannot roll an optimistic edit back. Start immediately with a +disconnected placeholder when the socket is absent, retain the last confirmed items across transport failures, surface the connection error inside the terminal, and keep retrying so starting or restarting the server reconnects without restarting the TUI. diff --git a/task.md b/task.md index a8f699c..a9abfb6 100644 --- a/task.md +++ b/task.md @@ -246,8 +246,9 @@ Dependencies: M3, M2; may proceed after the fake-adapter slice. cannot monopolize the state owner. - [x] **M7.3 Client resync:** full snapshot on attach/lag and revision checks for edits. Tests: TUI exit never stops the server; reconnect shows current observations; concurrent clients cannot overwrite each other's edits unnoticed. Each request receives a complete current snapshot; stale edit revisions - trigger a background resnapshot and one retry. The TUI's IPC worker exits - independently of the server, starts with a disconnected placeholder when the + trigger bounded background resnapshot/retry attempts, covering consecutive + server revisions without allowing an unbounded retry loop. The TUI's IPC + worker exits independently of the server, starts with a disconnected placeholder when the socket is absent, retains the last confirmed view during failures, and retries until a server appears again. Competing edits from one attached revision prove that only the first is accepted and that a lagging client obtains the newer diff --git a/tui/src/lib.rs b/tui/src/lib.rs index a9a364c..2f7f53d 100644 --- a/tui/src/lib.rs +++ b/tui/src/lib.rs @@ -676,6 +676,8 @@ fn execute_action( confirmed: &mut MixerSnapshot, action: &BufferedAction, ) -> io::Result { + const MAX_STALE_ATTEMPTS: usize = 8; + let execute = |client: &mut C, revision, configuration_revision| match action { BufferedAction::SetVolume { id, gain } => client.set_volume(id.clone(), *gain, revision), BufferedAction::ToggleMute { id } => client.toggle_mute(id.clone(), revision), @@ -691,24 +693,36 @@ fn execute_action( BufferedAction::CancelMidiLearn => client.cancel_midi_learn(), BufferedAction::SaveConfiguration => client.save_configuration(configuration_revision), }; - match execute(client, confirmed.revision, confirmed.configuration.revision) { - Ok(snapshot) => Ok(snapshot), - Err(first) - if matches!(action, BufferedAction::SetVolume { .. }) - || (matches!(action, BufferedAction::EditConfiguration { .. }) - && first.kind() == io::ErrorKind::InvalidData) - || (matches!(action, BufferedAction::SaveConfiguration) - && first.kind() == io::ErrorKind::InvalidData) - || (matches!(action, BufferedAction::ToggleMute { .. }) - && first.kind() == io::ErrorKind::InvalidData) => - { - *confirmed = client.snapshot()?; - execute(client, confirmed.revision, confirmed.configuration.revision).map_err( - |second| io::Error::other(format!("{first}; retry after resync failed: {second}")), - ) + let can_retry_stale = matches!( + action, + BufferedAction::SetVolume { .. } + | BufferedAction::ToggleMute { .. } + | BufferedAction::EditConfiguration { .. } + | BufferedAction::SaveConfiguration + ); + let mut first_error = None; + for attempt in 0..MAX_STALE_ATTEMPTS { + match execute(client, confirmed.revision, confirmed.configuration.revision) { + Ok(snapshot) => return Ok(snapshot), + Err(error) + if can_retry_stale + && error.kind() == io::ErrorKind::InvalidData + && attempt + 1 < MAX_STALE_ATTEMPTS => + { + first_error.get_or_insert_with(|| error.to_string()); + *confirmed = client.snapshot()?; + } + Err(error) => { + return Err(match first_error { + Some(first) => { + io::Error::other(format!("{first}; retry after resync failed: {error}")) + } + None => error, + }); + } } - Err(error) => Err(error), } + unreachable!("bounded action retry loop always returns") } fn publish_confirmed(state: &mut BufferedState, confirmed: &MixerSnapshot) { @@ -5580,4 +5594,84 @@ mod tests { assert_eq!(result.revision, 6); assert_eq!(result.items[0].gain, Some(0.8)); } + + #[test] + fn buffered_configuration_edit_survives_consecutive_revision_races() { + struct RacingConfigurationClient { + snapshot: MixerSnapshot, + attempts: Vec, + } + + impl MixerClient for RacingConfigurationClient { + fn snapshot(&mut self) -> io::Result { + Ok(self.snapshot.clone()) + } + + fn set_volume( + &mut self, + _id: MixerObjectId, + _gain: f64, + _expected_revision: u64, + ) -> io::Result { + unreachable!() + } + + fn toggle_mute( + &mut self, + _id: MixerObjectId, + _expected_revision: u64, + ) -> io::Result { + unreachable!() + } + + fn edit_configuration( + &mut self, + edit: ConfigurationEdit, + expected_revision: u64, + ) -> io::Result { + self.attempts.push(expected_revision); + if self.attempts.len() <= 2 { + self.snapshot.configuration.revision += 1; + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!( + "stale configuration revision {expected_revision}; current revision is {}", + self.snapshot.configuration.revision + ), + )); + } + apply_optimistic_configuration_edit(&mut self.snapshot, &edit); + self.snapshot.configuration.revision += 1; + Ok(self.snapshot.clone()) + } + } + + let layout = |id: &str| MixerLayoutEntry::Separator { + id: id.to_owned(), + label: id.to_owned(), + }; + let mut confirmed = DemoClient::default().view(); + confirmed.configuration.revision = 2; + confirmed.configuration.mixer_layout = vec![layout("a"), layout("b")]; + let mut client = RacingConfigurationClient { + snapshot: confirmed.clone(), + attempts: Vec::new(), + }; + + let result = execute_action( + &mut client, + &mut confirmed, + &BufferedAction::EditConfiguration { + edit: ConfigurationEdit::MoveLayoutEntry { + id: "a".to_owned(), + position: 1, + }, + }, + ) + .unwrap(); + + assert_eq!(client.attempts, [2, 3, 4]); + assert_eq!(layout_ids(&result), ["b", "a"]); + assert_eq!(result.configuration.revision, 5); + } }