Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Prefixed-line output for concurrent foreground commands (#2145)
Maximilian Roos101c34167319
diff --git a/src/commands/command_executor.rs b/src/commands/command_executor.rsindex fcf33911c..7bfd66687 100644--- a/src/commands/command_executor.rs+++ b/src/commands/command_executor.rs@@ -11,9 +11,9 @@ use worktrunk::git::{Repository, WorktrunkError};use worktrunk::path::{format_path_for_display, to_posix_path};use worktrunk::styling::{eprintln, error_message, format_bash_with_gutter, progress_message};-use super::force_serial_concurrent;use super::format_command_label;use super::hook_filter::HookSource;+use crate::output::concurrent::{ConcurrentCommand, run_concurrent_commands};use crate::output::{DirectivePassthrough, execute_shell_command};#[derive(Debug)]@@ -308,7 +308,7 @@ pub fn execute_pipeline_foreground()?;}PreparedStep::Concurrent(cmds) => {- if !concurrent || force_serial_concurrent() {+ if !concurrent {for cmd in cmds {run_one_command(cmd,@@ -320,29 +320,14 @@ pub fn execute_pipeline_foreground()?;}} else {- std::thread::scope(|s| {- let origin = &fg_step.origin;- let handles: Vec<_> = cmds- .iter()- .map(|cmd| {- s.spawn(|| {- run_one_command(- cmd,- origin,- repo,- wt_path,- directives,- failure_strategy,- )- })- })- .collect();- wait_first_error(- handles- .into_iter()- .map(|h| h.join().expect("command thread panicked")),- )- })?;+ run_concurrent_group(+ cmds,+ &fg_step.origin,+ repo,+ wt_path,+ directives,+ failure_strategy,+ )?;}}}@@ -350,6 +335,93 @@ pub fn execute_pipeline_foreground(Ok(())}+/// Run every command in a concurrent group via the prefixed-line executor.+///+/// Announces each command up front (origin-aware — hooks render per-command+/// announcements, aliases only announce the outer group), expands all+/// templates sequentially (template expansion reads git config; racing on+/// reads would produce inconsistent state), then dispatches to+/// `run_concurrent_commands` which streams each child's output prefixed by+/// its label and waits for all to complete before folding outcomes.+fn run_concurrent_group(+ cmds: &[PreparedCommand],+ origin: &CommandOrigin,+ repo: &Repository,+ wt_path: &Path,+ directives: &DirectivePassthrough,+ failure_strategy: FailureStrategy,+) -> anyhow::Result<()> {+ for cmd in cmds {+ announce_command(cmd, origin);+ }++ // The concurrent path requires `lazy_template` so expansion sees the+ // fresh git-config state at execution time (prior pipeline steps may+ // have set `vars.*`). Alias prep sets this unconditionally; hook prep+ // will when concurrent hooks ship.+ let mut expanded: Vec<String> = Vec::with_capacity(cmds.len());+ for cmd in cmds {+ let template = cmd+ .lazy_template+ .as_ref()+ .expect("concurrent group commands must carry a lazy_template");+ let label = expansion_label(cmd, origin);+ let context: HashMap<String, String> = serde_json::from_str(&cmd.context_json)+ .context("failed to deserialize context_json")?;+ expanded.push(expand_shell_template(template, &context, repo, &label)?);+ }++ let log_labels: Vec<Option<String>> = cmds+ .iter()+ .map(|cmd| command_log_label(cmd, origin))+ .collect();++ // Alias tables always produce named commands (TOML keys become `name`),+ // and the concurrent path is alias-only today — so `cmd.name` is always+ // `Some` here. When foreground concurrent hooks eventually ship, this+ // will need an origin-aware fallback; for now, require the name.+ let labels: Vec<&str> = cmds+ .iter()+ .map(|cmd| {+ cmd.name+ .as_deref()+ .expect("concurrent group commands are always named")+ })+ .collect();++ let specs: Vec<ConcurrentCommand<'_>> = cmds+ .iter()+ .enumerate()+ .map(|(i, cmd)| ConcurrentCommand {+ label: labels[i],+ expanded: &expanded[i],+ working_dir: wt_path,+ context_json: &cmd.context_json,+ log_label: log_labels[i].as_deref(),+ directives,+ })+ .collect();++ let outcomes = run_concurrent_commands(&specs)?;++ let mut first_failure: Option<anyhow::Error> = None;+ for (outcome, cmd) in outcomes.into_iter().zip(cmds) {+ let Err(err) = outcome else { continue };+ match handle_command_error(err, cmd, origin, failure_strategy) {+ Ok(()) => {}+ Err(e) => {+ if first_failure.is_none() {+ first_failure = Some(e);+ }+ }+ }+ }+ match first_failure {+ Some(err) => Err(err),+ None => Ok(()),+ }+}+/// Execute a single prepared command: announce, expand, run, handle errors.fn run_one_command(cmd: &PreparedCommand,@@ -496,10 +568,6 @@ fn expand_commands(source: HookSource,lazy_enabled: bool,) -> anyhow::Result<Vec<(Command, String, Option<String>)>> {- if commands.is_empty() {- return Ok(Vec::new());- }-let mut base_context = build_hook_context(ctx, extra_vars)?;// hook_type is always available as a template variable and in JSON context@@ -563,9 +631,6 @@ pub fn prepare_steps(source: HookSource,) -> anyhow::Result<Vec<PreparedStep>> {let steps = command_config.steps();- if steps.is_empty() {- return Ok(Vec::new());- }// Collect step sizes so we can re-partition after a single expand_commands call.// This avoids calling build_hook_context (which spawns git subprocesses) per step.diff --git a/src/output/concurrent.rs b/src/output/concurrent.rsnew file mode 100644index 000000000..50e574349--- /dev/null+++ b/src/output/concurrent.rs@@ -0,0 +1,539 @@+//! Concurrent execution of shell commands with prefixed-line output.+//!+//! Foreground concurrent groups (from `HookStep::Concurrent`) spawn every+//! command at once and combine their output into a single terminal stream,+//! each line prefixed with its command's colored label. Prefixed lines keep+//! full scrollback intact for debugging failures and work identically under+//! a TTY or pipe (CI logs).+//!+//! TODO: upgrade to a docker-compose-v2-style tailing display — each command+//! gets a fixed terminal region showing its last N lines, overwritten in+//! place as output arrives. Better signal-to-noise for long streams like+//! `cargo test`, but substantially more implementation (cursor tracking,+//! resize handling, TTY/non-TTY fallback, scrollback replay on failure).+//! Prefixed lines suffice until live sections are demonstrated to pay for+//! themselves.+//!+//! ## Execution model+//!+//! For each command:+//! 1. Spawn a shell child with stdout+stderr piped and (on Unix) its own+//! process group so SIGINT/SIGTERM can be delivered to the whole tree.+//! 2. Pipe `context_json` to stdin if provided, then close.+//! 3. Launch two reader threads that read lines and send labeled lines on+//! a shared channel. A single consumer writes to stderr — one writer+//! preserves line atomicity so readers never mix bytes mid-line.+//!+//! The main thread drains the channel. A lightweight ticker thread polls+//! `signal_hook::Signals` for SIGINT/SIGTERM and forwards with escalation+//! to every live child's process group.+//!+//! All children always run to completion. Per-child exit status is returned+//! for the caller to fold into a failure, matching alias `thread::scope` and+//! pipeline `run_concurrent_group` semantics.++use std::io::{BufRead, BufReader, Read, Write};+use std::path::Path;+use std::process::{Child, Stdio};+use std::sync::mpsc::{self, Sender};+use std::thread;+use std::time::Instant;++use anyhow::Context;++use worktrunk::command_log::log_command;+use worktrunk::git::WorktrunkError;+use worktrunk::shell_exec::{+ DIRECTIVE_CD_FILE_ENV_VAR, DIRECTIVE_FILE_ENV_VAR, ShellConfig, scrub_directive_env_vars,+};+use worktrunk::styling::stderr;++use super::handlers::DirectivePassthrough;++/// One command in a concurrent group.+pub struct ConcurrentCommand<'a> {+ /// Short label used as the line prefix (e.g., the command name).+ pub label: &'a str,+ /// Fully-expanded shell command string.+ pub expanded: &'a str,+ /// Child's working directory.+ pub working_dir: &'a Path,+ /// JSON blob written to the child's stdin and closed. Callers that have+ /// no context to pass should supply `"{}"`.+ pub context_json: &'a str,+ /// Optional label for `commands.jsonl` tracing.+ pub log_label: Option<&'a str>,+ /// Directive file env vars to pass through to the child. See+ /// `DirectivePassthrough` for the trust model (CD passthrough, EXEC scrub).+ pub directives: &'a DirectivePassthrough,+}++/// Run every command concurrently and return each per-child result in input+/// order. `Err(WorktrunkError::ChildProcessExited { .. })` signals a non-zero+/// exit; other errors come from spawn/IO failures.+///+/// When the `WORKTRUNK_TEST_SERIAL_CONCURRENT` env var is set, commands run+/// sequentially in input order — same prefix-line output path, just one child+/// at a time. Tests use this to pin deterministic interleaving for snapshots.+pub fn run_concurrent_commands(+ cmds: &[ConcurrentCommand<'_>],+) -> anyhow::Result<Vec<anyhow::Result<()>>> {+ let prefix_width = cmds.iter().map(|c| c.label.len()).max().unwrap_or(0);+ let shell = ShellConfig::get()?;++ if std::env::var_os("WORKTRUNK_TEST_SERIAL_CONCURRENT").is_some() {+ return Ok(run_serial_with_prefix(shell, cmds, prefix_width));+ }++ // Install the SIGINT/SIGTERM latch BEFORE spawning any children so a+ // signal that arrives mid-spawn is captured rather than default-killing+ // wt (which would orphan already-spawned children, since each runs in+ // its own process group and wouldn't see the tty's Ctrl-C broadcast).+ #[cfg(unix)]+ let signals = {+ use signal_hook::consts::{SIGINT, SIGTERM};+ signal_hook::iterator::Signals::new([SIGINT, SIGTERM])?+ };++ // Spawn each child and record its start time for commands.jsonl. If any+ // spawn fails partway, kill and reap every child we already spawned —+ // otherwise they'd outlive wt as unreaped orphans with nobody draining+ // their pipes (and `Child::drop` does not kill the process on Unix).+ let mut children: Vec<SpawnedChild> = Vec::with_capacity(cmds.len());+ for (i, cmd) in cmds.iter().enumerate() {+ match spawn_child(shell, i, cmd) {+ Ok(spawned) => children.push(spawned),+ Err(e) => {+ for mut prior in children {+ let _ = prior.child.kill();+ let _ = prior.child.wait();+ }+ return Err(e);+ }+ }+ }++ // Print one prefixed line at a time from a single writer. Each reader+ // sends complete lines through `line_tx`; the consumer drains them.+ let (line_tx, line_rx) = mpsc::channel::<LabeledLine>();++ let mut readers: Vec<thread::JoinHandle<()>> = Vec::new();+ for (i, spawned) in children.iter_mut().enumerate() {+ let label = cmds[i].label.to_string();+ if let Some(stdout) = spawned.child.stdout.take() {+ readers.push(spawn_reader(i, label.clone(), stdout, line_tx.clone()));+ }+ if let Some(stderr) = spawned.child.stderr.take() {+ readers.push(spawn_reader(i, label, stderr, line_tx.clone()));+ }+ }+ // Drop the original sender so the channel closes once every reader exits.+ drop(line_tx);++ // Start the forwarder thread now that we have the pgid list. The+ // `signals` latch was installed up-front, so any signal received during+ // the spawn loop was queued and will be delivered on the thread's first+ // poll.+ #[cfg(unix)]+ let signal_thread = spawn_signal_forwarder(+ signals,+ children+ .iter()+ .map(|c| c.child.id() as i32)+ .collect::<Vec<_>>(),+ );++ // Consume labeled lines until every reader drops its sender.+ {+ let mut stderr = stderr().lock();+ for labeled in line_rx {+ let prefix = render_prefix(labeled.index, &labeled.label, prefix_width);+ writeln!(stderr, "{}{}", prefix, labeled.line).ok();+ }+ }++ for r in readers {+ let _ = r.join();+ }++ // After the last output line prints, wait on each child.+ let mut outcomes = Vec::with_capacity(children.len());+ for (spawned, cmd) in children.into_iter().zip(cmds) {+ outcomes.push(collect_outcome(spawned, cmd));+ }++ #[cfg(unix)]+ {+ // All children have exited — shut the signal forwarder down.+ signal_thread.stop();+ }++ Ok(outcomes)+}++/// Serial fallback for `WORKTRUNK_TEST_SERIAL_CONCURRENT`.+///+/// Runs each command to completion before starting the next — same prefix-line+/// output, deterministic interleaving. Outcomes come back in input order.+fn run_serial_with_prefix(+ shell: &ShellConfig,+ cmds: &[ConcurrentCommand<'_>],+ prefix_width: usize,+) -> Vec<anyhow::Result<()>> {+ let mut results = Vec::with_capacity(cmds.len());+ for (i, cmd) in cmds.iter().enumerate() {+ let spawned = match spawn_child(shell, i, cmd) {+ Ok(s) => s,+ Err(e) => {+ results.push(Err(e));+ continue;+ }+ };+ let result = drain_and_wait_single(spawned, cmd, i, prefix_width);+ results.push(result);+ }+ results+}++/// Spawn readers for one child, drain to stderr with prefixes, then wait.+fn drain_and_wait_single(+ mut spawned: SpawnedChild,+ cmd: &ConcurrentCommand<'_>,+ index: usize,+ prefix_width: usize,+) -> anyhow::Result<()> {+ let (tx, rx) = mpsc::channel::<LabeledLine>();+ let mut readers: Vec<thread::JoinHandle<()>> = Vec::new();+ let label = cmd.label.to_string();+ if let Some(stdout) = spawned.child.stdout.take() {+ readers.push(spawn_reader(index, label.clone(), stdout, tx.clone()));+ }+ if let Some(stderr) = spawned.child.stderr.take() {+ readers.push(spawn_reader(index, label, stderr, tx.clone()));+ }+ drop(tx);+ {+ let mut out = stderr().lock();+ for labeled in rx {+ let prefix = render_prefix(labeled.index, &labeled.label, prefix_width);+ writeln!(out, "{prefix}{}", labeled.line).ok();+ }+ }+ for r in readers {+ let _ = r.join();+ }+ collect_outcome(spawned, cmd)+}++struct SpawnedChild {+ child: Child,+ cmd_str: String,+ log_label: Option<String>,+ started_at: Instant,+}++fn spawn_child(+ shell: &ShellConfig,+ index: usize,+ cmd: &ConcurrentCommand<'_>,+) -> anyhow::Result<SpawnedChild> {+ let mut command = shell.command(cmd.expanded);+ command+ .current_dir(cmd.working_dir)+ .stdin(Stdio::piped())+ .stdout(Stdio::piped())+ .stderr(Stdio::piped());++ // Scrub all directive env vars, then re-add the passthroughs.+ scrub_directive_env_vars(&mut command);+ if let Some(path) = &cmd.directives.cd_file {+ command.env(DIRECTIVE_CD_FILE_ENV_VAR, path);+ }+ if let Some(path) = &cmd.directives.legacy_file {+ command.env(DIRECTIVE_FILE_ENV_VAR, path);+ }++ #[cfg(unix)]+ {+ use std::os::unix::process::CommandExt;+ command.process_group(0);+ }++ log::debug!(+ "$ {} (concurrent #{index}, shell: {})",+ cmd.expanded,+ shell.name+ );++ let mut child = command+ .spawn()+ .with_context(|| format!("failed to spawn concurrent command '{}'", cmd.label))?;++ if let Some(mut stdin) = child.stdin.take() {+ // Ignore BrokenPipe — child may exit or close stdin early.+ let _ = stdin.write_all(cmd.context_json.as_bytes());+ }++ Ok(SpawnedChild {+ child,+ cmd_str: cmd.expanded.to_string(),+ log_label: cmd.log_label.map(str::to_string),+ started_at: Instant::now(),+ })+}++fn spawn_reader<R: Read + Send + 'static>(+ index: usize,+ label: String,+ stream: R,+ tx: Sender<LabeledLine>,+) -> thread::JoinHandle<()> {+ thread::spawn(move || {+ // Read by bytes, not by `BufRead::lines()` — `lines()` returns+ // `InvalidData` on non-UTF-8 bytes and terminates the iterator, which+ // would leave the child's pipe un-drained and eventually hang+ // `child.wait()` once the pipe buffer fills. Real-world triggers+ // include `git diff` of binary files, tools that honor non-UTF-8+ // locales, and any raw-byte output. Lossy-decoding preserves the+ // stream and keeps the child unblocked.+ let mut reader = BufReader::new(stream);+ let mut buf = Vec::with_capacity(256);+ loop {+ buf.clear();+ match reader.read_until(b'\n', &mut buf) {+ Ok(0) => return,+ Ok(_) => {+ // Strip the trailing newline (and optional \r) before+ // forwarding — the writer re-adds a newline per line.+ if buf.last() == Some(&b'\n') {+ buf.pop();+ if buf.last() == Some(&b'\r') {+ buf.pop();+ }+ }+ let line = String::from_utf8_lossy(&buf).into_owned();+ if tx+ .send(LabeledLine {+ index,+ label: label.clone(),+ line,+ })+ .is_err()+ {+ return; // consumer dropped+ }+ }+ Err(_) => return, // IO error on the pipe — give up on this stream+ }+ }+ })+}++fn collect_outcome(spawned: SpawnedChild, cmd: &ConcurrentCommand<'_>) -> anyhow::Result<()> {+ let SpawnedChild {+ mut child,+ cmd_str,+ log_label,+ started_at,+ } = spawned;++ let status = child+ .wait()+ .with_context(|| format!("failed to wait for concurrent command '{}'", cmd.label))?;++ let duration = started_at.elapsed();+ let exit_code = status.code();++ #[cfg(unix)]+ let signal = std::os::unix::process::ExitStatusExt::signal(&status);+ #[cfg(not(unix))]+ let signal: Option<i32> = None;++ let normalized_code = exit_code.or_else(|| signal.map(|s| 128 + s));++ if let Some(label) = log_label {+ log_command(&label, &cmd_str, normalized_code, Some(duration));+ }++ if status.success() {+ Ok(())+ } else if let Some(sig) = signal {+ Err(WorktrunkError::ChildProcessExited {+ code: 128 + sig,+ message: format!("terminated by signal {sig}"),+ }+ .into())+ } else {+ let code = exit_code.unwrap_or(1);+ Err(WorktrunkError::ChildProcessExited {+ code,+ message: format!("exit status: {code}"),+ }+ .into())+ }+}++struct LabeledLine {+ index: usize,+ label: String,+ line: String,+}++fn render_prefix(index: usize, label: &str, width: usize) -> String {+ use anstyle::{AnsiColor, Color, Style};+ let palette = [+ AnsiColor::Cyan,+ AnsiColor::Magenta,+ AnsiColor::Yellow,+ AnsiColor::Green,+ AnsiColor::Blue,+ AnsiColor::BrightCyan,+ AnsiColor::BrightMagenta,+ AnsiColor::BrightYellow,+ ];+ let style = Style::new()+ .fg_color(Some(Color::Ansi(palette[index % palette.len()])))+ .bold();+ format!("{style}{label:<width$}{style:#} │ ")+}++#[cfg(unix)]+struct SignalForwarder {+ stop: std::sync::Arc<std::sync::atomic::AtomicBool>,+ handle: thread::JoinHandle<()>,+}++#[cfg(unix)]+impl SignalForwarder {+ fn stop(self) {+ self.stop.store(true, std::sync::atomic::Ordering::Relaxed);+ let _ = self.handle.join();+ }+}++#[cfg(unix)]+fn spawn_signal_forwarder(+ mut signals: signal_hook::iterator::Signals,+ pgids: Vec<i32>,+) -> SignalForwarder {+ use std::sync::atomic::{AtomicBool, Ordering};++ let stop = std::sync::Arc::new(AtomicBool::new(false));+ let stop_clone = stop.clone();++ let handle = thread::spawn(move || {+ let mut seen_once = false;+ while !stop_clone.load(Ordering::Relaxed) {+ for sig in signals.pending() {+ if !seen_once {+ // First signal: graceful escalation per child+ // (SIGINT → SIGTERM → SIGKILL with grace windows).+ seen_once = true;+ for &pgid in &pgids {+ worktrunk::shell_exec::forward_signal_with_escalation(pgid, sig);+ }+ } else {+ // Subsequent signals: user is impatient — SIGKILL every+ // still-live process group without waiting.+ for &pgid in &pgids {+ let _ = nix::sys::signal::killpg(+ nix::unistd::Pid::from_raw(pgid),+ nix::sys::signal::Signal::SIGKILL,+ );+ }+ }+ }+ thread::sleep(std::time::Duration::from_millis(25));+ }+ });++ SignalForwarder { stop, handle }+}++#[cfg(test)]+mod tests {+ //! Unit tests that exercise the executor's option-bearing code paths which+ //! aren't reachable through the alias integration tests today: every child+ //! currently has `log_label=None` (aliases skip per-child logging) and the+ //! CD/legacy directive env vars are usually unset. Driving these branches+ //! with a direct call proves they behave correctly when a future caller+ //! (concurrent foreground hooks, once their deprecation completes) uses them.+ use super::*;++ fn run_one_with_directives(+ label: &str,+ script: &str,+ log_label: Option<&str>,+ directives: &DirectivePassthrough,+ ) -> Vec<anyhow::Result<()>> {+ let wd = std::env::temp_dir();+ let specs = vec![ConcurrentCommand {+ label,+ expanded: script,+ working_dir: &wd,+ context_json: "{}",+ log_label,+ directives,+ }];+ run_concurrent_commands(&specs).expect("spawn failed")+ }++ /// A command with a `log_label` exercises the `log_command` branch in+ /// `collect_outcome` — only hook-origin children take this path today.+ #[test]+ fn test_log_label_is_recorded() {+ let outcomes = run_one_with_directives(+ "job",+ "true",+ Some("test-label"),+ &DirectivePassthrough::none(),+ );+ assert_eq!(outcomes.len(), 1);+ assert!(outcomes[0].is_ok(), "`true` should exit 0");+ // The logger is a global OnceCell not initialised in tests, so+ // `log_command` is effectively a no-op — what we're testing is that+ // passing a log_label doesn't panic and the command still runs.+ }++ /// `DirectivePassthrough` with both `cd_file` and `legacy_file` set must+ /// propagate both env vars to the child. The child script echoes each env+ /// var; we assert both values are delivered.+ ///+ /// Unix-only: the script uses POSIX `sh` redirect syntax and relies on+ /// native temp paths that don't need escaping. Git Bash on Windows would+ /// need the paths converted to `/c/...` form first.+ #[test]+ #[cfg(unix)]+ fn test_directive_env_vars_passed_through() {+ use tempfile::NamedTempFile;+ let cd = NamedTempFile::new().unwrap();+ let legacy = NamedTempFile::new().unwrap();+ let directives = DirectivePassthrough {+ cd_file: Some(cd.path().to_path_buf()),+ legacy_file: Some(legacy.path().to_path_buf()),+ };+ // Write each env var's value to its matching temp file. If the child+ // didn't receive the env var, the redirect would fail or write an+ // empty file.+ let script = format!(+ "printf CD > {} && printf LEGACY > {}",+ cd.path().display(),+ legacy.path().display(),+ );+ let outcomes = run_one_with_directives("job", &script, None, &directives);+ assert!(outcomes[0].is_ok(), "child should exit 0");+ let cd_contents = std::fs::read_to_string(cd.path()).unwrap();+ let legacy_contents = std::fs::read_to_string(legacy.path()).unwrap();+ assert_eq!(cd_contents, "CD");+ assert_eq!(legacy_contents, "LEGACY");+ }++ /// An empty `cmds` slice must return an empty outcomes vec without+ /// spawning anything or erroring. Integration tests never hit this shape+ /// (parser guarantees non-empty Concurrent groups) so we cover it here.+ #[test]+ fn test_empty_cmds_returns_empty() {+ let outcomes = run_concurrent_commands(&[]).expect("no spawn should happen");+ assert!(outcomes.is_empty());+ }+}diff --git a/src/output/mod.rs b/src/output/mod.rsindex 7b75d6c77..bdfa06a04 100644--- a/src/output/mod.rs+++ b/src/output/mod.rs@@ -33,6 +33,7 @@//! See [`shell_integration`] module for the complete spec of warning messages.pub(crate) mod commit_generation;+pub(crate) mod concurrent;mod global;pub(crate) mod handlers;pub(crate) mod prompt;diff --git a/src/shell_exec.rs b/src/shell_exec.rsindex 0936edbe4..81976e0e6 100644--- a/src/shell_exec.rs+++ b/src/shell_exec.rs@@ -1241,7 +1241,7 @@ fn wait_for_exit(pgid: i32, grace: std::time::Duration) -> bool {}#[cfg(unix)]-fn forward_signal_with_escalation(pgid: i32, sig: i32) {+pub fn forward_signal_with_escalation(pgid: i32, sig: i32) {let pgid = nix::unistd::Pid::from_raw(pgid);let initial_signal = match sig {signal_hook::consts::SIGINT => nix::sys::signal::Signal::SIGINT,diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rsindex 9d4588b89..22fc1bdea 100644--- a/tests/integration_tests/step_alias.rs+++ b/tests/integration_tests/step_alias.rs@@ -657,6 +657,48 @@ test = "echo TEST"assert!(stderr.contains("TEST"), "expected TEST in output: {stderr}");}+/// Concurrent alias commands have their output streamed with a per-command+/// colored prefix label (`{name} │ …`), so multiple children's lines remain+/// attributable even when they interleave.+#[rstest]+fn test_alias_concurrent_prefixes_output(mut repo: TestRepo) {+ repo.write_test_config(+ r#"+[aliases.build]+lint = "echo HELLO_LINT"+test = "echo HELLO_TEST"+"#,+ );+ repo.commit("initial");+ let feature_path = repo.add_worktree("feature");++ let mut cmd = repo.wt_command();+ cmd.args(["step", "build"])+ .current_dir(&feature_path)+ .env("NO_COLOR", "1");+ let output = cmd.output().unwrap();++ assert!(+ output.status.success(),+ "concurrent alias failed: stderr={}",+ String::from_utf8_lossy(&output.stderr),+ );++ let stderr = String::from_utf8_lossy(&output.stderr);+ // Each command must produce a line starting with its prefixed label and+ // separated by the box-drawing `│` that the executor emits. Whitespace+ // between label and `│` varies with padding-to-widest-label.+ for (label, body) in [("lint", "HELLO_LINT"), ("test", "HELLO_TEST")] {+ let has_prefixed_line = stderr+ .lines()+ .any(|l| l.starts_with(label) && l.contains('│') && l.contains(body));+ assert!(+ has_prefixed_line,+ "expected a line starting with '{label}' containing '{body}' and a '│' separator, got:\n{stderr}"+ );+ }+}+/// A failing concurrent step causes the alias to fail. Covers the error/// propagation path in the `HookStep::Concurrent` join loop, complementing/// the happy-path `test_alias_concurrent_steps` above.@@ -680,6 +722,236 @@ fail = "exit 1");}+/// SIGINT sent to `wt step <alias>` while a concurrent group is mid-flight+/// must reach every child's process group and tear them all down — otherwise+/// Ctrl-C on a long-running concurrent alias would leave orphans behind.+///+/// We spawn the alias in its own process group, wait until BOTH children have+/// written their "start" marker (proving they're actually running concurrently),+/// send SIGINT to the group, then verify that the subsequent "done" markers+/// never appear — every child was interrupted.+#[rstest]+#[cfg(unix)]+fn test_alias_concurrent_receives_sigint(repo: TestRepo) {+ use crate::common::wait_for_file_content;+ use nix::sys::signal::{Signal, kill};+ use nix::unistd::Pid;+ use std::os::unix::process::CommandExt;+ use std::process::Stdio;++ repo.write_test_config(+ r#"+[aliases.slow]+one = "sh -c 'echo start-one >> slow_one.log; sleep 30; echo done-one >> slow_one.log'"+two = "sh -c 'echo start-two >> slow_two.log; sleep 30; echo done-two >> slow_two.log'"+"#,+ );+ repo.commit("initial");++ let mut cmd = repo.wt_command();+ cmd.args(["step", "slow"]);+ cmd.current_dir(repo.root_path());+ cmd.stdout(Stdio::null());+ cmd.stderr(Stdio::null());+ cmd.process_group(0); // wt becomes leader of its own process group+ let mut child = cmd.spawn().expect("failed to spawn wt step slow");++ // Wait until BOTH children write their start marker — proves the group+ // is running concurrently before we send the signal.+ let one_log = repo.root_path().join("slow_one.log");+ let two_log = repo.root_path().join("slow_two.log");+ wait_for_file_content(&one_log);+ wait_for_file_content(&two_log);++ // SIGINT the wt process group (wt == leader). The concurrent executor's+ // signal forwarder must propagate it to every child's process group.+ let wt_pgid = Pid::from_raw(child.id() as i32);+ kill(Pid::from_raw(-wt_pgid.as_raw()), Signal::SIGINT)+ .expect("failed to send SIGINT to wt's process group");++ let status = child.wait().expect("failed to wait for wt");++ use std::os::unix::process::ExitStatusExt;+ assert!(+ status.signal() == Some(2) || status.code() == Some(130),+ "wt should exit from SIGINT (signal 2) or with code 130, got: {status:?}"+ );++ // Grace period — the killed children must NOT reach their "done" write.+ std::thread::sleep(std::time::Duration::from_millis(500));+ for log in [&one_log, &two_log] {+ let contents = std::fs::read_to_string(log).unwrap_or_default();+ assert!(+ !contents.contains("done"),+ "sibling child reached 'done' after SIGINT, log: {contents:?}"+ );+ }+}++/// A second SIGINT (user mashing Ctrl-C) must escalate to SIGKILL on every+/// child immediately — otherwise a child that traps SIGINT keeps the group+/// alive for up to N × 400ms of per-pgid escalation, with subsequent+/// presses silently discarded.+#[rstest]+#[cfg(unix)]+fn test_alias_concurrent_second_sigint_kills(repo: TestRepo) {+ use crate::common::wait_for_file_content;+ use nix::sys::signal::{Signal, kill};+ use nix::unistd::Pid;+ use std::os::unix::process::CommandExt;+ use std::process::Stdio;++ // Both children trap SIGINT and sleep; first SIGINT does nothing+ // (graceful escalation to SIGTERM is also trapped), a second SIGINT+ // must SIGKILL the pgids and exit wt promptly.+ repo.write_test_config(+ r#"+[aliases.stubborn]+one = "sh -c 'trap \"\" INT TERM; echo start-one >> stubborn_one.log; sleep 30'"+two = "sh -c 'trap \"\" INT TERM; echo start-two >> stubborn_two.log; sleep 30'"+"#,+ );+ repo.commit("initial");++ let mut cmd = repo.wt_command();+ cmd.args(["step", "stubborn"]);+ cmd.current_dir(repo.root_path());+ cmd.stdout(Stdio::null());+ cmd.stderr(Stdio::null());+ cmd.process_group(0);+ let mut child = cmd.spawn().expect("failed to spawn wt step stubborn");++ wait_for_file_content(&repo.root_path().join("stubborn_one.log"));+ wait_for_file_content(&repo.root_path().join("stubborn_two.log"));++ let wt_pgid = Pid::from_raw(child.id() as i32);+ // First SIGINT — trapped by children; graceful path chews through+ // escalation serially.+ kill(Pid::from_raw(-wt_pgid.as_raw()), Signal::SIGINT).expect("failed to send first SIGINT");++ std::thread::sleep(std::time::Duration::from_millis(100));++ // Second SIGINT — impatient path should SIGKILL the whole tree now.+ kill(Pid::from_raw(-wt_pgid.as_raw()), Signal::SIGINT).expect("failed to send second SIGINT");++ let start = std::time::Instant::now();+ let _status = child.wait().expect("failed to wait for wt");+ let elapsed = start.elapsed();++ // With only graceful escalation (200ms × 2 grace windows × 2 pgids),+ // worst case would be ~800ms. The impatient SIGKILL should be faster+ // still. Give 3s headroom for slow CI without being so loose that a+ // regression (no SIGKILL on 2nd press) would slip through — that+ // regression would leave wt waiting ~60s for the sleeps to finish.+ assert!(+ elapsed < std::time::Duration::from_secs(3),+ "wt took too long to die after 2nd SIGINT; impatient path may not be firing: {elapsed:?}"+ );+}++/// Non-UTF-8 bytes and CRLF line endings in child output must not stall the+/// executor. Earlier code using `BufRead::lines()` returned `InvalidData` on+/// the first invalid byte and terminated the iterator, leaving the child's+/// pipe un-drained and `child.wait()` hanging forever. Also exercises the+/// `\r\n` strip path so trailing carriage returns don't render visibly.+#[rstest]+fn test_alias_concurrent_handles_non_utf8(repo: TestRepo) {+ // `printf` emits a raw 0xff byte (invalid as a lone UTF-8 sequence), a+ // CRLF-terminated line, then `yes` floods 50_000 more valid UTF-8 lines.+ // If the reader stopped at the bad byte, the pipe would fill and the+ // child would block — we'd time out.+ repo.write_test_config(+ r#"+[aliases.noisy]+mixed = "sh -c 'printf \"BEFORE\\n\\xff\\nCRLF-LINE\\r\\nAFTER\\n\"; yes PAYLOAD | head -n 50000'"+"#,+ );+ repo.commit("initial");++ let mut cmd = repo.wt_command();+ cmd.args(["step", "noisy"]);+ let output = cmd.output().expect("wt step noisy failed to spawn");++ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ output.status.success(),+ "alias should succeed despite non-UTF-8 byte, got: {status:?}\nlast 500 bytes of stderr: {tail}",+ status = output.status,+ tail = &stderr[stderr.len().saturating_sub(500)..],+ );++ // All clean lines plus all 50_000 flood lines must land — proves the+ // reader kept going past the invalid byte.+ assert!(stderr.contains("BEFORE"), "expected BEFORE in stderr");+ assert!(+ stderr.contains("AFTER"),+ "expected AFTER in stderr (reader stopped at the invalid byte)"+ );+ // CRLF line ending: the reader strips the trailing \r, so the visible+ // line is `CRLF-LINE` not `CRLF-LINE\r`. Any `\r` in the output would+ // indicate the strip didn't fire.+ assert!(+ stderr.contains("CRLF-LINE"),+ "expected CRLF-LINE (with the \\r stripped) in stderr"+ );+ assert!(+ !stderr.contains("CRLF-LINE\r"),+ "trailing \\r should have been stripped before printing"+ );+ assert_eq!(+ stderr.matches("PAYLOAD").count(),+ 50_000,+ "expected 50000 PAYLOAD lines after the invalid byte, got {}",+ stderr.matches("PAYLOAD").count(),+ );+}++/// A concurrent child that produces a large volume of stdout must not+/// deadlock the executor — the reader thread has to keep draining the pipe so+/// the child can keep writing. We run two commands, each emitting ~400 KB, and+/// assert both streams land in stderr intact.+#[rstest]+fn test_alias_concurrent_large_output(repo: TestRepo) {+ // yes piped through head is a portable way to generate many lines fast.+ // Each command produces ~400 KB = 50_000 * ~8 bytes ("aaaa...\n" etc.).+ // If the reader thread were ever to stall, the child's stdout pipe would+ // fill (default ~64 KB) and the child would block forever — the test would+ // time out rather than produce a misleading pass.+ repo.write_test_config(+ r#"+[aliases.bulk]+first = "yes 'FIRST-PAYLOAD-AAAAA' | head -n 50000"+second = "yes 'SECOND-PAYLOAD-BBBBB' | head -n 50000"+"#,+ );+ repo.commit("initial");++ let mut cmd = repo.wt_command();+ cmd.args(["step", "bulk"]);+ let output = cmd.output().expect("wt step bulk failed to spawn");++ let stderr = String::from_utf8_lossy(&output.stderr);+ assert!(+ output.status.success(),+ "concurrent alias with large output should exit 0, got: {status:?}\nlast 500 bytes of stderr: {tail}",+ status = output.status,+ tail = &stderr[stderr.len().saturating_sub(500)..],+ );++ // Count occurrences so we know both children's full output was streamed —+ // not truncated by a blocked pipe. 50_000 exact matches per payload.+ let first_count = stderr.matches("FIRST-PAYLOAD-AAAAA").count();+ let second_count = stderr.matches("SECOND-PAYLOAD-BBBBB").count();+ assert_eq!(+ first_count, 50_000,+ "expected 50000 occurrences of first payload in stderr, got {first_count}"+ );+ assert_eq!(+ second_count, 50_000,+ "expected 50000 occurrences of second payload in stderr, got {second_count}"+ );+}+/// Pipeline-form aliases (list of steps) run sequentially. A later step/// referencing `{{ vars.X }}` must see vars set by an earlier step —/// `expand_shell_template` reads `vars.*` fresh from git config on each call.diff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snapindex a7bdaed09..ea4ca751e 100644--- a/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snap+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_pipeline_announcement.snap@@ -47,5 +47,5 @@ exit_code: 0----- stderr -----[36m◎[39m [36mRunning alias [1mdeploy[22m: [1minstall[22m; [1mbuild[22m, [1mlint[22m[39m[0mINSTALL-[0mBUILD-[0mLINT+[1m[36mbuild[0m │ BUILD+[1m[35mlint [0m │ LINT