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