@jeiemgi/cckit 0.7.1 → 0.9.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 (82) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +15 -4
  3. package/README.md +25 -3
  4. package/bin/cckit +91 -8
  5. package/cckit.config.json +1 -1
  6. package/kit.config.schema.json +43 -2
  7. package/package.json +1 -1
  8. package/scripts/autopilot-test.sh +68 -0
  9. package/scripts/autopilot.sh +16 -10
  10. package/scripts/check.sh +14 -1
  11. package/scripts/copilot-loop-test.sh +3 -0
  12. package/scripts/guard-raw-git-ops-test.sh +73 -0
  13. package/scripts/init.sh +68 -8
  14. package/scripts/lib/agent-profile-test.sh +151 -0
  15. package/scripts/lib/agent-profile.sh +195 -0
  16. package/scripts/lib/agent-resolve-test.sh +156 -0
  17. package/scripts/lib/agent-resolve.sh +177 -0
  18. package/scripts/lib/branch-owner-test.sh +99 -0
  19. package/scripts/lib/branch-owner.sh +194 -0
  20. package/scripts/lib/captain-test.sh +86 -0
  21. package/scripts/lib/captain.sh +142 -7
  22. package/scripts/lib/contribute-root-test.sh +40 -0
  23. package/scripts/lib/contribute-root.sh +22 -0
  24. package/scripts/lib/effort-metrics.sh +14 -1
  25. package/scripts/lib/effort-ops-test.sh +54 -2
  26. package/scripts/lib/effort-ops.sh +243 -23
  27. package/scripts/lib/effort-parity-test.sh +5 -3
  28. package/scripts/lib/effort-slug.sh +16 -2
  29. package/scripts/lib/effort.sh +49 -15
  30. package/scripts/lib/gh-project.sh +14 -0
  31. package/scripts/lib/gh-rest-test.sh +301 -0
  32. package/scripts/lib/gh-rest.sh +289 -0
  33. package/scripts/lib/hook-refresh-test.sh +65 -0
  34. package/scripts/lib/hook-refresh.sh +62 -0
  35. package/scripts/lib/kit-config.sh +6 -0
  36. package/scripts/lib/kit-gc-test.sh +178 -1
  37. package/scripts/lib/kit-gc.sh +251 -55
  38. package/scripts/lib/kit-task-ops-test.sh +15 -0
  39. package/scripts/lib/kit-task-ops.sh +231 -58
  40. package/scripts/lib/kto-task-close-test.sh +83 -0
  41. package/scripts/lib/kto-task-pr-merge-test.sh +129 -0
  42. package/scripts/lib/kto-task-pr-test.sh +59 -0
  43. package/scripts/lib/orchestration-runtime-test.sh +142 -0
  44. package/scripts/lib/orchestration-runtime.sh +171 -0
  45. package/scripts/lib/review-stage-test.sh +241 -0
  46. package/scripts/lib/review-stage.sh +259 -0
  47. package/scripts/lib/stage-receipt-test.sh +252 -0
  48. package/scripts/lib/stage-receipt.sh +442 -0
  49. package/scripts/lib/test-env-test.sh +25 -0
  50. package/scripts/lib/test-env.sh +33 -0
  51. package/scripts/lib/worker-brief-test.sh +120 -0
  52. package/scripts/lib/worker-brief.sh +185 -0
  53. package/scripts/lib/worktree-issue.sh +14 -0
  54. package/scripts/lib/worktree-ports-test.sh +146 -0
  55. package/scripts/lib/worktree-start.sh +153 -8
  56. package/scripts/lib/zsh-safety-test.sh +67 -0
  57. package/scripts/orchestrate.sh +124 -22
  58. package/scripts/portable-test.sh +22 -1
  59. package/scripts/review-lint-baseline.txt +28 -0
  60. package/scripts/review-lint-test.sh +271 -0
  61. package/scripts/review-lint.sh +259 -0
  62. package/scripts/review-rules.conf +55 -0
  63. package/scripts/setup-labels.sh +21 -1
  64. package/scripts/skill-frontmatter-baseline.txt +24 -0
  65. package/scripts/skill-frontmatter-lint-test.sh +156 -0
  66. package/scripts/skill-frontmatter-lint.sh +182 -0
  67. package/scripts/task-sync.sh +8 -0
  68. package/scripts/test.sh +10 -1
  69. package/skills/kit-autopilot/SKILL.md +13 -1
  70. package/skills/kit-cleanup/SKILL.md +5 -3
  71. package/skills/kit-contribute/SKILL.md +6 -5
  72. package/skills/kit-effort-close/SKILL.md +8 -3
  73. package/skills/kit-effort-pr/SKILL.md +16 -95
  74. package/skills/kit-gc/SKILL.md +15 -10
  75. package/skills/kit-security-sweep/SKILL.md +4 -6
  76. package/skills/kit-ship/SKILL.md +1 -1
  77. package/skills/kit-task-pr/SKILL.md +2 -0
  78. package/templates/hooks/guard-raw-git-ops.sh.tmpl +193 -0
  79. package/templates/rules/branch-naming.md +6 -0
  80. package/templates/rules/effort-model.md +3 -0
  81. package/templates/rules/naming-and-ids.md +4 -1
  82. package/templates/settings/settings.json.tmpl +4 -2
@@ -20,9 +20,9 @@
20
20
  # kto_compose_issue_body <user-body> <plan> <blocked> <role-slug> echo an issue body
21
21
  # kto_closing_issue_numbers <pr-num> echo the issue #s a merged PR closes
22
22
  # kto_task_pr <num> <summary> [commit-msg] commit+push+open PR (board → In Review)
