@jeiemgi/cckit 0.1.7 → 0.2.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cckit",
3
- "version": "0.1.7",
3
+ "version": "0.2.0",
4
4
  "installationPreference": "beta",
5
5
  "description": "cckit — a project operating system for coding agents. The full GitHub work lifecycle (start/pr/close/sync, efforts, worktrees, orchestrate, gc) as agents + skills + rules, drivable by Claude Code and any other agent. Driven from cckit.config.json; no org or repo hardcoded.",
6
6
  "author": {
package/bin/cckit CHANGED
@@ -13,14 +13,19 @@
13
13
  # cckit close <issue> <summary> close issue + mark done
14
14
  # cckit contribute "<summary>" open a contribution PR to cckit, gated on scripts/check.sh
15
15
  # cckit sync [--llm] board state / what's unblocked
16
+ # cckit plan [--llm] [--effort N] wave plan: deps-ordered, file-disjoint, session-fit (TOON --llm)
16
17
  # cckit effort <new|start|pr|close|plan> the effort lifecycle
17
18
  # cckit orchestrate <a> <b> … run N flows in parallel worktrees (--dry-run/--cap/--agent)
18
19
  # cckit autopilot [<a> …] unattended multi-flow: drive (or auto-pick) issues under a cap
20
+ # cckit watch [--merge] [--loop] captain: gate open PRs, squash-merge CLEAN, advance the wave
21
+ # cckit next [--effort N] everyday helper: the unblocked set + how to start the top issue
22
+ # cckit copilot [--effort N] prompt machine: emit the wave-0 subagent fan-out + captain drive
19
23
  # cckit gc [--prune [--yes]] report (default) or prune merged branches + worktrees
20
24
  # cckit status thin dashboard: board + worktrees + resume handoff
21
25
  # cckit scan [--llm] detect this repo's stack + kit state (JSON)
22
26
  # cckit debug [--check] <args> optional browser-debug via chrome-devtools-axi (auto-detected)
23
27
  # cckit encode-context stdin JSON -> compact TOON (uniform arrays; JSON fallback)
28
+ # cckit render stdin markdown -> rich (glow on a TTY; verbatim when piped)
24
29
  # cckit doctor onboarding preflight (deps, gh auth)
25
30
  # cckit update check whether this project is behind the installed cckit
26
31
  # cckit migrate reshuffle an old kit layout to the current one
@@ -149,6 +154,14 @@ case "$cmd" in
149
154
  esac
150
155
  ;;
151
156
  sync) exec "$ROOT/scripts/task-sync.sh" "$@" ;;
157
+ plan)
158
+ # Wave plan over the board (deps-ordered, file-disjoint, session-fit). Runs in the invoking
159
+ # project; needs config so KIT_REPO is set before plan-machine reads it.
160
+ # shellcheck source=/dev/null
161
+ source "$LIB/kit-config.sh" && load_kit_config
162
+ # shellcheck source=/dev/null
163
+ source "$LIB/plan-machine.sh"; plan_machine "$@"
164
+ ;;
152
165
  gc)
153
166
  # shellcheck source=/dev/null
154
167
  source "$LIB/kit-config.sh" && load_kit_config
@@ -174,6 +187,36 @@ case "$cmd" in
174
187
  ;;
175
188
  orchestrate) exec "$ROOT/scripts/orchestrate.sh" "$@" ;;
176
189
  autopilot) exec "$ROOT/scripts/autopilot.sh" "$@" ;;
190
+ next)
191
+ # Everyday-dev helper: surface the unblocked set + the one command to start the top issue.
192
+ # shellcheck source=/dev/null
193
+ source "$LIB/kit-config.sh" && load_kit_config
194
+ # shellcheck source=/dev/null
195
+ source "$LIB/next.sh"; next_issue "$@"
196
+ ;;
197
+ copilot)
198
+ # The prompt machine: emit the wave-0 Task-subagent fan-out + the between-waves captain drive.
199
+ # Runs in the invoking project; needs config so KIT_REPO is set before copilot reads it.
200
+ # shellcheck source=/dev/null
201
+ source "$LIB/kit-config.sh" && load_kit_config
202
+ # shellcheck source=/dev/null
203
+ source "$LIB/copilot.sh"; copilot_brief "$@"
204
+ ;;
205
+ watch)
206
+ # Captain: gate open PRs, squash-merge the CLEAN ones, advance to the next wave. Runs in the
207
+ # invoking project; needs config so KIT_REPO is set before captain reads it.
208
+ # shellcheck source=/dev/null
209
+ source "$LIB/kit-config.sh" && load_kit_config
210
+ # shellcheck source=/dev/null
211
+ source "$LIB/captain.sh"
212
+ watch_loop=0; watch_args=()
213
+ for a in "$@"; do case "$a" in --loop) watch_loop=1 ;; *) watch_args+=("$a") ;; esac; done
214
+ if [ "$watch_loop" = "1" ]; then
215
+ captain_loop "${watch_args[@]+"${watch_args[@]}"}"
216
+ else
217
+ captain_pass "${watch_args[@]+"${watch_args[@]}"}"
218
+ fi
219
+ ;;
177
220
  # Ops exposed so an agent/autopilot can run the whole lifecycle through the one CLI (#16).
