@jaimevalasek/aioson 1.3.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 (288) hide show
  1. package/CHANGELOG.md +456 -0
  2. package/CODE_OF_CONDUCT.md +12 -0
  3. package/CONTRIBUTING.md +13 -0
  4. package/LICENSE +21 -0
  5. package/README.md +254 -0
  6. package/bin/aioson.js +4 -0
  7. package/docs/en/cli-reference.md +398 -0
  8. package/docs/en/i18n.md +52 -0
  9. package/docs/en/json-schemas.md +41 -0
  10. package/docs/en/mcp.md +56 -0
  11. package/docs/en/parallel.md +82 -0
  12. package/docs/en/qa-browser.md +339 -0
  13. package/docs/en/release-flow.md +22 -0
  14. package/docs/en/release-notes-template.md +41 -0
  15. package/docs/en/release.md +28 -0
  16. package/docs/en/schemas/agent-prompt.schema.json +17 -0
  17. package/docs/en/schemas/agents.schema.json +32 -0
  18. package/docs/en/schemas/context-validate.schema.json +36 -0
  19. package/docs/en/schemas/doctor.schema.json +89 -0
  20. package/docs/en/schemas/error.schema.json +24 -0
  21. package/docs/en/schemas/i18n-add.schema.json +15 -0
  22. package/docs/en/schemas/index.json +116 -0
  23. package/docs/en/schemas/info.schema.json +39 -0
  24. package/docs/en/schemas/init.schema.json +48 -0
  25. package/docs/en/schemas/install.schema.json +60 -0
  26. package/docs/en/schemas/locale-apply.schema.json +30 -0
  27. package/docs/en/schemas/mcp-doctor.schema.json +95 -0
  28. package/docs/en/schemas/mcp-init.schema.json +122 -0
  29. package/docs/en/schemas/package-test.schema.json +24 -0
  30. package/docs/en/schemas/parallel-assign.schema.json +57 -0
  31. package/docs/en/schemas/parallel-doctor.schema.json +86 -0
  32. package/docs/en/schemas/parallel-init.schema.json +53 -0
  33. package/docs/en/schemas/parallel-status.schema.json +94 -0
  34. package/docs/en/schemas/setup-context.schema.json +39 -0
  35. package/docs/en/schemas/smoke.schema.json +23 -0
  36. package/docs/en/schemas/update.schema.json +48 -0
  37. package/docs/en/schemas/workflow-plan.schema.json +30 -0
  38. package/docs/en/web3.md +54 -0
  39. package/docs/pt/README.md +46 -0
  40. package/docs/pt/advisor-spec.md +335 -0
  41. package/docs/pt/agentes.md +453 -0
  42. package/docs/pt/cenarios.md +1230 -0
  43. package/docs/pt/clientes-ai.md +224 -0
  44. package/docs/pt/comandos-cli.md +511 -0
  45. package/docs/pt/genome-3.0-spec.md +296 -0
  46. package/docs/pt/guia-engineer.md +226 -0
  47. package/docs/pt/inicio-rapido.md +138 -0
  48. package/docs/pt/profiler-system.md +214 -0
  49. package/docs/pt/runtime-observability.md +72 -0
  50. package/docs/pt/squad-genoma.md +777 -0
  51. package/docs/pt/web3.md +797 -0
  52. package/docs/testing/genome-2.0-manual-regression.md +23 -0
  53. package/docs/testing/genome-2.0-matrix.md +36 -0
  54. package/docs/testing/genome-2.0-rollout.md +184 -0
  55. package/package.json +50 -0
  56. package/src/agents.js +56 -0
  57. package/src/cli.js +497 -0
  58. package/src/commands/agents.js +142 -0
  59. package/src/commands/cloud.js +1767 -0
  60. package/src/commands/config.js +90 -0
  61. package/src/commands/context-validate.js +91 -0
  62. package/src/commands/doctor.js +123 -0
  63. package/src/commands/genome-doctor.js +41 -0
  64. package/src/commands/genome-migrate.js +49 -0
  65. package/src/commands/i18n-add.js +56 -0
  66. package/src/commands/info.js +41 -0
  67. package/src/commands/init.js +75 -0
  68. package/src/commands/install.js +68 -0
  69. package/src/commands/locale-apply.js +51 -0
  70. package/src/commands/locale-diff.js +126 -0
  71. package/src/commands/mcp-doctor.js +406 -0
  72. package/src/commands/mcp-init.js +379 -0
  73. package/src/commands/package-e2e.js +273 -0
  74. package/src/commands/parallel-assign.js +403 -0
  75. package/src/commands/parallel-doctor.js +437 -0
  76. package/src/commands/parallel-init.js +249 -0
  77. package/src/commands/parallel-status.js +290 -0
  78. package/src/commands/qa-doctor.js +185 -0
  79. package/src/commands/qa-init.js +161 -0
  80. package/src/commands/qa-report.js +58 -0
  81. package/src/commands/qa-run.js +873 -0
  82. package/src/commands/qa-scan.js +337 -0
  83. package/src/commands/runtime.js +948 -0
  84. package/src/commands/scan-project.js +1107 -0
  85. package/src/commands/setup-context.js +650 -0
  86. package/src/commands/smoke.js +426 -0
  87. package/src/commands/squad-doctor.js +358 -0
  88. package/src/commands/squad-export.js +46 -0
  89. package/src/commands/squad-pipeline.js +97 -0
  90. package/src/commands/squad-repair-genomes.js +39 -0
  91. package/src/commands/squad-status.js +424 -0
  92. package/src/commands/squad-validate.js +230 -0
  93. package/src/commands/test-agents.js +194 -0
  94. package/src/commands/update.js +55 -0
  95. package/src/commands/workflow-next.js +594 -0
  96. package/src/commands/workflow-plan.js +108 -0
  97. package/src/constants.js +314 -0
  98. package/src/context-parse-reason.js +22 -0
  99. package/src/context-writer.js +150 -0
  100. package/src/context.js +217 -0
  101. package/src/detector.js +261 -0
  102. package/src/doctor.js +289 -0
  103. package/src/execution-gateway.js +461 -0
  104. package/src/genome-files.js +198 -0
  105. package/src/genome-format.js +442 -0
  106. package/src/genome-schema.js +215 -0
  107. package/src/genomes/bindings.js +281 -0
  108. package/src/genomes.js +467 -0
  109. package/src/i18n/index.js +103 -0
  110. package/src/i18n/messages/en.js +784 -0
  111. package/src/i18n/messages/es.js +718 -0
  112. package/src/i18n/messages/fr.js +725 -0
  113. package/src/i18n/messages/pt-BR.js +818 -0
  114. package/src/i18n/scaffold.js +64 -0
  115. package/src/installer.js +232 -0
  116. package/src/lib/genomes/compat.js +206 -0
  117. package/src/lib/genomes/migrate.js +90 -0
  118. package/src/lib/squads/genome-repair.js +49 -0
  119. package/src/locales.js +84 -0
  120. package/src/onboarding.js +305 -0
  121. package/src/parser.js +53 -0
  122. package/src/prompt-tool.js +20 -0
  123. package/src/qa-html-report.js +472 -0
  124. package/src/runtime-store.js +1527 -0
  125. package/src/squads/apply-genome.js +21 -0
  126. package/src/squads/genome-binding-service.js +154 -0
  127. package/src/updater.js +32 -0
  128. package/src/utils.js +46 -0
  129. package/src/version.js +50 -0
  130. package/template/.aioson/advisors/.gitkeep +1 -0
  131. package/template/.aioson/agents/analyst.md +225 -0
  132. package/template/.aioson/agents/architect.md +221 -0
  133. package/template/.aioson/agents/dev.md +201 -0
  134. package/template/.aioson/agents/discovery-design-doc.md +196 -0
  135. package/template/.aioson/agents/genoma.md +300 -0
  136. package/template/.aioson/agents/orchestrator.md +107 -0
  137. package/template/.aioson/agents/pm.md +89 -0
  138. package/template/.aioson/agents/product.md +361 -0
  139. package/template/.aioson/agents/profiler-enricher.md +266 -0
  140. package/template/.aioson/agents/profiler-forge.md +188 -0
  141. package/template/.aioson/agents/profiler-researcher.md +245 -0
  142. package/template/.aioson/agents/qa.md +344 -0
  143. package/template/.aioson/agents/setup.md +381 -0
  144. package/template/.aioson/agents/squad.md +837 -0
  145. package/template/.aioson/agents/ux-ui.md +416 -0
  146. package/template/.aioson/config.md +56 -0
  147. package/template/.aioson/context/.gitkeep +0 -0
  148. package/template/.aioson/context/parallel/.gitkeep +0 -0
  149. package/template/.aioson/context/spec.md.template +37 -0
  150. package/template/.aioson/genomas/.gitkeep +0 -0
  151. package/template/.aioson/locales/en/agents/analyst.md +214 -0
  152. package/template/.aioson/locales/en/agents/architect.md +210 -0
  153. package/template/.aioson/locales/en/agents/dev.md +187 -0
  154. package/template/.aioson/locales/en/agents/discovery-design-doc.md +27 -0
  155. package/template/.aioson/locales/en/agents/genoma.md +212 -0
  156. package/template/.aioson/locales/en/agents/orchestrator.md +105 -0
  157. package/template/.aioson/locales/en/agents/pm.md +77 -0
  158. package/template/.aioson/locales/en/agents/product.md +310 -0
  159. package/template/.aioson/locales/en/agents/profiler-enricher.md +5 -0
  160. package/template/.aioson/locales/en/agents/profiler-forge.md +5 -0
  161. package/template/.aioson/locales/en/agents/profiler-researcher.md +5 -0
  162. package/template/.aioson/locales/en/agents/qa.md +214 -0
  163. package/template/.aioson/locales/en/agents/setup.md +342 -0
  164. package/template/.aioson/locales/en/agents/squad.md +247 -0
  165. package/template/.aioson/locales/en/agents/ux-ui.md +320 -0
  166. package/template/.aioson/locales/es/agents/analyst.md +203 -0
  167. package/template/.aioson/locales/es/agents/architect.md +208 -0
  168. package/template/.aioson/locales/es/agents/dev.md +183 -0
  169. package/template/.aioson/locales/es/agents/discovery-design-doc.md +19 -0
  170. package/template/.aioson/locales/es/agents/genoma.md +102 -0
  171. package/template/.aioson/locales/es/agents/orchestrator.md +108 -0
  172. package/template/.aioson/locales/es/agents/pm.md +81 -0
  173. package/template/.aioson/locales/es/agents/product.md +310 -0
  174. package/template/.aioson/locales/es/agents/profiler-enricher.md +5 -0
  175. package/template/.aioson/locales/es/agents/profiler-forge.md +5 -0
  176. package/template/.aioson/locales/es/agents/profiler-researcher.md +5 -0
  177. package/template/.aioson/locales/es/agents/qa.md +163 -0
  178. package/template/.aioson/locales/es/agents/setup.md +347 -0
  179. package/template/.aioson/locales/es/agents/squad.md +247 -0
  180. package/template/.aioson/locales/es/agents/ux-ui.md +201 -0
  181. package/template/.aioson/locales/fr/agents/analyst.md +203 -0
  182. package/template/.aioson/locales/fr/agents/architect.md +208 -0
  183. package/template/.aioson/locales/fr/agents/dev.md +183 -0
  184. package/template/.aioson/locales/fr/agents/discovery-design-doc.md +19 -0
  185. package/template/.aioson/locales/fr/agents/genoma.md +102 -0
  186. package/template/.aioson/locales/fr/agents/orchestrator.md +108 -0
  187. package/template/.aioson/locales/fr/agents/pm.md +81 -0
  188. package/template/.aioson/locales/fr/agents/product.md +310 -0
  189. package/template/.aioson/locales/fr/agents/profiler-enricher.md +5 -0
  190. package/template/.aioson/locales/fr/agents/profiler-forge.md +5 -0
  191. package/template/.aioson/locales/fr/agents/profiler-researcher.md +5 -0
  192. package/template/.aioson/locales/fr/agents/qa.md +163 -0
  193. package/template/.aioson/locales/fr/agents/setup.md +347 -0
  194. package/template/.aioson/locales/fr/agents/squad.md +247 -0
  195. package/template/.aioson/locales/fr/agents/ux-ui.md +201 -0
  196. package/template/.aioson/locales/pt-BR/agents/analyst.md +217 -0
  197. package/template/.aioson/locales/pt-BR/agents/architect.md +213 -0
  198. package/template/.aioson/locales/pt-BR/agents/dev.md +198 -0
  199. package/template/.aioson/locales/pt-BR/agents/discovery-design-doc.md +198 -0
  200. package/template/.aioson/locales/pt-BR/agents/genoma.md +297 -0
  201. package/template/.aioson/locales/pt-BR/agents/orchestrator.md +108 -0
  202. package/template/.aioson/locales/pt-BR/agents/pm.md +81 -0
  203. package/template/.aioson/locales/pt-BR/agents/product.md +316 -0
  204. package/template/.aioson/locales/pt-BR/agents/profiler-enricher.md +5 -0
  205. package/template/.aioson/locales/pt-BR/agents/profiler-forge.md +5 -0
  206. package/template/.aioson/locales/pt-BR/agents/profiler-researcher.md +5 -0
  207. package/template/.aioson/locales/pt-BR/agents/qa.md +217 -0
  208. package/template/.aioson/locales/pt-BR/agents/setup.md +371 -0
  209. package/template/.aioson/locales/pt-BR/agents/squad.md +772 -0
  210. package/template/.aioson/locales/pt-BR/agents/ux-ui.md +322 -0
  211. package/template/.aioson/mcp/servers.md +24 -0
  212. package/template/.aioson/profiler-reports/.gitkeep +1 -0
  213. package/template/.aioson/schemas/content-blueprint.schema.json +30 -0
  214. package/template/.aioson/schemas/genome-meta.schema.json +150 -0
  215. package/template/.aioson/schemas/genome.schema.json +115 -0
  216. package/template/.aioson/schemas/readiness.schema.json +27 -0
  217. package/template/.aioson/schemas/squad-blueprint.schema.json +172 -0
  218. package/template/.aioson/schemas/squad-manifest.schema.json +276 -0
  219. package/template/.aioson/skills/dynamic/README.md +30 -0
  220. package/template/.aioson/skills/dynamic/cardano-docs.md +16 -0
  221. package/template/.aioson/skills/dynamic/ethereum-docs.md +17 -0
  222. package/template/.aioson/skills/dynamic/flux-ui-docs.md +13 -0
  223. package/template/.aioson/skills/dynamic/laravel-docs.md +41 -0
  224. package/template/.aioson/skills/dynamic/npm-packages.md +16 -0
  225. package/template/.aioson/skills/dynamic/solana-docs.md +16 -0
  226. package/template/.aioson/skills/references/premium-command-center-ui/master-application-prompt.md +79 -0
  227. package/template/.aioson/skills/references/premium-command-center-ui/operational-ux-playbook.md +253 -0
  228. package/template/.aioson/skills/references/premium-command-center-ui/quality-validation-checklist.md +82 -0
  229. package/template/.aioson/skills/references/premium-command-center-ui/visual-system-and-component-patterns.md +270 -0
  230. package/template/.aioson/skills/static/django-patterns.md +342 -0
  231. package/template/.aioson/skills/static/fastapi-patterns.md +344 -0
  232. package/template/.aioson/skills/static/filament-patterns.md +267 -0
  233. package/template/.aioson/skills/static/flux-ui-components.md +262 -0
  234. package/template/.aioson/skills/static/git-conventions.md +227 -0
  235. package/template/.aioson/skills/static/interface-design.md +372 -0
  236. package/template/.aioson/skills/static/jetstream-setup.md +200 -0
  237. package/template/.aioson/skills/static/laravel-conventions.md +491 -0
  238. package/template/.aioson/skills/static/nextjs-patterns.md +321 -0
  239. package/template/.aioson/skills/static/node-express-patterns.md +317 -0
  240. package/template/.aioson/skills/static/node-typescript-patterns.md +282 -0
  241. package/template/.aioson/skills/static/premium-command-center-ui.md +190 -0
  242. package/template/.aioson/skills/static/rails-conventions.md +307 -0
  243. package/template/.aioson/skills/static/react-motion-patterns.md +577 -0
  244. package/template/.aioson/skills/static/static-html-patterns.md +1935 -0
  245. package/template/.aioson/skills/static/tall-stack-patterns.md +286 -0
  246. package/template/.aioson/skills/static/ui-ux-modern.md +75 -0
  247. package/template/.aioson/skills/static/web3-cardano-patterns.md +337 -0
  248. package/template/.aioson/skills/static/web3-ethereum-patterns.md +310 -0
  249. package/template/.aioson/skills/static/web3-security-checklist.md +284 -0
  250. package/template/.aioson/skills/static/web3-solana-patterns.md +324 -0
  251. package/template/.aioson/squads/.artisan/.gitkeep +0 -0
  252. package/template/.aioson/squads/.gitkeep +0 -0
  253. package/template/.aioson/squads/memory.md +5 -0
  254. package/template/.aioson/tasks/squad-analyze.md +83 -0
  255. package/template/.aioson/tasks/squad-create.md +99 -0
  256. package/template/.aioson/tasks/squad-design.md +100 -0
  257. package/template/.aioson/tasks/squad-export.md +20 -0
  258. package/template/.aioson/tasks/squad-extend.md +68 -0
  259. package/template/.aioson/tasks/squad-pipeline.md +122 -0
  260. package/template/.aioson/tasks/squad-repair.md +85 -0
  261. package/template/.aioson/tasks/squad-validate.md +58 -0
  262. package/template/.aioson/templates/squads/content-basic/template.json +21 -0
  263. package/template/.aioson/templates/squads/media-channel/template.json +24 -0
  264. package/template/.aioson/templates/squads/research-analysis/template.json +22 -0
  265. package/template/.aioson/templates/squads/software-delivery/template.json +21 -0
  266. package/template/.claude/commands/aioson/analyst.md +5 -0
  267. package/template/.claude/commands/aioson/architect.md +5 -0
  268. package/template/.claude/commands/aioson/dev.md +5 -0
  269. package/template/.claude/commands/aioson/orchestrator.md +5 -0
  270. package/template/.claude/commands/aioson/pm.md +5 -0
  271. package/template/.claude/commands/aioson/qa.md +5 -0
  272. package/template/.claude/commands/aioson/setup.md +5 -0
  273. package/template/.claude/commands/aioson/ux-ui.md +5 -0
  274. package/template/.gemini/GEMINI.md +10 -0
  275. package/template/.gemini/commands/aios-analyst.toml +4 -0
  276. package/template/.gemini/commands/aios-architect.toml +7 -0
  277. package/template/.gemini/commands/aios-dev.toml +8 -0
  278. package/template/.gemini/commands/aios-discovery-design-doc.toml +4 -0
  279. package/template/.gemini/commands/aios-orchestrator.toml +8 -0
  280. package/template/.gemini/commands/aios-pm.toml +8 -0
  281. package/template/.gemini/commands/aios-product.toml +4 -0
  282. package/template/.gemini/commands/aios-qa.toml +6 -0
  283. package/template/.gemini/commands/aios-setup.toml +3 -0
  284. package/template/.gemini/commands/aios-ux-ui.toml +8 -0
  285. package/template/AGENTS.md +67 -0
  286. package/template/CLAUDE.md +31 -0
  287. package/template/OPENCODE.md +24 -0
  288. package/template/aioson-models.json +40 -0
