@jeiemgi/cckit 0.7.0 → 0.8.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 (75) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +15 -4
  3. package/README.md +11 -3
  4. package/bin/cckit +86 -7
  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 +65 -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-ops-test.sh +42 -2
  25. package/scripts/lib/effort-ops.sh +157 -14
  26. package/scripts/lib/effort-parity-test.sh +5 -3
  27. package/scripts/lib/effort.sh +38 -14
  28. package/scripts/lib/hook-refresh-test.sh +65 -0
  29. package/scripts/lib/hook-refresh.sh +62 -0
  30. package/scripts/lib/kit-config.sh +6 -0
  31. package/scripts/lib/kit-gc-test.sh +178 -1
  32. package/scripts/lib/kit-gc.sh +233 -55
  33. package/scripts/lib/kit-task-ops-test.sh +15 -0
  34. package/scripts/lib/kit-task-ops.sh +65 -21
  35. package/scripts/lib/kto-task-pr-merge-test.sh +72 -0
  36. package/scripts/lib/kto-task-pr-test.sh +59 -0
  37. package/scripts/lib/orchestration-runtime-test.sh +142 -0
  38. package/scripts/lib/orchestration-runtime.sh +171 -0
  39. package/scripts/lib/review-stage-test.sh +241 -0
  40. package/scripts/lib/review-stage.sh +259 -0
  41. package/scripts/lib/stage-receipt-test.sh +252 -0
  42. package/scripts/lib/stage-receipt.sh +442 -0
  43. package/scripts/lib/test-env-test.sh +25 -0
  44. package/scripts/lib/test-env.sh +30 -0
  45. package/scripts/lib/worker-brief-test.sh +120 -0
  46. package/scripts/lib/worker-brief.sh +185 -0
  47. package/scripts/lib/worktree-ports-test.sh +146 -0
  48. package/scripts/lib/worktree-start.sh +137 -7
  49. package/scripts/lib/zsh-safety-test.sh +67 -0
  50. package/scripts/orchestrate.sh +124 -22
  51. package/scripts/portable-test.sh +22 -1
  52. package/scripts/review-lint-baseline.txt +28 -0
  53. package/scripts/review-lint-test.sh +271 -0
  54. package/scripts/review-lint.sh +259 -0
  55. package/scripts/review-rules.conf +55 -0
  56. package/scripts/setup-labels.sh +21 -1
  57. package/scripts/skill-frontmatter-baseline.txt +24 -0
  58. package/scripts/skill-frontmatter-lint-test.sh +156 -0
  59. package/scripts/skill-frontmatter-lint.sh +182 -0
  60. package/scripts/task-sync.sh +8 -0
  61. package/scripts/test.sh +10 -1
  62. package/skills/kit-autopilot/SKILL.md +13 -1
  63. package/skills/kit-cleanup/SKILL.md +5 -3
  64. package/skills/kit-contribute/SKILL.md +6 -5
  65. package/skills/kit-effort-close/SKILL.md +8 -3
  66. package/skills/kit-effort-pr/SKILL.md +16 -95
  67. package/skills/kit-gc/SKILL.md +15 -10
  68. package/skills/kit-security-sweep/SKILL.md +4 -6
  69. package/skills/kit-ship/SKILL.md +1 -1
  70. package/skills/kit-task-pr/SKILL.md +2 -0
  71. package/templates/hooks/guard-raw-git-ops.sh.tmpl +180 -0
  72. package/templates/rules/branch-naming.md +6 -0
  73. package/templates/rules/effort-model.md +3 -0
  74. package/templates/rules/naming-and-ids.md +4 -1
  75. package/templates/settings/settings.json.tmpl +4 -2
@@ -124,6 +124,92 @@ if [ -n "${CAP_TEST_INNER:-}" ]; then
124
124
  eq "branch fix" "$(_cap_issue_of_branch 'fix/9-roadmap')" "9"
125
125
  eq "branch effort" "$(_cap_issue_of_branch 'effort/123-copilot')" "123"
126
126
  eq "branch plain" "$(_cap_issue_of_branch 'main')" ""