23
- # kto_task_pr_merge [pr-num] squash-merge + close issues + switch develop + GC
24
- # kto_task_new <title> <role> <kind> <prio> <ms> [plan] [blocked] <body> create issue + board fields
25
- # kto_task_close <num> <summary> [pr-num] close issue + board → Done
23
+ # kto_task_pr_merge [pr-num] [--squash|--merge|--rebase] merge (squash default) + close issues + GC + update the PR base
24
+ # kto_task_new <title> <role> <kind> <prio> [ms] [plan] [blocked] <body> create issue + board fields (ms may be empty)
25
+ # kto_task_close <num> <summary> [pr-num] [--reason R] [--duplicate-of N] [--superseded-by N] close + reason + board → Done
26
26
  # errors: mixed — git/gh ops propagate; board + label side effects are best-effort
27
27
 
28
28
  # --- repo resolution -----------------------------------------------------------------------------
@@ -37,6 +37,19 @@ if [[ -z "${KIT_REPO:-}" ]]; then
37
37
  unset _kto_root
38
38
  fi
39
39
 
40
+ # The GitHub transport switch (#396): in rest mode every gh call below is `gh api repos/…`.
41
+ if ! type cckit_gh_rest >/dev/null 2>&1; then
42
+ if [ -n "${BASH_SOURCE:-}" ]; then _kto_self="${BASH_SOURCE[0]}"
43
+ elif [ -n "${ZSH_VERSION:-}" ]; then eval '_kto_self="${(%):-%x}"'
44
+ else _kto_self="$0"; fi
45
+ _kto_self="$(cd "$(dirname "$_kto_self")" 2>/dev/null && pwd)/gh-rest.sh"
46
+ # shellcheck source=gh-rest.sh
47
+ [ -f "$_kto_self" ] && . "$_kto_self"
48
+ unset _kto_self
49
+ fi
50
+ type cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
51
+ type cckit_board_skipped >/dev/null 2>&1 || cckit_board_skipped() { return 1; }
52
+
40
53
  kto_repo() {
41
54
  printf '%s' "${KIT_TASK_REPO:-${KIT_REPO:-}}"
42
55
  }
