@jeiemgi/cckit 0.8.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 (309) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +169 -21
  3. package/README.md +505 -35
  4. package/SECURITY.md +5 -0
  5. package/bin/cckit +189 -20
  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/guard-raw-git-ops-test.sh +8 -0
  59. package/scripts/hook-adapter-test.sh +193 -0
  60. package/scripts/init-content-test.sh +225 -0
  61. package/scripts/init-guided-test.sh +474 -0
  62. package/scripts/init-recipes-test.sh +104 -0
  63. package/scripts/init-upgrade-test.sh +341 -2
  64. package/scripts/init.sh +746 -95
  65. package/scripts/instruction-budget-baseline.txt +8 -0
  66. package/scripts/instruction-budget-lint-test.sh +166 -0
  67. package/scripts/instruction-budget-lint.sh +219 -0
  68. package/scripts/integrations-check-test.sh +200 -0
  69. package/scripts/integrations-check.sh +157 -0
  70. package/scripts/kit-add-test.sh +214 -0
  71. package/scripts/kit-add.sh +305 -5
  72. package/scripts/kit-adopt-test.sh +16 -0
  73. package/scripts/kit-adopt.sh +3 -2
  74. package/scripts/kit-digest.sh +2 -2
  75. package/scripts/kit-doctor-test.sh +293 -0
  76. package/scripts/kit-doctor.sh +272 -140
  77. package/scripts/kit-export-project-test.sh +3 -3
  78. package/scripts/kit-remove-test.sh +19 -0
  79. package/scripts/kit-routines.sh +2 -2
  80. package/scripts/kit-self-update.sh +1 -1
  81. package/scripts/kit-version-check-test.sh +4 -0
  82. package/scripts/kit-version-check.sh +4 -2
  83. package/scripts/knowledge-lint.sh +4 -4
  84. package/scripts/leak-fence-test.sh +89 -0
  85. package/scripts/lib/adapter-select-test.sh +136 -0
  86. package/scripts/lib/adapter-select.sh +158 -0
  87. package/scripts/lib/agent-def-test.sh +69 -0
  88. package/scripts/lib/agent-def.sh +106 -0
  89. package/scripts/lib/agent-launch-test.sh +212 -0
  90. package/scripts/lib/agent-launch.sh +335 -0
  91. package/scripts/lib/board-view-test.sh +11 -0
  92. package/scripts/lib/board-view.sh +42 -26
  93. package/scripts/lib/branch-owner-test.sh +26 -0
  94. package/scripts/lib/branch-owner.sh +16 -9
  95. package/scripts/lib/captain-test.sh +105 -0
  96. package/scripts/lib/captain.sh +87 -8
  97. package/scripts/lib/codex-hooks-test.sh +100 -0
  98. package/scripts/lib/codex-hooks.sh +69 -0
  99. package/scripts/lib/commitlint.sh +1 -1
  100. package/scripts/lib/content-kinds-gc-test.sh +88 -0
  101. package/scripts/lib/copilot-test.sh +70 -4
  102. package/scripts/lib/copilot.sh +66 -14
  103. package/scripts/lib/effort-dispatch-test.sh +3 -3
  104. package/scripts/lib/effort-metrics-test.sh +178 -1
  105. package/scripts/lib/effort-metrics.sh +188 -35
  106. package/scripts/lib/effort-ops-test.sh +217 -7
  107. package/scripts/lib/effort-ops.sh +281 -49
  108. package/scripts/lib/effort-parity-test.sh +49 -13
  109. package/scripts/lib/effort-plan-test.sh +337 -0
  110. package/scripts/lib/effort-plan.sh +200 -23
  111. package/scripts/lib/effort-slug.sh +16 -2
  112. package/scripts/lib/effort.sh +67 -12
  113. package/scripts/lib/errors-header-test.sh +1 -1
  114. package/scripts/lib/gh-project-test.sh +35 -1
  115. package/scripts/lib/gh-project.sh +37 -16
  116. package/scripts/lib/gh-rest-test.sh +330 -0
  117. package/scripts/lib/gh-rest.sh +304 -0
  118. package/scripts/lib/hook-refresh.sh +1 -1
  119. package/scripts/lib/init-github.sh +412 -0
  120. package/scripts/lib/init-guide.sh +630 -0
  121. package/scripts/lib/integration-install-test.sh +343 -0
  122. package/scripts/lib/integration-install.sh +257 -0
  123. package/scripts/lib/integrations-test.sh +366 -0
  124. package/scripts/lib/integrations.sh +244 -0
  125. package/scripts/lib/kinds-test.sh +125 -0
  126. package/scripts/lib/kinds.sh +66 -0
  127. package/scripts/lib/kit-brief-test.sh +22 -3
  128. package/scripts/lib/kit-brief.sh +45 -5
  129. package/scripts/lib/kit-config-resolve.sh +2 -2
  130. package/scripts/lib/kit-config-test.sh +64 -1
  131. package/scripts/lib/kit-config.sh +37 -1
  132. package/scripts/lib/kit-events.sh +7 -0
  133. package/scripts/lib/kit-gc-test.sh +173 -1
  134. package/scripts/lib/kit-gc.sh +244 -37
  135. package/scripts/lib/kit-hotfix.sh +216 -0
  136. package/scripts/lib/kit-interview-test.sh +93 -0
  137. package/scripts/lib/kit-interview.sh +117 -8
  138. package/scripts/lib/kit-lib-test.sh +1 -1
  139. package/scripts/lib/kit-lib.sh +3 -3
  140. package/scripts/lib/kit-local.sh +2 -2
  141. package/scripts/lib/kit-manifest.sh +33 -1
  142. package/scripts/lib/kit-profile-test.sh +3 -3
  143. package/scripts/lib/kit-status-test.sh +112 -4
  144. package/scripts/lib/kit-status.sh +246 -16
  145. package/scripts/lib/kit-task-ops-test.sh +37 -0
  146. package/scripts/lib/kit-task-ops.sh +362 -89
  147. package/scripts/lib/kto-hotfix-test.sh +232 -0
  148. package/scripts/lib/kto-task-close-test.sh +83 -0
  149. package/scripts/lib/kto-task-new-test.sh +95 -0
  150. package/scripts/lib/kto-task-pr-merge-test.sh +97 -6
  151. package/scripts/lib/kto-task-pr-test.sh +70 -0
  152. package/scripts/lib/leak-fence.sh +89 -0
  153. package/scripts/lib/light-task-run-test.sh +255 -0
  154. package/scripts/lib/light-task-test.sh +309 -0
  155. package/scripts/lib/light-task.sh +706 -0
  156. package/scripts/lib/next-test.sh +99 -0
  157. package/scripts/lib/next.sh +9 -3
  158. package/scripts/lib/orchestration-runtime-test.sh +72 -0
  159. package/scripts/lib/orchestration-runtime.sh +57 -5
  160. package/scripts/lib/plan-machine-test.sh +15 -2
  161. package/scripts/lib/plan-machine.sh +20 -7
  162. package/scripts/lib/plan-next-test.sh +1 -1
  163. package/scripts/lib/plan-next.sh +4 -4
  164. package/scripts/lib/pr-evidence-test.sh +3 -3
  165. package/scripts/lib/pr-evidence.sh +1 -1
  166. package/scripts/lib/qa-lane-test.sh +626 -0
  167. package/scripts/lib/qa-lane.sh +1357 -0
  168. package/scripts/lib/qa-lanes-test.sh +175 -0
  169. package/scripts/lib/react-detect.sh +1 -1
  170. package/scripts/lib/render-test.sh +1 -1
  171. package/scripts/lib/review-page-test.sh +318 -0
  172. package/scripts/lib/review-page.sh +369 -0
  173. package/scripts/lib/review-stage-test.sh +62 -11
  174. package/scripts/lib/review-stage.sh +42 -60
  175. package/scripts/lib/role-identity-test.sh +65 -0
  176. package/scripts/lib/role-identity.sh +25 -2
  177. package/scripts/lib/secret-guard.sh +3 -3
  178. package/scripts/lib/session-registry-test.sh +111 -0
  179. package/scripts/lib/session-registry.sh +233 -0
  180. package/scripts/lib/stage-receipt-test.sh +14 -1
  181. package/scripts/lib/stage-receipt.sh +13 -8
  182. package/scripts/lib/task-source-file.sh +309 -0
  183. package/scripts/lib/task-source.sh +130 -0
  184. package/scripts/lib/test-env-test.sh +3 -3
  185. package/scripts/lib/test-env.sh +7 -4
  186. package/scripts/lib/upgrade-report-test.sh +120 -0
  187. package/scripts/lib/upgrade-report.sh +138 -0
  188. package/scripts/lib/version-bump.sh +3 -3
  189. package/scripts/lib/wave-result-test.sh +80 -0
  190. package/scripts/lib/wave-result.sh +127 -0
  191. package/scripts/lib/worker-brief-test.sh +5 -0
  192. package/scripts/lib/worker-brief.sh +2 -2
  193. package/scripts/lib/worktree-issue-test.sh +30 -1
  194. package/scripts/lib/worktree-issue.sh +93 -9
  195. package/scripts/lib/worktree-ports-test.sh +57 -4
  196. package/scripts/lib/worktree-start-test.sh +197 -0
  197. package/scripts/lib/worktree-start.sh +169 -32
  198. package/scripts/lib/zsh-safety-test.sh +19 -3
  199. package/scripts/marketing-walkthrough-test.sh +152 -0
  200. package/scripts/night-grant-test.sh +37 -0
  201. package/scripts/night-grant.sh +448 -0
  202. package/scripts/night-run-test.sh +8 -0
  203. package/scripts/night-run.mjs +275 -0
  204. package/scripts/night-run.test.mjs +324 -0
  205. package/scripts/orchestrate-test.sh +190 -0
  206. package/scripts/orchestrate.sh +79 -8
  207. package/scripts/portable-test.sh +11 -2
  208. package/scripts/publish.sh +28 -3
  209. package/scripts/review-lint-test.sh +7 -7
  210. package/scripts/review-lint.sh +2 -2
  211. package/scripts/review-rules.conf +8 -0
  212. package/scripts/rule-recipes-test.sh +96 -0
  213. package/scripts/self-install-test.sh +39 -9
  214. package/scripts/setup-labels.sh +18 -1
  215. package/scripts/setup-milestones.sh +1 -1
  216. package/scripts/showcase.sh +1 -1
  217. package/scripts/skill-frontmatter-lint-test.sh +4 -4
  218. package/scripts/skill-frontmatter-lint.sh +1 -1
  219. package/scripts/status-test.sh +113 -2
  220. package/scripts/status.sh +16 -3
  221. package/scripts/task-sync-test.sh +154 -0
  222. package/scripts/task-sync.sh +243 -32
  223. package/scripts/test-runner-test.sh +158 -0
  224. package/scripts/test.sh +131 -23
  225. package/scripts/update-path-test.sh +279 -0
  226. package/scripts/ux-ds-audit.sh +1 -1
  227. package/scripts/ux-graph-scan.sh +6 -6
  228. package/scripts/wave-worker.sh +54 -0
  229. package/skills/kit-add/SKILL.md +77 -0
  230. package/skills/kit-autopilot/SKILL.md +123 -12
  231. package/skills/kit-cleanup/SKILL.md +5 -2
  232. package/skills/kit-contribute/SKILL.md +4 -4
  233. package/skills/kit-docs-writer/SKILL.md +16 -6
  234. package/skills/kit-effort-close/SKILL.md +17 -8
  235. package/skills/kit-effort-new/SKILL.md +15 -9
  236. package/skills/kit-effort-pr/SKILL.md +15 -7
  237. package/skills/kit-effort-start/SKILL.md +5 -2
  238. package/skills/kit-gc/SKILL.md +5 -2
  239. package/skills/kit-next/SKILL.md +5 -1
  240. package/skills/kit-onboard/SKILL.md +3 -3
  241. package/skills/kit-page/SKILL.md +93 -0
  242. package/skills/kit-plan-next/SKILL.md +8 -4
  243. package/skills/kit-security-sweep/SKILL.md +1 -3
  244. package/skills/kit-ship/SKILL.md +2 -3
  245. package/skills/kit-status/SKILL.md +35 -4
  246. package/skills/kit-task-new/SKILL.md +3 -3
  247. package/skills/kit-task-pr/SKILL.md +7 -2
  248. package/skills/kit-task-pr-merge/SKILL.md +7 -2
  249. package/skills/kit-task-start/SKILL.md +7 -7
  250. package/skills/kit-task-sync/SKILL.md +19 -3
  251. package/skills/kit-ux-audit/SKILL.md +3 -2
  252. package/templates/agents/auto-dev.md +5 -4
  253. package/templates/agents/backend.md +1 -1
  254. package/templates/agents/designer.md +2 -0
  255. package/templates/agents/devops.md +1 -1
  256. package/templates/agents/editor.md +9 -1
  257. package/templates/agents/frontend.md +1 -1
  258. package/templates/agents/local-delegate.md +1 -1
  259. package/templates/agents/pm.md +5 -5
  260. package/templates/agents/tech-lead.md +1 -1
  261. package/templates/benchmarks/SCHEMA.md +9 -3
  262. package/templates/codex/hooks.json.tmpl +89 -0
  263. package/templates/hooks/guard-base-branch-commit.sh.tmpl +13 -7
  264. package/templates/hooks/guard-raw-git-ops.sh.tmpl +27 -14
  265. package/templates/hooks/mempal_followup.sh.tmpl +2 -2
  266. package/templates/hooks/mempal_session_start.sh.tmpl +1 -1
  267. package/templates/hooks/prepush_gate.sh.tmpl +4 -2
  268. package/templates/hooks/repo-hygiene.sh.tmpl +3 -2
  269. package/templates/instructions.md.tmpl +174 -0
  270. package/templates/kit.config.json.tmpl +3 -3
  271. package/templates/knowledge-INDEX.md.tmpl +1 -1
  272. package/templates/lib/kit-sigil.sh.tmpl +1 -1
  273. package/templates/rules/authoring.md +106 -0
  274. package/templates/rules/branch-naming.md +40 -4
  275. package/templates/rules/code-retrieval.md +63 -0
  276. package/templates/rules/delegation-brief.md +0 -3
  277. package/templates/rules/design-routing.md +3 -3
  278. package/templates/rules/effort-model.md +14 -10
  279. package/templates/rules/gate-and-review.md +94 -0
  280. package/templates/rules/issue-lifecycle.md +88 -0
  281. package/templates/rules/knowledge-base.md +1 -1
  282. package/templates/rules/mempalace.md +1 -1
  283. package/templates/rules/merge-safety.md +108 -0
  284. package/templates/rules/naming-and-ids.md +23 -17
  285. package/templates/rules/plain-language.md +59 -0
  286. package/templates/rules/plan-next.md +9 -11
  287. package/templates/rules/react-annotate.md +6 -0
  288. package/templates/rules/review-pages.md +49 -0
  289. package/templates/rules/risk-tiered-review.md +13 -4
  290. package/templates/rules/shared-checkout.md +106 -0
  291. package/templates/rules/skill-gaps.md +2 -7
  292. package/templates/rules/task-management.md +13 -1
  293. package/templates/skills/brand-voice/SKILL.md +91 -0
  294. package/templates/skills/brand-voice/references/voice-template.md +61 -0
  295. package/templates/skills/campaign/SKILL.md +123 -0
  296. package/templates/skills/campaign/references/plan-template.md +55 -0
  297. package/templates/skills/concrete/SKILL.md +2 -2
  298. package/templates/skills/copywriting/SKILL.md +8 -1
  299. package/templates/skills/feature-build-refine/SKILL.md +2 -4
  300. package/templates/skills/morning-briefing/SKILL.md +2 -2
  301. package/templates/skills/page-design/SKILL.md +145 -0
  302. package/templates/skills/page-design/add.json +5 -0
  303. package/templates/skills/page-design/references/design-file-template.md +40 -0
  304. package/templates/skills/page-design/references/generic-page-patterns.md +43 -0
  305. package/templates/skills/page-design/references/minimum-rules.md +42 -0
  306. package/templates/skills/speckit/SKILL.md +4 -5
  307. package/templates/skills/supabase-patterns/SKILL.md +1 -1
  308. package/commands/kit-add.md +0 -42
  309. package/templates/CLAUDE.md.tmpl +0 -106
