@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,442 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # stage-receipt.sh — one durable receipt per stage attempt (#322).
4
+ #
5
+ # A worker's conversation is not a record. It lives in a pane, it does not survive a Herdr server
6
+ # restart (pane history is off by default and may hold secrets, so cckit must never require it),
7
+ # and nothing can query it. The receipt is the record: one small JSON file per attempt, written
8
+ # where every worktree of the repo can see it, holding only what a captain needs to pick the next
9
+ # wave without reading a transcript.
10
+ #
11
+ # Keyed by issue + stage + ATTEMPT, which is what makes a retry safe. The research decision on #326
12
+ # is explicit: operations must be idempotent and safe to re-run after an unknown result. Re-running
13
+ # one attempt rewrites its own file; a genuinely new try takes the next number and neither loses
14
+ # the other. Nothing appends, so a retry never doubles a record.
15
+ #
16
+ # The five reported fields are #318's acceptance and E318.5's receipt contract, verbatim — that
17
+ # contract is the format this file parses, so the prompt and the parser cannot drift apart. The
18
+ # resolved profile, its tier, where it was resolved from, and its permission policy are recorded
19
+ # alongside, per #326: a run's effective agent and policy must be auditable after the fact.
20
+ #
21
+ # sr_dir the receipts directory (does not create it)
22
+ # sr_path <issue> <stage> <attempt> one receipt's absolute path
23
+ # sr_next_attempt <issue> <stage> highest recorded attempt + 1 (1 when none)
24
+ # sr_parse stdin: a worker's report -> tab-separated fields
25
+ # sr_validate <outcome> <gate> <next> rc 2 with the vocabulary when a value is not in it
26
+ # sr_record <issue> <stage> <attempt> … parse + validate + write one receipt
27
+ # sr_read <issue> <stage> <attempt> echo one receipt's JSON
28
+ # sr_latest <issue> <stage> echo the most recent attempt's JSON
29
+ # sr_list [<issue>] one `issue stage attempt outcome` row per receipt
30
+ # sr_mirror <issue> <stage> <attempt> upsert that receipt as a comment on the issue (#333)
31
+ #
32
+ # errors: mixed — the parsers are pure; sr_record returns 2 on an invalid field, 3 without jq,
33
+ # 1 when the state directory cannot be created.
34
+
35
+ # BASH_SOURCE is bash-only and empty under zsh, the session shell here; `dirname ""` then yields
36
+ # "." and the sibling source below silently missed. Same defect and same fix as kit-gc.sh:38 (#219):
37
+ # `${BASH_SOURCE[0]:-$0}` covers zsh, CDPATH='' and the redirect stop an interactive zsh echoing the
38
+ # directory into the substitution.
39
+ _sr_dir="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null 2>&1 && pwd)"
40
+ # shellcheck source=/dev/null
41
+ command -v kit_state_dir >/dev/null 2>&1 || . "$_sr_dir/kit-state.sh"
42
+
43
+ # The controlled vocabularies. They are the receipt contract's, and a captain branches on them, so
44
+ # a value outside them is not a receipt with a typo — it is a routing decision nobody can make.
45
+ SR_OUTCOMES="merged pr-open closed-no-op blocked"
46
+ SR_GATES="pass fail"
47
+ SR_STAGES="build review design docs none"
48
+
49
+ _sr_need_jq() {
50
+ command -v jq >/dev/null 2>&1 || { echo "stage-receipt: jq is required to read and write receipts" >&2; return 3; }
51
+ }
52
+
53
+ _sr_in() {
54
+ local want="$1" list="$2" x
55
+ for x in $list; do [ "$x" = "$want" ] && return 0; done
56
+ return 1
57
+ }
58
+
59
+ # sr_dir — where receipts live. Under the shared state dir, so the same directory answers from
60
+ # every worktree: a receipt written by a worker in its own isolation worktree has to be readable
61
+ # by the captain standing somewhere else, which is the entire point of kit_state_dir.
62
+ sr_dir() { printf '%s/receipts\n' "$(kit_state_dir)"; }
63
+
64
+ # sr_path <issue> <stage> <attempt> — one receipt. The filename IS the key, so two writes of the
65
+ # same attempt land on the same file and the second replaces the first.
66
+ sr_path() {
67
+ local n="${1:-}" stage="${2:-}" attempt="${3:-1}"
68
+ [ -n "$n" ] && [ -n "$stage" ] || { echo "sr_path: <issue> <stage> required" >&2; return 2; }
69
+ printf '%s/%s-%s-%s.json\n' "$(sr_dir)" "$n" "$stage" "$attempt"
70
+ }
71
+
72
+ # sr_next_attempt <issue> <stage> — the number a NEW try should use. Derived from what is on disk
73
+ # rather than kept in a counter: a counter is state that can disagree with the files it counts.
74
+ sr_next_attempt() {
75
+ local n="${1:-}" stage="${2:-}" d f max=0 a
76
+ [ -n "$n" ] && [ -n "$stage" ] || { echo "sr_next_attempt: <issue> <stage> required" >&2; return 2; }
77
+ d="$(sr_dir)"
78
+ [ -d "$d" ] || { printf '1\n'; return 0; }
79
+ for f in "$d/$n-$stage-"*.json; do
80
+ [ -f "$f" ] || continue
81
+ a="${f##*-}"; a="${a%.json}"
82
+ case "$a" in ''|*[!0-9]*) continue ;; esac
83
+ [ "$a" -gt "$max" ] && max="$a"
84
+ done
85
+ printf '%s\n' "$(( max + 1 ))"
86
+ }
87
+
88
+ # sr_parse — stdin is whatever the worker reported; echo the five contract fields, tab-separated,
89
+ # in contract order: outcome, url, gate, blocker, next stage.
90
+ #
91
+ # Only the labelled lines are read, so a worker that wraps its receipt in prose still parses. The
92
+ # LAST occurrence of each label wins: a worker that restates its receipt after a correction means
93
+ # the correction, and taking the first would record the value it just retracted.
94
+ sr_parse() {
95
+ awk '
96
+ function val(s) { sub(/^[^:]*:[[:space:]]*/, "", s); gsub(/^[[:space:]]+|[[:space:]]+$/, "", s); return s }
97
+ /^[[:space:]]*outcome[[:space:]]*:/ { o = val($0) }
98
+ /^[[:space:]]*url[[:space:]]*:/ { u = val($0) }
99
+ /^[[:space:]]*gate[[:space:]]*:/ { g = val($0) }
100
+ /^[[:space:]]*blocker[[:space:]]*:/ { b = val($0) }
101
+ /^[[:space:]]*next[ _]stage[[:space:]]*:/ { s = val($0) }
102
+ END { printf "%s\t%s\t%s\t%s\t%s\n", o, u, g, b, s }
103
+ '
104
+ }
105
+
106
+ # sr_invalid_fields <outcome> <gate> <next-stage> — echo the name of each field whose value is
107
+ # outside its vocabulary, one per line. Empty output means the receipt is clean.
108
+ #
109
+ # An odd value is RECORDED and flagged, not refused. Refusing looked defensible — a crashed worker
110
+ # already leaves no receipt, so "no receipt" is a case the captain must handle regardless — but it
111
+ # collapses two states the captain has to tell apart:
112
+ #
113
+ # agent crashed -> no receipt -> re-run the whole stage
114
+ # agent finished, wrote `outcome: shipped` -> no receipt -> re-run work that is already DONE
115
+ #
116
+ # The second has a PR sitting there; it just used a word outside the list. Keeping the value and
117
+ # naming the offending field preserves that difference, which is what lets a wave keep moving
118
+ # without a human deciding which of the two it is looking at.
119
+ #
120
+ # `gate` is checked on its first word only: `fail — shellcheck` is well-formed, and the detail
121
+ # after the dash is the useful part.
122
+ sr_invalid_fields() {
123
+ local outcome="${1:-}" gate="${2:-}" next="${3:-}" g
124
+ _sr_in "$outcome" "$SR_OUTCOMES" || printf 'outcome\n'
125
+ g="${gate%%[[:space:]]*}"; g="${g%%—*}"
126
+ _sr_in "$g" "$SR_GATES" || printf 'gate\n'
127
+ _sr_in "$next" "$SR_STAGES" || printf 'next_stage\n'
128
+ return 0
129
+ }
130
+
131
+ # sr_validate <outcome> <gate> <next-stage> — rc 2 naming every offending field. The hard-check
132
+ # form, for a caller that wants a receipt refused rather than flagged.
133
+ sr_validate() {
134
+ local bad; bad="$(sr_invalid_fields "$@")"
135
+ [ -z "$bad" ] && return 0
136
+ printf 'stage-receipt: %s\n' "$(printf '%s' "$bad" | tr '\n' ' ')" >&2
137
+ echo " outcome: $SR_OUTCOMES" >&2
138
+ echo " gate: $SR_GATES" >&2
139
+ echo " next: $SR_STAGES" >&2
140
+ return 2
141
+ }
142
+
143
+ # sr_record <issue> <stage> <attempt> <profile> <tier> <source> <permissions> [<head-sha>] — stdin
144
+ # is the worker's report. Parse, validate, then write. Echoes the path it wrote.
145
+ #
146
+ # <head-sha> is the commit the work applies to. It is OPTIONAL and empty by default, so every caller
147
+ # written before it stays correct — but a receipt without one cannot be checked against the revision
148
+ # it was produced from, and a gate reading such a receipt is trusting a verdict that may predate the
149
+ # code. The review gate (captain.sh) requires it for exactly that reason.
150
+ sr_record() {
151
+ local n="${1:-}" stage="${2:-}" attempt="${3:-1}" prof="${4:-}" tier="${5:-}" src="${6:-}" perms="${7:-}" head="${8:-}"
152
+ local parsed="" outcome="" url="" gate="" blocker="" next="" d="" p=""
153
+ [ -n "$n" ] && [ -n "$stage" ] || { echo "sr_record: <issue> <stage> required" >&2; return 2; }
154
+ _sr_need_jq || return $?
155
+
156
+ parsed="$(sr_parse)"
157
+ IFS="$(printf '\t')" read -r outcome url gate blocker next <<EOF
158
+ $parsed
159
+ EOF
160
+ # The one hard refusal: no `outcome:` line at all means the worker reported nothing, and an
161
+ # all-empty receipt is noise a captain has to read past. "Reported nothing" and "reported oddly"
162
+ # are different, and only the first is worth discarding.
163
+ [ -n "$outcome" ] || {
164
+ echo "stage-receipt: the report has no 'outcome:' line — nothing to record" >&2
165
+ return 2
166
+ }
167
+ local invalid; invalid="$(sr_invalid_fields "$outcome" "$gate" "$next")"
168
+
169
+ d="$(sr_dir)"
170
+ mkdir -p "$d" 2>/dev/null || { echo "stage-receipt: cannot create $d" >&2; return 1; }
171
+ p="$(sr_path "$n" "$stage" "$attempt")"
172
+ jq -n \
173
+ --argjson issue "$n" --arg stage "$stage" --argjson attempt "$attempt" \
174
+ --arg profile "$prof" --arg tier "$tier" --arg profile_source "$src" --arg permissions "$perms" \
175
+ --arg head_sha "$head" \
176
+ --arg outcome "$outcome" --arg url "$url" --arg gate "$gate" \
177
+ --arg blocker "$blocker" --arg next_stage "$next" \
178
+ --arg recorded_at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
179
+ --arg invalid "$invalid" \
180
+ '{issue:$issue, stage:$stage, attempt:$attempt,
181
+ profile:$profile, tier:$tier, profile_source:$profile_source, permissions:$permissions,
182
+ head_sha:$head_sha,
183
+ outcome:$outcome, url:$url, gate:$gate, blocker:$blocker, next_stage:$next_stage,
184
+ invalid:($invalid | split("\n") | map(select(length > 0))),
185
+ recorded_at:$recorded_at}' > "$p" || return 1
186
+ printf '%s\n' "$p"
187
+ }
188
+
189
+ # sr_read <issue> <stage> <attempt> — echo one receipt. rc 1 when it does not exist.
190
+ sr_read() {
191
+ local p; p="$(sr_path "$@")" || return 2
192
+ [ -f "$p" ] || return 1
193
+ cat "$p"
194
+ }
195
+
196
+ # sr_latest <issue> <stage> — echo the highest-numbered attempt's receipt. rc 1 when there is none.
197
+ sr_latest() {
198
+ local n="${1:-}" stage="${2:-}" a
199
+ a="$(sr_next_attempt "$n" "$stage")" || return 2
200
+ [ "$a" -gt 1 ] || return 1
201
+ sr_read "$n" "$stage" "$(( a - 1 ))"
202
+ }
203
+
204
+ # sr_list [<issue>] — one `issue stage attempt outcome` row per receipt, sorted. Every receipt when
205
+ # no issue is given.
206
+ sr_list() {
207
+ local n="${1:-}" d f
208
+ _sr_need_jq || return $?
209
+ d="$(sr_dir)"
210
+ [ -d "$d" ] || return 0
211
+ for f in "$d/${n:+$n-}"*.json; do
212
+ [ -f "$f" ] || continue
213
+ jq -r '[(.issue|tostring), .stage, (.attempt|tostring),
214
+ (.outcome + (if ((.invalid // []) | length) > 0 then " (invalid: " + ((.invalid|join(","))) + ")" else "" end))]
215
+ | @tsv' "$f" 2>/dev/null
216
+ done | sort -k1,1n -k2,2 -k3,3n
217
+ }
218
+
219
+ # _sr_same_as_latest <issue> <stage> <outcome> <url> <gate> <blocker> <next> — rc 0 when the latest
220
+ # receipt already says exactly this.
221
+ _sr_same_as_latest() {
222
+ local n="$1" stage="$2" prev
223
+ prev="$(sr_latest "$n" "$stage" 2>/dev/null)" || return 1
224
+ [ -n "$prev" ] || return 1
225
+ # Field by field through jq, not by joining both sides into one string: a joined comparison needs
226
+ # a separator that cannot occur in a field, and the obvious candidate (a NUL) does not survive
227
+ # command substitution at all.
228
+ printf '%s' "$prev" | jq -e \
229
+ --arg o "$3" --arg u "$4" --arg g "$5" --arg b "$6" --arg s "$7" \
230
+ '.outcome == $o and .url == $u and .gate == $g and .blocker == $b and .next_stage == $s' \
231
+ >/dev/null 2>&1
232
+ }
233
+
234
+ # sr_cli <args> — the `cckit receipt` verb.
235
+ #
236
+ # The WORKER records its own receipt. Nothing else can: cckit does not read pane output, and #326
237
+ # is explicit that pane history is off by default, may hold secrets, and must never be required.
238
+ # A receipt the worker writes through a verb is durable whether or not its pane survives.
239
+ #
240
+ # The attempt number is allocated HERE rather than passed in, because the worker does not know it.
241
+ # A re-run whose five fields are IDENTICAL to the latest receipt rewrites that attempt instead of
242
+ # taking a new one — that is what makes the verb safe to retry after an unknown result, the #326
243
+ # requirement. A re-run that says something DIFFERENT genuinely is a new attempt and gets its own
244
+ # number, so neither record is lost.
245
+ sr_cli() {
246
+ # Every local gets a value: `local n stage` leaves them UNSET, not empty, and a caller running
247
+ # under `set -u` (bin/cckit does) dies on the first reference instead of reaching the refusal.
248
+ local sub="${1:-}" n="" stage="" attempt="" prof="" tier="" src="" perms="" report="" remote=1
249
+ case "$sub" in
250
+ list) shift; sr_list "${1:-}"; return $? ;;
251
+ show)
252
+ shift
253
+ [ -n "${1:-}" ] && [ -n "${2:-}" ] || { echo "cckit receipt show <issue> <stage> [<attempt>]" >&2; return 2; }
254
+ if [ -n "${3:-}" ]; then sr_read "$1" "$2" "$3"; else sr_latest "$1" "$2"; fi
255
+ return $?
256
+ ;;
257
+ ''|-h|--help)
258
+ cat >&2 <<'EOF'
259
+ cckit receipt <issue> --stage <stage> [--attempt <n>] [--profile <p>] [--tier <t>]
260
+ [--source <where>] [--permissions <policy>] [--no-remote]
261
+ < the report on stdin
262
+ cckit receipt list [<issue>]
263
+ cckit receipt show <issue> <stage> [<attempt>]
264
+
265
+ Stages: build review design docs none
266
+ Outcomes: merged pr-open closed-no-op blocked
267
+
268
+ The receipt is written locally AND mirrored as a comment on the issue. --no-remote (or
269
+ CCKIT_RECEIPT_REMOTE=0) writes the file and posts nothing; a remote failure only warns.
270
+ EOF
271
+ return 2
272
+ ;;
273
+ esac
274
+
275
+ n="$1"; shift
276
+ case "$n" in ''|*[!0-9]*) echo "cckit receipt: <issue> must be a number, got '$n'" >&2; return 2 ;; esac
277
+ while [ "$#" -gt 0 ]; do
278
+ case "$1" in
279
+ --stage) stage="$2"; shift 2 ;;
280
+ --stage=*) stage="${1#*=}"; shift ;;
281
+ --attempt) attempt="$2"; shift 2 ;;
282
+ --attempt=*) attempt="${1#*=}"; shift ;;
283
+ --profile) prof="$2"; shift 2 ;;
284
+ --profile=*) prof="${1#*=}"; shift ;;
285
+ --tier) tier="$2"; shift 2 ;;
286
+ --tier=*) tier="${1#*=}"; shift ;;
287
+ --source) src="$2"; shift 2 ;;
288
+ --source=*) src="${1#*=}"; shift ;;
289
+ --permissions) perms="$2"; shift 2 ;;
290
+ --permissions=*) perms="${1#*=}"; shift ;;
291
+ --no-remote) remote=0; shift ;;
292
+ *) echo "cckit receipt: unknown arg '$1'" >&2; return 2 ;;
293
+ esac
294
+ done
295
+ [ -n "$stage" ] || { echo "cckit receipt: --stage is required (one of: $SR_STAGES)" >&2; return 2; }
296
+ _sr_in "$stage" "$SR_STAGES" || { echo "cckit receipt: stage '$stage' is not one of: $SR_STAGES" >&2; return 2; }
297
+ _sr_need_jq || return $?
298
+
299
+ report="$(cat)"
300
+ if [ -z "$attempt" ]; then
301
+ local pf o u g b s
302
+ pf="$(printf '%s\n' "$report" | sr_parse)"
303
+ IFS="$(printf '\t')" read -r o u g b s <<PARSED
304
+ $pf
305
+ PARSED
306
+ if _sr_same_as_latest "$n" "$stage" "$o" "$u" "$g" "$b" "$s"; then
307
+ attempt="$(( $(sr_next_attempt "$n" "$stage") - 1 ))"
308
+ else
309
+ attempt="$(sr_next_attempt "$n" "$stage")"
310
+ fi
311
+ fi
312
+ local written
313
+ written="$(printf '%s\n' "$report" | sr_record "$n" "$stage" "$attempt" "$prof" "$tier" "$src" "$perms")" || return $?
314
+ printf '%s\n' "$written"
315
+
316
+ # Mirror AFTER the local write, never instead of it (#333). sr_mirror is best-effort and always
317
+ # returns 0, so a network failure cannot turn a recorded receipt into a failed command.
318
+ if [ "$remote" -eq 1 ]; then
319
+ sr_mirror "$n" "$stage" "$attempt" "$written"
320
+ else
321
+ SR_MIRROR_LAST_RESULT=no-remote
322
+ fi
323
+ return 0
324
+ }
325
+
326
+ # ── the GitHub mirror (#333) ────────────────────────────────────────────────────────────────────
327
+ # The local file is per-machine and gitignored. `effort-model.md` opens with "GitHub is the single
328
+ # source of truth" and #326 calls receipts the durable workflow record — a record that dies with
329
+ # the clone is neither. So every receipt is ALSO posted as a comment on its issue.
330
+ #
331
+ # The local store is not moved. It is what makes `cckit receipt list` and `show` answer offline,
332
+ # with no network round trip per read; the mirror is a second copy, not a relocation.
333
+ #
334
+ # UPSERT on the same marker pattern as pr-evidence.sh, keyed by issue+stage+ATTEMPT — the same key
335
+ # the local file uses. Re-running one attempt edits that attempt's comment; a genuinely new attempt
336
+ # gets its own. Neither doubles a record, which matches what sr_path already guarantees on disk.
337
+ #
338
+ # BEST-EFFORT BY DESIGN. Every remote failure warns and returns 0, because the local write has
339
+ # already succeeded by the time this runs: a receipt lost because the network blipped is worse than
340
+ # one that is only local. `$SR_MIRROR_LAST_RESULT` names the outcome so a caller can still tell —
341
+ # created · updated · no-gh · no-file · no-remote · lookup-failed · post-failed.
342
+
343
+ SR_MIRROR_LAST_RESULT="${SR_MIRROR_LAST_RESULT:-}"
344
+
345
+ # Stable across versions by contract: changing it orphans every receipt comment already posted, so
346
+ # an older cckit's comments would be appended to instead of edited.
347
+ SR_MARKER_PREFIX='<!-- cckit:receipt key='
348
+
349
+ # _sr_marker <issue> <stage> <attempt> — the invisible identity line. GitHub renders nothing.
350
+ _sr_marker() { printf '%s%s-%s-%s -->' "$SR_MARKER_PREFIX" "$1" "$2" "$3"; }
351
+
352
+ # _sr_api_path <repo> <suffix> — an empty repo yields the {owner}/{repo} placeholders, which gh
353
+ # fills from the current repo.
354
+ _sr_api_path() {
355
+ if [ -n "$1" ]; then printf 'repos/%s/%s' "$1" "$2"; else printf 'repos/{owner}/{repo}/%s' "$2"; fi
356
+ }
357
+
358
+ # _sr_repo — the target repo, or empty to let gh resolve it.
359
+ _sr_repo() { printf '%s' "${SR_REPO:-${KIT_REPO:-}}"; }
360
+
361
+ # _sr_mirror_body <issue> <stage> <attempt> <receipt-file> — the comment, marker first.
362
+ # Deterministic given the file, so re-posting an unchanged receipt is a genuine no-op edit rather
363
+ # than timeline churn. The JSON goes in verbatim: the receipt IS the record, and a prose rendering
364
+ # of it would be a second format to keep in sync with sr_record.
365
+ _sr_mirror_body() {
366
+ printf '%s\n\n' "$(_sr_marker "$1" "$2" "$3")"
367
+ printf '**cckit receipt** — stage `%s`, attempt %s\n\n' "$2" "$3"
368
+ printf '```json\n'
369
+ cat "$4"
370
+ printf '```\n'
371
+ }
372
+
373
+ # _sr_mirror_list <repo> <issue> — `<id><TAB><json-escaped body>` per comment. @json keeps each
374
+ # body on ONE line so a multi-line receipt can never be mistaken for another comment's row.
375
+ _sr_mirror_list() {
376
+ gh api --paginate "$(_sr_api_path "$1" "issues/$2/comments")" --jq '.[] | "\(.id)\t\(.body | @json)"' 2>/dev/null
377
+ }
378
+
379
+ # _sr_mirror_match_id <marker> — ids of every comment carrying the marker, oldest first. Pure:
380
+ # reads `<id><TAB><body>` on stdin, no gh. rc 1 when none match.
381
+ _sr_mirror_match_id() {
382
+ local marker="$1" id rest found=1
383
+ while read -r id rest; do
384
+ case "$rest" in *"$marker"*) ;; *) continue ;; esac
385
+ case "$id" in ''|*[!0-9]*) continue ;; esac
386
+ printf '%s\n' "$id"
387
+ found=0
388
+ done
389
+ return "$found"
390
+ }
391
+
392
+ _sr_mirror_edit() { gh api --method PATCH "$(_sr_api_path "$1" "issues/comments/$2")" -F "body=@$3" >/dev/null 2>&1; }
393
+ _sr_mirror_create() {
394
+ if [ -n "$1" ]; then gh issue comment "$2" --repo "$1" --body-file "$3" >/dev/null 2>&1
395
+ else gh issue comment "$2" --body-file "$3" >/dev/null 2>&1; fi
396
+ }
397
+
398
+ _sr_mirror_warn() { SR_MIRROR_LAST_RESULT="$1"; echo "sr_mirror: $2" >&2; return 0; }
399
+
400
+ # sr_mirror <issue> <stage> <attempt> [<receipt-file>] — upsert the receipt onto the issue.
401
+ # Always rc 0 (see BEST-EFFORT above); read $SR_MIRROR_LAST_RESULT for what happened.
402
+ sr_mirror() {
403
+ local n="${1:-}" stage="${2:-}" attempt="${3:-1}" file="${4:-}" repo marker ids id body rc
404
+ [ -n "$n" ] && [ -n "$stage" ] || { echo "sr_mirror: <issue> <stage> required" >&2; return 2; }
405
+ [ -n "$file" ] || file="$(sr_path "$n" "$stage" "$attempt")"
406
+
407
+ case "${CCKIT_RECEIPT_REMOTE:-1}" in
408
+ 0|false|no|off|FALSE|NO|OFF) _sr_mirror_warn no-remote "CCKIT_RECEIPT_REMOTE is off — receipt written locally only"; return 0 ;;
409
+ esac
410
+ command -v gh >/dev/null 2>&1 || { _sr_mirror_warn no-gh "gh is not installed — receipt written locally only"; return 0; }
411
+ [ -f "$file" ] || { _sr_mirror_warn no-file "no receipt at $file"; return 0; }
412
+
413
+ repo="$(_sr_repo)"
414
+ marker="$(_sr_marker "$n" "$stage" "$attempt")"
415
+
416
+ body="$(mktemp 2>/dev/null)" || { _sr_mirror_warn post-failed "no temp file"; return 0; }
417
+ _sr_mirror_body "$n" "$stage" "$attempt" "$file" > "$body"
418
+
419
+ # A lookup that FAILED and a lookup that found nothing are different: posting after a failed
420
+ # lookup is how a duplicate comment appears. Separate the gh rc from the match rc.
421
+ local listing
422
+ listing="$(_sr_mirror_list "$repo" "$n")"; rc=$?
423
+ if [ "$rc" -ne 0 ]; then
424
+ rm -f "$body"
425
+ _sr_mirror_warn lookup-failed "could not read the comments on #$n — not posting (a blind post would duplicate)"
426
+ return 0
427
+ fi
428
+
429
+ ids="$(printf '%s\n' "$listing" | _sr_mirror_match_id "$marker")" || ids=""
430
+ if [ -n "$ids" ]; then
431
+ id="$(printf '%s\n' "$ids" | head -1)"
432
+ if _sr_mirror_edit "$repo" "$id" "$body"; then
433
+ rm -f "$body"; SR_MIRROR_LAST_RESULT=updated; return 0
434
+ fi
435
+ rm -f "$body"; _sr_mirror_warn post-failed "could not edit comment $id on #$n"; return 0
436
+ fi
437
+
438
+ if _sr_mirror_create "$repo" "$n" "$body"; then
439
+ rm -f "$body"; SR_MIRROR_LAST_RESULT=created; return 0
440
+ fi
441
+ rm -f "$body"; _sr_mirror_warn post-failed "could not comment on #$n"; return 0
442
+ }
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env bash
2
+ # test-env-test.sh — test_env_seal removes the caller's kit environment, and only that (#369).
3
+ # Run: bash scripts/lib/test-env-test.sh
4
+ # errors: strict — a test runner: rc 1 on any failed assertion
5
+ set -u
6
+ LIB="$(cd "$(dirname "$0")" && pwd)"
7
+ fail=0
8
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
9
+
10
+ out="$(KIT_CONFIG=/nonexistent CCKIT_OUTPUT=json EFFORT_REPO=x/y KEEP_ME=1 bash -c '
11
+ . "$1/test-env.sh"
12
+ test_env_unset_args | paste -sd" " -
13
+ test_env_seal
14
+ echo "${KIT_CONFIG-unset} ${CCKIT_OUTPUT-unset} ${EFFORT_REPO-unset} ${KEEP_ME-unset}"' _ "$LIB")"
15
+ args="$(printf '%s\n' "$out" | sed -n 1p)"
16
+ after="$(printf '%s\n' "$out" | sed -n 2p)"
17
+ case " $args " in *" -u KIT_CONFIG "*) echo "ok: unset args name KIT_CONFIG" ;; *) echo "FAIL: unset args: $args"; fail=1 ;; esac
18
+ case " $args " in *" -u KEEP_ME "*) echo "FAIL: unset args name a non-kit variable: $args"; fail=1 ;; *) echo "ok: unset args leave other variables alone" ;; esac
19
+ t "seal unsets KIT_*, CCKIT_*, EFFORT_* and keeps the rest" "$after" "unset unset unset 1"
20
+
21
+ # The runner seals too: a test run under scripts/test.sh never sees the caller's KIT_CONFIG.
22
+ if [ -n "${KIT_CONFIG:-}" ]; then echo "FAIL: KIT_CONFIG leaked into this test"; fail=1; else echo "ok: no KIT_CONFIG reached this test"; fi
23
+
24
+ [ "$fail" -eq 0 ] && echo "test-env: all passed" || echo "test-env: FAILURES"
25
+ exit "$fail"
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # test-env.sh — seal a test from the caller's kit environment (#369).
4
+ #
5
+ # A developer shell that has run `load_kit_config` (or set KIT_CONFIG by hand) exports KIT_*,
6
+ # CCKIT_* and EFFORT_* variables. A test that inherits them reads the developer's project instead
7
+ # of its own fixture: KIT_CONFIG=/elsewhere made five suites fail. Source this first, then call
8
+ # test_env_seal before the test sets its own variables.
9
+ #
10
+ # test_env_seal unset every exported KIT_* / CCKIT_* / EFFORT_* variable in this shell
11
+ # test_env_unset_args print `-u NAME` pairs for env(1), one per line (the runner uses these)
12
+ #
13
+ # scripts/test.sh scrubs the same set before it runs each test; this covers a test run directly.
14
+ # errors: pure — no side effects beyond unsetting variables in the current shell
15
+ # bash 3.2 + zsh compatible.
16
+
17
+ _test_env_names() {
18
+ env | sed -n 's/^\(KIT_[A-Za-z0-9_]*\)=.*/\1/p; s/^\(CCKIT_[A-Za-z0-9_]*\)=.*/\1/p; s/^\(EFFORT_[A-Za-z0-9_]*\)=.*/\1/p'
19
+ }
20
+
21
+ test_env_seal() {
22
+ local n
23
+ for n in $(_test_env_names); do unset "$n"; done
24
+ return 0
25
+ }
26
+
27
+ test_env_unset_args() {
28
+ local n
29
+ for n in $(_test_env_names); do printf -- '-u\n%s\n' "$n"; done
30
+ }
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env bash
2
+ # worker-brief-test.sh — the bounded worker prompt (#321).
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
+ # shellcheck source=/dev/null
9
+ source "$ROOT/scripts/lib/worker-brief.sh"
10
+
11
+ fail=0
12
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
13
+ yes() { case "$2" in *"$3"*) echo "ok: $1" ;; *) echo "FAIL: $1 -> missing '$3' in [$2]"; fail=1 ;; esac; }
14
+ no() { case "$2" in *"$3"*) echo "FAIL: $1 -> found '$3' in [$2]"; fail=1 ;; *) echo "ok: $1" ;; esac; }
15
+
16
+ # Sections 2 and 3 are padded far larger than section 1 so a budget can keep exactly one of them
17
+ # without the test depending on byte-level arithmetic that drifts whenever the wording changes.
18
+ PAD="$(printf 'x%.0s' $(seq 1 2000))"
19
+ BRIEF="$(cat <<EOF
20
+ # Delegation brief — #41
21
+
22
+ - gate: \`bash scripts/check.sh\` must pass before the PR
23
+
24
+ ## Files this issue owns
25
+
26
+ - \`scripts/one.sh\`
27
+
28
+ ## Helpers you already have
29
+
30
+ $PAD
31
+
32
+ ## Standing gotchas
33
+
34
+ - Refresh the base first. $PAD
35
+ EOF
36
+ )"
37
+
38
+
39
+ # ── the invariants ─────────────────────────────────────────────────────────────────────────────
40
+ pre="$(wb_preamble 41 task/41-x)"
41
+ yes "the preamble names the issue" "$pre" "issue #41"
42
+ yes "the preamble names the branch" "$pre" "branch task/41-x"
43
+ yes "the preamble forbids reading the board" "$pre" "Do not read the board"
44
+
45
+ rc="$(wb_receipt_contract 41)"
46
+ for f in outcome url gate blocker "next stage"; do
47
+ yes "the receipt contract declares '$f'" "$rc" "$f:"
48
+ done
49
+ yes "the receipt contract names the PR verb" "$rc" "cckit pr 41"
50
+ yes "the receipt contract names the no-op close" "$rc" "cckit close 41"
51
+ # The contract must name the verb that PERSISTS it (#322). A prompt that only asks the worker to
52
+ # "report" leaves the record in the pane, which is what the receipt exists to replace.
53
+ yes "the receipt contract names the recording verb" "$rc" "cckit receipt 41 --stage"
54
+ yes "the receipt contract says a re-run is safe" "$rc" "rewrites its own attempt"
55
+
56
+ # ── unbounded: everything survives ─────────────────────────────────────────────────────────────
57
+ full="$(printf '%s\n' "$BRIEF" | wb_compose 41 task/41-x '')"
58
+ yes "with no budget the gotchas survive" "$full" "Refresh the base first"
59
+ yes "with no budget the framing survives" "$full" "running HEADLESS"
60
+ yes "with no budget the contract survives" "$full" "next stage:"
61
+ no "an unbounded brief claims no omission" "$full" "omitted to fit"
62
+
63
+ # ── bounded: sections drop from the END, and the cut is stated ──────────────────────────────────
64
+ # A budget with room for the invariants and the small first section, but not for either 2 KB one.
65
+ one=$(( $(wb_preamble 41 task/41-x | wc -c) + $(wb_receipt_contract 41 | wc -c) + 600 ))
66
+ cut="$(printf '%s\n' "$BRIEF" | wb_compose 41 task/41-x "$one")"
67
+ yes "the first section is kept" "$cut" "scripts/one.sh"
68
+ no "a later section is dropped" "$cut" "Refresh the base first"
69
+ yes "the cut is stated, not silent" "$cut" "omitted to fit"
70
+ yes "the omission NAMES what was dropped" "$cut" "Standing gotchas"
71
+ # `paste -d` cycles a LIST of delimiters, so `-d', '` joined names as `a,b c,d`.
72
+ yes "dropped names are comma-separated, not delimiter-cycled" "$cut" "Helpers you already have, Standing gotchas"
73
+ # The brief's pre-section metadata is where to work and what must pass — an invariant, not the
74
+ # cheapest section to drop.
75
+ yes "truncation keeps the brief's gate line" "$cut" "bash scripts/check.sh"
76
+ yes "truncation keeps the brief's title line" "$cut" "Delegation brief"
77
+ yes "the omission says how to recover it" "$cut" "cckit brief"
78
+ # Constraint 3: dropping sections must never drop the framing or the contract.
79
+ yes "truncation keeps the headless framing" "$cut" "running HEADLESS"
80
+ yes "truncation keeps the receipt contract" "$cut" "next stage:"
81
+ if [ "$(printf '%s' "$cut" | wc -c)" -le "$one" ]; then
82
+ echo "ok: the bounded brief is within its budget"
83
+ else
84
+ echo "FAIL: the bounded brief overran its budget"; fail=1
85
+ fi
86
+
87
+ # A budget too small for the invariants still emits them, and SAYS so — a worker with no receipt
88
+ # contract reports in a shape nothing can parse, which is worse than overrunning a number.
89
+ tiny="$(printf '%s\n' "$BRIEF" | wb_compose 41 task/41-x 10 2>/dev/null)"; trc=$?
90
+ t "an impossible budget is reported as rc 2" "$trc" "2"
91
+ yes "an impossible budget still emits the framing" "$tiny" "running HEADLESS"
92
+ yes "an impossible budget still emits the contract" "$tiny" "next stage:"
93
+ yes "an impossible budget explains itself on stderr" \
94
+ "$(printf '%s\n' "$BRIEF" | wb_compose 41 task/41-x 10 2>&1 >/dev/null)" "cannot hold the framing"
95
+
96
+ # ── the budget comes from the profile ──────────────────────────────────────────────────────────
97
+ cfg="$(mktemp)"
98
+ cat > "$cfg" <<'JSON'
99
+ {"agents":{"profiles":{
100
+ "cheap":{"kind":"claude","contextBudget":4096},
101
+ "loose":{"kind":"claude"},
102
+ "bad":{"kind":"claude","contextBudget":"lots"},
103
+ "zero":{"kind":"claude","contextBudget":0}
104
+ }}}
105
+ JSON
106
+ t "a declared budget is read" "$(wb_profile_budget "$cfg" cheap)" "4096"
107
+ t "an unset budget is empty (unbounded)" "$(wb_profile_budget "$cfg" loose)" ""
108
+ # Treated as absent, not as zero: refusing to brief a worker over a typo is the worse failure.
109
+ t "a non-numeric budget is treated as unset" "$(wb_profile_budget "$cfg" bad)" ""
110
+ t "a zero budget is treated as unset" "$(wb_profile_budget "$cfg" zero)" ""
111
+ t "an undeclared profile has no budget" "$(wb_profile_budget "$cfg" nope)" ""
112
+ rm -f "$cfg"
113
+
114
+ # ── the board never reaches a worker ───────────────────────────────────────────────────────────
115
+ printf '%s\n' "$full" | wb_has_board; t "a one-issue brief carries no board" "$?" "1"
116
+ printf -- '- #41 one\n- #42 two\n' | wb_has_board; t "an issue list IS board state" "$?" "0"
117
+ printf -- '| #41 | one |\n' | wb_has_board; t "a board table IS board state" "$?" "0"
118
+
119
+ [ "$fail" -eq 0 ] && echo "ALL OK" || echo "worker brief: FAILURES"
120
+ exit "$fail"