@jeiemgi/cckit 0.1.8 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +21 -8
  3. package/README.md +63 -5
  4. package/bin/cckit +77 -12
  5. package/cckit.config.json +1 -1
  6. package/commands/kit-doctor.md +3 -1
  7. package/kit.config.schema.json +24 -0
  8. package/package.json +1 -1
  9. package/scripts/annotate-setup.sh +7 -2
  10. package/scripts/capture-project-ids.sh +23 -10
  11. package/scripts/copilot-loop-test.sh +162 -0
  12. package/scripts/init.sh +22 -4
  13. package/scripts/kit-doctor.sh +185 -7
  14. package/scripts/kit-version-check-test.sh +36 -0
  15. package/scripts/kit-version-check.sh +19 -4
  16. package/scripts/knowledge-lint.sh +4 -1
  17. package/scripts/lib/board-view-test.sh +54 -0
  18. package/scripts/lib/board-view.sh +80 -0
  19. package/scripts/lib/captain-test.sh +76 -0
  20. package/scripts/lib/captain.sh +268 -0
  21. package/scripts/lib/config-path-test.sh +47 -0
  22. package/scripts/lib/config-path.sh +29 -0
  23. package/scripts/lib/copilot-test.sh +30 -0
  24. package/scripts/lib/copilot.sh +101 -0
  25. package/scripts/lib/effort-metrics-test.sh +57 -0
  26. package/scripts/lib/effort-metrics.sh +19 -5
  27. package/scripts/lib/effort-ops-test.sh +51 -2
  28. package/scripts/lib/effort-ops.sh +314 -49
  29. package/scripts/lib/effort-parity-test.sh +128 -0
  30. package/scripts/lib/effort-slug-test.sh +87 -0
  31. package/scripts/lib/effort-slug.sh +104 -0
  32. package/scripts/lib/effort.sh +34 -0
  33. package/scripts/lib/gh-project-test.sh +95 -0
  34. package/scripts/lib/gh-project.sh +22 -7
  35. package/scripts/lib/git-remote-test.sh +57 -0
  36. package/scripts/lib/git-remote.sh +21 -0
  37. package/scripts/lib/kit-config-test.sh +37 -0
  38. package/scripts/lib/kit-config.sh +33 -13
  39. package/scripts/lib/kit-events.sh +5 -3
  40. package/scripts/lib/kit-gc-test.sh +88 -0
  41. package/scripts/lib/kit-gc.sh +136 -19
  42. package/scripts/lib/kit-local.sh +8 -1
  43. package/scripts/lib/next.sh +75 -0
  44. package/scripts/lib/plan-machine-test.sh +72 -0
  45. package/scripts/lib/plan-machine.sh +260 -0
  46. package/scripts/lib/plan-next-test.sh +87 -0
  47. package/scripts/lib/plan-next.sh +212 -0
  48. package/scripts/lib/project-scan.sh +7 -1
  49. package/scripts/lib/render-test.sh +40 -0
  50. package/scripts/lib/render.sh +57 -0
  51. package/scripts/lib/worktree-start.sh +12 -3
  52. package/scripts/lib/zsh-safety-test.sh +73 -0
  53. package/scripts/portable-test.sh +62 -20
  54. package/scripts/setup-labels.sh +13 -2
  55. package/scripts/showcase.sh +13 -4
  56. package/scripts/status-test.sh +63 -0
  57. package/scripts/status.sh +12 -4
  58. package/scripts/task-sync.sh +79 -10
  59. package/skills/kit-autopilot/SKILL.md +5 -0
  60. package/skills/kit-docs-writer/SKILL.md +135 -0
  61. package/skills/kit-effort-new/SKILL.md +39 -111
  62. package/skills/kit-effort-pr/SKILL.md +6 -2
  63. package/skills/kit-next/SKILL.md +58 -0
  64. package/skills/kit-plan-next/SKILL.md +82 -0
  65. package/templates/hooks/mempal_session_start.sh.tmpl +36 -2
  66. package/templates/rules/effort-model.md +26 -6
  67. package/templates/rules/mempalace.md +1 -0
  68. package/templates/rules/plan-next.md +58 -0
@@ -6,13 +6,24 @@
6
6
  # Per-folder overrides: any .claudekit/config.json in an ancestor directory is deep-merged over the
7
7
  # project config, nearest-wins (like .editorconfig). Projects with no .claudekit/ behave unchanged.
8
8
 