@@ -0,0 +1,335 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # agent-launch.sh — the ONE headless launcher: run an agent CLI with no terminal, under a
4
+ # wall-clock bound, and read what the runtime itself reported (E257.7, #264).
5
+ #
6
+ # Every other launcher cckit has is interactive — `or_herdr_launch` focuses a pane and
7
+ # `or_tmux_agent_cmd` types a line into a pane's shell. Nothing could start an agent with nobody
8
+ # watching, so the review stage carried a private bounded runner (`_rs_run`) and made the project
9
+ # supply the whole command. This file is that runner, shared, plus the two non-interactive forms
10
+ # cckit knows:
11
+ #
12
+ # claude `claude -p` the prompt on stdin, the reply on stdout
13
+ # codex `codex exec -` the same; `-` is what makes Codex read the prompt from stdin
14
+ #
15
+ # Both forms come from each CLI's own `--help` (claude 2.1.322, codex-cli 0.154.0). No live agent
16
+ # session was run to write this file: the fixture tests drive stub binaries named `claude` and
17
+ # `codex`, which proves the argv cckit builds and the bound it enforces, not that either agent
18
+ # behaves. A kind with no known headless form is refused, never guessed at.
19
+ #
20
+ # cckit adds no permission flag and names no model, so what the agent may do is whatever the CLI's
21
+ # own defaults and the user's own settings allow: `codex exec --help` lists `read-only` among its
22
+ # sandbox modes and `claude -p` prompts nobody. A run that must write gets its flags from the
23
+ # caller (a profile's `args`, or `--agent-arg`); cckit does not verify the result is read-only or
24
+ # writable.
25
+ #
26
+ # al_supported <kind> rc 0 when the kind has a known headless form
27
+ # al_headless_cmd <kind> <text|json|events> [<args, one per line>]
28
+ # echo the shell command line; it reads the prompt on stdin
29
+ # al_run <seconds> <command> run it on stdin under the bound: rc 124 on expiry (TERM,
30
+ # then KILL), else the command's own exit status
31
+ # al_stop TERM-then-KILL whatever al_run has running (for a trap)
32
+ # al_pidfile_alive <file> rc 0 when the pid an earlier al_run recorded in <file>
33
+ # (AL_PIDFILE) still names a live process or process group
34
+ # al_usage <kind> <output-file> echo "<input> <output> <cost-usd>" from the runtime's own
35
+ # JSON output; each field is `unknown` when it is not there
36
+ # al_skills <kind> <output-file> echo the skills the runtime's own event output shows as
37
+ # loaded, one per line; rc 1 when the output holds no event
38
+ # (the question cannot be answered from it)
39
+ #
40
+ # bash 3.2 + zsh compatible: no arrays, no glob held in a variable.
41
+ # errors: mixed — al_supported/al_headless_cmd/al_usage are pure (al_headless_cmd returns 2 for an
42
+ # unsupported kind); al_run returns the command's status, or 124 when it killed it.
43
+
44
+ # The pid al_run is waiting on, and whether it leads its own process group. Global on purpose: a
45
+ # caller's signal trap has to be able to stop the agent, and it cannot reach a local.
46
+ AL_PID=""
47
+ AL_GROUP=0
48
+
49
+ # AL_PIDFILE — when a caller sets it, al_run writes "<pid> <group 0|1>" there while a command runs
50
+ # and removes it when the command is reaped. It is the only record of the command that outlives the
51
+ # calling shell: a caller killed with SIGKILL runs no trap, and its next start needs something to
52
+ # check before it puts a second writer on the branch (al_pidfile_alive).
53
+ AL_PIDFILE="${AL_PIDFILE:-}"
54
+
55
+ # The watchdog al_run starts under perl. It leads the command's process group, runs the command as
56
+ # its child, and exits with the command's status (128+N when a signal killed it). Every 0.2s it
57
+ # compares its parent pid with the one it started under: a different parent means the shell that
58
+ # called al_run is gone, so nothing is left to enforce the bound, and it stops the group itself
59
+ # with TERM, up to 10s of grace, then KILL. It ignores TERM/INT/HUP so a group-wide TERM from
60
+ # al_stop reaches the command while the watchdog stays to report how the command ended. The empty
61
+ # CHLD handler is there to cut the 0.2s sleep short: without it the watchdog outlives the command
62
+ # by up to one tick, and a start made in that tick is refused for a process that is already over.
63
+ _AL_WATCHDOG='
64
+ setpgrp(0, 0);
65
+ my $parent = getppid();
66
+ my $kid = fork();
67
+ exit 126 unless defined $kid;
68
+ if ($kid == 0) { exec @ARGV; exit 127 }
69
+ $SIG{TERM} = $SIG{INT} = $SIG{HUP} = $SIG{CHLD} = sub { };
70
+ sub status { my $s = shift; exit(($s & 127) ? 128 + ($s & 127) : $s >> 8) }
71
+ while (1) {
72
+ my $r = waitpid($kid, 1);
73
+ status($?) if $r == $kid;
74
+ exit 127 if $r < 0;
75
+ if (getppid() != $parent) {
76
+ kill("-TERM", $$);
77
+ for (my $i = 0; $i < 50; $i++) {
78
+ last if waitpid($kid, 1) != 0;
79
+ select(undef, undef, undef, 0.2);
80
+ }
81
+ kill("-KILL", $$);
82
+ exit 137;
83
+ }
84
+ select(undef, undef, undef, 0.2);
85
+ }
86
+ '
87
+
88
+ # al_supported <kind> — matches on the basename, so `--agent /opt/bin/codex` still resolves.
89
+ al_supported() {
90
+ case "${1##*/}" in claude|codex) return 0 ;; esac
91
+ return 1
92
+ }
93
+
94
+ _al_quote() {
95
+ local a="${1:-}" esc
96
+ # Escaped in its own assignment — the `'\''` idiom inside a double-quoted concatenation adds a
97
+ # second level of quoting (orchestration-runtime.sh:or_tmux_agent_cmd records the same bug).
98
+ esc=${a//\'/\'\\\'\'}
99
+ printf "'%s'" "$esc"
100
+ }
101
+
102
+ # al_headless_cmd <kind> <text|json> [<args-nl>] — the command line for one non-interactive turn.
103
+ #
104
+ # `text` is the plain reply (what a reviewer's report is parsed from). `json` asks the runtime for
105
+ # its machine-readable output, which is the only place token usage appears: one result object from
106
+ # `claude -p --output-format json`, JSONL events from `codex exec --json`.
107
+ #
108
+ # `events` asks for every event of the turn, one JSON object per line, which is the only place a
109
+ # tool call appears (#504): `claude -p --output-format stream-json --verbose` (`claude --help`,
110
+ # 2.1.323: stream-json is the "realtime streaming" format of --print). The closing `result` object
111
+ # is still in that stream, so al_usage reads it unchanged. Codex prints events in its one JSON mode
112
+ # already, so `events` and `json` are the same command there.
113
+ al_headless_cmd() {
114
+ local kind="${1:-}" mode="${2:-text}" args_nl="${3:-}" cmd tail="" a
115
+ case "${kind##*/}" in
116
+ claude) cmd="$(_al_quote "$kind") -p"
117
+ [ "$mode" = "json" ] && cmd="$cmd --output-format json"
118
+ [ "$mode" = "events" ] && cmd="$cmd --output-format stream-json --verbose" ;;
119
+ codex) cmd="$(_al_quote "$kind") exec"
120
+ case "$mode" in json|events) cmd="$cmd --json" ;; esac
121
+ tail=" -" ;;
122
+ *) echo "agent-launch: no headless form is known for agent '$kind' (supported: claude, codex)" >&2
123
+ return 2 ;;
124
+ esac
125
+ if [ -n "$args_nl" ]; then
126
+ while IFS= read -r a; do
127
+ [ -n "$a" ] || continue
128
+ cmd="$cmd $(_al_quote "$a")"
129
+ done <<EOF
130
+ $args_nl
131
+ EOF
132
+ fi
133
+ printf '%s%s\n' "$cmd" "$tail"
134
+ }
135
+
136
+ # _al_signal <SIGNAL> — signal the running command. The whole process group when it has one, so a
137
+ # grandchild (the agent's own shell tool, a test runner it started) does not outlive the bound.
138
+ _al_signal() {
139
+ local sig="${1:-TERM}"
140
+ [ -n "$AL_PID" ] || return 0
141
+ if [ "$AL_GROUP" = "1" ] && kill -s "$sig" -- "-$AL_PID" 2>/dev/null; then return 0; fi
142
+ kill -s "$sig" "$AL_PID" 2>/dev/null
143
+ return 0
144
+ }
145
+
146
+ # al_stop — SIGTERM, up to 10s of grace, then SIGKILL. TERM first because an agent that traps it
147
+ # to clean up should get the chance; KILL after because one that ignores it should not get to stay.
148
+ al_stop() {
149
+ local grace=0
150
+ [ -n "$AL_PID" ] || return 0
151
+ _al_signal TERM
152
+ while [ "$grace" -lt 50 ] && kill -0 "$AL_PID" 2>/dev/null; do sleep 0.2; grace=$((grace + 1)); done
153
+ # Unconditional: the leader may be gone while a group member that ignored TERM is not.
154
+ _al_signal KILL
155
+ wait "$AL_PID" 2>/dev/null
156
+ AL_PID=""
157
+ [ -z "$AL_PIDFILE" ] || rm -f "$AL_PIDFILE"
158
+ return 0
159
+ }
160
+
161
+ # al_pidfile_alive <file> — rc 0 when the "<pid> <group>" al_run wrote there is still running.
162
+ # The group is probed as well as the pid: the leader can be gone while a member it started is not.
163
+ #
164
+ # A zombie is not running. `kill -0` succeeds on one, and an orphaned watchdog stays a zombie for
165
+ # as long as whatever adopted it does not reap (a container whose pid 1 is not an init), so the
166
+ # process state is read from ps when ps is there. When it is not, `kill -0` is the answer: a
167
+ # refusal the user can clear by removing the file is the safe side of that doubt.
168
+ al_pidfile_alive() {
169
+ local f="${1:-}" p="" g="" rows=""
170
+ [ -n "$f" ] && [ -f "$f" ] || return 1
171
+ read -r p g < "$f" || true
172
+ case "$p" in ''|*[!0-9]*) return 1 ;; esac
173
+ if [ "$g" = "1" ]; then
174
+ kill -0 "$p" 2>/dev/null || kill -0 -- "-$p" 2>/dev/null || return 1
175
+ else
176
+ kill -0 "$p" 2>/dev/null || return 1
177
+ fi
178
+ rows="$(ps -A -o pid=,pgid=,stat= 2>/dev/null)" || rows=""
179
+ [ -n "$rows" ] || return 0
180
+ printf '%s\n' "$rows" | awk -v p="$p" -v g="$g" '
181
+ ($1 == p || (g == "1" && $2 == p)) && $3 !~ /^Z/ { live = 1 }
182
+ END { exit live ? 0 : 1 }'
183
+ }
184
+
185
+ # al_run <seconds> <command> — run the command with this shell's stdin, stdout and stderr, bounded.
186
+ #
187
+ # One code path on every system. `timeout(1)` is absent on macOS, so a "use timeout when present"
188
+ # design leaves the platform this is most run from on the untested branch — and a foreground
189
+ # `timeout` also defers a caller's signal trap until the command returns, which is exactly when an
190
+ # interrupted unattended run needs it to fire. The command is a background job polled every 0.2s.
191
+ #
192
+ # `<&0` is load-bearing: a background job in a non-interactive shell gets /dev/null as stdin unless
193
+ # it is redirected explicitly, and the prompt arrives on stdin.
194
+ #
195
+ # perl puts the command in its own process group so the bound can signal the group, and stays as
196
+ # that group's watchdog (_AL_WATCHDOG) so the command cannot outlive the shell that bounds it: a
197
+ # caller killed with SIGKILL takes the bound with it, and the watchdog then stops the group.
198
+ # Without perl only the direct child is signalled, a grandchild that ignores a closed pipe can
199
+ # survive, and a command whose caller was killed keeps running — AL_PIDFILE is what a later start
200
+ # checks in that case.
201
+ al_run() {
202
+ local secs="${1:-900}" cmd="${2:-}" ticks=0 limit rc=0
203
+ case "$secs" in ''|*[!0-9]*) secs=900 ;; esac
204
+ limit=$((secs * 5))
205
+ if command -v perl >/dev/null 2>&1; then
206
+ perl -e "$_AL_WATCHDOG" sh -c "$cmd" <&0 &
207
+ AL_PID=$!; AL_GROUP=1
208
+ else
209
+ sh -c "$cmd" <&0 &
210
+ AL_PID=$!; AL_GROUP=0
211
+ fi
212
+ [ -z "$AL_PIDFILE" ] || printf '%s %s\n' "$AL_PID" "$AL_GROUP" > "$AL_PIDFILE"
213
+ while kill -0 "$AL_PID" 2>/dev/null; do
214
+ if [ "$ticks" -ge "$limit" ]; then
215
+ al_stop
216
+ return 124
217
+ fi
218
+ sleep 0.2; ticks=$((ticks + 1))
219
+ done
220
+ wait "$AL_PID" 2>/dev/null; rc=$?
221
+ AL_PID=""
222
+ [ -z "$AL_PIDFILE" ] || rm -f "$AL_PIDFILE"
223
+ return "$rc"
224
+ }
225
+
226
+ # al_usage <kind> <output-file> — "<input-tokens> <output-tokens> <cost-usd>" for one turn.
227
+ #
228
+ # Read from what the runtime printed, never computed. Claude's result object carries `usage` and
229
+ # `total_cost_usd`; Codex's `turn.completed` events carry `usage` and no cost. Input counts every
230
+ # input token the runtime reported, cached or not. A field that is not in the output is the word
231
+ # `unknown` — a list price applied to a token count is an estimate, and one priced for a different
232
+ # provider is a wrong number (effort-metrics.sh prices Claude transcripts only, and stays that way).
233
+ #
234
+ # The field names are the documented ones; no live run confirmed them. A runtime that prints a
235
+ # different shape yields `unknown`, never a guess.
236
+ al_usage() {
237
+ local kind="${1:-}" f="${2:-}" got=""
238
+ if [ -s "$f" ] && command -v jq >/dev/null 2>&1; then
239
+ case "${kind##*/}" in
240
+ claude)
241
+ got="$(jq -rs '
242
+ [ .[] | .. | objects | select(.type? == "result" and ((.usage? | type) == "object")) ] | last
243
+ | if . == null then empty else
244
+ ( [ .usage.input_tokens, .usage.cache_creation_input_tokens, .usage.cache_read_input_tokens ]
245
+ | map(numbers) ) as $in
246
+ | "\(if ($in | length) == 0 then "unknown" else ($in | add) end) \(.usage.output_tokens | if type == "number" then . else "unknown" end) \(.total_cost_usd | if type == "number" then . else "unknown" end)"
247
+ end' "$f" 2>/dev/null)" || got=""
248
+ # An event stream (`events` mode) is one object per line. A line that is not JSON (a
249
+ # truncated last line of a killed round) fails the slurp above, so read line by line.
250
+ if [ -z "$got" ]; then
251
+ got="$(jq -rRs '
252
+ [ split("\n")[] | fromjson? | objects | select(.type? == "result" and ((.usage? | type) == "object")) ] | last
253
+ | if . == null then empty else
254
+ ( [ .usage.input_tokens, .usage.cache_creation_input_tokens, .usage.cache_read_input_tokens ]
255
+ | map(numbers) ) as $in
256
+ | "\(if ($in | length) == 0 then "unknown" else ($in | add) end) \(.usage.output_tokens | if type == "number" then . else "unknown" end) \(.total_cost_usd | if type == "number" then . else "unknown" end)"
257
+ end' "$f" 2>/dev/null)" || got=""
258
+ fi
259
+ ;;
260
+ codex)
261
+ got="$(jq -rRs '
262
+ [ split("\n")[] | fromjson? | objects
263
+ | select(.type? == "turn.completed" and ((.usage? | type) == "object")) | .usage ]
264
+ | if length == 0 then empty else
265
+ "\([.[].input_tokens | numbers] | if length == 0 then "unknown" else add end) \([.[].output_tokens | numbers] | if length == 0 then "unknown" else add end) unknown"
266
+ end' "$f" 2>/dev/null)" || got=""
267
+ ;;
268
+ esac
269
+ fi
270
+ [ -n "$got" ] || got="unknown unknown unknown"
271
+ printf '%s\n' "$got"
272
+ }
273
+
274
+ # al_skills <kind> <output-file> — the skills one turn LOADED, one name per line, sorted, as the
275
+ # runtime's own event output shows them (#504). Never what the agent says it used.
276
+ #
277
+ # rc 0: the output held runtime events, so the list is a reading — an empty list means no skill
278
+ # was loaded. rc 1: no event in the output (text mode, `claude --output-format json`, an
279
+ # empty or missing file, no jq): which skills were loaded cannot be told from it.
280
+ #
281
+ # What counts as loaded:
282
+ # claude an assistant `tool_use` block named `Skill` (the name is `input.skill`), or one named
283
+ # `Read` whose `input.file_path` ends in `skills/<name>/SKILL.md` — and, for both, a
284
+ # `tool_result` for that call that is not `is_error`. A call for a skill that is not
285
+ # installed returns an error result, so asking for a skill is not loading it.
286
+ # codex a completed `command_execution` item with `exit_code` 0 whose command names a
287
+ # `skills/<name>/SKILL.md` file. Codex has no skill tool: it reads the file with a
288
+ # shell command.
289
+ #
290
+ # The Claude shape (`type: tool_use`, `name: Skill`, `input.skill`) is the one Claude Code stores
291
+ # in its session transcripts. No live headless run confirmed that `stream-json` prints the same
292
+ # objects, and none confirmed the Codex field names: a runtime that prints another shape yields an
293
+ # empty list or rc 1, never a guessed name.
294
+ al_skills() {
295
+ local kind="${1:-}" f="${2:-}" got=""
296
+ [ -s "$f" ] || return 1
297
+ command -v jq >/dev/null 2>&1 || return 1
298
+ case "${kind##*/}" in
299
+ claude)
300
+ got="$(jq -rRs '
301
+ def skillfile: (capture("(^|/)skills/(?<n>[^/]+)/SKILL\\.md$")? | .n) // empty;
302
+ [ split("\n")[] | fromjson? | if type == "array" then .[] else . end | objects ] as $ev
303
+ | [ $ev[] | select(.type? == "system" or .type? == "assistant" or .type? == "user") ] as $rt
304
+ | if ($rt | length) == 0 then "al:none" else
305
+ ( [ $rt[] | select(.type == "user") | .message.content? | arrays | .[] | objects
306
+ | select(.type? == "tool_result" and (.is_error? != true)) | .tool_use_id ] ) as $done
307
+ | "al:events",
308
+ ( [ $rt[] | select(.type == "assistant") | .message.content? | arrays | .[] | objects
309
+ | select(.type? == "tool_use") | . as $t
310
+ | select(any($done[]; . == $t.id))
311
+ | if $t.name == "Skill" then ($t.input.skill? | strings)
312
+ elif $t.name == "Read" then ($t.input.file_path? | strings | skillfile)
313
+ else empty end ]
314
+ | map(select(length > 0)) | unique | .[] )
315
+ end' "$f" 2>/dev/null)" || got=""
316
+ ;;
317
+ codex)
318
+ got="$(jq -rRs '
319
+ [ split("\n")[] | fromjson? | objects ] as $ev
320
+ | [ $ev[] | select((.type? | type) == "string" and (.type | test("^(thread|turn|item)\\."))) ] as $rt
321
+ | if ($rt | length) == 0 then "al:none" else
322
+ "al:events",
323
+ ( [ $rt[] | select(.type == "item.completed") | .item? | objects
324
+ | select(.type? == "command_execution" and .exit_code? == 0)
325
+ | .command? | strings
326
+ | [ match("(^|[/ \"\u0027=])skills/([A-Za-z0-9._-]+)/SKILL\\.md"; "g") | .captures[1].string ] | .[] ]
327
+ | unique | .[] )
328
+ end' "$f" 2>/dev/null)" || got=""
329
+ ;;
330
+ esac
331
+ case "$got" in
332
+ al:events*) printf '%s\n' "$got" | sed 1d | grep . || true; return 0 ;;
333
+ esac
334
+ return 1
335
+ }
@@ -31,6 +31,14 @@ has "draft PR marked · draft" "$MQ" "· draft"
31
31
  has "blocked PR marked ▪ blocked" "$MQ" "▪ blocked"