127
+ # The effort sub form. The local regex needs a "-" straight after the digits and so returns
128
+ # nothing here; wt_issue_number is what parses it. A sub-branch PR with no issue has no review
129
+ # verdict to look up and would read CLEAN (#348).
130
+ if command -v wt_issue_number >/dev/null 2>&1; then
131
+ eq "branch effort sub" "$(_cap_issue_of_branch 'sub/1604a-parser')" "1604"
132
+ fi
133
+
134
+ # ── the agent review gate (#348) ─────────────────────────────────────────────────────────────
135
+ # cap_review_summary: one token out of a receipt, the same shape cap_checks_summary has.
136
+ if command -v jq >/dev/null 2>&1; then
137
+ eq "review: gate pass" "$(printf '{"gate":"pass"}' | cap_review_summary)" "PASS"
138
+ eq "review: gate fail" "$(printf '{"gate":"fail"}' | cap_review_summary)" "FAIL"
139
+ # The detail after the dash is the useful half of a real verdict; matching the whole field
140
+ # would score every annotated receipt as NONE.
141
+ eq "review: annotated fail" "$(printf '{"gate":"fail — the retry loop never exits"}' | cap_review_summary)" "FAIL"
142
+ eq "review: annotated pass" "$(printf '{"gate":"pass — read the whole diff"}' | cap_review_summary)" "PASS"
143
+ eq "review: no gate field" "$(printf '{"outcome":"pr-open"}' | cap_review_summary)" "NONE"
144
+ eq "review: empty stdin" "$(printf '' | cap_review_summary)" "NONE"
145
+ eq "review: unknown word" "$(printf '{"gate":"maybe"}' | cap_review_summary)" "NONE"
146
+ fi
147
+
148
+ # A recorded FAIL always blocks — it is evidence, not absence of it, so no flag gates it.
149
+ ( KIT_CAPTAIN_REQUIRE_REVIEW=0
150
+ eq "review FAIL blocks even unrequired" "$(cap_classify MERGEABLE CLEAN PASS FAIL)" "REVIEW_FAILING" ) || fail=1
151
+ eq "REVIEW_FAILING -> fix" "$(cap_action REVIEW_FAILING)" "fix"
152
+
153
+ # A MISSING verdict is gated on the requirement, exactly like CHECKS_MISSING.
154
+ ( KIT_CAPTAIN_REQUIRE_REVIEW=0
155
+ eq "no verdict merges when not required" "$(cap_classify MERGEABLE CLEAN PASS NONE)" "CLEAN" ) || fail=1
156
+ ( KIT_CAPTAIN_REQUIRE_REVIEW=1
157
+ eq "no verdict blocks when required" "$(cap_classify MERGEABLE CLEAN PASS NONE)" "REVIEW_MISSING" ) || fail=1
158
+ eq "REVIEW_MISSING -> verify" "$(cap_action REVIEW_MISSING)" "verify"
159
+
160
+ # A passing verdict never blocks, required or not.
161
+ ( KIT_CAPTAIN_REQUIRE_REVIEW=1
162
+ eq "a passing verdict merges" "$(cap_classify MERGEABLE CLEAN PASS PASS)" "CLEAN" ) || fail=1
163
+
164
+ # Red CI outranks the review: the PR will be rewritten before the review matters.
165
+ ( KIT_CAPTAIN_REQUIRE_REVIEW=1
166
+ eq "failing checks outrank a missing review" "$(cap_classify MERGEABLE CLEAN FAIL NONE)" "CHECKS_FAILING"
167
+ eq "a conflict outranks a failing review" "$(cap_classify CONFLICTING DIRTY PASS FAIL)" "CONFLICTING"
168
+ eq "a draft outranks a failing review" "$(cap_classify MERGEABLE DRAFT PASS FAIL)" "DRAFT" ) || fail=1
169
+
170
+ # Omitting the argument must not change any existing verdict — every caller predating #348 passes
171
+ # three arguments, and a default of anything but NONE would re-decide them.
172
+ eq "a 3-arg call still reads CLEAN" "$(cap_classify MERGEABLE CLEAN PASS)" "CLEAN"
173
+ ( KIT_CAPTAIN_REQUIRE_REVIEW=1
174
+ eq "a 3-arg call defaults to NONE" "$(cap_classify MERGEABLE CLEAN PASS)" "REVIEW_MISSING" ) || fail=1
175
+
176
+ # head_sha binds a verdict to a revision (#351 review). Without it a PASS earned on one commit
177
+ # gates every commit pushed after it — sr_latest is newest-by-issue and knows nothing about SHAs.
178
+ if command -v jq >/dev/null 2>&1; then
179
+ eq "review: a matching head still passes" \
180
+ "$(printf '{"gate":"pass","head_sha":"abc"}' | cap_review_summary abc)" "PASS"
181
+ eq "review: a stale verdict reads NONE, not PASS" \
182
+ "$(printf '{"gate":"pass","head_sha":"abc"}' | cap_review_summary def)" "NONE"
183
+ # A stale FAIL is also absence: the reviewer read code that is no longer there, so it is neither
184
+ # a finding against this revision nor a pass for it.
185
+ eq "review: a stale fail reads NONE too" \
186
+ "$(printf '{"gate":"fail","head_sha":"abc"}' | cap_review_summary def)" "NONE"
187
+ # No head asked: the check is skipped. This is also what keeps a receipt written before head_sha
188
+ # existed (empty field) readable instead of permanently stale.
189
+ eq "review: no head asked skips the check" \
190
+ "$(printf '{"gate":"pass","head_sha":"abc"}' | cap_review_summary)" "PASS"
191
+ eq "review: a head-less receipt does not match a head" \
192
+ "$(printf '{"gate":"pass"}' | cap_review_summary abc)" "NONE"
193
+ fi
194
+
195
+ # The config bridge: review.command implies the requirement; requireReview:false opts back out.
196
+ if command -v jq >/dev/null 2>&1; then
197
+ revbridge() {
198
+ ( KIT_CONFIG="$1"; KIT_CAPTAIN_FLOORS=; KIT_CAPTAIN_EXTRA_GLOBS=; KIT_CAPTAIN_REQUIRE_CHECKS=
199
+ KIT_CAPTAIN_REQUIRE_REVIEW="$2"
200
+ _cap_load_policy_config; printf '%s' "${KIT_CAPTAIN_REQUIRE_REVIEW:-unset}" )
201
+ }
202
+ rc1="$(mktemp)"; printf '{"review":{"command":"true"}}' > "$rc1"
203
+ rc2="$(mktemp)"; printf '{"review":{"command":"true"},"captain":{"mergePolicy":{"requireReview":false}}}' > "$rc2"
204
+ rc3="$(mktemp)"; printf '{"captain":{"mergePolicy":{"requireReview":true}}}' > "$rc3"
205
+ rc4="$(mktemp)"; printf '{}' > "$rc4"
206
+ eq "bridge: review.command implies required" "$(revbridge "$rc1" "")" "1"
207
+ eq "bridge: requireReview:false opts out" "$(revbridge "$rc2" "")" "0"
208
+ eq "bridge: requireReview:true with no cmd" "$(revbridge "$rc3" "")" "1"
209
+ eq "bridge: neither is off" "$(revbridge "$rc4" "")" "0"
210
+ eq "bridge: env 0 beats review.command" "$(revbridge "$rc1" "0")" "0"
211
+ rm -f "$rc1" "$rc2" "$rc3" "$rc4"
212
+ fi
127
213
 
