Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference/FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ GENERATED , do not hand-edit. Regenerate: `bash lib/registry/feature-registry.sh
| `/kit:verify` | `[H/I]` | Re-run the test levels (kit:task-verifier + kit:integration-verifier + kit:acceptance-verifier, plus kit:system-verifier only with --full) … | SPEC-002, SPEC-003, SPEC-006 +57 | test-board-digest.sh, test-board-hermes.sh, test-board-lock-required.sh +35 |
| `/kit:visual-team` | `[H/I]` | Parallel multi-lens critique of a visual/UI design. Dispatches 5 design lenses, merges findings, reports a verdict. Report-only, downstream… | SPEC-016, SPEC-018, SPEC-019 +12 | test-command-emit-sweep.sh, test-meta-review-verifiers.sh, test-meta-spec-depth.sh +1 |
| `/kit:wayfind` | `[H]` | Plan a chunk of work too big for one agent session as a shared decision map: map.md + typed decision tickets in the mega-goal folder, resol… | SPEC-206, SPEC-207, SPEC-217 +2 | - |
| `/kit:wrap` | `[H/I]` | The session-scoped landing step after ship: flips board rows, merges green PRs, checks deploys, tidies worktrees, prints the skim-first rep… | SPEC-020, SPEC-060, SPEC-072 +75 | test-bash32-parse.sh, test-bin-forwarders.sh, test-board-lock-required.sh +56 |
| `/kit:wrap` | `[H/I]` | The session-scoped landing step after ship: flips board rows, merges green PRs, checks deploys, tidies worktrees, prints the skim-first rep… | SPEC-020, SPEC-060, SPEC-072 +75 | test-bash32-parse.sh, test-bin-forwarders.sh, test-board-lock-required.sh +57 |

## Agents

Expand Down
154 changes: 154 additions & 0 deletions docs/verification/worktree-removers-ignored-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Verification: worktree-removers-ignored-guard

Headless shell library fix (off `origin/master`): no visible UI surface.

Closes the remaining user-worktree removal paths outside `lib/wrap/wrap-apply.sh` that still called `git worktree remove --force` / `-f -f` without checking for unique ignored files, hidden index edits, initialized submodules, or uncommitted work:

- `lib/goal/wt.sh` (`close`)
- `lib/queue/orchestrate.sh` (`_wave_worktree`)

`lib/adapters/hermes/board-writeback.sh` stays identical to `origin/master` by design: writeback creates and owns its throwaway scratch worktree on the fixed `chore/board-sync` branch for only its own staged edit, and refusing removal there wedges later writeback runs on `chore/board-sync`.

## Root cause and fix

1. Extracted the worktree safety helpers (`_san`, `_ignored_match`, `_ARCHIVE_IGNORED_REGENERABLE`, `_wt_status_raw`, `_wt_status`, `_wt_hidden_edits`, `_wt_init_submodule`, `_wt_synced_copy`, `_wt_ignored_blockers`, `_wt_ignored_guard`) out of `lib/wrap/wrap-common.sh` and `lib/wrap/wrap-apply.sh` into one shared file: `lib/wrap/wt-guard.sh`, safe under both `set -uo pipefail` and `set -euo pipefail` and bash 3.2+. Added `_wt_main_checkout` and `_wt_remove_guard` (two-pass status check with `cmp -s` stability verification right before `git worktree remove`).
2. `lib/wrap/wrap-common.sh` and `lib/wrap/wrap-apply.sh` source `lib/wrap/wt-guard.sh` instead of keeping duplicate definitions.
3. `lib/goal/wt.sh`: sources `lib/wrap/wt-guard.sh`, runs `_wt_remove_guard` before removing `$wt`, checks `git worktree remove -f -f "$wt"` exit status, and releases the registry claim (`goal-registry.sh release`) only after the worktree removal succeeds. Any guard or remove failure exits 1 with the reason on stderr and leaves the registry claim, worktree, local branch, and remote branch untouched.
4. `lib/queue/orchestrate.sh`: sources `lib/wrap/wt-guard.sh` and runs `_wt_remove_guard` inside `_wave_worktree` before removing any non-reusable worktree. Refusal returns 1 with the guard reason on stderr and leaves the worktree intact. Fixed 7 pre-existing shellcheck `-S warning` lines in `orchestrate.sh` (`SC1010` quoted `"done"` args, `SC2097`/`SC2098` local capture before subshell).

## Baseline red runs

### 1. Missing worktree guard on unmodified origin/master (e75b5b2d)

```text
Command: bash tests/test-worktree-removers-ignored.sh
Exit: 1
Output:
=== A. lib/goal/wt.sh close ===
FAIL wt env: refuses (nonzero)
FAIL wt env: says why (.env)
FAIL wt env: kept file exists
...
=== B. lib/queue/orchestrate.sh (_wave_worktree) ===
FAIL wave env: refuses (nonzero)
FAIL wave env: says why (.env)
FAIL wave env: .env kept
...
test-worktree-removers-ignored: 28 passed, 92 FAILED of 120
Verdict: RED (reproduced on unmodified origin/master)
```

### 2. Early registry-claim release before failed worktree remove (review B2, red before fix)

```text
Command: /bin/bash tests/test-worktree-removers-ignored.sh
Exit: 1
Output:
PASS wt rmfail: setup claim exists
PASS wt rmfail: refuses (nonzero)
PASS wt rmfail: says worktree remove failed
FAIL wt rmfail: registry claim survives
PASS wt rmfail: kept file exists
PASS wt rmfail: worktree exists
PASS wt rmfail: local branch exists
PASS wt rmfail: origin branch exists
...
test-worktree-removers-ignored: 106 passed, 1 FAILED of 107
Verdict: RED (claim was released before git worktree remove failed)
```

## Green runs

### 1. Shellcheck across all touched shell scripts

```text
Command: shellcheck --norc -S warning lib/wrap/wt-guard.sh lib/goal/wt.sh lib/queue/orchestrate.sh tests/test-worktree-removers-ignored.sh
Exit: 0
Output:
(no warnings or errors)
Verdict: PASS
```

### 2. New remover suite, wrap-apply-ignored, orchestrate, and adapter suites on macOS bash 3.2 and bash 5

```text
Command: /bin/bash tests/test-worktree-removers-ignored.sh && bash tests/test-worktree-removers-ignored.sh && /bin/bash tests/test-wrap-apply-ignored.sh && bash tests/test-wrap-apply-ignored.sh && /bin/bash lib/adapters/tests/test-adapters.sh && bash lib/adapters/tests/test-adapters.sh && /bin/bash tests/test-orchestrate-hardening.sh && /bin/bash tests/test-wave-converge.sh
Exit: 0
Output:
test-worktree-removers-ignored: all 107 passed
test-worktree-removers-ignored: all 107 passed
test-wrap-apply-ignored: all 211 passed
test-wrap-apply-ignored: all 211 passed
=== 218 passed, 0 failed ===
=== 218 passed, 0 failed ===
=== 13/13 passed, 0 failed ===
All wave-converge tests passed.
Verdict: PASS
```

### 3. Feature registry freshness

```text
Command: bash lib/registry/feature-registry.sh check
Exit: 0
Output:
feature-registry: docs/reference/FEATURES.md is fresh
Verdict: PASS
```

### 4. Diff-scoped affected suites (`run-all.sh --changed --time`, all 90/90 green)

```text
Command: RUN_ALL_TIMEOUT_SECS=450 bash tests/run-all.sh --changed --time
Exit: 0
Output:
run-all: --changed against e75b5b2d: 9 changed files -> 91 suites (84 named, the rest always-on)
test-harness-dispatch skip (needs opencode)
run-all: 90 suites, 4 at a time, 1 serial
test-board-writeback ok (13s)
test-branch-merged-callers ok (37s)
test-kit-contract ok (5s)
test-meta-docs-registry ok (15s)
test-no-personal-paths ok (1s)
test-no-scattered-ids ok (2s)
test-operator-pin ok (6s)
test-orchestrate ok (109s)
test-shared-lookups ok (1s)
test-worktree-removers-ignored ok (16s)
test-wrap-apply-ignored ok (47s)
test-wrap-apply ok (226s)
test-wrap-sweep ok (48s)
lib-adapters-test-adapters ok (33s)
run-all: all 90 suites passed, 1 skipped for missing tooling
Verdict: PASS
```

## Negative control

Four planted faults tested in isolated scratch copies against `tests/test-worktree-removers-ignored.sh` under `/bin/bash` (3.2): bypassing `_wt_remove_guard` in `wt.sh` (`m1-wt-bypass`) and `orchestrate.sh` (`m2-orch-bypass`), moving `goal-registry.sh release` back before `git worktree remove` in `wt.sh` (`m3-wt-early-release`), and dropping the second-pass `cmp -s "$raw1" "$raw2"` check in `lib/wrap/wt-guard.sh` (`m4-drop-2nd-pass`). Every mutant turned the suite red:

```text
Command: python3 <scratch-mutant-runner> # plants 1 fault per scratch copy and runs /bin/bash tests/test-worktree-removers-ignored.sh
Exit: 1
Output:
MUTANT m1-wt-bypass exit=1 test-worktree-removers-ignored: 53 passed, 54 FAILED of 107
MUTANT m2-orch-bypass exit=1 test-worktree-removers-ignored: 85 passed, 22 FAILED of 107
MUTANT m3-wt-early-release exit=1 test-worktree-removers-ignored: 106 passed, 1 FAILED of 107
MUTANT m4-drop-2nd-pass exit=1 test-worktree-removers-ignored: 101 passed, 6 FAILED of 107
Verdict: RED, as expected
```

## Not proven locally

- Linux / Ubuntu 26.04 execution is verified on GitHub Actions CI (`ubuntu-latest`).
- D22a (`lib/queue/orchestrate.sh` goal-done semantics) was not implemented and was stopped for design clarification in the round-1 report.

## Rollback

Revert the branch or merge commit to restore `lib/wrap/wrap-common.sh`, `lib/wrap/wrap-apply.sh`, `lib/goal/wt.sh`, `lib/queue/orchestrate.sh`, and `docs/reference/FEATURES.md`, and remove `lib/wrap/wt-guard.sh`, `tests/test-worktree-removers-ignored.sh`, and this proof file:

```bash
git revert <merge-sha>
```

No persistent on-disk state, config schema, or ledger format changes.
20 changes: 18 additions & 2 deletions lib/goal/wt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ cd "$MAIN" || exit 2
LIB="$MAIN/lib"
# shellcheck source=lib/gate/branch-merged.sh
source "$LIB/gate/branch-merged.sh" || { echo "wt.sh: lib/gate/branch-merged.sh missing or unreadable" >&2; exit 2; }
_WT_GUARD="$LIB/wrap/wt-guard.sh"
[ -f "$_WT_GUARD" ] || _WT_GUARD="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/wrap/wt-guard.sh"
# shellcheck source=lib/wrap/wt-guard.sh
source "$_WT_GUARD" || { echo "wt.sh: lib/wrap/wt-guard.sh missing or unreadable" >&2; exit 2; }

usage() { sed -n '2,20p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit "${1:-2}"; }
verb="${1:-}"; shift || true
Expand Down Expand Up @@ -82,10 +86,22 @@ case "$verb" in
if [ -n "$rsha" ] && [ "$rsha" != "$tip" ]; then
echo "wt.sh: refusing to close: LEAVE remote $rsha (origin/$br is not at the judged tip)" >&2; exit 1
fi
bash "$LIB/goal/goal-registry.sh" release "$slug" 2>/dev/null || true
if [ -d "$wt" ]; then
main_wt="$(_wt_main_checkout "$MAIN" 2>/dev/null || true)"
if _wt_remove_guard "$wt" "$main_wt"; then
echo "wt.sh: refusing to close: $(_san "$wt"): ${WT_IGN_MSG}" >&2
exit 1
fi
fi
# -f -f: a single --force no-ops on a locked worktree, and the `git branch -D` below then
# fails with "used by worktree", which reads as a second unrelated problem.
[ -d "$wt" ] && git worktree remove -f -f "$wt"
if [ -d "$wt" ]; then
if ! git worktree remove -f -f "$wt"; then
echo "wt.sh: refusing to close: worktree remove failed for $(_san "$wt")" >&2
exit 1
fi
fi
bash "$LIB/goal/goal-registry.sh" release "$slug" 2>/dev/null || true
git branch -D "$br" >/dev/null
# Leased to the tip that was judged: an origin branch that moved on (a newer PR from the same head) stays.
git push origin --force-with-lease="refs/heads/$br:$tip" ":refs/heads/$br" >/dev/null 2>&1 || true
Expand Down
30 changes: 21 additions & 9 deletions lib/queue/orchestrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ BACKLOG_LIB="${BACKLOG_LIB:-$LIB_ROOT/board/backlog.sh}"
# boxes of SG-NN sub-goals only: a parked box is not done and is never auto-picked, so it is never listed.
# shellcheck source=../mega/box.sh
. "$LIB_ROOT/mega/box.sh" || exit 1
_SG_BOX_RE="$(box_re open done)"
_SG_BOX_RE="$(box_re open "done")"

# Multi-vendor dispatch adapter: vendor -> headless argv + prompt-delivery mode.
# SOURCED, unlike dispatch-gate.sh which must be a subprocess because its `set -euo` would leak `-e`
Expand All @@ -163,6 +163,9 @@ CONFIG_LIB="${CONFIG_LIB:-$LIB_ROOT/config/kit-config.sh}"
# the converge guard's base. Idempotent if a caller already sourced it.
# shellcheck source=lib/config/merge-command.sh
. "$LIB_ROOT/config/merge-command.sh" || { echo "FATAL: lib/config/merge-command.sh missing or unreadable" >&2; exit 1; }
# Worktree removal guard: keeps unique ignored/uncommitted work before removing a stale worktree.
# shellcheck source=lib/wrap/wt-guard.sh
. "$LIB_ROOT/wrap/wt-guard.sh" || { echo "FATAL: lib/wrap/wt-guard.sh missing or unreadable" >&2; exit 1; }

# _kit_bool01 <val> <default01> -- normalize a TOML `true`/`false` (or already-0/1) config
# value to the `0`/`1` shape orchestrate.sh's env-var knobs use; anything else (unset, typo)
Expand Down Expand Up @@ -406,7 +409,7 @@ _subgoals() {
id=$(printf '%s' "$line" | grep -oE "$BOX_SG_RE" | head -1)
[ -n "$id" ] || continue
policy=$(printf '%s' "$line" | awk -F',' '{for(i=1;i<=NF;i++){f=$i; gsub(/^[ \t]+|[ \t]+$/,"",f); lf=tolower(f); if(lf=="auto"||lf=="gate"||lf=="gate!"){print lf; exit}}}')
box_line_is done "$line" && checked=1
box_line_is "done" "$line" && checked=1
printf '%s\t%s\t%s\n' "$id" "${policy:-gate}" "$checked"
done || true
}
Expand Down Expand Up @@ -1007,11 +1010,11 @@ cmd_flip() { # megadir id
# Re-read the line UNDER the lock: a sibling flip may have checked it since the pre-lock probe.
local line rc=0
line=$(_sg_line "$roadmap" "$id")
box_line_is done "$line" && { _unlock "$lockdir"; return 0; } # already checked -> idempotent no-op
box_line_is "done" "$line" && { _unlock "$lockdir"; return 0; } # already checked -> idempotent no-op

local tmp
tmp=$(mktemp "$megadir/.roadmap.flip.XXXXXX" 2>/dev/null) || { _unlock "$lockdir"; echo "flip: mktemp failed" >&2; return 1; }
if awk -v id="$id" -v otok="$(box_token open)" -v dtok="$(box_token done)" \
if awk -v id="$id" -v otok="$(box_token open)" -v dtok="$(box_token "done")" \
'{ pre = "- " otok " "; if (index($0, pre id " ") == 1) $0 = "- " dtok " " substr($0, length(pre) + 1); print }' \
"$roadmap" > "$tmp" && mv -f "$tmp" "$roadmap"; then
_emit_event "$megadir" "$id" flip "box checked"
Expand Down Expand Up @@ -1470,13 +1473,21 @@ _wave_worktree() { # repo id branch

if [ -e "$wt/.git" ]; then
# A linked worktree carries a `.git` FILE (a gitdir pointer). Clean + on <branch> => resume.
local dirty cur
local dirty cur main_wt
dirty=$(git -C "$wt" status --porcelain 2>/dev/null)
cur=$(git -C "$wt" rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ -z "$dirty" ] && [ "$cur" = "$branch" ]; then
printf '%s\n' "$wt"; return 0
fi
git -C "$repo" worktree remove --force "$wt" 2>/dev/null || true
main_wt="$(_wt_main_checkout "$repo" 2>/dev/null || true)"
if _wt_remove_guard "$wt" "$main_wt"; then
echo "[orchestrate] [wave] $id: refusing to remove worktree $(_san "$wt"): ${WT_IGN_MSG}" >&2
return 1
fi
if ! git -C "$repo" worktree remove --force "$wt" 2>/dev/null; then
echo "[orchestrate] [wave] $id: failed to remove worktree $(_san "$wt")" >&2
return 1
fi
elif [ -e "$wt" ]; then
# non-worktree collision at the path (leftover from a crash): move aside, never delete.
mv -f "$wt" "${TMPDIR:-/tmp}/wave-wt-stale.$id.$$.$RANDOM" 2>/dev/null || true
Expand Down Expand Up @@ -2066,8 +2077,9 @@ _wave_spawn() { # megadir roadmap repo mega_abs id
# restores the pre-fix spawn so a test can prove the stop; never an operator flag.
[ "${NC_SKIP_WAVE_DETACH:-0}" = 1 ] || { command -v perl >/dev/null 2>&1 && [ -x "$ORCH_DIR/detach-run" ] && detach_cmd="$ORCH_DIR/detach-run"; }
[ "${_WAVE_ABORTED:-0}" = 1 ] && { rm -f "$pfile"; return 1; }
local orig_claude="$CLAUDE_CMD"
set -m
( cd "$wt" 2>/dev/null && KIT_DETACH_CMD="$CLAUDE_CMD" CLAUDE_CMD="$detach_cmd" \
( cd "$wt" 2>/dev/null && KIT_DETACH_CMD="$orig_claude" CLAUDE_CMD="$detach_cmd" \
_run_one_session "$megadir" "$id" "$pfile" "$route_flags" 0 ) &
pid=$!
set +m
Expand Down Expand Up @@ -3429,8 +3441,8 @@ cmd_run() {
# A box checked on origin with NO `PR #<n>` is a claim, not a completion: it is never retried
# either (the fast-forward above may already show it checked locally, and a retry would read
# that as a flip), so the loop halts for a human exactly as before.
box_line_is done "$rline" && ! printf '%s' "$rline" | grep -qE 'PR #[0-9]+' && bare_remote=1
if box_line_is done "$rline" && printf '%s' "$rline" | grep -qE 'PR #[0-9]+'; then
box_line_is "done" "$rline" && ! printf '%s' "$rline" | grep -qE 'PR #[0-9]+' && bare_remote=1
if box_line_is "done" "$rline" && printf '%s' "$rline" | grep -qE 'PR #[0-9]+'; then
checked=1
_say "[orchestrate] [reconcile] $id box is checked on origin with $(printf '%s' "$rline" | grep -oE 'PR #[0-9]+' | head -1); accepting the merged-PR flip."
# Mirror the merged flip into the LOCAL ROADMAP when the fast-forward could not run
Expand Down
Loading
Loading