@jaimevalasek/aioson 1.7.2 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1049) hide show
  1. package/CHANGELOG.md +595 -560
  2. package/CODE_OF_CONDUCT.md +12 -12
  3. package/CONTRIBUTING.md +13 -13
  4. package/LICENSE +661 -661
  5. package/README.md +919 -776
  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 -409
  41. package/docs/en/5-reference/i18n.md +52 -0
  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 -116
  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 -57
  64. package/docs/en/{schemas → 5-reference/schemas}/parallel-doctor.schema.json +122 -86
  65. package/docs/en/5-reference/schemas/parallel-guard.schema.json +63 -0
  66. package/docs/en/{schemas → 5-reference/schemas}/parallel-init.schema.json +53 -53
  67. package/docs/en/5-reference/schemas/parallel-merge.schema.json +84 -0
  68. package/docs/en/5-reference/schemas/parallel-status.schema.json +184 -0
  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 -0
  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 -286
  149. package/docs/pt/{comandos-cli.md → 5-referencia/comandos-cli.md} +1823 -1634
  150. package/docs/pt/5-referencia/compress-agents.md +304 -0
  151. package/docs/pt/5-referencia/design-docs-governance.md +59 -0
  152. package/docs/pt/{devlog-pipeline.md → 5-referencia/devlog-pipeline.md} +270 -270
  153. package/docs/pt/5-referencia/feature-archive.md +199 -0
  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 -296
  157. package/docs/pt/5-referencia/genome-distribution.md +232 -0
  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/5-referencia/motor-hardening.md +493 -0
  163. package/docs/pt/{output-strategy-delivery.md → 5-referencia/output-strategy-delivery.md} +655 -655
  164. package/docs/pt/5-referencia/runner-system.md +113 -0
  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 -116
  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 -250
  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 -672
  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 -50
  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 -0
  221. package/src/agents.js +92 -72
  222. package/src/autonomy-policy.js +163 -0
  223. package/src/backup-local.js +74 -74
  224. package/src/backup-provider.js +303 -303
  225. package/src/brain-query.js +171 -0
  226. package/src/cli.js +1450 -1099
  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 -160
  231. package/src/commands/artifact-validate.js +218 -189
  232. package/src/commands/auth.js +298 -0
  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 -0
  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 -0
  239. package/src/commands/classify.js +256 -256
  240. package/src/commands/cloud.js +1767 -1767
  241. package/src/commands/commit-prepare.js +610 -0
  242. package/src/commands/compress-agents.js +416 -0
  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 -175
  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 -177
  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 -292
  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 -0
  263. package/src/commands/feature-archive.js +513 -0
  264. package/src/commands/feature-close.js +554 -165
  265. package/src/commands/gate-approve.js +198 -0
  266. package/src/commands/gate-check.js +247 -228
  267. package/src/commands/genome-doctor.js +489 -41
  268. package/src/commands/genome-migrate.js +49 -49
  269. package/src/commands/git-guard.js +170 -0
  270. package/src/commands/harness.js +307 -0
  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 -340
  276. package/src/commands/info.js +41 -41
  277. package/src/commands/init.js +120 -116
  278. package/src/commands/install.js +162 -107
  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 -1641
  285. package/src/commands/locale-apply.js +54 -51
  286. package/src/commands/locale-diff.js +25 -126
  287. package/src/commands/mcp-doctor.js +407 -406
  288. package/src/commands/mcp-init.js +373 -379
  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 -0
  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 -403
  298. package/src/commands/parallel-doctor.js +850 -437
  299. package/src/commands/parallel-guard.js +241 -0
  300. package/src/commands/parallel-init.js +311 -249
  301. package/src/commands/parallel-merge.js +299 -0
  302. package/src/commands/parallel-status.js +434 -290
  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 -208
  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 -161
  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 -0
  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 -0
  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 -0
  327. package/src/commands/security-scan.js +376 -0
  328. package/src/commands/self-implement-loop.js +306 -256
  329. package/src/commands/session-guard.js +218 -218
  330. package/src/commands/setup-context.js +699 -698
  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 -788
  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 -261
  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 -329
  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 -55
  360. package/src/commands/squad-score.js +311 -250
  361. package/src/commands/squad-status.js +481 -460
  362. package/src/commands/squad-tool-register.js +157 -157
  363. package/src/commands/squad-validate.js +438 -347
  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 +122 -122
  368. package/src/commands/store-genome.js +667 -0
  369. package/src/commands/store-skill.js +247 -0
  370. package/src/commands/store-squad.js +431 -0
  371. package/src/commands/store-system.js +392 -0
  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 -0
  375. package/src/commands/tool-registry-cmd.js +232 -232
  376. package/src/commands/update.js +64 -64
  377. package/src/commands/verify-gate.js +612 -572
  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 -241
  381. package/src/commands/workflow-harden.js +231 -0
  382. package/src/commands/workflow-heal.js +136 -0
  383. package/src/commands/workflow-next.js +1279 -601
  384. package/src/commands/workflow-plan.js +108 -108
  385. package/src/commands/workflow-status.js +440 -250
  386. package/src/commands/workspace.js +144 -0
  387. package/src/constants.js +413 -417
  388. package/src/context-cache.js +159 -159
  389. package/src/context-memory.js +975 -837
  390. package/src/context-parse-reason.js +22 -22
  391. package/src/context-search.js +326 -326
  392. package/src/context-writer.js +197 -196
  393. package/src/context.js +247 -217
  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 +760 -289
  398. package/src/dossier/codemap-store.js +267 -0
  399. package/src/dossier/dossier-bootstrap.js +222 -0
  400. package/src/dossier/dossier-compact.js +159 -0
  401. package/src/dossier/lock.js +128 -0
  402. package/src/dossier/research-index-store.js +233 -0
  403. package/src/dossier/revision-store.js +313 -0
  404. package/src/dossier/schema.js +162 -0
  405. package/src/dossier/scout-section.js +127 -0
  406. package/src/dossier/store.js +406 -0
  407. package/src/execution-gateway.js +464 -461
  408. package/src/friction-scanner.js +202 -0
  409. package/src/genome-files.js +198 -198
  410. package/src/genome-format.js +442 -442
  411. package/src/genome-schema.js +238 -215
  412. package/src/genomes/bindings.js +281 -281
  413. package/src/genomes.js +500 -467
  414. package/src/handoff-contract.js +417 -0
  415. package/src/handoff-validator.js +45 -0
  416. package/src/harness/circuit-breaker.js +135 -0
  417. package/src/i18n/index.js +103 -103
  418. package/src/i18n/messages/en.js +1541 -1139
  419. package/src/i18n/messages/es.js +1325 -980
  420. package/src/i18n/messages/fr.js +1333 -987
  421. package/src/i18n/messages/pt-BR.js +1561 -1166
  422. package/src/i18n/scaffold.js +64 -64
  423. package/src/install-animation.js +260 -260
  424. package/src/install-profile.js +127 -143
  425. package/src/install-wizard.js +475 -475
  426. package/src/installer-config-merge.js +207 -0
  427. package/src/installer.js +449 -294
  428. package/src/learning-loop-archive.js +595 -0
  429. package/src/learning-loop-doctor.js +217 -0
  430. package/src/learning-loop-engine.js +254 -0
  431. package/src/learning-loop-fts5.js +132 -0
  432. package/src/learning-loop-migration.js +163 -0
  433. package/src/lib/dev-resume.js +140 -0
  434. package/src/lib/dossier-telemetry.js +36 -0
  435. package/src/lib/genomes/compat.js +206 -206
  436. package/src/lib/genomes/migrate.js +90 -90
  437. package/src/lib/git-commit-guard.js +751 -0
  438. package/src/lib/health-check.js +158 -158
  439. package/src/lib/hook-protocol.js +76 -76
  440. package/src/lib/llm-content-sanitizer.js +44 -0
  441. package/src/lib/security/artifact-reader.js +167 -0
  442. package/src/lib/security/exit-codes.js +51 -0
  443. package/src/lib/security/findings-writer.js +176 -0
  444. package/src/lib/security/runtime-events.js +77 -0
  445. package/src/lib/security/secrets-regex.js +115 -0
  446. package/src/lib/squads/genome-repair.js +49 -49
  447. package/src/lib/store/security-scan.js +175 -0
  448. package/src/lib/terminal-checkbox.js +135 -0
  449. package/src/lib/terminal-picker.js +447 -0
  450. package/src/lib/tmux-launcher.js +163 -0
  451. package/src/lib/tool-capabilities.js +102 -0
  452. package/src/lib/webhook-server.js +328 -328
  453. package/src/locales.js +88 -84
  454. package/src/mcp/apps/squad-dashboard/app.js +163 -163
  455. package/src/mcp/apps/squad-dashboard/index.html +261 -261
  456. package/src/mcp/apps/squad-dashboard/mcp-manifest.json +23 -23
  457. package/src/mcp/resources/squad-state.js +130 -130
  458. package/src/mcp-connectors/registry.js +602 -602
  459. package/src/memory-reflect-engine.js +359 -0
  460. package/src/notify-renderer.js +32 -0
  461. package/src/onboarding.js +305 -305
  462. package/src/parallel-workspace.js +756 -0
  463. package/src/parser.js +66 -59
  464. package/src/path-guard.js +47 -0
  465. package/src/permissions-generator.js +400 -0
  466. package/src/preflight-engine.js +654 -443
  467. package/src/prompt-tool.js +20 -20
  468. package/src/qa-html-report.js +472 -472
  469. package/src/recovery-context-session.js +154 -154
  470. package/src/runner/cascade.js +97 -97
  471. package/src/runner/cli-launcher.js +109 -109
  472. package/src/runner/plan-importer.js +63 -63
  473. package/src/runner/queue-store.js +159 -159
  474. package/src/runtime-store.js +2720 -2676
  475. package/src/sandbox.js +194 -177
  476. package/src/self-healing.js +142 -0
  477. package/src/session-handoff.js +295 -77
  478. package/src/squad/agent-teams-adapter.js +270 -264
  479. package/src/squad/brief-validator.js +350 -350
  480. package/src/squad/bus-bridge.js +140 -140
  481. package/src/squad/context-compactor.js +265 -265
  482. package/src/squad/cross-ai-synthesizer.js +250 -250
  483. package/src/squad/external-session.js +180 -180
  484. package/src/squad/hooks-generator.js +196 -196
  485. package/src/squad/inter-squad-events.js +175 -175
  486. package/src/squad/inter-squad.js +74 -74
  487. package/src/squad/intra-bus.js +345 -345
  488. package/src/squad/learning-extractor.js +213 -213
  489. package/src/squad/pattern-detector.js +365 -365
  490. package/src/squad/preflight-context.js +296 -296
  491. package/src/squad/recovery-context.js +372 -372
  492. package/src/squad/reflection.js +365 -365
  493. package/src/squad/squad-scaffold.js +341 -177
  494. package/src/squad/state-manager.js +310 -310
  495. package/src/squad/task-decomposer.js +652 -652
  496. package/src/squad/verify-gate.js +303 -303
  497. package/src/squad/worktree-manager.js +114 -114
  498. package/src/squad-daemon.js +490 -490
  499. package/src/squad-dashboard/api.js +223 -223
  500. package/src/squad-dashboard/attachment-handler.js +93 -93
  501. package/src/squad-dashboard/context-monitor.js +157 -157
  502. package/src/squad-dashboard/execution-logs.js +115 -115
  503. package/src/squad-dashboard/hunk-review.js +209 -209
  504. package/src/squad-dashboard/metrics.js +133 -133
  505. package/src/squad-dashboard/process-monitor.js +125 -125
  506. package/src/squad-dashboard/renderer.js +858 -858
  507. package/src/squad-dashboard/server.js +232 -232
  508. package/src/squad-dashboard/styles.js +525 -525
  509. package/src/squad-dashboard/token-tracker.js +99 -99
  510. package/src/squads/apply-genome.js +21 -21
  511. package/src/squads/genome-binding-service.js +154 -154
  512. package/src/sub-task-engine.js +415 -0
  513. package/src/sub-task-schemas.js +150 -0
  514. package/src/sub-task-state.js +152 -0
  515. package/src/sub-task-telemetry.js +69 -0
  516. package/src/test-briefing.js +226 -0
  517. package/src/tool-executor.js +94 -94
  518. package/src/updater.js +39 -39
  519. package/src/utils.js +49 -46
  520. package/src/version.js +50 -50
  521. package/src/web.js +284 -284
  522. package/src/worker-runner.js +541 -524
  523. package/src/workflow-gates.js +185 -0
  524. package/template/.aioson/advisors/.gitkeep +1 -1
  525. package/template/.aioson/agents/analyst.md +333 -372
  526. package/template/.aioson/agents/architect.md +325 -338
  527. package/template/.aioson/agents/briefing.md +264 -0
  528. package/template/.aioson/agents/committer.md +161 -0
  529. package/template/.aioson/agents/copywriter.md +937 -463
  530. package/template/.aioson/agents/design-hybrid-forge.md +141 -141
  531. package/template/.aioson/agents/dev.md +295 -779
  532. package/template/.aioson/agents/deyvin.md +198 -290
  533. package/template/.aioson/agents/discover.md +235 -0
  534. package/template/.aioson/agents/discovery-design-doc.md +56 -264
  535. package/template/.aioson/agents/genome.md +1904 -314
  536. package/template/.aioson/agents/manifests/analyst.manifest.json +26 -0
  537. package/template/.aioson/agents/manifests/architect.manifest.json +23 -0
  538. package/template/.aioson/agents/manifests/committer.manifest.json +23 -0
  539. package/template/.aioson/agents/manifests/dev.manifest.json +54 -0
  540. package/template/.aioson/agents/manifests/deyvin.manifest.json +41 -0
  541. package/template/.aioson/agents/manifests/orchestrator.manifest.json +30 -0
  542. package/template/.aioson/agents/manifests/pentester.manifest.json +39 -0
  543. package/template/.aioson/agents/manifests/pm.manifest.json +26 -0
  544. package/template/.aioson/agents/manifests/product.manifest.json +23 -0
  545. package/template/.aioson/agents/manifests/qa.manifest.json +41 -0
  546. package/template/.aioson/agents/manifests/setup.manifest.json +20 -0
  547. package/template/.aioson/agents/manifests/ux-ui.manifest.json +24 -0
  548. package/template/.aioson/agents/neo.md +341 -233
  549. package/template/.aioson/agents/orache.md +430 -434
  550. package/template/.aioson/agents/orchestrator.md +274 -364
  551. package/template/.aioson/agents/pair.md +5 -5
  552. package/template/.aioson/agents/pentester.md +289 -0
  553. package/template/.aioson/agents/pm.md +141 -194
  554. package/template/.aioson/agents/product.md +351 -518
  555. package/template/.aioson/agents/profiler-enricher.md +331 -280
  556. package/template/.aioson/agents/profiler-forge.md +212 -202
  557. package/template/.aioson/agents/profiler-researcher.md +282 -259
  558. package/template/.aioson/agents/qa.md +432 -688
  559. package/template/.aioson/agents/setup.md +423 -649
  560. package/template/.aioson/agents/sheldon.md +259 -829
  561. package/template/.aioson/agents/site-forge.md +281 -1753
  562. package/template/.aioson/agents/squad.md +160 -2027
  563. package/template/.aioson/agents/tester.md +536 -463
  564. package/template/.aioson/agents/ux-ui.md +195 -870
  565. package/template/.aioson/agents/validator.md +101 -0
  566. package/template/.aioson/brains/README.md +132 -128
  567. package/template/.aioson/brains/_archived/.gitkeep +0 -0
  568. package/template/.aioson/brains/_index.json +34 -16
  569. package/template/.aioson/brains/dev/patterns.brain.json +79 -0
  570. package/template/.aioson/brains/scripts/query.js +107 -103
  571. package/template/.aioson/brains/sheldon/architecture-decisions.brain.json +79 -0
  572. package/template/.aioson/brains/site-forge/visual-patterns.brain.json +205 -205
  573. package/template/.aioson/config/autonomy-protocol.json +125 -0
  574. package/template/.aioson/config/learning-loop.json +10 -0
  575. package/template/.aioson/config/scout-engine.json +1 -0
  576. package/template/.aioson/config.md +410 -382
  577. package/template/.aioson/constitution.md +36 -33
  578. package/template/.aioson/context/_archived/.gitkeep +0 -0
  579. package/template/.aioson/context/design-doc.md +136 -0
  580. package/template/.aioson/context/project-map.md +57 -0
  581. package/template/.aioson/context/project-pulse.md +34 -34
  582. package/template/.aioson/context/seeds/seed-example.md +27 -27
  583. package/template/.aioson/context/spec.md.template +54 -54
  584. package/template/.aioson/context/user-profile.md +42 -42
  585. package/template/.aioson/design-docs/code-reuse.md +48 -0
  586. package/template/.aioson/design-docs/componentization.md +47 -0
  587. package/template/.aioson/design-docs/file-size.md +52 -0
  588. package/template/.aioson/design-docs/folder-structure.md +51 -0
  589. package/template/.aioson/design-docs/naming.md +54 -0
  590. package/template/.aioson/docs/LAYERS.md +89 -79
  591. package/template/.aioson/docs/README.md +76 -76
  592. package/template/.aioson/docs/autonomy-protocol.md +80 -0
  593. package/template/.aioson/docs/briefing/briefing-craft.md +237 -0
  594. package/template/.aioson/docs/dev/execution-discipline.md +106 -0
  595. package/template/.aioson/docs/dev/stack-conventions.md +83 -0
  596. package/template/.aioson/docs/deyvin/continuity-recovery.md +57 -0
  597. package/template/.aioson/docs/deyvin/debugging-escalation.md +30 -0
  598. package/template/.aioson/docs/deyvin/pair-execution.md +44 -0
  599. package/template/.aioson/docs/deyvin/runtime-handoffs.md +36 -0
  600. package/template/.aioson/docs/example-external-api-context.md +72 -72
  601. package/template/.aioson/docs/pentester/app-playbooks.md +206 -0
  602. package/template/.aioson/docs/pentester/llm-supplychain.md +165 -0
  603. package/template/.aioson/docs/product/conversation-playbook.md +116 -0
  604. package/template/.aioson/docs/product/prd-contract.md +107 -0
  605. package/template/.aioson/docs/product/quality-lens.md +57 -0
  606. package/template/.aioson/docs/product/research-loop.md +65 -0
  607. package/template/.aioson/docs/sheldon/enrichment-paths.md +134 -0
  608. package/template/.aioson/docs/sheldon/harness-contract.md +118 -0
  609. package/template/.aioson/docs/sheldon/quality-lens.md +57 -0
  610. package/template/.aioson/docs/sheldon/research-loop.md +56 -0
  611. package/template/.aioson/docs/sheldon/web-intelligence.md +75 -0
  612. package/template/.aioson/docs/site-forge-build.md +195 -0
  613. package/template/.aioson/docs/site-forge-extraction.md +135 -0
  614. package/template/.aioson/docs/site-forge-qa.md +155 -0
  615. package/template/.aioson/docs/site-forge-recon.md +434 -0
  616. package/template/.aioson/docs/site-forge-transform.md +249 -0
  617. package/template/.aioson/docs/squad/content-output.md +91 -0
  618. package/template/.aioson/docs/squad/creation-flow.md +149 -0
  619. package/template/.aioson/docs/squad/domain-breadth.md +322 -0
  620. package/template/.aioson/docs/squad/domain-classification.md +117 -0
  621. package/template/.aioson/docs/squad/genome-bindings.md +47 -0
  622. package/template/.aioson/docs/squad/package-contract.md +260 -0
  623. package/template/.aioson/docs/squad/quality-lens.md +60 -0
  624. package/template/.aioson/docs/squad/research-loop.md +59 -0
  625. package/template/.aioson/docs/squad/session-operations.md +117 -0
  626. package/template/.aioson/docs/squad/workflow-quality.md +165 -0
  627. package/template/.aioson/docs/tester/coverage-quality.md +351 -0
  628. package/template/.aioson/docs/ux-ui/accessibility-audit.md +55 -0
  629. package/template/.aioson/docs/ux-ui/audit-mode.md +86 -0
  630. package/template/.aioson/docs/ux-ui/component-map.md +35 -0
  631. package/template/.aioson/docs/ux-ui/design-execution.md +111 -0
  632. package/template/.aioson/docs/ux-ui/design-gate.md +27 -0
  633. package/template/.aioson/docs/ux-ui/research-mode.md +39 -0
  634. package/template/.aioson/docs/ux-ui/site-delivery.md +156 -0
  635. package/template/.aioson/docs/ux-ui/token-contract.md +57 -0
  636. package/template/.aioson/genomes/INDEX.md +195 -0
  637. package/template/.aioson/genomes/copywriting/SKILL.md +137 -0
  638. package/template/.aioson/genomes/copywriting/manifest.json +140 -0
  639. package/template/.aioson/genomes/copywriting/references/application-notes.md +145 -0
  640. package/template/.aioson/genomes/copywriting/references/decision-weights.md +45 -0
  641. package/template/.aioson/genomes/copywriting/references/frameworks/5-act-narrative.md +184 -0
  642. package/template/.aioson/genomes/copywriting/references/frameworks/classical-formulas.md +164 -0
  643. package/template/.aioson/genomes/copywriting/references/frameworks/offer-stack.md +195 -0
  644. package/template/.aioson/genomes/copywriting/references/frameworks/one-belief.md +135 -0
  645. package/template/.aioson/genomes/copywriting/references/frameworks/pms-research.md +211 -0
  646. package/template/.aioson/genomes/copywriting/references/frameworks/two-paths-close.md +190 -0
  647. package/template/.aioson/genomes/copywriting/references/heuristics.md +114 -0
  648. package/template/.aioson/genomes/copywriting/references/meta-axioms.md +68 -0
  649. package/template/.aioson/genomes/copywriting/references/methodology.md +115 -0
  650. package/template/.aioson/genomes/copywriting-brunson/SKILL.md +133 -0
  651. package/template/.aioson/genomes/copywriting-brunson/manifest.json +152 -0
  652. package/template/.aioson/genomes/copywriting-brunson/references/application-notes.md +113 -0
  653. package/template/.aioson/genomes/copywriting-brunson/references/decision-weights.md +33 -0
  654. package/template/.aioson/genomes/copywriting-brunson/references/evidence-and-attribution.md +81 -0
  655. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/6-part-structure.md +136 -0
  656. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/origin-story.md +121 -0
  657. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/perfect-webinar-script.md +139 -0
  658. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/persuasive-storytelling-5-structures.md +164 -0
  659. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/value-stack.md +136 -0
  660. package/template/.aioson/genomes/copywriting-brunson/references/frameworks/who-what-why-how.md +110 -0
  661. package/template/.aioson/genomes/copywriting-brunson/references/meta-axioms.md +36 -0
  662. package/template/.aioson/genomes/copywriting-brunson/references/methodology.md +112 -0
  663. package/template/.aioson/git-guard.json +12 -0
  664. package/template/.aioson/mcp/servers.md +23 -24
  665. package/template/.aioson/profiler-reports/.gitkeep +1 -1
  666. package/template/.aioson/rules/README.md +69 -69
  667. package/template/.aioson/rules/_archived/.gitkeep +0 -0
  668. package/template/.aioson/rules/agent-language-policy.md +93 -0
  669. package/template/.aioson/rules/aioson-context-boundary.md +63 -0
  670. package/template/.aioson/rules/canonical-path-contract.md +47 -0
  671. package/template/.aioson/rules/data-format-convention.md +74 -136
  672. package/template/.aioson/rules/disk-first-artifacts.md +44 -0
  673. package/template/.aioson/rules/example-monetary-values.md +30 -30
  674. package/template/.aioson/rules/output-brevity.md +44 -0
  675. package/template/.aioson/rules/prd-section-ownership.md +49 -0
  676. package/template/.aioson/rules/security-baseline.md +139 -0
  677. package/template/.aioson/rules/spec-level-ownership.md +61 -0
  678. package/template/.aioson/rules/squad/README.md +50 -50
  679. package/template/.aioson/rules/squad-driver-pattern.md +81 -0
  680. package/template/.aioson/schemas/content-blueprint.schema.json +30 -30
  681. package/template/.aioson/schemas/genome-meta.schema.json +150 -150
  682. package/template/.aioson/schemas/genome.schema.json +115 -115
  683. package/template/.aioson/schemas/readiness.schema.json +27 -27
  684. package/template/.aioson/schemas/squad-blueprint.schema.json +228 -204
  685. package/template/.aioson/schemas/squad-manifest.schema.json +874 -830
  686. package/template/.aioson/skills/design/aurora-command-ui/SKILL.md +243 -243
  687. package/template/.aioson/skills/design/aurora-command-ui/references/art-direction.md +293 -293
  688. package/template/.aioson/skills/design/aurora-command-ui/references/components.md +827 -827
  689. package/template/.aioson/skills/design/aurora-command-ui/references/dashboards.md +250 -250
  690. package/template/.aioson/skills/design/aurora-command-ui/references/design-tokens.md +585 -585
  691. package/template/.aioson/skills/design/aurora-command-ui/references/motion.md +365 -365
  692. package/template/.aioson/skills/design/aurora-command-ui/references/patterns.md +482 -482
  693. package/template/.aioson/skills/design/aurora-command-ui/references/websites.md +387 -387
  694. package/template/.aioson/skills/design/bold-editorial-ui/SKILL.md +205 -205
  695. package/template/.aioson/skills/design/bold-editorial-ui/references/art-direction.md +338 -338
  696. package/template/.aioson/skills/design/bold-editorial-ui/references/components.md +977 -977
  697. package/template/.aioson/skills/design/bold-editorial-ui/references/dashboards.md +218 -218
  698. package/template/.aioson/skills/design/bold-editorial-ui/references/design-tokens.md +326 -326
  699. package/template/.aioson/skills/design/bold-editorial-ui/references/motion.md +461 -461
  700. package/template/.aioson/skills/design/bold-editorial-ui/references/patterns.md +293 -293
  701. package/template/.aioson/skills/design/bold-editorial-ui/references/websites.md +352 -352
  702. package/template/.aioson/skills/design/clean-saas-ui/SKILL.md +210 -210
  703. package/template/.aioson/skills/design/clean-saas-ui/references/art-direction.md +319 -319
  704. package/template/.aioson/skills/design/clean-saas-ui/references/components.md +365 -365
  705. package/template/.aioson/skills/design/clean-saas-ui/references/dashboards.md +196 -196
  706. package/template/.aioson/skills/design/clean-saas-ui/references/design-tokens.md +244 -244
  707. package/template/.aioson/skills/design/clean-saas-ui/references/motion.md +235 -235
  708. package/template/.aioson/skills/design/clean-saas-ui/references/patterns.md +215 -215
  709. package/template/.aioson/skills/design/clean-saas-ui/references/websites.md +295 -295
  710. package/template/.aioson/skills/design/cognitive-core-ui/SKILL.md +203 -203
  711. package/template/.aioson/skills/design/cognitive-core-ui/references/art-direction.md +339 -339
  712. package/template/.aioson/skills/design/cognitive-core-ui/references/components.md +407 -407
  713. package/template/.aioson/skills/design/cognitive-core-ui/references/dashboards.md +272 -272
  714. package/template/.aioson/skills/design/cognitive-core-ui/references/design-tokens.md +524 -524
  715. package/template/.aioson/skills/design/cognitive-core-ui/references/motion.md +279 -279
  716. package/template/.aioson/skills/design/cognitive-core-ui/references/patterns.md +289 -289
  717. package/template/.aioson/skills/design/cognitive-core-ui/references/websites.md +437 -437
  718. package/template/.aioson/skills/design/glassmorphism-ui/SKILL.md +222 -222
  719. package/template/.aioson/skills/design/glassmorphism-ui/references/art-direction.md +159 -159
  720. package/template/.aioson/skills/design/glassmorphism-ui/references/components.md +498 -498
  721. package/template/.aioson/skills/design/glassmorphism-ui/references/dashboards.md +236 -236
  722. package/template/.aioson/skills/design/glassmorphism-ui/references/design-tokens.md +274 -274
  723. package/template/.aioson/skills/design/glassmorphism-ui/references/motion.md +355 -355
  724. package/template/.aioson/skills/design/glassmorphism-ui/references/patterns.md +198 -198
  725. package/template/.aioson/skills/design/glassmorphism-ui/references/websites.md +307 -307
  726. package/template/.aioson/skills/design/interface-design/SKILL.md +47 -47
  727. package/template/.aioson/skills/design/interface-design/references/components-and-states.md +105 -105
  728. package/template/.aioson/skills/design/interface-design/references/design-directions.md +101 -101
  729. package/template/.aioson/skills/design/interface-design/references/handoff-and-quality.md +71 -71
  730. package/template/.aioson/skills/design/interface-design/references/intent-and-domain.md +74 -74
  731. package/template/.aioson/skills/design/interface-design/references/tokens-and-depth.md +173 -173
  732. package/template/.aioson/skills/design/neo-brutalist-ui/SKILL.md +213 -213
  733. package/template/.aioson/skills/design/neo-brutalist-ui/references/art-direction.md +228 -228
  734. package/template/.aioson/skills/design/neo-brutalist-ui/references/components.md +855 -855
  735. package/template/.aioson/skills/design/neo-brutalist-ui/references/dashboards.md +334 -334
  736. package/template/.aioson/skills/design/neo-brutalist-ui/references/design-tokens.md +342 -342
  737. package/template/.aioson/skills/design/neo-brutalist-ui/references/motion.md +286 -286
  738. package/template/.aioson/skills/design/neo-brutalist-ui/references/patterns.md +458 -458
  739. package/template/.aioson/skills/design/neo-brutalist-ui/references/websites.md +723 -723
  740. package/template/.aioson/skills/design/premium-command-center-ui/SKILL.md +62 -62
  741. package/template/.aioson/skills/design/premium-command-center-ui/references/operations.md +74 -74
  742. package/template/.aioson/skills/design/premium-command-center-ui/references/patterns.md +116 -116
  743. package/template/.aioson/skills/design/premium-command-center-ui/references/validation.md +47 -47
  744. package/template/.aioson/skills/design/premium-command-center-ui/references/visual-system.md +215 -215
  745. package/template/.aioson/skills/design/pt.squarespace.com/.skill-meta.json +31 -31
  746. package/template/.aioson/skills/design/pt.squarespace.com/SKILL.md +66 -66
  747. package/template/.aioson/skills/design/pt.squarespace.com/references/components.md +368 -368
  748. package/template/.aioson/skills/design/pt.squarespace.com/references/design-tokens.md +150 -150
  749. package/template/.aioson/skills/design/pt.squarespace.com/references/motion.md +270 -270
  750. package/template/.aioson/skills/design/pt.squarespace.com/references/patterns.md +189 -189
  751. package/template/.aioson/skills/design/pt.squarespace.com/references/websites.md +165 -165
  752. package/template/.aioson/skills/design/warm-craft-ui/SKILL.md +209 -209
  753. package/template/.aioson/skills/design/warm-craft-ui/references/art-direction.md +324 -324
  754. package/template/.aioson/skills/design/warm-craft-ui/references/components.md +508 -508
  755. package/template/.aioson/skills/design/warm-craft-ui/references/dashboards.md +223 -223
  756. package/template/.aioson/skills/design/warm-craft-ui/references/design-tokens.md +374 -374
  757. package/template/.aioson/skills/design/warm-craft-ui/references/motion.md +356 -356
  758. package/template/.aioson/skills/design/warm-craft-ui/references/patterns.md +288 -288
  759. package/template/.aioson/skills/design/warm-craft-ui/references/websites.md +289 -289
  760. package/template/.aioson/skills/design-system/SKILL.md +92 -92
  761. package/template/.aioson/skills/design-system/components/SKILL.md +274 -274
  762. package/template/.aioson/skills/design-system/dashboards/SKILL.md +184 -184
  763. package/template/.aioson/skills/design-system/foundations/SKILL.md +250 -250
  764. package/template/.aioson/skills/design-system/motion/SKILL.md +197 -197
  765. package/template/.aioson/skills/design-system/patterns/SKILL.md +231 -231
  766. package/template/.aioson/skills/dynamic/README.md +30 -30
  767. package/template/.aioson/skills/dynamic/cardano-docs.md +16 -16
  768. package/template/.aioson/skills/dynamic/ethereum-docs.md +17 -17
  769. package/template/.aioson/skills/dynamic/flux-ui-docs.md +13 -13
  770. package/template/.aioson/skills/dynamic/laravel-docs.md +41 -41
  771. package/template/.aioson/skills/dynamic/npm-packages.md +16 -16
  772. package/template/.aioson/skills/dynamic/solana-docs.md +16 -16
  773. package/template/.aioson/skills/marketing/references/anti-patterns.md +254 -254
  774. package/template/.aioson/skills/marketing/references/cta-matrix.md +361 -0
  775. package/template/.aioson/skills/marketing/references/fascinations.md +192 -192
  776. package/template/.aioson/skills/marketing/references/five-acts.md +248 -248
  777. package/template/.aioson/skills/marketing/references/headline-matrix.md +358 -0
  778. package/template/.aioson/skills/marketing/references/market-intelligence.md +198 -198
  779. package/template/.aioson/skills/marketing/references/offer-structure.md +203 -203
  780. package/template/.aioson/skills/marketing/references/one-belief.md +149 -149
  781. package/template/.aioson/skills/marketing/references/patterns.md +218 -218
  782. package/template/.aioson/skills/marketing/references/platform-constraints.md +337 -0
  783. package/template/.aioson/skills/marketing/references/pms-research.md +193 -193
  784. package/template/.aioson/skills/marketing/vsl-craft.md +385 -385
  785. package/template/.aioson/skills/premium-visual-design/SKILL.md +83 -83
  786. package/template/.aioson/skills/premium-visual-design/components/agent-badge.md +92 -92
  787. package/template/.aioson/skills/premium-visual-design/components/dependency-node.md +102 -102
  788. package/template/.aioson/skills/premium-visual-design/components/mention-autocomplete.md +136 -136
  789. package/template/.aioson/skills/premium-visual-design/components/notification-center.md +136 -136
  790. package/template/.aioson/skills/premium-visual-design/components/review-action-bar.md +188 -188
  791. package/template/.aioson/skills/premium-visual-design/components/team-switcher.md +131 -131
  792. package/template/.aioson/skills/premium-visual-design/patterns/agent-message-thread.md +198 -198
  793. package/template/.aioson/skills/premium-visual-design/patterns/notification-panel.md +275 -275
  794. package/template/.aioson/skills/premium-visual-design/patterns/review-workflow-ui.md +234 -234
  795. package/template/.aioson/skills/premium-visual-design/patterns/task-dependency-graph.md +147 -147
  796. package/template/.aioson/skills/premium-visual-design/tokens/status-extended.md +142 -142
  797. package/template/.aioson/skills/process/aioson-spec-driven/SKILL.md +46 -46
  798. package/template/.aioson/skills/process/aioson-spec-driven/references/analyst.md +30 -30
  799. package/template/.aioson/skills/process/aioson-spec-driven/references/approval-gates.md +109 -109
  800. package/template/.aioson/skills/process/aioson-spec-driven/references/architect.md +23 -23
  801. package/template/.aioson/skills/process/aioson-spec-driven/references/artifact-map.md +44 -44
  802. package/template/.aioson/skills/process/aioson-spec-driven/references/classification-map.md +37 -37
  803. package/template/.aioson/skills/process/aioson-spec-driven/references/dev.md +47 -47
  804. package/template/.aioson/skills/process/aioson-spec-driven/references/deyvin.md +27 -27
  805. package/template/.aioson/skills/process/aioson-spec-driven/references/hardening-lane.md +49 -49
  806. package/template/.aioson/skills/process/aioson-spec-driven/references/maintenance-and-state.md +101 -101
  807. package/template/.aioson/skills/process/aioson-spec-driven/references/pm.md +30 -0
  808. package/template/.aioson/skills/process/aioson-spec-driven/references/product.md +25 -25
  809. package/template/.aioson/skills/process/aioson-spec-driven/references/qa.md +30 -30
  810. package/template/.aioson/skills/process/aioson-spec-driven/references/sheldon.md +25 -25
  811. package/template/.aioson/skills/process/aioson-spec-driven/references/ui-language.md +75 -75
  812. package/template/.aioson/skills/process/design-hybrid-forge/SKILL.md +147 -147
  813. package/template/.aioson/skills/process/design-hybrid-forge/references/crossover-protocol.md +221 -221
  814. package/template/.aioson/skills/process/design-hybrid-forge/references/naming-registry.md +88 -88
  815. package/template/.aioson/skills/process/design-hybrid-forge/references/output-contract.md +306 -306
  816. package/template/.aioson/skills/process/design-hybrid-forge/references/pair-compatibility.md +149 -149
  817. package/template/.aioson/skills/process/design-hybrid-forge/references/quality-gates.md +208 -208
  818. package/template/.aioson/skills/process/design-hybrid-forge/references/variation-library.md +125 -125
  819. package/template/.aioson/skills/process/secure-tdd/SKILL.md +97 -0
  820. package/template/.aioson/skills/process/simplify/SKILL.md +173 -173
  821. package/template/.aioson/skills/references/premium-command-center-ui/master-application-prompt.md +79 -79
  822. package/template/.aioson/skills/references/premium-command-center-ui/operational-ux-playbook.md +253 -253
  823. package/template/.aioson/skills/references/premium-command-center-ui/quality-validation-checklist.md +82 -82
  824. package/template/.aioson/skills/references/premium-command-center-ui/visual-system-and-component-patterns.md +270 -270
  825. package/template/.aioson/skills/squad/SKILL.md +58 -58
  826. package/template/.aioson/skills/squad/formats/catalog.json +15 -15
  827. package/template/.aioson/skills/squad/formats/content/blog-post.md +47 -47
  828. package/template/.aioson/skills/squad/formats/content/newsletter.md +47 -47
  829. package/template/.aioson/skills/squad/formats/creative/podcast-script.md +43 -43
  830. package/template/.aioson/skills/squad/formats/creative/video-script.md +41 -41
  831. package/template/.aioson/skills/squad/formats/social/instagram-feed.md +42 -42
  832. package/template/.aioson/skills/squad/formats/social/linkedin-post.md +42 -42
  833. package/template/.aioson/skills/squad/formats/social/tiktok.md +39 -39
  834. package/template/.aioson/skills/squad/formats/social/twitter-thread.md +39 -39
  835. package/template/.aioson/skills/squad/formats/social/youtube-long.md +47 -47
  836. package/template/.aioson/skills/squad/formats/social/youtube-shorts.md +39 -39
  837. package/template/.aioson/skills/squad/patterns/multi-platform-pattern.md +108 -108
  838. package/template/.aioson/skills/squad/patterns/persona-based-pattern.md +98 -98
  839. package/template/.aioson/skills/squad/patterns/pipeline-pattern.md +106 -106
  840. package/template/.aioson/skills/squad/patterns/review-loop-pattern.md +81 -81
  841. package/template/.aioson/skills/squad/references/checklist-templates.md +122 -122
  842. package/template/.aioson/skills/squad/references/executor-archetypes.md +123 -123
  843. package/template/.aioson/skills/squad/references/workflow-templates.md +169 -169
  844. package/template/.aioson/skills/static/context-budget-guide.md +46 -46
  845. package/template/.aioson/skills/static/debugging-protocol.md +42 -42
  846. package/template/.aioson/skills/static/django-patterns.md +342 -342
  847. package/template/.aioson/skills/static/fastapi-patterns.md +344 -344
  848. package/template/.aioson/skills/static/filament-patterns.md +267 -267
  849. package/template/.aioson/skills/static/flux-ui-components.md +262 -262
  850. package/template/.aioson/skills/static/git-conventions.md +227 -227
  851. package/template/.aioson/skills/static/git-worktrees.md +36 -36
  852. package/template/.aioson/skills/static/harness-sensors.md +74 -74
  853. package/template/.aioson/skills/static/harness-validate/SKILL.md +46 -0
  854. package/template/.aioson/skills/static/jetstream-setup.md +200 -200
  855. package/template/.aioson/skills/static/landing-page-deploy.md +192 -192
  856. package/template/.aioson/skills/static/landing-page-forge.md +730 -730
  857. package/template/.aioson/skills/static/laravel-conventions.md +491 -491
  858. package/template/.aioson/skills/static/multi-agent-patterns.md +43 -43
  859. package/template/.aioson/skills/static/nextjs-patterns.md +321 -321
  860. package/template/.aioson/skills/static/node-express-patterns.md +317 -317
  861. package/template/.aioson/skills/static/node-typescript-patterns.md +282 -282
  862. package/template/.aioson/skills/static/rails-conventions.md +307 -307
  863. package/template/.aioson/skills/static/react-motion-patterns.md +599 -599
  864. package/template/.aioson/skills/static/static-html-patterns/checklists.md +43 -43
  865. package/template/.aioson/skills/static/static-html-patterns/css-tokens.md +609 -609
  866. package/template/.aioson/skills/static/static-html-patterns/motion.md +193 -193
  867. package/template/.aioson/skills/static/static-html-patterns/premium.md +711 -711
  868. package/template/.aioson/skills/static/static-html-patterns/structure.md +209 -209
  869. package/template/.aioson/skills/static/static-html-patterns/utilities.md +190 -190
  870. package/template/.aioson/skills/static/static-html-patterns.md +80 -80
  871. package/template/.aioson/skills/static/tall-stack-patterns.md +286 -286
  872. package/template/.aioson/skills/static/threejs-patterns.md +929 -929
  873. package/template/.aioson/skills/static/ui-ux-modern.md +76 -76
  874. package/template/.aioson/skills/static/web-research-cache.md +115 -112
  875. package/template/.aioson/skills/static/web3-cardano-patterns.md +337 -337
  876. package/template/.aioson/skills/static/web3-ethereum-patterns.md +310 -310
  877. package/template/.aioson/skills/static/web3-security-checklist.md +284 -284
  878. package/template/.aioson/skills/static/web3-solana-patterns.md +324 -324
  879. package/template/.aioson/squads/memory.md +5 -5
  880. package/template/.aioson/tasks/implementation-plan.md +327 -327
  881. package/template/.aioson/tasks/squad-analyze.md +83 -83
  882. package/template/.aioson/tasks/squad-create.md +148 -121
  883. package/template/.aioson/tasks/squad-design.md +206 -158
  884. package/template/.aioson/tasks/squad-execution-plan.md +279 -279
  885. package/template/.aioson/tasks/squad-export.md +20 -20
  886. package/template/.aioson/tasks/squad-extend.md +68 -68
  887. package/template/.aioson/tasks/squad-investigate.md +57 -44
  888. package/template/.aioson/tasks/squad-learning-review.md +44 -44
  889. package/template/.aioson/tasks/squad-output-config.md +177 -177
  890. package/template/.aioson/tasks/squad-pipeline.md +122 -122
  891. package/template/.aioson/tasks/squad-profile.md +48 -48
  892. package/template/.aioson/tasks/squad-refresh.md +236 -0
  893. package/template/.aioson/tasks/squad-repair.md +85 -85
  894. package/template/.aioson/tasks/squad-review.md +61 -61
  895. package/template/.aioson/tasks/squad-task-decompose.md +66 -66
  896. package/template/.aioson/tasks/squad-validate.md +58 -58
  897. package/template/.aioson/templates/reflect-prompts/current-state.md +36 -0
  898. package/template/.aioson/templates/reflect-prompts/how-it-works.md +23 -0
  899. package/template/.aioson/templates/reflect-prompts/what-it-does.md +21 -0
  900. package/template/.aioson/templates/squads/content-basic/template.json +21 -21
  901. package/template/.aioson/templates/squads/digital-marketing-agency/template.json +96 -96
  902. package/template/.aioson/templates/squads/media-channel/template.json +24 -24
  903. package/template/.aioson/templates/squads/research-analysis/template.json +22 -22
  904. package/template/.aioson/templates/squads/software-delivery/template.json +21 -21
  905. package/template/.claude/commands/aioson/agent/analyst.md +5 -5
  906. package/template/.claude/commands/aioson/agent/architect.md +5 -5
  907. package/template/.claude/commands/aioson/agent/briefing.md +5 -0
  908. package/template/.claude/commands/aioson/agent/committer.md +5 -0
  909. package/template/.claude/commands/aioson/agent/copywriter.md +5 -0
  910. package/template/.claude/commands/aioson/agent/design-hybrid-forge.md +5 -5
  911. package/template/.claude/commands/aioson/agent/dev.md +5 -5
  912. package/template/.claude/commands/aioson/agent/deyvin.md +5 -5
  913. package/template/.claude/commands/aioson/agent/discover.md +5 -0
  914. package/template/.claude/commands/aioson/agent/discovery-design-doc.md +5 -5
  915. package/template/.claude/commands/aioson/agent/genome.md +5 -5
  916. package/template/.claude/commands/aioson/agent/neo.md +5 -5
  917. package/template/.claude/commands/aioson/agent/orache.md +5 -5
  918. package/template/.claude/commands/aioson/agent/orchestrator.md +5 -5
  919. package/template/.claude/commands/aioson/agent/pair.md +5 -0
  920. package/template/.claude/commands/aioson/agent/pentester.md +5 -0
  921. package/template/.claude/commands/aioson/agent/pm.md +5 -5
  922. package/template/.claude/commands/aioson/agent/product.md +5 -5
  923. package/template/.claude/commands/aioson/agent/profiler-enricher.md +5 -5
  924. package/template/.claude/commands/aioson/agent/profiler-forge.md +5 -5
  925. package/template/.claude/commands/aioson/agent/profiler-researcher.md +5 -5
  926. package/template/.claude/commands/aioson/agent/qa.md +5 -5
  927. package/template/.claude/commands/aioson/agent/setup.md +5 -5
  928. package/template/.claude/commands/aioson/agent/sheldon.md +5 -5
  929. package/template/.claude/commands/aioson/agent/site-forge.md +5 -5
  930. package/template/.claude/commands/aioson/agent/squad.md +5 -5
  931. package/template/.claude/commands/aioson/agent/tester.md +5 -5
  932. package/template/.claude/commands/aioson/agent/ux-ui.md +5 -5
  933. package/template/.claude/commands/aioson/agent/validator.md +5 -0
  934. package/template/.gemini/GEMINI.md +13 -13
  935. package/template/.gemini/commands/aios-analyst.toml +7 -4
  936. package/template/.gemini/commands/aios-architect.toml +8 -7
  937. package/template/.gemini/commands/aios-committer.toml +7 -0
  938. package/template/.gemini/commands/aios-copywriter.toml +7 -0
  939. package/template/.gemini/commands/aios-cypher.toml +7 -0
  940. package/template/.gemini/commands/aios-dev.toml +9 -8
  941. package/template/.gemini/commands/aios-deyvin.toml +7 -6
  942. package/template/.gemini/commands/aios-discover.toml +6 -0
  943. package/template/.gemini/commands/aios-discovery-design-doc.toml +7 -4
  944. package/template/.gemini/commands/aios-genome.toml +7 -0
  945. package/template/.gemini/commands/aios-neo.toml +6 -4
  946. package/template/.gemini/commands/aios-orache.toml +7 -0
  947. package/template/.gemini/commands/aios-orchestrator.toml +9 -8
  948. package/template/.gemini/commands/aios-pair.toml +7 -6
  949. package/template/.gemini/commands/aios-pm.toml +9 -8
  950. package/template/.gemini/commands/aios-product.toml +6 -4
  951. package/template/.gemini/commands/aios-qa.toml +7 -6
  952. package/template/.gemini/commands/aios-setup.toml +6 -3
  953. package/template/.gemini/commands/aios-sheldon.toml +7 -0
  954. package/template/.gemini/commands/aios-site-forge.toml +7 -0
  955. package/template/.gemini/commands/aios-squad.toml +7 -0
  956. package/template/.gemini/commands/aios-tester.toml +7 -6
  957. package/template/.gemini/commands/aios-ux-ui.toml +9 -8
  958. package/template/.gemini/commands/aios-validator.toml +7 -0
  959. package/template/AGENTS.md +184 -172
  960. package/template/CLAUDE.md +98 -93
  961. package/template/OPENCODE.md +35 -34
  962. package/template/aioson-models.json +40 -40
  963. package/docs/en/i18n.md +0 -52
  964. package/docs/en/schemas/parallel-status.schema.json +0 -94
  965. package/template/.aioson/genomes/copywriting.md +0 -204
  966. package/template/.aioson/locales/en/agents/analyst.md +0 -244
  967. package/template/.aioson/locales/en/agents/architect.md +0 -245
  968. package/template/.aioson/locales/en/agents/dev.md +0 -397
  969. package/template/.aioson/locales/en/agents/deyvin.md +0 -137
  970. package/template/.aioson/locales/en/agents/discovery-design-doc.md +0 -27
  971. package/template/.aioson/locales/en/agents/genome.md +0 -212
  972. package/template/.aioson/locales/en/agents/neo.md +0 -8
  973. package/template/.aioson/locales/en/agents/orache.md +0 -6
  974. package/template/.aioson/locales/en/agents/orchestrator.md +0 -189
  975. package/template/.aioson/locales/en/agents/pair.md +0 -5
  976. package/template/.aioson/locales/en/agents/pm.md +0 -84
  977. package/template/.aioson/locales/en/agents/product.md +0 -378
  978. package/template/.aioson/locales/en/agents/profiler-enricher.md +0 -5
  979. package/template/.aioson/locales/en/agents/profiler-forge.md +0 -5
  980. package/template/.aioson/locales/en/agents/profiler-researcher.md +0 -5
  981. package/template/.aioson/locales/en/agents/qa.md +0 -270
  982. package/template/.aioson/locales/en/agents/setup.md +0 -421
  983. package/template/.aioson/locales/en/agents/sheldon.md +0 -455
  984. package/template/.aioson/locales/en/agents/squad.md +0 -449
  985. package/template/.aioson/locales/en/agents/tester.md +0 -6
  986. package/template/.aioson/locales/en/agents/ux-ui.md +0 -668
  987. package/template/.aioson/locales/es/agents/analyst.md +0 -225
  988. package/template/.aioson/locales/es/agents/architect.md +0 -245
  989. package/template/.aioson/locales/es/agents/dev.md +0 -370
  990. package/template/.aioson/locales/es/agents/deyvin.md +0 -99
  991. package/template/.aioson/locales/es/agents/discovery-design-doc.md +0 -21
  992. package/template/.aioson/locales/es/agents/genome.md +0 -104
  993. package/template/.aioson/locales/es/agents/neo.md +0 -50
  994. package/template/.aioson/locales/es/agents/orache.md +0 -105
  995. package/template/.aioson/locales/es/agents/orchestrator.md +0 -194
  996. package/template/.aioson/locales/es/agents/pair.md +0 -7
  997. package/template/.aioson/locales/es/agents/pm.md +0 -90
  998. package/template/.aioson/locales/es/agents/product.md +0 -372
  999. package/template/.aioson/locales/es/agents/profiler-enricher.md +0 -7
  1000. package/template/.aioson/locales/es/agents/profiler-forge.md +0 -7
  1001. package/template/.aioson/locales/es/agents/profiler-researcher.md +0 -7
  1002. package/template/.aioson/locales/es/agents/qa.md +0 -198
  1003. package/template/.aioson/locales/es/agents/setup.md +0 -405
  1004. package/template/.aioson/locales/es/agents/sheldon.md +0 -309
  1005. package/template/.aioson/locales/es/agents/squad.md +0 -532
  1006. package/template/.aioson/locales/es/agents/tester.md +0 -9
  1007. package/template/.aioson/locales/es/agents/ux-ui.md +0 -212
  1008. package/template/.aioson/locales/fr/agents/analyst.md +0 -225
  1009. package/template/.aioson/locales/fr/agents/architect.md +0 -245
  1010. package/template/.aioson/locales/fr/agents/dev.md +0 -370
  1011. package/template/.aioson/locales/fr/agents/deyvin.md +0 -99
  1012. package/template/.aioson/locales/fr/agents/discovery-design-doc.md +0 -21
  1013. package/template/.aioson/locales/fr/agents/genome.md +0 -104
  1014. package/template/.aioson/locales/fr/agents/neo.md +0 -50
  1015. package/template/.aioson/locales/fr/agents/orache.md +0 -106
  1016. package/template/.aioson/locales/fr/agents/orchestrator.md +0 -194
  1017. package/template/.aioson/locales/fr/agents/pair.md +0 -7
  1018. package/template/.aioson/locales/fr/agents/pm.md +0 -90
  1019. package/template/.aioson/locales/fr/agents/product.md +0 -372
  1020. package/template/.aioson/locales/fr/agents/profiler-enricher.md +0 -7
  1021. package/template/.aioson/locales/fr/agents/profiler-forge.md +0 -7
  1022. package/template/.aioson/locales/fr/agents/profiler-researcher.md +0 -7
  1023. package/template/.aioson/locales/fr/agents/qa.md +0 -198
  1024. package/template/.aioson/locales/fr/agents/setup.md +0 -405
  1025. package/template/.aioson/locales/fr/agents/sheldon.md +0 -309
  1026. package/template/.aioson/locales/fr/agents/squad.md +0 -532
  1027. package/template/.aioson/locales/fr/agents/tester.md +0 -9
  1028. package/template/.aioson/locales/fr/agents/ux-ui.md +0 -212
  1029. package/template/.aioson/locales/pt-BR/agents/analyst.md +0 -319
  1030. package/template/.aioson/locales/pt-BR/agents/architect.md +0 -284
  1031. package/template/.aioson/locales/pt-BR/agents/dev.md +0 -483
  1032. package/template/.aioson/locales/pt-BR/agents/deyvin.md +0 -184
  1033. package/template/.aioson/locales/pt-BR/agents/discovery-design-doc.md +0 -198
  1034. package/template/.aioson/locales/pt-BR/agents/genome.md +0 -297
  1035. package/template/.aioson/locales/pt-BR/agents/neo.md +0 -208
  1036. package/template/.aioson/locales/pt-BR/agents/orache.md +0 -137
  1037. package/template/.aioson/locales/pt-BR/agents/orchestrator.md +0 -324
  1038. package/template/.aioson/locales/pt-BR/agents/pair.md +0 -5
  1039. package/template/.aioson/locales/pt-BR/agents/pm.md +0 -182
  1040. package/template/.aioson/locales/pt-BR/agents/product.md +0 -466
  1041. package/template/.aioson/locales/pt-BR/agents/profiler-enricher.md +0 -5
  1042. package/template/.aioson/locales/pt-BR/agents/profiler-forge.md +0 -5
  1043. package/template/.aioson/locales/pt-BR/agents/profiler-researcher.md +0 -5
  1044. package/template/.aioson/locales/pt-BR/agents/qa.md +0 -300
  1045. package/template/.aioson/locales/pt-BR/agents/setup.md +0 -533
  1046. package/template/.aioson/locales/pt-BR/agents/sheldon.md +0 -323
  1047. package/template/.aioson/locales/pt-BR/agents/squad.md +0 -1330
  1048. package/template/.aioson/locales/pt-BR/agents/tester.md +0 -449
  1049. package/template/.aioson/locales/pt-BR/agents/ux-ui.md +0 -669
