Resilient Worktree Cleanup in Default Workflow (Issue #647)¶
Late-stage workflow steps (19c, 20b, 21) no longer hard-fail when the worktree directory has been cleaned up before the finalize phase completes.
Problem¶
Steps 19c (zero-BS verification), 20b (push-cleanup), and 21 (pr-ready) each
cd into WORKTREE_SETUP_WORKTREE_PATH using a ${VAR:?} guard. When the
worktree directory no longer exists — because the agent, a prior step, or an
external process already removed it — the cd fails and aborts the recipe with
exit code 1.
This is a false failure. By the time these steps run, the actual work (coding, testing, committing, pushing, PR creation) is complete. The remaining steps perform read-only verification and cleanup that can function from the repository root.
# Typical error before this fix:
bash: line 3: cd: /tmp/worktrees/feat-auth-1234: No such file or directory
# Recipe aborts — PR already open, branch already pushed
Solution: Resilient cd Fallback Chain¶
Steps 19c and 21 use a three-tier directory resolution instead of a hard-fail
cd. Step 20b, which commits, skips instead; see
Steps that commit never fall back.
WORKTREE_SETUP_WORKTREE_PATH (preferred — run in the worktree)
│ not found
▼
REPO_PATH (fallback — run from repo root)
│ not found
▼
$(pwd) (final — run from current directory)
Each fallback emits a WARNING to stderr so the behavior is visible in logs:
WARNING: WORKTREE_SETUP_WORKTREE_PATH=/tmp/worktrees/feat-auth-1234 not found, falling back to REPO_PATH
Which Steps Are Resilient¶
| Step | Recipe | Behavior | Rationale |
|---|---|---|---|
| 15 (design) | workflow-pr-review | Hard-fail | Needs worktree files to review |
| 16 (review) | workflow-pr-review | Hard-fail | Needs worktree files to review |
| 18c (enforce-verdict) | workflow-pr-review | Hard-fail | Verifies work artifacts in worktree |
| 19c (zero-BS) | workflow-pr-review | Resilient | Read-only checks; can use repo root |
| checkpoint-after-implementation | workflow-tdd | Hard-fail | Stages and commits; only at the top of the linked task worktree (issue #1528) |
| checkpoint-after-review-feedback | workflow-refactor-review | Hard-fail | Stages and commits; only at the top of the linked task worktree (issue #1528) |
| 15 (commit-push) | workflow-publish | Hard-fail | Stages, commits and pushes; only at the top of the linked task worktree (issue #1528) |
| 20a (artifact-guard) | workflow-finalize | Skips | Runs the runtime preflight; never in REPO_PATH (issue #1528) |
| 20b (push-cleanup) | workflow-finalize | Skips | Stages, commits and pushes; never in REPO_PATH (issue #1528) |
| 21 (pr-ready) | workflow-finalize | Resilient | Final status check; reads git remote |
Early-stage steps (15, 16, 18c) retain their ${WORKTREE_SETUP_WORKTREE_PATH:?}
hard-fail guards. These steps genuinely require the worktree to be present — the
files under review live there.
Steps that commit never fall back (issue #1528)¶
REPO_PATH is the checkout the run was launched from, often a repository's
main checkout. A step that stages or commits there can stage the run worktrees
under its worktrees/ directory. Steps 20a and 20b therefore run only in the
task worktree. When WORKTREE_SETUP_WORKTREE_PATH is empty or not a directory,
or is a directory that is not the top of a linked worktree (for example the
main checkout), each prints one warning on stderr and exits 0:
WARNING: step-20a worktree path gone; skipping the artifact guard — never runs in REPO_PATH (#1528)
WARNING: step-20a worktree path is not a linked worktree; skipping the artifact guard — never runs in REPO_PATH (#1528)
WARNING: step-20b worktree path gone; skipping cleanup commit/push — never commits in REPO_PATH (#1528)
WARNING: step-20b worktree path is not a linked worktree; skipping cleanup commit/push — never commits in REPO_PATH (#1528)
The recipe still completes, as #647 requires. See Workflow Runtime Artifacts: lifecycle integration.
The phase-1 commit sites apply the same check but stop the recipe instead of
skipping, because a skipped checkpoint or publish commit would lose work
without notice. These are checkpoint-after-implementation in
workflow-tdd.yaml, checkpoint-after-review-feedback in
workflow-refactor-review.yaml and step-15-commit-push in
workflow-publish.yaml. None of them falls back to REPO_PATH. When
WORKTREE_SETUP_WORKTREE_PATH is empty the ${WORKTREE_SETUP_WORKTREE_PATH:?}
expansion stops the step; when the cd fails the step stops; and when the
directory is not the top of a linked worktree (the main checkout, or a
subdirectory of a worktree) the step prints one error and exits 1 before it
stages anything:
ERROR: checkpoint-after-implementation: <dir> is not the top of a linked task worktree; refusing to stage or commit; never commits in REPO_PATH (#1528)
ERROR: checkpoint-after-review-feedback: <dir> is not the top of a linked task worktree; refusing to stage or commit; never commits in REPO_PATH (#1528)
ERROR: step-15-commit-push: <dir> is not the top of a linked task worktree; refusing to stage or commit; never commits in REPO_PATH (#1528)
workflow-worktree always produces a linked worktree (issue #858), so a
correctly propagated worktree path passes the check. Step 15 additionally keeps
its refusal to commit on main or master.
Fallback Pattern¶
Each step sets the local WORKTREE_SETUP_WORKTREE_PATH from
worktree_setup_path.sh,
which reads only RECIPE_VAR_worktree_setup and checks the path, so an
inherited WORKTREE_SETUP_WORKTREE_PATH is never used (issue #1561). Step 21
in workflow-finalize.yaml:
if WORKTREE_SETUP_WORKTREE_PATH="$(bash "${WT_TOOL:?worktree_setup_path.sh not found}" step-21-pr-ready)"; then cd "$WORKTREE_SETUP_WORKTREE_PATH"; elif [ -d "${REPO_PATH:-}" ]; then echo "WARNING: step-21 worktree path gone, falling back to REPO_PATH=$REPO_PATH" >&2; cd "$REPO_PATH"; else echo "WARNING: step-21 worktree path gone and REPO_PATH unset, using cwd=$(pwd)" >&2; fi
The forms below show the original #647 shape of the same fallback.
Compact form (used in workflow-pr-review.yaml which is at 399/400 LOC brick limit):
if [ -d "${WORKTREE_SETUP_WORKTREE_PATH:-}" ]; then cd "$WORKTREE_SETUP_WORKTREE_PATH"; elif [ -d "${REPO_PATH:-}" ]; then echo "WARNING: WORKTREE_SETUP_WORKTREE_PATH=${WORKTREE_SETUP_WORKTREE_PATH:-unset} not found, falling back to REPO_PATH" >&2; cd "$REPO_PATH"; else echo "WARNING: WORKTREE_SETUP_WORKTREE_PATH=${WORKTREE_SETUP_WORKTREE_PATH:-unset} not found, falling back to cwd ($(pwd))" >&2; fi
Multi-line form (for recipes with line headroom):
if [ -d "${WORKTREE_SETUP_WORKTREE_PATH:-}" ]; then
cd "$WORKTREE_SETUP_WORKTREE_PATH"
elif [ -d "${REPO_PATH:-}" ]; then
echo "WARNING: WORKTREE_SETUP_WORKTREE_PATH=${WORKTREE_SETUP_WORKTREE_PATH:-unset} not found, falling back to REPO_PATH" >&2
cd "$REPO_PATH"
else
echo "WARNING: WORKTREE_SETUP_WORKTREE_PATH=${WORKTREE_SETUP_WORKTREE_PATH:-unset} not found, falling back to cwd ($(pwd))" >&2
fi
Both forms are semantically identical. The compact form is required because
workflow-pr-review.yaml is at 399 of its 400-line brick limit. Neither form
belongs in a step that stages or commits; see
Steps that commit never fall back.
Configuration¶
No configuration required. The resilient fallback is always active for steps 19c and 21, and steps 20a and 20b always skip when the worktree is gone. There is no way to force a hard-fail on these steps — the old behavior was a bug, not a feature.
Security¶
- All variables remain double-quoted — no shell injection risk.
set -euo pipefailis preserved in all steps. Only thecdtarget selection is resilient; git operations still fail-closed on errors.- WARNING output goes to stderr only — no information disclosure beyond local paths that are already in the recipe log.
- No
|| true,>/dev/null 2>&1,set +e, or other error-suppression patterns are used. The fallback is explicit conditional logic. REPO_PATHis typically available via implicit context propagation from the parent recipe — no new inputs are added toworkflow-pr-review.yaml. The$(pwd)final fallback covers cases whereREPO_PATHis not set.
Validation¶
Integration Tests¶
default_workflow_decomposition_test.rs validates the hard-fail vs. resilient
split:
| Test target | Assertion |
|---|---|
| step-18c | Contains WORKTREE_SETUP_WORKTREE_PATH:? (hard-fail preserved) |
| step-18c | Contains set -euo pipefail |
| step-19c | Contains set -euo pipefail |
| step-19c | Contains WARNING (resilient fallback emits warning) |
| step-19c | Contains REPO_PATH (fallback target) |
| step-19c | Does NOT contain WORKTREE_SETUP_WORKTREE_PATH:? with cd (no hard-fail) |
Shell Tests¶
| Test file | What changed |
|---|---|
tests/issue_412_fail_loud_other_recipes.sh |
:? count assertions updated: finalize 2→0, pr-review 2→1 |
tests/issue_414_fail_loud_phase_bricks.sh |
Section B counts, Section C loop membership, Section F resilience assertions updated |
Running the Tests¶
# Rust integration tests
cargo test default_workflow_decomposition -- --nocapture
# Shell assertion tests
bash tests/issue_412_fail_loud_other_recipes.sh
bash tests/issue_414_fail_loud_phase_bricks.sh
Related¶
- Recipe Resilience — Branch sanitization, worktree bases, and late-stage resilience
- worktree_setup Propagation — How
WORKTREE_SETUP_WORKTREE_PATHflows through recipes - Troubleshoot Worktree — Includes entry for recipe abort after early cleanup
- P1 Workflow Reliability Fixes — Prior reliability improvements