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
4 changes: 2 additions & 2 deletions docs/reference/FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ GENERATED , do not hand-edit. Regenerate: `bash lib/registry/feature-registry.sh
| `/kit:ship` | `[H/I]` | Ship: review gate, tests, version bump, changelog, conventional commit, docs update, PR. Complete pipeline from done to merged. | SPEC-001, SPEC-002, SPEC-003 +111 | test-active-spec-pick.sh, test-adopt.sh, test-board-mirror.sh +65 |
| `/kit:spec-validate` | `[H/I]` | Adversarial review of a spec before implementation. 7 specialist lenses attack the spec from different angles (6 advisory, 1 blocking on th… | SPEC-002, SPEC-003, SPEC-004 +70 | test-command-emit-sweep.sh, test-design-record.sh, test-every-step-review.sh +10 |
| `/kit:spec` | `[H/I]` | Generate a development spec from a feature idea or decision brief. Creates docs/specs/ with structured requirements. | SPEC-001, SPEC-002, SPEC-003 +241 | test-active-spec-pick.sh, test-audit-verdict-grammar.sh, test-bin-forwarders.sh +111 |
| `/kit:start` | `[H/I]` | Detect project state and suggest the right next command. The entry point for any session. | SPEC-002, SPEC-003, SPEC-004 +94 | test-anomalies-advisor.sh, test-audit-cadence.sh, test-board-add.sh +96 |
| `/kit:start` | `[H/I]` | Detect project state and suggest the right next command. The entry point for any session. | SPEC-002, SPEC-003, SPEC-004 +94 | test-anomalies-advisor.sh, test-audit-cadence.sh, test-board-add.sh +98 |
| `/kit:test-plan-review-team` | `[H/I]` | Parallel multi-lens adversarial critique of a spec's test plan (the ## Test plan section), with a bounded revise loop that tightens it. Dis… | SPEC-031, SPEC-052, SPEC-062 +14 | test-meta-docs-registry.sh, test-meta-review-verifiers.sh, test-meta-vmodel-dispatch.sh +3 |
| `/kit:test-plan` | `[H/I]` | Derive a test-case coverage matrix from a spec's acceptance criteria before /kit:execute. Writes a `## Test plan` section into the active s… | SPEC-004, SPEC-016, SPEC-018 +38 | test-ceremony-lens.sh, test-e2e.sh, test-gate-ledger-inherit.sh +15 |
| `/kit:test-write` | `[H/I]` | Turn a SOLID-verdict `## Test plan critique` into real, executing test code via `kit:test-writer`, one case per matrix row. Never dispatche… | SPEC-203, SPEC-227, SPEC-237 +2 | test-test-writer-contract.sh |
Expand All @@ -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 +57 |
| `/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 +59 |

## Agents

Expand Down
6 changes: 3 additions & 3 deletions docs/reference/wrap.md

Large diffs are not rendered by default.

323 changes: 323 additions & 0 deletions docs/verification/wrap-safety.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion lib/config/module-registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ never turns the step off.
| KIT_WRAP_LAND_GRACE_SECS | env-only | `30` | [impl] | wrap | `wrap land`, before the first merge, on a repo with a `pull_request` workflow: seconds, one read every 10s, to hold while no check has registered on the PR yet, for a workflow with a `paths` or `labeled` filter (it may start nothing). Pending checks then wait to `KIT_WRAP_CARRY_CHECKS_SECS`. A non-numeric value falls back to 30. |
| KIT_WRAP_LAND_REGISTER_SECS | env-only | `300` | [impl] | wrap | `wrap land`, before the first merge: the same hold, for a repo whose `pull_request` workflow carries no `paths` or `labeled` filter and so must start a run on every PR. GitHub has queued such a run 190s after the PR opened. The hold ends at the first check that appears. When none registers it prints `no checks registered on #<n> after <secs>s; merging with none`. A non-numeric value falls back to 300. |
| KIT_WRAP_LAND_GRACE_SECS | env-only | `30` | [impl] | wrap | `wrap land`, before the first merge, on a repo with a `pull_request` workflow: seconds, one read every 10s, to hold while no check has registered on the PR yet. Pending checks then wait to `KIT_WRAP_CARRY_CHECKS_SECS`. A non-numeric value falls back to 30. |
| KIT_WRAP_SKIP_BUSY_CHECK | env-only | `0` | [impl] | wrap | `1` skips the busy check `land`, `apply --worktrees` and `apply --own` run before removing a worktree. The check refuses a worktree a live process outside the wrap run still holds (cwd or an open file inside it): `SKIP <wt>: busy, held by pid <pid> (<command>)`, worktree and branch kept. The caller's own pipeline reader (stdin a pipe, in wrap's process group or its shell's, e.g. `timeout 120 wrap land <wt> | tail`) never counts. Without `lsof` the check prints one NOTE and removal proceeds. `apply --archive-worktrees` fails closed instead: with this set to `1`, or with no `lsof`, its whole section prints `SKIP archive worktrees: busy check unavailable` and archives nothing, because it pushes and removes unmerged work. |
| KIT_SKILL_DIRS | env-only | `$HOME/.claude/skills` plus `${CLAUDE_PLUGIN_ROOT:-}/skills` when set | [consumer] | wrap | Colon-separated list of skill directories `config seams` searches for a `skill` kind row's `SKILL.md` (e.g. `wrap.before`). Entries whose realpath does not sit under `$HOME` are dropped, because a repo `.envrc` can set this. Not read by any code yet; `config seams` is the first consumer. |

