@bonesofspring/ai-rules 0.2.63 → 0.2.65

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 (421) hide show
  1. package/CHANGELOG.md +96 -1
  2. package/README.md +11 -4
  3. package/bin/cli.js +7 -5
  4. package/bin/lib/ai-local-drift.js +7 -6
  5. package/bin/lib/engine-scripts.js +17 -4
  6. package/bin/lib/engine-upgrade.js +10 -2
  7. package/bin/lib/manifest-schema.js +12 -0
  8. package/bin/lib/seed-ai-local-team.js +20 -0
  9. package/bin/mavis-install.js +4 -3
  10. package/bin/opencode-install.js +22 -6
  11. package/bin/zcode-install.js +4 -3
  12. package/docs/MCP-CATALOG.md +1 -1
  13. package/docs/OPENCODE-COMPATIBILITY.md +3 -2
  14. package/docs/OPENCODE.md +22 -2
  15. package/docs/PRESET-CONTRIBUTION.md +43 -2
  16. package/docs/TEAM-DB.md +213 -5
  17. package/docs/openapi/team-db.v1.json +5928 -3045
  18. package/mcp/defectdojo-mcp.mjs +44 -12
  19. package/mcp/lib/defectdojo.mjs +57 -2
  20. package/package.json +27 -15
  21. package/presets/_shared/ai-local/scripts/aggregate-agent-task-metrics.mjs +279 -0
  22. package/presets/_shared/ai-local/scripts/process-lease.mjs +576 -0
  23. package/presets/_shared/ai-local/scripts/processes.mjs +1618 -0
  24. package/presets/_shared/ai-local/scripts/run.mjs +5 -5
  25. package/presets/_shared/ai-local/scripts/team-db-api.mjs +784 -7
  26. package/presets/_shared/ai-local/scripts/team-db-dashboard.mjs +235 -10
  27. package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +813 -7
  28. package/presets/_shared/ai-local/scripts/team-db-project.mjs +397 -4
  29. package/presets/_shared/ai-local/scripts/team-db-scheduler.mjs +86 -10
  30. package/presets/_shared/ai-local/scripts/team-db-schema.mjs +221 -0
  31. package/presets/_shared/ai-local/scripts/team-db-task-runner.mjs +567 -0
  32. package/presets/_shared/ai-local/scripts/team-db-tracker.mjs +2069 -0
  33. package/presets/_shared/ai-local/scripts/team-db-ui.mjs +750 -0
  34. package/presets/_shared/ai-local/scripts/team-db.mjs +151 -7
  35. package/presets/_shared/ai-local/scripts/upstream-research.mjs +3 -0
  36. package/presets/_shared/core/agent-team/agent-task-metrics.md +7 -2
  37. package/presets/_shared/core/agent-team/agent-team-orchestrator.md +2 -1
  38. package/presets/_shared/hooks/chain-team-phases.sh.tmpl +314 -1
  39. package/presets/_shared/hooks/guard-shell-command.mjs.tmpl +40 -2
  40. package/presets/_shared/hooks/run-hook.mjs +2 -1
  41. package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +314 -1
  42. package/presets/claude/android-kotlin/hooks/guard-shell-command.mjs +41 -2
  43. package/presets/claude/android-kotlin/hooks/run-hook.mjs +2 -1
  44. package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  45. package/presets/claude/android-kotlin/settings.json +6 -1
  46. package/presets/claude/go/hooks/chain-team-phases.sh +314 -1
  47. package/presets/claude/go/hooks/guard-shell-command.mjs +41 -2
  48. package/presets/claude/go/hooks/run-hook.mjs +2 -1
  49. package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  50. package/presets/claude/go/settings.json +6 -1
  51. package/presets/claude/ios-swift/hooks/chain-team-phases.sh +314 -1
  52. package/presets/claude/ios-swift/hooks/guard-shell-command.mjs +41 -2
  53. package/presets/claude/ios-swift/hooks/run-hook.mjs +2 -1
  54. package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  55. package/presets/claude/ios-swift/settings.json +6 -1
  56. package/presets/claude/java/hooks/chain-team-phases.sh +314 -1
  57. package/presets/claude/java/hooks/guard-shell-command.mjs +41 -2
  58. package/presets/claude/java/hooks/run-hook.mjs +2 -1
  59. package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  60. package/presets/claude/java/settings.json +6 -1
  61. package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +314 -1
  62. package/presets/claude/mcp-ts/hooks/guard-shell-command.mjs +41 -2
  63. package/presets/claude/mcp-ts/hooks/run-hook.mjs +2 -1
  64. package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  65. package/presets/claude/mcp-ts/settings.json +6 -1
  66. package/presets/claude/mol/hooks/chain-team-phases.sh +314 -1
  67. package/presets/claude/mol/hooks/guard-shell-command.mjs +41 -2
  68. package/presets/claude/mol/hooks/run-hook.mjs +2 -1
  69. package/presets/claude/mol/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  70. package/presets/claude/mol/settings.json +6 -1
  71. package/presets/claude/next/MCP.md +2 -0
  72. package/presets/claude/next/REPO_AGENTS.md +2 -0
  73. package/presets/claude/next/agents/build-verifier.md +1 -1
  74. package/presets/claude/next/agents/ci-investigator.md +3 -0
  75. package/presets/claude/next/agents/code-reviewer.md +1 -0
  76. package/presets/claude/next/agents/debugger.md +3 -0
  77. package/presets/claude/next/agents/feature-developer.md +3 -0
  78. package/presets/claude/next/agents/mockup-reviewer.md +4 -1
  79. package/presets/claude/next/agents/performance-auditor.md +2 -0
  80. package/presets/claude/next/agents/playwright-test-generator.md +3 -0
  81. package/presets/claude/next/agents/playwright-test-healer.md +3 -0
  82. package/presets/claude/next/agents/playwright-test-planner.md +3 -0
  83. package/presets/claude/next/agents/qa-tester.md +3 -0
  84. package/presets/claude/next/agents/task-analyst.md +2 -0
  85. package/presets/claude/next/commands/README.md +6 -0
  86. package/presets/claude/next/commands/task.md +7 -1
  87. package/presets/claude/next/commands/technical-retro.md +7 -1
  88. package/presets/claude/next/commands/ui-start.md +78 -0
  89. package/presets/claude/next/commands/ui-stop.md +71 -0
  90. package/presets/claude/next/commands/upstream-research.md +1 -1
  91. package/presets/claude/next/hooks/chain-team-phases.sh +314 -1
  92. package/presets/claude/next/hooks/guard-shell-command.mjs +41 -2
  93. package/presets/claude/next/hooks/run-hook.mjs +2 -1
  94. package/presets/claude/next/rules/README.md +2 -1
  95. package/presets/claude/next/rules/stack/next-app-core.md +1 -0
  96. package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  97. package/presets/claude/next/rules/tooling-and-review/dev-process-lifecycle.md +43 -0
  98. package/presets/claude/next/settings.json +6 -1
  99. package/presets/claude/next/skills/figma-fidelity/SKILL.md +4 -1
  100. package/presets/claude/next/skills/mockup-review/SKILL.md +4 -0
  101. package/presets/claude/next/team/README.md +5 -1
  102. package/presets/claude/next/team/upstream/jobs.json +34 -0
  103. package/presets/claude/nuxt/hooks/chain-team-phases.sh +314 -1
  104. package/presets/claude/nuxt/hooks/guard-shell-command.mjs +41 -2
  105. package/presets/claude/nuxt/hooks/run-hook.mjs +2 -1
  106. package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  107. package/presets/claude/nuxt/settings.json +6 -1
  108. package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +314 -1
  109. package/presets/claude/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
  110. package/presets/claude/php-hexagonal/hooks/run-hook.mjs +2 -1
  111. package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  112. package/presets/claude/php-hexagonal/settings.json +6 -1
  113. package/presets/claude/php-laravel/hooks/chain-team-phases.sh +315 -2
  114. package/presets/claude/php-laravel/hooks/guard-shell-command.mjs +41 -2
  115. package/presets/claude/php-laravel/hooks/run-hook.mjs +2 -1
  116. package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  117. package/presets/claude/php-laravel/settings.json +6 -1
  118. package/presets/claude/svelte/hooks/chain-team-phases.sh +314 -1
  119. package/presets/claude/svelte/hooks/guard-shell-command.mjs +41 -2
  120. package/presets/claude/svelte/hooks/run-hook.mjs +2 -1
  121. package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +2 -1
  122. package/presets/claude/svelte/settings.json +6 -1
  123. package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +314 -1
  124. package/presets/cursor/android-kotlin/hooks/guard-shell-command.mjs +41 -2
  125. package/presets/cursor/android-kotlin/hooks/run-hook.mjs +2 -1
  126. package/presets/cursor/go/hooks/chain-team-phases.sh +314 -1
  127. package/presets/cursor/go/hooks/guard-shell-command.mjs +41 -2
  128. package/presets/cursor/go/hooks/run-hook.mjs +2 -1
  129. package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +314 -1
  130. package/presets/cursor/ios-swift/hooks/guard-shell-command.mjs +41 -2
  131. package/presets/cursor/ios-swift/hooks/run-hook.mjs +2 -1
  132. package/presets/cursor/java/hooks/chain-team-phases.sh +314 -1
  133. package/presets/cursor/java/hooks/guard-shell-command.mjs +41 -2
  134. package/presets/cursor/java/hooks/run-hook.mjs +2 -1
  135. package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +314 -1
  136. package/presets/cursor/mcp-ts/hooks/guard-shell-command.mjs +41 -2
  137. package/presets/cursor/mcp-ts/hooks/run-hook.mjs +2 -1
  138. package/presets/cursor/mol/hooks/chain-team-phases.sh +314 -1
  139. package/presets/cursor/mol/hooks/guard-shell-command.mjs +41 -2
  140. package/presets/cursor/mol/hooks/run-hook.mjs +2 -1
  141. package/presets/cursor/next/MCP.md +2 -0
  142. package/presets/cursor/next/REPO_AGENTS.md +2 -0
  143. package/presets/cursor/next/agents/build-verifier.md +1 -1
  144. package/presets/cursor/next/agents/ci-investigator.md +2 -0
  145. package/presets/cursor/next/agents/code-reviewer.md +1 -0
  146. package/presets/cursor/next/agents/debugger.md +2 -0
  147. package/presets/cursor/next/agents/feature-developer.md +2 -0
  148. package/presets/cursor/next/agents/mockup-reviewer.md +4 -1
  149. package/presets/cursor/next/agents/performance-auditor.md +2 -0
  150. package/presets/cursor/next/agents/playwright-test-generator.md +2 -0
  151. package/presets/cursor/next/agents/playwright-test-healer.md +2 -0
  152. package/presets/cursor/next/agents/playwright-test-planner.md +2 -0
  153. package/presets/cursor/next/agents/qa-tester.md +2 -0
  154. package/presets/cursor/next/agents/task-analyst.md +2 -0
  155. package/presets/cursor/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
  156. package/presets/cursor/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
  157. package/presets/cursor/next/commands/README.md +6 -0
  158. package/presets/cursor/next/commands/task.md +7 -1
  159. package/presets/cursor/next/commands/technical-retro.md +7 -1
  160. package/presets/cursor/next/commands/ui-start.md +78 -0
  161. package/presets/cursor/next/commands/ui-stop.md +71 -0
  162. package/presets/cursor/next/commands/upstream-research.md +1 -1
  163. package/presets/cursor/next/hooks/chain-team-phases.sh +314 -1
  164. package/presets/cursor/next/hooks/guard-shell-command.mjs +41 -2
  165. package/presets/cursor/next/hooks/run-hook.mjs +2 -1
  166. package/presets/cursor/next/rules/README.md +2 -1
  167. package/presets/cursor/next/rules/dev-process-lifecycle.mdc +43 -0
  168. package/presets/cursor/next/rules/next-app-core.mdc +1 -0
  169. package/presets/cursor/next/skills/figma-fidelity/SKILL.md +4 -1
  170. package/presets/cursor/next/skills/mockup-review/SKILL.md +4 -0
  171. package/presets/cursor/next/team/README.md +5 -1
  172. package/presets/cursor/next/team/upstream/jobs.json +34 -0
  173. package/presets/cursor/nuxt/hooks/chain-team-phases.sh +314 -1
  174. package/presets/cursor/nuxt/hooks/guard-shell-command.mjs +41 -2
  175. package/presets/cursor/nuxt/hooks/run-hook.mjs +2 -1
  176. package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +314 -1
  177. package/presets/cursor/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
  178. package/presets/cursor/php-hexagonal/hooks/run-hook.mjs +2 -1
  179. package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +315 -2
  180. package/presets/cursor/php-laravel/hooks/guard-shell-command.mjs +41 -2
  181. package/presets/cursor/php-laravel/hooks/run-hook.mjs +2 -1
  182. package/presets/cursor/svelte/hooks/chain-team-phases.sh +314 -1
  183. package/presets/cursor/svelte/hooks/guard-shell-command.mjs +41 -2
  184. package/presets/cursor/svelte/hooks/run-hook.mjs +2 -1
  185. package/presets/mavis/android-kotlin/adapter-map.json +1 -1
  186. package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +314 -1
  187. package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +2 -1
  188. package/presets/mavis/go/adapter-map.json +1 -1
  189. package/presets/mavis/go/hooks/chain-team-phases.sh +314 -1
  190. package/presets/mavis/go/rules/agent-team-orchestrator.md +2 -1
  191. package/presets/mavis/ios-swift/adapter-map.json +1 -1
  192. package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +314 -1
  193. package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +2 -1
  194. package/presets/mavis/java/adapter-map.json +1 -1
  195. package/presets/mavis/java/hooks/chain-team-phases.sh +314 -1
  196. package/presets/mavis/java/rules/agent-team-orchestrator.md +2 -1
  197. package/presets/mavis/mcp-ts/adapter-map.json +1 -1
  198. package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +314 -1
  199. package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +2 -1
  200. package/presets/mavis/mol/adapter-map.json +1 -1
  201. package/presets/mavis/mol/hooks/chain-team-phases.sh +314 -1
  202. package/presets/mavis/mol/rules/agent-team-orchestrator.md +2 -1
  203. package/presets/mavis/next/MCP.md +2 -0
  204. package/presets/mavis/next/REPO_AGENTS.md +2 -0
  205. package/presets/mavis/next/adapter-map.json +33 -1
  206. package/presets/mavis/next/agents/build-verifier.md +1 -1
  207. package/presets/mavis/next/agents/ci-investigator.md +3 -0
  208. package/presets/mavis/next/agents/code-reviewer.md +1 -0
  209. package/presets/mavis/next/agents/debugger.md +3 -0
  210. package/presets/mavis/next/agents/feature-developer.md +3 -0
  211. package/presets/mavis/next/agents/mockup-reviewer.md +4 -1
  212. package/presets/mavis/next/agents/performance-auditor.md +2 -0
  213. package/presets/mavis/next/agents/playwright-test-generator.md +3 -0
  214. package/presets/mavis/next/agents/playwright-test-healer.md +3 -0
  215. package/presets/mavis/next/agents/playwright-test-planner.md +3 -0
  216. package/presets/mavis/next/agents/qa-tester.md +3 -0
  217. package/presets/mavis/next/agents/task-analyst.md +2 -0
  218. package/presets/mavis/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
  219. package/presets/mavis/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
  220. package/presets/mavis/next/commands/README.md +6 -0
  221. package/presets/mavis/next/commands/task.md +7 -1
  222. package/presets/mavis/next/commands/technical-retro.md +7 -1
  223. package/presets/mavis/next/commands/ui-start.md +73 -0
  224. package/presets/mavis/next/commands/ui-stop.md +66 -0
  225. package/presets/mavis/next/commands/upstream-research.md +1 -1
  226. package/presets/mavis/next/hooks/chain-team-phases.sh +314 -1
  227. package/presets/mavis/next/rules/agent-team-orchestrator.md +2 -1
  228. package/presets/mavis/next/rules/dev-process-lifecycle.md +38 -0
  229. package/presets/mavis/next/rules/next-app-core.md +1 -0
  230. package/presets/mavis/next/skills/figma-fidelity/SKILL.md +4 -1
  231. package/presets/mavis/next/skills/mockup-review/SKILL.md +4 -0
  232. package/presets/mavis/next/team/README.md +5 -1
  233. package/presets/mavis/next/team/upstream/jobs.json +34 -0
  234. package/presets/mavis/nuxt/adapter-map.json +1 -1
  235. package/presets/mavis/nuxt/hooks/chain-team-phases.sh +314 -1
  236. package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +2 -1
  237. package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
  238. package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +314 -1
  239. package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +2 -1
  240. package/presets/mavis/php-laravel/adapter-map.json +1 -1
  241. package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +315 -2
  242. package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +2 -1
  243. package/presets/mavis/svelte/adapter-map.json +1 -1
  244. package/presets/mavis/svelte/hooks/chain-team-phases.sh +314 -1
  245. package/presets/mavis/svelte/rules/agent-team-orchestrator.md +2 -1
  246. package/presets/opencode/android-kotlin/OPENCODE.md +4 -1
  247. package/presets/opencode/android-kotlin/README.md +1 -0
  248. package/presets/opencode/android-kotlin/adapter-map.json +1 -1
  249. package/presets/opencode/android-kotlin/hooks/README.md +4 -2
  250. package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +314 -1
  251. package/presets/opencode/android-kotlin/opencode.json +5 -1
  252. package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +2 -1
  253. package/presets/opencode/go/OPENCODE.md +4 -1
  254. package/presets/opencode/go/README.md +1 -0
  255. package/presets/opencode/go/adapter-map.json +1 -1
  256. package/presets/opencode/go/hooks/README.md +4 -2
  257. package/presets/opencode/go/hooks/chain-team-phases.sh +314 -1
  258. package/presets/opencode/go/opencode.json +5 -1
  259. package/presets/opencode/go/rules/agent-team-orchestrator.md +2 -1
  260. package/presets/opencode/ios-swift/OPENCODE.md +4 -1
  261. package/presets/opencode/ios-swift/README.md +1 -0
  262. package/presets/opencode/ios-swift/adapter-map.json +1 -1
  263. package/presets/opencode/ios-swift/hooks/README.md +4 -2
  264. package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +314 -1
  265. package/presets/opencode/ios-swift/opencode.json +5 -1
  266. package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +2 -1
  267. package/presets/opencode/java/OPENCODE.md +4 -1
  268. package/presets/opencode/java/README.md +1 -0
  269. package/presets/opencode/java/adapter-map.json +1 -1
  270. package/presets/opencode/java/hooks/README.md +4 -2
  271. package/presets/opencode/java/hooks/chain-team-phases.sh +314 -1
  272. package/presets/opencode/java/opencode.json +5 -1
  273. package/presets/opencode/java/rules/agent-team-orchestrator.md +2 -1
  274. package/presets/opencode/mcp-ts/OPENCODE.md +4 -1
  275. package/presets/opencode/mcp-ts/README.md +1 -0
  276. package/presets/opencode/mcp-ts/adapter-map.json +1 -1
  277. package/presets/opencode/mcp-ts/hooks/README.md +4 -2
  278. package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +314 -1
  279. package/presets/opencode/mcp-ts/opencode.json +5 -1
  280. package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +2 -1
  281. package/presets/opencode/mol/OPENCODE.md +4 -1
  282. package/presets/opencode/mol/README.md +1 -0
  283. package/presets/opencode/mol/adapter-map.json +1 -1
  284. package/presets/opencode/mol/hooks/README.md +4 -2
  285. package/presets/opencode/mol/hooks/chain-team-phases.sh +314 -1
  286. package/presets/opencode/mol/opencode.json +4 -0
  287. package/presets/opencode/mol/rules/agent-team-orchestrator.md +2 -1
  288. package/presets/opencode/next/MCP.md +2 -0
  289. package/presets/opencode/next/OPENCODE.md +4 -1
  290. package/presets/opencode/next/README.md +1 -0
  291. package/presets/opencode/next/REPO_AGENTS.md +2 -0
  292. package/presets/opencode/next/adapter-map.json +33 -1
  293. package/presets/opencode/next/agents/build-verifier.md +1 -1
  294. package/presets/opencode/next/agents/ci-investigator.md +3 -0
  295. package/presets/opencode/next/agents/code-reviewer.md +1 -0
  296. package/presets/opencode/next/agents/debugger.md +3 -0
  297. package/presets/opencode/next/agents/feature-developer.md +3 -0
  298. package/presets/opencode/next/agents/mockup-reviewer.md +4 -1
  299. package/presets/opencode/next/agents/performance-auditor.md +2 -0
  300. package/presets/opencode/next/agents/playwright-test-generator.md +3 -0
  301. package/presets/opencode/next/agents/playwright-test-healer.md +3 -0
  302. package/presets/opencode/next/agents/playwright-test-planner.md +3 -0
  303. package/presets/opencode/next/agents/qa-tester.md +3 -0
  304. package/presets/opencode/next/agents/task-analyst.md +2 -0
  305. package/presets/opencode/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
  306. package/presets/opencode/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
  307. package/presets/opencode/next/commands/README.md +6 -0
  308. package/presets/opencode/next/commands/task.md +7 -1
  309. package/presets/opencode/next/commands/technical-retro.md +7 -1
  310. package/presets/opencode/next/commands/ui-start.md +81 -0
  311. package/presets/opencode/next/commands/ui-stop.md +74 -0
  312. package/presets/opencode/next/commands/upstream-research.md +1 -1
  313. package/presets/opencode/next/hooks/README.md +4 -2
  314. package/presets/opencode/next/hooks/chain-team-phases.sh +314 -1
  315. package/presets/opencode/next/opencode.json +4 -0
  316. package/presets/opencode/next/rules/agent-team-orchestrator.md +2 -1
  317. package/presets/opencode/next/rules/dev-process-lifecycle.md +38 -0
  318. package/presets/opencode/next/rules/next-app-core.md +1 -0
  319. package/presets/opencode/next/skills/figma-fidelity/SKILL.md +4 -1
  320. package/presets/opencode/next/skills/mockup-review/SKILL.md +4 -0
  321. package/presets/opencode/next/team/README.md +5 -1
  322. package/presets/opencode/next/team/upstream/jobs.json +34 -0
  323. package/presets/opencode/nuxt/OPENCODE.md +4 -1
  324. package/presets/opencode/nuxt/README.md +1 -0
  325. package/presets/opencode/nuxt/adapter-map.json +1 -1
  326. package/presets/opencode/nuxt/hooks/README.md +4 -2
  327. package/presets/opencode/nuxt/hooks/chain-team-phases.sh +314 -1
  328. package/presets/opencode/nuxt/opencode.json +4 -0
  329. package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +2 -1
  330. package/presets/opencode/php-hexagonal/OPENCODE.md +4 -1
  331. package/presets/opencode/php-hexagonal/README.md +1 -0
  332. package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
  333. package/presets/opencode/php-hexagonal/hooks/README.md +4 -2
  334. package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +314 -1
  335. package/presets/opencode/php-hexagonal/opencode.json +5 -1
  336. package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +2 -1
  337. package/presets/opencode/php-laravel/OPENCODE.md +4 -1
  338. package/presets/opencode/php-laravel/README.md +1 -0
  339. package/presets/opencode/php-laravel/adapter-map.json +1 -1
  340. package/presets/opencode/php-laravel/hooks/README.md +4 -2
  341. package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +315 -2
  342. package/presets/opencode/php-laravel/opencode.json +5 -1
  343. package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +2 -1
  344. package/presets/opencode/platform-contract.json +6 -0
  345. package/presets/opencode/plugins/task-status/README.md +92 -0
  346. package/presets/opencode/plugins/task-status/details-data.ts +130 -0
  347. package/presets/opencode/plugins/task-status/details.tsx +84 -0
  348. package/presets/opencode/plugins/task-status/eta.ts +230 -0
  349. package/presets/opencode/plugins/task-status/index.ts +6 -0
  350. package/presets/opencode/plugins/task-status/package.json +10 -0
  351. package/presets/opencode/plugins/task-status/task-view.ts +606 -0
  352. package/presets/opencode/plugins/task-status/tui.tsx +139 -0
  353. package/presets/opencode/svelte/OPENCODE.md +4 -1
  354. package/presets/opencode/svelte/README.md +1 -0
  355. package/presets/opencode/svelte/adapter-map.json +1 -1
  356. package/presets/opencode/svelte/hooks/README.md +4 -2
  357. package/presets/opencode/svelte/hooks/chain-team-phases.sh +314 -1
  358. package/presets/opencode/svelte/opencode.json +4 -0
  359. package/presets/opencode/svelte/rules/agent-team-orchestrator.md +2 -1
  360. package/presets/zcode/android-kotlin/adapter-map.json +1 -1
  361. package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +314 -1
  362. package/presets/zcode/android-kotlin/rules/agent-team-orchestrator.md +2 -1
  363. package/presets/zcode/go/adapter-map.json +1 -1
  364. package/presets/zcode/go/hooks/chain-team-phases.sh +314 -1
  365. package/presets/zcode/go/rules/agent-team-orchestrator.md +2 -1
  366. package/presets/zcode/ios-swift/adapter-map.json +1 -1
  367. package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +314 -1
  368. package/presets/zcode/ios-swift/rules/agent-team-orchestrator.md +2 -1
  369. package/presets/zcode/java/adapter-map.json +1 -1
  370. package/presets/zcode/java/hooks/chain-team-phases.sh +314 -1
  371. package/presets/zcode/java/rules/agent-team-orchestrator.md +2 -1
  372. package/presets/zcode/mcp-ts/adapter-map.json +1 -1
  373. package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +314 -1
  374. package/presets/zcode/mcp-ts/rules/agent-team-orchestrator.md +2 -1
  375. package/presets/zcode/mol/adapter-map.json +1 -1
  376. package/presets/zcode/mol/hooks/chain-team-phases.sh +314 -1
  377. package/presets/zcode/mol/rules/agent-team-orchestrator.md +2 -1
  378. package/presets/zcode/next/MCP.md +2 -0
  379. package/presets/zcode/next/REPO_AGENTS.md +2 -0
  380. package/presets/zcode/next/adapter-map.json +33 -1
  381. package/presets/zcode/next/agents/build-verifier.md +1 -1
  382. package/presets/zcode/next/agents/ci-investigator.md +3 -0
  383. package/presets/zcode/next/agents/code-reviewer.md +1 -0
  384. package/presets/zcode/next/agents/debugger.md +3 -0
  385. package/presets/zcode/next/agents/feature-developer.md +3 -0
  386. package/presets/zcode/next/agents/mockup-reviewer.md +4 -1
  387. package/presets/zcode/next/agents/performance-auditor.md +2 -0
  388. package/presets/zcode/next/agents/playwright-test-generator.md +3 -0
  389. package/presets/zcode/next/agents/playwright-test-healer.md +3 -0
  390. package/presets/zcode/next/agents/playwright-test-planner.md +3 -0
  391. package/presets/zcode/next/agents/qa-tester.md +3 -0
  392. package/presets/zcode/next/agents/task-analyst.md +2 -0
  393. package/presets/zcode/next/ai-local/rules-library/storybook-dev-compare-figma.md +2 -1
  394. package/presets/zcode/next/ai-local/rules-library/ui-change-review-gate.md +1 -0
  395. package/presets/zcode/next/commands/README.md +6 -0
  396. package/presets/zcode/next/commands/task.md +7 -1
  397. package/presets/zcode/next/commands/technical-retro.md +7 -1
  398. package/presets/zcode/next/commands/ui-start.md +81 -0
  399. package/presets/zcode/next/commands/ui-stop.md +74 -0
  400. package/presets/zcode/next/commands/upstream-research.md +1 -1
  401. package/presets/zcode/next/hooks/chain-team-phases.sh +314 -1
  402. package/presets/zcode/next/rules/agent-team-orchestrator.md +2 -1
  403. package/presets/zcode/next/rules/dev-process-lifecycle.md +38 -0
  404. package/presets/zcode/next/rules/next-app-core.md +1 -0
  405. package/presets/zcode/next/skills/figma-fidelity/SKILL.md +4 -1
  406. package/presets/zcode/next/skills/mockup-review/SKILL.md +4 -0
  407. package/presets/zcode/next/team/README.md +5 -1
  408. package/presets/zcode/next/team/upstream/jobs.json +34 -0
  409. package/presets/zcode/nuxt/adapter-map.json +1 -1
  410. package/presets/zcode/nuxt/hooks/chain-team-phases.sh +314 -1
  411. package/presets/zcode/nuxt/rules/agent-team-orchestrator.md +2 -1
  412. package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
  413. package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +314 -1
  414. package/presets/zcode/php-hexagonal/rules/agent-team-orchestrator.md +2 -1
  415. package/presets/zcode/php-laravel/adapter-map.json +1 -1
  416. package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +315 -2
  417. package/presets/zcode/php-laravel/rules/agent-team-orchestrator.md +2 -1
  418. package/presets/zcode/svelte/adapter-map.json +1 -1
  419. package/presets/zcode/svelte/hooks/chain-team-phases.sh +314 -1
  420. package/presets/zcode/svelte/rules/agent-team-orchestrator.md +2 -1
  421. package/scripts/aggregate-agent-task-metrics.mjs +48 -4
