@jaimevalasek/aioson 1.8.0 → 1.9.1

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 (974) hide show
  1. package/CHANGELOG.md +595 -595
  2. package/CODE_OF_CONDUCT.md +12 -12
  3. package/CONTRIBUTING.md +13 -13
  4. package/LICENSE +661 -661
  5. package/README.md +919 -919
  6. package/bin/aioson.js +4 -4
  7. package/docs/design-previews/aurora-command-ui-website.html +884 -884
  8. package/docs/design-previews/aurora-command-ui.html +682 -682
  9. package/docs/design-previews/bold-editorial-ui-website.html +658 -658
  10. package/docs/design-previews/bold-editorial-ui.html +717 -717
  11. package/docs/design-previews/clean-saas-ui-website.html +1202 -1202
  12. package/docs/design-previews/clean-saas-ui.html +549 -549
  13. package/docs/design-previews/cognitive-core-ui-website.html +1009 -1009
  14. package/docs/design-previews/cognitive-core-ui.html +463 -463
  15. package/docs/design-previews/glassmorphism-ui-website.html +572 -572
  16. package/docs/design-previews/glassmorphism-ui.html +886 -886
  17. package/docs/design-previews/index.html +699 -699
  18. package/docs/design-previews/interface-design-website.html +1187 -1187
  19. package/docs/design-previews/interface-design.html +513 -513
  20. package/docs/design-previews/neo-brutalist-ui-website.html +621 -621
  21. package/docs/design-previews/neo-brutalist-ui.html +797 -797
  22. package/docs/design-previews/premium-command-center-ui-website.html +1217 -1217
  23. package/docs/design-previews/premium-command-center-ui.html +552 -552
  24. package/docs/design-previews/pt.squarespace.com-homepage.html +889 -889
  25. package/docs/design-previews/warm-craft-ui-website.html +684 -684
  26. package/docs/design-previews/warm-craft-ui.html +739 -739
  27. package/docs/en/1-understand/ecosystem-map.md +228 -0
  28. package/docs/en/1-understand/glossary.md +288 -0
  29. package/docs/en/1-understand/what-is-aioson.md +94 -0
  30. package/docs/en/1-understand/why-it-exists.md +106 -0
  31. package/docs/en/2-start/existing-project.md +246 -0
  32. package/docs/en/2-start/first-project.md +307 -0
  33. package/docs/en/2-start/initial-decisions.md +223 -0
  34. package/docs/en/3-recipes/README.md +28 -0
  35. package/docs/en/3-recipes/continuity-between-sessions.md +303 -0
  36. package/docs/en/3-recipes/from-idea-to-prd-via-briefing.md +235 -0
  37. package/docs/en/3-recipes/full-feature-with-sheldon.md +338 -0
  38. package/docs/en/4-agents/README.md +56 -0
  39. package/docs/en/5-reference/README.md +60 -0
  40. package/docs/en/{cli-reference.md → 5-reference/cli-reference.md} +639 -464
  41. package/docs/en/{i18n.md → 5-reference/i18n.md} +52 -52
  42. package/docs/en/{json-schemas.md → 5-reference/json-schemas.md} +41 -41
  43. package/docs/en/{mcp.md → 5-reference/mcp.md} +56 -56
  44. package/docs/en/{parallel.md → 5-reference/parallel.md} +82 -82
  45. package/docs/en/{qa-browser.md → 5-reference/qa-browser.md} +339 -339
  46. package/docs/en/{release-flow.md → 5-reference/release-flow.md} +22 -22
  47. package/docs/en/{release-notes-template.md → 5-reference/release-notes-template.md} +41 -41
  48. package/docs/en/{release.md → 5-reference/release.md} +28 -28
  49. package/docs/en/{schemas → 5-reference/schemas}/agent-prompt.schema.json +17 -17
  50. package/docs/en/{schemas → 5-reference/schemas}/agents.schema.json +32 -32
  51. package/docs/en/{schemas → 5-reference/schemas}/context-validate.schema.json +36 -36
  52. package/docs/en/{schemas → 5-reference/schemas}/doctor.schema.json +89 -89
  53. package/docs/en/{schemas → 5-reference/schemas}/error.schema.json +24 -24
  54. package/docs/en/{schemas → 5-reference/schemas}/i18n-add.schema.json +15 -15
  55. package/docs/en/{schemas → 5-reference/schemas}/index.json +126 -126
  56. package/docs/en/{schemas → 5-reference/schemas}/info.schema.json +39 -39
  57. package/docs/en/{schemas → 5-reference/schemas}/init.schema.json +48 -48
  58. package/docs/en/{schemas → 5-reference/schemas}/install.schema.json +60 -60
  59. package/docs/en/{schemas → 5-reference/schemas}/locale-apply.schema.json +30 -30
  60. package/docs/en/{schemas → 5-reference/schemas}/mcp-doctor.schema.json +95 -95
  61. package/docs/en/{schemas → 5-reference/schemas}/mcp-init.schema.json +122 -122
  62. package/docs/en/{schemas → 5-reference/schemas}/package-test.schema.json +24 -24
  63. package/docs/en/{schemas → 5-reference/schemas}/parallel-assign.schema.json +66 -66
  64. package/docs/en/{schemas → 5-reference/schemas}/parallel-doctor.schema.json +122 -122
  65. package/docs/en/{schemas → 5-reference/schemas}/parallel-guard.schema.json +63 -63
  66. package/docs/en/{schemas → 5-reference/schemas}/parallel-init.schema.json +53 -53
  67. package/docs/en/{schemas → 5-reference/schemas}/parallel-merge.schema.json +84 -84
  68. package/docs/en/{schemas → 5-reference/schemas}/parallel-status.schema.json +184 -184
  69. package/docs/en/{schemas → 5-reference/schemas}/setup-context.schema.json +39 -39
  70. package/docs/en/{schemas → 5-reference/schemas}/smoke.schema.json +23 -23
  71. package/docs/en/{schemas → 5-reference/schemas}/update.schema.json +48 -48
  72. package/docs/en/{schemas → 5-reference/schemas}/workflow-plan.schema.json +30 -30
  73. package/docs/en/{squad-dashboard.md → 5-reference/squad-dashboard.md} +372 -372
  74. package/docs/en/{web3.md → 5-reference/web3.md} +54 -54
  75. package/docs/en/README.md +115 -0
  76. package/docs/en/active-learning-loop/README.md +117 -0
  77. package/docs/en/active-learning-loop/active-learning-loop.md +117 -0
  78. package/docs/en/active-learning-loop/cli-commands.md +320 -0
  79. package/docs/en/active-learning-loop/diagrams.md +225 -0
  80. package/docs/en/active-learning-loop/doctor-checks.md +151 -0
  81. package/docs/en/active-learning-loop/how-to-use.md +313 -0
  82. package/docs/en/active-learning-loop/troubleshooting.md +283 -0
  83. package/docs/en/deyvin-subtask-scout/README.md +109 -0
  84. package/docs/en/deyvin-subtask-scout/cli-commands.md +248 -0
  85. package/docs/en/deyvin-subtask-scout/diagrams.md +124 -0
  86. package/docs/en/deyvin-subtask-scout/how-to-use.md +221 -0
  87. package/docs/en/deyvin-subtask-scout/sub-task-scout.md +115 -0
  88. package/docs/en/deyvin-subtask-scout/troubleshooting.md +184 -0
  89. package/docs/integrations/apps-publish-marketplace.md +94 -94
  90. package/docs/integrations/sdlc-genius-boundary.md +76 -76
  91. package/docs/integrations/sdlc-genius-eval-matrix.md +75 -75
  92. package/docs/integrations/sdlc-genius-install-checklist.md +93 -93
  93. package/docs/integrations/sdlc-genius-review-samples.md +86 -86
  94. package/docs/openclaw-bridge.md +308 -308
  95. package/docs/pt/1-entender/glossario.md +288 -0
  96. package/docs/pt/1-entender/mapa-do-ecossistema.md +228 -0
  97. package/docs/pt/1-entender/o-que-e-aioson.md +94 -0
  98. package/docs/pt/1-entender/por-que-existe.md +107 -0
  99. package/docs/pt/2-comecar/decisoes-iniciais.md +223 -0
  100. package/docs/pt/2-comecar/primeiro-projeto.md +307 -0
  101. package/docs/pt/2-comecar/projeto-existente.md +245 -0
  102. package/docs/pt/3-receitas/README.md +28 -0
  103. package/docs/pt/3-receitas/app-saas-do-zero.md +324 -0
  104. package/docs/pt/3-receitas/auditoria-seguranca.md +254 -0
  105. package/docs/pt/3-receitas/clonar-design-de-site.md +211 -0
  106. package/docs/pt/3-receitas/continuidade-entre-sessoes.md +303 -0
  107. package/docs/pt/3-receitas/da-ideia-ao-prd-via-briefing.md +234 -0
  108. package/docs/pt/3-receitas/feature-completa-com-sheldon.md +338 -0
  109. package/docs/pt/3-receitas/integracao-em-codebase-grande.md +243 -0
  110. package/docs/pt/3-receitas/landing-page.md +281 -0
  111. package/docs/pt/3-receitas/plans-externos-para-product.md +191 -0
  112. package/docs/pt/3-receitas/publicar-no-aioson-com.md +219 -0
  113. package/docs/pt/3-receitas/refatoracao-grande.md +251 -0
  114. package/docs/pt/4-agentes/README.md +65 -0
  115. package/docs/pt/4-agentes/analyst.md +111 -0
  116. package/docs/pt/4-agentes/architect.md +113 -0
  117. package/docs/pt/4-agentes/briefing.md +95 -0
  118. package/docs/pt/4-agentes/committer.md +108 -0
  119. package/docs/pt/4-agentes/copywriter.md +279 -0
  120. package/docs/pt/4-agentes/design-hybrid-forge.md +116 -0
  121. package/docs/pt/4-agentes/dev.md +136 -0
  122. package/docs/pt/4-agentes/deyvin.md +99 -0
  123. package/docs/pt/4-agentes/discover.md +122 -0
  124. package/docs/pt/4-agentes/discovery-design-doc.md +91 -0
  125. package/docs/pt/4-agentes/genome.md +115 -0
  126. package/docs/pt/4-agentes/neo.md +93 -0
  127. package/docs/pt/4-agentes/orache.md +107 -0
  128. package/docs/pt/4-agentes/orchestrator.md +118 -0
  129. package/docs/pt/4-agentes/pentester.md +131 -0
  130. package/docs/pt/4-agentes/pm.md +97 -0
  131. package/docs/pt/4-agentes/product.md +114 -0
  132. package/docs/pt/4-agentes/profiler-enricher.md +93 -0
  133. package/docs/pt/4-agentes/profiler-forge.md +93 -0
  134. package/docs/pt/4-agentes/profiler-researcher.md +98 -0
  135. package/docs/pt/4-agentes/qa.md +124 -0
  136. package/docs/pt/4-agentes/setup.md +104 -0
  137. package/docs/pt/4-agentes/sheldon.md +95 -0
  138. package/docs/pt/4-agentes/site-forge.md +104 -0
  139. package/docs/pt/4-agentes/squad.md +127 -0
  140. package/docs/pt/4-agentes/tester.md +105 -0
  141. package/docs/pt/4-agentes/ux-ui.md +110 -0
  142. package/docs/pt/4-agentes/validator.md +118 -0
  143. package/docs/pt/5-referencia/README.md +88 -0
  144. package/docs/pt/5-referencia/agent-chain-continuity.md +124 -0
  145. package/docs/pt/{agent-sharding.md → 5-referencia/agent-sharding.md} +132 -132
  146. package/docs/pt/5-referencia/aioson-com-store.md +119 -0
  147. package/docs/pt/{automacao-squads.md → 5-referencia/automacao-squads.md} +407 -407
  148. package/docs/pt/{clientes-ai.md → 5-referencia/clientes-ai.md} +300 -290
  149. package/docs/pt/{comandos-cli.md → 5-referencia/comandos-cli.md} +1823 -1781
  150. package/docs/pt/{compress-agents.md → 5-referencia/compress-agents.md} +304 -304
  151. package/docs/pt/{design-docs-governance.md → 5-referencia/design-docs-governance.md} +59 -59
  152. package/docs/pt/{devlog-pipeline.md → 5-referencia/devlog-pipeline.md} +270 -270
  153. package/docs/pt/{feature-archive.md → 5-referencia/feature-archive.md} +199 -191
  154. package/docs/pt/5-referencia/feature-dossier.md +121 -0
  155. package/docs/pt/{fluxo-artefatos.md → 5-referencia/fluxo-artefatos.md} +179 -178
  156. package/docs/pt/{genome-3.0-spec.md → 5-referencia/genome-4.0-spec.md} +407 -407
  157. package/docs/pt/{genome-distribution.md → 5-referencia/genome-distribution.md} +232 -232
  158. package/docs/pt/{hooks-session-guard.md → 5-referencia/hooks-session-guard.md} +454 -454
  159. package/docs/pt/{inteligencia-adaptativa.md → 5-referencia/inteligencia-adaptativa.md} +324 -324
  160. package/docs/pt/5-referencia/live-sessions.md +144 -0
  161. package/docs/pt/5-referencia/memoria-e-contexto.md +340 -0
  162. package/docs/pt/{motor-hardening.md → 5-referencia/motor-hardening.md} +493 -492
  163. package/docs/pt/{output-strategy-delivery.md → 5-referencia/output-strategy-delivery.md} +655 -655
  164. package/docs/pt/{runner-system.md → 5-referencia/runner-system.md} +113 -113
  165. package/docs/pt/{runtime-observability.md → 5-referencia/runtime-observability.md} +76 -76
  166. package/docs/pt/{sandbox.md → 5-referencia/sandbox.md} +125 -125
  167. package/docs/pt/{sdd-automation-scripts.md → 5-referencia/sdd-automation-scripts.md} +559 -557
  168. package/docs/pt/5-referencia/sdd-framework.md +115 -0
  169. package/docs/pt/5-referencia/sdd-planos-e-estrutura.md +321 -0
  170. package/docs/pt/5-referencia/secure-by-default.md +117 -0
  171. package/docs/pt/{skills.md → 5-referencia/skills.md} +275 -267
  172. package/docs/pt/{spec-learnings-pipeline.md → 5-referencia/spec-learnings-pipeline.md} +265 -265
  173. package/docs/pt/{squad-dashboard.md → 5-referencia/squad-dashboard.md} +373 -373
  174. package/docs/pt/{web3.md → 5-referencia/web3.md} +797 -797
  175. package/docs/pt/README.md +111 -125
  176. package/docs/pt/_arquivo/README.md +130 -0
  177. package/docs/pt/{advisor-spec.md → _arquivo/advisor-spec.md} +343 -335
  178. package/docs/pt/{agentes-customizados.md → _arquivo/agentes-customizados.md} +678 -670
  179. package/docs/pt/{busca-de-contexto.md → _arquivo/busca-de-contexto.md} +136 -129
  180. package/docs/pt/{cache-de-contexto.md → _arquivo/cache-de-contexto.md} +163 -156
  181. package/docs/pt/{cenarios.md → _arquivo/cenarios.md} +1282 -1274
  182. package/docs/pt/{design-hybrid-forge.md → _arquivo/design-hybrid-forge.md} +365 -356
  183. package/docs/pt/{deyvin.md → _arquivo/deyvin.md} +123 -115
  184. package/docs/pt/{guia-engineer.md → _arquivo/guia-engineer.md} +234 -226
  185. package/docs/pt/{inicio-rapido.md → _arquivo/inicio-rapido.md} +261 -251
  186. package/docs/pt/{memoria-contexto.md → _arquivo/memoria-contexto.md} +262 -255
  187. package/docs/pt/{monitor-de-contexto.md → _arquivo/monitor-de-contexto.md} +165 -158
  188. package/docs/pt/{profiler-system.md → _arquivo/profiler-system.md} +222 -214
  189. package/docs/pt/{recuperacao-de-sessao.md → _arquivo/recuperacao-de-sessao.md} +134 -125
  190. package/docs/pt/{site-forge.md → _arquivo/site-forge.md} +318 -309
  191. package/docs/pt/{squad-genome.md → _arquivo/squad-genome.md} +793 -783
  192. package/docs/pt/active-learning-loop/README.md +117 -0
  193. package/docs/pt/active-learning-loop/ativo-learning-loop.md +117 -0
  194. package/docs/pt/active-learning-loop/comandos-cli.md +320 -0
  195. package/docs/pt/active-learning-loop/como-usar.md +313 -0
  196. package/docs/pt/active-learning-loop/diagramas.md +225 -0
  197. package/docs/pt/active-learning-loop/doctor-checks.md +151 -0
  198. package/docs/pt/active-learning-loop/troubleshooting.md +283 -0
  199. package/docs/pt/agentes.md +996 -993
  200. package/docs/pt/deyvin-subtask-scout/README.md +109 -0
  201. package/docs/pt/deyvin-subtask-scout/comandos-cli.md +248 -0
  202. package/docs/pt/deyvin-subtask-scout/como-usar.md +221 -0
  203. package/docs/pt/deyvin-subtask-scout/diagramas.md +124 -0
  204. package/docs/pt/deyvin-subtask-scout/sub-task-scout.md +113 -0
  205. package/docs/pt/deyvin-subtask-scout/troubleshooting.md +184 -0
  206. package/docs/pt/living-memory/README.md +81 -0
  207. package/docs/pt/living-memory/autonomy-contract.md +206 -0
  208. package/docs/pt/living-memory/diagramas.md +365 -0
  209. package/docs/pt/living-memory/memoria-viva.md +141 -0
  210. package/docs/pt/living-memory/notificacoes-info.md +142 -0
  211. package/docs/pt/living-memory/reflexao-in-harness.md +218 -0
  212. package/docs/pt/living-memory/troubleshooting.md +286 -0
  213. package/docs/testing/genome-2.0-manual-regression.md +23 -23
  214. package/docs/testing/genome-2.0-matrix.md +36 -36
  215. package/docs/testing/genome-2.0-rollout.md +184 -184
  216. package/package.json +51 -51
  217. package/src/a2a/client.js +165 -165
  218. package/src/a2a/server.js +223 -223
  219. package/src/agent-loader.js +280 -280
  220. package/src/agent-manifests.js +86 -66
  221. package/src/agents.js +92 -92
  222. package/src/autonomy-policy.js +163 -139
  223. package/src/backup-local.js +74 -74
  224. package/src/backup-provider.js +303 -303
  225. package/src/brain-query.js +171 -161
  226. package/src/cli.js +85 -5
  227. package/src/commands/agent-audit.js +397 -397
  228. package/src/commands/agent-export-skill.js +229 -229
  229. package/src/commands/agent-loader.js +85 -85
  230. package/src/commands/agents.js +273 -255
  231. package/src/commands/artifact-validate.js +218 -218
  232. package/src/commands/auth.js +298 -272
  233. package/src/commands/backup-local-cmd.js +25 -25
  234. package/src/commands/backup.js +533 -533
  235. package/src/commands/brain-query.js +44 -44
  236. package/src/commands/brief-gen.js +405 -405
  237. package/src/commands/brief-validate.js +65 -65
  238. package/src/commands/briefing.js +344 -344
  239. package/src/commands/classify.js +256 -256
  240. package/src/commands/cloud.js +1767 -1767
  241. package/src/commands/commit-prepare.js +610 -547
  242. package/src/commands/compress-agents.js +416 -416
  243. package/src/commands/config.js +90 -90
  244. package/src/commands/context-cache.js +90 -90
  245. package/src/commands/context-compact.js +49 -49
  246. package/src/commands/context-health.js +187 -177
  247. package/src/commands/context-load.js +219 -0
  248. package/src/commands/context-monitor.js +163 -163
  249. package/src/commands/context-pack.js +45 -45
  250. package/src/commands/context-search.js +66 -66
  251. package/src/commands/context-trim.js +183 -183
  252. package/src/commands/context-validate.js +91 -91
  253. package/src/commands/design-hybrid-options.js +385 -385
  254. package/src/commands/detect-test-runner.js +55 -55
  255. package/src/commands/dev-resume.js +32 -0
  256. package/src/commands/devlog-export-brains.js +27 -27
  257. package/src/commands/devlog-process.js +294 -294
  258. package/src/commands/devlog-watch.js +131 -131
  259. package/src/commands/doctor.js +123 -123
  260. package/src/commands/dossier-add-research.js +114 -0
  261. package/src/commands/dossier-audit.js +222 -0
  262. package/src/commands/dossier.js +423 -423
  263. package/src/commands/feature-archive.js +513 -513
  264. package/src/commands/feature-close.js +554 -270
  265. package/src/commands/gate-approve.js +198 -198
  266. package/src/commands/gate-check.js +247 -247
  267. package/src/commands/genome-doctor.js +489 -198
  268. package/src/commands/genome-migrate.js +49 -49
  269. package/src/commands/git-guard.js +170 -170
  270. package/src/commands/harness.js +307 -121
  271. package/src/commands/health.js +214 -214
  272. package/src/commands/hooks-emit.js +253 -253
  273. package/src/commands/hooks-install.js +347 -347
  274. package/src/commands/i18n-add.js +56 -56
  275. package/src/commands/implementation-plan.js +367 -367
  276. package/src/commands/info.js +41 -41
  277. package/src/commands/init.js +120 -120
  278. package/src/commands/install.js +162 -111
  279. package/src/commands/learning-auto-promote.js +197 -195
  280. package/src/commands/learning-evolve.js +364 -364
  281. package/src/commands/learning-export.js +103 -103
  282. package/src/commands/learning-rollback.js +164 -164
  283. package/src/commands/learning.js +134 -134
  284. package/src/commands/live.js +2101 -2082
  285. package/src/commands/locale-apply.js +54 -54
  286. package/src/commands/locale-diff.js +25 -25
  287. package/src/commands/mcp-doctor.js +407 -407
  288. package/src/commands/mcp-init.js +373 -373
  289. package/src/commands/memory-archive.js +193 -0
  290. package/src/commands/memory-reflect-commit.js +148 -0
  291. package/src/commands/memory-reflect-prepare.js +97 -0
  292. package/src/commands/memory-restore.js +177 -0
  293. package/src/commands/memory-search.js +135 -0
  294. package/src/commands/memory.js +299 -234
  295. package/src/commands/notify.js +68 -0
  296. package/src/commands/package-e2e.js +273 -273
  297. package/src/commands/parallel-assign.js +483 -483
  298. package/src/commands/parallel-doctor.js +850 -850
  299. package/src/commands/parallel-guard.js +241 -241
  300. package/src/commands/parallel-init.js +311 -311
  301. package/src/commands/parallel-merge.js +299 -299
  302. package/src/commands/parallel-status.js +434 -434
  303. package/src/commands/pattern-detect.js +33 -33
  304. package/src/commands/preflight-context.js +30 -30
  305. package/src/commands/preflight.js +267 -267
  306. package/src/commands/pulse-update.js +130 -130
  307. package/src/commands/qa-doctor.js +185 -185
  308. package/src/commands/qa-init.js +166 -166
  309. package/src/commands/qa-report.js +58 -58
  310. package/src/commands/qa-run.js +873 -873
  311. package/src/commands/qa-scan.js +337 -337
  312. package/src/commands/recovery.js +43 -43
  313. package/src/commands/revision.js +235 -235
  314. package/src/commands/runner-daemon.js +274 -274
  315. package/src/commands/runner-plan.js +70 -70
  316. package/src/commands/runner-queue-from-plan.js +166 -166
  317. package/src/commands/runner-queue.js +189 -189
  318. package/src/commands/runner-run.js +129 -129
  319. package/src/commands/runtime.js +2086 -2067
  320. package/src/commands/sandbox.js +37 -37
  321. package/src/commands/scaffold-complete.js +188 -188
  322. package/src/commands/scan-project.js +1371 -1371
  323. package/src/commands/scout-commit.js +163 -0
  324. package/src/commands/scout-prep.js +214 -0
  325. package/src/commands/scout-validate.js +112 -0
  326. package/src/commands/security-audit.js +275 -275
  327. package/src/commands/security-scan.js +376 -376
  328. package/src/commands/self-implement-loop.js +306 -300
  329. package/src/commands/session-guard.js +218 -218
  330. package/src/commands/setup-context.js +699 -699
  331. package/src/commands/setup.js +178 -178
  332. package/src/commands/sizing.js +165 -165
  333. package/src/commands/skill.js +670 -670
  334. package/src/commands/smoke.js +426 -426
  335. package/src/commands/spec-checkpoint.js +177 -177
  336. package/src/commands/spec-status.js +79 -79
  337. package/src/commands/spec-sync.js +190 -190
  338. package/src/commands/spec-tasks.js +288 -288
  339. package/src/commands/squad-agent-create.js +830 -830
  340. package/src/commands/squad-autorun.js +1220 -1220
  341. package/src/commands/squad-bus.js +217 -217
  342. package/src/commands/squad-card.js +149 -149
  343. package/src/commands/squad-daemon.js +343 -343
  344. package/src/commands/squad-dashboard.js +39 -39
  345. package/src/commands/squad-dependency-graph.js +164 -164
  346. package/src/commands/squad-deploy.js +64 -64
  347. package/src/commands/squad-doctor.js +460 -460
  348. package/src/commands/squad-export.js +77 -46
  349. package/src/commands/squad-investigate.js +314 -314
  350. package/src/commands/squad-learning.js +209 -209
  351. package/src/commands/squad-mcp.js +270 -270
  352. package/src/commands/squad-pipeline.js +343 -343
  353. package/src/commands/squad-plan.js +361 -361
  354. package/src/commands/squad-processes.js +56 -56
  355. package/src/commands/squad-recovery.js +42 -42
  356. package/src/commands/squad-repair-genomes.js +39 -39
  357. package/src/commands/squad-review.js +106 -106
  358. package/src/commands/squad-roi.js +291 -291
  359. package/src/commands/squad-scaffold.js +56 -56
  360. package/src/commands/squad-score.js +311 -307
  361. package/src/commands/squad-status.js +481 -481
  362. package/src/commands/squad-tool-register.js +157 -157
  363. package/src/commands/squad-validate.js +438 -438
  364. package/src/commands/squad-webhook.js +160 -160
  365. package/src/commands/squad-worker.js +191 -191
  366. package/src/commands/squad-worktrees.js +75 -75
  367. package/src/commands/state-save.js +220 -122
  368. package/src/commands/store-genome.js +667 -304
  369. package/src/commands/store-skill.js +247 -247
  370. package/src/commands/store-squad.js +431 -431
  371. package/src/commands/store-system.js +392 -392
  372. package/src/commands/sync-agents-preflight.js +176 -0
  373. package/src/commands/test-agents.js +199 -199
  374. package/src/commands/tool-capabilities.js +63 -63
  375. package/src/commands/tool-registry-cmd.js +232 -232
  376. package/src/commands/update.js +68 -64
  377. package/src/commands/verify-gate.js +612 -612
  378. package/src/commands/web-map.js +70 -70
  379. package/src/commands/web-scrape.js +71 -71
  380. package/src/commands/workflow-execute.js +730 -730
  381. package/src/commands/workflow-harden.js +231 -231
  382. package/src/commands/workflow-heal.js +136 -136
  383. package/src/commands/workflow-next.js +1279 -1039
  384. package/src/commands/workflow-plan.js +108 -108
  385. package/src/commands/workflow-status.js +440 -440
  386. package/src/commands/workspace.js +144 -144
  387. package/src/constants.js +417 -384
  388. package/src/context-cache.js +159 -159
  389. package/src/context-memory.js +975 -966
  390. package/src/context-parse-reason.js +22 -22
  391. package/src/context-search.js +326 -326
  392. package/src/context-writer.js +197 -197
  393. package/src/context.js +247 -247
  394. package/src/delivery-runner.js +319 -319
  395. package/src/design-variation-catalog.js +503 -503
  396. package/src/detector.js +261 -261
  397. package/src/doctor.js +812 -329
  398. package/src/dossier/codemap-store.js +267 -267
  399. package/src/dossier/dossier-bootstrap.js +222 -222
  400. package/src/dossier/dossier-compact.js +159 -159
  401. package/src/dossier/lock.js +128 -128
  402. package/src/dossier/research-index-store.js +233 -0
  403. package/src/dossier/revision-store.js +313 -313
  404. package/src/dossier/schema.js +162 -155
  405. package/src/dossier/scout-section.js +127 -0
  406. package/src/dossier/store.js +406 -400
  407. package/src/execution-gateway.js +464 -464
  408. package/src/friction-scanner.js +202 -202
  409. package/src/gateway-pointer-merge.js +101 -0
  410. package/src/genome-files.js +198 -198
  411. package/src/genome-format.js +442 -442
  412. package/src/genome-schema.js +238 -238
  413. package/src/genomes/bindings.js +281 -281
  414. package/src/genomes.js +500 -500
  415. package/src/handoff-contract.js +417 -363
  416. package/src/handoff-validator.js +45 -45
  417. package/src/harness/circuit-breaker.js +135 -135
  418. package/src/i18n/index.js +103 -103
  419. package/src/i18n/messages/en.js +1548 -1434
  420. package/src/i18n/messages/es.js +1332 -1221
  421. package/src/i18n/messages/fr.js +1340 -1229
  422. package/src/i18n/messages/pt-BR.js +1568 -1457
  423. package/src/i18n/scaffold.js +64 -64
  424. package/src/install-animation.js +260 -260
  425. package/src/install-profile.js +127 -127
  426. package/src/install-wizard.js +475 -475
  427. package/src/installer-config-merge.js +207 -0
  428. package/src/installer.js +487 -358
  429. package/src/jargon-leak-doctor.js +257 -0
  430. package/src/learning-loop-archive.js +595 -0
  431. package/src/learning-loop-doctor.js +217 -0
  432. package/src/learning-loop-engine.js +254 -0
  433. package/src/learning-loop-fts5.js +132 -0
  434. package/src/learning-loop-migration.js +163 -0
  435. package/src/lib/dev-resume.js +140 -0
  436. package/src/lib/dossier-telemetry.js +36 -0
  437. package/src/lib/genomes/compat.js +206 -206
  438. package/src/lib/genomes/migrate.js +90 -90
  439. package/src/lib/git-commit-guard.js +751 -691
  440. package/src/lib/health-check.js +158 -158
  441. package/src/lib/hook-protocol.js +76 -76
  442. package/src/lib/llm-content-sanitizer.js +44 -0
  443. package/src/lib/security/artifact-reader.js +167 -167
  444. package/src/lib/security/exit-codes.js +51 -51
  445. package/src/lib/security/findings-writer.js +176 -176
  446. package/src/lib/security/runtime-events.js +77 -77
  447. package/src/lib/security/secrets-regex.js +115 -115
  448. package/src/lib/squads/genome-repair.js +49 -49
  449. package/src/lib/store/security-scan.js +175 -173
  450. package/src/lib/terminal-checkbox.js +135 -130
  451. package/src/lib/terminal-picker.js +447 -0
  452. package/src/lib/tmux-launcher.js +163 -163
  453. package/src/lib/tool-capabilities.js +102 -102
  454. package/src/lib/webhook-server.js +328 -328
  455. package/src/locales.js +88 -88
  456. package/src/mcp/apps/squad-dashboard/app.js +163 -163
  457. package/src/mcp/apps/squad-dashboard/index.html +261 -261
  458. package/src/mcp/apps/squad-dashboard/mcp-manifest.json +23 -23
  459. package/src/mcp/resources/squad-state.js +130 -130
  460. package/src/mcp-connectors/registry.js +602 -602
  461. package/src/memory-reflect-engine.js +359 -0
  462. package/src/migrations/profile-rename.js +66 -0
  463. package/src/notify-renderer.js +32 -0
  464. package/src/onboarding.js +307 -305
  465. package/src/parallel-workspace.js +756 -756
  466. package/src/parser.js +74 -66
  467. package/src/path-guard.js +47 -47
  468. package/src/permissions-generator.js +400 -0
  469. package/src/preflight-engine.js +654 -654
  470. package/src/prompt-tool.js +20 -20
  471. package/src/qa-html-report.js +472 -472
  472. package/src/recovery-context-session.js +154 -154
  473. package/src/runner/cascade.js +97 -97
  474. package/src/runner/cli-launcher.js +109 -109
  475. package/src/runner/plan-importer.js +63 -63
  476. package/src/runner/queue-store.js +159 -159
  477. package/src/runtime-store.js +2720 -2676
  478. package/src/sandbox.js +194 -177
  479. package/src/self-healing.js +142 -142
  480. package/src/session-handoff.js +295 -187
  481. package/src/squad/agent-teams-adapter.js +270 -264
  482. package/src/squad/brief-validator.js +350 -350
  483. package/src/squad/bus-bridge.js +140 -140
  484. package/src/squad/context-compactor.js +265 -265
  485. package/src/squad/cross-ai-synthesizer.js +250 -250
  486. package/src/squad/external-session.js +180 -180
  487. package/src/squad/hooks-generator.js +196 -196
  488. package/src/squad/inter-squad-events.js +175 -175
  489. package/src/squad/inter-squad.js +74 -74
  490. package/src/squad/intra-bus.js +345 -345
  491. package/src/squad/learning-extractor.js +213 -213
  492. package/src/squad/pattern-detector.js +365 -365
  493. package/src/squad/preflight-context.js +296 -296
  494. package/src/squad/recovery-context.js +372 -372
  495. package/src/squad/reflection.js +365 -365
  496. package/src/squad/squad-scaffold.js +341 -341
  497. package/src/squad/state-manager.js +310 -310
  498. package/src/squad/task-decomposer.js +652 -652
  499. package/src/squad/verify-gate.js +303 -303
  500. package/src/squad/worktree-manager.js +114 -114
  501. package/src/squad-daemon.js +490 -490
  502. package/src/squad-dashboard/api.js +223 -223
  503. package/src/squad-dashboard/attachment-handler.js +93 -93
  504. package/src/squad-dashboard/context-monitor.js +157 -157
  505. package/src/squad-dashboard/execution-logs.js +115 -115
  506. package/src/squad-dashboard/hunk-review.js +209 -209
  507. package/src/squad-dashboard/metrics.js +133 -133
  508. package/src/squad-dashboard/process-monitor.js +125 -125
  509. package/src/squad-dashboard/renderer.js +858 -858
  510. package/src/squad-dashboard/server.js +232 -232
  511. package/src/squad-dashboard/styles.js +525 -525
  512. package/src/squad-dashboard/token-tracker.js +99 -99
  513. package/src/squads/apply-genome.js +21 -21
  514. package/src/squads/genome-binding-service.js +154 -154
  515. package/src/sub-task-engine.js +415 -0
  516. package/src/sub-task-schemas.js +150 -0
  517. package/src/sub-task-state.js +152 -0
  518. package/src/sub-task-telemetry.js +69 -0
  519. package/src/test-briefing.js +226 -226
  520. package/src/tool-executor.js +94 -94
  521. package/src/updater.js +52 -39
  522. package/src/utils.js +49 -49
  523. package/src/version.js +50 -50
  524. package/src/web.js +284 -284
  525. package/src/worker-runner.js +541 -524
  526. package/src/workflow-gates.js +185 -185
  527. package/template/.aioson/advisors/.gitkeep +1 -1
  528. package/template/.aioson/agents/analyst.md +345 -318
  529. package/template/.aioson/agents/architect.md +325 -305
  530. package/template/.aioson/agents/{cypher.md → briefing.md} +264 -252
  531. package/template/.aioson/agents/committer.md +161 -161
  532. package/template/.aioson/agents/copywriter.md +937 -463
  533. package/template/.aioson/agents/design-hybrid-forge.md +141 -141
  534. package/template/.aioson/agents/dev.md +298 -263
  535. package/template/.aioson/agents/deyvin.md +200 -87
  536. package/template/.aioson/agents/discover.md +235 -235
  537. package/template/.aioson/agents/discovery-design-doc.md +56 -29
  538. package/template/.aioson/agents/genome.md +1904 -364
  539. package/template/.aioson/agents/manifests/analyst.manifest.json +26 -26
  540. package/template/.aioson/agents/manifests/architect.manifest.json +23 -23
  541. package/template/.aioson/agents/manifests/committer.manifest.json +23 -23
  542. package/template/.aioson/agents/manifests/dev.manifest.json +54 -37
  543. package/template/.aioson/agents/manifests/deyvin.manifest.json +41 -0
  544. package/template/.aioson/agents/manifests/orchestrator.manifest.json +30 -30
  545. package/template/.aioson/agents/manifests/pentester.manifest.json +39 -39
  546. package/template/.aioson/agents/manifests/pm.manifest.json +26 -26
  547. package/template/.aioson/agents/manifests/product.manifest.json +23 -23
  548. package/template/.aioson/agents/manifests/qa.manifest.json +41 -25
  549. package/template/.aioson/agents/manifests/setup.manifest.json +20 -20
  550. package/template/.aioson/agents/manifests/ux-ui.manifest.json +24 -24
  551. package/template/.aioson/agents/neo.md +356 -231
  552. package/template/.aioson/agents/orache.md +430 -430
  553. package/template/.aioson/agents/orchestrator.md +274 -263
  554. package/template/.aioson/agents/pair.md +5 -5
  555. package/template/.aioson/agents/pentester.md +289 -235
  556. package/template/.aioson/agents/pm.md +141 -130
  557. package/template/.aioson/agents/product.md +367 -273
  558. package/template/.aioson/agents/profiler-enricher.md +331 -331
  559. package/template/.aioson/agents/profiler-forge.md +212 -212
  560. package/template/.aioson/agents/profiler-researcher.md +282 -282
  561. package/template/.aioson/agents/qa.md +432 -342
  562. package/template/.aioson/agents/setup.md +425 -423
  563. package/template/.aioson/agents/sheldon.md +259 -197
  564. package/template/.aioson/agents/site-forge.md +281 -281
  565. package/template/.aioson/agents/squad.md +160 -156
  566. package/template/.aioson/agents/tester.md +536 -473
  567. package/template/.aioson/agents/ux-ui.md +195 -162
  568. package/template/.aioson/agents/validator.md +101 -69
  569. package/template/.aioson/brains/README.md +132 -128
  570. package/template/.aioson/brains/_archived/.gitkeep +0 -0
  571. package/template/.aioson/brains/_index.json +34 -16
  572. package/template/.aioson/brains/dev/patterns.brain.json +79 -0
  573. package/template/.aioson/brains/scripts/query.js +107 -107
  574. package/template/.aioson/brains/sheldon/architecture-decisions.brain.json +79 -0
  575. package/template/.aioson/brains/site-forge/visual-patterns.brain.json +205 -205
  576. package/template/.aioson/config/autonomy-protocol.json +125 -43
  577. package/template/.aioson/config/learning-loop.json +10 -0
  578. package/template/.aioson/config/scout-engine.json +1 -0
  579. package/template/.aioson/config.md +410 -410
  580. package/template/.aioson/context/_archived/.gitkeep +0 -0
  581. package/template/.aioson/context/design-doc.md +136 -136
  582. package/template/.aioson/context/project-map.md +57 -57
  583. package/template/.aioson/context/project-pulse.md +34 -34
  584. package/template/.aioson/context/seeds/seed-example.md +27 -27
  585. package/template/.aioson/context/spec.md.template +54 -54
  586. package/template/.aioson/context/user-profile.md +42 -42
  587. package/template/.aioson/design-docs/code-reuse.md +48 -48
  588. package/template/.aioson/design-docs/componentization.md +47 -47
  589. package/template/.aioson/design-docs/file-size.md +52 -52
  590. package/template/.aioson/design-docs/folder-structure.md +51 -51
  591. package/template/.aioson/design-docs/naming.md +54 -54
  592. package/template/.aioson/docs/LAYERS.md +89 -89
  593. package/template/.aioson/docs/README.md +76 -76
  594. package/template/.aioson/docs/autonomy-protocol.md +80 -0
  595. package/template/.aioson/docs/briefing/briefing-craft.md +237 -0
  596. package/template/.aioson/docs/dev/execution-discipline.md +106 -106
  597. package/template/.aioson/docs/dev/stack-conventions.md +83 -83
  598. package/template/.aioson/docs/deyvin/continuity-recovery.md +57 -57
  599. package/template/.aioson/docs/deyvin/debugging-escalation.md +30 -30
  600. package/template/.aioson/docs/deyvin/pair-execution.md +44 -44
  601. package/template/.aioson/docs/deyvin/runtime-handoffs.md +42 -36
  602. package/template/.aioson/docs/example-external-api-context.md +72 -72
  603. package/template/.aioson/docs/handoff-persistence.md +94 -0
  604. package/template/.aioson/docs/pentester/app-playbooks.md +206 -0
  605. package/template/.aioson/docs/pentester/llm-supplychain.md +165 -0
  606. package/template/.aioson/docs/product/conversation-playbook.md +116 -116
  607. package/template/.aioson/docs/product/prd-contract.md +107 -107
  608. package/template/.aioson/docs/product/quality-lens.md +57 -57
  609. package/template/.aioson/docs/product/research-loop.md +65 -65
  610. package/template/.aioson/docs/sheldon/enrichment-paths.md +134 -134
  611. package/template/.aioson/docs/sheldon/harness-contract.md +118 -0
  612. package/template/.aioson/docs/sheldon/quality-lens.md +57 -57
  613. package/template/.aioson/docs/sheldon/research-loop.md +56 -56
  614. package/template/.aioson/docs/sheldon/web-intelligence.md +75 -75
  615. package/template/.aioson/docs/site-forge-build.md +195 -195
  616. package/template/.aioson/docs/site-forge-extraction.md +135 -135
  617. package/template/.aioson/docs/site-forge-qa.md +155 -155
  618. package/template/.aioson/docs/site-forge-recon.md +434 -434
  619. package/template/.aioson/docs/site-forge-transform.md +249 -249
  620. package/template/.aioson/docs/squad/content-output.md +91 -91
  621. package/template/.aioson/docs/squad/creation-flow.md +149 -135
  622. package/template/.aioson/docs/squad/domain-breadth.md +322 -0
  623. package/template/.aioson/docs/squad/domain-classification.md +117 -117
  624. package/template/.aioson/docs/squad/genome-bindings.md +47 -47
  625. package/template/.aioson/docs/squad/package-contract.md +260 -234
  626. package/template/.aioson/docs/squad/quality-lens.md +60 -56
  627. package/template/.aioson/docs/squad/research-loop.md +59 -59
  628. package/template/.aioson/docs/squad/session-operations.md +117 -117
  629. package/template/.aioson/docs/squad/workflow-quality.md +165 -165
  630. package/template/.aioson/docs/tester/coverage-quality.md +351 -0
  631. package/template/.aioson/docs/ux-ui/accessibility-audit.md +55 -55
  632. package/template/.aioson/docs/ux-ui/audit-mode.md +86 -86
  633. package/template/.aioson/docs/ux-ui/component-map.md +35 -35
  634. package/template/.aioson/docs/ux-ui/design-execution.md +111 -111
  635. package/template/.aioson/docs/ux-ui/design-gate.md +27 -27
  636. package/template/.aioson/docs/ux-ui/research-mode.md +39 -39
  637. package/template/.aioson/docs/ux-ui/site-delivery.md +156 -156
  638. package/template/.aioson/docs/ux-ui/token-contract.md +57 -57
  639. package/template/.aioson/genomes/INDEX.md +195 -0
  640. package/template/.aioson/genomes/copywriting/SKILL.md +137 -0
  641. package/template/.aioson/genomes/copywriting/manifest.json +140 -0
  642. package/template/.aioson/genomes/copywriting/references/application-notes.md +145 -0
  643. package/template/.aioson/genomes/copywriting/references/decision-weights.md +45 -0
  644. package/template/.aioson/genomes/copywriting/references/frameworks/5-act-narrative.md +184 -0
  645. package/template/.aioson/genomes/copywriting/references/frameworks/classical-formulas.md +164 -0
  646. package/template/.aioson/genomes/copywriting/references/frameworks/offer-stack.md +195 -0
  647. package/template/.aioson/genomes/copywriting/references/frameworks/one-belief.md +135 -0
  648. package/template/.aioson/genomes/copywriting/references/frameworks/pms-research.md +211 -0
  649. package/template/.aioson/genomes/copywriting/references/frameworks/two-paths-close.md +190 -0
  650. package/template/.aioson/genomes/copywriting/references/heuristics.md +114 -0
  651. package/template/.aioson/genomes/copywriting/references/meta-axioms.md +68 -0
  652. package/template/.aioson/genomes/copywriting/references/methodology.md +115 -0
  653. package/template/.aioson/genomes/copywriting-brunson/SKILL.md +133 -0
  654. package/template/.aioson/genomes/copywriting-brunson/manifest.json +152 -0
  655. package/template/.aioson/genomes/copywriting-brunson/references/application-notes.md +113 -0
  656. package/template/.aioson/genomes/copywriting-brunson/references/decision-weights.md +33 -0
  657. package/template/.aioson/genomes/copywriting-brunson/references/evidence-and-attribution.md +81 -0
  658. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/6-part-structure.md +136 -0
  659. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/origin-story.md +121 -0
  660. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/perfect-webinar-script.md +139 -0
  661. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/persuasive-storytelling-5-structures.md +164 -0
  662. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/value-stack.md +136 -0
  663. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/who-what-why-how.md +110 -0
  664. package/template/.aioson/genomes/copywriting-brunson/references/meta-axioms.md +36 -0
  665. package/template/.aioson/genomes/copywriting-brunson/references/methodology.md +112 -0
  666. package/template/.aioson/git-guard.json +12 -11
  667. package/template/.aioson/mcp/servers.md +23 -23
  668. package/template/.aioson/profiler-reports/.gitkeep +1 -1
  669. package/template/.aioson/rules/README.md +69 -69
  670. package/template/.aioson/rules/_archived/.gitkeep +0 -0
  671. package/template/.aioson/rules/agent-language-policy.md +93 -93
  672. package/template/.aioson/rules/aioson-context-boundary.md +63 -63
  673. package/template/.aioson/rules/canonical-path-contract.md +47 -47
  674. package/template/.aioson/rules/data-format-convention.md +74 -74
  675. package/template/.aioson/rules/disk-first-artifacts.md +44 -44
  676. package/template/.aioson/rules/example-monetary-values.md +30 -30
  677. package/template/.aioson/rules/output-brevity.md +44 -44
  678. package/template/.aioson/rules/prd-section-ownership.md +49 -49
  679. package/template/.aioson/rules/security-baseline.md +139 -139
  680. package/template/.aioson/rules/spec-level-ownership.md +61 -61
  681. package/template/.aioson/rules/squad/README.md +50 -50
  682. package/template/.aioson/rules/squad-driver-pattern.md +81 -81
  683. package/template/.aioson/schemas/content-blueprint.schema.json +30 -30
  684. package/template/.aioson/schemas/genome-meta.schema.json +150 -150
  685. package/template/.aioson/schemas/genome.schema.json +115 -115
  686. package/template/.aioson/schemas/readiness.schema.json +27 -27
  687. package/template/.aioson/schemas/squad-blueprint.schema.json +228 -228
  688. package/template/.aioson/schemas/squad-manifest.schema.json +874 -874
  689. package/template/.aioson/skills/design/aurora-command-ui/SKILL.md +243 -243
  690. package/template/.aioson/skills/design/aurora-command-ui/references/art-direction.md +293 -293
  691. package/template/.aioson/skills/design/aurora-command-ui/references/components.md +827 -827
  692. package/template/.aioson/skills/design/aurora-command-ui/references/dashboards.md +250 -250
  693. package/template/.aioson/skills/design/aurora-command-ui/references/design-tokens.md +585 -585
  694. package/template/.aioson/skills/design/aurora-command-ui/references/motion.md +365 -365
  695. package/template/.aioson/skills/design/aurora-command-ui/references/patterns.md +482 -482
  696. package/template/.aioson/skills/design/aurora-command-ui/references/websites.md +387 -387
  697. package/template/.aioson/skills/design/bold-editorial-ui/SKILL.md +205 -205
  698. package/template/.aioson/skills/design/bold-editorial-ui/references/art-direction.md +338 -338
  699. package/template/.aioson/skills/design/bold-editorial-ui/references/components.md +977 -977
  700. package/template/.aioson/skills/design/bold-editorial-ui/references/dashboards.md +218 -218
  701. package/template/.aioson/skills/design/bold-editorial-ui/references/design-tokens.md +326 -326
  702. package/template/.aioson/skills/design/bold-editorial-ui/references/motion.md +461 -461
  703. package/template/.aioson/skills/design/bold-editorial-ui/references/patterns.md +293 -293
  704. package/template/.aioson/skills/design/bold-editorial-ui/references/websites.md +352 -352
  705. package/template/.aioson/skills/design/clean-saas-ui/SKILL.md +210 -210
  706. package/template/.aioson/skills/design/clean-saas-ui/references/art-direction.md +319 -319
  707. package/template/.aioson/skills/design/clean-saas-ui/references/components.md +365 -365
  708. package/template/.aioson/skills/design/clean-saas-ui/references/dashboards.md +196 -196
  709. package/template/.aioson/skills/design/clean-saas-ui/references/design-tokens.md +244 -244
  710. package/template/.aioson/skills/design/clean-saas-ui/references/motion.md +235 -235
  711. package/template/.aioson/skills/design/clean-saas-ui/references/patterns.md +215 -215
  712. package/template/.aioson/skills/design/clean-saas-ui/references/websites.md +295 -295
  713. package/template/.aioson/skills/design/cognitive-core-ui/SKILL.md +203 -203
  714. package/template/.aioson/skills/design/cognitive-core-ui/references/art-direction.md +339 -339
  715. package/template/.aioson/skills/design/cognitive-core-ui/references/components.md +407 -407
  716. package/template/.aioson/skills/design/cognitive-core-ui/references/dashboards.md +272 -272
  717. package/template/.aioson/skills/design/cognitive-core-ui/references/design-tokens.md +524 -524
  718. package/template/.aioson/skills/design/cognitive-core-ui/references/motion.md +279 -279
  719. package/template/.aioson/skills/design/cognitive-core-ui/references/patterns.md +289 -289
  720. package/template/.aioson/skills/design/cognitive-core-ui/references/websites.md +437 -437
  721. package/template/.aioson/skills/design/glassmorphism-ui/SKILL.md +222 -222
  722. package/template/.aioson/skills/design/glassmorphism-ui/references/art-direction.md +159 -159
  723. package/template/.aioson/skills/design/glassmorphism-ui/references/components.md +498 -498
  724. package/template/.aioson/skills/design/glassmorphism-ui/references/dashboards.md +236 -236
  725. package/template/.aioson/skills/design/glassmorphism-ui/references/design-tokens.md +274 -274
  726. package/template/.aioson/skills/design/glassmorphism-ui/references/motion.md +355 -355
  727. package/template/.aioson/skills/design/glassmorphism-ui/references/patterns.md +198 -198
  728. package/template/.aioson/skills/design/glassmorphism-ui/references/websites.md +307 -307
  729. package/template/.aioson/skills/design/interface-design/SKILL.md +47 -47
  730. package/template/.aioson/skills/design/interface-design/references/components-and-states.md +105 -105
  731. package/template/.aioson/skills/design/interface-design/references/design-directions.md +101 -101
  732. package/template/.aioson/skills/design/interface-design/references/handoff-and-quality.md +71 -71
  733. package/template/.aioson/skills/design/interface-design/references/intent-and-domain.md +74 -74
  734. package/template/.aioson/skills/design/interface-design/references/tokens-and-depth.md +173 -173
  735. package/template/.aioson/skills/design/neo-brutalist-ui/SKILL.md +213 -213
  736. package/template/.aioson/skills/design/neo-brutalist-ui/references/art-direction.md +228 -228
  737. package/template/.aioson/skills/design/neo-brutalist-ui/references/components.md +855 -855
  738. package/template/.aioson/skills/design/neo-brutalist-ui/references/dashboards.md +334 -334
  739. package/template/.aioson/skills/design/neo-brutalist-ui/references/design-tokens.md +342 -342
  740. package/template/.aioson/skills/design/neo-brutalist-ui/references/motion.md +286 -286
  741. package/template/.aioson/skills/design/neo-brutalist-ui/references/patterns.md +458 -458
  742. package/template/.aioson/skills/design/neo-brutalist-ui/references/websites.md +723 -723
  743. package/template/.aioson/skills/design/premium-command-center-ui/SKILL.md +62 -62
  744. package/template/.aioson/skills/design/premium-command-center-ui/references/operations.md +74 -74
  745. package/template/.aioson/skills/design/premium-command-center-ui/references/patterns.md +116 -116
  746. package/template/.aioson/skills/design/premium-command-center-ui/references/validation.md +47 -47
  747. package/template/.aioson/skills/design/premium-command-center-ui/references/visual-system.md +215 -215
  748. package/template/.aioson/skills/design/pt.squarespace.com/.skill-meta.json +31 -31
  749. package/template/.aioson/skills/design/pt.squarespace.com/SKILL.md +66 -66
  750. package/template/.aioson/skills/design/pt.squarespace.com/references/components.md +368 -368
  751. package/template/.aioson/skills/design/pt.squarespace.com/references/design-tokens.md +150 -150
  752. package/template/.aioson/skills/design/pt.squarespace.com/references/motion.md +270 -270
  753. package/template/.aioson/skills/design/pt.squarespace.com/references/patterns.md +189 -189
  754. package/template/.aioson/skills/design/pt.squarespace.com/references/websites.md +165 -165
  755. package/template/.aioson/skills/design/warm-craft-ui/SKILL.md +209 -209
  756. package/template/.aioson/skills/design/warm-craft-ui/references/art-direction.md +324 -324
  757. package/template/.aioson/skills/design/warm-craft-ui/references/components.md +508 -508
  758. package/template/.aioson/skills/design/warm-craft-ui/references/dashboards.md +223 -223
  759. package/template/.aioson/skills/design/warm-craft-ui/references/design-tokens.md +374 -374
  760. package/template/.aioson/skills/design/warm-craft-ui/references/motion.md +356 -356
  761. package/template/.aioson/skills/design/warm-craft-ui/references/patterns.md +288 -288
  762. package/template/.aioson/skills/design/warm-craft-ui/references/websites.md +289 -289
  763. package/template/.aioson/skills/design-system/SKILL.md +92 -92
  764. package/template/.aioson/skills/design-system/components/SKILL.md +274 -274
  765. package/template/.aioson/skills/design-system/dashboards/SKILL.md +184 -184
  766. package/template/.aioson/skills/design-system/foundations/SKILL.md +250 -250
  767. package/template/.aioson/skills/design-system/motion/SKILL.md +197 -197
  768. package/template/.aioson/skills/design-system/patterns/SKILL.md +231 -231
  769. package/template/.aioson/skills/dynamic/README.md +30 -30
  770. package/template/.aioson/skills/dynamic/cardano-docs.md +16 -16
  771. package/template/.aioson/skills/dynamic/ethereum-docs.md +17 -17
  772. package/template/.aioson/skills/dynamic/flux-ui-docs.md +13 -13
  773. package/template/.aioson/skills/dynamic/laravel-docs.md +41 -41
  774. package/template/.aioson/skills/dynamic/npm-packages.md +16 -16
  775. package/template/.aioson/skills/dynamic/solana-docs.md +16 -16
  776. package/template/.aioson/skills/marketing/references/anti-patterns.md +254 -254
  777. package/template/.aioson/skills/marketing/references/cta-matrix.md +361 -0
  778. package/template/.aioson/skills/marketing/references/fascinations.md +192 -192
  779. package/template/.aioson/skills/marketing/references/five-acts.md +248 -248
  780. package/template/.aioson/skills/marketing/references/headline-matrix.md +358 -0
  781. package/template/.aioson/skills/marketing/references/market-intelligence.md +198 -198
  782. package/template/.aioson/skills/marketing/references/offer-structure.md +203 -203
  783. package/template/.aioson/skills/marketing/references/one-belief.md +149 -149
  784. package/template/.aioson/skills/marketing/references/patterns.md +218 -218
  785. package/template/.aioson/skills/marketing/references/platform-constraints.md +337 -0
  786. package/template/.aioson/skills/marketing/references/pms-research.md +193 -193
  787. package/template/.aioson/skills/marketing/vsl-craft.md +385 -385
  788. package/template/.aioson/skills/premium-visual-design/SKILL.md +83 -83
  789. package/template/.aioson/skills/premium-visual-design/components/agent-badge.md +92 -92
  790. package/template/.aioson/skills/premium-visual-design/components/dependency-node.md +102 -102
  791. package/template/.aioson/skills/premium-visual-design/components/mention-autocomplete.md +136 -136
  792. package/template/.aioson/skills/premium-visual-design/components/notification-center.md +136 -136
  793. package/template/.aioson/skills/premium-visual-design/components/review-action-bar.md +188 -188
  794. package/template/.aioson/skills/premium-visual-design/components/team-switcher.md +131 -131
  795. package/template/.aioson/skills/premium-visual-design/patterns/agent-message-thread.md +198 -198
  796. package/template/.aioson/skills/premium-visual-design/patterns/notification-panel.md +275 -275
  797. package/template/.aioson/skills/premium-visual-design/patterns/review-workflow-ui.md +234 -234
  798. package/template/.aioson/skills/premium-visual-design/patterns/task-dependency-graph.md +147 -147
  799. package/template/.aioson/skills/premium-visual-design/tokens/status-extended.md +142 -142
  800. package/template/.aioson/skills/process/aioson-spec-driven/SKILL.md +46 -46
  801. package/template/.aioson/skills/process/aioson-spec-driven/references/analyst.md +30 -30
  802. package/template/.aioson/skills/process/aioson-spec-driven/references/approval-gates.md +109 -109
  803. package/template/.aioson/skills/process/aioson-spec-driven/references/architect.md +23 -23
  804. package/template/.aioson/skills/process/aioson-spec-driven/references/artifact-map.md +44 -44
  805. package/template/.aioson/skills/process/aioson-spec-driven/references/classification-map.md +37 -37
  806. package/template/.aioson/skills/process/aioson-spec-driven/references/dev.md +47 -47
  807. package/template/.aioson/skills/process/aioson-spec-driven/references/deyvin.md +27 -27
  808. package/template/.aioson/skills/process/aioson-spec-driven/references/hardening-lane.md +49 -49
  809. package/template/.aioson/skills/process/aioson-spec-driven/references/maintenance-and-state.md +101 -101
  810. package/template/.aioson/skills/process/aioson-spec-driven/references/pm.md +30 -30
  811. package/template/.aioson/skills/process/aioson-spec-driven/references/product.md +25 -25
  812. package/template/.aioson/skills/process/aioson-spec-driven/references/qa.md +30 -30
  813. package/template/.aioson/skills/process/aioson-spec-driven/references/sheldon.md +25 -25
  814. package/template/.aioson/skills/process/aioson-spec-driven/references/ui-language.md +75 -75
  815. package/template/.aioson/skills/process/decision-presentation/SKILL.md +119 -0
  816. package/template/.aioson/skills/process/decision-presentation/references/jargon-map.en.yaml +108 -0
  817. package/template/.aioson/skills/process/decision-presentation/references/jargon-map.pt-BR.yaml +108 -0
  818. package/template/.aioson/skills/process/design-hybrid-forge/SKILL.md +147 -147
  819. package/template/.aioson/skills/process/design-hybrid-forge/references/crossover-protocol.md +221 -221
  820. package/template/.aioson/skills/process/design-hybrid-forge/references/naming-registry.md +88 -88
  821. package/template/.aioson/skills/process/design-hybrid-forge/references/output-contract.md +306 -306
  822. package/template/.aioson/skills/process/design-hybrid-forge/references/pair-compatibility.md +149 -149
  823. package/template/.aioson/skills/process/design-hybrid-forge/references/quality-gates.md +208 -208
  824. package/template/.aioson/skills/process/design-hybrid-forge/references/variation-library.md +125 -125
  825. package/template/.aioson/skills/process/secure-tdd/SKILL.md +97 -97
  826. package/template/.aioson/skills/process/simplify/SKILL.md +173 -173
  827. package/template/.aioson/skills/references/premium-command-center-ui/master-application-prompt.md +79 -79
  828. package/template/.aioson/skills/references/premium-command-center-ui/operational-ux-playbook.md +253 -253
  829. package/template/.aioson/skills/references/premium-command-center-ui/quality-validation-checklist.md +82 -82
  830. package/template/.aioson/skills/references/premium-command-center-ui/visual-system-and-component-patterns.md +270 -270
  831. package/template/.aioson/skills/squad/SKILL.md +58 -58
  832. package/template/.aioson/skills/squad/formats/catalog.json +15 -15
  833. package/template/.aioson/skills/squad/formats/content/blog-post.md +47 -47
  834. package/template/.aioson/skills/squad/formats/content/newsletter.md +47 -47
  835. package/template/.aioson/skills/squad/formats/creative/podcast-script.md +43 -43
  836. package/template/.aioson/skills/squad/formats/creative/video-script.md +41 -41
  837. package/template/.aioson/skills/squad/formats/social/instagram-feed.md +42 -42
  838. package/template/.aioson/skills/squad/formats/social/linkedin-post.md +42 -42
  839. package/template/.aioson/skills/squad/formats/social/tiktok.md +39 -39
  840. package/template/.aioson/skills/squad/formats/social/twitter-thread.md +39 -39
  841. package/template/.aioson/skills/squad/formats/social/youtube-long.md +47 -47
  842. package/template/.aioson/skills/squad/formats/social/youtube-shorts.md +39 -39
  843. package/template/.aioson/skills/squad/patterns/multi-platform-pattern.md +108 -108
  844. package/template/.aioson/skills/squad/patterns/persona-based-pattern.md +98 -98
  845. package/template/.aioson/skills/squad/patterns/pipeline-pattern.md +106 -106
  846. package/template/.aioson/skills/squad/patterns/review-loop-pattern.md +81 -81
  847. package/template/.aioson/skills/squad/references/checklist-templates.md +122 -122
  848. package/template/.aioson/skills/squad/references/executor-archetypes.md +123 -123
  849. package/template/.aioson/skills/squad/references/workflow-templates.md +169 -169
  850. package/template/.aioson/skills/static/context-budget-guide.md +46 -46
  851. package/template/.aioson/skills/static/debugging-protocol.md +42 -42
  852. package/template/.aioson/skills/static/django-patterns.md +342 -342
  853. package/template/.aioson/skills/static/fastapi-patterns.md +344 -344
  854. package/template/.aioson/skills/static/filament-patterns.md +267 -267
  855. package/template/.aioson/skills/static/flux-ui-components.md +262 -262
  856. package/template/.aioson/skills/static/git-conventions.md +227 -227
  857. package/template/.aioson/skills/static/git-worktrees.md +36 -36
  858. package/template/.aioson/skills/static/harness-sensors.md +74 -74
  859. package/template/.aioson/skills/static/harness-validate/SKILL.md +46 -46
  860. package/template/.aioson/skills/static/jetstream-setup.md +200 -200
  861. package/template/.aioson/skills/static/landing-page-deploy.md +192 -192
  862. package/template/.aioson/skills/static/landing-page-forge.md +730 -730
  863. package/template/.aioson/skills/static/laravel-conventions.md +491 -491
  864. package/template/.aioson/skills/static/multi-agent-patterns.md +43 -43
  865. package/template/.aioson/skills/static/nextjs-patterns.md +321 -321
  866. package/template/.aioson/skills/static/node-express-patterns.md +317 -317
  867. package/template/.aioson/skills/static/node-typescript-patterns.md +282 -282
  868. package/template/.aioson/skills/static/rails-conventions.md +307 -307
  869. package/template/.aioson/skills/static/react-motion-patterns.md +599 -599
  870. package/template/.aioson/skills/static/static-html-patterns/checklists.md +43 -43
  871. package/template/.aioson/skills/static/static-html-patterns/css-tokens.md +609 -609
  872. package/template/.aioson/skills/static/static-html-patterns/motion.md +193 -193
  873. package/template/.aioson/skills/static/static-html-patterns/premium.md +711 -711
  874. package/template/.aioson/skills/static/static-html-patterns/structure.md +209 -209
  875. package/template/.aioson/skills/static/static-html-patterns/utilities.md +190 -190
  876. package/template/.aioson/skills/static/static-html-patterns.md +80 -80
  877. package/template/.aioson/skills/static/tall-stack-patterns.md +286 -286
  878. package/template/.aioson/skills/static/threejs-patterns.md +929 -929
  879. package/template/.aioson/skills/static/ui-ux-modern.md +76 -76
  880. package/template/.aioson/skills/static/web-research-cache.md +115 -115
  881. package/template/.aioson/skills/static/web3-cardano-patterns.md +337 -337
  882. package/template/.aioson/skills/static/web3-ethereum-patterns.md +310 -310
  883. package/template/.aioson/skills/static/web3-security-checklist.md +284 -284
  884. package/template/.aioson/skills/static/web3-solana-patterns.md +324 -324
  885. package/template/.aioson/squads/memory.md +5 -5
  886. package/template/.aioson/tasks/implementation-plan.md +327 -327
  887. package/template/.aioson/tasks/squad-analyze.md +83 -83
  888. package/template/.aioson/tasks/squad-create.md +148 -148
  889. package/template/.aioson/tasks/squad-design.md +206 -206
  890. package/template/.aioson/tasks/squad-execution-plan.md +279 -279
  891. package/template/.aioson/tasks/squad-export.md +20 -20
  892. package/template/.aioson/tasks/squad-extend.md +68 -68
  893. package/template/.aioson/tasks/squad-investigate.md +57 -57
  894. package/template/.aioson/tasks/squad-learning-review.md +44 -44
  895. package/template/.aioson/tasks/squad-output-config.md +177 -177
  896. package/template/.aioson/tasks/squad-pipeline.md +122 -122
  897. package/template/.aioson/tasks/squad-profile.md +48 -48
  898. package/template/.aioson/tasks/squad-refresh.md +236 -0
  899. package/template/.aioson/tasks/squad-repair.md +85 -85
  900. package/template/.aioson/tasks/squad-review.md +61 -61
  901. package/template/.aioson/tasks/squad-task-decompose.md +66 -66
  902. package/template/.aioson/tasks/squad-validate.md +58 -58
  903. package/template/.aioson/templates/reflect-prompts/current-state.md +36 -0
  904. package/template/.aioson/templates/reflect-prompts/how-it-works.md +23 -0
  905. package/template/.aioson/templates/reflect-prompts/what-it-does.md +21 -0
  906. package/template/.aioson/templates/squads/content-basic/template.json +21 -21
  907. package/template/.aioson/templates/squads/digital-marketing-agency/template.json +96 -96
  908. package/template/.aioson/templates/squads/media-channel/template.json +24 -24
  909. package/template/.aioson/templates/squads/research-analysis/template.json +22 -22
  910. package/template/.aioson/templates/squads/software-delivery/template.json +21 -21
  911. package/template/.claude/commands/aioson/agent/analyst.md +5 -5
  912. package/template/.claude/commands/aioson/agent/architect.md +5 -5
  913. package/template/.claude/commands/aioson/agent/briefing.md +5 -0
  914. package/template/.claude/commands/aioson/agent/committer.md +5 -5
  915. package/template/.claude/commands/aioson/agent/copywriter.md +5 -5
  916. package/template/.claude/commands/aioson/agent/design-hybrid-forge.md +5 -5
  917. package/template/.claude/commands/aioson/agent/dev.md +5 -5
  918. package/template/.claude/commands/aioson/agent/deyvin.md +5 -5
  919. package/template/.claude/commands/aioson/agent/discover.md +5 -0
  920. package/template/.claude/commands/aioson/agent/discovery-design-doc.md +5 -5
  921. package/template/.claude/commands/aioson/agent/genome.md +5 -5
  922. package/template/.claude/commands/aioson/agent/neo.md +5 -5
  923. package/template/.claude/commands/aioson/agent/orache.md +5 -5
  924. package/template/.claude/commands/aioson/agent/orchestrator.md +5 -5
  925. package/template/.claude/commands/aioson/agent/pair.md +5 -5
  926. package/template/.claude/commands/aioson/agent/pentester.md +5 -0
  927. package/template/.claude/commands/aioson/agent/pm.md +5 -5
  928. package/template/.claude/commands/aioson/agent/product.md +5 -5
  929. package/template/.claude/commands/aioson/agent/profiler-enricher.md +5 -5
  930. package/template/.claude/commands/aioson/agent/profiler-forge.md +5 -5
  931. package/template/.claude/commands/aioson/agent/profiler-researcher.md +5 -5
  932. package/template/.claude/commands/aioson/agent/qa.md +5 -5
  933. package/template/.claude/commands/aioson/agent/setup.md +5 -5
  934. package/template/.claude/commands/aioson/agent/sheldon.md +5 -5
  935. package/template/.claude/commands/aioson/agent/site-forge.md +5 -5
  936. package/template/.claude/commands/aioson/agent/squad.md +5 -5
  937. package/template/.claude/commands/aioson/agent/tester.md +5 -5
  938. package/template/.claude/commands/aioson/agent/ux-ui.md +5 -5
  939. package/template/.claude/commands/aioson/agent/validator.md +5 -5
  940. package/template/.gemini/GEMINI.md +13 -13
  941. package/template/.gemini/commands/aios-analyst.toml +7 -7
  942. package/template/.gemini/commands/aios-architect.toml +8 -8
  943. package/template/.gemini/commands/aios-committer.toml +7 -7
  944. package/template/.gemini/commands/aios-copywriter.toml +7 -7
  945. package/template/.gemini/commands/aios-cypher.toml +7 -7
  946. package/template/.gemini/commands/aios-dev.toml +9 -9
  947. package/template/.gemini/commands/aios-deyvin.toml +7 -7
  948. package/template/.gemini/commands/aios-discover.toml +6 -0
  949. package/template/.gemini/commands/aios-discovery-design-doc.toml +7 -7
  950. package/template/.gemini/commands/aios-genome.toml +7 -7
  951. package/template/.gemini/commands/aios-neo.toml +6 -6
  952. package/template/.gemini/commands/aios-orache.toml +7 -7
  953. package/template/.gemini/commands/aios-orchestrator.toml +9 -9
  954. package/template/.gemini/commands/aios-pair.toml +7 -7
  955. package/template/.gemini/commands/aios-pm.toml +9 -9
  956. package/template/.gemini/commands/aios-product.toml +6 -6
  957. package/template/.gemini/commands/aios-qa.toml +7 -7
  958. package/template/.gemini/commands/aios-setup.toml +6 -6
  959. package/template/.gemini/commands/aios-sheldon.toml +7 -7
  960. package/template/.gemini/commands/aios-site-forge.toml +7 -7
  961. package/template/.gemini/commands/aios-squad.toml +7 -7
  962. package/template/.gemini/commands/aios-tester.toml +7 -7
  963. package/template/.gemini/commands/aios-ux-ui.toml +9 -9
  964. package/template/.gemini/commands/aios-validator.toml +7 -7
  965. package/template/AGENTS.md +184 -183
  966. package/template/CLAUDE.md +98 -97
  967. package/template/OPENCODE.md +35 -34
  968. package/template/aioson-models.json +40 -40
  969. package/template/.aioson/genomes/copywriting.md +0 -204
  970. package/template/.aioson/genomes/copywriting.meta.json +0 -48
  971. package/template/.aioson/skills/process/secure-tdd/references/nextjs.md +0 -81
  972. package/template/.aioson/skills/process/secure-tdd/references/node-express.md +0 -91
  973. package/template/.aioson/skills/process/secure-tdd/references/planned-stacks.md +0 -33
  974. package/template/.claude/commands/aioson/agent/cypher.md +0 -5
