diff --git a/crates/engine/src/actions_cmds.rs b/crates/engine/src/actions_cmds.rs index 8c3a277b7a..0d1cb29cdc 100644 --- a/crates/engine/src/actions_cmds.rs +++ b/crates/engine/src/actions_cmds.rs @@ -98,6 +98,10 @@ pub struct ActionState { /// Bumped whenever `list` changes, so the preset store can skip unchanged writes. #[serde(skip)] pub rev: u64, + /// Bumped by each `actions.record`, so a background job knows whether the recording it was + /// started in is still the one running when its result lands. + #[serde(skip)] + pub take: u64, } impl ActionState { @@ -298,6 +302,7 @@ fn record(s: &mut Session, p: &Value) -> Result { s.actions.list.len() - 1 }; s.actions.recording = Some((s.journal.len(), idx)); + s.actions.take = s.actions.take.wrapping_add(1); let name = s.actions.list.get(idx).map(|a| a.name.clone()).unwrap_or_default(); Ok(json!({"action": name, "index": idx})) } diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs index 0ee0573170..7aa3fffd46 100644 --- a/crates/engine/src/jobs.rs +++ b/crates/engine/src/jobs.rs @@ -212,6 +212,10 @@ struct Running { label: String, params: Value, journal: bool, + /// The recording (`ActionState::take`) running when the job started, if any (set by + /// `dispatch`, once recording resumes). Only that recording gets the job's step: one started + /// later must not pick up an edit made before it. + recorded_by: Option, doc: Option, /// The document as the job saw it: applying fails if it was edited meanwhile. snapshot: Option>, @@ -400,6 +404,7 @@ pub fn run( label: label.to_string(), params: Value::Null, journal: true, + recorded_by: None, doc, snapshot, ctx, @@ -780,6 +785,7 @@ impl Session { job.command = id.to_string(); job.params = params; job.journal = spec.journal; + job.recorded_by = self.actions.recording.map(|_| self.actions.take); let jid = job.id; self.jobs.running.push(job); Ok(Started::Job(jid)) @@ -868,7 +874,13 @@ impl Session { } fn apply_job(&mut self, job: &Running, apply: Option, payload: Payload) -> Result { - let Running { label, doc, snapshot, coalesce, color_restrict, layers, command, params, journal, .. } = job; + let Running { label, doc, snapshot, coalesce, color_restrict, layers, command, params, journal, recorded_by, .. } = job; + // A recording started after the job (#7289) keeps what it has so far, and the job's step + // goes into the journal past its start, so that recording doesn't get it. + let foreign = self.actions.recording.is_some() && *recorded_by != Some(self.actions.take); + if foreign { + crate::actions_cmds::flush_recording(self)?; + } let prev_active = self.active; let target = match doc { Some(d) => { @@ -917,6 +929,9 @@ impl Session { // to that document instead, when the user switched tabs mid-job. if r.is_ok() { self.after_command(command, params.clone(), *journal, Some(nested_from)); + if foreign && let Some((start, _)) = self.actions.recording.as_mut() { + *start = self.journal.len(); + } } // Give the user back the layers they selected while the job ran, unless the job chose a // selection itself (a new layer, say) or one of their layers is gone. diff --git a/crates/engine/src/jobs_tests.rs b/crates/engine/src/jobs_tests.rs index 2bcdda76fc..f7e041ed92 100644 --- a/crates/engine/src/jobs_tests.rs +++ b/crates/engine/src/jobs_tests.rs @@ -605,3 +605,45 @@ fn an_inline_job_ctx_lets_another_thread_cancel_inline_work() { s.set_inline_job_ctx(None); s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap(); } + +/// The command ids an action holds. +fn action_steps(s: &mut Session, name: &str) -> Vec { + let a = s.execute("actions.get", json!({"action": name})).unwrap(); + a["steps"].as_array().unwrap().iter().map(|step| step[0].as_str().unwrap().to_string()).collect() +} + +/// A job's result is recorded only by the recording it was started in: one started later keeps +/// its own steps around it but doesn't pick up the older edit (#7289). +#[test] +fn a_job_lands_in_the_recording_it_was_started_in_only() { + // Started while recording A, applied after A stopped and B started. + let mut s = session(40, 30); + s.execute("actions.record", json!({"name": "A"})).unwrap(); + let id = job(s.start("filter.blur.gaussianBlur", json!({"radius": 1})).unwrap()); + s.execute("actions.stop", json!({})).unwrap(); + s.execute("actions.record", json!({"name": "B"})).unwrap(); + // B's own steps before and after the result lands stay in B (a new document, so the job's + // document is unchanged and its result applies). + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + s.wait_job(id).unwrap(); + s.execute("image.adjustments.desaturate", json!({})).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + assert!(action_steps(&mut s, "A").is_empty()); + assert_eq!(action_steps(&mut s, "B"), ["file.new", "image.adjustments.desaturate"]); + + // Started before any recording, applied while B records. + let mut s = session(40, 30); + let id = job(s.start("filter.blur.gaussianBlur", json!({"radius": 1})).unwrap()); + s.execute("actions.record", json!({"name": "B"})).unwrap(); + s.wait_job(id).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + assert!(action_steps(&mut s, "B").is_empty()); + + // Started and applied while A records: A has it, as before. + let mut s = session(40, 30); + s.execute("actions.record", json!({"name": "A"})).unwrap(); + let id = job(s.start("filter.blur.gaussianBlur", json!({"radius": 1})).unwrap()); + s.wait_job(id).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + assert_eq!(action_steps(&mut s, "A"), ["filter.blur.gaussianBlur"]); +}