From ae0da189a2bd1365eba9ea19b7b0f4f809f6af00 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 3 Nov 2026 17:26:39 +0100 Subject: [PATCH] Address CodeRabbit review: a cancel after an action's saved snapshot keeps that file When an action saves and then closes or switches away, Batch writes the saved snapshot inside the action closure. A cancel landing after that write made process_one return Cancelled, so the input was reported as skipped although its output exists. The snapshot write is now final: the cancel stops only the inputs after it. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/file_cmds/batch_run.rs | 23 +++++++++++--------- crates/engine/src/cmd/file_cmds/tests.rs | 22 +++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/file_cmds/batch_run.rs b/crates/engine/src/cmd/file_cmds/batch_run.rs index 5a93e670a87..1a981298c03 100644 --- a/crates/engine/src/cmd/file_cmds/batch_run.rs +++ b/crates/engine/src/cmd/file_cmds/batch_run.rs @@ -221,24 +221,27 @@ fn process_one(raw_ppi: f64, path: &str, out: &str, save: SaveOpts, options: Pro ctx.publish_batch_preview(path, scratch.active().map(|state| state.doc.clone())); } let saved_by_action = f(&mut scratch, out)?; + if saved_by_action { + // The action's saved snapshot is already written: a cancel or Esc arriving now stops the + // inputs after this one, not this result. + return Ok(()); + } if let Some(ctx) = options.ctx { ctx.check()?; if ctx.batch_interrupt_requested() { return Err(EngineError::Cancelled); } } - if !saved_by_action { - let d = scratch.active().ok_or(EngineError::NoDocument)?; - if let Some(format) = options.format { - let ext = if format == "same" { same_format(path) } else { format.trim_start_matches('.').to_ascii_lowercase() }; - if let Some(ctx) = options.ctx { - save_batch_doc(&d.doc, out, &ext, save, ctx)?; - } else { - save_doc_as(&d.doc, out, &ext, save)?; - } + let d = scratch.active().ok_or(EngineError::NoDocument)?; + if let Some(format) = options.format { + let ext = if format == "same" { same_format(path) } else { format.trim_start_matches('.').to_ascii_lowercase() }; + if let Some(ctx) = options.ctx { + save_batch_doc(&d.doc, out, &ext, save, ctx)?; } else { - save_doc(&d.doc, out, save)?; + save_doc_as(&d.doc, out, &ext, save)?; } + } else { + save_doc(&d.doc, out, save)?; } Ok(()) } diff --git a/crates/engine/src/cmd/file_cmds/tests.rs b/crates/engine/src/cmd/file_cmds/tests.rs index 1334cb77a24..804e6a01cac 100644 --- a/crates/engine/src/cmd/file_cmds/tests.rs +++ b/crates/engine/src/cmd/file_cmds/tests.rs @@ -89,6 +89,28 @@ fn batch_cancelled_during_an_action_does_not_save_that_input() { assert!(!std::path::Path::new(&out).exists()); } +/// An action that saved and then closed its document has its saved snapshot written by the time +/// it returns: a cancel landing then counts that file as written, not skipped. +#[test] +fn batch_cancel_after_a_saved_snapshot_is_written_keeps_that_file() { + let dir = tmp("batch-cancel-after-snapshot"); + let first = png(&dir, "first.png", 8, 6, "#ff0000"); + let second = png(&dir, "second.png", 8, 6, "#00ff00"); + let first_out = join(&dir, "out/first.png"); + let ctx = crate::jobs::JobCtx::new(); + let policy = super::batch_run::BatchPolicy { stop_on_error: false, error_log: None }; + let outputs = vec![(first, first_out.clone()), (second.clone(), join(&dir, "out/second.png"))]; + let result = super::batch_run::process_batch_outputs(72.0, outputs, "png", SaveOpts::default(), &policy, &ctx, &|s, out| { + save_doc(&s.active().unwrap().doc, out, None).unwrap(); + ctx.cancel(); + Ok(true) + }) + .unwrap(); + assert_eq!(result["files"], json!([first_out]), "{result}"); + assert_eq!(result["skipped"], json!([second]), "{result}"); + assert_eq!(result["cancelled"], json!(true), "{result}"); +} + /// A cancel that lands while an input fails on its own keeps that failure and its reason; only /// the inputs after it are skipped. #[test]