Skip to content

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

feat: simplify exit code handling and capture

Maximilian Roos9b691bf7f32e
diff --git a/templates/posix_directives.sh b/templates/posix_directives.sh
index 0a3abf69d..f2f754270 100644
--- a/templates/posix_directives.sh
+++ b/templates/posix_directives.sh
@@ -1,19 +1,19 @@
# 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.
+# Streams worktrunk's NUL-delimited output in real-time via FIFO while keeping
+# stderr attached to the TTY for child processes (colors, progress bars).
+#
# Note: Named without leading underscore to avoid being filtered by shell
# snapshot systems (e.g., Claude Code) that exclude private functions.
# Note: Uses ${_WORKTRUNK_CMD:-{{ cmd_prefix }}} fallback because shell snapshot
# systems may capture functions but not environment variables.
wt_exec() {
- local exec_cmd="" chunk="" exit_code=0 tmp_dir="" exit_file="" fifo_path="" runner_pid=""
+ local exec_cmd="" chunk="" exit_code=0 tmp_dir="" fifo_path="" runner_pid=""
+ # Create temp directory with FIFO for streaming output
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
@@ -22,44 +22,38 @@ wt_exec() {
return 1
fi
- # Run command in background to enable real-time streaming via FIFO.
- (
- command "${_WORKTRUNK_CMD:-{{ cmd_prefix }}}" "$@"
- printf '%s' "$?" >"$exit_file"
- ) >"$fifo_path" &
+ # Run worktrunk in background, piping stdout to FIFO
+ # (stderr stays attached to TTY for child process colors/progress)
+ command "${_WORKTRUNK_CMD:-{{ cmd_prefix }}}" "$@" >"$fifo_path" &
runner_pid=$!
- # Remove job from shell's job table to prevent job control notifications
- disown "$runner_pid" 2>/dev/null || true
- # Read directives as they arrive; keep looping even if the final chunk isn't NUL-terminated.
+ # Parse directives as they stream in
while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
+ # Directory change directive
local path="${chunk#__WORKTRUNK_CD__}"
\cd "$path"
elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
+ # Command execution directive (deferred until after worktrunk exits)
exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
else
- # Regular output - print it with newline (ignore empty chunks)
+ # Regular output - print to stdout
[[ -n "$chunk" ]] && 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
+ # Wait for worktrunk to complete and capture its exit code
+ wait "$runner_pid" >/dev/null 2>&1 || exit_code=$?
- /bin/rm -f "$exit_file" "$fifo_path"
+ # Cleanup
+ /bin/rm -f "$fifo_path"
/bin/rmdir "$tmp_dir" 2>/dev/null || true
+ # Execute deferred command if specified (its exit code takes precedence)
if [[ -n "$exec_cmd" ]]; then
eval "$exec_cmd"
exit_code=$?
fi
- return "$exit_code"
+ return "${exit_code:-0}"
}
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 906fef54d..90f22dfbe 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/.tmp2HZYvq/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpcSRq4c/test-config.toml
---
success: true
exit_code: 0
@@ -30,21 +30,21 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
_WORKTRUNK_CMD="${WORKTRUNK_BIN:-wt}"
# 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.
+# Streams worktrunk's NUL-delimited output in real-time via FIFO while keeping
+# stderr attached to the TTY for child processes (colors, progress bars).
+#
# Note: Named without leading underscore to avoid being filtered by shell
# snapshot systems (e.g., Claude Code) that exclude private functions.
# Note: Uses ${_WORKTRUNK_CMD:-wt} fallback because shell snapshot
# systems may capture functions but not environment variables.
wt_exec() {
- local exec_cmd="" chunk="" exit_code=0 tmp_dir="" exit_file="" fifo_path="" runner_pid=""
+ local exec_cmd="" chunk="" exit_code=0 tmp_dir="" fifo_path="" runner_pid=""
+ # Create temp directory with FIFO for streaming output
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
@@ -53,46 +53,40 @@ wt_exec() {
return 1
fi
- # Run command in background to enable real-time streaming via FIFO.
- (
- command "${_WORKTRUNK_CMD:-wt}" "$@"
- printf '%s' "$?" >"$exit_file"
- ) >"$fifo_path" &
+ # Run worktrunk in background, piping stdout to FIFO
+ # (stderr stays attached to TTY for child process colors/progress)
+ command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" &
runner_pid=$!
- # Remove job from shell's job table to prevent job control notifications
- disown "$runner_pid" 2>/dev/null || true
- # Read directives as they arrive; keep looping even if the final chunk isn't NUL-terminated.
+ # Parse directives as they stream in
while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
+ # Directory change directive
local path="${chunk#__WORKTRUNK_CD__}"
\cd "$path"
elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
+ # Command execution directive (deferred until after worktrunk exits)
exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
else
- # Regular output - print it with newline (ignore empty chunks)
+ # Regular output - print to stdout
[[ -n "$chunk" ]] && 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
+ # Wait for worktrunk to complete and capture its exit code
+ wait "$runner_pid" >/dev/null 2>&1 || exit_code=$?
- /bin/rm -f "$exit_file" "$fifo_path"
+ # Cleanup
+ /bin/rm -f "$fifo_path"
/bin/rmdir "$tmp_dir" 2>/dev/null || true
+ # Execute deferred command if specified (its exit code takes precedence)
if [[ -n "$exec_cmd" ]]; then
eval "$exec_cmd"
exit_code=$?
fi
- return "$exit_code"
+ return "${exit_code:-0}"
}
# Override wt command to add --internal flag
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index 0f705eb30..05d2222d8 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/.tmp5Cg1aN/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpCLX2oz/test-config.toml
---
success: true
exit_code: 0
@@ -30,21 +30,21 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then
_WORKTRUNK_CMD="${WORKTRUNK_BIN:-wt}"
# 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.
+# Streams worktrunk's NUL-delimited output in real-time via FIFO while keeping
+# stderr attached to the TTY for child processes (colors, progress bars).
+#
# Note: Named without leading underscore to avoid being filtered by shell
# snapshot systems (e.g., Claude Code) that exclude private functions.
# Note: Uses ${_WORKTRUNK_CMD:-wt} fallback because shell snapshot
# systems may capture functions but not environment variables.
wt_exec() {
- local exec_cmd="" chunk="" exit_code=0 tmp_dir="" exit_file="" fifo_path="" runner_pid=""
+ local exec_cmd="" chunk="" exit_code=0 tmp_dir="" fifo_path="" runner_pid=""
+ # Create temp directory with FIFO for streaming output
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
@@ -53,46 +53,40 @@ wt_exec() {
return 1
fi
- # Run command in background to enable real-time streaming via FIFO.
- (
- command "${_WORKTRUNK_CMD:-wt}" "$@"
- printf '%s' "$?" >"$exit_file"
- ) >"$fifo_path" &
+ # Run worktrunk in background, piping stdout to FIFO
+ # (stderr stays attached to TTY for child process colors/progress)
+ command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" &
runner_pid=$!
- # Remove job from shell's job table to prevent job control notifications
- disown "$runner_pid" 2>/dev/null || true
- # Read directives as they arrive; keep looping even if the final chunk isn't NUL-terminated.
+ # Parse directives as they stream in
while IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do
if [[ "$chunk" == __WORKTRUNK_CD__* ]]; then
+ # Directory change directive
local path="${chunk#__WORKTRUNK_CD__}"
\cd "$path"
elif [[ "$chunk" == __WORKTRUNK_EXEC__* ]]; then
+ # Command execution directive (deferred until after worktrunk exits)
exec_cmd="${chunk#__WORKTRUNK_EXEC__}"
else
- # Regular output - print it with newline (ignore empty chunks)
+ # Regular output - print to stdout
[[ -n "$chunk" ]] && 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
+ # Wait for worktrunk to complete and capture its exit code
+ wait "$runner_pid" >/dev/null 2>&1 || exit_code=$?
- /bin/rm -f "$exit_file" "$fifo_path"
+ # Cleanup
+ /bin/rm -f "$fifo_path"
/bin/rmdir "$tmp_dir" 2>/dev/null || true
+ # Execute deferred command if specified (its exit code takes precedence)
if [[ -n "$exec_cmd" ]]; then
eval "$exec_cmd"
exit_code=$?
fi
- return "$exit_code"
+ return "${exit_code:-0}"
}
# Override wt command to add --internal flag