32
32
  has "agent branch flagged ● agent" "$MQ" "● agent"
33
33
 
34
+ # The data form carries the same order as the renderer (one ordering, two outputs).
35
+ MQD="$(board_merge_queue_data "$PRS")"
36
+ t "merge queue data is in the rendered order" "$(printf '%s' "$MQD" | jq -r '[.[].number|tostring]|join(" ")')" "14 13 12 11 10"
37
+ t "merge queue data names each state" "$(printf '%s' "$MQD" | jq -r '[.[].state]|join(" ")')" "ready ready ready draft blocked"
38
+ t "merge queue data flags the agent branch" "$(printf '%s' "$MQD" | jq -r '[.[]|select(.agent)|.number|tostring]|join(" ")')" "12"
39
+ t "merge queue data drops the sort keys" "$(printf '%s' "$MQD" | jq -r '.[0]|has("rr") or has("sr") or has("ready")')" "false"
40
+ t "empty merge queue data is []" "$(board_merge_queue_data '[]')" "[]"
41
+
34
42
  # empty PR set renders the no-open-PRs row, not an error.
35
43
  has "empty merge queue renders a friendly row" "$(board_merge_queue '[]')" "no open PRs ✓"
36
44
 
@@ -45,6 +53,9 @@ has "stale list includes the 20d issue" "$sl" "#1(20d)"
45
53
  case "$sl" in *"#2"*) echo "FAIL: fresh issue wrongly flagged stale"; fail=1 ;; *) echo "ok: fresh issue not flagged stale" ;; esac