### test (dev-loop host-load warning and suite timing history, no install module)
Expand Down
104 changes: 78 additions & 26 deletions lib/wrap/wrap-apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ _wt_cleared() {
return 0
}

# _delete_branch_leased <repo> <branch> <expected-tip> -- atomically deletes refs/heads/<branch>
# only when it still points at <expected-tip>, is not a symbolic ref, and is checked out in
# neither HEAD nor any worktree. Removes branch.<branch> config on success.
_delete_branch_leased() {
local repo="$1" b="$2" expected="$3" wl
[ -n "$b" ] && [ -n "$expected" ] || return 1
if git -C "$repo" symbolic-ref -q "refs/heads/${b}" >/dev/null 2>&1; then
return 1
fi
if [ "$(git -C "$repo" symbolic-ref -q HEAD 2>/dev/null)" = "refs/heads/${b}" ]; then
return 1
fi
# Capture before matching: an early grep exit under pipefail can SIGPIPE git.
if ! wl="$(git -C "$repo" worktree list --porcelain 2>/dev/null)" \
|| case $'\n'"${wl}"$'\n' in *$'\n'"branch refs/heads/${b}"$'\n'*) true ;; *) false ;; esac; then
return 1
fi
# branch -D also refuses a branch a worktree is rebasing or bisecting (HEAD detached there).
local cdir gd
cdir="$(git -C "$repo" rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" || return 1
for gd in "$cdir" "$cdir"/worktrees/*; do
[ -d "$gd" ] || continue
[ "$(cat "$gd/rebase-merge/head-name" 2>/dev/null)" = "refs/heads/${b}" ] && return 1
[ "$(cat "$gd/rebase-apply/head-name" 2>/dev/null)" = "refs/heads/${b}" ] && return 1
[ "$(cat "$gd/BISECT_START" 2>/dev/null)" = "$b" ] && return 1
done
git -C "$repo" update-ref --no-deref -d "refs/heads/${b}" "$expected" || return $?
git -C "$repo" config --remove-section "branch.${b}" >/dev/null 2>&1 || true
return 0
}

# _wt_busy <worktree path> -- 0 when a live process outside this wrap run holds the worktree:
# its current directory or an open file or mapping sits at or under the path. Sets BUSY_MSG to
# the one-line `SKIP <wt>: busy, held by pid <pid> (<command>)` (plus `and N more`). A
Expand All @@ -55,8 +86,7 @@ _wt_cleared() {
# inside the worktree, lives only until wrap exits, and would otherwise read as a holder that
# never leaves. A background job (stdin not a pipe) or one from an earlier call (another
# group) still counts.
# KIT_WRAP_SKIP_BUSY_CHECK=1 turns the check off. Without lsof it prints one NOTE per run and
# answers "not busy", today's behavior.
# Without lsof it prints one NOTE per run and answers "not busy", today's behavior.
BUSY_MSG=""
BUSY_PID=""
_WT_BUSY_NOTED=0
Expand All @@ -81,7 +111,6 @@ _wt_busy() {
local wt="$1" mode="${2:-snap}" canon holders psnap pid cmd n
BUSY_MSG=""
BUSY_PID=""
[ "${KIT_WRAP_SKIP_BUSY_CHECK:-0}" = 1 ] && return 1
if ! command -v lsof >/dev/null 2>&1; then
if [ "$_WT_BUSY_NOTED" = 0 ]; then
echo " NOTE: busy check unavailable (no lsof)"; _WT_BUSY_NOTED=1
Expand Down Expand Up @@ -310,11 +339,12 @@ _sweep_wt_holder() {
esac
}

# _emit_sweep_row <kind> <path> <branch> <verdict> <reason> <pr> <pr_state> <dirty> <holder> <own_commits> <workspace_rm>
# _emit_sweep_row <kind> <path> <branch> <verdict> <reason> <pr> <pr_state> <dirty> <holder> <own_commits> <workspace_rm> [<deleted_tip>]
_emit_sweep_row() {
[ -n "${_SWEEP_ROWS_FILE:-}" ] || return 0
local kind="$1" path="$2" branch="$3" verdict="$4" reason="$5"
local pr="$6" pr_state="$7" dirty="$8" holder="$9" own_commits="${10}" workspace_rm="${11}"
local deleted_tip="${12:-}"
jq -cn \
--arg kind "$kind" \
--arg path "$path" \
Expand All @@ -326,7 +356,8 @@ _emit_sweep_row() {
--arg dirty "$dirty" \
--arg holder "$holder" \
--arg own_commits "$own_commits" \
--arg workspace_rm "$workspace_rm" '
--arg workspace_rm "$workspace_rm" \
--arg deleted_tip "$deleted_tip" '
{
kind: $kind,
path: (if $path == "" then null else $path end),
Expand All @@ -338,7 +369,8 @@ _emit_sweep_row() {
dirty: (if $dirty == "true" then true elif $dirty == "false" then false else null end),
holder: (if ($holder | test("^[0-9]+$")) then ($holder | tonumber) else null end),
own_commits: (if ($own_commits | test("^[0-9]+$")) then ($own_commits | tonumber) else null end),
workspace_rm: (if ($workspace_rm == "" or $workspace_rm == "null") then null else $workspace_rm end)
workspace_rm: (if ($workspace_rm == "" or $workspace_rm == "null") then null else $workspace_rm end),
deleted_tip: (if ($deleted_tip == "" or $deleted_tip == "null") then null else $deleted_tip end)
}' >> "$_SWEEP_ROWS_FILE"
}

Expand Down Expand Up @@ -450,7 +482,8 @@ _apply_worktrees() {
fi
continue
fi
read -r lcode lreason proof <<< "$(_apply_branch_merged "$repo" "$def" "$ghs" "$wtb")"
tip="$(git -C "$repo" rev-parse --verify --quiet "refs/heads/${wtb}" 2>/dev/null)"
read -r lcode lreason proof <<< "$(_apply_branch_merged --tip "$tip" "$repo" "$def" "$ghs" "$wtb")"
if [ "$lcode" != SAFE ]; then
local skip_reason
if [ "${lcode} ${lreason}" = "LEAVE fresh" ]; then
Expand All @@ -469,8 +502,8 @@ _apply_worktrees() {
continue
fi
# The proof above can cost a network round trip, so both destructive inputs are re-read right
# before the force: `-f -f` overrides a worktree that went dirty, and `-D` discards a commit
# made since the run's own tip snapshot.
# before the force: `-f -f` overrides a worktree that went dirty, and a leased delete refuses
# a branch whose tip moved since the proof read.
if [ -n "$(git -C "$wt" status --short 2>/dev/null)" ]; then
echo " SKIP ${wt}: went dirty while the proof was read"
if [ -n "${_SWEEP_ROWS_FILE:-}" ]; then
Expand All @@ -482,19 +515,32 @@ _apply_worktrees() {
fi
continue
fi
tip="$(git -C "$repo" rev-parse --verify --quiet "refs/heads/${wtb}" 2>/dev/null)"
local tip_now
tip_now="$(git -C "$repo" rev-parse --verify --quiet "refs/heads/${wtb}" 2>/dev/null)"
scanned="$(_scanned_tip "$wtb")"
if [ -n "$scanned" ] && [ "$tip" != "$scanned" ]; then
local moved_msg="${wtb} tip moved during this run ($(_short "$scanned") -> $(_short "$tip"))"
if [ -n "$scanned" ] && [ "$tip_now" != "$scanned" ]; then
local moved_msg="${wtb} tip moved during this run ($(_short "$scanned") -> $(_short "$tip_now"))"
echo " SKIP ${wt}: ${moved_msg}"
if [ -n "${_SWEEP_ROWS_FILE:-}" ]; then
IFS=$'\t' read -r pr_n pr_st <<< "$(_sweep_row_pr_info "$repo" "$def" "$ghs" "$wtb" "$tip")"
IFS=$'\t' read -r pr_n pr_st <<< "$(_sweep_row_pr_info "$repo" "$def" "$ghs" "$wtb" "$tip_now")"
own_c="$(_branch_own_commits "$repo" "$def" "$wtb")"
hpid="$(_sweep_wt_holder "$wt")"
_emit_sweep_row worktree "$wt" "$wtb" keep "$moved_msg" "$pr_n" "$pr_st" false "$hpid" "$own_c" null
fi
continue
fi
if [ -n "$tip" ] && [ "$tip_now" != "$tip" ]; then
local moved_msg="${wtb} tip moved while the proof was read ($(_short "$tip") -> $(_short "$tip_now"))"
echo " SKIP ${wt}: ${moved_msg}"
if [ -n "${_SWEEP_ROWS_FILE:-}" ]; then
IFS=$'\t' read -r pr_n pr_st <<< "$(_sweep_row_pr_info "$repo" "$def" "$ghs" "$wtb" "$tip_now")"
own_c="$(_branch_own_commits "$repo" "$def" "$wtb")"
hpid="$(_sweep_wt_holder "$wt")"
_emit_sweep_row worktree "$wt" "$wtb" keep "$moved_msg" "$pr_n" "$pr_st" false "$hpid" "$own_c" null
fi
continue
fi
tip="$tip_now"
lock="unlocked"; _wt_locked "$wt" && lock="locked"
if [ "$lock" = "locked" ] && _wt_lock_live "$wt"; then
local lpid; lpid="$(_wt_lock_pid "$wt")"
Expand Down Expand Up @@ -570,6 +616,7 @@ _apply_worktrees() {
elif ! _write_guard "$repo"; then WT_IGN_MSG="index.lock held by another writer"
elif ! _wt_status_raw "$wt" "$raw2"; then WT_IGN_MSG="git status failed"
elif ! cmp -s "$raw1" "$raw2"; then WT_IGN_MSG="status changed before removal"
elif [ "$(git -C "$repo" rev-parse --verify --quiet "refs/heads/${wtb}" 2>/dev/null)" != "$tip" ]; then WT_IGN_MSG="${wtb} tip moved before removal"
fi
if [ -n "$WT_IGN_MSG" ]; then
[ -n "$raw1$raw2" ] && rm -f ${raw1:+"$raw1"} ${raw2:+"$raw2"}; echo " SKIP $(_san "$wt"): ${WT_IGN_MSG}"
Expand All @@ -583,9 +630,13 @@ _apply_worktrees() {
if _wt_cleared "$repo" "$wt"; then
wrm_res="$(_run_seam_workspace_rm "$_APPLY_WORKSPACE_RM" "$wt" "$repo")"
run "$repo" "delete ${wtb} (${proof}, its ${lock} worktree is gone)" \
git -C "$repo" branch -D "$wtb"
_delete_branch_leased "$repo" "$wtb" "$tip"
if [ -n "${_SWEEP_ROWS_FILE:-}" ]; then
_emit_sweep_row worktree "$wt" "$wtb" removed "${proof}" "$pr_n" "$pr_st" false null "$own_c" "$wrm_res"
if [ "$_RUN_LAST_RC" -eq 0 ] && ! _ref_exists "$repo" "refs/heads/${wtb}"; then
_emit_sweep_row worktree "$wt" "$wtb" removed "${proof}" "$pr_n" "$pr_st" false null "$own_c" "$wrm_res" "$tip"
else
_emit_sweep_row worktree "$wt" "$wtb" error "${wt} removed, ${wtb} not deleted" "$pr_n" "$pr_st" false null "$own_c" "$wrm_res" null
fi
fi
else
echo " FAILED ${verdict}: ${wt} survived the removal, ${wtb} not deleted"
Expand Down Expand Up @@ -657,20 +708,19 @@ _apply_branches() {
IFS=$'\t' read -r pr_n pr_st <<< "$(_sweep_row_pr_info "$repo" "$def" "$ghs" "$b" "$tip")"
own_c="$(_branch_own_commits "$repo" "$def" "$b")"
fi
# -D, not -d: the proof is wrap's own, against origin/<def>. `branch -d` judges against the
# branch's OWN upstream (or HEAD when it has none), so it refused a branch that tracks
# another ref or nothing even though origin/<def> already holds its tip. The proof judges the
# sha read above, see branch_merged.
# Leased ref delete, not branch -d/-D: the proof is wrap's own against origin/<def> on the
# exact tip read above, so a commit added in the window before delete refuses instead of
# being lost.
read -r lcode lreason ldetail <<< "$(_apply_branch_merged --tip "$tip" "$repo" "$def" "$ghs" "$b")"
local b_verdict="keep" b_reason=""
case "${lcode} ${lreason}" in
"SAFE ancestor"|"SAFE absorbed")
b_reason="${ldetail}"
run "$repo" "delete ${b} (${ldetail})" git -C "$repo" branch -D "$b" ;;
run "$repo" "delete ${b} (${ldetail})" _delete_branch_leased "$repo" "$b" "$tip" ;;
"SAFE squash")
b_reason="squash-merged into ${def}, tip matches the PR head"
run "$repo" "delete ${b} (${b_reason})" \
git -C "$repo" branch -D "$b" ;;
_delete_branch_leased "$repo" "$b" "$tip" ;;
"LEAVE fresh")
b_reason="${ldetail}"
echo " SKIP ${b}: ${ldetail}" ;;
Expand All @@ -688,16 +738,18 @@ _apply_branches() {
echo " SKIP ${b}: ${b_reason}" ;;
esac
if [ -n "${_SWEEP_ROWS_FILE:-}" ]; then
local b_dtip=""
if [ "$lcode" = SAFE ]; then
if [ "$APPLY" != 1 ]; then
b_verdict="would_remove"
elif ! _ref_exists "$repo" "refs/heads/${b}"; then
elif [ "$_RUN_LAST_RC" -eq 0 ] && ! _ref_exists "$repo" "refs/heads/${b}"; then
b_verdict="removed"
b_dtip="$tip"
else
b_verdict="error"
fi
fi
_emit_sweep_row branch "" "$b" "$b_verdict" "$b_reason" "$pr_n" "$pr_st" null null "$own_c" null
_emit_sweep_row branch "" "$b" "$b_verdict" "$b_reason" "$pr_n" "$pr_st" null null "$own_c" null "$b_dtip"
fi
done
}
Expand Down Expand Up @@ -958,8 +1010,8 @@ _apply_archive_worktrees() {
esac
# Fail closed: --worktrees removes only proven-merged work and proceeds without lsof; this
# sweep pushes and removes unmerged work, so it needs the busy check.
if [ "${KIT_WRAP_SKIP_BUSY_CHECK:-0}" = 1 ] || ! command -v lsof >/dev/null 2>&1; then
echo " SKIP archive worktrees: busy check unavailable (no lsof, or KIT_WRAP_SKIP_BUSY_CHECK=1)"; return 0
if ! command -v lsof >/dev/null 2>&1; then
echo " SKIP archive worktrees: busy check unavailable (no lsof)"; return 0
fi
# lsof always lists its caller: empty output means it failed (denied, broken), and
# _wt_busy would then read every worktree as idle.
Expand Down Expand Up @@ -1440,7 +1492,7 @@ _apply_origin_branches() {
if printf '%s\n' "$rem_names" | grep -qxF "${b_arr[$j]}"; then
_emit_sweep_row origin_branch "" "${b_arr[$j]}" error "failed to delete merged branch on origin" "$pr_n" "$pr_st" null null null null
else
_emit_sweep_row origin_branch "" "${b_arr[$j]}" removed "merged branch on origin" "$pr_n" "$pr_st" null null null null
_emit_sweep_row origin_branch "" "${b_arr[$j]}" removed "merged branch on origin" "$pr_n" "$pr_st" null null null null "${t_arr[$j]}"
fi
j=$(( j + 1 ))
done
Expand Down
4 changes: 4 additions & 0 deletions lib/wrap/wrap-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,13 @@ _write_guard() {

# run <repo> <verdict> <command...> -- print the verdict, execute only under --apply.
# A failed write is reported once and turns the run's exit code into 2. Nothing is retried.
_RUN_LAST_RC=0
run() {
_RUN_LAST_RC=0
local repo="$1" verdict="$2"; shift 2
if ! _write_guard "$repo"; then
echo " SKIP ${verdict}: index.lock held by another writer"
_RUN_LAST_RC=1
# Under --pull-only the pull is the whole job, so a skipped write is a failed call.
[ "$PULL_ONLY" = 1 ] && [ "$APPLY" = 1 ] && FAILURES=1
return 0
Expand All @@ -327,6 +330,7 @@ run() {
[ "$APPLY" = 1 ] || return 0
"$@"
local rc=$?
_RUN_LAST_RC=$rc
if [ "$rc" -ne 0 ]; then
echo " FAILED ${verdict}: exit ${rc}"
FAILURES=1
Expand Down
Loading
Loading