@jeiemgi/cckit 0.5.0 → 0.7.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 (144) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +34 -3
  3. package/README.md +95 -6
  4. package/bin/cckit +90 -3
  5. package/cckit.config.json +7 -2
  6. package/kit.config.schema.json +26 -1
  7. package/package.json +1 -1
  8. package/profiles/automation.json +2 -1
  9. package/profiles/content.json +2 -1
  10. package/profiles/minimal.json +2 -1
  11. package/profiles/research.json +2 -1
  12. package/profiles/software.json +2 -1
  13. package/scripts/benchmark/benchmark-test.sh +83 -0
  14. package/scripts/benchmark/gate.sh +55 -0
  15. package/scripts/benchmark/lib/bench-common.sh +70 -0
  16. package/scripts/benchmark/lib/model.sh +116 -0
  17. package/scripts/benchmark/report.sh +105 -0
  18. package/scripts/benchmark/run-quality.sh +102 -0
  19. package/scripts/benchmark/run.sh +166 -0
  20. package/scripts/benchmark/stop-check.sh +83 -0
  21. package/scripts/commitlint-test.sh +58 -0
  22. package/scripts/init.sh +57 -1
  23. package/scripts/kit-self-update.sh +55 -0
  24. package/scripts/lib/board-view-test.sh +1 -0
  25. package/scripts/lib/board-view.sh +1 -0
  26. package/scripts/lib/captain-test.sh +62 -0
  27. package/scripts/lib/captain.sh +105 -17
  28. package/scripts/lib/cckit-output.sh +1 -0
  29. package/scripts/lib/commitlint.sh +54 -0
  30. package/scripts/lib/config-path-test.sh +1 -0
  31. package/scripts/lib/config-path.sh +1 -0
  32. package/scripts/lib/copilot-test.sh +1 -0
  33. package/scripts/lib/copilot.sh +1 -0
  34. package/scripts/lib/effort-dispatch-test.sh +10 -0
  35. package/scripts/lib/effort-flows-test.sh +1 -0
  36. package/scripts/lib/effort-metrics-test.sh +1 -0
  37. package/scripts/lib/effort-metrics.sh +1 -0
  38. package/scripts/lib/effort-ops-test.sh +360 -0
  39. package/scripts/lib/effort-ops.sh +372 -16
  40. package/scripts/lib/effort-parity-test.sh +1 -0
  41. package/scripts/lib/effort-plan.sh +1 -0
  42. package/scripts/lib/effort-slug-test.sh +32 -0
  43. package/scripts/lib/effort-slug.sh +72 -8
  44. package/scripts/lib/effort.sh +1 -0
  45. package/scripts/lib/engine-adapter.sh +1 -0
  46. package/scripts/lib/errors-header-test.sh +86 -0
  47. package/scripts/lib/gh-log.sh +1 -0
  48. package/scripts/lib/gh-project-test.sh +1 -0
  49. package/scripts/lib/gh-project.sh +1 -0
  50. package/scripts/lib/git-remote-test.sh +1 -0
  51. package/scripts/lib/git-remote.sh +1 -0
  52. package/scripts/lib/handoff.sh +23 -2
  53. package/scripts/lib/kit-brief-test.sh +292 -0
  54. package/scripts/lib/kit-brief.sh +376 -0
  55. package/scripts/lib/kit-cli-test.sh +1 -0
  56. package/scripts/lib/kit-cli.sh +1 -0
  57. package/scripts/lib/kit-config-resolve.sh +1 -0
  58. package/scripts/lib/kit-config-test.sh +14 -0
  59. package/scripts/lib/kit-config.sh +7 -0
  60. package/scripts/lib/kit-engine-test.sh +1 -0
  61. package/scripts/lib/kit-events.sh +1 -0
  62. package/scripts/lib/kit-gc-test.sh +125 -0
  63. package/scripts/lib/kit-gc.sh +193 -5
  64. package/scripts/lib/kit-interview-test.sh +1 -0
  65. package/scripts/lib/kit-interview.sh +1 -0
  66. package/scripts/lib/kit-lib-test.sh +226 -0
  67. package/scripts/lib/kit-lib.sh +308 -0
  68. package/scripts/lib/kit-local.sh +1 -0
  69. package/scripts/lib/kit-manifest.sh +1 -0
  70. package/scripts/lib/kit-mode-test.sh +1 -0
  71. package/scripts/lib/kit-mode.sh +1 -0
  72. package/scripts/lib/kit-msg-test.sh +1 -0
  73. package/scripts/lib/kit-msg.sh +1 -0
  74. package/scripts/lib/kit-operate.sh +1 -0
  75. package/scripts/lib/kit-profile-test.sh +1 -0
  76. package/scripts/lib/kit-profile.sh +1 -0
  77. package/scripts/lib/kit-state-test.sh +89 -0
  78. package/scripts/lib/kit-state.sh +61 -0
  79. package/scripts/lib/kit-status-test.sh +233 -0
  80. package/scripts/lib/kit-status.sh +415 -0
  81. package/scripts/lib/kit-task-ops-test.sh +22 -0
  82. package/scripts/lib/kit-task-ops.sh +51 -2
  83. package/scripts/lib/next.sh +1 -0
  84. package/scripts/lib/plan-machine-test.sh +54 -0
  85. package/scripts/lib/plan-machine.sh +86 -25
  86. package/scripts/lib/plan-next-test.sh +1 -0
  87. package/scripts/lib/plan-next.sh +1 -0
  88. package/scripts/lib/pr-evidence-test.sh +259 -0
  89. package/scripts/lib/pr-evidence.sh +220 -35
  90. package/scripts/lib/project-scan.sh +1 -0
  91. package/scripts/lib/react-detect.sh +1 -0
  92. package/scripts/lib/render-test.sh +1 -0
  93. package/scripts/lib/render.sh +1 -0
  94. package/scripts/lib/role-identity.sh +1 -0
  95. package/scripts/lib/secret-guard.sh +21 -1
  96. package/scripts/lib/toon.sh +1 -0
  97. package/scripts/lib/ui.sh +1 -0
  98. package/scripts/lib/version-bump-test.sh +1 -0
  99. package/scripts/lib/version-bump.sh +1 -0
  100. package/scripts/lib/worktree-bootstrap-test.sh +220 -0
  101. package/scripts/lib/worktree-issue-test.sh +15 -1
  102. package/scripts/lib/worktree-issue.sh +23 -8
  103. package/scripts/lib/worktree-start.sh +191 -7
  104. package/scripts/lib/zsh-safety-test.sh +10 -0
  105. package/scripts/orchestrate.sh +8 -1
  106. package/scripts/portable-test.sh +13 -2
  107. package/scripts/self-install-test.sh +282 -0
  108. package/scripts/setup-labels.sh +1 -0
  109. package/scripts/status.sh +68 -40
  110. package/scripts/task-sync.sh +13 -0
  111. package/scripts/ux-a11y-scan.sh +96 -0
  112. package/scripts/ux-ds-audit.sh +146 -0
  113. package/scripts/ux-graph-scan.sh +188 -0
  114. package/skills/kit-autopilot/SKILL.md +6 -1
  115. package/skills/kit-cleanup/SKILL.md +83 -0
  116. package/skills/kit-effort-close/SKILL.md +3 -0
  117. package/skills/kit-effort-new/SKILL.md +7 -0
  118. package/skills/kit-effort-start/SKILL.md +39 -4
  119. package/skills/kit-gc/SKILL.md +19 -0
  120. package/skills/kit-next/SKILL.md +5 -0
  121. package/skills/kit-status/SKILL.md +92 -0
  122. package/skills/kit-task-start/SKILL.md +7 -1
  123. package/skills/kit-ux-audit/SKILL.md +241 -0
  124. package/skills/kit-ux-audit/references/a11y.md +79 -0
  125. package/skills/kit-ux-audit/references/consistency.md +106 -0
  126. package/skills/kit-ux-audit/references/graph-model.md +92 -0
  127. package/skills/kit-ux-audit/references/heuristics.md +157 -0
  128. package/skills/kit-ux-audit/references/report.md +172 -0
  129. package/templates/agents/ux.md +89 -0
  130. package/templates/benchmarks/SCHEMA.md +75 -0
  131. package/templates/benchmarks/abstain.jsonl +8 -0
  132. package/templates/benchmarks/adversarial.jsonl +8 -0
  133. package/templates/benchmarks/benchmark.config.json +14 -0
  134. package/templates/benchmarks/quality.jsonl +8 -0
  135. package/templates/benchmarks/retrieval.jsonl +7 -0
  136. package/templates/rules/branch-naming.md +20 -1
  137. package/templates/rules/communication-style.md +8 -0
  138. package/templates/rules/delegation-brief.md +38 -4
  139. package/templates/rules/effort-model.md +47 -4
  140. package/templates/rules/naming-and-ids.md +199 -0
  141. package/templates/rules/task-management.md +1 -1
  142. package/templates/settings/settings.json.tmpl +9 -0
  143. package/templates/skills/concrete/SKILL.md +117 -0
  144. package/templates/skills/karpathy-guidelines/SKILL.md +1 -0
