@bonesofspring/ai-rules 0.2.86 → 0.2.87

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 (425) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/bin/lib/engine-scripts.js +4 -0
  3. package/bin/lib/seed-ai-local-team.js +39 -2
  4. package/bin/opencode-install.js +3 -2
  5. package/docs/OPENCODE-COMPATIBILITY.md +6 -4
  6. package/docs/OPENCODE.md +8 -5
  7. package/docs/PRESET-CONTRIBUTION.md +6 -6
  8. package/docs/TOKEN-ECONOMY.md +20 -0
  9. package/package.json +1 -1
  10. package/presets/_shared/ai-local/scripts/report-step-cost.mjs +544 -0
  11. package/presets/_shared/assets/model-tiers.json +6 -0
  12. package/presets/_shared/core/agent-team/agent-team-orchestrator.md +3 -3
  13. package/presets/_shared/hooks/guard-shell-command.mjs.tmpl +125 -105
  14. package/presets/claude/android-kotlin/hooks/guard-shell-command.mjs +125 -105
  15. package/presets/claude/android-kotlin/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  16. package/presets/claude/go/hooks/guard-shell-command.mjs +125 -105
  17. package/presets/claude/go/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  18. package/presets/claude/ios-swift/hooks/guard-shell-command.mjs +125 -105
  19. package/presets/claude/ios-swift/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  20. package/presets/claude/java/hooks/guard-shell-command.mjs +125 -105
  21. package/presets/claude/java/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  22. package/presets/claude/mcp-ts/hooks/guard-shell-command.mjs +125 -105
  23. package/presets/claude/mcp-ts/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  24. package/presets/claude/mol/hooks/guard-shell-command.mjs +125 -105
  25. package/presets/claude/mol/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  26. package/presets/claude/next/hooks/guard-shell-command.mjs +125 -105
  27. package/presets/claude/next/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  28. package/presets/claude/nuxt/hooks/guard-shell-command.mjs +125 -105
  29. package/presets/claude/nuxt/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  30. package/presets/claude/php-hexagonal/hooks/guard-shell-command.mjs +125 -105
  31. package/presets/claude/php-hexagonal/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  32. package/presets/claude/php-laravel/hooks/guard-shell-command.mjs +125 -105
  33. package/presets/claude/php-laravel/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  34. package/presets/claude/svelte/hooks/guard-shell-command.mjs +125 -105
  35. package/presets/claude/svelte/rules/tooling-and-review/agent-team-orchestrator.md +3 -3
  36. package/presets/cursor/android-kotlin/hooks/guard-shell-command.mjs +125 -105
  37. package/presets/cursor/android-kotlin/rules/agent-team-orchestrator.mdc +3 -3
  38. package/presets/cursor/go/hooks/guard-shell-command.mjs +125 -105
  39. package/presets/cursor/go/rules/agent-team-orchestrator.mdc +3 -3
  40. package/presets/cursor/ios-swift/hooks/guard-shell-command.mjs +125 -105
  41. package/presets/cursor/ios-swift/rules/agent-team-orchestrator.mdc +3 -3
  42. package/presets/cursor/java/hooks/guard-shell-command.mjs +125 -105
  43. package/presets/cursor/java/rules/agent-team-orchestrator.mdc +3 -3
  44. package/presets/cursor/mcp-ts/hooks/guard-shell-command.mjs +125 -105
  45. package/presets/cursor/mcp-ts/rules/agent-team-orchestrator.mdc +3 -3
  46. package/presets/cursor/mol/hooks/guard-shell-command.mjs +125 -105
  47. package/presets/cursor/mol/rules/agent-team-orchestrator.mdc +3 -3
  48. package/presets/cursor/next/hooks/guard-shell-command.mjs +125 -105
  49. package/presets/cursor/next/rules/agent-team-orchestrator.mdc +3 -3
  50. package/presets/cursor/nuxt/hooks/guard-shell-command.mjs +125 -105
  51. package/presets/cursor/nuxt/rules/agent-team-orchestrator.mdc +3 -3
  52. package/presets/cursor/php-hexagonal/hooks/guard-shell-command.mjs +125 -105
  53. package/presets/cursor/php-hexagonal/rules/agent-team-orchestrator.mdc +3 -3
  54. package/presets/cursor/php-laravel/hooks/guard-shell-command.mjs +125 -105
  55. package/presets/cursor/php-laravel/rules/agent-team-orchestrator.mdc +3 -3
  56. package/presets/cursor/svelte/hooks/guard-shell-command.mjs +125 -105
  57. package/presets/cursor/svelte/rules/agent-team-orchestrator.mdc +3 -3
  58. package/presets/mavis/android-kotlin/adapter-map.json +1 -1
  59. package/presets/mavis/android-kotlin/rules/agent-team-orchestrator.md +3 -3
  60. package/presets/mavis/go/adapter-map.json +1 -1
  61. package/presets/mavis/go/rules/agent-team-orchestrator.md +3 -3
  62. package/presets/mavis/ios-swift/adapter-map.json +1 -1
  63. package/presets/mavis/ios-swift/rules/agent-team-orchestrator.md +3 -3
  64. package/presets/mavis/java/adapter-map.json +1 -1
  65. package/presets/mavis/java/rules/agent-team-orchestrator.md +3 -3
  66. package/presets/mavis/mcp-ts/adapter-map.json +1 -1
  67. package/presets/mavis/mcp-ts/rules/agent-team-orchestrator.md +3 -3
  68. package/presets/mavis/mol/adapter-map.json +1 -1
  69. package/presets/mavis/mol/rules/agent-team-orchestrator.md +3 -3
  70. package/presets/mavis/next/adapter-map.json +1 -1
  71. package/presets/mavis/next/rules/agent-team-orchestrator.md +3 -3
  72. package/presets/mavis/nuxt/adapter-map.json +1 -1
  73. package/presets/mavis/nuxt/rules/agent-team-orchestrator.md +3 -3
  74. package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
  75. package/presets/mavis/php-hexagonal/rules/agent-team-orchestrator.md +3 -3
  76. package/presets/mavis/php-laravel/adapter-map.json +1 -1
  77. package/presets/mavis/php-laravel/rules/agent-team-orchestrator.md +3 -3
  78. package/presets/mavis/svelte/adapter-map.json +1 -1
  79. package/presets/mavis/svelte/rules/agent-team-orchestrator.md +3 -3
  80. package/presets/opencode/android-kotlin/OPENCODE.md +5 -66
  81. package/presets/opencode/android-kotlin/adapter-map.json +42 -42
  82. package/presets/opencode/android-kotlin/agents/accessibility-reviewer.md +1 -1
  83. package/presets/opencode/android-kotlin/agents/api-contract-reviewer.md +1 -1
  84. package/presets/opencode/android-kotlin/agents/build-verifier.md +1 -1
  85. package/presets/opencode/android-kotlin/agents/ci-investigator.md +1 -1
  86. package/presets/opencode/android-kotlin/agents/code-reviewer.md +1 -1
  87. package/presets/opencode/android-kotlin/agents/codebase-analyzer.md +1 -1
  88. package/presets/opencode/android-kotlin/agents/debugger.md +1 -1
  89. package/presets/opencode/android-kotlin/agents/feature-developer.md +1 -1
  90. package/presets/opencode/android-kotlin/agents/instrumentation-test-generator.md +1 -1
  91. package/presets/opencode/android-kotlin/agents/instrumentation-test-healer.md +1 -1
  92. package/presets/opencode/android-kotlin/agents/instrumentation-test-planner.md +1 -1
  93. package/presets/opencode/android-kotlin/agents/migration-specialist.md +1 -1
  94. package/presets/opencode/android-kotlin/agents/mockup-reviewer.md +1 -1
  95. package/presets/opencode/android-kotlin/agents/openspec-author.md +1 -1
  96. package/presets/opencode/android-kotlin/agents/performance-auditor.md +1 -1
  97. package/presets/opencode/android-kotlin/agents/qa-tester.md +1 -1
  98. package/presets/opencode/android-kotlin/agents/security-reviewer.md +1 -1
  99. package/presets/opencode/android-kotlin/agents/solution-architect.md +1 -1
  100. package/presets/opencode/android-kotlin/agents/task-analyst.md +1 -1
  101. package/presets/opencode/android-kotlin/agents/task-router.md +1 -1
  102. package/presets/opencode/android-kotlin/agents/tech-writer.md +1 -1
  103. package/presets/opencode/android-kotlin/agents/unit-test-generator.md +1 -1
  104. package/presets/opencode/android-kotlin/agents/unit-test-healer.md +1 -1
  105. package/presets/opencode/android-kotlin/agents/unit-test-planner.md +1 -1
  106. package/presets/opencode/android-kotlin/commands/task-continue.md +1 -1
  107. package/presets/opencode/android-kotlin/hooks/README.md +11 -1
  108. package/presets/opencode/android-kotlin/hooks/guard-shell-command.mjs +155 -0
  109. package/presets/opencode/android-kotlin/plugins/rule-attach/globs.json +167 -0
  110. package/presets/opencode/android-kotlin/rules/agent-team-orchestrator.md +5 -5
  111. package/presets/opencode/go/OPENCODE.md +5 -51
  112. package/presets/opencode/go/adapter-map.json +45 -45
  113. package/presets/opencode/go/agents/api-contract-reviewer.md +1 -1
  114. package/presets/opencode/go/agents/build-verifier.md +1 -1
  115. package/presets/opencode/go/agents/ci-investigator.md +1 -1
  116. package/presets/opencode/go/agents/code-reviewer.md +1 -1
  117. package/presets/opencode/go/agents/codebase-analyzer.md +1 -1
  118. package/presets/opencode/go/agents/debugger.md +1 -1
  119. package/presets/opencode/go/agents/feature-developer.md +1 -1
  120. package/presets/opencode/go/agents/integration-test-generator.md +1 -1
  121. package/presets/opencode/go/agents/integration-test-healer.md +1 -1
  122. package/presets/opencode/go/agents/integration-test-planner.md +1 -1
  123. package/presets/opencode/go/agents/migration-specialist.md +1 -1
  124. package/presets/opencode/go/agents/openspec-author.md +1 -1
  125. package/presets/opencode/go/agents/performance-auditor.md +1 -1
  126. package/presets/opencode/go/agents/qa-tester.md +1 -1
  127. package/presets/opencode/go/agents/security-reviewer.md +1 -1
  128. package/presets/opencode/go/agents/solution-architect.md +1 -1
  129. package/presets/opencode/go/agents/task-analyst.md +1 -1
  130. package/presets/opencode/go/agents/task-router.md +1 -1
  131. package/presets/opencode/go/agents/tech-writer.md +1 -1
  132. package/presets/opencode/go/agents/unit-test-generator.md +1 -1
  133. package/presets/opencode/go/agents/unit-test-healer.md +1 -1
  134. package/presets/opencode/go/agents/unit-test-planner.md +1 -1
  135. package/presets/opencode/go/commands/task-continue.md +1 -1
  136. package/presets/opencode/go/hooks/README.md +11 -1
  137. package/presets/opencode/go/hooks/guard-shell-command.mjs +154 -0
  138. package/presets/opencode/go/plugins/rule-attach/globs.json +198 -0
  139. package/presets/opencode/go/rules/agent-team-orchestrator.md +5 -5
  140. package/presets/opencode/ios-swift/OPENCODE.md +5 -66
  141. package/presets/opencode/ios-swift/adapter-map.json +41 -41
  142. package/presets/opencode/ios-swift/agents/accessibility-reviewer.md +1 -1
  143. package/presets/opencode/ios-swift/agents/api-contract-reviewer.md +1 -1
  144. package/presets/opencode/ios-swift/agents/build-verifier.md +1 -1
  145. package/presets/opencode/ios-swift/agents/ci-investigator.md +1 -1
  146. package/presets/opencode/ios-swift/agents/code-reviewer.md +1 -1
  147. package/presets/opencode/ios-swift/agents/codebase-analyzer.md +1 -1
  148. package/presets/opencode/ios-swift/agents/debugger.md +1 -1
  149. package/presets/opencode/ios-swift/agents/feature-developer.md +1 -1
  150. package/presets/opencode/ios-swift/agents/migration-specialist.md +1 -1
  151. package/presets/opencode/ios-swift/agents/mockup-reviewer.md +1 -1
  152. package/presets/opencode/ios-swift/agents/openspec-author.md +1 -1
  153. package/presets/opencode/ios-swift/agents/performance-auditor.md +1 -1
  154. package/presets/opencode/ios-swift/agents/qa-tester.md +1 -1
  155. package/presets/opencode/ios-swift/agents/security-reviewer.md +1 -1
  156. package/presets/opencode/ios-swift/agents/solution-architect.md +1 -1
  157. package/presets/opencode/ios-swift/agents/task-analyst.md +1 -1
  158. package/presets/opencode/ios-swift/agents/task-router.md +1 -1
  159. package/presets/opencode/ios-swift/agents/tech-writer.md +1 -1
  160. package/presets/opencode/ios-swift/agents/unit-test-generator.md +1 -1
  161. package/presets/opencode/ios-swift/agents/unit-test-healer.md +1 -1
  162. package/presets/opencode/ios-swift/agents/unit-test-planner.md +1 -1
  163. package/presets/opencode/ios-swift/agents/xcuitest-test-generator.md +1 -1
  164. package/presets/opencode/ios-swift/agents/xcuitest-test-healer.md +1 -1
  165. package/presets/opencode/ios-swift/agents/xcuitest-test-planner.md +1 -1
  166. package/presets/opencode/ios-swift/commands/task-continue.md +1 -1
  167. package/presets/opencode/ios-swift/hooks/README.md +11 -1
  168. package/presets/opencode/ios-swift/hooks/guard-shell-command.mjs +154 -0
  169. package/presets/opencode/ios-swift/plugins/rule-attach/globs.json +160 -0
  170. package/presets/opencode/ios-swift/rules/agent-team-orchestrator.md +5 -5
  171. package/presets/opencode/java/OPENCODE.md +5 -58
  172. package/presets/opencode/java/adapter-map.json +45 -45
  173. package/presets/opencode/java/agents/api-contract-reviewer.md +1 -1
  174. package/presets/opencode/java/agents/build-verifier.md +1 -1
  175. package/presets/opencode/java/agents/ci-investigator.md +1 -1
  176. package/presets/opencode/java/agents/code-reviewer.md +1 -1
  177. package/presets/opencode/java/agents/codebase-analyzer.md +1 -1
  178. package/presets/opencode/java/agents/debugger.md +1 -1
  179. package/presets/opencode/java/agents/feature-developer.md +1 -1
  180. package/presets/opencode/java/agents/integration-test-generator.md +1 -1
  181. package/presets/opencode/java/agents/integration-test-healer.md +1 -1
  182. package/presets/opencode/java/agents/integration-test-planner.md +1 -1
  183. package/presets/opencode/java/agents/migration-specialist.md +1 -1
  184. package/presets/opencode/java/agents/openspec-author.md +1 -1
  185. package/presets/opencode/java/agents/performance-auditor.md +1 -1
  186. package/presets/opencode/java/agents/qa-tester.md +1 -1
  187. package/presets/opencode/java/agents/security-reviewer.md +1 -1
  188. package/presets/opencode/java/agents/solution-architect.md +1 -1
  189. package/presets/opencode/java/agents/task-analyst.md +1 -1
  190. package/presets/opencode/java/agents/task-router.md +1 -1
  191. package/presets/opencode/java/agents/tech-writer.md +1 -1
  192. package/presets/opencode/java/agents/unit-test-generator.md +1 -1
  193. package/presets/opencode/java/agents/unit-test-healer.md +1 -1
  194. package/presets/opencode/java/agents/unit-test-planner.md +1 -1
  195. package/presets/opencode/java/commands/task-continue.md +1 -1
  196. package/presets/opencode/java/hooks/README.md +11 -1
  197. package/presets/opencode/java/hooks/guard-shell-command.mjs +155 -0
  198. package/presets/opencode/java/plugins/rule-attach/globs.json +230 -0
  199. package/presets/opencode/java/rules/agent-team-orchestrator.md +5 -5
  200. package/presets/opencode/mcp-ts/OPENCODE.md +5 -48
  201. package/presets/opencode/mcp-ts/adapter-map.json +36 -36
  202. package/presets/opencode/mcp-ts/agents/build-verifier.md +1 -1
  203. package/presets/opencode/mcp-ts/agents/feature-developer.md +1 -1
  204. package/presets/opencode/mcp-ts/agents/security-reviewer.md +1 -1
  205. package/presets/opencode/mcp-ts/agents/solution-architect.md +1 -1
  206. package/presets/opencode/mcp-ts/agents/task-analyst.md +1 -1
  207. package/presets/opencode/mcp-ts/agents/task-router.md +1 -1
  208. package/presets/opencode/mcp-ts/commands/task-continue.md +1 -1
  209. package/presets/opencode/mcp-ts/hooks/README.md +11 -1
  210. package/presets/opencode/mcp-ts/hooks/guard-shell-command.mjs +154 -0
  211. package/presets/opencode/mcp-ts/plugins/rule-attach/globs.json +151 -0
  212. package/presets/opencode/mcp-ts/rules/agent-team-orchestrator.md +5 -5
  213. package/presets/opencode/mol/OPENCODE.md +5 -56
  214. package/presets/opencode/mol/adapter-map.json +31 -31
  215. package/presets/opencode/mol/agents/accessibility-reviewer.md +1 -1
  216. package/presets/opencode/mol/agents/api-contract-reviewer.md +1 -1
  217. package/presets/opencode/mol/agents/build-verifier.md +1 -1
  218. package/presets/opencode/mol/agents/ci-investigator.md +1 -1
  219. package/presets/opencode/mol/agents/code-reviewer.md +1 -1
  220. package/presets/opencode/mol/agents/codebase-analyzer.md +1 -1
  221. package/presets/opencode/mol/agents/debugger.md +1 -1
  222. package/presets/opencode/mol/agents/feature-developer.md +1 -1
  223. package/presets/opencode/mol/agents/migration-specialist.md +1 -1
  224. package/presets/opencode/mol/agents/mockup-reviewer.md +1 -1
  225. package/presets/opencode/mol/agents/openspec-author.md +1 -1
  226. package/presets/opencode/mol/agents/performance-auditor.md +1 -1
  227. package/presets/opencode/mol/agents/playwright-test-generator.md +1 -1
  228. package/presets/opencode/mol/agents/playwright-test-healer.md +1 -1
  229. package/presets/opencode/mol/agents/playwright-test-planner.md +1 -1
  230. package/presets/opencode/mol/agents/qa-tester.md +1 -1
  231. package/presets/opencode/mol/agents/security-reviewer.md +1 -1
  232. package/presets/opencode/mol/agents/solution-architect.md +1 -1
  233. package/presets/opencode/mol/agents/task-analyst.md +1 -1
  234. package/presets/opencode/mol/agents/task-router.md +1 -1
  235. package/presets/opencode/mol/agents/tech-writer.md +1 -1
  236. package/presets/opencode/mol/agents/unit-test-generator.md +1 -1
  237. package/presets/opencode/mol/agents/unit-test-healer.md +1 -1
  238. package/presets/opencode/mol/agents/unit-test-planner.md +1 -1
  239. package/presets/opencode/mol/commands/task-continue.md +1 -1
  240. package/presets/opencode/mol/hooks/README.md +11 -1
  241. package/presets/opencode/mol/hooks/guard-shell-command.mjs +153 -0
  242. package/presets/opencode/mol/plugins/rule-attach/globs.json +112 -0
  243. package/presets/opencode/mol/rules/agent-team-orchestrator.md +5 -5
  244. package/presets/opencode/next/OPENCODE.md +5 -55
  245. package/presets/opencode/next/adapter-map.json +56 -56
  246. package/presets/opencode/next/agents/accessibility-reviewer.md +1 -1
  247. package/presets/opencode/next/agents/api-contract-reviewer.md +1 -1
  248. package/presets/opencode/next/agents/build-verifier.md +1 -1
  249. package/presets/opencode/next/agents/ci-investigator.md +1 -1
  250. package/presets/opencode/next/agents/code-reviewer.md +1 -1
  251. package/presets/opencode/next/agents/codebase-analyzer.md +1 -1
  252. package/presets/opencode/next/agents/debugger.md +1 -1
  253. package/presets/opencode/next/agents/feature-developer.md +1 -1
  254. package/presets/opencode/next/agents/migration-specialist.md +1 -1
  255. package/presets/opencode/next/agents/mockup-reviewer.md +1 -1
  256. package/presets/opencode/next/agents/openspec-author.md +1 -1
  257. package/presets/opencode/next/agents/performance-auditor.md +1 -1
  258. package/presets/opencode/next/agents/playwright-test-generator.md +1 -1
  259. package/presets/opencode/next/agents/playwright-test-healer.md +1 -1
  260. package/presets/opencode/next/agents/playwright-test-planner.md +1 -1
  261. package/presets/opencode/next/agents/qa-tester.md +1 -1
  262. package/presets/opencode/next/agents/security-reviewer.md +1 -1
  263. package/presets/opencode/next/agents/solution-architect.md +1 -1
  264. package/presets/opencode/next/agents/task-analyst.md +1 -1
  265. package/presets/opencode/next/agents/task-router.md +1 -1
  266. package/presets/opencode/next/agents/tech-writer.md +1 -1
  267. package/presets/opencode/next/agents/unit-test-generator.md +1 -1
  268. package/presets/opencode/next/agents/unit-test-healer.md +1 -1
  269. package/presets/opencode/next/agents/unit-test-planner.md +1 -1
  270. package/presets/opencode/next/commands/task-continue.md +1 -1
  271. package/presets/opencode/next/hooks/README.md +11 -1
  272. package/presets/opencode/next/hooks/guard-shell-command.mjs +153 -0
  273. package/presets/opencode/next/plugins/rule-attach/globs.json +276 -0
  274. package/presets/opencode/next/rules/agent-team-orchestrator.md +5 -5
  275. package/presets/opencode/nuxt/OPENCODE.md +5 -54
  276. package/presets/opencode/nuxt/adapter-map.json +51 -51
  277. package/presets/opencode/nuxt/agents/accessibility-reviewer.md +1 -1
  278. package/presets/opencode/nuxt/agents/api-contract-reviewer.md +1 -1
  279. package/presets/opencode/nuxt/agents/build-verifier.md +1 -1
  280. package/presets/opencode/nuxt/agents/ci-investigator.md +1 -1
  281. package/presets/opencode/nuxt/agents/code-reviewer.md +1 -1
  282. package/presets/opencode/nuxt/agents/codebase-analyzer.md +1 -1
  283. package/presets/opencode/nuxt/agents/debugger.md +1 -1
  284. package/presets/opencode/nuxt/agents/feature-developer.md +1 -1
  285. package/presets/opencode/nuxt/agents/migration-specialist.md +1 -1
  286. package/presets/opencode/nuxt/agents/mockup-reviewer.md +1 -1
  287. package/presets/opencode/nuxt/agents/openspec-author.md +1 -1
  288. package/presets/opencode/nuxt/agents/performance-auditor.md +1 -1
  289. package/presets/opencode/nuxt/agents/playwright-test-generator.md +1 -1
  290. package/presets/opencode/nuxt/agents/playwright-test-healer.md +1 -1
  291. package/presets/opencode/nuxt/agents/playwright-test-planner.md +1 -1
  292. package/presets/opencode/nuxt/agents/qa-tester.md +1 -1
  293. package/presets/opencode/nuxt/agents/security-reviewer.md +1 -1
  294. package/presets/opencode/nuxt/agents/solution-architect.md +1 -1
  295. package/presets/opencode/nuxt/agents/task-analyst.md +1 -1
  296. package/presets/opencode/nuxt/agents/task-router.md +1 -1
  297. package/presets/opencode/nuxt/agents/tech-writer.md +1 -1
  298. package/presets/opencode/nuxt/agents/unit-test-generator.md +1 -1
  299. package/presets/opencode/nuxt/agents/unit-test-healer.md +1 -1
  300. package/presets/opencode/nuxt/agents/unit-test-planner.md +1 -1
  301. package/presets/opencode/nuxt/commands/task-continue.md +1 -1
  302. package/presets/opencode/nuxt/hooks/README.md +11 -1
  303. package/presets/opencode/nuxt/hooks/guard-shell-command.mjs +153 -0
  304. package/presets/opencode/nuxt/plugins/rule-attach/globs.json +266 -0
  305. package/presets/opencode/nuxt/rules/agent-team-orchestrator.md +5 -5
  306. package/presets/opencode/php-hexagonal/OPENCODE.md +5 -60
  307. package/presets/opencode/php-hexagonal/adapter-map.json +45 -45
  308. package/presets/opencode/php-hexagonal/agents/api-contract-reviewer.md +1 -1
  309. package/presets/opencode/php-hexagonal/agents/build-verifier.md +1 -1
  310. package/presets/opencode/php-hexagonal/agents/ci-investigator.md +1 -1
  311. package/presets/opencode/php-hexagonal/agents/code-reviewer.md +1 -1
  312. package/presets/opencode/php-hexagonal/agents/codebase-analyzer.md +1 -1
  313. package/presets/opencode/php-hexagonal/agents/debugger.md +1 -1
  314. package/presets/opencode/php-hexagonal/agents/feature-developer.md +1 -1
  315. package/presets/opencode/php-hexagonal/agents/integration-test-generator.md +1 -1
  316. package/presets/opencode/php-hexagonal/agents/integration-test-healer.md +1 -1
  317. package/presets/opencode/php-hexagonal/agents/integration-test-planner.md +1 -1
  318. package/presets/opencode/php-hexagonal/agents/migration-specialist.md +1 -1
  319. package/presets/opencode/php-hexagonal/agents/openspec-author.md +1 -1
  320. package/presets/opencode/php-hexagonal/agents/performance-auditor.md +1 -1
  321. package/presets/opencode/php-hexagonal/agents/qa-tester.md +1 -1
  322. package/presets/opencode/php-hexagonal/agents/security-reviewer.md +1 -1
  323. package/presets/opencode/php-hexagonal/agents/solution-architect.md +1 -1
  324. package/presets/opencode/php-hexagonal/agents/task-analyst.md +1 -1
  325. package/presets/opencode/php-hexagonal/agents/task-router.md +1 -1
  326. package/presets/opencode/php-hexagonal/agents/tech-writer.md +1 -1
  327. package/presets/opencode/php-hexagonal/agents/unit-test-generator.md +1 -1
  328. package/presets/opencode/php-hexagonal/agents/unit-test-healer.md +1 -1
  329. package/presets/opencode/php-hexagonal/agents/unit-test-planner.md +1 -1
  330. package/presets/opencode/php-hexagonal/commands/task-continue.md +1 -1
  331. package/presets/opencode/php-hexagonal/hooks/README.md +11 -1
  332. package/presets/opencode/php-hexagonal/hooks/guard-shell-command.mjs +155 -0
  333. package/presets/opencode/php-hexagonal/plugins/rule-attach/globs.json +242 -0
  334. package/presets/opencode/php-hexagonal/rules/agent-team-orchestrator.md +5 -5
  335. package/presets/opencode/php-laravel/OPENCODE.md +5 -60
  336. package/presets/opencode/php-laravel/adapter-map.json +45 -45
  337. package/presets/opencode/php-laravel/agents/api-contract-reviewer.md +1 -1
  338. package/presets/opencode/php-laravel/agents/build-verifier.md +1 -1
  339. package/presets/opencode/php-laravel/agents/ci-investigator.md +1 -1
  340. package/presets/opencode/php-laravel/agents/code-reviewer.md +1 -1
  341. package/presets/opencode/php-laravel/agents/codebase-analyzer.md +1 -1
  342. package/presets/opencode/php-laravel/agents/debugger.md +1 -1
  343. package/presets/opencode/php-laravel/agents/feature-developer.md +1 -1
  344. package/presets/opencode/php-laravel/agents/integration-test-generator.md +1 -1
  345. package/presets/opencode/php-laravel/agents/integration-test-healer.md +1 -1
  346. package/presets/opencode/php-laravel/agents/integration-test-planner.md +1 -1
  347. package/presets/opencode/php-laravel/agents/migration-specialist.md +1 -1
  348. package/presets/opencode/php-laravel/agents/openspec-author.md +1 -1
  349. package/presets/opencode/php-laravel/agents/performance-auditor.md +1 -1
  350. package/presets/opencode/php-laravel/agents/qa-tester.md +1 -1
  351. package/presets/opencode/php-laravel/agents/security-reviewer.md +1 -1
  352. package/presets/opencode/php-laravel/agents/solution-architect.md +1 -1
  353. package/presets/opencode/php-laravel/agents/task-analyst.md +1 -1
  354. package/presets/opencode/php-laravel/agents/task-router.md +1 -1
  355. package/presets/opencode/php-laravel/agents/tech-writer.md +1 -1
  356. package/presets/opencode/php-laravel/agents/unit-test-generator.md +1 -1
  357. package/presets/opencode/php-laravel/agents/unit-test-healer.md +1 -1
  358. package/presets/opencode/php-laravel/agents/unit-test-planner.md +1 -1
  359. package/presets/opencode/php-laravel/commands/task-continue.md +1 -1
  360. package/presets/opencode/php-laravel/hooks/README.md +11 -1
  361. package/presets/opencode/php-laravel/hooks/guard-shell-command.mjs +155 -0
  362. package/presets/opencode/php-laravel/plugins/rule-attach/globs.json +232 -0
  363. package/presets/opencode/php-laravel/rules/agent-team-orchestrator.md +5 -5
  364. package/presets/opencode/platform-contract.json +4 -0
  365. package/presets/opencode/plugins/output-guard/README.md +1 -1
  366. package/presets/opencode/plugins/output-guard/index.ts +8 -2
  367. package/presets/opencode/plugins/output-guard/rewrite.ts +37 -0
  368. package/presets/opencode/plugins/rule-attach/README.md +34 -0
  369. package/presets/opencode/plugins/rule-attach/attach.ts +224 -0
  370. package/presets/opencode/plugins/rule-attach/index.ts +169 -0
  371. package/presets/opencode/plugins/rule-attach/package.json +9 -0
  372. package/presets/opencode/svelte/OPENCODE.md +5 -54
  373. package/presets/opencode/svelte/adapter-map.json +51 -51
  374. package/presets/opencode/svelte/agents/accessibility-reviewer.md +1 -1
  375. package/presets/opencode/svelte/agents/api-contract-reviewer.md +1 -1
  376. package/presets/opencode/svelte/agents/build-verifier.md +1 -1
  377. package/presets/opencode/svelte/agents/ci-investigator.md +1 -1
  378. package/presets/opencode/svelte/agents/code-reviewer.md +1 -1
  379. package/presets/opencode/svelte/agents/codebase-analyzer.md +1 -1
  380. package/presets/opencode/svelte/agents/debugger.md +1 -1
  381. package/presets/opencode/svelte/agents/feature-developer.md +1 -1
  382. package/presets/opencode/svelte/agents/migration-specialist.md +1 -1
  383. package/presets/opencode/svelte/agents/mockup-reviewer.md +1 -1
  384. package/presets/opencode/svelte/agents/openspec-author.md +1 -1
  385. package/presets/opencode/svelte/agents/performance-auditor.md +1 -1
  386. package/presets/opencode/svelte/agents/playwright-test-generator.md +1 -1
  387. package/presets/opencode/svelte/agents/playwright-test-healer.md +1 -1
  388. package/presets/opencode/svelte/agents/playwright-test-planner.md +1 -1
  389. package/presets/opencode/svelte/agents/qa-tester.md +1 -1
  390. package/presets/opencode/svelte/agents/security-reviewer.md +1 -1
  391. package/presets/opencode/svelte/agents/solution-architect.md +1 -1
  392. package/presets/opencode/svelte/agents/task-analyst.md +1 -1
  393. package/presets/opencode/svelte/agents/task-router.md +1 -1
  394. package/presets/opencode/svelte/agents/tech-writer.md +1 -1
  395. package/presets/opencode/svelte/agents/unit-test-generator.md +1 -1
  396. package/presets/opencode/svelte/agents/unit-test-healer.md +1 -1
  397. package/presets/opencode/svelte/agents/unit-test-planner.md +1 -1
  398. package/presets/opencode/svelte/commands/task-continue.md +1 -1
  399. package/presets/opencode/svelte/hooks/README.md +11 -1
  400. package/presets/opencode/svelte/hooks/guard-shell-command.mjs +153 -0
  401. package/presets/opencode/svelte/plugins/rule-attach/globs.json +245 -0
  402. package/presets/opencode/svelte/rules/agent-team-orchestrator.md +5 -5
  403. package/presets/zcode/android-kotlin/adapter-map.json +1 -1
  404. package/presets/zcode/android-kotlin/rules/agent-team-orchestrator.md +3 -3
  405. package/presets/zcode/go/adapter-map.json +1 -1
  406. package/presets/zcode/go/rules/agent-team-orchestrator.md +3 -3
  407. package/presets/zcode/ios-swift/adapter-map.json +1 -1
  408. package/presets/zcode/ios-swift/rules/agent-team-orchestrator.md +3 -3
  409. package/presets/zcode/java/adapter-map.json +1 -1
  410. package/presets/zcode/java/rules/agent-team-orchestrator.md +3 -3
  411. package/presets/zcode/mcp-ts/adapter-map.json +1 -1
  412. package/presets/zcode/mcp-ts/rules/agent-team-orchestrator.md +3 -3
  413. package/presets/zcode/mol/adapter-map.json +1 -1
  414. package/presets/zcode/mol/rules/agent-team-orchestrator.md +3 -3
  415. package/presets/zcode/next/adapter-map.json +1 -1
  416. package/presets/zcode/next/rules/agent-team-orchestrator.md +3 -3
  417. package/presets/zcode/nuxt/adapter-map.json +1 -1
  418. package/presets/zcode/nuxt/rules/agent-team-orchestrator.md +3 -3
  419. package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
  420. package/presets/zcode/php-hexagonal/rules/agent-team-orchestrator.md +3 -3
  421. package/presets/zcode/php-laravel/adapter-map.json +1 -1
  422. package/presets/zcode/php-laravel/rules/agent-team-orchestrator.md +3 -3
  423. package/presets/zcode/svelte/adapter-map.json +1 -1
  424. package/presets/zcode/svelte/rules/agent-team-orchestrator.md +3 -3
  425. package/scripts/report-context-cost.mjs +1 -20