@@ -0,0 +1,461 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ openRuntimeDb,
5
+ startTask,
6
+ updateTask,
7
+ startRun,
8
+ updateRun,
9
+ appendRunEvent
10
+ } = require('./runtime-store');
11
+
12
+ function normalizeText(value) {
13
+ return String(value || '').trim();
14
+ }
15
+
16
+ function normalizeAgentName(value) {
17
+ const text = normalizeText(value).toLowerCase();
18
+ return text ? (text.startsWith('@') ? text : `@${text}`) : '';
19
+ }
20
+
21
+ function classifyDirectAgentRuntime(agentName) {
22
+ const normalized = normalizeAgentName(agentName);
23
+
24
+ if (normalized === '@squad') {
25
+ return {
26
+ agentName: normalized,
27
+ agentKind: 'squad',
28
+ source: 'squad_session'
29
+ };
30
+ }
31
+
32
+ if (normalized === '@workflow') {
33
+ return {
34
+ agentName: normalized,
35
+ agentKind: 'workflow',
36
+ source: 'workflow'
37
+ };
38
+ }
39
+
40
+ if (normalized === '@orchestrator') {
41
+ return {
42
+ agentName: normalized,
43
+ agentKind: 'official',
44
+ source: 'orchestration'
45
+ };
46
+ }
47
+
48
+ return {
49
+ agentName: normalized,
50
+ agentKind: 'official',
51
+ source: 'direct'
52
+ };
53
+ }
54
+
55
+ function makeWorkflowSessionKey(state) {
56
+ const scope = state.featureSlug ? `feature:${state.featureSlug}` : 'project:default';
57
+ return `workflow:${normalizeText(state.mode || 'project')}:${scope}`;
58
+ }
59
+
60
+ function makeWorkflowTaskTitle(state) {
61
+ return state.featureSlug
62
+ ? `Workflow da feature ${state.featureSlug}`
63
+ : 'Workflow do projeto';
64
+ }
65
+
66
+ function makeWorkflowTaskGoal(state) {
67
+ return state.featureSlug
68
+ ? `Governar a execucao da feature ${state.featureSlug}`
69
+ : 'Governar a execucao do projeto';
70
+ }
71
+
72
+ function makeWorkflowControllerTitle(state) {
73
+ return state.featureSlug
74
+ ? `Workflow controller (${state.featureSlug})`
75
+ : 'Workflow controller';
76
+ }
77
+
78
+ function makeWorkflowStageTitle(agentName) {
79
+ return `Workflow stage ${agentName}`;
80
+ }
81
+
82
+ function findTaskBySession(db, sessionKey) {
83
+ return db
84
+ .prepare(
85
+ `
86
+ SELECT task_key, status
87
+ FROM tasks
88
+ WHERE session_key = ?
89
+ ORDER BY updated_at DESC, created_at DESC
90
+ LIMIT 1
91
+ `
92
+ )
93
+ .get(sessionKey);
94
+ }
95
+
96
+ function findRunByTaskAndAgent(db, taskKey, agentName, options = {}) {
97
+ const statuses = Array.isArray(options.statuses) && options.statuses.length > 0
98
+ ? options.statuses
99
+ : null;
100
+ const workflowStage = options.workflowStage ? normalizeText(options.workflowStage) : null;
101
+
102
+ let sql = `
103
+ SELECT run_key, status
104
+ FROM agent_runs
105
+ WHERE task_key = ?
106
+ AND agent_name = ?
107
+ `;
108
+ const params = [taskKey, agentName];
109
+
110
+ if (workflowStage) {
111
+ sql += ' AND workflow_stage = ?';
112
+ params.push(workflowStage);
113
+ }
114
+
115
+ if (statuses) {
116
+ sql += ` AND status IN (${statuses.map(() => '?').join(', ')})`;
117
+ params.push(...statuses);
118
+ }
119
+
120
+ sql += ' ORDER BY updated_at DESC, started_at DESC LIMIT 1';
121
+ return db.prepare(sql).get(...params);
122
+ }
123
+
124
+ async function ensureProjectRuntime(targetDir) {
125
+ const handle = await openRuntimeDb(targetDir);
126
+ try {
127
+ return {
128
+ runtimeDir: handle.runtimeDir,
129
+ dbPath: handle.dbPath
130
+ };
131
+ } finally {
132
+ handle.db.close();
133
+ }
134
+ }
135
+
136
+ async function recordRuntimeOperation(targetDir, input) {
137
+ const handle = await openRuntimeDb(targetDir);
138
+ const { db, dbPath } = handle;
139
+
140
+ try {
141
+ const classification = classifyDirectAgentRuntime(input.agentName || 'orchestrator');
142
+ const sessionKey = normalizeText(input.sessionKey) || `operation:${Date.now()}:${classification.agentName}`;
143
+ const title = normalizeText(input.title) || `Runtime operation ${classification.agentName}`;
144
+ const goal = normalizeText(input.goal) || `Registrar operacao ${classification.agentName}`;
145
+ const phase = normalizeText(input.phase) || 'operation';
146
+ const source = normalizeText(input.source) || classification.source;
147
+ const finalStatus = normalizeText(input.status) || 'completed';
148
+ const failure = finalStatus === 'failed';
149
+
150
+ let task = findTaskBySession(db, sessionKey);
151
+ if (!task) {
152
+ task = {
153
+ task_key: startTask(db, {
154
+ sessionKey,
155
+ title,
156
+ goal,
157
+ createdBy: classification.agentName,
158
+ status: 'running'
159
+ }),
160
+ status: 'running'
161
+ };
162
+ } else {
163
+ updateTask(db, { taskKey: task.task_key, status: 'running' });
164
+ }
165
+
166
+ const runTitle = normalizeText(input.runTitle) || normalizeText(input.commandName) || title;
167
+ const startMessage = normalizeText(input.message) || `Operation started: ${runTitle}`;
168
+ const summary = normalizeText(input.summary) || startMessage;
169
+ const runKey = startRun(db, {
170
+ taskKey: task.task_key,
171
+ agentName: classification.agentName,
172
+ agentKind: normalizeText(input.agentKind) || classification.agentKind,
173
+ sessionKey,
174
+ source,
175
+ workflowId: normalizeText(input.workflowId) || null,
176
+ workflowStage: normalizeText(input.workflowStage) || null,
177
+ parentRunKey: normalizeText(input.parentRunKey) || null,
178
+ title: runTitle,
179
+ status: 'running',
180
+ message: startMessage,
181
+ payload: input.payload || null,
182
+ phase
183
+ });
184
+
185
+ updateRun(db, {
186
+ runKey,
187
+ status: finalStatus,
188
+ eventType: normalizeText(input.eventType) || (failure ? 'failed' : 'completed'),
189
+ phase,
190
+ source,
191
+ workflowId: normalizeText(input.workflowId) || null,
192
+ workflowStage: normalizeText(input.workflowStage) || null,
193
+ parentRunKey: normalizeText(input.parentRunKey) || null,
194
+ message: summary,
195
+ summary,
196
+ payload: input.payload || null,
197
+ toolName: normalizeText(input.toolName) || null
198
+ });
199
+
200
+ updateTask(db, {
201
+ taskKey: task.task_key,
202
+ status: failure ? 'failed' : 'completed'
203
+ });
204
+
205
+ return {
206
+ ok: true,
207
+ dbPath,
208
+ taskKey: task.task_key,
209
+ runKey
210
+ };
211
+ } finally {
212
+ db.close();
213
+ }
214
+ }
215
+
216
+ async function syncWorkflowRuntime(targetDir, input) {
217
+ const handle = await openRuntimeDb(targetDir);
218
+ const { db, dbPath } = handle;
219
+
220
+ try {
221
+ const state = input.state || {};
222
+ const eventPayload = input.eventPayload || {};
223
+ const activationAgent = normalizeText(input.activationAgent);
224
+ const completedStage = normalizeText(input.completedStage);
225
+ const sessionKey = makeWorkflowSessionKey(state);
226
+ const workflowId = sessionKey;
227
+
228
+ let task = findTaskBySession(db, sessionKey);
229
+ if (!task) {
230
+ task = {
231
+ task_key: startTask(db, {
232
+ sessionKey,
233
+ title: makeWorkflowTaskTitle(state),
234
+ goal: makeWorkflowTaskGoal(state),
235
+ createdBy: '@workflow',
236
+ status: activationAgent ? 'running' : 'queued'
237
+ }),
238
+ status: activationAgent ? 'running' : 'queued'
239
+ };
240
+ }
241
+
242
+ const taskKey = task.task_key;
243
+ const controllerAgent = '@workflow';
244
+ let controllerRun = findRunByTaskAndAgent(db, taskKey, controllerAgent, {
245
+ statuses: ['queued', 'running']
246
+ });
247
+
248
+ if (!controllerRun) {
249
+ controllerRun = {
250
+ run_key: startRun(db, {
251
+ taskKey,
252
+ agentName: controllerAgent,
253
+ agentKind: 'workflow',
254
+ sessionKey,
255
+ source: 'workflow',
256
+ workflowId,
257
+ workflowStage: activationAgent || completedStage || normalizeText(state.current || state.next),
258
+ title: makeWorkflowControllerTitle(state),
259
+ message: eventPayload.message || 'Workflow initialized'
260
+ })
261
+ };
262
+ } else {
263
+ appendRunEvent(db, {
264
+ runKey: controllerRun.run_key,
265
+ eventType: eventPayload.eventType || 'workflow',
266
+ phase: 'workflow',
267
+ status: activationAgent ? 'running' : 'queued',
268
+ message: eventPayload.message || 'Workflow updated',
269
+ payload: eventPayload
270
+ });
271
+ }
272
+
273
+ if (controllerRun.run_key && eventPayload.eventType && eventPayload.eventType !== 'start' && eventPayload.eventType !== 'completed') {
274
+ appendRunEvent(db, {
275
+ runKey: controllerRun.run_key,
276
+ eventType: eventPayload.eventType,
277
+ phase: 'workflow',
278
+ status: activationAgent ? 'running' : 'queued',
279
+ message: eventPayload.message || 'Workflow updated',
280
+ payload: eventPayload
281
+ });
282
+ }
283
+
284
+ if (completedStage) {
285
+ const completedAgent = normalizeAgentName(completedStage);
286
+ let completedRun = findRunByTaskAndAgent(db, taskKey, completedAgent, {
287
+ workflowStage: completedStage,
288
+ statuses: ['queued', 'running']
289
+ });
290
+
291
+ if (!completedRun) {
292
+ completedRun = {
293
+ run_key: startRun(db, {
294
+ taskKey,
295
+ agentName: completedAgent,
296
+ agentKind: 'official',
297
+ sessionKey,
298
+ source: 'workflow',
299
+ workflowId,
300
+ workflowStage: completedStage,
301
+ parentRunKey: controllerRun.run_key,
302
+ title: makeWorkflowStageTitle(completedAgent),
303
+ message: `Stage ${completedAgent} restored for completion`
304
+ })
305
+ };
306
+ }
307
+
308
+ updateRun(db, {
309
+ runKey: completedRun.run_key,
310
+ status: 'completed',
311
+ eventType: 'completed',
312
+ phase: 'workflow',
313
+ source: 'workflow',
314
+ workflowId,
315
+ workflowStage: completedStage,
316
+ parentRunKey: controllerRun.run_key,
317
+ message: `Workflow stage completed: ${completedAgent}`,
318
+ summary: eventPayload.message || `Stage ${completedAgent} completed`,
319
+ payload: eventPayload
320
+ });
321
+ }
322
+
323
+ let stageRunKey = null;
324
+ if (activationAgent) {
325
+ const stageAgent = normalizeAgentName(activationAgent);
326
+ let stageRun = findRunByTaskAndAgent(db, taskKey, stageAgent, {
327
+ workflowStage: activationAgent,
328
+ statuses: ['queued', 'running']
329
+ });
330
+
331
+ if (!stageRun) {
332
+ stageRun = {
333
+ run_key: startRun(db, {
334
+ taskKey,
335
+ agentName: stageAgent,
336
+ agentKind: 'official',
337
+ sessionKey,
338
+ source: 'workflow',
339
+ workflowId,
340
+ workflowStage: activationAgent,
341
+ parentRunKey: controllerRun.run_key,
342
+ title: makeWorkflowStageTitle(stageAgent),
343
+ message: eventPayload.message || `Workflow routed to ${stageAgent}`
344
+ })
345
+ };
346
+ } else {
347
+ appendRunEvent(db, {
348
+ runKey: stageRun.run_key,
349
+ eventType: eventPayload.eventType || 'workflow',
350
+ phase: 'workflow',
351
+ status: 'running',
352
+ message: eventPayload.message || `Workflow stage active: ${stageAgent}`,
353
+ payload: eventPayload
354
+ });
355
+ }
356
+
357
+ stageRunKey = stageRun.run_key;
358
+ if (eventPayload.eventType && eventPayload.eventType !== 'start' && eventPayload.eventType !== 'completed') {
359
+ appendRunEvent(db, {
360
+ runKey: stageRun.run_key,
361
+ eventType: eventPayload.eventType,
362
+ phase: 'workflow',
363
+ status: 'running',
364
+ message: eventPayload.message || `Workflow stage active: ${stageAgent}`,
365
+ payload: eventPayload
366
+ });
367
+ }
368
+ updateTask(db, { taskKey, status: 'running' });
369
+ } else {
370
+ updateRun(db, {
371
+ runKey: controllerRun.run_key,
372
+ status: 'completed',
373
+ eventType: 'completed',
374
+ phase: 'workflow',
375
+ source: 'workflow',
376
+ workflowId,
377
+ workflowStage: normalizeText(state.current || state.next),
378
+ message: eventPayload.message || 'Workflow completed',
379
+ summary: eventPayload.message || 'Workflow completed',
380
+ payload: eventPayload
381
+ });
382
+ updateTask(db, { taskKey, status: 'completed' });
383
+ }
384
+
385
+ return {
386
+ ok: true,
387
+ dbPath,
388
+ taskKey,
389
+ controllerRunKey: controllerRun.run_key,
390
+ stageRunKey
391
+ };
392
+ } finally {
393
+ db.close();
394
+ }
395
+ }
396
+
397
+ async function bootstrapDirectAgentPrompt(targetDir, input) {
398
+ const handle = await openRuntimeDb(targetDir);
399
+ const { db, dbPath } = handle;
400
+
401
+ try {
402
+ const classification = classifyDirectAgentRuntime(input.agentName);
403
+ const agentName = classification.agentName;
404
+ const title = normalizeText(input.title) || `Direct handoff for ${agentName}`;
405
+ const taskKey = startTask(db, {
406
+ sessionKey: normalizeText(input.sessionKey) || `direct:${Date.now()}:${agentName}`,
407
+ title,
408
+ goal: normalizeText(input.goal) || `Entregar prompt e handoff para ${agentName}`,
409
+ createdBy: '@runtime',
410
+ status: 'queued'
411
+ });
412
+
413
+ const runKey = startRun(db, {
414
+ taskKey,
415
+ agentName,
416
+ agentKind: normalizeText(input.agentKind) || classification.agentKind,
417
+ sessionKey: normalizeText(input.sessionKey) || null,
418
+ source: normalizeText(input.source) || classification.source,
419
+ workflowId: null,
420
+ workflowStage: null,
421
+ title,
422
+ status: 'queued',
423
+ message: input.message || `Prompt generated for ${agentName}`,
424
+ payload: input.payload || null,
425
+ phase: 'handoff'
426
+ });
427
+
428
+ appendRunEvent(db, {
429
+ runKey,
430
+ eventType: 'prompt.generated',
431
+ phase: 'handoff',
432
+ status: 'queued',
433
+ message: input.message || `Prompt generated for ${agentName}`,
434
+ payload: {
435
+ tool: input.tool || 'codex',
436
+ locale: input.locale || 'en',
437
+ instructionPath: input.instructionPath || null,
438
+ prompt: input.prompt || null,
439
+ requestedAgent: agentName
440
+ }
441
+ });
442
+
443
+ return {
444
+ ok: true,
445
+ dbPath,
446
+ taskKey,
447
+ runKey
448
+ };
449
+ } finally {
450
+ db.close();
451
+ }
452
+ }
453
+
454
+ module.exports = {
455
+ ensureProjectRuntime,
456
+ syncWorkflowRuntime,
457
+ makeWorkflowSessionKey,
458
+ classifyDirectAgentRuntime,
459
+ bootstrapDirectAgentPrompt,
460
+ recordRuntimeOperation
461
+ };
@@ -0,0 +1,198 @@
1
+ 'use strict';
2
+
3
+ const fs = require('node:fs/promises');
4
+ const path = require('node:path');
5
+ const {
6
+ isGenomeV2,
7
+ normalizeGenome,
8
+ normalizeGenomeMeta
9
+ } = require('./genomes');
10
+ const { parseGenomeMarkdown, serializeGenomeMarkdown } = require('./genome-format');
11
+ const { assertValidGenome, assertValidGenomeMeta } = require('./genome-schema');
12
+ const { ensureDir, exists } = require('./utils');
13
+
14
+ function getGenomeDir(projectRoot) {
15
+ return path.join(projectRoot, '.aioson', 'genomas');
16
+ }
17
+
18
+ function getGenomeMarkdownPath(projectRoot, slug) {
19
+ return path.join(getGenomeDir(projectRoot), `${slug}.md`);
20
+ }
21
+
22
+ function getGenomeMetaPath(projectRoot, slug) {
23
+ return path.join(getGenomeDir(projectRoot), `${slug}.meta.json`);
24
+ }
25
+
26
+ async function readMetaFile(filePath, slug) {
27
+ try {
28
+ const text = await fs.readFile(filePath, 'utf8');
29
+ return JSON.parse(text);
30
+ } catch (error) {
31
+ if (error && error.code === 'ENOENT') return null;
32
+ throw new Error(`Invalid genome meta JSON for "${slug}" at ${filePath}: ${error.message}`);
33
+ }
34
+ }
35
+
36
+ function mergeGenomeWithMeta(genome, rawMeta, slug) {
37
+ if (!rawMeta) {
38
+ return normalizeGenome({
39
+ ...genome,
40
+ slug: genome.slug || slug
41
+ });
42
+ }
43
+
44
+ return normalizeGenome({
45
+ ...genome,
46
+ slug: genome.slug || rawMeta.slug || slug,
47
+ domain: genome.domain || rawMeta.domain,
48
+ type: genome.hasFrontmatter ? genome.type : rawMeta.type,
49
+ language: genome.hasFrontmatter ? genome.language : rawMeta.language,
50
+ depth: genome.hasFrontmatter ? genome.depth : rawMeta.depth,
51
+ version: genome.hasFrontmatter ? genome.version : rawMeta.version,
52
+ format: genome.hasFrontmatter ? genome.format : rawMeta.format,
53
+ evidenceMode: genome.hasFrontmatter ? genome.evidenceMode : rawMeta.evidenceMode,
54
+ sourceCount: genome.hasFrontmatter ? genome.sourceCount : rawMeta.sourceCount,
55
+ personaSource: genome.hasFrontmatter ? genome.personaSource : rawMeta.personaSource,
56
+ personaSources: genome.hasFrontmatter ? genome.personaSources : rawMeta.personaSources,
57
+ disc: genome.hasFrontmatter ? genome.disc : rawMeta.disc,
58
+ enneagram: genome.hasFrontmatter ? genome.enneagram : rawMeta.enneagram,
59
+ bigFive: genome.hasFrontmatter ? genome.bigFive : rawMeta.bigFive,
60
+ mbti: genome.hasFrontmatter ? genome.mbti : rawMeta.mbti,
61
+ confidence: genome.hasFrontmatter ? genome.confidence : rawMeta.confidence,
62
+ profilerReport: genome.hasFrontmatter ? genome.profilerReport : rawMeta.profilerReport,
63
+ hybridMode: genome.hasFrontmatter ? genome.hybridMode : rawMeta.hybridMode
64
+ });
65
+ }
66
+
67
+ async function readGenome(projectRoot, slug) {
68
+ const markdownPath = getGenomeMarkdownPath(projectRoot, slug);
69
+ const metaPath = getGenomeMetaPath(projectRoot, slug);
70
+
71
+ if (!(await exists(markdownPath))) {
72
+ throw new Error(`Genome "${slug}" not found at ${markdownPath}`);
73
+ }
74
+
75
+ const markdown = await fs.readFile(markdownPath, 'utf8');
76
+ const parsedGenome = parseGenomeMarkdown(markdown);
77
+ const rawMeta = await readMetaFile(metaPath, slug);
78
+ const genome = assertValidGenome(mergeGenomeWithMeta(parsedGenome, rawMeta, slug));
79
+
80
+ let meta;
81
+ if (rawMeta) {
82
+ meta = assertValidGenomeMeta({
83
+ ...rawMeta,
84
+ genome,
85
+ compat: {
86
+ ...(rawMeta.compat || {}),
87
+ legacyMarkdownCompatible: true
88
+ }
89
+ });
90
+ } else {
91
+ meta = assertValidGenomeMeta(
92
+ normalizeGenomeMeta({
93
+ genome,
94
+ compat: {
95
+ legacyMarkdownCompatible: true,
96
+ synthesizedFromLegacy: !isGenomeV2(parsedGenome)
97
+ }
98
+ })
99
+ );
100
+ }
101
+
102
+ return {
103
+ genome,
104
+ meta,
105
+ paths: {
106
+ markdownPath,
107
+ metaPath
108
+ }
109
+ };
110
+ }
111
+
112
+ async function loadExistingMeta(projectRoot, slug) {
113
+ const metaPath = getGenomeMetaPath(projectRoot, slug);
114
+ const rawMeta = await readMetaFile(metaPath, slug);
115
+ if (!rawMeta) return null;
116
+ try {
117
+ return normalizeGenomeMeta(rawMeta);
118
+ } catch {
119
+ return null;
120
+ }
121
+ }
122
+
123
+ async function writeGenome(projectRoot, genomeInput, metaInput) {
124
+ const normalizedGenome = assertValidGenome(
125
+ normalizeGenome({
126
+ ...genomeInput,
127
+ generated: genomeInput && genomeInput.generated ? genomeInput.generated : new Date().toISOString().slice(0, 10),
128
+ legacyFormat: false,
129
+ hasFrontmatter: true
130
+ })
131
+ );
132
+ const markdownPath = getGenomeMarkdownPath(projectRoot, normalizedGenome.slug);
133
+ const metaPath = getGenomeMetaPath(projectRoot, normalizedGenome.slug);
134
+ const existingMeta = await loadExistingMeta(projectRoot, normalizedGenome.slug);
135
+
136
+ const normalizedMeta = assertValidGenomeMeta(
137
+ normalizeGenomeMeta({
138
+ ...(existingMeta || {}),
139
+ ...(metaInput || {}),
140
+ schemaVersion: normalizedGenome.version,
141
+ version: normalizedGenome.version,
142
+ format: normalizedGenome.format,
143
+ genome: normalizedGenome,
144
+ compat: {
145
+ ...((existingMeta && existingMeta.compat) || {}),
146
+ ...((metaInput && metaInput.compat) || {}),
147
+ legacyMarkdownCompatible: true,
148
+ synthesizedFromLegacy: false
149
+ },
150
+ createdAt:
151
+ (metaInput && metaInput.createdAt) ||
152
+ (existingMeta && existingMeta.createdAt) ||
153
+ `${normalizedGenome.generated}T00:00:00.000Z`,
154
+ updatedAt: new Date().toISOString()
155
+ })
156
+ );
157
+
158
+ await ensureDir(getGenomeDir(projectRoot));
159
+ await fs.writeFile(markdownPath, serializeGenomeMarkdown(normalizedGenome), 'utf8');
160
+ await fs.writeFile(metaPath, `${JSON.stringify(normalizedMeta, null, 2)}\n`, 'utf8');
161
+
162
+ return {
163
+ genome: normalizedGenome,
164
+ meta: normalizedMeta,
165
+ paths: {
166
+ markdownPath,
167
+ metaPath
168
+ }
169
+ };
170
+ }
171
+
172
+ async function listGenomes(projectRoot) {
173
+ const genomeDir = getGenomeDir(projectRoot);
174
+ try {
175
+ const entries = await fs.readdir(genomeDir, { withFileTypes: true });
176
+ return entries
177
+ .filter((entry) => entry.isFile() && entry.name.endsWith('.md') && !entry.name.endsWith('.meta.json'))
178
+ .map((entry) => entry.name.slice(0, -3))
179
+ .sort();
180
+ } catch (error) {
181
+ if (error && error.code === 'ENOENT') return [];
182
+ throw error;
183
+ }
184
+ }
185
+
186
+ async function genomeExists(projectRoot, slug) {
187
+ return exists(getGenomeMarkdownPath(projectRoot, slug));
188
+ }
189
+
190
+ module.exports = {
191
+ getGenomeDir,
192
+ getGenomeMarkdownPath,
193
+ getGenomeMetaPath,
194
+ readGenome,
195
+ writeGenome,
196
+ listGenomes,
197
+ genomeExists
198
+ };