@@ -48,8 +61,11 @@ kto_base_branch() {
48
61
 
49
62
  # True (rc 0) iff board updates should run. Off when KIT_PROJECTS_V2 is explicitly "false" — keeps
50
63
  # the op portable to a project with no Projects v2 board (the plugin's KIT_PROJECTS_V2 toggle).
64
+ # In rest mode (#396) the board is GraphQL-only: skipped with one notice, never a failure.
51
65
  kto_board_enabled() {
52
- [[ "${KIT_PROJECTS_V2:-true}" != "false" ]]
66
+ [[ "${KIT_PROJECTS_V2:-true}" != "false" ]] || return 1
67
+ cckit_board_skipped && return 1
68
+ return 0
53
69
  }
54
70
 
55
71
  # --- pure parsers (network-free; covered by the self-test) ---------------------------------------
@@ -81,6 +97,11 @@ kto_labels_kind() {
81
97
  # Network-free (covered by the self-test). Output always passes commitlint_check.
82
98
  kto_pr_title() {
83
99
  local raw="${1:-}" summary="${2:-}" labels="${3:-}" kind ctype desc
100
+ # pr.titleStyle = issue-title (KIT_PR_TITLE_STYLE): the PR title IS the issue title, verbatim — for
101
+ # repos whose convention is "PR title = exact issue title" and that run no commitlint gate.
102
+ if [ "${KIT_PR_TITLE_STYLE:-conventional}" = "issue-title" ] && [ -n "$raw" ]; then
103
+ printf '%s' "$raw"; return 0
104
+ fi
84
105
  # Reuse the ONE commit-lint implementation. bin/cckit sources it; if a bare caller hasn't, pull it
85
106
  # from the repo (kto_task_pr always runs in-repo). Degrades gracefully if it can't be found.
86
107
  if ! type commitlint_check >/dev/null 2>&1; then
@@ -116,10 +137,15 @@ kto_pr_title() {
116
137
  printf '%s: %s' "$ctype" "$desc"
117
138
  }
118
139
 
119
- # Compose a PR body from the kit-task-pr template. Args: <num> <summary> <role-slug>
140
+ # Compose a PR body from the kit-task-pr template. Args: <num> <summary> <role-slug> [issue-title]
141
+ # pr.orchestratorSection = true (KIT_PR_ORCHESTRATOR_SECTION) prepends a 2-line
142
+ # `## For the orchestrator` section: the issue id + title, then what to review.
120
143
  kto_compose_pr_body() {
121
- local num="$1" summary="$2" role="$3" sig=""
144
+ local num="$1" summary="$2" role="$3" title="${4:-}" sig=""
122
145
  if type role_signature >/dev/null 2>&1; then sig="$(role_signature "$role")"; fi
146
+ if [ "${KIT_PR_ORCHESTRATOR_SECTION:-false}" = "true" ]; then
147
+ printf '## For the orchestrator\n\n#%s%s\nReview: the summary below and the diff.\n\n' "$num" "${title:+ · $title}"
148
+ fi
123
149
  cat <<EOF
124
150
  ## Closes
125
151
 
@@ -168,6 +194,7 @@ kto_compose_issue_body() {
168
194
  kto_closing_issue_numbers() {
169
195
  local pr="$1" repo issues body
170
196
  repo="$(kto_repo)"
197
+ if cckit_gh_rest; then gh_rest_pr_closing_issues "$repo" "$pr"; return 0; fi
171
198
  issues=$(gh pr view "$pr" --repo "$repo" --json closingIssuesReferences \
172
199
  --jq '.closingIssuesReferences[].number' 2>/dev/null)
173
200
  if [[ -z "$issues" ]]; then
@@ -197,7 +224,7 @@ _kto_role_option_for_slug() {
197
224
  kto_task_pr() {
198
225
  # Default-safe reads: callers (scripts/kit) may run under `set -u`.
199
226
  local num="${1:-}" summary="${2:-}" commit_msg="${3:-}"
200
- local repo branch meta title labels milestone role author url pr_num pr_node item role_opt issue_item
227
+ local repo branch meta title labels milestone role author="" url pr_num pr_node item role_opt issue_item
201
228
  repo="$(kto_repo)"
202
229
  branch="$(git rev-parse --abbrev-ref HEAD)"
203
230
 
@@ -205,7 +232,8 @@ kto_task_pr() {
205
232
  [[ -n "$num" ]] || { echo "✗ kto_task_pr: branch '$branch' doesn't match <kind>/<N>-<slug> and no issue number passed" >&2; return 1; }
206
233
  [[ -n "$summary" ]] || { echo "✗ kto_task_pr: a Summary is required (never invent one)" >&2; return 1; }
207
234
 
208
- meta=$(gh issue view "$num" --repo "$repo" --json title,labels,milestone) || return 1
235
+ if cckit_gh_rest; then meta=$(gh_rest_issue_json "$repo" "$num") || return 1
236
+ else meta=$(gh issue view "$num" --repo "$repo" --json title,labels,milestone) || return 1; fi
209
237
  title=$(printf '%s' "$meta" | jq -r .title)
210
238
  labels=$(printf '%s' "$meta" | jq -r '[.labels[].name] | join(",")')
211
239
  milestone=$(printf '%s' "$meta" | jq -r '.milestone.title // ""')
@@ -214,8 +242,14 @@ kto_task_pr() {
214
242
  local pr_title; pr_title="$(kto_pr_title "$title" "$summary" "$labels")"
215
243
  [[ -z "$commit_msg" ]] && commit_msg="$pr_title"
216
244
 
217
- if [[ -n "$(git status --porcelain)" ]]; then
218
- git add -A
245
+ # TRACKED changes only: `git add -A` would sweep an untracked .env.local (copied into every worktree)
246
+ # into the PR. Untracked files are listed, never committed — `git add` them yourself if they belong.
247
+ if [[ -n "$(git ls-files --others --exclude-standard 2>/dev/null)" ]]; then
248
+ echo "kto_task_pr: untracked files NOT committed (git add them first if they belong):" >&2
249
+ git ls-files --others --exclude-standard 2>/dev/null | sed 's/^/ /' >&2
250
+ fi
251
+ if [[ -n "$(git status --porcelain --untracked-files=no)" ]]; then
252
+ git add -u
219
253
  if type role_git_author >/dev/null 2>&1; then author="$(role_git_author "$role")"; fi
220
254
  if [[ -n "$author" ]]; then
221
255
  git commit -m "$commit_msg" -m "Co-authored-by: ${author%%|*} <${author##*|}>"
@@ -226,13 +260,18 @@ kto_task_pr() {
226
260
 
227
261
  git push -u origin "$branch" || return 1
228
262
 
263
+ if cckit_gh_rest; then
264
+ url=$(gh_rest_pr_create "$repo" "$(kto_base_branch)" "$branch" "$pr_title" \
265
+ "$(kto_compose_pr_body "$num" "$summary" "$role" "$title")" "$labels" "$milestone") || return 1
266
+ else
229
267
  url=$(gh pr create --repo "$repo" \
230
268
  --base "$(kto_base_branch)" --head "$branch" \
231
269
  --title "$pr_title" \
232
- --body "$(kto_compose_pr_body "$num" "$summary" "$role")" \
270
+ --body "$(kto_compose_pr_body "$num" "$summary" "$role" "$title")" \
233
271
  --label "$labels" \
234
272
  ${milestone:+--milestone "$milestone"} \
235
273
  --assignee "@me") || return 1
274
+ fi
236
275
  echo "✓ $url"
237
276
 
238
277
  # Board: add the PR (In Review) + move the issue to In Review. Best-effort — never fail the op.
@@ -253,13 +292,32 @@ kto_task_pr() {
253
292
  fi
254
293
  }
255
294
 
256
- # kit task pr-merge — squash-merge the open PR for the current branch (rebase-fix conflicts onto
257
- # develop first), close its linked issues (GitHub native auto-close only fires on main), switch the
258
- # main checkout to develop + pull, then clean up the merged branch + worktree.
259
- # Args: [pr-num] (defaults to the open PR for the current branch)
295
+ # kit task pr-merge — merge a PR (squash by default; --merge or --rebase), rebase-fix conflicts onto
296
+ # the PR's base first, close its linked issues (GitHub native auto-close only fires on the default
297
+ # branch), then clean up the PR's head branch + worktree and bring the PR's base up to date.
298
+ # Args: [<pr-num>] [--squash|--merge|--rebase] (the PR defaults to the open PR for the current branch)
299
+ #
300
+ # The PR's base and head come from GitHub, never from the current checkout (#376): the base may be an
301
+ # effort/<N> branch, and a sub PR merged from the effort worktree must not GC the effort branch.
260
302
  kto_task_pr_merge() {
261
- local input_pr="${1:-}"
262
- local repo branch base pr_num mergeable merge_state issues n state wt main_wt
303
+ local input_pr="" method="squash" a
304
+ for a in "$@"; do
305
+ case "$a" in
306
+ --squash) method="squash" ;;
307
+ --merge) method="merge" ;;
308
+ --rebase) method="rebase" ;;
309
+ -*) echo "✗ kto_task_pr_merge: unknown flag '$a' — usage: cckit merge [<pr>] [--squash|--merge|--rebase]" >&2; return 2 ;;
310
+ *)
311
+ # Refuse anything but a PR number (#366). `gh pr merge --help` exits 0, so a flag taken as
312
+ # the number used to "merge", then run the cleanup on the base branch.
313
+ if [[ -n "$input_pr" || ! "$a" =~ ^#?[0-9]+$ ]]; then
314
+ echo "✗ kto_task_pr_merge: '$a' is not a PR number — usage: cckit merge [<pr>] [--squash|--merge|--rebase]" >&2; return 2
315
+ fi
316
+ input_pr="${a#\#}" ;;
317
+ esac
318
+ done
319
+
320
+ local repo branch base pr_num meta merge_state pr_base head issues n state wt main_wt base_wt common
263
321
  repo="$(kto_repo)"
264
322
  base="$(kto_base_branch)"
265
323
  branch="$(git rev-parse --abbrev-ref HEAD)"
@@ -270,88 +328,145 @@ kto_task_pr_merge() {
270
328
 
271
329
  pr_num="$input_pr"
272
330
  if [[ -z "$pr_num" ]]; then
273
- pr_num=$(gh pr list --repo "$repo" --head "$branch" --state open --json number --jq '.[0].number // empty')
331
+ if cckit_gh_rest; then pr_num=$(gh_rest_pr_for_head "$repo" "$branch")
332
+ else pr_num=$(gh pr list --repo "$repo" --head "$branch" --state open --json number --jq '.[0].number // empty'); fi
274
333
  [[ -z "$pr_num" ]] && { echo "✗ No open PR found for branch $branch" >&2; return 1; }
275
334
  fi
276
- echo "→ PR #$pr_num on branch $branch"
277
335
 
278
- mergeable=$(gh pr view "$pr_num" --repo "$repo" --json mergeable,mergeStateStatus \
279
- --jq '{mergeable:.mergeable, status:.mergeStateStatus}')
280
- merge_state=$(printf '%s' "$mergeable" | jq -r .mergeable)
281
- echo " mergeable=$merge_state status=$(printf '%s' "$mergeable" | jq -r .status)"
336
+ if cckit_gh_rest; then meta=$(gh_rest_pr_json "$repo" "$pr_num") || return 1
337
+ else meta=$(gh pr view "$pr_num" --repo "$repo" --json mergeable,mergeStateStatus,baseRefName,headRefName) || return 1; fi
338
+ merge_state=$(printf '%s' "$meta" | jq -r '.mergeable // ""')
339
+ pr_base=$(printf '%s' "$meta" | jq -r '.baseRefName // empty'); pr_base="${pr_base:-$base}"
340
+ head=$(printf '%s' "$meta" | jq -r '.headRefName // empty'); head="${head:-$branch}"
341
+ echo "→ PR #$pr_num: $head → $pr_base ($method)"
342
+ echo " mergeable=$merge_state status=$(printf '%s' "$meta" | jq -r '.mergeStateStatus // ""')"
343
+
344
+ # Branches that are never rebased, force-pushed or deleted here: the repo base, the PR's base
345
+ # (it may be an effort branch), and the release branches (#366).
346
+ _kto_protected() { case "$1" in "$base"|"$pr_base"|main|master|develop|HEAD) return 0 ;; esac; return 1; }
282
347
 
283
348
  if [[ "$merge_state" == "CONFLICTING" ]]; then
284
- echo "→ Conflicts detected — rebasing onto $base..."
285
- git fetch origin "$base"
286
- if git rebase "origin/$base"; then
287
- git push --force-with-lease origin "$branch"
349
+ if [[ "$branch" != "$head" ]] || _kto_protected "$branch"; then
350
+ echo "✗ PR #$pr_num conflicts — rebase $head onto $pr_base from its own worktree (not from $branch)" >&2; return 1
351
+ fi
352
+ echo "→ Conflicts detected — rebasing onto $pr_base..."
353
+ git fetch origin "$pr_base"
354
+ if git rebase "origin/$pr_base"; then
355
+ git push --force-with-lease origin "$head"
288
356
  echo "✓ Rebased and pushed — re-checking mergeability..."
289
357
  else
290
- echo "✗ Rebase had conflicts that need manual resolution — resolve, then run pr-merge again" >&2
358
+ echo "✗ Rebase had conflicts that need manual resolution — resolve, then run cckit merge again" >&2
291
359
  git rebase --abort 2>/dev/null || true
292
360
  return 1
293
361
  fi
294
362
  fi
295
363
 
296
- if ! gh pr merge "$pr_num" --repo "$repo" --squash 2>&1; then
364
+ if cckit_gh_rest; then
365
+ if ! gh_rest_pr_merge "$repo" "$pr_num" "$method" 2>&1; then
366
+ echo "✗ Merge failed — check PR status: https://github.com/$repo/pull/$pr_num" >&2
367
+ return 1
368
+ fi
369
+ elif ! gh pr merge "$pr_num" --repo "$repo" "--$method" 2>&1; then
297
370
  echo "✗ Merge failed — check PR status: https://github.com/$repo/pull/$pr_num" >&2
298
371
  return 1
299
372
  fi
300
- echo "✓ PR #$pr_num merged"
373
+ echo "✓ PR #$pr_num merged ($method)"
301
374
 
302
375
  # Close linked issues (replaces issue-close-on-merge.yml; native auto-close only fires on main).
303
376
  issues="$(kto_closing_issue_numbers "$pr_num")"
304
377
  for n in $issues; do
305
- state=$(gh issue view "$n" --repo "$repo" --json state --jq .state 2>/dev/null)
306
- if [[ "$state" == "OPEN" ]]; then
378
+ if cckit_gh_rest; then state=$(gh_rest_issue_state "$repo" "$n" 2>/dev/null)
379
+ else state=$(gh issue view "$n" --repo "$repo" --json state --jq .state 2>/dev/null); fi
380
+ if [[ "$state" == "OPEN" ]] && cckit_gh_rest; then
381
+ gh_rest_issue_close "$repo" "$n" completed \
382
+ "Closed automatically on merge of #$pr_num to $pr_base (cckit merge; GitHub native auto-close only fires on the default branch)." \
383
+ && echo "✓ Closed issue #$n"
384
+ elif [[ "$state" == "OPEN" ]]; then
307
385
  gh issue close "$n" --repo "$repo" \
308
- --comment "Closed automatically on merge of #$pr_num to $base (kit task pr-merge; GitHub native auto-close only fires on the default branch)." \
386
+ --comment "Closed automatically on merge of #$pr_num to $pr_base (cckit merge; GitHub native auto-close only fires on the default branch)." \
309
387
  && echo "✓ Closed issue #$n"
310
388
  elif [[ -n "$state" ]]; then
311
389
  echo " issue #$n already $state — skipped"
312
390
  fi
313
391
  done
314
392
 
315
- # Locate the branch's worktree + the main worktree, switch develop + pull, clean up.
316
- wt=$(git worktree list --porcelain | awk -v b="refs/heads/$branch" '/^worktree /{p=$2} /^branch /{if($2==b) print p}')
317
- main_wt=$(git worktree list --porcelain | awk '/^worktree /{print $2; exit}')
318
- git -C "$main_wt" checkout "$base" 2>/dev/null || git checkout "$base"
319
- git -C "$main_wt" pull origin "$base" 2>/dev/null || git pull origin "$base"
320
-
321
- if [[ -n "$wt" && "$wt" != "$main_wt" ]]; then
322
- git worktree remove "$wt" --force && echo "✓ Removed worktree $wt"
393
+ # Clean up FROM the main checkout (#371): run from the PR's own worktree, removing it deletes our
394
+ # cwd. Main checkout = parent of the shared git dir; fall back to the first `worktree list` entry.
395
+ wt=$(git worktree list --porcelain | awk -v b="refs/heads/$head" '/^worktree /{p=$2} /^branch /{if($2==b) print p}')
396
+ base_wt=$(git worktree list --porcelain | awk -v b="refs/heads/$pr_base" '/^worktree /{p=$2} /^branch /{if($2==b) print p}')
397
+ common="$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)"
398
+ main_wt=""
399
+ [[ -n "$common" && "$(basename "$common")" == ".git" ]] && main_wt="$(dirname "$common")"
400
+ [[ -n "$main_wt" ]] || main_wt=$(git worktree list --porcelain | awk '/^worktree /{print $2; exit}')
401
+ cd "$main_wt" || { echo "✗ cannot reach the main checkout $main_wt for cleanup" >&2; return 1; }
402
+
403
+ if [[ -n "$wt" && "$wt" != "$main_wt" ]] && ! _kto_protected "$head"; then
404
+ git -C "$main_wt" worktree remove "$wt" --force && echo "✓ Removed worktree $wt"
405
+ fi
406
+ # -D, not -d: a squash merge leaves the branch "not fully merged" by ancestry.
407
+ if _kto_protected "$head"; then
408
+ echo " ($head is protected — no branch cleanup)"
409
+ else
410
+ git -C "$main_wt" branch -D "$head" 2>/dev/null && echo "✓ Deleted local branch $head"
411
+ if cckit_gh_rest; then
412
+ # The cloud proxy rejects branch deletion (#396): keep it and say so.
413
+ echo " (remote branch $head kept — branch deletion is unavailable here; delete it on GitHub)"
414
+ else
415
+ git -C "$main_wt" push origin --delete "$head" 2>/dev/null && echo "✓ Deleted remote branch $head" || echo " (remote branch already gone)"
416
+ fi
417
+ fi
418
+ git -C "$main_wt" worktree prune
419
+
420
+ # Bring the PR's base up to date where it lives. The repo base: the main checkout switches to it and
421
+ # pulls. An effort/<N> base is checked out in its own worktree (git allows one checkout per branch),
422
+ # so that worktree fast-forwards; with no worktree, the local ref is fast-forwarded by fetch.
423
+ if [[ "$pr_base" == "$base" ]]; then
424
+ git -C "$main_wt" checkout "$pr_base" 2>/dev/null || true
425
+ git -C "$main_wt" pull --ff-only origin "$pr_base" 2>/dev/null || true
426
+ echo "✓ On $pr_base in $main_wt, up to date — merged branch + worktree cleaned"
427
+ elif [[ -n "$base_wt" ]]; then
428
+ git -C "$base_wt" pull --ff-only origin "$pr_base" 2>/dev/null || true
429
+ echo "✓ $pr_base updated in $base_wt — continue there (merged branch + worktree cleaned)"
430
+ else
431
+ git -C "$main_wt" fetch origin "$pr_base:$pr_base" 2>/dev/null || git -C "$main_wt" fetch origin "$pr_base" 2>/dev/null || true
432
+ echo "✓ $pr_base fetched (no worktree has it checked out) — merged branch + worktree cleaned"
323
433
  fi
324
- git branch -D "$branch" 2>/dev/null && echo "✓ Deleted local branch $branch"
325
- git push origin --delete "$branch" 2>/dev/null && echo "✓ Deleted remote branch $branch" || echo " (remote branch already gone)"
326
- git worktree prune
327
- echo "✓ On $base, up to date — merged branch + worktree cleaned"
328
434
  }
329
435
 
330
436
  # kit task new — create a GitHub issue with labels + milestone, add it to the board, set Role +
331
437
  # Status(Todo) + Plan Link. Inputs are passed (the caller does the AskUserQuestion). No invented
332
438
  # values; fails loudly on an unknown label family upstream (the caller validates the closed sets).
333
- # Args: <title> <role-display-or-slug> <kind> <priority> <milestone> [plan-link] [blocked-by] <body>
439
+ # Args: <title> <role-display-or-slug> <kind> <priority> [milestone|""] [plan-link] [blocked-by] <body>
334
440
  kto_task_new() {
335
441
  local title="${1:-}" role="${2:-}" kind="${3:-}" prio="${4:-}" milestone="${5:-}" plan="${6:-}" blocked="${7:-}" body_user="${8:-}"
336
442
  local repo role_slug labels body url num node item role_opt
337
443
  repo="$(kto_repo)"
338
- [[ -n "$title" && -n "$role" && -n "$kind" && -n "$prio" && -n "$milestone" ]] || {
339
- echo "✗ kto_task_new: title, role, kind, priority, milestone are required" >&2; return 1; }
444
+ # Milestone is optional: a repo with no milestones (cckit itself) must still be able to file an
445
+ # issue through the verb, or agents fall back to raw `gh issue create` and the board drops it.
446
+ [[ -n "$title" && -n "$role" && -n "$kind" && -n "$prio" ]] || {
447
+ echo "✗ kto_task_new: title, role, kind, priority are required" >&2; return 1; }
340
448
 
341
449
  role_slug="$(printf '%s' "$role" | tr 'A-Z ' 'a-z-')"
342
450
  labels="kind:${kind},priority:${prio},role:${role_slug}"
343
451
  body="$(kto_compose_issue_body "$body_user" "$plan" "$blocked" "$role_slug")"
344
452
 
453
+ if cckit_gh_rest; then
454
+ url=$(gh_rest_issue_create "$repo" "$title" "$body" "$labels" "$milestone") || return 1
455
+ else
345
456
  url=$(gh issue create --repo "$repo" \
346
457
  --title "$title" --body "$body" \
347
- --label "$labels" --milestone "$milestone" --assignee "@me") || return 1
458
+ --label "$labels" ${milestone:+--milestone "$milestone"} --assignee "@me") || return 1
459
+ fi
348
460
  num=$(basename "$url")
349
461
  echo "✓ $url"
350
462
 
351
463
  if kto_board_enabled && type load_project_ids >/dev/null 2>&1 && load_project_ids 2>/dev/null; then
352
464
  node=$(gh api "repos/$repo/issues/$num" --jq .node_id 2>/dev/null)
465
+ item=""
466
+ [[ -n "$node" ]] && item=$(project_add_item "$node" 2>/dev/null || true)
467
+ # Loud, not silent: an issue missing from the board is the failure this verb exists to prevent.
468
+ [[ -z "$item" ]] && echo "⚠ #$num was created but NOT added to the board — add it by hand" >&2
353
469
  if [[ -n "$node" ]]; then
354
- item=$(project_add_item "$node" 2>/dev/null || true)
355
470
  if [[ -n "$item" ]]; then
356
471
  role_opt="$(_kto_role_option_for_slug "$role_slug")"
357
472
  [[ -n "$role_opt" ]] && project_set_single_select "$item" "$ROLE_FIELD_ID" "$role_opt" 2>/dev/null || true
@@ -363,16 +478,62 @@ kto_task_new() {
363
478
  printf '%s' "$num"
364
479
  }
365
480
 
366
- # kit task close — set the board Status to Done and close the issue (with a summary comment).
367
- # Plan-badge flip stays in the skill (it edits a repo file → needs a worktree/PR). Args: <num> <summary> [pr-num]
481
+ # kit task close — set the board Status to Done and close the issue with a summary comment and a
482
+ # close reason (#377). Plan-badge flip stays in the skill (it edits a repo file → needs a worktree/PR).
483
+ # Args: <num> <summary> [pr-num] [--reason completed|not-planned|duplicate] [--duplicate-of <N>]
484
+ # [--superseded-by <N>]
485
+ # --duplicate-of N GitHub state_reason duplicate, linked to #N (implies --reason duplicate)
486
+ # --superseded-by N state_reason not planned + "Superseded by #N" in the comment
487
+ # The summary may be omitted with --duplicate-of / --superseded-by: it defaults to "Duplicate of #N."
488
+ # / "Superseded by #N.". Flags may come anywhere; the closed set is the close-reason vocabulary.
368
489
  kto_task_close() {
369
- local num="${1:-}" summary="${2:-}"
370
- local repo issue_json state item
371
- repo="$(kto_repo)"
490
+ local num="" summary="" reason="" dup="" sup="" a pos=0
491
+ while [[ $# -gt 0 ]]; do
492
+ a="$1"
493
+ case "$a" in
494
+ --reason) reason="${2:-}"; shift 2 ;;
495
+ --reason=*) reason="${a#--reason=}"; shift ;;
496
+ --duplicate-of) dup="${2:-}"; shift 2 ;;
497
+ --superseded-by) sup="${2:-}"; shift 2 ;;
498
+ -*) echo "✗ kto_task_close: unknown flag '$a' — usage: cckit close <issue> [\"<summary>\"] [--reason completed|not-planned|duplicate] [--duplicate-of N] [--superseded-by N]" >&2; return 2 ;;
499
+ *)
500
+ pos=$((pos+1))
501
+ case "$pos" in 1) num="$a" ;; 2) summary="$a" ;; *) : ;; esac # 3rd positional = legacy pr-num
502
+ shift ;;
503
+ esac
504
+ done
505
+ dup="${dup#\#}"; sup="${sup#\#}"
372
506
  [[ -n "$num" ]] || { echo "✗ kto_task_close: issue number required" >&2; return 1; }
507
+ [[ -z "$dup" || "$dup" =~ ^[0-9]+$ ]] || { echo "✗ kto_task_close: --duplicate-of needs an issue number" >&2; return 2; }
508
+ [[ -z "$sup" || "$sup" =~ ^[0-9]+$ ]] || { echo "✗ kto_task_close: --superseded-by needs an issue number" >&2; return 2; }
509
+ [[ -n "$dup" && -n "$sup" ]] && { echo "✗ kto_task_close: --duplicate-of and --superseded-by are exclusive" >&2; return 2; }
510
+
511
+ # The close-reason set (completed · not planned · duplicate · superseded) → GitHub's state_reason.
512
+ local gh_reason
513
+ case "$reason" in
514
+ ""|completed) gh_reason="completed" ;;
515
+ not-planned|"not planned"|not_planned) gh_reason="not planned" ;;
516
+ duplicate) gh_reason="duplicate" ;;
517
+ *) echo "✗ kto_task_close: --reason must be completed, not-planned or duplicate (got '$reason')" >&2; return 2 ;;
518
+ esac
519
+ if [[ -n "$dup" ]]; then
520
+ [[ -z "$reason" || "$gh_reason" == "duplicate" ]] || { echo "✗ kto_task_close: --duplicate-of implies --reason duplicate" >&2; return 2; }
521
+ gh_reason="duplicate"; summary="${summary:-Duplicate of #$dup.}"
522
+ fi
523
+ if [[ -n "$sup" ]]; then
524
+ [[ -z "$reason" || "$gh_reason" == "not planned" ]] || { echo "✗ kto_task_close: --superseded-by implies --reason not-planned" >&2; return 2; }
525
+ gh_reason="not planned"
526
+ if [[ -z "$summary" ]]; then summary="Superseded by #$sup."
527
+ else case "$summary" in *"#$sup"*) : ;; *) summary="$summary"$'\n\n'"Superseded by #$sup." ;; esac
528
+ fi
529
+ fi
530
+ [[ "$gh_reason" == "duplicate" && -z "$dup" ]] && { echo "✗ kto_task_close: --reason duplicate needs --duplicate-of <N>" >&2; return 2; }
373
531
  [[ -n "$summary" ]] || { echo "✗ kto_task_close: a summary is required" >&2; return 1; }
