From dbffa7b901b3c9b358b54a54bb2b19fd0c4ce648 Mon Sep 17 00:00:00 2001 From: wstock <1394687+wstock@users.noreply.github.com> Date: Thu, 8 Oct 2026 11:22:52 +0000 Subject: [PATCH] channel.merge: reject a repeated source index instead of closing an unrelated document Fixes #934. `channel.merge` closes its sources by current index, sorted and in reverse, so `documents: [0, 0, 1]` passed validation and then ran `close(1)`, `close(0)`, `close(0)`; the last call removed whatever document had shifted into index 0, i.e. one that was never named, with its unsaved edits and history. Repeated indices are now rejected as bad params before anything is built or closed, and the first-source lookup no longer indexes the document list directly. Co-Authored-By: Claude Fable 5.1 --- crates/engine/src/channel_cmds.rs | 10 +++++++++- crates/engine/src/channel_cmds/tests.rs | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs index 935ef794f7..6892f31fde 100644 --- a/crates/engine/src/channel_cmds.rs +++ b/crates/engine/src/channel_cmds.rs @@ -1313,7 +1313,15 @@ fn merge(s: &mut Session, p: &Value) -> Result { if srcs.len() < mode.color_channels() { return Err(bad(cmd, format!("{mode:?} needs {} grayscale documents, got {}", mode.color_channels(), srcs.len()))); } - let first = s.documents()[srcs[0]].doc.clone(); + // Each source closes by its *current* index, so a repeated index would shift the later + // removals onto a document that was never named (#934). Refuse it rather than guess. + let mut seen = std::collections::HashSet::new(); + if let Some(dup) = srcs.iter().find(|i| !seen.insert(**i)) { + return Err(bad(cmd, format!("document {dup} is listed more than once"))); + } + let Some(first) = srcs.first().and_then(|&i| s.documents().get(i)).map(|d| d.doc.clone()) else { + return Err(bad(cmd, "bad document index")); + }; let mut planes = Vec::new(); for &i in &srcs { let d = &s.documents()[i].doc; diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rs index 9d571862aa..87dca83c94 100644 --- a/crates/engine/src/channel_cmds/tests.rs +++ b/crates/engine/src/channel_cmds/tests.rs @@ -450,6 +450,29 @@ fn split_and_merge_channels() { assert!(s.execute("channel.merge", json!({"mode": "cmyk"})).is_err()); } +#[test] +fn merge_rejects_a_repeated_source_and_keeps_unrelated_documents() { + // #934: `documents: [0, 0, 1]` passed validation, then the close loop ran `close(1)`, + // `close(0)`, `close(0)` and the last call removed the unrelated RGB document at index 2. + let mut s = session(); + s.execute("channel.split", json!({})).unwrap(); + assert_eq!(s.documents().len(), 3); + s.execute("file.new", json!({"width": 40, "height": 20})).unwrap(); + s.execute("edit.fill", json!({"color": "#00ff00"})).unwrap(); + let keep = s.documents()[3].doc.id; + let before: Vec<_> = s.documents().iter().map(|d| d.doc.id).collect(); + let err = s.execute("channel.merge", json!({"mode": "rgb", "documents": [0, 0, 1]})).unwrap_err(); + assert!(err.to_string().contains("more than once"), "{err}"); + let after: Vec<_> = s.documents().iter().map(|d| d.doc.id).collect(); + assert_eq!(after, before, "a rejected merge closes nothing"); + assert!(s.documents().iter().any(|d| d.doc.id == keep)); + assert!(s.set_active(3) && s.undo(), "the unrelated document keeps its history"); + // Distinct indices still merge and close exactly the named sources. + s.execute("channel.merge", json!({"mode": "rgb", "documents": [0, 1, 2]})).unwrap(); + assert_eq!(s.documents().len(), 2); + assert!(s.documents().iter().any(|d| d.doc.id == keep)); +} + #[test] fn duplicate_to_other_document() { let mut s = session();