@jeiemgi/cckit 0.7.1 → 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
@@ -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
+ }
@@ -0,0 +1,252 @@
1
+ #!/usr/bin/env bash
2
+ # stage-receipt-test.sh — one durable receipt per stage attempt (#322).
3
+ # errors: strict — a test runner: rc 1 on any failed assertion
4
+ # shellcheck shell=bash
5
+ set -u
6
+
7
+ ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
8
+ command -v jq >/dev/null 2>&1 || { echo "stage-receipt-test: jq absent — skipping"; exit 0; }
9
+
10
+ # Every receipt lands in a scratch dir, never the real .cckit/.
11
+ KIT_STATE_DIR="$(mktemp -d)/state"; export KIT_STATE_DIR
12
+
13
+ # The GitHub mirror (#333) is ON by default, and `gh` is authenticated on a developer machine — an
14
+ # unguarded `sr_cli` case here would post real comments to the real repo from the test suite. Off
15
+ # for every case above; the mirror's own cases below stub `gh` and turn it back on explicitly.
16
+ CCKIT_RECEIPT_REMOTE=0; export CCKIT_RECEIPT_REMOTE
17
+ # shellcheck source=/dev/null
18
+ source "$ROOT/scripts/lib/stage-receipt.sh"
19
+
20
+ fail=0
21
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
22
+ yes() { case "$2" in *"$3"*) echo "ok: $1" ;; *) echo "FAIL: $1 -> missing '$3' in [$2]"; fail=1 ;; esac; }
23
+
24
+ REPORT="$(cat <<'EOF'
25
+ Here is what I did, at some length, which is not part of the receipt.
26
+
27
+ outcome: pr-open
28
+ url: https://github.com/jeiemgi/cckit/pull/999
29
+ gate: pass
30
+ blocker: none
31
+ next stage: review
32
+ EOF
33
+ )"
34
+
35
+ # ── parsing E318.5's contract ──────────────────────────────────────────────────────────────────
36
+ p="$(printf '%s\n' "$REPORT" | sr_parse)"
37
+ t "outcome is parsed" "$(printf '%s' "$p" | cut -f1)" "pr-open"
38
+ t "url is parsed" "$(printf '%s' "$p" | cut -f2)" "https://github.com/jeiemgi/cckit/pull/999"
39
+ t "gate is parsed" "$(printf '%s' "$p" | cut -f3)" "pass"
40
+ t "blocker is parsed" "$(printf '%s' "$p" | cut -f4)" "none"
41
+ t "next stage is parsed" "$(printf '%s' "$p" | cut -f5)" "review"
42
+ # The worker's prose around the receipt must not become part of it.
43
+ t "surrounding prose is not captured" "$(printf '%s' "$p" | cut -f2)" "https://github.com/jeiemgi/cckit/pull/999"
44
+ # A worker that corrects itself means the correction; taking the first would record the retraction.
45
+ t "the LAST occurrence of a field wins" \
46
+ "$(printf 'outcome: blocked\noutcome: merged\n' | sr_parse | cut -f1)" "merged"
47
+ t "next_stage with an underscore also parses" \
48
+ "$(printf 'next_stage: docs\n' | sr_parse | cut -f5)" "docs"
49
+
50
+ # ── odd values are NAMED, not silently accepted ────────────────────────────────────────────────
51
+ t "a contract-shaped receipt has no invalid fields" "$(sr_invalid_fields pr-open pass review)" ""
52
+ t "an invented outcome is named" "$(sr_invalid_fields shipped pass review)" "outcome"
53
+ t "an invented gate is named" "$(sr_invalid_fields pr-open green review)" "gate"
54
+ t "an invented next stage is named" "$(sr_invalid_fields pr-open pass deploy)" "next_stage"
55
+ # `gate: fail — the failing check` keeps its detail; the failing check is the useful part.
56
+ t "gate detail after a fail is allowed" "$(sr_invalid_fields blocked "fail — shellcheck" none)" ""
57
+ sr_validate pr-open pass review 2>/dev/null; t "sr_validate passes a clean receipt" "$?" "0"
58
+ sr_validate shipped pass review 2>/dev/null; t "sr_validate is the hard-check form" "$?" "2"
59
+ yes "the hard check names the vocabulary" "$(sr_validate shipped pass review 2>&1)" "merged pr-open closed-no-op blocked"
60
+
61
+ # ── writing ────────────────────────────────────────────────────────────────────────────────────
62
+ t "the first attempt is 1" "$(sr_next_attempt 321 build)" "1"
63
+ out="$(printf '%s\n' "$REPORT" | sr_record 321 build 1 cheap low agents.default acceptEdits)"
64
+ t "the receipt is written where sr_path says" "$out" "$(sr_path 321 build 1)"
65
+ j="$(sr_read 321 build 1)"
66
+ t "the issue is recorded as a number" "$(printf '%s' "$j" | jq -r '.issue')" "321"
67
+ t "the five contract fields survive" \
68
+ "$(printf '%s' "$j" | jq -r '[.outcome,.gate,.blocker,.next_stage] | join("|")')" "pr-open|pass|none|review"
69
+ # #326: a run's effective agent and policy must be auditable after the fact.
70
+ t "the resolved profile is recorded" \
71
+ "$(printf '%s' "$j" | jq -r '[.profile,.tier,.profile_source,.permissions] | join("|")')" \
72
+ "cheap|low|agents.default|acceptEdits"
73
+
74
+ # ── idempotence: a retry must not double the record ────────────────────────────────────────────
75
+ printf '%s\n' "$REPORT" | sr_record 321 build 1 cheap low agents.default acceptEdits >/dev/null
76
+ t "re-recording one attempt leaves one file" \
77
+ "$(find "$(sr_dir)" -name '321-build-*.json' | wc -l | tr -d ' ')" "1"
78
+ t "a rewritten attempt keeps its content" "$(sr_read 321 build 1 | jq -r '.outcome')" "pr-open"
79
+
80
+ # A genuinely new try takes the next number, and neither attempt is lost.
81
+ t "the next attempt is 2" "$(sr_next_attempt 321 build)" "2"
82
+ printf 'outcome: blocked\nurl: -\ngate: fail — shellcheck\nblocker: needs a decision\nnext stage: none\n' \
83
+ | sr_record 321 build 2 cheap low agents.default acceptEdits >/dev/null
84
+ t "both attempts are on disk" \
85
+ "$(find "$(sr_dir)" -name '321-build-*.json' | wc -l | tr -d ' ')" "2"
86
+ t "attempt 1 is untouched by attempt 2" "$(sr_read 321 build 1 | jq -r '.outcome')" "pr-open"
87
+ t "sr_latest returns the newest attempt" "$(sr_latest 321 build | jq -r '.outcome')" "blocked"
88
+ t "a fail keeps which check failed" "$(sr_read 321 build 2 | jq -r '.gate')" "fail — shellcheck"
89
+
90
+ # ── an odd value is RECORDED and flagged, never discarded ──────────────────────────────────────
91
+ # Refusing would make "agent crashed" and "agent finished but said it oddly" look identical, and
92
+ # only the first needs the stage re-run. Keeping the value is what lets a wave keep moving.
93
+ printf 'outcome: shipped\nurl: https://x/1\ngate: pass\nblocker: none\nnext stage: review\n' \
94
+ | sr_record 321 review 1 cheap low agents.default '' >/dev/null 2>&1
95
+ t "an odd receipt is still written" "$?" "0"
96
+ t "the odd value is kept verbatim" "$(sr_read 321 review 1 | jq -r '.outcome')" "shipped"
97
+ t "the offending field is named" "$(sr_read 321 review 1 | jq -r '.invalid | join(",")')" "outcome"
98
+ t "a clean receipt flags nothing" "$(sr_read 321 build 1 | jq -r '.invalid | length')" "0"
99
+ yes "sr_list surfaces the flag so a wave scan cannot miss it" "$(sr_list 321)" "invalid: outcome"
100
+
101
+ # The ONE hard refusal: a report with no outcome line is not an odd receipt, it is no receipt.
102
+ printf 'I could not figure this out.\n' | sr_record 321 docs 1 cheap low agents.default '' >/dev/null 2>&1
103
+ t "a report with no outcome line is refused" "$?" "2"
104
+ t "the refused report left no file" "$([ -f "$(sr_path 321 docs 1)" ] && echo yes || echo no)" "no"
105
+
106
+ # ── listing ────────────────────────────────────────────────────────────────────────────────────
107
+ t "sr_list rows one receipt each" "$(sr_list 321 | wc -l | tr -d ' ')" "3"
108
+ yes "sr_list names the outcome" "$(sr_list 321)" "pr-open"
109
+ t "sr_list of an unknown issue is empty" "$(sr_list 999 | wc -l | tr -d ' ')" "0"
110
+ t "sr_read of a missing receipt is rc 1" "$(sr_read 321 docs 1 >/dev/null 2>&1; echo $?)" "1"
111
+ t "sr_latest with no receipts is rc 1" "$(sr_latest 321 docs >/dev/null 2>&1; echo $?)" "1"
112
+
113
+ # ── the `cckit receipt` verb ───────────────────────────────────────────────────────────────────
114
+ # The WORKER records its own receipt: cckit never reads pane output, because #326 requires that
115
+ # pane history (off by default, may hold secrets) is never a dependency.
116
+ sr_cli 500 --stage build --profile cheap --tier low --source agents.default --permissions plan \
117
+ <<'EOF' >/dev/null
118
+ outcome: pr-open
119
+ url: https://github.com/x/y/pull/1
120
+ gate: pass
121
+ blocker: none
122
+ next stage: review
123
+ EOF
124
+ t "the verb allocates the first attempt" "$(sr_read 500 build 1 | jq -r '.attempt')" "1"
125
+ t "the verb records the flags it was given" \
126
+ "$(sr_read 500 build 1 | jq -r '[.profile,.tier,.profile_source,.permissions] | join("|")')" \
127
+ "cheap|low|agents.default|plan"
128
+
129
+ # Retry after an unknown result: the SAME five fields must not become a second attempt.
130
+ sr_cli 500 --stage build <<'EOF' >/dev/null
131
+ outcome: pr-open
132
+ url: https://github.com/x/y/pull/1
133
+ gate: pass
134
+ blocker: none
135
+ next stage: review
136
+ EOF
137
+ t "an identical re-run rewrites its own attempt" \
138
+ "$(find "$(sr_dir)" -name '500-build-*.json' | wc -l | tr -d ' ')" "1"
139
+
140
+ # A re-run that says something DIFFERENT is a real second try and keeps both records.
141
+ sr_cli 500 --stage build <<'EOF' >/dev/null
142
+ outcome: merged
143
+ url: https://github.com/x/y/pull/1
144
+ gate: pass
145
+ blocker: none
146
+ next stage: none
147
+ EOF
148
+ t "a changed re-run takes the next attempt" \
149
+ "$(find "$(sr_dir)" -name '500-build-*.json' | wc -l | tr -d ' ')" "2"
150
+ t "the earlier attempt still says what it said" "$(sr_read 500 build 1 | jq -r '.outcome')" "pr-open"
151
+ t "show with no attempt gives the latest" "$(sr_cli show 500 build | jq -r '.outcome')" "merged"
152
+ t "show with an attempt gives that one" "$(sr_cli show 500 build 1 | jq -r '.outcome')" "pr-open"
153
+
154
+ sr_cli 500 --stage deploy </dev/null >/dev/null 2>&1; t "an unknown stage is refused" "$?" "2"
155
+ sr_cli 500 </dev/null >/dev/null 2>&1; t "a missing --stage is refused" "$?" "2"
156
+ sr_cli abc --stage build </dev/null >/dev/null 2>&1; t "a non-numeric issue is refused" "$?" "2"
157
+ sr_cli 500 --stage build --nope x </dev/null >/dev/null 2>&1; t "an unknown flag is refused" "$?" "2"
158
+ yes "the verb help names the stages" "$(sr_cli --help 2>&1)" "build review design docs none"
159
+
160
+ # ── the receipts dir is shared, not per-worktree ───────────────────────────────────────────────
161
+ # kit_state_dir anchors a relative override to the repo's shared root, so standing somewhere else
162
+ # must resolve to the SAME directory — a receipt a worker writes in its isolation worktree has to
163
+ # be readable by the captain.
164
+ here="$(sr_dir)"
165
+ there="$(cd /tmp && KIT_STATE_DIR="$KIT_STATE_DIR" bash -c "source '$ROOT/scripts/lib/stage-receipt.sh'; sr_dir")"
166
+ t "sr_dir is the same from another directory" "$there" "$here"
167
+
168
+ # ── the GitHub mirror (#333) ───────────────────────────────────────────────────────────────────
169
+ # `gh` is stubbed on PATH: it logs every invocation and serves a canned comment list, so the upsert
170
+ # is exercised end to end with no network and no real repo touched.
171
+ ghtmp="$(mktemp -d)"; mkdir -p "$ghtmp/bin"
172
+ export GH_LOG="$ghtmp/gh.log"; export GH_COMMENTS="$ghtmp/comments.txt"
173
+ : > "$GH_LOG"; : > "$GH_COMMENTS"
174
+ cat > "$ghtmp/bin/gh" <<'SH'
175
+ #!/usr/bin/env bash
176
+ echo "$*" >> "$GH_LOG"
177
+ case "$*" in
178
+ *"issues/"*"/comments"*) cat "$GH_COMMENTS" ;; # the listing (--jq is applied by the real gh)
179
+ *) : ;;
180
+ esac
181
+ exit "${GH_RC:-0}"
182
+ SH
183
+ chmod +x "$ghtmp/bin/gh"
184
+ OLDPATH="$PATH"; PATH="$ghtmp/bin:$PATH"; export PATH
185
+
186
+ # ── pure: the marker is the identity, and it is per ATTEMPT ────────────────────────────────────
187
+ t "the marker keys on issue, stage and attempt" \
188
+ "$(_sr_marker 42 build 2)" "<!-- cckit:receipt key=42-build-2 -->"
189
+ yes "a different attempt is a different marker" "$(_sr_marker 42 build 3)" "42-build-3"
190
+
191
+ # ── pure: matching finds only this receipt's own comment ───────────────────────────────────────
192
+ listing="$(printf '%s\t%s\n%s\t%s\n' \
193
+ 111 '"<!-- cckit:receipt key=42-build-1 --> other"' \
194
+ 222 '"<!-- cckit:receipt key=42-build-2 --> mine"')"
195
+ t "the matching attempt's comment id" "$(printf '%s\n' "$listing" | _sr_mirror_match_id "$(_sr_marker 42 build 2)")" "222"
196
+ printf '%s\n' "$listing" | _sr_mirror_match_id "$(_sr_marker 42 build 9)" >/dev/null 2>&1
197
+ t "no match returns rc 1" "$?" "1"
198
+
199
+ # The receipt the mirror cases post. Written with the mirror off so this setup step posts nothing.
200
+ printf '%s\n' "$REPORT" | CCKIT_RECEIPT_REMOTE=0 sr_cli 42 --stage build --attempt 1 >/dev/null 2>&1
201
+
202
+ # ── a first mirror CREATES ─────────────────────────────────────────────────────────────────────
203
+ CCKIT_RECEIPT_REMOTE=1 sr_mirror 42 build 1 >/dev/null 2>&1
204
+ t "a first mirror creates" "$SR_MIRROR_LAST_RESULT" "created"
205
+ yes "and it commented on the issue" "$(cat "$GH_LOG")" "issue comment 42"
206
+
207
+ # ── the same attempt again EDITS the one comment, it does not append a second ──────────────────
208
+ printf '%s\t%s\n' 777 '"<!-- cckit:receipt key=42-build-1 --> already here"' > "$GH_COMMENTS"
209
+ : > "$GH_LOG"
210
+ CCKIT_RECEIPT_REMOTE=1 sr_mirror 42 build 1 >/dev/null 2>&1
211
+ t "a second identical run edits" "$SR_MIRROR_LAST_RESULT" "updated"
212
+ yes "and it PATCHed the existing comment" "$(cat "$GH_LOG")" "issues/comments/777"
213
+ t "it did not also create one" "$(grep -c 'issue comment' "$GH_LOG" || true)" "0"
214
+
215
+ # ── a FAILED lookup must not post: a blind post is how a duplicate appears ─────────────────────
216
+ : > "$GH_LOG"
217
+ GH_RC=1 CCKIT_RECEIPT_REMOTE=1 sr_mirror 42 build 1 >/dev/null 2>&1
218
+ t "a failed lookup does not post" "$SR_MIRROR_LAST_RESULT" "lookup-failed"
219
+ t "and nothing was created" "$(grep -c 'issue comment' "$GH_LOG" || true)" "0"
220
+
221
+ # ── an unreachable GitHub still leaves the local file, and exits 0 ─────────────────────────────
222
+ GH_RC=1 CCKIT_RECEIPT_REMOTE=1 sr_mirror 42 build 1 >/dev/null 2>&1
223
+ t "an unreachable GitHub exits 0" "$?" "0"
224
+ t "and the local receipt is still there" "$([ -f "$(sr_path 42 build 1)" ] && echo yes)" "yes"
225
+
226
+ # ── --no-remote writes the file and posts nothing ──────────────────────────────────────────────
227
+ : > "$GH_LOG"
228
+ out="$(printf '%s\n' "$REPORT" | CCKIT_RECEIPT_REMOTE=1 sr_cli 43 --stage build --attempt 1 --no-remote 2>/dev/null)"
229
+ t "--no-remote still writes the receipt" "$([ -f "$out" ] && echo yes)" "yes"
230
+ t "--no-remote posts nothing" "$(wc -l < "$GH_LOG" | tr -d ' ')" "0"
231
+ # SR_MIRROR_LAST_RESULT is checked on sr_mirror directly, not through sr_cli: a command
232
+ # substitution runs in a subshell, so a variable the callee sets never reaches this shell.
233
+
234
+ # ── CCKIT_RECEIPT_REMOTE=0 is the same switch, as an env var ───────────────────────────────────
235
+ : > "$GH_LOG"
236
+ CCKIT_RECEIPT_REMOTE=0 sr_mirror 42 build 1 >/dev/null 2>&1
237
+ t "the env var turns the mirror off" "$SR_MIRROR_LAST_RESULT" "no-remote"
238
+ t "and posts nothing" "$(wc -l < "$GH_LOG" | tr -d ' ')" "0"
239
+
240
+ # ── the body carries the receipt verbatim ──────────────────────────────────────────────────────
241
+ body="$(_sr_mirror_body 42 build 1 "$(sr_path 42 build 1)")"
242
+ yes "the body carries its marker" "$body" "<!-- cckit:receipt key=42-build-1 -->"
243
+ yes "the body names the stage" "$body" 'stage `build`'
244
+ yes "the body embeds the JSON" "$body" '"outcome": "pr-open"'
245
+
246
+ PATH="$OLDPATH"; export PATH
247
+ rm -rf "$ghtmp"
248
+
249
+
250
+ rm -rf "$(dirname "$KIT_STATE_DIR")"
251
+ [ "$fail" -eq 0 ] && echo "ALL OK" || echo "stage receipt: FAILURES"
252
+ exit "$fail"