128
214
  if [ "$fail" -eq 0 ]; then echo "PASS($CAP_TEST_INNER): captain gate policy"; fi
129
215
  exit "$fail"
@@ -34,6 +34,20 @@ if [ -f "$(dirname "$_cap_self")/kit-state.sh" ]; then
34
34
  # shellcheck source=kit-state.sh
35
35
  . "$(dirname "$_cap_self")/kit-state.sh"
36
36
  fi
37
+ # The review stage (#346) is sourced here, not left to a caller: captain_pass guards on
38
+ # `command -v rs_needs_dispatch`, so a missing source would switch the stage off silently rather
39
+ # than fail. CAPTAIN_CFG is what rs_command reads `review.command` out of — resolved through
40
+ # kit_config_path because two config layouts are current and neither path is safe to hard-code.
41
+ if [ -f "$(dirname "$_cap_self")/review-stage.sh" ]; then
42
+ # shellcheck source=config-path.sh
43
+ [ -f "$(dirname "$_cap_self")/config-path.sh" ] && . "$(dirname "$_cap_self")/config-path.sh"
44
+ # shellcheck source=review-stage.sh
45
+ . "$(dirname "$_cap_self")/review-stage.sh"
46
+ fi
47
+ if [ -z "${CAPTAIN_CFG:-}" ] && command -v kit_config_path >/dev/null 2>&1; then
48
+ CAPTAIN_CFG="$(kit_config_path 2>/dev/null || true)"
49
+ fi
50
+ CAPTAIN_CFG="${CAPTAIN_CFG:-}"
37
51
  unset _cap_self
38
52
  if [ -z "${CAPTAIN_STATE:-}" ] && command -v kit_state_file >/dev/null 2>&1; then
39
53
  CAPTAIN_STATE="$(kit_state_file captain.state)"
@@ -53,6 +67,36 @@ cap_checks_summary() {
53
67
  else "NONE" end' 2>/dev/null || echo NONE
54
68
  }
55
69
 
70
+ # cap_review_summary — collapse a review receipt (stdin, sr_record's JSON) to one token:
71
+ # FAIL (the reviewer said gate: fail) > PASS (gate: pass) > NONE (no receipt, or no gate line).
72
+ # Pure apart from jq. Empty stdin is NONE, which is the state every repo is in until a reviewer runs.
73
+ #
74
+ # `gate` is read on its FIRST WORD, the same way sr_invalid_fields reads it: a receipt says
75
+ # `fail — the retry loop never exits` and the detail after the dash is the useful half, not part of
76
+ # the verdict. Matching the whole field would score every annotated verdict as NONE.
77
+ #
78
+ # <head-sha> BINDS THE VERDICT TO A REVISION. sr_latest returns the newest receipt for an issue and
79
+ # knows nothing about commits, so without this a PASS earned on one commit gates every commit
80
+ # pushed after it — the reviewer approved code that is no longer there. Given a head, a receipt
81
+ # recorded against a different one reads NONE: not a failure, an absence, which is what it is. An
82
+ # empty head skips the check, for a caller with no revision to compare (and for every receipt
83
+ # written before head_sha existed, whose field is empty and would otherwise never match).
84
+ cap_review_summary() {
85
+ local head="${1:-}"
86
+ command -v jq >/dev/null 2>&1 || { echo NONE; return 0; }
87
+ # Empty stdin is the common case — sr_latest echoes nothing when there is no receipt — and jq
88
+ # reads no values from it, printing nothing and exiting 0. So the `|| echo NONE` fallback never
89
+ # fires and the caller gets an empty token instead of a verdict. Substitute an empty object.
90
+ local j; j="$(cat)"
91
+ [ -n "$j" ] || j='{}'
92
+ printf '%s' "$j" | jq -r --arg head "$head" '
93
+ if ($head != "" and ((.head_sha // "") != $head)) then "NONE"
94
+ else (.gate // "") | split(" ")[0] | ascii_upcase
95
+ | if . == "FAIL" then "FAIL" elif . == "PASS" then "PASS" else "NONE" end
96
+ end' 2>/dev/null \
97
+ || echo NONE
98
+ }
99
+
56
100
  # cap_classify <mergeable> <mergeStateStatus> <checksSummary> — pure verdict for one PR.
57
101
  # mergeable: MERGEABLE|CONFLICTING|UNKNOWN · mss: CLEAN|DIRTY|DRAFT|BLOCKED|BEHIND|UNSTABLE|…
58
102
  #
@@ -69,8 +113,18 @@ cap_checks_summary() {
69
113
  # today's behaviour and `captain_pass` instead PRINTS the assumption it is making (see the "green was
70
114
  # assumed" advisory) with the key that closes it. The narrow guard below is deliberate: only the
71
115
  # verdict that would have been CLEAN can change, so nothing else in the vocabulary moves.
116
+ #
117
+ # REVIEW_FAILING / REVIEW_MISSING (#348) are the same two shapes for the agent review stage, and
118
+ # they split along the same line: a recorded `gate: fail` is EVIDENCE and always blocks, while a
119
+ # missing verdict is ABSENCE of evidence and blocks only when review is required. Absence is the
120
+ # state of every repo that has not run a reviewer — cckit's own included, which has no receipts at
121
+ # all — so requiring it unconditionally would stop every captain on upgrade.
122
+ #
123
+ # Required when review.command is configured (running a reviewer IS the opt-in) or when
124
+ # KIT_CAPTAIN_REQUIRE_REVIEW=1 / captain.mergePolicy.requireReview:true says so; env wins, as it
125
+ # does for checks. Off in both, captain_pass prints the advisory naming the key instead.
72
126
  cap_classify() {
73
- local mergeable="$1" mss="$2" checks="$3" require
127
+ local mergeable="$1" mss="$2" checks="$3" review="${4:-NONE}" require review_req
74
128
  case "$mss" in DRAFT) echo DRAFT; return 0 ;; esac
75
129
  case "$mergeable" in CONFLICTING) echo CONFLICTING; return 0 ;; esac
76
130
  case "$mss" in DIRTY) echo CONFLICTING; return 0 ;; esac
@@ -78,14 +132,19 @@ cap_classify() {
78
132
  FAIL) echo CHECKS_FAILING; return 0 ;;
79
133
  PENDING) echo CHECKS_PENDING; return 0 ;;
80
134
  esac
135
+ # A recorded failing review outranks the requirement flag: the reviewer ran and said no. This sits
136
+ # AFTER the check verdicts because a PR with red CI will be rewritten before the review matters.
137
+ case "$review" in FAIL) echo REVIEW_FAILING; return 0 ;; esac
81
138
  # Literal case patterns only (a variable in a case pattern matches literally under zsh).
82
139
  case "${KIT_CAPTAIN_REQUIRE_CHECKS:-0}" in 1|true|yes|on) require=1 ;; *) require=0 ;; esac
