@jaimevalasek/aioson 1.4.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/CHANGELOG.md +31 -1
  2. package/LICENSE +661 -21
  3. package/README.md +9 -1
  4. package/docs/design-previews/aurora-command-ui-website.html +884 -0
  5. package/docs/design-previews/aurora-command-ui.html +682 -0
  6. package/docs/design-previews/bold-editorial-ui-website.html +658 -0
  7. package/docs/design-previews/bold-editorial-ui.html +717 -0
  8. package/docs/design-previews/clean-saas-ui-website.html +1202 -0
  9. package/docs/design-previews/clean-saas-ui.html +549 -0
  10. package/docs/design-previews/cognitive-core-ui-website.html +1009 -0
  11. package/docs/design-previews/cognitive-core-ui.html +463 -0
  12. package/docs/design-previews/glassmorphism-ui-website.html +572 -0
  13. package/docs/design-previews/glassmorphism-ui.html +886 -0
  14. package/docs/design-previews/index.html +699 -0
  15. package/docs/design-previews/interface-design-website.html +1187 -0
  16. package/docs/design-previews/interface-design.html +513 -0
  17. package/docs/design-previews/neo-brutalist-ui-website.html +621 -0
  18. package/docs/design-previews/neo-brutalist-ui.html +797 -0
  19. package/docs/design-previews/premium-command-center-ui-website.html +1217 -0
  20. package/docs/design-previews/premium-command-center-ui.html +552 -0
  21. package/docs/design-previews/warm-craft-ui-website.html +684 -0
  22. package/docs/design-previews/warm-craft-ui.html +739 -0
  23. package/docs/en/cli-reference.md +20 -9
  24. package/docs/en/squad-dashboard.md +372 -0
  25. package/docs/openclaw-bridge.md +308 -0
  26. package/docs/pt/README.md +7 -0
  27. package/docs/pt/agent-sharding.md +132 -0
  28. package/docs/pt/agentes.md +131 -11
  29. package/docs/pt/busca-de-contexto.md +129 -0
  30. package/docs/pt/cache-de-contexto.md +156 -0
  31. package/docs/pt/cenarios.md +46 -2
  32. package/docs/pt/comandos-cli.md +88 -1
  33. package/docs/pt/design-hybrid-forge.md +107 -0
  34. package/docs/pt/inicio-rapido.md +72 -5
  35. package/docs/pt/inteligencia-adaptativa.md +324 -0
  36. package/docs/pt/monitor-de-contexto.md +104 -0
  37. package/docs/pt/recuperacao-de-sessao.md +125 -0
  38. package/docs/pt/sandbox.md +125 -0
  39. package/docs/pt/skills.md +98 -6
  40. package/docs/pt/squad-dashboard.md +373 -0
  41. package/docs/testing/genome-2.0-matrix.md +5 -5
  42. package/docs/testing/genome-2.0-rollout.md +9 -9
  43. package/package.json +2 -2
  44. package/src/agent-loader.js +280 -0
  45. package/src/backup-local.js +74 -0
  46. package/src/cli.js +192 -0
  47. package/src/commands/agent-loader.js +85 -0
  48. package/src/commands/backup-local-cmd.js +25 -0
  49. package/src/commands/context-cache.js +90 -0
  50. package/src/commands/context-monitor.js +92 -0
  51. package/src/commands/context-search.js +66 -0
  52. package/src/commands/design-hybrid-options.js +385 -0
  53. package/src/commands/health.js +214 -0
  54. package/src/commands/init.js +54 -13
  55. package/src/commands/install.js +52 -13
  56. package/src/commands/learning-evolve.js +355 -0
  57. package/src/commands/live.js +34 -0
  58. package/src/commands/recovery.js +43 -0
  59. package/src/commands/runtime.js +242 -0
  60. package/src/commands/sandbox.js +37 -0
  61. package/src/commands/setup-context.js +29 -4
  62. package/src/commands/setup.js +178 -0
  63. package/src/commands/skill.js +79 -32
  64. package/src/commands/squad-daemon.js +209 -0
  65. package/src/commands/squad-dashboard.js +39 -0
  66. package/src/commands/squad-deploy.js +64 -0
  67. package/src/commands/squad-doctor.js +52 -0
  68. package/src/commands/squad-mcp.js +270 -0
  69. package/src/commands/squad-processes.js +56 -0
  70. package/src/commands/squad-recovery.js +42 -0
  71. package/src/commands/squad-roi.js +291 -0
  72. package/src/commands/squad-score.js +250 -0
  73. package/src/commands/squad-status.js +37 -1
  74. package/src/commands/squad-validate.js +62 -1
  75. package/src/commands/squad-webhook.js +160 -0
  76. package/src/commands/squad-worker.js +191 -0
  77. package/src/commands/squad-worktrees.js +75 -0
  78. package/src/commands/tool-registry-cmd.js +232 -0
  79. package/src/commands/update.js +7 -0
  80. package/src/commands/web-map.js +70 -0
  81. package/src/commands/web-scrape.js +71 -0
  82. package/src/constants.js +17 -0
  83. package/src/context-cache.js +159 -0
  84. package/src/context-search.js +326 -0
  85. package/src/context-writer.js +45 -1
  86. package/src/design-variation-catalog.js +503 -0
  87. package/src/i18n/messages/en.js +159 -3
  88. package/src/i18n/messages/es.js +147 -2
  89. package/src/i18n/messages/fr.js +147 -2
  90. package/src/i18n/messages/pt-BR.js +158 -3
  91. package/src/install-animation.js +260 -0
  92. package/src/install-profile.js +143 -0
  93. package/src/install-wizard.js +474 -0
  94. package/src/installer.js +38 -10
  95. package/src/lib/webhook-server.js +328 -0
  96. package/src/mcp-connectors/registry.js +602 -0
  97. package/src/parser.js +7 -1
  98. package/src/recovery-context-session.js +154 -0
  99. package/src/runtime-store.js +355 -2
  100. package/src/sandbox.js +177 -0
  101. package/src/squad/external-session.js +180 -0
  102. package/src/squad/inter-squad.js +74 -0
  103. package/src/squad/recovery-context.js +201 -0
  104. package/src/squad/worktree-manager.js +114 -0
  105. package/src/squad-daemon.js +490 -0
  106. package/src/squad-dashboard/api.js +223 -0
  107. package/src/squad-dashboard/attachment-handler.js +93 -0
  108. package/src/squad-dashboard/context-monitor.js +157 -0
  109. package/src/squad-dashboard/execution-logs.js +115 -0
  110. package/src/squad-dashboard/hunk-review.js +209 -0
  111. package/src/squad-dashboard/metrics.js +133 -0
  112. package/src/squad-dashboard/process-monitor.js +125 -0
  113. package/src/squad-dashboard/renderer.js +858 -0
  114. package/src/squad-dashboard/server.js +232 -0
  115. package/src/squad-dashboard/styles.js +525 -0
  116. package/src/squad-dashboard/token-tracker.js +99 -0
  117. package/src/tool-executor.js +94 -0
  118. package/src/updater.js +11 -3
  119. package/src/web.js +284 -0
  120. package/src/worker-runner.js +339 -0
  121. package/template/.aioson/agents/analyst.md +62 -3
  122. package/template/.aioson/agents/architect.md +42 -0
  123. package/template/.aioson/agents/design-hybrid-forge.md +127 -0
  124. package/template/.aioson/agents/dev.md +223 -11
  125. package/template/.aioson/agents/deyvin.md +65 -0
  126. package/template/.aioson/agents/neo.md +152 -0
  127. package/template/.aioson/agents/orache.md +17 -0
  128. package/template/.aioson/agents/orchestrator.md +26 -0
  129. package/template/.aioson/agents/pm.md +58 -0
  130. package/template/.aioson/agents/product.md +88 -12
  131. package/template/.aioson/agents/qa.md +80 -0
  132. package/template/.aioson/agents/setup.md +128 -22
  133. package/template/.aioson/agents/sheldon.md +704 -0
  134. package/template/.aioson/agents/squad.md +191 -0
  135. package/template/.aioson/agents/tester.md +410 -0
  136. package/template/.aioson/agents/ux-ui.md +12 -0
  137. package/template/.aioson/config.md +21 -0
  138. package/template/.aioson/context/forensics/.gitkeep +0 -0
  139. package/template/.aioson/context/seeds/seed-example.md +27 -0
  140. package/template/.aioson/context/user-profile.md +42 -0
  141. package/template/.aioson/locales/en/agents/analyst.md +8 -0
  142. package/template/.aioson/locales/en/agents/architect.md +8 -0
  143. package/template/.aioson/locales/en/agents/dev.md +66 -7
  144. package/template/.aioson/locales/en/agents/deyvin.md +8 -0
  145. package/template/.aioson/locales/en/agents/neo.md +8 -0
  146. package/template/.aioson/locales/en/agents/orchestrator.md +26 -0
  147. package/template/.aioson/locales/en/agents/qa.md +49 -0
  148. package/template/.aioson/locales/en/agents/setup.md +35 -2
  149. package/template/.aioson/locales/en/agents/sheldon.md +340 -0
  150. package/template/.aioson/locales/en/agents/ux-ui.md +8 -0
  151. package/template/.aioson/locales/es/agents/analyst.md +8 -0
  152. package/template/.aioson/locales/es/agents/architect.md +8 -0
  153. package/template/.aioson/locales/es/agents/dev.md +66 -7
  154. package/template/.aioson/locales/es/agents/deyvin.md +8 -0
  155. package/template/.aioson/locales/es/agents/neo.md +48 -0
  156. package/template/.aioson/locales/es/agents/orchestrator.md +26 -0
  157. package/template/.aioson/locales/es/agents/qa.md +26 -0
  158. package/template/.aioson/locales/es/agents/setup.md +35 -2
  159. package/template/.aioson/locales/es/agents/sheldon.md +192 -0
  160. package/template/.aioson/locales/es/agents/squad.md +63 -0
  161. package/template/.aioson/locales/es/agents/ux-ui.md +8 -0
  162. package/template/.aioson/locales/fr/agents/analyst.md +8 -0
  163. package/template/.aioson/locales/fr/agents/architect.md +8 -0
  164. package/template/.aioson/locales/fr/agents/dev.md +66 -7
  165. package/template/.aioson/locales/fr/agents/deyvin.md +8 -0
  166. package/template/.aioson/locales/fr/agents/neo.md +48 -0
  167. package/template/.aioson/locales/fr/agents/orchestrator.md +26 -0
  168. package/template/.aioson/locales/fr/agents/qa.md +26 -0
  169. package/template/.aioson/locales/fr/agents/setup.md +35 -2
  170. package/template/.aioson/locales/fr/agents/sheldon.md +192 -0
  171. package/template/.aioson/locales/fr/agents/squad.md +63 -0
  172. package/template/.aioson/locales/fr/agents/ux-ui.md +8 -0
  173. package/template/.aioson/locales/pt-BR/agents/analyst.md +19 -0
  174. package/template/.aioson/locales/pt-BR/agents/architect.md +19 -0
  175. package/template/.aioson/locales/pt-BR/agents/dev.md +75 -12
  176. package/template/.aioson/locales/pt-BR/agents/deyvin.md +8 -0
  177. package/template/.aioson/locales/pt-BR/agents/neo.md +147 -0
  178. package/template/.aioson/locales/pt-BR/agents/orchestrator.md +26 -0
  179. package/template/.aioson/locales/pt-BR/agents/product.md +8 -3
  180. package/template/.aioson/locales/pt-BR/agents/qa.md +60 -0
  181. package/template/.aioson/locales/pt-BR/agents/setup.md +35 -2
  182. package/template/.aioson/locales/pt-BR/agents/sheldon.md +192 -0
  183. package/template/.aioson/locales/pt-BR/agents/squad.md +105 -0
  184. package/template/.aioson/locales/pt-BR/agents/ux-ui.md +8 -0
  185. package/template/.aioson/schemas/squad-blueprint.schema.json +21 -0
  186. package/template/.aioson/schemas/squad-manifest.schema.json +178 -1
  187. package/template/.aioson/skills/design/aurora-command-ui/SKILL.md +243 -0
  188. package/template/.aioson/skills/design/aurora-command-ui/references/art-direction.md +293 -0
  189. package/template/.aioson/skills/design/aurora-command-ui/references/components.md +827 -0
  190. package/template/.aioson/skills/design/aurora-command-ui/references/dashboards.md +250 -0
  191. package/template/.aioson/skills/design/aurora-command-ui/references/design-tokens.md +585 -0
  192. package/template/.aioson/skills/design/aurora-command-ui/references/motion.md +365 -0
  193. package/template/.aioson/skills/design/aurora-command-ui/references/patterns.md +482 -0
  194. package/template/.aioson/skills/design/aurora-command-ui/references/websites.md +387 -0
  195. package/template/.aioson/skills/design/bold-editorial-ui/SKILL.md +205 -0
  196. package/template/.aioson/skills/design/bold-editorial-ui/references/art-direction.md +338 -0
  197. package/template/.aioson/skills/design/bold-editorial-ui/references/components.md +977 -0
  198. package/template/.aioson/skills/design/bold-editorial-ui/references/dashboards.md +218 -0
  199. package/template/.aioson/skills/design/bold-editorial-ui/references/design-tokens.md +326 -0
  200. package/template/.aioson/skills/design/bold-editorial-ui/references/motion.md +461 -0
  201. package/template/.aioson/skills/design/bold-editorial-ui/references/patterns.md +293 -0
  202. package/template/.aioson/skills/design/bold-editorial-ui/references/websites.md +352 -0
  203. package/template/.aioson/skills/design/clean-saas-ui/SKILL.md +210 -0
  204. package/template/.aioson/skills/design/clean-saas-ui/references/art-direction.md +319 -0
  205. package/template/.aioson/skills/design/clean-saas-ui/references/components.md +365 -0
  206. package/template/.aioson/skills/design/clean-saas-ui/references/dashboards.md +196 -0
  207. package/template/.aioson/skills/design/clean-saas-ui/references/design-tokens.md +244 -0
  208. package/template/.aioson/skills/design/clean-saas-ui/references/motion.md +235 -0
  209. package/template/.aioson/skills/design/clean-saas-ui/references/patterns.md +215 -0
  210. package/template/.aioson/skills/design/clean-saas-ui/references/websites.md +295 -0
  211. package/template/.aioson/skills/design/cognitive-core-ui/SKILL.md +55 -9
  212. package/template/.aioson/skills/design/cognitive-core-ui/references/art-direction.md +339 -0
  213. package/template/.aioson/skills/design/cognitive-core-ui/references/components.md +1 -1
  214. package/template/.aioson/skills/design/cognitive-core-ui/references/dashboards.md +100 -0
  215. package/template/.aioson/skills/design/cognitive-core-ui/references/design-tokens.md +43 -9
  216. package/template/.aioson/skills/design/cognitive-core-ui/references/motion.md +40 -0
  217. package/template/.aioson/skills/design/cognitive-core-ui/references/patterns.md +1 -1
  218. package/template/.aioson/skills/design/cognitive-core-ui/references/websites.md +99 -12
  219. package/template/.aioson/skills/design/glassmorphism-ui/SKILL.md +222 -0
  220. package/template/.aioson/skills/design/glassmorphism-ui/references/art-direction.md +159 -0
  221. package/template/.aioson/skills/design/glassmorphism-ui/references/components.md +498 -0
  222. package/template/.aioson/skills/design/glassmorphism-ui/references/dashboards.md +236 -0
  223. package/template/.aioson/skills/design/glassmorphism-ui/references/design-tokens.md +274 -0
  224. package/template/.aioson/skills/design/glassmorphism-ui/references/motion.md +355 -0
  225. package/template/.aioson/skills/design/glassmorphism-ui/references/patterns.md +198 -0
  226. package/template/.aioson/skills/design/glassmorphism-ui/references/websites.md +307 -0
  227. package/template/.aioson/skills/design/neo-brutalist-ui/SKILL.md +213 -0
  228. package/template/.aioson/skills/design/neo-brutalist-ui/references/art-direction.md +228 -0
  229. package/template/.aioson/skills/design/neo-brutalist-ui/references/components.md +855 -0
  230. package/template/.aioson/skills/design/neo-brutalist-ui/references/dashboards.md +334 -0
  231. package/template/.aioson/skills/design/neo-brutalist-ui/references/design-tokens.md +342 -0
  232. package/template/.aioson/skills/design/neo-brutalist-ui/references/motion.md +286 -0
  233. package/template/.aioson/skills/design/neo-brutalist-ui/references/patterns.md +458 -0
  234. package/template/.aioson/skills/design/neo-brutalist-ui/references/websites.md +723 -0
  235. package/template/.aioson/skills/design/warm-craft-ui/SKILL.md +209 -0
  236. package/template/.aioson/skills/design/warm-craft-ui/references/art-direction.md +324 -0
  237. package/template/.aioson/skills/design/warm-craft-ui/references/components.md +508 -0
  238. package/template/.aioson/skills/design/warm-craft-ui/references/dashboards.md +223 -0
  239. package/template/.aioson/skills/design/warm-craft-ui/references/design-tokens.md +374 -0
  240. package/template/.aioson/skills/design/warm-craft-ui/references/motion.md +356 -0
  241. package/template/.aioson/skills/design/warm-craft-ui/references/patterns.md +288 -0
  242. package/template/.aioson/skills/design/warm-craft-ui/references/websites.md +289 -0
  243. package/template/.aioson/skills/premium-visual-design/SKILL.md +83 -0
  244. package/template/.aioson/skills/premium-visual-design/components/agent-badge.md +92 -0
  245. package/template/.aioson/skills/premium-visual-design/components/dependency-node.md +102 -0
  246. package/template/.aioson/skills/premium-visual-design/components/mention-autocomplete.md +136 -0
  247. package/template/.aioson/skills/premium-visual-design/components/notification-center.md +136 -0
  248. package/template/.aioson/skills/premium-visual-design/components/review-action-bar.md +188 -0
  249. package/template/.aioson/skills/premium-visual-design/components/team-switcher.md +131 -0
  250. package/template/.aioson/skills/premium-visual-design/patterns/agent-message-thread.md +198 -0
  251. package/template/.aioson/skills/premium-visual-design/patterns/notification-panel.md +275 -0
  252. package/template/.aioson/skills/premium-visual-design/patterns/review-workflow-ui.md +234 -0
  253. package/template/.aioson/skills/premium-visual-design/patterns/task-dependency-graph.md +147 -0
  254. package/template/.aioson/skills/premium-visual-design/tokens/status-extended.md +142 -0
  255. package/template/.aioson/skills/process/aioson-spec-driven/SKILL.md +45 -0
  256. package/template/.aioson/skills/process/aioson-spec-driven/references/approval-gates.md +109 -0
  257. package/template/.aioson/skills/process/aioson-spec-driven/references/artifact-map.md +44 -0
  258. package/template/.aioson/skills/process/aioson-spec-driven/references/classification-map.md +37 -0
  259. package/template/.aioson/skills/process/aioson-spec-driven/references/hardening-lane.md +49 -0
  260. package/template/.aioson/skills/process/aioson-spec-driven/references/maintenance-and-state.md +66 -0
  261. package/template/.aioson/skills/process/aioson-spec-driven/references/ui-language.md +75 -0
  262. package/template/.aioson/skills/process/design-hybrid-forge/SKILL.md +144 -0
  263. package/template/.aioson/skills/process/design-hybrid-forge/references/crossover-protocol.md +221 -0
  264. package/template/.aioson/skills/process/design-hybrid-forge/references/naming-registry.md +88 -0
  265. package/template/.aioson/skills/process/design-hybrid-forge/references/output-contract.md +291 -0
  266. package/template/.aioson/skills/process/design-hybrid-forge/references/pair-compatibility.md +117 -0
  267. package/template/.aioson/skills/process/design-hybrid-forge/references/quality-gates.md +188 -0
  268. package/template/.aioson/skills/process/design-hybrid-forge/references/variation-library.md +125 -0
  269. package/template/.aioson/skills/squad/formats/catalog.json +15 -0
  270. package/template/.aioson/skills/squad/formats/content/blog-post.md +47 -0
  271. package/template/.aioson/skills/squad/formats/content/newsletter.md +47 -0
  272. package/template/.aioson/skills/squad/formats/creative/podcast-script.md +43 -0
  273. package/template/.aioson/skills/squad/formats/creative/video-script.md +41 -0
  274. package/template/.aioson/skills/squad/formats/social/instagram-feed.md +42 -0
  275. package/template/.aioson/skills/squad/formats/social/linkedin-post.md +42 -0
  276. package/template/.aioson/skills/squad/formats/social/tiktok.md +39 -0
  277. package/template/.aioson/skills/squad/formats/social/twitter-thread.md +39 -0
  278. package/template/.aioson/skills/squad/formats/social/youtube-long.md +47 -0
  279. package/template/.aioson/skills/squad/formats/social/youtube-shorts.md +39 -0
  280. package/template/.aioson/skills/squad/patterns/multi-platform-pattern.md +108 -0
  281. package/template/.aioson/skills/squad/patterns/persona-based-pattern.md +98 -0
  282. package/template/.aioson/skills/squad/patterns/pipeline-pattern.md +106 -0
  283. package/template/.aioson/skills/squad/patterns/review-loop-pattern.md +81 -0
  284. package/template/.aioson/skills/squad/references/checklist-templates.md +122 -0
  285. package/template/.aioson/skills/squad/references/executor-archetypes.md +123 -0
  286. package/template/.aioson/skills/squad/references/workflow-templates.md +169 -0
  287. package/template/.aioson/skills/static/debugging-protocol.md +42 -0
  288. package/template/.aioson/skills/static/git-worktrees.md +36 -0
  289. package/template/.aioson/tasks/implementation-plan.md +19 -0
  290. package/template/.aioson/tasks/squad-design.md +28 -0
  291. package/template/.aioson/tasks/squad-profile.md +48 -0
  292. package/template/.aioson/tasks/squad-review.md +61 -0
  293. package/template/.aioson/tasks/squad-task-decompose.md +66 -0
  294. package/template/.claude/commands/aioson/agent/neo.md +5 -0
  295. package/template/.claude/commands/aioson/agent/tester.md +5 -0
  296. package/template/.gemini/GEMINI.md +1 -0
  297. package/template/.gemini/commands/aios-neo.toml +4 -0
  298. package/template/.gemini/commands/aios-tester.toml +6 -0
  299. package/template/AGENTS.md +26 -1
  300. package/template/CLAUDE.md +6 -2
  301. package/template/OPENCODE.md +2 -0
