Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Rust-orchestrated pipeline execution with stdin spec passing (#1893)
Maximilian Roos5913e3d56ba0
diff --git a/docs/content/hook.md b/docs/content/hook.mdindex 8a2d79c2a..4cfee9dcf 100644--- a/docs/content/hook.md+++ b/docs/content/hook.md@@ -253,13 +253,7 @@ In summary:## How it works-Steps are chained with `&&` in a compound shell command, so a failing step skips all later steps. A multi-entry map spawns its commands as background processes and waits for all to complete before the next step.--For the example above, the generated command is:--```-{ npm install; } && { { npm run build; } & { npm run lint; } & wait; }-```+Steps run in order. A failing step aborts the pipeline — later steps don't run. A multi-entry map spawns its commands concurrently and waits for all to complete before the next step.Pre-* hooks ignore pipeline structure — all commands run serially regardless, since pre-* hooks are blocking by nature.diff --git a/skills/worktrunk/reference/hook.md b/skills/worktrunk/reference/hook.mdindex 2ed4d31ef..9c7ef67b0 100644--- a/skills/worktrunk/reference/hook.md+++ b/skills/worktrunk/reference/hook.md@@ -251,13 +251,7 @@ In summary:## How it works-Steps are chained with `&&` in a compound shell command, so a failing step skips all later steps. A multi-entry map spawns its commands as background processes and waits for all to complete before the next step.--For the example above, the generated command is:--```-{ npm install; } && { { npm run build; } & { npm run lint; } & wait; }-```+Steps run in order. A failing step aborts the pipeline — later steps don't run. A multi-entry map spawns its commands concurrently and waits for all to complete before the next step.Pre-* hooks ignore pipeline structure — all commands run serially regardless, since pre-* hooks are blocking by nature.diff --git a/src/cli/hook.rs b/src/cli/hook.rsindex 136f3c9fd..169891d59 100644--- a/src/cli/hook.rs+++ b/src/cli/hook.rs@@ -273,6 +273,10 @@ pub enum HookCommand {vars: Vec<(String, String)>,},+ /// Internal: run a serialized pipeline from stdin+ #[command(hide = true, name = "run-pipeline")]+ RunPipeline,+/// Manage command approvals#[command(after_long_help = r#"Project hooks require approval on first run to prevent untrusted projects from running arbitrary commands.diff --git a/src/cli/mod.rs b/src/cli/mod.rsindex a7eab81f3..dc1c64e91 100644--- a/src/cli/mod.rs+++ b/src/cli/mod.rs@@ -1365,13 +1365,7 @@ In summary:## How it works-Steps are chained with `&&` in a compound shell command, so a failing step skips all later steps. A multi-entry map spawns its commands as background processes and waits for all to complete before the next step.--For the example above, the generated command is:--```-{ npm install; } && { { npm run build; } & { npm run lint; } & wait; }-```+Steps run in order. A failing step aborts the pipeline — later steps don't run. A multi-entry map spawns its commands concurrently and waits for all to complete before the next step.Pre-* hooks ignore pipeline structure — all commands run serially regardless, since pre-* hooks are blocking by nature.diff --git a/src/cli/step.rs b/src/cli/step.rsindex 6a39831b4..39f965f0d 100644--- a/src/cli/step.rs+++ b/src/cli/step.rs@@ -393,10 +393,6 @@ Note: This command is experimental and may change in future versions./// Template expression to evaluatetemplate: String,- /// Shell-escape substituted values (used internally by pipeline lazy expansion)- #[arg(long, hide = true)]- shell_escape: bool,-/// Show template variables and expanded result#[arg(long)]dry_run: bool,diff --git a/src/commands/eval.rs b/src/commands/eval.rsindex 2ad4bb7ba..bc4a6f809 100644--- a/src/commands/eval.rs+++ b/src/commands/eval.rs@@ -1,8 +1,7 @@//! Eval command implementation//!//! Evaluates a template expression in the current worktree context and prints-//! the result to stdout. Output is raw by default; `--shell-escape` enables-//! shell escaping for safe use with `eval`.+//! the result to stdout.use std::collections::HashMap;@@ -18,7 +17,7 @@ use crate::commands::command_executor::{CommandContext, build_hook_context};////// With `dry_run`, prints the template variables and the expanded result/// to stderr — useful for debugging templates.-pub fn step_eval(template: &str, shell_escape: bool, dry_run: bool) -> anyhow::Result<()> {+pub fn step_eval(template: &str, dry_run: bool) -> anyhow::Result<()> {let repo = Repository::current()?;let config = UserConfig::load()?;@@ -34,7 +33,7 @@ pub fn step_eval(template: &str, shell_escape: bool, dry_run: bool) -> anyhow::R.map(|(k, v)| (k.as_str(), v.as_str())).collect();- let result = expand_template(template, &vars, shell_escape, &repo, "eval")?;+ let result = expand_template(template, &vars, false, &repo, "eval")?;if dry_run {let mut keys: Vec<&str> = context_map.keys().map(|k| k.as_str()).collect();diff --git a/src/commands/hook_commands.rs b/src/commands/hook_commands.rsindex 245d25fa2..89b30a451 100644--- a/src/commands/hook_commands.rs+++ b/src/commands/hook_commands.rs@@ -67,7 +67,7 @@ fn run_post_hook() -> anyhow::Result<()> {// Default to background execution; --foreground is for debugging.if !foreground.unwrap_or(false) {- // Pipeline configs (list form) use compound shell command path.+ // Pipeline configs (list form) use the background pipeline runner.// Name filtering falls through to the flat path (individual command execution).let has_pipeline = [user_config, project_config].iter()diff --git a/src/commands/hooks.rs b/src/commands/hooks.rsindex 130c06fbe..9133abdc1 100644--- a/src/commands/hooks.rs+++ b/src/commands/hooks.rs@@ -1,5 +1,6 @@use std::path::{Path, PathBuf};+use anyhow::Context;use color_print::cformat;use worktrunk::HookType;use worktrunk::config::CommandConfig;@@ -12,7 +13,7 @@ use worktrunk::styling::{use super::command_executor::{CommandContext, PreparedCommand, PreparedStep, prepare_commands, prepare_steps,};-use crate::commands::process::{HookLog, spawn_detached};+use crate::commands::process::{HookLog, spawn_detached, spawn_detached_exec};use crate::output::execute_command_in_worktree;/// A prepared command with its source information.@@ -244,7 +245,6 @@ pub fn spawn_background_hooks(ctx.branch_or_head(),&hook_log,Some(&cmd.prepared.context_json),- &[],) {let err_msg = err.to_string();let message = match &cmd.prepared.name {@@ -265,7 +265,7 @@ pub fn spawn_background_hooks(pub(crate) enum PreparedHooks {/// Traditional flat commands (string or map config) — spawned independently.Flat(Vec<SourcedCommand>),- /// Pipeline steps (list config) — spawned as one compound shell command.+ /// Pipeline steps (list config) — spawned as `wt hook run-pipeline`.Pipeline(Vec<SourcedStep>),}@@ -277,60 +277,6 @@ pub struct SourcedStep {pub display_path: Option<PathBuf>,}-/// Build a compound shell command from pipeline steps, collecting env vars for-/// lazy template expansion.-///-/// Serial steps are chained with `&&`. Concurrent groups use background-/// processes with `wait`. Commands referencing `vars.` are wrapped in-/// `eval "$(wt step eval --shell-escape ...)"` and their raw templates are-/// returned as env vars for the spawned process.-///-/// Returns `(shell_command, env_vars)`.-fn build_pipeline_command(steps: &[SourcedStep]) -> (String, Vec<(String, String)>) {- let wt_bin = std::env::current_exe()- .ok()- .map(|p| shell_escape::escape(p.to_string_lossy()).into_owned())- .unwrap_or_else(|| "wt".to_string());-- let mut parts = Vec::new();- let mut env_vars: Vec<(String, String)> = Vec::new();-- for step in steps {- match &step.step {- PreparedStep::Single(cmd) => {- // Wrap in braces to prevent operator interaction between steps.- // Without this, `false || echo fallback && echo next` would- // parse as `false || (echo fallback && echo next)`.- parts.push(format!(- "{{ {}; }}",- format_cmd(cmd, &wt_bin, &mut env_vars)- ));- }- PreparedStep::Concurrent(cmds) => {- let bg: Vec<String> = cmds- .iter()- .map(|c| format!("{{ {}; }} &", format_cmd(c, &wt_bin, &mut env_vars)))- .collect();- parts.push(format!("{{ {} wait; }}", bg.join(" ")));- }- }- }- (parts.join(" && "), env_vars)-}--/// Format a single command for the pipeline, using lazy eval wrapping for commands-/// with `lazy_template`.-fn format_cmd(cmd: &PreparedCommand, wt_bin: &str, env_vars: &mut Vec<(String, String)>) -> String {- match &cmd.lazy_template {- Some(template) => {- let var_name = format!("__WT_TPL_{}", env_vars.len());- env_vars.push((var_name.clone(), template.clone()));- format!(r#"eval "$({wt_bin} step eval --shell-escape "${var_name}")""#)- }- None => cmd.expanded.clone(),- }-}-/// Format a summary description of a pipeline for display.////// Shows step names/counts with `→` separating serial steps.@@ -370,14 +316,17 @@ fn format_pipeline_summary(steps: &[SourcedStep]) -> String {parts.join(" → ")}-/// Spawn a hook pipeline as one background process.+/// Spawn a hook pipeline as a background `wt hook run-pipeline` process.///-/// Builds a compound shell command where serial steps are chained with `&&`-/// and concurrent groups use background processes. The entire pipeline runs-/// as one detached process.+/// Serializes the pipeline steps to JSON and spawns `wt hook run-pipeline`+/// as a detached process, piping the spec to stdin. The background+/// process expands templates just-in-time and executes each step via+/// the detected shell.////// Used for post-* hooks when the config uses a pipeline (list form).pub fn spawn_hook_pipeline(ctx: &CommandContext, steps: Vec<SourcedStep>) -> anyhow::Result<()> {+ use super::pipeline_spec::{PipelineCommandSpec, PipelineSpec, PipelineStepSpec};+if steps.is_empty() {return Ok(());}@@ -397,37 +346,70 @@ pub fn spawn_hook_pipeline(ctx: &CommandContext, steps: Vec<SourcedStep>) -> any};eprintln!("{}", progress_message(message));- let (pipeline_cmd, lazy_env) = build_pipeline_command(&steps);- let hook_log = HookLog::hook(source, hook_type, "pipeline");- let log_label = format!("{hook_type} {source} pipeline");-- // Use the first command's context JSON for the whole pipeline.- // All steps share the same base context (branch, worktree_path, etc.).- let context_json = steps.iter().find_map(|s| match &s.step {- PreparedStep::Single(cmd) => Some(cmd.context_json.as_str()),- PreparedStep::Concurrent(cmds) => cmds.first().map(|c| c.context_json.as_str()),- });-- let extra_env: Vec<(&str, &str)> = lazy_env+ // Extract context from the first command. All steps share the same base context.+ let context: std::collections::HashMap<String, String> = steps+ .iter()+ .find_map(|s| match &s.step {+ PreparedStep::Single(cmd) => Some(&cmd.context_json),+ PreparedStep::Concurrent(cmds) => cmds.first().map(|c| &c.context_json),+ })+ .map(|json| serde_json::from_str(json).context("failed to deserialize context_json"))+ .transpose()?+ .unwrap_or_default();++ // Build pipeline spec from prepared steps. Use the raw template for lazy+ // steps (vars-referencing) and the expanded command for eager steps.+ let spec_steps: Vec<PipelineStepSpec> = steps.iter()- .map(|(k, v)| (k.as_str(), v.as_str()))+ .map(|s| match &s.step {+ PreparedStep::Single(cmd) => PipelineStepSpec::Single {+ name: cmd.name.clone(),+ template: cmd.lazy_template.as_ref().unwrap_or(&cmd.expanded).clone(),+ },+ PreparedStep::Concurrent(cmds) => PipelineStepSpec::Concurrent {+ commands: cmds+ .iter()+ .map(|c| PipelineCommandSpec {+ name: c.name.clone(),+ template: c.lazy_template.as_ref().unwrap_or(&c.expanded).clone(),+ })+ .collect(),+ },+ }).collect();- if let Err(err) = spawn_detached(+ let spec = PipelineSpec {+ worktree_path: ctx.worktree_path.to_path_buf(),+ branch: ctx.branch_or_head().to_string(),+ hook_type: hook_type.to_string(),+ source: source.to_string(),+ context,+ steps: spec_steps,+ };++ let spec_json = serde_json::to_vec(&spec).context("failed to serialize pipeline spec")?;++ let wt_bin = std::env::current_exe().context("failed to resolve wt binary path")?;++ let hook_log = HookLog::hook(source, hook_type, "pipeline");+ let log_label = format!("{hook_type} {source} pipeline");++ if let Err(err) = spawn_detached_exec(ctx.repo,ctx.worktree_path,- &pipeline_cmd,+ &wt_bin,+ &["hook", "run-pipeline"],ctx.branch_or_head(),&hook_log,- context_json,- &extra_env,+ &spec_json,) {eprintln!("{}",warning_message(format!("Failed to spawn pipeline: {err}")));} else {- worktrunk::command_log::log_command(&log_label, &pipeline_cmd, None, None);+ let cmd_display = format!("{} hook run-pipeline", wt_bin.display());+ worktrunk::command_log::log_command(&log_label, &cmd_display, None, None);}Ok(())@@ -797,48 +779,6 @@ mod tests {}}- #[test]- fn test_build_pipeline_command_serial_only() {- let steps = vec![- make_sourced_step(PreparedStep::Single(make_cmd(None, "npm install"))),- make_sourced_step(PreparedStep::Single(make_cmd(- Some("build"),- "npm run build",- ))),- ];- let (cmd, env) = build_pipeline_command(&steps);- assert_eq!(cmd, "{ npm install; } && { npm run build; }");- assert!(env.is_empty());- }-- #[test]- fn test_build_pipeline_command_serial_then_concurrent() {- let steps = vec![- make_sourced_step(PreparedStep::Single(make_cmd(None, "npm install"))),- make_sourced_step(PreparedStep::Concurrent(vec![- make_cmd(Some("build"), "npm run build"),- make_cmd(Some("lint"), "npm run lint"),- ])),- ];- let (cmd, env) = build_pipeline_command(&steps);- assert_eq!(- cmd,- "{ npm install; } && { { npm run build; } & { npm run lint; } & wait; }"- );- assert!(env.is_empty());- }-- #[test]- fn test_build_pipeline_command_single_concurrent() {- let steps = vec![make_sourced_step(PreparedStep::Concurrent(vec![make_cmd(- Some("only"),- "echo hi",- )]))];- let (cmd, env) = build_pipeline_command(&steps);- assert_eq!(cmd, "{ { echo hi; } & wait; }");- assert!(env.is_empty());- }-#[test]fn test_format_pipeline_summary_named() {let steps = vec![@@ -877,96 +817,4 @@ mod tests {let single = CommandConfig::single("npm install");assert!(!single.is_pipeline());}-- fn make_lazy_cmd(name: Option<&str>, template: &str) -> PreparedCommand {- PreparedCommand {- name: name.map(String::from),- expanded: template.to_string(),- context_json: "{}".to_string(),- lazy_template: Some(template.to_string()),- }- }-- #[test]- fn test_build_pipeline_command_lazy_step() {- let steps = vec![- make_sourced_step(PreparedStep::Single(make_cmd(None, "echo setup"))),- make_sourced_step(PreparedStep::Single(make_lazy_cmd(- Some("db"),- "docker run --name {{ vars.container }}",- ))),- ];- let (cmd, env) = build_pipeline_command(&steps);-- // Lazy step uses eval wrapping with properly quoted env var reference.- // The closing ")" must be outside $() — a missing " produces broken shell.- assert!(- cmd.contains("eval \"$("),- "should contain eval wrapping: {cmd}"- );- assert!(- cmd.contains("\"$__WT_TPL_0\")\""),- "should close with proper quoting (var quote, paren, outer quote): {cmd}"- );-- // Env var contains the raw template- assert_eq!(env.len(), 1);- assert_eq!(env[0].0, "__WT_TPL_0");- assert_eq!(env[0].1, "docker run --name {{ vars.container }}");- }-- #[test]- fn test_build_pipeline_command_mixed_lazy_and_eager() {- let steps = vec![- make_sourced_step(PreparedStep::Single(make_cmd(None, "echo setup"))),- make_sourced_step(PreparedStep::Concurrent(vec![- make_lazy_cmd(Some("db"), "docker run {{ vars.name }}"),- make_cmd(Some("lint"), "npm run lint"),- ])),- ];- let (cmd, env) = build_pipeline_command(&steps);-- // First step is eager (no eval wrapping)- assert!(cmd.starts_with("{ echo setup; }"), "eager step: {cmd}");-- // Concurrent group has one lazy and one eager- assert!(cmd.contains("npm run lint"), "eager concurrent cmd: {cmd}");- assert!(- cmd.contains("\"$__WT_TPL_0\")\""),- "lazy concurrent cmd should have proper quoting: {cmd}"- );-- // Only one env var (for the lazy command)- assert_eq!(env.len(), 1);- assert_eq!(env[0].1, "docker run {{ vars.name }}");- }-- #[test]- fn test_build_pipeline_command_multiple_lazy_steps() {- let steps = vec![- make_sourced_step(PreparedStep::Single(make_cmd(None, "echo setup"))),- make_sourced_step(PreparedStep::Single(make_lazy_cmd(- None,- "echo {{ vars.a }}",- ))),- make_sourced_step(PreparedStep::Single(make_lazy_cmd(- None,- "echo {{ vars.b }}",- ))),- ];- let (cmd, env) = build_pipeline_command(&steps);-- // Each lazy step gets its own numbered env var with proper quoting- assert_eq!(env.len(), 2);- assert_eq!(env[0].0, "__WT_TPL_0");- assert_eq!(env[1].0, "__WT_TPL_1");- assert!(- cmd.contains("\"$__WT_TPL_0\")\""),- "first lazy step should have proper quoting: {cmd}"- );- assert!(- cmd.contains("\"$__WT_TPL_1\")\""),- "second lazy step should have proper quoting: {cmd}"- );- }}diff --git a/src/commands/mod.rs b/src/commands/mod.rsindex 49a0ba0c3..40dad6019 100644--- a/src/commands/mod.rs+++ b/src/commands/mod.rs@@ -17,10 +17,12 @@ pub(crate) mod list;pub(crate) mod merge;#[cfg(unix)]pub(crate) mod picker;+pub(crate) mod pipeline_spec;pub(crate) mod process;pub(crate) mod project_config;mod relocate;pub(crate) mod repository_ext;+mod run_pipeline;pub(crate) mod statusline;pub(crate) mod step_commands;pub(crate) mod worktree;@@ -46,6 +48,7 @@ pub(crate) use merge::{MergeOptions, handle_merge};#[cfg(unix)]pub(crate) use picker::handle_picker;pub(crate) use repository_ext::RemoveTarget;+pub(crate) use run_pipeline::run_pipeline;pub(crate) use step_commands::{PromoteResult, RebaseResult, SquashResult, handle_promote, handle_rebase, handle_squash,step_commit, step_copy_ignored, step_diff, step_prune, step_relocate, step_show_squash_prompt,diff --git a/src/commands/pipeline_spec.rs b/src/commands/pipeline_spec.rsnew file mode 100644index 000000000..41b2b39dd--- /dev/null+++ b/src/commands/pipeline_spec.rs@@ -0,0 +1,99 @@+use serde::{Deserialize, Serialize};+use std::collections::HashMap;+use std::path::PathBuf;++/// Serialized specification for a background pipeline.+///+/// Serialized to JSON and piped to stdin of `wt hook run-pipeline`.+/// Contains raw templates — expansion happens at execution time in+/// the background process.+#[derive(Serialize, Deserialize)]+pub struct PipelineSpec {+ pub worktree_path: PathBuf,+ pub branch: String,+ pub hook_type: String,+ pub source: String,+ /// Base context variables for template expansion.+ pub context: HashMap<String, String>,+ pub steps: Vec<PipelineStepSpec>,+}++#[derive(Serialize, Deserialize)]+pub enum PipelineStepSpec {+ Single {+ name: Option<String>,+ template: String,+ },+ Concurrent {+ commands: Vec<PipelineCommandSpec>,+ },+}++#[derive(Serialize, Deserialize)]+pub struct PipelineCommandSpec {+ pub name: Option<String>,+ pub template: String,+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn test_pipeline_spec_roundtrip() {+ let spec = PipelineSpec {+ worktree_path: "/tmp/test-worktree".into(),+ branch: "feature/auth".into(),+ hook_type: "post-start".into(),+ source: "user".into(),+ context: [("branch".into(), "feature/auth".into())]+ .into_iter()+ .collect(),+ steps: vec![+ PipelineStepSpec::Single {+ name: Some("install".into()),+ template: "npm install".into(),+ },+ PipelineStepSpec::Concurrent {+ commands: vec![+ PipelineCommandSpec {+ name: Some("build".into()),+ template: "npm run build".into(),+ },+ PipelineCommandSpec {+ name: None,+ template: "echo {{ vars.tag }}".into(),+ },+ ],+ },+ ],+ };++ let json = serde_json::to_string(&spec).unwrap();+ let roundtripped: PipelineSpec = serde_json::from_str(&json).unwrap();++ assert_eq!(roundtripped.worktree_path, spec.worktree_path);+ assert_eq!(roundtripped.branch, spec.branch);+ assert_eq!(roundtripped.hook_type, spec.hook_type);+ assert_eq!(roundtripped.source, spec.source);+ assert_eq!(roundtripped.context, spec.context);+ assert_eq!(roundtripped.steps.len(), 2);++ // Verify step structure survives roundtrip+ match &roundtripped.steps[0] {+ PipelineStepSpec::Single { name, template } => {+ assert_eq!(name.as_deref(), Some("install"));+ assert_eq!(template, "npm install");+ }+ _ => panic!("expected Single step"),+ }+ match &roundtripped.steps[1] {+ PipelineStepSpec::Concurrent { commands } => {+ assert_eq!(commands.len(), 2);+ assert_eq!(commands[0].name.as_deref(), Some("build"));+ assert!(commands[1].template.contains("vars.tag"));+ }+ _ => panic!("expected Concurrent step"),+ }+ }+}diff --git a/src/commands/process.rs b/src/commands/process.rsindex f4c86f48a..42b1ce54b 100644--- a/src/commands/process.rs+++ b/src/commands/process.rs@@ -2,7 +2,6 @@ use anyhow::Context;use color_print::cformat;use std::fs;use std::path::{Path, PathBuf};-#[cfg(unix)]use std::process::Command;use std::process::Stdio;use std::str::FromStr;@@ -194,20 +193,18 @@ fn posix_command_separator(command: &str) -> &'static str {/// * `branch` - Branch name for log organization/// * `hook_log` - Log specification (determines the log filename)/// * `context_json` - Optional JSON context to pipe to command's stdin-/// * `extra_env` - Additional environment variables for the spawned process////// # Returns/// Path to the log file where output is being written-pub fn spawn_detached(+/// Create the log directory and file for a detached process.+///+/// Returns `(log_path, log_file)`. Shared by `spawn_detached` and+/// `spawn_detached_exec`.+fn create_detach_log(repo: &Repository,- worktree_path: &Path,- command: &str,branch: &str,hook_log: &HookLog,- context_json: Option<&str>,- extra_env: &[(&str, &str)],-) -> anyhow::Result<std::path::PathBuf> {- // Create log directory in the common git directory+) -> anyhow::Result<(PathBuf, fs::File)> {let log_dir = repo.wt_logs_dir();fs::create_dir_all(&log_dir).with_context(|| {format!(@@ -216,10 +213,7 @@ pub fn spawn_detached()})?;- // Generate log path using the HookLog specificationlet log_path = hook_log.path(&log_dir, branch);-- // Create log filelet log_file = fs::File::create(&log_path).with_context(|| {format!("Failed to create log file {}",@@ -227,6 +221,19 @@ pub fn spawn_detached()})?;+ Ok((log_path, log_file))+}++pub fn spawn_detached(+ repo: &Repository,+ worktree_path: &Path,+ command: &str,+ branch: &str,+ hook_log: &HookLog,+ context_json: Option<&str>,+) -> anyhow::Result<std::path::PathBuf> {+ let (log_path, log_file) = create_detach_log(repo, branch, hook_log)?;+log::debug!("$ {} (detached, logging to {})",command,@@ -235,12 +242,12 @@ pub fn spawn_detached(#[cfg(unix)]{- spawn_detached_unix(worktree_path, command, log_file, context_json, extra_env)?;+ spawn_detached_unix(worktree_path, command, log_file, context_json)?;}#[cfg(windows)]{- spawn_detached_windows(worktree_path, command, log_file, context_json, extra_env)?;+ spawn_detached_windows(worktree_path, command, log_file, context_json)?;}Ok(log_path)@@ -252,7 +259,6 @@ fn spawn_detached_unix(command: &str,log_file: fs::File,context_json: Option<&str>,- extra_env: &[(&str, &str)],) -> anyhow::Result<()> {use std::os::unix::process::CommandExt;@@ -298,7 +304,6 @@ fn spawn_detached_unix(.stderr(Stdio::from(log_file))// Prevent hooks from writing to the directive file.env_remove(worktrunk::shell_exec::DIRECTIVE_FILE_ENV_VAR)- .envs(extra_env.iter().map(|(k, v)| (k, v))).process_group(0) // New process group, not in PTY's foreground group.spawn().context("Failed to spawn detached process")?;@@ -317,7 +322,6 @@ fn spawn_detached_windows(command: &str,log_file: fs::File,context_json: Option<&str>,- extra_env: &[(&str, &str)],) -> anyhow::Result<()> {use std::os::windows::process::CommandExt;use worktrunk::shell_exec::ShellConfig;@@ -373,7 +377,6 @@ fn spawn_detached_windows(.stderr(Stdio::from(log_file))// Prevent hooks from writing to the directive file.env_remove(worktrunk::shell_exec::DIRECTIVE_FILE_ENV_VAR)- .envs(extra_env.iter().map(|(k, v)| (k, v))).creation_flags(CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS).spawn().context("Failed to spawn detached process")?;@@ -384,6 +387,115 @@ fn spawn_detached_windows(Ok(())}+/// Spawn a detached background process by executing a binary directly.+///+/// Unlike [`spawn_detached`] (which wraps a shell command in `sh -c`), this+/// spawns the executable without an intermediate shell. Stdin bytes are written+/// to the child's stdin pipe and then the pipe is closed.+///+/// Used for structured child processes like `wt hook run-pipeline` where the parent+/// passes data via stdin rather than through a temp file or shell arguments.+pub fn spawn_detached_exec(+ repo: &Repository,+ worktree_path: &Path,+ program: &Path,+ args: &[&str],+ branch: &str,+ hook_log: &HookLog,+ stdin_bytes: &[u8],+) -> anyhow::Result<std::path::PathBuf> {+ let (log_path, log_file) = create_detach_log(repo, branch, hook_log)?;++ log::debug!(+ "$ {} {} (detached, logging to {})",+ program.display(),+ args.join(" "),+ log_path.file_name().unwrap_or_default().to_string_lossy()+ );++ #[cfg(unix)]+ {+ spawn_detached_exec_unix(worktree_path, program, args, log_file, stdin_bytes)?;+ }++ #[cfg(windows)]+ {+ spawn_detached_exec_windows(worktree_path, program, args, log_file, stdin_bytes)?;+ }++ Ok(log_path)+}++#[cfg(unix)]+fn spawn_detached_exec_unix(+ worktree_path: &Path,+ program: &Path,+ args: &[&str],+ log_file: fs::File,+ stdin_bytes: &[u8],+) -> anyhow::Result<()> {+ use std::io::Write;+ use std::os::unix::process::CommandExt;++ let mut child = Command::new(program)+ .args(args)+ .current_dir(worktree_path)+ .stdin(Stdio::piped())+ .stdout(Stdio::from(+ log_file+ .try_clone()+ .context("Failed to clone log file handle")?,+ ))+ .stderr(Stdio::from(log_file))+ .env_remove(worktrunk::shell_exec::DIRECTIVE_FILE_ENV_VAR)+ .process_group(0)+ .spawn()+ .context("Failed to spawn detached process")?;++ if let Some(mut stdin) = child.stdin.take() {+ // Ignore BrokenPipe — child may exit before reading all input.+ let _ = stdin.write_all(stdin_bytes);+ }++ Ok(())+}++#[cfg(windows)]+fn spawn_detached_exec_windows(+ worktree_path: &Path,+ program: &Path,+ args: &[&str],+ log_file: fs::File,+ stdin_bytes: &[u8],+) -> anyhow::Result<()> {+ use std::io::Write;+ use std::os::windows::process::CommandExt;++ const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;+ const DETACHED_PROCESS: u32 = 0x00000008;++ let mut child = Command::new(program)+ .args(args)+ .current_dir(worktree_path)+ .stdin(Stdio::piped())+ .stdout(Stdio::from(+ log_file+ .try_clone()+ .context("Failed to clone log file handle")?,+ ))+ .stderr(Stdio::from(log_file))+ .env_remove(worktrunk::shell_exec::DIRECTIVE_FILE_ENV_VAR)+ .creation_flags(CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS)+ .spawn()+ .context("Failed to spawn detached process")?;++ if let Some(mut stdin) = child.stdin.take() {+ let _ = stdin.write_all(stdin_bytes);+ }++ Ok(())+}+/// Generate a staging path for worktree removal.////// Places the staging directory inside `.git/wt/trash/` so it is hidden from thediff --git a/src/commands/run_pipeline.rs b/src/commands/run_pipeline.rsnew file mode 100644index 000000000..0c5a52f12--- /dev/null+++ b/src/commands/run_pipeline.rs@@ -0,0 +1,190 @@+//! Pipeline runner for background hook execution.+//!+//! The parent `wt` process serializes a [`PipelineSpec`] to JSON and spawns+//! `wt hook run-pipeline` as a detached process (via `spawn_detached_exec`, which+//! pipes the JSON to stdin, redirects stdout/stderr to a log file, and puts+//! the process in its own process group). This module is that background+//! process.+//!+//! ## Lifecycle+//!+//! 1. Read and deserialize the spec from stdin.+//! 2. Open a [`Repository`] from the worktree path in the spec.+//! 3. Walk steps in order. For each step, expand templates and spawn shell+//! children (see Execution model). Abort on the first serial step failure.+//! 4. Exit. The log file in `.git/wt/logs/` is the only artifact.+//!+//! ## Execution model+//!+//! Each command — whether serial or concurrent — gets its own shell process+//! via [`ShellConfig`] (`sh` on Unix, Git Bash on Windows). Shell state+//! (`cd`, `export`, environment) does not carry across steps.+//!+//! **Serial steps** run one at a time. If a step exits non-zero, the+//! pipeline aborts — later steps don't run.+//!+//! **Concurrent groups** spawn all children at once, then wait for every+//! child before proceeding. If any child fails, the group is reported as+//! failed, but all children are allowed to finish. Template expansion for+//! concurrent commands happens sequentially before any child is spawned+//! (expansion may read git config, so order matters for `vars.*`).+//!+//! **Stdin**: every child receives the spec's context as JSON on stdin,+//! matching the foreground hook convention. Commands that don't read stdin+//! ignore it.+//!+//! ## Template freshness+//!+//! The spec carries two kinds of template input:+//!+//! - **Base context** (`branch`, `commit`, `worktree_path`, …) — snapshotted+//! once when the parent builds the spec. A step that creates a new commit+//! won't update `{{ commit }}` for later steps.+//!+//! - **`vars.*`** — read fresh from git config on every `expand_template`+//! call. A step that runs `wt config state vars set key=val` makes+//! `{{ vars.key }}` available to subsequent steps.+//!+//! This distinction exists because `vars.*` are the intended inter-step+//! communication channel (cheap git-config reads), while rebuilding the full+//! base context would spawn multiple git subprocesses per step.+//!+//! Template values are shell-escaped at expansion time (`shell_escape=true`)+//! since the expanded string is passed to a shell for interpretation.++use std::collections::HashMap;+use std::io::Read as _;+use std::path::Path;+use std::process::{Child, Stdio};++use anyhow::{Context, bail};++use worktrunk::config::expand_template;+use worktrunk::git::Repository;+use worktrunk::shell_exec::ShellConfig;++use super::pipeline_spec::{PipelineSpec, PipelineStepSpec};++/// Run a serialized pipeline from stdin.+///+/// This is the entry point for `wt hook run-pipeline`.+/// The orchestrator is a long-lived background process spawned by+/// `spawn_detached_exec`; stdout/stderr are already redirected to a log file.+pub fn run_pipeline() -> anyhow::Result<()> {+ let mut contents = String::new();+ std::io::stdin()+ .read_to_string(&mut contents)+ .context("failed to read pipeline spec from stdin")?;++ let spec: PipelineSpec =+ serde_json::from_str(&contents).context("failed to deserialize pipeline spec")?;++ let repo =+ Repository::at(&spec.worktree_path).context("failed to open repository for pipeline")?;++ let context_json =+ serde_json::to_string(&spec.context).context("failed to serialize pipeline context")?;++ for step in &spec.steps {+ match step {+ PipelineStepSpec::Single { template, name } => {+ let expanded = expand_now(template, &spec, &repo, name.as_deref())?;+ let mut child = spawn_shell_command(&expanded, &spec.worktree_path, &context_json)?;+ let status = child.wait().context("failed to wait for child process")?;+ if !status.success() {+ bail!(+ "command failed with {}: {}",+ format_exit(status.code()),+ expanded,+ );+ }+ }+ PipelineStepSpec::Concurrent { commands } => {+ run_concurrent_group(commands, &spec, &repo, &context_json)?;+ }+ }+ }++ Ok(())+}++/// Expand a template using the spec's context and fresh vars from git config.+fn expand_now(+ template: &str,+ spec: &PipelineSpec,+ repo: &Repository,+ name: Option<&str>,+) -> anyhow::Result<String> {+ let vars: HashMap<&str, &str> = spec+ .context+ .iter()+ .map(|(k, v)| (k.as_str(), v.as_str()))+ .collect();+ let label = name.unwrap_or("pipeline step");+ // shell_escape=true — values are interpolated into a string passed to a shell,+ // so they must be escaped to prevent word splitting and metachar injection.+ Ok(expand_template(template, &vars, true, repo, label)?)+}++/// Spawn a shell command with context JSON piped to stdin.+///+/// Uses `ShellConfig` for portable shell detection (Git Bash on Windows,+/// `sh` on Unix). Returns the `Child` so the caller controls when to wait.+fn spawn_shell_command(+ expanded: &str,+ worktree_path: &Path,+ context_json: &str,+) -> anyhow::Result<Child> {+ let shell = ShellConfig::get()?;+ let mut child = shell+ .command(expanded)+ .current_dir(worktree_path)+ .stdin(Stdio::piped())+ .spawn()+ .with_context(|| format!("failed to spawn: {expanded}"))?;++ // Write context JSON to stdin, then drop to close the pipe.+ if let Some(mut stdin) = child.stdin.take() {+ use std::io::Write;+ // Ignore BrokenPipe — child may exit or close stdin early.+ let _ = stdin.write_all(context_json.as_bytes());+ }++ Ok(child)+}++/// Spawn all commands in a concurrent group, then wait for all.+fn run_concurrent_group(+ commands: &[super::pipeline_spec::PipelineCommandSpec],+ spec: &PipelineSpec,+ repo: &Repository,+ context_json: &str,+) -> anyhow::Result<()> {+ let mut children = Vec::with_capacity(commands.len());++ for cmd in commands {+ let expanded = expand_now(&cmd.template, spec, repo, cmd.name.as_deref())?;+ let child = spawn_shell_command(&expanded, &spec.worktree_path, context_json)?;+ children.push((cmd.name.clone(), expanded, child));+ }++ let mut failures = Vec::new();+ for (name, expanded, mut child) in children {+ let status = child+ .wait()+ .with_context(|| format!("failed to wait for: {expanded}"))?;+ if !status.success() {+ let label = name.as_deref().unwrap_or(&expanded);+ failures.push(label.to_string());+ }+ }++ if !failures.is_empty() {+ bail!("concurrent group had failures: {}", failures.join(", "));+ }+ Ok(())+}++fn format_exit(code: Option<i32>) -> String {+ code.map_or("signal".to_string(), |c| format!("exit code {c}"))+}diff --git a/src/main.rs b/src/main.rsindex 5061ff6f7..5e0997e92 100644--- a/src/main.rs+++ b/src/main.rs@@ -247,6 +247,7 @@ fn handle_hook_command(action: HookCommand) -> anyhow::Result<()> {name.as_deref(),&vars,),+ HookCommand::RunPipeline => commands::run_pipeline(),HookCommand::Approvals { action } => match action {ApprovalsCommand::Add { all } => add_approvals(all),ApprovalsCommand::Clear { global } => clear_approvals(global),@@ -321,11 +322,7 @@ fn handle_step_command(action: StepCommand) -> anyhow::Result<()> {dry_run,force,} => step_copy_ignored(from.as_deref(), to.as_deref(), dry_run, force),- StepCommand::Eval {- template,- shell_escape,- dry_run,- } => step_eval(&template, shell_escape, dry_run),+ StepCommand::Eval { template, dry_run } => step_eval(&template, dry_run),StepCommand::ForEach { args } => step_for_each(args),StepCommand::Promote { branch } => {handle_promote(branch.as_deref()).map(|result| match result {diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex ed62e1416..bb1bfce4b 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -78,7 +78,6 @@ fn spawn_background_removal(log_label,&HookLog::internal(InternalOp::Remove),None,- &[],)?;Ok(())}diff --git a/tests/integration_tests/eval.rs b/tests/integration_tests/eval.rsindex 191329a28..37c131732 100644--- a/tests/integration_tests/eval.rs+++ b/tests/integration_tests/eval.rs@@ -79,30 +79,3 @@ fn test_eval_conditional(repo: TestRepo) {None,));}--#[rstest]-fn test_eval_shell_escape(repo: TestRepo) {- // --shell-escape quotes values containing shell metacharacters- assert_cmd_snapshot!(make_snapshot_cmd(- &repo,- "step",- &["eval", "--shell-escape", "echo {{ branch }}"],- None,- ));-}--#[rstest]-fn test_eval_shell_escape_with_vars(repo: TestRepo) {- // Set a var with spaces, then expand with shell escaping- repo.run_git(&["config", "worktrunk.state.main.vars.name", "my container"]);- assert_cmd_snapshot!(make_snapshot_cmd(- &repo,- "step",- &[- "eval",- "--shell-escape",- "docker run --name {{ vars.name }}"- ],- None,- ));-}diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rsindex 7f893994c..113f51e26 100644--- a/tests/integration_tests/user_hooks.rs+++ b/tests/integration_tests/user_hooks.rs@@ -2310,8 +2310,8 @@ check = "test -d {{ cwd }} && echo 'cwd_exists=true' > ../cwd_check.txt || echo#[rstest]fn test_user_post_start_pipeline_serial_ordering(repo: TestRepo) {// Pipeline: serial step creates a marker, concurrent step reads it.- // The compound shell command chains them with &&, so the marker- // exists when the concurrent step runs.+ // Serial steps run in order, so the marker exists when the+ // concurrent step runs.repo.write_test_config(r#"post-start = ["echo SETUP_DONE > pipeline_marker.txt",@@ -2339,7 +2339,7 @@ fn test_user_post_start_pipeline_serial_ordering(repo: TestRepo) {#[rstest]fn test_user_post_start_pipeline_failure_skips_later_steps(repo: TestRepo) {- // First step fails → second step should not run (chained with &&).+ // First step fails → second step should not run (pipeline aborts on failure).repo.write_test_config(r#"post-start = ["exit 1",@@ -2411,11 +2411,9 @@ fn test_user_post_start_pipeline_lazy_vars_foreground(repo: TestRepo) {#[rstest]fn test_user_post_start_pipeline_lazy_vars_background(repo: TestRepo) {// Pipeline step 1 sets a var via git config (not `wt config` — bare `wt`- // isn't on PATH in the detached background shell). Step 2 references- // {{ vars.name }}, which triggers the lazy expansion path where- // build_pipeline_command() wraps it in- // eval "$(path/to/wt step eval --shell-escape ...)"- // The absolute binary path comes from std::env::current_exe().+ // isn't on PATH in the detached background process). Step 2 references+ // {{ vars.name }}, which is expanded just-in-time by the background+ // pipeline runner reading fresh vars from git config.repo.write_test_config(r#"post-start = ["git config worktrunk.state.{{ branch }}.vars.name '{{ branch | sanitize }}-postgres'",@@ -2440,3 +2438,122 @@ fn test_user_post_start_pipeline_lazy_vars_background(repo: TestRepo) {"Background lazy step should see var set by prior step");}++#[rstest]+fn test_user_post_start_pipeline_concurrent_all_run(repo: TestRepo) {+ // Concurrent group: both commands should run and produce output.+ repo.write_test_config(+ r#"post-start = [+ { a = "echo AAA > concurrent_a.txt", b = "echo BBB > concurrent_b.txt" }+]+"#,+ );++ snapshot_switch(+ "user_post_start_pipeline_concurrent_all",+ &repo,+ &["--create", "feature"],+ );++ let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");+ let file_a = worktree_path.join("concurrent_a.txt");+ let file_b = worktree_path.join("concurrent_b.txt");+ wait_for_file_content(&file_a);+ wait_for_file_content(&file_b);++ let a = fs::read_to_string(&file_a).unwrap();+ let b = fs::read_to_string(&file_b).unwrap();+ assert!(+ a.contains("AAA"),+ "concurrent command 'a' should run, got: {a}"+ );+ assert!(+ b.contains("BBB"),+ "concurrent command 'b' should run, got: {b}"+ );+}++#[rstest]+fn test_user_post_start_pipeline_concurrent_partial_failure(repo: TestRepo) {+ // One command in a concurrent group fails. The other should still+ // complete (pipeline waits for all children), and later steps should+ // not run (group reported as failed).+ repo.write_test_config(+ r#"post-start = [+ { fail = "exit 1", ok = "echo SURVIVED > concurrent_survivor.txt" },+ "echo SHOULD_NOT_RUN > after_concurrent.txt"+]+"#,+ );++ snapshot_switch(+ "user_post_start_pipeline_concurrent_failure",+ &repo,+ &["--create", "feature"],+ );++ let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");++ // The surviving command should complete despite the sibling failing.+ let survivor = worktree_path.join("concurrent_survivor.txt");+ wait_for_file_content(&survivor);+ let content = fs::read_to_string(&survivor).unwrap();+ assert!(+ content.contains("SURVIVED"),+ "Non-failing concurrent command should still complete, got: {content}"+ );++ // The step after the failed group should NOT run.+ thread::sleep(SLEEP_FOR_ABSENCE_CHECK);+ let after = worktree_path.join("after_concurrent.txt");+ assert!(+ !after.exists(),+ "Steps after a failed concurrent group should not run"+ );+}++#[rstest]+fn test_user_post_start_pipeline_shell_escaping(repo: TestRepo) {+ // Template values containing shell metacharacters must be safely+ // escaped. Step 1 sets a var with spaces, quotes, and a dollar sign.+ // Step 2 expands it into a shell command — without shell_escape=true,+ // the value would be word-split or trigger expansion.+ repo.write_test_config(+ r#"post-start = [+ "git config worktrunk.state.{{ branch }}.vars.tricky 'hello world $HOME \"quotes\"'",+ { check = "echo {{ vars.tricky }} > escaped_output.txt" }+]+"#,+ );++ // Use foreground so we can check the result immediately.+ let mut cmd = crate::common::wt_command();+ cmd.current_dir(repo.root_path());+ cmd.env("WORKTRUNK_CONFIG_PATH", repo.test_config_path());+ cmd.args(["hook", "post-start", "--yes", "--foreground"]);++ let output = cmd.output().expect("Failed to run foreground hook");+ assert!(+ output.status.success(),+ "Hook should succeed.\nstderr: {}",+ String::from_utf8_lossy(&output.stderr),+ );++ let marker_file = repo.root_path().join("escaped_output.txt");+ assert!(marker_file.exists(), "Escaped output file should exist");++ let content = fs::read_to_string(&marker_file).unwrap().trim().to_string();+ // The value should arrive intact — not word-split, not $HOME-expanded.+ assert!(+ content.contains("hello world"),+ "Spaces should not cause word splitting, got: {content}"+ );+ assert!(+ content.contains("$HOME"),+ "$HOME should be literal, not expanded, got: {content}"+ );+ assert!(+ content.contains("\"quotes\""),+ "Quotes should survive escaping, got: {content}"+ );+}diff --git a/tests/snapshots/integration__integration_tests__eval__eval_shell_escape.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_pipeline_concurrent_all.snapsimilarity index 65%rename from tests/snapshots/integration__integration_tests__eval__eval_shell_escape.snaprename to tests/snapshots/integration__integration_tests__user_hooks__user_post_start_pipeline_concurrent_all.snapindex b05c1f54d..8826fa696 100644--- a/tests/snapshots/integration__integration_tests__eval__eval_shell_escape.snap+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_pipeline_concurrent_all.snap@@ -1,12 +1,11 @@----source: tests/integration_tests/eval.rs+source: tests/integration_tests/user_hooks.rsinfo:program: wtargs:- - step- - eval- - "--shell-escape"- - "echo {{ branch }}"+ - switch+ - "--create"+ - featureenv:APPDATA: "[TEST_CONFIG_HOME]"CLICOLOR_FORCE: "1"@@ -42,6 +41,10 @@ info:success: trueexit_code: 0----- stdout ------echo main----- stderr -----+[32m✓[39m [32mCreated branch [1mfeature[22m from [1mmain[22m and worktree @ [1m_REPO_.feature[22m[39m+[2m↳[22m [2mTo customize worktree locations, run [4mwt config create[24m[22m+[33m▲[39m [33mCannot change directory — shell integration not installed[39m+[2m↳[22m [2mTo enable automatic cd, run [4mwt config shell install[24m[22m+[36m◎[39m [36mRunning post-start: [1muser:a[22m, [1muser:b[22m @ [1m_REPO_.feature[22m[39mdiff --git a/tests/snapshots/integration__integration_tests__eval__eval_shell_escape_with_vars.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_pipeline_concurrent_failure.snapsimilarity index 65%rename from tests/snapshots/integration__integration_tests__eval__eval_shell_escape_with_vars.snaprename to tests/snapshots/integration__integration_tests__user_hooks__user_post_start_pipeline_concurrent_failure.snapindex c35c62195..9c1f8b784 100644--- a/tests/snapshots/integration__integration_tests__eval__eval_shell_escape_with_vars.snap+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_post_start_pipeline_concurrent_failure.snap@@ -1,12 +1,11 @@----source: tests/integration_tests/eval.rs+source: tests/integration_tests/user_hooks.rsinfo:program: wtargs:- - step- - eval- - "--shell-escape"- - "docker run --name {{ vars.name }}"+ - switch+ - "--create"+ - featureenv:APPDATA: "[TEST_CONFIG_HOME]"CLICOLOR_FORCE: "1"@@ -42,6 +41,10 @@ info:success: trueexit_code: 0----- stdout ------docker run --name 'my container'----- stderr -----+[32m✓[39m [32mCreated branch [1mfeature[22m from [1mmain[22m and worktree @ [1m_REPO_.feature[22m[39m+[2m↳[22m [2mTo customize worktree locations, run [4mwt config create[24m[22m+[33m▲[39m [33mCannot change directory — shell integration not installed[39m+[2m↳[22m [2mTo enable automatic cd, run [4mwt config shell install[24m[22m+[36m◎[39m [36mRunning post-start: [1mfail[22m, [1mok[22m → [1mcmd-0[22m @ [1m_REPO_.feature[22m[39m