@@ -0,0 +1,544 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Per-phase step cost for one task, read from the ledgers the OpenCode output
4
+ * guard already writes.
5
+ *
6
+ * Phases are the task's own attempts: `metrics.json` `attempt_started` /
7
+ * `attempt_finished` events give each step's agent and window, so a phase needs
8
+ * no guessing about which session belonged to which step. Every window is
9
+ * half-open `[start, end)`; an attempt with no `attempt_finished` (a step still
10
+ * running, or a manual host that never wrote the pair) ends at the next start,
11
+ * or at `now` for the last one.
12
+ *
13
+ * Three figures per phase, each a direct count over a deduped row set:
14
+ * `turns` is one `usage.jsonl` row per `(sessionID, messageID)`, `callsByName`
15
+ * is one `results.jsonl` row per `(sessionID, callID)` grouped by tool, and
16
+ * `chainInvocations` is a `bash` / `shell` / `sh` command that runs
17
+ * `chain-team-phases.sh` through an interpreter — the manual step this task
18
+ * removes. A mention inside `echo` does not count.
19
+ *
20
+ * Windows do not overlap: a finished attempt is clipped at the next attempt's
21
+ * start. A row missing the id used for dedupe stays in `unattributed`.
22
+ * When `.ai.local/team/.session-leases/` names this task, only that session
23
+ * and its child sessions are counted. With no lease the report says the
24
+ * window is the whole host ledger.
25
+ *
26
+ * Usage (any cwd):
27
+ * node .ai.local/scripts/report-step-cost.mjs --task <slug> [--json]
28
+ * [--root <team-dir>] [--dir <output-guard-dir>]
29
+ *
30
+ * Read-only: prints to stdout, writes nothing. Exit 0 whenever a report is
31
+ * produced — a task with no ledger rows is a report of zeros, not an error.
32
+ * Exit 2 on a usage error or a `metrics.json` that cannot be parsed.
33
+ */
34
+ import fs from 'node:fs'
35
+ import path from 'node:path'
36
+ import { fileURLToPath } from 'node:url'
37
+
38
+ const USAGE = `usage: report-step-cost.mjs --task <slug> [--json] [--root <team-dir>] [--dir <output-guard-dir>]`
39
+
40
+ const SCHEMA_VERSION = 1
41
+ const LEDGER_ARCHIVES = 3
42
+
43
+ const formatInt = (value) => value.toLocaleString('en-US')
44
+ const padEnd = (value, width) => String(value).padEnd(width)
45
+ const padStart = (value, width) => String(value).padStart(width)
46
+
47
+ /** Nearest `.ai.local/team` above `start`; `null` when the walk reaches the root. */
48
+ export function findTeamRoot(start) {
49
+ let dir = path.resolve(start)
50
+ for (;;) {
51
+ const candidate = path.join(dir, '.ai.local', 'team')
52
+ try {
53
+ if (fs.statSync(candidate).isDirectory()) return candidate
54
+ } catch {
55
+ /* keep walking up */
56
+ }
57
+ const parent = path.dirname(dir)
58
+ if (parent === dir) return null
59
+ dir = parent
60
+ }
61
+ }
62
+
63
+ const parseArgs = (argv) => {
64
+ const options = { task: null, json: false, root: null, dir: null }
65
+ for (let index = 0; index < argv.length; index += 1) {
66
+ const arg = argv[index]
67
+ if (arg === '--json') options.json = true
68
+ else if (arg === '--task') options.task = argv[++index] ?? null
69
+ else if (arg === '--root') options.root = argv[++index] ?? null
70
+ else if (arg === '--dir') options.dir = argv[++index] ?? null
71
+ else if (arg === '--help' || arg === '-h') {
72
+ process.stdout.write(`${USAGE}\n`)
73
+ process.exit(0)
74
+ } else {
75
+ process.stderr.write(`report-step-cost: unknown argument ${arg}\n${USAGE}\n`)
76
+ process.exit(2)
77
+ }
78
+ }
79
+ return options
80
+ }
81
+
82
+ /** JSONL rows; unparsable lines are counted and skipped, an absent file is empty. */
83
+ function readJsonl(file) {
84
+ let text
85
+ try {
86
+ text = fs.readFileSync(file, 'utf8')
87
+ } catch (error) {
88
+ if (error.code === 'ENOENT') return { rows: [], invalid: 0, files: 0 }
89
+ throw error
90
+ }
91
+ const rows = []
92
+ let invalid = 0
93
+ for (const line of text.split('\n')) {
94
+ if (line.trim() === '') continue
95
+ try {
96
+ rows.push(JSON.parse(line))
97
+ } catch {
98
+ invalid += 1
99
+ }
100
+ }
101
+ return { rows, invalid, files: 1 }
102
+ }
103
+
104
+ /** `results.jsonl` plus its rotation archives. The live file is first, so a re-record keeps the newest row. */
105
+ export function readLedger(directory, stem) {
106
+ const rows = []
107
+ let invalid = 0
108
+ let files = 0
109
+ for (let index = 0; index < LEDGER_ARCHIVES; index += 1) {
110
+ const name = index === 0 ? `${stem}.jsonl` : `${stem}.${index}.jsonl`
111
+ const part = readJsonl(path.join(directory, name))
112
+ rows.push(...part.rows)
113
+ invalid += part.invalid
114
+ files += part.files
115
+ }
116
+ return { rows, invalid, files }
117
+ }
118
+
119
+ const numericTime = (value) => {
120
+ const at = Date.parse(value)
121
+ return Number.isFinite(at) ? at : null
122
+ }
123
+
124
+ const stepLabel = (stepIndex) => (stepIndex === null ? 'step?' : `step${stepIndex}`)
125
+
126
+ const phaseLabel = (members) => {
127
+ if (members.length === 1) return `${stepLabel(members[0].stepIndex)}:${members[0].agent}`
128
+ const steps = new Set(members.map((member) => member.stepIndex))
129
+ if (steps.size === 1) return `${stepLabel(members[0].stepIndex)}:${members.map((member) => member.agent).join('+')}`
130
+ return members.map((member) => `${stepLabel(member.stepIndex)}:${member.agent}`).join('+')
131
+ }
132
+
133
+ /** One id for a shared phase: the parallel group, else the wave, else the first attempt. */
134
+ const sharedAttemptId = (members) => {
135
+ if (members.length === 1) return members[0].attemptId
136
+ const groups = new Set(members.map((member) => member.parallelGroupId).filter((id) => id !== ''))
137
+ if (groups.size === 1) return [...groups][0]
138
+ const waves = new Set(members.map((member) => member.waveId).filter((id) => id !== ''))
139
+ if (waves.size === 1) return [...waves][0]
140
+ return members[0].attemptId
141
+ }
142
+
143
+ /**
144
+ * One phase per attempt, ordered by start. Attempts that share a start are one
145
+ * phase: a parallel step and a ready wave record every agent at the same
146
+ * timestamp, and time cannot split their rows. A finished phase closes at its
147
+ * latest finish, clipped at the next later start. An open phase closes at that
148
+ * next start, and the last open phase closes at `now`.
149
+ */
150
+ export function buildPhases(events, now) {
151
+ const started = (Array.isArray(events) ? events : [])
152
+ .filter((event) => event && event.type === 'attempt_started')
153
+ .map((event, index) => ({
154
+ attemptId: typeof event.attemptId === 'string' ? event.attemptId : '',
155
+ stepIndex: Number.isInteger(event.stepIndex) ? event.stepIndex : null,
156
+ agent: typeof event.agent === 'string' ? event.agent : 'unknown',
157
+ parallelGroupId: typeof event.parallelGroupId === 'string' ? event.parallelGroupId : '',
158
+ waveId: typeof event.waveId === 'string' ? event.waveId : '',
159
+ start: numericTime(event.at),
160
+ order: index,
161
+ }))
162
+ .filter((entry) => entry.start !== null)
163
+ .sort((a, b) => a.start - b.start || a.order - b.order)
164
+ const finished = new Map()
165
+ for (const event of Array.isArray(events) ? events : []) {
166
+ if (!event || event.type !== 'attempt_finished') continue
167
+ const at = numericTime(event.at)
168
+ if (at === null || typeof event.attemptId !== 'string') continue
169
+ finished.set(event.attemptId, at)
170
+ }
171
+ const clusters = []
172
+ for (const entry of started) {
173
+ const current = clusters[clusters.length - 1]
174
+ if (current && current[0].start === entry.start) current.push(entry)
175
+ else clusters.push([entry])
176
+ }
177
+ return clusters.map((members, index) => {
178
+ const nextStart = index + 1 < clusters.length ? clusters[index + 1][0].start : now
179
+ const finishes = members
180
+ .map((member) => finished.get(member.attemptId))
181
+ .filter((at) => at !== undefined)
182
+ const allFinished = finishes.length === members.length
183
+ const finishedAt = allFinished ? Math.max(...finishes) : undefined
184
+ const to = Math.min(finishedAt ?? nextStart, nextStart)
185
+ return {
186
+ phase: phaseLabel(members),
187
+ attemptId: sharedAttemptId(members),
188
+ stepIndex: members[0].stepIndex,
189
+ agent: members.length === 1 ? members[0].agent : members.map((member) => member.agent).join('+'),
190
+ startedAt: new Date(members[0].start).toISOString(),
191
+ endedAt: new Date(to).toISOString(),
192
+ open: !allFinished,
193
+ from: members[0].start,
194
+ to,
195
+ }
196
+ })
197
+ }
198
+
199
+ /** Count the deduped rows that fall inside `[from, to)`. */
200
+ function countWindow(usageRows, resultRows, from, to) {
201
+ const turns = []
202
+ for (const row of usageRows) {
203
+ const at = numericTime(row?.at)
204
+ if (at === null || at < from || at >= to) continue
205
+ turns.push(row)
206
+ }
207
+ const calls = resultRows.filter((row) => {
208
+ const at = numericTime(row?.at)
209
+ return at !== null && at >= from && at < to
210
+ })
211
+ return { turns, calls }
212
+ }
213
+
214
+ const SHELL_TOOLS = new Set(['bash', 'shell', 'sh'])
215
+ const CHAIN_PATH = /(?:^|\/)chain-team-phases\.sh$/
216
+
217
+ /** Quoted or bare tokens. A quoted sentence stays one token. */
218
+ export function shellTokens(command) {
219
+ const tokens = []
220
+ const pattern = /"([^"]*)"|'([^']*)'|`([^`]*)`|(\S+)/g
221
+ for (let match = pattern.exec(command); match !== null; match = pattern.exec(command)) {
222
+ const token = match[1] ?? match[2] ?? match[3] ?? match[4] ?? ''
223
+ if (token !== '') tokens.push(token)
224
+ }
225
+ return tokens
226
+ }
227
+
228
+ /** A shell command that executes the chain script, not one that only mentions it. */
229
+ export function isChainInvocation(tool, command) {
230
+ if (typeof tool !== 'string' || !SHELL_TOOLS.has(tool)) return false
231
+ if (typeof command !== 'string' || command === '') return false
232
+ const tokens = shellTokens(command)
233
+ return tokens.some((token, index) => {
234
+ if (!CHAIN_PATH.test(token)) return false
235
+ for (let cursor = index - 1; cursor >= 0; cursor -= 1) {
236
+ const previous = tokens[cursor]
237
+ if (previous === '|' || previous === '||' || previous === '&&' || previous === ';' || previous === '&') {
238
+ return false
239
+ }
240
+ if (previous.startsWith('-') || /^[A-Za-z_][A-Za-z0-9_]*=/.test(previous)) continue
241
+ return previous === 'bash' || previous === 'sh' || previous === 'zsh' || previous === 'source' || previous === '.'
242
+ }
243
+ return false
244
+ })
245
+ }
246
+
247
+ /** `{calls, callsByName, chainInvocations}` for one deduped call set. */
248
+ export function summarizeCalls(rows) {
249
+ const counts = new Map()
250
+ let chainInvocations = 0
251
+ for (const row of rows) {
252
+ const tool = typeof row?.tool === 'string' && row.tool !== '' ? row.tool : 'unknown'
253
+ counts.set(tool, (counts.get(tool) ?? 0) + 1)
254
+ if (isChainInvocation(tool, row?.command)) chainInvocations += 1
255
+ }
256
+ return {
257
+ calls: rows.length,
258
+ callsByName: Object.fromEntries([...counts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))),
259
+ chainInvocations,
260
+ }
261
+ }
262
+
263
+ const usageKey = (row) => {
264
+ const sessionID = typeof row?.sessionID === 'string' ? row.sessionID : ''
265
+ const messageID = typeof row?.messageID === 'string' ? row.messageID : ''
266
+ return sessionID === '' || messageID === '' ? null : `${sessionID}|${messageID}`
267
+ }
268
+
269
+ const callKey = (row) => {
270
+ const sessionID = typeof row?.sessionID === 'string' ? row.sessionID : ''
271
+ const callID = typeof row?.callID === 'string' ? row.callID : ''
272
+ return sessionID === '' || callID === '' ? null : `${sessionID}|${callID}`
273
+ }
274
+
275
+ const splitRows = (rows, keyOf) => {
276
+ const keyed = []
277
+ const anonymous = []
278
+ const seen = new Set()
279
+ for (const row of rows) {
280
+ const key = keyOf(row)
281
+ if (key === null) {
282
+ anonymous.push(row)
283
+ continue
284
+ }
285
+ if (seen.has(key)) continue
286
+ seen.add(key)
287
+ keyed.push(row)
288
+ }
289
+ return { keyed, anonymous }
290
+ }
291
+
292
+ const LEASE_TTL_MS = 7 * 24 * 60 * 60 * 1000
293
+
294
+ /** Unexpired `.session-leases/*.json` rows. A bad file is skipped. */
295
+ export function readSessionLeases(teamRoot, now = Date.now()) {
296
+ const dir = path.join(teamRoot, '.session-leases')
297
+ let names
298
+ try {
299
+ names = fs.readdirSync(dir)
300
+ } catch {
301
+ return []
302
+ }
303
+ const leases = []
304
+ for (const name of names) {
305
+ if (!name.endsWith('.json')) continue
306
+ const file = path.join(dir, name)
307
+ try {
308
+ if (now - fs.statSync(file).mtimeMs > LEASE_TTL_MS) continue
309
+ const parsed = JSON.parse(fs.readFileSync(file, 'utf8'))
310
+ if (!parsed || typeof parsed.slug !== 'string' || typeof parsed.sessionId !== 'string') continue
311
+ leases.push({ slug: parsed.slug, sessionId: parsed.sessionId })
312
+ } catch {
313
+ /* one bad lease does not hide the others */
314
+ }
315
+ }
316
+ return leases
317
+ }
318
+
319
+ /**
320
+ * `null` when no lease names `task` (the caller keeps the host window).
321
+ * Otherwise a predicate for that session and usage children whose
322
+ * `parentSessionID` chain reaches it.
323
+ */
324
+ export function sessionsForSlug(leases, usageRows, task) {
325
+ const owned = new Set()
326
+ for (const lease of leases) {
327
+ if (lease && lease.slug === task && typeof lease.sessionId === 'string' && lease.sessionId !== '') {
328
+ owned.add(lease.sessionId)
329
+ }
330
+ }
331
+ if (owned.size === 0) return null
332
+ const parentOf = new Map()
333
+ for (const row of usageRows) {
334
+ const sessionID = typeof row?.sessionID === 'string' ? row.sessionID : ''
335
+ const parent = typeof row?.parentSessionID === 'string' ? row.parentSessionID : ''
336
+ if (sessionID !== '' && parent !== '') parentOf.set(sessionID, parent)
337
+ }
338
+ const memo = new Map()
339
+ return (sessionID) => {
340
+ if (memo.has(sessionID)) return memo.get(sessionID)
341
+ const seen = new Set()
342
+ let current = sessionID
343
+ let ok = false
344
+ while (typeof current === 'string' && current !== '' && !seen.has(current)) {
345
+ if (owned.has(current)) {
346
+ ok = true
347
+ break
348
+ }
349
+ seen.add(current)
350
+ current = parentOf.get(current)
351
+ }
352
+ for (const id of seen) memo.set(id, ok)
353
+ memo.set(sessionID, ok)
354
+ return ok
355
+ }
356
+ }
357
+
358
+ const partitionSessions = (rows, includeSession) => {
359
+ if (typeof includeSession !== 'function') return { included: rows, excluded: [] }
360
+ const included = []
361
+ const excluded = []
362
+ for (const row of rows) {
363
+ const sessionID = typeof row?.sessionID === 'string' ? row.sessionID : ''
364
+ if (sessionID !== '' && includeSession(sessionID)) included.push(row)
365
+ else excluded.push(row)
366
+ }
367
+ return { included, excluded }
368
+ }
369
+
370
+ /**
371
+ * The whole report: phases from `metrics.json`, rows from the ledger, and one
372
+ * `unattributed` bucket for in-scope rows no attempt window covers. Rows with
373
+ * no dedupe id stay in that bucket and in the totals when the report is the
374
+ * host window. A session lease moves those rows to `excluded` with the other
375
+ * sessions, because they cannot be tied to the lease.
376
+ */
377
+ export function buildReport({
378
+ task,
379
+ events,
380
+ ledgerDirectory,
381
+ usage,
382
+ results,
383
+ now = Date.now(),
384
+ includeSession = null,
385
+ }) {
386
+ const phases = buildPhases(events, now)
387
+ const usageSplit = splitRows(usage, usageKey)
388
+ const resultSplit = splitRows(results, callKey)
389
+ const usageSessions = partitionSessions(usageSplit.keyed, includeSession)
390
+ const resultSessions = partitionSessions(resultSplit.keyed, includeSession)
391
+ const usageRows = usageSessions.included
392
+ const resultRows = resultSessions.included
393
+ const covered = { usage: new Set(), results: new Set() }
394
+
395
+ const rows = phases.map((phase) => {
396
+ const window = countWindow(usageRows, resultRows, phase.from, phase.to)
397
+ for (const row of window.turns) covered.usage.add(row)
398
+ for (const row of window.calls) covered.results.add(row)
399
+ return {
400
+ phase: phase.phase,
401
+ stepIndex: phase.stepIndex,
402
+ agent: phase.agent,
403
+ attemptId: phase.attemptId,
404
+ startedAt: phase.startedAt,
405
+ endedAt: phase.endedAt,
406
+ open: phase.open,
407
+ turns: window.turns.length,
408
+ ...summarizeCalls(window.calls),
409
+ }
410
+ })
411
+
412
+ const lease = typeof includeSession === 'function'
413
+ const usageAnonymous = lease ? [] : usageSplit.anonymous
414
+ const resultAnonymous = lease ? [] : resultSplit.anonymous
415
+ const leftoversUsage = usageRows.filter((row) => !covered.usage.has(row))
416
+ const leftoversResults = resultRows.filter((row) => !covered.results.has(row))
417
+ const unattributed = {
418
+ phase: 'unattributed',
419
+ turns: leftoversUsage.length + usageAnonymous.length,
420
+ ...summarizeCalls([...leftoversResults, ...resultAnonymous]),
421
+ }
422
+ const total = summarizeCalls([...resultRows, ...resultAnonymous])
423
+ const excluded = summarizeCalls([
424
+ ...resultSessions.excluded,
425
+ ...(lease ? resultSplit.anonymous : []),
426
+ ])
427
+ return {
428
+ schemaVersion: SCHEMA_VERSION,
429
+ task,
430
+ generatedAt: new Date(now).toISOString(),
431
+ ledgerDirectory,
432
+ attribution: lease ? 'session-lease' : 'host-window',
433
+ totals: {
434
+ phases: rows.length,
435
+ turns: usageRows.length + usageAnonymous.length,
436
+ ...total,
437
+ },
438
+ phases: rows,
439
+ unattributed,
440
+ excluded: {
441
+ turns: usageSessions.excluded.length + (lease ? usageSplit.anonymous.length : 0),
442
+ calls: excluded.calls,
443
+ },
444
+ }
445
+ }
446
+
447
+ const renderPhase = (row, { tools = true } = {}) => {
448
+ const calls = Object.entries(row.callsByName)
449
+ .map(([tool, count]) => `${tool} ${count}`)
450
+ .join(', ')
451
+ const detail = tools ? ` ${calls}` : ' (outside every phase window; per-tool split in --json)'
452
+ return [
453
+ padEnd(row.phase, 34),
454
+ padEnd(row.attemptId ?? '', 46),
455
+ padStart(formatInt(row.turns), 6),
456
+ padStart(formatInt(row.calls), 6),
457
+ padStart(formatInt(row.chainInvocations), 6),
458
+ detail,
459
+ ].join('')
460
+ }
461
+
462
+ export function renderText(report) {
463
+ const attribution = report.attribution === 'session-lease'
464
+ ? 'attribution: session lease'
465
+ : 'attribution: host window (no session lease for this task)'
466
+ const lines = [
467
+ `step cost — ${report.task}`,
468
+ `ledger: ${report.ledgerDirectory}`,
469
+ attribution,
470
+ '',
471
+ `${padEnd('phase', 34)}${padEnd('attempt', 46)}${padStart('turns', 6)}${padStart('calls', 6)}${padStart('chain', 6)} calls by tool`,
472
+ ]
473
+ for (const row of report.phases) lines.push(renderPhase(row))
474
+ lines.push(renderPhase(report.unattributed, { tools: false }))
475
+ lines.push(
476
+ '',
477
+ `totals: ${formatInt(report.totals.turns)} turns, ${formatInt(report.totals.calls)} calls, `
478
+ + `${formatInt(report.totals.chainInvocations)} chain invocation(s) across ${formatInt(report.totals.phases)} phase(s)`,
479
+ )
480
+ if (report.excluded && (report.excluded.turns > 0 || report.excluded.calls > 0)) {
481
+ lines.push(
482
+ `excluded other sessions: ${formatInt(report.excluded.turns)} turns, ${formatInt(report.excluded.calls)} calls`,
483
+ )
484
+ }
485
+ return `${lines.join('\n')}\n`
486
+ }
487
+
488
+ const readMetrics = (file) => {
489
+ let text
490
+ try {
491
+ text = fs.readFileSync(file, 'utf8')
492
+ } catch (error) {
493
+ if (error.code === 'ENOENT') return { events: [], present: false }
494
+ throw error
495
+ }
496
+ return { events: JSON.parse(text)?.events ?? [], present: true }
497
+ }
498
+
499
+ const main = () => {
500
+ const options = parseArgs(process.argv.slice(2))
501
+ if (!options.task) {
502
+ process.stderr.write(`report-step-cost: --task is required\n${USAGE}\n`)
503
+ process.exit(2)
504
+ }
505
+ const teamRoot = options.root ? path.resolve(options.root) : findTeamRoot(process.cwd())
506
+ if (teamRoot === null) {
507
+ process.stderr.write('report-step-cost: no .ai.local/team above the working directory; pass --root\n')
508
+ process.exit(2)
509
+ }
510
+ const ledgerDirectory = options.dir
511
+ ? path.resolve(options.dir)
512
+ : path.join(teamRoot, 'output-guard')
513
+ const metricsFile = path.join(teamRoot, 'tasks', options.task, 'metrics.json')
514
+ let metrics
515
+ try {
516
+ metrics = readMetrics(metricsFile)
517
+ } catch (error) {
518
+ process.stderr.write(`report-step-cost: cannot parse ${metricsFile}: ${error.message}\n`)
519
+ process.exit(2)
520
+ }
521
+ const usage = readLedger(ledgerDirectory, 'usage')
522
+ const results = readLedger(ledgerDirectory, 'results')
523
+ const includeSession = sessionsForSlug(readSessionLeases(teamRoot), usage.rows, options.task)
524
+ const report = buildReport({
525
+ task: options.task,
526
+ events: metrics.events,
527
+ ledgerDirectory,
528
+ usage: usage.rows,
529
+ results: results.rows,
530
+ includeSession,
531
+ })
532
+ report.ledger = {
533
+ usageFiles: usage.files,
534
+ resultFiles: results.files,
535
+ invalidUsageRows: usage.invalid,
536
+ invalidResultRows: results.invalid,
537
+ metricsPresent: metrics.present,
538
+ }
539
+ process.stdout.write(options.json ? `${JSON.stringify(report, null, 2)}\n` : renderText(report))
540
+ }
541
+
542
+ const invokedDirectly =
543
+ process.argv[1] !== undefined && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url))
544
+ if (invokedDirectly) main()
@@ -0,0 +1,6 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "approvedBy": "",
4
+ "note": "Resolve a step's model tier through this map: `cheap` for the router, analyst, tech-writer, codebase-analyzer and the review roles; `standard` for implementation and the other specialists; `strong` for architecture, security, migration and performance. A value is the `providerID/modelID` (or `providerID/modelID#variant`) your provider serves — take it from the models your host reports, never a guess. While `tiers` is empty and `approvedBy` is empty, this file is the unfilled template and the session model applies. After you set `approvedBy` or any tier, a declared tier with no mapping stops the step with `model-tier-unmapped`.",
5
+ "tiers": {}
6
+ }
@@ -263,12 +263,12 @@ Use Task tool or `/agent-name`. Pass: slug, artifact paths, step `scope` if set.
263
263
  ### Model when invoking (Task)