9
+ # Source the ONE shared config-path resolver (config-path.sh, #69). Self-locate portably: BASH_SOURCE
10
+ # is bash-only (empty under zsh, where dirname "" -> CWD and the lib is sought in the wrong dir), so
11
+ # use zsh's %x prompt escape there. eval keeps bash from parsing the zsh-only syntax.
12
+ if ! command -v kit_config_path >/dev/null 2>&1; then
13
+ if [ -n "${BASH_SOURCE:-}" ]; then _kc_self="${BASH_SOURCE[0]}"
14
+ elif [ -n "${ZSH_VERSION:-}" ]; then eval '_kc_self="${(%):-%x}"'
15
+ else _kc_self="$0"; fi
16
+ _kc_dir="$(cd "$(dirname "$_kc_self")" && pwd)"
17
+ # shellcheck source=/dev/null
18
+ [ -f "$_kc_dir/config-path.sh" ] && . "$_kc_dir/config-path.sh"
19
+ unset _kc_self _kc_dir
20
+ fi
21
+
9
22
  load_kit_config() {
10
- # Resolve the project config like the dispatcher (bin/cckit _cckit_find_config): an explicit
11
- # KIT_CONFIG wins, else a root cckit.config.json, else .claude/kit.config.json.
12
- local cfg="${KIT_CONFIG:-}"
13
- if [[ -z "$cfg" ]]; then
14
- if [[ -f cckit.config.json ]]; then cfg="cckit.config.json"; else cfg=".claude/kit.config.json"; fi
15
- fi
23
+ # Resolve the project config through the ONE shared resolver: an explicit KIT_CONFIG wins, else
24
+ # walk up for a root cckit.config.json or a .claude/kit.config.json.
25
+ local cfg; cfg="$(kit_config_path 2>/dev/null || true)"
26
+ [[ -n "$cfg" ]] || cfg=".claude/kit.config.json"
16
27
  if [[ ! -f "$cfg" ]]; then
17
28
  echo "✗ $cfg not found. Run /kit-init (or scripts/init.sh) first." >&2
18
29
  return 1
@@ -30,10 +41,16 @@ load_kit_config() {
30
41
 
31
42
  export KIT_REPO="$(jq -r '.github.repo' "$cfg")"
32
43
  export KIT_OWNER="$(jq -r '.github.owner' "$cfg")"
33
- export KIT_BASE_BRANCH="$(jq -r '.github.baseBranch // "main"' "$cfg")" # integration branch (default main; e.g. develop)
44
+ # Integration branch, resolved through a fallback chain so a host project that names its
45
+ # integration branch under an older/alternate key still works on adoption: baseBranch (canonical)
46
+ # -> integrationBranch -> flow -> "main". First non-null wins (#117).
47
+ export KIT_BASE_BRANCH="$(jq -r '.github.baseBranch // .github.integrationBranch // .github.flow // "main"' "$cfg")"
34
48
  export KIT_PROJECTS_V2="$(jq -r '.github.projectsV2' "$cfg")"
35
49
  export KIT_PROJECT_NUMBER="$(jq -r '.github.projectNumber' "$cfg")"
36
50
  export KIT_PROJECT_TITLE="$(jq -r '.github.projectTitle' "$cfg")"
51
+ # Board owner type — "user" or "organization". A board can live under a user OR an org login, and
52
+ # every projectV2-by-number GraphQL query must select the matching root, so capture this once (#118).
53
+ export KIT_PROJECT_OWNER_TYPE="$(jq -r '.github.projectOwnerType // "user"' "$cfg")"
37
54
 
38
55
  export KIT_PLANS_FORMAT="$(jq -r '.plans.format' "$cfg")"
39
56
  export KIT_PLANS_DIR="$(jq -r '.plans.dir' "$cfg")"
@@ -83,11 +100,14 @@ _kit_apply_claudekit_overlays() {
83
100
  merged="$(jq -s '.[0] * .[1]' <(printf '%s' "$merged") "$o" 2>/dev/null || printf '%s' "$merged")"
84
101
  done
85
102
 
86
- local g; g() { printf '%s' "$merged" | jq -r "$1" 2>/dev/null; }
87
- export KIT_LANG="$(g '.project.language // env.KIT_LANG')"
88
- export KIT_PLANS_FORMAT="$(g '.plans.format // env.KIT_PLANS_FORMAT')"
89
- export KIT_ANNOTATE_ENABLED="$(g '.annotate.enabled // false')"
90
- export KIT_ANNOTATE_BACKEND="$(g '.annotate.backend // ""')"
91
- export KIT_ANNOTATE_FRAMEWORK="$(g '.annotate.framework // ""')"
103
+ # Named `_kit_cfg_get`, not `g`: a one-letter `g` collides with a universal `alias g=git` when
104
+ # this file is sourced under zsh (interactive shells) -> "defining function based on alias" parse
105
+ # error. A namespaced name can never be an alias.
106
+ _kit_cfg_get() { printf '%s' "$merged" | jq -r "$1" 2>/dev/null; }
107
+ export KIT_LANG="$(_kit_cfg_get '.project.language // env.KIT_LANG')"
108
+ export KIT_PLANS_FORMAT="$(_kit_cfg_get '.plans.format // env.KIT_PLANS_FORMAT')"
109
+ export KIT_ANNOTATE_ENABLED="$(_kit_cfg_get '.annotate.enabled // false')"
110
+ export KIT_ANNOTATE_BACKEND="$(_kit_cfg_get '.annotate.backend // ""')"
111
+ export KIT_ANNOTATE_FRAMEWORK="$(_kit_cfg_get '.annotate.framework // ""')"
92
112
  export KIT_EFFECTIVE_CONFIG="$merged"
93
113
  }
@@ -43,8 +43,10 @@ emit_event() {
43
43
  [[ -n "$payload" ]] || payload='{}'
44
44
  command -v jq >/dev/null 2>&1 || return 0
45
45
 
46
- local path ts line
47
- path="$(kit_events_path)" || return 0
46
+ # `logpath`, not `path`: under zsh `path` is tied to PATH (special array); assigning to a bare
47
+ # `path` local would clobber the command search path. A namespaced name is inert.
48
+ local logpath ts line
49
+ logpath="$(kit_events_path)" || return 0
48
50
  ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
49
51
 
50
52
  # Build the line with jq so payload is validated + the record is well-formed.
@@ -58,5 +60,5 @@ emit_event() {
58
60
  '{ts:$ts, type:$type, op:$op, payload:{note:$raw}}' 2>/dev/null)" || return 0
59
61
 
60
62
  # Single-writer append; >> is atomic for short lines on local fs.
61
- printf '%s\n' "$line" >> "$path" 2>/dev/null || return 0
63
+ printf '%s\n' "$line" >> "$logpath" 2>/dev/null || return 0
62
64
  }
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # kit-gc-test.sh — covers the recover-before-prune contract (#111). A ZOMBIE worktree (working dir
4
+ # gone, admin metadata lingers) can hold staged-but-uncommitted work in its admin index — the sole
5
+ # blob->path map. `git worktree prune` deletes that index, orphaning the blobs (real data loss). gc
6
+ # must (a) detect zombies + staged deltas as their own analysis bucket, (b) recover any staged delta
7
+ # to its branch as a commit BEFORE pruning, and (c) never prune in a dry-run.
8
+ # Hermetic: a throwaway git repo, no network/gh. Run: bash scripts/lib/kit-gc-test.sh
9
+ set -u
10
+ ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
11
+ LIB="$ROOT/scripts/lib"
12
+ fail=0
13
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
14
+ has(){ case "$2" in *"$3"*) echo "ok: $1" ;; *) echo "FAIL: $1 -> '$2' lacks '$3'"; fail=1 ;; esac; }
15
+ command -v git >/dev/null 2>&1 || { echo "kit-gc-test: git required" >&2; exit 1; }
16
+
17
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
18
+ export KIT_GC_REPO="o/r"
19
+
20
+ # ── fabricate a zombie worktree with a staged delta ─────────────────────────────────────────────
21
+ cd "$tmp"
22
+ git init -q main
23
+ cd main
24
+ git config user.email t@t; git config user.name t
25
+ echo "base" > base.txt; git add base.txt; git commit -q -m "init"
26
+ git worktree add -q ../wt -b feat/5-recover >/dev/null 2>&1
27
+ # Stage a NEW file inside the worktree (writes the worktree's admin index; the blob lands in the
28
+ # shared object store and so survives the worktree dir's death).
29
+ echo "precious staged work" > ../wt/keep.txt
30
+ git -C ../wt add keep.txt
31
+ TIP_BEFORE="$(git rev-parse --verify refs/heads/feat/5-recover)"
32
+ # Kill the worktree dir — the crash/ephemeral-mount death that leaves a zombie.
33
+ rm -rf ../wt
34
+
35
+ # shellcheck source=/dev/null
36
+ source "$LIB/kit-gc.sh"
37
+
38
+ # ── (a) detection: the zombie + its staged delta show up as their own bucket ─────────────────────
39
+ z="$(_kit_gc_zombies)"
40
+ has "zombie detected with its branch" "$z" "feat/5-recover"
41
+ case "$z" in *" yes"*) echo "ok: zombie flagged as STAGED" ;; *) echo "FAIL: zombie not flagged staged: $z"; fail=1 ;; esac
42
+ analysis="$(kit_gc_analyze 2>/dev/null)"
43
+ has "analyze lists a zombies bucket" "$analysis" "# zombies"
44
+ has "analyze flags STAGED work in the zombie" "$analysis" "ZOMBIE with STAGED work"
45
+
46
+ # ── (c) dry-run recovers NOTHING (no new commit, admin dir intact) ──────────────────────────────
47
+ kit_gc_recover_zombies 0 >/dev/null 2>&1
48
+ t "dry-run leaves the branch tip unchanged" "$(git rev-parse --verify refs/heads/feat/5-recover)" "$TIP_BEFORE"
49
+ [ -d "$(git rev-parse --git-common-dir)/worktrees/wt" ] && echo "ok: dry-run leaves the admin dir intact" || { echo "FAIL: dry-run removed the admin dir"; fail=1; }
50
+
51
+ # ── (b) recover-before-prune: the staged delta lands as a commit on the branch ──────────────────
52
+ kit_gc_recover_zombies 1 >/dev/null 2>&1
53
+ TIP_AFTER="$(git rev-parse --verify refs/heads/feat/5-recover)"
54
+ [ "$TIP_AFTER" != "$TIP_BEFORE" ] && echo "ok: recovery advanced the branch to a new commit" || { echo "FAIL: branch tip did not advance"; fail=1; }
55
+ t "the recovered commit's parent is the old tip" "$(git rev-parse --verify "${TIP_AFTER}^")" "$TIP_BEFORE"
56
+ # The staged file is present in the recovered commit's tree with its exact content.
57
+ has "recovered commit carries the staged file" "$(git ls-tree -r --name-only "$TIP_AFTER")" "keep.txt"
58
+ t "recovered file content is intact" "$(git show "$TIP_AFTER:keep.txt")" "precious staged work"
59
+
60
+ # ── prune gating: `git worktree prune` only removes the admin dir after recovery ─────────────────
61
+ git worktree prune 2>/dev/null || true
62
+ [ -d "$(git rev-parse --git-common-dir)/worktrees/wt" ] && { echo "FAIL: admin dir survived prune"; fail=1; } || echo "ok: admin dir pruned after the work was recovered"
63
+
64
+ # ── #124: single-call PR index + local lookup ───────────────────────────────────────────────────
65
+ IDX="$(printf 'feat/1-a\tPR#11 MERGED\nfeat/2-b\tPR#22 OPEN')"
66
+ t "pr index lookup finds a branch" "$(_kit_gc_pr_for "$IDX" feat/1-a)" "PR#11 MERGED"
67
+ t "pr index lookup finds another branch" "$(_kit_gc_pr_for "$IDX" feat/2-b)" "PR#22 OPEN"
68
+ t "pr index lookup misses an absent branch" "$(_kit_gc_pr_for "$IDX" feat/9-z)" ""
69
+
70
+ # kit_gc_analyze must issue exactly ONE `gh pr list` regardless of branch count (no per-branch N+1).
71
+ git branch feat/30-x >/dev/null 2>&1; git branch feat/31-y >/dev/null 2>&1; git branch feat/32-z >/dev/null 2>&1
72
+ stub="$tmp/ghbin"; mkdir -p "$stub"
73
+ cat > "$stub/gh" <<'SH'
74
+ #!/usr/bin/env bash
75
+ echo "$*" >> "$GH_CALLS"
76
+ case "$1 $2" in
77
+ "pr list") printf 'feat/30-x\tPR#30 MERGED\n' ;;
78
+ "issue view") echo "open" ;;
79
+ *) : ;;
80
+ esac
81
+ SH
82
+ chmod +x "$stub/gh"
83
+ export GH_CALLS="$tmp/ghcalls"; : > "$GH_CALLS"
84
+ PATH="$stub:$PATH" KIT_GC_REPO="o/r" kit_gc_analyze >/dev/null 2>&1
85
+ t "kit_gc_analyze makes exactly ONE gh pr list call" "$(grep -c '^pr list' "$GH_CALLS")" "1"
86
+
87
+ [ "$fail" -eq 0 ] && echo "ALL OK (kit-gc)" || echo "kit-gc: FAILURES"
88
+ exit "$fail"
@@ -29,27 +29,47 @@ _kit_gc_load_deps() {
29
29
  [ -f "$d/worktree-issue.sh" ] && . "$d/worktree-issue.sh"
30
30
  }
