@bonesofspring/ai-rules 0.2.64 → 0.2.66

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 (464) hide show
  1. package/CHANGELOG.md +80 -11
  2. package/README.md +24 -16
  3. package/bin/lib/engine-scripts.js +52 -12
  4. package/bin/lib/engine-upgrade.js +5 -2
  5. package/bin/lib/seed-ai-local-team.js +13 -3
  6. package/docs/OPENCODE.md +17 -9
  7. package/docs/PRESET-CONTRIBUTION.md +41 -0
  8. package/docs/TEAM-DB.md +240 -60
  9. package/docs/TOKEN-ECONOMY.md +79 -0
  10. package/docs/openapi/team-db.v1.json +1238 -377
  11. package/mcp/defectdojo-mcp.mjs +7 -5
  12. package/mcp/lib/defectdojo.mjs +26 -14
  13. package/package.json +31 -20
  14. package/presets/_shared/ai-local/scripts/dump-output.mjs +371 -0
  15. package/presets/_shared/ai-local/scripts/process-lease.mjs +576 -0
  16. package/presets/_shared/ai-local/scripts/processes.mjs +1618 -0
  17. package/presets/_shared/ai-local/scripts/run.mjs +5 -5
  18. package/presets/_shared/ai-local/scripts/team-db-api.mjs +465 -152
  19. package/presets/_shared/ai-local/scripts/team-db-dashboard.mjs +235 -10
  20. package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +416 -176
  21. package/presets/_shared/ai-local/scripts/team-db-project.mjs +171 -62
  22. package/presets/_shared/ai-local/scripts/team-db-scheduler.mjs +4 -0
  23. package/presets/_shared/ai-local/scripts/team-db-schema.mjs +143 -5
  24. package/presets/_shared/ai-local/scripts/team-db-task-runner.mjs +567 -0
  25. package/presets/_shared/ai-local/scripts/{team-db-backlog.mjs → team-db-tracker.mjs} +900 -197
  26. package/presets/_shared/ai-local/scripts/team-db-ui.mjs +750 -0
  27. package/presets/_shared/ai-local/scripts/team-db.mjs +184 -23
  28. package/presets/_shared/core/agent-team/agent-team-orchestrator.md +4 -2
  29. package/presets/_shared/core/architecture/feature-delivery-workflow.md +4 -0
  30. package/presets/_shared/core/quality/context-and-iteration.md +1 -0
  31. package/presets/_shared/hooks/chain-team-phases.sh.tmpl +105 -17
  32. package/presets/_shared/hooks/guard-shell-command.mjs.tmpl +40 -2
  33. package/presets/claude/android-kotlin/agents/task-analyst.md +1 -1
  34. package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +105 -17
  35. package/presets/claude/android-kotlin/hooks/guard-shell-command.mjs +41 -2
  36. package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  37. package/presets/claude/android-kotlin/settings.json +6 -1
  38. package/presets/claude/go/agents/task-analyst.md +1 -1
  39. package/presets/claude/go/hooks/chain-team-phases.sh +105 -17
  40. package/presets/claude/go/hooks/guard-shell-command.mjs +41 -2
  41. package/presets/claude/go/rules/architecture/feature-delivery.md +4 -0
  42. package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  43. package/presets/claude/go/settings.json +6 -1
  44. package/presets/claude/ios-swift/agents/task-analyst.md +1 -1
  45. package/presets/claude/ios-swift/hooks/chain-team-phases.sh +105 -17
  46. package/presets/claude/ios-swift/hooks/guard-shell-command.mjs +41 -2
  47. package/presets/claude/ios-swift/rules/architecture/feature-delivery.md +4 -0
  48. package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  49. package/presets/claude/ios-swift/settings.json +6 -1
  50. package/presets/claude/java/agents/task-analyst.md +1 -1
  51. package/presets/claude/java/hooks/chain-team-phases.sh +105 -17
  52. package/presets/claude/java/hooks/guard-shell-command.mjs +41 -2
  53. package/presets/claude/java/rules/architecture/feature-delivery.md +4 -0
  54. package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  55. package/presets/claude/java/settings.json +6 -1
  56. package/presets/claude/mcp-ts/agents/task-analyst.md +1 -1
  57. package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +105 -17
  58. package/presets/claude/mcp-ts/hooks/guard-shell-command.mjs +41 -2
  59. package/presets/claude/mcp-ts/rules/architecture/feature-delivery.md +4 -0
  60. package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  61. package/presets/claude/mcp-ts/settings.json +6 -1
  62. package/presets/claude/mol/agents/task-analyst.md +1 -1
  63. package/presets/claude/mol/hooks/chain-team-phases.sh +105 -17
  64. package/presets/claude/mol/hooks/guard-shell-command.mjs +41 -2
  65. package/presets/claude/mol/rules/architecture/feature-delivery-workflow.md +4 -0
  66. package/presets/claude/mol/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  67. package/presets/claude/mol/settings.json +6 -1
  68. package/presets/claude/next/MCP.md +2 -0
  69. package/presets/claude/next/REPO_AGENTS.md +2 -0
  70. package/presets/claude/next/agents/build-verifier.md +1 -1
  71. package/presets/claude/next/agents/ci-investigator.md +3 -0
  72. package/presets/claude/next/agents/debugger.md +3 -0
  73. package/presets/claude/next/agents/feature-developer.md +3 -0
  74. package/presets/claude/next/agents/mockup-reviewer.md +2 -0
  75. package/presets/claude/next/agents/performance-auditor.md +2 -0
  76. package/presets/claude/next/agents/playwright-test-generator.md +3 -0
  77. package/presets/claude/next/agents/playwright-test-healer.md +3 -0
  78. package/presets/claude/next/agents/playwright-test-planner.md +3 -0
  79. package/presets/claude/next/agents/qa-tester.md +3 -0
  80. package/presets/claude/next/agents/task-analyst.md +1 -1
  81. package/presets/claude/next/commands/README.md +7 -1
  82. package/presets/claude/next/commands/task.md +1 -1
  83. package/presets/claude/next/commands/ui-start.md +78 -0
  84. package/presets/claude/next/commands/ui-stop.md +71 -0
  85. package/presets/claude/next/hooks/chain-team-phases.sh +105 -17
  86. package/presets/claude/next/hooks/guard-shell-command.mjs +41 -2
  87. package/presets/claude/next/rules/README.md +2 -1
  88. package/presets/claude/next/rules/architecture/feature-delivery-workflow.md +4 -0
  89. package/presets/claude/next/rules/stack/next-app-core.md +1 -0
  90. package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  91. package/presets/claude/next/rules/tooling-and-review/dev-process-lifecycle.md +43 -0
  92. package/presets/claude/next/settings.json +6 -1
  93. package/presets/claude/next/team/README.md +7 -3
  94. package/presets/claude/nuxt/agents/task-analyst.md +1 -1
  95. package/presets/claude/nuxt/hooks/chain-team-phases.sh +105 -17
  96. package/presets/claude/nuxt/hooks/guard-shell-command.mjs +41 -2
  97. package/presets/claude/nuxt/rules/architecture/feature-delivery-workflow.md +4 -0
  98. package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  99. package/presets/claude/nuxt/settings.json +6 -1
  100. package/presets/claude/php-hexagonal/agents/task-analyst.md +1 -1
  101. package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +105 -17
  102. package/presets/claude/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
  103. package/presets/claude/php-hexagonal/rules/architecture/feature-delivery.md +4 -0
  104. package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  105. package/presets/claude/php-hexagonal/settings.json +6 -1
  106. package/presets/claude/php-laravel/agents/task-analyst.md +1 -1
  107. package/presets/claude/php-laravel/hooks/chain-team-phases.sh +106 -18
  108. package/presets/claude/php-laravel/hooks/guard-shell-command.mjs +41 -2
  109. package/presets/claude/php-laravel/rules/architecture/feature-delivery.md +4 -0
  110. package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  111. package/presets/claude/php-laravel/settings.json +6 -1
  112. package/presets/claude/svelte/agents/task-analyst.md +1 -1
  113. package/presets/claude/svelte/hooks/chain-team-phases.sh +105 -17
  114. package/presets/claude/svelte/hooks/guard-shell-command.mjs +41 -2
  115. package/presets/claude/svelte/rules/architecture/feature-delivery-workflow.md +4 -0
  116. package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +4 -2
  117. package/presets/claude/svelte/settings.json +6 -1
  118. package/presets/cursor/android-kotlin/agents/task-analyst.md +1 -1
  119. package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +105 -17
  120. package/presets/cursor/android-kotlin/hooks/guard-shell-command.mjs +41 -2
  121. package/presets/cursor/android-kotlin/rules/agent-team-orchestrator.mdc +1 -1
  122. package/presets/cursor/go/agents/task-analyst.md +1 -1
  123. package/presets/cursor/go/hooks/chain-team-phases.sh +105 -17
  124. package/presets/cursor/go/hooks/guard-shell-command.mjs +41 -2
  125. package/presets/cursor/go/rules/agent-team-orchestrator.mdc +1 -1
  126. package/presets/cursor/ios-swift/agents/task-analyst.md +1 -1
  127. package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +105 -17
  128. package/presets/cursor/ios-swift/hooks/guard-shell-command.mjs +41 -2
  129. package/presets/cursor/ios-swift/rules/agent-team-orchestrator.mdc +1 -1
  130. package/presets/cursor/java/agents/task-analyst.md +1 -1
  131. package/presets/cursor/java/hooks/chain-team-phases.sh +105 -17
  132. package/presets/cursor/java/hooks/guard-shell-command.mjs +41 -2
  133. package/presets/cursor/java/rules/agent-team-orchestrator.mdc +1 -1
  134. package/presets/cursor/mcp-ts/agents/task-analyst.md +1 -1
  135. package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +105 -17
  136. package/presets/cursor/mcp-ts/hooks/guard-shell-command.mjs +41 -2
  137. package/presets/cursor/mcp-ts/rules/agent-team-orchestrator.mdc +1 -1
  138. package/presets/cursor/mol/agents/task-analyst.md +1 -1
  139. package/presets/cursor/mol/hooks/chain-team-phases.sh +105 -17
  140. package/presets/cursor/mol/hooks/guard-shell-command.mjs +41 -2
  141. package/presets/cursor/mol/rules/agent-team-orchestrator.mdc +1 -1
  142. package/presets/cursor/next/MCP.md +2 -0
  143. package/presets/cursor/next/REPO_AGENTS.md +2 -0
  144. package/presets/cursor/next/agents/build-verifier.md +1 -1
  145. package/presets/cursor/next/agents/ci-investigator.md +2 -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 +2 -0
  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 +1 -1
  155. package/presets/cursor/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
  156. package/presets/cursor/next/commands/README.md +7 -1
  157. package/presets/cursor/next/commands/task.md +1 -1
  158. package/presets/cursor/next/commands/ui-start.md +78 -0
  159. package/presets/cursor/next/commands/ui-stop.md +71 -0
  160. package/presets/cursor/next/hooks/chain-team-phases.sh +105 -17
  161. package/presets/cursor/next/hooks/guard-shell-command.mjs +41 -2
  162. package/presets/cursor/next/rules/README.md +2 -1
  163. package/presets/cursor/next/rules/agent-team-orchestrator.mdc +3 -1
  164. package/presets/cursor/next/rules/dev-process-lifecycle.mdc +43 -0
  165. package/presets/cursor/next/rules/next-app-core.mdc +1 -0
  166. package/presets/cursor/next/team/README.md +7 -3
  167. package/presets/cursor/nuxt/agents/task-analyst.md +1 -1
  168. package/presets/cursor/nuxt/hooks/chain-team-phases.sh +105 -17
  169. package/presets/cursor/nuxt/hooks/guard-shell-command.mjs +41 -2
  170. package/presets/cursor/nuxt/rules/agent-team-orchestrator.mdc +1 -1
  171. package/presets/cursor/php-hexagonal/agents/task-analyst.md +1 -1
  172. package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +105 -17
  173. package/presets/cursor/php-hexagonal/hooks/guard-shell-command.mjs +41 -2
  174. package/presets/cursor/php-hexagonal/rules/agent-team-orchestrator.mdc +1 -1
  175. package/presets/cursor/php-laravel/agents/task-analyst.md +1 -1
  176. package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +106 -18
  177. package/presets/cursor/php-laravel/hooks/guard-shell-command.mjs +41 -2
  178. package/presets/cursor/php-laravel/rules/agent-team-orchestrator.mdc +1 -1
  179. package/presets/cursor/svelte/agents/task-analyst.md +1 -1
  180. package/presets/cursor/svelte/hooks/chain-team-phases.sh +105 -17
  181. package/presets/cursor/svelte/hooks/guard-shell-command.mjs +41 -2
  182. package/presets/cursor/svelte/rules/agent-team-orchestrator.mdc +1 -1
  183. package/presets/mavis/android-kotlin/adapter-map.json +1 -1
  184. package/presets/mavis/android-kotlin/agents/task-analyst.md +1 -1
  185. package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +105 -17
  186. package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +4 -2
  187. package/presets/mavis/go/adapter-map.json +1 -1
  188. package/presets/mavis/go/agents/task-analyst.md +1 -1
  189. package/presets/mavis/go/hooks/chain-team-phases.sh +105 -17
  190. package/presets/mavis/go/rules/agent-team-orchestrator.md +4 -2
  191. package/presets/mavis/go/rules/feature-delivery-workflow.md +4 -0
  192. package/presets/mavis/ios-swift/adapter-map.json +1 -1
  193. package/presets/mavis/ios-swift/agents/task-analyst.md +1 -1
  194. package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +105 -17
  195. package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +4 -2
  196. package/presets/mavis/ios-swift/rules/feature-delivery-workflow.md +4 -0
  197. package/presets/mavis/java/adapter-map.json +1 -1
  198. package/presets/mavis/java/agents/task-analyst.md +1 -1
  199. package/presets/mavis/java/hooks/chain-team-phases.sh +105 -17
  200. package/presets/mavis/java/rules/agent-team-orchestrator.md +4 -2
  201. package/presets/mavis/java/rules/feature-delivery-workflow.md +4 -0
  202. package/presets/mavis/mcp-ts/adapter-map.json +1 -1
  203. package/presets/mavis/mcp-ts/agents/task-analyst.md +1 -1
  204. package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +105 -17
  205. package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +4 -2
  206. package/presets/mavis/mcp-ts/rules/feature-delivery-workflow.md +4 -0
  207. package/presets/mavis/mol/adapter-map.json +1 -1
  208. package/presets/mavis/mol/agents/task-analyst.md +1 -1
  209. package/presets/mavis/mol/hooks/chain-team-phases.sh +105 -17
  210. package/presets/mavis/mol/rules/agent-team-orchestrator.md +4 -2
  211. package/presets/mavis/mol/rules/feature-delivery-workflow.md +4 -0
  212. package/presets/mavis/next/MCP.md +2 -0
  213. package/presets/mavis/next/REPO_AGENTS.md +2 -0
  214. package/presets/mavis/next/adapter-map.json +33 -1
  215. package/presets/mavis/next/agents/build-verifier.md +1 -1
  216. package/presets/mavis/next/agents/ci-investigator.md +3 -0
  217. package/presets/mavis/next/agents/debugger.md +3 -0
  218. package/presets/mavis/next/agents/feature-developer.md +3 -0
  219. package/presets/mavis/next/agents/mockup-reviewer.md +2 -0
  220. package/presets/mavis/next/agents/performance-auditor.md +2 -0
  221. package/presets/mavis/next/agents/playwright-test-generator.md +3 -0
  222. package/presets/mavis/next/agents/playwright-test-healer.md +3 -0
  223. package/presets/mavis/next/agents/playwright-test-planner.md +3 -0
  224. package/presets/mavis/next/agents/qa-tester.md +3 -0
  225. package/presets/mavis/next/agents/task-analyst.md +1 -1
  226. package/presets/mavis/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
  227. package/presets/mavis/next/commands/README.md +7 -1
  228. package/presets/mavis/next/commands/task.md +1 -1
  229. package/presets/mavis/next/commands/ui-start.md +73 -0
  230. package/presets/mavis/next/commands/ui-stop.md +66 -0
  231. package/presets/mavis/next/hooks/chain-team-phases.sh +105 -17
  232. package/presets/mavis/next/rules/agent-team-orchestrator.md +4 -2
  233. package/presets/mavis/next/rules/dev-process-lifecycle.md +38 -0
  234. package/presets/mavis/next/rules/feature-delivery-workflow.md +4 -0
  235. package/presets/mavis/next/rules/next-app-core.md +1 -0
  236. package/presets/mavis/next/team/README.md +7 -3
  237. package/presets/mavis/nuxt/adapter-map.json +1 -1
  238. package/presets/mavis/nuxt/agents/task-analyst.md +1 -1
  239. package/presets/mavis/nuxt/hooks/chain-team-phases.sh +105 -17
  240. package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +4 -2
  241. package/presets/mavis/nuxt/rules/feature-delivery-workflow.md +4 -0
  242. package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
  243. package/presets/mavis/php-hexagonal/agents/task-analyst.md +1 -1
  244. package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +105 -17
  245. package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +4 -2
  246. package/presets/mavis/php-hexagonal/rules/feature-delivery-workflow.md +4 -0
  247. package/presets/mavis/php-laravel/adapter-map.json +1 -1
  248. package/presets/mavis/php-laravel/agents/task-analyst.md +1 -1
  249. package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +106 -18
  250. package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +4 -2
  251. package/presets/mavis/php-laravel/rules/feature-delivery-workflow.md +4 -0
  252. package/presets/mavis/svelte/adapter-map.json +1 -1
  253. package/presets/mavis/svelte/agents/task-analyst.md +1 -1
  254. package/presets/mavis/svelte/hooks/chain-team-phases.sh +105 -17
  255. package/presets/mavis/svelte/rules/agent-team-orchestrator.md +4 -2
  256. package/presets/mavis/svelte/rules/feature-delivery-workflow.md +4 -0
  257. package/presets/opencode/android-kotlin/adapter-map.json +1 -1
  258. package/presets/opencode/android-kotlin/agents/task-analyst.md +1 -1
  259. package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +105 -17
  260. package/presets/opencode/android-kotlin/opencode.json +5 -1
  261. package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +4 -2
  262. package/presets/opencode/go/adapter-map.json +1 -1
  263. package/presets/opencode/go/agents/task-analyst.md +1 -1
  264. package/presets/opencode/go/hooks/chain-team-phases.sh +105 -17
  265. package/presets/opencode/go/opencode.json +5 -1
  266. package/presets/opencode/go/rules/agent-team-orchestrator.md +4 -2
  267. package/presets/opencode/go/rules/feature-delivery-workflow.md +4 -0
  268. package/presets/opencode/ios-swift/adapter-map.json +1 -1
  269. package/presets/opencode/ios-swift/agents/task-analyst.md +1 -1
  270. package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +105 -17
  271. package/presets/opencode/ios-swift/opencode.json +5 -1
  272. package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +4 -2
  273. package/presets/opencode/ios-swift/rules/feature-delivery-workflow.md +4 -0
  274. package/presets/opencode/java/adapter-map.json +1 -1
  275. package/presets/opencode/java/agents/task-analyst.md +1 -1
  276. package/presets/opencode/java/hooks/chain-team-phases.sh +105 -17
  277. package/presets/opencode/java/opencode.json +5 -1
  278. package/presets/opencode/java/rules/agent-team-orchestrator.md +4 -2
  279. package/presets/opencode/java/rules/feature-delivery-workflow.md +4 -0
  280. package/presets/opencode/mcp-ts/adapter-map.json +1 -1
  281. package/presets/opencode/mcp-ts/agents/task-analyst.md +1 -1
  282. package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +105 -17
  283. package/presets/opencode/mcp-ts/opencode.json +5 -1
  284. package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +4 -2
  285. package/presets/opencode/mcp-ts/rules/feature-delivery-workflow.md +4 -0
  286. package/presets/opencode/mol/adapter-map.json +1 -1
  287. package/presets/opencode/mol/agents/task-analyst.md +1 -1
  288. package/presets/opencode/mol/hooks/chain-team-phases.sh +105 -17
  289. package/presets/opencode/mol/opencode.json +4 -0
  290. package/presets/opencode/mol/rules/agent-team-orchestrator.md +4 -2
  291. package/presets/opencode/mol/rules/feature-delivery-workflow.md +4 -0
  292. package/presets/opencode/next/MCP.md +2 -0
  293. package/presets/opencode/next/REPO_AGENTS.md +2 -0
  294. package/presets/opencode/next/adapter-map.json +33 -1
  295. package/presets/opencode/next/agents/build-verifier.md +1 -1
  296. package/presets/opencode/next/agents/ci-investigator.md +3 -0
  297. package/presets/opencode/next/agents/debugger.md +3 -0
  298. package/presets/opencode/next/agents/feature-developer.md +3 -0
  299. package/presets/opencode/next/agents/mockup-reviewer.md +2 -0
  300. package/presets/opencode/next/agents/performance-auditor.md +2 -0
  301. package/presets/opencode/next/agents/playwright-test-generator.md +3 -0
  302. package/presets/opencode/next/agents/playwright-test-healer.md +3 -0
  303. package/presets/opencode/next/agents/playwright-test-planner.md +3 -0
  304. package/presets/opencode/next/agents/qa-tester.md +3 -0
  305. package/presets/opencode/next/agents/task-analyst.md +1 -1
  306. package/presets/opencode/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
  307. package/presets/opencode/next/commands/README.md +7 -1
  308. package/presets/opencode/next/commands/task.md +1 -1
  309. package/presets/opencode/next/commands/ui-start.md +81 -0
  310. package/presets/opencode/next/commands/ui-stop.md +74 -0
  311. package/presets/opencode/next/hooks/chain-team-phases.sh +105 -17
  312. package/presets/opencode/next/opencode.json +4 -0
  313. package/presets/opencode/next/rules/agent-team-orchestrator.md +4 -2
  314. package/presets/opencode/next/rules/dev-process-lifecycle.md +38 -0
  315. package/presets/opencode/next/rules/feature-delivery-workflow.md +4 -0
  316. package/presets/opencode/next/rules/next-app-core.md +1 -0
  317. package/presets/opencode/next/team/README.md +7 -3
  318. package/presets/opencode/nuxt/adapter-map.json +1 -1
  319. package/presets/opencode/nuxt/agents/task-analyst.md +1 -1
  320. package/presets/opencode/nuxt/hooks/chain-team-phases.sh +105 -17
  321. package/presets/opencode/nuxt/opencode.json +4 -0
  322. package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +4 -2
  323. package/presets/opencode/nuxt/rules/feature-delivery-workflow.md +4 -0
  324. package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
  325. package/presets/opencode/php-hexagonal/agents/task-analyst.md +1 -1
  326. package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +105 -17
  327. package/presets/opencode/php-hexagonal/opencode.json +5 -1
  328. package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +4 -2
  329. package/presets/opencode/php-hexagonal/rules/feature-delivery-workflow.md +4 -0
  330. package/presets/opencode/php-laravel/adapter-map.json +1 -1
  331. package/presets/opencode/php-laravel/agents/task-analyst.md +1 -1
  332. package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +106 -18
  333. package/presets/opencode/php-laravel/opencode.json +5 -1
  334. package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +4 -2
  335. package/presets/opencode/php-laravel/rules/feature-delivery-workflow.md +4 -0
  336. package/presets/opencode/plugins/task-status/README.md +23 -11
  337. package/presets/opencode/plugins/task-status/details-data.ts +64 -46
  338. package/presets/opencode/plugins/task-status/details.tsx +12 -2
  339. package/presets/opencode/plugins/task-status/format.ts +16 -0
  340. package/presets/opencode/plugins/task-status/prompt-text.ts +37 -0
  341. package/presets/opencode/plugins/task-status/task-view.ts +241 -177
  342. package/presets/opencode/plugins/task-status/tui.tsx +57 -15
  343. package/presets/opencode/plugins/task-status/types.ts +74 -0
  344. package/presets/opencode/svelte/adapter-map.json +1 -1
  345. package/presets/opencode/svelte/agents/task-analyst.md +1 -1
  346. package/presets/opencode/svelte/hooks/chain-team-phases.sh +105 -17
  347. package/presets/opencode/svelte/opencode.json +4 -0
  348. package/presets/opencode/svelte/rules/agent-team-orchestrator.md +4 -2
  349. package/presets/opencode/svelte/rules/feature-delivery-workflow.md +4 -0
  350. package/presets/zcode/android-kotlin/adapter-map.json +1 -1
  351. package/presets/zcode/android-kotlin/agents/task-analyst.md +1 -1
  352. package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +105 -17
  353. package/presets/zcode/android-kotlin/rules/agent-team-orchestrator.md +4 -2
  354. package/presets/zcode/go/adapter-map.json +1 -1
  355. package/presets/zcode/go/agents/task-analyst.md +1 -1
  356. package/presets/zcode/go/hooks/chain-team-phases.sh +105 -17
  357. package/presets/zcode/go/rules/agent-team-orchestrator.md +4 -2
  358. package/presets/zcode/go/rules/feature-delivery-workflow.md +4 -0
  359. package/presets/zcode/ios-swift/adapter-map.json +1 -1
  360. package/presets/zcode/ios-swift/agents/task-analyst.md +1 -1
  361. package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +105 -17
  362. package/presets/zcode/ios-swift/rules/agent-team-orchestrator.md +4 -2
  363. package/presets/zcode/ios-swift/rules/feature-delivery-workflow.md +4 -0
  364. package/presets/zcode/java/adapter-map.json +1 -1
  365. package/presets/zcode/java/agents/task-analyst.md +1 -1
  366. package/presets/zcode/java/hooks/chain-team-phases.sh +105 -17
  367. package/presets/zcode/java/rules/agent-team-orchestrator.md +4 -2
  368. package/presets/zcode/java/rules/feature-delivery-workflow.md +4 -0
  369. package/presets/zcode/mcp-ts/adapter-map.json +1 -1
  370. package/presets/zcode/mcp-ts/agents/task-analyst.md +1 -1
  371. package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +105 -17
  372. package/presets/zcode/mcp-ts/rules/agent-team-orchestrator.md +4 -2
  373. package/presets/zcode/mcp-ts/rules/feature-delivery-workflow.md +4 -0
  374. package/presets/zcode/mol/adapter-map.json +1 -1
  375. package/presets/zcode/mol/agents/task-analyst.md +1 -1
  376. package/presets/zcode/mol/hooks/chain-team-phases.sh +105 -17
  377. package/presets/zcode/mol/rules/agent-team-orchestrator.md +4 -2
  378. package/presets/zcode/mol/rules/feature-delivery-workflow.md +4 -0
  379. package/presets/zcode/next/MCP.md +2 -0
  380. package/presets/zcode/next/REPO_AGENTS.md +2 -0
  381. package/presets/zcode/next/adapter-map.json +33 -1
  382. package/presets/zcode/next/agents/build-verifier.md +1 -1
  383. package/presets/zcode/next/agents/ci-investigator.md +3 -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 +2 -0
  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 +1 -1
  393. package/presets/zcode/next/ai-local/rules-library/storybook-dev-compare-figma.md +1 -1
  394. package/presets/zcode/next/commands/README.md +7 -1
  395. package/presets/zcode/next/commands/task.md +1 -1
  396. package/presets/zcode/next/commands/ui-start.md +81 -0
  397. package/presets/zcode/next/commands/ui-stop.md +74 -0
  398. package/presets/zcode/next/hooks/chain-team-phases.sh +105 -17
  399. package/presets/zcode/next/rules/agent-team-orchestrator.md +4 -2
  400. package/presets/zcode/next/rules/dev-process-lifecycle.md +38 -0
  401. package/presets/zcode/next/rules/feature-delivery-workflow.md +4 -0
  402. package/presets/zcode/next/rules/next-app-core.md +1 -0
  403. package/presets/zcode/next/team/README.md +7 -3
  404. package/presets/zcode/nuxt/adapter-map.json +1 -1
  405. package/presets/zcode/nuxt/agents/task-analyst.md +1 -1
  406. package/presets/zcode/nuxt/hooks/chain-team-phases.sh +105 -17
  407. package/presets/zcode/nuxt/rules/agent-team-orchestrator.md +4 -2
  408. package/presets/zcode/nuxt/rules/feature-delivery-workflow.md +4 -0
  409. package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
  410. package/presets/zcode/php-hexagonal/agents/task-analyst.md +1 -1
  411. package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +105 -17
  412. package/presets/zcode/php-hexagonal/rules/agent-team-orchestrator.md +4 -2
  413. package/presets/zcode/php-hexagonal/rules/feature-delivery-workflow.md +4 -0
  414. package/presets/zcode/php-laravel/adapter-map.json +1 -1
  415. package/presets/zcode/php-laravel/agents/task-analyst.md +1 -1
  416. package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +106 -18
  417. package/presets/zcode/php-laravel/rules/agent-team-orchestrator.md +4 -2
  418. package/presets/zcode/php-laravel/rules/feature-delivery-workflow.md +4 -0
  419. package/presets/zcode/svelte/adapter-map.json +1 -1
  420. package/presets/zcode/svelte/agents/task-analyst.md +1 -1
  421. package/presets/zcode/svelte/hooks/chain-team-phases.sh +105 -17
  422. package/presets/zcode/svelte/rules/agent-team-orchestrator.md +4 -2
  423. package/presets/zcode/svelte/rules/feature-delivery-workflow.md +4 -0
  424. package/presets/_shared/ai-local/scripts/upstream-research.mjs +0 -2964
  425. package/presets/claude/next/commands/upstream-research.md +0 -23
  426. package/presets/claude/next/team/upstream/config.json +0 -31
  427. package/presets/claude/next/team/upstream/digest-template.md +0 -55
  428. package/presets/claude/next/team/upstream/jobs.json +0 -34
  429. package/presets/claude/next/team/upstream/opencode.json +0 -34
  430. package/presets/claude/next/team/upstream/projects.json +0 -4
  431. package/presets/claude/next/team/upstream/prompt.md +0 -77
  432. package/presets/claude/next/team/upstream/signatures.json +0 -9
  433. package/presets/cursor/next/commands/upstream-research.md +0 -23
  434. package/presets/cursor/next/team/upstream/config.json +0 -31
  435. package/presets/cursor/next/team/upstream/digest-template.md +0 -55
  436. package/presets/cursor/next/team/upstream/jobs.json +0 -34
  437. package/presets/cursor/next/team/upstream/opencode.json +0 -34
  438. package/presets/cursor/next/team/upstream/projects.json +0 -4
  439. package/presets/cursor/next/team/upstream/prompt.md +0 -77
  440. package/presets/cursor/next/team/upstream/signatures.json +0 -9
  441. package/presets/mavis/next/commands/upstream-research.md +0 -18
  442. package/presets/mavis/next/team/upstream/config.json +0 -31
  443. package/presets/mavis/next/team/upstream/digest-template.md +0 -55
  444. package/presets/mavis/next/team/upstream/jobs.json +0 -34
  445. package/presets/mavis/next/team/upstream/opencode.json +0 -34
  446. package/presets/mavis/next/team/upstream/projects.json +0 -4
  447. package/presets/mavis/next/team/upstream/prompt.md +0 -77
  448. package/presets/mavis/next/team/upstream/signatures.json +0 -9
  449. package/presets/opencode/next/commands/upstream-research.md +0 -26
  450. package/presets/opencode/next/team/upstream/config.json +0 -31
  451. package/presets/opencode/next/team/upstream/digest-template.md +0 -55
  452. package/presets/opencode/next/team/upstream/jobs.json +0 -34
  453. package/presets/opencode/next/team/upstream/opencode.json +0 -34
  454. package/presets/opencode/next/team/upstream/projects.json +0 -4
  455. package/presets/opencode/next/team/upstream/prompt.md +0 -77
  456. package/presets/opencode/next/team/upstream/signatures.json +0 -9
  457. package/presets/zcode/next/commands/upstream-research.md +0 -26
  458. package/presets/zcode/next/team/upstream/config.json +0 -31
  459. package/presets/zcode/next/team/upstream/digest-template.md +0 -55
  460. package/presets/zcode/next/team/upstream/jobs.json +0 -34
  461. package/presets/zcode/next/team/upstream/opencode.json +0 -34
  462. package/presets/zcode/next/team/upstream/projects.json +0 -4
  463. package/presets/zcode/next/team/upstream/prompt.md +0 -77
  464. package/presets/zcode/next/team/upstream/signatures.json +0 -9