140
+ case "${KIT_CAPTAIN_REQUIRE_REVIEW:-0}" in 1|true|yes|on) review_req=1 ;; *) review_req=0 ;; esac
83
141
  case "$mergeable" in
84
142
  MERGEABLE)
85
143
  # passing or no required checks, and not draft/dirty/conflicting -> ready.
86
144
  case "$mss" in
87
145
  CLEAN|UNSTABLE|HAS_HOOKS|"")
88
146
  if [ "$require" = "1" ] && [ "$checks" = "NONE" ]; then echo CHECKS_MISSING; return 0; fi
147
+ if [ "$review_req" = "1" ] && [ "$review" = "NONE" ]; then echo REVIEW_MISSING; return 0; fi
89
148
  echo CLEAN; return 0 ;;
90
149
  esac
91
150
  echo BLOCKED; return 0 ;;
@@ -104,6 +163,8 @@ cap_action() {
104
163
  CHECKS_FAILING) echo fix ;;
105
164
  CHECKS_PENDING) echo wait ;;
106
165
  CHECKS_MISSING) echo verify ;;
166
+ REVIEW_FAILING) echo fix ;;
167
+ REVIEW_MISSING) echo verify ;;
107
168
  DRAFT) echo wait ;;
108
169
  HELD) echo hold ;;
109
170
  *) echo skip ;;
@@ -200,11 +261,35 @@ _cap_load_policy_config() {
200
261
  true) KIT_CAPTAIN_REQUIRE_CHECKS=1 ;; false) KIT_CAPTAIN_REQUIRE_CHECKS=0 ;;
201
262
  esac
202
263
  fi
203
- export KIT_CAPTAIN_FLOORS KIT_CAPTAIN_EXTRA_GLOBS KIT_CAPTAIN_REQUIRE_CHECKS
264
+ # Review requirement (#348). Configuring review.command IS the opt-in — a project that runs a
265
+ # reviewer on every mergeable PR and then merges the ones the reviewer never got to has gained
266
+ # nothing. requireReview:false turns that implication off without giving up the reviewer, and an
267
+ # explicit requireReview:true requires a verdict with no review.command (a reviewer run by hand
268
+ # or by CI, recorded through `cckit receipt`).
269
+ if [ -z "${KIT_CAPTAIN_REQUIRE_REVIEW:-}" ]; then
270
+ case "$(_cap_cfg_bool "$cfg" requireReview)" in
271
+ true) KIT_CAPTAIN_REQUIRE_REVIEW=1 ;;
272
+ false) KIT_CAPTAIN_REQUIRE_REVIEW=0 ;;
273
+ *) if command -v rs_enabled >/dev/null 2>&1 && rs_enabled "$cfg"; then
274
+ KIT_CAPTAIN_REQUIRE_REVIEW=1
275
+ else
276
+ KIT_CAPTAIN_REQUIRE_REVIEW=0
277
+ fi ;;
278
+ esac
279
+ fi
280
+ export KIT_CAPTAIN_FLOORS KIT_CAPTAIN_EXTRA_GLOBS KIT_CAPTAIN_REQUIRE_CHECKS KIT_CAPTAIN_REQUIRE_REVIEW
204
281
  }
205
282
 
