Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor: Extract shared POSIX directive parser for bash/zsh

Maximilian Roos3c02157151d2
diff --git a/src/shell.rs b/src/shell.rs
index add0e0ae2..34dfe9ae6 100644
--- a/src/shell.rs
+++ b/src/shell.rs
@@ -1,6 +1,8 @@
use askama::Template;
use std::path::PathBuf;
+const POSIX_DIRECTIVE_SHIM: &str = include_str!("../templates/posix_directives.sh");
+
/// Supported shells
#[derive(Debug, Clone, Copy, clap::ValueEnum, strum::Display, strum::EnumString)]
#[strum(serialize_all = "lowercase", ascii_case_insensitive)]
@@ -301,12 +303,14 @@ impl ShellInit {
let template = BashTemplate {
shell_name: self.shell.to_string(),
cmd_prefix: &self.cmd_prefix,
+ posix_shim: POSIX_DIRECTIVE_SHIM,
};
template.render()
}
Shell::Zsh => {
let template = ZshTemplate {
cmd_prefix: &self.cmd_prefix,
+ posix_shim: POSIX_DIRECTIVE_SHIM,
};
template.render()
}
@@ -353,6 +357,7 @@ impl ShellInit {
struct BashTemplate<'a> {
shell_name: String,
cmd_prefix: &'a str,
+ posix_shim: &'a str,
}
/// Zsh shell template
@@ -360,6 +365,7 @@ struct BashTemplate<'a> {
#[template(path = "zsh.zsh", escape = "none")]
struct ZshTemplate<'a> {
cmd_prefix: &'a str,
+ posix_shim: &'a str,
}
/// Fish shell template
diff --git a/templates/bash.sh b/templates/bash.sh
index 5453420eb..15c14ed7d 100644
--- a/templates/bash.sh
+++ b/templates/bash.sh
@@ -6,49 +6,7 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];
# This allows testing development builds: export WORKTRUNK_BIN=./target/debug/{{ cmd_prefix }}
_WORKTRUNK_CMD="${WORKTRUNK_BIN:-{{ cmd_prefix }}}"
- # Helper function to parse wt output and handle directives
- # Directives are NUL-terminated to support multi-line commands
- _wt_exec() {
- local exec_cmd="" chunk exit_code
- local exit_code_file
- exit_code_file=$(mktemp) || { echo "Failed to create temp file" >&2; return 1; }
-
- # Parse stdout for directives (NUL-delimited)
- # Let stderr pass through to terminal (preserves TTY for color detection)
- # Write exit code to temp file since it can't be captured from process substitution
- # The || [[ -n "$chunk" ]] handles non-NUL-terminated output (e.g., error messages)
- while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
- if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
- # CD directive - extract path and change directory
- local path="${chunk#__WORKTRUNK_CD__}"
- \cd "$path"
- elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
- # EXEC directive - extract command (may contain newlines)
- exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
- else
- # Regular output - print it with newline
- printf '%s\n' "$chunk"
- fi
- done < <(command "$_WORKTRUNK_CMD" "$@"; echo "$?" > "$exit_code_file")
-
- # Read exit code from temp file
- exit_code=$(cat "$exit_code_file" 2>/dev/null || echo 0)
-
- # Cleanup temp file
- \rm -f "$exit_code_file"
-
- # Execute command if one was specified
- # Security: Command is user-provided from -x flag; eval is intentional.
- # NUL-termination allows multi-line commands.
- # Exit code semantics: If wt fails, returns wt's exit code (command never executes).
- # If wt succeeds but command fails, returns the command's exit code.
- if [[ -n "$exec_cmd" ]]; then
- eval "$exec_cmd"
- exit_code=$?
- fi
-
- return $exit_code
- }
+{{ posix_shim }}
# Override {{ cmd_prefix }} command to add --internal flag
{{ cmd_prefix }}() {
diff --git a/templates/posix_directives.sh b/templates/posix_directives.sh
new file mode 100644
index 000000000..57a7104ac
--- /dev/null
+++ b/templates/posix_directives.sh
@@ -0,0 +1,57 @@
+# Shared directive parser for POSIX shells (bash, zsh, oil).
+# Reads worktrunk's NUL-delimited output in real-time via a FIFO so progress
+# and hint messages stream immediately while keeping stderr attached to the TTY.
+_wt_exec() {
+ local exec_cmd="" chunk="" exit_code=0 tmp_dir="" exit_file="" fifo_path="" runner_pid=""
+
+ tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/wt.XXXXXX") || {
+ echo "Failed to create temp directory for worktrunk shim" >&2
+ return 1
+ }
+
+ exit_file="$tmp_dir/exit-code"
+ fifo_path="$tmp_dir/stdout.fifo"
+
+ if ! mkfifo "$fifo_path"; then
+ echo "Failed to create FIFO for worktrunk shim" >&2
+ /bin/rm -rf "$tmp_dir"
+ return 1
+ fi
+
+ (
+ command "$_WORKTRUNK_CMD" "$@"
+ printf '%s' "$?" >"$exit_file"
+ ) >"$fifo_path" &
+ runner_pid=$!
+
+ # Read directives as they arrive; keep looping even if the final chunk isn't NUL-terminated.
+ while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
+ if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
+ local path="${chunk#__WORKTRUNK_CD__}"
+ \cd "$path"
+ elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
+ exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
+ else
+ printf '%s\n' "$chunk"
+ fi
+ done <"$fifo_path"
+
+ # Ensure the background runner has exited before reading artifacts.
+ if [[ -n "$runner_pid" ]]; then
+ wait "$runner_pid" >/dev/null 2>&1 || true
+ fi
+
+ if [[ -f "$exit_file" ]]; then
+ read -r exit_code < "$exit_file"
+ fi
+
+ /bin/rm -f "$exit_file" "$fifo_path"
+ /bin/rmdir "$tmp_dir" 2>/dev/null || true
+
+ if [[ -n "$exec_cmd" ]]; then
+ eval "$exec_cmd"
+ exit_code=$?
+ fi
+
+ return "$exit_code"
+}
diff --git a/templates/zsh.zsh b/templates/zsh.zsh
index 53a11d055..921b6ba6f 100644
--- a/templates/zsh.zsh
+++ b/templates/zsh.zsh
@@ -6,54 +6,7 @@ if command -v {{ cmd_prefix }} >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]];
# This allows testing development builds: export WORKTRUNK_BIN=./target/debug/{{ cmd_prefix }}
_WORKTRUNK_CMD="${WORKTRUNK_BIN:-{{ cmd_prefix }}}"
- # Helper function to parse wt output and handle directives
- # Directives are NUL-terminated to support multi-line commands
- _wt_exec() {
- local exec_cmd="" chunk exit_code
- local exit_code_file stdout_file
- exit_code_file=$(mktemp) || { echo "Failed to create temp file" >&2; return 1; }
- stdout_file=$(mktemp) || { /bin/rm -f "$exit_code_file" 2>/dev/null; echo "Failed to create temp file" >&2; return 1; }
-
- # Run command and capture output to file to avoid process substitution issues
- # Process substitution in zsh can hang in non-interactive mode
- # Let stderr pass through to terminal (preserves TTY for color detection)
- command "$_WORKTRUNK_CMD" "$@" > "$stdout_file"
- echo "$?" > "$exit_code_file"
-
- # Parse stdout for directives (NUL-delimited)
- # The || [[ -n "$chunk" ]] handles non-NUL-terminated output (e.g., error messages)
- while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
- if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
- # CD directive - extract path and change directory
- local path="${chunk#__WORKTRUNK_CD__}"
- \cd "$path"
- elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
- # EXEC directive - extract command (may contain newlines)
- exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
- else
- # Regular output - print it with newline
- printf '%s\n' "$chunk"
- fi
- done < "$stdout_file"
-
- # Read exit code from temp file
- exit_code=$(cat "$exit_code_file" 2>/dev/null || echo 0)
-
- # Cleanup temp files (use absolute path for --no-rcs compatibility)
- /bin/rm -f "$exit_code_file" "$stdout_file" 2>/dev/null || true
-
- # Execute command if one was specified
- # Security: Command is user-provided from -x flag; eval is intentional.
- # NUL-termination allows multi-line commands.
- # Exit code semantics: If wt fails, returns wt's exit code (command never executes).
- # If wt succeeds but command fails, returns the command's exit code.
- if [[ -n "$exec_cmd" ]]; then
- eval "$exec_cmd"
- exit_code=$?
- fi
-
- return $exit_code
- }
+{{ posix_shim }}
# Override {{ cmd_prefix }} command to add --internal flag
{{ cmd_prefix }}() {
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_zsh.snap
index 07fba04a8..83da391ca 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_mixed_stdout_stderr_zsh.snap
@@ -2,6 +2,9 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized()
---
+πŸ”„ Running pre-merge check1:
+  [WORKSPACE]/tests/fixtures/mixed-output.sh check1 3
+
[check1] stdout line 1
[check1] stderr line 1
[check1] stdout line 2
@@ -10,6 +13,10 @@ expression: output.normalized()
[check1] stderr line 3
[check1] stdout complete
[check1] stderr complete
+
+πŸ”„ Running pre-merge check2:
+  [WORKSPACE]/tests/fixtures/mixed-output.sh check2 3
+
[check2] stdout line 1
[check2] stderr line 1
[check2] stdout line 2
@@ -18,13 +25,6 @@ expression: output.normalized()
[check2] stderr line 3
[check2] stdout complete
[check2] stderr complete
-πŸ”„ Running pre-merge check1:
-  [WORKSPACE]/tests/fixtures/mixed-output.sh check1 3
-
-
-πŸ”„ Running pre-merge check2:
-  [WORKSPACE]/tests/fixtures/mixed-output.sh check2 3
-
πŸ”„ Merging 1 commit to main @ [HASH] (no commit/squash/rebase needed)
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
index 93c1a6485..218fd4dae 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_failure_zsh.snap
@@ -2,15 +2,15 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized()
---
-βœ“ Code formatting check passed
-βœ— Test suite failed: 3 tests failing
πŸ”„ Running pre-merge format:
  echo 'βœ“ Code formatting check passed'
+βœ“ Code formatting check passed
πŸ”„ Running pre-merge test:
  echo 'βœ— Test suite failed: 3 tests failing' && exit 1
+βœ— Test suite failed: 3 tests failing
❌ pre-merge command failed: test: exit status: 1
πŸ’‘ Use --no-verify to skip pre-merge commands
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap
index 448ecefc2..238fc6b08 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__merge_with_pre_merge_success_zsh.snap
@@ -2,20 +2,20 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized()
---
-βœ“ Code formatting check passed
-βœ“ Linting passed - no warnings
-βœ“ All 47 tests passed in 2.3s
πŸ”„ Running pre-merge format:
  echo 'βœ“ Code formatting check passed'
+βœ“ Code formatting check passed
πŸ”„ Running pre-merge lint:
  echo 'βœ“ Linting passed - no warnings'
+βœ“ Linting passed - no warnings
πŸ”„ Running pre-merge test:
  echo 'βœ“ All 47 tests passed in 2.3s'
+βœ“ All 47 tests passed in 2.3s
πŸ”„ Merging 1 commit to main @ [HASH] (no commit/squash/rebase needed)
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap
index 0a9640851..8be679a0d 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__switch_with_hooks_zsh.snap
@@ -2,15 +2,15 @@
source: tests/integration_tests/shell_wrapper.rs
expression: output.normalized()
---
-Installing dependencies...
-Building project...
πŸ”„ Running post-create:
  echo 'Installing dependencies...'
+Installing dependencies...
πŸ”„ Running post-create:
  echo 'Building project...'
+Building project...
βœ… Created new worktree for feature-hooks from main at [TMPDIR]/test-repo.feature-hooks
πŸ”„ Running post-start server:
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index b10285728..bd51bf447 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp4sEkZq/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpiqC07O/test-config.toml
---
success: true
exit_code: 0
@@ -29,49 +29,64 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
# This allows testing development builds: export WORKTRUNK_BIN=./target/debug/wt
_WORKTRUNK_CMD="${WORKTRUNK_BIN:-wt}"
- # Helper function to parse wt output and handle directives
- # Directives are NUL-terminated to support multi-line commands
- _wt_exec() {
- local exec_cmd="" chunk exit_code
- local exit_code_file
- exit_code_file=$(mktemp) || { echo "Failed to create temp file" >&2; return 1; }
+# Shared directive parser for POSIX shells (bash, zsh, oil).
+# Reads worktrunk's NUL-delimited output in real-time via a FIFO so progress
+# and hint messages stream immediately while keeping stderr attached to the TTY.
+_wt_exec() {
+ local exec_cmd="" chunk="" exit_code=0 tmp_dir="" exit_file="" fifo_path="" runner_pid=""
- # Parse stdout for directives (NUL-delimited)
- # Let stderr pass through to terminal (preserves TTY for color detection)
- # Write exit code to temp file since it can't be captured from process substitution
- # The || [[ -n "$chunk" ]] handles non-NUL-terminated output (e.g., error messages)
- while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
- if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
- # CD directive - extract path and change directory
- local path="${chunk#__WORKTRUNK_CD__}"
- \cd "$path"
- elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
- # EXEC directive - extract command (may contain newlines)
- exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
- else
- # Regular output - print it with newline
- printf '%s\n' "$chunk"
- fi
- done < <(command "$_WORKTRUNK_CMD" "$@"; echo "$?" > "$exit_code_file")
+ tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/wt.XXXXXX") || {
+ echo "Failed to create temp directory for worktrunk shim" >&2
+ return 1
+ }
+
+ exit_file="$tmp_dir/exit-code"
+ fifo_path="$tmp_dir/stdout.fifo"
- # Read exit code from temp file
- exit_code=$(cat "$exit_code_file" 2>/dev/null || echo 0)
+ if ! mkfifo "$fifo_path"; then
+ echo "Failed to create FIFO for worktrunk shim" >&2
+ /bin/rm -rf "$tmp_dir"
+ return 1
+ fi
- # Cleanup temp file
- \rm -f "$exit_code_file"
+ (
+ command "$_WORKTRUNK_CMD" "$@"
+ printf '%s' "$?" >"$exit_file"
+ ) >"$fifo_path" &
+ runner_pid=$!
- # Execute command if one was specified
- # Security: Command is user-provided from -x flag; eval is intentional.
- # NUL-termination allows multi-line commands.
- # Exit code semantics: If wt fails, returns wt's exit code (command never executes).
- # If wt succeeds but command fails, returns the command's exit code.
- if [[ -n "$exec_cmd" ]]; then
- eval "$exec_cmd"
- exit_code=$?
+ # Read directives as they arrive; keep looping even if the final chunk isn't NUL-terminated.
+ while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
+ if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
+ local path="${chunk#__WORKTRUNK_CD__}"
+ \cd "$path"
+ elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
+ exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
+ else
+ printf '%s\n' "$chunk"
fi
+ done <"$fifo_path"
+
+ # Ensure the background runner has exited before reading artifacts.
+ if [[ -n "$runner_pid" ]]; then
+ wait "$runner_pid" >/dev/null 2>&1 || true
+ fi
+
+ if [[ -f "$exit_file" ]]; then
+ read -r exit_code < "$exit_file"
+ fi
+
+ /bin/rm -f "$exit_file" "$fifo_path"
+ /bin/rmdir "$tmp_dir" 2>/dev/null || true
+
+ if [[ -n "$exec_cmd" ]]; then
+ eval "$exec_cmd"
+ exit_code=$?
+ fi
+
+ return "$exit_code"
+}
- return $exit_code
- }
# Override wt command to add --internal flag
wt() {
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 194584f96..4e8c8b8a6 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpIZkO2j/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpPLmErG/test-config.toml
---
success: true
exit_code: 0
@@ -29,55 +29,65 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
# This allows testing development builds: export WORKTRUNK_BIN=./target/debug/wt
_WORKTRUNK_CMD="${WORKTRUNK_BIN:-wt}"
- # Helper function to parse wt output and handle directives
- # Directives are NUL-terminated to support multi-line commands
- _wt_exec() {
- local exec_cmd="" chunk exit_code
- local exit_code_file stdout_file
- exit_code_file=$(mktemp) || { echo "Failed to create temp file" >&2; return 1; }
- stdout_file=$(mktemp) || { /bin/rm -f "$exit_code_file" 2>/dev/null; echo "Failed to create temp file" >&2; return 1; }
-
- # Run command and capture output to file to avoid process substitution issues
- # Process substitution in zsh can hang in non-interactive mode
- # Let stderr pass through to terminal (preserves TTY for color detection)
- command "$_WORKTRUNK_CMD" "$@" > "$stdout_file"
- echo "$?" > "$exit_code_file"
-
- # Parse stdout for directives (NUL-delimited)
- # The || [[ -n "$chunk" ]] handles non-NUL-terminated output (e.g., error messages)
- while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
- if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
- # CD directive - extract path and change directory
- local path="${chunk#__WORKTRUNK_CD__}"
- \cd "$path"
- elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
- # EXEC directive - extract command (may contain newlines)
- exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
- else
- # Regular output - print it with newline
- printf '%s\n' "$chunk"
- fi
- done < "$stdout_file"
-
- # Read exit code from temp file
- exit_code=$(cat "$exit_code_file" 2>/dev/null || echo 0)
-
- # Cleanup temp files (use absolute path for --no-rcs compatibility)
- /bin/rm -f "$exit_code_file" "$stdout_file" 2>/dev/null || true
-
- # Execute command if one was specified
- # Security: Command is user-provided from -x flag; eval is intentional.
- # NUL-termination allows multi-line commands.
- # Exit code semantics: If wt fails, returns wt's exit code (command never executes).
- # If wt succeeds but command fails, returns the command's exit code.
- if [[ -n "$exec_cmd" ]]; then
- eval "$exec_cmd"
- exit_code=$?
- fi
+# Shared directive parser for POSIX shells (bash, zsh, oil).
+# Reads worktrunk's NUL-delimited output in real-time via a FIFO so progress
+# and hint messages stream immediately while keeping stderr attached to the TTY.
+_wt_exec() {
+ local exec_cmd="" chunk="" exit_code=0 tmp_dir="" exit_file="" fifo_path="" runner_pid=""
- return $exit_code
+ tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/wt.XXXXXX") || {
+ echo "Failed to create temp directory for worktrunk shim" >&2
+ return 1
}
+ exit_file="$tmp_dir/exit-code"
+ fifo_path="$tmp_dir/stdout.fifo"
+
+ if ! mkfifo "$fifo_path"; then
+ echo "Failed to create FIFO for worktrunk shim" >&2
+ /bin/rm -rf "$tmp_dir"
+ return 1
+ fi
+
+ (
+ command "$_WORKTRUNK_CMD" "$@"
+ printf '%s' "$?" >"$exit_file"
+ ) >"$fifo_path" &
+ runner_pid=$!
+
+ # Read directives as they arrive; keep looping even if the final chunk isn't NUL-terminated.
+ while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
+ if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
+ local path="${chunk#__WORKTRUNK_CD__}"
+ \cd "$path"
+ elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
+ exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
+ else
+ printf '%s\n' "$chunk"
+ fi
+ done <"$fifo_path"
+
+ # Ensure the background runner has exited before reading artifacts.
+ if [[ -n "$runner_pid" ]]; then
+ wait "$runner_pid" >/dev/null 2>&1 || true
+ fi
+
+ if [[ -f "$exit_file" ]]; then
+ read -r exit_code < "$exit_file"
+ fi
+
+ /bin/rm -f "$exit_file" "$fifo_path"
+ /bin/rmdir "$tmp_dir" 2>/dev/null || true
+
+ if [[ -n "$exec_cmd" ]]; then
+ eval "$exec_cmd"
+ exit_code=$?
+ fi
+
+ return "$exit_code"
+}
+
+
# Override wt command to add --internal flag
wt() {
local use_source=false