@zaip-ai/cli 2.2.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (420) hide show
  1. package/dist/services/adapters.d.ts +1 -1
  2. package/dist/services/adapters.d.ts.map +1 -1
  3. package/dist/services/adapters.js +23 -9
  4. package/dist/services/adapters.js.map +1 -1
  5. package/dist/services/agent-adapters.js +25 -9
  6. package/dist/services/agent-adapters.js.map +1 -1
  7. package/dist/services/agent-discovery.d.ts +27 -0
  8. package/dist/services/agent-discovery.d.ts.map +1 -0
  9. package/dist/services/agent-discovery.js +73 -0
  10. package/dist/services/agent-discovery.js.map +1 -0
  11. package/dist/services/codex-conversation-lifecycle-hook.d.ts.map +1 -1
  12. package/dist/services/codex-conversation-lifecycle-hook.js +153 -24
  13. package/dist/services/codex-conversation-lifecycle-hook.js.map +1 -1
  14. package/dist/services/codex-pre-tool-hook-template.d.ts.map +1 -1
  15. package/dist/services/codex-pre-tool-hook-template.js +146 -72
  16. package/dist/services/codex-pre-tool-hook-template.js.map +1 -1
  17. package/dist/services/codex-rules.d.ts +1 -1
  18. package/dist/services/codex-rules.d.ts.map +1 -1
  19. package/dist/services/codex-rules.js +3 -3
  20. package/dist/services/codex-subagent-hook-template.d.ts +1 -1
  21. package/dist/services/codex-subagent-hook-template.d.ts.map +1 -1
  22. package/dist/services/codex-subagent-hook-template.js +117 -66
  23. package/dist/services/codex-subagent-hook-template.js.map +1 -1
  24. package/dist/services/install-agents-template.d.ts.map +1 -1
  25. package/dist/services/install-agents-template.js +56 -6
  26. package/dist/services/install-agents-template.js.map +1 -1
  27. package/dist/services/logger.js +1 -1
  28. package/dist/services/logger.js.map +1 -1
  29. package/dist/services/logger.test.js +22 -1
  30. package/dist/services/logger.test.js.map +1 -1
  31. package/dist/services/runtime-template.test.js +129 -0
  32. package/dist/services/runtime-template.test.js.map +1 -1
  33. package/package.json +1 -1
  34. package/templates/.agents/.env.example +9 -3
  35. package/templates/.agents/README-ENG.md +30 -5
  36. package/templates/.agents/README.md +30 -7
  37. package/templates/.agents/architecture/software-architecture.md +1 -59
  38. package/templates/.agents/branding/brandbook.md +3 -0
  39. package/templates/.agents/config/agents/catalog.yaml +22 -10
  40. package/templates/.agents/config/agents/discovery.json +319 -0
  41. package/templates/.agents/config/clients/codex/agents/zaip-agent-backend.toml +2 -2
  42. package/templates/.agents/config/clients/codex/agents/zaip-agent-builder.toml +2 -2
  43. package/templates/.agents/config/clients/codex/agents/zaip-agent-cloud-engineer.toml +11 -8
  44. package/templates/.agents/config/clients/codex/agents/zaip-agent-designer.toml +2 -2
  45. package/templates/.agents/config/clients/codex/agents/zaip-agent-frontend.toml +2 -2
  46. package/templates/.agents/config/clients/codex/agents/zaip-agent-task-executor.toml +2 -2
  47. package/templates/.agents/config/clients/codex/agents/zaip-agent-website-developer.toml +2 -2
  48. package/templates/.agents/config/pricing/models.json +9 -5
  49. package/templates/.agents/config/project.yaml +12 -1
  50. package/templates/.agents/dashboard/.zaip-managed.json +375 -95
  51. package/templates/.agents/dashboard/apps/frontend/dist/assets/CostCalculationGuidePage-CGxeQak4.js +1 -0
  52. package/templates/.agents/dashboard/apps/frontend/dist/assets/{DashboardHelpDialog-DaSllaJp.js → DashboardHelpDialog-tzS7YxYB.js} +57 -33
  53. package/templates/.agents/dashboard/apps/frontend/dist/assets/PricingSettingsDialog-DEbCi1gP.js +1 -0
  54. package/templates/.agents/dashboard/apps/frontend/dist/assets/ProjectSettingsPage-1L7Qw-Gq.js +3 -0
  55. package/templates/.agents/dashboard/apps/frontend/dist/assets/ProjectSettingsPage-EInWr641.css +1 -0
  56. package/templates/.agents/dashboard/apps/frontend/dist/assets/TraceabilityWorkspace-t1Om0Abw.js +1 -0
  57. package/templates/.agents/dashboard/apps/frontend/dist/assets/cost-monitor-B6aZfo5f.js +68 -0
  58. package/templates/.agents/dashboard/apps/frontend/dist/assets/{icons-vendor-TPUT2IUt.js → icons-vendor-DiGC-dsJ.js} +1 -1
  59. package/templates/.agents/dashboard/apps/frontend/dist/assets/index-DgTmNHN5.js +2 -0
  60. package/templates/.agents/dashboard/apps/frontend/dist/assets/{react-vendor-Ct48XHGL.js → react-vendor-kPO6yNed.js} +1 -1
  61. package/templates/.agents/dashboard/apps/frontend/dist/assets/rolldown-runtime-Bh1tDfsg.js +1 -0
  62. package/templates/.agents/dashboard/apps/frontend/dist/assets/traceability-DQpKX9c1.js +1 -0
  63. package/templates/.agents/dashboard/apps/frontend/dist/index.html +6 -6
  64. package/templates/.agents/dashboard/apps/frontend/package.json +1 -1
  65. package/templates/.agents/dashboard/apps/frontend/src/App.tsx +3 -1264
  66. package/templates/.agents/dashboard/apps/frontend/src/AppDashboardContent.tsx +363 -0
  67. package/templates/.agents/dashboard/apps/frontend/src/ProjectSettingsPage.dom.test.ts +99 -0
  68. package/templates/.agents/dashboard/apps/frontend/src/ProjectSettingsPage.tsx +62 -6
  69. package/templates/.agents/dashboard/apps/frontend/src/ProjectStackEditor.tsx +3 -1
  70. package/templates/.agents/dashboard/apps/frontend/src/__tests__/app-composition-support.ts +7 -0
  71. package/templates/.agents/dashboard/apps/frontend/src/cost-period-refresh.test.ts +9 -7
  72. package/templates/.agents/dashboard/apps/frontend/src/dashboard-api-settings.ts +151 -0
  73. package/templates/.agents/dashboard/apps/frontend/src/dashboard-api.ts +28 -153
  74. package/templates/.agents/dashboard/apps/frontend/src/dashboard-app-shell.tsx +33 -0
  75. package/templates/.agents/dashboard/apps/frontend/src/dashboard-cost-facts.ts +30 -0
  76. package/templates/.agents/dashboard/apps/frontend/src/dashboard-project-overview.ts +7 -1
  77. package/templates/.agents/dashboard/apps/frontend/src/dashboard-retry.ts +17 -0
  78. package/templates/.agents/dashboard/apps/frontend/src/dashboard-setup-route.ts +29 -0
  79. package/templates/.agents/dashboard/apps/frontend/src/dashboard-view-actions.ts +66 -0
  80. package/templates/.agents/dashboard/apps/frontend/src/operations-period-render.test.ts +3 -2
  81. package/templates/.agents/dashboard/apps/frontend/src/overview-status-page.test.ts +2 -2
  82. package/templates/.agents/dashboard/apps/frontend/src/project-installation-status.test.ts +48 -0
  83. package/templates/.agents/dashboard/apps/frontend/src/project-installation-status.ts +29 -0
  84. package/templates/.agents/dashboard/apps/frontend/src/project-settings-page.css +68 -0
  85. package/templates/.agents/dashboard/apps/frontend/src/project-settings.test.ts +2 -2
  86. package/templates/.agents/dashboard/apps/frontend/src/sao-paulo-day-rollover.test.ts +36 -0
  87. package/templates/.agents/dashboard/apps/frontend/src/sao-paulo-day-rollover.ts +30 -0
  88. package/templates/.agents/dashboard/apps/frontend/src/snapshot-banner-semantics.test.ts +2 -5
  89. package/templates/.agents/dashboard/apps/frontend/src/snapshot-coherence.test.ts +30 -19
  90. package/templates/.agents/dashboard/apps/frontend/src/use-app-effects.ts +9 -1
  91. package/templates/.agents/dashboard/apps/frontend/src/use-app-trace-task-pagination.ts +77 -0
  92. package/templates/.agents/dashboard/apps/frontend/src/use-app-work-board.ts +397 -0
  93. package/templates/.agents/dashboard/apps/frontend/src/use-dashboard-app-model.tsx +782 -0
  94. package/templates/.agents/dashboard/apps/frontend/src/use-snapshot-notice.tsx +49 -0
  95. package/templates/.agents/dashboard/modules/cost-monitor/package.json +1 -1
  96. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostDeveloperPanelSlot.tsx +16 -0
  97. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostGroupsTable.tsx +459 -0
  98. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostMonitorPage.test.ts +27 -17
  99. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostMonitorPage.tsx +62 -186
  100. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostMonitorRunDialog.tsx +20 -0
  101. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostTables-hides-calls-that-have-no-collected.test.ts +80 -0
  102. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostTables-keeps-legacy-codex-identity-fields-blank.test.ts +455 -0
  103. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostTables.test.ts +18 -525
  104. package/templates/.agents/dashboard/modules/cost-monitor/src/react/CostTables.tsx +32 -508
  105. package/templates/.agents/dashboard/modules/cost-monitor/src/react/cost-monitor-export-sources.ts +74 -0
  106. package/templates/.agents/dashboard/modules/cost-monitor/src/react/cost-monitor-page-actions.ts +19 -0
  107. package/templates/.agents/dashboard/modules/cost-monitor/src/react/cost-monitor-page-selectors.ts +49 -0
  108. package/templates/.agents/dashboard/modules/cost-monitor/src/react/cost-table-formatters.ts +115 -0
  109. package/templates/.agents/dashboard/modules/cost-monitor/src/server/build-cost-monitor-projection.test-fixtures.ts +2 -0
  110. package/templates/.agents/dashboard/modules/operations/package.json +1 -1
  111. package/templates/.agents/dashboard/modules/operations/src/contracts.ts +4 -0
  112. package/templates/.agents/dashboard/modules/operations/src/react/EntityTableBadges.tsx +34 -0
  113. package/templates/.agents/dashboard/modules/operations/src/react/EntityTables.tsx +8 -38
  114. package/templates/.agents/dashboard/modules/operations/src/react/OperationsOverview-keeps-structure-classes-mutually-exclusive.test.ts +243 -0
  115. package/templates/.agents/dashboard/modules/operations/src/react/OperationsOverview.test.ts +27 -275
  116. package/templates/.agents/dashboard/modules/operations/src/react/OperationsOverview.tsx +70 -247
  117. package/templates/.agents/dashboard/modules/operations/src/react/OperationsOverviewCharts.tsx +171 -0
  118. package/templates/.agents/dashboard/modules/operations/src/react/OperationsOverviewPanels.tsx +64 -0
  119. package/templates/.agents/dashboard/modules/operations/src/react/OperationsPage.tsx +4 -3
  120. package/templates/.agents/dashboard/modules/operations/src/react/PrdDocumentDialog.test.tsx +2 -1
  121. package/templates/.agents/dashboard/modules/operations/src/react/manual-work-priority.test.ts +2 -1
  122. package/templates/.agents/dashboard/modules/operations/src/react/operations-coverage-toast.test.ts +6 -2
  123. package/templates/.agents/dashboard/modules/operations/src/react/operations-overview-period-filter.test.ts +6 -14
  124. package/templates/.agents/dashboard/modules/operations/src/react/operations-overview.status-cards.test.ts +3 -1
  125. package/templates/.agents/dashboard/modules/operations/src/react/operations-page-contract.ts +2 -0
  126. package/templates/.agents/dashboard/modules/operations/src/react/overview-metric-format.ts +32 -0
  127. package/templates/.agents/dashboard/modules/traceability/package.json +1 -1
  128. package/templates/.agents/dashboard/packages/branding/package.json +1 -1
  129. package/templates/.agents/dashboard/packages/branding/src/index.ts +1 -1
  130. package/templates/.agents/dashboard/packages/contracts/package.json +1 -1
  131. package/templates/.agents/dashboard/packages/data-export/package.json +1 -1
  132. package/templates/.agents/dashboard/packages/local-runtime/dist/server.cjs +1458 -1458
  133. package/templates/.agents/dashboard/packages/local-runtime/dist/sqlite-read-model.cjs +1425 -1425
  134. package/templates/.agents/dashboard/packages/local-runtime/package.json +2 -1
  135. package/templates/.agents/dashboard/packages/local-runtime/scripts/build-sqlite-runtime.mjs +7 -3
  136. package/templates/.agents/dashboard/packages/local-runtime/server.mjs +1458 -1459
  137. package/templates/.agents/dashboard/packages/local-runtime/src/active-codex-telemetry.ts +25 -0
  138. package/templates/.agents/dashboard/packages/local-runtime/src/agent-catalog-settings.test.ts +1 -1
  139. package/templates/.agents/dashboard/packages/local-runtime/src/project-settings.test.ts +53 -8
  140. package/templates/.agents/dashboard/packages/local-runtime/src/project-settings.ts +79 -24
  141. package/templates/.agents/dashboard/packages/local-runtime/src/projection-refresh-diagnostics.ts +35 -0
  142. package/templates/.agents/dashboard/packages/local-runtime/src/server-costs-routes.ts +168 -0
  143. package/templates/.agents/dashboard/packages/local-runtime/src/server-dual-reads-manifest-summaries-without-losing.test.ts +344 -0
  144. package/templates/.agents/dashboard/packages/local-runtime/src/server-http-contracts.ts +31 -0
  145. package/templates/.agents/dashboard/packages/local-runtime/src/server-operations-routes.ts +345 -0
  146. package/templates/.agents/dashboard/packages/local-runtime/src/server-read-projections.ts +461 -0
  147. package/templates/.agents/dashboard/packages/local-runtime/src/server-reconciliation-refresh.test.ts +78 -0
  148. package/templates/.agents/dashboard/packages/local-runtime/src/server-route-context.ts +13 -0
  149. package/templates/.agents/dashboard/packages/local-runtime/src/server-snapshot-synchronization.test.ts +18 -0
  150. package/templates/.agents/dashboard/packages/local-runtime/src/server-snapshot-synchronization.ts +19 -3
  151. package/templates/.agents/dashboard/packages/local-runtime/src/server-snapshot.test.ts +22 -21
  152. package/templates/.agents/dashboard/packages/local-runtime/src/server-sqlite-view-allowlist.test.ts +25 -0
  153. package/templates/.agents/dashboard/packages/local-runtime/src/server-trace-routes.ts +64 -0
  154. package/templates/.agents/dashboard/packages/local-runtime/src/server-work-reconciliation.test.ts +2 -30
  155. package/templates/.agents/dashboard/packages/local-runtime/src/server-work-reconciliation.ts +6 -23
  156. package/templates/.agents/dashboard/packages/local-runtime/src/server.test-fixtures.ts +1 -1
  157. package/templates/.agents/dashboard/packages/local-runtime/src/server.test.ts +2 -330
  158. package/templates/.agents/dashboard/packages/local-runtime/src/server.ts +73 -1060
  159. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-materialize-first-fold.ts +78 -0
  160. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-materialize-projections.ts +561 -0
  161. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-materialize-runs.ts +600 -0
  162. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-metric-contract.test.ts +1 -1
  163. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-agent-dimension.ts +269 -0
  164. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-context.ts +32 -0
  165. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-entity-group.ts +244 -0
  166. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-filtered-dimension.ts +292 -0
  167. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-financial.ts +547 -0
  168. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-model-dimension.ts +360 -0
  169. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-provider-dimension.ts +169 -0
  170. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-summary.ts +475 -0
  171. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-temporal-dimension.ts +132 -0
  172. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-timing.ts +299 -0
  173. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-query-view-page.ts +692 -0
  174. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-counts-every-root-execution-day-returns.test.ts +739 -0
  175. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-does-not-convert-delayed-historical-finalizer.test.ts +706 -0
  176. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-filesystem.ts +748 -0
  177. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-first-fold-types.ts +1 -1
  178. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-keeps-orphan-telemetry-out-kanban-without.test.ts +710 -0
  179. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-marks-token-cost-telemetry-unavailable-when.test.ts +618 -0
  180. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-rebuild.ts +755 -0
  181. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-rebuilds-disposable-snapshot-serves-zaip-records.test.ts +276 -0
  182. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-schema.ts +193 -0
  183. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model-types.ts +123 -0
  184. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model.test.ts +1 -2805
  185. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-read-model.ts +17 -6930
  186. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-schema-base-views.ts +385 -0
  187. package/templates/.agents/dashboard/packages/local-runtime/src/sqlite-schema-dashboard-views.ts +412 -0
  188. package/templates/.agents/dashboard/packages/local-runtime/src/work-priority-contract.test.ts +4 -3
  189. package/templates/.agents/dashboard/packages/local-runtime/start-dashboard-build.mjs +195 -0
  190. package/templates/.agents/dashboard/packages/local-runtime/start-dashboard-lifecycle.test.mjs +2 -7
  191. package/templates/.agents/dashboard/packages/local-runtime/start-dashboard-logging.mjs +35 -0
  192. package/templates/.agents/dashboard/packages/local-runtime/start-dashboard-navigation.mjs +114 -0
  193. package/templates/.agents/dashboard/packages/local-runtime/start-dashboard-paths.mjs +90 -0
  194. package/templates/.agents/dashboard/packages/local-runtime/start-dashboard.mjs +89 -418
  195. package/templates/.agents/dashboard/packages/local-runtime/start-dashboard.test.mjs +22 -12
  196. package/templates/.agents/dashboard/packages/ui/package.json +1 -1
  197. package/templates/.agents/dashboard/tests/e2e/package.json +1 -1
  198. package/templates/.agents/dashboard/tests/e2e/playwright.config.ts +4 -1
  199. package/templates/.agents/design/.zaip-managed.json +5 -5
  200. package/templates/.agents/design/DESIGN.md +2 -186
  201. package/templates/.agents/design/README.md +12 -2
  202. package/templates/.agents/design/apps/catalog/package.json +1 -1
  203. package/templates/.agents/design/packages/local-runtime/package.json +1 -1
  204. package/templates/.agents/growth/strategy.md +3 -0
  205. package/templates/.agents/marketing/strategy.md +3 -0
  206. package/templates/.agents/requirements/autonomous-assurance.md +2 -1
  207. package/templates/.agents/requirements/cloud-hostinger-autonomy.md +1 -1
  208. package/templates/.agents/requirements/comparative-audit.md +1 -1
  209. package/templates/.agents/requirements/context-engine.md +1 -1
  210. package/templates/.agents/requirements/cost-observability.md +1 -1
  211. package/templates/.agents/requirements/dashboard-launch.md +1 -1
  212. package/templates/.agents/requirements/data-export.md +1 -1
  213. package/templates/.agents/requirements/design-system.md +13 -4
  214. package/templates/.agents/requirements/execution-engine.md +1 -1
  215. package/templates/.agents/requirements/kanban-dashboard.md +1 -1
  216. package/templates/.agents/requirements/model-orchestration.md +1 -1
  217. package/templates/.agents/requirements/public-distribution.md +4 -2
  218. package/templates/.agents/requirements/software-architecture.md +1 -1
  219. package/templates/.agents/requirements/strategic-improvements.md +1 -1
  220. package/templates/.agents/requirements/work-lifecycle.md +3 -3
  221. package/templates/.agents/requirements/zaip-strategic-95.md +1 -1
  222. package/templates/.agents/rules/architecture.md +2 -0
  223. package/templates/.agents/rules/astra-diagnostic.md +1 -1
  224. package/templates/.agents/rules/autonomy-decision.md +3 -1
  225. package/templates/.agents/rules/cloud-hostinger-autonomy.md +2 -0
  226. package/templates/.agents/rules/context.md +2 -1
  227. package/templates/.agents/rules/core.md +3 -3
  228. package/templates/.agents/rules/design-system-central-contract.md +17 -0
  229. package/templates/.agents/rules/evolution.md +3 -1
  230. package/templates/.agents/rules/frontend-lite.md +3 -1
  231. package/templates/.agents/rules/local-sqlite-storage.md +2 -2
  232. package/templates/.agents/rules/meta-mcp.md +3 -1
  233. package/templates/.agents/rules/model-orchestration.md +1 -1
  234. package/templates/.agents/rules/run-lifecycle-closure.md +6 -4
  235. package/templates/.agents/rules/security.md +3 -1
  236. package/templates/.agents/rules/specialist-routing.md +3 -1
  237. package/templates/.agents/rules/text-writing-utf8.md +1 -1
  238. package/templates/.agents/rules/workflow.md +7 -5
  239. package/templates/.agents/runtime/VERSION +1 -1
  240. package/templates/.agents/runtime/bin/zaip.mjs +519 -247
  241. package/templates/.agents/runtime/checksums.sha256 +6 -6
  242. package/templates/.agents/runtime/manifest.json +1 -1
  243. package/templates/.agents/runtime/pnpm-lock.sha256 +1 -1
  244. package/templates/.agents/runtime/provenance.json +5 -5
  245. package/templates/.agents/runtime/sbom.cdx.json +1 -1
  246. package/templates/.agents/sales/playbook.md +3 -0
  247. package/templates/.agents/skills/zaip-aios/SKILL.md +5 -3
  248. package/templates/.agents/skills/zaip-skill-adapt-stack/SKILL.md +1 -0
  249. package/templates/.agents/skills/zaip-skill-api-contracts/SKILL.md +14 -0
  250. package/templates/.agents/skills/zaip-skill-api-contracts/agents/openai.yaml +4 -0
  251. package/templates/.agents/skills/zaip-skill-application-observability/SKILL.md +15 -0
  252. package/templates/.agents/skills/zaip-skill-application-observability/agents/openai.yaml +4 -0
  253. package/templates/.agents/skills/zaip-skill-application-observability/references/signal-design.md +3 -0
  254. package/templates/.agents/skills/zaip-skill-application-readiness/SKILL.md +15 -0
  255. package/templates/.agents/skills/zaip-skill-application-readiness/agents/openai.yaml +4 -0
  256. package/templates/.agents/skills/zaip-skill-application-readiness/references/evidence-matrix.md +3 -0
  257. package/templates/.agents/skills/zaip-skill-architecture-design/SKILL.md +14 -0
  258. package/templates/.agents/skills/zaip-skill-architecture-design/agents/openai.yaml +4 -0
  259. package/templates/.agents/skills/zaip-skill-async-jobs/SKILL.md +14 -0
  260. package/templates/.agents/skills/zaip-skill-async-jobs/agents/openai.yaml +4 -0
  261. package/templates/.agents/skills/zaip-skill-auth-tenancy/SKILL.md +15 -0
  262. package/templates/.agents/skills/zaip-skill-auth-tenancy/agents/openai.yaml +4 -0
  263. package/templates/.agents/skills/zaip-skill-auth-tenancy/references/data-access-paths.md +9 -0
  264. package/templates/.agents/skills/zaip-skill-auth-tenancy/references/worker-scenario.md +5 -0
  265. package/templates/.agents/skills/zaip-skill-auth-tenancy/scripts/worker-contract.test.mjs +27 -0
  266. package/templates/.agents/skills/zaip-skill-author-skills/SKILL.md +2 -0
  267. package/templates/.agents/skills/zaip-skill-author-skills/references/engineering-skill-index.md +9 -0
  268. package/templates/.agents/skills/zaip-skill-author-skills/scripts/audit-skill-catalog.mjs +5 -3
  269. package/templates/.agents/skills/zaip-skill-author-skills/scripts/author-skill-scripts.test.mjs +27 -0
  270. package/templates/.agents/skills/zaip-skill-backend/SKILL.md +4 -0
  271. package/templates/.agents/skills/zaip-skill-backend/references/deployment-environments.md +75 -0
  272. package/templates/.agents/skills/zaip-skill-backend/references/supabase-migration-contract.md +23 -0
  273. package/templates/.agents/skills/zaip-skill-brand-governance/SKILL.md +2 -0
  274. package/templates/.agents/skills/zaip-skill-cache-strategy/SKILL.md +14 -0
  275. package/templates/.agents/skills/zaip-skill-cache-strategy/agents/openai.yaml +4 -0
  276. package/templates/.agents/skills/zaip-skill-change/SKILL.md +1 -1
  277. package/templates/.agents/skills/zaip-skill-cloud-engineering/SKILL.md +7 -0
  278. package/templates/.agents/skills/zaip-skill-cloudflare-github-deploy/SKILL.md +37 -0
  279. package/templates/.agents/skills/zaip-skill-cloudflare-github-deploy/agents/openai.yaml +6 -0
  280. package/templates/.agents/skills/zaip-skill-cloudflare-github-deploy/references/workers-static-assets.md +26 -0
  281. package/templates/.agents/skills/zaip-skill-cloudflare-github-deploy/references/workflow-templates.md +71 -0
  282. package/templates/.agents/skills/zaip-skill-cloudflare-github-deploy/scripts/cloudflare-github-deploy-contract.test.mjs +77 -0
  283. package/templates/.agents/skills/zaip-skill-config/SKILL.md +21 -11
  284. package/templates/.agents/skills/zaip-skill-config/agents/openai.yaml +2 -2
  285. package/templates/.agents/skills/zaip-skill-container-runtime/SKILL.md +14 -0
  286. package/templates/.agents/skills/zaip-skill-container-runtime/agents/openai.yaml +4 -0
  287. package/templates/.agents/skills/zaip-skill-context/SKILL.md +6 -1
  288. package/templates/.agents/skills/zaip-skill-context/references/prd-spec-task-handoff.md +10 -0
  289. package/templates/.agents/skills/zaip-skill-context/references/rule-selection.md +20 -0
  290. package/templates/.agents/skills/zaip-skill-cqrs/SKILL.md +1 -0
  291. package/templates/.agents/skills/zaip-skill-cybersecurity/SKILL.md +18 -0
  292. package/templates/.agents/skills/zaip-skill-cybersecurity/agents/openai.yaml +4 -0
  293. package/templates/.agents/skills/zaip-skill-cybersecurity/references/audit-contract.md +94 -0
  294. package/templates/.agents/skills/zaip-skill-cybersecurity/scripts/score-audit.mjs +185 -0
  295. package/templates/.agents/skills/zaip-skill-cybersecurity/scripts/score-audit.test.mjs +101 -0
  296. package/templates/.agents/skills/zaip-skill-data-modeling/SKILL.md +14 -0
  297. package/templates/.agents/skills/zaip-skill-data-modeling/agents/openai.yaml +4 -0
  298. package/templates/.agents/skills/zaip-skill-database-migrations/SKILL.md +20 -0
  299. package/templates/.agents/skills/zaip-skill-database-migrations/agents/openai.yaml +4 -0
  300. package/templates/.agents/skills/zaip-skill-database-migrations/references/manual-mode.md +15 -0
  301. package/templates/.agents/skills/zaip-skill-database-migrations/references/promotion-example.md +5 -0
  302. package/templates/.agents/skills/zaip-skill-database-migrations/scripts/export-manual-sql.mjs +145 -0
  303. package/templates/.agents/skills/zaip-skill-database-migrations/scripts/export-manual-sql.test.mjs +178 -0
  304. package/templates/.agents/skills/zaip-skill-dependency-upgrade/SKILL.md +14 -0
  305. package/templates/.agents/skills/zaip-skill-dependency-upgrade/agents/openai.yaml +4 -0
  306. package/templates/.agents/skills/zaip-skill-deployment-pipeline/SKILL.md +15 -0
  307. package/templates/.agents/skills/zaip-skill-deployment-pipeline/agents/openai.yaml +4 -0
  308. package/templates/.agents/skills/zaip-skill-deployment-pipeline/references/runtime-shapes.md +3 -0
  309. package/templates/.agents/skills/zaip-skill-design-system/SKILL.md +20 -9
  310. package/templates/.agents/skills/zaip-skill-dev-environment/SKILL.md +14 -0
  311. package/templates/.agents/skills/zaip-skill-dev-environment/agents/openai.yaml +4 -0
  312. package/templates/.agents/skills/zaip-skill-dev-environment/references/inventory-contract.md +7 -0
  313. package/templates/.agents/skills/zaip-skill-dev-environment/scripts/inventory.mjs +77 -0
  314. package/templates/.agents/skills/zaip-skill-disaster-recovery/SKILL.md +15 -0
  315. package/templates/.agents/skills/zaip-skill-disaster-recovery/agents/openai.yaml +4 -0
  316. package/templates/.agents/skills/zaip-skill-disaster-recovery/references/recovery-scope.md +3 -0
  317. package/templates/.agents/skills/zaip-skill-execution/SKILL.md +3 -3
  318. package/templates/.agents/skills/zaip-skill-execution/references/correction-cycle.md +7 -0
  319. package/templates/.agents/skills/zaip-skill-execution/references/runtime-governance.md +1 -5
  320. package/templates/.agents/skills/zaip-skill-external-integrations/SKILL.md +14 -0
  321. package/templates/.agents/skills/zaip-skill-external-integrations/agents/openai.yaml +4 -0
  322. package/templates/.agents/skills/zaip-skill-frontend/SKILL.md +4 -0
  323. package/templates/.agents/skills/zaip-skill-frontend-componentization/SKILL.md +44 -0
  324. package/templates/.agents/skills/zaip-skill-frontend-componentization/agents/openai.yaml +4 -0
  325. package/templates/.agents/skills/zaip-skill-frontend-data-flows/SKILL.md +14 -0
  326. package/templates/.agents/skills/zaip-skill-frontend-data-flows/agents/openai.yaml +4 -0
  327. package/templates/.agents/skills/zaip-skill-general-operations/SKILL.md +2 -0
  328. package/templates/.agents/skills/zaip-skill-github-actions-setup/SKILL.md +36 -0
  329. package/templates/.agents/skills/zaip-skill-github-actions-setup/agents/openai.yaml +4 -0
  330. package/templates/.agents/skills/zaip-skill-github-actions-setup/references/cloudflare-pages.md +33 -0
  331. package/templates/.agents/skills/zaip-skill-github-actions-setup/references/github.md +63 -0
  332. package/templates/.agents/skills/zaip-skill-github-actions-setup/references/hostinger-vps.md +55 -0
  333. package/templates/.agents/skills/zaip-skill-github-actions-setup/references/npm.md +30 -0
  334. package/templates/.agents/skills/zaip-skill-github-actions-setup/scripts/check-skill.mjs +55 -0
  335. package/templates/.agents/skills/zaip-skill-github-actions-setup/scripts/verify-cloudflare.mjs +61 -0
  336. package/templates/.agents/skills/zaip-skill-github-actions-setup/scripts/verify-cloudflare.test.mjs +128 -0
  337. package/templates/.agents/skills/zaip-skill-github-actions-setup/scripts/verify-ssh.integration.test.mjs +106 -0
  338. package/templates/.agents/skills/zaip-skill-github-deploy/SKILL.md +43 -76
  339. package/templates/.agents/skills/zaip-skill-github-deploy/references/deployment-workflow.md +131 -0
  340. package/templates/.agents/skills/zaip-skill-github-deploy/references/github-release-workflow-template.md +31 -0
  341. package/templates/.agents/skills/zaip-skill-github-deploy/references/release-gate-matrix.md +24 -0
  342. package/templates/.agents/skills/zaip-skill-github-deploy/references/release-identity.md +44 -0
  343. package/templates/.agents/skills/zaip-skill-github-deploy/references/release-notes.md +134 -0
  344. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/consumer-release-preparation.mjs +389 -0
  345. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/consumer-release-preparation.test.mjs +134 -0
  346. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/github-deploy-contract.test.mjs +25 -2
  347. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/github-readiness-api.test.mjs +134 -0
  348. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/github-readiness.mjs +98 -0
  349. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/github-readiness.test.mjs +25 -0
  350. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/install-github-readiness-workflow.mjs +159 -0
  351. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/install-github-readiness-workflow.test.mjs +160 -0
  352. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/preflight.mjs +149 -12
  353. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/preflight.test.mjs +64 -5
  354. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/release-notes-coverage.test.mjs +126 -0
  355. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/resolve-release-identity.mjs +78 -0
  356. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/resolve-release-identity.test.mjs +51 -0
  357. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/validate-release-notes.mjs +184 -0
  358. package/templates/.agents/skills/zaip-skill-github-deploy/scripts/validate-release-notes.test.mjs +109 -0
  359. package/templates/.agents/skills/zaip-skill-growth-strategy-revops/SKILL.md +2 -0
  360. package/templates/.agents/skills/zaip-skill-hostinger-cloudflare-terraform/SKILL.md +18 -10
  361. package/templates/.agents/skills/zaip-skill-hostinger-cloudflare-terraform/agents/openai.yaml +3 -4
  362. package/templates/.agents/skills/zaip-skill-hostinger-cloudflare-terraform/references/aws-cli-terraform.md +62 -0
  363. package/templates/.agents/skills/zaip-skill-hostinger-cloudflare-terraform/references/guia-iniciante-hostinger-vps.md +183 -0
  364. package/templates/.agents/skills/zaip-skill-marketing/SKILL.md +2 -0
  365. package/templates/.agents/skills/zaip-skill-module-scaffold/SKILL.md +14 -0
  366. package/templates/.agents/skills/zaip-skill-module-scaffold/agents/openai.yaml +4 -0
  367. package/templates/.agents/skills/zaip-skill-module-scaffold/references/scaffold-contract.md +9 -0
  368. package/templates/.agents/skills/zaip-skill-module-scaffold/scripts/helpers.test.mjs +121 -0
  369. package/templates/.agents/skills/zaip-skill-module-scaffold/scripts/scaffold.mjs +169 -0
  370. package/templates/.agents/skills/zaip-skill-move-prd/SKILL.md +1 -1
  371. package/templates/.agents/skills/zaip-skill-move-spec/SKILL.md +1 -1
  372. package/templates/.agents/skills/zaip-skill-performance-engineering/SKILL.md +14 -0
  373. package/templates/.agents/skills/zaip-skill-performance-engineering/agents/openai.yaml +4 -0
  374. package/templates/.agents/skills/zaip-skill-prd/SKILL.md +1 -1
  375. package/templates/.agents/skills/zaip-skill-reconcile-telemetry/scripts/reconcile-local-telemetry.mjs +1 -0
  376. package/templates/.agents/skills/zaip-skill-reconcile-telemetry/scripts/reconcile-telemetry-lib.mjs +26 -9
  377. package/templates/.agents/skills/zaip-skill-reconcile-telemetry/scripts/reconcile-telemetry-lib.test.mjs +39 -13
  378. package/templates/.agents/skills/zaip-skill-reconcile-telemetry/scripts/reconcile-telemetry-remeasurement.mjs +1 -0
  379. package/templates/.agents/skills/zaip-skill-refactor-landing-page/SKILL.md +66 -0
  380. package/templates/.agents/skills/zaip-skill-refactor-landing-page/agents/openai.yaml +4 -0
  381. package/templates/.agents/skills/zaip-skill-refactor-landing-page/references/behavioral-dry-run.md +23 -0
  382. package/templates/.agents/skills/zaip-skill-release-qualification/SKILL.md +40 -1
  383. package/templates/.agents/skills/zaip-skill-release-qualification/references/local-preparation.md +98 -0
  384. package/templates/.agents/skills/zaip-skill-review/SKILL.md +1 -0
  385. package/templates/.agents/skills/zaip-skill-rules/SKILL.md +3 -3
  386. package/templates/.agents/skills/zaip-skill-sales-methodology/SKILL.md +2 -0
  387. package/templates/.agents/skills/zaip-skill-setup/SKILL.md +19 -21
  388. package/templates/.agents/skills/zaip-skill-setup/agents/openai.yaml +2 -2
  389. package/templates/.agents/skills/zaip-skill-spec/SKILL.md +3 -2
  390. package/templates/.agents/skills/zaip-skill-storage-uploads/SKILL.md +14 -0
  391. package/templates/.agents/skills/zaip-skill-storage-uploads/agents/openai.yaml +4 -0
  392. package/templates/.agents/skills/zaip-skill-systematic-debugging/SKILL.md +14 -0
  393. package/templates/.agents/skills/zaip-skill-systematic-debugging/agents/openai.yaml +4 -0
  394. package/templates/.agents/skills/zaip-skill-task/SKILL.md +16 -7
  395. package/templates/.agents/skills/zaip-skill-task/references/commit-policy.md +25 -0
  396. package/templates/.agents/skills/zaip-skill-test-data/SKILL.md +14 -0
  397. package/templates/.agents/skills/zaip-skill-test-data/agents/openai.yaml +4 -0
  398. package/templates/.agents/skills/zaip-skill-test-data/scripts/generate.mjs +72 -0
  399. package/templates/.agents/skills/zaip-skill-testing-strategy/SKILL.md +15 -0
  400. package/templates/.agents/skills/zaip-skill-testing-strategy/agents/openai.yaml +4 -0
  401. package/templates/.agents/skills/zaip-skill-testing-strategy/references/example-map.md +3 -0
  402. package/templates/.agents/skills/zaip-skill-website-readiness/SKILL.md +8 -0
  403. package/templates/.agents/zaip-aios/VERSION +1 -1
  404. package/templates/.agents/zaip-aios/guides/10-seguranca.md +2 -1
  405. package/templates/.agents/zaip-aios/guides/20-instalacao-detalhada.md +5 -3
  406. package/templates/scripts/validate-cloud-hostinger-contract.mjs +34 -0
  407. package/templates/template-manifest.json +955 -211
  408. package/templates/.agents/branding/.gitkeep +0 -0
  409. package/templates/.agents/dashboard/apps/frontend/dist/assets/CostCalculationGuidePage-snUCQYTC.js +0 -1
  410. package/templates/.agents/dashboard/apps/frontend/dist/assets/PricingSettingsDialog-BPtVUGgs.js +0 -1
  411. package/templates/.agents/dashboard/apps/frontend/dist/assets/ProjectSettingsPage-3Awyaovx.js +0 -3
  412. package/templates/.agents/dashboard/apps/frontend/dist/assets/ProjectSettingsPage-GgzpZ_PY.css +0 -1
  413. package/templates/.agents/dashboard/apps/frontend/dist/assets/TraceabilityWorkspace-BCvZFMEf.js +0 -1
  414. package/templates/.agents/dashboard/apps/frontend/dist/assets/cost-monitor-DbaLbU2q.js +0 -68
  415. package/templates/.agents/dashboard/apps/frontend/dist/assets/index-_MEktQNM.js +0 -2
  416. package/templates/.agents/dashboard/apps/frontend/dist/assets/rolldown-runtime-D9-fqq9M.js +0 -1
  417. package/templates/.agents/dashboard/apps/frontend/dist/assets/traceability-BDe4_Ubl.js +0 -1
  418. package/templates/.agents/growth/.gitkeep +0 -0
  419. package/templates/.agents/marketing/.gitkeep +0 -0
  420. package/templates/.agents/sales/.gitkeep +0 -0
