@jeiemgi/cckit 0.9.0 → 0.10.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 (305) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +169 -21
  3. package/README.md +491 -35
  4. package/SECURITY.md +5 -0
  5. package/bin/cckit +182 -17
  6. package/cckit.config.json +1 -1
  7. package/commands/kit-doctor.md +13 -5
  8. package/commands/kit-export-project.md +0 -2
  9. package/commands/kit-export-training.md +6 -9
  10. package/commands/kit-init.md +43 -13
  11. package/commands/kit-routines.md +1 -1
  12. package/commands/kit-update.md +11 -6
  13. package/databases/supabase/guide.html +106 -0
  14. package/databases/supabase/nextjs/lib/supabase/client.ts +9 -0
  15. package/databases/supabase/nextjs/lib/supabase/server.ts +29 -0
  16. package/databases/supabase.json +70 -0
  17. package/integrations/emil-design-eng.json +41 -0
  18. package/integrations/graft.json +81 -0
  19. package/integrations/impeccable.json +42 -0
  20. package/integrations/integration.schema.json +215 -0
  21. package/integrations/lavish.json +43 -0
  22. package/integrations/mempalace.json +28 -0
  23. package/integrations/mlx-lm.json +32 -0
  24. package/integrations/web-design-guidelines.json +42 -0
  25. package/kit.config.schema.json +183 -5
  26. package/package.json +4 -1
  27. package/profiles/automation.json +7 -1
  28. package/profiles/content.json +10 -2
  29. package/profiles/marketing.json +57 -0
  30. package/profiles/minimal.json +7 -1
  31. package/profiles/research.json +7 -1
  32. package/profiles/software.json +7 -1
  33. package/recipes/nextjs-shadcn.json +49 -0
  34. package/scripts/agent-skills-external.txt +45 -0
  35. package/scripts/agent-skills-lint-test.sh +131 -0
  36. package/scripts/agent-skills-lint.sh +115 -0
  37. package/scripts/agent-template-lint-test.sh +42 -0
  38. package/scripts/agent-template-lint.sh +58 -0
  39. package/scripts/annotate-setup.sh +1 -1
  40. package/scripts/autopilot-run-test.sh +338 -0
  41. package/scripts/autopilot-run.sh +448 -0
  42. package/scripts/autopilot-test.sh +8 -0
  43. package/scripts/autopilot.sh +10 -1
  44. package/scripts/benchmark/benchmark-test.sh +85 -0
  45. package/scripts/benchmark/checkpoint-test.sh +314 -0
  46. package/scripts/benchmark/checkpoint.sh +408 -0
  47. package/scripts/benchmark/lib/model.sh +31 -6
  48. package/scripts/benchmark/report.sh +17 -2
  49. package/scripts/benchmark/run-quality.sh +36 -10
  50. package/scripts/benchmark/verb-timings-test.sh +124 -0
  51. package/scripts/benchmark/verb-timings.sh +149 -0
  52. package/scripts/check.sh +29 -19
  53. package/scripts/content-skills-test.sh +144 -0
  54. package/scripts/copilot-loop-test.sh +25 -0
  55. package/scripts/database-supabase-test.sh +121 -0
  56. package/scripts/docs-marketing-words-test.sh +73 -0
  57. package/scripts/docs-record-tables-test.sh +72 -0
  58. package/scripts/hook-adapter-test.sh +193 -0
  59. package/scripts/init-content-test.sh +225 -0
  60. package/scripts/init-guided-test.sh +474 -0
  61. package/scripts/init-recipes-test.sh +104 -0
  62. package/scripts/init-upgrade-test.sh +341 -2
  63. package/scripts/init.sh +746 -95
  64. package/scripts/instruction-budget-baseline.txt +8 -0
  65. package/scripts/instruction-budget-lint-test.sh +166 -0
  66. package/scripts/instruction-budget-lint.sh +219 -0
  67. package/scripts/integrations-check-test.sh +200 -0
  68. package/scripts/integrations-check.sh +157 -0
  69. package/scripts/kit-add-test.sh +214 -0
  70. package/scripts/kit-add.sh +305 -5
  71. package/scripts/kit-adopt-test.sh +16 -0
  72. package/scripts/kit-adopt.sh +3 -2
  73. package/scripts/kit-digest.sh +2 -2
  74. package/scripts/kit-doctor-test.sh +293 -0
  75. package/scripts/kit-doctor.sh +272 -140
  76. package/scripts/kit-export-project-test.sh +3 -3
  77. package/scripts/kit-remove-test.sh +19 -0
  78. package/scripts/kit-routines.sh +2 -2
  79. package/scripts/kit-self-update.sh +1 -1
  80. package/scripts/kit-version-check-test.sh +4 -0
  81. package/scripts/kit-version-check.sh +4 -2
  82. package/scripts/knowledge-lint.sh +4 -4
  83. package/scripts/leak-fence-test.sh +89 -0
  84. package/scripts/lib/adapter-select-test.sh +136 -0
  85. package/scripts/lib/adapter-select.sh +158 -0
  86. package/scripts/lib/agent-def-test.sh +69 -0
  87. package/scripts/lib/agent-def.sh +106 -0
  88. package/scripts/lib/agent-launch-test.sh +212 -0
  89. package/scripts/lib/agent-launch.sh +335 -0
  90. package/scripts/lib/board-view-test.sh +11 -0
  91. package/scripts/lib/board-view.sh +42 -26
  92. package/scripts/lib/branch-owner-test.sh +26 -0
  93. package/scripts/lib/branch-owner.sh +16 -9
  94. package/scripts/lib/captain-test.sh +105 -0
  95. package/scripts/lib/captain.sh +87 -8
  96. package/scripts/lib/codex-hooks-test.sh +100 -0
  97. package/scripts/lib/codex-hooks.sh +69 -0
  98. package/scripts/lib/commitlint.sh +1 -1
  99. package/scripts/lib/content-kinds-gc-test.sh +88 -0
  100. package/scripts/lib/copilot-test.sh +70 -4
  101. package/scripts/lib/copilot.sh +66 -14
  102. package/scripts/lib/effort-dispatch-test.sh +3 -3
  103. package/scripts/lib/effort-metrics-test.sh +178 -1
  104. package/scripts/lib/effort-metrics.sh +175 -35
  105. package/scripts/lib/effort-ops-test.sh +205 -7
  106. package/scripts/lib/effort-ops.sh +198 -43
  107. package/scripts/lib/effort-parity-test.sh +49 -13
  108. package/scripts/lib/effort-plan-test.sh +337 -0
  109. package/scripts/lib/effort-plan.sh +200 -23
  110. package/scripts/lib/effort.sh +56 -11
  111. package/scripts/lib/errors-header-test.sh +1 -1
  112. package/scripts/lib/gh-project-test.sh +35 -1
  113. package/scripts/lib/gh-project.sh +23 -16
  114. package/scripts/lib/gh-rest-test.sh +32 -3
  115. package/scripts/lib/gh-rest.sh +17 -2
  116. package/scripts/lib/hook-refresh.sh +1 -1
  117. package/scripts/lib/init-github.sh +412 -0
  118. package/scripts/lib/init-guide.sh +630 -0
  119. package/scripts/lib/integration-install-test.sh +343 -0
  120. package/scripts/lib/integration-install.sh +257 -0
  121. package/scripts/lib/integrations-test.sh +366 -0
  122. package/scripts/lib/integrations.sh +244 -0
  123. package/scripts/lib/kinds-test.sh +125 -0
  124. package/scripts/lib/kinds.sh +66 -0
  125. package/scripts/lib/kit-brief-test.sh +22 -3
  126. package/scripts/lib/kit-brief.sh +45 -5
  127. package/scripts/lib/kit-config-resolve.sh +2 -2
  128. package/scripts/lib/kit-config-test.sh +64 -1
  129. package/scripts/lib/kit-config.sh +37 -1
  130. package/scripts/lib/kit-events.sh +7 -0
  131. package/scripts/lib/kit-gc-test.sh +173 -1
  132. package/scripts/lib/kit-gc.sh +226 -37
  133. package/scripts/lib/kit-hotfix.sh +216 -0
  134. package/scripts/lib/kit-interview-test.sh +93 -0
  135. package/scripts/lib/kit-interview.sh +117 -8
  136. package/scripts/lib/kit-lib-test.sh +1 -1
  137. package/scripts/lib/kit-lib.sh +3 -3
  138. package/scripts/lib/kit-local.sh +2 -2
  139. package/scripts/lib/kit-manifest.sh +33 -1
  140. package/scripts/lib/kit-profile-test.sh +3 -3
  141. package/scripts/lib/kit-status-test.sh +112 -4
  142. package/scripts/lib/kit-status.sh +246 -16
  143. package/scripts/lib/kit-task-ops-test.sh +37 -0
  144. package/scripts/lib/kit-task-ops.sh +175 -31
  145. package/scripts/lib/kto-hotfix-test.sh +232 -0
  146. package/scripts/lib/kto-task-close-test.sh +1 -1
  147. package/scripts/lib/kto-task-new-test.sh +95 -0
  148. package/scripts/lib/kto-task-pr-merge-test.sh +37 -3
  149. package/scripts/lib/kto-task-pr-test.sh +70 -0
  150. package/scripts/lib/leak-fence.sh +89 -0
  151. package/scripts/lib/light-task-run-test.sh +255 -0
  152. package/scripts/lib/light-task-test.sh +309 -0
  153. package/scripts/lib/light-task.sh +706 -0
  154. package/scripts/lib/next-test.sh +99 -0
  155. package/scripts/lib/next.sh +9 -3
  156. package/scripts/lib/orchestration-runtime-test.sh +72 -0
  157. package/scripts/lib/orchestration-runtime.sh +57 -5
  158. package/scripts/lib/plan-machine-test.sh +15 -2
  159. package/scripts/lib/plan-machine.sh +20 -7
  160. package/scripts/lib/plan-next-test.sh +1 -1
  161. package/scripts/lib/plan-next.sh +4 -4
  162. package/scripts/lib/pr-evidence-test.sh +3 -3
  163. package/scripts/lib/pr-evidence.sh +1 -1
  164. package/scripts/lib/qa-lane-test.sh +626 -0
  165. package/scripts/lib/qa-lane.sh +1357 -0
  166. package/scripts/lib/qa-lanes-test.sh +175 -0
  167. package/scripts/lib/react-detect.sh +1 -1
  168. package/scripts/lib/render-test.sh +1 -1
  169. package/scripts/lib/review-page-test.sh +318 -0
  170. package/scripts/lib/review-page.sh +369 -0
  171. package/scripts/lib/review-stage-test.sh +62 -11
  172. package/scripts/lib/review-stage.sh +42 -60
  173. package/scripts/lib/role-identity-test.sh +65 -0
  174. package/scripts/lib/role-identity.sh +25 -2
  175. package/scripts/lib/secret-guard.sh +3 -3
  176. package/scripts/lib/session-registry-test.sh +111 -0
  177. package/scripts/lib/session-registry.sh +233 -0
  178. package/scripts/lib/stage-receipt-test.sh +14 -1
  179. package/scripts/lib/stage-receipt.sh +13 -8
  180. package/scripts/lib/task-source-file.sh +309 -0
  181. package/scripts/lib/task-source.sh +130 -0
  182. package/scripts/lib/upgrade-report-test.sh +120 -0
  183. package/scripts/lib/upgrade-report.sh +138 -0
  184. package/scripts/lib/version-bump.sh +3 -3
  185. package/scripts/lib/wave-result-test.sh +80 -0
  186. package/scripts/lib/wave-result.sh +127 -0
  187. package/scripts/lib/worker-brief-test.sh +5 -0
  188. package/scripts/lib/worker-brief.sh +2 -2
  189. package/scripts/lib/worktree-issue-test.sh +30 -1
  190. package/scripts/lib/worktree-issue.sh +82 -12
  191. package/scripts/lib/worktree-ports-test.sh +57 -4
  192. package/scripts/lib/worktree-start-test.sh +197 -0
  193. package/scripts/lib/worktree-start.sh +153 -31
  194. package/scripts/lib/zsh-safety-test.sh +19 -3
  195. package/scripts/marketing-walkthrough-test.sh +152 -0
  196. package/scripts/night-grant-test.sh +37 -0
  197. package/scripts/night-grant.sh +448 -0
  198. package/scripts/night-run-test.sh +8 -0
  199. package/scripts/night-run.mjs +275 -0
  200. package/scripts/night-run.test.mjs +324 -0
  201. package/scripts/orchestrate-test.sh +190 -0
  202. package/scripts/orchestrate.sh +79 -8
  203. package/scripts/portable-test.sh +11 -2
  204. package/scripts/publish.sh +28 -3
  205. package/scripts/review-lint-test.sh +7 -7
  206. package/scripts/review-lint.sh +2 -2
  207. package/scripts/review-rules.conf +8 -0
  208. package/scripts/rule-recipes-test.sh +96 -0
  209. package/scripts/self-install-test.sh +39 -9
  210. package/scripts/setup-labels.sh +18 -1
  211. package/scripts/setup-milestones.sh +1 -1
  212. package/scripts/showcase.sh +1 -1
  213. package/scripts/skill-frontmatter-lint-test.sh +4 -4
  214. package/scripts/skill-frontmatter-lint.sh +1 -1
  215. package/scripts/status-test.sh +113 -2
  216. package/scripts/status.sh +16 -3
  217. package/scripts/task-sync-test.sh +154 -0
  218. package/scripts/task-sync.sh +243 -32
  219. package/scripts/test-runner-test.sh +158 -0
  220. package/scripts/test.sh +131 -23
  221. package/scripts/update-path-test.sh +279 -0
  222. package/scripts/ux-ds-audit.sh +1 -1
  223. package/scripts/ux-graph-scan.sh +6 -6
  224. package/scripts/wave-worker.sh +54 -0
  225. package/skills/kit-add/SKILL.md +77 -0
  226. package/skills/kit-autopilot/SKILL.md +123 -12
  227. package/skills/kit-cleanup/SKILL.md +5 -2
  228. package/skills/kit-contribute/SKILL.md +4 -4
  229. package/skills/kit-docs-writer/SKILL.md +16 -6
  230. package/skills/kit-effort-close/SKILL.md +17 -8
  231. package/skills/kit-effort-new/SKILL.md +15 -9
  232. package/skills/kit-effort-pr/SKILL.md +15 -7
  233. package/skills/kit-effort-start/SKILL.md +5 -2
  234. package/skills/kit-gc/SKILL.md +5 -2
  235. package/skills/kit-next/SKILL.md +5 -1
  236. package/skills/kit-onboard/SKILL.md +3 -3
  237. package/skills/kit-page/SKILL.md +93 -0
  238. package/skills/kit-plan-next/SKILL.md +8 -4
  239. package/skills/kit-security-sweep/SKILL.md +1 -3
  240. package/skills/kit-ship/SKILL.md +2 -3
  241. package/skills/kit-status/SKILL.md +35 -4
  242. package/skills/kit-task-new/SKILL.md +3 -3
  243. package/skills/kit-task-pr/SKILL.md +7 -2
  244. package/skills/kit-task-pr-merge/SKILL.md +7 -2
  245. package/skills/kit-task-start/SKILL.md +7 -7
  246. package/skills/kit-task-sync/SKILL.md +19 -3
  247. package/skills/kit-ux-audit/SKILL.md +3 -2
  248. package/templates/agents/auto-dev.md +5 -4
  249. package/templates/agents/backend.md +1 -1
  250. package/templates/agents/designer.md +2 -0
  251. package/templates/agents/devops.md +1 -1
  252. package/templates/agents/editor.md +9 -1
  253. package/templates/agents/frontend.md +1 -1
  254. package/templates/agents/local-delegate.md +1 -1
  255. package/templates/agents/pm.md +5 -5
  256. package/templates/agents/tech-lead.md +1 -1
  257. package/templates/benchmarks/SCHEMA.md +9 -3
  258. package/templates/codex/hooks.json.tmpl +89 -0
  259. package/templates/hooks/guard-base-branch-commit.sh.tmpl +13 -7
  260. package/templates/hooks/guard-raw-git-ops.sh.tmpl +12 -12
  261. package/templates/hooks/mempal_followup.sh.tmpl +2 -2
  262. package/templates/hooks/mempal_session_start.sh.tmpl +1 -1
  263. package/templates/hooks/prepush_gate.sh.tmpl +4 -2
  264. package/templates/hooks/repo-hygiene.sh.tmpl +3 -2
  265. package/templates/instructions.md.tmpl +174 -0
  266. package/templates/kit.config.json.tmpl +3 -3
  267. package/templates/knowledge-INDEX.md.tmpl +1 -1
  268. package/templates/lib/kit-sigil.sh.tmpl +1 -1
  269. package/templates/rules/authoring.md +106 -0
  270. package/templates/rules/branch-naming.md +40 -4
  271. package/templates/rules/code-retrieval.md +63 -0
  272. package/templates/rules/delegation-brief.md +0 -3
  273. package/templates/rules/design-routing.md +3 -3
  274. package/templates/rules/effort-model.md +14 -10
  275. package/templates/rules/gate-and-review.md +94 -0
  276. package/templates/rules/issue-lifecycle.md +88 -0
  277. package/templates/rules/knowledge-base.md +1 -1
  278. package/templates/rules/mempalace.md +1 -1
  279. package/templates/rules/merge-safety.md +108 -0
  280. package/templates/rules/naming-and-ids.md +23 -17
  281. package/templates/rules/plain-language.md +59 -0
  282. package/templates/rules/plan-next.md +9 -11
  283. package/templates/rules/react-annotate.md +6 -0
  284. package/templates/rules/review-pages.md +49 -0
  285. package/templates/rules/risk-tiered-review.md +13 -4
  286. package/templates/rules/shared-checkout.md +106 -0
  287. package/templates/rules/skill-gaps.md +2 -7
  288. package/templates/rules/task-management.md +13 -1
  289. package/templates/skills/brand-voice/SKILL.md +91 -0
  290. package/templates/skills/brand-voice/references/voice-template.md +61 -0
  291. package/templates/skills/campaign/SKILL.md +123 -0
  292. package/templates/skills/campaign/references/plan-template.md +55 -0
  293. package/templates/skills/concrete/SKILL.md +2 -2
  294. package/templates/skills/copywriting/SKILL.md +8 -1
  295. package/templates/skills/feature-build-refine/SKILL.md +2 -4
  296. package/templates/skills/morning-briefing/SKILL.md +2 -2
  297. package/templates/skills/page-design/SKILL.md +145 -0
  298. package/templates/skills/page-design/add.json +5 -0
  299. package/templates/skills/page-design/references/design-file-template.md +40 -0
  300. package/templates/skills/page-design/references/generic-page-patterns.md +43 -0
  301. package/templates/skills/page-design/references/minimum-rules.md +42 -0
  302. package/templates/skills/speckit/SKILL.md +4 -5
  303. package/templates/skills/supabase-patterns/SKILL.md +1 -1
  304. package/commands/kit-add.md +0 -42
  305. package/templates/CLAUDE.md.tmpl +0 -106
