@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.
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env bash
2
+ # copilot.sh — the copilot driver: turn the wave plan into a fan-out BRIEF that Claude Code enacts
3
+ # (Effort 75 · #78). This is cckit's prompt machine.
4
+ #
5
+ # The orchestration brain is the driving agent, not a background launcher. So the copilot does not
6
+ # spawn anything itself — it EMITS the plan-as-instructions: for the current wave, a ready-to-run
7
+ # Task-subagent prompt per issue (worktree-isolated, background), and the between-waves drive (gate +
8
+ # merge with the captain, then re-run for the next wave). The agent reads the brief and acts.
9
+ #
10
+ # cckit copilot brief for the open board: wave 0 fan-out + the loop
11
+ # cckit copilot --effort <N> brief scoped to effort #N's sub-issues
12
+ # cckit copilot --cap <K> session ctx budget per wave (passed to the plan machine)
13
+ # cckit copilot --llm the fan-out as TOON rows (wave,number,ctx,prompt) for an agent
14
+ #
15
+ # Requires: gh, jq (via the plan machine). bash 3.2 / zsh compatible.
16
+
17
+ COPILOT_REPO="${COPILOT_REPO:-${KIT_REPO:-}}"
18
+
19
+ # _copilot_seed <num> <title> — the headless subagent prompt for one issue (worktree already isolated).
20
+ _copilot_seed() {
21
+ local num="$1" title="$2"
22
+ printf 'You are a Task subagent in a cckit copilot run — NO human is present, so never ask: decide autonomously and apply effort proportional to the task. Your worktree and branch for issue #%s are already isolated. Do: `gh issue view %s`, implement "%s", run `bash scripts/check.sh` until green, then open the PR with `cckit pr %s "<summary>"`. If it is a no-op, run `cckit close %s "<reason>"` with why. Return a one-line result.' \
23
+ "$num" "$num" "$title" "$num" "$num"
24
+ }
25
+
26
+ copilot_brief() {
27
+ command -v gh >/dev/null 2>&1 && command -v jq >/dev/null 2>&1 || { echo "copilot: needs gh + jq" >&2; return 1; }
28
+ local repo="$COPILOT_REPO" out="${CCKIT_OUTPUT:-human}" effort="" cap="" planargs=()
29
+ while [ "$#" -gt 0 ]; do
30
+ case "$1" in
31
+ --llm|--output=json) out="json"; shift ;;
32
+ --effort) effort="$2"; planargs+=(--effort "$2"); shift 2 ;;
33
+ --effort=*) effort="${1#*=}"; planargs+=(--effort "${1#*=}"); shift ;;
34
+ --cap) cap="$2"; planargs+=(--cap "$2"); shift 2 ;;
35
+ --cap=*) cap="${1#*=}"; planargs+=(--cap "${1#*=}"); shift ;;
36
+ -h|--help) sed -n '13,17p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; return 0 ;;
37
+ *) echo "copilot: unknown arg '$1'" >&2; return 2 ;;
38
+ esac
39
+ done
40
+ [ -n "$repo" ] || { echo "copilot: no repo (set KIT_REPO / COPILOT_REPO)" >&2; return 1; }
41
+
42
+ # pull the wave plan as raw rows from the plan machine (#76).
43
+ local here; here="$(dirname "${BASH_SOURCE[0]}")"
44
+ # shellcheck source=/dev/null
45
+ . "$here/plan-machine.sh"
46
+ local rows
47
+ rows="$(CCKIT_OUTPUT=tsv plan_machine "${planargs[@]+"${planargs[@]}"}" 2>/dev/null)"
48
+ if [ -z "$rows" ]; then
49
+ [ "$out" = "json" ] && echo "[]" || echo "copilot: nothing to drive (no open issues in scope)"
50
+ return 0
51
+ fi
52
+
53
+ # wave 0 = the issues to fan out right now (everything currently unblocked).
54
+ local wave0; wave0="$(printf '%s\n' "$rows" | awk -F'\t' '$1==0')"
55
+ local waves_total; waves_total="$(printf '%s\n' "$rows" | awk -F'\t' '{print $1}' | sort -nu | tail -1)"
56
+
57
+ if [ "$out" = "json" ]; then
58
+ # TOON rows {wave,number,ctx,prompt} — the fan-out as token-cheap agent input.
59
+ local json
60
+ json="$(printf '%s\n' "$wave0" | awk -F'\t' '{print $3"\t"$4"\t"$6}' \
61
+ | while IFS="$(printf '\t')" read -r num ctx title; do
62
+ [ -n "$num" ] || continue
63
+ disp="$(printf '%s' "$title" | sed -E 's/^\[Effort( [0-9]+)?\] [0-9]+ · ?//; s/^\[[A-Za-z]+\] //')"
64
+ printf '%s\t%s\t%s\n' "$num" "$ctx" "$(_copilot_seed "$num" "$disp")"
65
+ done \
66
+ | jq -R -s '[ split("\n")[] | select(length>0) | split("\t")
67
+ | {wave:0, number:(.[0]|tonumber), ctx:.[1], prompt:.[2]} ]')"
68
+ # shellcheck source=/dev/null
69
+ . "$here/toon.sh"
70
+ printf '%s' "$json" | toon_encode
71
+ return 0
72
+ fi
73
+
74
+ # human brief — markdown the agent enacts, routed through the rendering seam (#82): rich via glow
75
+ # in a TTY, verbatim markdown otherwise (renders natively in Claude Code, pipe-safe).
76
+ # shellcheck source=/dev/null
77
+ . "$here/render.sh"
78
+ local n0 esfx; n0="$(printf '%s\n' "$wave0" | grep -c .)"
79
+ esfx="$([ -n "$effort" ] && echo " --effort $effort")"
80
+ {
81
+ printf '# cckit copilot — %s%s\n' "$repo" "$([ -n "$effort" ] && echo " · effort #$effort")"
82
+ printf '\nThe plan machine laid out %s wave(s). You are the orchestrator: fan wave 0 out as parallel\nTask subagents, gate + merge with the captain, then re-run this brief for the next wave.\n' "$((waves_total + 1))"
83
+
84
+ printf '\n## Wave 0 — spawn %s parallel Task subagent(s)\n\n' "$n0"
85
+ printf 'Each in its OWN worktree (Task isolation: "worktree", run in background). Branches are\nfile-disjoint within a wave, so they will not collide.\n\n'
86
+ printf '%s\n' "$wave0" | awk -F'\t' '{print $3"\t"$4"\t"$6}' \
87
+ | while IFS="$(printf '\t')" read -r num ctx title; do
88
+ [ -n "$num" ] || continue
89
+ disp="$(printf '%s' "$title" | sed -E 's/^\[Effort( [0-9]+)?\] [0-9]+ · ?//; s/^\[[A-Za-z]+\] //')"
90
+ printf '### #%s · [%s] · %s\n\n' "$num" "$ctx" "$disp"
91
+ printf '> %s\n\n' "$(_copilot_seed "$num" "$disp")"
92
+ done
93
+
94
+ printf '## Between waves — drive the captain\n\n'
95
+ printf '1. When the wave-0 subagents have opened their PRs, gate + merge them:\n'
96
+ printf ' ```\n cckit watch --merge%s\n ```\n' "$esfx"
97
+ printf '2. Merging unblocks the next wave. Re-run this brief to get it:\n'
98
+ printf ' ```\n cckit copilot%s\n ```\n' "$esfx"
99
+ printf '3. Repeat until `cckit copilot` reports nothing to drive. To let the captain self-pace the\n gate/merge passes, use `cckit watch --loop` (or drive this brief under `/loop`).\n\n'
100
+ } | cckit_render
101
+ }
@@ -41,7 +41,7 @@ estimate_effort() {
41
41
  est="$d/estimator.json"; [[ -f "$est" ]] || return 0
42
42
  command -v jq >/dev/null 2>&1 || return 0
43
43
 
44
- repo="${EFFORT_REPO:-jeiemgi/cckit}"
44
+ repo="${EFFORT_REPO:-${KIT_REPO:-}}"
45
45
  subs="$(gh api graphql -f query='query($o:String!,$r:String!,$n:Int!){repository(owner:$o,name:$r){issue(number:$n){subIssues(first:50){totalCount}}}}' \
46
46
  -F o="${repo%/*}" -F r="${repo#*/}" -F n="$num" --jq '.data.repository.issue.subIssues.totalCount' 2>/dev/null)"
