diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs index 5ea48f9431..6c63a1d9c5 100644 --- a/crates/engine/src/jobs.rs +++ b/crates/engine/src/jobs.rs @@ -751,18 +751,25 @@ impl Session { }; self.coalesce_request = coalesce.clone(); self.color_restrict = *color_restrict; + // Apply-time hooks can dispatch commands too. Keep the same single-step recording + // contract as synchronous dispatch without truncating work done while the worker ran. + let nested_from = self.journal.len(); + let recording = if *journal && crate::actions_cmds::replayable(command) { self.actions.recording.take() } else { None }; let r = match apply { Some(f) => std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self, payload))) .unwrap_or_else(|_| Err(EngineError::Other(format!("`{label}` failed with an internal error (logged); the document is unchanged")))), None => Err(EngineError::Other("internal error: job applied twice".into())), }; + if self.actions.recording.is_none() { + self.actions.recording = recording; + } self.coalesce_request = None; self.color_restrict = None; // Finish successful command bookkeeping on the document the job edited. // Restoring the viewed document first would attribute Fade and slice updates // to that document instead, when the user switched tabs mid-job. if r.is_ok() { - self.after_command(command, params.clone(), *journal, None); + self.after_command(command, params.clone(), *journal, Some(nested_from)); } // Keep the user's active document unless the job opened a new one. // This also restores the selection when the apply step returned an error. diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index 061e7538d4..c58ae86e45 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -254,7 +254,10 @@ fn spool_transport_streams_pdf_and_propagates_refusal() { assert!(s.execute("jobs.cancel", json!({"job": job.0})).unwrap_err().to_string().contains("system print queue")); s.execute("file.new", json!({"width": 40, "height": 10})).unwrap(); let viewed = s.active().unwrap().doc.id; + let journal_before_completion = s.journal.len(); let out = s.wait_job(job).unwrap(); + assert_eq!(s.journal.len(), journal_before_completion + 1, "print hooks must not become duplicate recorded action steps"); + assert_eq!(s.journal.last().unwrap().0, "file.print"); assert_eq!(s.active().unwrap().doc.id, viewed, "completion keeps the user's tab"); assert_eq!(s.active().unwrap().doc.size.width, 40, "print hook must not act on the newly viewed document"); let printed = &s.documents().iter().find(|st| st.doc.id == source).unwrap().doc;