@@ -1,2067 +1,2086 @@
1
- 'use strict';
2
-
3
- const fs = require('node:fs/promises');
4
- const path = require('node:path');
5
- const {
6
- resolveRuntimePaths,
7
- openRuntimeDb,
8
- runtimeStoreExists,
9
- startTask,
10
- updateTask,
11
- startRun,
12
- updateRun,
13
- attachArtifact,
14
- upsertContentItem,
15
- getStatusSnapshot,
16
- logAgentEvent,
17
- appendRunEvent,
18
- readAgentSession,
19
- clearAgentSession
20
- } = require('../runtime-store');
21
- const { runAutoDelivery } = require('../delivery-runner');
22
- const { writeHandoff, buildRuntimeLogHandoff } = require('../session-handoff');
23
- const { backupAiosonDocs, isDocCreatingAgent } = require('../backup-local');
24
-
25
- const ALLOWED_LAYOUTS = new Set(['document', 'tabs', 'accordion', 'stack', 'mixed']);
26
- const DEFAULT_TEXT_FIELDS = ['content', 'text', 'body', 'lyrics', 'markdown'];
27
-
28
- function resolveTargetDir(args) {
29
- return path.resolve(process.cwd(), args[0] || '.');
30
- }
31
-
32
- function requireOption(options, key, t) {
33
- const value = options[key];
34
- if (value === undefined || value === null || String(value).trim() === '') {
35
- throw new Error(t('runtime.option_required', { option: `--${key}` }));
36
- }
37
- return String(value).trim();
38
- }
39
-
40
- function normalizeAgentHandle(value) {
41
- const text = String(value || '').trim();
42
- if (!text) return '';
43
- return text.startsWith('@') ? text : `@${text}`;
44
- }
45
-
46
- function makeDirectSessionKey(agentName) {
47
- return `direct-session:${Date.now()}:${String(agentName || '').replace(/^@/, '')}`;
48
- }
49
-
50
- function parseWatchSeconds(value) {
51
- if (value === undefined || value === null || value === false) return null;
52
- if (value === true || value === '') return 2;
53
-
54
- const parsed = Number(value);
55
- if (!Number.isFinite(parsed) || parsed <= 0) return 2;
56
- return parsed;
57
- }
58
-
59
- function sleep(ms) {
60
- return new Promise((resolve) => setTimeout(resolve, ms));
61
- }
62
-
63
- async function collectRuntimeSessionSnapshot(db, runtimeDir, agentName, options = {}) {
64
- const normalizedAgent = normalizeAgentHandle(agentName);
65
- const eventLimit = Math.max(1, Math.min(Number(options.limit) || 8, 20));
66
- const session = await readAgentSession(runtimeDir, normalizedAgent);
67
- const activeSession = session && !session.finished ? session : null;
68
-
69
- let run = null;
70
- if (activeSession && activeSession.runKey) {
71
- run = db.prepare(`
72
- SELECT
73
- run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
74
- title, status, summary, output_path, started_at, updated_at, finished_at
75
- FROM agent_runs
76
- WHERE run_key = ?
77
- LIMIT 1
78
- `).get(activeSession.runKey);
79
- }
80
-
81
- if (!run) {
82
- run = db.prepare(`
83
- SELECT
84
- run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
85
- title, status, summary, output_path, started_at, updated_at, finished_at
86
- FROM agent_runs
87
- WHERE agent_name = ?
88
- ORDER BY updated_at DESC, started_at DESC
89
- LIMIT 1
90
- `).get(normalizedAgent);
91
- }
92
-
93
- const task = run && run.task_key
94
- ? db.prepare(`
95
- SELECT
96
- task_key, squad_slug, session_key, title, goal, status, created_by, created_at, updated_at, finished_at
97
- FROM tasks
98
- WHERE task_key = ?
99
- LIMIT 1
100
- `).get(run.task_key)
101
- : null;
102
-
103
- const recentEvents = run
104
- ? db.prepare(`
105
- SELECT event_type, phase, status, message, created_at
106
- FROM execution_events
107
- WHERE run_key = ?
108
- ORDER BY created_at DESC, id DESC
109
- LIMIT ?
110
- `).all(run.run_key, eventLimit).reverse()
111
- : [];
112
-
113
- const open = Boolean(activeSession && run && (run.status === 'running' || run.status === 'queued'));
114
- const state = open ? 'open' : (run ? 'closed' : 'idle');
115
-
116
- return {
117
- agent: normalizedAgent,
118
- state,
119
- open,
120
- sessionKey: activeSession?.sessionKey || run?.session_key || task?.session_key || null,
121
- startedAt: activeSession?.startedAt || run?.started_at || task?.created_at || null,
122
- updatedAt: run?.updated_at || task?.updated_at || null,
123
- session: activeSession,
124
- run,
125
- task,
126
- recentEvents
127
- };
128
- }
129
-
130
- async function getRuntimeSessionSnapshot(targetDir, agentName, t, options = {}) {
131
- const { dbPath, runtimeDir } = resolveRuntimePaths(targetDir);
132
-
133
- if (!(await runtimeStoreExists(targetDir))) {
134
- throw new Error(t('runtime.store_missing', { path: dbPath }));
135
- }
136
-
137
- const { db } = await openRuntimeDb(targetDir, { mustExist: true });
138
- try {
139
- const snapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, options);
140
- return {
141
- ok: true,
142
- targetDir,
143
- dbPath,
144
- ...snapshot
145
- };
146
- } finally {
147
- db.close();
148
- }
149
- }
150
-
151
- function printRuntimeSessionSnapshot(snapshot, logger) {
152
- logger.log(`Direct session: ${snapshot.agent}`);
153
- logger.log(`State: ${snapshot.state}`);
154
-
155
- if (snapshot.sessionKey) {
156
- logger.log(`Session: ${snapshot.sessionKey}`);
157
- }
158
-
159
- if (snapshot.task) {
160
- logger.log(`Task: ${snapshot.task.task_key} | status: ${snapshot.task.status} | work: ${snapshot.task.title || '—'}`);
161
- }
162
-
163
- if (snapshot.run) {
164
- logger.log(`Run: ${snapshot.run.run_key} | status: ${snapshot.run.status} | work: ${snapshot.run.title || snapshot.run.summary || '—'}`);
165
- }
166
-
167
- if (snapshot.startedAt) {
168
- logger.log(`Started: ${snapshot.startedAt}`);
169
- }
170
-
171
- if (snapshot.updatedAt) {
172
- logger.log(`Updated: ${snapshot.updatedAt}`);
173
- }
174
-
175
- if (snapshot.recentEvents.length === 0) {
176
- logger.log('Recent events: none');
177
- return;
178
- }
179
-
180
- logger.log('Recent events:');
181
- for (const event of snapshot.recentEvents) {
182
- logger.log(`- ${event.created_at} | ${event.event_type} | ${event.message || '—'}`);
183
- }
184
- }
185
-
186
- async function readJsonIfExists(filePath) {
187
- try {
188
- const raw = await fs.readFile(filePath, 'utf8');
189
- return JSON.parse(raw);
190
- } catch {
191
- return null;
192
- }
193
- }
194
-
195
- function maybeResolveContentPaths(targetDir, outputPath) {
196
- if (!outputPath) return null;
197
-
198
- const relative = String(outputPath).replace(/\\/g, '/').trim();
199
- if (!/\/index\.html?$/i.test(relative)) return null;
200
-
201
- const absoluteHtmlPath = path.isAbsolute(relative) ? relative : path.join(targetDir, relative);
202
- const absoluteJsonPath = path.join(path.dirname(absoluteHtmlPath), 'content.json');
203
-
204
- return {
205
- relativeHtmlPath: path.isAbsolute(relative) ? path.relative(targetDir, absoluteHtmlPath).replace(/\\/g, '/') : relative,
206
- relativeJsonPath: path.relative(targetDir, absoluteJsonPath).replace(/\\/g, '/'),
207
- absoluteJsonPath
208
- };
209
- }
210
-
211
- function asObject(value) {
212
- return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
213
- }
214
-
215
- function asString(value) {
216
- return typeof value === 'string' ? value.trim() : '';
217
- }
218
-
219
- function asArray(value) {
220
- return Array.isArray(value) ? value : [];
221
- }
222
-
223
- function normalizeStringArray(value) {
224
- const values = Array.isArray(value)
225
- ? value
226
- : typeof value === 'string'
227
- ? value.split(',')
228
- : [];
229
-
230
- return Array.from(
231
- new Set(
232
- values
233
- .map((entry) => String(entry || '').trim())
234
- .filter(Boolean)
235
- )
236
- );
237
- }
238
-
239
- function parseJsonArray(value) {
240
- if (!value) return [];
241
- try {
242
- return normalizeStringArray(JSON.parse(value));
243
- } catch {
244
- return [];
245
- }
246
- }
247
-
248
- function titleize(value) {
249
- return String(value || '')
250
- .replace(/\.[^.]+$/, '')
251
- .replace(/[-_]+/g, ' ')
252
- .replace(/\s+/g, ' ')
253
- .trim()
254
- .replace(/\b\w/g, (char) => char.toUpperCase());
255
- }
256
-
257
- function makeContentKey(value) {
258
- return String(value || 'content')
259
- .toLowerCase()
260
- .replace(/[^a-z0-9]+/g, '-')
261
- .replace(/^-+|-+$/g, '')
262
- .slice(0, 80) || `content-${Date.now()}`;
263
- }
264
-
265
- function truncateText(value, max = 12000) {
266
- const text = String(value || '').trim();
267
- if (text.length <= max) return text;
268
- return `${text.slice(0, max).trim()}\n\n[...]`;
269
- }
270
-
271
- function stripHtml(html) {
272
- return String(html || '')
273
- .replace(/<script[\s\S]*?<\/script>/gi, ' ')
274
- .replace(/<style[\s\S]*?<\/style>/gi, ' ')
275
- .replace(/<[^>]+>/g, ' ')
276
- .replace(/\s+/g, ' ')
277
- .trim();
278
- }
279
-
280
- function firstNonEmptyText(record, keys) {
281
- for (const key of keys) {
282
- const value = asString(record[key]);
283
- if (value) return value;
284
- }
285
- return '';
286
- }
287
-
288
- function normalizeSimpleContentBlocks(content) {
289
- const text = firstNonEmptyText(content, DEFAULT_TEXT_FIELDS);
290
- if (text) {
291
- return [
292
- {
293
- type: 'rich-text',
294
- content: truncateText(text)
295
- }
296
- ];
297
- }
298
-
299
- const html = asString(content.html);
300
- if (html) {
301
- const preview = stripHtml(html);
302
- return [
303
- {
304
- type: 'callout',
305
- title: 'Conteudo HTML indexado automaticamente',
306
- content:
307
- 'Este item foi convertido para o indice de conteudos a partir de um arquivo HTML gerado pelo squad.'
308
- },
309
- {
310
- type: 'rich-text',
311
- content: truncateText(preview || 'Nao foi possivel extrair preview textual do HTML.')
312
- }
313
- ];
314
- }
315
-
316
- return [];
317
- }
318
-
319
- function isValidBlock(value) {
320
- const block = asObject(value);
321
- if (!block) return false;
322
-
323
- const type = asString(block.type);
324
- if (!type) return false;
325
-
326
- if (type === 'tabs') {
327
- const items = asArray(block.items);
328
- return items.every((item) => {
329
- const tab = asObject(item);
330
- if (!tab || !asString(tab.label)) return false;
331
- return asArray(tab.blocks).every(isValidBlock);
332
- });
333
- }
334
-
335
- if (type === 'accordion') {
336
- const items = asArray(block.items);
337
- return items.every((item) => {
338
- const entry = asObject(item);
339
- if (!entry || !asString(entry.title)) return false;
340
- const content = asString(entry.content);
341
- const nestedBlocks = asArray(entry.blocks);
342
- if (!content && nestedBlocks.length === 0) return false;
343
- return nestedBlocks.every(isValidBlock);
344
- });
345
- }
346
-
347
- if (type === 'section') {
348
- return asArray(block.blocks).every(isValidBlock);
349
- }
350
-
351
- return true;
352
- }
353
-
354
- function validateContentPayload(payload) {
355
- const content = asObject(payload);
356
- if (!content) {
357
- return { ok: false, reason: 'content.json must be an object' };
358
- }
359
-
360
- const contentKey = asString(content.contentKey || content.content_key);
361
- if (!contentKey) {
362
- return { ok: false, reason: 'content.json is missing contentKey' };
363
- }
364
-
365
- const title = asString(content.title);
366
- if (!title) {
367
- return { ok: false, reason: 'content.json is missing title' };
368
- }
369
-
370
- const contentType = asString(content.contentType || content.content_type);
371
- if (!contentType) {
372
- return { ok: false, reason: 'content.json is missing contentType' };
373
- }
374
-
375
- const layoutType = asString(content.layoutType || content.layout_type || 'document');
376
- if (!ALLOWED_LAYOUTS.has(layoutType)) {
377
- return { ok: false, reason: `content.json has unsupported layoutType: ${layoutType}` };
378
- }
379
-
380
- const blocks = asArray(content.blocks);
381
- const normalizedBlocks = blocks.length > 0 ? blocks : normalizeSimpleContentBlocks(content);
382
-
383
- if (normalizedBlocks.length === 0) {
384
- return { ok: false, reason: 'content.json must include blocks or a simple text field' };
385
- }
386
-
387
- if (!normalizedBlocks.every(isValidBlock)) {
388
- return { ok: false, reason: 'content.json contains invalid blocks' };
389
- }
390
-
391
- return {
392
- ok: true,
393
- normalized: {
394
- ...content,
395
- contentKey,
396
- title,
397
- contentType,
398
- layoutType,
399
- blocks: normalizedBlocks,
400
- blueprint: asString(content.blueprint || content.blueprintSlug || content.blueprint_slug),
401
- usedSkills: normalizeStringArray(
402
- content.usedSkills ||
403
- content.used_skills ||
404
- content.meta?.usedSkills ||
405
- content.meta?.used_skills ||
406
- []
407
- )
408
- }
409
- };
410
- }
411
-
412
- async function listFilesRecursive(rootDir) {
413
- const result = [];
414
- const queue = [rootDir];
415
-
416
- while (queue.length > 0) {
417
- const currentDir = queue.shift();
418
- const entries = await fs.readdir(currentDir, { withFileTypes: true }).catch(() => []);
419
- for (const entry of entries) {
420
- const fullPath = path.join(currentDir, entry.name);
421
- if (entry.isDirectory()) {
422
- queue.push(fullPath);
423
- continue;
424
- }
425
- if (entry.isFile()) {
426
- result.push(fullPath);
427
- }
428
- }
429
- }
430
-
431
- return result;
432
- }
433
-
434
- function inferSquadSlugFromOutputPath(targetDir, absolutePath) {
435
- const outputRoot = path.join(targetDir, 'output');
436
- const relativePath = path.relative(outputRoot, absolutePath).replace(/\\/g, '/');
437
- const [slug] = relativePath.split('/');
438
- return slug || '';
439
- }
440
-
441
- function relativeContentKeyFromOutput(targetDir, absolutePath, squadSlug) {
442
- const squadRoot = path.join(targetDir, 'output', squadSlug);
443
- const relativeToSquad = path.relative(squadRoot, absolutePath).replace(/\\/g, '/');
444
- return makeContentKey(relativeToSquad.replace(/\.[^.]+$/, ''));
445
- }
446
-
447
- function synthesizeContentPayload({ targetDir, absolutePath, squadSlug, rawContent }) {
448
- const ext = path.extname(absolutePath).toLowerCase();
449
- const relativePath = path.relative(targetDir, absolutePath).replace(/\\/g, '/');
450
- const title = titleize(path.basename(absolutePath));
451
- const contentKey = relativeContentKeyFromOutput(targetDir, absolutePath, squadSlug);
452
-
453
- if (ext === '.md') {
454
- return {
455
- contentKey,
456
- title,
457
- contentType: 'text-content',
458
- layoutType: 'document',
459
- summary: `Conteudo indexado automaticamente de ${relativePath}.`,
460
- blocks: [
461
- {
462
- type: 'rich-text',
463
- content: truncateText(rawContent)
464
- }
465
- ],
466
- meta: {
467
- autoIndexed: true,
468
- sourceFormat: 'markdown',
469
- sourcePath: relativePath
470
- }
471
- };
472
- }
473
-
474
- if (ext === '.html' || ext === '.htm') {
475
- const preview = stripHtml(rawContent);
476
- return {
477
- contentKey,
478
- title,
479
- contentType: 'html-content',
480
- layoutType: 'document',
481
- summary: `Conteudo HTML indexado automaticamente de ${relativePath}.`,
482
- blocks: [
483
- {
484
- type: 'callout',
485
- title: 'Preview indexado automaticamente',
486
- content: 'O arquivo HTML original continua no output do squad. Este viewer mostra uma versao textual para indexacao e sync.'
487
- },
488
- {
489
- type: 'rich-text',
490
- content: truncateText(preview || 'Nao foi possivel gerar preview textual deste HTML.')
491
- }
492
- ],
493
- meta: {
494
- autoIndexed: true,
495
- sourceFormat: 'html',
496
- sourcePath: relativePath
497
- }
498
- };
499
- }
500
-
501
- return null;
502
- }
503
-
504
- async function resolveIngestCandidates(targetDir, options = {}) {
505
- const outputRoot = path.join(targetDir, 'output');
506
- const scopedRoot = options.squad ? path.join(outputRoot, String(options.squad).trim()) : outputRoot;
507
- const rootExists = await fs.stat(scopedRoot).then((stat) => stat.isDirectory()).catch(() => false);
508
-
509
- if (!rootExists) {
510
- return [];
511
- }
512
-
513
- const allFiles = await listFilesRecursive(scopedRoot);
514
- const contentJsonDirs = new Set(
515
- allFiles
516
- .filter((filePath) => path.basename(filePath).toLowerCase() === 'content.json')
517
- .map((filePath) => path.dirname(filePath))
518
- );
519
-
520
- return allFiles.filter((filePath) => {
521
- const ext = path.extname(filePath).toLowerCase();
522
- const base = path.basename(filePath).toLowerCase();
523
-
524
- if (base === 'content.json') return true;
525
- if (ext !== '.md' && ext !== '.html' && ext !== '.htm') return false;
526
- if (contentJsonDirs.has(path.dirname(filePath))) return false;
527
-
528
- return true;
529
- });
530
- }
531
-
532
- async function ingestContentCandidate(db, targetDir, absolutePath, options = {}) {
533
- const baseName = path.basename(absolutePath).toLowerCase();
534
- const relativePath = path.relative(targetDir, absolutePath).replace(/\\/g, '/');
535
- const squadSlug = options.squad ? String(options.squad).trim() : inferSquadSlugFromOutputPath(targetDir, absolutePath);
536
-
537
- if (!squadSlug) {
538
- return { indexed: false, reason: 'missing_squad' };
539
- }
540
-
541
- if (baseName === 'content.json') {
542
- const payload = await readJsonIfExists(absolutePath);
543
- const validation = validateContentPayload(payload);
544
- if (!validation.ok) {
545
- return { indexed: false, reason: validation.reason };
546
- }
547
-
548
- const content = validation.normalized;
549
- const siblingIndex = path.join(path.dirname(absolutePath), 'index.html');
550
- const siblingHtmlExists = await fs.stat(siblingIndex).then((stat) => stat.isFile()).catch(() => false);
551
-
552
- upsertContentItem(db, {
553
- contentKey: content.contentKey,
554
- taskKey: options.task || content.taskKey || content.task_key || null,
555
- runKey: options.run || content.runKey || content.run_key || null,
556
- squadSlug,
557
- sessionKey: options.session || content.sessionKey || content.session_key || null,
558
- title: content.title,
559
- contentType: content.contentType,
560
- layoutType: content.layoutType,
561
- status: content.status || 'completed',
562
- summary: content.summary || `Conteudo indexado automaticamente de ${relativePath}.`,
563
- blueprintSlug: content.blueprint || null,
564
- usedSkills: normalizeStringArray(options.usedSkills || content.usedSkills),
565
- payload: content,
566
- jsonPath: relativePath,
567
- htmlPath: siblingHtmlExists
568
- ? path.relative(targetDir, siblingIndex).replace(/\\/g, '/')
569
- : null,
570
- createdByAgent: options.agent || content.createdByAgent || content.created_by_agent || null
571
- });
572
-
573
- // Fire auto-delivery if configured (non-blocking)
574
- runAutoDelivery(db, {
575
- projectDir: targetDir,
576
- squadSlug,
577
- contentKey: content.contentKey,
578
- contentPayload: content
579
- }).catch(() => {}); // Swallow errors — delivery failure should not break ingestion
580
-
581
- return { indexed: true, kind: 'content-json', contentKey: content.contentKey };
582
- }
583
-
584
- const rawContent = await fs.readFile(absolutePath, 'utf8').catch(() => '');
585
- if (!rawContent.trim()) {
586
- return { indexed: false, reason: 'empty_file' };
587
- }
588
-
589
- const payload = synthesizeContentPayload({
590
- targetDir,
591
- absolutePath,
592
- squadSlug,
593
- rawContent
594
- });
595
-
596
- if (!payload) {
597
- return { indexed: false, reason: 'unsupported_file' };
598
- }
599
-
600
- upsertContentItem(db, {
601
- contentKey: payload.contentKey,
602
- taskKey: options.task || null,
603
- runKey: options.run || null,
604
- squadSlug,
605
- sessionKey: options.session || null,
606
- title: payload.title,
607
- contentType: payload.contentType,
608
- layoutType: payload.layoutType,
609
- status: 'completed',
610
- summary: payload.summary,
611
- blueprintSlug: payload.blueprint || null,
612
- usedSkills: normalizeStringArray(options.usedSkills),
613
- payload,
614
- jsonPath: null,
615
- htmlPath: path.extname(absolutePath).toLowerCase().startsWith('.ht')
616
- ? relativePath
617
- : null,
618
- createdByAgent: options.agent || null
619
- });
620
-
621
- // Fire auto-delivery if configured (non-blocking)
622
- runAutoDelivery(db, {
623
- projectDir: targetDir,
624
- squadSlug,
625
- contentKey: payload.contentKey,
626
- contentPayload: payload
627
- }).catch(() => {}); // Swallow errors — delivery failure should not break ingestion
628
-
629
- return { indexed: true, kind: path.extname(absolutePath).toLowerCase(), contentKey: payload.contentKey };
630
- }
631
-
632
- async function withRuntimeDb(targetDir, t) {
633
- const handle = await openRuntimeDb(targetDir, { mustExist: true });
634
- if (!handle) {
635
- throw new Error(t('runtime.store_missing', { path: resolveRuntimePaths(targetDir).dbPath }));
636
- }
637
- return handle;
638
- }
639
-
640
- async function runRuntimeInit({ args, options = {}, logger, t }) {
641
- const targetDir = resolveTargetDir(args);
642
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
643
- db.close();
644
-
645
- if (!options.json) {
646
- logger.log(t('runtime.init_ok', { path: dbPath }));
647
- }
648
-
649
- return { ok: true, targetDir, runtimeDir, dbPath };
650
- }
651
-
652
- async function runRuntimeIngest({ args, options = {}, logger, t }) {
653
- const targetDir = resolveTargetDir(args);
654
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
655
- const ingestOptions = {
656
- ...options,
657
- usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills)
658
- };
659
-
660
- try {
661
- const candidates = await resolveIngestCandidates(targetDir, ingestOptions);
662
- let indexed = 0;
663
- let skipped = 0;
664
- const reasons = [];
665
-
666
- for (const candidate of candidates) {
667
- const result = await ingestContentCandidate(db, targetDir, candidate, ingestOptions);
668
- if (result.indexed) {
669
- indexed += 1;
670
- continue;
671
- }
672
- skipped += 1;
673
- if (result.reason) {
674
- reasons.push(`${path.relative(targetDir, candidate).replace(/\\/g, '/')}: ${result.reason}`);
675
- }
676
- }
677
-
678
- if (!options.json) {
679
- logger.log(
680
- t('runtime.ingest_ok', {
681
- indexed,
682
- skipped,
683
- path: dbPath
684
- })
685
- );
686
- if (reasons.length > 0) {
687
- for (const reason of reasons.slice(0, 10)) {
688
- logger.log(`- ${reason}`);
689
- }
690
- }
691
- }
692
-
693
- return {
694
- ok: true,
695
- targetDir,
696
- dbPath,
697
- indexed,
698
- skipped,
699
- reasons
700
- };
701
- } finally {
702
- db.close();
703
- }
704
- }
705
-
706
- async function runRuntimeTaskStart({ args, options = {}, logger, t }) {
707
- const targetDir = resolveTargetDir(args);
708
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
709
-
710
- try {
711
- const taskKey = startTask(db, {
712
- taskKey: options.task,
713
- squadSlug: options.squad,
714
- sessionKey: options.session,
715
- title: requireOption(options, 'title', t),
716
- goal: options.goal,
717
- createdBy: options.by
718
- });
719
-
720
- if (!options.json) {
721
- logger.log(t('runtime.task_start_ok', { task: taskKey, path: dbPath }));
722
- }
723
-
724
- return {
725
- ok: true,
726
- targetDir,
727
- dbPath,
728
- taskKey,
729
- status: 'running'
730
- };
731
- } finally {
732
- db.close();
733
- }
734
- }
735
-
736
- async function runRuntimeStart({ args, options = {}, logger, t }) {
737
- const targetDir = resolveTargetDir(args);
738
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
739
-
740
- try {
741
- const runKey = startRun(db, {
742
- runKey: options.run,
743
- taskKey: options.task,
744
- agentName: requireOption(options, 'agent', t),
745
- agentKind: options.kind,
746
- squadSlug: options.squad,
747
- sessionKey: options.session,
748
- title: options.title,
749
- message: options.message,
750
- summary: options.summary,
751
- usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills),
752
- outputPath: options.output
753
- });
754
-
755
- const snapshot = getStatusSnapshot(db);
756
- if (!options.json) {
757
- logger.log(t('runtime.start_ok', { run: runKey, path: dbPath }));
758
- }
759
-
760
- return {
761
- ok: true,
762
- targetDir,
763
- dbPath,
764
- runKey,
765
- status: 'running',
766
- activeCount: snapshot.activeRuns.length
767
- };
768
- } finally {
769
- db.close();
770
- }
771
- }
772
-
773
- async function runRuntimeUpdate({ args, options = {}, logger, t }) {
774
- const targetDir = resolveTargetDir(args);
775
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
776
-
777
- try {
778
- const runKey = requireOption(options, 'run', t);
779
- const status = updateRun(db, {
780
- runKey,
781
- status: 'running',
782
- taskKey: options.task,
783
- eventType: 'progress',
784
- message: options.message,
785
- summary: options.summary,
786
- usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills),
787
- outputPath: options.output
788
- });
789
-
790
- if (!options.json) {
791
- logger.log(t('runtime.update_ok', { run: runKey, path: dbPath }));
792
- }
793
-
794
- return {
795
- ok: true,
796
- targetDir,
797
- dbPath,
798
- runKey,
799
- status
800
- };
801
- } finally {
802
- db.close();
803
- }
804
- }
805
-
806
- async function runRuntimeFinish({ args, options = {}, logger, t }) {
807
- const targetDir = resolveTargetDir(args);
808
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
809
-
810
- try {
811
- const runKey = requireOption(options, 'run', t);
812
- const status = updateRun(db, {
813
- runKey,
814
- status: 'completed',
815
- taskKey: options.task,
816
- eventType: 'finish',
817
- message: options.message || options.summary || 'Run completed',
818
- summary: options.summary,
819
- usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills),
820
- outputPath: options.output
821
- });
822
-
823
- const finishedRun = db
824
- .prepare('SELECT run_key, task_key, squad_slug, session_key, agent_name, output_path, used_skills_json FROM agent_runs WHERE run_key = ?')
825
- .get(runKey);
826
- if (finishedRun && finishedRun.output_path) {
827
- attachArtifact(db, {
828
- taskKey: finishedRun.task_key,
829
- runKey: finishedRun.run_key,
830
- squadSlug: finishedRun.squad_slug,
831
- agentName: finishedRun.agent_name,
832
- filePath: finishedRun.output_path,
833
- title: options.title || options.summary || 'Artifact generated'
834
- });
835
-
836
- const absoluteOutputPath = path.isAbsolute(finishedRun.output_path)
837
- ? finishedRun.output_path
838
- : path.join(targetDir, finishedRun.output_path);
839
- const contentPaths = maybeResolveContentPaths(targetDir, finishedRun.output_path);
840
- const preferredCandidate = contentPaths
841
- ? (await fs
842
- .stat(contentPaths.absoluteJsonPath)
843
- .then((stat) => (stat.isFile() ? contentPaths.absoluteJsonPath : null))
844
- .catch(() => null))
845
- : absoluteOutputPath;
846
-
847
- if (preferredCandidate) {
848
- const ingestion = await ingestContentCandidate(db, targetDir, preferredCandidate, {
849
- task: finishedRun.task_key,
850
- run: finishedRun.run_key,
851
- squad: finishedRun.squad_slug,
852
- session: options.session || finishedRun.session_key,
853
- agent: finishedRun.agent_name,
854
- usedSkills: parseJsonArray(finishedRun.used_skills_json)
855
- });
856
- if (!ingestion.indexed && !options.json && logger?.log) {
857
- logger.log(`[runtime] skipped content indexing for ${finishedRun.run_key}: ${ingestion.reason}`);
858
- }
859
- }
860
- }
861
-
862
- if (!options.json) {
863
- logger.log(t('runtime.finish_ok', { run: runKey, path: dbPath }));
864
- }
865
-
866
- return {
867
- ok: true,
868
- targetDir,
869
- dbPath,
870
- runKey,
871
- status
872
- };
873
- } finally {
874
- db.close();
875
- }
876
- }
877
-
878
- async function runRuntimeTaskFinish({ args, options = {}, logger, t }) {
879
- const targetDir = resolveTargetDir(args);
880
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
881
-
882
- try {
883
- const taskKey = requireOption(options, 'task', t);
884
- const taskStatus = updateTask(db, {
885
- taskKey,
886
- status: 'completed',
887
- goal: options.goal
888
- });
889
-
890
- if (!options.json) {
891
- logger.log(t('runtime.task_finish_ok', { task: taskKey, path: dbPath }));
892
- }
893
-
894
- return {
895
- ok: true,
896
- targetDir,
897
- dbPath,
898
- taskKey,
899
- status: taskStatus
900
- };
901
- } finally {
902
- db.close();
903
- }
904
- }
905
-
906
- async function runRuntimeFail({ args, options = {}, logger, t }) {
907
- const targetDir = resolveTargetDir(args);
908
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
909
-
910
- try {
911
- const runKey = requireOption(options, 'run', t);
912
- const status = updateRun(db, {
913
- runKey,
914
- taskKey: options.task,
915
- status: 'failed',
916
- eventType: 'fail',
917
- message: options.message || options.summary || 'Run failed',
918
- summary: options.summary,
919
- outputPath: options.output
920
- });
921
-
922
- if (!options.json) {
923
- logger.log(t('runtime.fail_ok', { run: runKey, path: dbPath }));
924
- }
925
-
926
- return {
927
- ok: true,
928
- targetDir,
929
- dbPath,
930
- runKey,
931
- status
932
- };
933
- } finally {
934
- db.close();
935
- }
936
- }
937
-
938
- async function runRuntimeTaskFail({ args, options = {}, logger, t }) {
939
- const targetDir = resolveTargetDir(args);
940
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
941
-
942
- try {
943
- const taskKey = requireOption(options, 'task', t);
944
- const status = updateTask(db, {
945
- taskKey,
946
- status: 'failed',
947
- goal: options.goal
948
- });
949
-
950
- if (!options.json) {
951
- logger.log(t('runtime.task_fail_ok', { task: taskKey, path: dbPath }));
952
- }
953
-
954
- return {
955
- ok: true,
956
- targetDir,
957
- dbPath,
958
- taskKey,
959
- status
960
- };
961
- } finally {
962
- db.close();
963
- }
964
- }
965
-
966
- async function runRuntimeStatus({ args, options = {}, logger, t }) {
967
- const targetDir = resolveTargetDir(args);
968
- const { dbPath } = resolveRuntimePaths(targetDir);
969
-
970
- if (!(await runtimeStoreExists(targetDir))) {
971
- if (options.json) {
972
- return { ok: false, error: 'store_missing', dbPath };
973
- }
974
- throw new Error(t('runtime.store_missing', { path: dbPath }));
975
- }
976
-
977
- const { db } = await openRuntimeDb(targetDir, { mustExist: true });
978
-
979
- try {
980
- const snapshot = getStatusSnapshot(db);
981
- const payload = {
982
- ok: true,
983
- targetDir,
984
- dbPath,
985
- taskCounts: snapshot.taskCounts,
986
- counts: snapshot.counts,
987
- activeTasks: snapshot.activeTasks,
988
- recentTasks: snapshot.recentTasks,
989
- activeRuns: snapshot.activeRuns,
990
- recentRuns: snapshot.recentRuns,
991
- activeLiveSessions: snapshot.activeLiveSessions,
992
- activeMicroTasks: snapshot.activeMicroTasks,
993
- recentLiveSessions: snapshot.recentLiveSessions,
994
- recentMicroTasks: snapshot.recentMicroTasks,
995
- recentHandoffs: snapshot.recentHandoffs,
996
- recentArtifacts: snapshot.recentArtifacts,
997
- recentContentItems: snapshot.recentContentItems,
998
- recentExecutionEvents: snapshot.recentExecutionEvents
999
- };
1000
-
1001
- if (!options.json) {
1002
- logger.log(t('runtime.status_title', { path: targetDir }));
1003
- logger.log(t('runtime.status_db', { path: dbPath }));
1004
- logger.log(
1005
- t('runtime.status_task_counts', {
1006
- queued: payload.taskCounts.queued,
1007
- running: payload.taskCounts.running,
1008
- completed: payload.taskCounts.completed,
1009
- failed: payload.taskCounts.failed
1010
- })
1011
- );
1012
- logger.log(
1013
- t('runtime.status_counts', {
1014
- queued: payload.counts.queued,
1015
- running: payload.counts.running,
1016
- completed: payload.counts.completed,
1017
- failed: payload.counts.failed
1018
- })
1019
- );
1020
- if (snapshot.activeTasks.length === 0) {
1021
- logger.log(t('runtime.status_no_active_tasks'));
1022
- } else {
1023
- logger.log(t('runtime.status_active_tasks_title'));
1024
- for (const task of snapshot.activeTasks) {
1025
- logger.log(
1026
- t('runtime.status_active_task_line', {
1027
- task: task.task_key,
1028
- squad: task.squad_slug || '—',
1029
- status: task.status,
1030
- title: task.title
1031
- })
1032
- );
1033
- }
1034
- }
1035
- if (snapshot.activeRuns.length === 0) {
1036
- logger.log(t('runtime.status_no_active'));
1037
- } else {
1038
- logger.log(t('runtime.status_active_title'));
1039
- for (const run of snapshot.activeRuns) {
1040
- logger.log(
1041
- t('runtime.status_active_line', {
1042
- agent: run.agent_name,
1043
- squad: run.squad_slug || '—',
1044
- status: run.status,
1045
- title: run.title || run.summary || '—'
1046
- })
1047
- );
1048
- }
1049
- }
1050
- if (snapshot.activeLiveSessions.length > 0) {
1051
- logger.log(t('runtime.status_live_sessions_title'));
1052
- for (const task of snapshot.activeLiveSessions) {
1053
- logger.log(
1054
- t('runtime.status_live_session_line', {
1055
- task: task.task_key,
1056
- agent: task.latest_agent_name || task.created_by || '—',
1057
- status: task.status,
1058
- plan: task.plan_steps_total > 0 ? `${task.plan_steps_done}/${task.plan_steps_total}` : '—',
1059
- micro: `${task.completed_child_task_count || 0}/${task.child_task_count || 0}`,
1060
- handoffs: task.handoff_count || 0,
1061
- title: task.title || '—'
1062
- })
1063
- );
1064
- }
1065
- }
1066
- if (snapshot.activeMicroTasks.length > 0) {
1067
- logger.log(t('runtime.status_micro_tasks_title'));
1068
- for (const task of snapshot.activeMicroTasks) {
1069
- logger.log(
1070
- t('runtime.status_micro_task_line', {
1071
- task: task.task_key,
1072
- parent: task.parent_task_key || '—',
1073
- status: task.status,
1074
- title: task.title || task.goal || '—'
1075
- })
1076
- );
1077
- }
1078
- }
1079
- if (snapshot.recentHandoffs.length > 0) {
1080
- logger.log(t('runtime.status_handoffs_title'));
1081
- for (const event of snapshot.recentHandoffs.slice(0, 5)) {
1082
- logger.log(
1083
- t('runtime.status_handoff_line', {
1084
- created: event.created_at,
1085
- from: event.handoff_from || event.agent_name || '—',
1086
- to: event.handoff_to || '—',
1087
- session: event.session_key || '—',
1088
- message: event.message || '—'
1089
- })
1090
- );
1091
- }
1092
- }
1093
- }
1094
-
1095
- return payload;
1096
- } finally {
1097
- db.close();
1098
- }
1099
- }
1100
-
1101
- /**
1102
- * aioson runtime-log --agent=<name> --message=<text> [--type=<event>] [--finish] [--status=completed|failed] [--summary=<text>] [--title=<task-title>]
1103
- *
1104
- * Stateful single-command logger for official AIOSON agents.
1105
- * First call creates task + run in SQLite; subsequent calls add events.
1106
- * --finish closes the run and clears the session.
1107
- */
1108
- async function runRuntimeLog({ args, options = {}, logger, t }) {
1109
- const targetDir = resolveTargetDir(args);
1110
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1111
-
1112
- try {
1113
- const agentName = options.agent;
1114
- if (!agentName) {
1115
- throw new Error(t('runtime.log_agent_required'));
1116
- }
1117
-
1118
- const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1119
- agentName,
1120
- squadSlug: options.squad || null,
1121
- message: options.message || '',
1122
- type: options.type || 'status',
1123
- taskTitle: options.title,
1124
- finish: Boolean(options.finish),
1125
- status: options.status,
1126
- summary: options.summary,
1127
- meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1128
- });
1129
-
1130
- // Generate session handoff on --finish
1131
- if (options.finish) {
1132
- const handoffData = buildRuntimeLogHandoff(
1133
- agentName,
1134
- options.message || '',
1135
- options.summary || ''
1136
- );
1137
- await writeHandoff(targetDir, handoffData);
1138
- }
1139
-
1140
- if (!options.json) {
1141
- const isFinish = Boolean(options.finish);
1142
- logger.log(isFinish
1143
- ? t('runtime.log_finish_ok', { agent: agentName, run: runKey, path: dbPath })
1144
- : t('runtime.log_ok', { agent: agentName, run: runKey, path: dbPath })
1145
- );
1146
- }
1147
-
1148
- return {
1149
- ok: true,
1150
- targetDir,
1151
- dbPath,
1152
- runKey,
1153
- taskKey,
1154
- agent: agentName,
1155
- finished: Boolean(options.finish)
1156
- };
1157
- } finally {
1158
- db.close();
1159
- }
1160
- }
1161
-
1162
-
1163
- /**
1164
- * aioson agent:done . --agent=<name> --summary="..." [--title="..."] [--status=completed|failed]
1165
- *
1166
- * Safe self-registration for official agents invoked directly (not via workflow:next or live:start).
1167
- * - If an active live session exists for the agent: appends a completion event without closing the session.
1168
- * - If no session exists: creates a standalone task+run and immediately marks it completed.
1169
- *
1170
- * Intended to be called ONCE at the very end of an agent session, after delivering the main artifact.
1171
- */
1172
- async function runAgentDone({ args, options = {}, logger, t }) {
1173
- const targetDir = resolveTargetDir(args);
1174
- const agentName = String(options.agent || '').trim();
1175
- if (!agentName) {
1176
- throw new Error('--agent is required');
1177
- }
1178
- const normalizedAgent = agentName.startsWith('@') ? agentName : `@${agentName}`;
1179
- const summary = String(options.summary || options.message || `${normalizedAgent} session completed`).trim();
1180
- const title = options.title ? String(options.title).trim() : null;
1181
- const status = options.status || 'completed';
1182
- const verdict = options.verdict ? String(options.verdict).trim().toUpperCase() : null;
1183
- const planStepId = options['plan-step'] ? String(options['plan-step']).trim() : null;
1184
- const artifactPaths = options.artifacts
1185
- ? String(options.artifacts).split(',').map((p) => p.trim()).filter(Boolean)
1186
- : [];
1187
-
1188
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1189
-
1190
- try {
1191
- const session = await readAgentSession(runtimeDir, normalizedAgent);
1192
- const hasActiveSession = session && !session.finished && session.runKey;
1193
-
1194
- if (hasActiveSession) {
1195
- // Live or tracked session is already open — only append a completion note.
1196
- // Do NOT close the session: live:handoff or live:close owns the lifecycle.
1197
- appendRunEvent(db, {
1198
- runKey: session.runKey,
1199
- eventType: 'agent_done',
1200
- phase: 'live',
1201
- status: 'running',
1202
- message: summary,
1203
- verdict,
1204
- planStepId
1205
- });
1206
-
1207
- if (artifactPaths.length > 0) {
1208
- for (const filePath of artifactPaths) {
1209
- try {
1210
- attachArtifact(db, {
1211
- runKey: session.runKey,
1212
- agentName: normalizedAgent,
1213
- kind: 'output',
1214
- filePath
1215
- });
1216
- } catch { /* non-fatal */ }
1217
- }
1218
- }
1219
-
1220
- if (!options.json) {
1221
- logger.log(`agent:done — ${normalizedAgent} | live session active, event logged | run: ${session.runKey} (${dbPath})`);
1222
- }
1223
-
1224
- if (isDocCreatingAgent(normalizedAgent)) {
1225
- backupAiosonDocs(targetDir).catch(() => {});
1226
- }
1227
-
1228
- return { ok: true, targetDir, dbPath, agent: normalizedAgent, mode: 'live_event', runKey: session.runKey };
1229
- }
1230
-
1231
- // No active session — create a standalone task+run and immediately complete it.
1232
- const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1233
- agentName: normalizedAgent,
1234
- message: summary,
1235
- type: 'completed',
1236
- taskTitle: title || normalizedAgent,
1237
- finish: true,
1238
- status,
1239
- summary
1240
- });
1241
-
1242
- if (verdict || planStepId) {
1243
- appendRunEvent(db, {
1244
- runKey,
1245
- eventType: 'agent_done',
1246
- phase: 'direct',
1247
- status: 'completed',
1248
- message: summary,
1249
- verdict,
1250
- planStepId
1251
- });
1252
- }
1253
-
1254
- if (artifactPaths.length > 0) {
1255
- for (const filePath of artifactPaths) {
1256
- try {
1257
- attachArtifact(db, {
1258
- runKey,
1259
- taskKey,
1260
- agentName: normalizedAgent,
1261
- kind: 'output',
1262
- filePath
1263
- });
1264
- } catch { /* non-fatal */ }
1265
- }
1266
- }
1267
-
1268
- if (!options.json) {
1269
- logger.log(`agent:done — ${normalizedAgent} | task: ${taskKey} | run: ${runKey} (${dbPath})`);
1270
- }
1271
-
1272
- if (isDocCreatingAgent(normalizedAgent)) {
1273
- backupAiosonDocs(targetDir).catch(() => {});
1274
- }
1275
-
1276
- return { ok: true, targetDir, dbPath, agent: normalizedAgent, mode: 'standalone', runKey, taskKey };
1277
- } finally {
1278
- db.close();
1279
- }
1280
- }
1281
-
1282
-
1283
- async function runRuntimeSessionStart({ args, options = {}, logger, t }) {
1284
- const targetDir = resolveTargetDir(args);
1285
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1286
-
1287
- try {
1288
- const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1289
- const existingSnapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, { limit: options.limit });
1290
-
1291
- if (existingSnapshot.session && !existingSnapshot.open) {
1292
- await clearAgentSession(runtimeDir, agentName);
1293
- }
1294
-
1295
- if (existingSnapshot.open) {
1296
- if (!options.json) {
1297
- logger.log(`Direct session already active: ${agentName} | task: ${existingSnapshot.task?.task_key || '—'} | run: ${existingSnapshot.run?.run_key || '—'} (${dbPath})`);
1298
- }
1299
- return {
1300
- ok: true,
1301
- targetDir,
1302
- dbPath,
1303
- agent: agentName,
1304
- taskKey: existingSnapshot.task?.task_key || existingSnapshot.session?.taskKey || null,
1305
- runKey: existingSnapshot.run?.run_key || existingSnapshot.session?.runKey || null,
1306
- sessionKey: existingSnapshot.sessionKey,
1307
- status: existingSnapshot.run?.status || 'running',
1308
- reused: true,
1309
- open: true
1310
- };
1311
- }
1312
-
1313
- const sessionKey = options.session ? String(options.session).trim() : makeDirectSessionKey(agentName);
1314
- const title = options.title ? String(options.title).trim() : `Direct session ${agentName}`;
1315
- const message = options.message ? String(options.message).trim() : `Session started for ${agentName}`;
1316
- const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1317
- agentName,
1318
- message,
1319
- type: options.type || 'session.start',
1320
- taskTitle: title,
1321
- sessionKey,
1322
- meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1323
- });
1324
-
1325
- if (!options.json) {
1326
- logger.log(`Direct session started: ${agentName} | task: ${taskKey} | run: ${runKey} (${dbPath})`);
1327
- }
1328
-
1329
- return {
1330
- ok: true,
1331
- targetDir,
1332
- dbPath,
1333
- agent: agentName,
1334
- taskKey,
1335
- runKey,
1336
- sessionKey,
1337
- status: 'running',
1338
- reused: false,
1339
- open: true
1340
- };
1341
- } finally {
1342
- db.close();
1343
- }
1344
- }
1345
-
1346
- async function runRuntimeSessionLog({ args, options = {}, logger, t }) {
1347
- const targetDir = resolveTargetDir(args);
1348
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1349
-
1350
- try {
1351
- const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1352
- const message = requireOption(options, 'message', t);
1353
- const existingSnapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, { limit: options.limit });
1354
-
1355
- if (existingSnapshot.session && !existingSnapshot.open) {
1356
- await clearAgentSession(runtimeDir, agentName);
1357
- }
1358
-
1359
- const autoStarted = !existingSnapshot.open;
1360
- const sessionKey = existingSnapshot.sessionKey || (options.session ? String(options.session).trim() : makeDirectSessionKey(agentName));
1361
- const title = options.title ? String(options.title).trim() : `Direct session ${agentName}`;
1362
- const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1363
- agentName,
1364
- message,
1365
- type: options.type || 'session.log',
1366
- taskTitle: title,
1367
- sessionKey,
1368
- meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1369
- });
1370
-
1371
- if (!options.json) {
1372
- logger.log(`Direct session log recorded: ${agentName} | run: ${runKey} (${dbPath})`);
1373
- }
1374
-
1375
- return {
1376
- ok: true,
1377
- targetDir,
1378
- dbPath,
1379
- agent: agentName,
1380
- taskKey,
1381
- runKey,
1382
- sessionKey,
1383
- status: 'running',
1384
- autoStarted,
1385
- open: true
1386
- };
1387
- } finally {
1388
- db.close();
1389
- }
1390
- }
1391
-
1392
- async function runRuntimeSessionFinish({ args, options = {}, logger, t }) {
1393
- const targetDir = resolveTargetDir(args);
1394
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1395
-
1396
- try {
1397
- const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1398
- const existingSnapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, { limit: options.limit });
1399
-
1400
- if (!existingSnapshot.open) {
1401
- throw new Error(`No active direct session for ${agentName}.`);
1402
- }
1403
-
1404
- const summary = options.summary ? String(options.summary).trim() : '';
1405
- const message = options.message ? String(options.message).trim() : (summary || `Session finished for ${agentName}`);
1406
- const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1407
- agentName,
1408
- message,
1409
- type: options.type || 'session.finish',
1410
- finish: true,
1411
- status: options.status || 'completed',
1412
- summary,
1413
- meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1414
- });
1415
-
1416
- if (!options.json) {
1417
- logger.log(`Direct session finished: ${agentName} | run: ${runKey} (${dbPath})`);
1418
- }
1419
-
1420
- return {
1421
- ok: true,
1422
- targetDir,
1423
- dbPath,
1424
- agent: agentName,
1425
- taskKey,
1426
- runKey,
1427
- sessionKey: existingSnapshot.sessionKey,
1428
- status: options.status || 'completed',
1429
- finished: true,
1430
- open: false
1431
- };
1432
- } finally {
1433
- db.close();
1434
- }
1435
- }
1436
-
1437
- async function runRuntimeSessionStatus({ args, options = {}, logger, t }) {
1438
- const targetDir = resolveTargetDir(args);
1439
- const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1440
- const watchSeconds = parseWatchSeconds(options.watch);
1441
-
1442
- if (watchSeconds && options.json) {
1443
- throw new Error('--watch cannot be combined with --json.');
1444
- }
1445
-
1446
- if (!watchSeconds) {
1447
- const snapshot = await getRuntimeSessionSnapshot(targetDir, agentName, t, { limit: options.limit });
1448
- if (!options.json) {
1449
- printRuntimeSessionSnapshot(snapshot, logger);
1450
- }
1451
- return snapshot;
1452
- }
1453
-
1454
- while (true) {
1455
- const snapshot = await getRuntimeSessionSnapshot(targetDir, agentName, t, { limit: options.limit });
1456
- if (process.stdout && process.stdout.isTTY) {
1457
- process.stdout.write('\x1Bc');
1458
- }
1459
- printRuntimeSessionSnapshot(snapshot, logger);
1460
- await sleep(Math.round(watchSeconds * 1000));
1461
- }
1462
- }
1463
-
1464
- async function runDeliver({ args, options = {}, logger, t }) {
1465
- const targetDir = resolveTargetDir(args);
1466
- const squadSlug = requireOption(options, 'squad', t);
1467
- const contentKey = options['content-key'] || options.contentKey || null;
1468
- const triggerType = options.trigger || 'manual';
1469
-
1470
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
1471
-
1472
- try {
1473
- const { runManualDelivery } = require('../delivery-runner');
1474
-
1475
- // Optionally load content payload from DB
1476
- let contentPayload = null;
1477
- if (contentKey) {
1478
- const row = db.prepare('SELECT payload_json FROM content_items WHERE content_key = ? AND squad_slug = ?').get(contentKey, squadSlug);
1479
- if (row && row.payload_json) {
1480
- try { contentPayload = JSON.parse(row.payload_json); } catch { /* ignore */ }
1481
- }
1482
- }
1483
-
1484
- const result = await runManualDelivery(db, {
1485
- projectDir: targetDir,
1486
- squadSlug,
1487
- contentKey,
1488
- triggerType,
1489
- contentPayload
1490
- });
1491
-
1492
- if (!result.delivered) {
1493
- logger.log(`Delivery skipped: ${result.reason}`);
1494
- return { ok: false, ...result };
1495
- }
1496
-
1497
- for (const r of result.results || []) {
1498
- const status = r.ok ? 'OK' : 'FAIL';
1499
- logger.log(` ${status} ${r.webhookSlug} ${r.statusCode || 'no response'} (${r.attempts} attempt${r.attempts > 1 ? 's' : ''})`);
1500
- if (r.error) logger.log(` Error: ${r.error}`);
1501
- }
1502
-
1503
- logger.log(`\nDelivery ${result.allOk ? 'completed' : 'completed with errors'}.`);
1504
- return { ok: result.allOk, ...result };
1505
- } finally {
1506
- db.close();
1507
- }
1508
- }
1509
-
1510
- async function findManifestPath(projectDir, slug) {
1511
- const candidates = [
1512
- path.join(projectDir, '.aioson', 'squads', slug, 'squad.manifest.json'),
1513
- path.join(projectDir, 'agents', slug, 'squad.manifest.json')
1514
- ];
1515
- for (const p of candidates) {
1516
- try { await fs.stat(p); return p; } catch { continue; }
1517
- }
1518
- return null;
1519
- }
1520
-
1521
- async function runOutputStrategyExport({ args, options = {}, logger, t }) {
1522
- const projectDir = resolveTargetDir(args);
1523
- const slug = requireOption(options, 'squad', t);
1524
- const manifestPath = await findManifestPath(projectDir, slug);
1525
-
1526
- if (!manifestPath) {
1527
- logger.error(`Manifest not found for squad "${slug}"`);
1528
- return { ok: false, error: 'Manifest not found' };
1529
- }
1530
-
1531
- const raw = await fs.readFile(manifestPath, 'utf8');
1532
- const manifest = JSON.parse(raw);
1533
- const strategy = manifest.outputStrategy || null;
1534
-
1535
- if (!strategy) {
1536
- logger.log(`Squad "${slug}" has no outputStrategy configured.`);
1537
- return { ok: false, error: 'No outputStrategy found' };
1538
- }
1539
-
1540
- const exportsDir = path.join(projectDir, '.aioson', 'squads', 'exports');
1541
- await fs.mkdir(exportsDir, { recursive: true });
1542
- const outFile = path.join(exportsDir, `${slug}.output-strategy.json`);
1543
- await fs.writeFile(outFile, JSON.stringify(strategy, null, 2) + '\n', 'utf8');
1544
-
1545
- const relOut = path.relative(projectDir, outFile).replace(/\\/g, '/');
1546
- logger.log(`Exported outputStrategy from "${slug}" → ${relOut}`);
1547
- return { ok: true, file: relOut, strategy };
1548
- }
1549
-
1550
- async function runOutputStrategyImport({ args, options = {}, logger, t }) {
1551
- const projectDir = resolveTargetDir(args);
1552
- const slug = requireOption(options, 'squad', t);
1553
- const fromSlug = options.from || null;
1554
- const fromFile = options.file || null;
1555
-
1556
- if (!fromSlug && !fromFile) {
1557
- logger.error('Usage: aioson output-strategy:import --squad=<target> --from=<source-slug> | --file=<path>');
1558
- return { ok: false, error: 'Provide --from or --file' };
1559
- }
1560
-
1561
- // Load source strategy
1562
- let strategy;
1563
- if (fromFile) {
1564
- const absFile = path.resolve(projectDir, fromFile);
1565
- const raw = await fs.readFile(absFile, 'utf8');
1566
- strategy = JSON.parse(raw);
1567
- } else {
1568
- const srcPath = await findManifestPath(projectDir, fromSlug);
1569
- if (!srcPath) {
1570
- logger.error(`Source squad "${fromSlug}" manifest not found`);
1571
- return { ok: false, error: 'Source manifest not found' };
1572
- }
1573
- const srcManifest = JSON.parse(await fs.readFile(srcPath, 'utf8'));
1574
- strategy = srcManifest.outputStrategy || null;
1575
- if (!strategy) {
1576
- logger.error(`Source squad "${fromSlug}" has no outputStrategy`);
1577
- return { ok: false, error: 'Source has no outputStrategy' };
1578
- }
1579
- }
1580
-
1581
- // Write to target
1582
- const targetPath = await findManifestPath(projectDir, slug);
1583
- if (!targetPath) {
1584
- logger.error(`Target squad "${slug}" manifest not found`);
1585
- return { ok: false, error: 'Target manifest not found' };
1586
- }
1587
-
1588
- const targetManifest = JSON.parse(await fs.readFile(targetPath, 'utf8'));
1589
- targetManifest.outputStrategy = strategy;
1590
- await fs.writeFile(targetPath, JSON.stringify(targetManifest, null, 2) + '\n', 'utf8');
1591
-
1592
- logger.log(`Imported outputStrategy into "${slug}" from ${fromSlug || fromFile}`);
1593
- return { ok: true, squad: slug, source: fromSlug || fromFile };
1594
- }
1595
-
1596
- /**
1597
- * aioson devlog:sync [targetDir]
1598
- *
1599
- * Parses aioson-logs/devlog-*.md files, imports them into SQLite as
1600
- * task + run + events, then renames each file to .synced so it is not
1601
- * re-imported on subsequent runs.
1602
- */
1603
- async function runDevlogSync({ args, options = {}, logger, t }) {
1604
- const targetDir = resolveTargetDir(args);
1605
- const logsDir = path.join(targetDir, 'aioson-logs');
1606
-
1607
- let entries;
1608
- try {
1609
- entries = await fs.readdir(logsDir);
1610
- } catch {
1611
- logger.log('No aioson-logs/ directory found nothing to sync.');
1612
- return { ok: true, synced: 0 };
1613
- }
1614
-
1615
- const devlogFiles = entries
1616
- .filter(f => f.startsWith('devlog-') && f.endsWith('.md'))
1617
- .sort();
1618
-
1619
- if (devlogFiles.length === 0) {
1620
- logger.log('No devlog files to sync.');
1621
- return { ok: true, synced: 0 };
1622
- }
1623
-
1624
- const { db, dbPath } = await openRuntimeDb(targetDir);
1625
- let synced = 0;
1626
- const parsedDevlogs = [];
1627
-
1628
- try {
1629
- for (const file of devlogFiles) {
1630
- const filePath = path.join(logsDir, file);
1631
- const raw = await fs.readFile(filePath, 'utf8');
1632
-
1633
- // Parse YAML frontmatter
1634
- const fm = parseFrontmatter(raw);
1635
- const agent = fm.agent || 'unknown';
1636
- const summary = fm.summary || file;
1637
- const sessionStart = fm.session_start || null;
1638
- const sessionEnd = fm.session_end || null;
1639
- const status = fm.status || 'completed';
1640
- const body = raw.replace(/^---[\s\S]*?---\s*/, '');
1641
-
1642
- parsedDevlogs.push({ filename: file, agent, summary, sessionStart, sessionEnd, status, body });
1643
-
1644
- // Create task + run
1645
- const taskKey = startTask(db, {
1646
- title: `devlog: ${summary}`,
1647
- squadSlug: null,
1648
- status: status === 'partial' ? 'running' : 'completed',
1649
- createdBy: agent
1650
- });
1651
-
1652
- const runKey = startRun(db, {
1653
- taskKey,
1654
- agentName: agent,
1655
- agentKind: 'devlog',
1656
- squadSlug: null,
1657
- title: `@${agent} devlog`,
1658
- message: summary
1659
- });
1660
-
1661
- // Extract body sections as events
1662
- const sections = body.split(/^## /m).filter(Boolean);
1663
- for (const section of sections) {
1664
- const firstLine = section.split('\n')[0].trim();
1665
- const content = section.slice(firstLine.length).trim();
1666
- if (content) {
1667
- appendRunEvent(db, {
1668
- runKey,
1669
- eventType: 'devlog',
1670
- phase: firstLine.toLowerCase().replace(/\s+/g, '_'),
1671
- status: 'completed',
1672
- message: `## ${firstLine}\n${content}`,
1673
- createdAt: sessionEnd || new Date().toISOString()
1674
- });
1675
- }
1676
- }
1677
-
1678
- // Close the run
1679
- updateRun(db, runKey, {
1680
- status: status === 'partial' ? 'running' : 'completed',
1681
- summary,
1682
- finishedAt: sessionEnd || new Date().toISOString()
1683
- });
1684
-
1685
- if (status !== 'partial') {
1686
- updateTask(db, taskKey, {
1687
- status: 'completed',
1688
- finishedAt: sessionEnd || new Date().toISOString()
1689
- });
1690
- }
1691
-
1692
- // Rename to .synced
1693
- await fs.rename(filePath, filePath.replace(/\.md$/, '.synced.md'));
1694
- synced++;
1695
- logger.log(` Synced: ${file} → task=${taskKey} run=${runKey}`);
1696
- }
1697
-
1698
- logger.log(`Synced ${synced} devlog(s) into ${dbPath}`);
1699
-
1700
- // Cloud sync
1701
- if (options.cloud) {
1702
- const cloudResult = await syncDevlogsToCloud(targetDir, parsedDevlogs, options, logger);
1703
- return { ok: true, synced, dbPath, cloud: cloudResult };
1704
- }
1705
-
1706
- return { ok: true, synced, dbPath };
1707
- } finally {
1708
- db.close();
1709
- }
1710
- }
1711
-
1712
- /**
1713
- * Sends parsed devlogs to the cloud endpoint.
1714
- * Reads cloud config from .aioson/install.json or --url / --token options.
1715
- */
1716
- async function syncDevlogsToCloud(targetDir, devlogs, options, logger) {
1717
- const cloudUrl = options.url || options['cloud-url'] || await resolveCloudUrl(targetDir);
1718
- const cloudToken = options.token || options['cloud-token'] || await resolveCloudToken(targetDir);
1719
-
1720
- if (!cloudUrl) {
1721
- logger.error('Cloud URL not configured. Use --url or set cloudBaseUrl in dashboard project settings.');
1722
- return { ok: false, error: 'missing_cloud_url' };
1723
- }
1724
- if (!cloudToken) {
1725
- logger.error('Cloud token not configured. Use --token or set cloudApiToken in dashboard project settings.');
1726
- return { ok: false, error: 'missing_cloud_token' };
1727
- }
1728
-
1729
- const endpoint = `${cloudUrl.replace(/\/+$/, '')}/api/publish/runtime`;
1730
- const payload = {
1731
- tasks: [],
1732
- devlogs: devlogs.map(d => ({
1733
- filename: d.filename,
1734
- agent: d.agent,
1735
- sessionStart: d.sessionStart,
1736
- sessionEnd: d.sessionEnd,
1737
- status: d.status,
1738
- summary: d.summary,
1739
- body: d.body
1740
- }))
1741
- };
1742
-
1743
- logger.log(` Pushing ${devlogs.length} devlog(s) to ${endpoint}...`);
1744
-
1745
- const response = await fetch(endpoint, {
1746
- method: 'POST',
1747
- headers: {
1748
- 'accept': 'application/json',
1749
- 'content-type': 'application/json',
1750
- 'authorization': `Bearer ${cloudToken}`
1751
- },
1752
- body: JSON.stringify(payload),
1753
- signal: AbortSignal.timeout(15000)
1754
- });
1755
-
1756
- const text = await response.text();
1757
- let result;
1758
- try { result = JSON.parse(text); } catch { result = { ok: false, error: text }; }
1759
-
1760
- if (result.ok) {
1761
- logger.log(` Cloud sync OK: ${result.devlogsStored || 0} devlog(s) stored.`);
1762
- } else {
1763
- logger.error(` Cloud sync failed: ${result.error || response.status}`);
1764
- }
1765
-
1766
- return result;
1767
- }
1768
-
1769
- async function resolveCloudUrl(targetDir) {
1770
- try {
1771
- const raw = await fs.readFile(path.join(targetDir, '.aioson/install.json'), 'utf8');
1772
- const meta = JSON.parse(raw);
1773
- return meta.cloudBaseUrl || null;
1774
- } catch { return null; }
1775
- }
1776
-
1777
- async function resolveCloudToken(targetDir) {
1778
- try {
1779
- const raw = await fs.readFile(path.join(targetDir, '.aioson/install.json'), 'utf8');
1780
- const meta = JSON.parse(raw);
1781
- return meta.cloudApiToken || null;
1782
- } catch { return null; }
1783
- }
1784
-
1785
- /**
1786
- * Minimal YAML frontmatter parser (no external deps).
1787
- * Returns an object with frontmatter keys, or {} if none.
1788
- */
1789
- function parseFrontmatter(content) {
1790
- const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
1791
- if (!match) return {};
1792
- const result = {};
1793
- for (const line of match[1].split('\n')) {
1794
- const idx = line.indexOf(':');
1795
- if (idx === -1) continue;
1796
- const key = line.slice(0, idx).trim();
1797
- let val = line.slice(idx + 1).trim();
1798
- // Strip surrounding quotes
1799
- if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
1800
- val = val.slice(1, -1);
1801
- }
1802
- result[key] = val;
1803
- }
1804
- return result;
1805
- }
1806
-
1807
- /**
1808
- * Parses a duration string like "24h", "30m", "7d" into milliseconds.
1809
- * Falls back to treating the raw value as hours.
1810
- */
1811
- function parseDurationMs(value, defaultHours = 24) {
1812
- const text = String(value || '').trim().toLowerCase();
1813
- if (!text) return defaultHours * 60 * 60 * 1000;
1814
-
1815
- const match = text.match(/^(\d+(?:\.\d+)?)\s*([hmd]?)$/);
1816
- if (!match) return defaultHours * 60 * 60 * 1000;
1817
-
1818
- const n = parseFloat(match[1]);
1819
- const unit = match[2] || 'h';
1820
- if (unit === 'd') return n * 24 * 60 * 60 * 1000;
1821
- if (unit === 'm') return n * 60 * 1000;
1822
- return n * 60 * 60 * 1000; // hours (default)
1823
- }
1824
-
1825
- /**
1826
- * aioson agent:recover [targetDir] [--older-than=<duration>] [--dry-run]
1827
- *
1828
- * Detects and closes agent sessions that were abandoned (Claude Code closed before
1829
- * agent:done was called, or live:start session was never closed).
1830
- *
1831
- * Sources checked:
1832
- * 1. Session files in .aioson/.sessions/ with finished=false older than threshold.
1833
- * 2. agent_runs rows with status='running'/'queued' and started_at older than threshold
1834
- * that have no corresponding live session file (orphaned DB records).
1835
- *
1836
- * --older-than Duration threshold. Accepts: 24h (default), 8h, 30m, 7d.
1837
- * --dry-run Report what would be recovered without making any changes.
1838
- * --json Output JSON result.
1839
- */
1840
- async function runAgentRecover({ args, options = {}, logger }) {
1841
- const targetDir = resolveTargetDir(args);
1842
- const dryRun = Boolean(options['dry-run'] || options.dryRun);
1843
- const olderThanMs = parseDurationMs(options['older-than'] || options.olderThan, 24);
1844
- const cutoffMs = Date.now() - olderThanMs;
1845
- const cutoffIso = new Date(cutoffMs).toISOString();
1846
- const now = new Date().toISOString();
1847
-
1848
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1849
-
1850
- const recovered = [];
1851
- const skipped = [];
1852
-
1853
- try {
1854
- // ── 1. Scan session files ─────────────────────────────────────────────────
1855
- const sessionsDir = path.join(runtimeDir, '.sessions');
1856
- let sessionFiles = [];
1857
- try {
1858
- const entries = await fs.readdir(sessionsDir);
1859
- sessionFiles = entries.filter((f) => f.endsWith('.json'));
1860
- } catch {
1861
- // .sessions dir may not exist — that's fine
1862
- }
1863
-
1864
- for (const file of sessionFiles) {
1865
- const filePath = path.join(sessionsDir, file);
1866
- let session;
1867
- try {
1868
- session = JSON.parse(await fs.readFile(filePath, 'utf8'));
1869
- } catch {
1870
- continue;
1871
- }
1872
-
1873
- if (session.finished) continue;
1874
-
1875
- const startedAt = session.startedAt ? new Date(session.startedAt).getTime() : 0;
1876
- if (startedAt > cutoffMs) {
1877
- skipped.push({ source: 'session_file', file, reason: 'within_threshold', startedAt: session.startedAt });
1878
- continue;
1879
- }
1880
-
1881
- const agentName = file.replace(/\.json$/, '');
1882
- const runKey = session.runKey || null;
1883
- const taskKey = session.taskKey || null;
1884
-
1885
- if (!dryRun) {
1886
- // Mark run as abandoned
1887
- if (runKey) {
1888
- const runRow = db.prepare('SELECT run_key, status FROM agent_runs WHERE run_key = ?').get(runKey);
1889
- if (runRow && (runRow.status === 'running' || runRow.status === 'queued')) {
1890
- db.prepare(`
1891
- UPDATE agent_runs
1892
- SET status = 'abandoned', summary = 'Recovered: session abandoned without close', updated_at = ?, finished_at = ?
1893
- WHERE run_key = ?
1894
- `).run(now, now, runKey);
1895
- }
1896
- }
1897
- // Mark task as abandoned
1898
- if (taskKey) {
1899
- const taskRow = db.prepare('SELECT task_key, status FROM tasks WHERE task_key = ?').get(taskKey);
1900
- if (taskRow && (taskRow.status === 'running' || taskRow.status === 'queued')) {
1901
- db.prepare(`
1902
- UPDATE tasks
1903
- SET status = 'abandoned', updated_at = ?, finished_at = ?
1904
- WHERE task_key = ?
1905
- `).run(now, now, taskKey);
1906
- }
1907
- }
1908
- // Remove session file
1909
- try { await fs.unlink(filePath); } catch { /* noop */ }
1910
- }
1911
-
1912
- recovered.push({ source: 'session_file', agent: agentName, runKey, taskKey, startedAt: session.startedAt });
1913
- }
1914
-
1915
- // ── 2. Scan DB for orphaned running runs (no session file) ────────────────
1916
- const orphanedRuns = db.prepare(`
1917
- SELECT run_key, task_key, agent_name, started_at
1918
- FROM agent_runs
1919
- WHERE status IN ('running', 'queued')
1920
- AND source = 'direct'
1921
- AND started_at < ?
1922
- `).all(cutoffIso);
1923
-
1924
- for (const run of orphanedRuns) {
1925
- // Skip if already recovered via session file
1926
- if (recovered.some((r) => r.runKey === run.run_key)) continue;
1927
-
1928
- if (!dryRun) {
1929
- db.prepare(`
1930
- UPDATE agent_runs
1931
- SET status = 'abandoned', summary = 'Recovered: orphaned run with no session file', updated_at = ?, finished_at = ?
1932
- WHERE run_key = ?
1933
- `).run(now, now, run.run_key);
1934
-
1935
- if (run.task_key) {
1936
- const taskRow = db.prepare('SELECT task_key, status FROM tasks WHERE task_key = ?').get(run.task_key);
1937
- if (taskRow && (taskRow.status === 'running' || taskRow.status === 'queued')) {
1938
- db.prepare(`
1939
- UPDATE tasks
1940
- SET status = 'abandoned', updated_at = ?, finished_at = ?
1941
- WHERE task_key = ?
1942
- `).run(now, now, run.task_key);
1943
- }
1944
- }
1945
- }
1946
-
1947
- recovered.push({ source: 'orphaned_run', agent: run.agent_name, runKey: run.run_key, taskKey: run.task_key, startedAt: run.started_at });
1948
- }
1949
-
1950
- // ── Output ────────────────────────────────────────────────────────────────
1951
- const olderThanLabel = options['older-than'] || options.olderThan || '24h';
1952
- if (recovered.length === 0) {
1953
- logger.log(`agent:recover — no abandoned sessions found older than ${olderThanLabel} (${dbPath})`);
1954
- } else {
1955
- const verb = dryRun ? '[dry-run] would recover' : 'recovered';
1956
- logger.log(`agent:recover ${verb} ${recovered.length} abandoned session(s) older than ${olderThanLabel} (${dbPath})`);
1957
- for (const r of recovered) {
1958
- logger.log(` ${r.agent} started: ${r.startedAt || '?'} run: ${r.runKey || '—'} [${r.source}]`);
1959
- }
1960
- }
1961
- if (skipped.length > 0) {
1962
- logger.log(` skipped ${skipped.length} session(s) within threshold.`);
1963
- }
1964
-
1965
- return { ok: true, targetDir, dbPath, dryRun, cutoff: cutoffIso, recovered, skipped };
1966
- } finally {
1967
- db.close();
1968
- }
1969
- }
1970
-
1971
-
1972
- /**
1973
- * aioson runtime:prune [targetDir] --older-than=<days>
1974
- *
1975
- * Removes execution_events, agent_events, and completed agent_runs
1976
- * older than the specified number of days. Tasks are kept but their
1977
- * events are cleaned up.
1978
- */
1979
- async function runRuntimePrune({ args, options = {}, logger, t }) {
1980
- const targetDir = resolveTargetDir(args);
1981
- const days = parseInt(options['older-than'] || options.olderThan || '30', 10);
1982
-
1983
- if (isNaN(days) || days < 1) {
1984
- logger.error('Usage: aioson runtime:prune --older-than=<days> (minimum 1)');
1985
- return { ok: false, error: 'Invalid --older-than value' };
1986
- }
1987
-
1988
- const { db, dbPath } = await withRuntimeDb(targetDir, t);
1989
-
1990
- try {
1991
- const cutoff = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();
1992
-
1993
- const execEvents = db.prepare(
1994
- `DELETE FROM execution_events WHERE created_at < ?`
1995
- ).run(cutoff);
1996
-
1997
- const agentEvents = db.prepare(
1998
- `DELETE FROM agent_events WHERE created_at < ?`
1999
- ).run(cutoff);
2000
-
2001
- const runs = db.prepare(
2002
- `DELETE FROM agent_runs WHERE status IN ('completed', 'failed') AND finished_at < ?`
2003
- ).run(cutoff);
2004
-
2005
- const tasks = db.prepare(
2006
- `DELETE FROM tasks WHERE status IN ('completed', 'failed') AND finished_at < ?`
2007
- ).run(cutoff);
2008
-
2009
- const deliveryLogs = db.prepare(
2010
- `DELETE FROM delivery_log WHERE created_at < ?`
2011
- ).run(cutoff);
2012
-
2013
- // Reclaim disk space
2014
- db.pragma('wal_checkpoint(TRUNCATE)');
2015
-
2016
- const total = execEvents.changes + agentEvents.changes + runs.changes + tasks.changes + deliveryLogs.changes;
2017
-
2018
- logger.log(`Pruned ${total} records older than ${days} days from ${dbPath}:`);
2019
- logger.log(` execution_events: ${execEvents.changes}`);
2020
- logger.log(` agent_events: ${agentEvents.changes}`);
2021
- logger.log(` agent_runs: ${runs.changes}`);
2022
- logger.log(` tasks: ${tasks.changes}`);
2023
- logger.log(` delivery_log: ${deliveryLogs.changes}`);
2024
-
2025
- return {
2026
- ok: true,
2027
- dbPath,
2028
- days,
2029
- cutoff,
2030
- deleted: {
2031
- execution_events: execEvents.changes,
2032
- agent_events: agentEvents.changes,
2033
- agent_runs: runs.changes,
2034
- tasks: tasks.changes,
2035
- delivery_log: deliveryLogs.changes,
2036
- total
2037
- }
2038
- };
2039
- } finally {
2040
- db.close();
2041
- }
2042
- }
2043
-
2044
- module.exports = {
2045
- runRuntimeInit,
2046
- runRuntimeIngest,
2047
- runRuntimeTaskStart,
2048
- runRuntimeStart,
2049
- runRuntimeUpdate,
2050
- runRuntimeTaskFinish,
2051
- runRuntimeFinish,
2052
- runRuntimeTaskFail,
2053
- runRuntimeFail,
2054
- runRuntimeStatus,
2055
- runRuntimeLog,
2056
- runAgentDone,
2057
- runAgentRecover,
2058
- runRuntimeSessionStart,
2059
- runRuntimeSessionLog,
2060
- runRuntimeSessionFinish,
2061
- runRuntimeSessionStatus,
2062
- runDeliver,
2063
- runOutputStrategyExport,
2064
- runOutputStrategyImport,
2065
- runDevlogSync,
2066
- runRuntimePrune
2067
- };
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs/promises');
4
+ const path = require('node:path');
5
+ const {
6
+ resolveRuntimePaths,
7
+ openRuntimeDb,
8
+ runtimeStoreExists,
9
+ startTask,
10
+ updateTask,
11
+ startRun,
12
+ updateRun,
13
+ attachArtifact,
14
+ upsertContentItem,
15
+ getStatusSnapshot,
16
+ logAgentEvent,
17
+ appendRunEvent,
18
+ readAgentSession,
19
+ clearAgentSession
20
+ } = require('../runtime-store');
21
+ const { runAutoDelivery } = require('../delivery-runner');
22
+ const { writeHandoff, buildRuntimeLogHandoff } = require('../session-handoff');
23
+ const { backupAiosonDocs, isDocCreatingAgent } = require('../backup-local');
24
+ const { runMemoryReflectPrepare } = require('./memory-reflect-prepare');
25
+
26
+ const ALLOWED_LAYOUTS = new Set(['document', 'tabs', 'accordion', 'stack', 'mixed']);
27
+ const DEFAULT_TEXT_FIELDS = ['content', 'text', 'body', 'lyrics', 'markdown'];
28
+
29
+ function resolveTargetDir(args) {
30
+ return path.resolve(process.cwd(), args[0] || '.');
31
+ }
32
+
33
+ function requireOption(options, key, t) {
34
+ const value = options[key];
35
+ if (value === undefined || value === null || String(value).trim() === '') {
36
+ throw new Error(t('runtime.option_required', { option: `--${key}` }));
37
+ }
38
+ return String(value).trim();
39
+ }
40
+
41
+ function normalizeAgentHandle(value) {
42
+ const text = String(value || '').trim();
43
+ if (!text) return '';
44
+ return text.startsWith('@') ? text : `@${text}`;
45
+ }
46
+
47
+ function makeDirectSessionKey(agentName) {
48
+ return `direct-session:${Date.now()}:${String(agentName || '').replace(/^@/, '')}`;
49
+ }
50
+
51
+ function parseWatchSeconds(value) {
52
+ if (value === undefined || value === null || value === false) return null;
53
+ if (value === true || value === '') return 2;
54
+
55
+ const parsed = Number(value);
56
+ if (!Number.isFinite(parsed) || parsed <= 0) return 2;
57
+ return parsed;
58
+ }
59
+
60
+ function sleep(ms) {
61
+ return new Promise((resolve) => setTimeout(resolve, ms));
62
+ }
63
+
64
+ async function collectRuntimeSessionSnapshot(db, runtimeDir, agentName, options = {}) {
65
+ const normalizedAgent = normalizeAgentHandle(agentName);
66
+ const eventLimit = Math.max(1, Math.min(Number(options.limit) || 8, 20));
67
+ const session = await readAgentSession(runtimeDir, normalizedAgent);
68
+ const activeSession = session && !session.finished ? session : null;
69
+
70
+ let run = null;
71
+ if (activeSession && activeSession.runKey) {
72
+ run = db.prepare(`
73
+ SELECT
74
+ run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
75
+ title, status, summary, output_path, started_at, updated_at, finished_at
76
+ FROM agent_runs
77
+ WHERE run_key = ?
78
+ LIMIT 1
79
+ `).get(activeSession.runKey);
80
+ }
81
+
82
+ if (!run) {
83
+ run = db.prepare(`
84
+ SELECT
85
+ run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
86
+ title, status, summary, output_path, started_at, updated_at, finished_at
87
+ FROM agent_runs
88
+ WHERE agent_name = ?
89
+ ORDER BY updated_at DESC, started_at DESC
90
+ LIMIT 1
91
+ `).get(normalizedAgent);
92
+ }
93
+
94
+ const task = run && run.task_key
95
+ ? db.prepare(`
96
+ SELECT
97
+ task_key, squad_slug, session_key, title, goal, status, created_by, created_at, updated_at, finished_at
98
+ FROM tasks
99
+ WHERE task_key = ?
100
+ LIMIT 1
101
+ `).get(run.task_key)
102
+ : null;
103
+
104
+ const recentEvents = run
105
+ ? db.prepare(`
106
+ SELECT event_type, phase, status, message, created_at
107
+ FROM execution_events
108
+ WHERE run_key = ?
109
+ ORDER BY created_at DESC, id DESC
110
+ LIMIT ?
111
+ `).all(run.run_key, eventLimit).reverse()
112
+ : [];
113
+
114
+ const open = Boolean(activeSession && run && (run.status === 'running' || run.status === 'queued'));
115
+ const state = open ? 'open' : (run ? 'closed' : 'idle');
116
+
117
+ return {
118
+ agent: normalizedAgent,
119
+ state,
120
+ open,
121
+ sessionKey: activeSession?.sessionKey || run?.session_key || task?.session_key || null,
122
+ startedAt: activeSession?.startedAt || run?.started_at || task?.created_at || null,
123
+ updatedAt: run?.updated_at || task?.updated_at || null,
124
+ session: activeSession,
125
+ run,
126
+ task,
127
+ recentEvents
128
+ };
129
+ }
130
+
131
+ async function getRuntimeSessionSnapshot(targetDir, agentName, t, options = {}) {
132
+ const { dbPath, runtimeDir } = resolveRuntimePaths(targetDir);
133
+
134
+ if (!(await runtimeStoreExists(targetDir))) {
135
+ throw new Error(t('runtime.store_missing', { path: dbPath }));
136
+ }
137
+
138
+ const { db } = await openRuntimeDb(targetDir, { mustExist: true });
139
+ try {
140
+ const snapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, options);
141
+ return {
142
+ ok: true,
143
+ targetDir,
144
+ dbPath,
145
+ ...snapshot
146
+ };
147
+ } finally {
148
+ db.close();
149
+ }
150
+ }
151
+
152
+ function printRuntimeSessionSnapshot(snapshot, logger) {
153
+ logger.log(`Direct session: ${snapshot.agent}`);
154
+ logger.log(`State: ${snapshot.state}`);
155
+
156
+ if (snapshot.sessionKey) {
157
+ logger.log(`Session: ${snapshot.sessionKey}`);
158
+ }
159
+
160
+ if (snapshot.task) {
161
+ logger.log(`Task: ${snapshot.task.task_key} | status: ${snapshot.task.status} | work: ${snapshot.task.title || '—'}`);
162
+ }
163
+
164
+ if (snapshot.run) {
165
+ logger.log(`Run: ${snapshot.run.run_key} | status: ${snapshot.run.status} | work: ${snapshot.run.title || snapshot.run.summary || '—'}`);
166
+ }
167
+
168
+ if (snapshot.startedAt) {
169
+ logger.log(`Started: ${snapshot.startedAt}`);
170
+ }
171
+
172
+ if (snapshot.updatedAt) {
173
+ logger.log(`Updated: ${snapshot.updatedAt}`);
174
+ }
175
+
176
+ if (snapshot.recentEvents.length === 0) {
177
+ logger.log('Recent events: none');
178
+ return;
179
+ }
180
+
181
+ logger.log('Recent events:');
182
+ for (const event of snapshot.recentEvents) {
183
+ logger.log(`- ${event.created_at} | ${event.event_type} | ${event.message || '—'}`);
184
+ }
185
+ }
186
+
187
+ async function readJsonIfExists(filePath) {
188
+ try {
189
+ const raw = await fs.readFile(filePath, 'utf8');
190
+ return JSON.parse(raw);
191
+ } catch {
192
+ return null;
193
+ }
194
+ }
195
+
196
+ function maybeResolveContentPaths(targetDir, outputPath) {
197
+ if (!outputPath) return null;
198
+
199
+ const relative = String(outputPath).replace(/\\/g, '/').trim();
200
+ if (!/\/index\.html?$/i.test(relative)) return null;
201
+
202
+ const absoluteHtmlPath = path.isAbsolute(relative) ? relative : path.join(targetDir, relative);
203
+ const absoluteJsonPath = path.join(path.dirname(absoluteHtmlPath), 'content.json');
204
+
205
+ return {
206
+ relativeHtmlPath: path.isAbsolute(relative) ? path.relative(targetDir, absoluteHtmlPath).replace(/\\/g, '/') : relative,
207
+ relativeJsonPath: path.relative(targetDir, absoluteJsonPath).replace(/\\/g, '/'),
208
+ absoluteJsonPath
209
+ };
210
+ }
211
+
212
+ function asObject(value) {
213
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
214
+ }
215
+
216
+ function asString(value) {
217
+ return typeof value === 'string' ? value.trim() : '';
218
+ }
219
+
220
+ function asArray(value) {
221
+ return Array.isArray(value) ? value : [];
222
+ }
223
+
224
+ function normalizeStringArray(value) {
225
+ const values = Array.isArray(value)
226
+ ? value
227
+ : typeof value === 'string'
228
+ ? value.split(',')
229
+ : [];
230
+
231
+ return Array.from(
232
+ new Set(
233
+ values
234
+ .map((entry) => String(entry || '').trim())
235
+ .filter(Boolean)
236
+ )
237
+ );
238
+ }
239
+
240
+ function parseJsonArray(value) {
241
+ if (!value) return [];
242
+ try {
243
+ return normalizeStringArray(JSON.parse(value));
244
+ } catch {
245
+ return [];
246
+ }
247
+ }
248
+
249
+ function titleize(value) {
250
+ return String(value || '')
251
+ .replace(/\.[^.]+$/, '')
252
+ .replace(/[-_]+/g, ' ')
253
+ .replace(/\s+/g, ' ')
254
+ .trim()
255
+ .replace(/\b\w/g, (char) => char.toUpperCase());
256
+ }
257
+
258
+ function makeContentKey(value) {
259
+ return String(value || 'content')
260
+ .toLowerCase()
261
+ .replace(/[^a-z0-9]+/g, '-')
262
+ .replace(/^-+|-+$/g, '')
263
+ .slice(0, 80) || `content-${Date.now()}`;
264
+ }
265
+
266
+ function truncateText(value, max = 12000) {
267
+ const text = String(value || '').trim();
268
+ if (text.length <= max) return text;
269
+ return `${text.slice(0, max).trim()}\n\n[...]`;
270
+ }
271
+
272
+ function stripHtml(html) {
273
+ return String(html || '')
274
+ .replace(/<script[\s\S]*?<\/script>/gi, ' ')
275
+ .replace(/<style[\s\S]*?<\/style>/gi, ' ')
276
+ .replace(/<[^>]+>/g, ' ')
277
+ .replace(/\s+/g, ' ')
278
+ .trim();
279
+ }
280
+
281
+ function firstNonEmptyText(record, keys) {
282
+ for (const key of keys) {
283
+ const value = asString(record[key]);
284
+ if (value) return value;
285
+ }
286
+ return '';
287
+ }
288
+
289
+ function normalizeSimpleContentBlocks(content) {
290
+ const text = firstNonEmptyText(content, DEFAULT_TEXT_FIELDS);
291
+ if (text) {
292
+ return [
293
+ {
294
+ type: 'rich-text',
295
+ content: truncateText(text)
296
+ }
297
+ ];
298
+ }
299
+
300
+ const html = asString(content.html);
301
+ if (html) {
302
+ const preview = stripHtml(html);
303
+ return [
304
+ {
305
+ type: 'callout',
306
+ title: 'Conteudo HTML indexado automaticamente',
307
+ content:
308
+ 'Este item foi convertido para o indice de conteudos a partir de um arquivo HTML gerado pelo squad.'
309
+ },
310
+ {
311
+ type: 'rich-text',
312
+ content: truncateText(preview || 'Nao foi possivel extrair preview textual do HTML.')
313
+ }
314
+ ];
315
+ }
316
+
317
+ return [];
318
+ }
319
+
320
+ function isValidBlock(value) {
321
+ const block = asObject(value);
322
+ if (!block) return false;
323
+
324
+ const type = asString(block.type);
325
+ if (!type) return false;
326
+
327
+ if (type === 'tabs') {
328
+ const items = asArray(block.items);
329
+ return items.every((item) => {
330
+ const tab = asObject(item);
331
+ if (!tab || !asString(tab.label)) return false;
332
+ return asArray(tab.blocks).every(isValidBlock);
333
+ });
334
+ }
335
+
336
+ if (type === 'accordion') {
337
+ const items = asArray(block.items);
338
+ return items.every((item) => {
339
+ const entry = asObject(item);
340
+ if (!entry || !asString(entry.title)) return false;
341
+ const content = asString(entry.content);
342
+ const nestedBlocks = asArray(entry.blocks);
343
+ if (!content && nestedBlocks.length === 0) return false;
344
+ return nestedBlocks.every(isValidBlock);
345
+ });
346
+ }
347
+
348
+ if (type === 'section') {
349
+ return asArray(block.blocks).every(isValidBlock);
350
+ }
351
+
352
+ return true;
353
+ }
354
+
355
+ function validateContentPayload(payload) {
356
+ const content = asObject(payload);
357
+ if (!content) {
358
+ return { ok: false, reason: 'content.json must be an object' };
359
+ }
360
+
361
+ const contentKey = asString(content.contentKey || content.content_key);
362
+ if (!contentKey) {
363
+ return { ok: false, reason: 'content.json is missing contentKey' };
364
+ }
365
+
366
+ const title = asString(content.title);
367
+ if (!title) {
368
+ return { ok: false, reason: 'content.json is missing title' };
369
+ }
370
+
371
+ const contentType = asString(content.contentType || content.content_type);
372
+ if (!contentType) {
373
+ return { ok: false, reason: 'content.json is missing contentType' };
374
+ }
375
+
376
+ const layoutType = asString(content.layoutType || content.layout_type || 'document');
377
+ if (!ALLOWED_LAYOUTS.has(layoutType)) {
378
+ return { ok: false, reason: `content.json has unsupported layoutType: ${layoutType}` };
379
+ }
380
+
381
+ const blocks = asArray(content.blocks);
382
+ const normalizedBlocks = blocks.length > 0 ? blocks : normalizeSimpleContentBlocks(content);
383
+
384
+ if (normalizedBlocks.length === 0) {
385
+ return { ok: false, reason: 'content.json must include blocks or a simple text field' };
386
+ }
387
+
388
+ if (!normalizedBlocks.every(isValidBlock)) {
389
+ return { ok: false, reason: 'content.json contains invalid blocks' };
390
+ }
391
+
392
+ return {
393
+ ok: true,
394
+ normalized: {
395
+ ...content,
396
+ contentKey,
397
+ title,
398
+ contentType,
399
+ layoutType,
400
+ blocks: normalizedBlocks,
401
+ blueprint: asString(content.blueprint || content.blueprintSlug || content.blueprint_slug),
402
+ usedSkills: normalizeStringArray(
403
+ content.usedSkills ||
404
+ content.used_skills ||
405
+ content.meta?.usedSkills ||
406
+ content.meta?.used_skills ||
407
+ []
408
+ )
409
+ }
410
+ };
411
+ }
412
+
413
+ async function listFilesRecursive(rootDir) {
414
+ const result = [];
415
+ const queue = [rootDir];
416
+
417
+ while (queue.length > 0) {
418
+ const currentDir = queue.shift();
419
+ const entries = await fs.readdir(currentDir, { withFileTypes: true }).catch(() => []);
420
+ for (const entry of entries) {
421
+ const fullPath = path.join(currentDir, entry.name);
422
+ if (entry.isDirectory()) {
423
+ queue.push(fullPath);
424
+ continue;
425
+ }
426
+ if (entry.isFile()) {
427
+ result.push(fullPath);
428
+ }
429
+ }
430
+ }
431
+
432
+ return result;
433
+ }
434
+
435
+ function inferSquadSlugFromOutputPath(targetDir, absolutePath) {
436
+ const outputRoot = path.join(targetDir, 'output');
437
+ const relativePath = path.relative(outputRoot, absolutePath).replace(/\\/g, '/');
438
+ const [slug] = relativePath.split('/');
439
+ return slug || '';
440
+ }
441
+
442
+ function relativeContentKeyFromOutput(targetDir, absolutePath, squadSlug) {
443
+ const squadRoot = path.join(targetDir, 'output', squadSlug);
444
+ const relativeToSquad = path.relative(squadRoot, absolutePath).replace(/\\/g, '/');
445
+ return makeContentKey(relativeToSquad.replace(/\.[^.]+$/, ''));
446
+ }
447
+
448
+ function synthesizeContentPayload({ targetDir, absolutePath, squadSlug, rawContent }) {
449
+ const ext = path.extname(absolutePath).toLowerCase();
450
+ const relativePath = path.relative(targetDir, absolutePath).replace(/\\/g, '/');
451
+ const title = titleize(path.basename(absolutePath));
452
+ const contentKey = relativeContentKeyFromOutput(targetDir, absolutePath, squadSlug);
453
+
454
+ if (ext === '.md') {
455
+ return {
456
+ contentKey,
457
+ title,
458
+ contentType: 'text-content',
459
+ layoutType: 'document',
460
+ summary: `Conteudo indexado automaticamente de ${relativePath}.`,
461
+ blocks: [
462
+ {
463
+ type: 'rich-text',
464
+ content: truncateText(rawContent)
465
+ }
466
+ ],
467
+ meta: {
468
+ autoIndexed: true,
469
+ sourceFormat: 'markdown',
470
+ sourcePath: relativePath
471
+ }
472
+ };
473
+ }
474
+
475
+ if (ext === '.html' || ext === '.htm') {
476
+ const preview = stripHtml(rawContent);
477
+ return {
478
+ contentKey,
479
+ title,
480
+ contentType: 'html-content',
481
+ layoutType: 'document',
482
+ summary: `Conteudo HTML indexado automaticamente de ${relativePath}.`,
483
+ blocks: [
484
+ {
485
+ type: 'callout',
486
+ title: 'Preview indexado automaticamente',
487
+ content: 'O arquivo HTML original continua no output do squad. Este viewer mostra uma versao textual para indexacao e sync.'
488
+ },
489
+ {
490
+ type: 'rich-text',
491
+ content: truncateText(preview || 'Nao foi possivel gerar preview textual deste HTML.')
492
+ }
493
+ ],
494
+ meta: {
495
+ autoIndexed: true,
496
+ sourceFormat: 'html',
497
+ sourcePath: relativePath
498
+ }
499
+ };
500
+ }
501
+
502
+ return null;
503
+ }
504
+
505
+ async function resolveIngestCandidates(targetDir, options = {}) {
506
+ const outputRoot = path.join(targetDir, 'output');
507
+ const scopedRoot = options.squad ? path.join(outputRoot, String(options.squad).trim()) : outputRoot;
508
+ const rootExists = await fs.stat(scopedRoot).then((stat) => stat.isDirectory()).catch(() => false);
509
+
510
+ if (!rootExists) {
511
+ return [];
512
+ }
513
+
514
+ const allFiles = await listFilesRecursive(scopedRoot);
515
+ const contentJsonDirs = new Set(
516
+ allFiles
517
+ .filter((filePath) => path.basename(filePath).toLowerCase() === 'content.json')
518
+ .map((filePath) => path.dirname(filePath))
519
+ );
520
+
521
+ return allFiles.filter((filePath) => {
522
+ const ext = path.extname(filePath).toLowerCase();
523
+ const base = path.basename(filePath).toLowerCase();
524
+
525
+ if (base === 'content.json') return true;
526
+ if (ext !== '.md' && ext !== '.html' && ext !== '.htm') return false;
527
+ if (contentJsonDirs.has(path.dirname(filePath))) return false;
528
+
529
+ return true;
530
+ });
531
+ }
532
+
533
+ async function ingestContentCandidate(db, targetDir, absolutePath, options = {}) {
534
+ const baseName = path.basename(absolutePath).toLowerCase();
535
+ const relativePath = path.relative(targetDir, absolutePath).replace(/\\/g, '/');
536
+ const squadSlug = options.squad ? String(options.squad).trim() : inferSquadSlugFromOutputPath(targetDir, absolutePath);
537
+
538
+ if (!squadSlug) {
539
+ return { indexed: false, reason: 'missing_squad' };
540
+ }
541
+
542
+ if (baseName === 'content.json') {
543
+ const payload = await readJsonIfExists(absolutePath);
544
+ const validation = validateContentPayload(payload);
545
+ if (!validation.ok) {
546
+ return { indexed: false, reason: validation.reason };
547
+ }
548
+
549
+ const content = validation.normalized;
550
+ const siblingIndex = path.join(path.dirname(absolutePath), 'index.html');
551
+ const siblingHtmlExists = await fs.stat(siblingIndex).then((stat) => stat.isFile()).catch(() => false);
552
+
553
+ upsertContentItem(db, {
554
+ contentKey: content.contentKey,
555
+ taskKey: options.task || content.taskKey || content.task_key || null,
556
+ runKey: options.run || content.runKey || content.run_key || null,
557
+ squadSlug,
558
+ sessionKey: options.session || content.sessionKey || content.session_key || null,
559
+ title: content.title,
560
+ contentType: content.contentType,
561
+ layoutType: content.layoutType,
562
+ status: content.status || 'completed',
563
+ summary: content.summary || `Conteudo indexado automaticamente de ${relativePath}.`,
564
+ blueprintSlug: content.blueprint || null,
565
+ usedSkills: normalizeStringArray(options.usedSkills || content.usedSkills),
566
+ payload: content,
567
+ jsonPath: relativePath,
568
+ htmlPath: siblingHtmlExists
569
+ ? path.relative(targetDir, siblingIndex).replace(/\\/g, '/')
570
+ : null,
571
+ createdByAgent: options.agent || content.createdByAgent || content.created_by_agent || null
572
+ });
573
+
574
+ // Fire auto-delivery if configured (non-blocking)
575
+ runAutoDelivery(db, {
576
+ projectDir: targetDir,
577
+ squadSlug,
578
+ contentKey: content.contentKey,
579
+ contentPayload: content
580
+ }).catch(() => {}); // Swallow errors — delivery failure should not break ingestion
581
+
582
+ return { indexed: true, kind: 'content-json', contentKey: content.contentKey };
583
+ }
584
+
585
+ const rawContent = await fs.readFile(absolutePath, 'utf8').catch(() => '');
586
+ if (!rawContent.trim()) {
587
+ return { indexed: false, reason: 'empty_file' };
588
+ }
589
+
590
+ const payload = synthesizeContentPayload({
591
+ targetDir,
592
+ absolutePath,
593
+ squadSlug,
594
+ rawContent
595
+ });
596
+
597
+ if (!payload) {
598
+ return { indexed: false, reason: 'unsupported_file' };
599
+ }
600
+
601
+ upsertContentItem(db, {
602
+ contentKey: payload.contentKey,
603
+ taskKey: options.task || null,
604
+ runKey: options.run || null,
605
+ squadSlug,
606
+ sessionKey: options.session || null,
607
+ title: payload.title,
608
+ contentType: payload.contentType,
609
+ layoutType: payload.layoutType,
610
+ status: 'completed',
611
+ summary: payload.summary,
612
+ blueprintSlug: payload.blueprint || null,
613
+ usedSkills: normalizeStringArray(options.usedSkills),
614
+ payload,
615
+ jsonPath: null,
616
+ htmlPath: path.extname(absolutePath).toLowerCase().startsWith('.ht')
617
+ ? relativePath
618
+ : null,
619
+ createdByAgent: options.agent || null
620
+ });
621
+
622
+ // Fire auto-delivery if configured (non-blocking)
623
+ runAutoDelivery(db, {
624
+ projectDir: targetDir,
625
+ squadSlug,
626
+ contentKey: payload.contentKey,
627
+ contentPayload: payload
628
+ }).catch(() => {}); // Swallow errors — delivery failure should not break ingestion
629
+
630
+ return { indexed: true, kind: path.extname(absolutePath).toLowerCase(), contentKey: payload.contentKey };
631
+ }
632
+
633
+ async function withRuntimeDb(targetDir, t) {
634
+ const handle = await openRuntimeDb(targetDir, { mustExist: true });
635
+ if (!handle) {
636
+ throw new Error(t('runtime.store_missing', { path: resolveRuntimePaths(targetDir).dbPath }));
637
+ }
638
+ return handle;
639
+ }
640
+
641
+ async function runRuntimeInit({ args, options = {}, logger, t }) {
642
+ const targetDir = resolveTargetDir(args);
643
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
644
+ db.close();
645
+
646
+ if (!options.json) {
647
+ logger.log(t('runtime.init_ok', { path: dbPath }));
648
+ }
649
+
650
+ return { ok: true, targetDir, runtimeDir, dbPath };
651
+ }
652
+
653
+ async function runRuntimeIngest({ args, options = {}, logger, t }) {
654
+ const targetDir = resolveTargetDir(args);
655
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
656
+ const ingestOptions = {
657
+ ...options,
658
+ usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills)
659
+ };
660
+
661
+ try {
662
+ const candidates = await resolveIngestCandidates(targetDir, ingestOptions);
663
+ let indexed = 0;
664
+ let skipped = 0;
665
+ const reasons = [];
666
+
667
+ for (const candidate of candidates) {
668
+ const result = await ingestContentCandidate(db, targetDir, candidate, ingestOptions);
669
+ if (result.indexed) {
670
+ indexed += 1;
671
+ continue;
672
+ }
673
+ skipped += 1;
674
+ if (result.reason) {
675
+ reasons.push(`${path.relative(targetDir, candidate).replace(/\\/g, '/')}: ${result.reason}`);
676
+ }
677
+ }
678
+
679
+ if (!options.json) {
680
+ logger.log(
681
+ t('runtime.ingest_ok', {
682
+ indexed,
683
+ skipped,
684
+ path: dbPath
685
+ })
686
+ );
687
+ if (reasons.length > 0) {
688
+ for (const reason of reasons.slice(0, 10)) {
689
+ logger.log(`- ${reason}`);
690
+ }
691
+ }
692
+ }
693
+
694
+ return {
695
+ ok: true,
696
+ targetDir,
697
+ dbPath,
698
+ indexed,
699
+ skipped,
700
+ reasons
701
+ };
702
+ } finally {
703
+ db.close();
704
+ }
705
+ }
706
+
707
+ async function runRuntimeTaskStart({ args, options = {}, logger, t }) {
708
+ const targetDir = resolveTargetDir(args);
709
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
710
+
711
+ try {
712
+ const taskKey = startTask(db, {
713
+ taskKey: options.task,
714
+ squadSlug: options.squad,
715
+ sessionKey: options.session,
716
+ title: requireOption(options, 'title', t),
717
+ goal: options.goal,
718
+ createdBy: options.by
719
+ });
720
+
721
+ if (!options.json) {
722
+ logger.log(t('runtime.task_start_ok', { task: taskKey, path: dbPath }));
723
+ }
724
+
725
+ return {
726
+ ok: true,
727
+ targetDir,
728
+ dbPath,
729
+ taskKey,
730
+ status: 'running'
731
+ };
732
+ } finally {
733
+ db.close();
734
+ }
735
+ }
736
+
737
+ async function runRuntimeStart({ args, options = {}, logger, t }) {
738
+ const targetDir = resolveTargetDir(args);
739
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
740
+
741
+ try {
742
+ const runKey = startRun(db, {
743
+ runKey: options.run,
744
+ taskKey: options.task,
745
+ agentName: requireOption(options, 'agent', t),
746
+ agentKind: options.kind,
747
+ squadSlug: options.squad,
748
+ sessionKey: options.session,
749
+ title: options.title,
750
+ message: options.message,
751
+ summary: options.summary,
752
+ usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills),
753
+ outputPath: options.output
754
+ });
755
+
756
+ const snapshot = getStatusSnapshot(db);
757
+ if (!options.json) {
758
+ logger.log(t('runtime.start_ok', { run: runKey, path: dbPath }));
759
+ }
760
+
761
+ return {
762
+ ok: true,
763
+ targetDir,
764
+ dbPath,
765
+ runKey,
766
+ status: 'running',
767
+ activeCount: snapshot.activeRuns.length
768
+ };
769
+ } finally {
770
+ db.close();
771
+ }
772
+ }
773
+
774
+ async function runRuntimeUpdate({ args, options = {}, logger, t }) {
775
+ const targetDir = resolveTargetDir(args);
776
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
777
+
778
+ try {
779
+ const runKey = requireOption(options, 'run', t);
780
+ const status = updateRun(db, {
781
+ runKey,
782
+ status: 'running',
783
+ taskKey: options.task,
784
+ eventType: 'progress',
785
+ message: options.message,
786
+ summary: options.summary,
787
+ usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills),
788
+ outputPath: options.output
789
+ });
790
+
791
+ if (!options.json) {
792
+ logger.log(t('runtime.update_ok', { run: runKey, path: dbPath }));
793
+ }
794
+
795
+ return {
796
+ ok: true,
797
+ targetDir,
798
+ dbPath,
799
+ runKey,
800
+ status
801
+ };
802
+ } finally {
803
+ db.close();
804
+ }
805
+ }
806
+
807
+ async function runRuntimeFinish({ args, options = {}, logger, t }) {
808
+ const targetDir = resolveTargetDir(args);
809
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
810
+
811
+ try {
812
+ const runKey = requireOption(options, 'run', t);
813
+ const status = updateRun(db, {
814
+ runKey,
815
+ status: 'completed',
816
+ taskKey: options.task,
817
+ eventType: 'finish',
818
+ message: options.message || options.summary || 'Run completed',
819
+ summary: options.summary,
820
+ usedSkills: normalizeStringArray(options['used-skills'] || options.usedSkills),
821
+ outputPath: options.output
822
+ });
823
+
824
+ const finishedRun = db
825
+ .prepare('SELECT run_key, task_key, squad_slug, session_key, agent_name, output_path, used_skills_json FROM agent_runs WHERE run_key = ?')
826
+ .get(runKey);
827
+ if (finishedRun && finishedRun.output_path) {
828
+ attachArtifact(db, {
829
+ taskKey: finishedRun.task_key,
830
+ runKey: finishedRun.run_key,
831
+ squadSlug: finishedRun.squad_slug,
832
+ agentName: finishedRun.agent_name,
833
+ filePath: finishedRun.output_path,
834
+ title: options.title || options.summary || 'Artifact generated'
835
+ });
836
+
837
+ const absoluteOutputPath = path.isAbsolute(finishedRun.output_path)
838
+ ? finishedRun.output_path
839
+ : path.join(targetDir, finishedRun.output_path);
840
+ const contentPaths = maybeResolveContentPaths(targetDir, finishedRun.output_path);
841
+ const preferredCandidate = contentPaths
842
+ ? (await fs
843
+ .stat(contentPaths.absoluteJsonPath)
844
+ .then((stat) => (stat.isFile() ? contentPaths.absoluteJsonPath : null))
845
+ .catch(() => null))
846
+ : absoluteOutputPath;
847
+
848
+ if (preferredCandidate) {
849
+ const ingestion = await ingestContentCandidate(db, targetDir, preferredCandidate, {
850
+ task: finishedRun.task_key,
851
+ run: finishedRun.run_key,
852
+ squad: finishedRun.squad_slug,
853
+ session: options.session || finishedRun.session_key,
854
+ agent: finishedRun.agent_name,
855
+ usedSkills: parseJsonArray(finishedRun.used_skills_json)
856
+ });
857
+ if (!ingestion.indexed && !options.json && logger?.log) {
858
+ logger.log(`[runtime] skipped content indexing for ${finishedRun.run_key}: ${ingestion.reason}`);
859
+ }
860
+ }
861
+ }
862
+
863
+ if (!options.json) {
864
+ logger.log(t('runtime.finish_ok', { run: runKey, path: dbPath }));
865
+ }
866
+
867
+ return {
868
+ ok: true,
869
+ targetDir,
870
+ dbPath,
871
+ runKey,
872
+ status
873
+ };
874
+ } finally {
875
+ db.close();
876
+ }
877
+ }
878
+
879
+ async function runRuntimeTaskFinish({ args, options = {}, logger, t }) {
880
+ const targetDir = resolveTargetDir(args);
881
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
882
+
883
+ try {
884
+ const taskKey = requireOption(options, 'task', t);
885
+ const taskStatus = updateTask(db, {
886
+ taskKey,
887
+ status: 'completed',
888
+ goal: options.goal
889
+ });
890
+
891
+ if (!options.json) {
892
+ logger.log(t('runtime.task_finish_ok', { task: taskKey, path: dbPath }));
893
+ }
894
+
895
+ return {
896
+ ok: true,
897
+ targetDir,
898
+ dbPath,
899
+ taskKey,
900
+ status: taskStatus
901
+ };
902
+ } finally {
903
+ db.close();
904
+ }
905
+ }
906
+
907
+ async function runRuntimeFail({ args, options = {}, logger, t }) {
908
+ const targetDir = resolveTargetDir(args);
909
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
910
+
911
+ try {
912
+ const runKey = requireOption(options, 'run', t);
913
+ const status = updateRun(db, {
914
+ runKey,
915
+ taskKey: options.task,
916
+ status: 'failed',
917
+ eventType: 'fail',
918
+ message: options.message || options.summary || 'Run failed',
919
+ summary: options.summary,
920
+ outputPath: options.output
921
+ });
922
+
923
+ if (!options.json) {
924
+ logger.log(t('runtime.fail_ok', { run: runKey, path: dbPath }));
925
+ }
926
+
927
+ return {
928
+ ok: true,
929
+ targetDir,
930
+ dbPath,
931
+ runKey,
932
+ status
933
+ };
934
+ } finally {
935
+ db.close();
936
+ }
937
+ }
938
+
939
+ async function runRuntimeTaskFail({ args, options = {}, logger, t }) {
940
+ const targetDir = resolveTargetDir(args);
941
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
942
+
943
+ try {
944
+ const taskKey = requireOption(options, 'task', t);
945
+ const status = updateTask(db, {
946
+ taskKey,
947
+ status: 'failed',
948
+ goal: options.goal
949
+ });
950
+
951
+ if (!options.json) {
952
+ logger.log(t('runtime.task_fail_ok', { task: taskKey, path: dbPath }));
953
+ }
954
+
955
+ return {
956
+ ok: true,
957
+ targetDir,
958
+ dbPath,
959
+ taskKey,
960
+ status
961
+ };
962
+ } finally {
963
+ db.close();
964
+ }
965
+ }
966
+
967
+ async function runRuntimeStatus({ args, options = {}, logger, t }) {
968
+ const targetDir = resolveTargetDir(args);
969
+ const { dbPath } = resolveRuntimePaths(targetDir);
970
+
971
+ if (!(await runtimeStoreExists(targetDir))) {
972
+ if (options.json) {
973
+ return { ok: false, error: 'store_missing', dbPath };
974
+ }
975
+ throw new Error(t('runtime.store_missing', { path: dbPath }));
976
+ }
977
+
978
+ const { db } = await openRuntimeDb(targetDir, { mustExist: true });
979
+
980
+ try {
981
+ const snapshot = getStatusSnapshot(db);
982
+ const payload = {
983
+ ok: true,
984
+ targetDir,
985
+ dbPath,
986
+ taskCounts: snapshot.taskCounts,
987
+ counts: snapshot.counts,
988
+ activeTasks: snapshot.activeTasks,
989
+ recentTasks: snapshot.recentTasks,
990
+ activeRuns: snapshot.activeRuns,
991
+ recentRuns: snapshot.recentRuns,
992
+ activeLiveSessions: snapshot.activeLiveSessions,
993
+ activeMicroTasks: snapshot.activeMicroTasks,
994
+ recentLiveSessions: snapshot.recentLiveSessions,
995
+ recentMicroTasks: snapshot.recentMicroTasks,
996
+ recentHandoffs: snapshot.recentHandoffs,
997
+ recentArtifacts: snapshot.recentArtifacts,
998
+ recentContentItems: snapshot.recentContentItems,
999
+ recentExecutionEvents: snapshot.recentExecutionEvents
1000
+ };
1001
+
1002
+ if (!options.json) {
1003
+ logger.log(t('runtime.status_title', { path: targetDir }));
1004
+ logger.log(t('runtime.status_db', { path: dbPath }));
1005
+ logger.log(
1006
+ t('runtime.status_task_counts', {
1007
+ queued: payload.taskCounts.queued,
1008
+ running: payload.taskCounts.running,
1009
+ completed: payload.taskCounts.completed,
1010
+ failed: payload.taskCounts.failed
1011
+ })
1012
+ );
1013
+ logger.log(
1014
+ t('runtime.status_counts', {
1015
+ queued: payload.counts.queued,
1016
+ running: payload.counts.running,
1017
+ completed: payload.counts.completed,
1018
+ failed: payload.counts.failed
1019
+ })
1020
+ );
1021
+ if (snapshot.activeTasks.length === 0) {
1022
+ logger.log(t('runtime.status_no_active_tasks'));
1023
+ } else {
1024
+ logger.log(t('runtime.status_active_tasks_title'));
1025
+ for (const task of snapshot.activeTasks) {
1026
+ logger.log(
1027
+ t('runtime.status_active_task_line', {
1028
+ task: task.task_key,
1029
+ squad: task.squad_slug || '—',
1030
+ status: task.status,
1031
+ title: task.title
1032
+ })
1033
+ );
1034
+ }
1035
+ }
1036
+ if (snapshot.activeRuns.length === 0) {
1037
+ logger.log(t('runtime.status_no_active'));
1038
+ } else {
1039
+ logger.log(t('runtime.status_active_title'));
1040
+ for (const run of snapshot.activeRuns) {
1041
+ logger.log(
1042
+ t('runtime.status_active_line', {
1043
+ agent: run.agent_name,
1044
+ squad: run.squad_slug || '—',
1045
+ status: run.status,
1046
+ title: run.title || run.summary || '—'
1047
+ })
1048
+ );
1049
+ }
1050
+ }
1051
+ if (snapshot.activeLiveSessions.length > 0) {
1052
+ logger.log(t('runtime.status_live_sessions_title'));
1053
+ for (const task of snapshot.activeLiveSessions) {
1054
+ logger.log(
1055
+ t('runtime.status_live_session_line', {
1056
+ task: task.task_key,
1057
+ agent: task.latest_agent_name || task.created_by || '—',
1058
+ status: task.status,
1059
+ plan: task.plan_steps_total > 0 ? `${task.plan_steps_done}/${task.plan_steps_total}` : '—',
1060
+ micro: `${task.completed_child_task_count || 0}/${task.child_task_count || 0}`,
1061
+ handoffs: task.handoff_count || 0,
1062
+ title: task.title || '—'
1063
+ })
1064
+ );
1065
+ }
1066
+ }
1067
+ if (snapshot.activeMicroTasks.length > 0) {
1068
+ logger.log(t('runtime.status_micro_tasks_title'));
1069
+ for (const task of snapshot.activeMicroTasks) {
1070
+ logger.log(
1071
+ t('runtime.status_micro_task_line', {
1072
+ task: task.task_key,
1073
+ parent: task.parent_task_key || '—',
1074
+ status: task.status,
1075
+ title: task.title || task.goal || '—'
1076
+ })
1077
+ );
1078
+ }
1079
+ }
1080
+ if (snapshot.recentHandoffs.length > 0) {
1081
+ logger.log(t('runtime.status_handoffs_title'));
1082
+ for (const event of snapshot.recentHandoffs.slice(0, 5)) {
1083
+ logger.log(
1084
+ t('runtime.status_handoff_line', {
1085
+ created: event.created_at,
1086
+ from: event.handoff_from || event.agent_name || '—',
1087
+ to: event.handoff_to || '—',
1088
+ session: event.session_key || '—',
1089
+ message: event.message || '—'
1090
+ })
1091
+ );
1092
+ }
1093
+ }
1094
+ }
1095
+
1096
+ return payload;
1097
+ } finally {
1098
+ db.close();
1099
+ }
1100
+ }
1101
+
1102
+ /**
1103
+ * aioson runtime-log --agent=<name> --message=<text> [--type=<event>] [--finish] [--status=completed|failed] [--summary=<text>] [--title=<task-title>]
1104
+ *
1105
+ * Stateful single-command logger for official AIOSON agents.
1106
+ * First call creates task + run in SQLite; subsequent calls add events.
1107
+ * --finish closes the run and clears the session.
1108
+ */
1109
+ async function runRuntimeLog({ args, options = {}, logger, t }) {
1110
+ const targetDir = resolveTargetDir(args);
1111
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1112
+
1113
+ try {
1114
+ const agentName = options.agent;
1115
+ if (!agentName) {
1116
+ throw new Error(t('runtime.log_agent_required'));
1117
+ }
1118
+
1119
+ const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1120
+ agentName,
1121
+ squadSlug: options.squad || null,
1122
+ message: options.message || '',
1123
+ type: options.type || 'status',
1124
+ taskTitle: options.title,
1125
+ finish: Boolean(options.finish),
1126
+ status: options.status,
1127
+ summary: options.summary,
1128
+ meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1129
+ });
1130
+
1131
+ // Generate session handoff on --finish
1132
+ if (options.finish) {
1133
+ const handoffData = buildRuntimeLogHandoff(
1134
+ agentName,
1135
+ options.message || '',
1136
+ options.summary || ''
1137
+ );
1138
+ await writeHandoff(targetDir, handoffData);
1139
+ }
1140
+
1141
+ if (!options.json) {
1142
+ const isFinish = Boolean(options.finish);
1143
+ logger.log(isFinish
1144
+ ? t('runtime.log_finish_ok', { agent: agentName, run: runKey, path: dbPath })
1145
+ : t('runtime.log_ok', { agent: agentName, run: runKey, path: dbPath })
1146
+ );
1147
+ }
1148
+
1149
+ return {
1150
+ ok: true,
1151
+ targetDir,
1152
+ dbPath,
1153
+ runKey,
1154
+ taskKey,
1155
+ agent: agentName,
1156
+ finished: Boolean(options.finish)
1157
+ };
1158
+ } finally {
1159
+ db.close();
1160
+ }
1161
+ }
1162
+
1163
+
1164
+ /**
1165
+ * aioson agent:done . --agent=<name> --summary="..." [--title="..."] [--status=completed|failed]
1166
+ *
1167
+ * Safe self-registration for official agents invoked directly (not via workflow:next or live:start).
1168
+ * - If an active live session exists for the agent: appends a completion event without closing the session.
1169
+ * - If no session exists: creates a standalone task+run and immediately marks it completed.
1170
+ *
1171
+ * Intended to be called ONCE at the very end of an agent session, after delivering the main artifact.
1172
+ */
1173
+ async function runAgentDone({ args, options = {}, logger, t }) {
1174
+ const targetDir = resolveTargetDir(args);
1175
+ const agentName = String(options.agent || '').trim();
1176
+ if (!agentName) {
1177
+ throw new Error('--agent is required');
1178
+ }
1179
+ const normalizedAgent = agentName.startsWith('@') ? agentName : `@${agentName}`;
1180
+ const summary = String(options.summary || options.message || `${normalizedAgent} session completed`).trim();
1181
+ const title = options.title ? String(options.title).trim() : null;
1182
+ const status = options.status || 'completed';
1183
+ const verdict = options.verdict ? String(options.verdict).trim().toUpperCase() : null;
1184
+ const planStepId = options['plan-step'] ? String(options['plan-step']).trim() : null;
1185
+ const artifactPaths = options.artifacts
1186
+ ? String(options.artifacts).split(',').map((p) => p.trim()).filter(Boolean)
1187
+ : [];
1188
+
1189
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1190
+
1191
+ try {
1192
+ const session = await readAgentSession(runtimeDir, normalizedAgent);
1193
+ const hasActiveSession = session && !session.finished && session.runKey;
1194
+
1195
+ if (hasActiveSession) {
1196
+ // Live or tracked session is already open only append a completion note.
1197
+ // Do NOT close the session: live:handoff or live:close owns the lifecycle.
1198
+ appendRunEvent(db, {
1199
+ runKey: session.runKey,
1200
+ eventType: 'agent_done',
1201
+ phase: 'live',
1202
+ status: 'running',
1203
+ message: summary,
1204
+ verdict,
1205
+ planStepId
1206
+ });
1207
+
1208
+ if (artifactPaths.length > 0) {
1209
+ for (const filePath of artifactPaths) {
1210
+ try {
1211
+ attachArtifact(db, {
1212
+ runKey: session.runKey,
1213
+ agentName: normalizedAgent,
1214
+ kind: 'output',
1215
+ filePath
1216
+ });
1217
+ } catch { /* non-fatal */ }
1218
+ }
1219
+ }
1220
+
1221
+ if (!options.json) {
1222
+ logger.log(`agent:done — ${normalizedAgent} | live session active, event logged | run: ${session.runKey} (${dbPath})`);
1223
+ }
1224
+
1225
+ if (isDocCreatingAgent(normalizedAgent)) {
1226
+ backupAiosonDocs(targetDir).catch(() => {});
1227
+ }
1228
+
1229
+ // Living Memory: best-effort reflection (never blocks the close)
1230
+ try {
1231
+ await runMemoryReflectPrepare({
1232
+ args: [targetDir],
1233
+ options: { agent: normalizedAgent.replace(/^@/, ''), json: true },
1234
+ logger: { log: () => {}, error: () => {} }
1235
+ });
1236
+ } catch { /* ignore */ }
1237
+
1238
+ return { ok: true, targetDir, dbPath, agent: normalizedAgent, mode: 'live_event', runKey: session.runKey };
1239
+ }
1240
+
1241
+ // No active session — create a standalone task+run and immediately complete it.
1242
+ const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1243
+ agentName: normalizedAgent,
1244
+ message: summary,
1245
+ type: 'completed',
1246
+ taskTitle: title || normalizedAgent,
1247
+ finish: true,
1248
+ status,
1249
+ summary
1250
+ });
1251
+
1252
+ if (verdict || planStepId) {
1253
+ appendRunEvent(db, {
1254
+ runKey,
1255
+ eventType: 'agent_done',
1256
+ phase: 'direct',
1257
+ status: 'completed',
1258
+ message: summary,
1259
+ verdict,
1260
+ planStepId
1261
+ });
1262
+ }
1263
+
1264
+ if (artifactPaths.length > 0) {
1265
+ for (const filePath of artifactPaths) {
1266
+ try {
1267
+ attachArtifact(db, {
1268
+ runKey,
1269
+ taskKey,
1270
+ agentName: normalizedAgent,
1271
+ kind: 'output',
1272
+ filePath
1273
+ });
1274
+ } catch { /* non-fatal */ }
1275
+ }
1276
+ }
1277
+
1278
+ if (!options.json) {
1279
+ logger.log(`agent:done — ${normalizedAgent} | task: ${taskKey} | run: ${runKey} (${dbPath})`);
1280
+ }
1281
+
1282
+ if (isDocCreatingAgent(normalizedAgent)) {
1283
+ backupAiosonDocs(targetDir).catch(() => {});
1284
+ }
1285
+
1286
+ // Living Memory: best-effort reflection (never blocks the close)
1287
+ try {
1288
+ await runMemoryReflectPrepare({
1289
+ args: [targetDir],
1290
+ options: { agent: normalizedAgent.replace(/^@/, ''), json: true },
1291
+ logger: { log: () => {}, error: () => {} }
1292
+ });
1293
+ } catch { /* ignore */ }
1294
+
1295
+ return { ok: true, targetDir, dbPath, agent: normalizedAgent, mode: 'standalone', runKey, taskKey };
1296
+ } finally {
1297
+ db.close();
1298
+ }
1299
+ }
1300
+
1301
+
1302
+ async function runRuntimeSessionStart({ args, options = {}, logger, t }) {
1303
+ const targetDir = resolveTargetDir(args);
1304
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1305
+
1306
+ try {
1307
+ const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1308
+ const existingSnapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, { limit: options.limit });
1309
+
1310
+ if (existingSnapshot.session && !existingSnapshot.open) {
1311
+ await clearAgentSession(runtimeDir, agentName);
1312
+ }
1313
+
1314
+ if (existingSnapshot.open) {
1315
+ if (!options.json) {
1316
+ logger.log(`Direct session already active: ${agentName} | task: ${existingSnapshot.task?.task_key || '—'} | run: ${existingSnapshot.run?.run_key || '—'} (${dbPath})`);
1317
+ }
1318
+ return {
1319
+ ok: true,
1320
+ targetDir,
1321
+ dbPath,
1322
+ agent: agentName,
1323
+ taskKey: existingSnapshot.task?.task_key || existingSnapshot.session?.taskKey || null,
1324
+ runKey: existingSnapshot.run?.run_key || existingSnapshot.session?.runKey || null,
1325
+ sessionKey: existingSnapshot.sessionKey,
1326
+ status: existingSnapshot.run?.status || 'running',
1327
+ reused: true,
1328
+ open: true
1329
+ };
1330
+ }
1331
+
1332
+ const sessionKey = options.session ? String(options.session).trim() : makeDirectSessionKey(agentName);
1333
+ const title = options.title ? String(options.title).trim() : `Direct session ${agentName}`;
1334
+ const message = options.message ? String(options.message).trim() : `Session started for ${agentName}`;
1335
+ const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1336
+ agentName,
1337
+ message,
1338
+ type: options.type || 'session.start',
1339
+ taskTitle: title,
1340
+ sessionKey,
1341
+ meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1342
+ });
1343
+
1344
+ if (!options.json) {
1345
+ logger.log(`Direct session started: ${agentName} | task: ${taskKey} | run: ${runKey} (${dbPath})`);
1346
+ }
1347
+
1348
+ return {
1349
+ ok: true,
1350
+ targetDir,
1351
+ dbPath,
1352
+ agent: agentName,
1353
+ taskKey,
1354
+ runKey,
1355
+ sessionKey,
1356
+ status: 'running',
1357
+ reused: false,
1358
+ open: true
1359
+ };
1360
+ } finally {
1361
+ db.close();
1362
+ }
1363
+ }
1364
+
1365
+ async function runRuntimeSessionLog({ args, options = {}, logger, t }) {
1366
+ const targetDir = resolveTargetDir(args);
1367
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1368
+
1369
+ try {
1370
+ const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1371
+ const message = requireOption(options, 'message', t);
1372
+ const existingSnapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, { limit: options.limit });
1373
+
1374
+ if (existingSnapshot.session && !existingSnapshot.open) {
1375
+ await clearAgentSession(runtimeDir, agentName);
1376
+ }
1377
+
1378
+ const autoStarted = !existingSnapshot.open;
1379
+ const sessionKey = existingSnapshot.sessionKey || (options.session ? String(options.session).trim() : makeDirectSessionKey(agentName));
1380
+ const title = options.title ? String(options.title).trim() : `Direct session ${agentName}`;
1381
+ const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1382
+ agentName,
1383
+ message,
1384
+ type: options.type || 'session.log',
1385
+ taskTitle: title,
1386
+ sessionKey,
1387
+ meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1388
+ });
1389
+
1390
+ if (!options.json) {
1391
+ logger.log(`Direct session log recorded: ${agentName} | run: ${runKey} (${dbPath})`);
1392
+ }
1393
+
1394
+ return {
1395
+ ok: true,
1396
+ targetDir,
1397
+ dbPath,
1398
+ agent: agentName,
1399
+ taskKey,
1400
+ runKey,
1401
+ sessionKey,
1402
+ status: 'running',
1403
+ autoStarted,
1404
+ open: true
1405
+ };
1406
+ } finally {
1407
+ db.close();
1408
+ }
1409
+ }
1410
+
1411
+ async function runRuntimeSessionFinish({ args, options = {}, logger, t }) {
1412
+ const targetDir = resolveTargetDir(args);
1413
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1414
+
1415
+ try {
1416
+ const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1417
+ const existingSnapshot = await collectRuntimeSessionSnapshot(db, runtimeDir, agentName, { limit: options.limit });
1418
+
1419
+ if (!existingSnapshot.open) {
1420
+ throw new Error(`No active direct session for ${agentName}.`);
1421
+ }
1422
+
1423
+ const summary = options.summary ? String(options.summary).trim() : '';
1424
+ const message = options.message ? String(options.message).trim() : (summary || `Session finished for ${agentName}`);
1425
+ const { runKey, taskKey } = await logAgentEvent(db, runtimeDir, {
1426
+ agentName,
1427
+ message,
1428
+ type: options.type || 'session.finish',
1429
+ finish: true,
1430
+ status: options.status || 'completed',
1431
+ summary,
1432
+ meta: options.meta ? (() => { try { return JSON.parse(options.meta); } catch { return { raw: options.meta }; } })() : undefined
1433
+ });
1434
+
1435
+ if (!options.json) {
1436
+ logger.log(`Direct session finished: ${agentName} | run: ${runKey} (${dbPath})`);
1437
+ }
1438
+
1439
+ return {
1440
+ ok: true,
1441
+ targetDir,
1442
+ dbPath,
1443
+ agent: agentName,
1444
+ taskKey,
1445
+ runKey,
1446
+ sessionKey: existingSnapshot.sessionKey,
1447
+ status: options.status || 'completed',
1448
+ finished: true,
1449
+ open: false
1450
+ };
1451
+ } finally {
1452
+ db.close();
1453
+ }
1454
+ }
1455
+
1456
+ async function runRuntimeSessionStatus({ args, options = {}, logger, t }) {
1457
+ const targetDir = resolveTargetDir(args);
1458
+ const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1459
+ const watchSeconds = parseWatchSeconds(options.watch);
1460
+
1461
+ if (watchSeconds && options.json) {
1462
+ throw new Error('--watch cannot be combined with --json.');
1463
+ }
1464
+
1465
+ if (!watchSeconds) {
1466
+ const snapshot = await getRuntimeSessionSnapshot(targetDir, agentName, t, { limit: options.limit });
1467
+ if (!options.json) {
1468
+ printRuntimeSessionSnapshot(snapshot, logger);
1469
+ }
1470
+ return snapshot;
1471
+ }
1472
+
1473
+ while (true) {
1474
+ const snapshot = await getRuntimeSessionSnapshot(targetDir, agentName, t, { limit: options.limit });
1475
+ if (process.stdout && process.stdout.isTTY) {
1476
+ process.stdout.write('\x1Bc');
1477
+ }
1478
+ printRuntimeSessionSnapshot(snapshot, logger);
1479
+ await sleep(Math.round(watchSeconds * 1000));
1480
+ }
1481
+ }
1482
+
1483
+ async function runDeliver({ args, options = {}, logger, t }) {
1484
+ const targetDir = resolveTargetDir(args);
1485
+ const squadSlug = requireOption(options, 'squad', t);
1486
+ const contentKey = options['content-key'] || options.contentKey || null;
1487
+ const triggerType = options.trigger || 'manual';
1488
+
1489
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
1490
+
1491
+ try {
1492
+ const { runManualDelivery } = require('../delivery-runner');
1493
+
1494
+ // Optionally load content payload from DB
1495
+ let contentPayload = null;
1496
+ if (contentKey) {
1497
+ const row = db.prepare('SELECT payload_json FROM content_items WHERE content_key = ? AND squad_slug = ?').get(contentKey, squadSlug);
1498
+ if (row && row.payload_json) {
1499
+ try { contentPayload = JSON.parse(row.payload_json); } catch { /* ignore */ }
1500
+ }
1501
+ }
1502
+
1503
+ const result = await runManualDelivery(db, {
1504
+ projectDir: targetDir,
1505
+ squadSlug,
1506
+ contentKey,
1507
+ triggerType,
1508
+ contentPayload
1509
+ });
1510
+
1511
+ if (!result.delivered) {
1512
+ logger.log(`Delivery skipped: ${result.reason}`);
1513
+ return { ok: false, ...result };
1514
+ }
1515
+
1516
+ for (const r of result.results || []) {
1517
+ const status = r.ok ? 'OK' : 'FAIL';
1518
+ logger.log(` ${status} ${r.webhookSlug} — ${r.statusCode || 'no response'} (${r.attempts} attempt${r.attempts > 1 ? 's' : ''})`);
1519
+ if (r.error) logger.log(` Error: ${r.error}`);
1520
+ }
1521
+
1522
+ logger.log(`\nDelivery ${result.allOk ? 'completed' : 'completed with errors'}.`);
1523
+ return { ok: result.allOk, ...result };
1524
+ } finally {
1525
+ db.close();
1526
+ }
1527
+ }
1528
+
1529
+ async function findManifestPath(projectDir, slug) {
1530
+ const candidates = [
1531
+ path.join(projectDir, '.aioson', 'squads', slug, 'squad.manifest.json'),
1532
+ path.join(projectDir, 'agents', slug, 'squad.manifest.json')
1533
+ ];
1534
+ for (const p of candidates) {
1535
+ try { await fs.stat(p); return p; } catch { continue; }
1536
+ }
1537
+ return null;
1538
+ }
1539
+
1540
+ async function runOutputStrategyExport({ args, options = {}, logger, t }) {
1541
+ const projectDir = resolveTargetDir(args);
1542
+ const slug = requireOption(options, 'squad', t);
1543
+ const manifestPath = await findManifestPath(projectDir, slug);
1544
+
1545
+ if (!manifestPath) {
1546
+ logger.error(`Manifest not found for squad "${slug}"`);
1547
+ return { ok: false, error: 'Manifest not found' };
1548
+ }
1549
+
1550
+ const raw = await fs.readFile(manifestPath, 'utf8');
1551
+ const manifest = JSON.parse(raw);
1552
+ const strategy = manifest.outputStrategy || null;
1553
+
1554
+ if (!strategy) {
1555
+ logger.log(`Squad "${slug}" has no outputStrategy configured.`);
1556
+ return { ok: false, error: 'No outputStrategy found' };
1557
+ }
1558
+
1559
+ const exportsDir = path.join(projectDir, '.aioson', 'squads', 'exports');
1560
+ await fs.mkdir(exportsDir, { recursive: true });
1561
+ const outFile = path.join(exportsDir, `${slug}.output-strategy.json`);
1562
+ await fs.writeFile(outFile, JSON.stringify(strategy, null, 2) + '\n', 'utf8');
1563
+
1564
+ const relOut = path.relative(projectDir, outFile).replace(/\\/g, '/');
1565
+ logger.log(`Exported outputStrategy from "${slug}" ${relOut}`);
1566
+ return { ok: true, file: relOut, strategy };
1567
+ }
1568
+
1569
+ async function runOutputStrategyImport({ args, options = {}, logger, t }) {
1570
+ const projectDir = resolveTargetDir(args);
1571
+ const slug = requireOption(options, 'squad', t);
1572
+ const fromSlug = options.from || null;
1573
+ const fromFile = options.file || null;
1574
+
1575
+ if (!fromSlug && !fromFile) {
1576
+ logger.error('Usage: aioson output-strategy:import --squad=<target> --from=<source-slug> | --file=<path>');
1577
+ return { ok: false, error: 'Provide --from or --file' };
1578
+ }
1579
+
1580
+ // Load source strategy
1581
+ let strategy;
1582
+ if (fromFile) {
1583
+ const absFile = path.resolve(projectDir, fromFile);
1584
+ const raw = await fs.readFile(absFile, 'utf8');
1585
+ strategy = JSON.parse(raw);
1586
+ } else {
1587
+ const srcPath = await findManifestPath(projectDir, fromSlug);
1588
+ if (!srcPath) {
1589
+ logger.error(`Source squad "${fromSlug}" manifest not found`);
1590
+ return { ok: false, error: 'Source manifest not found' };
1591
+ }
1592
+ const srcManifest = JSON.parse(await fs.readFile(srcPath, 'utf8'));
1593
+ strategy = srcManifest.outputStrategy || null;
1594
+ if (!strategy) {
1595
+ logger.error(`Source squad "${fromSlug}" has no outputStrategy`);
1596
+ return { ok: false, error: 'Source has no outputStrategy' };
1597
+ }
1598
+ }
1599
+
1600
+ // Write to target
1601
+ const targetPath = await findManifestPath(projectDir, slug);
1602
+ if (!targetPath) {
1603
+ logger.error(`Target squad "${slug}" manifest not found`);
1604
+ return { ok: false, error: 'Target manifest not found' };
1605
+ }
1606
+
1607
+ const targetManifest = JSON.parse(await fs.readFile(targetPath, 'utf8'));
1608
+ targetManifest.outputStrategy = strategy;
1609
+ await fs.writeFile(targetPath, JSON.stringify(targetManifest, null, 2) + '\n', 'utf8');
1610
+
1611
+ logger.log(`Imported outputStrategy into "${slug}" from ${fromSlug || fromFile}`);
1612
+ return { ok: true, squad: slug, source: fromSlug || fromFile };
1613
+ }
1614
+
1615
+ /**
1616
+ * aioson devlog:sync [targetDir]
1617
+ *
1618
+ * Parses aioson-logs/devlog-*.md files, imports them into SQLite as
1619
+ * task + run + events, then renames each file to .synced so it is not
1620
+ * re-imported on subsequent runs.
1621
+ */
1622
+ async function runDevlogSync({ args, options = {}, logger, t }) {
1623
+ const targetDir = resolveTargetDir(args);
1624
+ const logsDir = path.join(targetDir, 'aioson-logs');
1625
+
1626
+ let entries;
1627
+ try {
1628
+ entries = await fs.readdir(logsDir);
1629
+ } catch {
1630
+ logger.log('No aioson-logs/ directory found — nothing to sync.');
1631
+ return { ok: true, synced: 0 };
1632
+ }
1633
+
1634
+ const devlogFiles = entries
1635
+ .filter(f => f.startsWith('devlog-') && f.endsWith('.md'))
1636
+ .sort();
1637
+
1638
+ if (devlogFiles.length === 0) {
1639
+ logger.log('No devlog files to sync.');
1640
+ return { ok: true, synced: 0 };
1641
+ }
1642
+
1643
+ const { db, dbPath } = await openRuntimeDb(targetDir);
1644
+ let synced = 0;
1645
+ const parsedDevlogs = [];
1646
+
1647
+ try {
1648
+ for (const file of devlogFiles) {
1649
+ const filePath = path.join(logsDir, file);
1650
+ const raw = await fs.readFile(filePath, 'utf8');
1651
+
1652
+ // Parse YAML frontmatter
1653
+ const fm = parseFrontmatter(raw);
1654
+ const agent = fm.agent || 'unknown';
1655
+ const summary = fm.summary || file;
1656
+ const sessionStart = fm.session_start || null;
1657
+ const sessionEnd = fm.session_end || null;
1658
+ const status = fm.status || 'completed';
1659
+ const body = raw.replace(/^---[\s\S]*?---\s*/, '');
1660
+
1661
+ parsedDevlogs.push({ filename: file, agent, summary, sessionStart, sessionEnd, status, body });
1662
+
1663
+ // Create task + run
1664
+ const taskKey = startTask(db, {
1665
+ title: `devlog: ${summary}`,
1666
+ squadSlug: null,
1667
+ status: status === 'partial' ? 'running' : 'completed',
1668
+ createdBy: agent
1669
+ });
1670
+
1671
+ const runKey = startRun(db, {
1672
+ taskKey,
1673
+ agentName: agent,
1674
+ agentKind: 'devlog',
1675
+ squadSlug: null,
1676
+ title: `@${agent} devlog`,
1677
+ message: summary
1678
+ });
1679
+
1680
+ // Extract body sections as events
1681
+ const sections = body.split(/^## /m).filter(Boolean);
1682
+ for (const section of sections) {
1683
+ const firstLine = section.split('\n')[0].trim();
1684
+ const content = section.slice(firstLine.length).trim();
1685
+ if (content) {
1686
+ appendRunEvent(db, {
1687
+ runKey,
1688
+ eventType: 'devlog',
1689
+ phase: firstLine.toLowerCase().replace(/\s+/g, '_'),
1690
+ status: 'completed',
1691
+ message: `## ${firstLine}\n${content}`,
1692
+ createdAt: sessionEnd || new Date().toISOString()
1693
+ });
1694
+ }
1695
+ }
1696
+
1697
+ // Close the run
1698
+ updateRun(db, runKey, {
1699
+ status: status === 'partial' ? 'running' : 'completed',
1700
+ summary,
1701
+ finishedAt: sessionEnd || new Date().toISOString()
1702
+ });
1703
+
1704
+ if (status !== 'partial') {
1705
+ updateTask(db, taskKey, {
1706
+ status: 'completed',
1707
+ finishedAt: sessionEnd || new Date().toISOString()
1708
+ });
1709
+ }
1710
+
1711
+ // Rename to .synced
1712
+ await fs.rename(filePath, filePath.replace(/\.md$/, '.synced.md'));
1713
+ synced++;
1714
+ logger.log(` Synced: ${file} task=${taskKey} run=${runKey}`);
1715
+ }
1716
+
1717
+ logger.log(`Synced ${synced} devlog(s) into ${dbPath}`);
1718
+
1719
+ // Cloud sync
1720
+ if (options.cloud) {
1721
+ const cloudResult = await syncDevlogsToCloud(targetDir, parsedDevlogs, options, logger);
1722
+ return { ok: true, synced, dbPath, cloud: cloudResult };
1723
+ }
1724
+
1725
+ return { ok: true, synced, dbPath };
1726
+ } finally {
1727
+ db.close();
1728
+ }
1729
+ }
1730
+
1731
+ /**
1732
+ * Sends parsed devlogs to the cloud endpoint.
1733
+ * Reads cloud config from .aioson/install.json or --url / --token options.
1734
+ */
1735
+ async function syncDevlogsToCloud(targetDir, devlogs, options, logger) {
1736
+ const cloudUrl = options.url || options['cloud-url'] || await resolveCloudUrl(targetDir);
1737
+ const cloudToken = options.token || options['cloud-token'] || await resolveCloudToken(targetDir);
1738
+
1739
+ if (!cloudUrl) {
1740
+ logger.error('Cloud URL not configured. Use --url or set cloudBaseUrl in dashboard project settings.');
1741
+ return { ok: false, error: 'missing_cloud_url' };
1742
+ }
1743
+ if (!cloudToken) {
1744
+ logger.error('Cloud token not configured. Use --token or set cloudApiToken in dashboard project settings.');
1745
+ return { ok: false, error: 'missing_cloud_token' };
1746
+ }
1747
+
1748
+ const endpoint = `${cloudUrl.replace(/\/+$/, '')}/api/publish/runtime`;
1749
+ const payload = {
1750
+ tasks: [],
1751
+ devlogs: devlogs.map(d => ({
1752
+ filename: d.filename,
1753
+ agent: d.agent,
1754
+ sessionStart: d.sessionStart,
1755
+ sessionEnd: d.sessionEnd,
1756
+ status: d.status,
1757
+ summary: d.summary,
1758
+ body: d.body
1759
+ }))
1760
+ };
1761
+
1762
+ logger.log(` Pushing ${devlogs.length} devlog(s) to ${endpoint}...`);
1763
+
1764
+ const response = await fetch(endpoint, {
1765
+ method: 'POST',
1766
+ headers: {
1767
+ 'accept': 'application/json',
1768
+ 'content-type': 'application/json',
1769
+ 'authorization': `Bearer ${cloudToken}`
1770
+ },
1771
+ body: JSON.stringify(payload),
1772
+ signal: AbortSignal.timeout(15000)
1773
+ });
1774
+
1775
+ const text = await response.text();
1776
+ let result;
1777
+ try { result = JSON.parse(text); } catch { result = { ok: false, error: text }; }
1778
+
1779
+ if (result.ok) {
1780
+ logger.log(` Cloud sync OK: ${result.devlogsStored || 0} devlog(s) stored.`);
1781
+ } else {
1782
+ logger.error(` Cloud sync failed: ${result.error || response.status}`);
1783
+ }
1784
+
1785
+ return result;
1786
+ }
1787
+
1788
+ async function resolveCloudUrl(targetDir) {
1789
+ try {
1790
+ const raw = await fs.readFile(path.join(targetDir, '.aioson/install.json'), 'utf8');
1791
+ const meta = JSON.parse(raw);
1792
+ return meta.cloudBaseUrl || null;
1793
+ } catch { return null; }
1794
+ }
1795
+
1796
+ async function resolveCloudToken(targetDir) {
1797
+ try {
1798
+ const raw = await fs.readFile(path.join(targetDir, '.aioson/install.json'), 'utf8');
1799
+ const meta = JSON.parse(raw);
1800
+ return meta.cloudApiToken || null;
1801
+ } catch { return null; }
1802
+ }
1803
+
1804
+ /**
1805
+ * Minimal YAML frontmatter parser (no external deps).
1806
+ * Returns an object with frontmatter keys, or {} if none.
1807
+ */
1808
+ function parseFrontmatter(content) {
1809
+ const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
1810
+ if (!match) return {};
1811
+ const result = {};
1812
+ for (const line of match[1].split('\n')) {
1813
+ const idx = line.indexOf(':');
1814
+ if (idx === -1) continue;
1815
+ const key = line.slice(0, idx).trim();
1816
+ let val = line.slice(idx + 1).trim();
1817
+ // Strip surrounding quotes
1818
+ if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
1819
+ val = val.slice(1, -1);
1820
+ }
1821
+ result[key] = val;
1822
+ }
1823
+ return result;
1824
+ }
1825
+
1826
+ /**
1827
+ * Parses a duration string like "24h", "30m", "7d" into milliseconds.
1828
+ * Falls back to treating the raw value as hours.
1829
+ */
1830
+ function parseDurationMs(value, defaultHours = 24) {
1831
+ const text = String(value || '').trim().toLowerCase();
1832
+ if (!text) return defaultHours * 60 * 60 * 1000;
1833
+
1834
+ const match = text.match(/^(\d+(?:\.\d+)?)\s*([hmd]?)$/);
1835
+ if (!match) return defaultHours * 60 * 60 * 1000;
1836
+
1837
+ const n = parseFloat(match[1]);
1838
+ const unit = match[2] || 'h';
1839
+ if (unit === 'd') return n * 24 * 60 * 60 * 1000;
1840
+ if (unit === 'm') return n * 60 * 1000;
1841
+ return n * 60 * 60 * 1000; // hours (default)
1842
+ }
1843
+
1844
+ /**
1845
+ * aioson agent:recover [targetDir] [--older-than=<duration>] [--dry-run]
1846
+ *
1847
+ * Detects and closes agent sessions that were abandoned (Claude Code closed before
1848
+ * agent:done was called, or live:start session was never closed).
1849
+ *
1850
+ * Sources checked:
1851
+ * 1. Session files in .aioson/.sessions/ with finished=false older than threshold.
1852
+ * 2. agent_runs rows with status='running'/'queued' and started_at older than threshold
1853
+ * that have no corresponding live session file (orphaned DB records).
1854
+ *
1855
+ * --older-than Duration threshold. Accepts: 24h (default), 8h, 30m, 7d.
1856
+ * --dry-run Report what would be recovered without making any changes.
1857
+ * --json Output JSON result.
1858
+ */
1859
+ async function runAgentRecover({ args, options = {}, logger }) {
1860
+ const targetDir = resolveTargetDir(args);
1861
+ const dryRun = Boolean(options['dry-run'] || options.dryRun);
1862
+ const olderThanMs = parseDurationMs(options['older-than'] || options.olderThan, 24);
1863
+ const cutoffMs = Date.now() - olderThanMs;
1864
+ const cutoffIso = new Date(cutoffMs).toISOString();
1865
+ const now = new Date().toISOString();
1866
+
1867
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1868
+
1869
+ const recovered = [];
1870
+ const skipped = [];
1871
+
1872
+ try {
1873
+ // ── 1. Scan session files ─────────────────────────────────────────────────
1874
+ const sessionsDir = path.join(runtimeDir, '.sessions');
1875
+ let sessionFiles = [];
1876
+ try {
1877
+ const entries = await fs.readdir(sessionsDir);
1878
+ sessionFiles = entries.filter((f) => f.endsWith('.json'));
1879
+ } catch {
1880
+ // .sessions dir may not exist — that's fine
1881
+ }
1882
+
1883
+ for (const file of sessionFiles) {
1884
+ const filePath = path.join(sessionsDir, file);
1885
+ let session;
1886
+ try {
1887
+ session = JSON.parse(await fs.readFile(filePath, 'utf8'));
1888
+ } catch {
1889
+ continue;
1890
+ }
1891
+
1892
+ if (session.finished) continue;
1893
+
1894
+ const startedAt = session.startedAt ? new Date(session.startedAt).getTime() : 0;
1895
+ if (startedAt > cutoffMs) {
1896
+ skipped.push({ source: 'session_file', file, reason: 'within_threshold', startedAt: session.startedAt });
1897
+ continue;
1898
+ }
1899
+
1900
+ const agentName = file.replace(/\.json$/, '');
1901
+ const runKey = session.runKey || null;
1902
+ const taskKey = session.taskKey || null;
1903
+
1904
+ if (!dryRun) {
1905
+ // Mark run as abandoned
1906
+ if (runKey) {
1907
+ const runRow = db.prepare('SELECT run_key, status FROM agent_runs WHERE run_key = ?').get(runKey);
1908
+ if (runRow && (runRow.status === 'running' || runRow.status === 'queued')) {
1909
+ db.prepare(`
1910
+ UPDATE agent_runs
1911
+ SET status = 'abandoned', summary = 'Recovered: session abandoned without close', updated_at = ?, finished_at = ?
1912
+ WHERE run_key = ?
1913
+ `).run(now, now, runKey);
1914
+ }
1915
+ }
1916
+ // Mark task as abandoned
1917
+ if (taskKey) {
1918
+ const taskRow = db.prepare('SELECT task_key, status FROM tasks WHERE task_key = ?').get(taskKey);
1919
+ if (taskRow && (taskRow.status === 'running' || taskRow.status === 'queued')) {
1920
+ db.prepare(`
1921
+ UPDATE tasks
1922
+ SET status = 'abandoned', updated_at = ?, finished_at = ?
1923
+ WHERE task_key = ?
1924
+ `).run(now, now, taskKey);
1925
+ }
1926
+ }
1927
+ // Remove session file
1928
+ try { await fs.unlink(filePath); } catch { /* noop */ }
1929
+ }
1930
+
1931
+ recovered.push({ source: 'session_file', agent: agentName, runKey, taskKey, startedAt: session.startedAt });
1932
+ }
1933
+
1934
+ // ── 2. Scan DB for orphaned running runs (no session file) ────────────────
1935
+ const orphanedRuns = db.prepare(`
1936
+ SELECT run_key, task_key, agent_name, started_at
1937
+ FROM agent_runs
1938
+ WHERE status IN ('running', 'queued')
1939
+ AND source = 'direct'
1940
+ AND started_at < ?
1941
+ `).all(cutoffIso);
1942
+
1943
+ for (const run of orphanedRuns) {
1944
+ // Skip if already recovered via session file
1945
+ if (recovered.some((r) => r.runKey === run.run_key)) continue;
1946
+
1947
+ if (!dryRun) {
1948
+ db.prepare(`
1949
+ UPDATE agent_runs
1950
+ SET status = 'abandoned', summary = 'Recovered: orphaned run with no session file', updated_at = ?, finished_at = ?
1951
+ WHERE run_key = ?
1952
+ `).run(now, now, run.run_key);
1953
+
1954
+ if (run.task_key) {
1955
+ const taskRow = db.prepare('SELECT task_key, status FROM tasks WHERE task_key = ?').get(run.task_key);
1956
+ if (taskRow && (taskRow.status === 'running' || taskRow.status === 'queued')) {
1957
+ db.prepare(`
1958
+ UPDATE tasks
1959
+ SET status = 'abandoned', updated_at = ?, finished_at = ?
1960
+ WHERE task_key = ?
1961
+ `).run(now, now, run.task_key);
1962
+ }
1963
+ }
1964
+ }
1965
+
1966
+ recovered.push({ source: 'orphaned_run', agent: run.agent_name, runKey: run.run_key, taskKey: run.task_key, startedAt: run.started_at });
1967
+ }
1968
+
1969
+ // ── Output ────────────────────────────────────────────────────────────────
1970
+ const olderThanLabel = options['older-than'] || options.olderThan || '24h';
1971
+ if (recovered.length === 0) {
1972
+ logger.log(`agent:recover — no abandoned sessions found older than ${olderThanLabel} (${dbPath})`);
1973
+ } else {
1974
+ const verb = dryRun ? '[dry-run] would recover' : 'recovered';
1975
+ logger.log(`agent:recover ${verb} ${recovered.length} abandoned session(s) older than ${olderThanLabel} (${dbPath})`);
1976
+ for (const r of recovered) {
1977
+ logger.log(` ${r.agent} started: ${r.startedAt || '?'} run: ${r.runKey || '—'} [${r.source}]`);
1978
+ }
1979
+ }
1980
+ if (skipped.length > 0) {
1981
+ logger.log(` skipped ${skipped.length} session(s) within threshold.`);
1982
+ }
1983
+
1984
+ return { ok: true, targetDir, dbPath, dryRun, cutoff: cutoffIso, recovered, skipped };
1985
+ } finally {
1986
+ db.close();
1987
+ }
1988
+ }
1989
+
1990
+
1991
+ /**
1992
+ * aioson runtime:prune [targetDir] --older-than=<days>
1993
+ *
1994
+ * Removes execution_events, agent_events, and completed agent_runs
1995
+ * older than the specified number of days. Tasks are kept but their
1996
+ * events are cleaned up.
1997
+ */
1998
+ async function runRuntimePrune({ args, options = {}, logger, t }) {
1999
+ const targetDir = resolveTargetDir(args);
2000
+ const days = parseInt(options['older-than'] || options.olderThan || '30', 10);
2001
+
2002
+ if (isNaN(days) || days < 1) {
2003
+ logger.error('Usage: aioson runtime:prune --older-than=<days> (minimum 1)');
2004
+ return { ok: false, error: 'Invalid --older-than value' };
2005
+ }
2006
+
2007
+ const { db, dbPath } = await withRuntimeDb(targetDir, t);
2008
+
2009
+ try {
2010
+ const cutoff = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();
2011
+
2012
+ const execEvents = db.prepare(
2013
+ `DELETE FROM execution_events WHERE created_at < ?`
2014
+ ).run(cutoff);
2015
+
2016
+ const agentEvents = db.prepare(
2017
+ `DELETE FROM agent_events WHERE created_at < ?`
2018
+ ).run(cutoff);
2019
+
2020
+ const runs = db.prepare(
2021
+ `DELETE FROM agent_runs WHERE status IN ('completed', 'failed') AND finished_at < ?`
2022
+ ).run(cutoff);
2023
+
2024
+ const tasks = db.prepare(
2025
+ `DELETE FROM tasks WHERE status IN ('completed', 'failed') AND finished_at < ?`
2026
+ ).run(cutoff);
2027
+
2028
+ const deliveryLogs = db.prepare(
2029
+ `DELETE FROM delivery_log WHERE created_at < ?`
2030
+ ).run(cutoff);
2031
+
2032
+ // Reclaim disk space
2033
+ db.pragma('wal_checkpoint(TRUNCATE)');
2034
+
2035
+ const total = execEvents.changes + agentEvents.changes + runs.changes + tasks.changes + deliveryLogs.changes;
2036
+
2037
+ logger.log(`Pruned ${total} records older than ${days} days from ${dbPath}:`);
2038
+ logger.log(` execution_events: ${execEvents.changes}`);
2039
+ logger.log(` agent_events: ${agentEvents.changes}`);
2040
+ logger.log(` agent_runs: ${runs.changes}`);
2041
+ logger.log(` tasks: ${tasks.changes}`);
2042
+ logger.log(` delivery_log: ${deliveryLogs.changes}`);
2043
+
2044
+ return {
2045
+ ok: true,
2046
+ dbPath,
2047
+ days,
2048
+ cutoff,
2049
+ deleted: {
2050
+ execution_events: execEvents.changes,
2051
+ agent_events: agentEvents.changes,
2052
+ agent_runs: runs.changes,
2053
+ tasks: tasks.changes,
2054
+ delivery_log: deliveryLogs.changes,
2055
+ total
2056
+ }
2057
+ };
2058
+ } finally {
2059
+ db.close();
2060
+ }
2061
+ }
2062
+
2063
+ module.exports = {
2064
+ runRuntimeInit,
2065
+ runRuntimeIngest,
2066
+ runRuntimeTaskStart,
2067
+ runRuntimeStart,
2068
+ runRuntimeUpdate,
2069
+ runRuntimeTaskFinish,
2070
+ runRuntimeFinish,
2071
+ runRuntimeTaskFail,
2072
+ runRuntimeFail,
2073
+ runRuntimeStatus,
2074
+ runRuntimeLog,
2075
+ runAgentDone,
2076
+ runAgentRecover,
2077
+ runRuntimeSessionStart,
2078
+ runRuntimeSessionLog,
2079
+ runRuntimeSessionFinish,
2080
+ runRuntimeSessionStatus,
2081
+ runDeliver,
2082
+ runOutputStrategyExport,
2083
+ runOutputStrategyImport,
2084
+ runDevlogSync,
2085
+ runRuntimePrune
2086
+ };