@@ -0,0 +1,114 @@
1
+ 'use strict';
2
+
3
+ const { spawnSync } = require('node:child_process');
4
+ const path = require('node:path');
5
+
6
+ /**
7
+ * Create a git worktree for a squad agent.
8
+ * Branch: aioson/{squadSlug}/{agentSlug}
9
+ * Path: .aioson/squads/{squadSlug}/worktrees/{agentSlug}
10
+ */
11
+ function createWorktree(projectDir, squadSlug, agentSlug) {
12
+ const branchName = `aioson/${squadSlug}/${agentSlug}`;
13
+ const worktreePath = path.join(projectDir, '.aioson', 'squads', squadSlug, 'worktrees', agentSlug);
14
+
15
+ // Try with new branch first
16
+ let result = spawnSync('git', ['worktree', 'add', '-b', branchName, worktreePath], {
17
+ cwd: projectDir,
18
+ encoding: 'utf8'
19
+ });
20
+
21
+ if (result.status !== 0) {
22
+ // Branch may already exist — try without -b
23
+ result = spawnSync('git', ['worktree', 'add', worktreePath, branchName], {
24
+ cwd: projectDir,
25
+ encoding: 'utf8'
26
+ });
27
+ if (result.status !== 0) {
28
+ throw new Error(result.stderr || 'Failed to create worktree');
29
+ }
30
+ }
31
+
32
+ return { ok: true, branchName, worktreePath };
33
+ }
34
+
35
+ /**
36
+ * Merge worktree branch back to the current branch.
37
+ * autoMerge=true → attempt fast-forward. false → report without merging.
38
+ */
39
+ function mergeWorktree(projectDir, squadSlug, agentSlug, autoMerge = false) {
40
+ const branchName = `aioson/${squadSlug}/${agentSlug}`;
41
+
42
+ if (!autoMerge) {
43
+ return { ok: false, reason: 'auto_merge_disabled', branch: branchName };
44
+ }
45
+
46
+ const result = spawnSync('git', ['merge', '--ff-only', branchName], {
47
+ cwd: projectDir,
48
+ encoding: 'utf8'
49
+ });
50
+
51
+ if (result.status === 0) {
52
+ return { ok: true, branch: branchName, merged: true };
53
+ }
54
+
55
+ return {
56
+ ok: false,
57
+ reason: 'merge_conflict',
58
+ branch: branchName,
59
+ detail: (result.stderr || result.stdout || '').trim()
60
+ };
61
+ }
62
+
63
+ /**
64
+ * Remove a worktree (and optionally delete its branch).
65
+ */
66
+ function cleanupWorktree(projectDir, squadSlug, agentSlug, deleteBranch = false) {
67
+ const worktreePath = path.join(projectDir, '.aioson', 'squads', squadSlug, 'worktrees', agentSlug);
68
+ const branchName = `aioson/${squadSlug}/${agentSlug}`;
69
+
70
+ const result = spawnSync('git', ['worktree', 'remove', '--force', worktreePath], {
71
+ cwd: projectDir,
72
+ encoding: 'utf8'
73
+ });
74
+
75
+ const removed = result.status === 0;
76
+
77
+ if (deleteBranch) {
78
+ spawnSync('git', ['branch', '-D', branchName], { cwd: projectDir, encoding: 'utf8' });
79
+ }
80
+
81
+ return { ok: removed, worktreePath, branchName };
82
+ }
83
+
84
+ /**
85
+ * List all squad worktrees (filters by aioson/{squadSlug}/ branch prefix).
86
+ */
87
+ function listWorktrees(projectDir, squadSlug) {
88
+ const result = spawnSync('git', ['worktree', 'list', '--porcelain'], {
89
+ cwd: projectDir,
90
+ encoding: 'utf8'
91
+ });
92
+
93
+ if (result.status !== 0) return [];
94
+
95
+ const entries = [];
96
+ const blocks = result.stdout.trim().split('\n\n');
97
+ for (const block of blocks) {
98
+ if (!block.trim()) continue;
99
+ const entry = {};
100
+ for (const line of block.split('\n')) {
101
+ if (line.startsWith('worktree ')) entry.path = line.slice(9);
102
+ else if (line.startsWith('branch ')) entry.branch = line.slice(7).replace('refs/heads/', '');
103
+ else if (line.startsWith('HEAD ')) entry.head = line.slice(5);
104
+ }
105
+ if (entry.branch && entry.branch.startsWith(`aioson/${squadSlug}/`)) {
106
+ entry.agentSlug = entry.branch.split('/').pop();
107
+ entries.push(entry);
108
+ }
109
+ }
110
+
111
+ return entries;
112
+ }
113
+
114
+ module.exports = { createWorktree, mergeWorktree, cleanupWorktree, listWorktrees };
@@ -0,0 +1,490 @@
1
+ 'use strict';
2
+
3
+ const http = require('node:http');
4
+ const fs = require('node:fs/promises');
5
+ const path = require('node:path');
6
+ const { openRuntimeDb, insertWorkerRun } = require('./runtime-store');
7
+ const { listWorkers, runWorker, loadWorkerConfig } = require('./worker-runner');
8
+
9
+ const SQUADS_DIR = path.join('.aioson', 'squads');
10
+
11
+ // --- Simple Cron Parser (zero dependencies) ---
12
+
13
+ const CRON_PRESETS = {
14
+ '@yearly': '0 0 1 1 *',
15
+ '@monthly': '0 0 1 * *',
16
+ '@weekly': '0 0 * * 0',
17
+ '@daily': '0 0 * * *',
18
+ '@hourly': '0 * * * *',
19
+ '@every5m': '*/5 * * * *',
20
+ '@every10m': '*/10 * * * *',
21
+ '@every15m': '*/15 * * * *',
22
+ '@every30m': '*/30 * * * *'
23
+ };
24
+
25
+ function parseCronField(field, min, max) {
26
+ if (field === '*') return null; // matches all
27
+ const values = new Set();
28
+ for (const part of field.split(',')) {
29
+ const stepMatch = part.match(/^(\*|\d+(?:-\d+)?)\/(\d+)$/);
30
+ if (stepMatch) {
31
+ const step = parseInt(stepMatch[2], 10);
32
+ let start = min;
33
+ let end = max;
34
+ if (stepMatch[1] !== '*') {
35
+ const range = stepMatch[1].split('-');
36
+ start = parseInt(range[0], 10);
37
+ if (range[1]) end = parseInt(range[1], 10);
38
+ }
39
+ for (let i = start; i <= end; i += step) values.add(i);
40
+ } else if (part.includes('-')) {
41
+ const [a, b] = part.split('-').map(Number);
42
+ for (let i = a; i <= b; i++) values.add(i);
43
+ } else {
44
+ values.add(parseInt(part, 10));
45
+ }
46
+ }
47
+ return values;
48
+ }
49
+
50
+ function parseCronExpression(expr) {
51
+ const resolved = CRON_PRESETS[expr] || expr;
52
+ const parts = resolved.trim().split(/\s+/);
53
+ if (parts.length !== 5) return null;
54
+ return {
55
+ minute: parseCronField(parts[0], 0, 59),
56
+ hour: parseCronField(parts[1], 0, 23),
57
+ dayOfMonth: parseCronField(parts[2], 1, 31),
58
+ month: parseCronField(parts[3], 1, 12),
59
+ dayOfWeek: parseCronField(parts[4], 0, 6)
60
+ };
61
+ }
62
+
63
+ function cronMatches(parsed, date) {
64
+ if (!parsed) return false;
65
+ const checks = [
66
+ [parsed.minute, date.getMinutes()],
67
+ [parsed.hour, date.getHours()],
68
+ [parsed.dayOfMonth, date.getDate()],
69
+ [parsed.month, date.getMonth() + 1],
70
+ [parsed.dayOfWeek, date.getDay()]
71
+ ];
72
+ return checks.every(([field, val]) => field === null || field.has(val));
73
+ }
74
+
75
+ // --- Squad Daemon ---
76
+
77
+ class SquadDaemon {
78
+ constructor(projectDir, squadSlug, options = {}) {
79
+ this.projectDir = projectDir;
80
+ this.squadSlug = squadSlug;
81
+ this.webhookPort = options.port || 0;
82
+ this.pollInterval = options.poll || 10000;
83
+ this.config = options.config || {};
84
+ this.running = false;
85
+ this.db = null;
86
+ this.httpServer = null;
87
+ this.cronTimer = null;
88
+ this.pollTimer = null;
89
+ this.cronJobs = [];
90
+ this.lastCronCheck = null;
91
+ this.eventLog = [];
92
+ this.startedAt = null;
93
+ }
94
+
95
+ log(level, message, data) {
96
+ const entry = {
97
+ ts: new Date().toISOString(),
98
+ level,
99
+ squad: this.squadSlug,
100
+ message,
101
+ ...(data || {})
102
+ };
103
+ this.eventLog.push(entry);
104
+ if (this.eventLog.length > 500) this.eventLog.shift();
105
+ return entry;
106
+ }
107
+
108
+ async start() {
109
+ // 1. Open runtime DB
110
+ const handle = await openRuntimeDb(this.projectDir, { mustExist: false });
111
+ if (!handle) {
112
+ throw new Error('Could not open runtime database');
113
+ }
114
+ this.db = handle.db;
115
+
116
+ // 2. Load workers and register cron jobs
117
+ const workers = await listWorkers(this.projectDir, this.squadSlug);
118
+ this.cronJobs = [];
119
+ for (const worker of workers) {
120
+ if (worker.type === 'scheduled' && worker.trigger && worker.trigger.cron) {
121
+ const parsed = parseCronExpression(worker.trigger.cron);
122
+ if (parsed) {
123
+ this.cronJobs.push({ workerSlug: worker.slug, cron: worker.trigger.cron, parsed });
124
+ }
125
+ }
126
+ }
127
+
128
+ // 3. Start webhook HTTP server
129
+ this.httpServer = await this._startWebhookServer();
130
+
131
+ // 4. Start cron check loop (every 60s, checks at minute boundaries)
132
+ this.lastCronCheck = new Date();
133
+ this.cronTimer = setInterval(() => this._checkCron(), 60000);
134
+
135
+ // 5. Start event poll loop
136
+ this.pollTimer = setInterval(() => this._pollEvents(), this.pollInterval);
137
+
138
+ // 6. Register daemon in SQLite
139
+ this._upsertDaemonRecord('running');
140
+
141
+ this.running = true;
142
+ await this._processInbox();
143
+
144
+ this.startedAt = new Date().toISOString();
145
+ this.log('info', 'Daemon started', {
146
+ port: this.webhookPort,
147
+ cronJobs: this.cronJobs.length,
148
+ workers: workers.length
149
+ });
150
+
151
+ return {
152
+ port: this.webhookPort,
153
+ cronJobs: this.cronJobs.length,
154
+ workers: workers.length
155
+ };
156
+ }
157
+
158
+ async stop() {
159
+ this.running = false;
160
+
161
+ if (this.cronTimer) {
162
+ clearInterval(this.cronTimer);
163
+ this.cronTimer = null;
164
+ }
165
+ if (this.pollTimer) {
166
+ clearInterval(this.pollTimer);
167
+ this.pollTimer = null;
168
+ }
169
+
170
+ if (this.httpServer) {
171
+ await new Promise((resolve) => this.httpServer.close(resolve));
172
+ this.httpServer = null;
173
+ }
174
+
175
+ this._upsertDaemonRecord('stopped');
176
+
177
+ if (this.db) {
178
+ this.db.close();
179
+ this.db = null;
180
+ }
181
+
182
+ this.log('info', 'Daemon stopped');
183
+ }
184
+
185
+ getStatus() {
186
+ return {
187
+ squad: this.squadSlug,
188
+ running: this.running,
189
+ port: this.webhookPort,
190
+ startedAt: this.startedAt,
191
+ cronJobs: this.cronJobs.map(j => ({ worker: j.workerSlug, cron: j.cron })),
192
+ recentLogs: this.eventLog.slice(-20)
193
+ };
194
+ }
195
+
196
+ // --- Private methods ---
197
+
198
+ _startWebhookServer() {
199
+ return new Promise((resolve, reject) => {
200
+ const server = http.createServer(async (req, res) => {
201
+ await this._handleWebhook(req, res);
202
+ });
203
+ server.on('error', reject);
204
+ const bindAddr = this.config.webhook?.bind || '127.0.0.1';
205
+ server.listen(this.webhookPort, bindAddr, () => {
206
+ this.webhookPort = server.address().port;
207
+ resolve(server);
208
+ });
209
+ });
210
+ }
211
+
212
+ async _handleWebhook(req, res) {
213
+ const segments = (req.url || '').replace(/\/+$/, '').split('/').filter(Boolean);
214
+
215
+ // Status aceita GET e POST (health check externo)
216
+ if (segments[0] === 'status' && segments.length === 1) {
217
+ res.writeHead(200, { 'Content-Type': 'application/json' });
218
+ res.end(JSON.stringify(this.getStatus()));
219
+ return;
220
+ }
221
+
222
+ if (req.method !== 'POST' && req.method !== 'OPTIONS') {
223
+ res.writeHead(405, { 'Content-Type': 'application/json' });
224
+ res.end(JSON.stringify({ error: 'Method not allowed' }));
225
+ return;
226
+ }
227
+
228
+ // Read raw body (must be before JSON.parse so HMAC validates original bytes)
229
+ let rawBody = '';
230
+ for await (const chunk of req) rawBody += chunk;
231
+
232
+ // HMAC signature validation
233
+ if (this.config.webhook?.validate_signature) {
234
+ const { createHmac, timingSafeEqual } = require('node:crypto');
235
+ const envKey = this.config.webhook.signature_env || 'WEBHOOK_SECRET';
236
+ const headerKey = (this.config.webhook.signature_header || 'x-hub-signature-256').toLowerCase();
237
+ const secret = process.env[envKey];
238
+ const receivedSig = req.headers[headerKey];
239
+
240
+ if (!secret || !receivedSig) {
241
+ res.writeHead(401, { 'Content-Type': 'application/json' });
242
+ res.end(JSON.stringify({ error: 'signature_required' }));
243
+ return;
244
+ }
245
+ const expected = 'sha256=' + createHmac('sha256', secret).update(rawBody).digest('hex');
246
+ try {
247
+ if (!timingSafeEqual(Buffer.from(receivedSig), Buffer.from(expected))) {
248
+ res.writeHead(401, { 'Content-Type': 'application/json' });
249
+ res.end(JSON.stringify({ error: 'invalid_signature' }));
250
+ return;
251
+ }
252
+ } catch {
253
+ res.writeHead(401, { 'Content-Type': 'application/json' });
254
+ res.end(JSON.stringify({ error: 'invalid_signature' }));
255
+ return;
256
+ }
257
+ }
258
+
259
+ let payload;
260
+ try {
261
+ payload = JSON.parse(rawBody || '{}');
262
+ } catch {
263
+ res.writeHead(400, { 'Content-Type': 'application/json' });
264
+ res.end(JSON.stringify({ error: 'Invalid JSON body' }));
265
+ return;
266
+ }
267
+
268
+ // POST /call/:worker (inter-squad)
269
+ if (segments[0] === 'call' && segments[1]) {
270
+ const depth = payload?._inter_squad?.depth ?? 0;
271
+ if (depth > 5) {
272
+ res.writeHead(429, { 'Content-Type': 'application/json' });
273
+ res.end(JSON.stringify({ error: 'cascade_guard' }));
274
+ return;
275
+ }
276
+ const result = await this._executeWorker(segments[1], payload, 'inter-squad');
277
+ res.writeHead(result.ok ? 200 : 500, { 'Content-Type': 'application/json' });
278
+ res.end(JSON.stringify(result));
279
+ return;
280
+ }
281
+
282
+ // /api/:path (public API endpoints)
283
+ if (segments[0] === 'api' && segments[1]) {
284
+ const apiPath = '/' + segments[1];
285
+ const endpoint = (this.config.api_endpoints || []).find(e => e.path === apiPath);
286
+
287
+ if (!endpoint) {
288
+ res.writeHead(404, { 'Content-Type': 'application/json' });
289
+ res.end(JSON.stringify({ error: 'api_endpoint_not_found' }));
290
+ return;
291
+ }
292
+
293
+ const origin = req.headers['origin'];
294
+ const corsOrigins = endpoint.cors_origins || [];
295
+ if (origin && corsOrigins.includes(origin)) {
296
+ res.setHeader('Access-Control-Allow-Origin', origin);
297
+ res.setHeader('Access-Control-Allow-Methods', endpoint.method || 'POST');
298
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
299
+ res.setHeader('Vary', 'Origin');
300
+ }
301
+ if (req.method === 'OPTIONS') {
302
+ res.writeHead(204);
303
+ res.end();
304
+ return;
305
+ }
306
+
307
+ const result = await this._executeWorker(endpoint.worker, payload, 'api');
308
+ res.writeHead(result.ok ? 200 : 500, { 'Content-Type': 'application/json' });
309
+ res.end(JSON.stringify(result));
310
+ return;
311
+ }
312
+
313
+ if (segments[0] !== 'webhook' || !segments[1]) {
314
+ res.writeHead(404, { 'Content-Type': 'application/json' });
315
+ res.end(JSON.stringify({ error: 'Not found. Use POST /webhook/<worker-slug>' }));
316
+ return;
317
+ }
318
+
319
+ const workerSlug = segments[1];
320
+
321
+ this.log('info', `Webhook received for ${workerSlug}`, { payload });
322
+
323
+ // Execute worker
324
+ const result = await this._executeWorker(workerSlug, payload, 'webhook');
325
+
326
+ const statusCode = result.ok ? 200 : 500;
327
+ res.writeHead(statusCode, { 'Content-Type': 'application/json' });
328
+ res.end(JSON.stringify(result));
329
+ }
330
+
331
+ async _checkCron() {
332
+ if (!this.running || this.cronJobs.length === 0) return;
333
+
334
+ const now = new Date();
335
+ for (const job of this.cronJobs) {
336
+ if (cronMatches(job.parsed, now)) {
337
+ this.log('info', `Cron triggered: ${job.workerSlug}`, { cron: job.cron });
338
+ this._executeWorker(job.workerSlug, {}, 'scheduled').catch(err => {
339
+ this.log('error', `Cron execution failed: ${job.workerSlug}`, { error: err.message });
340
+ });
341
+ }
342
+ }
343
+ this.lastCronCheck = now;
344
+
345
+ // Update heartbeat
346
+ this._updateHeartbeat();
347
+ }
348
+
349
+ async _pollEvents() {
350
+ if (!this.running || !this.db) return;
351
+
352
+ // Poll for pending handoffs targeted at this squad
353
+ try {
354
+ const pending = this.db.prepare(
355
+ "SELECT * FROM squad_handoffs WHERE to_squad = ? AND status = 'pending' ORDER BY created_at ASC LIMIT 5"
356
+ ).all(this.squadSlug);
357
+
358
+ for (const handoff of pending) {
359
+ const payload = handoff.payload_json ? JSON.parse(handoff.payload_json) : {};
360
+ this.log('info', `Handoff received from ${handoff.from_squad}`, { handoffId: handoff.id });
361
+
362
+ // Find event-triggered workers
363
+ const workers = await listWorkers(this.projectDir, this.squadSlug);
364
+ for (const w of workers) {
365
+ if (w.type === 'event' && w.trigger && w.trigger.source === 'handoff') {
366
+ await this._executeWorker(w.slug, payload, 'event');
367
+ }
368
+ }
369
+
370
+ // Mark handoff consumed
371
+ this.db.prepare(
372
+ "UPDATE squad_handoffs SET status = 'consumed', consumed_at = datetime('now') WHERE id = ?"
373
+ ).run(handoff.id);
374
+ }
375
+ } catch (err) {
376
+ this.log('error', 'Poll error', { error: err.message });
377
+ }
378
+
379
+ this._updateHeartbeat();
380
+ }
381
+
382
+ async _executeWorker(workerSlug, inputPayload, triggerType) {
383
+ const result = await runWorker(this.projectDir, this.squadSlug, workerSlug, inputPayload, {
384
+ triggerType,
385
+ noRetry: false
386
+ });
387
+
388
+ // Log to runtime store
389
+ if (this.db) {
390
+ try {
391
+ const conversationId = triggerType === 'inter-squad'
392
+ ? inputPayload?._inter_squad?.conversationId
393
+ : undefined;
394
+ insertWorkerRun(this.db, {
395
+ squadSlug: this.squadSlug,
396
+ workerSlug,
397
+ triggerType,
398
+ inputJson: JSON.stringify(inputPayload),
399
+ outputJson: result.ok ? JSON.stringify(result.output) : null,
400
+ status: result.ok ? 'completed' : 'failed',
401
+ errorMessage: result.ok ? null : result.error,
402
+ durationMs: result.durationMs || 0,
403
+ attempt: result.attempt || 1,
404
+ conversationId
405
+ });
406
+ } catch (err) {
407
+ this.log('error', 'Failed to log worker run', { error: err.message });
408
+ }
409
+ }
410
+
411
+ this.log(result.ok ? 'info' : 'error', `Worker ${workerSlug}: ${result.ok ? 'completed' : 'failed'}`, {
412
+ triggerType,
413
+ durationMs: result.durationMs
414
+ });
415
+
416
+ return result;
417
+ }
418
+
419
+ async _processInbox() {
420
+ const inboxDir = path.join(this.projectDir, '.aioson', 'squads', this.squadSlug, 'inbox');
421
+ let entries;
422
+ try { entries = await fs.readdir(inboxDir); } catch { return; }
423
+
424
+ for (const file of entries.filter(f => f.endsWith('.json'))) {
425
+ const filePath = path.join(inboxDir, file);
426
+ try {
427
+ const raw = await fs.readFile(filePath, 'utf8');
428
+ const msg = JSON.parse(raw);
429
+ const result = await this._executeWorker(msg.worker, msg.payload, 'inter-squad');
430
+ if (result.ok) {
431
+ await fs.unlink(filePath);
432
+ } else {
433
+ const failDir = path.join(inboxDir, 'failed');
434
+ await fs.mkdir(failDir, { recursive: true });
435
+ await fs.rename(filePath, path.join(failDir, file));
436
+ }
437
+ } catch {
438
+ // arquivo corrompido — mover para failed
439
+ }
440
+ }
441
+ }
442
+
443
+ _upsertDaemonRecord(status) {
444
+ if (!this.db) return;
445
+ try {
446
+ this.db.prepare(`
447
+ INSERT INTO squad_daemons (squad_slug, status, pid, port, started_at, last_heartbeat, config_json)
448
+ VALUES (@squad_slug, @status, @pid, @port, @started_at, @last_heartbeat, @config_json)
449
+ ON CONFLICT(squad_slug) DO UPDATE SET
450
+ status = excluded.status,
451
+ pid = excluded.pid,
452
+ port = excluded.port,
453
+ started_at = CASE WHEN excluded.status = 'running' THEN excluded.started_at ELSE squad_daemons.started_at END,
454
+ last_heartbeat = excluded.last_heartbeat,
455
+ config_json = excluded.config_json,
456
+ error_message = NULL
457
+ `).run({
458
+ squad_slug: this.squadSlug,
459
+ status,
460
+ pid: process.pid,
461
+ port: this.webhookPort || null,
462
+ started_at: this.startedAt || new Date().toISOString(),
463
+ last_heartbeat: new Date().toISOString(),
464
+ config_json: JSON.stringify({
465
+ cronJobs: this.cronJobs.map(j => ({ worker: j.workerSlug, cron: j.cron }))
466
+ })
467
+ });
468
+ } catch {
469
+ // Ignore — daemon record is nice-to-have
470
+ }
471
+ }
472
+
473
+ _updateHeartbeat() {
474
+ if (!this.db) return;
475
+ try {
476
+ this.db.prepare(
477
+ "UPDATE squad_daemons SET last_heartbeat = datetime('now') WHERE squad_slug = ?"
478
+ ).run(this.squadSlug);
479
+ } catch {
480
+ // Ignore
481
+ }
482
+ }
483
+ }
484
+
485
+ module.exports = {
486
+ SquadDaemon,
487
+ parseCronExpression,
488
+ cronMatches,
489
+ parseCronField
490
+ };