@@ -0,0 +1,576 @@
1
+ /**
2
+ * Process leases: one JSON file per named resource, one holder at a time.
3
+ *
4
+ * State lives in `<aiLocalRoot>/.processes/`: `leases/<name>.json`, an
5
+ * append-only `events.jsonl`, and a `.reclaim/` guard directory per lease that
6
+ * was judged stale. There is no daemon — the files are the truth and a crash
7
+ * leaves a lease the next acquire can reclaim.
8
+ *
9
+ * A lease is taken with `openSync(path, 'wx')`, so exactly one contender wins
10
+ * the create and no separate write window exists. A holder is *live* when its
11
+ * pid is alive **and** the recorded start time matches the live one; a
12
+ * different start time means the pid was reused, so the lease is stale. A
13
+ * stale lease is reclaimed inside `.reclaim/<name>`: `mkdir` is the
14
+ * serialisation point, the lease is re-read after the guard is taken, and only
15
+ * a still-stale file is unlinked. A heartbeat never makes a live holder stale.
16
+ *
17
+ * Every acquire result carries `fatal` and `code` before `skipped`: `skipped`
18
+ * is `true` only when the caller opted in with `allowUnlocked`.
19
+ */
20
+ import crypto from 'node:crypto';
21
+ import fs from 'node:fs';
22
+ import path from 'node:path';
23
+ import { spawnSync } from 'node:child_process';
24
+
25
+ export const LEASE_SCHEMA_VERSION = 1;
26
+
27
+ export const PROCESSES_DIR_NAME = '.processes';
28
+
29
+ /** Poll interval for `waitMs`; the engine lock's busy-wait precedent. */
30
+ export const DEFAULT_POLL_MS = 500;
31
+
32
+ const LEASES_DIR_NAME = 'leases';
33
+ const RECLAIM_DIR_NAME = '.reclaim';
34
+ const EVENT_FILE_NAME = 'events.jsonl';
35
+ const EVENT_MAX_BYTES = 512;
36
+ /** A guard left by a killed reclaimer only delays the next one. */
37
+ const GUARD_STALE_MS = 30000;
38
+ /**
39
+ * An unparseable lease is either a crash-truncated file or a writer between
40
+ * `open` and `write`; it is reclaimable only after this window, so a create in
41
+ * flight is never stolen.
42
+ */
43
+ const LEASE_WARM_MS = 5000;
44
+ const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
45
+ const KINDS = new Set(['exclusive', 'port', 'advisory']);
46
+
47
+ const newToken = () => crypto.randomBytes(12).toString('hex');
48
+
49
+ const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
50
+
51
+ const normalize = (value) => String(value).replace(/\s+/g, ' ').trim();
52
+
53
+ /** Synchronous sleep: `acquireLease` has a synchronous contract. */
54
+ function sleepSync(ms) {
55
+ if (ms > 0) {
56
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
57
+ }
58
+ }
59
+
60
+ /* ----------------------------------- paths -------------------------------- */
61
+
62
+ export const processesRoot = (aiLocalRoot) => path.join(aiLocalRoot, PROCESSES_DIR_NAME);
63
+
64
+ export const leasesDir = (aiLocalRoot) => path.join(processesRoot(aiLocalRoot), LEASES_DIR_NAME);
65
+
66
+ export const reclaimDir = (aiLocalRoot) => path.join(processesRoot(aiLocalRoot), RECLAIM_DIR_NAME);
67
+
68
+ export const leasePath = (aiLocalRoot, name) => path.join(leasesDir(aiLocalRoot), `${name}.json`);
69
+
70
+ export const eventsFile = (aiLocalRoot) => path.join(processesRoot(aiLocalRoot), EVENT_FILE_NAME);
71
+
72
+ export const isValidLeaseName = (name) => typeof name === 'string' && NAME_PATTERN.test(name);
73
+
74
+ /* --------------------------------- process -------------------------------- */
75
+
76
+ /** True when the OS still knows the pid; EPERM means alive but not ours. */
77
+ export function isPidAlive(pid) {
78
+ if (!Number.isInteger(pid) || pid <= 0) {
79
+ return false;
80
+ }
81
+ try {
82
+ process.kill(pid, 0);
83
+ return true;
84
+ } catch (error) {
85
+ return error instanceof Error && error.code === 'EPERM';
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Linux fallback for a host without `ps`: field 22 of `/proc/<pid>/stat` is the
91
+ * boot-relative start time, which is stable for the life of the process. The
92
+ * `proc:` prefix keeps it distinguishable from a `ps -o lstart=` value, so the
93
+ * two formats are never compared against each other.
94
+ */
95
+ function procStartTime(pid) {
96
+ try {
97
+ const stat = fs.readFileSync(`/proc/${pid}/stat`, 'utf8');
98
+ const close = stat.lastIndexOf(')');
99
+ if (close < 0) return null;
100
+ const fields = stat.slice(close + 2).trim().split(/\s+/);
101
+ const startTime = fields[19];
102
+ return startTime ? `proc:${startTime}` : null;
103
+ } catch {
104
+ return null;
105
+ }
106
+ }
107
+
108
+ /**
109
+ * The process start time, or `null` when the platform cannot report it
110
+ * (Windows has no `ps`). Callers degrade to pid-only liveness and say so; they
111
+ * never treat "unknown" as "matches".
112
+ */
113
+ export function processStartTime(pid) {
114
+ if (!Number.isInteger(pid) || pid <= 0) {
115
+ return null;
116
+ }
117
+ const result = spawnSync('ps', ['-o', 'lstart=', '-p', String(pid)], {
118
+ encoding: 'utf8',
119
+ // `ps` localises `lstart`; a lease written under one locale must compare
120
+ // equal under another, or a live holder reads as stale.
121
+ env: { ...process.env, LC_ALL: 'C', LC_TIME: 'C' },
122
+ });
123
+ if (result.error) {
124
+ // `ps` itself is missing, not the pid: only then is the /proc shape safe to
125
+ // mix into a comparison.
126
+ return procStartTime(pid);
127
+ }
128
+ if (result.status !== 0) {
129
+ return null;
130
+ }
131
+ const value = String(result.stdout ?? '').trim();
132
+ return value ? value : null;
133
+ }
134
+
135
+ /* ---------------------------------- lease --------------------------------- */
136
+
137
+ export function readLease(aiLocalRoot, name) {
138
+ try {
139
+ const parsed = JSON.parse(fs.readFileSync(leasePath(aiLocalRoot, name), 'utf8'));
140
+ return isRecord(parsed) ? parsed : null;
141
+ } catch {
142
+ return null;
143
+ }
144
+ }
145
+
146
+ export function listLeases(aiLocalRoot) {
147
+ const dir = leasesDir(aiLocalRoot);
148
+ let entries;
149
+ try {
150
+ entries = fs.readdirSync(dir);
151
+ } catch {
152
+ return [];
153
+ }
154
+ return entries
155
+ .filter((file) => file.endsWith('.json'))
156
+ .sort()
157
+ .map((file) => {
158
+ const name = file.slice(0, -'.json'.length);
159
+ return { name, file: path.join(dir, file), record: readLease(aiLocalRoot, name) };
160
+ });
161
+ }
162
+
163
+ /**
164
+ * Judge one lease. `startTime` is injectable so a caller (or a fixture) can
165
+ * prove its own view without a second `ps` call.
166
+ *
167
+ * @returns {{ state: 'live'|'stale'|'invalid', why: string, alive?: boolean, recordedPidStart?: string|null, livePidStart?: string|null }}
168
+ */
169
+ export function leaseState(record, { startTime = processStartTime } = {}) {
170
+ const owner = isRecord(record) ? record.owner : null;
171
+ if (!isRecord(owner) || !Number.isInteger(owner.pid) || owner.pid <= 0) {
172
+ return { state: 'invalid', why: 'no-owner' };
173
+ }
174
+ if (!isPidAlive(owner.pid)) {
175
+ return { state: 'stale', why: 'pid-dead', alive: false, recordedPidStart: owner.pidStart ?? null };
176
+ }
177
+ if (owner.pidStart) {
178
+ const live = startTime(owner.pid);
179
+ if (live === null) {
180
+ return {
181
+ state: 'live',
182
+ why: 'start-time-unavailable',
183
+ alive: true,
184
+ recordedPidStart: owner.pidStart,
185
+ livePidStart: null,
186
+ };
187
+ }
188
+ if (normalize(live) !== normalize(owner.pidStart)) {
189
+ return {
190
+ state: 'stale',
191
+ why: 'start-time-mismatch',
192
+ alive: true,
193
+ recordedPidStart: owner.pidStart,
194
+ livePidStart: live,
195
+ };
196
+ }
197
+ }
198
+ return { state: 'live', why: 'pid-alive', alive: true, recordedPidStart: owner.pidStart ?? null };
199
+ }
200
+
201
+ /* ---------------------------------- state --------------------------------- */
202
+
203
+ function ensureStateDirs(aiLocalRoot) {
204
+ for (const dir of [processesRoot(aiLocalRoot), leasesDir(aiLocalRoot), reclaimDir(aiLocalRoot)]) {
205
+ fs.mkdirSync(dir, { recursive: true });
206
+ }
207
+ }
208
+
209
+ function fileAgeMs(file) {
210
+ try {
211
+ return Date.now() - fs.statSync(file).mtimeMs;
212
+ } catch {
213
+ return Number.POSITIVE_INFINITY;
214
+ }
215
+ }
216
+
217
+ /* --------------------------------- events --------------------------------- */
218
+
219
+ /**
220
+ * Append one event line. The log is observability only: a write failure is
221
+ * reported as `false` and never fails the caller's operation.
222
+ */
223
+ export function appendEvent({ aiLocalRoot, type, name, pid, reason, ...rest }) {
224
+ if (typeof type !== 'string' || type === '') return false;
225
+ const base = { at: new Date().toISOString(), type, name: name ?? null };
226
+ if (Number.isInteger(pid) && pid > 0) base.pid = pid;
227
+ const event = { ...base, ...rest };
228
+ if (reason !== undefined && reason !== null) event.reason = String(reason);
229
+ let line = JSON.stringify(event);
230
+ if (Buffer.byteLength(line, 'utf8') > EVENT_MAX_BYTES) {
231
+ delete event.reason;
232
+ line = JSON.stringify(event);
233
+ }
234
+ if (Buffer.byteLength(line, 'utf8') > EVENT_MAX_BYTES) {
235
+ line = JSON.stringify(base);
236
+ }
237
+ try {
238
+ fs.mkdirSync(processesRoot(aiLocalRoot), { recursive: true });
239
+ fs.appendFileSync(eventsFile(aiLocalRoot), `${line}\n`, 'utf8');
240
+ return true;
241
+ } catch {
242
+ return false;
243
+ }
244
+ }
245
+
246
+ /* -------------------------------- reclaim --------------------------------- */
247
+
248
+ /** Serialise reclaim through a guard directory; the winner re-reads the lease. */
249
+ function takeGuard(aiLocalRoot, name) {
250
+ const guard = path.join(reclaimDir(aiLocalRoot), name);
251
+ try {
252
+ fs.mkdirSync(guard);
253
+ return { guard, taken: true };
254
+ } catch (error) {
255
+ if (error instanceof Error && error.code === 'EEXIST') {
256
+ // A guard older than the stale window belonged to a killed reclaimer.
257
+ if (fileAgeMs(guard) > GUARD_STALE_MS) {
258
+ try {
259
+ fs.rmSync(guard, { recursive: true, force: true });
260
+ } catch {
261
+ /* the holder still owns it */
262
+ }
263
+ }
264
+ return { guard: null, taken: false };
265
+ }
266
+ return { guard: null, taken: false, error };
267
+ }
268
+ }
269
+
270
+ function releaseGuard(guard) {
271
+ if (!guard) return;
272
+ try {
273
+ fs.rmSync(guard, { recursive: true, force: true });
274
+ } catch {
275
+ /* best effort: a leftover guard ages out */
276
+ }
277
+ }
278
+
279
+ /* -------------------------------- acquire --------------------------------- */
280
+
281
+ const describeOwner = (record) => {
282
+ const owner = isRecord(record) ? record.owner : null;
283
+ if (!isRecord(owner)) return 'an unreadable lease';
284
+ const label = owner.label ? ` (${owner.label})` : '';
285
+ return `pid ${owner.pid}${label} since ${owner.startedAt ?? '?'}`;
286
+ };
287
+
288
+ /**
289
+ * Take one named lease.
290
+ *
291
+ * @returns {{ acquired: boolean, fatal: boolean, code: 'acquired'|'held'|'timeout'|'unwritable', skipped: boolean, name: string, path: string, owner?: object, token?: string, reason?: string }}
292
+ */
293
+ export function acquireLease({
294
+ aiLocalRoot,
295
+ name,
296
+ kind = 'exclusive',
297
+ port = null,
298
+ label = null,
299
+ task = null,
300
+ agent = null,
301
+ cwd = process.cwd(),
302
+ command = null,
303
+ pid = process.pid,
304
+ pidStart,
305
+ token = newToken(),
306
+ waitMs = 0,
307
+ ttlMs = null,
308
+ allowUnlocked = false,
309
+ }) {
310
+ const target = leasePath(aiLocalRoot, name);
311
+ const budget = Number.isFinite(waitMs) && waitMs > 0 ? Math.floor(waitMs) : 0;
312
+ const deadline = Date.now() + budget;
313
+ const recordedStart = pidStart === undefined ? processStartTime(pid) : pidStart;
314
+
315
+ try {
316
+ ensureStateDirs(aiLocalRoot);
317
+ } catch (error) {
318
+ const code = error instanceof Error ? error.code ?? error.message : String(error);
319
+ return {
320
+ acquired: false,
321
+ fatal: true,
322
+ code: 'unwritable',
323
+ skipped: allowUnlocked === true,
324
+ name,
325
+ path: target,
326
+ reason: `the process state directory is not writable (${code})`,
327
+ };
328
+ }
329
+
330
+ const record = () => {
331
+ const startedAt = new Date().toISOString();
332
+ return {
333
+ schemaVersion: LEASE_SCHEMA_VERSION,
334
+ name,
335
+ kind,
336
+ owner: {
337
+ pid,
338
+ pidStart: recordedStart,
339
+ token,
340
+ label,
341
+ task,
342
+ agent,
343
+ cwd,
344
+ command,
345
+ startedAt,
346
+ heartbeatAt: startedAt,
347
+ },
348
+ port: Number.isInteger(port) && port > 0 ? port : null,
349
+ children: [],
350
+ ttlMs: Number.isFinite(ttlMs) && ttlMs > 0 ? Math.floor(ttlMs) : null,
351
+ };
352
+ };
353
+
354
+ const take = () => {
355
+ const value = record();
356
+ const fd = fs.openSync(target, 'wx');
357
+ try {
358
+ fs.writeFileSync(fd, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
359
+ } finally {
360
+ fs.closeSync(fd);
361
+ }
362
+ return value;
363
+ };
364
+
365
+ const held = (owner, why) => ({
366
+ acquired: false,
367
+ fatal: false,
368
+ code: 'held',
369
+ skipped: false,
370
+ name,
371
+ path: target,
372
+ owner: isRecord(owner) ? owner : undefined,
373
+ reason: why,
374
+ });
375
+
376
+ const attempt = () => {
377
+ try {
378
+ const value = take();
379
+ appendEvent({ aiLocalRoot, type: 'acquired', name, pid, kind });
380
+ return {
381
+ acquired: true,
382
+ fatal: false,
383
+ code: 'acquired',
384
+ skipped: false,
385
+ name,
386
+ path: target,
387
+ owner: value.owner,
388
+ token,
389
+ };
390
+ } catch (error) {
391
+ if (!(error instanceof Error) || error.code !== 'EEXIST') {
392
+ const code = error instanceof Error ? error.code ?? error.message : String(error);
393
+ return {
394
+ acquired: false,
395
+ fatal: true,
396
+ code: 'unwritable',
397
+ skipped: false,
398
+ name,
399
+ path: target,
400
+ reason: `the lease file is not writable (${code})`,
401
+ };
402
+ }
403
+ }
404
+
405
+ const existing = readLease(aiLocalRoot, name);
406
+ if (existing === null && !fs.existsSync(target)) {
407
+ return held(undefined, 'the lease disappeared while it was being judged');
408
+ }
409
+ const state = existing === null ? { state: 'invalid', why: 'unparseable' } : leaseState(existing);
410
+ if (state.state === 'invalid' && fileAgeMs(target) <= LEASE_WARM_MS) {
411
+ return held(existing, `${describeOwner(existing)} is being written`);
412
+ }
413
+ if (state.state === 'live') {
414
+ return held(existing, `held by ${describeOwner(existing)}`);
415
+ }
416
+
417
+ const guard = takeGuard(aiLocalRoot, name);
418
+ if (!guard.taken) {
419
+ const current = readLease(aiLocalRoot, name) ?? existing;
420
+ const currentState = current === null ? { state: 'invalid' } : leaseState(current);
421
+ if (currentState.state === 'live') {
422
+ return held(current, `held by ${describeOwner(current)}`);
423
+ }
424
+ return held(current, `a stale lease is being reclaimed by another process (${currentState.why})`);
425
+ }
426
+ let reclaimed = false;
427
+ try {
428
+ const fresh = readLease(aiLocalRoot, name);
429
+ if (fresh === null && !fs.existsSync(target)) {
430
+ reclaimed = true;
431
+ } else {
432
+ const freshState = fresh === null ? { state: 'invalid', why: 'unparseable' } : leaseState(fresh);
433
+ if (freshState.state === 'live') {
434
+ return held(fresh, `held by ${describeOwner(fresh)}`);
435
+ }
436
+ appendEvent({
437
+ aiLocalRoot,
438
+ type: 'reclaimed',
439
+ name,
440
+ pid: isRecord(fresh?.owner) ? fresh.owner.pid : undefined,
441
+ why: freshState.why,
442
+ });
443
+ try {
444
+ fs.rmSync(target, { force: true });
445
+ } catch {
446
+ return {
447
+ acquired: false,
448
+ fatal: true,
449
+ code: 'unwritable',
450
+ skipped: false,
451
+ name,
452
+ path: target,
453
+ reason: 'a stale lease could not be removed',
454
+ };
455
+ }
456
+ reclaimed = true;
457
+ }
458
+ } finally {
459
+ releaseGuard(guard.guard);
460
+ }
461
+ if (!reclaimed) {
462
+ return held(readLease(aiLocalRoot, name), 'the stale lease could not be judged');
463
+ }
464
+
465
+ try {
466
+ const value = take();
467
+ appendEvent({ aiLocalRoot, type: 'acquired', name, pid, kind });
468
+ return {
469
+ acquired: true,
470
+ fatal: false,
471
+ code: 'acquired',
472
+ skipped: false,
473
+ name,
474
+ path: target,
475
+ owner: value.owner,
476
+ token,
477
+ reason: `reclaimed a stale lease (${state.why})`,
478
+ };
479
+ } catch (error) {
480
+ const fresh = readLease(aiLocalRoot, name);
481
+ if (error instanceof Error && error.code === 'EEXIST') {
482
+ return held(fresh, `held by ${describeOwner(fresh)}`);
483
+ }
484
+ const code = error instanceof Error ? error.code ?? error.message : String(error);
485
+ return {
486
+ acquired: false,
487
+ fatal: true,
488
+ code: 'unwritable',
489
+ skipped: false,
490
+ name,
491
+ path: target,
492
+ reason: `the reclaimed lease file is not writable (${code})`,
493
+ };
494
+ }
495
+ };
496
+
497
+ let result;
498
+ for (;;) {
499
+ result = attempt();
500
+ if (result.acquired || result.fatal || budget === 0) {
501
+ break;
502
+ }
503
+ const remaining = deadline - Date.now();
504
+ if (remaining <= 0) {
505
+ break;
506
+ }
507
+ sleepSync(Math.min(DEFAULT_POLL_MS, remaining));
508
+ }
509
+
510
+ if (result.fatal) {
511
+ return { ...result, skipped: allowUnlocked === true };
512
+ }
513
+ if (result.acquired || budget === 0) {
514
+ return result;
515
+ }
516
+ return {
517
+ acquired: false,
518
+ fatal: false,
519
+ code: 'timeout',
520
+ skipped: false,
521
+ name,
522
+ path: target,
523
+ owner: result.owner,
524
+ reason: `timed out after ${budget}ms; ${result.owner ? describeOwner(result.owner) : 'owner unknown'}`,
525
+ };
526
+ }
527
+
528
+ /* -------------------------------- release --------------------------------- */
529
+
530
+ /**
531
+ * Drop one lease only when the caller owns it: the token must match, or — for
532
+ * a token-less legacy record — the pid. A foreign token is refused and the
533
+ * lease is left standing.
534
+ */
535
+ export function releaseLease({ aiLocalRoot, name, token, pid = process.pid }) {
536
+ const target = leasePath(aiLocalRoot, name);
537
+ const record = readLease(aiLocalRoot, name);
538
+ if (record === null && !fs.existsSync(target)) {
539
+ return { released: false, reason: `no lease named "${name}"` };
540
+ }
541
+ const owner = isRecord(record) ? record.owner : null;
542
+ if (!isRecord(owner)) {
543
+ // A crash-truncated record and a writer between `open` and `write` look the
544
+ // same, and an unreadable record names no owner to compare a token with:
545
+ // inside the warm window the lease is not ours to judge; outside it no
546
+ // writer is left, so the dead record can be cleared.
547
+ if (fileAgeMs(target) <= LEASE_WARM_MS) {
548
+ return { released: false, reason: 'the lease is being written' };
549
+ }
550
+ try {
551
+ fs.rmSync(target, { force: true });
552
+ return { released: true };
553
+ } catch {
554
+ return { released: false, reason: 'the unreadable lease could not be removed' };
555
+ }
556
+ }
557
+ if (owner.token) {
558
+ if (owner.token !== token) {
559
+ appendEvent({ aiLocalRoot, type: 'refused', name, pid, reason: 'release token does not match' });
560
+ return { released: false, reason: `held by ${describeOwner(record)}` };
561
+ }
562
+ } else if (owner.pid !== pid) {
563
+ appendEvent({ aiLocalRoot, type: 'refused', name, pid, reason: 'release pid does not match' });
564
+ return { released: false, reason: `held by ${describeOwner(record)}` };
565
+ }
566
+ try {
567
+ fs.rmSync(target, { force: true });
568
+ } catch (error) {
569
+ return {
570
+ released: false,
571
+ reason: `the lease file could not be removed (${error instanceof Error ? error.code ?? error.message : String(error)})`,
572
+ };
573
+ }
574
+ appendEvent({ aiLocalRoot, type: 'released', name, pid: owner.pid });
575
+ return { released: true };
576
+ }