47
47
  [[ "$subs" =~ ^[0-9]+$ ]] || subs=1; [[ "$subs" -lt 1 ]] && subs=1
@@ -126,7 +126,7 @@ _em_token_sum() {
126
126
  }
127
127
 
128
128
  capture_effort_metrics() {
129
- local num="$1" base="${2:-origin/develop}" d start now build_s shortstat files added removed commits churn diff_auto branch
129
+ local num="$1" base="${2:-origin/${KIT_BASE_BRANCH:-main}}" d start now build_s shortstat files added removed commits churn diff_auto branch
130
130
  local toks tin tout tcr tcw tokens_real cost_real cost_source tokens_est cost_est rec
131
131
  local IFS=' ' # force space-split for `set --` below (ambient IFS may carry a NUL)
132
132
  [[ -n "$num" ]] || return 0
@@ -213,7 +213,7 @@ _EM_JUDGE_MAX_CHARS="${EM_JUDGE_MAX_CHARS:-12000}"
213
213
  # (<git-common-dir>/traces/effort-<N>/*.diff, written pre-squash by effort_snapshot_subs) and falls
214
214
  # back to `git diff <base>...HEAD`. Echoes the (untruncated) diff; truncation happens in the caller.
215
215
  _em_judge_diff() {
216
- local num="$1" base="${2:-origin/develop}" common tdir
216
+ local num="$1" base="${2:-origin/${KIT_BASE_BRANCH:-main}}" common tdir
217
217
  common="$(git rev-parse --git-common-dir 2>/dev/null)" || true
218
218
  if [[ -n "$common" ]]; then
219
219
  case "$common" in /*) : ;; *) common="$PWD/$common" ;; esac
@@ -231,7 +231,7 @@ _em_judge_diff() {
231
231
  _em_judge_goal() {
232
232
  local num="$1" repo body
233
233
  command -v gh >/dev/null 2>&1 || { printf ''; return 0; }
234
- repo="${EFFORT_REPO:-jeiemgi/cckit}"
234
+ repo="${EFFORT_REPO:-${KIT_REPO:-}}"
235
235
  body="$(gh issue view "$num" --repo "$repo" --json body --jq '.body' 2>/dev/null)" || { printf ''; return 0; }
236
236
  [[ -n "$body" ]] || { printf ''; return 0; }
237
237
  # Extract the lines after "## Goal" up to the next "## " heading.
@@ -282,7 +282,7 @@ _em_judge_call() {
282
282
  }
283
283
 
284
284
  judge_effort_metrics() {
285
- local num="$1" base="${2:-origin/develop}" d f goal diff truncated_note system user out model reply json dj sj
285
+ local num="$1" base="${2:-origin/${KIT_BASE_BRANCH:-main}}" d f goal diff truncated_note system user out model reply json dj sj
286
286
  [[ -n "$num" ]] || return 0
287
287
  command -v jq >/dev/null 2>&1 || return 0
288
288
  d="$(_em_dir)" || return 0; f="$d/records.jsonl"
@@ -7,7 +7,12 @@
7
7
  # project config, nearest-wins (like .editorconfig). Projects with no .claudekit/ behave unchanged.
8
8
 
9
9
  load_kit_config() {
10
- local cfg="${KIT_CONFIG:-.claude/kit.config.json}"
10
+ # Resolve the project config like the dispatcher (bin/cckit _cckit_find_config): an explicit
11
+ # KIT_CONFIG wins, else a root cckit.config.json, else .claude/kit.config.json.
12
+ local cfg="${KIT_CONFIG:-}"
13
+ if [[ -z "$cfg" ]]; then
14
+ if [[ -f cckit.config.json ]]; then cfg="cckit.config.json"; else cfg=".claude/kit.config.json"; fi
15
+ fi
11
16
  if [[ ! -f "$cfg" ]]; then
12
17
  echo "✗ $cfg not found. Run /kit-init (or scripts/init.sh) first." >&2
13
18
  return 1
@@ -48,7 +48,7 @@ kit_gc_analyze() {
48
48
 
49
49
  echo "# branches"
50
50
  for b in $(git branch --format='%(refname:short)' 2>/dev/null); do
51
- case "$b" in develop|main) echo " $b -> ACTIVE (base branch)"; continue;; esac
51
+ case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) echo " $b -> ACTIVE (base branch)"; continue;; esac
52
52
  pr="$(gh pr list --repo "$repo" --head "$b" --state all --json number,state --jq '.[0]|"PR#\(.number) \(.state)"' 2>/dev/null || true)"
53
53
  prot="$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)"
54
54
  if [ -n "$prot" ]; then
@@ -86,7 +86,7 @@ kit_gc_prune() {
86
86
  | awk '/^worktree /{w=$2} /^branch /{print w" "$2}' \
87
87
  | while read -r path ref; do
88
88
  b="${ref#refs/heads/}"
89
- case "$b" in develop|main|"") continue ;; esac
89
+ case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main|"") continue ;; esac
90
90
  [ -n "$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)" ] && continue
91
91
  pr="$(gh pr list --repo "$repo" --head "$b" --state all --json state --jq '.[0].state' 2>/dev/null || true)"
92
92
  [ "$pr" = "MERGED" ] || continue
@@ -103,7 +103,7 @@ kit_gc_prune() {
103
103
 
104
104
  # Then local branches whose PR merged (worktree now gone).
105
105
  for b in $(git branch --format='%(refname:short)' 2>/dev/null); do
106
- case "$b" in develop|main) continue ;; esac
106
+ case "$b" in "${KIT_BASE_BRANCH:-main}"|develop|main) continue ;; esac
107
107
  [ -n "$(wt_protected_reason "$b" "$repo" 2>/dev/null || true)" ] && continue
108
108
  pr="$(gh pr list --repo "$repo" --head "$b" --state all --json state --jq '.[0].state' 2>/dev/null || true)"
109
109
  [ "$pr" = "MERGED" ] || continue
@@ -26,9 +26,9 @@ if [ -n "${KTO_TEST_INNER:-}" ]; then
26
26
  eq "branch effort/851-z" "$(kto_branch_issue_number 'effort/851-tail')" "851"
27
27
 
28
28
  # kto_base_branch / kto_board_enabled (portability toggles)
29
- eq "base default" "$(kto_base_branch)" "develop"
29
+ eq "base default" "$(unset KIT_BASE_BRANCH; kto_base_branch)" "main"
30
30
  eq "base override" "$(KIT_BASE_BRANCH=main; kto_base_branch)" "main"
31
- if kto_board_enabled; then eq "board default on" "on" "on"; else eq "board default on" "off" "on"; fi
31
+ if ( unset KIT_PROJECTS_V2; kto_board_enabled ); then eq "board default on" "on" "on"; else eq "board default on" "off" "on"; fi
32
32
  ( KIT_PROJECTS_V2=false; if kto_board_enabled; then exit 0; else exit 1; fi ) \
33
33
  && eq "board off" "on" "off" || eq "board off" "off" "off"
34
34
 
@@ -37,12 +37,12 @@ if [[ -z "${KIT_REPO:-}" ]]; then
37
37
  fi
38
38
 
39
39
  kto_repo() {
40
- printf '%s' "${KIT_TASK_REPO:-${KIT_REPO:-jeiemgi/cckit}}"
40
+ printf '%s' "${KIT_TASK_REPO:-${KIT_REPO:-}}"
41
41
  }
42
42
 
43
43
  # The base branch a PR targets / pr-merge returns to (portable: KIT_BASE_BRANCH, default develop).
44
44
  kto_base_branch() {
45
- printf '%s' "${KIT_BASE_BRANCH:-develop}"
45
+ printf '%s' "${KIT_BASE_BRANCH:-main}"
46
46
  }
47
47
 
48
48
  # True (rc 0) iff board updates should run. Off when KIT_PROJECTS_V2 is explicitly "false" — keeps
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env bash
2
+ # next.sh — "what can I pick up right now?" The everyday-dev helper (Effort 75 · #79).
3
+ #
4
+ # Surfaces the unblocked set (wave 0 of the plan machine) and the single recommended next issue with
5
+ # the one command to start it. This is the daily-loop counterpart to the copilot: the copilot fans a
6
+ # whole wave out to subagents; `cckit next` is for a human (or agent) picking up one issue by hand.
7
+ #
8
+ # cckit next the unblocked issues + the top one's start command
9
+ # cckit next --effort <N> scope to effort #N's sub-issues
10
+ # cckit next --llm the unblocked set as TOON rows {number,ctx,title}
11
+ #
12
+ # Requires gh + jq (via the plan machine). bash 3.2 / zsh compatible.
13
+
14
+ NEXT_REPO="${NEXT_REPO:-${KIT_REPO:-}}"
15
+
16
+ next_issue() {
17
+ command -v gh >/dev/null 2>&1 && command -v jq >/dev/null 2>&1 || { echo "next: needs gh + jq" >&2; return 1; }
18
+ local repo="$NEXT_REPO" out="${CCKIT_OUTPUT:-human}" effort="" planargs=()
19
+ while [ "$#" -gt 0 ]; do
20
+ case "$1" in
21
+ --llm|--output=json) out="json"; shift ;;
22
+ --effort) effort="$2"; planargs+=(--effort "$2"); shift 2 ;;
23
+ --effort=*) effort="${1#*=}"; planargs+=(--effort "${1#*=}"); shift ;;
24
+ -h|--help) sed -n '10,13p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; return 0 ;;
25
+ *) echo "next: unknown arg '$1'" >&2; return 2 ;;
26
+ esac
27
+ done
28
+ [ -n "$repo" ] || { echo "next: no repo (set KIT_REPO / NEXT_REPO)" >&2; return 1; }
29
+
30
+ local here; here="$(dirname "${BASH_SOURCE[0]}")"
31
+ # shellcheck source=/dev/null
32
+ . "$here/plan-machine.sh"
33
+ local rows wave0
34
+ rows="$(CCKIT_OUTPUT=tsv plan_machine "${planargs[@]+"${planargs[@]}"}" 2>/dev/null)"
35
+ wave0="$(printf '%s\n' "$rows" | awk -F'\t' '$1==0')"
36
+ if [ -z "$wave0" ]; then
37
+ [ "$out" = "json" ] && echo "[]" || echo "next: nothing unblocked — the board is blocked or empty."
38
+ return 0
39
+ fi
40
+
41
+ if [ "$out" = "json" ]; then
42
+ local json
43
+ json="$(printf '%s\n' "$wave0" | awk -F'\t' '{
44
+ t=$6; sub(/^\[Effort( [0-9]+)?\] [0-9]+ · ?/,"",t); sub(/^\[[A-Za-z]+\] /,"",t)
45
+ print $3"\t"$4"\t"t
46
+ }' \
47
+ | jq -R -s '[ split("\n")[] | select(length>0) | split("\t")
48
+ | {number:(.[0]|tonumber), ctx:.[1], title:.[2]} ]')"
49
+ # shellcheck source=/dev/null
50
+ . "$here/toon.sh"
51
+ printf '%s' "$json" | toon_encode
52
+ return 0
53
+ fi
54
+
55
+ # human: the unblocked table + the top issue's start command, through the rendering seam.
56
+ # shellcheck source=/dev/null
57
+ . "$here/render.sh"
58
+ local top topnum toptitle
59
+ top="$(printf '%s\n' "$wave0" | head -1)"
60
+ topnum="$(printf '%s' "$top" | cut -f3)"
61
+ toptitle="$(printf '%s' "$top" | cut -f6 | sed -E 's/^\[Effort( [0-9]+)?\] [0-9]+ · ?//; s/^\[[A-Za-z]+\] //')"
62
+ {
63
+ printf '# Next up — %s%s\n\n' "$repo" "$([ -n "$effort" ] && echo " · effort #$effort")"
64
+ printf '## Unblocked now\n\n'
65
+ printf '| # | ctx | issue |\n|---|-----|-------|\n'
66
+ printf '%s\n' "$wave0" | awk -F'\t' '{
67
+ t=$6; sub(/^\[Effort( [0-9]+)?\] [0-9]+ · ?/,"",t); sub(/^\[[A-Za-z]+\] /,"",t)
68
+ printf "| #%s | %s | %s |\n", $3, $4, t
69
+ }'
70
+ printf '\n## Pick up #%s — %s\n\n' "$topnum" "$toptitle"
71
+ printf 'Start it in an isolated worktree + branch:\n\n'
72
+ printf '```\ncckit start %s\n```\n\n' "$topnum"
73
+ printf 'Or fan the whole unblocked wave out to subagents with `cckit copilot%s`.\n' "$([ -n "$effort" ] && echo " --effort $effort")"
74
+ } | cckit_render
75
+ }
@@ -0,0 +1,72 @@
1
+ #!/bin/sh
2
+ # plan-machine-test.sh — self-test for plan-machine.sh pure helpers under bash AND zsh.
3
+ # Network-free: covers the wave layering, session/file packer, and file-hint parser only
4
+ # (the gh-calling plan_machine orchestration is not exercised).
5
+ # Run: bash scripts/lib/plan-machine-test.sh
6
+
7
+ dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
8
+ TAB=$(printf '\t')
9
+
10
+ if [ -n "${PM_TEST_INNER:-}" ]; then
11
+ . "$dir/plan-machine.sh"
12
+ fail=0
13
+ eq() { # <label> <got> <want>
14
+ if [ "$2" != "$3" ]; then echo "FAIL($PM_TEST_INNER): $1 -> '[$2]', want '[$3]'"; fail=1; fi
15
+ }
16
+
17
+ # _pm_weight
18
+ eq "weight S" "$(_pm_weight S)" "1"
19
+ eq "weight XL" "$(_pm_weight XL)" "8"
20
+ eq "weight ?" "$(_pm_weight '')" "2"
21
+
22
+ # pm_waves: linear chain 3<-2<-1 (1 blocks 2 blocks 3) -> waves 0,1,2.
23
+ chain="$(printf '1\t\n2\t1\n3\t2\n' | pm_waves)"
24
+ eq "chain wave of 1" "$(printf '%s\n' "$chain" | awk -F"$TAB" '$1==1{print $2}')" "0"
25
+ eq "chain wave of 2" "$(printf '%s\n' "$chain" | awk -F"$TAB" '$1==2{print $2}')" "1"
26
+ eq "chain wave of 3" "$(printf '%s\n' "$chain" | awk -F"$TAB" '$1==3{print $2}')" "2"
27
+
28
+ # pm_waves: diamond 1 -> {2,3} -> 4. 2 and 3 share wave 1; 4 in wave 2.
29
+ dia="$(printf '1\t\n2\t1\n3\t1\n4\t2,3\n' | pm_waves)"
30
+ eq "diamond 2==3 wave" \
31
+ "$(printf '%s\n' "$dia" | awk -F"$TAB" '$1==2{print $2}')$(printf '%s\n' "$dia" | awk -F"$TAB" '$1==3{print $2}')" "11"
32
+ eq "diamond wave of 4" "$(printf '%s\n' "$dia" | awk -F"$TAB" '$1==4{print $2}')" "2"
33
+
34
+ # pm_waves: out-of-set blocker (99 absent) is ignored -> node 5 still wave 0.
35
+ oos="$(printf '5\t99\n' | pm_waves)"
36
+ eq "out-of-set blocker ignored" "$(printf '%s\n' "$oos" | awk -F"$TAB" '$1==5{print $2}')" "0"
37
+
38
+ # pm_waves: cycle (1<-2, 2<-1) must terminate, both land in the final wave (no hang, no drop).
39
+ cyc="$(printf '1\t2\n2\t1\n' | pm_waves | wc -l | tr -d ' ')"
40
+ eq "cycle emits both rows" "$cyc" "2"
41
+
42
+ # pm_batches: budget 4, three M(=2) issues with no file hints -> 2,2 then overflow -> batches 0,0,1.
43
+ pk="$(printf '1\tM\t\n2\tM\t\n3\tM\t\n' | pm_batches 4)"
44
+ eq "pack 1 batch0" "$(printf '%s\n' "$pk" | awk -F"$TAB" '$1==1{print $2}')" "0"
45
+ eq "pack 2 batch0" "$(printf '%s\n' "$pk" | awk -F"$TAB" '$1==2{print $2}')" "0"
46
+ eq "pack 3 batch1" "$(printf '%s\n' "$pk" | awk -F"$TAB" '$1==3{print $2}')" "1"
47
+
48
+ # pm_batches: file overlap forces a new batch even under budget.
49
+ fo="$(printf '1\tS\ta.sh\n2\tS\ta.sh\n' | pm_batches 8)"
50
+ eq "overlap splits batch" "$(printf '%s\n' "$fo" | awk -F"$TAB" '$1==2{print $2}')" "1"
51
+ # disjoint files stay in one batch under budget.
52
+ dj="$(printf '1\tS\ta.sh\n2\tS\tb.sh\n' | pm_batches 8)"
53
+ eq "disjoint same batch" "$(printf '%s\n' "$dj" | awk -F"$TAB" '$1==2{print $2}')" "0"
54
+
55
+ # _pm_files: pulls a Files:/Touches: line, normalizes separators.
56
+ eq "files hint comma" "$(_pm_files 'intro
57
+ Files: a.sh, b.sh
58
+ tail')" "a.sh b.sh"
59
+ eq "touches hint" "$(_pm_files 'Touches: x/y.ts')" "x/y.ts"
60
+ eq "no hint" "$(_pm_files 'just prose, no files')" ""
61
+
62
+ if [ "$fail" -eq 0 ]; then echo "PASS($PM_TEST_INNER): plan-machine helpers"; fi
63
+ exit "$fail"
64
+ fi
65
+
66
+ # Outer: re-run under each available shell.
67
+ rc=0
68
+ for sh in bash zsh; do
69
+ command -v "$sh" >/dev/null 2>&1 || continue
70
+ PM_TEST_INNER="$sh" "$sh" "$0" || rc=1
71
+ done
72
+ exit "$rc"
@@ -0,0 +1,260 @@
1
+ #!/usr/bin/env bash
2
+ # plan-machine.sh — the wave plan: turn the board into a deps-ordered, file-disjoint, session-fit
3
+ # execution plan the copilot fans out over (Effort 75 · #76).
4
+ #
5
+ # `cckit plan` reads the open board (or one effort's sub-issues), resolves the native GitHub
6
+ # `blocked_by` edges, and layers the issues into WAVES: wave 0 is everything unblocked, wave N is
7
+ # everything whose blockers all landed in earlier waves. Every issue in a wave is independent of the
8
+ # others in that wave, so a wave is exactly "what N agents can run in parallel right now". Within a
9
+ # wave we pack issues into BATCHES under a session ctx budget, starting a new batch when the budget
10
+ # would overflow or two issues declare overlapping files (so parallel subagents don't collide).
11
+ #
12
+ # cckit plan human wave plan for the open board
13
+ # cckit plan --llm TOON wave plan (uniform rows: wave,batch,number,ctx,blockers,title)
14
+ # cckit plan --effort <N> plan only effort #N's sub-issues
15
+ # cckit plan --milestone <M> filter to milestone M
16
+ # cckit plan --cap <N> session ctx budget per batch (default KIT_SESSION_BUDGET or 4)
17
+ #
18
+ # ctx weights: S=1 · M=2 · L=4 · XL=8. File hints: a "Files:"/"Touches:" line in the issue body
19
+ # (comma/space-separated paths) drives intra-wave disjointness; absent → ctx budget alone batches.
20
+ #
21
+ # Requires: gh, jq. bash 3.2 (no associative arrays — parallel indexed arrays + linear scan; N small).
22
+
23
+ PLAN_REPO="${PLAN_REPO:-${KIT_REPO:-}}"
24
+
25
+ _pm_weight() { case "$1" in S) echo 1 ;; M) echo 2 ;; L) echo 4 ;; XL) echo 8 ;; *) echo 2 ;; esac; }
26
+
27
+ # pm_waves — pure layered topo-sort. stdin: lines "num<TAB>csvBlockers"; stdout: "num<TAB>wave".
28
+ # Only blockers present in the input set constrain layering (out-of-set/closed blockers are the
29
+ # caller's concern). Cycle-safe: any leftover after no-progress lands in a final wave.
30
+ pm_waves() {
31
+ # dual-shell arrays: ksh_arrays gives zsh 0-based indexing + bash-style subscripting.
32
+ [ -n "${ZSH_VERSION:-}" ] && setopt local_options ksh_arrays sh_word_split 2>/dev/null
33
+ local -a N=() B=() W=()
34
+ local num blk
35
+ while IFS="$(printf '\t')" read -r num blk; do
36
+ [ -n "$num" ] || continue
37
+ N[${#N[@]}]="$num"; B[${#B[@]}]="$blk"; W[${#W[@]}]="-1"
38
+ done
39
+ local n="${#N[@]}" w=0 remaining="${#N[@]}" progress=1 i j b bl ok
40
+ [ "$remaining" -gt 0 ] || return 0
41
+ while [ "$remaining" -gt 0 ] && [ "$progress" -eq 1 ]; do
42
+ progress=0
43
+ local -a ready=()
44
+ i=0
45
+ while [ "$i" -lt "$n" ]; do
46
+ if [ "${W[$i]}" = "-1" ]; then
47
+ ok=1; bl="${B[$i]}"
48
+ for b in ${bl//,/ }; do
49
+ j=0
50
+ while [ "$j" -lt "$n" ]; do
51
+ if [ "${N[$j]}" = "$b" ] && [ "${W[$j]}" = "-1" ]; then ok=0; fi
52
+ j=$((j + 1))
53
+ done
54
+ done
55
+ [ "$ok" = "1" ] && ready[${#ready[@]}]="$i"
56
+ fi
57
+ i=$((i + 1))
58
+ done
59
+ if [ "${#ready[@]}" -gt 0 ]; then
60
+ for i in "${ready[@]}"; do W[$i]="$w"; done
61
+ remaining=$((remaining - ${#ready[@]})); w=$((w + 1)); progress=1
62
+ fi
63
+ done
64
+ i=0
65
+ while [ "$i" -lt "$n" ]; do [ "${W[$i]}" = "-1" ] && W[$i]="$w"; i=$((i + 1)); done
66
+ i=0
67
+ while [ "$i" -lt "$n" ]; do printf '%s\t%s\n' "${N[$i]}" "${W[$i]}"; i=$((i + 1)); done
68
+ }
69
+
70
+ # pm_batches — pure session-fit + file-disjoint packer for ONE wave (already deps-independent).
71
+ # stdin: lines "num<TAB>ctx<TAB>spaceSepFiles"; stdout: "num<TAB>batch". Budget = $1 (default 4).
72
+ # New batch when load+weight would overflow the budget OR the issue's files intersect the batch.
73
+ pm_batches() {
74
+ [ -n "${ZSH_VERSION:-}" ] && setopt local_options ksh_arrays sh_word_split 2>/dev/null
75
+ local budget="${1:-4}"
76
+ local -a N=() C=() F=()
77
+ local num ctx files
78
+ while IFS="$(printf '\t')" read -r num ctx files; do
79
+ [ -n "$num" ] || continue
80
+ N[${#N[@]}]="$num"; C[${#C[@]}]="$ctx"; F[${#F[@]}]="$files"
81
+ done
82
+ local n="${#N[@]}" batch=0 load=0 batchfiles=" " i w f ff need
83
+ i=0
84
+ while [ "$i" -lt "$n" ]; do
85
+ w="$(_pm_weight "${C[$i]}")"
86
+ need=0
87
+ [ $((load + w)) -gt "$budget" ] && need=1
88
+ ff="${F[$i]}"
89
+ for f in $ff; do
90
+ case "$batchfiles" in *" $f "*) need=1 ;; esac
91
+ done
92
+ if [ "$need" = "1" ] && [ "$load" -gt 0 ]; then
93
+ batch=$((batch + 1)); load=0; batchfiles=" "
94
+ fi
95
+ load=$((load + w))
96
+ for f in $ff; do batchfiles="$batchfiles$f "; done
97
+ printf '%s\t%s\n' "${N[$i]}" "$batch"
98
+ i=$((i + 1))
99
+ done
100
+ }
101
+
102
+ # _pm_files — pull a "Files:" / "Touches:" hint line from an issue body; emit space-separated paths.
103
+ _pm_files() {
104
+ printf '%s' "$1" | grep -ioE '^[[:space:]]*(Files|Touches):[[:space:]]*.*' 2>/dev/null \
105
+ | head -1 | sed -E 's/^[[:space:]]*(Files|Touches):[[:space:]]*//I; s/[,]+/ /g' \
106
+ | tr -s ' ' | sed 's/^ //; s/ $//'
107
+ }
108
+
109
+ plan_machine() {
110
+ command -v gh >/dev/null 2>&1 && command -v jq >/dev/null 2>&1 || { echo "plan: needs gh + jq" >&2; return 1; }
111
+ # output mode: the dispatcher strips --llm and exports CCKIT_OUTPUT=json, so honor that env first;
112
+ # --llm/--output=json are kept for direct lib calls / tests.
113
+ local repo="$PLAN_REPO" budget="${KIT_SESSION_BUDGET:-4}" out="${CCKIT_OUTPUT:-human}" effort="" ms=""
114
+ while [ "$#" -gt 0 ]; do
115
+ case "$1" in
116
+ --llm|--output=json) out="json"; shift ;;
117
+ --effort) effort="$2"; shift 2 ;;
118
+ --effort=*) effort="${1#*=}"; shift ;;
119
+ --milestone) ms="$2"; shift 2 ;;
120
+ --milestone=*) ms="${1#*=}"; shift ;;
121
+ --cap) budget="$2"; shift 2 ;;
122
+ --cap=*) budget="${1#*=}"; shift ;;
123
+ -h|--help) sed -n '14,21p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; return 0 ;;
124
+ *) echo "plan: unknown arg '$1'" >&2; return 2 ;;
125
+ esac
126
+ done
127
+ [ -n "$repo" ] || { echo "plan: no repo (set KIT_REPO / PLAN_REPO)" >&2; return 1; }
128
+ case "$budget" in ''|*[!0-9]*) echo "plan: --cap needs a number (got '$budget')" >&2; return 2 ;; esac
129
+
130
+ # 1. the issue set: an effort's sub-issues, or the open board.
131
+ local raw
132
+ if [ -n "$effort" ]; then
133
+ raw="$(gh api "repos/$repo/issues/$effort/sub_issues" --paginate \
134
+ --jq '.[] | select(.state=="open") | [(.number|tostring), .title, .body] | @tsv' 2>/dev/null)" \
135
+ || { echo "plan: failed to read sub-issues of #$effort" >&2; return 1; }
136
+ else
137
+ local mjq='.[]'
138
+ [ -n "$ms" ] && mjq=".[] | select((.milestone.title // \"\") == \"$ms\")"
139
+ raw="$(gh issue list --repo "$repo" --state open --limit 200 --json number,title,body,milestone \
140
+ --jq "$mjq | [(.number|tostring), .title, .body] | @tsv" 2>/dev/null)" \
141
+ || { echo "plan: gh query failed (check gh auth)" >&2; return 1; }
142
+ fi
143
+ [ -n "$raw" ] || { [ "$out" = "json" ] && echo "[]" || echo "plan: no open issues to plan"; return 0; }
144
+
145
+ # 2. parallel arrays + per-issue ctx label, file hint, and OPEN blocked_by edges.
146
+ local -a nums=() titles=() ctxs=() files=() blkall=()
147
+ local n t b body lab
148
+ while IFS="$(printf '\t')" read -r n t body; do
149
+ [ -n "$n" ] || continue
150
+ nums+=("$n"); titles+=("$t")
151
+ lab="$(gh issue view "$n" --repo "$repo" --json labels \
152
+ --jq '[.labels[].name|select(startswith("ctx:"))|ltrimstr("ctx:")]|first // "M"' 2>/dev/null)"
153
+ ctxs+=("${lab:-M}")
154
+ files+=("$(_pm_files "$body")")
155
+ # only OPEN blockers gate execution; closed ones are already satisfied.
156
+ b="$(gh api "repos/$repo/issues/$n/dependencies/blocked_by" \
157
+ --jq '[.[]|select(.state=="open")|.number]|join(",")' 2>/dev/null)"
158
+ blkall+=("$b")
159
+ done <<EOF
160
+ $raw
161
+ EOF
162
+
163
+ # 3. split blockers into in-set (gate the wave layering) vs external-open (issue can't start yet).
164
+ local -a inset=() ext=()
165
+ local i j bb keepin keepext present
166
+ for i in "${!nums[@]}"; do
167
+ keepin=""; keepext=""
168
+ for bb in ${blkall[$i]//,/ }; do
169
+ present=0
170
+ for j in "${!nums[@]}"; do [ "${nums[$j]}" = "$bb" ] && present=1; done
171
+ if [ "$present" = "1" ]; then keepin="$keepin,$bb"; else keepext="$keepext,$bb"; fi
172
+ done
173
+ inset+=("${keepin#,}"); ext+=("${keepext#,}")
174
+ done
175
+
176
+ # 4. layer into waves (in-set deps only).
177
+ local wavetsv waves
178
+ wavetsv="$(for i in "${!nums[@]}"; do printf '%s\t%s\n' "${nums[$i]}" "${inset[$i]}"; done | pm_waves)"
179
+ # map number -> wave (linear; N small)
180
+ _wave_of() { printf '%s' "$wavetsv" | awk -F'\t' -v k="$1" '$1==k{print $2; exit}'; }
181
+ waves="$(printf '%s' "$wavetsv" | awk -F'\t' '{print $2}' | sort -n | uniq)"
182
+
183
+ # 5. within each wave, pack into batches (ctx budget + file overlap), then emit.
184
+ emit_rows() { # writes "wave<TAB>batch<TAB>number<TAB>ctx<TAB>blockers<TAB>title"
185
+ local wv batchtsv num bt
186
+ for wv in $waves; do
187
+ # gather this wave's issues as "num<TAB>ctx<TAB>files" in array order
188
+ batchtsv="$(for i in "${!nums[@]}"; do
189
+ [ "$(_wave_of "${nums[$i]}")" = "$wv" ] || continue
190
+ printf '%s\t%s\t%s\n' "${nums[$i]}" "${ctxs[$i]}" "${files[$i]}"
191
+ done | pm_batches "$budget")"
192
+ while IFS="$(printf '\t')" read -r num bt; do
193
+ [ -n "$num" ] || continue
194
+ for i in "${!nums[@]}"; do
195
+ if [ "${nums[$i]}" = "$num" ]; then
196
+ printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$wv" "$bt" "$num" "${ctxs[$i]}" "${inset[$i]}" "${titles[$i]}"
197
+ fi
198
+ done
199
+ done <<EOF2
200
+ $batchtsv
201
+ EOF2
202
+ done
203
+ }
204
+
205
+ local here; here="$(dirname "${BASH_SOURCE[0]}")"
206
+
207
+ # raw rows (wave<TAB>batch<TAB>number<TAB>ctx<TAB>blockers<TAB>title) — the copilot driver (#78)
208
+ # consumes this; not a public output mode.
209
+ if [ "$out" = "tsv" ]; then emit_rows; return 0; fi
210
+
211
+ if [ "$out" = "json" ]; then
212
+ # TOON-encode a uniform row array (wave,batch,number,ctx,blockers,title) for the copilot.
213
+ local json
214
+ json="$(emit_rows | jq -R -s '
215
+ [ split("\n")[] | select(length>0) | split("\t")
216
+ | {wave:(.[0]|tonumber), batch:(.[1]|tonumber), number:(.[2]|tonumber),
217
+ ctx:.[3], blockers:.[4], title:.[5]} ]')"
218
+ # route through the TOON encoder (uniform array -> tabular; falls back to JSON under the gate).
219
+ # shellcheck source=/dev/null
220
+ . "$here/toon.sh"
221
+ printf '%s' "$json" | toon_encode
222
+ return 0
223
+ fi
224
+
225
+ # human render: emit MARKDOWN (a table per wave) and route it through the rendering seam (#82) —
226
+ # rich via glow in a TTY, verbatim markdown otherwise (renders natively in Claude Code, pipe-safe).
227
+ # awk -F'\t' is used so the empty blockers field never collapses the columns.
228
+ # shellcheck source=/dev/null
229
+ . "$here/render.sh"
230
+ {
231
+ printf '# cckit plan — %s\n\n' "$repo"
232
+ printf '_session budget %s · ctx S=1 M=2 L=4 XL=8 · each wave runs in parallel_\n' "$budget"
233
+ emit_rows | awk -F'\t' '
234
+ BEGIN { lw="" }
235
+ {
236
+ wv=$1; bt=$2; num=$3; ctx=$4; blk=$5; title=$6
237
+ if (wv != lw) {
238
+ printf "\n## Wave %s\n\n", wv
239
+ print "| # | ctx | batch | issue | after |"
240
+ print "|---|-----|-------|-------|-------|"
241
+ lw=wv
242
+ }
243
+ sub(/^\[Effort( [0-9]+)?\] [0-9]+ · ?/, "", title)
244
+ sub(/^\[[A-Za-z]+\] /, "", title)
245
+ dep="—"
246
+ if (blk != "") { gsub(/,/, ", #", blk); dep="#" blk }
247
+ printf "| #%s | %s | %s | %s | %s |\n", num, ctx, bt, title, dep
248
+ }'
249
+ # externally-blocked issues never enter a wave — surface them so they aren't silently dropped.
250
+ local anyext=0
251
+ for i in "${!nums[@]}"; do [ -n "${ext[$i]}" ] && anyext=1; done
252
+ if [ "$anyext" = "1" ]; then
253
+ printf '\n## Blocked (open blockers outside this set — not scheduled)\n\n'
254
+ for i in "${!nums[@]}"; do
255
+ [ -n "${ext[$i]}" ] && printf -- '- #%s waits on #%s\n' "${nums[$i]}" "${ext[$i]//,/, #}"
256
+ done
257
+ fi
258
+ printf '\n'
259
+ } | cckit_render
260
+ }
@@ -8,7 +8,7 @@ project_scan() {
8
8
  [ -f "$root/pyproject.toml" ] && stack+=("python")
9
9
  [ -f "$root/go.mod" ] && stack+=("go")
10
10
  [ -f "$root/Cargo.toml" ] && stack+=("rust")
11
- if [ -f "$root/cckit.config.json" ]; then kit="configured"
11
+ if [ -f "$root/cckit.config.json" ] || [ -f "$root/.claude/kit.config.json" ]; then kit="configured"
12
12
  elif [ -d "$root/.cckit" ]; then kit="partial"
13
13
  elif [ -d "$root/.claude" ]; then kit="claude-only"; fi
14
14
  printf '{"root":"%s","stack":[%s],"kit":"%s"}\n' \