@@ -0,0 +1,475 @@
1
+ import type { CostSummarySource } from "@zaip-ai/dashboard-cost-monitor/server";
2
+ import { artifactStructuralRowsSql } from "./sqlite-artifact-counts.js";
3
+ import type { SqliteQueryContext } from "./sqlite-query-context.js";
4
+ import { costMonitorFilterDay } from "./sqlite-read-model-canonical.js";
5
+ import type { SqliteReadModelFileSystem } from "./sqlite-read-model-filesystem.js";
6
+ export function queryCostMonitorSummaryAggregate(
7
+ context: SqliteQueryContext,
8
+ filter: Parameters<SqliteReadModelFileSystem["activityCostRows"]>[0],
9
+ ): CostSummarySource {
10
+ const { from, to, providers = [], models = [] } = filter;
11
+ const sourceFrom = costMonitorFilterDay(from);
12
+ const sourceTo = costMonitorFilterDay(to);
13
+ const cacheKey = JSON.stringify({ ...filter, from: sourceFrom, to: sourceTo });
14
+ const generationCached = context.costMonitorSummaryCache.get(cacheKey);
15
+ if (generationCached !== undefined) return structuredClone(generationCached);
16
+ const remember = (value: CostSummarySource) => {
17
+ if (context.costMonitorSummaryCache.size >= 64) {
18
+ const oldest = context.costMonitorSummaryCache.keys().next().value as string | undefined;
19
+ if (oldest !== undefined) context.costMonitorSummaryCache.delete(oldest);
20
+ }
21
+ context.costMonitorSummaryCache.set(cacheKey, structuredClone(value));
22
+ return structuredClone(value);
23
+ };
24
+ const unfilteredAllTime =
25
+ sourceFrom === null &&
26
+ sourceTo === null &&
27
+ [
28
+ providers,
29
+ models,
30
+ filter.agents,
31
+ filter.phases,
32
+ filter.statuses,
33
+ filter.tasks,
34
+ filter.specs,
35
+ filter.prds,
36
+ filter.executions,
37
+ filter.changes,
38
+ ].every((values) => values === undefined || values.length === 0);
39
+ if (unfilteredAllTime) {
40
+ const cached = context.database
41
+ .prepare(
42
+ "SELECT payload_json FROM zaip_aios_activities WHERE payload_type='cost_monitor_summary_cache' ORDER BY recorded_at DESC LIMIT 1",
43
+ )
44
+ .get() as { payload_json?: string } | undefined;
45
+ if (typeof cached?.payload_json === "string")
46
+ return remember(JSON.parse(cached.payload_json) as CostSummarySource);
47
+ }
48
+ if (
49
+ sourceFrom !== null &&
50
+ sourceTo !== null &&
51
+ [
52
+ providers,
53
+ models,
54
+ filter.agents,
55
+ filter.phases,
56
+ filter.statuses,
57
+ filter.tasks,
58
+ filter.specs,
59
+ filter.prds,
60
+ filter.executions,
61
+ filter.changes,
62
+ ].every((values) => values === undefined || values.length === 0)
63
+ ) {
64
+ const cached = context.database
65
+ .prepare(
66
+ "SELECT payload_json FROM zaip_aios_activities WHERE payload_type='cost_monitor_summary_month_cache' ORDER BY recorded_at DESC LIMIT 1",
67
+ )
68
+ .get() as { payload_json?: string } | undefined;
69
+ if (typeof cached?.payload_json === "string") {
70
+ const payload = JSON.parse(cached.payload_json) as {
71
+ from: string;
72
+ to: string;
73
+ aggregate: CostSummarySource;
74
+ };
75
+ if (payload.from === sourceFrom && payload.to === sourceTo)
76
+ return remember(payload.aggregate);
77
+ }
78
+ }
79
+ const sourceDay =
80
+ "date(COALESCE(a.source_started_at, a.source_occurred_at, a.occurred_at), '-3 hours')";
81
+ const clauses = [
82
+ "(a.total_tokens IS NOT NULL OR a.cost_brl IS NOT NULL OR a.cost_usd IS NOT NULL)",
83
+ `(? IS NULL OR ${sourceDay} >= ?)`,
84
+ `(? IS NULL OR ${sourceDay} <= ?)`,
85
+ ];
86
+ const values: Array<string | number | null> = [sourceFrom, sourceFrom, sourceTo, sourceTo];
87
+ const add = (column: string, items?: readonly string[]) => {
88
+ if (items === undefined || items.length === 0) return;
89
+ clauses.push(`a.${column} IN (${items.map(() => "?").join(",")})`);
90
+ values.push(...items);
91
+ };
92
+ add("zaip_agent", filter.agents);
93
+ add("phase", filter.phases);
94
+ add("status", filter.statuses);
95
+ add("task_id", filter.tasks);
96
+ add("spec_id", filter.specs);
97
+ add("prd_id", filter.prds);
98
+ add("execution_id", filter.executions);
99
+ add("change_id", filter.changes);
100
+ if (providers.length > 0 || models.length > 0) {
101
+ const usageClauses: string[] = [];
102
+ const directClauses: string[] = [];
103
+ const usageValues: string[] = [];
104
+ const directValues: string[] = [];
105
+ if (providers.length > 0) {
106
+ usageClauses.push(
107
+ `COALESCE(u.provider, 'unavailable') IN (${providers.map(() => "?").join(",")})`,
108
+ );
109
+ directClauses.push(
110
+ `COALESCE(a.provider, 'unavailable') IN (${providers.map(() => "?").join(",")})`,
111
+ );
112
+ usageValues.push(...providers);
113
+ directValues.push(...providers);
114
+ }
115
+ if (models.length > 0) {
116
+ usageClauses.push(`COALESCE(u.model, 'unavailable') IN (${models.map(() => "?").join(",")})`);
117
+ directClauses.push(
118
+ `COALESCE(a.model, 'unavailable') IN (${models.map(() => "?").join(",")})`,
119
+ );
120
+ usageValues.push(...models);
121
+ directValues.push(...models);
122
+ }
123
+ clauses.push(
124
+ `((a.agent_run_id IS NULL AND ${directClauses.join(" AND ")}) OR ` +
125
+ `(a.agent_run_id IS NOT NULL AND a.agent_run_id IN (` +
126
+ `SELECT u.agent_invocation_id FROM invocation_usage u WHERE ${usageClauses.join(" AND ")})))`,
127
+ );
128
+ values.push(...directValues, ...usageValues);
129
+ }
130
+ const selected = `WITH selected AS (
131
+ SELECT ${sourceDay} AS day,
132
+ CASE WHEN a.phase='conversation' THEN 'conversation'
133
+ WHEN a.phase='planning' THEN 'planning' ELSE 'execution' END AS phase_key,
134
+ a.task_id, a.model, a.agent_run_id, a.activity_source_kind,
135
+ a.source_event_count AS event_count,
136
+ a.attributed_token_active_ms AS duration_ms,
137
+ a.input_tokens, a.cached_input_tokens, a.output_tokens, a.reasoning_tokens,
138
+ a.total_tokens, a.cost_usd, a.cost_brl
139
+ FROM activities a WHERE ${clauses.join(" AND ")}
140
+ )`;
141
+ const aggregateColumns = `COUNT(*) AS eligible_runs,
142
+ json_group_array(DISTINCT task_id) AS task_ids_json,
143
+ COALESCE(SUM(event_count), 0) AS events,
144
+ SUM(duration_ms) AS duration_ms,
145
+ SUM(input_tokens) AS input_tokens,
146
+ SUM(cached_input_tokens) AS cached_input_tokens,
147
+ SUM(output_tokens) AS output_tokens,
148
+ SUM(reasoning_tokens) AS reasoning_tokens,
149
+ SUM(total_tokens) AS total_tokens,
150
+ SUM(cost_usd) AS cost_usd,
151
+ SUM(cost_brl) AS cost_brl,
152
+ COUNT(duration_ms) AS duration_known,
153
+ COUNT(total_tokens) AS tokens_known,
154
+ COUNT(cost_usd) AS usd_known,
155
+ COUNT(cost_brl) AS brl_known`;
156
+ // SQLite returns at most one aggregate row per day/phase. Node rolls those
157
+ // tiny rows up for the cards and daily table, so the expensive financial
158
+ // population is evaluated once rather than once per dashboard widget.
159
+ const factRows = context.database
160
+ .prepare(`${selected}
161
+ SELECT day, phase_key, ${aggregateColumns}
162
+ FROM selected GROUP BY day, phase_key ORDER BY day, phase_key`)
163
+ .all(...values) as Array<Record<string, unknown>>;
164
+ const nullableColumns = [
165
+ "duration_ms",
166
+ "input_tokens",
167
+ "cached_input_tokens",
168
+ "output_tokens",
169
+ "reasoning_tokens",
170
+ "total_tokens",
171
+ "cost_usd",
172
+ "cost_brl",
173
+ ] as const;
174
+ const additiveColumns = [
175
+ "eligible_runs",
176
+ "events",
177
+ "duration_known",
178
+ "tokens_known",
179
+ "usd_known",
180
+ "brl_known",
181
+ ] as const;
182
+ const rollup = (key: string, rows: readonly Record<string, unknown>[]) => {
183
+ const result: Record<string, unknown> = { key };
184
+ for (const column of additiveColumns)
185
+ result[column] = rows.reduce(
186
+ (sum, row) => sum + (typeof row[column] === "number" ? Number(row[column]) : 0),
187
+ 0,
188
+ );
189
+ for (const column of nullableColumns) {
190
+ const present = rows.filter((row) => typeof row[column] === "number");
191
+ result[column] =
192
+ present.length === 0 ? null : present.reduce((sum, row) => sum + Number(row[column]), 0);
193
+ }
194
+ const taskIds = new Set<string>();
195
+ for (const row of rows) {
196
+ if (typeof row.task_ids_json !== "string") continue;
197
+ for (const value of JSON.parse(row.task_ids_json) as unknown[])
198
+ if (typeof value === "string") taskIds.add(value);
199
+ }
200
+ result.task_count = taskIds.size;
201
+ return result;
202
+ };
203
+ const phaseKeys = ["conversation", "planning", "execution"] as const;
204
+ const phaseRows = phaseKeys.map((key) =>
205
+ rollup(
206
+ key,
207
+ factRows.filter((row) => row.phase_key === key),
208
+ ),
209
+ );
210
+ phaseRows.push(rollup("total", factRows));
211
+ const dailyRows = [...new Set(factRows.map((row) => String(row.day)))].map((day) =>
212
+ rollup(
213
+ day,
214
+ factRows.filter((row) => String(row.day) === day),
215
+ ),
216
+ );
217
+ const structuralRows = context.database
218
+ .prepare(`WITH canonical_ids AS MATERIALIZED (
219
+ SELECT entity_kind AS kind, id FROM canonical_work_items
220
+ ), work_item_days AS (
221
+ SELECT entity_kind AS kind, id, occurred_day_sp AS day FROM canonical_work_items
222
+ UNION ALL
223
+ SELECT 'task', a.task_id, MIN(a.occurred_day_sp) FROM activities a
224
+ LEFT JOIN canonical_ids c ON c.kind='task' AND c.id=a.task_id
225
+ WHERE a.task_id IS NOT NULL AND c.id IS NULL
226
+ GROUP BY a.task_id
227
+ UNION ALL
228
+ SELECT 'spec', a.spec_id, MIN(a.occurred_day_sp) FROM activities a
229
+ LEFT JOIN canonical_ids c ON c.kind='spec' AND c.id=a.spec_id
230
+ WHERE a.spec_id IS NOT NULL AND c.id IS NULL
231
+ GROUP BY a.spec_id
232
+ UNION ALL
233
+ SELECT 'prd', a.prd_id, MIN(a.occurred_day_sp) FROM activities a
234
+ LEFT JOIN canonical_ids c ON c.kind='prd' AND c.id=a.prd_id
235
+ WHERE a.prd_id IS NOT NULL AND c.id IS NULL
236
+ GROUP BY a.prd_id
237
+ ), structural AS (
238
+ SELECT day,
239
+ SUM(CASE WHEN kind='prd' THEN 1 ELSE 0 END) AS prd_count,
240
+ SUM(CASE WHEN kind='spec' THEN 1 ELSE 0 END) AS spec_count,
241
+ SUM(CASE WHEN kind='task' THEN 1 ELSE 0 END) AS task_count,
242
+ 0 AS validation_count, 0 AS evidence_count
243
+ FROM work_item_days
244
+ WHERE day IS NOT NULL
245
+ AND (? IS NULL OR day >= ?)
246
+ AND (? IS NULL OR day <= ?)
247
+ GROUP BY day
248
+ ${artifactStructuralRowsSql()}
249
+ )
250
+ SELECT day,
251
+ SUM(prd_count) AS prd_count,
252
+ SUM(spec_count) AS spec_count,
253
+ SUM(task_count) AS task_count,
254
+ SUM(validation_count) AS validation_count,
255
+ SUM(evidence_count) AS evidence_count
256
+ FROM structural GROUP BY day ORDER BY day`)
257
+ .all(
258
+ sourceFrom,
259
+ sourceFrom,
260
+ sourceTo,
261
+ sourceTo,
262
+ sourceFrom,
263
+ sourceFrom,
264
+ sourceTo,
265
+ sourceTo,
266
+ ) as Array<Record<string, unknown>>;
267
+ const number = (value: unknown) => (typeof value === "number" ? value : 0);
268
+ const nullable = (value: unknown) => (typeof value === "number" ? value : null);
269
+ const metric = (row?: Record<string, unknown>) => {
270
+ const eligible = number(row?.eligible_runs);
271
+ return {
272
+ events: number(row?.events),
273
+ duration_ms: nullable(row?.duration_ms),
274
+ tokens: {
275
+ input: nullable(row?.input_tokens),
276
+ cached_input: nullable(row?.cached_input_tokens),
277
+ output: nullable(row?.output_tokens),
278
+ reasoning: nullable(row?.reasoning_tokens),
279
+ total: nullable(row?.total_tokens),
280
+ },
281
+ cost: { usd: nullable(row?.cost_usd), brl: nullable(row?.cost_brl) },
282
+ coverage: {
283
+ events: number(row?.events),
284
+ tokens: number(row?.tokens_known),
285
+ usd: number(row?.usd_known),
286
+ brl: number(row?.brl_known),
287
+ },
288
+ _duration_population: {
289
+ eligible_runs: eligible,
290
+ known_runs: number(row?.duration_known),
291
+ },
292
+ };
293
+ };
294
+ const byKey = new Map(phaseRows.map((row) => [String(row.key), row]));
295
+ const totalRow = byKey.get("total");
296
+ const stripDurationPopulation = (value: ReturnType<typeof metric>) => {
297
+ const { _duration_population: _ignored, ...publicMetric } = value;
298
+ return publicMetric;
299
+ };
300
+ const conversation = stripDurationPopulation(metric(byKey.get("conversation")));
301
+ const planning = stripDurationPopulation(metric(byKey.get("planning")));
302
+ const execution = stripDurationPopulation(metric(byKey.get("execution")));
303
+ const total = stripDurationPopulation(metric(byKey.get("total")));
304
+ const emptyMetric = stripDurationPopulation(metric());
305
+ const financialByDay = new Map(dailyRows.map((row) => [String(row.key), row]));
306
+ const structuralByDay = new Map(structuralRows.map((row) => [String(row.day), row]));
307
+ const days = [...new Set([...financialByDay.keys(), ...structuralByDay.keys()])].sort();
308
+ const compactDailyRows = days.map((day) => {
309
+ const financial = financialByDay.get(day);
310
+ const structural = structuralByDay.get(day);
311
+ const eligible = number(financial?.eligible_runs);
312
+ const knownDuration = number(financial?.duration_known);
313
+ return {
314
+ key: day,
315
+ prd_count: number(structural?.prd_count),
316
+ spec_count: number(structural?.spec_count),
317
+ task_count: number(structural?.task_count),
318
+ validation_count: number(structural?.validation_count),
319
+ evidence_count: number(structural?.evidence_count),
320
+ total_run_count: eligible,
321
+ total_duration: nullable(financial?.duration_ms),
322
+ duration_known: number(financial?.duration_ms),
323
+ duration_unknown: Math.max(0, eligible - knownDuration),
324
+ total_tokens: nullable(financial?.total_tokens),
325
+ total_cost_usd: nullable(financial?.cost_usd),
326
+ total_cost_brl: nullable(financial?.cost_brl),
327
+ source_event_count: number(financial?.events),
328
+ };
329
+ });
330
+ const periods = Object.fromEntries(
331
+ compactDailyRows.map((row) => {
332
+ const financial = financialByDay.get(row.key);
333
+ const eligible = number(financial?.eligible_runs);
334
+ const tokensKnown = number(financial?.tokens_known);
335
+ const costKnown = number(financial?.brl_known);
336
+ return [
337
+ row.key,
338
+ {
339
+ prds: row.prd_count,
340
+ specs: row.spec_count,
341
+ tasks: row.task_count,
342
+ validations: row.validation_count,
343
+ evidences: row.evidence_count,
344
+ runs: eligible,
345
+ usage: {
346
+ total_tokens: {
347
+ known_sum: number(financial?.total_tokens),
348
+ unknown: eligible - tokensKnown,
349
+ complete: tokensKnown === eligible,
350
+ coverage_ratio: eligible === 0 ? 1 : tokensKnown / eligible,
351
+ },
352
+ cost: {
353
+ known_sum: number(financial?.cost_brl),
354
+ unknown: eligible - costKnown,
355
+ complete: costKnown === eligible,
356
+ coverage_ratio: eligible === 0 ? 1 : costKnown / eligible,
357
+ currency: "BRL" as const,
358
+ },
359
+ },
360
+ events: {
361
+ known_sum: number(financial?.events),
362
+ unknown: 0,
363
+ complete: true,
364
+ coverage_ratio: 1,
365
+ },
366
+ },
367
+ ];
368
+ }),
369
+ );
370
+ const durations = Object.fromEntries(
371
+ dailyRows.map((row) => {
372
+ const value = metric(row);
373
+ return [
374
+ String(row.key),
375
+ {
376
+ known_sum: number(row.duration_ms),
377
+ eligible_runs: value._duration_population.eligible_runs,
378
+ known_runs: value._duration_population.known_runs,
379
+ },
380
+ ];
381
+ }),
382
+ );
383
+ // Build models from the same selected financial population as the cards,
384
+ // phases and daily rows. Calling the paginated view here was tempting but
385
+ // silently loses multi-value and lineage filters (Task/SPEC/PRD/etc.).
386
+ // This remains one bounded SQL aggregate, without transporting Runs.
387
+ const summaryModelRows = context.database
388
+ .prepare(`${selected},
389
+ segment_totals AS (
390
+ SELECT agent_invocation_id, SUM(total_tokens) AS total_tokens,
391
+ SUM(cost_usd) AS cost_usd, SUM(cost_brl) AS cost_brl
392
+ FROM cost_invocation_dimension_facts
393
+ WHERE total_tokens IS NOT NULL OR cost_usd IS NOT NULL OR cost_brl IS NOT NULL
394
+ GROUP BY agent_invocation_id
395
+ ), components AS (
396
+ -- Agent facts can contain several observed models. Attribute their
397
+ -- financial components to the model segment, never to a mixed parent.
398
+ SELECT CASE WHEN f.model IS NULL OR trim(f.model) = '' OR lower(f.model) IN ('unavailable', 'unknown', 'mixed')
399
+ THEN 'unavailable' ELSE f.model END AS key,
400
+ SUM(f.total_tokens) AS total_tokens, SUM(f.cost_usd) AS total_cost_usd,
401
+ SUM(f.cost_brl) AS total_cost_brl
402
+ FROM selected s JOIN cost_invocation_dimension_facts f
403
+ ON f.agent_invocation_id=s.agent_run_id
404
+ WHERE s.agent_run_id IS NOT NULL
405
+ GROUP BY key
406
+ UNION ALL
407
+ -- Non-agent financial facts have no invocation segments, so their
408
+ -- observed model remains the only factual dimension.
409
+ SELECT CASE WHEN model IS NULL OR trim(model) = '' OR lower(model) IN ('unavailable', 'unknown', 'mixed')
410
+ THEN 'unavailable' ELSE model END AS key,
411
+ SUM(total_tokens), SUM(cost_usd), SUM(cost_brl)
412
+ FROM selected WHERE agent_run_id IS NULL GROUP BY key
413
+ UNION ALL
414
+ -- Preserve any consolidated invocation remainder as Sem dimensão so
415
+ -- the model chart reconciles to the same financial population.
416
+ SELECT 'unavailable' AS key,
417
+ SUM(s.total_tokens - COALESCE(st.total_tokens, 0)),
418
+ SUM(s.cost_usd - COALESCE(st.cost_usd, 0)),
419
+ SUM(s.cost_brl - COALESCE(st.cost_brl, 0))
420
+ FROM selected s LEFT JOIN segment_totals st ON st.agent_invocation_id=s.agent_run_id
421
+ WHERE s.agent_run_id IS NOT NULL
422
+ AND (s.total_tokens <> COALESCE(st.total_tokens, 0)
423
+ OR s.cost_usd <> COALESCE(st.cost_usd, 0)
424
+ OR s.cost_brl <> COALESCE(st.cost_brl, 0))
425
+ )
426
+ SELECT key, 0 AS invocation_count, NULL AS task_count,
427
+ NULL AS total_duration, 0 AS duration_unknown, 0 AS duration_known,
428
+ 0 AS source_event_count, SUM(total_tokens) AS total_tokens,
429
+ SUM(total_cost_usd) AS total_cost_usd, SUM(total_cost_brl) AS total_cost_brl
430
+ FROM components GROUP BY key ORDER BY total_cost_brl DESC, key`)
431
+ .all(...values) as Array<Record<string, unknown>>;
432
+ const model_groups = summaryModelRows.map((row) => {
433
+ const duration = nullable(row.total_duration);
434
+ const durationUnknown = number(row.duration_unknown);
435
+ const durationKnown = nullable(row.duration_known) ?? duration ?? 0;
436
+ return {
437
+ key: String(row.key ?? "unavailable"),
438
+ label: String(row.label ?? row.key ?? "unavailable"),
439
+ latest_started_at: "",
440
+ run_count: number(row.invocation_count),
441
+ task_count: nullable(row.task_count),
442
+ prd_count: 0,
443
+ spec_count: 0,
444
+ evidence_count: 0,
445
+ test_count: 0,
446
+ event_count: number(row.source_event_count),
447
+ total_tokens: nullable(row.total_tokens),
448
+ estimated_usd: nullable(row.total_cost_usd),
449
+ estimated_brl: nullable(row.total_cost_brl),
450
+ duration_ms: duration,
451
+ duration: {
452
+ known_sum: durationKnown,
453
+ unknown: durationUnknown,
454
+ complete: durationUnknown === 0,
455
+ coverage_ratio: durationUnknown === 0 ? 1 : 0,
456
+ },
457
+ coverage: { events: 0, tokens: 0, usd: 0, brl: 0 },
458
+ };
459
+ });
460
+ return remember({
461
+ periods: { daily: periods },
462
+ aggregate_usage: {
463
+ conversation,
464
+ planning,
465
+ execution,
466
+ total,
467
+ new_work: total,
468
+ rework: emptyMetric,
469
+ },
470
+ task_count: number(totalRow?.task_count),
471
+ model_groups,
472
+ cost_monitor_daily_duration: durations,
473
+ daily_rows: compactDailyRows,
474
+ });
475
+ }
@@ -0,0 +1,132 @@
1
+ import type { SqliteQueryContext, SqliteViewInput } from "./sqlite-query-context.js";
2
+ import { costMonitorFilterDay } from "./sqlite-read-model-canonical.js";
3
+ import type { SqliteViewName } from "./sqlite-read-model-types.js";
4
+ export function readTemporalDimension(
5
+ context: SqliteQueryContext,
6
+ view: SqliteViewName,
7
+ input: SqliteViewInput,
8
+ filteredCostGroup: "day" | "month" | "year",
9
+ ) {
10
+ const from = costMonitorFilterDay(input.from);
11
+ const to = costMonitorFilterDay(input.to);
12
+ const aggregate = context.costMonitorSummaryAggregate({ from, to });
13
+ const financialRows = aggregate.daily_rows as Array<Record<string, unknown>>;
14
+ const financialByDay = new Map(financialRows.map((row) => [String(row.key), row]));
15
+ const days = [...financialByDay.keys()].sort();
16
+ const grouped = new Map<string, Record<string, unknown>>();
17
+ const additive = [
18
+ // These fields exist only in the unfiltered temporal daily cache. Do
19
+ // not add them to the dimensional invocation grouping below: its
20
+ // source cannot establish canonical lifecycle attribution.
21
+ "prd_count",
22
+ "spec_count",
23
+ "task_count",
24
+ "validation_count",
25
+ "evidence_count",
26
+ "total_run_count",
27
+ "invocation_count",
28
+ "cost_monitor_invocation_count",
29
+ "llm_call_count",
30
+ "source_event_count",
31
+ "total_tokens",
32
+ "total_cost_usd",
33
+ "total_cost_brl",
34
+ ] as const;
35
+ for (const day of days) {
36
+ const key =
37
+ filteredCostGroup === "day"
38
+ ? day
39
+ : filteredCostGroup === "month"
40
+ ? day.slice(0, 7)
41
+ : day.slice(0, 4);
42
+ const target = grouped.get(key) ?? {
43
+ key,
44
+ label: key,
45
+ period_key: key,
46
+ start_day: day,
47
+ end_day: day,
48
+ total_duration: null,
49
+ duration_incomplete: false,
50
+ duration_provenance: "unavailable",
51
+ estimation_basis: null,
52
+ };
53
+ target.start_day = String(target.start_day) < day ? target.start_day : day;
54
+ target.end_day = String(target.end_day) > day ? target.end_day : day;
55
+ const financial = financialByDay.get(day);
56
+ for (const column of additive) {
57
+ const value = financial?.[column];
58
+ if (typeof value === "number")
59
+ target[column] = (typeof target[column] === "number" ? Number(target[column]) : 0) + value;
60
+ }
61
+ const duration = financial?.total_duration;
62
+ if (typeof duration === "number") {
63
+ target.total_duration =
64
+ (typeof target.total_duration === "number" ? Number(target.total_duration) : 0) + duration;
65
+ }
66
+ if (typeof financial?.duration_unknown === "number" && financial.duration_unknown > 0)
67
+ target.duration_incomplete = true;
68
+ grouped.set(key, target);
69
+ }
70
+ const query = input.query?.toLocaleLowerCase() ?? null;
71
+ const allRows = [...grouped.values()]
72
+ .map<Record<string, unknown>>((row) => {
73
+ // Coverage remains explicit on the row; it must not erase a
74
+ // calculable shared temporal total from the visible Cost table.
75
+ const factual = typeof row.total_duration === "number" ? row.total_duration : null;
76
+ const durationUnknown = row.duration_incomplete === true ? 1 : 0;
77
+ return {
78
+ // Daily first fold is an allowlisted table contract. Do not leak
79
+ // cache fields, technical payload columns, or unused aggregates.
80
+ key: String(row.key),
81
+ label: String(row.label),
82
+ prd_count: Number(row.prd_count ?? 0),
83
+ spec_count: Number(row.spec_count ?? 0),
84
+ task_count: Number(row.task_count ?? 0),
85
+ total_run_count: Number(row.total_run_count ?? 0),
86
+ validation_count: Number(row.validation_count ?? 0),
87
+ evidence_count: Number(row.evidence_count ?? 0),
88
+ total_duration: factual,
89
+ duration_known: factual ?? 0,
90
+ duration_unknown: durationUnknown,
91
+ total_tokens: typeof row.total_tokens === "number" ? row.total_tokens : null,
92
+ total_cost_usd: typeof row.total_cost_usd === "number" ? row.total_cost_usd : null,
93
+ total_cost_brl: typeof row.total_cost_brl === "number" ? row.total_cost_brl : null,
94
+ };
95
+ })
96
+ .filter((row) => query === null || String(row.key).toLocaleLowerCase().includes(query))
97
+ .sort((left, right) => String(right.key).localeCompare(String(left.key)));
98
+ const totals = allRows.reduce<Record<string, number | null>>((result, row) => {
99
+ for (const key of [
100
+ "prd_count",
101
+ "spec_count",
102
+ "task_count",
103
+ "total_run_count",
104
+ "validation_count",
105
+ "evidence_count",
106
+ "total_duration",
107
+ "duration_known",
108
+ "duration_unknown",
109
+ "total_tokens",
110
+ "total_cost_usd",
111
+ "total_cost_brl",
112
+ ] as const) {
113
+ const value = row[key];
114
+ if (typeof value === "number") result[key] = (result[key] ?? 0) + value;
115
+ else if (!(key in result)) result[key] = null;
116
+ }
117
+ return result;
118
+ }, {});
119
+ const limit = Math.min(200, Math.max(1, input.limit ?? 50));
120
+ const offset = Math.max(0, input.offset ?? 0);
121
+ const rows = allRows.slice(offset, offset + limit);
122
+ return {
123
+ data_source: "sqlite_read_model" as const,
124
+ view,
125
+ offset,
126
+ limit,
127
+ total_count: allRows.length,
128
+ has_more: offset + rows.length < allRows.length,
129
+ totals,
130
+ rows,
131
+ };
132
+ }