206
283
  # _cap_issue_of_branch <branch> — issue number a flow branch encodes (task/47-x, fix/9-y, effort/12-z).
284
+ #
285
+ # wt_issue_number when it is loaded, because the local regex requires a "-" immediately after the
286
+ # digits and so does NOT match the effort sub form `sub/<N><letter>-<slug>`. That was cosmetic while
287
+ # the number was only printed; it is not now — the review verdict is looked up BY issue (#348), so a
288
+ # sub-branch PR parsing to no issue would silently have no verdict to gate on and read CLEAN.
207
289
  _cap_issue_of_branch() {
290
+ if command -v wt_issue_number >/dev/null 2>&1; then
291
+ wt_issue_number "$1"; return 0
292
+ fi
208
293
  printf '%s' "$1" | sed -nE 's#^[a-z]+/([0-9]+)-.*#\1#p'
209
294
  }
210
295
 
@@ -214,8 +299,8 @@ _cap_issue_of_branch() {
214
299
  # carries the floor that tripped (empty otherwise). `checks` is the raw rollup token (FAIL/PENDING/
215
300
  # PASS/NONE) — REPORTED, not re-decided, so captain_pass can say when a merge rests on NONE.
216
301
  captain_gate() {
217
- local repo="$CAPTAIN_REPO" pr="$1" j mergeable mss checks issue state action title branch files labels reason
218
- j="$(gh pr view "$pr" --repo "$repo" --json number,title,headRefName,mergeable,mergeStateStatus,statusCheckRollup,files,labels 2>/dev/null)" \
302
+ local repo="$CAPTAIN_REPO" pr="$1" j mergeable mss checks review head issue state action title branch files labels reason
303
+ j="$(gh pr view "$pr" --repo "$repo" --json number,title,headRefName,headRefOid,mergeable,mergeStateStatus,statusCheckRollup,files,labels 2>/dev/null)" \
219
304
  || { echo "captain: cannot read PR #$pr" >&2; return 1; }
220
305
  mergeable="$(printf '%s' "$j" | jq -r '.mergeable // "UNKNOWN"')"
221
306
  mss="$(printf '%s' "$j" | jq -r '.mergeStateStatus // "UNKNOWN"')"
@@ -225,7 +310,16 @@ captain_gate() {
225
310
  files="$(printf '%s' "$j" | jq -r '.files[]?.path // empty')"
226
311
  labels="$(printf '%s' "$j" | jq -r '[.labels[]?.name] | join(" ")')"
227
312
  issue="$(_cap_issue_of_branch "$branch")"
228
- state="$(cap_classify "$mergeable" "$mss" "$checks")"
313
+ # The review verdict is local state, not a GitHub field: sr_latest reads the receipt this repo's
314
+ # reviewer wrote. No receipt, no issue, or no stage-receipt lib all collapse to NONE — absence,
315
+ # which cap_classify gates on only when review is required.
316
+ head="$(printf '%s' "$j" | jq -r '.headRefOid // ""')"
317
+ review=NONE
318
+ if [ -n "$issue" ] && command -v sr_latest >/dev/null 2>&1; then
319
+ review="$(sr_latest "$issue" review 2>/dev/null | cap_review_summary "$head")"
320
+ [ -n "$review" ] || review=NONE
321
+ fi
322
+ state="$(cap_classify "$mergeable" "$mss" "$checks" "$review")"
229
323
  action="$(cap_action "$state")"
230
324
  reason=""
231
325
  # Policy floor: only a would-be merge is at risk, so gate only the CLEAN/merge verdict.
@@ -233,7 +327,7 @@ captain_gate() {
233
327
  reason="$(cap_policy_floor "$files" "$labels")"
234
328
  [ -n "$reason" ] && { state="HELD"; action="hold"; }
235
329
  fi
236
- printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$pr" "${issue:-—}" "$state" "$action" "$title" "$reason" "$checks"
330
+ printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$pr" "${issue:-—}" "$state" "$action" "$title" "$reason" "$checks" "$head"
237
331
  }
238
332
 
239
333
  # _cap_open_prs [effort] — open PR numbers, optionally only those whose branch-issue is a sub of <effort>.
@@ -271,7 +365,7 @@ captain_pass() {
271
365
  CAPTAIN_MERGED=0
272
366
  _cap_load_policy_config # bridge captain.mergePolicy from config into the floor env (env wins)
273
367
 
274
- local prs pr row state action issue title reason checks merged_any=0 unproven=0
368
+ local prs pr row state action issue issue_num title reason checks head merged_any=0 unproven=0 unreviewed=0
275
369
  prs="$(_cap_open_prs "$effort")"
276
370
  [ -n "$prs" ] || { echo "captain: no open PRs in scope"; return 0; }
277
371
 
@@ -289,10 +383,41 @@ captain_pass() {
289
383
  title="$(printf '%s' "$row" | cut -f5)"
290
384
  reason="$(printf '%s' "$row" | cut -f6)"
291
385
  checks="$(printf '%s' "$row" | cut -f7)"
386
+ head="$(printf '%s' "$row" | cut -f8)"
387
+ # Field 2 is a DISPLAY field carrying `—` for a PR whose branch encodes no issue. rs_issue_num
388
+ # is what everything keying storage off it goes through — see its comment for why.
389
+ issue_num="$issue"
390
+ command -v rs_issue_num >/dev/null 2>&1 && issue_num="$(rs_issue_num "$issue")"
292
391
  { printf '%s\t%s\n' "$pr" "$state" >> "$CAPTAIN_STATE"; } 2>/dev/null || true
293
392
  # A merge resting on an EMPTY rollup: green was assumed, never observed. Counted here and named
294
393
  # once at the end of the pass, so the assumption is on screen even with the requirement OFF.
295
394
  if [ "$action" = "merge" ] && [ "$checks" = "NONE" ]; then unproven=$((unproven + 1)); fi
395
+ # Same shape for the review stage: a PR the captain will merge with NO recorded verdict. Counted
396
+ # only while review is NOT required, because once it is those PRs read REVIEW_MISSING -> verify
397
+ # and never reach `merge` — so the advisory self-silences exactly like the checks one.
398
+ if [ "$action" = "merge" ] && [ "${KIT_CAPTAIN_REQUIRE_REVIEW:-0}" != "1" ]; then
399
+ local rv=NONE
400
+ [ -n "$issue_num" ] && command -v sr_latest >/dev/null 2>&1 \
401
+ && rv="$(sr_latest "$issue_num" review 2>/dev/null | cap_review_summary "$head")"
402
+ [ "${rv:-NONE}" = "NONE" ] && unreviewed=$((unreviewed + 1))
403
+ fi
404
+ # Review stage (#346). Dispatch runs INSIDE this pass, before the merge decision is acted on,
405
+ # so a PR the captain would merge gets a reviewer first. A fresh dispatch never unblocks the
406
+ # same pass: the receipt it writes is read by the gate on the NEXT pass, which is correct —
407
+ # a verdict must be recorded before it can be gated on, not assumed while the agent is running.
408
+ # Off unless review.command is configured; rs_dispatch echoes rc 4 and does nothing then.
409
+ if command -v rs_needs_dispatch >/dev/null 2>&1 && rs_enabled "$CAPTAIN_CFG"; then
410
+ local have=0; rs_have_receipt "$issue_num" "$head" && have=1
411
+ if rs_needs_dispatch "$state" "$action" "$have"; then
412
+ if rs_dispatch "$CAPTAIN_CFG" "$repo" "$pr" "$issue_num" "$head" >/dev/null; then
413
+ printf ' PR #%-4s %-14s -> reviewed (#%s %s)\n' "$pr" "$state" "$issue" "$title"
414
+ fi
415
+ # Held whether or not the reviewer succeeded. A failed dispatch leaves no receipt, and
416
+ # no receipt is missing evidence, not a pass — the same rule the empty-rollup advisory
417
+ # below states for checks.
418
+ action=verify
419
+ fi
420
+ fi
296
421
  if [ "$action" = "merge" ] && [ "$do_merge" = "1" ] && [ "$dry" = "0" ]; then
297
422
  if gh pr merge "$pr" --repo "$repo" --squash --delete-branch >/dev/null 2>&1; then
298
423
  printf ' PR #%-4s %-14s MERGED (#%s %s)\n' "$pr" "$state" "$issue" "$title"
@@ -321,6 +446,16 @@ captain_pass() {
321
446
  printf ' -> verify, and the captain will not merge them.\n'
322
447
  fi
323
448
 
449
+ # The review stage's counterpart advisory. It fires only when a reviewer could have run and did
450
+ # not, names the two keys that close it, and goes quiet the moment either is on.
451
+ if [ "$unreviewed" -gt 0 ]; then
452
+ printf 'captain: %s PR(s) read CLEAN with NO agent review recorded.\n' "$unreviewed"
453
+ printf ' run one: set review.command in your kit config (the captain then dispatches a\n'
454
+ printf ' reviewer and requires its verdict). To require a verdict WITHOUT cckit running\n'
455
+ printf ' the reviewer, set captain.mergePolicy.requireReview:true. Those PRs then read\n'
456
+ printf ' REVIEW_MISSING -> verify, and the captain will not merge them.\n'
457
+ fi
458
+
324
459
  # advance: after merges, the newly-unblocked work shows up as the next wave.
325
460
  if [ "$merged_any" -gt 0 ]; then
326
461
  echo "captain: merged $merged_any — next unblocked wave:"
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env bash
2
+ # contribute-root-test.sh — `cckit contribute` acts on the cckit checkout containing cwd (#368).
3
+ # Hermetic: a fixture "cckit" repo whose scripts/contribute.sh is a stub that reports where it ran,
4
+ # so the real gate (scripts/check.sh) never runs from inside the test suite.
5
+ # Run: bash scripts/lib/contribute-root-test.sh
6
+ # errors: strict — a test runner: rc 1 on any failed assertion
7
+ set -u
8
+ ROOT="$(cd "$(dirname "$0")/../.." && 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
+ # shellcheck source=/dev/null
12
+ . "$ROOT/scripts/lib/contribute-root.sh"
13
+
14
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
15
+ fx="$tmp/cckit"; plain="$tmp/plain"
16
+ git init -q "$fx"
17
+ mkdir -p "$fx/bin" "$fx/scripts"
18
+ printf '#!/usr/bin/env bash\n' > "$fx/bin/cckit"
19
+ printf '#!/usr/bin/env bash\necho "STUB pwd=$(pwd -P) branch=$(git rev-parse --abbrev-ref HEAD) args=$*"\n' > "$fx/scripts/contribute.sh"
20
+ chmod +x "$fx/bin/cckit" "$fx/scripts/contribute.sh"
21
+ git -C "$fx" add -A && git -C "$fx" -c user.email=t@t -c user.name=t commit -q -m init
22
+ wt="$fx/.claude/worktrees/task+1-x"
23
+ git -C "$fx" worktree add -q -b task/1-x "$wt"
24
+ git init -q "$plain"
25
+ wtp="$(cd "$wt" && pwd -P)"
26
+
27
+ t "a worktree resolves to itself" "$(cd "$(contribute_root "$wt/scripts" /installed)" && pwd -P)" "$wtp"
28
+ t "the main checkout resolves to itself" "$(contribute_root "$fx" /installed)" "$(git -C "$fx" rev-parse --show-toplevel)"
29
+ t "a non-cckit repo falls back to installed" "$(contribute_root "$plain" /installed)" "/installed"
30
+ t "outside any repo falls back to installed" "$(contribute_root / /installed)" "/installed"
31
+
32
+ # End to end through the real dispatcher: run from inside the fixture worktree, the fixture
33
+ # worktree's own contribute.sh must run, on the worktree's branch.
34
+ out="$(cd "$wt/scripts" && bash "$ROOT/bin/cckit" contribute --dry-run "a summary" 2>/dev/null)"
35
+ t "cckit contribute runs in the worktree" "$(printf '%s' "$out" | sed -n 's/.*pwd=\([^ ]*\).*/\1/p')" "$wtp"
36
+ t "on the worktree's branch" "$(printf '%s' "$out" | sed -n 's/.*branch=\([^ ]*\).*/\1/p')" "task/1-x"
37
+ t "with the caller's arguments" "$(printf '%s' "$out" | sed -n 's/.*args=//p')" "--dry-run a summary"
38
+
39
+ [ "$fail" -eq 0 ] && echo "contribute-root: all passed" || echo "contribute-root: FAILURES"
40
+ exit "$fail"
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # contribute-root.sh — which cckit checkout `cckit contribute` acts on (#368).
4
+ #
5
+ # contribute_root <dir> <installed-root> print the checkout to contribute from
6
+ #
7
+ # The cckit checkout that contains <dir> wins: a worktree (where the rules say work happens) or any
8
+ # other clone. "A cckit checkout" = a git toplevel carrying bin/cckit and scripts/contribute.sh.
9
+ # Otherwise the installed checkout <installed-root> is used, as before. Before #368 it was always
10
+ # the installed checkout, so contribute ran the gate and opened the PR for whatever branch that
11
+ # checkout had, never the worktree the agent was working in.
12
+ # errors: pure — prints a path, rc 0; never changes directory
13
+ # bash 3.2 + zsh compatible.
14
+ contribute_root() {
15
+ local dir="${1:-$PWD}" installed="${2:-}" top
16
+ top="$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null)" || top=""
17
+ if [ -n "$top" ] && [ -f "$top/bin/cckit" ] && [ -f "$top/scripts/contribute.sh" ]; then
18
+ printf '%s\n' "$top"
19
+ else
20
+ printf '%s\n' "$installed"
21
+ fi
22
+ }
@@ -5,7 +5,11 @@
5
5
  # errors: strict — a test runner: rc 1 on any failed assertion
6
6
  set -u
7
7
  ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
8
+ # Sealed from the caller's KIT_*/CCKIT_*/EFFORT_* (#369): this suite builds its own fixture.
9
+ # shellcheck source=/dev/null
10
+ . "$ROOT/scripts/lib/test-env.sh" && test_env_seal
8
11
  LIB="$ROOT/scripts/lib"
12
+ SCRIPTS="$ROOT/scripts"
9
13
  fail=0
10
14
  t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
11
15
  tc() { if grep -qE "$2" "$1"; then echo "ok: $3"; else echo "FAIL: $3 (no /$2/ in gh log)"; fail=1; fi; }
@@ -24,7 +28,10 @@ case "$1 $2" in
24
28
  "issue create") n=$(( $(cat "$GH_N" 2>/dev/null || echo 0) + 1 )); echo "$n" > "$GH_N"
25
29
  echo "https://github.com/o/r/issues/$n" ;;
26
30
  "issue edit"|"issue close"|"pr merge") exit 0 ;;
27
- "issue view") echo "[Effort] 99 · demo effort" ;; # --json title -q .title
31
+ "issue view") case "$*" in # effort_pr reads title+labels+milestone
32
+ *labels,milestone*) echo '{"title":"[Effort] 99 · demo effort","labels":[{"name":"role:tech-lead"},{"name":"kind:task"}],"milestone":{"title":"M1"}}' ;;
33
+ *) echo "[Effort] 99 · demo effort" ;; # --json title -q .title
34
+ esac ;;
28
35
  "pr create") echo "https://github.com/o/r/pull/7" ;;
