@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,1357 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # qa-lane.sh — one review stack per lane: launch the command the project declared, then say
4
+ # whether the stack is USABLE (#295), then open the screen the change touched (#297), then stop
5
+ # it so the ports are actually free (#298), then record the reviewer's verdict against the commit
6
+ # that was reviewed (#299).
7
+ #
8
+ # Launched and usable are different facts. A dev server answers on its port seconds before the
9
+ # app behind it can serve a page, and a web process that is up while its API is down renders an
10
+ # error screen with a 200. So a lane is usable only when EVERY readiness probe in the `qa` config
11
+ # block answers in the same polling round, and only that verdict may open a browser.
12
+ #
13
+ # cckit does not boot the project by guessing. Everything here reads the `qa` block that
14
+ # `kit-add.sh qa` recorded (#294): `qa.command`, `qa.ports` as `{env, base}`, `qa.readiness` as
15
+ # `{env, path}`. The launched command gets the project's OWN port variable names — the ones the
16
+ # owner said the command honours — each set to `base + slot * stride`, where the slot is this
17
+ # lane's row in the port-slot ledger (wt_port_slot, worktree-start.sh). Two lanes hold two slots,
18
+ # so their ports are disjoint by allocation.
19
+ #
20
+ # A lane is keyed by an issue NUMBER: the one in the current branch name, or `--lane <n>`. The
21
+ # ledger rows are `<issue> <slot> <worktree>`, so a lane running in an issue's own worktree shares
22
+ # the slot `cckit start` already gave that worktree.
23
+ #
24
+ # Runtime state is per lane under the shared state dir (kit_state_dir — the same directory from
25
+ # every worktree): `qa/<lane>/pid`, `qa/<lane>/ports` (one `NAME=port` line per variable, exactly
26
+ # what the command was launched with) and `qa/<lane>/launch.log` (the command's stdout + stderr).
27
+ # Two more files exist so `qa stop` can tell the stack from a stranger: `qa/<lane>/started` (the
28
+ # launched pid's start time — a recycled pid has another) and `qa/<lane>/pgid` (written only when
29
+ # the command was started as its own process-group leader, which needs perl).
30
+ #
31
+ # What "up" means for one probe: the HTTP status is 2xx or 3xx. Redirects are NOT followed, so a
32
+ # 302 to a sign-in page is up — the app answered, and it answered with a decision. A 4xx, a 5xx
33
+ # and a refused connection are all "not yet".
34
+ #
35
+ # qa_cfg the project config path (rc 1 when there is none)
36
+ # qa_lane_id [<lane>] the lane number: the argument, else the branch's issue
37
+ # qa_lane_dir <lane> that lane's state directory (does not create it)
38
+ # qa_lane_log <lane> that lane's launch log path
39
+ # qa_ports_check <cfg> <stride> rc 2 + a reason when qa.ports cannot give disjoint lanes
40
+ # qa_lane_ports <cfg> <slot> <stride> one `NAME=port` line per declared port variable
41
+ # qa_probe_urls <cfg> <ports-file> one `NAME<TAB>url` line per readiness probe
42
+ # qa_probe <url> rc 0 when the url answers 2xx/3xx; echoes the status
43
+ # qa_launch <lane> [<worktree>] start the declared command with the lane's ports
44
+ # qa_ready <lane> [<timeout-seconds>] poll every probe until all answer; rc 1 on timeout
45
+ # qa_route_declared stdin is a PR body; echo the route its `QA route:` line names
46
+ # qa_route <lane> [<pr>] `declared<TAB>route` from the lane's PR, else `configured<TAB>route`
47
+ # qa_usable <lane> ONE probe round, no waiting, no event: rc 0 usable · rc 1 not
48
+ # qa_open <lane> [<pr>] [<route>] [<print-only>] [<port-env>] hand the reviewer the changed screen
49
+ # qa_port_tool `lsof` or `ss`: what can name a port's holder; rc 1 for neither
50
+ # qa_port_pids <port> [<tool>] the pids LISTENING on <port>, one per line
51
+ # qa_port_held <port> [<tool>] rc 0 when something listens on <port>, pid known or not
52
+ # qa_stop <lane> [<force>] stop the lane's supervisors, then its listeners; rc 1 if a port stays held
53
+ # qa_verdict_state <lane> [<head>] `pass` · `fail` · `stale` · `none` for the lane's verdict; rc 0 only for pass
54
+ # qa_verdict <lane> <pass|fail> [<pr>] [<head>] [<notes>] [<to>] [<shots>] record it, post it, mail a fail
55
+ # qa_verdict_show <lane> [<pr>] [<head>] print that state against the lane's head now
56
+ # qa_lanes one row per lane: branch, ports, up, verdict, worktree — read-only
57
+ # qa_cli <up|ready|open|stop|verdict|lanes> [--lane N] … the `cckit qa` verb (`stop --force`)
58
+ #
59
+ # The review route comes from the WORK, not from a smoke list: the builder writes one
60
+ # `QA route: /path` line in the PR body (`cckit brief` asks for it when a `qa` block exists) and
61
+ # `qa open` lands there. cckit hardcodes no project layout, so it never derives a route from
62
+ # changed file paths. With no declared line the configured `qa.route` is the fallback. The URL is
63
+ # always `http://<QA_HOST>:<lane port><route>`: the route is appended to a host and port cckit
64
+ # chose, so a PR body can pick the screen and nothing else. The port is the FIRST variable in
65
+ # `qa.ports` (the one a browser opens) unless `--port-env` names another.
66
+ #
67
+ # Events (kit-events.jsonl, through emit_event): `op.start qa.launch` when the command starts,
68
+ # `op.done qa.ready` when the lane becomes usable, `op.done qa.timeout` when the wait runs out,
69
+ # `op.done qa.exited` when the command died before the lane became usable, `op.done qa.open` when
70
+ # a usable lane's URL is handed over (payload: lane, url, route, source, opened), `op.done qa.stop`
71
+ # when a stop ran against a lane with a record (payload: lane, stopped, ports, held, freed),
72
+ # `op.done qa.verdict` for each recorded verdict (payload: lane, verdict, head, pr, attempt,
73
+ # comment, mailed).
74
+ #
75
+ # Tunables: QA_READY_TIMEOUT (seconds, default 120) · QA_READY_INTERVAL (seconds between rounds,
76
+ # default 2) · QA_PROBE_TIMEOUT (seconds per request, default 5) · QA_HOST (default localhost) ·
77
+ # QA_OPENER (the command that opens a URL; default `open`, else `xdg-open`) · QA_STOP_GRACE
78
+ # (seconds a process gets to exit on TERM before KILL, default 5) · QA_STOP_ROUNDS (how many times
79
+ # the port holders are re-resolved to catch a respawn, default 5) · QA_PORT_TOOL (`lsof`, `ss` or
80
+ # `none`: force the holder lookup; default is whichever exists).
81
+ #
82
+ # errors: strict — rc 2 on a usage or config refusal, or a lane port already in use (nothing
83
+ # launched, no record written), rc 3 without jq or curl,
84
+ # rc 1 when the lane is not usable; every rc-1 message names the launch log to read. For
85
+ # `verdict`: rc 2 refuses before any write, rc 1 after a recorded fail whose notes reached no
86
+ # mailbox, and reading a verdict is rc 0 only for a pass on the lane's current head.
87
+
88
+ # `${BASH_SOURCE[0]:-$0}`: BASH_SOURCE is empty under zsh, and `dirname ""` is "." — the sibling
89
+ # sources below would silently miss. Same fix as stage-receipt.sh and kit-gc.sh (#219).
90
+ _qa_dir="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null 2>&1 && pwd)"
91
+ # shellcheck source=/dev/null
92
+ command -v kit_state_dir >/dev/null 2>&1 || . "$_qa_dir/kit-state.sh"
93
+ # shellcheck source=/dev/null
94
+ command -v kit_config_path >/dev/null 2>&1 || . "$_qa_dir/config-path.sh"
95
+ # shellcheck source=/dev/null
96
+ command -v emit_event >/dev/null 2>&1 || . "$_qa_dir/kit-events.sh"
97
+ # shellcheck source=/dev/null
98
+ command -v wt_issue_number >/dev/null 2>&1 || . "$_qa_dir/worktree-issue.sh"
99
+ # shellcheck source=/dev/null
100
+ command -v wt_port_slot >/dev/null 2>&1 || . "$_qa_dir/worktree-start.sh"
101
+ # shellcheck source=/dev/null
102
+ command -v sr_record >/dev/null 2>&1 || . "$_qa_dir/stage-receipt.sh"
103
+ # shellcheck source=/dev/null
104
+ command -v pr_attach_evidence >/dev/null 2>&1 || . "$_qa_dir/pr-evidence.sh"
105
+ # shellcheck source=/dev/null
106
+ command -v msg_send >/dev/null 2>&1 || . "$_qa_dir/kit-msg.sh"
107
+ unset _qa_dir
108
+
109
+ _QA_NAME_RE='^[A-Za-z_][A-Za-z0-9_]*$'
110
+ # A route is a path: a leading slash, then URL path/query/fragment characters. No whitespace, no
111
+ # quotes, no backticks — it is read out of a PR body and handed to a browser opener as one argument.
112
+ _QA_ROUTE_RE='^/[A-Za-z0-9._~%/?#&=:@+,;!*-]*$'
113
+
114
+ _qa_need() {
115
+ command -v jq >/dev/null 2>&1 || { echo "cckit qa: jq is required" >&2; return 3; }
116
+ command -v curl >/dev/null 2>&1 || { echo "cckit qa: curl is required to probe readiness" >&2; return 3; }
117
+ }
118
+
119
+ # qa_cfg — the project config, by the one shared resolver. rc 1 when there is none.
120
+ qa_cfg() {
121
+ local cfg
122
+ cfg="$(kit_config_path 2>/dev/null)" || return 1
123
+ [ -n "$cfg" ] && [ -f "$cfg" ] || return 1
124
+ printf '%s\n' "$cfg"
125
+ }
126
+
127
+ # qa_lane_id [<lane>] — echo the lane number. An explicit argument must be a number; with none,
128
+ # the issue number in the current branch name is the lane. rc 2 when neither yields one.
129
+ qa_lane_id() {
130
+ local lane="${1:-}" branch=""
131
+ if [ -z "$lane" ]; then
132
+ branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)" || branch=""
133
+ [ -n "$branch" ] && lane="$(wt_issue_number "$branch" 2>/dev/null)"
134
+ fi
135
+ case "$lane" in
136
+ ''|*[!0-9]*)
137
+ echo "cckit qa: no lane — the branch '${branch:-?}' carries no issue number; pass --lane <number>" >&2
138
+ return 2 ;;
139
+ esac
140
+ printf '%s\n' "$lane"
141
+ }
142
+
143
+ qa_lane_dir() { printf '%s/qa/%s\n' "$(kit_state_dir)" "$1"; }
144
+ qa_lane_log() { printf '%s/launch.log\n' "$(qa_lane_dir "$1")"; }
145
+
146
+ # qa_ports_check <cfg> <stride> — rc 2 with the reason on stderr when `qa.ports` cannot hand every
147
+ # lane a disjoint port set. A lane's port is `base + slot * stride` over 40 slots, so two bases
148
+ # that differ by a multiple of the stride, fewer than 40 strides apart, are the SAME port in two
149
+ # different lanes (bases 3000 and 3100 at stride 10: lane 10's first port is lane 0's second).
150
+ # Refused here, before anything launches — a shared port looks like a working stack until the
151
+ # second lane starts.
152
+ qa_ports_check() {
153
+ local cfg="$1" stride="$2" why=""
154
+ why="$(jq -r --argjson stride "$stride" --arg re "$_QA_NAME_RE" '
155
+ (.qa.ports // []) as $p
156
+ | if ($p | type) != "array" or ($p | length) == 0 then
157
+ "qa.ports is empty — the stack command must honour at least one port env var"
158
+ elif any($p[]; (.env | type) != "string" or (.env | test($re) | not)) then
159
+ "qa.ports has an entry whose env is not a variable name"
160
+ elif any($p[]; (.base | type) != "number" or .base < 1 or (.base + 39 * $stride) > 65535) then
161
+ "qa.ports has a base that leaves the port range within 40 lanes at stride \($stride)"
162
+ else
163
+ ([ range(0; $p | length) as $i | range($i + 1; $p | length) as $j
164
+ | { a: $p[$i], b: $p[$j], d: (($p[$i].base - $p[$j].base) | fabs) }
165
+ | select(.a.env == .b.env or ((.d % $stride) == 0 and (.d / $stride) < 40)) ] | .[0]) as $c
166
+ | if $c == null then ""
167
+ elif $c.a.env == $c.b.env then "qa.ports declares \($c.a.env) twice"
168
+ else "qa.ports bases \($c.a.env)=\($c.a.base) and \($c.b.env)=\($c.b.base) are \($c.d) apart, a multiple of the stride \($stride): two lanes would share a port. Move one base off the multiple (or 40 strides away), or change worktree.devPortStride"
169
+ end
170
+ end' "$cfg" 2>/dev/null)" || why="the qa block in $cfg could not be read"
171
+ [ -z "$why" ] && return 0
172
+ echo "cckit qa: $why" >&2
173
+ return 2
174
+ }
175
+
176
+ # qa_lane_ports <cfg> <slot> <stride> — one `NAME=port` line per declared port variable.
177
+ qa_lane_ports() {
178
+ jq -r --argjson slot "$2" --argjson stride "$3" \
179
+ '(.qa.ports // [])[] | "\(.env)=\(.base + $slot * $stride)"' "$1" 2>/dev/null
180
+ }
181
+
182
+ # qa_probe_urls <cfg> <ports-file> — one `NAME<TAB>url` line per readiness probe, built from the
183
+ # ports the lane was actually LAUNCHED with (the recorded file), not recomputed from config: a
184
+ # config edited after launch must not move the probes off the running stack. rc 2 when there is no
185
+ # probe, or a probe names a variable the lane has no port for.
186
+ qa_probe_urls() {
187
+ local cfg="$1" pfile="$2" host="${QA_HOST:-localhost}" rows="" name="" rpath="" port="" out="" tab
188
+ tab="$(printf '\t')"
189
+ rows="$(jq -r '(.qa.readiness // [])[] | "\(.env // "")\t\(.path // "/")"' "$cfg" 2>/dev/null)"
190
+ if [ -z "$rows" ]; then
191
+ echo "cckit qa: qa.readiness is empty — with no probe, launched cannot be told from usable. Declare one path per service (kit-add.sh qa)" >&2
192
+ return 2
193
+ fi
194
+ while IFS="$tab" read -r name rpath; do
195
+ [ -n "$name" ] || continue
196
+ port="$(awk -F= -v n="$name" '$1 == n { print $2; exit }' "$pfile" 2>/dev/null)"
197
+ case "$port" in
198
+ ''|*[!0-9]*)
199
+ echo "cckit qa: qa.readiness probes $name, which is not a port variable in qa.ports" >&2
200
+ return 2 ;;
201
+ esac
202
+ case "$rpath" in /*) : ;; *) rpath="/$rpath" ;; esac
203
+ out="$out$name$tab""http://$host:$port$rpath
204
+ "
205
+ done <<EOF
206
+ $rows
207
+ EOF
208
+ printf '%s' "$out"
209
+ }
210
+
211
+ # qa_probe <url> — echo the HTTP status; rc 0 when it is 2xx or 3xx. Redirects are not followed:
212
+ # a 302 to sign-in is the app answering. A refused connection reads as 000.
213
+ qa_probe() {
214
+ local code=""
215
+ code="$(curl -s -o /dev/null -w '%{http_code}' --max-time "${QA_PROBE_TIMEOUT:-5}" "$1" 2>/dev/null)" || true
216
+ case "$code" in [0-9][0-9][0-9]) : ;; *) code="000" ;; esac
217
+ printf '%s' "$code"
218
+ case "$code" in 2??|3??) return 0 ;; esac
219
+ return 1
220
+ }
221
+
222
+ # _qa_pid_live <lane> — rc 0 when the lane's recorded command is still running.
223
+ _qa_pid_live() {
224
+ local pid=""
225
+ pid="$(cat "$(qa_lane_dir "$1")/pid" 2>/dev/null)" || return 1
226
+ case "$pid" in ''|*[!0-9]*) return 1 ;; esac
227
+ kill -0 "$pid" 2>/dev/null
228
+ }
229
+
230
+ # _qa_ports_json <ports-file> — the recorded ports as one JSON object, for event payloads.
231
+ _qa_ports_json() {
232
+ jq -Rn '[inputs | select(length > 0) | split("=") | { key: .[0], value: (.[1] | tonumber) }] | from_entries' \
233
+ < "$1" 2>/dev/null || printf '{}'
234
+ }
235
+
236
+ # _qa_ports_free <lane> <ports> — rc 0 when no listener holds any `NAME=port` line of <ports>;
237
+ # rc 1 after naming every held port and its listener on stderr. With neither lsof nor ss the ports
238
+ # cannot be checked: that is said on stderr and the launch goes on. The check and the command's
239
+ # own bind are two moments, so a listener that arrives between them is not caught here — `qa stop`
240
+ # leaves such a stranger running unless it is forced.
241
+ _qa_ports_free() {
242
+ local lane="$1" ports="$2" tool="" line="" port="" who="" busy=""
243
+ tool="$(qa_port_tool)" || {
244
+ echo "[qa $lane] ports NOT checked before launch — neither lsof nor ss is installed" >&2
245
+ return 0
246
+ }
247
+ while IFS= read -r line; do
248
+ [ -n "$line" ] || continue
249
+ port="${line#*=}"
250
+ qa_port_held "$port" "$tool" || continue
251
+ who="$(qa_port_pids "$port" "$tool" | tr '\n' ' ')"; who="${who% }"
252
+ busy="$busy
253
+ $line is already listened on by ${who:+pid }${who:-a process this user cannot name}"
254
+ done <<EOF
255
+ $ports
256
+ EOF
257
+ [ -z "$busy" ] && return 0
258
+ echo "cckit qa: lane $lane NOT launched — a port of this lane is already in use:$busy" >&2
259
+ echo " the declared command could not bind it, and that listener would answer the readiness probes." >&2
260
+ echo " Stop that process, or run cckit qa stop --lane $lane if it is this lane's earlier stack." >&2
261
+ return 1
262
+ }
263
+
264
+ # qa_launch <lane> [<worktree>] — start the declared command in <worktree> (default: the current
265
+ # checkout) with every declared port variable set to this lane's port. Echoes the pid.
266
+ #
267
+ # Idempotent: a lane whose recorded command is still running is left alone and its pid is echoed —
268
+ # a second `up` must never start a second stack on the same ports.
269
+ qa_launch() {
270
+ local lane="$1" wt="${2:-}" cfg="" cmd="" stride="" slot="" ports="" d="" log="" pid="" line="" payload="" grouped=0
271
+ _qa_need || return $?
272
+ cfg="$(qa_cfg)" || { echo "cckit qa: no kit config found — run cckit init, then kit-add.sh qa" >&2; return 2; }
273
+ cmd="$(jq -r '.qa.command // empty' "$cfg" 2>/dev/null)"
274
+ if [ -z "$cmd" ]; then
275
+ echo "cckit qa: no qa.command in $cfg — cckit does not guess how a project boots. Record it with kit-add.sh qa" >&2
276
+ return 2
277
+ fi
278
+ [ -n "$wt" ] || wt="$(git rev-parse --show-toplevel 2>/dev/null)" || wt="$PWD"
279
+
280
+ stride="$(wt_port_stride "$cfg")"
281
+ qa_ports_check "$cfg" "$stride" || return $?
282
+ d="$(qa_lane_dir "$lane")"; log="$d/launch.log"
283
+
284
+ if _qa_pid_live "$lane"; then
285
+ pid="$(cat "$d/pid")"
286
+ echo "[qa $lane] already launched (pid $pid) — not starting a second stack" >&2
287
+ printf '%s\n' "$pid"
288
+ return 0
289
+ fi
290
+
291
+ slot="$(wt_port_slot "$wt" "$lane" "$wt")"
292
+ case "$slot" in ''|*[!0-9]*) echo "cckit qa: could not allocate a port slot for lane $lane" >&2; return 1 ;; esac
293
+ ports="$(qa_lane_ports "$cfg" "$slot" "$stride")"
294
+ # A port somebody already listens on is refused BEFORE the record exists. The declared command
295
+ # could not bind it, yet that listener would answer the readiness probes: the lane would read
296
+ # usable and `qa open` would show another app.
297
+ _qa_ports_free "$lane" "$ports" || return 2
298
+ # Probe wiring is a config fact — refuse a stack nothing could ever call usable BEFORE starting it.
299
+ mkdir -p "$d" 2>/dev/null || { echo "cckit qa: cannot create $d" >&2; return 1; }
300
+ printf '%s\n' "$ports" > "$d/ports"
301
+ # A lane with a ports file reads as launched, so a refusal here must not leave one behind.
302
+ qa_probe_urls "$cfg" "$d/ports" >/dev/null || { rm -f "$d/ports"; return 2; }
303
+
304
+ : > "$log"
305
+ command -v perl >/dev/null 2>&1 && grouped=1
306
+ (
307
+ cd "$wt" || exit 1
308
+ while IFS= read -r line; do
309
+ # shellcheck disable=SC2163 # $line IS the `NAME=port` assignment to export
310
+ [ -n "$line" ] && export "$line"
311
+ done <<EOF
312
+ $ports
313
+ EOF
314
+ # Its own process group when perl can make one: `qa stop` then finds every member by group
315
+ # even after this root has died and its children were reparented to pid 1 (#298).
316
+ if [ "$grouped" = 1 ]; then
317
+ if command -v nohup >/dev/null 2>&1; then
318
+ exec perl -e 'setpgrp(0, 0); exec @ARGV; exit 127' nohup sh -c "$cmd"
319
+ else
320
+ exec perl -e 'setpgrp(0, 0); exec @ARGV; exit 127' sh -c "$cmd"
321
+ fi
322
+ fi
323
+ if command -v nohup >/dev/null 2>&1; then exec nohup sh -c "$cmd"; else exec sh -c "$cmd"; fi
324
+ ) >> "$log" 2>&1 < /dev/null &
325
+ pid=$!
326
+ printf '%s\n' "$pid" > "$d/pid"
327
+ rm -f "$d/pgid" "$d/started"
328
+ [ "$grouped" = 1 ] && printf '%s\n' "$pid" > "$d/pgid"
329
+ _qa_pid_started "$pid" > "$d/started" 2>/dev/null || rm -f "$d/started"
330
+
331
+ payload="$(jq -cn --arg lane "$lane" --argjson pid "$pid" --arg log "$log" --arg wt "$wt" \
332
+ --argjson ports "$(_qa_ports_json "$d/ports")" \
333
+ '{lane: $lane, pid: $pid, log: $log, worktree: $wt, ports: $ports}' 2>/dev/null)"
334
+ emit_event op.start qa.launch "$payload"
335
+ echo "[qa $lane] launched (pid $pid) · $(tr '\n' ' ' < "$d/ports")· log: $log" >&2
336
+ printf '%s\n' "$pid"
337
+ }
338
+
339
+ # qa_ready <lane> [<timeout-seconds>] — poll every readiness probe until ALL answer in one round.
340
+ # rc 0 usable · rc 1 not usable (timed out, or the command exited first) · rc 2 config refusal.
341
+ # Never reports usable on a partial round: a web 200 with the API probe failing is not usable.
342
+ qa_ready() {
343
+ local lane="$1" limit="${2:-${QA_READY_TIMEOUT:-120}}" every="${QA_READY_INTERVAL:-2}"
344
+ local cfg="" d="" log="" urls="" name="" url="" code="" pending="" start="" now="" waited=0 tab
345
+ tab="$(printf '\t')"
346
+ _qa_need || return $?
347
+ case "$limit" in ''|*[!0-9]*) echo "cckit qa: --timeout takes whole seconds, got '$limit'" >&2; return 2 ;; esac
348
+ cfg="$(qa_cfg)" || { echo "cckit qa: no kit config found" >&2; return 2; }
349
+ d="$(qa_lane_dir "$lane")"; log="$d/launch.log"
350
+ if [ ! -f "$d/ports" ]; then
351
+ echo "cckit qa: lane $lane was never launched (no $d/ports) — run cckit qa up" >&2
352
+ return 2
353
+ fi
354
+ urls="$(qa_probe_urls "$cfg" "$d/ports")" || return $?
355
+
356
+ start="$(date +%s)"
357
+ while :; do
358
+ pending=""
359
+ while IFS="$tab" read -r name url; do
360
+ [ -n "$name" ] || continue
361
+ code="$(qa_probe "$url")" || pending="$pending $name $url -> $code;"
362
+ done <<EOF
363
+ $urls
364
+ EOF
365
+ now="$(date +%s)"; waited=$(( now - start ))
366
+ if [ -z "$pending" ]; then
367
+ emit_event op.done qa.ready "$(jq -cn --arg lane "$lane" --argjson s "$waited" \
368
+ --argjson ports "$(_qa_ports_json "$d/ports")" '{lane: $lane, seconds: $s, ports: $ports}' 2>/dev/null)"
369
+ echo "[qa $lane] usable after ${waited}s — every readiness probe answered" >&2
370
+ return 0
371
+ fi
372
+ # A recorded command that has exited will never answer; say so now rather than at the timeout.
373
+ if [ -f "$d/pid" ] && ! _qa_pid_live "$lane"; then
374
+ emit_event op.done qa.exited "$(jq -cn --arg lane "$lane" --arg log "$log" --arg pending "${pending# }" \
375
+ '{lane: $lane, log: $log, pending: $pending}' 2>/dev/null)"
376
+ echo "cckit qa: lane $lane is NOT usable — the stack command exited before it was ready." >&2
377
+ echo " not answering:${pending%;}" >&2
378
+ echo " read the launch log: $log" >&2
379
+ return 1
380
+ fi
381
+ if [ "$waited" -ge "$limit" ]; then
382
+ emit_event op.done qa.timeout "$(jq -cn --arg lane "$lane" --arg log "$log" --argjson s "$limit" \
383
+ --arg pending "${pending# }" '{lane: $lane, timeout: $s, log: $log, pending: $pending}' 2>/dev/null)"
384
+ echo "cckit qa: lane $lane is NOT usable — timed out after ${limit}s." >&2
385
+ echo " not answering:${pending%;}" >&2
386
+ echo " read the launch log: $log" >&2
387
+ return 1
388
+ fi
389
+ sleep "$every" 2>/dev/null || sleep 1
390
+ done
391
+ }
392
+
393
+ # ── the route: declared by the work, else configured (#297) ────────────────────────────────────
394
+
395
+ # qa_route_valid <route> — rc 0 when <route> is one line matching _QA_ROUTE_RE.
396
+ qa_route_valid() {
397
+ case "${1:-}" in *'
398
+ '*) return 1 ;; esac
399
+ grep -qE "$_QA_ROUTE_RE" <<<"${1:-}"
400
+ }
401
+
402
+ # qa_route_declared — stdin is a PR body; echo the route its first usable `QA route:` line names.
403
+ # Tolerates the shapes people write: a list bullet, bold markers, backticks around the value, any
404
+ # letter case. A line whose value is not a route (a template's `QA route: <path>` placeholder) is
405
+ # skipped, so an unfilled template reads as "none declared". rc 1 when there is none. Pure.
406
+ qa_route_declared() {
407
+ local r=""
408
+ r="$(tr -d '\r' \
409
+ | sed -nE 's/^[[:space:]]*([-*][[:space:]]+)?\**[Qq][Aa] [Rr][Oo][Uu][Tt][Ee]\**:\**[[:space:]]*`?([^`[:space:]]*).*$/\2/p' \
410
+ | grep -E "$_QA_ROUTE_RE" | sed -n 1p)"
411
+ [ -n "$r" ] || return 1
412
+ printf '%s\n' "$r"
413
+ }
414
+
415
+ # _qa_lane_branch <lane> — the local branch whose issue number is <lane>: the current branch when
416
+ # it matches, else the first local head that does. Echoes nothing when there is none.
417
+ _qa_lane_branch() {
418
+ local lane="$1" b=""
419
+ b="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)" || b=""
420
+ if [ -n "$b" ] && [ "$(wt_issue_number "$b" 2>/dev/null)" = "$lane" ]; then
421
+ printf '%s\n' "$b"; return 0
422
+ fi
423
+ git for-each-ref --format='%(refname:short)' refs/heads 2>/dev/null | while IFS= read -r b; do
424
+ [ "$(wt_issue_number "$b" 2>/dev/null)" = "$lane" ] || continue
425
+ printf '%s\n' "$b"; break
426
+ done
427
+ }
428
+
429
+ # qa_route <lane> [<pr>] — echo `<source>\t<route>`: `declared` when the lane's PR body carries a
430
+ # `QA route:` line, else `configured` with `qa.route` (`/` when the config has none). The PR is
431
+ # <pr> when given, else the one open from the lane's branch. Reading the PR is best-effort — no
432
+ # gh, no PR yet, or an unreadable body all fall back to the configured route. rc 2 only when the
433
+ # CONFIGURED route is not a route, since then there is nothing valid to fall back to.
434
+ qa_route() {
435
+ local lane="$1" pr="${2:-}" cfg="" ref="" body="" route="" tab
436
+ tab="$(printf '\t')"
437
+ ref="$pr"
438
+ [ -n "$ref" ] || ref="$(_qa_lane_branch "$lane")"
439
+ if [ -n "$ref" ] && command -v gh >/dev/null 2>&1; then
440
+ body="$(gh pr view "$ref" --json body --jq '.body // ""' 2>/dev/null)" || body=""
441
+ if [ -n "$body" ] && route="$(printf '%s\n' "$body" | qa_route_declared)"; then
442
+ printf 'declared%s%s\n' "$tab" "$route"
443
+ return 0
444
+ fi
445
+ fi
446
+ cfg="$(qa_cfg)" || { echo "cckit qa: no kit config found" >&2; return 2; }
447
+ route="$(jq -r '.qa.route // "/"' "$cfg" 2>/dev/null)"
448
+ [ -n "$route" ] || route="/"
449
+ if ! qa_route_valid "$route"; then
450
+ echo "cckit qa: qa.route in $cfg is not a route — it must start with / and hold no spaces" >&2
451
+ return 2
452
+ fi
453
+ printf 'configured%s%s\n' "$tab" "$route"
454
+ }
455
+
456
+ # qa_usable <lane> — ONE probe round against a launched lane: rc 0 when every readiness probe
457
+ # answers now, rc 1 (the probes still failing on stderr) when one does not, rc 2 when the lane was
458
+ # never launched. Unlike qa_ready it does not wait and emits no event — it is a question, not a wait.
459
+ qa_usable() {
460
+ local lane="$1" cfg="" d="" urls="" name="" url="" code="" pending="" tab
461
+ tab="$(printf '\t')"
462
+ cfg="$(qa_cfg)" || { echo "cckit qa: no kit config found" >&2; return 2; }
463
+ d="$(qa_lane_dir "$lane")"
464
+ if [ ! -f "$d/ports" ]; then
465
+ echo "cckit qa: lane $lane was never launched (no $d/ports) — run cckit qa up" >&2
466
+ return 2
467
+ fi
468
+ urls="$(qa_probe_urls "$cfg" "$d/ports")" || return $?
469
+ while IFS="$tab" read -r name url; do
470
+ [ -n "$name" ] || continue
471
+ code="$(qa_probe "$url")" || pending="$pending $name $url -> $code;"
472
+ done <<EOF
473
+ $urls
474
+ EOF
475
+ [ -z "$pending" ] && return 0
476
+ echo " not answering:${pending%;}" >&2
477
+ return 1
478
+ }
479
+
480
+ # qa_open <lane> [<pr>] [<route>] [<print-only>] [<port-env>] — hand the reviewer the screen the
481
+ # change touched: `http://<host>:<lane port><route>` on stdout, and opened in a browser unless
482
+ # <print-only> is 1. An explicit <route> wins over the PR's declared one.
483
+ #
484
+ # Refuses a lane that is not usable RIGHT NOW (one probe round): a browser on a stack that is
485
+ # still booting shows a connection error the reviewer reads as a broken change. rc 1 not usable ·
486
+ # rc 2 never launched or a bad route/port variable. With no opener on the machine the URL is still
487
+ # printed and the rc is 0 — the URL is the hand-over, the browser is a convenience.
488
+ qa_open() {
489
+ local lane="$1" pr="${2:-}" route="${3:-}" only="${4:-0}" penv="${5:-}"
490
+ local d="" row="" src="" port="" url="" opener="" opened=false why="" rc=0 tab
491
+ tab="$(printf '\t')"
492
+ _qa_need || return $?
493
+ d="$(qa_lane_dir "$lane")"
494
+ # qa_usable writes only to stderr, so its whole output is the reason.
495
+ why="$(qa_usable "$lane" 2>&1)" || rc=$?
496
+ if [ "$rc" -eq 1 ]; then
497
+ echo "cckit qa: lane $lane is NOT usable — refusing to open a browser on it." >&2
498
+ [ -z "$why" ] || printf '%s\n' "$why" >&2
499
+ echo " wait for it with: cckit qa ready --lane $lane · launch log: $d/launch.log" >&2
500
+ return 1
501
+ elif [ "$rc" -ne 0 ]; then
502
+ [ -z "$why" ] || printf '%s\n' "$why" >&2
503
+ return "$rc"
504
+ fi
505
+
506
+ if [ -n "$route" ]; then
507
+ qa_route_valid "$route" || { echo "cckit qa: --route '$route' is not a route — it must start with / and hold no spaces" >&2; return 2; }
508
+ src="flag"
509
+ else
510
+ row="$(qa_route "$lane" "$pr")" || return $?
511
+ src="${row%%"$tab"*}"; route="${row#*"$tab"}"
512
+ fi
513
+
514
+ if [ -n "$penv" ]; then
515
+ port="$(awk -F= -v n="$penv" '$1 == n { print $2; exit }' "$d/ports" 2>/dev/null)"
516
+ else
517
+ port="$(awk -F= 'NF > 1 { print $2; exit }' "$d/ports" 2>/dev/null)"
518
+ fi
519
+ case "$port" in
520
+ ''|*[!0-9]*)
521
+ echo "cckit qa: lane $lane has no port for '${penv:-the first qa.ports variable}' — its variables: $(cut -d= -f1 "$d/ports" | tr '\n' ' ')" >&2
522
+ return 2 ;;
523
+ esac
524
+ url="http://${QA_HOST:-localhost}:$port$route"
525
+
526
+ if [ "$only" != 1 ]; then
527
+ opener="${QA_OPENER:-}"
528
+ if [ -z "$opener" ]; then
529
+ if command -v open >/dev/null 2>&1; then opener=open
530
+ elif command -v xdg-open >/dev/null 2>&1; then opener=xdg-open
531
+ fi
532
+ fi
533
+ if [ -z "$opener" ]; then
534
+ echo "[qa $lane] no browser opener here (open, xdg-open, or QA_OPENER) — open the URL by hand" >&2
535
+ elif "$opener" "$url" >/dev/null 2>&1; then
536
+ opened=true
537
+ else
538
+ echo "[qa $lane] '$opener' could not open the URL — open it by hand" >&2
539
+ fi
540
+ fi
541
+
542
+ emit_event op.done qa.open "$(jq -cn --arg lane "$lane" --arg url "$url" --arg route "$route" \
543
+ --arg source "$src" --argjson opened "$opened" \
544
+ '{lane: $lane, url: $url, route: $route, source: $source, opened: $opened}' 2>/dev/null)"
545
+ echo "[qa $lane] $src route $route" >&2
546
+ if [ "${CCKIT_OUTPUT:-human}" = json ]; then
547
+ jq -cn --arg lane "$lane" --arg url "$url" --arg route "$route" --arg source "$src" \
548
+ --argjson opened "$opened" '{lane: $lane, url: $url, route: $route, source: $source, opened: $opened}'
549
+ else
550
+ printf '%s\n' "$url"
551
+ fi
552
+ }
553
+
554
+ # ── stop: free the ports for real (#298) ───────────────────────────────────────────────────────
555
+ #
556
+ # A dev stack is a process tree, and the process holding a port is rarely the one that was
557
+ # launched: `pnpm dev` starts a runner that starts a watcher that starts the server. Killing the
558
+ # listener alone frees the port for as long as its supervisor takes to respawn it. So a stop
559
+ # resolves two sets and orders them:
560
+ #
561
+ # owned every descendant of the recorded pid, plus every member of the lane's process group
562
+ # (`qa/<lane>/pgid`) — the group still names them after the root died and its children
563
+ # were reparented to pid 1.
564
+ # holders whoever LISTENS on each recorded port right now (lsof, else ss).
565
+ #
566
+ # Everything that is not a holder is a supervisor and is stopped first, parents before children;
567
+ # the holders go last. The holders are then resolved again, because only a port with no listener
568
+ # proves the stack is down — a listener that came back names a supervisor the first round missed.
569
+ #
570
+ # A holder is stopped only when it is OWNED. A listener on the lane's port that is neither in the
571
+ # launched tree nor in the lane's group is somebody else's server (a dev server that took the port
572
+ # after the launch-time check, or a stack whose root died with no group on record): `stop` names
573
+ # it, leaves it running and exits 1. `--force` stops it too, with its ancestors for as long as they
574
+ # share its process group. That climb is bounded on purpose: it never reaches pid 1, this shell, or
575
+ # any ancestor of this shell, and it stops at a process-group boundary, so a forced stop costs that
576
+ # server and its own job, not the terminal it was started from.
577
+
578
+ # _qa_pid_started <pid> — the process start time as ps prints it; identifies a pid across reuse.
579
+ _qa_pid_started() {
580
+ local s=""
581
+ s="$(ps -o lstart= -p "$1" 2>/dev/null)" || return 1
582
+ [ -n "$s" ] || return 1
583
+ printf '%s\n' "$s" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//'
584
+ }
585
+
586
+ # _qa_alive <pid> — rc 0 when <pid> is a running process. A zombie is not: it holds no port and
587
+ # cannot respawn anything, it is only waiting for its parent to read its exit code.
588
+ _qa_alive() {
589
+ local st=""
590
+ kill -0 "$1" 2>/dev/null || return 1
591
+ st="$(ps -o stat= -p "$1" 2>/dev/null)" || return 1
592
+ case "$st" in ''|*Z*) return 1 ;; esac
593
+ return 0
594
+ }
595
+
596
+ # qa_port_tool — echo the tool that can name a port's holder: `lsof`, else `ss`. rc 1 when there
597
+ # is neither (or QA_PORT_TOOL=none). QA_PORT_TOOL forces one.
598
+ qa_port_tool() {
599
+ case "${QA_PORT_TOOL:-}" in
600
+ none) return 1 ;;
601
+ lsof|ss) command -v "$QA_PORT_TOOL" >/dev/null 2>&1 || return 1; printf '%s\n' "$QA_PORT_TOOL"; return 0 ;;
602
+ esac
603
+ if command -v lsof >/dev/null 2>&1; then printf 'lsof\n'
604
+ elif command -v ss >/dev/null 2>&1; then printf 'ss\n'
605
+ else return 1
606
+ fi
607
+ }
608
+
609
+ # qa_port_pids <port> [<tool>] — the pids LISTENING on TCP <port>, one per line, deduplicated.
610
+ # Empty output is "no listener this user can see": ss names a pid only for the caller's own
611
+ # processes, and so does lsof without root.
612
+ qa_port_pids() {
613
+ local port="$1" tool="${2:-}"
614
+ case "$port" in ''|*[!0-9]*) return 0 ;; esac
615
+ [ -n "$tool" ] || tool="$(qa_port_tool)" || return 1
616
+ case "$tool" in
617
+ lsof) lsof -nP -iTCP:"$port" -sTCP:LISTEN -t 2>/dev/null | grep -E '^[0-9]+$' | sort -un ;;
618
+ ss) ss -H -ltnp "sport = :$port" 2>/dev/null | grep -oE 'pid=[0-9]+' | cut -d= -f2 | sort -un ;;
619
+ *) return 1 ;;
620
+ esac
621
+ return 0
622
+ }
623
+
624
+ # qa_port_held <port> [<tool>] — rc 0 when something LISTENS on TCP <port>, whether or not its pid
625
+ # can be named: ss lists another user's listening socket without a pid, so "no pid" is not "free".
626
+ # lsof without root shows only the caller's own sockets; there the two answers are the same.
627
+ qa_port_held() {
628
+ local port="$1" tool="${2:-}"
629
+ case "$port" in ''|*[!0-9]*) return 1 ;; esac
630
+ [ -n "$tool" ] || tool="$(qa_port_tool)" || return 1
631
+ case "$tool" in
632
+ ss) [ -n "$(ss -H -ltn "sport = :$port" 2>/dev/null)" ] ;;
633
+ *) [ -n "$(qa_port_pids "$port" "$tool")" ] ;;
634
+ esac
635
+ }
636
+
637
+ # _qa_lane_holders <ports-file> <tool> — the pids listening on any recorded port, one per line.
638
+ _qa_lane_holders() {
639
+ local pfile="$1" tool="$2" port=""
640
+ [ -n "$tool" ] && [ -f "$pfile" ] || return 0
641
+ cut -d= -f2 "$pfile" 2>/dev/null | while IFS= read -r port; do
642
+ qa_port_pids "$port" "$tool"
643
+ done | sort -un
644
+ }
645
+
646
+ # _qa_stop_plan <root> <group> <holders> [<force>] — the ordered stop list, one `sup <pid>` or
647
+ # `lis <pid>` line each: supervisors parents-first, then the holders. <root> and <group> may be
648
+ # empty; <holders> is whitespace-separated pids. Reads one process snapshot; zombies are left out.
649
+ # A holder outside the owned tree and group is planned only when <force> is 1, and only then are
650
+ # its ancestors climbed: an owned holder's supervisors are already in the tree or the group.
651
+ _qa_stop_plan() {
652
+ # shellcheck disable=SC2009 # the whole table is the input — pgrep cannot give ppid and pgid
653
+ ps -A -o pid= -o ppid= -o pgid= -o stat= 2>/dev/null | awk -v root="$1" -v grp="$2" -v holders="$3" -v force="${4:-0}" -v self="$$" '
654
+ $4 !~ /Z/ { pp[$1] = $2; pg[$1] = $3 }
655
+ END {
656
+ mine[1] = 1
657
+ for (a = self; (a in pp) && a > 1 && !(a in mine); a = pp[a]) mine[a] = 1
658
+ for (p in pp) {
659
+ if (p in mine) continue
660
+ if (grp != "" && pg[p] == grp) { own[p] = 1; continue }
661
+ if (root == "") continue
662
+ n = 0
663
+ for (a = p; (a in pp) && a > 1 && n < 200; a = pp[a]) { if (a == root) { own[p] = 1; break }; n++ }
664
+ }
665
+ nh = split(holders, hs, /[ \t\n]+/)
666
+ for (i = 1; i <= nh; i++) {
667
+ h = hs[i]
668
+ if (h == "" || !(h in pp) || (h in mine)) continue
669
+ if (h in own) { lis[h] = 1; continue }
670
+ if (force != 1) continue
671
+ own[h] = 1; lis[h] = 1
672
+ n = 0
673
+ for (a = pp[h]; (a in pp) && a > 1 && !(a in mine) && pg[a] == pg[h] && n < 200; a = pp[a]) { own[a] = 1; n++ }
674
+ }
675
+ for (p in own) {
676
+ depth = 0
677
+ for (a = p; (a in pp) && a > 1 && depth < 200; a = pp[a]) depth++
678
+ printf "%s %d %s\n", ((p in lis) ? "1 lis" : "0 sup"), depth, p
679
+ }
680
+ }' | sort -k1,1n -k3,3n -k4,4n | awk '{ print $2, $4 }'
681
+ }
682
+
683
+ # _qa_kill_wait <pids> — TERM each pid in the order given (one per line), give them QA_STOP_GRACE
684
+ # seconds to exit, then KILL what is left. Echoes every pid it signalled.
685
+ _qa_kill_wait() {
686
+ local pids="$1" p="" left="" ticks=0 limit=0
687
+ [ -n "$pids" ] || return 0
688
+ limit=$(( ${QA_STOP_GRACE:-5} * 10 ))
689
+ while IFS= read -r p; do
690
+ [ -n "$p" ] || continue
691
+ _qa_alive "$p" || continue
692
+ kill -TERM "$p" 2>/dev/null && printf '%s\n' "$p"
693
+ done <<EOF
694
+ $pids
695
+ EOF
696
+ while :; do
697
+ left=""
698
+ while IFS= read -r p; do
699
+ [ -n "$p" ] || continue
700
+ _qa_alive "$p" && left="$left$p
701
+ "
702
+ done <<EOF
703
+ $pids
704
+ EOF
705
+ [ -z "$left" ] && return 0
706
+ [ "$ticks" -ge "$limit" ] && break
707
+ sleep 0.1 2>/dev/null || sleep 1
708
+ ticks=$(( ticks + 1 ))
709
+ done
710
+ while IFS= read -r p; do
711
+ [ -n "$p" ] || continue
712
+ kill -KILL "$p" 2>/dev/null || true
713
+ done <<EOF
714
+ $left
715
+ EOF
716
+ ticks=0
717
+ while [ "$ticks" -lt 20 ]; do
718
+ left=""
719
+ while IFS= read -r p; do
720
+ [ -n "$p" ] || continue
721
+ _qa_alive "$p" && left=1
722
+ done <<EOF
723
+ $pids
724
+ EOF
725
+ [ -z "$left" ] && return 0
726
+ sleep 0.1 2>/dev/null || sleep 1
727
+ ticks=$(( ticks + 1 ))
728
+ done
729
+ return 0
730
+ }
731
+
732
+ # qa_stop <lane> [<force>] — stop the lane's stack and say what actually stopped. <force> = 1 also
733
+ # stops a listener on the lane's ports that the lane did not launch.
734
+ #
735
+ # rc 0 when the lane is down: every recorded port has no listener and nothing signalled survived.
736
+ # That includes a lane that was already down (or never launched) — "nothing stopped" is a true
737
+ # report, not an error, so the op is safe to re-run. rc 1 when a port is still held or a process
738
+ # outlived KILL (another user's process, typically); the record is kept so a retry can find it.
739
+ # A port held by a process outside the launched tree and group is rc 1 as well: it is left running
740
+ # and named, and only <force> stops it.
741
+ #
742
+ # With neither lsof nor ss the holders cannot be resolved: the recorded tree and group are still
743
+ # stopped, and the report says the ports were NOT checked rather than calling them free.
744
+ qa_stop() {
745
+ local lane="$1" force="${2:-0}" d="" pid="" root="" grp="" tool="" holders="" plan="" sups="" liss="" tried="" got=""
746
+ local stopped="" alive="" held="" foreign="" p="" round=0 rounds="${QA_STOP_ROUNDS:-5}" freed="null" n=0 rc=0
747
+ d="$(qa_lane_dir "$lane")"
748
+ if [ ! -f "$d/ports" ] && [ ! -f "$d/pid" ]; then
749
+ echo "[qa $lane] nothing stopped — the lane has no running stack on record" >&2
750
+ [ "${CCKIT_OUTPUT:-human}" = json ] && printf '{"lane":"%s","stopped":[],"held":[],"freed":null}\n' "$lane"
751
+ return 0
752
+ fi
753
+ case "$rounds" in ''|*[!0-9]*) rounds=5 ;; esac
754
+ tool="$(qa_port_tool)" || tool=""
755
+
756
+ # The recorded pid is trusted as the tree root only while it is the SAME process: a pid recycled
757
+ # after a reboot has another start time, and stopping its tree would stop a stranger.
758
+ pid="$(cat "$d/pid" 2>/dev/null)" || pid=""
759
+ case "$pid" in ''|*[!0-9]*) pid="" ;; esac
760
+ if [ -n "$pid" ] && _qa_alive "$pid"; then
761
+ if [ ! -f "$d/started" ] || [ "$(_qa_pid_started "$pid")" = "$(cat "$d/started" 2>/dev/null)" ]; then
762
+ root="$pid"
763
+ [ -f "$d/pgid" ] && grp="$pid"
764
+ fi
765
+ elif [ -n "$pid" ] && [ -f "$d/pgid" ]; then
766
+ # The leader is gone. A pid is not reused while its group has members, so these are ours.
767
+ grp="$pid"
768
+ fi
769
+
770
+ while [ "$round" -lt "$rounds" ]; do
771
+ round=$(( round + 1 ))
772
+ holders="$(_qa_lane_holders "$d/ports" "$tool" | tr '\n' ' ')"
773
+ plan="$(_qa_stop_plan "$root" "$grp" "$holders" "$force")"
774
+ [ -n "$plan" ] || break
775
+ sups="$(printf '%s\n' "$plan" | awk '$1 == "sup" { print $2 }')"
776
+ liss="$(printf '%s\n' "$plan" | awk '$1 == "lis" { print $2 }')"
777
+ got="$(_qa_kill_wait "$sups")"; [ -z "$got" ] || tried="$tried$got
778
+ "
779
+ got="$(_qa_kill_wait "$liss")"; [ -z "$got" ] || tried="$tried$got
780
+ "
781
+ done
782
+
783
+ # Report from what is true NOW, not from what was signalled.
784
+ while IFS= read -r p; do
785
+ [ -n "$p" ] || continue
786
+ if _qa_alive "$p"; then alive="$alive $p"; else stopped="$stopped $p"; n=$(( n + 1 )); fi
787
+ done <<EOF
788
+ $(printf '%s' "$tried" | sort -un)
789
+ EOF
790
+ stopped="${stopped# }"; alive="${alive# }"
791
+ if [ -n "$tool" ]; then
792
+ held="$(_qa_lane_holders "$d/ports" "$tool" | tr '\n' ' ')"; held="${held% }"
793
+ if [ -z "$held" ]; then freed=true; else freed=false; fi
794
+ fi
795
+ [ -z "$held" ] && [ -z "$alive" ] || rc=1
796
+
797
+ emit_event op.done qa.stop "$(jq -cn --arg lane "$lane" --arg stopped "$stopped" --arg held "$held" \
798
+ --argjson freed "$freed" --argjson ports "$(_qa_ports_json "$d/ports")" \
799
+ '{lane: $lane, stopped: ($stopped | split(" ") | map(tonumber)), ports: $ports,
800
+ held: ($held | split(" ") | map(tonumber)), freed: $freed}' 2>/dev/null)"
801
+
802
+ if [ "$n" -eq 0 ]; then
803
+ echo "[qa $lane] nothing stopped — no process of this lane was running" >&2
804
+ else
805
+ echo "[qa $lane] stopped $n process(es): $stopped" >&2
806
+ fi
807
+ if [ -n "$alive" ]; then
808
+ echo "cckit qa: lane $lane — still running after KILL (not yours to signal?): $alive" >&2
809
+ fi
810
+ if [ -n "$held" ]; then
811
+ echo "cckit qa: lane $lane is NOT down — still listening on its ports ($(cut -d= -f2 "$d/ports" 2>/dev/null | tr '\n' ' ')): pid $held" >&2
812
+ foreign=""
813
+ for p in $(printf '%s\n' "$held"); do
814
+ grep -qx "$p" <<<"$tried" || foreign="$foreign $p"
815
+ done
816
+ if [ "$force" != 1 ] && [ -n "$foreign" ]; then
817
+ echo " this lane did not launch pid${foreign}, so it was left running. Stop it yourself, or run cckit qa stop --lane $lane --force to stop whatever listens there." >&2
818
+ fi
819
+ elif [ -z "$tool" ]; then
820
+ echo "[qa $lane] ports NOT checked — neither lsof nor ss is installed, so a listener outside the launched tree would go unseen" >&2
821
+ else
822
+ echo "[qa $lane] ports free: $(cut -d= -f2 "$d/ports" 2>/dev/null | tr '\n' ' ')" >&2
823
+ fi
824
+ if [ "${CCKIT_OUTPUT:-human}" = json ]; then
825
+ jq -cn --arg lane "$lane" --arg stopped "$stopped" --arg held "$held" --argjson freed "$freed" \
826
+ '{lane: $lane, stopped: ($stopped | split(" ") | map(tonumber)),
827
+ held: ($held | split(" ") | map(tonumber)), freed: $freed}'
828
+ fi
829
+ # A lane that is down has no record: `ready` and `open` then say "never launched", which is true.
830
+ [ "$rc" -eq 0 ] && rm -f "$d/pid" "$d/pgid" "$d/started" "$d/ports"
831
+ return "$rc"
832
+ }
833
+
834
+ # ── the verdict: pass or fail against the commit that was reviewed (#299) ──────────────────────
835
+ #
836
+ # A verdict is a `qa` stage receipt (stage-receipt.sh) keyed by the lane number, and its head_sha
837
+ # is the commit the reviewer looked at. That one field is what makes it expire: the receipt is
838
+ # read back against the lane's head NOW, and any other commit reads `stale`. Nothing has to notice
839
+ # the push — the comparison fails on its own.
840
+ #
841
+ # The commit is resolved locally first, because the stack ran from a local checkout: `--head`,
842
+ # else the tip of the PR's branch as this repo has it, else the tip of the lane's own branch, else
843
+ # the head GitHub reports for the PR. When the PR's head on GitHub is a different commit the
844
+ # verdict is recorded anyway and the mismatch is said out loud — it reads stale against the PR
845
+ # until the reviewed commit is pushed.
846
+ #
847
+ # Three writes follow the receipt, in this order, and none of them can undo it:
848
+ # the PR comment one comment, upserted through pr_attach_evidence under the caption
849
+ # `QA verdict`, so a second verdict EDITS it. Each `--shot` file is one more
850
+ # upserted comment (`QA screenshot <n>`). pr-evidence.sh cannot upload a binary:
851
+ # a screenshot is a local path reference unless KIT_EVIDENCE_URL_BASE is set.
852
+ # the mail a FAIL sends its notes to the builder's branch with msg_send --steer, so the
853
+ # agent that built the change reads them between two tool calls.
854
+ # the event one `op.done qa.verdict` line.
855
+ # The receipt is not mirrored onto the issue: the PR comment is the GitHub copy, and a second
856
+ # comment on the lane's issue would say the same thing in another place.
857
+
858
+ # _qa_pr_meta <ref> — number, url, headRefOid, headRefName of the PR <ref> names, one per line.
859
+ # Best-effort: no gh, no PR, or an unreadable answer is rc 1 and prints nothing.
860
+ _qa_pr_meta() {
861
+ local out=""
862
+ [ -n "${1:-}" ] || return 1
863
+ command -v gh >/dev/null 2>&1 || return 1
864
+ out="$(gh pr view "$1" --json number,url,headRefOid,headRefName \
865
+ --jq '(.number | tostring), .url, .headRefOid, .headRefName' 2>/dev/null)" || return 1
866
+ case "$(printf '%s\n' "$out" | sed -n 1p)" in ''|*[!0-9]*) return 1 ;; esac
867
+ printf '%s\n' "$out"
868
+ }
869
+
870
+ # _qa_verdict_resolve <lane> [<pr>] [<head>] — set _QA_V_HEAD (the reviewed commit), _QA_V_PR,
871
+ # _QA_V_URL, _QA_V_PRHEAD and _QA_V_BRANCH (the builder's branch). Globals, not a tab-separated
872
+ # echo: an empty field between two tabs collapses under `read`. rc 2 when no commit can be named.
873
+ _qa_verdict_resolve() {
874
+ local lane="$1" pr="${2:-}" head="${3:-}" branch="" meta="" tip=""
875
+ _QA_V_HEAD=""; _QA_V_PR=""; _QA_V_URL=""; _QA_V_PRHEAD=""; _QA_V_BRANCH=""
876
+ branch="$(_qa_lane_branch "$lane")"
877
+ if meta="$(_qa_pr_meta "${pr:-$branch}")"; then
878
+ _QA_V_PR="$(printf '%s\n' "$meta" | sed -n 1p)"
879
+ _QA_V_URL="$(printf '%s\n' "$meta" | sed -n 2p)"
880
+ _QA_V_PRHEAD="$(printf '%s\n' "$meta" | sed -n 3p)"
881
+ _QA_V_BRANCH="$(printf '%s\n' "$meta" | sed -n 4p)"
882
+ fi
883
+ [ -n "$_QA_V_BRANCH" ] || _QA_V_BRANCH="$branch"
884
+ if [ -n "$head" ]; then
885
+ tip="$(git rev-parse --verify -q "$head^{commit}" 2>/dev/null)" || tip=""
886
+ if [ -z "$tip" ]; then
887
+ # A commit this clone does not have is still a commit: accept a full sha as written.
888
+ grep -qE '^[0-9a-f]{40}$' <<<"$head" || {
889
+ echo "cckit qa verdict: --head '$head' is not a commit here and not a full sha" >&2; return 2; }
890
+ tip="$head"
891
+ fi
892
+ else
893
+ [ -z "$_QA_V_BRANCH" ] || tip="$(git rev-parse --verify -q "refs/heads/$_QA_V_BRANCH^{commit}" 2>/dev/null)" || tip=""
894
+ [ -n "$tip" ] || tip="$_QA_V_PRHEAD"
895
+ fi
896
+ [ -n "$tip" ] || {
897
+ echo "cckit qa verdict: no commit to bind lane $lane to — no local branch carries issue $lane and no PR answered; pass --head <sha>" >&2
898
+ return 2; }
899
+ _QA_V_HEAD="$tip"
900
+ }
901
+
902
+ # qa_verdict_state <lane> [<head>] — echo how the lane's latest verdict reads against <head>:
903
+ # `pass` (rc 0) · `fail` · `stale` (recorded against another commit, or against none) · `none`
904
+ # (no verdict at all). Every answer but `pass` is rc 1. An empty <head> cannot be compared, so it
905
+ # reads `stale`: a verdict nobody can check against a commit is not a fresh one.
906
+ qa_verdict_state() {
907
+ local lane="$1" head="${2:-}" got="" rhead="" gate=""
908
+ got="$(sr_latest "$lane" qa 2>/dev/null)" || got=""
909
+ [ -n "$got" ] || { printf 'none\n'; return 1; }
910
+ rhead="$(printf '%s' "$got" | jq -r '.head_sha // ""' 2>/dev/null)"
911
+ if [ -z "$head" ] || [ -z "$rhead" ] || [ "$rhead" != "$head" ]; then
912
+ printf 'stale\n'; return 1
913
+ fi
914
+ gate="$(printf '%s' "$got" | jq -r '.gate // ""' 2>/dev/null)"
915
+ gate="${gate%%[[:space:]]*}"
916
+ [ "$gate" = pass ] && { printf 'pass\n'; return 0; }
917
+ printf 'fail\n'; return 1
918
+ }
919
+
920
+ # qa_verdict <lane> <pass|fail> [<pr>] [<head>] [<notes>] [<to-branch>] [<shots>] — record the
921
+ # verdict, then upsert it onto the PR, mail a fail's notes to the builder, and emit one event.
922
+ # <shots> is one screenshot path per line. Echoes the receipt path (a JSON object under --llm).
923
+ #
924
+ # Everything that can refuse does so before the receipt is written: an unknown verdict, a fail
925
+ # with no notes, a screenshot that is not a file, a commit that cannot be named (all rc 2).
926
+ # rc 1 means the receipt IS written and a fail's notes reached no mailbox.
927
+ qa_verdict() {
928
+ local lane="$1" verdict="${2:-}" pr="${3:-}" head="${4:-}" notes="${5:-}" to="${6:-}" shots="${7:-}"
929
+ local one="" outcome="" gate="" blocker="" next="" url="" attempt="" prev="" rpath="" d="" file=""
930
+ local comment="no-pr" mailed=false shot="" i=0 nshots=0 rc=0 target=""
931
+ case "$verdict" in pass|fail) ;; *) echo "cckit qa verdict: the verdict is pass or fail, got '${verdict:-nothing}'" >&2; return 2 ;; esac
932
+ command -v jq >/dev/null 2>&1 || { echo "cckit qa: jq is required" >&2; return 3; }
933
+ one="$(printf '%s' "$notes" | tr '\r\n\t' ' ' | sed -e 's/ */ /g' -e 's/^ //' -e 's/ $//')"
934
+ if [ "$verdict" = fail ] && [ -z "$one" ]; then
935
+ echo "cckit qa verdict: a fail needs its notes — --note \"<what is wrong>\" or --notes-file <file>. The builder gets nothing to act on otherwise." >&2
936
+ return 2
937
+ fi
938
+ while IFS= read -r shot; do
939
+ [ -n "$shot" ] || continue
940
+ [ -f "$shot" ] || { echo "cckit qa verdict: --shot '$shot' is not a file" >&2; return 2; }
941
+ nshots=$((nshots + 1))
942
+ done <<EOF
943
+ $shots
944
+ EOF
945
+ _qa_verdict_resolve "$lane" "$pr" "$head" || return $?
946
+ head="$_QA_V_HEAD"
947
+
948
+ url="${_QA_V_URL:--}"
949
+ if [ "$verdict" = pass ]; then
950
+ outcome="pr-open"; gate="pass"; blocker="none"; next="none"
951
+ else
952
+ outcome="blocked"; gate="fail — $one"; blocker="$one"; next="build"
953
+ fi
954
+ # The same verdict on the same commit rewrites its own attempt; anything else is a new one.
955
+ attempt="$(sr_next_attempt "$lane" qa)" || return $?
956
+ if _sr_same_as_latest "$lane" qa "$outcome" "$url" "$gate" "$blocker" "$next"; then
957
+ prev="$(sr_latest "$lane" qa 2>/dev/null | jq -r '.head_sha // ""' 2>/dev/null)"
958
+ [ "$prev" = "$head" ] && attempt=$((attempt - 1))
959
+ fi
960
+ rpath="$(printf 'outcome: %s\nurl: %s\ngate: %s\nblocker: %s\nnext stage: %s\n' \
961
+ "$outcome" "$url" "$gate" "$blocker" "$next" \
962
+ | sr_record "$lane" qa "$attempt" "" "" "cckit qa verdict" "read-only" "$head")" || return $?
963
+ echo "[qa $lane] $verdict recorded against $head (attempt $attempt)" >&2
964
+ if [ -n "$_QA_V_PRHEAD" ] && [ "$_QA_V_PRHEAD" != "$head" ]; then
965
+ echo "[qa $lane] PR #$_QA_V_PR is at $_QA_V_PRHEAD on GitHub — this verdict reads stale against it until $head is pushed" >&2
966
+ fi
967
+
968
+ # The comment body carries no timestamp and no attempt number, so repeating a verdict is a
969
+ # byte-identical edit and not timeline churn.
970
+ if [ -n "$_QA_V_PR" ]; then
971
+ d="$(qa_lane_dir "$lane")"; file="$d/verdict.txt"
972
+ if mkdir -p "$d" 2>/dev/null; then
973
+ {
974
+ printf 'verdict: %s\ncommit: %s\nlane: %s\n' "$verdict" "$head" "$lane"
975
+ [ -z "$notes" ] || printf '\nnotes:\n%s\n' "$notes"
976
+ if [ "$nshots" -gt 0 ]; then
977
+ printf '\nscreenshots (one comment each, below):\n'
978
+ while IFS= read -r shot; do
979
+ [ -n "$shot" ] || continue
980
+ i=$((i + 1)); printf ' QA screenshot %s: %s\n' "$i" "$(basename "$shot")"
981
+ done <<EOF
982
+ $shots
983
+ EOF
984
+ fi
985
+ printf '\nA push after %s makes this verdict stale.\n' "$head"
986
+ } > "$file"
987
+ pr_attach_evidence "$_QA_V_PR" "$file" "QA verdict"
988
+ comment="${PR_EVIDENCE_LAST_RESULT:-post-failed}"
989
+ i=0
990
+ while IFS= read -r shot; do
991
+ [ -n "$shot" ] || continue
992
+ i=$((i + 1)); pr_attach_evidence "$_QA_V_PR" "$shot" "QA screenshot $i"
993
+ done <<EOF
994
+ $shots
995
+ EOF
996
+ else
997
+ comment="post-failed"; echo "[qa $lane] cannot create $d — nothing posted to PR #$_QA_V_PR" >&2
998
+ fi
999
+ else
1000
+ echo "[qa $lane] no pull request found for lane $lane — the verdict is recorded locally and posted nowhere (--pr <n> names one)" >&2
1001
+ fi
1002
+
1003
+ if [ "$verdict" = fail ]; then
1004
+ target="${to:-$_QA_V_BRANCH}"
1005
+ if [ -z "$target" ]; then
1006
+ echo "[qa $lane] no builder branch to mail — the notes reached nobody; name one with --to <branch>" >&2
1007
+ rc=1
1008
+ elif msg_send "$target" --steer "QA FAIL on $head (lane $lane${_QA_V_PR:+, PR #$_QA_V_PR}). Fix this before anything else:
1009
+ $notes"; then
1010
+ mailed=true
1011
+ else
1012
+ echo "[qa $lane] the mail to $target was not written — the notes reached nobody" >&2
1013
+ rc=1
1014
+ fi
1015
+ fi
1016
+
1017
+ emit_event op.done qa.verdict "$(jq -cn --arg lane "$lane" --arg verdict "$verdict" --arg head "$head" \
1018
+ --arg pr "$_QA_V_PR" --argjson attempt "$attempt" --arg comment "$comment" --argjson mailed "$mailed" \
1019
+ '{lane: $lane, verdict: $verdict, head: $head, pr: ($pr | tonumber? // null), attempt: $attempt,
1020
+ comment: $comment, mailed: $mailed}' 2>/dev/null)"
1021
+ if [ "${CCKIT_OUTPUT:-human}" = json ]; then
1022
+ jq -cn --arg lane "$lane" --arg verdict "$verdict" --arg head "$head" --arg pr "$_QA_V_PR" \
1023
+ --argjson attempt "$attempt" --arg receipt "$rpath" --arg comment "$comment" --argjson mailed "$mailed" \
1024
+ '{lane: $lane, verdict: $verdict, head: $head, pr: ($pr | tonumber? // null), attempt: $attempt,
1025
+ receipt: $receipt, comment: $comment, mailed: $mailed}'
1026
+ else
1027
+ printf '%s\n' "$rpath"
1028
+ fi
1029
+ return "$rc"
1030
+ }
1031
+
1032
+ # qa_verdict_show <lane> [<pr>] [<head>] — print how the lane's verdict reads against its head
1033
+ # now (qa_verdict_state's word; an object under --llm). rc 0 only for a fresh pass.
1034
+ qa_verdict_show() {
1035
+ local lane="$1" state="" rc=0 got="" rhead=""
1036
+ command -v jq >/dev/null 2>&1 || { echo "cckit qa: jq is required" >&2; return 3; }
1037
+ _qa_verdict_resolve "$lane" "${2:-}" "${3:-}" || return $?
1038
+ state="$(qa_verdict_state "$lane" "$_QA_V_HEAD")" || rc=1
1039
+ got="$(sr_latest "$lane" qa 2>/dev/null)" || got=""
1040
+ rhead="$(printf '%s' "$got" | jq -r '.head_sha // ""' 2>/dev/null)"
1041
+ case "$state" in
1042
+ none) echo "[qa $lane] no verdict recorded — cckit qa verdict <pass|fail>" >&2 ;;
1043
+ stale) echo "[qa $lane] the verdict was recorded against ${rhead:-no commit}; the lane is at $_QA_V_HEAD now — review it again" >&2 ;;
1044
+ *) echo "[qa $lane] $state against $_QA_V_HEAD" >&2 ;;
1045
+ esac
1046
+ if [ "${CCKIT_OUTPUT:-human}" = json ]; then
1047
+ jq -cn --arg lane "$lane" --arg state "$state" --arg head "$_QA_V_HEAD" --arg rhead "$rhead" \
1048
+ '{lane: $lane, state: $state, head: $head, verdict_head: (if $rhead == "" then null else $rhead end)}'
1049
+ else
1050
+ printf '%s\n' "$state"
1051
+ fi
1052
+ return "$rc"
1053
+ }
1054
+
1055
+ # ── the lanes table (#301): read what is already recorded, launch and judge nothing ───────────
1056
+ # `cckit qa lanes` is a READ of four things other verbs wrote: the port-slot ledger (`cckit
1057
+ # start`), a lane's `qa/<lane>/ports` record (`qa up`), the listeners on those ports, and the
1058
+ # latest `qa` receipt (`qa verdict`). It writes no file, emits no event, kills nothing and calls
1059
+ # neither gh nor curl. Two consequences of making no network call:
1060
+ # - the slot is read with awk, never through wt_port_slot, which ALLOCATES on first use;
1061
+ # - a verdict is compared against the LOCAL tip of the lane's branch. `qa verdict` falls back to
1062
+ # the PR's head through gh; here a lane with a verdict and no local branch reads `unknown`.
1063
+
1064
+ # _qa_ledger_field <lane> <2|3> — the slot (2) or worktree path (3) the port-slot ledger records
1065
+ # for <lane>. Empty when there is no ledger, no row, or the row's worktree is gone: the next
1066
+ # `cckit start` reclaims such a row, so its slot is nobody's.
1067
+ _qa_ledger_field() {
1068
+ local f="" row="" tab="" wt=""
1069
+ tab="$(printf '\t')"
1070
+ f="$(_wt_slots_file . 2>/dev/null)" || return 0
1071
+ [ -f "$f" ] || return 0
1072
+ row="$(awk -F"$tab" -v n="$1" '$1 == n { print; exit }' "$f" 2>/dev/null)" || row=""
1073
+ [ -n "$row" ] || return 0
1074
+ wt="$(printf '%s\n' "$row" | cut -f3)"
1075
+ [ -z "$wt" ] || [ -d "$wt" ] || return 0
1076
+ printf '%s\n' "$row" | cut -f"$2"
1077
+ }
1078
+
1079
+ # _qa_lanes_ids — every lane number something recorded, sorted: a ledger row whose worktree still
1080
+ # exists, a `qa/<lane>` state directory, or a `qa` receipt.
1081
+ _qa_lanes_ids() {
1082
+ local f="" tab="" n="" slot="" wt=""
1083
+ tab="$(printf '\t')"
1084
+ {
1085
+ ls "$(kit_state_dir)/qa" 2>/dev/null
1086
+ ls "$(sr_dir)" 2>/dev/null | sed -n 's/^\([0-9][0-9]*\)-qa-[0-9][0-9]*\.json$/\1/p'
1087
+ f="$(_wt_slots_file . 2>/dev/null)" || f=""
1088
+ if [ -n "$f" ] && [ -f "$f" ]; then
1089
+ while IFS="$tab" read -r n slot wt; do
1090
+ [ -z "$wt" ] || [ -d "$wt" ] || continue
1091
+ printf '%s\n' "$n"
1092
+ done < "$f"
1093
+ fi
1094
+ } | grep -E '^[0-9]+$' | sort -un
1095
+ return 0
1096
+ }
1097
+
1098
+ # _qa_branch_worktree <branch> — the worktree that has <branch> checked out; empty when none does.
1099
+ _qa_branch_worktree() {
1100
+ git worktree list --porcelain 2>/dev/null \
1101
+ | awk -v b="refs/heads/$1" '/^worktree / { w = substr($0, 10) } $1 == "branch" && $2 == b && !hit { print w; hit = 1 }'
1102
+ }
1103
+
1104
+ # _qa_lane_ports_now <lane> — the lane's `NAME=port` lines: the ports it was LAUNCHED with when
1105
+ # `qa up` recorded them, else the ports it would get from `qa.ports` and its ledger slot. Empty
1106
+ # when neither exists (no qa block, or no slot yet).
1107
+ _qa_lane_ports_now() {
1108
+ local lane="$1" pfile="" cfg="" slot=""
1109
+ pfile="$(qa_lane_dir "$lane")/ports"
1110
+ if [ -s "$pfile" ]; then grep -E '^[A-Za-z_][A-Za-z0-9_]*=[0-9]+$' "$pfile" 2>/dev/null; return 0; fi
1111
+ cfg="$(qa_cfg 2>/dev/null)" || return 0
1112
+ slot="$(_qa_ledger_field "$lane" 2)"
1113
+ case "$slot" in ''|*[!0-9]*) return 0 ;; esac
1114
+ qa_lane_ports "$cfg" "$slot" "$(wt_port_stride "$cfg")"
1115
+ return 0
1116
+ }
1117
+
1118
+ # _qa_lane_up <ports-lines> — `up` when every port has a listener, `partial` when some do, `down`
1119
+ # when none does, `unknown` when there is no port to check or no tool (lsof, ss) to check it with.
1120
+ # A listener is whatever holds the port: this does not tell the lane's stack from a stranger.
1121
+ _qa_lane_up() {
1122
+ local ports="$1" tool="" line="" total=0 held=0
1123
+ [ -n "$ports" ] || { printf 'unknown\n'; return 0; }
1124
+ tool="$(qa_port_tool)" || { printf 'unknown\n'; return 0; }
1125
+ while IFS= read -r line; do
1126
+ [ -n "$line" ] || continue
1127
+ total=$((total + 1))
1128
+ if qa_port_held "${line#*=}" "$tool"; then held=$((held + 1)); fi
1129
+ done <<EOF
1130
+ $ports
1131
+ EOF
1132
+ if [ "$held" -eq 0 ]; then printf 'down\n'
1133
+ elif [ "$held" -eq "$total" ]; then printf 'up\n'
1134
+ else printf 'partial\n'
1135
+ fi
1136
+ }
1137
+
1138
+ # _qa_lane_verdict <lane> <branch> — qa_verdict_state's word against the local tip of <branch>.
1139
+ # `unknown` when a verdict exists and no local branch gives a commit to compare it with.
1140
+ _qa_lane_verdict() {
1141
+ local lane="$1" branch="$2" tip="" state=""
1142
+ [ -z "$branch" ] || tip="$(git rev-parse --verify -q "refs/heads/$branch^{commit}" 2>/dev/null)" || tip=""
1143
+ state="$(qa_verdict_state "$lane" "$tip")" || true
1144
+ if [ "$state" = stale ] && [ -z "$tip" ]; then state="unknown"; fi
1145
+ printf '%s\n' "${state:-unknown}"
1146
+ }
1147
+
1148
+ # qa_lanes — one row per lane: lane, branch, ports, up, verdict, worktree. A table on stdout, or a
1149
+ # JSON array under --llm (`[]` with no lane). rc 0 whether or not there is a lane; rc 3 without jq.
1150
+ qa_lanes() {
1151
+ local lane="" branch="" wt="" ports="" up="" verdict="" rows="" tab="" objs="" shown=""
1152
+ command -v jq >/dev/null 2>&1 || { echo "cckit qa: jq is required" >&2; return 3; }
1153
+ tab="$(printf '\t')"
1154
+ while IFS= read -r lane; do
1155
+ [ -n "$lane" ] || continue
1156
+ branch="$(_qa_lane_branch "$lane")" || branch=""
1157
+ wt=""
1158
+ [ -z "$branch" ] || wt="$(_qa_branch_worktree "$branch")" || wt=""
1159
+ [ -n "$wt" ] || wt="$(_qa_ledger_field "$lane" 3)" || wt=""
1160
+ ports="$(_qa_lane_ports_now "$lane")" || ports=""
1161
+ up="$(_qa_lane_up "$ports")" || up="unknown"
1162
+ verdict="$(_qa_lane_verdict "$lane" "$branch")" || verdict="unknown"
1163
+ if [ "${CCKIT_OUTPUT:-human}" = json ]; then
1164
+ objs="$objs$(printf '%s\n' "$ports" | jq -Rcn --arg lane "$lane" --arg branch "$branch" --arg wt "$wt" \
1165
+ --arg up "$up" --arg verdict "$verdict" '
1166
+ ([inputs | select(length > 0) | split("=") | { key: .[0], value: (.[1] | tonumber) }] | from_entries) as $p
1167
+ | { lane: $lane,
1168
+ branch: (if $branch == "" then null else $branch end),
1169
+ worktree: (if $wt == "" then null else $wt end),
1170
+ ports: $p, up: $up, verdict: $verdict }')
1171
+ "
1172
+ else
1173
+ shown="$(printf '%s' "$ports" | tr '\n' ' ')"; shown="${shown% }"
1174
+ rows="$rows$lane$tab${branch:--}$tab${shown:--}$tab$up$tab$verdict$tab${wt:--}
1175
+ "
1176
+ fi
1177
+ done <<EOF
1178
+ $(_qa_lanes_ids)
1179
+ EOF
1180
+ if [ "${CCKIT_OUTPUT:-human}" = json ]; then
1181
+ printf '%s' "$objs" | jq -cs '.'
1182
+ return 0
1183
+ fi
1184
+ if [ -z "$rows" ]; then
1185
+ echo "no QA lanes — cckit start <issue> gives an issue its port slot, cckit qa up launches its stack" >&2
1186
+ return 0
1187
+ fi
1188
+ printf 'LANE\tBRANCH\tPORTS\tUP\tVERDICT\tWORKTREE\n%s' "$rows" | awk -F"$tab" '
1189
+ { n = NR; for (i = 1; i <= NF; i++) { c[NR, i] = $i; if (length($i) > w[i]) w[i] = length($i) } nf = NF }
1190
+ END {
1191
+ for (r = 1; r <= n; r++) {
1192
+ line = ""
1193
+ for (i = 1; i < nf; i++) line = line sprintf("%-" w[i] "s ", c[r, i])
1194
+ print line c[r, nf]
1195
+ }
1196
+ }'
1197
+ }
1198
+
1199
+ # qa_cli <args> — the `cckit qa` verb.
1200
+ qa_cli() {
1201
+ # Every local gets a value: bin/cckit runs under `set -u`, and a bare `local x` is UNSET.
1202
+ local sub="${1:-}" lane="" limit="" pid="" d="" rc=0 pr="" route="" only=0 penv="" force=0
1203
+ local verdict="" head="" notes="" nfile="" to="" shots=""
1204
+ case "$sub" in
1205
+ up|ready|open|stop|verdict) shift ;;
1206
+ lanes)
1207
+ shift
1208
+ [ "$#" -eq 0 ] || { echo "cckit qa lanes: unknown argument '$1' — it takes none and lists every lane" >&2; return 2; }
1209
+ qa_lanes
1210
+ return $? ;;
1211
+ ''|-h|--help|help)
1212
+ cat >&2 <<'EOF'
1213
+ cckit qa up [--lane <n>] [--timeout <s>] launch the declared stack command with this lane's
1214
+ ports, then wait until every readiness probe answers;
1215
+ refuses (exit 2) when a lane port is already in use
1216
+ cckit qa ready [--lane <n>] [--timeout <s>] wait on a lane that is already launched
1217
+ cckit qa open [--lane <n>] [--pr <n>] [--route </path>] [--port-env <NAME>] [--print]
1218
+ open the screen the change touched on a usable lane
1219
+ cckit qa stop [--lane <n>] [--force] stop the lane's stack: supervisors first, then its
1220
+ listeners. A listener the lane did not launch is
1221
+ left running (exit 1) unless --force
1222
+ cckit qa verdict <pass|fail> [--lane <n>] [--pr <n>] [--head <sha>] [--note <text>]
1223
+ [--notes-file <file>] [--shot <file>]… [--to <branch>]
1224
+ record the verdict against the reviewed commit
1225
+ cckit qa verdict [--lane <n>] [--pr <n>] [--head <sha>]
1226
+ read it back: pass · fail · stale · none
1227
+ cckit qa lanes list every lane: branch, ports, up, verdict, worktree
1228
+
1229
+ The lane is the issue number in the current branch, or --lane. Exit 0 only when the stack is
1230
+ USABLE: every qa.readiness probe answered 2xx/3xx in one round (a redirect to sign-in is up).
1231
+ Exit 1 names the launch log. Reads the `qa` block recorded by kit-add.sh qa.
1232
+
1233
+ open lands on http://localhost:<lane port><route>. The route is the `QA route: /path` line in the
1234
+ lane's PR body (--pr picks another PR), else the configured qa.route; --route overrides both. The
1235
+ port is the first qa.ports variable, or --port-env. A lane that is not usable right now is refused
1236
+ (exit 1). --print prints the URL without opening a browser.
1237
+
1238
+ stop resolves who listens on each of the lane's ports (lsof, else ss) and stops the supervisors
1239
+ before the listeners, so nothing respawns. It reports only the processes that are actually gone.
1240
+ Exit 0 when the ports are free, including on a lane that was already down; exit 1 names the pid
1241
+ still listening.
1242
+
1243
+ verdict writes a `qa` receipt (cckit receipt show <lane> qa) bound to the reviewed commit: --head,
1244
+ else the local tip of the lane's branch, else the PR's head. It upserts ONE comment on the lane's
1245
+ PR (a second verdict edits it) plus one per --shot; a screenshot is a path reference unless
1246
+ KIT_EVIDENCE_URL_BASE is set, because gh cannot upload an image. A fail needs notes and mails them
1247
+ to the builder's branch as a steer (--to names another branch); exit 1 when they reached nobody.
1248
+ With no pass|fail it prints how the verdict reads against the lane's head now and exits 0 only for
1249
+ a pass on that commit — a push since the verdict reads `stale`.
1250
+
1251
+ lanes is read-only: no launch, no stop, no write, no gh or curl call. One row per lane that has a
1252
+ port slot (cckit start), a launch record or a verdict. PORTS are the ports the lane was launched
1253
+ with, else the ones it would get. UP is up · partial · down by who listens on them (unknown with
1254
+ no port or neither lsof nor ss). VERDICT is pass · fail · stale · none against the local tip of
1255
+ the lane's branch, unknown when a verdict exists and no local branch does. Exit 0 with or without
1256
+ lanes; --llm prints a JSON array.
1257
+ EOF
1258
+ return 2 ;;
1259
+ *) echo "cckit qa: unknown subcommand '$sub' — cckit qa <up|ready|open|stop|verdict|lanes>" >&2; return 2 ;;
1260
+ esac
1261
+ while [ "$#" -gt 0 ]; do
1262
+ case "$1" in
1263
+ --lane) lane="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1264
+ --lane=*) lane="${1#*=}"; shift ;;
1265
+ --force)
1266
+ [ "$sub" = stop ] || { echo "cckit qa $sub: '--force' belongs to cckit qa stop" >&2; return 2; }
1267
+ force=1; shift ;;
1268
+ --timeout) limit="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1269
+ --timeout=*) limit="${1#*=}"; shift ;;
1270
+ pass|fail)
1271
+ [ "$sub" = verdict ] && [ -z "$verdict" ] || { echo "cckit qa $sub: unknown argument '$1'" >&2; return 2; }
1272
+ verdict="$1"; shift ;;
1273
+ --head|--note|--notes-file|--shot|--to|--head=*|--note=*|--notes-file=*|--shot=*|--to=*)
1274
+ [ "$sub" = verdict ] || { echo "cckit qa $sub: '${1%%=*}' belongs to cckit qa verdict" >&2; return 2; }
1275
+ case "$1" in
1276
+ --head) head="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1277
+ --head=*) head="${1#*=}"; shift ;;
1278
+ --note) notes="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1279
+ --note=*) notes="${1#*=}"; shift ;;
1280
+ --notes-file) nfile="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1281
+ --notes-file=*) nfile="${1#*=}"; shift ;;
1282
+ --to) to="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1283
+ --to=*) to="${1#*=}"; shift ;;
1284
+ --shot) shots="$shots${2:-}
1285
+ "; shift; [ "$#" -gt 0 ] && shift ;;
1286
+ --shot=*) shots="$shots${1#*=}
1287
+ "; shift ;;
1288
+ esac ;;
1289
+ --pr|--pr=*)
1290
+ case "$sub" in open|verdict) ;; *) echo "cckit qa $sub: '--pr' belongs to cckit qa open and verdict" >&2; return 2 ;; esac
1291
+ case "$1" in
1292
+ --pr) pr="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1293
+ --pr=*) pr="${1#*=}"; shift ;;
1294
+ esac ;;
1295
+ --route|--port-env|--print|--route=*|--port-env=*)
1296
+ [ "$sub" = open ] || { echo "cckit qa $sub: '${1%%=*}' belongs to cckit qa open" >&2; return 2; }
1297
+ case "$1" in
1298
+ --print) only=1; shift ;;
1299
+ --route) route="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1300
+ --route=*) route="${1#*=}"; shift ;;
1301
+ --port-env) penv="${2:-}"; shift; [ "$#" -gt 0 ] && shift ;;
1302
+ --port-env=*) penv="${1#*=}"; shift ;;
1303
+ esac ;;
1304
+ *) echo "cckit qa $sub: unknown argument '$1'" >&2; return 2 ;;
1305
+ esac
1306
+ done
1307
+ lane="$(qa_lane_id "$lane")" || return $?
1308
+ if [ "$sub" = stop ]; then
1309
+ [ -z "$limit" ] || { echo "cckit qa stop: --timeout belongs to up and ready — the stop grace is QA_STOP_GRACE" >&2; return 2; }
1310
+ command -v jq >/dev/null 2>&1 || { echo "cckit qa: jq is required" >&2; return 3; }
1311
+ qa_stop "$lane" "$force"
1312
+ return $?
1313
+ fi
1314
+ if [ "$sub" = verdict ]; then
1315
+ [ -z "$limit" ] || { echo "cckit qa verdict: --timeout belongs to up and ready" >&2; return 2; }
1316
+ case "$pr" in *[!0-9]*) echo "cckit qa verdict: --pr takes a pull request number, got '$pr'" >&2; return 2 ;; esac
1317
+ if [ -z "$verdict" ]; then
1318
+ if [ -n "$notes$nfile$to$shots" ]; then
1319
+ echo "cckit qa verdict: say pass or fail to record one — notes, screenshots and --to belong to a recorded verdict" >&2
1320
+ return 2
1321
+ fi
1322
+ qa_verdict_show "$lane" "$pr" "$head"
1323
+ return $?
1324
+ fi
1325
+ if [ -n "$nfile" ]; then
1326
+ [ -z "$notes" ] || { echo "cckit qa verdict: --note and --notes-file are two sources for one text — pass one" >&2; return 2; }
1327
+ [ -f "$nfile" ] || { echo "cckit qa verdict: --notes-file '$nfile' is not a file" >&2; return 2; }
1328
+ notes="$(cat "$nfile")"
1329
+ fi
1330
+ qa_verdict "$lane" "$verdict" "$pr" "$head" "$notes" "$to" "$shots"
1331
+ return $?
1332
+ fi
1333
+ if [ "$sub" = open ]; then
1334
+ [ -z "$limit" ] || { echo "cckit qa open: --timeout belongs to up and ready — open does not wait" >&2; return 2; }
1335
+ case "$pr" in *[!0-9]*) echo "cckit qa open: --pr takes a pull request number, got '$pr'" >&2; return 2 ;; esac
1336
+ qa_open "$lane" "$pr" "$route" "$only" "$penv"
1337
+ return $?
1338
+ fi
1339
+ [ -n "$limit" ] || limit="${QA_READY_TIMEOUT:-120}"
1340
+
1341
+ if [ "$sub" = up ]; then
1342
+ pid="$(qa_launch "$lane")" || return $?
1343
+ fi
1344
+ qa_ready "$lane" "$limit" || rc=$?
1345
+ case "$rc" in 2|3) return "$rc" ;; esac
1346
+
1347
+ d="$(qa_lane_dir "$lane")"
1348
+ [ -n "$pid" ] || pid="$(cat "$d/pid" 2>/dev/null)" || pid=""
1349
+ if [ "${CCKIT_OUTPUT:-human}" = json ]; then
1350
+ jq -cn --arg lane "$lane" --arg pid "$pid" --arg log "$d/launch.log" --argjson usable "$([ "$rc" -eq 0 ] && echo true || echo false)" \
1351
+ --argjson ports "$(_qa_ports_json "$d/ports")" \
1352
+ '{lane: $lane, usable: $usable, pid: ($pid | tonumber? // null), ports: $ports, log: $log}'
1353
+ elif [ "$rc" -eq 0 ]; then
1354
+ printf 'lane %s usable · %s\n' "$lane" "$(tr '\n' ' ' < "$d/ports" | sed 's/ $//')"
1355
+ fi
1356
+ return "$rc"
1357
+ }