@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
@@ -0,0 +1,241 @@
1
+ #!/usr/bin/env bash
2
+ # review-stage-test.sh — the review DISPATCH contract (#346).
3
+ #
4
+ # Hermetic. The configured `review.command` is a shell script in a temp dir, so a real dispatch runs
5
+ # end to end — brief on stdin, report on stdout, receipt on disk — with no agent, no gh and no
6
+ # network. The one thing stubbed is ap_pr_context, which is the gh-touching half of resolution and
7
+ # already has its own coverage in agent-resolve-test.sh.
8
+ # Run: bash scripts/lib/review-stage-test.sh
9
+ # errors: strict — a test runner: rc 1 on any failed assertion
10
+ # shellcheck shell=bash
11
+ set -u
12
+ ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
13
+
14
+ command -v jq >/dev/null 2>&1 || { echo "review-stage-test: jq absent — skipping"; exit 0; }
15
+
16
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
17
+ # Point the receipt store at the temp dir BEFORE sourcing, so nothing writes into the real .cckit/.
18
+ export KIT_STATE_DIR="$tmp/state"
19
+ # shellcheck source=/dev/null
20
+ source "$ROOT/scripts/lib/review-stage.sh"
21
+
22
+ fail=0
23
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
24
+ rc() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> rc '[$2]' want '[$3]'"; fail=1; fi; }
25
+ has(){ case "$2" in *"$3"*) echo "ok: $1" ;; *) echo "FAIL: $1 -> '[$2]' lacks '$3'"; fail=1 ;; esac; }
26
+
27
+ CFG="$tmp/kit.config.json"
28
+ cat > "$CFG" <<'JSON'
29
+ {
30
+ "review": { "command": "cat > /dev/null; printf 'outcome: pr-open\nurl: u\ngate: pass — read the diff\nblocker: none\nnext stage: none\n'" },
31
+ "agents": {
32
+ "default": "builder",
33
+ "profiles": {
34
+ "builder": { "kind": "claude", "stages": ["build"], "write": true },
35
+ "critic": { "kind": "codex", "stages": ["review"], "tier": "low" },
36
+ "writer": { "kind": "claude", "stages": ["review"], "write": true }
37
+ }
38
+ }
39
+ }
40
+ JSON
41
+
42
+ # ── the off switch ─────────────────────────────────────────────────────────────────────────────
43
+ echo '{}' > "$tmp/none.json"
44
+ t "no review.command is empty" "$(rs_command "$tmp/none.json")" ""
45
+ rs_enabled "$tmp/none.json"; rc "a config with no review.command is off" "$?" "1"
46
+ rs_enabled "$CFG"; rc "a configured command is on" "$?" "0"
47
+ t "a missing config file is empty" "$(rs_command "$tmp/absent.json")" ""
48
+
49
+ # ── when a PR earns a reviewer (pure) ──────────────────────────────────────────────────────────
50
+ rs_needs_dispatch CLEAN merge 0; rc "a mergeable PR with no receipt gets one" "$?" "0"
51
+ rs_needs_dispatch CLEAN merge 1; rc "a PR already reviewed does not" "$?" "1"
52
+ rs_needs_dispatch CHECKS_MISSING verify 0; rc "a PR still verifying does not" "$?" "1"
53
+ rs_needs_dispatch HELD hold 0; rc "a PR held by a policy floor does not" "$?" "1"
54
+ rs_needs_dispatch CONFLICTING rebase 0; rc "a conflicting PR does not" "$?" "1"
55
+ rs_needs_dispatch '' '' 0; rc "an empty state and action does not" "$?" "1"
56
+
57
+ # The deadlock (#351 review). REVIEW_MISSING is `verify`, not `merge`: a merge-only predicate meant
58
+ # that once review.command turned the requirement on, the FIRST pass classified every unreviewed PR
59
+ # REVIEW_MISSING, the reviewer never ran, and the PR sat at `verify` for good.
60
+ rs_needs_dispatch REVIEW_MISSING verify 0; rc "a PR blocked ON the missing review gets one" "$?" "0"
61
+ # Unless it already has a verdict — then REVIEW_MISSING means the verdict is stale, and a receipt
62
+ # the head check rejected sets have=0, so this case only arises from a caller that lost track.
63
+ rs_needs_dispatch REVIEW_MISSING verify 1; rc "but never twice for the same verdict" "$?" "1"
64
+ # A recorded FAIL is not re-reviewed on its own: the author acts on it and pushes, which changes the
65
+ # head, which makes the verdict stale, which is what earns the next review.
66
+ rs_needs_dispatch REVIEW_FAILING fix 1; rc "a failing review is not re-run" "$?" "1"
67
+
68
+ # The display placeholder must never reach receipt storage.
69
+ t "a real issue number passes through" "$(rs_issue_num 42)" "42"
70
+ t "the em-dash placeholder is empty" "$(rs_issue_num '—')" ""
71
+ t "an empty field stays empty" "$(rs_issue_num '')" ""
72
+ t "a non-numeric field is empty" "$(rs_issue_num 'main')" ""
73
+
74
+ # ── the brief carries its three invariants ─────────────────────────────────────────────────────
75
+ b="$(rs_brief 'o/r' 7 42)"
76
+ has "the brief says no human is there" "$b" "HEADLESS"
77
+ has "the brief forbids writing" "$b" "READ ONLY"
78
+ has "the brief names the PR" "$b" "pull request #7"
79
+ has "the brief names the issue" "$b" "issue #42"
80
+ # Without the closing block in sr_parse's shape, sr_record finds no outcome: and refuses the report.
81
+ for f in "outcome:" "gate:" "blocker:" "next stage:"; do
82
+ has "the brief spells out '$f'" "$b" "$f"
83
+ done
84
+ # The brief must offer words sr_invalid_fields accepts. Telling a reviewer to say `shipped` records
85
+ # a receipt flagged invalid — recorded, but carrying a flag the captain then has to interpret.
86
+ for w in pr-open blocked pass fail; do
87
+ has "the brief offers the real vocabulary word '$w'" "$b" "$w"
88
+ done
89
+
90
+ # ── dispatch ───────────────────────────────────────────────────────────────────────────────────
91
+ # The mirror is a network call. Off for the run so nothing here touches GitHub; the cases at the end
92
+ # assert that a dispatch still records a verdict with the mirror unavailable.
93
+ export CCKIT_RECEIPT_REMOTE=0
94
+ # ap_pr_context is the gh half; agent-resolve-test.sh covers it. Here it is fixed so the rest is real.
95
+ FX_CTX="42\t\t"
96
+ ap_pr_context() { printf "$FX_CTX\n"; }
97
+
98
+ rs_dispatch "$tmp/none.json" 'o/r' 7 42 >/dev/null 2>&1
99
+ rc "no configured command dispatches nothing" "$?" "4"
100
+
101
+ rs_dispatch "$CFG" 'o/r' 7 '' >/dev/null 2>&1
102
+ rc "a PR with no linked issue is refused" "$?" "2"
103
+
104
+ # agents.default is `builder`, which does not list the review stage — a build agent must not be
105
+ # handed a review just because it is the default.
106
+ rs_dispatch "$CFG" 'o/r' 7 42 >/dev/null 2>&1
107
+ rc "a default barred from the review stage refuses" "$?" "2"
108
+
109
+ # An agent: label naming a review profile that CAN write is refused before it ever runs.
110
+ FX_CTX="42\tagent:writer\t"
111
+ rs_dispatch "$CFG" 'o/r' 7 42 >/dev/null 2>&1
112
+ rc "a write:true review profile is refused" "$?" "2"
113
+
114
+ # The real path: a read-only review profile, the configured command, a receipt on disk.
115
+ FX_CTX="42\tagent:critic\t"
116
+ p="$(rs_dispatch "$CFG" 'o/r' 7 42)"
117
+ rc "a read-only review profile dispatches" "$?" "0"
118
+ [ -f "$p" ] && echo "ok: the receipt exists on disk" || { echo "FAIL: no receipt at '$p'"; fail=1; }
119
+ t "the receipt is at stage review" "$(jq -r .stage "$p")" "review"
120
+ t "the receipt names the profile" "$(jq -r .profile "$p")" "critic"
121
+ t "the receipt records the verdict" "$(jq -r .outcome "$p")" "pr-open"
122
+ t "the receipt records the gate" "$(jq -r .gate "$p" | cut -d' ' -f1)" "pass"
123
+ t "the receipt records read-only" "$(jq -r .permissions "$p")" "read-only"
124
+ t "the receipt names the issue" "$(jq -r .issue "$p")" "42"
125
+ t "nothing is flagged invalid" "$(jq -r '.invalid | length' "$p")" "0"
126
+
127
+ # The receipt is now findable, which is what closes the dispatch loop: a second pass must not
128
+ # re-review a PR that already has a verdict.
129
+ rs_have_receipt 42; rc "the verdict is findable afterwards" "$?" "0"
130
+ rs_have_receipt 99; rc "an unreviewed issue has none" "$?" "1"
131
+ have=0; rs_have_receipt 42 && have=1
132
+ rs_needs_dispatch CLEAN merge "$have"; rc "a reviewed PR is not dispatched twice" "$?" "1"
133
+
134
+ # A command that exits non-zero writes NO receipt — a failed reviewer must not look like a verdict.
135
+ cat > "$tmp/fail.json" <<'JSON'
136
+ { "review": { "command": "exit 9" },
137
+ "agents": { "default": "critic",
138
+ "profiles": { "critic": { "kind": "codex", "stages": ["review"] } } } }
139
+ JSON
140
+ before="$(ls "$KIT_STATE_DIR/receipts" 2>/dev/null | wc -l | tr -d ' ')"
141
+ rs_dispatch "$tmp/fail.json" 'o/r' 8 55 >/dev/null 2>&1
142
+ rc "a failing review command is rc 3" "$?" "3"
143
+ t "a failing review command writes no receipt" \
144
+ "$(ls "$KIT_STATE_DIR/receipts" 2>/dev/null | wc -l | tr -d ' ')" "$before"
145
+
146
+ # A command that succeeds but reports nothing parseable is sr_record's refusal, not a silent pass.
147
+ cat > "$tmp/mute.json" <<'JSON'
148
+ { "review": { "command": "cat > /dev/null; echo 'looks fine to me'" },
149
+ "agents": { "default": "critic",
150
+ "profiles": { "critic": { "kind": "codex", "stages": ["review"] } } } }
151
+ JSON
152
+ rs_dispatch "$tmp/mute.json" 'o/r' 8 55 >/dev/null 2>&1
153
+ rc "a report with no outcome: line is refused" "$?" "2"
154
+ t "and it writes no receipt either" \
155
+ "$(ls "$KIT_STATE_DIR/receipts" 2>/dev/null | wc -l | tr -d ' ')" "$before"
156
+
157
+ # ── the mirror never costs the verdict (#347) ──────────────────────────────────────────────────
158
+ # sr_mirror is best-effort: an unmirrored receipt is still the receipt the merge gate reads. Failing
159
+ # a dispatch on an offline mirror would stall every PR at `verify` over a visibility problem.
160
+ FX_CTX="42\tagent:critic\t"
161
+ # NOT a command substitution: SR_MIRROR_LAST_RESULT is set by sr_mirror in whatever shell runs it,
162
+ # and `$(rs_dispatch …)` would set it in a subshell that then exits. Redirect to a file instead.
163
+ rs_dispatch "$CFG" 'o/r' 7 42 > "$tmp/p2"
164
+ rc "a dispatch succeeds with the mirror off" "$?" "0"
165
+ p2="$(cat "$tmp/p2")"
166
+ t "sr_mirror reported why it did not post" "$SR_MIRROR_LAST_RESULT" "no-remote"
167
+ [ -f "$p2" ] && echo "ok: the receipt is on disk anyway" || { echo "FAIL: no receipt at '$p2'"; fail=1; }
168
+ t "and it is a second attempt, not an overwrite" "$(jq -r .attempt "$p2")" "2"
169
+ [ "$p2" != "$p" ] && echo "ok: the two attempts are separate files" || { echo "FAIL: attempt 2 reused $p"; fail=1; }
170
+
171
+ # ── the PR review findings (#351) ──────────────────────────────────────────────────────────────
172
+
173
+ # The timeout bound. review.timeoutSeconds, with a default and a refusal to honour "no limit".
174
+ t "the default bound is 900s" "$(rs_timeout "$CFG")" "900"
175
+ cat > "$tmp/to.json" <<'JSON'
176
+ { "review": { "command": "true", "timeoutSeconds": 30 } }
177
+ JSON
178
+ t "a configured bound is used" "$(rs_timeout "$tmp/to.json")" "30"
179
+ cat > "$tmp/tobad.json" <<'JSON'
180
+ { "review": { "command": "true", "timeoutSeconds": "none" } }
181
+ JSON
182
+ # "none" reads like a request for NO limit. Honouring it would hand an unattended captain the hang
183
+ # the bound exists to prevent, so it falls back to the default instead.
184
+ t "a non-numeric bound falls back" "$(rs_timeout "$tmp/tobad.json" 2>/dev/null)" "900"
185
+ cat > "$tmp/tozero.json" <<'JSON'
186
+ { "review": { "command": "true", "timeoutSeconds": 0 } }
187
+ JSON
188
+ t "a zero bound falls back" "$(rs_timeout "$tmp/tozero.json" 2>/dev/null)" "900"
189
+
190
+ # _rs_run actually kills a hung command, on whichever path this system takes.
191
+ start=$(date +%s)
192
+ printf 'ignored
193
+ ' | _rs_run 2 'sleep 60' >/dev/null 2>&1
194
+ rc "a command past its bound is rc 124" "$?" "124"
195
+ elapsed=$(( $(date +%s) - start ))
196
+ [ "$elapsed" -lt 30 ] && echo "ok: it was killed in ${elapsed}s, not left running" \
197
+ || { echo "FAIL: still ran ${elapsed}s after a 2s bound"; fail=1; }
198
+ t "a command inside its bound returns its output" "$(printf '' | _rs_run 10 'echo alive' 2>/dev/null)" "alive"
199
+ printf '' | _rs_run 10 'exit 7' >/dev/null 2>&1
200
+ rc "a command's own exit code survives" "$?" "7"
201
+
202
+ # A dispatch whose reviewer hangs writes NO receipt — a killed reviewer is not a verdict.
203
+ cat > "$tmp/hang.json" <<'JSON'
204
+ { "review": { "command": "sleep 60", "timeoutSeconds": 2 },
205
+ "agents": { "default": "critic",
206
+ "profiles": { "critic": { "kind": "codex", "stages": ["review"] } } } }
207
+ JSON
208
+ FX_CTX="77\t\t"
209
+ before2="$(ls "$KIT_STATE_DIR/receipts" 2>/dev/null | wc -l | tr -d ' ')"
210
+ rs_dispatch "$tmp/hang.json" 'o/r' 9 77 >/dev/null 2>&1
211
+ rc "a hung reviewer is rc 3" "$?" "3"
212
+ t "a hung reviewer writes no receipt" \
213
+ "$(ls "$KIT_STATE_DIR/receipts" 2>/dev/null | wc -l | tr -d ' ')" "$before2"
214
+
215
+ # The resolved profile reaches the command. Without this an agent: label named a profile that only
216
+ # labelled the receipt, while the fixed command ran something else.
217
+ cat > "$tmp/env.json" <<'JSON'
218
+ { "review": { "command": "cat > /dev/null; printf 'outcome: pr-open\nurl: u\ngate: pass — %s/%s\nblocker: none\nnext stage: none\n' $CCKIT_REVIEW_PROFILE $CCKIT_REVIEW_KIND" },
219
+ "agents": { "default": "critic",
220
+ "profiles": { "critic": { "kind": "codex", "stages": ["review"] } } } }
221
+ JSON
222
+ FX_CTX="78\t\t"
223
+ pe="$(rs_dispatch "$tmp/env.json" 'o/r' 9 78)"
224
+ t "the command sees the resolved profile and kind" \
225
+ "$(jq -r .gate "$pe" | sed 's/^pass — //')" "critic/codex"
226
+
227
+ # head_sha binds a verdict to a revision (the stale-PASS hole).
228
+ FX_CTX="80\tagent:critic\t"
229
+ ph="$(rs_dispatch "$CFG" 'o/r' 9 80 'abc123')"
230
+ t "the receipt records the head it reviewed" "$(jq -r .head_sha "$ph")" "abc123"
231
+ rs_have_receipt 80 'abc123'; rc "the verdict counts for that head" "$?" "0"
232
+ rs_have_receipt 80 'def456'; rc "it does NOT count for a later commit" "$?" "1"
233
+ rs_have_receipt 80; rc "with no head given, the issue alone counts" "$?" "0"
234
+
235
+ # A receipt written before head_sha existed has an empty field. Asked about a head it cannot match,
236
+ # it reads as absent — which is correct, and is why the gate skips the check on an empty head.
237
+ rs_have_receipt 42 'abc123'; rc "a head-less receipt does not cover a head" "$?" "1"
238
+ rs_have_receipt 42; rc "but still counts when no head is asked" "$?" "0"
239
+
240
+ [ "$fail" -eq 0 ] && echo "review-stage-test: PASS" || echo "review-stage-test: FAILED"
241
+ exit "$fail"
@@ -0,0 +1,259 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # review-stage.sh — run `review` as a real stage of the agent pipeline (#346).
4
+ #
5
+ # E318 declared `review` in SR_STAGES, let a profile be barred from it, and let a receipt name it as
6
+ # next_stage — but nothing dispatched a reviewer and nothing read a verdict. This file is the
7
+ # dispatch half; the merge gate that reads the verdict is separate on purpose (#348), so the gate
8
+ # stays testable against a receipt that this file never produced.
9
+ #
10
+ # WHO STARTS THE AGENT. cckit does not know. Every launcher it has is interactive —
11
+ # `or_herdr_launch` focuses a pane, `or_tmux_agent_cmd` writes a line into a pane's shell — and the
12
+ # captain runs with no pane and nobody watching. Non-interactive execution is an open gap owned by
13
+ # #257 (agent-compatibility.mdx, scenario A07), so this file does not invent one: the project names
14
+ # the command in `review.command` and cckit pipes a brief to its stdin and reads the report off its
15
+ # stdout. That is the same shape sr_record already takes, and it means cckit asserts nothing about
16
+ # any agent's flags. No `review.command` configured is the off switch, and it is the default.
17
+ #
18
+ # THE REVIEWER DOES NOT OWN THE BRANCH. A review profile must not write to the branch it reviews:
19
+ # ap_profile_writes defaults to false and branch-owner.sh already refuses a second writer. Nothing
20
+ # here claims a branch, and rs_dispatch refuses a write-capable profile outright rather than
21
+ # relying on the reviewer to behave.
22
+ # errors: mixed — the predicates and the brief are pure; rs_dispatch propagates rc 2 from
23
+ # ap_resolve_pr (undeclared/ambiguous/stage-barred profile) and from a write-capable profile, rc 3
24
+ # when the configured command fails, and rc 4 when no profile resolves at all. The issue mirror
25
+ # never changes the rc — sr_mirror is best-effort and a lost mirror does not lose the verdict.
26
+
27
+ # BASH_SOURCE is bash-only and empty under zsh, the session shell here; `dirname ""` then yields
28
+ # "." and the sibling source below silently missed. Same defect and same fix as kit-gc.sh:38 (#219):
29
+ # `${BASH_SOURCE[0]:-$0}` covers zsh, CDPATH='' and the redirect stop an interactive zsh echoing the
30
+ # directory into the substitution.
31
+ _rs_dir="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null 2>&1 && pwd)"
32
+ # shellcheck source=/dev/null
33
+ command -v ap_resolve_pr >/dev/null 2>&1 || . "$_rs_dir/agent-resolve.sh"
34
+ # shellcheck source=/dev/null
35
+ command -v sr_record >/dev/null 2>&1 || . "$_rs_dir/stage-receipt.sh"
36
+
37
+ # rs_command <cfg> — the command the project configured to run a reviewer. Empty when unset, which
38
+ # is what keeps the stage off in every project that has not opted in.
39
+ rs_command() {
40
+ local cfg="${1:-}"
41
+ [ -n "$cfg" ] && [ -f "$cfg" ] || return 0
42
+ jq -r '.review.command // ""' "$cfg" 2>/dev/null || true
43
+ }
44
+
45
+ # rs_enabled <cfg> — rc 0 when a review command is configured.
46
+ rs_enabled() { [ -n "$(rs_command "${1:-}")" ]; }
47
+
48
+ # rs_needs_dispatch <state> <action> <have-receipt> — rc 0 when this PR should get a reviewer now.
49
+ # Pure.
50
+ #
51
+ # Only a PR the captain would otherwise MERGE is worth a reviewer: one that conflicts, fails a
52
+ # check, or is still a draft will change before anyone should read it, and a review of the version
53
+ # that is about to be rewritten is wasted spend. `hold` is excluded for the opposite reason — a
54
+ # policy floor already routed it to a human, who does not need a second opinion first.
55
+ #
56
+ # REVIEW_MISSING is the second way in, and leaving it out DEADLOCKED the feature. Configuring
57
+ # review.command turns the requirement on, so the first pass over an unreviewed PR classifies it
58
+ # REVIEW_MISSING, whose action is `verify` — not `merge`. With a merge-only predicate the reviewer
59
+ # never ran, the receipt never appeared, and the PR sat at `verify` for good. cap_classify only
60
+ # returns REVIEW_MISSING where it would otherwise have returned CLEAN, so it names exactly the PR
61
+ # that has earned a reviewer; the state is what says so once the gate is on.
62
+ rs_needs_dispatch() {
63
+ local state="${1:-}" action="${2:-}" have="${3:-}"
64
+ [ "$have" = "1" ] && return 1
65
+ [ "$state" = "REVIEW_MISSING" ] && return 0
66
+ [ "$action" = "merge" ] || return 1
67
+ return 0
68
+ }
69
+
70
+ # rs_issue_num <field> — the issue number out of captain_gate's DISPLAY field, empty when it is not
71
+ # one. That field carries `—` for a PR whose branch encodes no issue, so the printed rows read
72
+ # well; everything that keys STORAGE off it must see empty instead. sr_path would otherwise build a
73
+ # receipt filename containing `—`, one file shared by every unlinked PR in the repo, and one PR
74
+ # would read another's verdict as its own.
75
+ rs_issue_num() {
76
+ case "${1:-}" in ''|*[!0-9]*) printf '' ;; *) printf '%s' "$1" ;; esac
77
+ }
78
+
79
+ # rs_have_receipt <issue> [<head-sha>] — rc 0 when a review verdict already covers that revision.
80
+ #
81
+ # With a head, a receipt recorded against a DIFFERENT commit does not count: the reviewer read code
82
+ # that is no longer there, so the PR has earned a fresh review, not a skip. Without one the check
83
+ # is by issue alone, which is what a caller with no revision to compare can ask.
84
+ rs_have_receipt() {
85
+ local n="${1:-}" head="${2:-}" got
86
+ [ -n "$n" ] || return 1
87
+ got="$(sr_latest "$n" review 2>/dev/null)" || return 1
88
+ [ -n "$got" ] || return 1
89
+ [ -n "$head" ] || return 0
90
+ [ "$(printf '%s' "$got" | jq -r '.head_sha // ""' 2>/dev/null)" = "$head" ]
91
+ }
92
+
93
+ # rs_brief <repo> <pr> <issue> — the prompt handed to the reviewer on stdin.
94
+ #
95
+ # Three things it must carry and never lose: that no human will answer a question, that the reviewer
96
+ # may not write, and the exact shape sr_parse reads back. The last is why the closing block is
97
+ # spelled out rather than summarized — a report in any other shape records `outcome:` as empty, and
98
+ # sr_record refuses it.
99
+ #
100
+ # The words in that block are SR_OUTCOMES and SR_GATES, not a description of them. A reviewer told
101
+ # to say `shipped` writes a receipt sr_invalid_fields flags, because `shipped` is not in the
102
+ # vocabulary — the receipt is still recorded (that is deliberate, see sr_invalid_fields) but it
103
+ # carries a flag no captain should have to interpret. `pr-open` and `blocked` are the two states a
104
+ # review can leave a PR in: it does not merge and it does not close.
105
+ rs_brief() {
106
+ local repo="${1:-?}" pr="${2:-?}" num="${3:-?}"
107
+ cat <<EOF
108
+ You are reviewing pull request #$pr in $repo. You are running HEADLESS: there is no human in this
109
+ session to answer a question, so decide within what the diff shows and finish.
110
+
111
+ READ ONLY. Do not commit, push, amend, rebase, or edit any file. You do not own this branch and a
112
+ second writer on it is refused. Read the diff and say what you found.
113
+
114
+ Review PR #$pr (issue #$num). Report defects you can point at in the diff: a wrong result for a
115
+ stated input, an unhandled case the code claims to handle, a check that cannot fail, a test that
116
+ asserts nothing. Say the file and line. Do not report style preferences, and do not restate what
117
+ the diff does.
118
+
119
+ ## Finish like this
120
+ outcome: <pr-open|blocked>
121
+ url: https://github.com/$repo/pull/$pr
122
+ gate: <pass|fail> — <what you read>
123
+ blocker: <the defect that stops this merging, or none>
124
+ next stage: <none if it may merge, build if it goes back to the author>
125
+ EOF
126
+ }
127
+
128
+ # rs_timeout <cfg> — seconds a reviewer may run, from `review.timeoutSeconds`. Default 900.
129
+ # A non-numeric or non-positive value is ignored with a warning rather than disabling the bound:
130
+ # `timeoutSeconds: "none"` reads like a request for no limit, and honouring that would hand an
131
+ # unattended captain the hang this exists to prevent.
132
+ rs_timeout() {
133
+ local cfg="${1:-}" v
134
+ v="$(jq -r '.review.timeoutSeconds // ""' "$cfg" 2>/dev/null)" || v=""
135
+ case "$v" in
136
+ ''|*[!0-9]*) [ -n "$v" ] && echo "review: review.timeoutSeconds '$v' is not a positive integer — using 900" >&2
137
+ printf '900\n' ;;
138
+ 0) echo "review: review.timeoutSeconds 0 is not a positive integer — using 900" >&2
139
+ printf '900\n' ;;
140
+ *) printf '%s\n' "$v" ;;
141
+ esac
142
+ }
143
+
144
+ # _rs_run <seconds> <command> — run the command on stdin under a wall-clock bound, rc 124 on expiry.
145
+ #
146
+ # `timeout(1)` when it is there, a poll loop when it is not. The fallback is not a nicety: macOS
147
+ # ships no timeout, so on the platform this is most often run from, "use timeout or go unbounded"
148
+ # means unbounded — and an unbounded reviewer hangs the whole captain pass, every other PR in it
149
+ # included, with nobody watching.
150
+ #
151
+ # The loop is safe because a background child that has exited is reaped before `kill -0` is asked
152
+ # about it, in both bash and zsh, so the poll terminates rather than spinning on a zombie. SIGTERM
153
+ # first, then SIGKILL, because an agent that traps TERM to clean up should get the chance and one
154
+ # that ignores it should not get to stay.
155
+ _rs_run() {
156
+ local secs="${1:-900}" cmd="${2:-}" t="" out rc=0 pid waited=0
157
+ if command -v timeout >/dev/null 2>&1; then t=timeout
158
+ elif command -v gtimeout >/dev/null 2>&1; then t=gtimeout
159
+ fi
160
+ if [ -n "$t" ]; then
161
+ "$t" --kill-after=10s "${secs}s" sh -c "$cmd" 2>/dev/null
162
+ return $?
163
+ fi
164
+
165
+ out="$(mktemp 2>/dev/null)" || { sh -c "$cmd" 2>/dev/null; return $?; }
166
+ sh -c "$cmd" > "$out" 2>/dev/null &
167
+ pid=$!
168
+ while kill -0 "$pid" 2>/dev/null; do
169
+ if [ "$waited" -ge "$secs" ]; then
170
+ kill -TERM "$pid" 2>/dev/null
171
+ # A short grace period, then SIGKILL. Not `sleep 10`: the captain is already waiting on this.
172
+ local grace=0
173
+ while [ "$grace" -lt 10 ] && kill -0 "$pid" 2>/dev/null; do sleep 1; grace=$((grace + 1)); done
174
+ kill -KILL "$pid" 2>/dev/null
175
+ wait "$pid" 2>/dev/null
176
+ rm -f "$out"
177
+ return 124
178
+ fi
179
+ sleep 1; waited=$((waited + 1))
180
+ done
181
+ wait "$pid" 2>/dev/null; rc=$?
182
+ cat "$out"; rm -f "$out"
183
+ return "$rc"
184
+ }
185
+
186
+ # rs_dispatch <cfg> <repo> <pr> <issue> [<head-sha>] — resolve a reviewer, run it, record its
187
+ # verdict. <head-sha> is the PR head the review applies to; it is stored on the receipt so a
188
+ # later commit cannot inherit this verdict (#351 review).
189
+ # Echoes the receipt path. The configured command receives the brief on stdin; its stdout is the
190
+ # report sr_record parses.
191
+ rs_dispatch() {
192
+ local cfg="${1:-}" repo="${2:-}" pr="${3:-}" num="${4:-}" head="${5:-}"
193
+ local cmd prof tier src kind args ctx il pl report attempt path secs rc_=0
194
+ [ -n "$num" ] || { echo "review: PR #${pr:-?} has no linked issue — nothing to record a receipt against" >&2; return 2; }
195
+ cmd="$(rs_command "$cfg")"
196
+ [ -n "$cmd" ] || return 4
197
+
198
+ # ONE context fetch, not two. ap_resolve_pr fetches it internally and a second call for
199
+ # ap_resolve_source would fetch again — two round trips whose answers can disagree, since every
200
+ # fetch is best-effort and a label can change between them. profile_source would then describe a
201
+ # different selection than profile. Resolve from the fields directly instead.
202
+ ctx="$(ap_pr_context "$repo" "$pr")"
203
+ il="$(printf '%s' "$ctx" | cut -f2)"
204
+ pl="$(printf '%s' "$ctx" | cut -f3)"
205
+ prof="$(ap_resolve "$cfg" review '' "$il" "$pl")" || { rc_=$?; return "$rc_"; }
206
+ src="$(ap_resolve_source "$cfg" '' "$il" "$pl")" || src=""
207
+ tier="$(ap_profile_tier "$cfg" "$prof")"
208
+
209
+ # A write-capable profile is refused here rather than trusted to stay read-only. The brief says
210
+ # read-only and branch-owner.sh refuses a second writer, but both are downstream of a profile the
211
+ # project already declared able to write — and a reviewer that commits to the branch it reviews
212
+ # is the one failure this stage must not have.
213
+ if ap_profile_writes "$cfg" "$prof"; then
214
+ echo "review: profile '$prof' declares write:true — a reviewer must not own the branch it reviews" >&2
215
+ return 2
216
+ fi
217
+
218
+ # The resolved profile reaches the command as environment, because cckit cannot build the argv
219
+ # itself (see the header). Without this, an `agent:<profile>` label selected a profile that only
220
+ # labelled the receipt while a fixed command ran something else — resolution the receipt claimed
221
+ # and the run did not honour. A review.command that ignores these still works; one that reads
222
+ # CCKIT_REVIEW_KIND / CCKIT_REVIEW_ARGS can dispatch the agent the label actually asked for.
223
+ kind="$(ap_profile_field "$cfg" "$prof" kind)"
224
+ args="$(ap_profile_args "$cfg" "$prof")"
225
+
226
+ # Bound the run. The captain is unattended and single-threaded: a reviewer that never returns
227
+ # blocks the whole pass, every other PR in it included, with no human to notice. `timeout` is not
228
+ # on every system (macOS has none by default), so its absence is a warning, not a refusal —
229
+ # degrading to today's unbounded behaviour beats refusing to review at all.
230
+ secs="$(rs_timeout "$cfg")"
231
+ report="$(rs_brief "$repo" "$pr" "$num" \
232
+ | CCKIT_REVIEW_PROFILE="$prof" CCKIT_REVIEW_KIND="$kind" CCKIT_REVIEW_ARGS="$args" \
233
+ CCKIT_REVIEW_PR="$pr" CCKIT_REVIEW_ISSUE="$num" CCKIT_REVIEW_REPO="$repo" \
234
+ _rs_run "$secs" "$cmd")" || {
235
+ rc_=$?
236
+ if [ "$rc_" = "124" ]; then
237
+ echo "review: review.command exceeded ${secs}s for PR #$pr — killed, no receipt written" >&2
238
+ else
239
+ echo "review: the configured review.command failed for PR #$pr — no receipt written" >&2
240
+ fi
241
+ return 3
242
+ }
243
+
244
+ attempt="$(sr_next_attempt "$num" review)"
245
+ path="$(printf '%s\n' "$report" | sr_record "$num" review "$attempt" "$prof" "$tier" "$src" "read-only" "$head")" || return $?
246
+
247
+ # Mirror onto the issue so the verdict is readable on GitHub, not only in local .cckit/ state.
248
+ # BEST-EFFORT by sr_mirror's own contract (always rc 0, outcome in SR_MIRROR_LAST_RESULT): an
249
+ # unmirrored verdict is still a verdict. The receipt on disk is what the merge gate reads, so
250
+ # failing the dispatch on an offline or rate-limited mirror would stall every PR at `verify` over
251
+ # a visibility problem.
252
+ #
253
+ # A caller that wants to know WHETHER it posted must not wrap this call in `$(…)`:
254
+ # SR_MIRROR_LAST_RESULT would then be set in a subshell that immediately exits. Redirect the
255
+ # receipt path to a file instead. captain_pass does exactly that (`>/dev/null`).
256
+ SR_REPO="$repo" sr_mirror "$num" review "$attempt" "$path"
257
+
258
+ printf '%s\n' "$path"
259
+ }