@@ -6,24 +6,72 @@
6
6
  # `blocked_by` edges, and layers the issues into WAVES: wave 0 is everything unblocked, wave N is
7
7
  # everything whose blockers all landed in earlier waves. Every issue in a wave is independent of the
8
8
  # others in that wave, so a wave is exactly "what N agents can run in parallel right now". Within a
9
- # wave we pack issues into BATCHES under a session ctx budget, starting a new batch when the budget
10
- # would overflow or two issues declare overlapping files (so parallel subagents don't collide).
9
+ # wave issues are ordered by PRIORITY (p0 → p1 → p2 → p3, unlabeled last), then packed into BATCHES
10
+ # under a session ctx budget, starting a new batch when the budget would overflow or two issues
11
+ # declare overlapping files (so parallel subagents don't collide).
11
12
  #
12
13
  # cckit plan human wave plan for the open board
13
- # cckit plan --llm TOON wave plan (uniform rows: wave,batch,number,ctx,blockers,title)
14
+ # cckit plan --llm TOON wave plan (rows: wave,batch,number,ctx,blockers,title,priority)
14
15
  # cckit plan --effort <N> plan only effort #N's sub-issues
15
16
  # cckit plan --milestone <M> filter to milestone M
16
17
  # cckit plan --cap <N> session ctx budget per batch (default KIT_SESSION_BUDGET or 4)
17
18
  #
18
19
  # ctx weights: S=1 · M=2 · L=4 · XL=8. File hints: a "Files:"/"Touches:" line in the issue body
19
20
  # (comma/space-separated paths) drives intra-wave disjointness; absent → ctx budget alone batches.
21
+ # Priority comes from the `priority:pN` label (`cckit effort new --priority p1`).
22
+ #
23
+ # The `blocked_by` edges this reads are written by `cckit effort new --depends-on` and by
24
+ # `cckit effort chain <a> <b> …` (#243), which declares a linear order over existing issues. `chain`
25
+ # refuses any edge that would close a cycle precisely because the wave layering below cannot order a
26
+ # cyclic graph — an issue in a cycle would never satisfy its blockers and never get a wave.
27
+ #
28
+ # DEPENDENCIES ALWAYS BEAT PRIORITY. The priority sort runs strictly INSIDE one wave, and pm_waves
29
+ # only ever co-locates issues with no `blocked_by` edge between them — a blocker is placed in a
30
+ # strictly earlier wave than anything it blocks. So a p3 blocker of a p0 dependent still runs first:
31
+ # they are in different waves and the intra-wave sort can never see them together. Priority is only
32
+ # ever the tiebreak among work dependencies have already declared simultaneously startable.
20
33
  #