31
31
 
32
+ # _kit_gc_pr_index [repo] — echo one "<headRefName>\tPR#<num> <STATE>" line per PR in ONE gh call, so
33
+ # branch classification is a local lookup instead of a per-branch `gh pr list --head` — an N+1 that
34
+ # scaled badly exactly when gc matters most (many stale branches). #124.
35
+ _kit_gc_pr_index() {
36
+ local repo="${1:-$KIT_GC_REPO}"
37
+ gh pr list --repo "$repo" --state all --limit 200 --json number,state,headRefName \
38
+ --jq '.[] | "\(.headRefName)\tPR#\(.number) \(.state)"' 2>/dev/null || true
39
+ }
40
+
41
+ # _kit_gc_pr_for <index> <branch> — first PR line matching <branch> as head (mirrors the old `.[0]`).
42
+ _kit_gc_pr_for() {
43
+ printf '%s\n' "$1" | awk -F'\t' -v want="$2" '$1==want{print $2; exit}'
44
+ }
45
+
32
46
  # kit_gc_analyze — read-only classification of worktrees, branches, and stashes. Writes NOTHING.
33
47
  # Each row is tagged PROTECTED / SAFE / ACTIVE / ORPHAN so a human or UI can decide what to prune.
34
48
  kit_gc_analyze() {
35
49
  _kit_gc_load_deps
36
- local repo="$KIT_GC_REPO" b ref path reason pr prot
50
+ # `wtpath`, not `path`: under zsh `path` is tied to PATH (special array), so a bare `path` local
51
+ # here would clobber the command search path on assignment. A namespaced name is inert.
52
+ local repo="$KIT_GC_REPO" b ref wtpath reason pr prot
37
53
  git fetch origin --prune --quiet 2>/dev/null || true
38
54
 
39
55
  echo "# worktrees"
40
56
  git worktree list --porcelain 2>/dev/null \
41
57
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
42
- | while read -r path ref; do
58
+ | while read -r wtpath ref; do
43
59
  b="${ref#refs/heads/}"
44
60
  reason="$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)"
45
- if [ -n "$reason" ]; then echo " $path [$b] -> PROTECTED: $reason"
46
- else echo " $path [$b] -> prunable if PR merged"; fi
61
+ if [ -n "$reason" ]; then echo " $wtpath [$b] -> PROTECTED: $reason"
62
+ else echo " $wtpath [$b] -> prunable if PR merged"; fi
47
63
  done
48
64
 
49
65
  echo "# branches"
50
- for b in $(git branch --format='%(refname:short)' 2>/dev/null); do
66
+ # All PRs in ONE call, indexed locally by head branch (#124). while-read (NOT `for b in $(...)`) so
67
+ # branch iteration survives a NUL-polluted IFS.
68
+ local pr_index; pr_index="$(_kit_gc_pr_index "$repo")"
69
+ git branch --format='%(refname:short)' 2>/dev/null | while IFS= read -r b; do
70
+ [ -n "$b" ] || continue
51
71
  case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) echo " $b -> ACTIVE (base branch)"; continue;; esac