46
54
  t "no stale issues yields empty (caller renders none)" "$(board_stale_list "[{\"number\":9,\"updatedAt\":\"$NOW\"}]" 14)" ""
47
55
 
56
+ t "stale data is [{number,days}], most-stale first" "$(board_stale_data "$ISS" 14)" '[{"number":3,"days":40},{"number":1,"days":20}]'
57
+ t "stale data is [] when nothing is stale" "$(board_stale_data "[{\"number\":9,\"updatedAt\":\"$NOW\"}]" 14)" "[]"
58
+
48
59
  # ── not-on-board + status lookup ────────────────────────────────────────────────────────────────
49
60
  t "not_on_board lists issues absent from the board map" "$(board_not_on_board '[{"number":1},{"number":2},{"number":3}]' '{"1":"Todo","3":"Done"}')" "#2"
50
61
  t "not_on_board empty when all issues are on the board" "$(board_not_on_board '[{"number":1}]' '{"1":"Todo"}')" ""
@@ -1,20 +1,21 @@
1
1
  #!/usr/bin/env bash
2
2
  # shellcheck shell=bash
3
- # board-view.sh — pure render helpers for the read-only board view (`cckit sync`). Each takes JSON
4
- # and emits markdown/text; none call gh, so they are unit-testable with canned fixtures (#122).
3
+ # board-view.sh — pure helpers for the read-only board view (`cckit sync`). Each takes JSON and
4
+ # emits markdown/text, or JSON for the two `_data` forms; none call gh, so they are unit-testable with canned fixtures (#122).
5
5
  # Ported from the host project's board view: a merge-ordered open-PR queue, a stale-issue flag, a
6
6
  # not-on-board flag, and Project Status per issue. bash 3.2 + zsh safe. Requires jq.
7
- # errors: pure — JSON in, text out; no gh, no network
7
+ # errors: pure — JSON in, text or JSON out; no gh, no network
8
8
 
9
- # board_merge_queue <prs-json> — render the open-PR merge queue as a markdown table. <prs-json> is
10
- # `gh pr list --json number,title,labels,isDraft,headRefName,reviewDecision,mergeable` output.
11
- # Ordering answers "in what order do I merge?": ready(1) -> draft(2) -> blocked(3); within a tier,
12
- # risk:low first, then smaller size, then issue number. Risk/size come from risk:/size: labels; a
13
- # `blocked` label marks blocked. An agent branch (agent|claude|auto/*) or an agent:auto label flags a
14
- # row as bot-authored so a human checks it before starting overlapping work.
15
- board_merge_queue() {
16
- local prs="${1:-[]}" rows tab; tab="$(printf '\t')"
17
- rows="$(printf '%s' "$prs" | jq -r '
9
+ # board_merge_queue_data <prs-json> — the open-PR merge queue as a JSON array, in merge order.
10
+ # <prs-json> is `gh pr list --json number,title,labels,isDraft,headRefName,reviewDecision,mergeable`
11
+ # output. Ordering answers "in what order do I merge?": ready(1) -> draft(2) -> blocked(3); within a
12
+ # tier, risk:low first, then smaller size, then issue number. Risk/size come from risk:/size: labels
13
+ # (size is "" when unlabeled); a `blocked` label marks blocked. An agent branch (agent|claude|auto/*)
14
+ # or an agent:auto label sets `agent`, so a human checks the row before starting overlapping work.
15
+ # `state` is one of blocked | draft | conflict | ready | needs-review. This is the ONE place the
16
+ # order is computed: board_merge_queue renders these rows, and `cckit sync --json` emits them.
17
+ board_merge_queue_data() {
18
+ printf '%s' "${1:-[]}" | jq -c '
18
19
  def lbl($p): (($p.labels // []) | map(.name));
19
20
  def risk($l): ($l | map(select(startswith("risk:"))) | first // "risk:med" | ltrimstr("risk:"));
20
21
  def size($l): ($l | map(select(startswith("size:"))) | first // "" | ltrimstr("size:"));
@@ -25,16 +26,25 @@ board_merge_queue() {
25
26
  | (any($l[]; . == "blocked")) as $blocked
26
27
  | (((($p.headRefName // "") | test("^(agent|claude|auto)/")) or (any($l[]; . == "agent:auto")))) as $agent
27
28
  | (if $blocked then 3 elif ($p.isDraft // false) then 2 else 1 end) as $ready
28
- | { n:$p.number, title:$p.title, risk:$risk,
29
- size:(if $size=="" then "—" else $size end),
29
+ | { number:$p.number, title:$p.title, risk:$risk, size:$size,
30
30
  blocked:$blocked, draft:($p.isDraft // false), agent:$agent,
31
31
  conflict:($p.mergeable == "CONFLICTING"), approved:($p.reviewDecision == "APPROVED"),
32
32
  ready:$ready, rr:riskRank($risk), sr:sizeRank($size) } )
33
- | sort_by(.ready, .rr, .sr, .n)
34
- | .[]
35
- | [ ("#" + (.n|tostring)), .risk, .size,
36
- (if .blocked then "▪ blocked" elif .draft then "· draft" elif .conflict then "◆ conflict"
37
- elif .approved then "● ready" else "· needs review" end),
33
+ | sort_by(.ready, .rr, .sr, .number)
34
+ | map( . + { state: (if .blocked then "blocked" elif .draft then "draft"
35
+ elif .conflict then "conflict" elif .approved then "ready"
36
+ else "needs-review" end) }
37
+ | del(.ready, .rr, .sr) )'
38
+ }
39
+
40
+ # board_merge_queue <prs-json> — render board_merge_queue_data as a markdown table (same input).
41
+ board_merge_queue() {
42
+ local prs="${1:-[]}" rows tab; tab="$(printf '\t')"
43
+ rows="$(board_merge_queue_data "$prs" | jq -r '
44
+ .[]
45
+ | [ ("#" + (.number|tostring)), .risk, (if .size=="" then "—" else .size end),
46
+ ({"blocked":"▪ blocked","draft":"· draft","conflict":"◆ conflict","ready":"● ready"}[.state]
47
+ // "· needs review"),
38
48
  (if .agent then "● agent" else "·" end),
39
49
  (if (.title|length) > 48 then (.title[0:48] + "…") else .title end) ] | @tsv')"
40
50
  printf '### Merge Queue (open PRs — merge top-down)\n'
@@ -52,17 +62,23 @@ EOF
52
62
  printf '\n_Order: ready → draft → blocked; within that, risk:low first, then smaller size. ● agent = a bot branch — check it before starting overlapping work._\n'
53
63
  }
54
64
 
55
- # board_stale_list <issues-json> [days] — space-joined "#N(Dd)" tokens for open issues untouched for
56
- # more than [days] (default 14), most-stale first. Empty when none. <issues-json> is
57
- # `gh issue list --json number,updatedAt`. The caller renders the "none ✓" fallback.
58
- board_stale_list() {
65
+ # board_stale_data <issues-json> [days] — JSON array [{number, days}] of open issues untouched for
66
+ # more than [days] (default 14), most-stale first; [] when none. <issues-json> is
67
+ # `gh issue list --json number,updatedAt`. The data form of the stale flag (`cckit sync --json`).
68
+ board_stale_data() {
59
69
  local issues="${1:-[]}" days="${2:-14}" now
60
70
  now="$(date -u +%s 2>/dev/null || echo 0)"
61
- printf '%s' "$issues" | jq -r --argjson now "$now" --argjson d "$days" '
71
+ printf '%s' "$issues" | jq -c --argjson now "$now" --argjson d "$days" '
62
72
  [ .[]
63
73
  | (($now - ((.updatedAt // "1970-01-01T00:00:00Z") | fromdateiso8601)) / 86400 | floor) as $age
64
- | select($age > $d) | {n:.number, age:$age} ]
65
- | sort_by(-.age) | map("#\(.n)(\(.age)d)") | join(" ")'
74
+ | select($age > $d) | {number:.number, days:$age} ]
75
+ | sort_by(-.days)'
76
+ }
77
+
78
+ # board_stale_list <issues-json> [days] — board_stale_data as space-joined "#N(Dd)" tokens. Empty
79
+ # when none; the caller renders the "none ✓" fallback.
80
+ board_stale_list() {
81
+ board_stale_data "${1:-[]}" "${2:-14}" | jq -r 'map("#\(.number)(\(.days)d)") | join(" ")'
66
82
  }
67
83
 
68
84
  # board_not_on_board <issues-json> <status-map-json> — space-joined "#N" for open issues that have
@@ -95,5 +95,31 @@ t "reap kept the live remote row" "$(grep -c 'remote' "$(bo_file)" 2>/dev/null |
95
95
  bo_claim effort/318-agents-run-by-profile-in-herdr agent-c "$WT_B" 2>/dev/null
96
96
  t "a slashed branch name round-trips" "$(bo_owner effort/318-agents-run-by-profile-in-herdr)" "agent-c"
97
97
 
98
+ # ── the ledger lock excludes: of eight simultaneous attempts exactly one wins (#449) ───────────
99
+ # The lock was `mkdir`, and the `mkdir` of uutils coreutils 0.10.0 (Ubuntu 26.04) exits 0 for the
100
+ # loser of a race, so two claimants held the lock together. Each process makes ONE attempt on the
101
+ # same path, released together by a start file; a check-then-create primitive reports several
102
+ # winners in most rounds.
103
+ L="$tmp/prim.lock"; go="$tmp/prim.go"; won="$tmp/prim.won"; lock_bad=0; r=0
104
+ while [ "$r" -lt 25 ]; do
105
+ r=$((r+1)); rm -rf "$L" "$go"; : > "$won"; pids=""
106
+ for k in 1 2 3 4 5 6 7 8; do
107
+ (
108
+ waited=0
109
+ while [ ! -e "$go" ] && [ "$waited" -lt 20000 ]; do waited=$((waited+1)); done
110
+ _bo_lock_try "$L" && echo "$k" >> "$won"
111
+ ) &
112
+ pids="$pids $!"
113
+ done
114
+ : > "$go"
115
+ for pid in $pids; do wait "$pid"; done
116
+ [ "$(grep -c . "$won")" = 1 ] || lock_bad=$((lock_bad+1))
117
+ done
118
+ t "the ledger lock has exactly one winner in each of 25 races" "$lock_bad" "0"
119
+ _bo_unlock "$L"; t "_bo_unlock removes the lock" "$([ -e "$L" ] && echo left || echo gone)" "gone"
120
+ # A cckit from before #449 leaves a lock DIRECTORY at the same path: it must still block.
121
+ mkdir "$L"; _bo_lock_try "$L"; rc "a lock directory from an older cckit still blocks" "$?" "1"
122
+ _bo_unlock "$L"; t "_bo_unlock removes an older lock directory" "$([ -e "$L" ] && echo left || echo gone)" "gone"
123
+
98
124
  [ "$fail" -eq 0 ] && echo "ALL OK (branch-owner)" || echo "branch-owner: FAILURES"
99
125
  exit "$fail"
@@ -52,29 +52,36 @@ bo_file() { kit_state_file branch-owners.tsv; }
52
52
  _bo_now() { date +%s 2>/dev/null || echo 0; }
53
53
  _bo_host() { hostname 2>/dev/null || echo unknown; }
54
54
 
55
- # _bo_lock / _bo_unlock — the same mkdir primitive the port-slot ledger uses (worktree-start.sh):
56
- # a stock macOS has no flock(1), and `set -C` redirection is not atomic on every filesystem.
55
+ # _bo_lock / _bo_unlock — the same primitive the port-slot ledger uses (worktree-start.sh): the
56
+ # shell's noclobber redirection, which opens with O_CREAT|O_EXCL. A stock macOS has no flock(1).
57
+ # It was `mkdir` until #449; the `mkdir` of uutils coreutils 0.10.0 (Ubuntu 26.04) exits 0 for the
58
+ # loser of a race, so two claimants held this lock together.
57
59
  #
58
60
  # Unlike the port lock, a caller that CANNOT lock here must NOT proceed. That lock guards an
59
61
  # allocation whose worst case is a duplicate port; this one guards the answer to "is someone else
60
62
  # writing this branch?", and answering it without the lock is how both claimants get a yes.
63
+
64
+ # _bo_lock_try <lockfile> — ONE attempt: 0 when this call created the lock, 1 when it exists.
65
+ _bo_lock_try() { ( set -C; : > "$1" ) 2>/dev/null; }
66
+
61
67
  _bo_lock() {
62
68
  local d="$1" tries=0 max
63
69
  max=$(( _BO_LOCK_WAIT * 10 ))
64
70
  while [ "$tries" -lt "$max" ]; do
65
- mkdir "$d" 2>/dev/null && return 0
71
+ _bo_lock_try "$d" && return 0
66
72
  sleep 0.1 2>/dev/null || sleep 1
67
73
  tries=$(( tries + 1 ))
68
74
  done
69
- # A lock directory older than a minute is abandoned: the age floor sits well above _BO_LOCK_WAIT,
70
- # so a slow-but-live holder is never stolen from.
75
+ # A lock older than a minute is abandoned: the age floor sits well above _BO_LOCK_WAIT, so a
76
+ # slow-but-live holder is never stolen from. `rm -rf` because a cckit from before #449 leaves a
77
+ # lock DIRECTORY at this path.
71
78
  if [ -n "$(find "$d" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then
72
79
  rm -rf "$d" 2>/dev/null
73
- mkdir "$d" 2>/dev/null && { echo "branch-owner: broke an abandoned ledger lock ($d)" >&2; return 0; }
80
+ _bo_lock_try "$d" && { echo "branch-owner: broke an abandoned ledger lock ($d)" >&2; return 0; }
74
81
  fi
75
82
  return 1
76
83
  }
77
- _bo_unlock() { rmdir "$1" 2>/dev/null || rm -rf "$1" 2>/dev/null; }
84
+ _bo_unlock() { rm -rf "$1" 2>/dev/null; }
78
85
 
79
86
  # _bo_alive <pid> <host> <worktree> — rc 0 when the lease holder may still be running.
80
87
  _bo_alive() {
@@ -124,7 +131,7 @@ bo_claim() {
124
131
  tmp="$(mktemp 2>/dev/null)" || { _bo_unlock "$lock"; echo "bo_claim: no temp file" >&2; return 1; }
125
132
  _bo_rows_live "$f" > "$tmp"
126
133
 
127
- held_owner="$(awk -F"\t" -v b="$branch" '$1==b {print $2; exit}' "$tmp" 2>/dev/null)"
134
+ held_owner="$(awk -F"\t" -v b="$branch" '$1==b && !hit {print $2; hit=1}' "$tmp" 2>/dev/null)"
128
135
  if [ -n "$held_owner" ] && [ "$held_owner" != "$owner" ]; then
129
136
  local held_wt
130
137
  held_wt="$(awk -F"\t" -v b="$branch" '$1==b {print $5; exit}' "$tmp" 2>/dev/null)"
@@ -170,7 +177,7 @@ bo_owner() {
170
177
  local branch="${1:-}" f
171
178
  [ -n "$branch" ] || return 2
172
179
  f="$(bo_file)"; [ -f "$f" ] || return 0
173
- _bo_rows_live "$f" | awk -F"\t" -v b="$branch" '$1==b {print $2; exit}'
180
+ _bo_rows_live "$f" | awk -F"\t" -v b="$branch" '$1==b && !hit {print $2; hit=1}'
174
181
  }
175
182
 
176
183
  # bo_list — every live lease, one row per line.