@@ -1,1641 +1,2101 @@
1
- 'use strict';
2
-
3
- const fs = require('node:fs/promises');
4
- const path = require('node:path');
5
- const { spawn } = require('node:child_process');
6
- const {
7
- resolveRuntimePaths,
8
- openRuntimeDb,
9
- runtimeStoreExists,
10
- startTask,
11
- updateTask,
12
- startRun,
13
- updateRun,
14
- appendRunEvent,
15
- readAgentSession,
16
- writeAgentSession,
17
- clearAgentSession
18
- } = require('../runtime-store');
19
- const { ensureDir, exists } = require('../utils');
20
- const { SUPPORTED_PROMPT_TOOLS } = require('../prompt-tool');
21
-
22
- const LIVE_EVENTS_LIMIT = 10;
23
- const LIVE_MESSAGE_LIMIT = 500;
24
-
25
- function resolveTargetDir(args) {
26
- return path.resolve(process.cwd(), args[0] || '.');
27
- }
28
-
29
- function requireOption(options, key, t) {
30
- const value = options[key];
31
- if (value === undefined || value === null || String(value).trim() === '') {
32
- throw new Error(t('runtime.option_required', { option: `--${key}` }));
33
- }
34
- return String(value).trim();
35
- }
36
-
37
- function normalizeAgentHandle(value) {
38
- const text = String(value || '').trim();
39
- if (!text) return '';
40
- return text.startsWith('@') ? text : `@${text}`;
41
- }
42
-
43
- function makeDirectSessionKey(agentName) {
44
- return `direct-session:${Date.now()}:${String(agentName || '').replace(/^@/, '')}`;
45
- }
46
-
47
- function parseWatchSeconds(value) {
48
- if (value === undefined || value === null || value === false) return null;
49
- if (value === true || value === '') return 2;
50
-
51
- const parsed = Number(value);
52
- if (!Number.isFinite(parsed) || parsed <= 0) return 2;
53
- return parsed;
54
- }
55
-
56
- function sleep(ms) {
57
- return new Promise((resolve) => setTimeout(resolve, ms));
58
- }
59
-
60
- async function withRuntimeDb(targetDir, t) {
61
- const handle = await openRuntimeDb(targetDir, { mustExist: true });
62
- if (!handle) {
63
- throw new Error(t('runtime.store_missing', { path: resolveRuntimePaths(targetDir).dbPath }));
64
- }
65
- return handle;
66
- }
67
-
68
- function resolveLivePaths(runtimeDir, sessionKey) {
69
- const sessionDir = path.join(runtimeDir, 'live', sessionKey);
70
- return {
71
- sessionDir,
72
- statePath: path.join(sessionDir, 'state.json'),
73
- eventsPath: path.join(sessionDir, 'events.ndjson'),
74
- summaryPath: path.join(sessionDir, 'summary.md')
75
- };
76
- }
77
-
78
- function truncateMessage(value, fallback = '') {
79
- const text = String(value || fallback || '').trim();
80
- if (!text) return '';
81
- if (text.length <= LIVE_MESSAGE_LIMIT) return text;
82
- return `${text.slice(0, LIVE_MESSAGE_LIMIT - 3).trimEnd()}...`;
83
- }
84
-
85
- function parseRefs(value) {
86
- const text = String(value || '').trim();
87
- if (!text) return [];
88
- return Array.from(new Set(text.split(',').map((entry) => entry.trim()).filter(Boolean)));
89
- }
90
-
91
- function parseJsonOption(value) {
92
- if (value === undefined || value === null || value === '') return null;
93
- if (typeof value === 'object' && !Array.isArray(value)) return value;
94
- try {
95
- const parsed = JSON.parse(String(value));
96
- return parsed && typeof parsed === 'object' ? parsed : { value: parsed };
97
- } catch {
98
- return { raw: String(value) };
99
- }
100
- }
101
-
102
- function parseToolArgs(value) {
103
- if (value === undefined || value === null || value === '') return [];
104
- if (Array.isArray(value)) return value.map((entry) => String(entry));
105
- const text = String(value).trim();
106
- if (!text) return [];
107
-
108
- if (text.startsWith('[')) {
109
- try {
110
- const parsed = JSON.parse(text);
111
- if (Array.isArray(parsed)) {
112
- return parsed.map((entry) => String(entry));
113
- }
114
- } catch {
115
- // fallback to whitespace split below
116
- }
117
- }
118
-
119
- return text.split(/\s+/).filter(Boolean);
120
- }
121
-
122
- function normalizeLiveTool(value, t) {
123
- const tool = String(value || '').trim().toLowerCase();
124
- if (SUPPORTED_PROMPT_TOOLS.has(tool)) {
125
- return tool;
126
- }
127
- const supported = Array.from(SUPPORTED_PROMPT_TOOLS).join(', ');
128
- throw new Error(t ? t('live.unsupported_tool', { tool: value, supported }) : `Unsupported live tool: ${value}. Supported: ${supported}`);
129
- }
130
-
131
-
132
- function normalizePlanStepId(value) {
133
- return String(value || '').trim().replace(/\s+/g, ' ');
134
- }
135
-
136
- function collectPlanSteps(markdown) {
137
- const steps = [];
138
- const seen = new Set();
139
-
140
- function pushStep(id, title) {
141
- const normalizedId = normalizePlanStepId(id);
142
- const normalizedTitle = String(title || '').trim();
143
- if (!normalizedId || !normalizedTitle) return;
144
- const key = normalizedId.toLowerCase();
145
- if (seen.has(key)) return;
146
- seen.add(key);
147
- steps.push({ id: normalizedId, title: normalizedTitle, done: false });
148
- }
149
-
150
- const blockPattern = /<!--\s*aioson:steps([\s\S]*?)-->/gi;
151
- let blockMatch = blockPattern.exec(markdown);
152
- while (blockMatch) {
153
- const lines = String(blockMatch[1] || '').split(/\r?\n/);
154
- for (const line of lines) {
155
- const trimmed = line.trim();
156
- if (!trimmed) continue;
157
- const match = trimmed.match(/^([^:]+):\s*(.+)$/);
158
- if (match) {
159
- pushStep(match[1], match[2]);
160
- }
161
- }
162
- blockMatch = blockPattern.exec(markdown);
163
- }
164
-
165
- const headingPattern = /^#{3,6}\s+((?:[A-Z]{2,}(?:-[A-Z0-9]+)*-\d+(?:\.\d+)?)|(?:Fase\s+\d+(?:\.\d+)?))\s*[-:]\s+(.+)$/gim;
166
- let headingMatch = headingPattern.exec(markdown);
167
- while (headingMatch) {
168
- pushStep(headingMatch[1], headingMatch[2]);
169
- headingMatch = headingPattern.exec(markdown);
170
- }
171
-
172
- return steps;
173
- }
174
-
175
- async function loadPlanReference(targetDir, planRef) {
176
- if (!planRef) {
177
- return { planRef: null, planPath: null, planSteps: [] };
178
- }
179
-
180
- const planPath = path.isAbsolute(planRef) ? planRef : path.resolve(targetDir, planRef);
181
- let markdown = '';
182
- try {
183
- markdown = await fs.readFile(planPath, 'utf8');
184
- } catch {
185
- throw new Error(`Plan file not found: ${planRef}`); // technical message, i18n at caller level
186
- }
187
-
188
- return {
189
- planRef,
190
- planPath,
191
- planSteps: collectPlanSteps(markdown)
192
- };
193
- }
194
-
195
-
196
- async function resolveExecutablePath(command) {
197
- const binary = String(command || '').trim();
198
- if (!binary) return null;
199
-
200
- if (path.isAbsolute(binary) || binary.includes(path.sep)) {
201
- const absolutePath = path.isAbsolute(binary) ? binary : path.resolve(binary);
202
- return (await exists(absolutePath)) ? absolutePath : null;
203
- }
204
-
205
- const pathEntries = String(process.env.PATH || '')
206
- .split(path.delimiter)
207
- .map((entry) => entry.trim())
208
- .filter(Boolean);
209
-
210
- const extensions = process.platform === 'win32'
211
- ? Array.from(new Set(['', ...String(process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM').split(';').map((entry) => entry.toLowerCase())]))
212
- : [''];
213
-
214
- for (const dir of pathEntries) {
215
- for (const ext of extensions) {
216
- const candidate = process.platform === 'win32' && ext && !binary.toLowerCase().endsWith(ext)
217
- ? path.join(dir, `${binary}${ext}`)
218
- : path.join(dir, binary);
219
- if (await exists(candidate)) {
220
- return candidate;
221
- }
222
- }
223
- }
224
-
225
- return null;
226
- }
227
-
228
- function detectProcessState(pid) {
229
- if (!pid) return 'not_tracked';
230
- try {
231
- process.kill(Number(pid), 0);
232
- return 'alive';
233
- } catch (error) {
234
- if (error && error.code === 'ESRCH') {
235
- return 'dead';
236
- }
237
- return 'unknown';
238
- }
239
- }
240
-
241
- function normalizeLiveStats(stats, fallback = {}) {
242
- return {
243
- tasks_completed: Number(stats?.tasks_completed || 0),
244
- events_total: Number(stats?.events_total || 0),
245
- plan_steps_done: Number(stats?.plan_steps_done ?? fallback.plan_steps_done ?? 0),
246
- plan_steps_total: Number(stats?.plan_steps_total ?? fallback.plan_steps_total ?? 0),
247
- events_by_type: stats?.events_by_type && typeof stats.events_by_type === 'object'
248
- ? { ...stats.events_by_type }
249
- : {}
250
- };
251
- }
252
-
253
- function parseTaskMeta(task) {
254
- if (!task || !task.meta_json) return {};
255
- try {
256
- const parsed = JSON.parse(task.meta_json);
257
- return parsed && typeof parsed === 'object' ? parsed : {};
258
- } catch {
259
- return {};
260
- }
261
- }
262
-
263
- function getPlanStats(meta) {
264
- const steps = Array.isArray(meta?.plan_steps) ? meta.plan_steps : [];
265
- const done = steps.filter((step) => step && step.done).length;
266
- return {
267
- plan_steps_done: done,
268
- plan_steps_total: steps.length
269
- };
270
- }
271
-
272
- async function readJsonIfExists(filePath) {
273
- try {
274
- const raw = await fs.readFile(filePath, 'utf8');
275
- return JSON.parse(raw);
276
- } catch {
277
- return null;
278
- }
279
- }
280
-
281
- async function readLiveState(runtimeDir, sessionKey) {
282
- if (!sessionKey) return null;
283
- return readJsonIfExists(resolveLivePaths(runtimeDir, sessionKey).statePath);
284
- }
285
-
286
- async function writeLiveState(runtimeDir, sessionKey, state) {
287
- try {
288
- const { statePath } = resolveLivePaths(runtimeDir, sessionKey);
289
- await ensureDir(path.dirname(statePath));
290
- await fs.writeFile(statePath, JSON.stringify(state, null, 2), 'utf8');
291
- } catch {
292
- // filesystem is auxiliary — SQLite is source of truth
293
- }
294
- }
295
-
296
- async function appendLiveEvent(runtimeDir, sessionKey, record) {
297
- try {
298
- const { eventsPath } = resolveLivePaths(runtimeDir, sessionKey);
299
- await ensureDir(path.dirname(eventsPath));
300
- await fs.appendFile(eventsPath, `${JSON.stringify(record)}\n`, 'utf8');
301
- } catch {
302
- // filesystem is auxiliary — SQLite is source of truth
303
- }
304
- }
305
-
306
- async function writeLiveSummary(runtimeDir, sessionKey, markdown) {
307
- try {
308
- const { summaryPath } = resolveLivePaths(runtimeDir, sessionKey);
309
- await ensureDir(path.dirname(summaryPath));
310
- await fs.writeFile(summaryPath, markdown, 'utf8');
311
- return summaryPath;
312
- } catch {
313
- return null;
314
- }
315
- }
316
-
317
- async function listLiveStates(runtimeDir) {
318
- const liveRoot = path.join(runtimeDir, 'live');
319
- if (!(await exists(liveRoot))) {
320
- return [];
321
- }
322
-
323
- const entries = await fs.readdir(liveRoot, { withFileTypes: true });
324
- const states = [];
325
- for (const entry of entries) {
326
- if (!entry.isDirectory()) continue;
327
- const state = await readJsonIfExists(path.join(liveRoot, entry.name, 'state.json'));
328
- if (state) {
329
- states.push(state);
330
- }
331
- }
332
-
333
- states.sort((left, right) => {
334
- const leftStamp = Date.parse(left.updated_at || left.closed_at || left.started_at || 0);
335
- const rightStamp = Date.parse(right.updated_at || right.closed_at || right.started_at || 0);
336
- return rightStamp - leftStamp;
337
- });
338
-
339
- return states;
340
- }
341
-
342
- function createLiveState(targetDir, run, task, options = {}) {
343
- const taskMeta = parseTaskMeta(task);
344
- const planStats = getPlanStats(taskMeta);
345
- return {
346
- session_key: run.session_key || task?.session_key || options.sessionKey || null,
347
- session_task_key: task?.task_key || options.taskKey || null,
348
- tool_session: options.tool || taskMeta.tool_session || null,
349
- active_agent: options.activeAgent || run.agent_name || null,
350
- plan_ref: options.planRef ?? taskMeta.plan_ref ?? null,
351
- phase: options.phase || (run.status === 'running' || run.status === 'queued' ? 'active' : 'closed'),
352
- title: options.title || task?.title || run.title || null,
353
- path: options.projectPath || targetDir,
354
- child_pid: options.childPid ?? taskMeta.child_pid ?? null,
355
- started_at: options.startedAt || run.started_at || task?.created_at || null,
356
- updated_at: options.updatedAt || run.updated_at || task?.updated_at || null,
357
- closed_at: options.closedAt || (run.status === 'completed' || run.status === 'failed' ? run.finished_at || task?.finished_at || null : null),
358
- current_task: options.currentTask ?? null,
359
- current_run_key: options.currentRunKey || run.run_key,
360
- stats: normalizeLiveStats(options.stats, planStats),
361
- last_events: Array.isArray(options.lastEvents) ? options.lastEvents.slice(-LIVE_EVENTS_LIMIT) : []
362
- };
363
- }
364
-
365
- function selectLiveRunByKey(db, runKey) {
366
- if (!runKey) return null;
367
- return db.prepare(`
368
- SELECT
369
- run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
370
- title, status, summary, output_path, started_at, updated_at, finished_at
371
- FROM agent_runs
372
- WHERE run_key = ? AND source = 'live'
373
- LIMIT 1
374
- `).get(String(runKey));
375
- }
376
-
377
- function selectLatestLiveRun(db, options = {}) {
378
- if (options.sessionKey) {
379
- return db.prepare(`
380
- SELECT
381
- run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
382
- title, status, summary, output_path, started_at, updated_at, finished_at
383
- FROM agent_runs
384
- WHERE source = 'live' AND session_key = ?
385
- ORDER BY updated_at DESC, started_at DESC
386
- LIMIT 1
387
- `).get(String(options.sessionKey));
388
- }
389
-
390
- if (options.agentName) {
391
- return db.prepare(`
392
- SELECT
393
- run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
394
- title, status, summary, output_path, started_at, updated_at, finished_at
395
- FROM agent_runs
396
- WHERE source = 'live' AND agent_name = ?
397
- ORDER BY updated_at DESC, started_at DESC
398
- LIMIT 1
399
- `).get(String(options.agentName));
400
- }
401
-
402
- return db.prepare(`
403
- SELECT
404
- run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
405
- title, status, summary, output_path, started_at, updated_at, finished_at
406
- FROM agent_runs
407
- WHERE source = 'live'
408
- ORDER BY updated_at DESC, started_at DESC
409
- LIMIT 1
410
- `).get();
411
- }
412
-
413
- function selectTaskByKey(db, taskKey) {
414
- if (!taskKey) return null;
415
- return db.prepare(`
416
- SELECT
417
- task_key, squad_slug, session_key, task_kind, parent_task_key,
418
- title, goal, meta_json, status, created_by, created_at, updated_at, finished_at
419
- FROM tasks
420
- WHERE task_key = ?
421
- LIMIT 1
422
- `).get(String(taskKey));
423
- }
424
-
425
- function mapRecentDbEvent(event) {
426
- return {
427
- ts: event.created_at,
428
- type: event.event_type,
429
- summary: event.message || '-'
430
- };
431
- }
432
-
433
- async function resolveLiveContext(targetDir, db, runtimeDir, options = {}) {
434
- let agentName = options.agentName ? normalizeAgentHandle(options.agentName) : null;
435
- let sessionRef = agentName ? await readAgentSession(runtimeDir, agentName) : null;
436
- let sessionKey = options.sessionKey ? String(options.sessionKey).trim() : null;
437
- let state = null;
438
-
439
- if (!sessionKey && sessionRef?.sessionKey) {
440
- sessionKey = String(sessionRef.sessionKey).trim();
441
- }
442
-
443
- if (!sessionKey && !agentName) {
444
- const liveStates = await listLiveStates(runtimeDir);
445
- if (liveStates.length > 0) {
446
- state = liveStates[0];
447
- sessionKey = state.session_key || null;
448
- if (state.active_agent) {
449
- agentName = normalizeAgentHandle(state.active_agent);
450
- }
451
- }
452
- }
453
-
454
- if (!state && sessionKey) {
455
- state = await readLiveState(runtimeDir, sessionKey);
456
- }
457
-
458
- if (!agentName && state?.active_agent) {
459
- agentName = normalizeAgentHandle(state.active_agent);
460
- }
461
-
462
- let run = sessionRef?.runKey ? selectLiveRunByKey(db, sessionRef.runKey) : null;
463
- if (!run && state?.current_run_key) {
464
- run = selectLiveRunByKey(db, state.current_run_key);
465
- }
466
- if (!run && sessionKey) {
467
- run = selectLatestLiveRun(db, { sessionKey });
468
- }
469
- if (!run && agentName) {
470
- run = selectLatestLiveRun(db, { agentName });
471
- }
472
- if (!run && !agentName && !sessionKey) {
473
- run = selectLatestLiveRun(db);
474
- }
475
-
476
- let task = null;
477
- if (run?.task_key) {
478
- task = selectTaskByKey(db, run.task_key);
479
- }
480
- if (!task && sessionRef?.taskKey) {
481
- task = selectTaskByKey(db, sessionRef.taskKey);
482
- }
483
- if (!task && state?.session_task_key) {
484
- task = selectTaskByKey(db, state.session_task_key);
485
- }
486
-
487
- if (!sessionKey) {
488
- sessionKey = run?.session_key || task?.session_key || state?.session_key || sessionRef?.sessionKey || null;
489
- }
490
-
491
- if (!state && sessionKey) {
492
- state = await readLiveState(runtimeDir, sessionKey);
493
- }
494
-
495
- if (run && !state) {
496
- state = createLiveState(targetDir, run, task, {
497
- sessionKey,
498
- activeAgent: agentName || run.agent_name,
499
- projectPath: targetDir
500
- });
501
- }
502
-
503
- const recentEvents = run
504
- ? db.prepare(`
505
- SELECT event_type, message, created_at
506
- FROM execution_events
507
- WHERE run_key = ?
508
- ORDER BY created_at DESC, id DESC
509
- LIMIT ?
510
- `).all(run.run_key, Math.max(1, Math.min(Number(options.limit) || 8, 20))).reverse().map(mapRecentDbEvent)
511
- : [];
512
-
513
- const processState = detectProcessState(state?.child_pid);
514
- const phase = state?.phase || (run && (run.status === 'running' || run.status === 'queued') ? 'active' : run ? 'closed' : 'idle');
515
- const open = phase === 'active' && Boolean(run && (run.status === 'running' || run.status === 'queued'));
516
-
517
- return {
518
- agentName: agentName || state?.active_agent || run?.agent_name || null,
519
- sessionRef,
520
- sessionKey,
521
- run,
522
- task,
523
- state,
524
- recentEvents,
525
- processState,
526
- phase,
527
- open,
528
- paths: sessionKey ? resolveLivePaths(runtimeDir, sessionKey) : null
529
- };
530
- }
531
-
532
- async function requireActiveLiveContext(targetDir, agentName, t, options = {}) {
533
- const { db, dbPath, runtimeDir } = await withRuntimeDb(targetDir, t);
534
- const context = await resolveLiveContext(targetDir, db, runtimeDir, {
535
- agentName,
536
- limit: options.limit
537
- });
538
-
539
- if (!context.run || context.run.source !== 'live' || !context.sessionKey || !context.task) {
540
- db.close();
541
- throw new Error(t('live.no_active_session', { agent: normalizeAgentHandle(agentName) }));
542
- }
543
-
544
- if (context.phase !== 'active') {
545
- db.close();
546
- throw new Error(t('live.session_not_active', { agent: normalizeAgentHandle(agentName) }));
547
- }
548
-
549
- return { db, dbPath, runtimeDir, context };
550
- }
551
-
552
- function applyEventToState(state, event, updates = {}) {
553
- const next = {
554
- ...state,
555
- updated_at: event.ts,
556
- stats: normalizeLiveStats(state?.stats)
557
- };
558
-
559
- next.last_events = [...(Array.isArray(state?.last_events) ? state.last_events : []), {
560
- ts: event.ts,
561
- type: event.type,
562
- summary: event.summary
563
- }].slice(-LIVE_EVENTS_LIMIT);
564
- next.stats.events_total += 1;
565
- if (event.type) {
566
- next.stats.events_by_type[event.type] = (next.stats.events_by_type[event.type] || 0) + 1;
567
- }
568
-
569
- if (event.type === 'task_completed') {
570
- next.stats.tasks_completed += 1;
571
- }
572
-
573
- if (Object.prototype.hasOwnProperty.call(updates, 'currentTask')) {
574
- next.current_task = updates.currentTask;
575
- }
576
- if (Object.prototype.hasOwnProperty.call(updates, 'phase')) {
577
- next.phase = updates.phase;
578
- }
579
- if (Object.prototype.hasOwnProperty.call(updates, 'closedAt')) {
580
- next.closed_at = updates.closedAt;
581
- }
582
- if (Object.prototype.hasOwnProperty.call(updates, 'activeAgent')) {
583
- next.active_agent = updates.activeAgent;
584
- }
585
- if (Object.prototype.hasOwnProperty.call(updates, 'currentRunKey')) {
586
- next.current_run_key = updates.currentRunKey;
587
- }
588
- if (Object.prototype.hasOwnProperty.call(updates, 'childPid')) {
589
- next.child_pid = updates.childPid;
590
- }
591
- if (updates.planStats) {
592
- next.stats.plan_steps_done = Number(updates.planStats.plan_steps_done || 0);
593
- next.stats.plan_steps_total = Number(updates.planStats.plan_steps_total || 0);
594
- }
595
-
596
- return next;
597
- }
598
-
599
- function createLiveEventRecord(context, options = {}) {
600
- return {
601
- ts: options.ts,
602
- type: options.type,
603
- summary: options.summary,
604
- agent: context.agentName,
605
- task_key: options.taskKey || context.task?.task_key || null,
606
- run_key: context.run?.run_key || null,
607
- session_key: context.sessionKey,
608
- refs: options.refs || [],
609
- plan_step: options.planStep || null,
610
- status: options.status || null,
611
- meta: options.meta || null
612
- };
613
- }
614
-
615
- function waitForChild(child) {
616
- return new Promise((resolve, reject) => {
617
- child.once('error', reject);
618
- child.once('close', (code, signal) => {
619
- resolve({ code: Number(code || 0), signal: signal || null });
620
- });
621
- });
622
- }
623
-
624
- async function runLocalProcess(command, args, options = {}) {
625
- return new Promise((resolve) => {
626
- const child = spawn(command, args.map((entry) => String(entry)), {
627
- cwd: options.cwd || process.cwd(),
628
- env: { ...process.env, ...(options.env || {}) },
629
- stdio: ['ignore', 'pipe', 'pipe']
630
- });
631
-
632
- let stdout = '';
633
- let stderr = '';
634
- child.stdout.on('data', (chunk) => {
635
- stdout += String(chunk);
636
- });
637
- child.stderr.on('data', (chunk) => {
638
- stderr += String(chunk);
639
- });
640
- child.on('error', () => {
641
- resolve({ code: 1, stdout, stderr });
642
- });
643
- child.on('close', (code) => {
644
- resolve({ code: Number(code || 0), stdout, stderr });
645
- });
646
- });
647
- }
648
-
649
- async function collectGitSnapshot(targetDir) {
650
- if (!(await exists(path.join(targetDir, '.git')))) {
651
- return null;
652
- }
653
-
654
- const [branch, commit, diffStat, status] = await Promise.all([
655
- runLocalProcess('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: targetDir }),
656
- runLocalProcess('git', ['rev-parse', '--short', 'HEAD'], { cwd: targetDir }),
657
- runLocalProcess('git', ['diff', '--stat'], { cwd: targetDir }),
658
- runLocalProcess('git', ['status', '--short'], { cwd: targetDir })
659
- ]);
660
-
661
- if (branch.code !== 0 && commit.code !== 0 && diffStat.code !== 0 && status.code !== 0) {
662
- return null;
663
- }
664
-
665
- return {
666
- branch: branch.code === 0 ? branch.stdout.trim() : null,
667
- commit: commit.code === 0 ? commit.stdout.trim() : null,
668
- diff_stat: diffStat.code === 0 ? diffStat.stdout.trim() : null,
669
- changed_files: status.code === 0
670
- ? status.stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean).map((line) => line.slice(3).trim() || line)
671
- : []
672
- };
673
- }
674
-
675
- function formatDuration(startedAt, closedAt) {
676
- if (!startedAt || !closedAt) return null;
677
- const ms = Date.parse(closedAt) - Date.parse(startedAt);
678
- if (!Number.isFinite(ms) || ms < 0) return null;
679
- const seconds = Math.floor(ms / 1000);
680
- const hours = Math.floor(seconds / 3600);
681
- const minutes = Math.floor((seconds % 3600) / 60);
682
- const secs = seconds % 60;
683
- if (hours > 0) return `${hours}h ${minutes}m`;
684
- if (minutes > 0) return `${minutes}m ${secs}s`;
685
- return `${secs}s`;
686
- }
687
-
688
- function renderLiveSummary(snapshot) {
689
- const duration = formatDuration(snapshot.startedAt, snapshot.closedAt);
690
- const lines = [
691
- '# Live Session Summary',
692
- '',
693
- `- Session: ${snapshot.sessionKey}`,
694
- `- Agent: ${snapshot.agent}`,
695
- `- Tool: ${snapshot.tool || 'unknown'}`,
696
- `- Status: ${snapshot.status}`,
697
- `- Started: ${snapshot.startedAt || 'unknown'}`,
698
- `- Closed: ${snapshot.closedAt || 'unknown'}`,
699
- ...(duration ? [`- Duration: ${duration}`] : []),
700
- `- Summary: ${snapshot.summary || 'n/a'}`
701
- ];
702
-
703
- if (snapshot.git) {
704
- lines.push('');
705
- lines.push('## Git');
706
- lines.push(`- Branch: ${snapshot.git.branch || 'unknown'}`);
707
- lines.push(`- Commit: ${snapshot.git.commit || 'unknown'}`);
708
- if (snapshot.git.diff_stat) {
709
- lines.push('');
710
- lines.push('```text');
711
- lines.push(snapshot.git.diff_stat);
712
- lines.push('```');
713
- }
714
- if (snapshot.git.changed_files.length > 0) {
715
- lines.push('');
716
- lines.push('## Changed Files');
717
- for (const file of snapshot.git.changed_files) {
718
- lines.push(`- ${file}`);
719
- }
720
- }
721
- }
722
-
723
- if (Array.isArray(snapshot.recentEvents) && snapshot.recentEvents.length > 0) {
724
- lines.push('');
725
- lines.push('## Recent Events');
726
- for (const event of snapshot.recentEvents) {
727
- lines.push(`- ${event.ts} | ${event.type} | ${event.summary}`);
728
- }
729
- }
730
-
731
- lines.push('');
732
- return lines.join('\n');
733
- }
734
-
735
- function printLiveStatusSnapshot(snapshot, logger) {
736
- logger.log(`Live session: ${snapshot.sessionKey || 'none'}`);
737
- logger.log(`Phase: ${snapshot.phase}`);
738
- logger.log(`Tool: ${snapshot.tool || '-'}`);
739
- logger.log(`Active agent: ${snapshot.agent || '-'}`);
740
- if (snapshot.stats && Number(snapshot.stats.plan_steps_total || 0) > 0) {
741
- logger.log(`Plan: ${snapshot.stats.plan_steps_done || 0}/${snapshot.stats.plan_steps_total || 0}`);
742
- }
743
- logger.log(`Process: ${snapshot.processState}${snapshot.pid ? ` (pid ${snapshot.pid})` : ''}`);
744
-
745
- if (snapshot.task) {
746
- logger.log(`Task: ${snapshot.task.task_key} | status: ${snapshot.task.status} | work: ${snapshot.task.title || '-'}`);
747
- }
748
- if (snapshot.run) {
749
- logger.log(`Run: ${snapshot.run.run_key} | status: ${snapshot.run.status} | work: ${snapshot.run.title || snapshot.run.summary || '-'}`);
750
- }
751
- if (snapshot.startedAt) {
752
- logger.log(`Started: ${snapshot.startedAt}`);
753
- }
754
- if (snapshot.updatedAt) {
755
- logger.log(`Updated: ${snapshot.updatedAt}`);
756
- }
757
- if (snapshot.closedAt) {
758
- logger.log(`Closed: ${snapshot.closedAt}`);
759
- }
760
- if (snapshot.warning) {
761
- logger.log(`Warning: ${snapshot.warning}`);
762
- }
763
-
764
- if (snapshot.recentEvents.length === 0) {
765
- logger.log('Recent events: none');
766
- return;
767
- }
768
-
769
- logger.log('Recent events:');
770
- for (const event of snapshot.recentEvents) {
771
- logger.log(`- ${event.ts} | ${event.type} | ${event.summary || '-'}`);
772
- }
773
- }
774
-
775
- async function getLiveStatusSnapshot(targetDir, t, options = {}) {
776
- const { dbPath } = resolveRuntimePaths(targetDir);
777
-
778
- if (!(await runtimeStoreExists(targetDir))) {
779
- throw new Error(t('runtime.store_missing', { path: dbPath }));
780
- }
781
-
782
- const { db, runtimeDir } = await openRuntimeDb(targetDir, { mustExist: true });
783
- try {
784
- const context = await resolveLiveContext(targetDir, db, runtimeDir, {
785
- agentName: options.agent,
786
- limit: options.limit
787
- });
788
-
789
- if (!context.run && !context.state) {
790
- return {
791
- ok: true,
792
- targetDir,
793
- dbPath,
794
- agent: context.agentName,
795
- tool: null,
796
- phase: 'idle',
797
- open: false,
798
- processState: 'not_tracked',
799
- pid: null,
800
- sessionKey: null,
801
- startedAt: null,
802
- updatedAt: null,
803
- closedAt: null,
804
- title: null,
805
- currentTask: null,
806
- run: null,
807
- task: null,
808
- stats: normalizeLiveStats(null),
809
- recentEvents: []
810
- };
811
- }
812
-
813
- const taskMeta = parseTaskMeta(context.task);
814
- const planStats = getPlanStats(taskMeta);
815
- const state = context.state || createLiveState(targetDir, context.run, context.task, {
816
- sessionKey: context.sessionKey,
817
- activeAgent: context.agentName,
818
- projectPath: targetDir
819
- });
820
- state.stats = normalizeLiveStats(state.stats, planStats);
821
-
822
- const snapshot = {
823
- ok: true,
824
- targetDir,
825
- dbPath,
826
- agent: context.agentName,
827
- tool: state.tool_session || null,
828
- phase: context.phase,
829
- open: context.open,
830
- processState: context.processState,
831
- pid: state.child_pid || null,
832
- sessionKey: context.sessionKey,
833
- startedAt: state.started_at || null,
834
- updatedAt: state.updated_at || null,
835
- closedAt: state.closed_at || null,
836
- title: state.title || null,
837
- currentTask: state.current_task || null,
838
- run: context.run,
839
- task: context.task,
840
- stats: state.stats,
841
- recentEvents: Array.isArray(state.last_events) && state.last_events.length > 0 ? state.last_events : context.recentEvents,
842
- warning: context.processState === 'dead' && context.phase === 'active'
843
- ? t('live.process_dead_warning')
844
- : null
845
- };
846
-
847
- return snapshot;
848
- } finally {
849
- db.close();
850
- }
851
- }
852
-
853
- async function runLiveStart({ args, options = {}, logger, t }) {
854
- const targetDir = resolveTargetDir(args);
855
- const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
856
- const tool = normalizeLiveTool(requireOption(options, 'tool', t), t);
857
- const noLaunch = Boolean(options['no-launch']);
858
-
859
- if (options.json && !noLaunch && !options.attach) {
860
- throw new Error(t('live.json_requires_no_launch'));
861
- }
862
-
863
- // ── 5.3 Ambient Intelligence health check alert ────────────────────────────
864
- if (!options.json && !options['no-health-check']) {
865
- try {
866
- const { runHealthCheck, formatHealthAlert } = require('../lib/health-check');
867
- const health = await runHealthCheck(targetDir);
868
- const alert = formatHealthAlert(health.items);
869
- if (alert) {
870
- logger.log('');
871
- logger.log(alert);
872
- logger.log('');
873
- }
874
- } catch { /* health check is non-fatal */ }
875
- }
876
-
877
- const toolBinary = String(options['tool-bin'] || tool).trim();
878
- const binaryPath = await resolveExecutablePath(toolBinary);
879
- if (!binaryPath) {
880
- throw new Error(t('live.tool_binary_not_found', { binary: toolBinary }));
881
- }
882
-
883
- const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
884
-
885
- try {
886
- const existing = await resolveLiveContext(targetDir, db, runtimeDir, {
887
- agentName,
888
- limit: options.limit
889
- });
890
-
891
- if (existing.run && existing.run.source === 'live' && existing.open) {
892
- const state = existing.state || createLiveState(targetDir, existing.run, existing.task, {
893
- sessionKey: existing.sessionKey,
894
- activeAgent: existing.agentName,
895
- projectPath: targetDir
896
- });
897
-
898
- const existingTool = state.tool_session || null;
899
- if (existingTool && existingTool !== tool) {
900
- throw new Error(t('live.tool_mismatch', { existing: existingTool, requested: tool }));
901
- }
902
-
903
- const attach = Boolean(options.attach);
904
- let attachChild = null;
905
- let attachResult = null;
906
-
907
- if (attach && !noLaunch) {
908
- attachChild = spawn(binaryPath, parseToolArgs(options['tool-args'] || options.toolArgs), {
909
- cwd: targetDir,
910
- env: process.env,
911
- stdio: 'inherit'
912
- });
913
- state.child_pid = attachChild.pid || null;
914
- if (existing.task?.task_key) {
915
- const taskMeta = parseTaskMeta(existing.task);
916
- taskMeta.child_pid = state.child_pid;
917
- updateTask(db, { taskKey: existing.task.task_key, metaJson: taskMeta });
918
- }
919
- }
920
-
921
- await writeLiveState(runtimeDir, existing.sessionKey, state);
922
-
923
- if (!options.json) {
924
- logger.log(t('live.session_already_active', { agent: agentName, session: existing.sessionKey, runKey: existing.run.run_key, dbPath }));
925
- }
926
-
927
- if (attachChild) {
928
- attachResult = await waitForChild(attachChild);
929
- }
930
-
931
- return {
932
- ok: true,
933
- targetDir,
934
- dbPath,
935
- agent: existing.agentName,
936
- tool: state.tool_session || tool,
937
- taskKey: existing.task?.task_key || existing.sessionRef?.taskKey || null,
938
- runKey: existing.run.run_key,
939
- sessionKey: existing.sessionKey,
940
- pid: state.child_pid || null,
941
- processState: detectProcessState(state.child_pid),
942
- reused: true,
943
- open: true,
944
- attached: attach,
945
- childExitCode: attachResult?.code ?? null,
946
- childSignal: attachResult?.signal ?? null
947
- };
948
- }
949
-
950
- const now = new Date().toISOString();
951
- const sessionKey = options.session ? String(options.session).trim() : makeDirectSessionKey(agentName);
952
- const title = options.title ? String(options.title).trim() : `live-${tool}-${Date.now()}`;
953
- const goal = options.goal ? String(options.goal).trim() : null;
954
- const planRef = options.plan ? String(options.plan).trim() : null;
955
- const plan = await loadPlanReference(targetDir, planRef);
956
- const startMessage = truncateMessage(options.message, `Live session started for ${agentName} with ${tool}`);
957
- const taskMeta = {
958
- tool_session: tool,
959
- plan_ref: plan.planRef,
960
- path: targetDir,
961
- child_pid: null
962
- };
963
- if (plan.planSteps.length > 0) {
964
- taskMeta.plan_steps = plan.planSteps;
965
- }
966
-
967
- const taskKey = startTask(db, {
968
- sessionKey,
969
- title,
970
- goal,
971
- status: 'running',
972
- createdBy: agentName,
973
- taskKind: 'live_session',
974
- metaJson: taskMeta
975
- });
976
-
977
- const runKey = startRun(db, {
978
- taskKey,
979
- agentName,
980
- agentKind: 'official',
981
- sessionKey,
982
- source: 'live',
983
- title,
984
- eventType: 'session_started',
985
- phase: 'live',
986
- message: startMessage,
987
- payload: {
988
- tool_session: tool,
989
- plan_ref: plan.planRef,
990
- plan_steps_total: plan.planSteps.length,
991
- path: targetDir
992
- }
993
- });
994
-
995
- let child = null;
996
- let childResult = null;
997
- if (!noLaunch) {
998
- child = spawn(binaryPath, parseToolArgs(options['tool-args'] || options.toolArgs), {
999
- cwd: targetDir,
1000
- env: process.env,
1001
- stdio: 'inherit'
1002
- });
1003
- taskMeta.child_pid = child.pid || null;
1004
- updateTask(db, {
1005
- taskKey,
1006
- metaJson: taskMeta
1007
- });
1008
- }
1009
-
1010
- await writeAgentSession(runtimeDir, agentName, {
1011
- runKey,
1012
- taskKey,
1013
- sessionKey,
1014
- startedAt: now,
1015
- finished: false,
1016
- source: 'live'
1017
- });
1018
-
1019
- const state = createLiveState(targetDir, {
1020
- run_key: runKey,
1021
- session_key: sessionKey,
1022
- agent_name: agentName,
1023
- title,
1024
- status: 'running',
1025
- started_at: now,
1026
- updated_at: now
1027
- }, {
1028
- task_key: taskKey,
1029
- session_key: sessionKey,
1030
- title,
1031
- meta_json: JSON.stringify(taskMeta),
1032
- created_at: now,
1033
- updated_at: now
1034
- }, {
1035
- tool,
1036
- planRef: plan.planRef,
1037
- activeAgent: agentName,
1038
- currentRunKey: runKey,
1039
- projectPath: targetDir,
1040
- childPid: taskMeta.child_pid,
1041
- stats: {
1042
- tasks_completed: 0,
1043
- events_total: 1,
1044
- plan_steps_done: 0,
1045
- plan_steps_total: plan.planSteps.length
1046
- },
1047
- lastEvents: [{
1048
- ts: now,
1049
- type: 'session_started',
1050
- summary: startMessage
1051
- }]
1052
- });
1053
-
1054
- await writeLiveState(runtimeDir, sessionKey, state);
1055
- await appendLiveEvent(runtimeDir, sessionKey, {
1056
- ts: now,
1057
- type: 'session_started',
1058
- summary: startMessage,
1059
- agent: agentName,
1060
- task_key: taskKey,
1061
- run_key: runKey,
1062
- session_key: sessionKey,
1063
- refs: [],
1064
- plan_step: null,
1065
- status: 'running',
1066
- meta: {
1067
- tool_session: tool,
1068
- child_pid: taskMeta.child_pid,
1069
- path: targetDir,
1070
- plan_ref: plan.planRef,
1071
- plan_steps_total: plan.planSteps.length
1072
- }
1073
- });
1074
-
1075
- if (!options.json) {
1076
- logger.log(t('live.session_started', { agent: agentName, tool, session: sessionKey, dbPath }));
1077
- }
1078
-
1079
- // Ambient Intelligence: exibe digest de saúde ao iniciar sessão
1080
- if (!options.json && !options['no-health']) {
1081
- try {
1082
- const { getHealthDigest } = require('./health');
1083
- const items = await getHealthDigest(targetDir);
1084
- if (items && items.length > 0) {
1085
- logger.log('');
1086
- logger.log('AIOSON Health — itens pendentes:');
1087
- for (const item of items) {
1088
- logger.log(` ● ${item}`);
1089
- }
1090
- logger.log(' → aioson health . para detalhes');
1091
- logger.log('');
1092
- }
1093
- } catch { /* não bloqueia o start */ }
1094
- }
1095
-
1096
- if (child) {
1097
- childResult = await waitForChild(child);
1098
- }
1099
-
1100
- return {
1101
- ok: true,
1102
- targetDir,
1103
- dbPath,
1104
- agent: agentName,
1105
- tool,
1106
- taskKey,
1107
- runKey,
1108
- sessionKey,
1109
- pid: taskMeta.child_pid,
1110
- processState: detectProcessState(taskMeta.child_pid),
1111
- reused: false,
1112
- open: true,
1113
- childExitCode: childResult?.code ?? null,
1114
- childSignal: childResult?.signal ?? null
1115
- };
1116
- } finally {
1117
- db.close();
1118
- }
1119
- }
1120
-
1121
- async function runRuntimeEmit({ args, options = {}, logger, t }) {
1122
- const targetDir = resolveTargetDir(args);
1123
- const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1124
- const eventType = String(options.type || 'note').trim() || 'note';
1125
-
1126
- const { db, dbPath, runtimeDir, context } = await requireActiveLiveContext(targetDir, agentName, t, {
1127
- limit: options.limit
1128
- });
1129
-
1130
- try {
1131
- const now = new Date().toISOString();
1132
- const refs = parseRefs(options.refs);
1133
- const planStep = options['plan-step'] ? String(options['plan-step']).trim() : null;
1134
- const summary = truncateMessage(
1135
- options.summary || options.message || options.title || `${eventType} emitted by ${agentName}`
1136
- );
1137
- const meta = parseJsonOption(options.meta);
1138
- const payload = meta && typeof meta === 'object' ? { ...meta } : {};
1139
- if (refs.length > 0) payload.refs = refs;
1140
- if (planStep) payload.plan_step = planStep;
1141
-
1142
- const state = context.state || createLiveState(targetDir, context.run, context.task, {
1143
- sessionKey: context.sessionKey,
1144
- activeAgent: context.agentName,
1145
- projectPath: targetDir
1146
- });
1147
-
1148
- let currentTaskKey = state.current_task || null;
1149
- let nextCurrentTask = currentTaskKey;
1150
-
1151
- if (eventType === 'task_started') {
1152
- if (currentTaskKey) {
1153
- throw new Error(t('live.micro_task_already_open', { agent: agentName }));
1154
- }
1155
-
1156
- currentTaskKey = startTask(db, {
1157
- sessionKey: context.sessionKey,
1158
- title: options.title ? String(options.title).trim() : summary,
1159
- goal: summary,
1160
- status: 'running',
1161
- createdBy: agentName,
1162
- taskKind: 'micro_task',
1163
- parentTaskKey: context.task.task_key,
1164
- metaJson: {
1165
- refs,
1166
- plan_step: planStep
1167
- }
1168
- });
1169
- nextCurrentTask = currentTaskKey;
1170
- payload.micro_task_key = currentTaskKey;
1171
- } else if (eventType === 'task_completed') {
1172
- if (currentTaskKey) {
1173
- updateTask(db, {
1174
- taskKey: currentTaskKey,
1175
- status: 'completed',
1176
- goal: summary,
1177
- metaJson: {
1178
- refs,
1179
- plan_step: planStep
1180
- }
1181
- });
1182
- } else {
1183
- currentTaskKey = startTask(db, {
1184
- sessionKey: context.sessionKey,
1185
- title: options.title ? String(options.title).trim() : summary,
1186
- goal: summary,
1187
- status: 'completed',
1188
- createdBy: agentName,
1189
- taskKind: 'micro_task',
1190
- parentTaskKey: context.task.task_key,
1191
- metaJson: {
1192
- refs,
1193
- plan_step: planStep,
1194
- implicit: true
1195
- }
1196
- });
1197
- }
1198
- nextCurrentTask = null;
1199
- payload.micro_task_key = currentTaskKey;
1200
- }
1201
-
1202
- let planStats = null;
1203
- if (eventType === 'plan_checkpoint' && planStep) {
1204
- const sessionMeta = parseTaskMeta(context.task);
1205
- if (Array.isArray(sessionMeta.plan_steps)) {
1206
- const normalizedPlanStep = normalizePlanStepId(planStep).toLowerCase();
1207
- let changed = false;
1208
- sessionMeta.plan_steps = sessionMeta.plan_steps.map((step) => {
1209
- if (!step || normalizePlanStepId(step.id).toLowerCase() !== normalizedPlanStep) return step;
1210
- if (step.done) return step;
1211
- changed = true;
1212
- return { ...step, done: true };
1213
- });
1214
- if (changed) {
1215
- updateTask(db, {
1216
- taskKey: context.task.task_key,
1217
- metaJson: sessionMeta
1218
- });
1219
- planStats = getPlanStats(sessionMeta);
1220
- }
1221
- }
1222
- }
1223
-
1224
- const workerStatus = options['worker-status'] ? String(options['worker-status']).trim() : null;
1225
- const verdict = options.verdict ? String(options.verdict).trim().toUpperCase() : null;
1226
- const tokenCount = options['token-count'] != null ? Number(options['token-count']) || null : null;
1227
- const progressPct = options['progress-pct'] != null ? Number(options['progress-pct']) || null : null;
1228
-
1229
- appendRunEvent(db, {
1230
- runKey: context.run.run_key,
1231
- eventType,
1232
- phase: 'live',
1233
- status: context.run.status || 'running',
1234
- message: summary,
1235
- payload: Object.keys(payload).length > 0 ? payload : null,
1236
- createdAt: now,
1237
- planStepId: planStep || null,
1238
- workerStatus,
1239
- verdict,
1240
- tokenCount,
1241
- progressPct
1242
- });
1243
-
1244
- const eventRecord = createLiveEventRecord(context, {
1245
- ts: now,
1246
- type: eventType,
1247
- summary,
1248
- refs,
1249
- planStep,
1250
- taskKey: currentTaskKey,
1251
- meta: meta && Object.keys(meta).length > 0 ? meta : null
1252
- });
1253
-
1254
- await appendLiveEvent(runtimeDir, context.sessionKey, eventRecord);
1255
-
1256
- const nextState = applyEventToState(state, eventRecord, {
1257
- currentTask: nextCurrentTask,
1258
- planStats,
1259
- activeAgent: context.agentName,
1260
- currentRunKey: context.run.run_key
1261
- });
1262
- await writeLiveState(runtimeDir, context.sessionKey, nextState);
1263
-
1264
- if (!options.json) {
1265
- logger.log(t('live.event_recorded', { agent: agentName, eventType, session: context.sessionKey, dbPath }));
1266
- }
1267
-
1268
- return {
1269
- ok: true,
1270
- targetDir,
1271
- dbPath,
1272
- agent: context.agentName,
1273
- eventType,
1274
- sessionKey: context.sessionKey,
1275
- runKey: context.run.run_key,
1276
- taskKey: currentTaskKey || context.task.task_key,
1277
- currentTask: nextCurrentTask,
1278
- open: true
1279
- };
1280
- } finally {
1281
- db.close();
1282
- }
1283
- }
1284
-
1285
-
1286
- async function runLiveHandoff({ args, options = {}, logger, t }) {
1287
- const targetDir = resolveTargetDir(args);
1288
- const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1289
- const nextAgent = normalizeAgentHandle(requireOption(options, 'to', t));
1290
-
1291
- if (agentName === nextAgent) {
1292
- throw new Error(t('live.handoff_same_agent'));
1293
- }
1294
-
1295
- const reason = truncateMessage(
1296
- options.reason || options.summary || options.message,
1297
- `Handoff from ${agentName} to ${nextAgent}`
1298
- );
1299
-
1300
- const { db, dbPath, runtimeDir, context } = await requireActiveLiveContext(targetDir, agentName, t, {
1301
- limit: options.limit
1302
- });
1303
-
1304
- try {
1305
- if (!context.run || context.run.agent_name !== agentName) {
1306
- throw new Error(t('live.handoff_agent_mismatch', { agent: agentName }));
1307
- }
1308
-
1309
- const now = new Date().toISOString();
1310
- const state = context.state || createLiveState(targetDir, context.run, context.task, {
1311
- sessionKey: context.sessionKey,
1312
- activeAgent: context.agentName,
1313
- projectPath: targetDir
1314
- });
1315
-
1316
- const handoffSummary = truncateMessage(`Handoff to ${nextAgent}: ${reason}`);
1317
- let currentTaskClosed = false;
1318
- if (state.current_task) {
1319
- updateTask(db, {
1320
- taskKey: state.current_task,
1321
- status: 'completed',
1322
- goal: truncateMessage(`Closed on handoff to ${nextAgent}: ${reason}`)
1323
- });
1324
- currentTaskClosed = true;
1325
- }
1326
-
1327
- updateRun(db, {
1328
- runKey: context.run.run_key,
1329
- status: 'completed',
1330
- summary: reason,
1331
- eventType: 'handoff',
1332
- phase: 'live',
1333
- message: handoffSummary,
1334
- payload: {
1335
- from: agentName,
1336
- to: nextAgent,
1337
- reason,
1338
- previous_run_key: context.run.run_key,
1339
- micro_task_key: state.current_task || null,
1340
- closed_by: 'live:handoff'
1341
- }
1342
- });
1343
-
1344
- const nextRunKey = startRun(db, {
1345
- taskKey: context.task.task_key,
1346
- agentName: nextAgent,
1347
- agentKind: 'official',
1348
- sessionKey: context.sessionKey,
1349
- source: 'live',
1350
- parentRunKey: context.run.run_key,
1351
- title: nextAgent,
1352
- phase: 'live',
1353
- message: truncateMessage(`Live handoff from ${agentName}`),
1354
- payload: {
1355
- handoff_from: agentName,
1356
- reason
1357
- }
1358
- });
1359
-
1360
- await clearAgentSession(runtimeDir, agentName);
1361
- await writeAgentSession(runtimeDir, nextAgent, {
1362
- runKey: nextRunKey,
1363
- taskKey: context.task.task_key,
1364
- sessionKey: context.sessionKey,
1365
- startedAt: now,
1366
- finished: false,
1367
- source: 'live'
1368
- });
1369
-
1370
- const eventRecord = createLiveEventRecord(context, {
1371
- ts: now,
1372
- type: 'handoff',
1373
- summary: handoffSummary,
1374
- taskKey: context.task.task_key,
1375
- status: 'completed',
1376
- meta: {
1377
- from: agentName,
1378
- to: nextAgent,
1379
- reason,
1380
- previous_run_key: context.run.run_key,
1381
- current_run_key: nextRunKey,
1382
- micro_task_key: state.current_task || null
1383
- }
1384
- });
1385
- await appendLiveEvent(runtimeDir, context.sessionKey, eventRecord);
1386
-
1387
- const nextState = applyEventToState(state, eventRecord, {
1388
- currentTask: null,
1389
- activeAgent: nextAgent,
1390
- currentRunKey: nextRunKey
1391
- });
1392
- if (currentTaskClosed) {
1393
- nextState.stats.tasks_completed += 1;
1394
- }
1395
- await writeLiveState(runtimeDir, context.sessionKey, nextState);
1396
-
1397
- if (!options.json) {
1398
- logger.log(t('live.handoff_recorded', { from: agentName, to: nextAgent, session: context.sessionKey, dbPath }));
1399
- }
1400
-
1401
- return {
1402
- ok: true,
1403
- targetDir,
1404
- dbPath,
1405
- agent: agentName,
1406
- nextAgent,
1407
- taskKey: context.task.task_key,
1408
- previousRunKey: context.run.run_key,
1409
- runKey: nextRunKey,
1410
- sessionKey: context.sessionKey,
1411
- open: true
1412
- };
1413
- } finally {
1414
- db.close();
1415
- }
1416
- }
1417
-
1418
- async function runLiveStatus({ args, options = {}, logger, t }) {
1419
- const targetDir = resolveTargetDir(args);
1420
- const watchSeconds = parseWatchSeconds(options.watch);
1421
-
1422
- if (watchSeconds && options.json) {
1423
- throw new Error(t('live.watch_json_conflict'));
1424
- }
1425
-
1426
- if (!watchSeconds) {
1427
- const snapshot = await getLiveStatusSnapshot(targetDir, t, options);
1428
- if (!options.json) {
1429
- printLiveStatusSnapshot(snapshot, logger);
1430
- }
1431
- return snapshot;
1432
- }
1433
-
1434
- let stopped = false;
1435
- const onSignal = () => { stopped = true; };
1436
- process.on('SIGINT', onSignal);
1437
- process.on('SIGTERM', onSignal);
1438
-
1439
- try {
1440
- while (!stopped) {
1441
- const snapshot = await getLiveStatusSnapshot(targetDir, t, options);
1442
- if (process.stdout && process.stdout.isTTY) {
1443
- process.stdout.write('\x1Bc');
1444
- }
1445
- printLiveStatusSnapshot(snapshot, logger);
1446
- if (stopped) break;
1447
- await sleep(Math.round(watchSeconds * 1000));
1448
- }
1449
- } finally {
1450
- process.removeListener('SIGINT', onSignal);
1451
- process.removeListener('SIGTERM', onSignal);
1452
- }
1453
- }
1454
-
1455
- async function runLiveClose({ args, options = {}, logger, t }) {
1456
- const targetDir = resolveTargetDir(args);
1457
- const requestedAgent = options.agent ? normalizeAgentHandle(options.agent) : null;
1458
- const { db, dbPath, runtimeDir } = await withRuntimeDb(targetDir, t);
1459
-
1460
- try {
1461
- const context = await resolveLiveContext(targetDir, db, runtimeDir, {
1462
- agentName: requestedAgent,
1463
- limit: options.limit
1464
- });
1465
-
1466
- if (!context.run || context.run.source !== 'live' || !context.sessionKey || !context.task) {
1467
- throw new Error(requestedAgent
1468
- ? t('live.no_session_for_agent', { agent: requestedAgent })
1469
- : t('live.no_session_found'));
1470
- }
1471
-
1472
- if (context.phase !== 'active') {
1473
- throw new Error(t('live.session_already_closed', { session: context.sessionKey }));
1474
- }
1475
-
1476
- const status = String(options.status || 'completed').trim().toLowerCase() === 'failed' ? 'failed' : 'completed';
1477
- const now = new Date().toISOString();
1478
- const summary = truncateMessage(options.summary || options.message || `Live session closed for ${context.agentName}`);
1479
- const state = context.state || createLiveState(targetDir, context.run, context.task, {
1480
- sessionKey: context.sessionKey,
1481
- activeAgent: context.agentName,
1482
- projectPath: targetDir
1483
- });
1484
-
1485
- let currentTaskClosed = false;
1486
- if (state.current_task) {
1487
- updateTask(db, {
1488
- taskKey: state.current_task,
1489
- status,
1490
- goal: summary
1491
- });
1492
- currentTaskClosed = true;
1493
- }
1494
-
1495
- updateRun(db, {
1496
- runKey: context.run.run_key,
1497
- status,
1498
- summary,
1499
- eventType: 'session_closed',
1500
- message: summary,
1501
- payload: {
1502
- closed_by: 'live:close'
1503
- }
1504
- });
1505
-
1506
- updateTask(db, {
1507
- taskKey: context.task.task_key,
1508
- status,
1509
- goal: summary
1510
- });
1511
-
1512
- const eventRecord = createLiveEventRecord(context, {
1513
- ts: now,
1514
- type: 'session_closed',
1515
- summary,
1516
- taskKey: context.task.task_key,
1517
- status
1518
- });
1519
- await appendLiveEvent(runtimeDir, context.sessionKey, eventRecord);
1520
-
1521
- const nextState = applyEventToState(state, eventRecord, {
1522
- currentTask: null,
1523
- phase: 'closed',
1524
- closedAt: now,
1525
- activeAgent: context.agentName,
1526
- currentRunKey: context.run.run_key
1527
- });
1528
- if (currentTaskClosed && status === 'completed') {
1529
- nextState.stats.tasks_completed += 1;
1530
- }
1531
-
1532
- const git = await collectGitSnapshot(targetDir);
1533
- await writeLiveState(runtimeDir, context.sessionKey, nextState);
1534
- const summaryPath = await writeLiveSummary(runtimeDir, context.sessionKey, renderLiveSummary({
1535
- sessionKey: context.sessionKey,
1536
- agent: context.agentName,
1537
- tool: nextState.tool_session,
1538
- status,
1539
- startedAt: nextState.started_at,
1540
- closedAt: now,
1541
- summary,
1542
- git,
1543
- recentEvents: nextState.last_events
1544
- }));
1545
-
1546
- await clearAgentSession(runtimeDir, context.agentName);
1547
-
1548
- if (!options.json) {
1549
- logger.log(t('live.session_closed', { agent: context.agentName, session: context.sessionKey, dbPath }));
1550
- }
1551
-
1552
- // Ambient Intelligence: sugere evolução se há learnings acumulados
1553
- if (!options.json && !options['no-health']) {
1554
- try {
1555
- const { getHealthDigest } = require('./health');
1556
- const items = await getHealthDigest(targetDir);
1557
- if (items && items.length > 0) {
1558
- logger.log('');
1559
- logger.log('AIOSON Health — itens após sessão:');
1560
- for (const item of items) {
1561
- logger.log(` ● ${item}`);
1562
- }
1563
- logger.log(' → aioson health . para detalhes e ações');
1564
- logger.log('');
1565
- }
1566
- } catch { /* não bloqueia o close */ }
1567
- }
1568
-
1569
- return {
1570
- ok: true,
1571
- targetDir,
1572
- dbPath,
1573
- agent: context.agentName,
1574
- taskKey: context.task.task_key,
1575
- runKey: context.run.run_key,
1576
- sessionKey: context.sessionKey,
1577
- status,
1578
- closed: true,
1579
- summaryPath,
1580
- git
1581
- };
1582
- } finally {
1583
- db.close();
1584
- }
1585
- }
1586
-
1587
- async function runLiveList({ args, options = {}, logger, t }) {
1588
- const targetDir = resolveTargetDir(args);
1589
- const { dbPath } = resolveRuntimePaths(targetDir);
1590
-
1591
- if (!(await runtimeStoreExists(targetDir))) {
1592
- throw new Error(t('runtime.store_missing', { path: dbPath }));
1593
- }
1594
-
1595
- const { db, runtimeDir } = await openRuntimeDb(targetDir, { mustExist: true });
1596
- db.close();
1597
- const states = await listLiveStates(runtimeDir);
1598
-
1599
- if (!options.json) {
1600
- if (states.length === 0) {
1601
- logger.log(t('live.list_empty'));
1602
- } else {
1603
- logger.log(t('live.list_title', { count: states.length }));
1604
- for (const state of states) {
1605
- logger.log(t('live.list_line', {
1606
- session: state.session_key || '-',
1607
- agent: state.active_agent || '-',
1608
- tool: state.tool_session || '-',
1609
- phase: state.phase || '-',
1610
- updatedAt: state.updated_at || state.started_at || '-'
1611
- }));
1612
- }
1613
- }
1614
- }
1615
-
1616
- return {
1617
- ok: true,
1618
- targetDir,
1619
- dbPath,
1620
- count: states.length,
1621
- sessions: states.map((state) => ({
1622
- sessionKey: state.session_key,
1623
- agent: state.active_agent,
1624
- tool: state.tool_session,
1625
- phase: state.phase,
1626
- title: state.title,
1627
- startedAt: state.started_at,
1628
- updatedAt: state.updated_at,
1629
- closedAt: state.closed_at
1630
- }))
1631
- };
1632
- }
1633
-
1634
- module.exports = {
1635
- runLiveStart,
1636
- runRuntimeEmit,
1637
- runLiveHandoff,
1638
- runLiveStatus,
1639
- runLiveClose,
1640
- runLiveList
1641
- };
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs/promises');
4
+ const path = require('node:path');
5
+ const { spawn } = require('node:child_process');
6
+ const {
7
+ resolveRuntimePaths,
8
+ openRuntimeDb,
9
+ runtimeStoreExists,
10
+ startTask,
11
+ updateTask,
12
+ startRun,
13
+ updateRun,
14
+ appendRunEvent,
15
+ readAgentSession,
16
+ writeAgentSession,
17
+ clearAgentSession
18
+ } = require('../runtime-store');
19
+ const { ensureDir, exists } = require('../utils');
20
+ const { SUPPORTED_PROMPT_TOOLS } = require('../prompt-tool');
21
+ const { isTmuxAvailable, launchTmuxSession, buildSessionName, hasSession, attachSession } = require('../lib/tmux-launcher');
22
+ const { resolveResumeArgs } = require('../lib/tool-capabilities');
23
+
24
+ const LIVE_EVENTS_LIMIT = 10;
25
+ const LIVE_MESSAGE_LIMIT = 500;
26
+
27
+ function resolveTargetDir(args) {
28
+ return path.resolve(process.cwd(), args[0] || '.');
29
+ }
30
+
31
+ function requireOption(options, key, t) {
32
+ const value = options[key];
33
+ if (value === undefined || value === null || String(value).trim() === '') {
34
+ throw new Error(t('runtime.option_required', { option: `--${key}` }));
35
+ }
36
+ return String(value).trim();
37
+ }
38
+
39
+ function normalizeAgentHandle(value) {
40
+ const text = String(value || '').trim();
41
+ if (!text) return '';
42
+ return text.startsWith('@') ? text : `@${text}`;
43
+ }
44
+
45
+ function makeDirectSessionKey(agentName) {
46
+ return `direct-session:${Date.now()}:${String(agentName || '').replace(/^@/, '')}`;
47
+ }
48
+
49
+ function parseWatchSeconds(value) {
50
+ if (value === undefined || value === null || value === false) return null;
51
+ if (value === true || value === '') return 2;
52
+
53
+ const parsed = Number(value);
54
+ if (!Number.isFinite(parsed) || parsed <= 0) return 2;
55
+ return parsed;
56
+ }
57
+
58
+ function sleep(ms) {
59
+ return new Promise((resolve) => setTimeout(resolve, ms));
60
+ }
61
+
62
+ async function withRuntimeDb(targetDir, t) {
63
+ const handle = await openRuntimeDb(targetDir, { mustExist: true });
64
+ if (!handle) {
65
+ throw new Error(t('runtime.store_missing', { path: resolveRuntimePaths(targetDir).dbPath }));
66
+ }
67
+ return handle;
68
+ }
69
+
70
+ // bug-found-005: session keys carry the format `direct-session:{ts}:{agent}`
71
+ // which contains colons. NTFS reserves `:` in filenames (it's the drive-letter
72
+ // separator and the alternate-data-stream syntax), so any `path.join(..., 'live',
73
+ // 'direct-session:1234:deyvin')` would have `mkdir` fail silently in the
74
+ // auxiliary-filesystem catches further down — leaving the dashboard's
75
+ // state.json/events.ndjson/summary.md never written on Windows.
76
+ //
77
+ // We sanitize only at the filesystem boundary: the session_key stored in
78
+ // SQLite, returned to callers, and printed by the CLI keeps its colons (it's
79
+ // a documented public identifier). Colons survive on POSIX too, but `__` is
80
+ // safe everywhere and lets a single code path handle both platforms.
81
+ function sessionKeyToDirName(sessionKey) {
82
+ return String(sessionKey).replace(/:/g, '__');
83
+ }
84
+
85
+ function resolveLivePaths(runtimeDir, sessionKey) {
86
+ const sessionDir = path.join(runtimeDir, 'live', sessionKeyToDirName(sessionKey));
87
+ return {
88
+ sessionDir,
89
+ statePath: path.join(sessionDir, 'state.json'),
90
+ eventsPath: path.join(sessionDir, 'events.ndjson'),
91
+ summaryPath: path.join(sessionDir, 'summary.md')
92
+ };
93
+ }
94
+
95
+ function truncateMessage(value, fallback = '') {
96
+ const text = String(value || fallback || '').trim();
97
+ if (!text) return '';
98
+ if (text.length <= LIVE_MESSAGE_LIMIT) return text;
99
+ return `${text.slice(0, LIVE_MESSAGE_LIMIT - 3).trimEnd()}...`;
100
+ }
101
+
102
+ function parseRefs(value) {
103
+ const text = String(value || '').trim();
104
+ if (!text) return [];
105
+ return Array.from(new Set(text.split(',').map((entry) => entry.trim()).filter(Boolean)));
106
+ }
107
+
108
+ function parseJsonOption(value) {
109
+ if (value === undefined || value === null || value === '') return null;
110
+ if (typeof value === 'object' && !Array.isArray(value)) return value;
111
+ try {
112
+ const parsed = JSON.parse(String(value));
113
+ return parsed && typeof parsed === 'object' ? parsed : { value: parsed };
114
+ } catch {
115
+ return { raw: String(value) };
116
+ }
117
+ }
118
+
119
+ // Combine `--resume` (mapped per-tool via TOOL_CAPS) with user-provided `--tool-args`.
120
+ // Resume args go FIRST so that codex `resume --last` (subcommand) lands at argv[1].
121
+ function buildLaunchArgs(options, tool) {
122
+ const resumeOpt = options.resume !== undefined ? options.resume : options.Resume;
123
+ const resumeArgs = resolveResumeArgs(tool, resumeOpt);
124
+ const userArgs = parseToolArgs(options['tool-args'] || options.toolArgs);
125
+ return [...resumeArgs, ...userArgs];
126
+ }
127
+
128
+ function parseToolArgs(value) {
129
+ if (value === undefined || value === null || value === '') return [];
130
+ if (Array.isArray(value)) return value.map((entry) => String(entry));
131
+ const text = String(value).trim();
132
+ if (!text) return [];
133
+
134
+ if (text.startsWith('[')) {
135
+ try {
136
+ const parsed = JSON.parse(text);
137
+ if (Array.isArray(parsed)) {
138
+ return parsed.map((entry) => String(entry));
139
+ }
140
+ } catch {
141
+ // fallback to whitespace split below
142
+ }
143
+ }
144
+
145
+ return text.split(/\s+/).filter(Boolean);
146
+ }
147
+
148
+ function normalizeLiveTool(value, t) {
149
+ const tool = String(value || '').trim().toLowerCase();
150
+ if (SUPPORTED_PROMPT_TOOLS.has(tool)) {
151
+ return tool;
152
+ }
153
+ const supported = Array.from(SUPPORTED_PROMPT_TOOLS).join(', ');
154
+ throw new Error(t ? t('live.unsupported_tool', { tool: value, supported }) : `Unsupported live tool: ${value}. Supported: ${supported}`);
155
+ }
156
+
157
+
158
+ function normalizePlanStepId(value) {
159
+ return String(value || '').trim().replace(/\s+/g, ' ');
160
+ }
161
+
162
+ function collectPlanSteps(markdown) {
163
+ const steps = [];
164
+ const seen = new Set();
165
+
166
+ function pushStep(id, title) {
167
+ const normalizedId = normalizePlanStepId(id);
168
+ const normalizedTitle = String(title || '').trim();
169
+ if (!normalizedId || !normalizedTitle) return;
170
+ const key = normalizedId.toLowerCase();
171
+ if (seen.has(key)) return;
172
+ seen.add(key);
173
+ steps.push({ id: normalizedId, title: normalizedTitle, done: false });
174
+ }
175
+
176
+ const blockPattern = /<!--\s*aioson:steps([\s\S]*?)-->/gi;
177
+ let blockMatch = blockPattern.exec(markdown);
178
+ while (blockMatch) {
179
+ const lines = String(blockMatch[1] || '').split(/\r?\n/);
180
+ for (const line of lines) {
181
+ const trimmed = line.trim();
182
+ if (!trimmed) continue;
183
+ const match = trimmed.match(/^([^:]+):\s*(.+)$/);
184
+ if (match) {
185
+ pushStep(match[1], match[2]);
186
+ }
187
+ }
188
+ blockMatch = blockPattern.exec(markdown);
189
+ }
190
+
191
+ const headingPattern = /^#{3,6}\s+((?:[A-Z]{2,}(?:-[A-Z0-9]+)*-\d+(?:\.\d+)?)|(?:Fase\s+\d+(?:\.\d+)?))\s*[-:]\s+(.+)$/gim;
192
+ let headingMatch = headingPattern.exec(markdown);
193
+ while (headingMatch) {
194
+ pushStep(headingMatch[1], headingMatch[2]);
195
+ headingMatch = headingPattern.exec(markdown);
196
+ }
197
+
198
+ return steps;
199
+ }
200
+
201
+ async function loadPlanReference(targetDir, planRef) {
202
+ if (!planRef) {
203
+ return { planRef: null, planPath: null, planSteps: [] };
204
+ }
205
+
206
+ const planPath = path.isAbsolute(planRef) ? planRef : path.resolve(targetDir, planRef);
207
+ let markdown = '';
208
+ try {
209
+ markdown = await fs.readFile(planPath, 'utf8');
210
+ } catch {
211
+ throw new Error(`Plan file not found: ${planRef}`); // technical message, i18n at caller level
212
+ }
213
+
214
+ return {
215
+ planRef,
216
+ planPath,
217
+ planSteps: collectPlanSteps(markdown)
218
+ };
219
+ }
220
+
221
+
222
+ async function resolveExecutablePath(command) {
223
+ const binary = String(command || '').trim();
224
+ if (!binary) return null;
225
+
226
+ if (path.isAbsolute(binary) || binary.includes(path.sep)) {
227
+ const absolutePath = path.isAbsolute(binary) ? binary : path.resolve(binary);
228
+ return (await exists(absolutePath)) ? absolutePath : null;
229
+ }
230
+
231
+ const pathEntries = String(process.env.PATH || '')
232
+ .split(path.delimiter)
233
+ .map((entry) => entry.trim())
234
+ .filter(Boolean);
235
+
236
+ const extensions = process.platform === 'win32'
237
+ ? Array.from(new Set(['', ...String(process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM').split(';').map((entry) => entry.toLowerCase())]))
238
+ : [''];
239
+
240
+ for (const dir of pathEntries) {
241
+ for (const ext of extensions) {
242
+ const candidate = process.platform === 'win32' && ext && !binary.toLowerCase().endsWith(ext)
243
+ ? path.join(dir, `${binary}${ext}`)
244
+ : path.join(dir, binary);
245
+ if (await exists(candidate)) {
246
+ return candidate;
247
+ }
248
+ }
249
+ }
250
+
251
+ return null;
252
+ }
253
+
254
+ function detectProcessState(pid) {
255
+ if (!pid) return 'not_tracked';
256
+ try {
257
+ process.kill(Number(pid), 0);
258
+ return 'alive';
259
+ } catch (error) {
260
+ if (error && error.code === 'ESRCH') {
261
+ return 'dead';
262
+ }
263
+ return 'unknown';
264
+ }
265
+ }
266
+
267
+ function normalizeLiveStats(stats, fallback = {}) {
268
+ return {
269
+ tasks_completed: Number(stats?.tasks_completed || 0),
270
+ events_total: Number(stats?.events_total || 0),
271
+ plan_steps_done: Number(stats?.plan_steps_done ?? fallback.plan_steps_done ?? 0),
272
+ plan_steps_total: Number(stats?.plan_steps_total ?? fallback.plan_steps_total ?? 0),
273
+ events_by_type: stats?.events_by_type && typeof stats.events_by_type === 'object'
274
+ ? { ...stats.events_by_type }
275
+ : {}
276
+ };
277
+ }
278
+
279
+ function parseTaskMeta(task) {
280
+ if (!task || !task.meta_json) return {};
281
+ try {
282
+ const parsed = JSON.parse(task.meta_json);
283
+ return parsed && typeof parsed === 'object' ? parsed : {};
284
+ } catch {
285
+ return {};
286
+ }
287
+ }
288
+
289
+ function getPlanStats(meta) {
290
+ const steps = Array.isArray(meta?.plan_steps) ? meta.plan_steps : [];
291
+ const done = steps.filter((step) => step && step.done).length;
292
+ return {
293
+ plan_steps_done: done,
294
+ plan_steps_total: steps.length
295
+ };
296
+ }
297
+
298
+ async function readJsonIfExists(filePath) {
299
+ try {
300
+ const raw = await fs.readFile(filePath, 'utf8');
301
+ return JSON.parse(raw);
302
+ } catch {
303
+ return null;
304
+ }
305
+ }
306
+
307
+ async function readLiveState(runtimeDir, sessionKey) {
308
+ if (!sessionKey) return null;
309
+ return readJsonIfExists(resolveLivePaths(runtimeDir, sessionKey).statePath);
310
+ }
311
+
312
+ async function writeLiveState(runtimeDir, sessionKey, state) {
313
+ try {
314
+ const { statePath } = resolveLivePaths(runtimeDir, sessionKey);
315
+ await ensureDir(path.dirname(statePath));
316
+ await fs.writeFile(statePath, JSON.stringify(state, null, 2), 'utf8');
317
+ } catch {
318
+ // filesystem is auxiliary — SQLite is source of truth
319
+ }
320
+ }
321
+
322
+ async function appendLiveEvent(runtimeDir, sessionKey, record) {
323
+ try {
324
+ const { eventsPath } = resolveLivePaths(runtimeDir, sessionKey);
325
+ await ensureDir(path.dirname(eventsPath));
326
+ await fs.appendFile(eventsPath, `${JSON.stringify(record)}\n`, 'utf8');
327
+ } catch {
328
+ // filesystem is auxiliary — SQLite is source of truth
329
+ }
330
+ }
331
+
332
+ async function writeLiveSummary(runtimeDir, sessionKey, markdown) {
333
+ try {
334
+ const { summaryPath } = resolveLivePaths(runtimeDir, sessionKey);
335
+ await ensureDir(path.dirname(summaryPath));
336
+ await fs.writeFile(summaryPath, markdown, 'utf8');
337
+ return summaryPath;
338
+ } catch {
339
+ return null;
340
+ }
341
+ }
342
+
343
+ async function listLiveStates(runtimeDir) {
344
+ const liveRoot = path.join(runtimeDir, 'live');
345
+ if (!(await exists(liveRoot))) {
346
+ return [];
347
+ }
348
+
349
+ const entries = await fs.readdir(liveRoot, { withFileTypes: true });
350
+ const states = [];
351
+ for (const entry of entries) {
352
+ if (!entry.isDirectory()) continue;
353
+ const state = await readJsonIfExists(path.join(liveRoot, entry.name, 'state.json'));
354
+ if (state) {
355
+ states.push(state);
356
+ }
357
+ }
358
+
359
+ states.sort((left, right) => {
360
+ const leftStamp = Date.parse(left.updated_at || left.closed_at || left.started_at || 0);
361
+ const rightStamp = Date.parse(right.updated_at || right.closed_at || right.started_at || 0);
362
+ return rightStamp - leftStamp;
363
+ });
364
+
365
+ return states;
366
+ }
367
+
368
+ function createLiveState(targetDir, run, task, options = {}) {
369
+ const taskMeta = parseTaskMeta(task);
370
+ const planStats = getPlanStats(taskMeta);
371
+ return {
372
+ session_key: run.session_key || task?.session_key || options.sessionKey || null,
373
+ session_task_key: task?.task_key || options.taskKey || null,
374
+ tool_session: options.tool || taskMeta.tool_session || null,
375
+ active_agent: options.activeAgent || run.agent_name || null,
376
+ plan_ref: options.planRef ?? taskMeta.plan_ref ?? null,
377
+ phase: options.phase || (run.status === 'running' || run.status === 'queued' ? 'active' : 'closed'),
378
+ title: options.title || task?.title || run.title || null,
379
+ path: options.projectPath || targetDir,
380
+ child_pid: options.childPid ?? taskMeta.child_pid ?? null,
381
+ started_at: options.startedAt || run.started_at || task?.created_at || null,
382
+ updated_at: options.updatedAt || run.updated_at || task?.updated_at || null,
383
+ closed_at: options.closedAt || (run.status === 'completed' || run.status === 'failed' ? run.finished_at || task?.finished_at || null : null),
384
+ current_task: options.currentTask ?? null,
385
+ current_run_key: options.currentRunKey || run.run_key,
386
+ stats: normalizeLiveStats(options.stats, planStats),
387
+ last_events: Array.isArray(options.lastEvents) ? options.lastEvents.slice(-LIVE_EVENTS_LIMIT) : []
388
+ };
389
+ }
390
+
391
+ function selectLiveRunByKey(db, runKey) {
392
+ if (!runKey) return null;
393
+ return db.prepare(`
394
+ SELECT
395
+ run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
396
+ title, status, summary, output_path, started_at, updated_at, finished_at
397
+ FROM agent_runs
398
+ WHERE run_key = ? AND source = 'live'
399
+ LIMIT 1
400
+ `).get(String(runKey));
401
+ }
402
+
403
+ function selectLatestLiveRun(db, options = {}) {
404
+ if (options.sessionKey) {
405
+ return db.prepare(`
406
+ SELECT
407
+ run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
408
+ title, status, summary, output_path, started_at, updated_at, finished_at
409
+ FROM agent_runs
410
+ WHERE source = 'live' AND session_key = ?
411
+ ORDER BY updated_at DESC, started_at DESC
412
+ LIMIT 1
413
+ `).get(String(options.sessionKey));
414
+ }
415
+
416
+ if (options.agentName) {
417
+ return db.prepare(`
418
+ SELECT
419
+ run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
420
+ title, status, summary, output_path, started_at, updated_at, finished_at
421
+ FROM agent_runs
422
+ WHERE source = 'live' AND agent_name = ?
423
+ ORDER BY updated_at DESC, started_at DESC
424
+ LIMIT 1
425
+ `).get(String(options.agentName));
426
+ }
427
+
428
+ return db.prepare(`
429
+ SELECT
430
+ run_key, task_key, agent_name, agent_kind, squad_slug, session_key, source,
431
+ title, status, summary, output_path, started_at, updated_at, finished_at
432
+ FROM agent_runs
433
+ WHERE source = 'live'
434
+ ORDER BY updated_at DESC, started_at DESC
435
+ LIMIT 1
436
+ `).get();
437
+ }
438
+
439
+ function selectTaskByKey(db, taskKey) {
440
+ if (!taskKey) return null;
441
+ return db.prepare(`
442
+ SELECT
443
+ task_key, squad_slug, session_key, task_kind, parent_task_key,
444
+ title, goal, meta_json, status, created_by, created_at, updated_at, finished_at
445
+ FROM tasks
446
+ WHERE task_key = ?
447
+ LIMIT 1
448
+ `).get(String(taskKey));
449
+ }
450
+
451
+ function mapRecentDbEvent(event) {
452
+ return {
453
+ ts: event.created_at,
454
+ type: event.event_type,
455
+ summary: event.message || '-'
456
+ };
457
+ }
458
+
459
+ async function resolveLiveContext(targetDir, db, runtimeDir, options = {}) {
460
+ let agentName = options.agentName ? normalizeAgentHandle(options.agentName) : null;
461
+ let sessionRef = agentName ? await readAgentSession(runtimeDir, agentName) : null;
462
+ let sessionKey = options.sessionKey ? String(options.sessionKey).trim() : null;
463
+ let state = null;
464
+
465
+ if (!sessionKey && sessionRef?.sessionKey) {
466
+ sessionKey = String(sessionRef.sessionKey).trim();
467
+ }
468
+
469
+ if (!sessionKey && !agentName) {
470
+ const liveStates = await listLiveStates(runtimeDir);
471
+ if (liveStates.length > 0) {
472
+ state = liveStates[0];
473
+ sessionKey = state.session_key || null;
474
+ if (state.active_agent) {
475
+ agentName = normalizeAgentHandle(state.active_agent);
476
+ }
477
+ }
478
+ }
479
+
480
+ if (!state && sessionKey) {
481
+ state = await readLiveState(runtimeDir, sessionKey);
482
+ }
483
+
484
+ if (!agentName && state?.active_agent) {
485
+ agentName = normalizeAgentHandle(state.active_agent);
486
+ }
487
+
488
+ let run = sessionRef?.runKey ? selectLiveRunByKey(db, sessionRef.runKey) : null;
489
+ if (!run && state?.current_run_key) {
490
+ run = selectLiveRunByKey(db, state.current_run_key);
491
+ }
492
+ if (!run && sessionKey) {
493
+ run = selectLatestLiveRun(db, { sessionKey });
494
+ }
495
+ if (!run && agentName) {
496
+ run = selectLatestLiveRun(db, { agentName });
497
+ }
498
+ if (!run && !agentName && !sessionKey) {
499
+ run = selectLatestLiveRun(db);
500
+ }
501
+
502
+ let task = null;
503
+ if (run?.task_key) {
504
+ task = selectTaskByKey(db, run.task_key);
505
+ }
506
+ if (!task && sessionRef?.taskKey) {
507
+ task = selectTaskByKey(db, sessionRef.taskKey);
508
+ }
509
+ if (!task && state?.session_task_key) {
510
+ task = selectTaskByKey(db, state.session_task_key);
511
+ }
512
+
513
+ if (!sessionKey) {
514
+ sessionKey = run?.session_key || task?.session_key || state?.session_key || sessionRef?.sessionKey || null;
515
+ }
516
+
517
+ if (!state && sessionKey) {
518
+ state = await readLiveState(runtimeDir, sessionKey);
519
+ }
520
+
521
+ if (run && !state) {
522
+ state = createLiveState(targetDir, run, task, {
523
+ sessionKey,
524
+ activeAgent: agentName || run.agent_name,
525
+ projectPath: targetDir
526
+ });
527
+ }
528
+
529
+ const recentEvents = run
530
+ ? db.prepare(`
531
+ SELECT event_type, message, created_at
532
+ FROM execution_events
533
+ WHERE run_key = ?
534
+ ORDER BY created_at DESC, id DESC
535
+ LIMIT ?
536
+ `).all(run.run_key, Math.max(1, Math.min(Number(options.limit) || 8, 20))).reverse().map(mapRecentDbEvent)
537
+ : [];
538
+
539
+ const processState = detectProcessState(state?.child_pid);
540
+ const phase = state?.phase || (run && (run.status === 'running' || run.status === 'queued') ? 'active' : run ? 'closed' : 'idle');
541
+ const open = phase === 'active' && Boolean(run && (run.status === 'running' || run.status === 'queued'));
542
+
543
+ return {
544
+ agentName: agentName || state?.active_agent || run?.agent_name || null,
545
+ sessionRef,
546
+ sessionKey,
547
+ run,
548
+ task,
549
+ state,
550
+ recentEvents,
551
+ processState,
552
+ phase,
553
+ open,
554
+ paths: sessionKey ? resolveLivePaths(runtimeDir, sessionKey) : null
555
+ };
556
+ }
557
+
558
+ async function requireActiveLiveContext(targetDir, agentName, t, options = {}) {
559
+ const { db, dbPath, runtimeDir } = await withRuntimeDb(targetDir, t);
560
+ const context = await resolveLiveContext(targetDir, db, runtimeDir, {
561
+ agentName,
562
+ limit: options.limit
563
+ });
564
+
565
+ if (!context.run || context.run.source !== 'live' || !context.sessionKey || !context.task) {
566
+ db.close();
567
+ throw new Error(t('live.no_active_session', { agent: normalizeAgentHandle(agentName) }));
568
+ }
569
+
570
+ if (context.phase !== 'active') {
571
+ db.close();
572
+ throw new Error(t('live.session_not_active', { agent: normalizeAgentHandle(agentName) }));
573
+ }
574
+
575
+ return { db, dbPath, runtimeDir, context };
576
+ }
577
+
578
+ function applyEventToState(state, event, updates = {}) {
579
+ const next = {
580
+ ...state,
581
+ updated_at: event.ts,
582
+ stats: normalizeLiveStats(state?.stats)
583
+ };
584
+
585
+ next.last_events = [...(Array.isArray(state?.last_events) ? state.last_events : []), {
586
+ ts: event.ts,
587
+ type: event.type,
588
+ summary: event.summary
589
+ }].slice(-LIVE_EVENTS_LIMIT);
590
+ next.stats.events_total += 1;
591
+ if (event.type) {
592
+ next.stats.events_by_type[event.type] = (next.stats.events_by_type[event.type] || 0) + 1;
593
+ }
594
+
595
+ if (event.type === 'task_completed') {
596
+ next.stats.tasks_completed += 1;
597
+ }
598
+
599
+ if (Object.prototype.hasOwnProperty.call(updates, 'currentTask')) {
600
+ next.current_task = updates.currentTask;
601
+ }
602
+ if (Object.prototype.hasOwnProperty.call(updates, 'phase')) {
603
+ next.phase = updates.phase;
604
+ }
605
+ if (Object.prototype.hasOwnProperty.call(updates, 'closedAt')) {
606
+ next.closed_at = updates.closedAt;
607
+ }
608
+ if (Object.prototype.hasOwnProperty.call(updates, 'activeAgent')) {
609
+ next.active_agent = updates.activeAgent;
610
+ }
611
+ if (Object.prototype.hasOwnProperty.call(updates, 'currentRunKey')) {
612
+ next.current_run_key = updates.currentRunKey;
613
+ }
614
+ if (Object.prototype.hasOwnProperty.call(updates, 'childPid')) {
615
+ next.child_pid = updates.childPid;
616
+ }
617
+ if (updates.planStats) {
618
+ next.stats.plan_steps_done = Number(updates.planStats.plan_steps_done || 0);
619
+ next.stats.plan_steps_total = Number(updates.planStats.plan_steps_total || 0);
620
+ }
621
+
622
+ return next;
623
+ }
624
+
625
+ function createLiveEventRecord(context, options = {}) {
626
+ return {
627
+ ts: options.ts,
628
+ type: options.type,
629
+ summary: options.summary,
630
+ agent: context.agentName,
631
+ task_key: options.taskKey || context.task?.task_key || null,
632
+ run_key: context.run?.run_key || null,
633
+ session_key: context.sessionKey,
634
+ refs: options.refs || [],
635
+ plan_step: options.planStep || null,
636
+ status: options.status || null,
637
+ meta: options.meta || null
638
+ };
639
+ }
640
+
641
+ function waitForChild(child) {
642
+ return new Promise((resolve, reject) => {
643
+ child.once('error', reject);
644
+ child.once('close', (code, signal) => {
645
+ resolve({ code: Number(code || 0), signal: signal || null });
646
+ });
647
+ });
648
+ }
649
+
650
+ async function runLocalProcess(command, args, options = {}) {
651
+ return new Promise((resolve) => {
652
+ const child = spawn(command, args.map((entry) => String(entry)), {
653
+ cwd: options.cwd || process.cwd(),
654
+ env: { ...process.env, ...(options.env || {}) },
655
+ stdio: ['ignore', 'pipe', 'pipe']
656
+ });
657
+
658
+ let stdout = '';
659
+ let stderr = '';
660
+ child.stdout.on('data', (chunk) => {
661
+ stdout += String(chunk);
662
+ });
663
+ child.stderr.on('data', (chunk) => {
664
+ stderr += String(chunk);
665
+ });
666
+ child.on('error', () => {
667
+ resolve({ code: 1, stdout, stderr });
668
+ });
669
+ child.on('close', (code) => {
670
+ resolve({ code: Number(code || 0), stdout, stderr });
671
+ });
672
+ });
673
+ }
674
+
675
+ async function collectGitSnapshot(targetDir) {
676
+ if (!(await exists(path.join(targetDir, '.git')))) {
677
+ return null;
678
+ }
679
+
680
+ const [branch, commit, diffStat, status] = await Promise.all([
681
+ runLocalProcess('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: targetDir }),
682
+ runLocalProcess('git', ['rev-parse', '--short', 'HEAD'], { cwd: targetDir }),
683
+ runLocalProcess('git', ['diff', '--stat'], { cwd: targetDir }),
684
+ runLocalProcess('git', ['status', '--short'], { cwd: targetDir })
685
+ ]);
686
+
687
+ if (branch.code !== 0 && commit.code !== 0 && diffStat.code !== 0 && status.code !== 0) {
688
+ return null;
689
+ }
690
+
691
+ return {
692
+ branch: branch.code === 0 ? branch.stdout.trim() : null,
693
+ commit: commit.code === 0 ? commit.stdout.trim() : null,
694
+ diff_stat: diffStat.code === 0 ? diffStat.stdout.trim() : null,
695
+ changed_files: status.code === 0
696
+ ? status.stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean).map((line) => line.slice(3).trim() || line)
697
+ : []
698
+ };
699
+ }
700
+
701
+ function formatDuration(startedAt, closedAt) {
702
+ if (!startedAt || !closedAt) return null;
703
+ const ms = Date.parse(closedAt) - Date.parse(startedAt);
704
+ if (!Number.isFinite(ms) || ms < 0) return null;
705
+ const seconds = Math.floor(ms / 1000);
706
+ const hours = Math.floor(seconds / 3600);
707
+ const minutes = Math.floor((seconds % 3600) / 60);
708
+ const secs = seconds % 60;
709
+ if (hours > 0) return `${hours}h ${minutes}m`;
710
+ if (minutes > 0) return `${minutes}m ${secs}s`;
711
+ return `${secs}s`;
712
+ }
713
+
714
+ function renderLiveSummary(snapshot) {
715
+ const duration = formatDuration(snapshot.startedAt, snapshot.closedAt);
716
+ const lines = [
717
+ '# Live Session Summary',
718
+ '',
719
+ `- Session: ${snapshot.sessionKey}`,
720
+ `- Agent: ${snapshot.agent}`,
721
+ `- Tool: ${snapshot.tool || 'unknown'}`,
722
+ `- Status: ${snapshot.status}`,
723
+ `- Started: ${snapshot.startedAt || 'unknown'}`,
724
+ `- Closed: ${snapshot.closedAt || 'unknown'}`,
725
+ ...(duration ? [`- Duration: ${duration}`] : []),
726
+ `- Summary: ${snapshot.summary || 'n/a'}`
727
+ ];
728
+
729
+ if (snapshot.git) {
730
+ lines.push('');
731
+ lines.push('## Git');
732
+ lines.push(`- Branch: ${snapshot.git.branch || 'unknown'}`);
733
+ lines.push(`- Commit: ${snapshot.git.commit || 'unknown'}`);
734
+ if (snapshot.git.diff_stat) {
735
+ lines.push('');
736
+ lines.push('```text');
737
+ lines.push(snapshot.git.diff_stat);
738
+ lines.push('```');
739
+ }
740
+ if (snapshot.git.changed_files.length > 0) {
741
+ lines.push('');
742
+ lines.push('## Changed Files');
743
+ for (const file of snapshot.git.changed_files) {
744
+ lines.push(`- ${file}`);
745
+ }
746
+ }
747
+ }
748
+
749
+ if (Array.isArray(snapshot.recentEvents) && snapshot.recentEvents.length > 0) {
750
+ lines.push('');
751
+ lines.push('## Recent Events');
752
+ for (const event of snapshot.recentEvents) {
753
+ lines.push(`- ${event.ts} | ${event.type} | ${event.summary}`);
754
+ }
755
+ }
756
+
757
+ lines.push('');
758
+ return lines.join('\n');
759
+ }
760
+
761
+ // ANSI color helpers (no external deps)
762
+ const ANSI = {
763
+ reset: '\x1b[0m',
764
+ bold: '\x1b[1m',
765
+ dim: '\x1b[2m',
766
+ green: '\x1b[32m',
767
+ yellow: '\x1b[33m',
768
+ red: '\x1b[31m',
769
+ cyan: '\x1b[36m',
770
+ magenta: '\x1b[35m',
771
+ blue: '\x1b[34m',
772
+ gray: '\x1b[90m'
773
+ };
774
+
775
+ function colorForContext(pct) {
776
+ if (pct >= 90) return ANSI.red;
777
+ if (pct >= 70) return ANSI.yellow;
778
+ return ANSI.green;
779
+ }
780
+
781
+ function colorForPhase(phase) {
782
+ if (phase === 'active') return ANSI.green;
783
+ if (phase === 'closed') return ANSI.gray;
784
+ return ANSI.yellow;
785
+ }
786
+
787
+ function colorForProcess(state) {
788
+ if (state === 'alive') return ANSI.green;
789
+ if (state === 'dead') return ANSI.red;
790
+ return ANSI.gray;
791
+ }
792
+
793
+ function formatDurationCompact(startedAt) {
794
+ if (!startedAt) return '';
795
+ const ms = Date.now() - Date.parse(startedAt);
796
+ if (!Number.isFinite(ms) || ms < 0) return '';
797
+ const m = Math.floor(ms / 60000);
798
+ const s = Math.floor((ms % 60000) / 1000);
799
+ if (m > 60) {
800
+ const h = Math.floor(m / 60);
801
+ return `${h}h${m % 60}m`;
802
+ }
803
+ return `${m}m${s}s`;
804
+ }
805
+
806
+ /**
807
+ * Print a one-line compact status bar with ANSI colors.
808
+ * Designed for small tmux panes (~4 lines).
809
+ */
810
+ function printCompactStatus(snapshot, logger) {
811
+ const agent = snapshot.agent || '-';
812
+ const tool = snapshot.tool || '-';
813
+ const phase = snapshot.phase || 'idle';
814
+ const proc = snapshot.processState || 'not_tracked';
815
+ const pid = snapshot.pid || null;
816
+
817
+ // Context percentage if available
818
+ let ctxStr = '';
819
+ if (snapshot.run && snapshot.run.context_pct != null) {
820
+ const pct = Number(snapshot.run.context_pct) || 0;
821
+ ctxStr = `${colorForContext(pct)}ctx:${pct}%${ANSI.reset}`;
822
+ }
823
+
824
+ // Token / cost if available
825
+ let costStr = '';
826
+ if (snapshot.stats && snapshot.stats.tokens_total) {
827
+ const tokens = snapshot.stats.tokens_total;
828
+ const cost = snapshot.stats.cost_usd;
829
+ costStr = `${ANSI.cyan}${tokens >= 1000 ? (tokens / 1000).toFixed(1) + 'k' : tokens}tk${ANSI.reset}`;
830
+ if (cost != null) {
831
+ costStr += `${ANSI.gray}/${ANSI.reset}${ANSI.cyan}$${cost.toFixed(3)}${ANSI.reset}`;
832
+ }
833
+ }
834
+
835
+ // Plan progress
836
+ let planStr = '';
837
+ const planDone = snapshot.stats?.plan_steps_done ?? 0;
838
+ const planTotal = snapshot.stats?.plan_steps_total ?? 0;
839
+ if (planTotal > 0) {
840
+ planStr = `${ANSI.magenta}plan:${planDone}/${planTotal}${ANSI.reset}`;
841
+ }
842
+
843
+ // Duration
844
+ const dur = formatDurationCompact(snapshot.startedAt);
845
+ const durStr = dur ? `${ANSI.blue}${dur}${ANSI.reset}` : '';
846
+
847
+ // Recent event
848
+ let eventStr = '';
849
+ if (snapshot.recentEvents && snapshot.recentEvents.length > 0) {
850
+ const ev = snapshot.recentEvents[snapshot.recentEvents.length - 1];
851
+ eventStr = `${ANSI.gray}${ev.type}${ANSI.reset}`;
852
+ if (ev.summary) {
853
+ const short = String(ev.summary).slice(0, 35);
854
+ eventStr += `:${ANSI.gray}${short}${ANSI.reset}`;
855
+ }
856
+ }
857
+
858
+ // Warning
859
+ let warnStr = '';
860
+ if (snapshot.warning) {
861
+ warnStr = `${ANSI.red}⚠ ${snapshot.warning}${ANSI.reset}`;
862
+ }
863
+
864
+ // Build line 1
865
+ const parts = [
866
+ `${colorForPhase(phase)}●${ANSI.reset}`,
867
+ `${ANSI.bold}${agent}${ANSI.reset}`,
868
+ `|`,
869
+ `${ANSI.blue}${tool}${ANSI.reset}`,
870
+ `|`,
871
+ `${colorForProcess(proc)}${proc}${ANSI.reset}`,
872
+ pid ? `${ANSI.gray}(pid:${pid})${ANSI.reset}` : '',
873
+ ctxStr,
874
+ costStr,
875
+ planStr,
876
+ durStr,
877
+ eventStr,
878
+ warnStr
879
+ ].filter(Boolean);
880
+
881
+ logger.log(parts.join(' '));
882
+ }
883
+
884
+ /**
885
+ * Print two plain-text lines optimized for tmux status-bar.
886
+ * No ANSI colors tmux handles its own styling.
887
+ * Designed for a 2-line pane.
888
+ */
889
+ function renderMiniBar(pct, width = 10, usedLabel = '', totalLabel = '') {
890
+ const filled = Math.round((pct / 100) * width);
891
+ const empty = width - filled;
892
+ const bar = '█'.repeat(filled) + '░'.repeat(empty);
893
+ const color = pct > 80 ? '\x1b[31m' : pct > 50 ? '\x1b[33m' : '\x1b[32m';
894
+ const reset = '\x1b[0m';
895
+ const abs = usedLabel && totalLabel ? ` ${usedLabel}/${totalLabel}` : '';
896
+ return `${color}[${bar}]${reset}${abs} ${pct}%`;
897
+ }
898
+
899
+ function formatProjectPath(targetDir) {
900
+ if (!targetDir) return '-';
901
+ const home = process.env.HOME || process.env.USERPROFILE || '';
902
+ let path = String(targetDir).replace(/\\/g, '/');
903
+ if (home && path.startsWith(home.replace(/\\/g, '/'))) {
904
+ path = '~' + path.slice(home.length);
905
+ }
906
+ if (path.length <= 28) return path;
907
+ // Too long: keep last 2 segments with ellipsis
908
+ const segments = path.split('/').filter(Boolean);
909
+ if (segments.length <= 2) return path;
910
+ const lastTwo = segments.slice(-2).join('/');
911
+ return `~/.../${lastTwo}`;
912
+ }
913
+
914
+ function printTmuxBar(snapshot) {
915
+ const agent = snapshot.agent || '-';
916
+ const tool = snapshot.tool || '-';
917
+ const phase = snapshot.phase || 'idle';
918
+ const projectDir = formatProjectPath(snapshot.targetDir);
919
+ const dur = formatDurationCompact(snapshot.startedAt);
920
+
921
+ // Build core info
922
+ const parts = [];
923
+ parts.push(`\x1b[1;36m${projectDir}\x1b[0m`);
924
+ parts.push(`\x1b[1;35m${agent}\x1b[0m`);
925
+ parts.push(`\x1b[90m${tool}\x1b[0m`);
926
+
927
+ if (phase === 'active') {
928
+ parts.push(`\x1b[32m●\x1b[0m`);
929
+ } else if (phase === 'closed') {
930
+ parts.push(`\x1b[31m○\x1b[0m`);
931
+ } else {
932
+ parts.push(`\x1b[33m${phase}\x1b[0m`);
933
+ }
934
+
935
+ if (dur) {
936
+ parts.push(dur);
937
+ }
938
+
939
+ // Plan progress
940
+ const planDone = snapshot.stats?.plan_steps_done ?? 0;
941
+ const planTotal = snapshot.stats?.plan_steps_total ?? 0;
942
+ if (planTotal > 0) {
943
+ parts.push(`step ${planDone}/${planTotal}`);
944
+ }
945
+
946
+ // Context bar with absolute numbers
947
+ if (snapshot.run && snapshot.run.context_pct != null) {
948
+ const pct = Number(snapshot.run.context_pct) || 0;
949
+ parts.push(`ctx ${renderMiniBar(pct)}`);
950
+ } else if (snapshot.contextEstimated) {
951
+ const est = snapshot.contextEstimated;
952
+ const pct = est.pct ?? 0;
953
+ const used = est.estimatedTokens >= 1000 ? (est.estimatedTokens / 1000).toFixed(1) + 'k' : String(est.estimatedTokens);
954
+ const total = est.windowSize >= 1000 ? (est.windowSize / 1000).toFixed(1) + 'k' : String(est.windowSize);
955
+ parts.push(`ctx ${renderMiniBar(pct, 10, used, total)}`);
956
+ }
957
+
958
+ // Cost
959
+ if (snapshot.stats && snapshot.stats.tokens_total) {
960
+ const tokens = snapshot.stats.tokens_total;
961
+ const cost = snapshot.stats.cost_usd;
962
+ const tk = tokens >= 1000 ? (tokens / 1000).toFixed(1) + 'k' : tokens;
963
+ if (cost != null) {
964
+ parts.push(`$${cost.toFixed(2)} (${tk}tk)`);
965
+ } else {
966
+ parts.push(`${tk}tk`);
967
+ }
968
+ }
969
+
970
+ // Recent useful event (skip session_started boilerplate)
971
+ let lastEvent = null;
972
+ if (snapshot.recentEvents && snapshot.recentEvents.length > 0) {
973
+ for (let i = snapshot.recentEvents.length - 1; i >= 0; i--) {
974
+ const ev = snapshot.recentEvents[i];
975
+ const type = String(ev.type || '');
976
+ if (type !== 'session_started' && type !== 'session_closed') {
977
+ lastEvent = ev;
978
+ break;
979
+ }
980
+ }
981
+ }
982
+ if (lastEvent) {
983
+ const short = String(lastEvent.summary || lastEvent.type || '').slice(0, 40);
984
+ if (short) {
985
+ parts.push(`\x1b[90m${short}\x1b[0m`);
986
+ }
987
+ }
988
+
989
+ // Warning
990
+ if (snapshot.warning) {
991
+ parts.push(`\x1b[1;31m! ${snapshot.warning}\x1b[0m`);
992
+ }
993
+
994
+ // When running inside the tmux updater, omit newline so the line can be overwritten.
995
+ // When called directly by a user, append newline for clean shell prompt.
996
+ const suffix = process.env.AIOSON_TMUX_BAR ? '' : '\n';
997
+ process.stdout.write(parts.join(' │ ') + suffix);
998
+ }
999
+
1000
+ function printLiveStatusSnapshot(snapshot, logger) {
1001
+ logger.log(`Live session: ${snapshot.sessionKey || 'none'}`);
1002
+ logger.log(`Phase: ${snapshot.phase}`);
1003
+ logger.log(`Tool: ${snapshot.tool || '-'}`);
1004
+ logger.log(`Active agent: ${snapshot.agent || '-'}`);
1005
+ if (snapshot.stats && Number(snapshot.stats.plan_steps_total || 0) > 0) {
1006
+ logger.log(`Plan: ${snapshot.stats.plan_steps_done || 0}/${snapshot.stats.plan_steps_total || 0}`);
1007
+ }
1008
+ logger.log(`Process: ${snapshot.processState}${snapshot.pid ? ` (pid ${snapshot.pid})` : ''}`);
1009
+
1010
+ if (snapshot.task) {
1011
+ logger.log(`Task: ${snapshot.task.task_key} | status: ${snapshot.task.status} | work: ${snapshot.task.title || '-'}`);
1012
+ }
1013
+ if (snapshot.run) {
1014
+ logger.log(`Run: ${snapshot.run.run_key} | status: ${snapshot.run.status} | work: ${snapshot.run.title || snapshot.run.summary || '-'}`);
1015
+ }
1016
+ if (snapshot.startedAt) {
1017
+ logger.log(`Started: ${snapshot.startedAt}`);
1018
+ }
1019
+ if (snapshot.updatedAt) {
1020
+ logger.log(`Updated: ${snapshot.updatedAt}`);
1021
+ }
1022
+ if (snapshot.closedAt) {
1023
+ logger.log(`Closed: ${snapshot.closedAt}`);
1024
+ }
1025
+ if (snapshot.warning) {
1026
+ logger.log(`Warning: ${snapshot.warning}`);
1027
+ }
1028
+
1029
+ if (snapshot.recentEvents.length === 0) {
1030
+ logger.log('Recent events: none');
1031
+ return;
1032
+ }
1033
+
1034
+ logger.log('Recent events:');
1035
+ for (const event of snapshot.recentEvents) {
1036
+ logger.log(`- ${event.ts} | ${event.type} | ${event.summary || '-'}`);
1037
+ }
1038
+ }
1039
+
1040
+ async function getLiveStatusSnapshot(targetDir, t, options = {}) {
1041
+ const { dbPath } = resolveRuntimePaths(targetDir);
1042
+
1043
+ if (!(await runtimeStoreExists(targetDir))) {
1044
+ throw new Error(t('runtime.store_missing', { path: dbPath }));
1045
+ }
1046
+
1047
+ const { db, runtimeDir } = await openRuntimeDb(targetDir, { mustExist: true });
1048
+ try {
1049
+ const context = await resolveLiveContext(targetDir, db, runtimeDir, {
1050
+ agentName: options.agent,
1051
+ limit: options.limit
1052
+ });
1053
+
1054
+ if (!context.run && !context.state) {
1055
+ return {
1056
+ ok: true,
1057
+ targetDir,
1058
+ dbPath,
1059
+ agent: context.agentName,
1060
+ tool: null,
1061
+ phase: 'idle',
1062
+ open: false,
1063
+ processState: 'not_tracked',
1064
+ pid: null,
1065
+ sessionKey: null,
1066
+ startedAt: null,
1067
+ updatedAt: null,
1068
+ closedAt: null,
1069
+ title: null,
1070
+ currentTask: null,
1071
+ run: null,
1072
+ task: null,
1073
+ stats: normalizeLiveStats(null),
1074
+ recentEvents: []
1075
+ };
1076
+ }
1077
+
1078
+ const taskMeta = parseTaskMeta(context.task);
1079
+ const planStats = getPlanStats(taskMeta);
1080
+ const state = context.state || createLiveState(targetDir, context.run, context.task, {
1081
+ sessionKey: context.sessionKey,
1082
+ activeAgent: context.agentName,
1083
+ projectPath: targetDir
1084
+ });
1085
+ state.stats = normalizeLiveStats(state.stats, planStats);
1086
+
1087
+ // Prefer run.agent_name when no explicit --agent was passed;
1088
+ // this lets events emitted by other agents update the bar dynamically.
1089
+ const effectiveAgent = options.agent
1090
+ ? context.agentName
1091
+ : (context.run?.agent_name || context.agentName);
1092
+
1093
+ const snapshot = {
1094
+ ok: true,
1095
+ targetDir,
1096
+ dbPath,
1097
+ agent: effectiveAgent,
1098
+ tool: state.tool_session || null,
1099
+ phase: context.phase,
1100
+ open: context.open,
1101
+ processState: context.processState,
1102
+ pid: state.child_pid || null,
1103
+ sessionKey: context.sessionKey,
1104
+ startedAt: state.started_at || null,
1105
+ updatedAt: state.updated_at || null,
1106
+ closedAt: state.closed_at || null,
1107
+ title: state.title || null,
1108
+ currentTask: state.current_task || null,
1109
+ run: context.run,
1110
+ task: context.task,
1111
+ stats: state.stats,
1112
+ recentEvents: Array.isArray(state.last_events) && state.last_events.length > 0 ? state.last_events : context.recentEvents,
1113
+ contextEstimated: state.context_estimated || null,
1114
+ warning: context.processState === 'dead' && context.phase === 'active'
1115
+ ? t('live.process_dead_warning')
1116
+ : null
1117
+ };
1118
+
1119
+ // Fallback: estimate context on-the-fly if not recorded at session start
1120
+ if (!snapshot.contextEstimated && snapshot.phase !== 'idle') {
1121
+ try {
1122
+ snapshot.contextEstimated = await estimateContextSize(targetDir);
1123
+ } catch {
1124
+ // non-fatal
1125
+ }
1126
+ }
1127
+
1128
+ return snapshot;
1129
+ } finally {
1130
+ db.close();
1131
+ }
1132
+ }
1133
+
1134
+ async function runLiveStart({ args, options = {}, logger, t }) {
1135
+ const targetDir = resolveTargetDir(args);
1136
+ const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1137
+ const tool = normalizeLiveTool(requireOption(options, 'tool', t), t);
1138
+ const noLaunch = Boolean(options['no-launch']);
1139
+
1140
+ if (options.json && !noLaunch && !options.attach) {
1141
+ throw new Error(t('live.json_requires_no_launch'));
1142
+ }
1143
+
1144
+ // ── 5.3 Ambient Intelligence health check alert ────────────────────────────
1145
+ if (!options.json && !options['no-health-check']) {
1146
+ try {
1147
+ const { runHealthCheck, formatHealthAlert } = require('../lib/health-check');
1148
+ const health = await runHealthCheck(targetDir);
1149
+ const alert = formatHealthAlert(health.items);
1150
+ if (alert) {
1151
+ logger.log('');
1152
+ logger.log(alert);
1153
+ logger.log('');
1154
+ }
1155
+ } catch { /* health check is non-fatal */ }
1156
+ }
1157
+
1158
+ const toolBinary = String(options['tool-bin'] || tool).trim();
1159
+ const binaryPath = await resolveExecutablePath(toolBinary);
1160
+ if (!binaryPath) {
1161
+ throw new Error(t('live.tool_binary_not_found', { binary: toolBinary }));
1162
+ }
1163
+
1164
+ const useTmux = Boolean(options.tmux) || process.env.AIOSON_TMUX === '1';
1165
+
1166
+ // Pre-check tmux availability so we can warn early
1167
+ if (useTmux && !noLaunch) {
1168
+ const tmuxOk = await isTmuxAvailable();
1169
+ if (!tmuxOk && !options.json) {
1170
+ logger.log(t('live.tmux_not_found', { tool }));
1171
+ }
1172
+ }
1173
+
1174
+ const { db, dbPath, runtimeDir } = await openRuntimeDb(targetDir);
1175
+
1176
+ try {
1177
+ const existing = await resolveLiveContext(targetDir, db, runtimeDir, {
1178
+ agentName,
1179
+ limit: options.limit
1180
+ });
1181
+
1182
+ if (existing.run && existing.run.source === 'live' && existing.open) {
1183
+ const state = existing.state || createLiveState(targetDir, existing.run, existing.task, {
1184
+ sessionKey: existing.sessionKey,
1185
+ activeAgent: existing.agentName,
1186
+ projectPath: targetDir
1187
+ });
1188
+
1189
+ // ── Tmux session recovery: if tmux was killed, close the stale live session ──
1190
+ if (useTmux) {
1191
+ const sessionName = buildSessionName(targetDir, agentName);
1192
+ const tmuxAlive = await hasSession(sessionName);
1193
+ if (!tmuxAlive) {
1194
+ // Tmux is gone — close the stale live session in DB and continue to create new
1195
+ const now = new Date().toISOString();
1196
+ updateRun(db, {
1197
+ runKey: existing.run.run_key,
1198
+ status: 'completed',
1199
+ summary: 'Closed because tmux session was terminated',
1200
+ eventType: 'session_closed',
1201
+ phase: 'live',
1202
+ message: 'Tmux session ended — live session auto-closed'
1203
+ });
1204
+ if (existing.task?.task_key) {
1205
+ updateTask(db, {
1206
+ taskKey: existing.task.task_key,
1207
+ status: 'completed',
1208
+ goal: 'Auto-closed after tmux termination'
1209
+ });
1210
+ }
1211
+ await clearAgentSession(runtimeDir, agentName);
1212
+ if (!options.json) {
1213
+ logger.log(t('live.tmux_recreate', { agent: agentName, session: existing.sessionKey }));
1214
+ }
1215
+ // Fall through to create a new session below
1216
+ } else {
1217
+ // Tmux still alive — reattach instead of creating new
1218
+ if (!options.json) {
1219
+ logger.log(t('live.tmux_reattach', { agent: agentName, session: existing.sessionKey }));
1220
+ }
1221
+ const sessionName = buildSessionName(targetDir, agentName);
1222
+ await attachSession(sessionName);
1223
+ return {
1224
+ ok: true,
1225
+ targetDir,
1226
+ dbPath,
1227
+ tmux: true,
1228
+ reused: true,
1229
+ agent: existing.agentName,
1230
+ tool: state.tool_session || tool,
1231
+ taskKey: existing.task?.task_key || existing.sessionRef?.taskKey || null,
1232
+ runKey: existing.run.run_key,
1233
+ sessionKey: existing.sessionKey,
1234
+ open: true
1235
+ };
1236
+ }
1237
+ } else {
1238
+ // Non-tmux reuse logic (original behavior)
1239
+ const existingTool = state.tool_session || null;
1240
+ if (existingTool && existingTool !== tool) {
1241
+ throw new Error(t('live.tool_mismatch', { existing: existingTool, requested: tool }));
1242
+ }
1243
+
1244
+ const attach = Boolean(options.attach);
1245
+ let attachChild = null;
1246
+ let attachResult = null;
1247
+
1248
+ if (attach && !noLaunch) {
1249
+ attachChild = spawn(binaryPath, buildLaunchArgs(options, tool), {
1250
+ cwd: targetDir,
1251
+ env: process.env,
1252
+ stdio: 'inherit'
1253
+ });
1254
+ state.child_pid = attachChild.pid || null;
1255
+ if (existing.task?.task_key) {
1256
+ const taskMeta = parseTaskMeta(existing.task);
1257
+ taskMeta.child_pid = state.child_pid;
1258
+ updateTask(db, { taskKey: existing.task.task_key, metaJson: taskMeta });
1259
+ }
1260
+ }
1261
+
1262
+ await writeLiveState(runtimeDir, existing.sessionKey, state);
1263
+
1264
+ if (!options.json) {
1265
+ logger.log(t('live.session_already_active', { agent: agentName, session: existing.sessionKey, runKey: existing.run.run_key, dbPath }));
1266
+ }
1267
+
1268
+ if (attachChild) {
1269
+ attachResult = await waitForChild(attachChild);
1270
+ }
1271
+
1272
+ return {
1273
+ ok: true,
1274
+ targetDir,
1275
+ dbPath,
1276
+ agent: existing.agentName,
1277
+ tool: state.tool_session || tool,
1278
+ taskKey: existing.task?.task_key || existing.sessionRef?.taskKey || null,
1279
+ runKey: existing.run.run_key,
1280
+ sessionKey: existing.sessionKey,
1281
+ pid: state.child_pid || null,
1282
+ processState: detectProcessState(state.child_pid),
1283
+ reused: true,
1284
+ open: true,
1285
+ attached: attach,
1286
+ childExitCode: attachResult?.code ?? null,
1287
+ childSignal: attachResult?.signal ?? null
1288
+ };
1289
+ }
1290
+ }
1291
+
1292
+ const now = new Date().toISOString();
1293
+ const sessionKey = options.session ? String(options.session).trim() : makeDirectSessionKey(agentName);
1294
+ const title = options.title ? String(options.title).trim() : `live-${tool}-${Date.now()}`;
1295
+ const goal = options.goal ? String(options.goal).trim() : null;
1296
+ const planRef = options.plan ? String(options.plan).trim() : null;
1297
+ const plan = await loadPlanReference(targetDir, planRef);
1298
+ const startMessage = truncateMessage(options.message, `Live session started for ${agentName} with ${tool}`);
1299
+ const taskMeta = {
1300
+ tool_session: tool,
1301
+ plan_ref: plan.planRef,
1302
+ path: targetDir,
1303
+ child_pid: null
1304
+ };
1305
+ if (plan.planSteps.length > 0) {
1306
+ taskMeta.plan_steps = plan.planSteps;
1307
+ }
1308
+
1309
+ const taskKey = startTask(db, {
1310
+ sessionKey,
1311
+ title,
1312
+ goal,
1313
+ status: 'running',
1314
+ createdBy: agentName,
1315
+ taskKind: 'live_session',
1316
+ metaJson: taskMeta
1317
+ });
1318
+
1319
+ const runKey = startRun(db, {
1320
+ taskKey,
1321
+ agentName,
1322
+ agentKind: 'official',
1323
+ sessionKey,
1324
+ source: 'live',
1325
+ title,
1326
+ eventType: 'session_started',
1327
+ phase: 'live',
1328
+ message: startMessage,
1329
+ payload: {
1330
+ tool_session: tool,
1331
+ plan_ref: plan.planRef,
1332
+ plan_steps_total: plan.planSteps.length,
1333
+ path: targetDir
1334
+ }
1335
+ });
1336
+
1337
+ let child = null;
1338
+ let childResult = null;
1339
+ let tmuxResult = null;
1340
+ if (!noLaunch) {
1341
+ if (useTmux) {
1342
+ const tmuxOk = await isTmuxAvailable();
1343
+ if (tmuxOk) {
1344
+ if (!options.json) {
1345
+ logger.log(t('live.tmux_starting', { agent: agentName, tool }));
1346
+ }
1347
+ tmuxResult = await launchTmuxSession({
1348
+ targetDir,
1349
+ agentName,
1350
+ tool,
1351
+ binaryPath,
1352
+ toolArgs: buildLaunchArgs(options, tool)
1353
+ });
1354
+ } else {
1355
+ // Fallback to normal spawn if tmux not available
1356
+ child = spawn(binaryPath, buildLaunchArgs(options, tool), {
1357
+ cwd: targetDir,
1358
+ env: process.env,
1359
+ stdio: 'inherit'
1360
+ });
1361
+ taskMeta.child_pid = child.pid || null;
1362
+ updateTask(db, {
1363
+ taskKey,
1364
+ metaJson: taskMeta
1365
+ });
1366
+ }
1367
+ } else {
1368
+ child = spawn(binaryPath, buildLaunchArgs(options, tool), {
1369
+ cwd: targetDir,
1370
+ env: process.env,
1371
+ stdio: 'inherit'
1372
+ });
1373
+ taskMeta.child_pid = child.pid || null;
1374
+ updateTask(db, {
1375
+ taskKey,
1376
+ metaJson: taskMeta
1377
+ });
1378
+ }
1379
+ }
1380
+
1381
+ await writeAgentSession(runtimeDir, agentName, {
1382
+ runKey,
1383
+ taskKey,
1384
+ sessionKey,
1385
+ startedAt: now,
1386
+ finished: false,
1387
+ source: 'live'
1388
+ });
1389
+
1390
+ const state = createLiveState(targetDir, {
1391
+ run_key: runKey,
1392
+ session_key: sessionKey,
1393
+ agent_name: agentName,
1394
+ title,
1395
+ status: 'running',
1396
+ started_at: now,
1397
+ updated_at: now
1398
+ }, {
1399
+ task_key: taskKey,
1400
+ session_key: sessionKey,
1401
+ title,
1402
+ meta_json: JSON.stringify(taskMeta),
1403
+ created_at: now,
1404
+ updated_at: now
1405
+ }, {
1406
+ tool,
1407
+ planRef: plan.planRef,
1408
+ activeAgent: agentName,
1409
+ currentRunKey: runKey,
1410
+ projectPath: targetDir,
1411
+ childPid: taskMeta.child_pid,
1412
+ stats: {
1413
+ tasks_completed: 0,
1414
+ events_total: 1,
1415
+ plan_steps_done: 0,
1416
+ plan_steps_total: plan.planSteps.length
1417
+ },
1418
+ lastEvents: [{
1419
+ ts: now,
1420
+ type: 'session_started',
1421
+ summary: startMessage
1422
+ }]
1423
+ });
1424
+
1425
+ // Estimate context size for observability
1426
+ try {
1427
+ const ctxEst = await estimateContextSize(targetDir);
1428
+ state.context_estimated = ctxEst;
1429
+ } catch {
1430
+ // non-fatal
1431
+ }
1432
+
1433
+ await writeLiveState(runtimeDir, sessionKey, state);
1434
+ await appendLiveEvent(runtimeDir, sessionKey, {
1435
+ ts: now,
1436
+ type: 'session_started',
1437
+ summary: startMessage,
1438
+ agent: agentName,
1439
+ task_key: taskKey,
1440
+ run_key: runKey,
1441
+ session_key: sessionKey,
1442
+ refs: [],
1443
+ plan_step: null,
1444
+ status: 'running',
1445
+ meta: {
1446
+ tool_session: tool,
1447
+ child_pid: taskMeta.child_pid,
1448
+ path: targetDir,
1449
+ plan_ref: plan.planRef,
1450
+ plan_steps_total: plan.planSteps.length
1451
+ }
1452
+ });
1453
+
1454
+ if (!options.json) {
1455
+ logger.log(t('live.session_started', { agent: agentName, tool, session: sessionKey, dbPath }));
1456
+ }
1457
+
1458
+ // Ambient Intelligence: exibe digest de saúde ao iniciar sessão
1459
+ if (!options.json && !options['no-health']) {
1460
+ try {
1461
+ const { getHealthDigest } = require('./health');
1462
+ const items = await getHealthDigest(targetDir);
1463
+ if (items && items.length > 0) {
1464
+ logger.log('');
1465
+ logger.log('AIOSON Health — itens pendentes:');
1466
+ for (const item of items) {
1467
+ logger.log(` ● ${item}`);
1468
+ }
1469
+ logger.log(' → aioson health . para detalhes');
1470
+ logger.log('');
1471
+ }
1472
+ } catch { /* não bloqueia o start */ }
1473
+ }
1474
+
1475
+ if (child) {
1476
+ childResult = await waitForChild(child);
1477
+ }
1478
+
1479
+ if (tmuxResult) {
1480
+ return {
1481
+ ok: true,
1482
+ targetDir,
1483
+ dbPath,
1484
+ tmux: true,
1485
+ sessionName: tmuxResult.sessionName,
1486
+ agent: agentName,
1487
+ tool,
1488
+ taskKey,
1489
+ runKey,
1490
+ sessionKey,
1491
+ pid: null,
1492
+ processState: 'tmux',
1493
+ reused: false,
1494
+ open: true
1495
+ };
1496
+ }
1497
+
1498
+ return {
1499
+ ok: true,
1500
+ targetDir,
1501
+ dbPath,
1502
+ agent: agentName,
1503
+ tool,
1504
+ taskKey,
1505
+ runKey,
1506
+ sessionKey,
1507
+ pid: taskMeta.child_pid,
1508
+ processState: detectProcessState(taskMeta.child_pid),
1509
+ reused: false,
1510
+ open: true,
1511
+ childExitCode: childResult?.code ?? null,
1512
+ childSignal: childResult?.signal ?? null
1513
+ };
1514
+ } finally {
1515
+ db.close();
1516
+ }
1517
+ }
1518
+
1519
+ async function runRuntimeEmit({ args, options = {}, logger, t }) {
1520
+ const targetDir = resolveTargetDir(args);
1521
+ const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1522
+ const eventType = String(options.type || 'note').trim() || 'note';
1523
+
1524
+ const { db, dbPath, runtimeDir, context } = await requireActiveLiveContext(targetDir, agentName, t, {
1525
+ limit: options.limit
1526
+ });
1527
+
1528
+ try {
1529
+ const now = new Date().toISOString();
1530
+ const refs = parseRefs(options.refs);
1531
+ const planStep = options['plan-step'] ? String(options['plan-step']).trim() : null;
1532
+ const summary = truncateMessage(
1533
+ options.summary || options.message || options.title || `${eventType} emitted by ${agentName}`
1534
+ );
1535
+ const meta = parseJsonOption(options.meta);
1536
+ const payload = meta && typeof meta === 'object' ? { ...meta } : {};
1537
+ if (refs.length > 0) payload.refs = refs;
1538
+ if (planStep) payload.plan_step = planStep;
1539
+
1540
+ const state = context.state || createLiveState(targetDir, context.run, context.task, {
1541
+ sessionKey: context.sessionKey,
1542
+ activeAgent: context.agentName,
1543
+ projectPath: targetDir
1544
+ });
1545
+
1546
+ let currentTaskKey = state.current_task || null;
1547
+ let nextCurrentTask = currentTaskKey;
1548
+
1549
+ if (eventType === 'task_started') {
1550
+ if (currentTaskKey) {
1551
+ throw new Error(t('live.micro_task_already_open', { agent: agentName }));
1552
+ }
1553
+
1554
+ currentTaskKey = startTask(db, {
1555
+ sessionKey: context.sessionKey,
1556
+ title: options.title ? String(options.title).trim() : summary,
1557
+ goal: summary,
1558
+ status: 'running',
1559
+ createdBy: agentName,
1560
+ taskKind: 'micro_task',
1561
+ parentTaskKey: context.task.task_key,
1562
+ metaJson: {
1563
+ refs,
1564
+ plan_step: planStep
1565
+ }
1566
+ });
1567
+ nextCurrentTask = currentTaskKey;
1568
+ payload.micro_task_key = currentTaskKey;
1569
+ } else if (eventType === 'task_completed') {
1570
+ if (currentTaskKey) {
1571
+ updateTask(db, {
1572
+ taskKey: currentTaskKey,
1573
+ status: 'completed',
1574
+ goal: summary,
1575
+ metaJson: {
1576
+ refs,
1577
+ plan_step: planStep
1578
+ }
1579
+ });
1580
+ } else {
1581
+ currentTaskKey = startTask(db, {
1582
+ sessionKey: context.sessionKey,
1583
+ title: options.title ? String(options.title).trim() : summary,
1584
+ goal: summary,
1585
+ status: 'completed',
1586
+ createdBy: agentName,
1587
+ taskKind: 'micro_task',
1588
+ parentTaskKey: context.task.task_key,
1589
+ metaJson: {
1590
+ refs,
1591
+ plan_step: planStep,
1592
+ implicit: true
1593
+ }
1594
+ });
1595
+ }
1596
+ nextCurrentTask = null;
1597
+ payload.micro_task_key = currentTaskKey;
1598
+ }
1599
+
1600
+ let planStats = null;
1601
+ if (eventType === 'plan_checkpoint' && planStep) {
1602
+ const sessionMeta = parseTaskMeta(context.task);
1603
+ if (Array.isArray(sessionMeta.plan_steps)) {
1604
+ const normalizedPlanStep = normalizePlanStepId(planStep).toLowerCase();
1605
+ let changed = false;
1606
+ sessionMeta.plan_steps = sessionMeta.plan_steps.map((step) => {
1607
+ if (!step || normalizePlanStepId(step.id).toLowerCase() !== normalizedPlanStep) return step;
1608
+ if (step.done) return step;
1609
+ changed = true;
1610
+ return { ...step, done: true };
1611
+ });
1612
+ if (changed) {
1613
+ updateTask(db, {
1614
+ taskKey: context.task.task_key,
1615
+ metaJson: sessionMeta
1616
+ });
1617
+ planStats = getPlanStats(sessionMeta);
1618
+ }
1619
+ }
1620
+ }
1621
+
1622
+ const workerStatus = options['worker-status'] ? String(options['worker-status']).trim() : null;
1623
+ const verdict = options.verdict ? String(options.verdict).trim().toUpperCase() : null;
1624
+ const tokenCount = options['token-count'] != null ? Number(options['token-count']) || null : null;
1625
+ const progressPct = options['progress-pct'] != null ? Number(options['progress-pct']) || null : null;
1626
+
1627
+ appendRunEvent(db, {
1628
+ runKey: context.run.run_key,
1629
+ eventType,
1630
+ phase: 'live',
1631
+ status: context.run.status || 'running',
1632
+ message: summary,
1633
+ payload: Object.keys(payload).length > 0 ? payload : null,
1634
+ createdAt: now,
1635
+ planStepId: planStep || null,
1636
+ workerStatus,
1637
+ verdict,
1638
+ tokenCount,
1639
+ progressPct
1640
+ });
1641
+
1642
+ const eventRecord = createLiveEventRecord(context, {
1643
+ ts: now,
1644
+ type: eventType,
1645
+ summary,
1646
+ refs,
1647
+ planStep,
1648
+ taskKey: currentTaskKey,
1649
+ meta: meta && Object.keys(meta).length > 0 ? meta : null
1650
+ });
1651
+
1652
+ await appendLiveEvent(runtimeDir, context.sessionKey, eventRecord);
1653
+
1654
+ const nextState = applyEventToState(state, eventRecord, {
1655
+ currentTask: nextCurrentTask,
1656
+ planStats,
1657
+ activeAgent: context.agentName,
1658
+ currentRunKey: context.run.run_key
1659
+ });
1660
+ await writeLiveState(runtimeDir, context.sessionKey, nextState);
1661
+
1662
+ if (!options.json) {
1663
+ logger.log(t('live.event_recorded', { agent: agentName, eventType, session: context.sessionKey, dbPath }));
1664
+ }
1665
+
1666
+ return {
1667
+ ok: true,
1668
+ targetDir,
1669
+ dbPath,
1670
+ agent: context.agentName,
1671
+ eventType,
1672
+ sessionKey: context.sessionKey,
1673
+ runKey: context.run.run_key,
1674
+ taskKey: currentTaskKey || context.task.task_key,
1675
+ currentTask: nextCurrentTask,
1676
+ open: true
1677
+ };
1678
+ } finally {
1679
+ db.close();
1680
+ }
1681
+ }
1682
+
1683
+
1684
+ async function runLiveHandoff({ args, options = {}, logger, t }) {
1685
+ const targetDir = resolveTargetDir(args);
1686
+ const agentName = normalizeAgentHandle(requireOption(options, 'agent', t));
1687
+ const nextAgent = normalizeAgentHandle(requireOption(options, 'to', t));
1688
+
1689
+ if (agentName === nextAgent) {
1690
+ throw new Error(t('live.handoff_same_agent'));
1691
+ }
1692
+
1693
+ const reason = truncateMessage(
1694
+ options.reason || options.summary || options.message,
1695
+ `Handoff from ${agentName} to ${nextAgent}`
1696
+ );
1697
+
1698
+ const { db, dbPath, runtimeDir, context } = await requireActiveLiveContext(targetDir, agentName, t, {
1699
+ limit: options.limit
1700
+ });
1701
+
1702
+ try {
1703
+ if (!context.run || context.run.agent_name !== agentName) {
1704
+ throw new Error(t('live.handoff_agent_mismatch', { agent: agentName }));
1705
+ }
1706
+
1707
+ const now = new Date().toISOString();
1708
+ const state = context.state || createLiveState(targetDir, context.run, context.task, {
1709
+ sessionKey: context.sessionKey,
1710
+ activeAgent: context.agentName,
1711
+ projectPath: targetDir
1712
+ });
1713
+
1714
+ const handoffSummary = truncateMessage(`Handoff to ${nextAgent}: ${reason}`);
1715
+ let currentTaskClosed = false;
1716
+ if (state.current_task) {
1717
+ updateTask(db, {
1718
+ taskKey: state.current_task,
1719
+ status: 'completed',
1720
+ goal: truncateMessage(`Closed on handoff to ${nextAgent}: ${reason}`)
1721
+ });
1722
+ currentTaskClosed = true;
1723
+ }
1724
+
1725
+ updateRun(db, {
1726
+ runKey: context.run.run_key,
1727
+ status: 'completed',
1728
+ summary: reason,
1729
+ eventType: 'handoff',
1730
+ phase: 'live',
1731
+ message: handoffSummary,
1732
+ payload: {
1733
+ from: agentName,
1734
+ to: nextAgent,
1735
+ reason,
1736
+ previous_run_key: context.run.run_key,
1737
+ micro_task_key: state.current_task || null,
1738
+ closed_by: 'live:handoff'
1739
+ }
1740
+ });
1741
+
1742
+ const nextRunKey = startRun(db, {
1743
+ taskKey: context.task.task_key,
1744
+ agentName: nextAgent,
1745
+ agentKind: 'official',
1746
+ sessionKey: context.sessionKey,
1747
+ source: 'live',
1748
+ parentRunKey: context.run.run_key,
1749
+ title: nextAgent,
1750
+ phase: 'live',
1751
+ message: truncateMessage(`Live handoff from ${agentName}`),
1752
+ payload: {
1753
+ handoff_from: agentName,
1754
+ reason
1755
+ }
1756
+ });
1757
+
1758
+ await clearAgentSession(runtimeDir, agentName);
1759
+ await writeAgentSession(runtimeDir, nextAgent, {
1760
+ runKey: nextRunKey,
1761
+ taskKey: context.task.task_key,
1762
+ sessionKey: context.sessionKey,
1763
+ startedAt: now,
1764
+ finished: false,
1765
+ source: 'live'
1766
+ });
1767
+
1768
+ const eventRecord = createLiveEventRecord(context, {
1769
+ ts: now,
1770
+ type: 'handoff',
1771
+ summary: handoffSummary,
1772
+ taskKey: context.task.task_key,
1773
+ status: 'completed',
1774
+ meta: {
1775
+ from: agentName,
1776
+ to: nextAgent,
1777
+ reason,
1778
+ previous_run_key: context.run.run_key,
1779
+ current_run_key: nextRunKey,
1780
+ micro_task_key: state.current_task || null
1781
+ }
1782
+ });
1783
+ await appendLiveEvent(runtimeDir, context.sessionKey, eventRecord);
1784
+
1785
+ const nextState = applyEventToState(state, eventRecord, {
1786
+ currentTask: null,
1787
+ activeAgent: nextAgent,
1788
+ currentRunKey: nextRunKey
1789
+ });
1790
+ if (currentTaskClosed) {
1791
+ nextState.stats.tasks_completed += 1;
1792
+ }
1793
+ await writeLiveState(runtimeDir, context.sessionKey, nextState);
1794
+
1795
+ if (!options.json) {
1796
+ logger.log(t('live.handoff_recorded', { from: agentName, to: nextAgent, session: context.sessionKey, dbPath }));
1797
+ }
1798
+
1799
+ return {
1800
+ ok: true,
1801
+ targetDir,
1802
+ dbPath,
1803
+ agent: agentName,
1804
+ nextAgent,
1805
+ taskKey: context.task.task_key,
1806
+ previousRunKey: context.run.run_key,
1807
+ runKey: nextRunKey,
1808
+ sessionKey: context.sessionKey,
1809
+ open: true
1810
+ };
1811
+ } finally {
1812
+ db.close();
1813
+ }
1814
+ }
1815
+
1816
+ async function runLiveStatus({ args, options = {}, logger, t }) {
1817
+ const targetDir = resolveTargetDir(args);
1818
+ const watchSeconds = parseWatchSeconds(options.watch);
1819
+
1820
+ if (watchSeconds && options.json) {
1821
+ throw new Error(t('live.watch_json_conflict'));
1822
+ }
1823
+
1824
+ if (!watchSeconds) {
1825
+ const snapshot = await getLiveStatusSnapshot(targetDir, t, options);
1826
+ if (!options.json) {
1827
+ if (options.format === 'compact') {
1828
+ printCompactStatus(snapshot, logger);
1829
+ } else if (options.format === 'tmux-bar') {
1830
+ printTmuxBar(snapshot, logger);
1831
+ } else {
1832
+ printLiveStatusSnapshot(snapshot, logger);
1833
+ }
1834
+ }
1835
+ return snapshot;
1836
+ }
1837
+
1838
+ let stopped = false;
1839
+ const onSignal = () => { stopped = true; };
1840
+ process.on('SIGINT', onSignal);
1841
+ process.on('SIGTERM', onSignal);
1842
+
1843
+ try {
1844
+ while (!stopped) {
1845
+ const snapshot = await getLiveStatusSnapshot(targetDir, t, options);
1846
+ if (process.stdout && process.stdout.isTTY) {
1847
+ process.stdout.write('\x1Bc');
1848
+ }
1849
+ if (options.format === 'compact') {
1850
+ printCompactStatus(snapshot, logger);
1851
+ } else if (options.format === 'tmux-bar') {
1852
+ printTmuxBar(snapshot, logger);
1853
+ } else {
1854
+ printLiveStatusSnapshot(snapshot, logger);
1855
+ }
1856
+ if (stopped) break;
1857
+ await sleep(Math.round(watchSeconds * 1000));
1858
+ }
1859
+ } finally {
1860
+ process.removeListener('SIGINT', onSignal);
1861
+ process.removeListener('SIGTERM', onSignal);
1862
+ }
1863
+ }
1864
+
1865
+ async function runLiveClose({ args, options = {}, logger, t }) {
1866
+ const targetDir = resolveTargetDir(args);
1867
+ const requestedAgent = options.agent ? normalizeAgentHandle(options.agent) : null;
1868
+ const { db, dbPath, runtimeDir } = await withRuntimeDb(targetDir, t);
1869
+
1870
+ try {
1871
+ const context = await resolveLiveContext(targetDir, db, runtimeDir, {
1872
+ agentName: requestedAgent,
1873
+ limit: options.limit
1874
+ });
1875
+
1876
+ if (!context.run || context.run.source !== 'live' || !context.sessionKey || !context.task) {
1877
+ throw new Error(requestedAgent
1878
+ ? t('live.no_session_for_agent', { agent: requestedAgent })
1879
+ : t('live.no_session_found'));
1880
+ }
1881
+
1882
+ if (context.phase !== 'active') {
1883
+ throw new Error(t('live.session_already_closed', { session: context.sessionKey }));
1884
+ }
1885
+
1886
+ const status = String(options.status || 'completed').trim().toLowerCase() === 'failed' ? 'failed' : 'completed';
1887
+ const now = new Date().toISOString();
1888
+ const summary = truncateMessage(options.summary || options.message || `Live session closed for ${context.agentName}`);
1889
+ const state = context.state || createLiveState(targetDir, context.run, context.task, {
1890
+ sessionKey: context.sessionKey,
1891
+ activeAgent: context.agentName,
1892
+ projectPath: targetDir
1893
+ });
1894
+
1895
+ let currentTaskClosed = false;
1896
+ if (state.current_task) {
1897
+ updateTask(db, {
1898
+ taskKey: state.current_task,
1899
+ status,
1900
+ goal: summary
1901
+ });
1902
+ currentTaskClosed = true;
1903
+ }
1904
+
1905
+ updateRun(db, {
1906
+ runKey: context.run.run_key,
1907
+ status,
1908
+ summary,
1909
+ eventType: 'session_closed',
1910
+ message: summary,
1911
+ payload: {
1912
+ closed_by: 'live:close'
1913
+ }
1914
+ });
1915
+
1916
+ updateTask(db, {
1917
+ taskKey: context.task.task_key,
1918
+ status,
1919
+ goal: summary
1920
+ });
1921
+
1922
+ const eventRecord = createLiveEventRecord(context, {
1923
+ ts: now,
1924
+ type: 'session_closed',
1925
+ summary,
1926
+ taskKey: context.task.task_key,
1927
+ status
1928
+ });
1929
+ await appendLiveEvent(runtimeDir, context.sessionKey, eventRecord);
1930
+
1931
+ const nextState = applyEventToState(state, eventRecord, {
1932
+ currentTask: null,
1933
+ phase: 'closed',
1934
+ closedAt: now,
1935
+ activeAgent: context.agentName,
1936
+ currentRunKey: context.run.run_key
1937
+ });
1938
+ if (currentTaskClosed && status === 'completed') {
1939
+ nextState.stats.tasks_completed += 1;
1940
+ }
1941
+
1942
+ const git = await collectGitSnapshot(targetDir);
1943
+ await writeLiveState(runtimeDir, context.sessionKey, nextState);
1944
+ const summaryPath = await writeLiveSummary(runtimeDir, context.sessionKey, renderLiveSummary({
1945
+ sessionKey: context.sessionKey,
1946
+ agent: context.agentName,
1947
+ tool: nextState.tool_session,
1948
+ status,
1949
+ startedAt: nextState.started_at,
1950
+ closedAt: now,
1951
+ summary,
1952
+ git,
1953
+ recentEvents: nextState.last_events
1954
+ }));
1955
+
1956
+ await clearAgentSession(runtimeDir, context.agentName);
1957
+
1958
+ if (!options.json) {
1959
+ logger.log(t('live.session_closed', { agent: context.agentName, session: context.sessionKey, dbPath }));
1960
+ }
1961
+
1962
+ // Ambient Intelligence: sugere evolução se há learnings acumulados
1963
+ if (!options.json && !options['no-health']) {
1964
+ try {
1965
+ const { getHealthDigest } = require('./health');
1966
+ const items = await getHealthDigest(targetDir);
1967
+ if (items && items.length > 0) {
1968
+ logger.log('');
1969
+ logger.log('AIOSON Health — itens após sessão:');
1970
+ for (const item of items) {
1971
+ logger.log(` ● ${item}`);
1972
+ }
1973
+ logger.log(' → aioson health . para detalhes e ações');
1974
+ logger.log('');
1975
+ }
1976
+ } catch { /* não bloqueia o close */ }
1977
+ }
1978
+
1979
+ return {
1980
+ ok: true,
1981
+ targetDir,
1982
+ dbPath,
1983
+ agent: context.agentName,
1984
+ taskKey: context.task.task_key,
1985
+ runKey: context.run.run_key,
1986
+ sessionKey: context.sessionKey,
1987
+ status,
1988
+ closed: true,
1989
+ summaryPath,
1990
+ git
1991
+ };
1992
+ } finally {
1993
+ db.close();
1994
+ }
1995
+ }
1996
+
1997
+ async function runLiveList({ args, options = {}, logger, t }) {
1998
+ const targetDir = resolveTargetDir(args);
1999
+ const { dbPath } = resolveRuntimePaths(targetDir);
2000
+
2001
+ if (!(await runtimeStoreExists(targetDir))) {
2002
+ throw new Error(t('runtime.store_missing', { path: dbPath }));
2003
+ }
2004
+
2005
+ const { db, runtimeDir } = await openRuntimeDb(targetDir, { mustExist: true });
2006
+ db.close();
2007
+ const states = await listLiveStates(runtimeDir);
2008
+
2009
+ if (!options.json) {
2010
+ if (states.length === 0) {
2011
+ logger.log(t('live.list_empty'));
2012
+ } else {
2013
+ logger.log(t('live.list_title', { count: states.length }));
2014
+ for (const state of states) {
2015
+ logger.log(t('live.list_line', {
2016
+ session: state.session_key || '-',
2017
+ agent: state.active_agent || '-',
2018
+ tool: state.tool_session || '-',
2019
+ phase: state.phase || '-',
2020
+ updatedAt: state.updated_at || state.started_at || '-'
2021
+ }));
2022
+ }
2023
+ }
2024
+ }
2025
+
2026
+ return {
2027
+ ok: true,
2028
+ targetDir,
2029
+ dbPath,
2030
+ count: states.length,
2031
+ sessions: states.map((state) => ({
2032
+ sessionKey: state.session_key,
2033
+ agent: state.active_agent,
2034
+ tool: state.tool_session,
2035
+ phase: state.phase,
2036
+ title: state.title,
2037
+ startedAt: state.started_at,
2038
+ updatedAt: state.updated_at,
2039
+ closedAt: state.closed_at
2040
+ }))
2041
+ };
2042
+ }
2043
+
2044
+ module.exports = {
2045
+ buildLaunchArgs,
2046
+ runLiveStart,
2047
+ runRuntimeEmit,
2048
+ runLiveHandoff,
2049
+ runLiveStatus,
2050
+ runLiveClose,
2051
+ runLiveList,
2052
+ // Exported so callers (including tests) can resolve the on-disk directory
2053
+ // for a given session key without re-implementing the sanitization rules.
2054
+ sessionKeyToDirName,
2055
+ resolveLivePaths
2056
+ };
2057
+
2058
+ // ── Context estimation helpers ──
2059
+
2060
+ const CONTEXT_FILES = [
2061
+ '.aioson/context/project.context.md',
2062
+ '.aioson/context/spec.md',
2063
+ '.aioson/context/features.md',
2064
+ '.aioson/context/context-pack.md',
2065
+ '.aioson/context/discovery.md',
2066
+ '.aioson/context/architecture.md',
2067
+ '.aioson/context/readiness.md',
2068
+ '.aioson/context/design-doc.md',
2069
+ '.aioson/context/skeleton-system.md'
2070
+ ];
2071
+
2072
+ async function estimateContextSize(projectDir) {
2073
+ let totalBytes = 0;
2074
+ const foundFiles = [];
2075
+
2076
+ for (const rel of CONTEXT_FILES) {
2077
+ const filePath = path.join(projectDir, rel);
2078
+ try {
2079
+ const stat = await fs.stat(filePath);
2080
+ if (stat.isFile()) {
2081
+ totalBytes += stat.size;
2082
+ foundFiles.push(rel);
2083
+ }
2084
+ } catch {
2085
+ // ignore missing files
2086
+ }
2087
+ }
2088
+
2089
+ // Heuristic: ~4 chars per token (english-ish text)
2090
+ const estimatedTokens = Math.round(totalBytes / 4);
2091
+ // Default window size assumption (200k for Sonnet-class)
2092
+ const windowSize = 200000;
2093
+
2094
+ return {
2095
+ totalBytes,
2096
+ estimatedTokens,
2097
+ windowSize,
2098
+ pct: Math.min(100, Math.round((estimatedTokens / windowSize) * 100)),
2099
+ files: foundFiles
2100
+ };
2101
+ }