52
- pr="$(gh pr list --repo "$repo" --head "$b" --state all --json number,state --jq '.[0]|"PR#\(.number) \(.state)"' 2>/dev/null || true)"
72
+ pr="$(_kit_gc_pr_for "$pr_index" "$b")"
53
73
  prot="$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)"
54
74
  if [ -n "$prot" ]; then
55
75
  echo " $b -> PROTECTED: $prot"
@@ -64,6 +84,21 @@ kit_gc_analyze() {
64
84
 
65
85
  echo "# stashes"
66
86
  git stash list 2>/dev/null | sed 's/^/ /' || true
87
+
88
+ echo "# zombies (worktree dir gone, admin metadata lingers)"
89
+ local zname zbranch zstaged found=0
90
+ while IFS="$(printf '\t')" read -r zname zbranch zstaged; do
91
+ [ -n "$zname" ] || continue
92
+ found=1
93
+ if [ "$zstaged" = yes ]; then
94
+ echo " $zname [$zbranch] -> ZOMBIE with STAGED work — recover-before-prune (gc --prune --yes recovers it to a commit)"
95
+ else
96
+ echo " $zname [$zbranch] -> ZOMBIE (no staged delta) — prunable"
97
+ fi
98
+ done <<EOF
99
+ $(_kit_gc_zombies)
100
+ EOF
101
+ [ "$found" -eq 1 ] || echo " (none)"
67
102
  }
68
103
 
69
104
  # kit_gc_has_prunable — rc 0 if at least one branch is SAFE to delete (a merged, unprotected branch).