29
36
  "pr diff") # a merged sub PR's diff (wave-style close, #164): one kit-managed + one app file
30
37
  printf 'diff --git a/.claude/skills/demo.md b/.claude/skills/demo.md\n+kit\ndiff --git a/src/app.ts b/src/app.ts\n+app\n' ;;
@@ -92,8 +99,22 @@ t "effort_start bootstrapped the worktree (.env.local copied)" \
92
99
  # move onto the effort branch (its worktree) for pr/close
93
100
  cd "$tmp/work/.claude/worktrees/effort+99-demo"
94
101
  : > "$GH_LOG"
95
- effort_pr 99 >/dev/null 2>&1
102
+ effort_pr 99 --summary "SUMMARY-MARKER" --for-agents "AGENTS-MARKER" >/dev/null 2>&1
96
103
  tc "$GH_LOG" 'pr create .*--base main --head effort/99-demo' "effort_pr opens effort/99 → main"
104
+ # #363: parity with the kit-effort-pr skill — the verb is the one implementation.
105
+ tc "$GH_LOG" 'pr create .*--title \[#99\] demo effort' "effort_pr titles with the [#N] id + parent name"
106
+ tc "$GH_LOG" '[-]-label role:tech-lead,kind:task' "effort_pr inherits the parent's labels"
107
+ tc "$GH_LOG" '[-]-milestone M1' "effort_pr inherits the parent's milestone"
108
+ tc "$GH_LOG" 'SUMMARY-MARKER' "effort_pr puts --summary in the body"
109
+ tc "$GH_LOG" 'AGENTS-MARKER' "effort_pr puts --for-agents in the body"
110
+ tc "$GH_LOG" '^Closes #99$' "effort_pr body closes the parent"
111
+ t "effort_pr never commits an untracked .env.local" "$(git ls-files .env.local)" ""
112
+ : > "$GH_LOG"
113
+ effort_pr 99 --title "feat(x): a conventional title" >/dev/null 2>&1 && rc=0 || rc=1
114
+ t "effort_pr refuses a --title without the [#N] id" "$rc" "1"
115
+ t "effort_pr opened nothing on a bad --title" "$(grep -c 'pr create' "$GH_LOG")" "0"
116
+ effort_pr 99 --title "feat(x): a conventional title [#99]" >/dev/null 2>&1
117
+ tc "$GH_LOG" 'pr create .*--title feat\(x\): a conventional title \[#99\]' "effort_pr honours --title"
97
118
 