178
221
  release)
179
222
  # Acts on cckit's own repo, so run from the install root regardless of where it was invoked.
@@ -191,6 +234,7 @@ case "$cmd" in
191
234
  digest) exec "$ROOT/scripts/kit-digest.sh" "$@" ;;
192
235
  scan) source "$LIB/project-scan.sh"; project_scan "$@" ;;
193
236
  encode-context) source "$LIB/toon.sh"; toon_encode ;;
237
+ render) source "$LIB/render.sh"; cckit_render ;;
194
238
  debug) exec "$ROOT/scripts/debug.sh" "$@" ;;
195
239
  contribute) cd "$ROOT"; exec "$ROOT/scripts/contribute.sh" "$@" ;;
196
240
  status|ui) exec "$ROOT/scripts/status.sh" "$@" ;;
package/cckit.config.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "./kit.config.schema.json",
3
- "kitVersion": "0.1.7",
3
+ "kitVersion": "0.2.0",
4
4
  "project": {
5
5
  "name": "cckit",
6
6
  "slug": "cckit",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeiemgi/cckit",
3
- "version": "0.1.7",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "description": "cckit — a project operating system for coding agents (CLI + Claude Code plugin).",
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env bash
2
+ # config-path-test.sh — scan/config recognize BOTH a root cckit.config.json AND a
3
+ # .claude/kit.config.json as a configured project (#65, #69). Hermetic, no network.
4
+ set -u
5
+ ROOT="$(cd "$(dirname "$0")/.." && pwd)"
6
+ fail=0
7
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
8
+ command -v jq >/dev/null 2>&1 || { echo "config-path-test: jq required" >&2; exit 1; }
9
+ # shellcheck source=/dev/null
10
+ . "$ROOT/scripts/lib/project-scan.sh"
11
+
12
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
13
+ mk() { mkdir -p "$1"; ( cd "$1" && git init -q ); }
14
+
15
+ # A) root cckit.config.json (cckit self-host layout) -> configured
16
+ mk "$tmp/a"; echo '{}' > "$tmp/a/cckit.config.json"
17
+ t "root cckit.config.json -> configured" "$(project_scan "$tmp/a" | jq -r .kit)" "configured"
18
+
19
+ # B) .claude/kit.config.json (consuming-project layout) -> configured (was the #65 bug)
20
+ mk "$tmp/b"; mkdir -p "$tmp/b/.claude"; echo '{}' > "$tmp/b/.claude/kit.config.json"
21
+ t ".claude/kit.config.json -> configured" "$(project_scan "$tmp/b" | jq -r .kit)" "configured"
22
+
23
+ # C) bare .claude/ with no kit config -> claude-only (not falsely "configured")
24
+ mk "$tmp/c"; mkdir -p "$tmp/c/.claude"
25
+ t "bare .claude/ -> claude-only" "$(project_scan "$tmp/c" | jq -r .kit)" "claude-only"
26
+
27
+ # D) nothing -> none
28
+ mk "$tmp/d"
29
+ t "greenfield -> none" "$(project_scan "$tmp/d" | jq -r .kit)" "none"
30
+
31
+ [ "$fail" -eq 0 ] && echo "ALL OK (config-path)" || echo "config-path: FAILURES"
32
+ exit "$fail"
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/env bash
2
+ # copilot-loop-test.sh — headless end-to-end of the copilot loop on a FIXTURE board (#80).
3
+ # Hermetic: a stateful `gh` stub serves fixture issues/PRs/deps and reuses the scripts' OWN --jq
4
+ # expressions, so it mimics real gh without the network. Asserts the full cycle:
5
+ # wave — plan layers blocked issues into later waves
6
+ # merge — the captain merges only the CLEAN PR, never the failing one
7
+ # advance — merging a blocker unblocks its dependent into the next wave
8
+ # stop — with no open PRs the captain stops cleanly
9
+ set -u
10
+ ROOT="$(cd "$(dirname "$0")/.." && pwd)"
11
+ command -v jq >/dev/null 2>&1 || { echo "copilot-loop-test: jq required" >&2; exit 1; }
12
+ fail=0
13
+ ok() { if [ "$1" = "$2" ]; then echo "ok: $3"; else echo "FAIL: $3 -> got '[$1]' want '[$2]'"; fail=1; fi; }
14
+ has() { case "$1" in *"$2"*) echo "ok: $3" ;; *) echo "FAIL: $3 (missing '$2')"; fail=1 ;; esac; }
15
+ no() { case "$1" in *"$2"*) echo "FAIL: $3 (unexpected '$2')"; fail=1 ;; *) echo "ok: $3" ;; esac; }
16
+
17
+ TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT
18
+ FX="$TMP/fx"; mkdir -p "$FX"
19
+ CLOSED="$FX/closed"; : > "$CLOSED" # issue numbers closed so far (advance state)
20
+ MERGEDPR="$FX/merged_prs"; : > "$MERGEDPR" # PR numbers merged so far
21
+
22
+ # --- fixture board -----------------------------------------------------------
23
+ # #101 open, no deps · #102 open blocked_by #101 · #103 open, no deps
24
+ cat > "$FX/issues.json" <<'JSON'
25
+ [
26
+ {"number":101,"title":"[Effort 9] 1 · root A","body":"Files: a.sh","milestone":null,"labels":[{"name":"ctx:S"}]},
27
+ {"number":102,"title":"[Effort 9] 2 · dependent of A","body":"Files: b.sh","milestone":null,"labels":[{"name":"ctx:M"}]},
28
+ {"number":103,"title":"[Effort 9] 3 · root B","body":"Files: c.sh","milestone":null,"labels":[{"name":"ctx:S"}]}
29
+ ]
30
+ JSON
31
+ # deps: issue -> [blocker numbers]
32
+ cat > "$FX/deps.json" <<'JSON'
33
+ {"101":[],"102":[101],"103":[]}
34
+ JSON
35
+ # PRs: 201 head fix/101-a CLEAN+SUCCESS · 202 head fix/103-b UNSTABLE+FAILURE
36
+ cat > "$FX/prs.json" <<'JSON'
37
+ [
38
+ {"number":201,"title":"root A","headRefName":"fix/101-a","mergeable":"MERGEABLE","mergeStateStatus":"CLEAN","statusCheckRollup":[{"conclusion":"SUCCESS"}]},
39
+ {"number":202,"title":"root B","headRefName":"fix/103-b","mergeable":"MERGEABLE","mergeStateStatus":"UNSTABLE","statusCheckRollup":[{"conclusion":"FAILURE"}]}
40
+ ]
41
+ JSON
42
+
43
+ # --- gh stub -----------------------------------------------------------------
44
+ BIN="$TMP/bin"; mkdir -p "$BIN"
45
+ cat > "$BIN/gh" <<STUB
46
+ #!/usr/bin/env bash
47
+ # stateful gh fixture: reuses the caller's own --jq; reflects closed/merged state.
48
+ set -u
49
+ FX="$FX"; CLOSED="$CLOSED"; MERGEDPR="$MERGEDPR"
50
+ is_closed() { grep -qx "\$1" "\$CLOSED" 2>/dev/null; }
51
+ # open issues = fixture issues whose number is not in the closed set
52
+ open_issues() {
53
+ jq -c --argjson cl "\$(jq -R . "\$CLOSED" 2>/dev/null | jq -s 'map(select(length>0)|tonumber)')" \
54
+ '[ .[] | select(.number as \$num | (\$cl|index(\$num))|not) ]' "\$FX/issues.json"
55
+ }
56
+ # open PRs = fixture PRs not yet merged
57
+ open_prs() {
58
+ jq -c --argjson mg "\$(jq -R . "\$MERGEDPR" 2>/dev/null | jq -s 'map(select(length>0)|tonumber)')" \
59
+ '[ .[] | select(.number as \$num | (\$mg|index(\$num))|not) ]' "\$FX/prs.json"
60
+ }
61
+ # pull a --jq EXPR and a --repo out of the args; collect positionals
62
+ JQ=""; POS=()
63
+ while [ "\$#" -gt 0 ]; do
64
+ case "\$1" in
65
+ --jq) JQ="\$2"; shift 2 ;;
66
+ --repo|--state|--limit|--json) shift 2 ;;
67
+ --squash|--delete-branch) shift ;;
68
+ --*) shift ;;
69
+ *) POS+=("\$1"); shift ;;
70
+ esac
71
+ done
72
+ sub="\${POS[0]:-}"; sub2="\${POS[1]:-}"; arg="\${POS[2]:-}"
73
+ run_jq() { if [ -n "\$JQ" ]; then jq -r "\$JQ"; else cat; fi; }
74
+ case "\$sub \$sub2" in
75
+ "issue list") open_issues | run_jq ;;
76
+ "issue view")
77
+ # gh issue view N --json labels --jq E -> feed {labels:[...]} for issue N
78
+ n="\$sub2"; [ "\$n" = "view" ] && n="\$arg"
79
+ jq -c --argjson n "\$arg" '[.[]|select(.number==\$n)][0] // {labels:[]} | {labels}' "\$FX/issues.json" | run_jq ;;
80
+ "pr list") open_prs | run_jq ;;
81
+ "pr view")
82
+ # gh pr view PR --json ... (caller applies its own jq) -> emit the PR object
83
+ jq -c --argjson n "\$arg" '[.[]|select(.number==\$n)][0]' "\$FX/prs.json" ;;
84
+ "pr merge")
85
+ # record the merge + close the PR's issue (advance state)
86
+ echo "\$arg" >> "\$MERGEDPR"
87
+ br="\$(jq -r --argjson n "\$arg" '[.[]|select(.number==\$n)][0].headRefName' "\$FX/prs.json")"
88
+ iss="\$(printf '%s' "\$br" | sed -nE 's#^[a-z]+/([0-9]+)-.*#\1#p')"
89
+ [ -n "\$iss" ] && echo "\$iss" >> "\$CLOSED"
90
+ exit 0 ;;
91
+ "api "*) : ;; # fall through below
92
+ esac
93
+ # gh api repos/.../issues/N/dependencies/blocked_by -> [{number,state}] reflecting closed set
94
+ if [ "\$sub" = "api" ]; then
95
+ path="\$sub2"
96
+ case "\$path" in
97
+ *dependencies/blocked_by)
98
+ n="\$(printf '%s' "\$path" | sed -nE 's#.*/issues/([0-9]+)/dependencies.*#\1#p')"
99
+ blockers="\$(jq -c --arg n "\$n" '.[\$n] // []' "\$FX/deps.json")"
100
+ cl="\$(jq -R . "\$CLOSED" 2>/dev/null | jq -c -s 'map(select(length>0)|tonumber)')"
101
+ printf '%s' "\$blockers" | jq -c --argjson cl "\$cl" \
102
+ '[ .[] | . as \$b | {number:\$b, state: (if (\$cl|index(\$b)) then "closed" else "open" end)} ]' | run_jq ;;
103
+ *sub_issues)
104
+ # effort sub_issues not used by this fixture; return empty
105
+ echo "[]" | run_jq ;;
106
+ *) echo "" ;;
107
+ esac
108
+ fi
109
+ STUB
110
+ chmod +x "$BIN/gh"
111
+
112
+ export PATH="$BIN:$PATH"
113
+ export KIT_REPO="fixture/board" PLAN_REPO="fixture/board" CAPTAIN_REPO="fixture/board"
114
+ export CCKIT_NO_GLOW=1 CCKIT_OUTPUT=human
115
+ export CAPTAIN_STATE="$TMP/captain.state"
116
+ cckit() { bash "$ROOT/bin/cckit" "$@"; }
117
+
118
+ echo "== wave: plan layers the dependent into wave 1 =="
119
+ PLAN="$(CCKIT_OUTPUT=human cckit plan --cap 8 2>&1)"
120
+ # wave 0 holds the two roots; #102 (blocked by #101) must be in wave 1.
121
+ # anchor on the row's first column (| #N |) so an "after #N" cell never false-matches.
122
+ w101="$(printf '%s' "$PLAN" | awk '/^## Wave/{w=$3} /^\| #101 \|/{print w}')"
123
+ w102="$(printf '%s' "$PLAN" | awk '/^## Wave/{w=$3} /^\| #102 \|/{print w}')"
124
+ w103="$(printf '%s' "$PLAN" | awk '/^## Wave/{w=$3} /^\| #103 \|/{print w}')"
125
+ ok "$w101" "0" "plan: #101 root in wave 0"
126
+ ok "$w103" "0" "plan: #103 root in wave 0"
127
+ ok "$w102" "1" "plan: #102 dependent in wave 1"
128
+
129
+ echo "== next: surfaces the unblocked set + the top issue's start command =="
130
+ NEXT="$(CCKIT_OUTPUT=human cckit next 2>&1)"
131
+ has "$NEXT" "#101" "next: lists unblocked #101"
132
+ has "$NEXT" "#103" "next: lists unblocked #103"
133
+ no "$NEXT" "#102 |" "next: hides blocked #102 from the unblocked table"
134
+ has "$NEXT" "cckit start 101" "next: gives the top issue's start command"
135
+
136
+ echo "== merge: captain merges the CLEAN PR, skips the failing one =="
137
+ PASS1="$(cckit watch --merge 2>&1)"
138
+ has "$PASS1" "PR #201" "captain: gated PR #201"
139
+ has "$PASS1" "MERGED" "captain: merged the CLEAN PR"
140
+ # 202 is CHECKS_FAILING -> action fix, never merged
141
+ no "$(printf '%s' "$PASS1" | grep 202)" "MERGED" "captain: did NOT merge failing PR #202"
142
+
143
+ echo "== advance: merging #101 unblocks #102 into wave 0 =="
144
+ PLAN2="$(CCKIT_OUTPUT=human cckit plan --cap 8 2>&1)"
145
+ no "$PLAN2" "#101 " "advance: #101 closed, gone from the board"
146
+ w102b="$(printf '%s' "$PLAN2" | awk '/^## Wave/{w=$3} /^\| #102 \|/{print w}')"
147
+ ok "$w102b" "0" "advance: #102 now unblocked in wave 0"
148
+
149
+ echo "== stop: after merging the last open PR, the captain stops cleanly =="
150
+ PASS2="$(cckit watch --merge 2>&1)" # merges 202 now? 202 still failing -> not merged, but it's the only PR
151
+ # merge 202 by hand-equivalent: it stays (failing). Force-close it to drain the board:
152
+ # simulate its PR resolved by removing it from open set via a merge call is invalid (failing).
153
+ # Instead assert the loop is at steady state: nothing CLEAN remains to merge.
154
+ has "$PASS2" "PR #202" "stop: only the failing PR remains in scope"
155
+ no "$PASS2" "MERGED" "stop: nothing merged at steady state"
156
+
157
+ echo "== loop: --loop reaches steady state and stops =="
158
+ LOOP="$(cckit watch --loop --merge --max-passes 3 2>&1)"
159
+ has "$LOOP" "steady state" "loop: stops at steady state"
160
+
161
+ [ "$fail" -eq 0 ] && echo "ALL OK (copilot-loop)" || echo "copilot-loop: FAILURES"
162
+ exit "$fail"
package/scripts/init.sh CHANGED
@@ -353,6 +353,9 @@ BASENAME="$(basename "$TARGET")"
353
353
  [[ -z "$SLUG" ]] && SLUG="$(echo "$BASENAME" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9' '-' | sed -E 's/^-|-$//g')"