@@ -71,6 +106,73 @@ kit_gc_has_prunable() {
71
106
  kit_gc_analyze 2>/dev/null | grep -q '> SAFE '
72
107
  }
73
108
 
109
+ # _kit_gc_zombies — echo "<name>\t<branch>\t<staged:yes|no>" for each ZOMBIE worktree: its working
110
+ # dir is gone but its admin metadata (<git-common-dir>/worktrees/<name>/) lingers. staged=yes when
111
+ # the admin index holds a tree that differs from the branch tip (staged-but-uncommitted work that
112
+ # `git worktree prune` would orphan). Writes NOTHING. bash 3.2.
113
+ _kit_gc_zombies() {
114
+ local common wdir name gitdir wtpath branch tree tip tiptree staged
115
+ common="$(git rev-parse --git-common-dir 2>/dev/null)" || return 0
116
+ case "$common" in /*) : ;; *) common="$(cd "$common" 2>/dev/null && pwd)" || return 0 ;; esac
117
+ [ -d "$common/worktrees" ] || return 0
118
+ for wdir in "$common"/worktrees/*/; do
119
+ [ -d "$wdir" ] || continue
120
+ gitdir="$(cat "$wdir/gitdir" 2>/dev/null)"; wtpath="${gitdir%/.git}"
121
+ # A zombie: the recorded working-tree path is set but no longer exists on disk.
122
+ [ -n "$wtpath" ] && [ ! -e "$wtpath" ] || continue
123
+ name="$(basename "$wdir")"
124
+ branch="$(sed -n 's#^ref: refs/heads/##p' "$wdir/HEAD" 2>/dev/null)"
125
+ tree=""; [ -f "$wdir/index" ] && tree="$(GIT_INDEX_FILE="$wdir/index" git write-tree 2>/dev/null)"
126
+ tip="$(git rev-parse --verify --quiet "refs/heads/${branch}" 2>/dev/null)"
127
+ tiptree=""; [ -n "$tip" ] && tiptree="$(git rev-parse --verify --quiet "${tip}^{tree}" 2>/dev/null)"
128
+ if [ -n "$tree" ] && [ "$tree" != "$tiptree" ]; then staged=yes; else staged=no; fi
129
+ printf '%s\t%s\t%s\n' "$name" "${branch:-?}" "$staged"
130
+ done
131
+ }
132
+
133
+ # kit_gc_recover_zombies <yes> — the recover-before-prune mechanic (#111). For each ZOMBIE worktree
134
+ # holding staged-but-uncommitted work, recover that delta to its branch via plumbing BEFORE any
135
+ # prune: GIT_INDEX_FILE=<admin>/index git write-tree -> git commit-tree -p <tip> -> update-ref. With
136
+ # <yes>=1 it performs the recovery + sweeps that zombie's stale locks; otherwise it reports what it
137
+ # WOULD recover (dry-run). Never prunes; that stays with the caller (gated on --yes). bash 3.2.
138
+ kit_gc_recover_zombies() {
139
+ local yes="${1:-0}" common wdir name gitdir wtpath branch tree tip tiptree newc msg
140
+ common="$(git rev-parse --git-common-dir 2>/dev/null)" || return 0
141
+ case "$common" in /*) : ;; *) common="$(cd "$common" 2>/dev/null && pwd)" || return 0 ;; esac
142
+ [ -d "$common/worktrees" ] || return 0
143
+ for wdir in "$common"/worktrees/*/; do
144
+ [ -d "$wdir" ] || continue
145
+ gitdir="$(cat "$wdir/gitdir" 2>/dev/null)"; wtpath="${gitdir%/.git}"
146
+ [ -n "$wtpath" ] && [ ! -e "$wtpath" ] || continue # only dead worktrees
147
+ name="$(basename "$wdir")"
148
+ branch="$(sed -n 's#^ref: refs/heads/##p' "$wdir/HEAD" 2>/dev/null)"
149
+ [ -f "$wdir/index" ] || continue
150
+ tree="$(GIT_INDEX_FILE="$wdir/index" git write-tree 2>/dev/null)" || continue
151
+ [ -n "$tree" ] || continue
152
+ tip="$(git rev-parse --verify --quiet "refs/heads/${branch}" 2>/dev/null)"
153
+ tiptree=""; [ -n "$tip" ] && tiptree="$(git rev-parse --verify --quiet "${tip}^{tree}" 2>/dev/null)"
154
+ # No staged delta beyond the tip → nothing to recover; the zombie is safe to prune as-is.
155
+ [ "$tree" = "$tiptree" ] && continue
156
+ [ -n "$branch" ] || { echo " gc: zombie $name is detached with staged work — leaving it for manual recovery" >&2; continue; }
157
+ if [ "$yes" -ne 1 ]; then
158
+ echo " would RECOVER staged work from zombie worktree $name -> a commit on $branch (staged tree $tree)" >&2
159
+ continue
160
+ fi
161
+ msg="kit gc: recovered staged work from zombie worktree $name (recover-before-prune)"
162
+ newc="$(printf '%s\n' "$msg" | GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-kit-gc}" GIT_AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL:-kit-gc@localhost}" GIT_COMMITTER_NAME="${GIT_COMMITTER_NAME:-kit-gc}" GIT_COMMITTER_EMAIL="${GIT_COMMITTER_EMAIL:-kit-gc@localhost}" git commit-tree "$tree" ${tip:+-p "$tip"} 2>/dev/null)"
163
+ if [ -n "$newc" ]; then
164
+ # Compare-and-swap on the tip so a concurrent update is never clobbered.
165
+ if git update-ref "refs/heads/$branch" "$newc" ${tip:+"$tip"} 2>/dev/null; then
166
+ echo " RECOVERED staged work from zombie $name -> commit ${newc%% *} on $branch" >&2
167
+ else
168
+ echo " gc: could not update refs/heads/$branch (moved concurrently?) — zombie $name left intact" >&2
169
+ fi
170
+ fi
171
+ # Stale-lock sweep — safe only because this worktree is provably dead.
172
+ rm -f "$wdir/index.lock" "$wdir/HEAD.lock" 2>/dev/null || true
173
+ done
174
+ }
175
+
74
176
  # kit_gc_prune [--yes] - remove worktrees + local branches whose PR is MERGED (the SAFE rows).