@@ -17,6 +17,12 @@
17
17
  # RECORDED under the git-common-dir, so uniqueness is by construction and an issue keeps its
18
18
  # slot for as long as its worktree lives.
19
19
  #
20
+ # 4. A LOCK THAT DID NOT EXCLUDE (#449). The ledger lock was `mkdir`. The `mkdir` of uutils
21
+ # coreutils 0.10.0 (Ubuntu 26.04, GitHub's `ubuntu-latest`) exits 0 for the loser of a race, so
22
+ # two allocations ran together and took the same slot: 4 of 70 runs of this file on that
23
+ # runner, 52 of 100 runs of section 6 in an Ubuntu 26.04 container. Section 7 races the
24
+ # primitive itself.
25
+ #
20
26
  # The bar: N worktrees over arbitrary issue numbers, in either config layout, produce N disjoint
21
27
  # port sets — and re-running for a live issue never renumbers it.
22
28
  #
@@ -85,6 +91,20 @@ wt_assign_ports "$W" 7 "$P" >/dev/null 2>&1
85
91
  after="$(ports_of "$W" | tr '\n' ' ')"
86
92
  [ "$before" = "$after" ] && ok || bad "re-run renumbered a live lane: '$before' -> '$after'"
87
93
 
94
+ # ── 4b. the public readers agree with the allocation (#295) ───────────────────────────────────
95
+ # The QA lane derives its ports from wt_port_slot + wt_port_stride. If either disagreed with what
96
+ # wt_assign_ports wrote, a lane's stack would boot off the worktree's own port block.
97
+ slot7="$(wt_port_slot "$P" 7 "$W" 2>/dev/null)"
98
+ stride7="$(wt_port_stride "$P/cckit.config.json")"
99
+ [ "$stride7" = 10 ] && ok || bad "wt_port_stride default: got '$stride7', want 10"
100
+ first7="$(ports_of "$W" | sed -n 1p)"
101
+ [ "$first7" = "$(( 3001 + slot7 * stride7 ))" ] && ok || bad "wt_port_slot disagrees with the assigned port: slot '$slot7' stride '$stride7' port '$first7'"
102
+ [ "$(wt_port_slot "$P" 7 "$W" 2>/dev/null)" = "$slot7" ] && ok || bad "wt_port_slot renumbered a live issue"
103
+ printf '%s' '{"worktree":{"devPortStride":25}}' > "$TMP/stride.json"
104
+ [ "$(wt_port_stride "$TMP/stride.json")" = 25 ] && ok || bad "wt_port_stride ignored worktree.devPortStride"
105
+ printf '%s' '{"worktree":{"devPortStride":"wide"}}' > "$TMP/stride.json"
106
+ [ "$(wt_port_stride "$TMP/stride.json")" = 10 ] && ok || bad "wt_port_stride accepted a non-integer stride"
107
+
88
108
  # ── 5. a freed slot is reclaimed, not leaked ──────────────────────────────────────────────────
