From 637a4e4e17aac0ae2e5a8868aa0ba4577c9a3d3e Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 18 Nov 2026 15:27:59 +0100 Subject: [PATCH] Offer explicit resolution choices for device state conflicts --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/canvas-pilot/src/device_send.rs | 3 +- crates/canvas-pilot/src/device_ui.rs | 376 ++++++++++++++++++++++++- docs/canvas-pilot/changelog.md | 8 + docs/canvas-pilot/guide/device.md | 19 ++ docs/interfaces/midra-device-send.md | 14 + 7 files changed, 418 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d751e3..7687f68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,7 +602,7 @@ dependencies = [ [[package]] name = "canvas-pilot" -version = "0.9.62" +version = "0.9.63" dependencies = [ "base64 0.22.1", "craft-dwg", diff --git a/Cargo.toml b/Cargo.toml index f6e8fb5..d6c4f18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["crates/canvas-pilot"] resolver = "3" [workspace.package] -version = "0.9.62" +version = "0.9.63" edition = "2024" license = "MIT" repository = "https://proxy.dandaedre.com/gitea/idle-org/canvas-pilot" diff --git a/crates/canvas-pilot/src/device_send.rs b/crates/canvas-pilot/src/device_send.rs index da8c9a4..4756bf1 100644 --- a/crates/canvas-pilot/src/device_send.rs +++ b/crates/canvas-pilot/src/device_send.rs @@ -458,7 +458,8 @@ pub(crate) fn send_with_awj_port( execute(plan, &mut connection) } -fn validate_fresh(plan: &Plan, fresh: &Snapshot) -> Result<(), String> { +/// Read-only stale-state validation; does not weaken execution-time checks. +pub fn validate_fresh(plan: &Plan, fresh: &Snapshot) -> Result<(), String> { if fresh.profile != plan.profile { return Err("Device profile changed; reconnect and review mapping.".into()); } diff --git a/crates/canvas-pilot/src/device_ui.rs b/crates/canvas-pilot/src/device_ui.rs index 83e070d..f986d36 100644 --- a/crates/canvas-pilot/src/device_ui.rs +++ b/crates/canvas-pilot/src/device_ui.rs @@ -9,12 +9,14 @@ use std::sync::mpsc::{self, Receiver, TryRecvError}; struct DeviceFailure { message: String, refreshed: Option>, + conflict: bool, } impl From for DeviceFailure { fn from(message: String) -> Self { Self { message, refreshed: None, + conflict: false, } } } @@ -29,15 +31,155 @@ impl DeviceFailure { Ok(snapshot) => Self { message, refreshed: Some(Box::new(snapshot)), + conflict: false, }, Err(error) => Self { message: format!("{message}\nCould not refresh device state: {error}"), refreshed: None, + conflict: false, }, } } } +#[derive(Clone)] +struct SendConflict { + host: String, + plans: Vec, + links: Vec<(usize, canvas_pilot::DeviceLink)>, + sizing: Sizing, + details: Vec, +} +impl SendConflict { + fn reload_links(&self) -> Result, String> { + let mut links = std::collections::BTreeMap::new(); + for (id, link) in &self.links { + if let Some(previous) = links.insert(*id, link.clone()) + && previous != *link + { + return Err("A local layer has multiple device destinations. Cancel and choose which layer to load using the mapping controls.".into()); + } + } + Ok(links.into_iter().collect()) + } + fn reprepare( + &self, + snapshot: &device::Snapshot, + layout: &canvas_pilot::Layout, + ) -> Result, String> { + self.plans + .iter() + .map(|old| { + if old.profile != snapshot.profile { + return Err( + "The device family changed. Reconnect and check the mapping.".into(), + ); + } + let index = snapshot + .screens + .iter() + .position(|s| s.id == old.screen) + .ok_or("A destination screen is no longer available.")?; + let screen = &snapshot.screens[index]; + let bank = if old.target == Target::Preview { + &screen.preview_bank + } else { + &screen.program_bank + }; + if bank != &old.bank { + return Err( + "Preview/Program roles changed. Cancel and select the destination again." + .into(), + ); + } + let mappings: Vec<_> = self + .links + .iter() + .filter(|(_, l)| l.screen == old.screen) + .map(|(id, l)| (*id, l.layer.clone())) + .collect(); + if mappings.is_empty() { + return Err("The original mapping is no longer available.".into()); + } + Plan::prepare(snapshot, layout, index, &mappings, old.target, self.sizing) + }) + .collect() + } +} + +fn conflict_details(plans: &[Plan], fresh: &device::Snapshot) -> Vec { + fn diff( + path: &str, + before: &serde_json::Value, + after: &serde_json::Value, + out: &mut std::collections::BTreeMap, + ) { + if before == after { + return; + } + if let (Some(a), Some(b)) = (before.as_object(), after.as_object()) { + let keys: std::collections::BTreeSet<_> = a.keys().chain(b.keys()).collect(); + for key in keys { + diff( + &format!("{path}/{key}"), + a.get(key).unwrap_or(&serde_json::Value::Null), + b.get(key).unwrap_or(&serde_json::Value::Null), + out, + ); + } + } else { + let label = path + .split('/') + .filter(|p| !matches!(*p, "" | "device" | "items" | "pp" | "status" | "control")) + .map(|p| match p { + "screenList" => "Screen", + "liveLayerList" => "Layer", + "presetList" => "Bank", + "inputList" => "Input", + "posH" => "Horizontal center", + "posV" => "Vertical center", + "sizeH" => "Width", + "sizeV" => "Height", + _ => p, + }) + .collect::>() + .join(" / "); + out.insert(path.into(), format!("{label}: {before} → {after}")); + } + } + let mut changes = std::collections::BTreeMap::new(); + for plan in plans { + for (path, before) in &plan.observed { + diff( + path, + before, + fresh.raw.pointer(path).unwrap_or(&serde_json::Value::Null), + &mut changes, + ); + } + for c in &plan.changes { + let path = format!("/{}", c.path.join("/")); + diff( + &path, + &c.before, + fresh.raw.pointer(&path).unwrap_or(&serde_json::Value::Null), + &mut changes, + ); + } + } + let count = changes.len(); + let mut details: Vec<_> = changes.into_values().take(100).collect(); + if count > 100 { + details.push(format!("{} additional device fields changed.", count - 100)); + } + if details.is_empty() { + details.push( + "Device family, destination availability or Preview/Program roles changed.".into(), + ); + } + details +} + fn linked_layer_label(layout: &canvas_pilot::Layout, id: usize) -> String { (0..layout.layers().len()) .find(|&i| layout.layer_info(i).id == id) @@ -157,6 +299,8 @@ pub(super) struct DeviceUi { destinations: Vec, mapping_scope: Option<(TransferScope, Vec, String)>, error: String, + send_attempt: Option, + conflict: Option, } impl DeviceUi { pub(super) fn try_startup_connection(&mut self, ctx: &egui::Context) { @@ -318,7 +462,7 @@ impl DeviceUi { pub(super) fn online(&self) -> bool { self.snapshot.is_some() } - fn send(&mut self, ctx: &egui::Context, plans: Vec) { + fn send(&mut self, ctx: &egui::Context, plans: Vec, layout: &canvas_pilot::Layout) { if self.input_sending { self.error = "Wait for the input send to finish.".into(); return; @@ -326,11 +470,52 @@ impl DeviceUi { let (tx, rx) = mpsc::channel(); self.pending = Some(rx); self.sending = true; + let links = self + .scoped_send_links(layout) + .into_iter() + .filter(|(_, link)| { + plans.iter().any(|p| { + p.screen == link.screen + && p.observed.iter().any(|(path, _)| { + *path == p.profile.layer_path(&p.screen, &p.bank, &link.layer) + }) + }) + }) + .collect(); + self.send_attempt = Some(SendConflict { + host: self.host.clone(), + plans: plans.clone(), + links, + sizing: self.sizing, + details: vec![], + }); + self.conflict = None; self.error.clear(); self.message.clear(); let host = self.host.clone(); let ctx = ctx.clone(); std::thread::spawn(move || { + // Check every screen before the first write, so this dialog cannot conceal a partial send. + let fresh = match device::fetch(&host) { + Ok(snapshot) => snapshot, + Err(error) => { + let _ = tx.send(Err(error.into())); + ctx.request_repaint(); + return; + } + }; + if let Some(error) = plans + .iter() + .find_map(|plan| device_send::validate_fresh(plan, &fresh).err()) + { + let _ = tx.send(Err(DeviceFailure { + message: error, + refreshed: Some(Box::new(fresh)), + conflict: true, + })); + ctx.request_repaint(); + return; + } let result: Result = (|| { let mut last = None; for (i, plan) in plans.iter().enumerate() { @@ -400,6 +585,12 @@ impl DeviceUi { self.error = failure.message.clone(); match failure.refreshed { Some(snapshot) => { + if failure.conflict + && let Some(mut attempt) = self.send_attempt.take() + { + attempt.details = conflict_details(&attempt.plans, &snapshot); + self.conflict = Some(attempt); + } self.message = "Device state refreshed. Check the updated layers before sending again; the send was not retried.".into(); Ok(*snapshot) } @@ -408,6 +599,7 @@ impl DeviceUi { } other => other.map_err(|failure| failure.message), }; + self.send_attempt = None; match result { Ok(snapshot) => { if let Ok(address) = device::address(&self.host) { @@ -783,7 +975,7 @@ impl Editor { { match plans { Ok(plans) => { - self.device.send(ui.ctx(), plans); + self.device.send(ui.ctx(), plans, &self.layout); } Err(error) => { self.device.error = error; @@ -850,6 +1042,76 @@ impl Editor { self.settings.save(&self.application_settings(ctx)); } } + fn send_conflict_window(&mut self, ctx: &egui::Context) { + let Some(conflict) = self.device.conflict.clone() else { + return; + }; + if conflict.host != self.device.host || !self.device.online() { + self.device.conflict = None; + return; + } + let prepared = conflict.reprepare(self.device.snapshot.as_ref().unwrap(), &self.layout); + let program = conflict.plans.iter().any(|p| p.target == Target::Program); + let force_allowed = + prepared.is_ok() && (!program || self.device.allow_program) && !self.device.busy(); + let reload_links = conflict.reload_links(); + let reload_allowed = + !program && !conflict.links.is_empty() && reload_links.is_ok() && !self.device.busy(); + let mut action = 0; + let modal=egui::Modal::new(egui::Id::new("device-send-conflict")).show(ctx, |ui| { + ui.set_max_width(680.); + ui.heading("Device state has changed"); + ui.label("The device no longer matches the state used to prepare this send. No layers were sent."); + egui::ScrollArea::vertical().max_height(260.).show(ui,|ui| {for detail in &conflict.details {ui.label(detail);}}); + ui.separator(); + ui.label("Send my layout anyway rebuilds the send against the current device state. Input routing is not changed, and all validation checks remain active."); + if let Err(error)=&prepared {ui.colored_label(ui.visuals().error_fg_color,error);} + ui.label("Discard local changes and reload replaces the mapped local layers’ geometry, crop, fit and masks with the device Preview values. Other layers remain unchanged. Undo is available."); + if program {ui.small("Reload is available for Preview destinations only.");} + if let Err(error)=&reload_links {ui.colored_label(ui.visuals().error_fg_color,error);} + if !self.device.error.is_empty() {ui.small(&self.device.error);} + ui.horizontal_wrapped(|ui| { + if ui.button("Cancel send").clicked() {action=1;} + if ui.add_enabled(force_allowed,egui::Button::new("Send my layout anyway")).clicked() {action=2;} + let destructive=egui::Button::new(egui::RichText::new("Discard local changes and reload").color(ui.visuals().error_fg_color)); + if ui.add_enabled(reload_allowed,destructive).clicked() {action=3;} + }); + }); + if modal.should_close() { + action = 1; + } + match action { + 1 => { + self.device.conflict = None; + } + 2 => { + if let Ok(plans) = prepared { + self.device.conflict = None; + let mut attempt = conflict.clone(); + attempt.plans = plans.clone(); + self.device.send(ctx, plans, &self.layout); + self.device.send_attempt = Some(attempt); + } + } + 3 => { + match load_linked_layers( + &mut self.layout, + self.device.snapshot.as_ref().unwrap(), + reload_links.as_ref().unwrap(), + ) { + Ok(()) => { + self.sync(); + self.device.conflict = None; + self.device.error.clear(); + self.device.message="Mapped layers reloaded from the device. Undo restores the previous local layout.".into(); + } + Err(error) => self.device.error = error, + } + } + _ => {} + } + } + pub(super) fn device_window(&mut self, ctx: &egui::Context) { if !self.device.allow_program { self.device.target = Target::Preview; @@ -857,6 +1119,7 @@ impl Editor { let was_pending = self.device.pending.is_some(); let recent_before = self.device.recent_hosts.clone(); self.device.poll(); + self.send_conflict_window(ctx); if self.device.recent_hosts != recent_before { self.settings.save(&self.application_settings(ctx)); } @@ -1057,7 +1320,7 @@ impl Editor { } self.device.open = open; if let Some(plan) = send_plan { - self.device.send(ctx, vec![plan]); + self.device.send(ctx, vec![plan], &self.layout); } if imported { self.sync(); @@ -1421,6 +1684,113 @@ mod tests { ) .unwrap() } + fn conflict_fixture() -> (Editor, SendConflict) { + let mut app = Editor::default(); + let old = linked_test_snapshot(); + let link = canvas_pilot::DeviceLink { + family: device::Family::Midra4k, + screen: "1".into(), + layer: "1".into(), + }; + app.layout.set_device_link(0, Some(link.clone())).unwrap(); + app.device.snapshot = Some(old.clone()); + let plans = app.device.linked_plans(&app.layout).unwrap(); + let mut fresh = old; + fresh.raw["device"]["screenList"]["items"]["1"]["presetList"]["items"]["UP"]["liveLayerList"] + ["items"]["1"]["position"]["pp"]["posH"] = serde_json::json!(900); + fresh.screens[0].layers[0].geometry.x += 100.; + let details = conflict_details(&plans, &fresh); + let conflict = SendConflict { + host: app.device.host.clone(), + plans, + links: vec![(app.layout.layer_info(0).id, link)], + sizing: Sizing::default(), + details, + }; + app.device.snapshot = Some(fresh); + (app, conflict) + } + #[test] + fn conflict_reprepare_preserves_intent_and_enforces_fresh_state() { + let (app, conflict) = conflict_fixture(); + let fresh = app.device.snapshot.as_ref().unwrap(); + assert!( + conflict + .details + .iter() + .any(|s| s.contains("Horizontal center: 500 → 900")) + ); + assert!(device_send::validate_fresh(&conflict.plans[0], fresh).is_err()); + let plans = conflict.reprepare(fresh, &app.layout).unwrap(); + assert!(device_send::validate_fresh(&plans[0], fresh).is_ok()); + let mut moved_again = fresh.clone(); + moved_again.raw["device"]["screenList"]["items"]["1"]["presetList"]["items"]["UP"]["liveLayerList"] + ["items"]["1"]["position"]["pp"]["posH"] = serde_json::json!(901); + assert!(device_send::validate_fresh(&plans[0], &moved_again).is_err()); + let mut swapped = fresh.clone(); + swapped.screens[0].preview_bank = "DOWN".into(); + assert!( + conflict + .reprepare(&swapped, &app.layout) + .unwrap_err() + .contains("roles changed") + ); + } + #[test] + fn preflight_conflict_opens_choices_with_fresh_connected_state() { + let (mut app, conflict) = conflict_fixture(); + let fresh = app.device.snapshot.take().unwrap(); + let (tx, rx) = mpsc::channel(); + app.device.pending = Some(rx); + app.device.sending = true; + app.device.send_attempt = Some(conflict); + tx.send(Err(DeviceFailure { + message: "Device state changed".into(), + refreshed: Some(Box::new(fresh)), + conflict: true, + })) + .unwrap(); + app.device.poll(); + assert!(app.device.online()); + assert!( + app.device + .conflict + .as_ref() + .unwrap() + .details + .iter() + .any(|s| s.contains("500 → 900")) + ); + assert!(app.device.pending.is_none()); + assert!(!app.device.sending); + } + #[test] + fn conflict_reload_requires_unambiguous_destinations() { + let (_, mut conflict) = conflict_fixture(); + let mut second = conflict.links[0].clone(); + second.1.layer = "2".into(); + conflict.links.push(second); + assert!(conflict.reload_links().is_err()); + } + #[test] + fn conflict_cancel_and_reload_are_local_and_reload_is_undoable() { + let ctx = egui::Context::default(); + let (mut app, conflict) = conflict_fixture(); + let before = app.layout.layers().clone(); + app.device.conflict = Some(conflict.clone()); + super::super::tests::click_text(&mut app, &ctx, "Cancel send"); + assert!(app.device.conflict.is_none()); + assert_eq!(app.layout.layers(), &before); + assert!(app.device.pending.is_none()); + app.device.conflict = Some(conflict); + super::super::tests::click_text(&mut app, &ctx, "Discard local changes and reload"); + assert!(app.device.conflict.is_none()); + assert_ne!(app.layout.layers()[0], before[0]); + assert_eq!(app.layout.layers()[1], before[1]); + assert!(app.device.pending.is_none()); + app.layout.undo(); + assert_eq!(app.layout.layers(), &before); + } #[test] fn directional_routes_load_separately_and_send_one_source_to_multiple_layers() { let snapshot = linked_test_snapshot(); diff --git a/docs/canvas-pilot/changelog.md b/docs/canvas-pilot/changelog.md index 993f34e..bd3882a 100644 --- a/docs/canvas-pilot/changelog.md +++ b/docs/canvas-pilot/changelog.md @@ -4,6 +4,14 @@ ## Unreleased +### v0.9.63 + +- Added a device-state conflict dialog with changed fields, Cancel send, Send my + layout anyway, and a warning-colored Discard local changes and reload action. + All screens are checked before the first write. Explicit overwrite rebuilds + plans against fresh state without bypassing validation; scoped Preview reload + is undoable and refuses ambiguous multi-destination layers. + ### v0.9.62 - A rejected or failed layer send now refreshes device state read-only and keeps diff --git a/docs/canvas-pilot/guide/device.md b/docs/canvas-pilot/guide/device.md index 01f7288..c803673 100644 --- a/docs/canvas-pilot/guide/device.md +++ b/docs/canvas-pilot/guide/device.md @@ -206,3 +206,22 @@ linked units and other Aquilon models remain unverified. See the [adapter contract](../../interfaces/device-adapters.md) for tested scope and limits. Shared device input drafts are session-only; [projects omit them](advanced-exchange.md#saved-input-corrections). Opening a file never changes an input. + +### When device state changes before sending + +**Device state has changed** lists the changed fields before any screen is sent: + +- **Cancel send** keeps your local edits and sends nothing. +- **Send my layout anyway** rebuilds the original mapped send against the refreshed + device state. It overwrites the mapped layout fields, not source routing. Range, + input compatibility and fresh-state checks still apply. A changed device family + or Preview/Program bank requires checking the mapping again instead. +- **Discard local changes and reload** uses a warning color because it replaces + the mapped local layers’ geometry, crop, fit and masks with the device Preview + snapshot shown in the dialog. Other layers stay unchanged; Undo restores your + edits. This action is unavailable for Program or ambiguous multiple destinations + belonging to the same local layer. + +If the device changes again before an explicit overwrite, sending can be stopped +again. Changes or failures occurring during a send remain reported as partial-send +errors, with the completed-screen count; they never trigger an automatic retry. diff --git a/docs/interfaces/midra-device-send.md b/docs/interfaces/midra-device-send.md index 954f36b..62dc07c 100644 --- a/docs/interfaces/midra-device-send.md +++ b/docs/interfaces/midra-device-send.md @@ -353,3 +353,17 @@ the send error and any completed-screen count. No send is retried automatically; check the refreshed state before explicitly sending again. If refreshing fails, the snapshot is cleared and both errors are reported. This also applies to partial send failures: refreshing does not imply rollback or successful completion. + +### Explicit stale-state conflict resolution + +Before a multi-screen send, a fresh read validates every plan before the first +write. A preflight conflict returns the fresh snapshot and opens a local dialog +with deduplicated changed fields (up to 100, with an additional-field count). +Overwrite recomputes plans from the current editor layout using the original +screen/layer mappings, sizing and target. It rejects changed physical bank roles +or device family and retains all per-send validation; there is no bypass flag. +The destructive reload imports only the mapped Preview layer geometry/framing, +fit and masks, atomically and through normal undo. Program and ambiguous fan-out +reloads are disabled. The dialog and attempted mapping are transient UI state; +project and exchange schemas are unchanged. Partial-send errors do not offer this +no-writes conflict dialog.