75
177
  # DRY-RUN by default (lists what it WOULD remove); --yes performs the deletions. Never touches a
76
178
  # PROTECTED/ACTIVE/ORPHAN branch, and never a DIRTY worktree (recover-before-prune): a worktree with
@@ -78,35 +180,50 @@ kit_gc_has_prunable() {
78
180
  # already deleted at merge time (gh pr merge --delete-branch); this cleans up the local side.
79
181
  kit_gc_prune() {
80
182
  _kit_gc_load_deps
81
- local repo="$KIT_GC_REPO" yes=0 a path ref b pr
183
+ # `wtpath`, not `path`: under zsh `path` is tied to PATH (special array), so assigning to a bare
184
+ # `path` local would clobber the command search path. A namespaced name is inert.
185
+ local repo="$KIT_GC_REPO" yes=0 a wtpath ref b pr pr_index
82
186
  for a in "$@"; do case "$a" in --yes|-y) yes=1 ;; esac; done
83
187
 
188
+ # All PRs in ONE call, indexed by head branch (#124) — shared by both loops below instead of a
189
+ # per-branch `gh pr list --head` (the N+1 that scaled badly with many stale branches).
190
+ pr_index="$(_kit_gc_pr_index "$repo")"
191
+
84
192
  # Worktrees first - a branch's worktree must be removed before the branch can be deleted.
85
193
  git worktree list --porcelain 2>/dev/null \
86
194
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
87
- | while read -r path ref; do
195
+ | while read -r wtpath ref; do
88
196
  b="${ref#refs/heads/}"
89
197
  case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main|"") continue ;; esac
90
198
  [ -n "$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)" ] && continue
91
- pr="$(gh pr list --repo "$repo" --head "$b" --state all --json state --jq '.[0].state' 2>/dev/null || true)"
92
- [ "$pr" = "MERGED" ] || continue
93
- if [ -n "$(git -C "$path" status --porcelain 2>/dev/null)" ]; then
94
- echo " SKIP dirty worktree $path [$b] - commit/recover before pruning" >&2; continue
199
+ pr="$(_kit_gc_pr_for "$pr_index" "$b")"
200
+ printf '%s' "$pr" | grep -q 'MERGED' || continue
201
+ if [ -n "$(git -C "$wtpath" status --porcelain 2>/dev/null)" ]; then
202
+ echo " SKIP dirty worktree $wtpath [$b] - commit/recover before pruning" >&2; continue
95
203
  fi
96
204
  if [ "$yes" -eq 1 ]; then
97
- git worktree remove --force "$path" 2>/dev/null && echo " removed worktree $path [$b]"
205
+ git worktree remove --force "$wtpath" 2>/dev/null && echo " removed worktree $wtpath [$b]"
98
206
  else
99
- echo " would remove worktree $path [$b] (PR MERGED)"
207
+ echo " would remove worktree $wtpath [$b] (PR MERGED)"
100
208
  fi
101
209
  done
102
- git worktree prune 2>/dev/null || true
103
210
 
104
- # Then local branches whose PR merged (worktree now gone).
105
- for b in $(git branch --format='%(refname:short)' 2>/dev/null); do
211
+ # recover-before-prune (#111): a zombie worktree (working dir gone, admin metadata lingers) may
212
+ # hold staged-but-uncommitted work in its admin index — the ONLY blob->path map. `git worktree
213
+ # prune` deletes that index, orphaning the blobs for a later `git gc` to reap — real data loss in a
214
+ # documented incident. Recover any staged delta to its branch FIRST, and gate the prune sweep
215
+ # behind --yes so it NEVER runs in a dry-run (the previous unconditional prune was the hazard).
216
+ kit_gc_recover_zombies "$yes"
217
+ [ "$yes" -eq 1 ] && { git worktree prune 2>/dev/null || true; }
218
+
219
+ # Then local branches whose PR merged (worktree now gone). while-read (NOT `for b in $(...)`) so
220
+ # branch iteration survives a NUL-polluted IFS (#124).
221
+ git branch --format='%(refname:short)' 2>/dev/null | while IFS= read -r b; do
222
+ [ -n "$b" ] || continue
106
223
  case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) continue ;; esac
107
224
  [ -n "$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)" ] && continue
108
- pr="$(gh pr list --repo "$repo" --head "$b" --state all --json state --jq '.[0].state' 2>/dev/null || true)"
109
- [ "$pr" = "MERGED" ] || continue
225
+ pr="$(_kit_gc_pr_for "$pr_index" "$b")"
226
+ printf '%s' "$pr" | grep -q 'MERGED' || continue
110
227
  if [ "$yes" -eq 1 ]; then
111
228
  git branch -D "$b" >/dev/null 2>&1 && echo " deleted local branch $b (PR MERGED)"
112
229
  else
@@ -16,7 +16,14 @@
16
16
  # KIT_LOCAL_TIMEOUT (default 90s; hooks should pass lower via env when latency matters).
17
17
  # Config: .claude/kit.config.json -> .local {enabled, port, model} (KIT_LOCAL_* env wins).
18
18
 