@@ -0,0 +1,750 @@
1
+ /**
2
+ * `ui` lifecycle for the shipped team-db dashboard.
3
+ *
4
+ * `start` is probe-first: a healthy server for this root is a no-op with the
5
+ * same pid and an untouched marker, so only a free port leads to a detached
6
+ * `serve` plus `<aiLocalRoot>/.ui/api.json`. `start` writes the marker and at
7
+ * most one bounded `sync` (the missing-projection repair) and nothing else;
8
+ * `restart` is the explicit owned-stop-then-start verb. `stop` re-proves
9
+ * ownership from the live command line, signals the leader pid only and keeps
10
+ * the marker when the process survives. `status` reports the marker, the health
11
+ * block and a stale-engine warning.
12
+ *
13
+ * Ownership is a live pid AND `ps -p <pid> -o command=` matching
14
+ * `/team-db\.mjs\s+serve\b/`: the marker alone never authorizes a signal. Only
15
+ * the leader pid is signalled, never the process group — `serve` spawns bounded
16
+ * delete children and a group kill could truncate a delete between its folder
17
+ * and row legs. The timer stays separate: `ui start` reports
18
+ * `/health.scheduler` and never starts `team-db.mjs schedule`.
19
+ *
20
+ * POSIX only: the ownership probe reads `ps`. `win32` has no equivalent
21
+ * command-line reader, so no ownership can be proven there and no signal is
22
+ * ever sent — use the foreground `serve` on Windows.
23
+ *
24
+ * Exit codes: 0 ok or no-op, 1 refusal, 2 start failure, 3 usage.
25
+ */
26
+
27
+ import { spawn, spawnSync } from 'node:child_process';
28
+ import fs from 'node:fs';
29
+ import path from 'node:path';
30
+ import { fileURLToPath } from 'node:url';
31
+
32
+ import { DEFAULT_HOST, DEFAULT_PORT, LOOPBACK_HOSTS } from './team-db-api.mjs';
33
+ import {
34
+ DEFAULT_TEAM_ROOT,
35
+ INDEX_UNAVAILABLE_LINE,
36
+ isKillSwitchSet,
37
+ isSqliteAvailable,
38
+ teamRootId,
39
+ } from './team-db-schema.mjs';
40
+
41
+ const MARKER_DIR_NAME = '.ui';
42
+ const MARKER_FILE_NAME = 'api.json';
43
+ const MARKER_SCHEMA_VERSION = 1;
44
+ const MARKER_FILE_MODE = 0o600;
45
+ const MARKER_COMMAND = 'node .ai.local/scripts/team-db.mjs serve';
46
+ const OWNED_COMMAND = /team-db\.mjs\s+serve\b/;
47
+ const PROBE_TIMEOUT_MS = 2000;
48
+ const START_TIMEOUT_MS = 30000;
49
+ const SYNC_TIMEOUT_MS = 30000;
50
+ const HEALTH_POLL_INTERVAL_MS = 250;
51
+ const TERMINATE_WAIT_MS = 5000;
52
+ const TERMINATE_POLL_INTERVAL_MS = 100;
53
+ const KILL_WAIT_MS = 2000;
54
+ const OWNER_STARTED = 'started by this repository';
55
+ const OWNER_PRE_EXISTING = 'pre-existing';
56
+
57
+ const VERB_FLAGS = {
58
+ start: new Set(['root', 'json', 'quiet', 'host', 'port', 'open']),
59
+ stop: new Set(['root', 'json', 'quiet']),
60
+ status: new Set(['root', 'json', 'quiet']),
61
+ restart: new Set(['root', 'json', 'quiet', 'host', 'port', 'open']),
62
+ };
63
+
64
+ class UiError extends Error {
65
+ constructor(code, message, exitCode) {
66
+ super(message);
67
+ this.name = 'UiError';
68
+ this.code = code;
69
+ this.exitCode = exitCode;
70
+ }
71
+ }
72
+
73
+ const uiFail = (code, message, exitCode) => {
74
+ throw new UiError(code, message, exitCode);
75
+ };
76
+
77
+ const sleep = (ms) =>
78
+ new Promise((resolve) => {
79
+ setTimeout(resolve, ms);
80
+ });
81
+
82
+ const cliPath = () => fileURLToPath(new URL('./team-db.mjs', import.meta.url));
83
+
84
+ const urlHost = (host) => (host.includes(':') ? `[${host}]` : host);
85
+
86
+ const describeTarget = (target) => `http://${urlHost(target.host)}:${target.port}`;
87
+
88
+ const resolveRoots = (flags) => {
89
+ // Same precedence as the projection layers (`team-db-schema.mjs`): an explicit
90
+ // `--root`, else `AI_RULES_TEAM_DB_ROOT`, else `<cwd>/.ai.local/team`. The ui
91
+ // verbs must find a server started through the environment override.
92
+ const teamRoot = path.resolve(flags.root || process.env.AI_RULES_TEAM_DB_ROOT || DEFAULT_TEAM_ROOT);
93
+ const markerDir = path.join(path.dirname(teamRoot), MARKER_DIR_NAME);
94
+ return {
95
+ teamRoot,
96
+ markerDir,
97
+ markerPath: path.join(markerDir, MARKER_FILE_NAME),
98
+ rootId: teamRootId(teamRoot),
99
+ };
100
+ };
101
+
102
+ /* --------------------------------- process -------------------------------- */
103
+
104
+ const isAlive = (pid) => {
105
+ try {
106
+ process.kill(pid, 0);
107
+ } catch (error) {
108
+ return error.code === 'EPERM';
109
+ }
110
+ // A killed child stays a zombie until its parent reaps it; it holds a pid
111
+ // slot but no longer runs, so `stop` must not wait on it forever.
112
+ const state = spawnSync('ps', ['-p', String(pid), '-o', 'stat='], { encoding: 'utf8' }).stdout.trim();
113
+ return state !== '' && !state.startsWith('Z');
114
+ };
115
+
116
+ const signalPid = (pid, signal) => {
117
+ try {
118
+ process.kill(pid, signal);
119
+ return true;
120
+ } catch {
121
+ return false;
122
+ }
123
+ };
124
+
125
+ const liveCommand = (pid) => {
126
+ const result = spawnSync('ps', ['-p', String(pid), '-o', 'command='], { encoding: 'utf8' });
127
+ if (result.status !== 0) return null;
128
+ return result.stdout.trim();
129
+ };
130
+
131
+ const ownsPid = (pid) => {
132
+ if (!isAlive(pid)) return false;
133
+ const command = liveCommand(pid);
134
+ return command !== null && OWNED_COMMAND.test(command);
135
+ };
136
+
137
+ const terminate = async (pid) => {
138
+ // Ownership is re-proved here rather than trusted from the caller's earlier
139
+ // read: a pid can be reused between the two calls.
140
+ if (!ownsPid(pid)) return !isAlive(pid);
141
+ if (!signalPid(pid, 'SIGTERM')) {
142
+ // ESRCH means it is already gone; EPERM means no signal can ever land, so
143
+ // the grace period could not change the outcome either.
144
+ return !isAlive(pid);
145
+ }
146
+ const deadline = Date.now() + TERMINATE_WAIT_MS;
147
+ while (Date.now() < deadline) {
148
+ if (!isAlive(pid)) return true;
149
+ await sleep(TERMINATE_POLL_INTERVAL_MS);
150
+ }
151
+ if (!isAlive(pid)) return true;
152
+ // A reused pid must not receive the escalation either.
153
+ if (ownsPid(pid)) signalPid(pid, 'SIGKILL');
154
+ const killDeadline = Date.now() + KILL_WAIT_MS;
155
+ while (Date.now() < killDeadline) {
156
+ if (!isAlive(pid)) return true;
157
+ await sleep(TERMINATE_POLL_INTERVAL_MS);
158
+ }
159
+ return !isAlive(pid);
160
+ };
161
+
162
+ /* ---------------------------------- marker -------------------------------- */
163
+
164
+ const readMarker = (roots) => {
165
+ if (!fs.existsSync(roots.markerPath)) return { marker: null, present: false };
166
+ try {
167
+ const marker = JSON.parse(fs.readFileSync(roots.markerPath, 'utf8'));
168
+ const valid =
169
+ Number.isInteger(marker?.pid) &&
170
+ marker.pid > 0 &&
171
+ typeof marker.host === 'string' &&
172
+ LOOPBACK_HOSTS.includes(marker.host) &&
173
+ Number.isInteger(marker.port) &&
174
+ marker.port >= 0 &&
175
+ marker.port <= 65535;
176
+ return { marker: valid ? marker : null, present: true };
177
+ } catch {
178
+ return { marker: null, present: true };
179
+ }
180
+ };
181
+
182
+ const removeMarker = (roots) => {
183
+ try {
184
+ fs.rmSync(roots.markerPath, { force: true });
185
+ } catch {
186
+ /* a marker that cannot be removed is reported by the next start probe */
187
+ }
188
+ };
189
+
190
+ const writeMarker = (roots, marker) => {
191
+ fs.mkdirSync(roots.markerDir, { recursive: true });
192
+ const temporary = `${roots.markerPath}.tmp`;
193
+ fs.writeFileSync(temporary, `${JSON.stringify(marker, null, 2)}\n`, { mode: MARKER_FILE_MODE });
194
+ // `mode` only applies on creation: a `.tmp` left by an earlier crash must not
195
+ // donate its permissions to the rename.
196
+ fs.chmodSync(temporary, MARKER_FILE_MODE);
197
+ fs.renameSync(temporary, roots.markerPath);
198
+ };
199
+
200
+ /* ---------------------------------- health -------------------------------- */
201
+
202
+ const isHealthDocument = (body) =>
203
+ body !== null &&
204
+ typeof body === 'object' &&
205
+ typeof body.status === 'string' &&
206
+ typeof body.db === 'object' &&
207
+ body.db !== null &&
208
+ typeof body.tasks === 'object' &&
209
+ body.tasks !== null;
210
+
211
+ const probeHealth = async (host, port) => {
212
+ if (port === 0) return { kind: 'free' };
213
+ try {
214
+ const response = await fetch(`${describeTarget({ host, port })}/health`, {
215
+ signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
216
+ });
217
+ let body = null;
218
+ try {
219
+ body = await response.json();
220
+ } catch {
221
+ body = null;
222
+ }
223
+ return { kind: 'response', status: response.status, body };
224
+ } catch (error) {
225
+ if (error?.name === 'TimeoutError') return { kind: 'timeout' };
226
+ if (error?.cause?.code === 'ECONNREFUSED' || error?.cause?.code === 'ECONNRESET') return { kind: 'refused' };
227
+ return { kind: 'error', reason: error?.cause?.message ?? error?.message };
228
+ }
229
+ };
230
+
231
+ /**
232
+ * `free` covers a refusal, a timeout and any other transport error: the only
233
+ * safe reading of "no health document came back" is "no known server", and the
234
+ * spawn decides the port race with its own bind (`EADDRINUSE`).
235
+ */
236
+ const classifyProbe = (probe) => {
237
+ if (probe.kind !== 'response') return { kind: 'free' };
238
+ if (probe.status === 200 && isHealthDocument(probe.body)) return { kind: 'health', body: probe.body };
239
+ if (probe.status === 503 && probe.body?.error?.code === 'E_DB_MISSING') return { kind: 'missing-db' };
240
+ return { kind: 'foreign', status: probe.status };
241
+ };
242
+
243
+ const repairProjection = (roots) => {
244
+ const result = spawnSync(process.execPath, [cliPath(), 'sync', '--root', roots.teamRoot, '--quiet'], {
245
+ encoding: 'utf8',
246
+ env: process.env,
247
+ timeout: SYNC_TIMEOUT_MS,
248
+ });
249
+ if (!result.error && result.status === 0) return;
250
+ const lastLine = result.stderr.trim().split('\n').filter(Boolean).slice(-1)[0];
251
+ const detail = result.error?.message ?? lastLine ?? `sync exited ${result.status}`;
252
+ uiFail('E_UI_DB_MISSING', `the projection is still missing and team-db.mjs sync failed: ${detail}`, 1);
253
+ };
254
+
255
+ /* ---------------------------------- spawn --------------------------------- */
256
+
257
+ const spawnServe = (roots, target) =>
258
+ spawn(
259
+ process.execPath,
260
+ [cliPath(), 'serve', '--root', roots.teamRoot, '--host', target.host, '--port', String(target.port)],
261
+ { cwd: process.cwd(), detached: true, stdio: ['ignore', 'pipe', 'pipe'] },
262
+ );
263
+
264
+ const readServingLine = (child) =>
265
+ new Promise((resolve, reject) => {
266
+ let stdout = '';
267
+ let stderr = '';
268
+ let settled = false;
269
+ const settle = (fn, value) => {
270
+ if (settled) return;
271
+ settled = true;
272
+ clearTimeout(timer);
273
+ fn(value);
274
+ };
275
+ const timer = setTimeout(
276
+ () =>
277
+ settle(
278
+ reject,
279
+ new UiError('E_UI_START_TIMEOUT', `serve did not report its URL within ${START_TIMEOUT_MS} ms`, 2),
280
+ ),
281
+ START_TIMEOUT_MS,
282
+ );
283
+ child.stdout.setEncoding('utf8');
284
+ child.stderr.setEncoding('utf8');
285
+ child.stdout.on('data', (chunk) => {
286
+ stdout += chunk;
287
+ const match = /^team-db: serving (\S+)/m.exec(stdout);
288
+ if (match) settle(resolve, { url: match[1] });
289
+ });
290
+ child.stderr.on('data', (chunk) => {
291
+ stderr += chunk;
292
+ });
293
+ child.once('error', (error) => settle(reject, new UiError('E_UI_CHILD_EXIT', error.message, 2)));
294
+ child.once('exit', (code, signal) => {
295
+ const lastLine = stderr.trim().split('\n').filter(Boolean).slice(-1)[0];
296
+ const failureCode = /EADDRINUSE/.test(`${stdout}\n${stderr}`) ? 'E_UI_PORT_IN_USE' : 'E_UI_CHILD_EXIT';
297
+ const reason = signal === null ? `code ${code}` : signal;
298
+ settle(
299
+ reject,
300
+ new UiError(failureCode, `serve exited (${reason})${lastLine ? `: ${lastLine}` : ''}`, 2),
301
+ );
302
+ });
303
+ });
304
+
305
+ /**
306
+ * The parent must exit while the server keeps running, so both pipes are
307
+ * destroyed rather than drained — draining would hold the event loop open. The
308
+ * child therefore gets EPIPE on its next diagnostic write after the serving
309
+ * line; `team-db.mjs serve` installs a no-op `error` handler for exactly that.
310
+ */
311
+ const detachChild = (child) => {
312
+ child.unref();
313
+ for (const stream of [child.stdout, child.stderr]) {
314
+ if (stream) stream.destroy();
315
+ }
316
+ };
317
+
318
+ const terminateSpawned = async (pid) => {
319
+ if (pid !== undefined && isAlive(pid)) await terminate(pid);
320
+ };
321
+
322
+ /* --------------------------------- results -------------------------------- */
323
+
324
+ const engineChangedSince = (startedAt) => {
325
+ const since = Date.parse(startedAt);
326
+ if (Number.isNaN(since)) return false;
327
+ try {
328
+ return fs.statSync(cliPath()).mtimeMs > since;
329
+ } catch {
330
+ return false;
331
+ }
332
+ };
333
+
334
+ const staleWarning = 'stale: the engine changed after the server started; run ui restart';
335
+
336
+ const runningResult = ({ verb, owner, pid, target, startedAt, rootId, marker, health, stale, spawned }) => ({
337
+ ok: true,
338
+ verb,
339
+ running: true,
340
+ spawned: Boolean(spawned),
341
+ owner,
342
+ pid: pid ?? null,
343
+ host: target.host,
344
+ port: target.port,
345
+ url: describeTarget(target),
346
+ startedAt: startedAt ?? null,
347
+ rootId,
348
+ marker,
349
+ health,
350
+ stale,
351
+ staleWarning: stale ? staleWarning : null,
352
+ });
353
+
354
+ const schedulerLine = (health) => {
355
+ const scheduler = health?.scheduler;
356
+ if (!scheduler || typeof scheduler.state !== 'string') return null;
357
+ return `scheduler: ${scheduler.state}${Number.isInteger(scheduler.pid) ? ` (pid ${scheduler.pid})` : ''}`;
358
+ };
359
+
360
+ const pidLabel = (pid) => (Number.isInteger(pid) ? `pid ${pid}, ` : '');
361
+
362
+ /* ---------------------------------- start --------------------------------- */
363
+
364
+ const settleRunning = (roots, marker, health, target) => {
365
+ const healthRootId = typeof health.rootId === 'string' ? health.rootId : null;
366
+ if (healthRootId !== null && healthRootId !== roots.rootId) {
367
+ uiFail(
368
+ 'E_UI_OTHER_ROOT',
369
+ `${describeTarget(target)} serves team root ${healthRootId}; this repository is ${roots.rootId}`,
370
+ 1,
371
+ );
372
+ }
373
+ const markerOwned = marker !== null && ownsPid(marker.pid);
374
+ const owned = markerOwned && marker.rootId === roots.rootId;
375
+ const startedAt = owned ? marker.startedAt : null;
376
+ return runningResult({
377
+ verb: 'start',
378
+ owner: owned ? OWNER_STARTED : OWNER_PRE_EXISTING,
379
+ pid: markerOwned ? marker.pid : null,
380
+ target,
381
+ startedAt,
382
+ rootId: roots.rootId,
383
+ marker,
384
+ health,
385
+ stale: owned && engineChangedSince(startedAt),
386
+ });
387
+ };
388
+
389
+ /**
390
+ * Probe with the one repair the lifecycle owns: a `503 E_DB_MISSING` answer buys
391
+ * exactly one bounded `sync` and one re-probe.
392
+ */
393
+ const probeView = async (roots, target) => {
394
+ const view = classifyProbe(await probeHealth(target.host, target.port));
395
+ if (view.kind === 'foreign') {
396
+ uiFail(
397
+ 'E_UI_PORT_FOREIGN',
398
+ `${describeTarget(target)} answered (HTTP ${view.status}) but not with a team-db health document`,
399
+ 1,
400
+ );
401
+ }
402
+ if (view.kind !== 'missing-db') return view;
403
+ repairProjection(roots);
404
+ const again = classifyProbe(await probeHealth(target.host, target.port));
405
+ if (again.kind !== 'health') {
406
+ uiFail('E_UI_DB_MISSING', `${describeTarget(target)} still reports a missing projection after one sync`, 1);
407
+ }
408
+ return again;
409
+ };
410
+
411
+ const awaitHealth = async (roots, target, deadline) => {
412
+ while (Date.now() < deadline) {
413
+ const view = classifyProbe(await probeHealth(target.host, target.port));
414
+ if (view.kind === 'health') return view.body;
415
+ if (view.kind === 'missing-db') {
416
+ repairProjection(roots);
417
+ const again = classifyProbe(await probeHealth(target.host, target.port));
418
+ if (again.kind === 'health') return again.body;
419
+ uiFail('E_UI_DB_MISSING', 'the projection is still missing after one sync; run team-db.mjs sync and retry', 1);
420
+ }
421
+ await sleep(HEALTH_POLL_INTERVAL_MS);
422
+ }
423
+ uiFail('E_UI_START_TIMEOUT', `no healthy answer on ${describeTarget(target)} within ${START_TIMEOUT_MS} ms`, 2);
424
+ };
425
+
426
+ const startUi = async (roots, target) => {
427
+ const marker = readMarker(roots).marker;
428
+ const ownedMarker = marker !== null && marker.rootId === roots.rootId && ownsPid(marker.pid) ? marker : null;
429
+
430
+ // A marker that names a live owned server on another target is the same
431
+ // "already running" no-op: one repository never runs two dashboards. A probe
432
+ // that answers anything else falls through to the requested target.
433
+ if (ownedMarker !== null && (ownedMarker.host !== target.host || ownedMarker.port !== target.port)) {
434
+ const own = classifyProbe(await probeHealth(ownedMarker.host, ownedMarker.port));
435
+ if (own.kind === 'health' && own.body.rootId === roots.rootId) {
436
+ return settleRunning(roots, ownedMarker, own.body, { host: ownedMarker.host, port: ownedMarker.port });
437
+ }
438
+ }
439
+
440
+ // Probe first: a server that already answers 200 for this root is the no-op
441
+ // the contract pins (same pid, unchanged `startedAt`). `restart` is the verb
442
+ // that stops and replaces it, so this path never stops or spawns.
443
+ const view = await probeView(roots, target);
444
+ if (view.kind === 'health') return settleRunning(roots, marker, view.body, target);
445
+
446
+ // The port is free: a marker whose pid is gone or not ours is stale.
447
+ if (readMarker(roots).present && ownedMarker === null) removeMarker(roots);
448
+
449
+ const startedAt = new Date().toISOString();
450
+ const deadline = Date.now() + START_TIMEOUT_MS;
451
+ let child;
452
+ try {
453
+ child = spawnServe(roots, target);
454
+ } catch (error) {
455
+ uiFail('E_UI_CHILD_EXIT', `serve could not start: ${error.message}`, 2);
456
+ }
457
+ let serving;
458
+ try {
459
+ serving = await readServingLine(child);
460
+ } catch (error) {
461
+ await terminateSpawned(child.pid);
462
+ if (error.code === 'E_UI_PORT_IN_USE') {
463
+ const raced = classifyProbe(await probeHealth(target.host, target.port));
464
+ if (raced.kind === 'health') return settleRunning(roots, marker, raced.body, target);
465
+ uiFail('E_UI_PORT_IN_USE', `port ${target.port} was taken between the probe and the spawn`, 2);
466
+ }
467
+ throw error;
468
+ }
469
+ detachChild(child);
470
+ const url = new URL(serving.url);
471
+ const boundPort = Number(url.port);
472
+ const nextMarker = {
473
+ schemaVersion: MARKER_SCHEMA_VERSION,
474
+ pid: child.pid,
475
+ host: target.host,
476
+ port: boundPort,
477
+ root: roots.teamRoot,
478
+ rootId: roots.rootId,
479
+ startedAt,
480
+ command: MARKER_COMMAND,
481
+ };
482
+ // Every throw after the spawn leaves neither the marker nor the child behind.
483
+ const cleanup = async () => {
484
+ try {
485
+ fs.rmSync(`${roots.markerPath}.tmp`, { force: true });
486
+ } catch {
487
+ /* the directory itself may be unwritable */
488
+ }
489
+ removeMarker(roots);
490
+ await terminateSpawned(child.pid);
491
+ };
492
+ try {
493
+ writeMarker(roots, nextMarker);
494
+ } catch (error) {
495
+ await cleanup();
496
+ uiFail('E_UI_MARKER_UNWRITABLE', `${roots.markerPath} could not be written: ${error.message}`, 2);
497
+ }
498
+ let health;
499
+ try {
500
+ health = await awaitHealth(roots, { host: target.host, port: boundPort }, deadline);
501
+ } catch (error) {
502
+ await cleanup();
503
+ if (error instanceof UiError) throw error;
504
+ uiFail('E_UI_CHILD_EXIT', `serve failed after it reported its URL: ${error.message}`, 2);
505
+ }
506
+ return runningResult({
507
+ verb: 'start',
508
+ spawned: true,
509
+ owner: OWNER_STARTED,
510
+ pid: child.pid,
511
+ target: { host: target.host, port: boundPort },
512
+ startedAt,
513
+ rootId: roots.rootId,
514
+ marker: nextMarker,
515
+ health,
516
+ stale: false,
517
+ });
518
+ };
519
+
520
+ /* ---------------------------------- stop ---------------------------------- */
521
+
522
+ const stopUi = async (roots) => {
523
+ const { marker, present } = readMarker(roots);
524
+ if (marker === null) {
525
+ if (!present) return { state: 'nothing', pid: null };
526
+ removeMarker(roots);
527
+ return { state: 'stale', pid: null };
528
+ }
529
+ if (!isAlive(marker.pid)) {
530
+ removeMarker(roots);
531
+ return { state: 'stale', pid: marker.pid };
532
+ }
533
+ const command = liveCommand(marker.pid);
534
+ if (command === null || !OWNED_COMMAND.test(command)) {
535
+ return { state: 'unowned', pid: marker.pid };
536
+ }
537
+ const gone = await terminate(marker.pid);
538
+ // A process that outlived SIGKILL keeps its marker: the next call must still
539
+ // find it instead of reporting a clean stop that never happened.
540
+ if (!gone) return { state: 'survived', pid: marker.pid };
541
+ removeMarker(roots);
542
+ return { state: 'stopped', pid: marker.pid };
543
+ };
544
+
545
+ /* --------------------------------- status --------------------------------- */
546
+
547
+ const statusUi = async (roots) => {
548
+ const { marker } = readMarker(roots);
549
+ const owned = marker !== null && ownsPid(marker.pid);
550
+ const target = {
551
+ host: marker?.host ?? DEFAULT_HOST,
552
+ port: marker?.port ?? DEFAULT_PORT,
553
+ };
554
+ const view = classifyProbe(await probeHealth(target.host, target.port));
555
+ if (view.kind !== 'health') {
556
+ return {
557
+ ok: true,
558
+ verb: 'status',
559
+ running: false,
560
+ owner: null,
561
+ pid: null,
562
+ host: target.host,
563
+ port: target.port,
564
+ url: describeTarget(target),
565
+ startedAt: null,
566
+ rootId: roots.rootId,
567
+ marker,
568
+ health: null,
569
+ stale: false,
570
+ staleWarning: null,
571
+ };
572
+ }
573
+ const startedAt = owned ? marker.startedAt : null;
574
+ return runningResult({
575
+ verb: 'status',
576
+ owner: owned ? OWNER_STARTED : OWNER_PRE_EXISTING,
577
+ pid: owned ? marker.pid : null,
578
+ target,
579
+ startedAt,
580
+ rootId: roots.rootId,
581
+ marker,
582
+ health: view.body,
583
+ stale: owned && engineChangedSince(startedAt),
584
+ });
585
+ };
586
+
587
+ /* -------------------------------- reporting ------------------------------- */
588
+
589
+ const createReporter = (flags) => {
590
+ const json = Boolean(flags.json);
591
+ const quiet = Boolean(flags.quiet);
592
+ return {
593
+ info: (line) => {
594
+ if (!json && !quiet) process.stdout.write(`${line}\n`);
595
+ },
596
+ warn: (line) => process.stderr.write(`${line}\n`),
597
+ result: (value) => {
598
+ if (json) process.stdout.write(`${JSON.stringify(value)}\n`);
599
+ },
600
+ };
601
+ };
602
+
603
+ const announceStart = (out, result) => {
604
+ out.info(`ui: already running (${pidLabel(result.pid)}owner: ${result.owner})`);
605
+ const scheduler = schedulerLine(result.health);
606
+ if (scheduler) out.info(scheduler);
607
+ if (result.stale) out.warn(`ui: ${result.staleWarning}`);
608
+ };
609
+
610
+ const announceFreshStart = (out, result) => {
611
+ out.info(`ui: ${result.url} (pid ${result.pid})`);
612
+ const scheduler = schedulerLine(result.health);
613
+ if (scheduler) out.info(scheduler);
614
+ };
615
+
616
+ const announceStatus = (out, result) => {
617
+ if (!result.running) {
618
+ out.info('ui: nothing running');
619
+ return;
620
+ }
621
+ out.info(`ui: running ${result.url} (${pidLabel(result.pid)}owner: ${result.owner})`);
622
+ const scheduler = schedulerLine(result.health);
623
+ if (scheduler) out.info(scheduler);
624
+ if (result.stale) out.warn(`ui: ${result.staleWarning}`);
625
+ };
626
+
627
+ const announceStop = (out, result) => {
628
+ if (result.state === 'stopped') out.info(`ui: stopped (pid ${result.pid})`);
629
+ else if (result.state === 'stale') out.info('ui: nothing running (stale marker removed)');
630
+ else if (result.state === 'unowned') out.info(`ui: marker pid ${result.pid} is not team-db.mjs serve; left running`);
631
+ else if (result.state === 'survived') {
632
+ out.warn(`ui: pid ${result.pid} is still running after the 5 s grace and SIGKILL; the marker is kept`);
633
+ } else out.info('ui: nothing running');
634
+ };
635
+
636
+ /* ---------------------------------- verbs --------------------------------- */
637
+
638
+ const parseTarget = (flags) => {
639
+ const host = typeof flags.host === 'string' ? flags.host.trim() : DEFAULT_HOST;
640
+ if (!LOOPBACK_HOSTS.includes(host)) {
641
+ uiFail('E_USAGE', `--host ${host || '(empty)'} is not loopback; a ui verb never binds a remote host`, 3);
642
+ }
643
+ const port = flags.port === undefined ? DEFAULT_PORT : Number(flags.port);
644
+ if (!Number.isInteger(port) || port < 0 || port > 65535) {
645
+ uiFail('E_USAGE', '--port must be an integer between 0 and 65535', 3);
646
+ }
647
+ return { host, port };
648
+ };
649
+
650
+ /**
651
+ * The two environment refusals every start-shaped verb shares. Both must land
652
+ * before the first write or probe: a disabled runtime must not stop a live
653
+ * server, write a marker or run the repair sync.
654
+ */
655
+ const assertStartAllowed = () => {
656
+ if (isKillSwitchSet(process.env)) uiFail('E_DISABLED', 'disabled by AI_RULES_TEAM_DB_DISABLE', 1);
657
+ if (!isSqliteAvailable(process.env)) uiFail('E_UI_UNAVAILABLE', INDEX_UNAVAILABLE_LINE, 1);
658
+ };
659
+
660
+ const openDashboard = (url) => {
661
+ try {
662
+ const command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open';
663
+ const child = spawn(command, [url], { detached: true, stdio: 'ignore' });
664
+ child.on('error', (error) => {
665
+ process.stderr.write(`ui: could not open ${url} (${error.code ?? error.message}); open it manually\n`);
666
+ });
667
+ child.unref();
668
+ } catch {
669
+ /* opening a browser is a convenience, never a failure */
670
+ }
671
+ };
672
+
673
+ const runStart = async (flags, out) => {
674
+ const roots = resolveRoots(flags);
675
+ const target = parseTarget(flags);
676
+ assertStartAllowed();
677
+ // Probe-first no-op; the only writes are the marker and at most one sync.
678
+ const result = await startUi(roots, target);
679
+ if (result.spawned) announceFreshStart(out, result);
680
+ else announceStart(out, result);
681
+ if (flags.open) openDashboard(result.url);
682
+ out.result(result);
683
+ return 0;
684
+ };
685
+
686
+ const runRestart = async (flags, out) => {
687
+ // `restart` is the explicit stop+start verb; `start` never stops anything.
688
+ const roots = resolveRoots(flags);
689
+ const target = parseTarget(flags);
690
+ assertStartAllowed();
691
+ const stopped = await stopUi(roots);
692
+ if (stopped.state === 'survived') {
693
+ announceStop(out, stopped);
694
+ out.result({ ok: false, verb: 'restart', state: stopped.state, pid: stopped.pid });
695
+ return 1;
696
+ }
697
+ if (stopped.state !== 'nothing') announceStop(out, stopped);
698
+ const result = await startUi(roots, target);
699
+ if (result.spawned) announceFreshStart(out, result);
700
+ else announceStart(out, result);
701
+ if (flags.open) openDashboard(result.url);
702
+ out.result(result);
703
+ return 0;
704
+ };
705
+
706
+ const runStop = async (flags, out) => {
707
+ const result = await stopUi(resolveRoots(flags));
708
+ announceStop(out, result);
709
+ out.result({ ok: result.state !== 'survived', verb: 'stop', state: result.state, pid: result.pid });
710
+ return result.state === 'survived' ? 1 : 0;
711
+ };
712
+
713
+ const runStatus = async (flags, out) => {
714
+ const result = await statusUi(resolveRoots(flags));
715
+ announceStatus(out, result);
716
+ out.result(result);
717
+ return 0;
718
+ };
719
+
720
+ const runVerb = async (verb, flags, out) => {
721
+ if (verb === 'stop') return runStop(flags, out);
722
+ if (verb === 'status') return runStatus(flags, out);
723
+ if (verb === 'restart') return runRestart(flags, out);
724
+ return runStart(flags, out);
725
+ };
726
+
727
+ /* ---------------------------------- entry --------------------------------- */
728
+
729
+ /**
730
+ * One `ui` dispatch: `positionals[0]` is the verb, and the flags already passed
731
+ * the CLI's union allow-list, so this layer owns the per-verb reach table.
732
+ */
733
+ export async function runUiCommand({ flags, positionals }) {
734
+ const out = createReporter(flags);
735
+ try {
736
+ const [verb, ...rest] = positionals;
737
+ if (verb === undefined) uiFail('E_USAGE', 'ui needs a verb: start, stop, status or restart', 3);
738
+ if (rest.length > 0) uiFail('E_USAGE', `ui ${verb} takes no further arguments`, 3);
739
+ if (!VERB_FLAGS[verb]) uiFail('E_USAGE', `unknown ui verb "${verb}"; expected start, stop, status or restart`, 3);
740
+ for (const name of Object.keys(flags)) {
741
+ if (!VERB_FLAGS[verb].has(name)) uiFail('E_USAGE', `flag --${name} does not apply to ui ${verb}`, 3);
742
+ }
743
+ return await runVerb(verb, flags, out);
744
+ } catch (error) {
745
+ if (!(error instanceof UiError)) throw error;
746
+ out.result({ ok: false, code: error.code, message: error.message });
747
+ out.warn(`ui: ${error.code}: ${error.message}`);
748
+ return error.exitCode;
749
+ }
750
+ }