98
119
  # ── effort_close #120: refuse-squash-without-trace backstop ────────────────────────────────────
99
120
  # With NO commits on the effort branch, effort_snapshot_subs captures no trace → close must refuse
@@ -147,6 +168,7 @@ t "effort_close GC deleted the local branch" \
147
168
  cat > "$stub/khook" <<'SH'
148
169
  #!/usr/bin/env bash
149
170
  printf 'ingested %s\n' "$1" > "$KHOOK_MARKER"
171
+ pwd -P > "$KHOOK_MARKER.cwd" 2>/dev/null || echo "NO-CWD" > "$KHOOK_MARKER.cwd"
150
172
  SH
151
173
  chmod +x "$stub/khook"
152
174
  start_out="$(effort_start 88 forced 2>/dev/null)"
@@ -156,6 +178,9 @@ forced_out="$(KHOOK_MARKER="$tmp/khook.out" KIT_EFFORT_KNOWLEDGE_HOOK="$stub/kho
156
178
  cd "$tmp/work"
157
179
  tc "$GH_LOG" 'pr merge effort/88-forced .*--squash' "KIT_FORCE=1 close merges despite no trace"
158
180
  t "knowledge-ingest hook ran with the effort number" "$(cat "$tmp/khook.out" 2>/dev/null)" "ingested 88"