19
- KIT_LOCAL_CONFIG="${KIT_CONFIG:-.claude/kit.config.json}"
19
+ # Resolve config through the ONE shared resolver (config-path.sh, #69): KIT_CONFIG wins, else a root
20
+ # cckit.config.json or a .claude/kit.config.json (walk up). Falls back to the scaffolded path.
21
+ if ! command -v kit_config_path >/dev/null 2>&1; then
22
+ _kl_dir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
23
+ # shellcheck source=/dev/null
24
+ [ -f "$_kl_dir/config-path.sh" ] && . "$_kl_dir/config-path.sh"; unset _kl_dir
25
+ fi
26
+ KIT_LOCAL_CONFIG="$(kit_config_path 2>/dev/null || true)"; [ -n "$KIT_LOCAL_CONFIG" ] || KIT_LOCAL_CONFIG=".claude/kit.config.json"
20
27
 
21
28
  _kit_local_cfg() { # _kit_local_cfg <jq-path> <default>
22
29
  local v=""
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env bash
2
+ # next.sh — "what can I pick up right now?" The everyday-dev helper (Effort 75 · #79).
3
+ #
4
+ # Surfaces the unblocked set (wave 0 of the plan machine) and the single recommended next issue with
5
+ # the one command to start it. This is the daily-loop counterpart to `cckit wave`: wave fans a whole
6
+ # wave out to subagents; `cckit next` is for a human (or agent) picking up one issue by hand.
7
+ #
8
+ # cckit next the unblocked issues + the top one's start command
9
+ # cckit next --effort <N> scope to effort #N's sub-issues
10
+ # cckit next --llm the unblocked set as TOON rows {number,ctx,title}
11
+ #
12
+ # Requires gh + jq (via the plan machine). bash 3.2 / zsh compatible.
13
+
14
+ NEXT_REPO="${NEXT_REPO:-${KIT_REPO:-}}"
15
+
16
+ next_issue() {
17
+ command -v gh >/dev/null 2>&1 && command -v jq >/dev/null 2>&1 || { echo "next: needs gh + jq" >&2; return 1; }
18
+ local repo="$NEXT_REPO" out="${CCKIT_OUTPUT:-human}" effort="" planargs=()
19
+ while [ "$#" -gt 0 ]; do
20
+ case "$1" in
21
+ --llm|--output=json) out="json"; shift ;;
22
+ --effort) effort="$2"; planargs+=(--effort "$2"); shift 2 ;;
23
+ --effort=*) effort="${1#*=}"; planargs+=(--effort "${1#*=}"); shift ;;
24
+ -h|--help) sed -n '10,13p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; return 0 ;;
25
+ *) echo "next: unknown arg '$1'" >&2; return 2 ;;
26
+ esac
27
+ done
28
+ [ -n "$repo" ] || { echo "next: no repo (set KIT_REPO / NEXT_REPO)" >&2; return 1; }
29
+
30
+ local here; here="$(dirname "${BASH_SOURCE[0]}")"
31
+ # shellcheck source=/dev/null
32
+ . "$here/plan-machine.sh"
33
+ local rows wave0
34
+ rows="$(CCKIT_OUTPUT=tsv plan_machine "${planargs[@]+"${planargs[@]}"}" 2>/dev/null)"
35
+ wave0="$(printf '%s\n' "$rows" | awk -F'\t' '$1==0')"
36
+ if [ -z "$wave0" ]; then
37
+ [ "$out" = "json" ] && echo "[]" || echo "next: nothing unblocked — the board is blocked or empty."
38
+ return 0
39
+ fi
40
+
41
+ if [ "$out" = "json" ]; then
42
+ local json
43
+ json="$(printf '%s\n' "$wave0" | awk -F'\t' '{
44
+ t=$6; sub(/^\[Effort( [0-9]+)?\] [0-9]+ · ?/,"",t); sub(/^\[[A-Za-z]+\] /,"",t)
45
+ print $3"\t"$4"\t"t
46
+ }' \
47
+ | jq -R -s '[ split("\n")[] | select(length>0) | split("\t")
48
+ | {number:(.[0]|tonumber), ctx:.[1], title:.[2]} ]')"
49
+ # shellcheck source=/dev/null
50
+ . "$here/toon.sh"
51
+ printf '%s' "$json" | toon_encode
52
+ return 0
53
+ fi
54
+
55
+ # human: the unblocked table + the top issue's start command, through the rendering seam.
56
+ # shellcheck source=/dev/null
57
+ . "$here/render.sh"
58
+ local top topnum toptitle
59
+ top="$(printf '%s\n' "$wave0" | head -1)"
60
+ topnum="$(printf '%s' "$top" | cut -f3)"
61
+ toptitle="$(printf '%s' "$top" | cut -f6 | sed -E 's/^\[Effort( [0-9]+)?\] [0-9]+ · ?//; s/^\[[A-Za-z]+\] //')"
62
+ {
63
+ printf '# Next up — %s%s\n\n' "$repo" "$([ -n "$effort" ] && echo " · effort #$effort")"
64
+ printf '## Unblocked now\n\n'
65
+ printf '| # | ctx | issue |\n|---|-----|-------|\n'
66
+ printf '%s\n' "$wave0" | awk -F'\t' '{
67
+ t=$6; sub(/^\[Effort( [0-9]+)?\] [0-9]+ · ?/,"",t); sub(/^\[[A-Za-z]+\] /,"",t)
68
+ printf "| #%s | %s | %s |\n", $3, $4, t
69
+ }'
70
+ printf '\n## Pick up #%s — %s\n\n' "$topnum" "$toptitle"
71
+ printf 'Start it in an isolated worktree + branch:\n\n'
72
+ printf '```\ncckit start %s\n```\n\n' "$topnum"
73
+ printf 'Or fan the whole unblocked wave out to subagents with `cckit wave%s`.\n' "$([ -n "$effort" ] && echo " --effort $effort")"
74
+ } | cckit_render
75
+ }
@@ -0,0 +1,72 @@
1
+ #!/bin/sh
2
+ # plan-machine-test.sh — self-test for plan-machine.sh pure helpers under bash AND zsh.
3
+ # Network-free: covers the wave layering, session/file packer, and file-hint parser only
4
+ # (the gh-calling plan_machine orchestration is not exercised).
5
+ # Run: bash scripts/lib/plan-machine-test.sh
6
+
7
+ dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
8
+ TAB=$(printf '\t')
9
+
10
+ if [ -n "${PM_TEST_INNER:-}" ]; then
11
+ . "$dir/plan-machine.sh"
12
+ fail=0
13
+ eq() { # <label> <got> <want>
14
+ if [ "$2" != "$3" ]; then echo "FAIL($PM_TEST_INNER): $1 -> '[$2]', want '[$3]'"; fail=1; fi
15
+ }
16
+
17
+ # _pm_weight
18
+ eq "weight S" "$(_pm_weight S)" "1"
19
+ eq "weight XL" "$(_pm_weight XL)" "8"
20
+ eq "weight ?" "$(_pm_weight '')" "2"
21
+
22
+ # pm_waves: linear chain 3<-2<-1 (1 blocks 2 blocks 3) -> waves 0,1,2.
23
+ chain="$(printf '1\t\n2\t1\n3\t2\n' | pm_waves)"
24
+ eq "chain wave of 1" "$(printf '%s\n' "$chain" | awk -F"$TAB" '$1==1{print $2}')" "0"
25
+ eq "chain wave of 2" "$(printf '%s\n' "$chain" | awk -F"$TAB" '$1==2{print $2}')" "1"
26
+ eq "chain wave of 3" "$(printf '%s\n' "$chain" | awk -F"$TAB" '$1==3{print $2}')" "2"
27
+
28
+ # pm_waves: diamond 1 -> {2,3} -> 4. 2 and 3 share wave 1; 4 in wave 2.
29
+ dia="$(printf '1\t\n2\t1\n3\t1\n4\t2,3\n' | pm_waves)"
30
+ eq "diamond 2==3 wave" \
31
+ "$(printf '%s\n' "$dia" | awk -F"$TAB" '$1==2{print $2}')$(printf '%s\n' "$dia" | awk -F"$TAB" '$1==3{print $2}')" "11"
32
+ eq "diamond wave of 4" "$(printf '%s\n' "$dia" | awk -F"$TAB" '$1==4{print $2}')" "2"
33
+
34
+ # pm_waves: out-of-set blocker (99 absent) is ignored -> node 5 still wave 0.
35
+ oos="$(printf '5\t99\n' | pm_waves)"
36
+ eq "out-of-set blocker ignored" "$(printf '%s\n' "$oos" | awk -F"$TAB" '$1==5{print $2}')" "0"
37
+
38
+ # pm_waves: cycle (1<-2, 2<-1) must terminate, both land in the final wave (no hang, no drop).
39
+ cyc="$(printf '1\t2\n2\t1\n' | pm_waves | wc -l | tr -d ' ')"
40
+ eq "cycle emits both rows" "$cyc" "2"
41
+
42
+ # pm_batches: budget 4, three M(=2) issues with no file hints -> 2,2 then overflow -> batches 0,0,1.
43
+ pk="$(printf '1\tM\t\n2\tM\t\n3\tM\t\n' | pm_batches 4)"
44
+ eq "pack 1 batch0" "$(printf '%s\n' "$pk" | awk -F"$TAB" '$1==1{print $2}')" "0"
45
+ eq "pack 2 batch0" "$(printf '%s\n' "$pk" | awk -F"$TAB" '$1==2{print $2}')" "0"
46
+ eq "pack 3 batch1" "$(printf '%s\n' "$pk" | awk -F"$TAB" '$1==3{print $2}')" "1"
47
+
48
+ # pm_batches: file overlap forces a new batch even under budget.
49
+ fo="$(printf '1\tS\ta.sh\n2\tS\ta.sh\n' | pm_batches 8)"
50
+ eq "overlap splits batch" "$(printf '%s\n' "$fo" | awk -F"$TAB" '$1==2{print $2}')" "1"
51
+ # disjoint files stay in one batch under budget.
52
+ dj="$(printf '1\tS\ta.sh\n2\tS\tb.sh\n' | pm_batches 8)"
53
+ eq "disjoint same batch" "$(printf '%s\n' "$dj" | awk -F"$TAB" '$1==2{print $2}')" "0"
54
+
55
+ # _pm_files: pulls a Files:/Touches: line, normalizes separators.
56
+ eq "files hint comma" "$(_pm_files 'intro
57
+ Files: a.sh, b.sh
58
+ tail')" "a.sh b.sh"
59
+ eq "touches hint" "$(_pm_files 'Touches: x/y.ts')" "x/y.ts"
60
+ eq "no hint" "$(_pm_files 'just prose, no files')" ""
61
+
62
+ if [ "$fail" -eq 0 ]; then echo "PASS($PM_TEST_INNER): plan-machine helpers"; fi
63
+ exit "$fail"
64
+ fi
65
+
66
+ # Outer: re-run under each available shell.
67
+ rc=0
68
+ for sh in bash zsh; do
69
+ command -v "$sh" >/dev/null 2>&1 || continue
70
+ PM_TEST_INNER="$sh" "$sh" "$0" || rc=1
71
+ done
72
+ exit "$rc"