374
532
 
375
- issue_json=$(gh issue view "$num" --repo "$repo" --json state,projectItems) || return 1
533
+ local repo issue_json state item
534
+ repo="$(kto_repo)"
535
+ if cckit_gh_rest; then issue_json=$(gh_rest_issue_json "$repo" "$num") || return 1
536
+ else issue_json=$(gh issue view "$num" --repo "$repo" --json state,projectItems) || return 1; fi
376
537
  state=$(printf '%s' "$issue_json" | jq -r .state)
377
538
 
378
539
  if kto_board_enabled && type load_project_ids >/dev/null 2>&1 && load_project_ids 2>/dev/null; then
@@ -382,8 +543,20 @@ kto_task_close() {
382
543
  echo "✓ Board Status → Done for #$num" || true
383
544
  fi
384
545
 
385
- if [[ "$state" == "OPEN" ]]; then
386
- gh issue close "$num" --repo "$repo" --comment "$summary" && echo "✓ Closed issue #$num"
546
+ if [[ "$state" == "OPEN" ]] && cckit_gh_rest; then
547
+ # REST has no duplicate link (#396): state_reason duplicate, and the comment names the original.
548
+ if [[ -n "$dup" ]]; then
549
+ case "$summary" in *"#$dup"*) : ;; *) summary="$summary"$'\n\n'"Duplicate of #$dup." ;; esac
550
+ fi
551
+ gh_rest_issue_close "$repo" "$num" "$gh_reason" "$summary" || return 1
552
+ echo "✓ Closed issue #$num ($gh_reason${dup:+ of #$dup}${sup:+, superseded by #$sup})"
553
+ elif [[ "$state" == "OPEN" ]]; then
554
+ if [[ -n "$dup" ]]; then
555
+ gh issue close "$num" --repo "$repo" --duplicate-of "$dup" --comment "$summary" || return 1
556
+ else
557
+ gh issue close "$num" --repo "$repo" --reason "$gh_reason" --comment "$summary" || return 1
558
+ fi
559
+ echo "✓ Closed issue #$num ($gh_reason${dup:+ of #$dup}${sup:+, superseded by #$sup})"
387
560
  else