21
34
  # Requires: gh, jq. bash 3.2 (no associative arrays — parallel indexed arrays + linear scan; N small).
35
+ # errors: strict — refuses to plan without gh + jq
22
36
 
23
37
  PLAN_REPO="${PLAN_REPO:-${KIT_REPO:-}}"
24
38
 
25
39
  _pm_weight() { case "$1" in S) echo 1 ;; M) echo 2 ;; L) echo 4 ;; XL) echo 8 ;; *) echo 2 ;; esac; }
26
40
 
41
+ # pm_prio_rank — `priority:pN` label value -> sort rank (lower runs earlier). Anything unrecognized
42
+ # or absent ranks 9, so an unlabeled issue sorts after every labeled priority and never jumps ahead
43
+ # of one; unlabeled issues keep their relative order among themselves.
44
+ pm_prio_rank() {
45
+ case "$1" in
46
+ p0|P0) echo 0 ;; p1|P1) echo 1 ;; p2|P2) echo 2 ;; p3|P3) echo 3 ;; *) echo 9 ;;
47
+ esac
48
+ }
49
+
50
+ # pm_prio_sort — STABLE priority sort for the issues of ONE wave.
51
+ # stdin/stdout: lines "num<TAB>priority<TAB>rest…" (rest passed through untouched).
52
+ # Ties keep input order — the sequence number is the second sort key, so equal priorities preserve
53
+ # the dependency-derived order they arrived in.
54
+ #
55
+ # Dependency-safe by construction: this only ever runs over a SINGLE wave, and pm_waves places an
56
+ # issue in wave N only once every in-set blocker sits in a wave < N. Two issues in the same wave
57
+ # therefore have no dependency edge between them, so no permutation of a wave can order a dependent
58
+ # before its blocker. Never call this across waves.
59
+ pm_prio_sort() {
60
+ [ -n "${ZSH_VERSION:-}" ] && setopt local_options ksh_arrays sh_word_split 2>/dev/null
61
+ local tab; tab="$(printf '\t')"
62
+ local line prio seq=0
63
+ while IFS= read -r line; do
64
+ [ -n "$line" ] || continue
65
+ seq=$((seq + 1))
66
+ # field 2 is the priority; a line with no tab at all has no priority field.
67
+ case "$line" in
68
+ *"$tab"*) prio="${line#*"$tab"}"; prio="${prio%%"$tab"*}" ;;
69
+ *) prio="" ;;
70
+ esac
71
+ printf '%s\t%06d\t%s\n' "$(pm_prio_rank "$prio")" "$seq" "$line"
72
+ done | sort -t "$tab" -k1,1n -k2,2n | cut -f3-
73
+ }
74
+
27
75
  # pm_waves — pure layered topo-sort. stdin: lines "num<TAB>csvBlockers"; stdout: "num<TAB>wave".
28
76
  # Only blockers present in the input set constrain layering (out-of-set/closed blockers are the
29
77
  # caller's concern). Cycle-safe: any leftover after no-progress lands in a final wave.
@@ -120,7 +168,7 @@ plan_machine() {
120
168
  --milestone=*) ms="${1#*=}"; shift ;;
121
169
  --cap) budget="$2"; shift 2 ;;
122
170
  --cap=*) budget="${1#*=}"; shift ;;
123
- -h|--help) sed -n '14,21p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; return 0 ;;
171
+ -h|--help) sed -n '13,21p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; return 0 ;;
124
172
  *) echo "plan: unknown arg '$1'" >&2; return 2 ;;
125
173
  esac
126
174
  done