181
+ # #371: the close GCs the worktree it was run from; the hook must run in the main checkout, not in
182
+ # that deleted directory (where every relative path and git call fails).
183
+ t "knowledge-ingest hook runs in the main checkout" "$(cat "$tmp/khook.out.cwd" 2>/dev/null)" "$(cd "$tmp/work" && pwd -P)"
159
184
  # #148: no kit-managed file touched → the drift check stays silent
160
185
  case "$forced_out" in *"kit-sync"*) echo "FAIL: kit-sync warning fired with no kit-managed change"; fail=1 ;; *) echo "ok: drift check silent when no kit-managed file changed" ;; esac
161
186
 
@@ -546,5 +571,20 @@ t "wip: the plain-task start path is not gated" \
546
571
 
547
572
  cd "$tmp/work" || exit 1
548
573
 
574
+ # 15. the label `effort_plan` READS is a label `effort_new` WRITES.
575
+ # These drifted apart silently: effort-plan.sh queried `--label effort`, effort_new applied
576
+ # ctx/kind/priority/role/flow/par/slug and never `effort`, and setup-labels.sh never created
577
+ # it. `cckit effort plan` therefore answered "no open efforts found" for every effort ever
578
+ # made, in every repo, with no error anywhere. A static check because the failure mode is
579
+ # silence — there is nothing at runtime to notice.
580
+ plan_label="$(sed -n 's/.*gh issue list [^|]*--label \([a-z][a-z-]*\).*/\1/p' "$LIB/effort-plan.sh" | head -1)"
581
+ t "plan queries a label at all" "${plan_label:-MISSING}" "effort"
582
+ t "effort_new applies the label plan queries" \
583
+ "$(grep -c "labels=\"$plan_label," "$LIB/effort-ops.sh" | tr -d ' ')" "1"
584
+ t "effort_new ensures the label exists before using it" \
585
+ "$(grep -c "_eff_ensure_label \"$plan_label\"" "$LIB/effort-ops.sh" | tr -d ' ')" "1"
586
+ t "setup-labels provisions the label plan queries" \
587
+ "$(grep -c "ensure_label \"$plan_label\"" "$SCRIPTS/setup-labels.sh" | tr -d ' ')" "1"
588
+
549
589
  [ "$fail" -eq 0 ] && echo "ALL OK (effort-ops)" || echo "effort-ops: FAILURES"
550
590
  exit "$fail"