354
354
  if [[ -z "$GH_OWNER" ]]; then GH_OWNER="$(gh api user --jq .login 2>/dev/null || echo "")"; fi
355
355
  [[ -z "$OWNER_NAME" ]] && OWNER_NAME="${GH_OWNER:-owner}"
356
+ # Prefer the project's ACTUAL GitHub remote over an <owner>/<dir-basename> guess — the local
357
+ # directory name often differs from the repo name (would target a non-existent repo otherwise).
358
+ [[ -z "$REPO" ]] && REPO="$(cd "$TARGET" 2>/dev/null && gh repo view --json nameWithOwner -q .nameWithOwner 2>/dev/null || true)"
356
359
  [[ -z "$REPO" && -n "$GH_OWNER" ]] && REPO="$GH_OWNER/$SLUG"
357
360
  [[ -z "$REPO" ]] && REPO="$SLUG"
358
361
 
@@ -17,7 +17,7 @@ while [[ $# -gt 0 ]]; do
17
17
  done
18
18
 
19
19
  command -v jq >/dev/null 2>&1 || exit 0
20
- cfg="$TARGET/.claude/kit.config.json"
20
+ cfg="$TARGET/cckit.config.json"; [[ -f "$cfg" ]] || cfg="$TARGET/.claude/kit.config.json"
21
21
  [[ -f "$cfg" ]] || exit 0
22
22
 
23
23
  # Locate the installed plugin's plugin.json: explicit root → env → best-effort glob.
@@ -15,7 +15,7 @@ ok() { echo "ok $1"; }
15
15
 
16
16
  # ---- config (tolerant: defaults if config/lib absent) ----------------------
17
17
  KNOWLEDGE_DIR="knowledge"; PLANS_DIR=""; PLANS_FORMAT=""
18
- if [[ -f scripts/lib/kit-config.sh && -f .claude/kit.config.json ]]; then
18
+ if [[ -f scripts/lib/kit-config.sh && ( -f .claude/kit.config.json || -f cckit.config.json ) ]]; then
19
19
  source scripts/lib/kit-config.sh
20
20
  load_kit_config >/dev/null 2>&1 || true
21
21
  KNOWLEDGE_DIR="${KIT_KNOWLEDGE_DIR:-knowledge}"
@@ -0,0 +1,58 @@
1
+ #!/bin/sh
2
+ # captain-test.sh — self-test for captain.sh pure gate policy under bash AND zsh.
3
+ # Network-free: covers cap_checks_summary / cap_classify / cap_action and the branch parser only
4
+ # (the gh-driven captain_gate/pass/loop are not exercised).
5
+ # Run: bash scripts/lib/captain-test.sh
6
+
7
+ dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
8
+
9
+ if [ -n "${CAP_TEST_INNER:-}" ]; then
10
+ . "$dir/captain.sh"
11
+ fail=0
12
+ eq() { if [ "$2" != "$3" ]; then echo "FAIL($CAP_TEST_INNER): $1 -> '[$2]', want '[$3]'"; fail=1; fi; }
13
+
14
+ # cap_checks_summary — worst-first precedence over a rollup JSON array.
15
+ eq "checks fail wins" "$(printf '[{"conclusion":"SUCCESS"},{"conclusion":"FAILURE"}]' | cap_checks_summary)" "FAIL"
16
+ eq "checks pending" "$(printf '[{"conclusion":"SUCCESS"},{"status":"IN_PROGRESS","conclusion":null}]' | cap_checks_summary)" "PENDING"
17
+ eq "checks pass" "$(printf '[{"conclusion":"SUCCESS"},{"conclusion":"SKIPPED"}]' | cap_checks_summary)" "PASS"
18
+ eq "checks none" "$(printf '[]' | cap_checks_summary)" "NONE"
19
+ eq "checks empty-concl pending" "$(printf '[{"conclusion":""}]' | cap_checks_summary)" "PENDING"
20
+
21
+ # cap_classify — the verdict matrix.
22
+ eq "clean" "$(cap_classify MERGEABLE CLEAN PASS)" "CLEAN"
23
+ eq "clean no checks" "$(cap_classify MERGEABLE CLEAN NONE)" "CLEAN"
24
+ eq "unstable+pass" "$(cap_classify MERGEABLE UNSTABLE PASS)" "CLEAN"
25
+ eq "conflicting flag" "$(cap_classify CONFLICTING BLOCKED PASS)" "CONFLICTING"
26
+ eq "dirty is conflict" "$(cap_classify UNKNOWN DIRTY PASS)" "CONFLICTING"
27
+ eq "checks failing" "$(cap_classify MERGEABLE UNSTABLE FAIL)" "CHECKS_FAILING"
28
+ eq "checks pending" "$(cap_classify MERGEABLE UNSTABLE PENDING)" "CHECKS_PENDING"
29
+ eq "draft first" "$(cap_classify MERGEABLE DRAFT PASS)" "DRAFT"
30
+ eq "blocked fallback" "$(cap_classify UNKNOWN BLOCKED PASS)" "BLOCKED"
31
+ # failing checks must not be hidden even when mergeable says MERGEABLE.
32
+ eq "fail beats mergeable" "$(cap_classify MERGEABLE CLEAN FAIL)" "CHECKS_FAILING"
33
+
34
+ # cap_action — verdict -> action.
35
+ eq "act clean" "$(cap_action CLEAN)" "merge"
36
+ eq "act conflicting" "$(cap_action CONFLICTING)" "rebase"
37
+ eq "act failing" "$(cap_action CHECKS_FAILING)" "fix"
38
+ eq "act pending" "$(cap_action CHECKS_PENDING)" "wait"
39
+ eq "act draft" "$(cap_action DRAFT)" "wait"
40
+ eq "act blocked" "$(cap_action BLOCKED)" "skip"
41
+
42
+ # _cap_issue_of_branch — pull the issue number out of a flow branch.
43
+ eq "branch task" "$(_cap_issue_of_branch 'task/47-admin-clerk')" "47"
44
+ eq "branch fix" "$(_cap_issue_of_branch 'fix/9-roadmap')" "9"
45
+ eq "branch effort" "$(_cap_issue_of_branch 'effort/123-copilot')" "123"
46
+ eq "branch plain" "$(_cap_issue_of_branch 'main')" ""
47
+
48
+ if [ "$fail" -eq 0 ]; then echo "PASS($CAP_TEST_INNER): captain gate policy"; fi
49
+ exit "$fail"
50
+ fi
51
+
52
+ # Outer: re-run under each available shell.
53
+ rc=0
54
+ for sh in bash zsh; do
55
+ command -v "$sh" >/dev/null 2>&1 || continue
56
+ CAP_TEST_INNER="$sh" "$sh" "$0" || rc=1
57
+ done
58
+ exit "$rc"
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/env bash
2
+ # captain.sh — the captain loop: gate open PRs, squash-merge the clean ones, advance the board to the
3
+ # next unblocked wave, and checkpoint so the loop can stop and resume (Effort 75 · #77).
4
+ #
5
+ # Autopilot has two halves. `orchestrate`/`autopilot` LAUNCH the per-issue flows; the captain CLOSES
6
+ # them — it inspects each PR, decides (CLEAN / CONFLICTING / CHECKS_FAILING / CHECKS_PENDING / DRAFT /
7
+ # BLOCKED), merges what is ready, and lets the freshly-merged issues unblock the next wave. This is
8
+ # the half that used to be left to the driving agent; now it is a script the agent (or `cckit watch`)
9
+ # runs in a loop.
10
+ #
11
+ # cckit watch one pass: gate every open PR, report (no merge) — safe default
12
+ # cckit watch --merge gate + squash-merge the CLEAN PRs, then show the next wave
13
+ # cckit watch --effort <N> scope to PRs for effort #N's sub-issues
14
+ # cckit watch --loop repeat passes until no CLEAN PR remains (checkpointed), then stop
15
+ # cckit watch --max-passes M cap the loop at M passes (default 10)
16
+ #
17
+ # Gate decision is three pure helpers (cap_checks_summary / cap_classify / cap_action) so the policy
18
+ # is unit-tested without the network. Requires gh + jq. bash 3.2 / zsh compatible.
19
+
20
+ CAPTAIN_REPO="${CAPTAIN_REPO:-${KIT_REPO:-}}"
21
+ CAPTAIN_STATE="${CAPTAIN_STATE:-.cckit/captain.state}"
22
+
23
+ # cap_checks_summary — collapse a gh statusCheckRollup JSON array (stdin) to one token:
24
+ # FAIL (any failure/error/cancelled/timed_out) > PENDING (any in-flight) > PASS (any success) > NONE.
25
+ # Precedence is worst-first so a single red check is never hidden by green ones.
26
+ cap_checks_summary() {
27
+ command -v jq >/dev/null 2>&1 || { echo NONE; return 0; }
28
+ jq -r '
29
+ [ .[]? | (.conclusion // .state // "") | ascii_upcase ] as $c
30
+ | if ($c | any(. == "FAILURE" or . == "ERROR" or . == "CANCELLED" or . == "TIMED_OUT" or . == "ACTION_REQUIRED" or . == "STARTUP_FAILURE")) then "FAIL"
31
+ elif ($c | any(. == "PENDING" or . == "IN_PROGRESS" or . == "QUEUED" or . == "EXPECTED" or . == "WAITING" or . == "")) then "PENDING"
32
+ elif ($c | any(. == "SUCCESS" or . == "NEUTRAL" or . == "SKIPPED")) then "PASS"
33
+ else "NONE" end' 2>/dev/null || echo NONE
34
+ }
35
+
36
+ # cap_classify <mergeable> <mergeStateStatus> <checksSummary> — pure verdict for one PR.
37
+ # mergeable: MERGEABLE|CONFLICTING|UNKNOWN · mss: CLEAN|DIRTY|DRAFT|BLOCKED|BEHIND|UNSTABLE|…
38
+ cap_classify() {
39
+ local mergeable="$1" mss="$2" checks="$3"
40
+ case "$mss" in DRAFT) echo DRAFT; return 0 ;; esac
41
+ case "$mergeable" in CONFLICTING) echo CONFLICTING; return 0 ;; esac
42
+ case "$mss" in DIRTY) echo CONFLICTING; return 0 ;; esac
43
+ case "$checks" in
44
+ FAIL) echo CHECKS_FAILING; return 0 ;;
45
+ PENDING) echo CHECKS_PENDING; return 0 ;;
46
+ esac
47
+ case "$mergeable" in
48
+ MERGEABLE)
49
+ # passing or no required checks, and not draft/dirty/conflicting -> ready.
50
+ case "$mss" in CLEAN|UNSTABLE|HAS_HOOKS|"") echo CLEAN; return 0 ;; esac
51
+ echo BLOCKED; return 0 ;;
52
+ esac
53
+ echo BLOCKED
54
+ }
55
+
56
+ # cap_action <state> — the action the captain takes for a verdict.
57
+ cap_action() {
58
+ case "$1" in
59
+ CLEAN) echo merge ;;
60
+ CONFLICTING) echo rebase ;;
61
+ CHECKS_FAILING) echo fix ;;
62
+ CHECKS_PENDING) echo wait ;;
63
+ DRAFT) echo wait ;;
64
+ *) echo skip ;;
65
+ esac
66
+ }
67
+
68
+ # _cap_issue_of_branch <branch> — issue number a flow branch encodes (task/47-x, fix/9-y, effort/12-z).
69
+ _cap_issue_of_branch() {
70
+ printf '%s' "$1" | sed -nE 's#^[a-z]+/([0-9]+)-.*#\1#p'
71
+ }
72
+
73
+ # captain_gate <pr#> — fetch + classify one PR. Echoes "pr<TAB>issue<TAB>state<TAB>action<TAB>title".
74
+ captain_gate() {
75
+ local repo="$CAPTAIN_REPO" pr="$1" j mergeable mss checks issue state action title branch
76
+ j="$(gh pr view "$pr" --repo "$repo" --json number,title,headRefName,mergeable,mergeStateStatus,statusCheckRollup 2>/dev/null)" \
77
+ || { echo "captain: cannot read PR #$pr" >&2; return 1; }
78
+ mergeable="$(printf '%s' "$j" | jq -r '.mergeable // "UNKNOWN"')"
79
+ mss="$(printf '%s' "$j" | jq -r '.mergeStateStatus // "UNKNOWN"')"
80
+ checks="$(printf '%s' "$j" | jq -c '.statusCheckRollup // []' | cap_checks_summary)"
81
+ branch="$(printf '%s' "$j" | jq -r '.headRefName // ""')"
82
+ title="$(printf '%s' "$j" | jq -r '.title // ""')"
83
+ issue="$(_cap_issue_of_branch "$branch")"
84
+ state="$(cap_classify "$mergeable" "$mss" "$checks")"
85
+ action="$(cap_action "$state")"
86
+ printf '%s\t%s\t%s\t%s\t%s\n' "$pr" "${issue:-—}" "$state" "$action" "$title"
87
+ }
88
+
89
+ # _cap_open_prs [effort] — open PR numbers, optionally only those whose branch-issue is a sub of <effort>.
90
+ _cap_open_prs() {
91
+ local repo="$CAPTAIN_REPO" effort="${1:-}"
92
+ if [ -n "$effort" ]; then
93
+ local subs; subs="$(gh api "repos/$repo/issues/$effort/sub_issues" --paginate --jq '.[].number' 2>/dev/null | tr '\n' ' ')"
94
+ local pr br iss
95
+ gh pr list --repo "$repo" --state open --json number,headRefName \
96
+ --jq '.[] | "\(.number)\t\(.headRefName)"' 2>/dev/null | while IFS="$(printf '\t')" read -r pr br; do
97
+ iss="$(_cap_issue_of_branch "$br")"
98
+ case " $subs " in *" $iss "*) printf '%s\n' "$pr" ;; esac
99
+ done
100
+ else
101
+ gh pr list --repo "$repo" --state open --json number --jq '.[].number' 2>/dev/null
102
+ fi
103
+ }
104
+
105
+ # captain_pass [--merge] [--effort N] — gate every open PR (in scope); merge the CLEAN ones if asked.
106
+ # Returns the count of PRs merged this pass via the CAPTAIN_MERGED global (for the loop to detect progress).
107
+ CAPTAIN_MERGED=0
108
+ captain_pass() {
109
+ command -v gh >/dev/null 2>&1 && command -v jq >/dev/null 2>&1 || { echo "captain: needs gh + jq" >&2; return 1; }
110
+ local repo="$CAPTAIN_REPO" do_merge=0 effort="" dry=0
111
+ while [ "$#" -gt 0 ]; do
112
+ case "$1" in
113
+ --merge) do_merge=1; shift ;;
114
+ --dry-run) dry=1; shift ;;
115
+ --effort) effort="$2"; shift 2 ;;
116
+ --effort=*) effort="${1#*=}"; shift ;;
117
+ *) echo "captain_pass: unknown arg '$1'" >&2; return 2 ;;
118
+ esac
119
+ done
120
+ [ -n "$repo" ] || { echo "captain: no repo (set KIT_REPO / CAPTAIN_REPO)" >&2; return 1; }
121
+ CAPTAIN_MERGED=0
122
+
123
+ local prs pr row state action issue title merged_any=0
124
+ prs="$(_cap_open_prs "$effort")"
125
+ [ -n "$prs" ] || { echo "captain: no open PRs in scope"; return 0; }
126
+
127
+ : > "$CAPTAIN_STATE" 2>/dev/null || true
128
+ echo "captain: gating $(printf '%s\n' "$prs" | grep -c .) open PR(s)$( [ -n "$effort" ] && echo " for effort #$effort")"
129
+ for pr in $prs; do
130
+ row="$(captain_gate "$pr")" || continue
131
+ state="$(printf '%s' "$row" | cut -f3)"
132
+ action="$(printf '%s' "$row" | cut -f4)"
133
+ issue="$(printf '%s' "$row" | cut -f2)"
134
+ title="$(printf '%s' "$row" | cut -f5)"
135
+ printf '%s\t%s\n' "$pr" "$state" >> "$CAPTAIN_STATE" 2>/dev/null || true
136
+ if [ "$action" = "merge" ] && [ "$do_merge" = "1" ] && [ "$dry" = "0" ]; then
137
+ if gh pr merge "$pr" --repo "$repo" --squash --delete-branch >/dev/null 2>&1; then
138
+ printf ' PR #%-4s %-14s MERGED (#%s %s)\n' "$pr" "$state" "$issue" "$title"
139
+ merged_any=$((merged_any + 1))
140
+ else
141
+ printf ' PR #%-4s %-14s merge FAILED (re-check) (#%s %s)\n' "$pr" "$state" "$issue" "$title"
142
+ fi
143
+ else
144
+ printf ' PR #%-4s %-14s -> %-7s (#%s %s)\n' "$pr" "$state" "$action" "$issue" "$title"
145
+ fi
146
+ done
147
+ CAPTAIN_MERGED="$merged_any"
148
+
149
+ # advance: after merges, the newly-unblocked work shows up as the next wave.
150
+ if [ "$merged_any" -gt 0 ]; then
151
+ echo "captain: merged $merged_any — next unblocked wave:"
152
+ local here; here="$(dirname "${BASH_SOURCE[0]}")"
153
+ if [ -f "$here/plan-machine.sh" ]; then
154
+ # shellcheck source=/dev/null
155
+ . "$here/plan-machine.sh"
156
+ if [ -n "$effort" ]; then CCKIT_OUTPUT=human plan_machine --effort "$effort" 2>/dev/null || true
157
+ else CCKIT_OUTPUT=human plan_machine 2>/dev/null || true; fi
158
+ fi
159
+ fi
160
+ }
161
+
162
+ # captain_loop — repeat passes until a pass merges nothing (steady state) or --max-passes is hit.
163
+ # Checkpointed: each pass overwrites CAPTAIN_STATE so a fresh invocation resumes from the live board.
164
+ captain_loop() {
165
+ local max=10 pass=0
166
+ local -a clean=()
167
+ while [ "$#" -gt 0 ]; do
168
+ case "$1" in
169
+ --max-passes) max="$2"; shift 2 ;;
170
+ --max-passes=*) max="${1#*=}"; shift ;;
171
+ *) clean+=("$1"); shift ;;
172
+ esac
173
+ done
174
+ case "$max" in ''|*[!0-9]*) max=10 ;; esac
175
+ while [ "$pass" -lt "$max" ]; do
176
+ pass=$((pass + 1))
177
+ echo "── captain pass $pass/$max ──"
178
+ captain_pass "${clean[@]+"${clean[@]}"}" || return 1
179
+ [ "$CAPTAIN_MERGED" -gt 0 ] || { echo "captain: steady state (no CLEAN PR merged) — stopping at pass $pass"; break; }
180
+ done
181
+ }
@@ -0,0 +1,30 @@
1
+ #!/bin/sh
2
+ # copilot-test.sh — self-test for copilot.sh pure helper under bash AND zsh.
3
+ # Network-free: covers the subagent seed composer only (the gh/plan-driven copilot_brief is not
4
+ # exercised). Run: bash scripts/lib/copilot-test.sh
5
+
6
+ dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
7
+
8
+ if [ -n "${CP_TEST_INNER:-}" ]; then
9
+ . "$dir/copilot.sh"
10
+ fail=0
11
+ has() { case "$2" in *"$3"*) ;; *) echo "FAIL($CP_TEST_INNER): $1 missing '[$3]'"; fail=1 ;; esac; }
12
+
13
+ seed="$(_copilot_seed 42 'wire the thing')"
14
+ has "seed has issue view" "$seed" 'gh issue view 42'
15
+ has "seed has title" "$seed" 'implement "wire the thing"'
16
+ has "seed has pr verb" "$seed" 'cckit pr 42'
17
+ has "seed has close verb" "$seed" 'cckit close 42'
18
+ has "seed has check gate" "$seed" 'bash scripts/check.sh'
19
+ has "seed forbids asking" "$seed" 'never ask'
20
+
21
+ if [ "$fail" -eq 0 ]; then echo "PASS($CP_TEST_INNER): copilot seed composer"; fi
22
+ exit "$fail"
23
+ fi
24
+
25
+ rc=0
26
+ for sh in bash zsh; do
27
+ command -v "$sh" >/dev/null 2>&1 || continue
28
+ CP_TEST_INNER="$sh" "$sh" "$0" || rc=1
29
+ done
30
+ exit "$rc"