388
561
  echo " issue #$num already $state — board status updated only"
389
562
  fi
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env bash
2
+ # kto-task-close-test.sh — `cckit close` closes with a reason from the close-reason set, maps it to
3
+ # GitHub's state_reason, still posts the summary and still sets the board to Done (#377).
4
+ # Hermetic: stubbed gh and board helpers. Run: bash scripts/lib/kto-task-close-test.sh
5
+ # errors: strict — a test runner: rc 1 on any failed assertion
6
+ set -u
7
+ LIB="$(cd "$(dirname "$0")" && pwd)"
8
+ ROOT="$(cd "$LIB/../.." && pwd)"
9
+ fail=0
10
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
11
+ tc() { if grep -qE -- "$2" "$1"; then echo "ok: $3"; else echo "FAIL: $3 (no /$2/ in $1)"; fail=1; fi; }
12
+ command -v jq >/dev/null 2>&1 || { echo "kto-task-close-test: jq required" >&2; exit 1; }
13
+
14
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
15
+ export GH_LOG="$tmp/gh.log" GH_STATE="OPEN"
16
+ mkdir -p "$tmp/bin"
17
+ cat > "$tmp/bin/gh" <<'SH'
18
+ #!/usr/bin/env bash
19
+ echo "$*" >> "$GH_LOG"
20
+ case "$1 $2" in
21
+ "issue view") printf '{"state":"%s","projectItems":[]}\n' "$GH_STATE" ;;
22
+ *) exit 0 ;;
23
+ esac
24
+ SH
25
+ chmod +x "$tmp/bin/gh"
26
+ export PATH="$tmp/bin:$PATH" KIT_REPO="o/r" KIT_PROJECTS_V2="true"
27
+ unset KIT_CONFIG
28
+
29
+ # shellcheck source=/dev/null
30
+ . "$LIB/kit-task-ops.sh"
31
+ # Board stubs: the close must still move the item to Done whatever the reason.
32
+ BOARD_LOG="$tmp/board.log"
33
+ load_project_ids() { STATUS_FIELD_ID="SF"; STATUS_OPT_DONE="DONE"; return 0; }
34
+ project_find_item_by_issue() { echo "ITEM-$1"; }
35
+ project_set_single_select() { echo "$*" >> "$BOARD_LOG"; }
36
+
37
+ run() { : > "$GH_LOG"; : > "$BOARD_LOG"; kto_task_close "$@" >/dev/null 2>"$tmp/err"; echo $?; }
38
+ closed() { grep -E '^issue close ' "$GH_LOG" | head -1; }
39
+
40
+ t "default close succeeds" "$(run 5 "Shipped in #9.")" 0
41
+ tc "$GH_LOG" '^issue close 5 --repo o/r --reason completed --comment Shipped in #9\.$' "default reason is completed, with the summary"
42
+ tc "$BOARD_LOG" '^ITEM-5 SF DONE$' "the board goes to Done"
43
+
44
+ t "--reason not-planned succeeds" "$(run 6 "Measured; not worth it." --reason not-planned)" 0
45
+ tc "$GH_LOG" '^issue close 6 --repo o/r --reason not planned --comment Measured; not worth it\.$' "not-planned maps to GitHub's 'not planned'"
46
+ tc "$BOARD_LOG" '^ITEM-6 SF DONE$' "not planned still sets Done"
47
+
48
+ t "--duplicate-of succeeds without a summary" "$(run 7 --duplicate-of 4)" 0
49
+ tc "$GH_LOG" '^issue close 7 --repo o/r --duplicate-of 4 --comment Duplicate of #4\.$' "--duplicate-of links the original and defaults the comment"
50
+ tc "$BOARD_LOG" '^ITEM-7 SF DONE$' "a duplicate still sets Done"
51
+ t "--reason duplicate + --duplicate-of" "$(run 7 "Same as #4." --reason duplicate --duplicate-of '#4')" 0
52
+ tc "$GH_LOG" '^issue close 7 --repo o/r --duplicate-of 4 --comment Same as #4\.$' "an explicit summary is kept; '#4' is accepted"
53
+
54
+ t "--superseded-by succeeds" "$(run 8 "Folded into the new effort." --superseded-by 12)" 0
55
+ tc "$GH_LOG" '^issue close 8 --repo o/r --reason not planned --comment Folded into the new effort\.$' "superseded closes as not planned"
56
+ tc "$GH_LOG" '^Superseded by #12\.$' "and the comment says Superseded by #12"
57
+ t "--superseded-by alone defaults the comment" "$(run 8 --superseded-by 12)" 0
58
+ tc "$GH_LOG" '^issue close 8 --repo o/r --reason not planned --comment Superseded by #12\.$' "default superseded comment"
59
+
60
+ t "--reason completed is accepted explicitly" "$(run 9 "Done." --reason completed)" 0
61
+ tc "$GH_LOG" '^issue close 9 --repo o/r --reason completed ' "explicit completed"
62
+
63
+ # refusals: nothing is closed and the board is not touched
64
+ t "an unknown reason is refused (rc 2)" "$(run 10 "x" --reason wontfix)" 2
65
+ t "an unknown flag is refused (rc 2)" "$(run 10 "x" --force)" 2
66
+ t "duplicate without --duplicate-of (rc 2)" "$(run 10 "x" --reason duplicate)" 2
67
+ t "--duplicate-of contradicting --reason (rc 2)" "$(run 10 "x" --reason completed --duplicate-of 4)" 2
68
+ t "--duplicate-of + --superseded-by (rc 2)" "$(run 10 "x" --duplicate-of 4 --superseded-by 5)" 2
69
+ t "a non-number --duplicate-of (rc 2)" "$(run 10 "x" --duplicate-of abc)" 2
70
+ t "no summary and no default (rc 1)" "$(run 10)" 1
71
+ t "refusals closed nothing" "$(grep -c 'issue close' "$GH_LOG")" 0
72
+ t "refusals left the board alone" "$(wc -l < "$BOARD_LOG" | tr -d ' ')" 0
73
+
74
+ # an already-closed issue: board only, no second close
75
+ GH_STATE="CLOSED"
76
+ t "an already-closed issue is fine" "$(run 11 "x" --reason not-planned)" 0
77
+ t "it is not closed again" "$(grep -c '^issue close' "$GH_LOG")" 0
78
+ tc "$BOARD_LOG" '^ITEM-11 SF DONE$' "but the board still goes to Done"
79
+
80
+ t "the dispatcher answers --help" "$("$ROOT/bin/cckit" close --help | cut -c1-19)" "cckit close <issue>"
81
+
82
+ [ "$fail" -eq 0 ] && echo "kto-task-close: all passed" || echo "kto-task-close: FAILURES"
83
+ exit "$fail"