@@ -142,15 +190,22 @@ plan_machine() {
142
190
  fi
143
191
  [ -n "$raw" ] || { [ "$out" = "json" ] && echo "[]" || echo "plan: no open issues to plan"; return 0; }
144
192
 
145
- # 2. parallel arrays + per-issue ctx label, file hint, and OPEN blocked_by edges.
146
- local -a nums=() titles=() ctxs=() files=() blkall=()
147
- local n t b body lab
193
+ # 2. parallel arrays + per-issue ctx label, priority label, file hint, and OPEN blocked_by edges.
194
+ local -a nums=() titles=() ctxs=() prios=() files=() blkall=()
195
+ local n t b body lab labtab ctxlab priolab
196
+ labtab="$(printf '\t')"
148
197
  while IFS="$(printf '\t')" read -r n t body; do
149
198
  [ -n "$n" ] || continue
150
199
  nums+=("$n"); titles+=("$t")
151
- lab="$(gh issue view "$n" --repo "$repo" --json labels \
152
- --jq '[.labels[].name|select(startswith("ctx:"))|ltrimstr("ctx:")]|first // "M"' 2>/dev/null)"
153
- ctxs+=("${lab:-M}")
200
+ # one label read serves both ctx: (session weight) and priority: (intra-wave order).
201
+ lab="$(gh issue view "$n" --repo "$repo" --json labels --jq \
202
+ '[ ([.labels[].name|select(startswith("ctx:"))|ltrimstr("ctx:")]|first // "M"),
203
+ ([.labels[].name|select(startswith("priority:"))|ltrimstr("priority:")]|first // "") ]|@tsv' 2>/dev/null)"
204
+ ctxlab="${lab%%"$labtab"*}"
205
+ priolab="${lab#*"$labtab"}"
206
+ [ "$priolab" = "$lab" ] && priolab=""
207
+ ctxs+=("${ctxlab:-M}")
208
+ prios+=("$priolab")
154
209
  files+=("$(_pm_files "$body")")
155
210
  # only OPEN blockers gate execution; closed ones are already satisfied.
156
211
  b="$(gh api "repos/$repo/issues/$n/dependencies/blocked_by" \
@@ -180,20 +235,24 @@ EOF
180
235
  _wave_of() { printf '%s' "$wavetsv" | awk -F'\t' -v k="$1" '$1==k{print $2; exit}'; }
181
236
  waves="$(printf '%s' "$wavetsv" | awk -F'\t' '{print $2}' | sort -n | uniq)"
182
237
 
183
- # 5. within each wave, pack into batches (ctx budget + file overlap), then emit.
184
- emit_rows() { # writes "wave<TAB>batch<TAB>number<TAB>ctx<TAB>blockers<TAB>title"
238
+ # 5. within each wave: order by priority (stable), then pack into batches (ctx budget + file
239
+ # overlap), then emit. The priority sort is confined to one wave, where no two issues share a
240
+ # dependency edge — so it reorders only work that is already simultaneously startable.
241
+ emit_rows() { # writes "wave<TAB>batch<TAB>number<TAB>ctx<TAB>blockers<TAB>title<TAB>priority"
185
242
  local wv batchtsv num bt
186
243
  for wv in $waves; do
187
- # gather this wave's issues as "num<TAB>ctx<TAB>files" in array order
244
+ # gather this wave's issues as "num<TAB>priority<TAB>ctx<TAB>files" in array order, sort them
245
+ # by priority (ties keep that dependency-derived order), then drop the priority column and pack.
188
246
  batchtsv="$(for i in "${!nums[@]}"; do
189
247
  [ "$(_wave_of "${nums[$i]}")" = "$wv" ] || continue
190
- printf '%s\t%s\t%s\n' "${nums[$i]}" "${ctxs[$i]}" "${files[$i]}"
191
- done | pm_batches "$budget")"
248
+ printf '%s\t%s\t%s\t%s\n' "${nums[$i]}" "${prios[$i]}" "${ctxs[$i]}" "${files[$i]}"
249
+ done | pm_prio_sort | cut -f1,3- | pm_batches "$budget")"
192
250
  while IFS="$(printf '\t')" read -r num bt; do
193
251
  [ -n "$num" ] || continue
194
252
  for i in "${!nums[@]}"; do
195
253
  if [ "${nums[$i]}" = "$num" ]; then
196
- printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$wv" "$bt" "$num" "${ctxs[$i]}" "${inset[$i]}" "${titles[$i]}"
254
+ printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
255
+ "$wv" "$bt" "$num" "${ctxs[$i]}" "${inset[$i]}" "${titles[$i]}" "${prios[$i]}"
197
256
  fi
198
257
  done
199
258
  done <<EOF2
@@ -204,17 +263,18 @@ EOF2
204
263
 
205
264
  local here; here="$(dirname "${BASH_SOURCE[0]}")"
206
265
 
207
- # raw rows (wave<TAB>batch<TAB>number<TAB>ctx<TAB>blockers<TAB>title) — the copilot driver (#78)
208
- # consumes this; not a public output mode.
266
+ # raw rows (wave<TAB>batch<TAB>number<TAB>ctx<TAB>blockers<TAB>title<TAB>priority) — the copilot
267
+ # driver (#78) and `cckit next` consume this; not a public output mode. Priority is APPENDED as
268
+ # column 7 so existing positional readers of columns 1-6 keep working.
209
269
  if [ "$out" = "tsv" ]; then emit_rows; return 0; fi
210
270
 
211
271
  if [ "$out" = "json" ]; then
212
- # TOON-encode a uniform row array (wave,batch,number,ctx,blockers,title) for the copilot.
272
+ # TOON-encode a uniform row array (wave,batch,number,ctx,blockers,title,priority) for the copilot.
213
273
  local json
214
274
  json="$(emit_rows | jq -R -s '
215
275
  [ split("\n")[] | select(length>0) | split("\t")
216
276
  | {wave:(.[0]|tonumber), batch:(.[1]|tonumber), number:(.[2]|tonumber),
217
- ctx:.[3], blockers:.[4], title:.[5]} ]')"
277
+ ctx:.[3], blockers:.[4], title:.[5], priority:(.[6] // "")} ]')"
218
278
  # route through the TOON encoder (uniform array -> tabular; falls back to JSON under the gate).
219
279
  # shellcheck source=/dev/null
220
280
  . "$here/toon.sh"
@@ -229,22 +289,23 @@ EOF2
229
289
  . "$here/render.sh"
230
290
  {
231
291
  printf '# cckit plan — %s\n\n' "$repo"
232
- printf '_session budget %s · ctx S=1 M=2 L=4 XL=8 · each wave runs in parallel_\n' "$budget"
292
+ printf '_session budget %s · ctx S=1 M=2 L=4 XL=8 · each wave runs in parallel, p0 first_\n' "$budget"
233
293
  emit_rows | awk -F'\t' '
234
294
  BEGIN { lw="" }
235
295
  {
236
- wv=$1; bt=$2; num=$3; ctx=$4; blk=$5; title=$6
296
+ wv=$1; bt=$2; num=$3; ctx=$4; blk=$5; title=$6; pri=$7
237
297
  if (wv != lw) {
238
298
  printf "\n## Wave %s\n\n", wv
239
- print "| # | ctx | batch | issue | after |"
240
- print "|---|-----|-------|-------|-------|"
299
+ print "| # | pri | ctx | batch | issue | after |"
300
+ print "|---|-----|-----|-------|-------|-------|"
241
301
  lw=wv
242
302
  }
243
303
  sub(/^\[Effort( [0-9]+)?\] [0-9]+ · ?/, "", title)
244
304
  sub(/^\[[A-Za-z]+\] /, "", title)
245
305
  dep="—"
246
306
  if (blk != "") { gsub(/,/, ", #", blk); dep="#" blk }
247
- printf "| #%s | %s | %s | %s | %s |\n", num, ctx, bt, title, dep
307
+ if (pri == "") pri="—"
308
+ printf "| #%s | %s | %s | %s | %s | %s |\n", num, pri, ctx, bt, title, dep
248
309
  }'
249
310
  # externally-blocked issues never enter a wave — surface them so they aren't silently dropped.
250
311
  local anyext=0
@@ -5,6 +5,7 @@
5
5
  # invariant that EVERY plan carries the mandatory docs + README step. The pure scanners + proposals
6
6
  # run under bash AND zsh; the full plan_next verb (which sources render/toon via BASH_SOURCE) runs
7
7
  # under bash, matching how the dispatcher invokes it. Run: bash scripts/lib/plan-next-test.sh
8
+ # errors: strict — a test runner: rc 1 on any failed assertion
8
9
 
9
10
  dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
10
11
 
@@ -15,6 +15,7 @@
15
15
  # not itself agent-context fuel. The verb and the skill both honor this; see rules/plan-next.md.
16
16
  #
17
17
  # Filesystem-only — needs jq only for --llm (TOON/JSON). bash 3.2 / zsh compatible.
18
+ # errors: best-effort — degrades to whatever capability dirs exist; never crashes
18
19
 
19
20
  # _pn_first_dir <dir> [dir …] — echo the first directory that exists (graceful host-project fallback).
20
21
  _pn_first_dir() {
@@ -0,0 +1,259 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # pr-evidence-test.sh — covers the PR evidence upsert + its two failure modes (#224).
4
+ #
5
+ # Two defects are under test, and both were invisible before:
6
+ # 1. the helper posted with a plain `gh pr comment`, so every push stacked ANOTHER evidence
7
+ # comment on the PR. The fix is a marker-based upsert, and the assertion that matters is
8
+ # "two consecutive calls produce one create and one edit" — which is why the gh stub here is
9
+ # STATEFUL: a stub that only counts calls cannot tell an upsert from an append.
10
+ # 2. best-effort swallowed a failed API call, so a caller that needed the evidence to exist had
11
+ # no way to learn it did not. The fix is an opt-in strict mode, asserted per failure class.
12
+ #
13
+ # NETWORK-FREE and gh-FREE. The pure halves (key, marker, body, match, api path) are driven with
14
+ # fixtures; the three gh seams are driven through a PATH shim in a temp dir, so nothing here can
15
+ # reach GitHub or spam a real PR. Run: bash scripts/lib/pr-evidence-test.sh
16
+ # errors: strict — a test runner: rc 1 on any failed assertion
17
+ set -u
18
+ ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
19
+ LIB="$ROOT/scripts/lib"
20
+ fail=0
21
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
22
+ has() { case "$2" in *"$3"*) echo "ok: $1" ;; *) echo "FAIL: $1 -> '$(printf '%s' "$2" | head -3)' lacks '$3'"; fail=1 ;; esac; }
23
+ hasnt(){ case "$2" in *"$3"*) echo "FAIL: $1 -> unexpectedly contains '$3'"; fail=1 ;; *) echo "ok: $1" ;; esac; }
24
+ ne() { if [ "$2" != "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> both are '[$2]', want different"; fail=1; fi; }
25
+
26
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
27
+
28
+ # shellcheck source=/dev/null
29
+ . "$LIB/pr-evidence.sh"
30
+
31
+ # ── 1. the key: one evidence artifact, one stable identity (pure) ───────────────────────────────
32
+ t "key is stable for the same caption" "$(_pr_evidence_key build)" "$(_pr_evidence_key build)"
33
+ t "key normalizes case + punctuation" "$(_pr_evidence_key 'Build — LOG (v2)!')" "build-log-v2"
34
+ ne "different captions -> different keys" "$(_pr_evidence_key build)" "$(_pr_evidence_key typecheck)"
35
+ a200="$(printf 'a%.0s' $(seq 1 200))"
36
+ a47="$(printf 'a%.0s' $(seq 1 47))"
37
+ t "key length is capped" "$(_pr_evidence_key "$a200" | wc -c | tr -d ' ')" "48"
38
+ # 47 chars + a separator lands the cut exactly on the dash; a trailing dash would be ugly and,
39
+ # worse, would collide with the 48-char caption whose 48th char is real.
40
+ hasnt "capped key has no trailing dash" "$(_pr_evidence_key "$a47 !!x")" "-"
41
+ # An emoji-only caption slugs to the empty string; without the checksum fallback every such
42
+ # caption would share one key and overwrite the others' comments.
43
+ t "unsluggable caption still yields a key" "$( [ -n "$(_pr_evidence_key '🎉')" ] && echo yes )" "yes"
44
+ ne "two unsluggable captions differ" "$(_pr_evidence_key '🎉')" "$(_pr_evidence_key '🔥')"
45
+
46
+ # ── 2. the marker: findable by us, invisible to a reader (pure) ─────────────────────────────────
47
+ m_build="$(_pr_evidence_marker build)"
48
+ has "marker is an HTML comment (open)" "$m_build" '<!-- cckit:pr-evidence key='
49
+ has "marker is an HTML comment (close)" "$m_build" '-->'
50
+ has "marker carries the key" "$m_build" 'key=build'
51
+ t "marker is one line" "$(printf '%s' "$m_build" | wc -l | tr -d ' ')" "0"
52
+ ne "markers differ per caption" "$m_build" "$(_pr_evidence_marker typecheck)"
53
+
54
+ # ── 3. the body: marker first, deterministic (pure) ────────────────────────────────────────────
55
+ printf 'compiled 3 files\nno errors\n' > "$tmp/build.log"
56
+ body="$(_pr_evidence_compose build "$tmp/build.log")"
57
+ t "body starts with the marker" "$(printf '%s\n' "$body" | head -1)" "$m_build"
58
+ has "body still carries the caption" "$body" '## Evidence — build'
59
+ has "body still inlines the log" "$body" 'compiled 3 files'
60
+ hasnt "body has no other caption marker" "$body" "$(_pr_evidence_marker typecheck)"
61
+ # No timestamp/run-id: an unchanged re-run must produce an identical body, or every push would
62
+ # churn the PR timeline with a cosmetic edit.
63
+ t "body is deterministic" "$body" "$(_pr_evidence_compose build "$tmp/build.log")"
64
+ printf 'x' > "$tmp/shot.png"
65
+ has "image body carries the marker" "$(_pr_evidence_compose 'rendered' "$tmp/shot.png")" "$(_pr_evidence_marker rendered)"
66
+
67
+ # ── 4. matching: find OUR previous comment, and only ours (pure, fixture listing) ───────────────
68
+ # Shape mirrors _pr_evidence_list: `<id><TAB><one-line body>`. Row 3 is a human's comment, row 4 is
69
+ # a DIFFERENT evidence artifact — neither may be mistaken for the build comment.
70
+ row() { printf '%s\t%s\n' "$1" "$2"; }
71
+ listing="$( row 11 'looks good to me'
72
+ row 22 "$m_build\\n\\n## Evidence — build"
73
+ row 33 'nice'
74
+ row 44 "$(_pr_evidence_marker typecheck)\\n\\n## Evidence — typecheck" )"
75
+ t "match finds our own comment" "$(printf '%s\n' "$listing" | _pr_evidence_match_id "$m_build")" "22"
76
+ t "match ignores another artifact" "$(printf '%s\n' "$listing" | _pr_evidence_match_id "$(_pr_evidence_marker typecheck)")" "44"
77
+ t "match rc 1 when absent" "$(printf '%s\n' "$listing" | _pr_evidence_match_id "$(_pr_evidence_marker lint)" >/dev/null; echo $?)" "1"
78
+ t "match rc 0 when present" "$(printf '%s\n' "$listing" | _pr_evidence_match_id "$m_build" >/dev/null; echo $?)" "0"
79
+ t "match on an empty listing rc 1" "$(printf '' | _pr_evidence_match_id "$m_build" >/dev/null; echo $?)" "1"
80
+ t "match reports every duplicate" "$( { row 22 "$m_build"; row 99 "$m_build"; } | _pr_evidence_match_id "$m_build" | tr '\n' ' ')" "22 99 "
81
+ t "match rejects a non-numeric id" "$(row abc "$m_build" | _pr_evidence_match_id "$m_build" >/dev/null; echo $?)" "1"
82
+
83
+ # ── 5. api path: gh placeholders when the repo is unset (pure) ──────────────────────────────────
84
+ t "api path with an explicit repo" "$(_pr_evidence_api_path acme/widgets 'issues/7/comments')" "repos/acme/widgets/issues/7/comments"
85
+ t "api path defers to gh" "$(_pr_evidence_api_path '' 'issues/7/comments')" "repos/{owner}/{repo}/issues/7/comments"
86
+
87
+ # ── the gh stub: stateful, so an append is distinguishable from an upsert ───────────────────────
88
+ mkdir -p "$tmp/bin" "$tmp/state"
89
+ cat > "$tmp/bin/gh" <<'STUB'
90
+ #!/usr/bin/env bash
91
+ # Stateful `gh` stub. State: $GH_STATE/index (id<TAB>bodyfile) + $GH_STATE/body.<id>.
92
+ # Log: one line per call in $GH_LOG ("list" | "create" | "edit <id>").
93
+ # GH_FAIL_LIST=1 makes the lookup fail; GH_FAIL_WRITE=1 makes create+edit fail.
94
+ set -u
95
+ idx="$GH_STATE/index"; [ -f "$idx" ] || : > "$idx"
96
+
97
+ if [ "${1:-}" = api ]; then
98
+ path=""; method=GET; bodyfile=""
99
+ while [ "$#" -gt 0 ]; do
100
+ case "$1" in
101
+ --method) method="$2"; shift 2 ;;
102
+ -F) case "$2" in body=@*) bodyfile="${2#body=@}" ;; esac; shift 2 ;;
103
+ --jq) shift 2 ;;
104
+ --paginate|api) shift ;;
105
+ -*) shift ;;
106
+ *) path="$1"; shift ;;
107
+ esac
108
+ done
109
+ if [ "$method" = PATCH ]; then
110
+ id="${path##*/}"
111
+ echo "edit $id" >> "$GH_LOG"
112
+ [ "${GH_FAIL_WRITE:-0}" = 1 ] && exit 1
113
+ cut -f1 "$idx" | grep -qx "$id" || exit 1 # 404 on an unknown comment, like the real API
114
+ cp "$bodyfile" "$GH_STATE/body.$id"
115
+ exit 0
116
+ fi
117
+ echo "list" >> "$GH_LOG"
118
+ [ "${GH_FAIL_LIST:-0}" = 1 ] && exit 1
119
+ # Mirror `--jq '.[] | "\(.id)\t\(.body | @json)"'`: one line per comment, newlines/tabs escaped.
120
+ while read -r id _; do
121
+ [ -n "$id" ] || continue
122
+ printf '%s\t%s\n' "$id" "$(awk '{ gsub(/\t/, "\\t"); printf "%s\\n", $0 }' "$GH_STATE/body.$id")"
123
+ done < "$idx"
124
+ exit 0
125
+ fi
126
+
127
+ if [ "${1:-}" = pr ] && [ "${2:-}" = comment ]; then
128
+ bodyfile=""
129
+ while [ "$#" -gt 0 ]; do
130
+ case "$1" in --body-file) bodyfile="$2"; shift 2 ;; *) shift ;; esac
131
+ done
132
+ echo "create" >> "$GH_LOG"
133
+ [ "${GH_FAIL_WRITE:-0}" = 1 ] && exit 1
134
+ n=$(( $(wc -l < "$idx" | tr -d ' ') + 1 ))
135
+ id=$(( 1000 + n ))
136
+ cp "$bodyfile" "$GH_STATE/body.$id"
137
+ printf '%s\t%s\n' "$id" "$GH_STATE/body.$id" >> "$idx"
138
+ echo "https://github.com/o/r/pull/7#issuecomment-$id"
139
+ exit 0
140
+ fi
141
+ echo "stub gh: unhandled: $*" >&2
142
+ exit 1
143
+ STUB
144
+ chmod +x "$tmp/bin/gh"
145
+
146
+ reset_stub() { rm -rf "$tmp/state"; mkdir -p "$tmp/state"; : > "$tmp/gh.log"; }
147
+ # attach [env...] -- runs pr_attach_evidence in a subshell with ONLY the stub bin on PATH, so a
148
+ # real gh on the developer's machine can never be reached.
149
+ attach() {
150
+ GH_STATE="$tmp/state" GH_LOG="$tmp/gh.log" PATH="$tmp/bin:/usr/bin:/bin:/usr/sbin:/sbin" \
151
+ pr_attach_evidence "$@" 2>>"$tmp/stderr.log"
152
+ }
153
+ log_calls() { tr '\n' ' ' < "$tmp/gh.log" | sed 's/ *$//'; }
154
+
155
+ # ── 6. idempotence: two calls == one create + one edit ─────────────────────────────────────────
156
+ reset_stub; : > "$tmp/stderr.log"
157
+ attach 7 "$tmp/build.log" build; rc1=$?
158
+ attach 7 "$tmp/build.log" build; rc2=$?
159
+ t "1st call rc 0" "$rc1" "0"
160
+ t "2nd call rc 0" "$rc2" "0"
161
+ t "1st call created, 2nd EDITED" "$(log_calls)" "list create list edit 1001"
162
+ t "exactly one comment exists" "$(wc -l < "$tmp/state/index" | tr -d ' ')" "1"
163
+ has "the stored comment carries the marker" "$(cat "$tmp/state/body.1001")" "$m_build"
164
+ t "result reports the update" "$(attach 7 "$tmp/build.log" build; printf '%s' "$PR_EVIDENCE_LAST_RESULT")" "updated"
165
+
166
+ # an edited comment reflects the NEW evidence, not the old
167
+ reset_stub
168
+ attach 7 "$tmp/build.log" build
169
+ printf 'compiled 4 files\nstill no errors\n' > "$tmp/build.log"
170
+ attach 7 "$tmp/build.log" build
171
+ has "edit replaced the body with fresh evidence" "$(cat "$tmp/state/body.1001")" 'compiled 4 files'
172
+ hasnt "edit dropped the stale evidence" "$(cat "$tmp/state/body.1001")" 'compiled 3 files'
173
+
174
+ # ── 7. two artifacts on one PR are two comments ────────────────────────────────────────────────
175
+ reset_stub
176
+ printf 'no type errors\n' > "$tmp/tc.log"
177
+ attach 7 "$tmp/build.log" build
178
+ attach 7 "$tmp/tc.log" typecheck
179
+ attach 7 "$tmp/build.log" build
180
+ t "distinct captions each get their own comment" "$(wc -l < "$tmp/state/index" | tr -d ' ')" "2"
181
+ t "and the repeat edits only its own" "$(log_calls)" "list create list create list edit 1001"
182
+
183
+ # default caption = basename, so the same file re-run is still an upsert
184
+ reset_stub
185
+ attach 7 "$tmp/build.log"
186
+ attach 7 "$tmp/build.log"
187
+ t "default caption upserts too" "$(log_calls)" "list create list edit 1001"
188
+
189
+ # ── 8. strict propagates; best-effort does not (the whole point of the sub) ────────────────────
190
+ # 8a. a failed WRITE (the API said no). Best-effort must not report green work it did not do —
191
+ # it returns 0 by contract but records post-failed; strict returns 7.
192
+ reset_stub
193
+ GH_FAIL_WRITE=1 attach 7 "$tmp/build.log" build; t "best-effort rc 0 on a failed post" "$?" "0"
194
+ t "best-effort still records the failure" "$(GH_FAIL_WRITE=1 attach 7 "$tmp/build.log" build; printf '%s' "$PR_EVIDENCE_LAST_RESULT")" "post-failed"
195
+ GH_FAIL_WRITE=1 attach --strict 7 "$tmp/build.log" build; t "strict rc 7 on a failed post" "$?" "7"
196
+ GH_FAIL_WRITE=1 PR_EVIDENCE_STRICT=1 attach 7 "$tmp/build.log" build; t "PR_EVIDENCE_STRICT=1 propagates too" "$?" "7"
197
+ GH_FAIL_WRITE=1 PR_EVIDENCE_STRICT=1 attach --best-effort 7 "$tmp/build.log" build; t "--best-effort overrides the env" "$?" "0"
198
+
199
+ # 8b. a failed EDIT is just as fatal in strict mode (the comment exists but was not refreshed).
200
+ reset_stub
201
+ attach 7 "$tmp/build.log" build
202
+ GH_FAIL_WRITE=1 attach --strict 7 "$tmp/build.log" build; t "strict rc 7 on a failed edit" "$?" "7"
203
+
204
+ # 8c. a failed LOOKUP. Strict: rc 6, nothing posted. Best-effort: falls FORWARD to a create — a
205
+ # visible duplicate beats silently losing the evidence, and it does not compound because the new
206
+ # comment carries the same marker.
207
+ reset_stub
208
+ GH_FAIL_LIST=1 attach --strict 7 "$tmp/build.log" build; t "strict rc 6 on a failed lookup" "$?" "6"
209
+ t "strict posted nothing after a failed lookup" "$(log_calls)" "list"
210
+ reset_stub
211
+ GH_FAIL_LIST=1 attach 7 "$tmp/build.log" build; t "best-effort rc 0 on a failed lookup" "$?" "0"
212
+ t "best-effort fell forward to a create" "$(log_calls)" "list create"
213
+ attach 7 "$tmp/build.log" build
214
+ t "and the next healthy run edits, not appends" "$(log_calls)" "list create list edit 1001"
215
+
216
+ # 8d. the pre-flight failures: bad args, missing gh, missing file.
217
+ attach --strict 2>/dev/null; t "strict rc 2 on missing args" "$?" "2"
218
+ attach 2>/dev/null; t "best-effort rc 0 on missing args" "$?" "0"
219
+ attach --strict --bogus 7 "$tmp/build.log" 2>/dev/null; t "strict rc 2 on a bad option" "$?" "2"
220
+ attach --strict 7 "$tmp/nope.log" build; t "strict rc 4 on a missing file" "$?" "4"
221
+ attach 7 "$tmp/nope.log" build; t "best-effort rc 0 on a missing file" "$?" "0"
222
+ t "missing file is recorded" "$(attach 7 "$tmp/nope.log" build; printf '%s' "$PR_EVIDENCE_LAST_RESULT")" "no-file"
223
+ # gh absent: an empty PATH dir, so `command -v gh` genuinely fails. `hash -r` first, or bash's
224
+ # cached path for gh would still resolve and the case would pass without testing anything.
225
+ mkdir -p "$tmp/nogh"
226
+ ( hash -r 2>/dev/null; PATH="$tmp/nogh" pr_attach_evidence --strict 7 "$tmp/build.log" build ) 2>/dev/null
227
+ t "strict rc 3 without gh" "$?" "3"
228
+ ( hash -r 2>/dev/null; PATH="$tmp/nogh" pr_attach_evidence 7 "$tmp/build.log" build ) 2>/dev/null
229
+ t "best-effort rc 0 without gh" "$?" "0"
230
+
231
+ # ── 9. the contract surface the callers depend on ──────────────────────────────────────────────
232
+ has "usage documents the upsert" "$(pr_evidence_usage)" "cckit:pr-evidence key="
233
+ has "usage documents strict mode" "$(pr_evidence_usage)" "PR_EVIDENCE_STRICT"
234
+ has "usage keeps the public signature" "$(pr_evidence_usage)" "<pr-number> <evidence-file> [caption]"
235
+ has "header declares mixed" "$(grep -m1 '^# errors:' "$LIB/pr-evidence.sh")" "mixed"
236
+ # The marker prefix is a WIRE FORMAT: evidence comments already live on open PRs, and changing it
237
+ # orphans every one of them (an upsert would silently become an append again). This pins it so that
238
+ # is a deliberate edit with a failing test, not a refactor side effect.
239
+ t "marker prefix is frozen" "$PR_EVIDENCE_MARKER_PREFIX" '<!-- cckit:pr-evidence key='
240
+
241
+ # ── 10. zsh: sources and upserts there too ─────────────────────────────────────────────────────
242
+ if command -v zsh >/dev/null 2>&1; then
243
+ reset_stub
244
+ zsh -c "cd '$ROOT'; . scripts/lib/pr-evidence.sh; \
245
+ export GH_STATE='$tmp/state' GH_LOG='$tmp/gh.log'; PATH='$tmp/bin:/usr/bin:/bin'; \
246
+ pr_attach_evidence 7 '$tmp/build.log' build >/dev/null 2>&1; \
247
+ pr_attach_evidence 7 '$tmp/build.log' build >/dev/null 2>&1" >/dev/null 2>&1
248
+ t "zsh: two calls == one create + one edit" "$(log_calls)" "list create list edit 1001"
249
+ reset_stub
250
+ zrc="$(zsh -c "cd '$ROOT'; . scripts/lib/pr-evidence.sh; \
251
+ export GH_STATE='$tmp/state' GH_LOG='$tmp/gh.log' GH_FAIL_WRITE=1; PATH='$tmp/bin:/usr/bin:/bin'; \
252
+ pr_attach_evidence --strict 7 '$tmp/build.log' build >/dev/null 2>&1; echo \$?" 2>/dev/null | tail -1)"
253
+ t "zsh: strict rc 7 on a failed write" "$zrc" "7"
254
+ else
255
+ echo "ok: zsh absent — skipping the zsh pass (dependency-light gate)"
256
+ fi
257
+
258
+ if [ "$fail" -eq 0 ]; then echo "pr-evidence-test: OK"; else echo "pr-evidence-test: FAILED"; fi
259
+ exit "$fail"