264
264
 
265
265
  1. Read `steps[i].model` if present; else use the agent’s frontmatter `model` (see `agents/README.md` matrix).
266
- 2. Resolve the tier through `.ai.local/team/model-tiers.json` → `tiers.<tier>` when that file exists. The user approved that map, so passing an explicit model is allowed. Pass the resolved `providerID/modelID` (or `providerID/modelID#variant`) to the Task/subagent tool.
266
+ 2. Resolve the tier through `.ai.local/team/model-tiers.json` → `tiers.<tier>` when that file is an approved map. An empty `tiers` object with an empty `approvedBy` is the unfilled template: omit the model and the session model applies. Once `approvedBy` is set or any tier value is set, the file is the map, so passing an explicit model is allowed. Pass the resolved `providerID/modelID` (or `providerID/modelID#variant`) to the Task/subagent tool.
267
267
  - **`cheap`** → router, analyst, tech-writer, codebase-analyzer, and the review roles `code-reviewer`, `build-verifier`, `api-contract-reviewer`, `mockup-reviewer`. Resolve the provider through `.ai.local/team/model-tiers.json`. Do not write a provider id into the agent file. Pass the mapped id as written; do not append `#high`.
268
268
  - **`standard`** → implementation and the other non-review specialists. `feature-developer` stays on this tier.
269
269
  - **`strong`** → architecture, security, migration, performance. Never downgrade a `strong` step to the parent default, and do not put `mockup-reviewer` on `strong`.
270
- - When `steps[i].model` is omitted and the agent file has no `model`, those four review roles stay `cheap` when `.ai.local/team/model-tiers.json` exists. Without that file, omit the model and the session model applies.
271
- - If a tier is declared but the file has no mapping, **STOP** and report `model-tier-unmapped`; do not silently inherit the parent model.
270
+ - When `steps[i].model` is omitted and the agent file has no `model`, those four review roles stay `cheap` when the map has a `cheap` value. Without the file, or while the template is still empty, omit the model and the session model applies.
271
+ - If a tier is declared, the map is approved, and that tier has no mapping, **STOP** and report `model-tier-unmapped`; do not silently inherit the parent model.
272
272
  3. If the tool does not accept a model (Cursor/Claude adapters), record the resolved tier in the handoff and let the platform pin apply.
273
273
  4. Parallel steps: apply the step-level `model` to **each** agent in the array (or omit → each agent’s own frontmatter).
274
274
  5. The tier never changes the step order, gates, receipts, or retry policy.