89
109
  slots="$(git -C "$P" rev-parse --git-common-dir 2>/dev/null)"
90
110
  case "$slots" in /*) : ;; *) slots="$(cd "$P" && cd "$slots" && pwd)" ;; esac
@@ -119,7 +139,7 @@ for num in $RACE_NUMS; do
119
139
  while [ "$(lines_in "$barrier")" -lt "$RACE_N" ] && [ "$waited" -lt 50 ]; do
120
140
  sleep 0.1; waited=$(( waited + 1 ))
121
141
  done
122
- wt_assign_ports "$W" "$num" "$P" >/dev/null 2>&1
142
+ wt_assign_ports "$W" "$num" "$P" >/dev/null 2>"$P/.err.$num"
123
143
  ) &
124
144
  pids="$pids $!"
125
145
  done
@@ -134,13 +154,46 @@ race_uniq="$(printf '%s' "$race" | sort -u | grep -c .)"
134
154
  [ "$race_total" = 24 ] && ok || bad "parallel: expected 24 ports over 8 concurrent worktrees, got $race_total"
135
155
  if [ "$race_total" = "$race_uniq" ]; then ok; else
136
156
  bad "parallel port collision: $race_total ports but only $race_uniq distinct ($(printf '%s' "$race" | sort | uniq -d | tr '\n' ' '))"
157
+ # The evidence, not only the verdict: what the ledger recorded and what the allocator said on
158
+ # stderr (a lock that timed out warns there). Without these a collision cannot be traced (#449).
159
+ echo " ledger:"; sed 's/^/ /' "$P/.git/kit-portslots.tsv" 2>&1
160
+ echo " allocator stderr:"; cat "$P"/.err.* 2>/dev/null | grep -v ' PORT=' | sed 's/^/ /'
137
161
  fi
138
162
 
139
- # The lock is released on the way out — a leftover lock directory wedges every later start until
140
- # the one-minute abandonment window elapses.
163
+ # The lock is released on the way out — a leftover lock wedges every later start until the
164
+ # one-minute abandonment window elapses.
141
165
  race_gcd="$(git -C "$P" rev-parse --git-common-dir 2>/dev/null)"
142
166
  case "$race_gcd" in /*) : ;; *) race_gcd="$(cd "$P" && cd "$race_gcd" && pwd)" ;; esac
143
- [ ! -d "$race_gcd/kit-portslots.tsv.lock" ] && ok || bad "the port-slot lock was left behind after allocation"
167
+ [ ! -e "$race_gcd/kit-portslots.tsv.lock" ] && ok || bad "the port-slot lock was left behind after allocation"
168
+
169
+ # ── 7. the lock primitive excludes: of N simultaneous attempts exactly one wins (#449) ────────
170
+ # Section 6 only fails when two allocations happen to overlap. This one asks the primitive
171
+ # directly: eight processes make ONE attempt each on the same lock, released together by a start
172
+ # file, and the winners are counted. A primitive that checks and then creates (uutils `mkdir`)
173
+ # reports several winners in most rounds, so 25 rounds do not pass by luck.
174
+ LOCK_ROUNDS=25; lock_bad=0; lock_detail=""
175
+ L="$TMP/prim.lock"; go="$TMP/prim.go"; won="$TMP/prim.won"
176
+ r=0
177
+ while [ "$r" -lt "$LOCK_ROUNDS" ]; do
178
+ r=$((r+1)); rm -rf "$L" "$go"; : > "$won"; pids=""
179
+ for k in 1 2 3 4 5 6 7 8; do
180
+ (
181
+ waited=0
182
+ while [ ! -e "$go" ] && [ "$waited" -lt 20000 ]; do waited=$((waited+1)); done # spin: a sleep would spread the attempts out
183
+ _wt_lock_try "$L" && echo "$k" >> "$won"
184
+ ) &
185
+ pids="$pids $!"
186
+ done
187
+ : > "$go"
188
+ for pid in $pids; do wait "$pid"; done
189
+ winners="$(lines_in "$won")"
190
+ [ "$winners" = 1 ] || { lock_bad=$((lock_bad+1)); lock_detail="$lock_detail $winners"; }
191
+ done
192
+ [ "$lock_bad" = 0 ] && ok || bad "the lock primitive let $lock_bad of $LOCK_ROUNDS rounds end without exactly one winner (winners per bad round:$lock_detail)"
193
+ _wt_unlock "$L"; [ ! -e "$L" ] && ok || bad "_wt_unlock left the lock in place"
194
+ # A cckit from before #449 leaves a lock DIRECTORY at the same path: it must still block.
195
+ mkdir "$L"; if _wt_lock_try "$L"; then bad "a lock directory from an older cckit did not block"; else ok; fi
196
+ _wt_unlock "$L"; [ ! -e "$L" ] && ok || bad "_wt_unlock left an older lock directory in place"
144
197
 
145
198
  echo "worktree-ports-test: $n assertion(s), $([ "$fail" = 0 ] && echo "all passed" || echo "FAILURES")"
146
199
  exit "$fail"
@@ -0,0 +1,197 @@
1
+ #!/usr/bin/env bash
2
+ # worktree-start-test.sh — wt_start sees live session claims (#384).
3
+ #
4
+ # The bug: a second `cckit start <N>` from another session got "reusing worktree" and rc 0, so two
5
+ # agents wrote one tree. The bar: a LIVE claim by another session refuses the start before any
6
+ # fetch, branch or worktree; `--force` / KIT_FORCE=1 proceeds; a dead session's claim is reaped;
7
+ # the claimant's own re-run stays a no-op; `cckit claims` lists and writes nothing.
8
+ #
9
+ # Hermetic: a throwaway repo with a local bare origin, a stubbed `gh` (no network, no auth) and a
10
+ # stubbed `zoxide` (so no temp path lands in the developer's jump database). A "session" is a
11
+ # background `sleep` whose pid is handed to wt_start through KIT_SESSION_PID.
12
+ #
13
+ # Run: bash scripts/lib/worktree-start-test.sh
14
+ # errors: strict — a test runner: rc 1 on any failed assertion
15
+ set -u
16
+ LIB="$(cd "$(dirname "$0")" && pwd)"
17
+ ROOT="$(cd "$LIB/../.." && pwd)"
18
+ # shellcheck source=/dev/null
19
+ . "$LIB/test-env.sh" && test_env_seal
20
+ command -v jq >/dev/null 2>&1 || { echo "worktree-start-test: jq required" >&2; exit 1; }
21
+
22
+ fail=0
23
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
24
+ has() { case "$2" in *"$3"*) echo "ok: $1" ;; *) echo "FAIL: $1 -> missing '$3' in [$2]"; fail=1 ;; esac; }
25
+
26
+ tmp="$(cd "$(mktemp -d 2>/dev/null || mktemp -d -t wst)" && pwd -P)"
27
+ SESS_A=""; SESS_B=""
28
+ cleanup() {
29
+ [ -n "$SESS_A" ] && kill "$SESS_A" 2>/dev/null
30
+ [ -n "$SESS_B" ] && kill "$SESS_B" 2>/dev/null
31
+ rm -rf "$tmp"
32
+ }
33
+ trap cleanup EXIT INT TERM
34
+
35
+ # ── fixture: stubs, a bare origin with `main`, and a clone to start worktrees from ─────────────
36
+ mkdir -p "$tmp/bin"
37
+ cat > "$tmp/bin/gh" <<'SH'
38
+ #!/usr/bin/env bash
39
+ # Every call is logged, so a test can assert a refusal made NO GitHub read.
40
+ printf '%s\n' "$*" >> "${GH_CALL_LOG:-/dev/null}"
41
+ case "$*" in
42
+ *"issue view"*) echo '{"title":"Claimed thing","labels":[{"name":"kind:task"}]}' ;;
43
+ *graphql*) echo '{"data":{}}' ;;
44
+ *) echo '[]' ;;
45
+ esac
46
+ SH
47
+ printf '#!/bin/sh\nexit 0\n' > "$tmp/bin/zoxide"
48
+ chmod +x "$tmp/bin/gh" "$tmp/bin/zoxide"
49
+ PATH="$tmp/bin:$PATH"; export PATH
50
+ export GH_CALL_LOG="$tmp/gh.calls"; : > "$GH_CALL_LOG"
51
+
52
+ git init -q --bare "$tmp/origin.git"
53
+ git init -q "$tmp/repo"
54
+ (
55
+ cd "$tmp/repo" || exit 1
56
+ git config user.email t@example.invalid; git config user.name t; git config commit.gpgsign false
57
+ git checkout -q -b main
58
+ printf '{ "kitVersion":"9.9.9", "project":{"name":"fx","slug":"fx"},\n "github":{"repo":"octo/fx","owner":"octo","baseBranch":"main","projectsV2":false} }\n' > cckit.config.json
59
+ git add cckit.config.json && git commit -q -m init
60
+ git remote add origin "$tmp/origin.git" && git push -q origin main
61
+ ) || { echo "FAIL: fixture repo setup"; exit 1; }
62
+ REPO="$tmp/repo"
63
+ REG="$REPO/.git/kit-sessions"
64
+
65
+ export WT_START_REPO="octo/fx" KIT_BASE_BRANCH="main" KIT_WT_INSTALL=0
66
+ # shellcheck source=/dev/null
67
+ . "$LIB/worktree-start.sh"
68
+
69
+ sleep 600 & SESS_A=$!
70
+ sleep 600 & SESS_B=$!
71
+
72
+ # start_as <session-pid> <args…> — wt_start from the main checkout, as that session.
73
+ start_as() { local sp="$1"; shift; ( cd "$REPO" && KIT_SESSION_PID="$sp" wt_start "$@" ); }
74
+ branches() { git -C "$REPO" for-each-ref --format='%(refname:short)' refs/heads | sort | tr '\n' ' '; }
75
+ worktrees() { git -C "$REPO" worktree list --porcelain | grep -c '^worktree '; }
76
+
77
+ # ── 1. first start: creates the worktree and writes the claim ──────────────────────────────────
78
+ out="$(start_as "$SESS_A" 7 2>/dev/null)"; rc=$?
79
+ t "first start succeeds" "$rc" "0"
80
+ WT7="$REPO/.claude/worktrees/task+7-claimed-thing"
81
+ t "first start prints path|branch|issue" "$out" "$WT7|task/7-claimed-thing|7"
82
+ t "claim file written" "$([ -f "$REG/issue-7.json" ] && echo yes || echo no)" "yes"
83
+ t "claim records the session pid" "$(jq -r .pid "$REG/issue-7.json" 2>/dev/null)" "$SESS_A"
84
+ t "claim records the worktree" "$(jq -r .worktree "$REG/issue-7.json" 2>/dev/null)" "$WT7"
85
+ t "claim records the branch" "$(jq -r .branch "$REG/issue-7.json" 2>/dev/null)" "task/7-claimed-thing"
86
+ t "claim records the issue" "$(jq -r .issue "$REG/issue-7.json" 2>/dev/null)" "7"
87
+ t "claim records a start time" "$(jq -r '.started | type' "$REG/issue-7.json" 2>/dev/null)" "number"
88
+
89
+ # ── 6. the EXISTING reader sees the claim (it reads .pid + .cwd, skips cache-*) ────────────────
90
+ _wt_session_owns "$REPO" "$WT7"; t "_wt_session_owns sees the wt_start claim" "$?" "0"
91
+ _wt_session_owns "$REPO" "$REPO/.claude/worktrees/task+8-nope"; t "_wt_session_owns is false for an unclaimed path" "$?" "1"
92
+
93
+ # ── 1. a second session is refused — and nothing is created, nothing is fetched or read ────────
94
+ # A second issue number makes "no new worktree, no new branch" observable: 7 already has both, so
95
+ # the refusal case that proves it is a claim on an issue whose worktree does NOT exist yet.
96
+ mkdir -p "$REG"
97
+ jq -n --argjson pid "$SESS_A" --arg host "$(hostname)" --arg wt "$REPO/.claude/worktrees/task+9-claimed-thing" \
98
+ --argjson started "$(date +%s)" \
99
+ '{issue:9, branch:"task/9-claimed-thing", worktree:$wt, cwd:$wt, pid:$pid, host:$host, started:$started}' \
100
+ > "$REG/issue-9.json"
101
+ b_before="$(branches)"; w_before="$(worktrees)"; : > "$GH_CALL_LOG"
102
+ fetch_before="$(stat -c %Y "$REPO/.git/FETCH_HEAD" 2>/dev/null || stat -f %m "$REPO/.git/FETCH_HEAD" 2>/dev/null)"
103
+ err="$(start_as "$SESS_B" 9 2>&1 >/dev/null)"; rc=$?
104
+ t "second session on a claimed issue is refused (rc 1)" "$rc" "1"
105
+ has "refusal names the claimant pid" "$err" "pid $SESS_A"
106
+ has "refusal names the escape hatch" "$err" "--force"
107
+ t "refusal creates no branch" "$(branches)" "$b_before"
108
+ t "refusal creates no worktree" "$(worktrees)" "$w_before"
109
+ t "refusal creates no worktree dir" "$([ -e "$REPO/.claude/worktrees/task+9-claimed-thing" ] && echo yes || echo no)" "no"
110
+ t "refusal makes no gh call (it runs before the issue read)" "$(wc -l < "$GH_CALL_LOG" | tr -d ' ')" "0"
111
+ t "refusal runs before the fetch (FETCH_HEAD untouched)" \
112
+ "$(stat -c %Y "$REPO/.git/FETCH_HEAD" 2>/dev/null || stat -f %m "$REPO/.git/FETCH_HEAD" 2>/dev/null)" "$fetch_before"
113
+ t "refusal leaves the claim with its owner" "$(jq -r .pid "$REG/issue-9.json")" "$SESS_A"
114
+
115
+ # The same refusal on the issue that already has a worktree: no "reusing worktree" success.
116
+ out="$(start_as "$SESS_B" 7 2>/dev/null)"; rc=$?
117
+ t "second session on a claimed, existing worktree is refused" "$rc" "1"
118
+ t "refused start prints no worktree line" "$out" ""
119
+
120
+ # ── 2. --force and KIT_FORCE=1 proceed ─────────────────────────────────────────────────────────
121
+ out="$(start_as "$SESS_B" --force 9 2>/dev/null)"; rc=$?
122
+ t "--force starts a claimed issue" "$rc" "0"
123
+ t "--force is not taken for the issue number" "$out" "$REPO/.claude/worktrees/task+9-claimed-thing|task/9-claimed-thing|9"
124
+ t "--force takes the claim over" "$(jq -r .pid "$REG/issue-9.json")" "$SESS_B"
125
+ out="$(start_as "$SESS_B" 7 --force 2>/dev/null)"; rc=$?
126
+ t "--force after the issue number also works" "$rc" "0"
127
+ t "claim 7 now belongs to the forcing session" "$(jq -r .pid "$REG/issue-7.json")" "$SESS_B"
128
+ out="$( cd "$REPO" && KIT_FORCE=1 KIT_SESSION_PID="$SESS_A" wt_start 7 2>/dev/null )"; rc=$?
129
+ t "KIT_FORCE=1 starts a claimed issue" "$rc" "0"
130
+ t "KIT_FORCE=1 takes the claim over" "$(jq -r .pid "$REG/issue-7.json")" "$SESS_A"
131
+ err="$(start_as "$SESS_A" --bogus 7 2>&1 >/dev/null)"; rc=$?
132
+ t "an unknown flag is refused, not read as the issue" "$rc" "1"
133
+
134
+ # ── 4. the claimant's own re-run is a no-op success ────────────────────────────────────────────
135
+ before="$(cat "$REG/issue-7.json")"
136
+ out="$(start_as "$SESS_A" 7 2>/dev/null)"; rc=$?
137
+ t "re-run by the claiming session succeeds" "$rc" "0"
138
+ t "re-run by the claiming session leaves the claim untouched" "$(cat "$REG/issue-7.json")" "$before"
139
+ # From inside the claim's own worktree, as a session with a DIFFERENT pid (an agent's fresh shell).
140
+ out="$( cd "$WT7" && KIT_SESSION_PID="$SESS_B" wt_start 7 2>/dev/null )"; rc=$?
141
+ t "re-run from the claim's own worktree succeeds" "$rc" "0"
142
+ t "re-run from the claim's own worktree prints the same line" "$out" "$WT7|task/7-claimed-thing|7"
143
+ t "re-run from the claim's own worktree leaves the claim untouched" "$(cat "$REG/issue-7.json")" "$before"
144
+ t "re-run added no worktree" "$(worktrees)" "3"
145
+
146
+ # ── 5. `cckit claims` lists the live claims and writes nothing ─────────────────────────────────
147
+ snap() { ( cd "$REG" && ls -la . && cat issue-*.json ) 2>/dev/null; }
148
+ before="$(snap)"
149
+ human="$( cd "$REPO" && "$ROOT/bin/cckit" claims 2>/dev/null )"; rc=$?
150
+ t "cckit claims exits 0" "$rc" "0"
151
+ has "cckit claims lists issue 7" "$human" "#7"
152
+ has "cckit claims lists the branch" "$human" "task/7-claimed-thing"
153
+ json="$( cd "$REPO" && "$ROOT/bin/cckit" claims --llm 2>/dev/null )"
154
+ t "cckit claims --llm is a JSON array of both live claims" "$(printf '%s' "$json" | jq -c '[.[].issue]' 2>/dev/null)" "[7,9]"
155
+ t "cckit claims --llm carries the pid" "$(printf '%s' "$json" | jq -r '.[0].pid' 2>/dev/null)" "$SESS_A"
156
+ t "cckit claims works from inside a worktree" "$( cd "$WT7" && "$ROOT/bin/cckit" claims --llm 2>/dev/null | jq -c '[.[].issue]' )" "[7,9]"
157
+ t "cckit claims writes nothing" "$(snap)" "$before"
158
+
159
+ # ── 3. a dead session's claim is reaped and the start proceeds ─────────────────────────────────
160
+ kill "$SESS_A" 2>/dev/null; wait "$SESS_A" 2>/dev/null; DEAD="$SESS_A"; SESS_A=""
161
+ before="$(snap)"
162
+ t "cckit claims hides a dead session's claim" "$( cd "$REPO" && "$ROOT/bin/cckit" claims --llm 2>/dev/null | jq -c '[.[].issue]' )" "[9]"
163
+ t "cckit claims does not reap it (read-only)" "$(snap)" "$before"
164
+ _wt_session_owns "$REPO" "$WT7"; t "_wt_session_owns is false once the session died" "$?" "1"
165
+ out="$(start_as "$SESS_B" 7 2>/dev/null)"; rc=$?
166
+ t "start over a dead session's claim proceeds" "$rc" "0"
167
+ t "the dead claim was replaced by the new session's" "$(jq -r .pid "$REG/issue-7.json")" "$SESS_B"
168
+ t "the dead pid is gone from the registry" "$(grep -l "\"pid\": *$DEAD\b" "$REG"/issue-*.json 2>/dev/null | wc -l | tr -d ' ')" "0"
169
+
170
+ # A claim whose worktree was removed (the PR merged, cleanup ran) is dead even with a live pid,
171
+ # once it is older than the grace that covers the claim→`worktree add` window.
172
+ jq -n --argjson pid "$SESS_B" --arg host "$(hostname)" --arg wt "$tmp/gone" --argjson started "$(( $(date +%s) - 4000 ))" \
173
+ '{issue:11, branch:"task/11-x", worktree:$wt, cwd:$wt, pid:$pid, host:$host, started:$started}' > "$REG/issue-11.json"
174
+ start_as "$$" 11 >/dev/null 2>&1; rc=$?
175
+ t "a claim whose worktree is long gone does not block" "$rc" "0"
176
+
177
+ # ── a failed start does not leave a claim behind ───────────────────────────────────────────────
178
+ # `git worktree add -B` fails when the branch is checked out in the main checkout.
179
+ git -C "$REPO" checkout -q -b task/12-claimed-thing
180
+ start_as "$SESS_B" 12 >/dev/null 2>&1; rc=$?
181
+ t "a start whose worktree add fails returns 1" "$rc" "1"
182
+ t "…and releases the claim it had just taken" "$([ -e "$REG/issue-12.json" ] && echo yes || echo no)" "no"
183
+ git -C "$REPO" checkout -q main
184
+
185
+ # ── 7. the CLI reports the ISSUE, not the flag ─────────────────────────────────────────────────
186
+ json="$( cd "$REPO" && KIT_SESSION_PID="$$" "$ROOT/bin/cckit" start --force 9 --llm 2>/dev/null )"
187
+ t "cckit start --force N --llm reports issue N" "$(printf '%s' "$json" | jq -r '.issue' 2>/dev/null)" "9"
188
+ t "cckit start --force N --llm is ok" "$(printf '%s' "$json" | jq -r '.ok' 2>/dev/null)" "true"
189
+ json="$( cd "$REPO" && KIT_SESSION_PID="$SESS_B" "$ROOT/bin/cckit" start 9 --llm 2>/dev/null )"; rc=$?
190
+ t "cckit start on a claimed issue exits 1" "$rc" "1"
191
+ t "cckit start on a claimed issue reports ok:false" "$(printf '%s' "$json" | jq -r '.ok' 2>/dev/null)" "false"
192
+ err="$( cd "$REPO" && KIT_SESSION_PID="$SESS_B" "$ROOT/bin/cckit" start 9 2>&1 >/dev/null )"; rc=$?
193
+ t "cckit start (human mode) on a claimed issue exits 1" "$rc" "1"
194
+ has "cckit start (human mode) prints the refusal" "$err" "refused: a live session already claims this issue"
195
+
196
+ [ "$fail" -eq 0 ] && echo "ALL OK (worktree-start)" || echo "worktree-start: FAILURES"
197
+ exit "$fail"
@@ -4,7 +4,10 @@
4
4
  # Family 1 of kit-engine-boundary.md (rule #1/#2): one bash home for the op, consumed by the
5
5
  # kit-task-start skill, scripts/orchestrate.sh, and `kit task start`. No second implementation.
6
6
  #
7
- # wt_start <issue-number> [slug-override]
7
+ # wt_start [--force] <issue-number> [slug-override]
8
+ # Refuses (rc 1, before any fetch / branch / worktree) when another LIVE session already holds
9
+ # the issue's claim in <git-common-dir>/kit-sessions (session-registry.sh). `--force` or
10
+ # KIT_FORCE=1 starts anyway and takes the claim over.
8
11
  # stdout: "<worktree-path>|<branch>|<issue-number>" (one line, machine-readable)
9
12
  # stderr: human progress
10
13
  # returns: 0 on success (created or reused), 1 on failure
@@ -21,6 +24,31 @@ if ! command -v cckit_gh_rest >/dev/null 2>&1; then
21
24
  fi
22
25
  command -v cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
23
26
 
27
+ # The claims registry (#384). Guarded: a project scaffolded before session-registry.sh shipped has
28
+ # worktree-start.sh without it, and there wt_start runs with no claim check rather than failing.
29
+ if ! command -v sreg_claim >/dev/null 2>&1; then
30
+ _sr_lib="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd)/session-registry.sh"
31
+ # shellcheck source=/dev/null
32
+ [ -f "$_sr_lib" ] && . "$_sr_lib"
33
+ unset _sr_lib
34
+ fi
35
+
36
+ # The closed kind: vocabulary (kinds.sh). The issue's `kind:` label becomes the branch prefix, so it
37
+ # is mapped through the set: a label outside it (kind:bug, kind:docs, a typo) would otherwise produce
38
+ # a branch the hygiene hook rejects as non-conventional.
39
+ if ! command -v kit_kind_branch >/dev/null 2>&1; then
40
+ _wtk_lib="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd)/kinds.sh"
41
+ # shellcheck source=/dev/null
42
+ [ -f "$_wtk_lib" ] && . "$_wtk_lib"
43
+ unset _wtk_lib
44
+ fi
45
+ # _wt_kind_prefix <kind> — the branch prefix for a kind label value. With kinds.sh loaded: the value
46
+ # itself when it is in the closed set, else `task` plus a warning on stderr. Without kinds.sh (a
47
+ # project scaffolded before it shipped) the value passes through unchanged, as it always did.
48
+ _wt_kind_prefix() {
49
+ if command -v kit_kind_branch >/dev/null 2>&1; then kit_kind_branch "${1:-}"; else printf '%s\n' "${1:-task}"; fi
50
+ }
51
+
24
52
  WT_START_REPO="${WT_START_REPO:-${KIT_REPO:-}}"
25
53
 
26
54
  # _wt_set_port <app-env-file> <port> <issue-num> — append a per-worktree dev PORT to an app's
@@ -31,7 +59,8 @@ WT_START_REPO="${WT_START_REPO:-${KIT_REPO:-}}"
31
59
  # file. A script like `next dev -p ${PORT:-3003}` is expanded by the npm-script shell, which never
32
60
  # reads .env.local, so the value is INERT for it and every worktree falls back to the same default.
33
61
  # cckit therefore does not RELY on it: everything cckit launches itself passes the port explicitly
34
- # (scripts/lib/qa-lane.sh injects one QA_PORT_<service> per service). Treat the .env.local write as
62
+ # (scripts/lib/qa-lane.sh exports each port env var the project declared in `qa.ports`, set to that
63
+ # lane's port, into the command it launches). Treat the .env.local write as
35
64
  # a convenience for apps that do read it — and if you start a dev server by hand in a worktree,
36
65
  # check which port you actually got.
37
66
  _wt_set_port() {
@@ -55,6 +84,16 @@ _wt_set_port() {
55
84
  # State lives beside the event log under the git-common-dir, so it is worktree-durable and shared by
56
85
  # every checkout. Each line: <issue>\t<slot>\t<worktree-path>. A slot whose worktree is gone is
57
86
  # reclaimed on the next call, which is what keeps the 40 slots from leaking away over a long project.
87
+ # _wt_num_hash <key> — <key> itself when it is a number, else a number derived from it. The slot
88
+ # ledger is keyed by issue number; a light task (#450) is keyed by its id (`t3f9a`), and the hashed
89
+ # fallbacks below do arithmetic on the key.
90
+ _wt_num_hash() {
91
+ case "$1" in
92
+ ''|*[!0-9]*) printf '%s' "$1" | cksum | cut -d' ' -f1 ;;
93
+ *) printf '%s' "$1" ;;
94
+ esac
95
+ }
96
+
58
97
  _wt_slots_file() {
59
98
  local gcd
60
99
  gcd="$(git -C "${1:-.}" rev-parse --git-common-dir 2>/dev/null)" || return 1
@@ -68,34 +107,46 @@ _wt_slots_file() {
68
107
  # very defect the slot ledger exists to remove. The reclaim rewrite races worse than the append:
69
108
  # it `mv`s a snapshot over the ledger, silently dropping any row another process added in between.
70
109
  #
71
- # mkdir is the lock primitive. A stock macOS has no flock(1) and this file targets bash 3.2, while
72
- # `set -C` redirection is not atomic on every filesystem; mkdir is atomic everywhere and fails when
73
- # the name already exists.
110
+ # The lock primitive is the shell's own noclobber redirection: `set -C; : > file` opens with
111
+ # O_CREAT|O_EXCL, which the kernel makes exclusive, and no external tool is involved. A stock macOS
112
+ # has no flock(1) and this file targets bash 3.2.
113
+ #
114
+ # It was `mkdir` until #449. `mkdir` is exclusive only when the binary makes ONE mkdir(2) call and
115
+ # reports its EEXIST. uutils coreutils 0.10.0 (the `mkdir` of Ubuntu 26.04, and of GitHub's
116
+ # `ubuntu-latest` runner) calls statx first and exits 0 for the loser of a race: 8 processes running
117
+ # `mkdir <same path>` at once all "succeeded" in 183 of 200 rounds. Two `cckit start` runs then held
118
+ # this lock together and were given the same slot.
74
119
  _WT_LOCK_WAIT="${_WT_LOCK_WAIT:-10}" # seconds to wait for a held lock before giving up
75
120
 
76
- # _wt_lock <lockdir> — 0 when the lock is held, 1 when it is not. A caller that fails to lock
121
+ # _wt_lock_try <lockfile> — ONE attempt: 0 when this call created the lock, 1 when it exists. The
122
+ # subshell keeps noclobber out of the caller's shell. Never replace this with an external command
123
+ # without racing it first (scripts/lib/worktree-ports-test.sh section 7 does).
124
+ _wt_lock_try() { ( set -C; : > "$1" ) 2>/dev/null; }
125
+
126
+ # _wt_lock <lockfile> — 0 when the lock is held, 1 when it is not. A caller that fails to lock
77
127
  # proceeds ANYWAY: an unserialized allocation can collide, but a `cckit start` that refuses to run
78
128
  # cannot be worked around. Degrade, don't block.
79
129
  #
80
- # A lock directory older than a minute is treated as abandoned and broken, so a process killed
130
+ # A lock older than a minute is treated as abandoned and broken, so a process killed
81
131
  # mid-allocation cannot wedge every future start. The age floor is well above _WT_LOCK_WAIT, so a
82
- # slow-but-live holder is never stolen from.
132
+ # slow-but-live holder is never stolen from. `rm -rf` because a cckit from before #449 leaves a
133
+ # lock DIRECTORY at the same path; either kind blocks the other, so mixed versions still exclude.
83
134
  _wt_lock() {
84
135
  local d="$1" tries=0 max
85
136
  max=$(( _WT_LOCK_WAIT * 10 ))
86
137
  while [[ "$tries" -lt "$max" ]]; do
87
- mkdir "$d" 2>/dev/null && return 0
138
+ _wt_lock_try "$d" && return 0
88
139
  sleep 0.1 2>/dev/null || sleep 1
89
140
  tries=$(( tries + 1 ))
90
141
  done
91
142
  if [[ -n "$(find "$d" -maxdepth 0 -mmin +1 2>/dev/null)" ]]; then
92
143
  rm -rf "$d" 2>/dev/null
93
- mkdir "$d" 2>/dev/null && { echo "warn: broke an abandoned port-slot lock ($d)" >&2; return 0; }
144
+ _wt_lock_try "$d" && { echo "warn: broke an abandoned port-slot lock ($d)" >&2; return 0; }
94
145
  fi
95
146
  return 1
96
147
  }
97
148
 
98
- _wt_unlock() { rmdir "$1" 2>/dev/null || rm -rf "$1" 2>/dev/null; }
149
+ _wt_unlock() { rm -rf "$1" 2>/dev/null; }
99
150
 
100
151
  # _wt_slot_alloc <ledger> <issue-num> <worktree> — the ledger transaction: reclaim dead rows,
101
152
  # return the issue's existing allocation if it has one, else take the lowest free slot and append
@@ -125,7 +176,7 @@ _wt_slot_alloc() {
125
176
  i=$(( i + 1 ))
126
177
  done
127
178
  echo "[#$num] warn: all 40 dev-port slots are held — falling back to a hashed port (collisions possible); run cckit gc" >&2
128
- printf '%s' $(( num % 40 ))
179
+ printf '%s' $(( $(_wt_num_hash "$num") % 40 ))
129
180
  }
130
181
 
131
182
  # _wt_slot_for <root> <issue-num> <worktree> — echo this issue's slot (0-39), allocating on first
@@ -134,8 +185,10 @@ _wt_slot_alloc() {
134
185
  # a warning) only when every slot is genuinely held.
135
186
  _wt_slot_for() {
136
187
  local root="$1" num="$2" wt="$3" f lock out locked=0
137
- f="$(_wt_slots_file "$root")" || { printf '%s' $(( num % 40 )); return 0; }
138
- [[ -f "$f" ]] || : > "$f" 2>/dev/null || { printf '%s' $(( num % 40 )); return 0; }
188
+ f="$(_wt_slots_file "$root")" || { printf '%s' $(( $(_wt_num_hash "$num") % 40 )); return 0; }
189
+ # `>>`, not `>`: this line runs BEFORE the lock, so a truncating create could empty a ledger that
190
+ # another process created and wrote to between the test and the redirection.
191
+ [[ -f "$f" ]] || : >> "$f" 2>/dev/null || { printf '%s' $(( $(_wt_num_hash "$num") % 40 )); return 0; }
139
192
 
140
193
  lock="$f.lock"
141
194
  if _wt_lock "$lock"; then locked=1; else
@@ -146,6 +199,22 @@ _wt_slot_for() {
146
199
  printf '%s' "$out"
147
200
  }
148
201
 
202
+ # wt_port_slot <root> <issue-num> <worktree> — the PUBLIC reader of the slot ledger: echo the slot
203
+ # (0-39) this issue holds, allocating one on first call. Same row `cckit start` uses, so a caller
204
+ # that launches something for an issue's worktree (the QA lane, qa-lane.sh) lands on the port block
205
+ # that worktree already owns instead of deriving a second one.
206
+ wt_port_slot() { _wt_slot_for "$1" "$2" "$3"; }
207
+
208
+ # wt_port_stride <config-file> — the distance between one slot's port block and the next:
209
+ # `worktree.devPortStride`, default 10. One reader, so wt_assign_ports and the QA lane cannot
210
+ # disagree about where the next block starts.
211
+ wt_port_stride() {
212
+ local stride=""
213
+ stride="$(jq -r '.worktree.devPortStride // empty' "${1:-}" 2>/dev/null)"
214
+ [[ "$stride" =~ ^[0-9]+$ && "$stride" -gt 0 ]] || stride=10
215
+ printf '%s' "$stride"
216
+ }
217
+
149
218
  wt_assign_ports() {
150
219
  # `apppath`, not `path`: under zsh `path` is tied to PATH (special array); a bare `path` local
151
220
  # would clobber the command search path on assignment. A namespaced name is inert.
@@ -168,8 +237,7 @@ wt_assign_ports() {
168
237
  # different worktrees. Real example (bases 3001/3003/3004, n=3): worktree offset k+1's admin port
169
238
  # (3004+3k) IS worktree offset k's api port. Configurable as `worktree.devPortStride`; the default
170
239
  # leaves room for up to 10 services per project.
171
- stride="$(jq -r '.worktree.devPortStride // empty' "$cfg" 2>/dev/null)"
172
- [[ "$stride" =~ ^[0-9]+$ && "$stride" -gt 0 ]] || stride=10
240
+ stride="$(wt_port_stride "$cfg")"
173
241
 
174
242
  # The invariant that keeps blocks disjoint: every base must sit inside ONE stride-wide window.
175
243
  # Warn rather than fail — the ports still work, they just stop being collision-proof, and a
@@ -180,7 +248,7 @@ wt_assign_ports() {
180
248
  fi
181
249
 
182
250
  offset="$(_wt_slot_for "$root" "$num" "$wt")"
183
- [[ "$offset" =~ ^[0-9]+$ ]] || offset=$(( num % 40 ))
251
+ [[ "$offset" =~ ^[0-9]+$ ]] || offset=$(( $(_wt_num_hash "$num") % 40 ))
184
252
  i=0
185
253
  while [[ "$i" -lt "$n" ]]; do
186
254
  apppath="$(jq -r ".[$i].path // empty" <<<"$ports")"
@@ -216,7 +284,7 @@ _wt_json_has() {
216
284
  if command -v jq >/dev/null 2>&1; then
217
285
  jq -e "$filter" "$file" >/dev/null 2>&1
218
286
  else
219
- tr -d ' \n\t' < "$file" 2>/dev/null | grep -qE "$re"
287
+ grep -qE "$re" <<<"$(tr -d ' \n\t' < "$file" 2>/dev/null)"
220
288
  fi
221
289
  }
222
290
 
@@ -423,7 +491,7 @@ wt_bootstrap() {
423
491
  # • no LIVE session owns it (kit-sessions registry: no live pid sitting in that dir)
424
492
 
425
493
  # _wt_mtime <path> — file mtime as epoch seconds (BSD stat, then GNU stat). Picks the oldest tree.
426
- _wt_mtime() { stat -f %m "$1" 2>/dev/null || stat -c %Y "$1" 2>/dev/null; }
494
+ _wt_mtime() { stat -c %Y "$1" 2>/dev/null || stat -f %m "$1" 2>/dev/null; }
427
495
 
428
496
  # _wt_list <root> — emit one "<path>\t<branch-or-->\t<locked:0|1>" line per worktree (porcelain).
429
497
  _wt_list() {
@@ -457,7 +525,8 @@ _wt_is_clean() {
457
525
  }
458
526
 
459
527
  # _wt_session_owns <root> <path> — true when a LIVE Claude session sits in <path> (or a subdir),
460
- # per the kit-sessions registry (.git/kit-sessions/*.json, written by session-registry.sh). A dead
528
+ # per the kit-sessions registry (.git/kit-sessions/*.json — wt_start writes one claim per issue
529
+ # through session-registry.sh, with `cwd` set to the worktree). A dead
461
530
  # pid is not an owner. No registry → no known live owner (returns false).
462
531
  _wt_session_owns() {
463
532
  local root="$1" wt_path="$2" common reg f opid ocwd
@@ -500,7 +569,7 @@ _wt_pool_find() {
500
569
  # mutates). Shows branch / merged-into-develop? / clean? / live-session? / overall reusable?.
501
570
  wt_pool_status() {
502
571
  local root wtdir wt_path branch locked merged clean owned reusable any=0
503
- root="$(git worktree list --porcelain | awk '/^worktree /{print $2; exit}')"
572
+ root="$(git worktree list --porcelain | awk '/^worktree / && !hit {print $2; hit=1}')"
504
573
  [[ -n "$root" ]] || { echo "wt_pool_status: not in a git repo" >&2; return 1; }
505
574
  wtdir="$root/.claude/worktrees/"
506
575
  git -C "$root" fetch origin "${KIT_BASE_BRANCH:-main}" --quiet 2>/dev/null || true
@@ -522,15 +591,46 @@ wt_pool_status() {
522
591
  [[ "$any" == "1" ]] || echo "(no pooled worktrees under $wtdir)"
523
592
  }
524
593
 
594
+ # _wt_claim_desc <tsv-row> — one human line for a claim row from sreg_issue_claim.
595
+ _wt_claim_desc() {
596
+ local c_issue c_branch c_wt c_pid c_host c_started
597
+ IFS=$'\t' read -r c_issue c_branch c_wt c_pid c_host c_started <<EOF
598
+ $1
599
+ EOF
600
+ printf 'pid %s on %s, worktree %s' "$c_pid" "$c_host" "$c_wt"
601
+ }
602
+
525
603
  wt_start() {
526
- local num="${1:-}" slug_override="${2:-}" root meta title kind slug branch wt reused cand
604
+ local num="" slug_override="" root meta title kind slug branch wt reused cand
605
+ local force="${KIT_FORCE:-0}" _arg _claim _claimed="" _crc
606
+ for _arg in "$@"; do
607
+ case "$_arg" in
608
+ --force) force=1 ;;
609
+ -*) echo "wt_start: unknown flag '$_arg' (usage: start [--force] <issue> [slug])" >&2; return 1 ;;
610
+ *) if [[ -z "$num" ]]; then num="$_arg"; elif [[ -z "$slug_override" ]]; then slug_override="$_arg"; fi ;;
611
+ esac
612
+ done
527
613
  [[ -n "$num" ]] || { echo "wt_start: issue number required" >&2; return 1; }
528
614
 
529
- root="$(git worktree list --porcelain | awk '/^worktree /{print $2; exit}')"
615
+ root="$(git worktree list --porcelain | awk '/^worktree / && !hit {print $2; hit=1}')"
530
616
  [[ -n "$root" ]] || { echo "wt_start: not in a git repo" >&2; return 1; }
531
617
 
532
- # The board's Status is set server-side by built-in automations (Item closed→Done, PR
533
- # merged→Done) — wt_start no longer writes In Progress, so no board helpers are loaded here.
618
+ # Claim check (#384) — BEFORE the issue read, the fetch and `git worktree add`, so a refusal
619
+ # makes no network call, no branch and no worktree. A claim this session already holds (same
620
+ # session pid, or the caller stands inside the claimed worktree) is not a collision: re-running
621
+ # start stays the no-op it always was.
622
+ if command -v sreg_issue_claim >/dev/null 2>&1 && _claim="$(sreg_issue_claim "$root" "$num")" \
623
+ && ! sreg_claim_mine "$root" "$num"; then
624
+ if [[ "$force" != "1" ]]; then
625
+ echo "[#$num] refused: a live session already claims this issue — $(_wt_claim_desc "$_claim")" >&2
626
+ echo "[#$num] see 'cckit claims'; 'cckit start --force $num' (or KIT_FORCE=1) starts anyway and takes the claim." >&2
627
+ return 1
628
+ fi
629
+ echo "[#$num] ⚠ --force: taking over the claim of a live session — $(_wt_claim_desc "$_claim")" >&2
630
+ fi
631
+
632
+ # The board helpers load lazily, further down: Status is otherwise set server-side by built-in
633
+ # automations (Item closed→Done, PR merged→Done), and In Progress is the one write wt_start owns.
534
634
 
535
635
  if cckit_gh_rest; then
536
636
  meta="$(gh_rest_issue_json "$WT_START_REPO" "$num" 2>/dev/null)" || { echo "[#$num] issue not found" >&2; return 1; }
@@ -540,6 +640,7 @@ wt_start() {
540
640
  fi
541
641
  title="$(echo "$meta" | jq -r '.title')"
542
642
  kind="$(echo "$meta" | jq -r '([.labels[].name | select(startswith("kind:"))][0] // "kind:task") | sub("^kind:";"")')"
643
+ kind="$(_wt_kind_prefix "$kind")"
543
644
  if [[ -n "$slug_override" ]]; then
544
645
  slug="$slug_override"
545
646
  else
@@ -552,8 +653,26 @@ wt_start() {
552
653
  branch="$kind/$num-$slug"
553
654
  wt="$root/.claude/worktrees/${kind}+${num}-${slug}"
554
655
 
656
+ # Take the claim now that the worktree path is known, still before the fetch. rc 3 = another
657
+ # session claimed the issue between the check above and here. An unwritable registry only warns:
658
+ # the registry is a guard, not a precondition for working.
659
+ if command -v sreg_claim >/dev/null 2>&1; then
660
+ # _claimed marks a claim THIS call writes (none before, or one taken over by force), so a
661
+ # failed `worktree add` below releases only that — never a claim the session already held.
662
+ sreg_claim_mine "$root" "$num" || _claimed="1"
663
+ # `&& … ||` so a caller running under `set -e` (bin/cckit) reaches the refusal message.
664
+ sreg_claim "$root" "$num" "$branch" "$wt" "$force" && _crc=0 || _crc=$?
665
+ if [[ "$_crc" -eq 3 ]]; then
666
+ echo "[#$num] refused: another session claimed this issue just now — see 'cckit claims'." >&2
667
+ return 1
668
+ elif [[ "$_crc" -ne 0 ]]; then
669
+ _claimed=""
670
+ echo "[#$num] ⚠ could not record the session claim; continuing without one." >&2
671
+ fi
672
+ fi
673
+
555
674
  git -C "$root" fetch origin "${KIT_BASE_BRANCH:-main}" --quiet
556
- if git -C "$root" worktree list --porcelain | grep -q "/${kind}+${num}-${slug}$"; then
675
+ if grep -q "/${kind}+${num}-${slug}$" <<<"$(git -C "$root" worktree list --porcelain)"; then
557
676
  echo "[#$num] reusing worktree $wt" >&2
558
677
  else
559
678
  reused=""
@@ -575,7 +694,8 @@ wt_start() {
575
694
  fi
576
695
  if [[ -z "$reused" ]]; then
577
696
  git -C "$root" worktree add -B "$branch" "$wt" origin/${KIT_BASE_BRANCH:-main} >/dev/null 2>&1 \
578
- || { echo "[#$num] worktree add failed (branch $branch may exist elsewhere)" >&2; return 1; }
697
+ || { if [[ -n "$_claimed" ]]; then sreg_release "$root" "$num" 2>/dev/null || true; fi
698
+ echo "[#$num] worktree add failed (branch $branch may exist elsewhere)" >&2; return 1; }
579
699
  echo "[#$num] created worktree $wt (branch $branch)" >&2
580
700
  fi
581
701
  fi
@@ -595,12 +715,14 @@ wt_start() {
595
715
  command -v cckit_board_skipped >/dev/null 2>&1 && cckit_board_skipped
596
716
  elif source "$root/scripts/lib/gh-project.sh" 2>/dev/null; then
597
717
  [[ -n "${STATUS_FIELD_ID:-}" ]] || load_project_ids >/dev/null 2>&1 || true
598
- # Claim precheck (#124): if the issue is ALREADY In Progress on the board, another session may
599
- # already own it — warn (never block; the start still proceeds) so two agents don't collide.
718
+ # Board precheck (#124, #384): an issue ALREADY In Progress or In Review may belong to someone
719
+ # the local claims registry cannot see (a teammate's machine). Warn only — wt_start writes In
720
+ # Progress itself and nothing resets it when a session dies, so refusing on board Status would
721
+ # lock out every restart after a crash. The refusal is the live claim, checked at the top.
600
722
  if command -v project_issue_status >/dev/null 2>&1; then
601
723
  local _prev_status; _prev_status="$(project_issue_status "$num" 2>/dev/null || true)"
602
- [[ "$_prev_status" == "In Progress" ]] \
603
- && echo "[#$num] ⚠ already In Progress on the board — another session may own this issue; continuing." >&2
724
+ [[ "$_prev_status" == "In Progress" || "$_prev_status" == "In Review" ]] \
725
+ && echo "[#$num] ⚠ already $_prev_status on the board — another session may own this issue; continuing." >&2
604
726
  fi
605
727
  item="$(project_find_item_by_issue "$num" 2>/dev/null)"
606
728
  if [[ -z "$item" ]]; then