@event4u/agent-config 6.1.0 → 7.0.1

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 (1539) hide show
  1. package/.claude-plugin/marketplace.json +35 -3
  2. package/AGENTS.md +8 -7
  3. package/CHANGELOG.md +31 -107
  4. package/CONTRIBUTING.md +1 -1
  5. package/README.md +17 -15
  6. package/dist/agent-src/commands/agent-status.md +2 -2
  7. package/dist/agent-src/commands/agents/audit.md +3 -3
  8. package/dist/agent-src/commands/agents/init.md +1 -1
  9. package/dist/agent-src/commands/agents/optimize.md +4 -4
  10. package/dist/agent-src/commands/analyze/decision.md +108 -0
  11. package/dist/agent-src/commands/analyze/incident.md +120 -0
  12. package/dist/agent-src/commands/analyze/near-miss.md +113 -0
  13. package/dist/agent-src/commands/analyze/postmortem.md +130 -0
  14. package/dist/agent-src/commands/analyze/premortem.md +104 -0
  15. package/dist/agent-src/commands/analyze.md +124 -0
  16. package/dist/agent-src/commands/brand/identity.md +27 -0
  17. package/dist/agent-src/commands/brand/review.md +27 -0
  18. package/dist/agent-src/commands/brand/strategy.md +27 -0
  19. package/dist/agent-src/commands/brand/tokens.md +28 -0
  20. package/dist/agent-src/commands/brand/voice.md +27 -0
  21. package/dist/agent-src/commands/brand.md +58 -0
  22. package/dist/agent-src/commands/check-current-md.md +3 -3
  23. package/dist/agent-src/commands/condense.md +2 -2
  24. package/dist/agent-src/commands/council/debate.md +2 -2
  25. package/dist/agent-src/commands/council/default.md +45 -18
  26. package/dist/agent-src/commands/fix/portability.md +3 -3
  27. package/dist/agent-src/commands/fix/refs.md +3 -3
  28. package/dist/agent-src/commands/implement-ticket.md +36 -6
  29. package/dist/agent-src/commands/knowledge/cross-repo.md +1 -1
  30. package/dist/agent-src/commands/memory/add.md +1 -1
  31. package/dist/agent-src/commands/mission/upgrade.md +182 -0
  32. package/dist/agent-src/commands/optimize/skills.md +2 -2
  33. package/dist/agent-src/commands/orchestrate.md +1 -1
  34. package/dist/agent-src/commands/pr/create.md +6 -4
  35. package/dist/agent-src/commands/review-changes.md +8 -0
  36. package/dist/agent-src/commands/roadmap/materialize.md +73 -0
  37. package/dist/agent-src/commands/skill/preview.md +1 -1
  38. package/dist/agent-src/commands/skills/discover.md +1 -1
  39. package/dist/agent-src/commands/threat-model.md +4 -4
  40. package/dist/agent-src/commands/upstream-contribute.md +3 -3
  41. package/dist/agent-src/commands/video/from-script.md +2 -2
  42. package/dist/agent-src/commands/video/from-song.md +3 -3
  43. package/dist/agent-src/commands/video/scene.md +1 -1
  44. package/dist/agent-src/commands/video/storyboard.md +1 -1
  45. package/dist/agent-src/commands/video.md +3 -3
  46. package/dist/agent-src/contexts/communication/rules-auto/source-of-truth-mechanics.md +3 -3
  47. package/dist/agent-src/contexts/communication/rules-auto/user-interaction-mechanics.md +1 -1
  48. package/dist/agent-src/contexts/execution/evidence-discipline.md +153 -0
  49. package/dist/agent-src/contexts/execution/project-intelligence.md +264 -0
  50. package/dist/agent-src/contexts/execution/roadmap-process-loop.md +2 -1
  51. package/dist/agent-src/personas/ai-video-technical-director.md +1 -1
  52. package/dist/agent-src/personas/brand-strategist.md +74 -0
  53. package/dist/agent-src/personas/design-director.md +74 -0
  54. package/dist/agent-src/rules/brand-consistency.md +77 -0
  55. package/dist/agent-src/rules/brand-source-of-truth.md +57 -0
  56. package/dist/agent-src/rules/direct-answers.md +2 -0
  57. package/dist/agent-src/rules/domain-safety-disclaimer.md +2 -0
  58. package/dist/agent-src/rules/git-history-discipline.md +1 -0
  59. package/dist/agent-src/rules/icon-consistency.md +53 -0
  60. package/dist/agent-src/rules/image-likeness-and-rights.md +67 -0
  61. package/dist/agent-src/rules/lethal-trifecta-guard.md +1 -1
  62. package/dist/agent-src/rules/persona-governance.md +2 -2
  63. package/dist/agent-src/rules/provider-lifecycle-discipline.md +3 -1
  64. package/dist/agent-src/rules/roadmap-progress-sync.md +10 -0
  65. package/dist/agent-src/rules/security-sensitive-stop.md +9 -3
  66. package/dist/agent-src/rules/size-enforcement.md +1 -1
  67. package/dist/agent-src/rules/source-confidentiality.md +3 -3
  68. package/dist/agent-src/rules/source-discovery-gate.md +98 -0
  69. package/dist/agent-src/rules/think-before-action.md +1 -0
  70. package/dist/agent-src/rules/ui-audit-gate.md +2 -0
  71. package/dist/agent-src/rules/untrusted-input-defense.md +1 -1
  72. package/dist/agent-src/rules/user-interaction.md +1 -1
  73. package/dist/agent-src/scripts/archive_completed_roadmaps.ts +392 -0
  74. package/dist/agent-src/scripts/update_roadmap_progress.ts +824 -0
  75. package/dist/agent-src/skills/adr-create/SKILL.md +5 -5
  76. package/dist/agent-src/skills/agent-security-review/evals/triggers.json +1 -0
  77. package/dist/agent-src/skills/agents-md-thin-root/SKILL.md +1 -1
  78. package/dist/agent-src/skills/ai-council/SKILL.md +1 -1
  79. package/dist/agent-src/skills/analysis-autonomous-mode/SKILL.md +9 -13
  80. package/dist/agent-src/skills/blade-ui/SKILL.md +12 -5
  81. package/dist/agent-src/skills/blameless-post-mortem/SKILL.md +199 -0
  82. package/dist/agent-src/skills/brand/ATTRIBUTION.md +38 -0
  83. package/dist/agent-src/skills/brand/SKILL.md +115 -0
  84. package/dist/agent-src/skills/brand/data/archetypes.csv +13 -0
  85. package/dist/agent-src/skills/brand/data/color-psychology.csv +14 -0
  86. package/dist/agent-src/skills/brand/data/logo-style-fit.csv +13 -0
  87. package/dist/agent-src/skills/brand/data/manifest.json +226 -0
  88. package/dist/agent-src/skills/brand/data/messaging-frameworks.csv +13 -0
  89. package/dist/agent-src/skills/brand/data/naming-patterns.csv +13 -0
  90. package/dist/agent-src/skills/brand/data/typography-principles.csv +13 -0
  91. package/dist/agent-src/skills/brand/data/voice-tone.csv +13 -0
  92. package/dist/agent-src/skills/brand/evals/triggers.json +17 -0
  93. package/dist/agent-src/skills/brand-asset-generation/SKILL.md +89 -0
  94. package/dist/agent-src/skills/brand-asset-generation/evals/triggers.json +17 -0
  95. package/dist/agent-src/skills/brand-audit/SKILL.md +67 -0
  96. package/dist/agent-src/skills/brand-audit/evals/triggers.json +17 -0
  97. package/dist/agent-src/skills/brand-identity/SKILL.md +101 -0
  98. package/dist/agent-src/skills/brand-identity/evals/triggers.json +17 -0
  99. package/dist/agent-src/skills/brand-strategy/SKILL.md +83 -0
  100. package/dist/agent-src/skills/brand-strategy/evals/triggers.json +17 -0
  101. package/dist/agent-src/skills/brand-to-tokens/SKILL.md +102 -0
  102. package/dist/agent-src/skills/brand-to-tokens/evals/triggers.json +17 -0
  103. package/dist/agent-src/skills/brand-to-tokens/templates/marp-brand-deck.md.example +46 -0
  104. package/dist/agent-src/skills/brand-to-tokens/templates/reveal-brand-deck.yaml +32 -0
  105. package/dist/agent-src/skills/canvas-design/evals/triggers.json +1 -0
  106. package/dist/agent-src/skills/check-refs/SKILL.md +5 -5
  107. package/dist/agent-src/skills/code-review/SKILL.md +6 -15
  108. package/dist/agent-src/skills/command-writing/SKILL.md +2 -2
  109. package/dist/agent-src/skills/complexity-first-planning/evals/triggers.json +1 -0
  110. package/dist/agent-src/skills/context-authoring/SKILL.md +2 -2
  111. package/dist/agent-src/skills/context-document/SKILL.md +35 -2
  112. package/dist/agent-src/skills/corpus-grounding/evals/triggers.json +1 -0
  113. package/dist/agent-src/skills/corpus-grounding/scripts/bm25_search.ts +482 -0
  114. package/dist/agent-src/skills/corpus-grounding/scripts/decision_engine.ts +803 -0
  115. package/dist/agent-src/skills/corpus-grounding/scripts/ground.ts +541 -0
  116. package/dist/agent-src/skills/corpus-grounding/scripts/schema_validator.ts +309 -0
  117. package/dist/agent-src/skills/database/SKILL.md +26 -4
  118. package/dist/agent-src/skills/decision-record/SKILL.md +1 -1
  119. package/dist/agent-src/skills/decision-record/evals/triggers.json +17 -0
  120. package/dist/agent-src/skills/decision-review/SKILL.md +179 -0
  121. package/dist/agent-src/skills/description-assist/SKILL.md +1 -1
  122. package/dist/agent-src/skills/design-intelligence/SKILL.md +1 -1
  123. package/dist/agent-src/skills/design-intelligence/data/manifest.json +23 -6
  124. package/dist/agent-src/skills/design-intelligence/evals/triggers.json +1 -0
  125. package/dist/agent-src/skills/design-tokens/evals/triggers.json +1 -0
  126. package/dist/agent-src/skills/design-tokens/scripts/tokens.ts +888 -0
  127. package/dist/agent-src/skills/doc-coauthoring/evals/triggers.json +1 -0
  128. package/dist/agent-src/skills/eloquent/evals/triggers.json +1 -0
  129. package/dist/agent-src/skills/emit-tickets/SKILL.md +198 -0
  130. package/dist/agent-src/skills/estimate-ticket/evals/triggers.json +1 -0
  131. package/dist/agent-src/skills/git-workflow/SKILL.md +33 -0
  132. package/dist/agent-src/skills/guideline-writing/SKILL.md +2 -2
  133. package/dist/agent-src/skills/iconography/SKILL.md +88 -0
  134. package/dist/agent-src/skills/iconography/evals/triggers.json +17 -0
  135. package/dist/agent-src/skills/image-analyser/evals/triggers.json +1 -0
  136. package/dist/agent-src/skills/image-creator/evals/triggers.json +1 -0
  137. package/dist/agent-src/skills/image-editing/SKILL.md +100 -0
  138. package/dist/agent-src/skills/image-editing/evals/triggers.json +17 -0
  139. package/dist/agent-src/skills/image-generation/SKILL.md +95 -0
  140. package/dist/agent-src/skills/image-generation/evals/triggers.json +17 -0
  141. package/dist/agent-src/skills/image-provider-routing/SKILL.md +96 -0
  142. package/dist/agent-src/skills/image-provider-routing/evals/triggers.json +17 -0
  143. package/dist/agent-src/skills/launch-readiness/SKILL.md +21 -0
  144. package/dist/agent-src/skills/learning-to-rule-or-skill/SKILL.md +12 -8
  145. package/dist/agent-src/skills/lint-skills/SKILL.md +5 -5
  146. package/dist/agent-src/skills/logo-generation/SKILL.md +98 -0
  147. package/dist/agent-src/skills/logo-generation/evals/triggers.json +17 -0
  148. package/dist/agent-src/skills/markitdown/SKILL.md +1 -1
  149. package/dist/agent-src/skills/md-language-check/SKILL.md +1 -1
  150. package/dist/agent-src/skills/motion-choreographer/SKILL.md +1 -1
  151. package/dist/agent-src/skills/php-coder/evals/triggers.json +1 -0
  152. package/dist/agent-src/skills/prediction-pool-optimizer/evals/triggers.json +1 -0
  153. package/dist/agent-src/skills/premortem/SKILL.md +137 -0
  154. package/dist/agent-src/skills/prompt-engineering-image/SKILL.md +115 -0
  155. package/dist/agent-src/skills/prompt-engineering-image/evals/triggers.json +17 -0
  156. package/dist/agent-src/skills/prompt-validator/evals/triggers.json +1 -0
  157. package/dist/agent-src/skills/react-shadcn-ui/SKILL.md +12 -5
  158. package/dist/agent-src/skills/react-shadcn-ui/scripts/shadcn_add.ts +388 -0
  159. package/dist/agent-src/skills/reasoning-orchestrator/SKILL.md +1 -1
  160. package/dist/agent-src/skills/reasoning-orchestrator/evals/triggers.json +1 -0
  161. package/dist/agent-src/skills/refine-ticket/evals/triggers.json +1 -0
  162. package/dist/agent-src/skills/roadmap-management/SKILL.md +16 -3
  163. package/dist/agent-src/skills/roadmap-writing/SKILL.md +76 -0
  164. package/dist/agent-src/skills/root-cause-frameworks/SKILL.md +146 -0
  165. package/dist/agent-src/skills/rule-refactor/SKILL.md +9 -9
  166. package/dist/agent-src/skills/rule-writing/SKILL.md +7 -7
  167. package/dist/agent-src/skills/script-writing/SKILL.md +2 -2
  168. package/dist/agent-src/skills/security-audit/SKILL.md +5 -0
  169. package/dist/agent-src/skills/skill-improvement-pipeline/SKILL.md +19 -3
  170. package/dist/agent-src/skills/skill-management/SKILL.md +3 -3
  171. package/dist/agent-src/skills/skill-reviewer/SKILL.md +1 -1
  172. package/dist/agent-src/skills/skill-writing/SKILL.md +5 -5
  173. package/dist/agent-src/skills/skill-writing/evals/triggers.json +1 -0
  174. package/dist/agent-src/skills/source-discovery/SKILL.md +182 -0
  175. package/dist/agent-src/skills/standards-from-config/SKILL.md +93 -0
  176. package/dist/agent-src/skills/systematic-debugging/SKILL.md +7 -0
  177. package/dist/agent-src/skills/tailwind-engineer/scripts/tailwind_config_gen.ts +561 -0
  178. package/dist/agent-src/skills/threat-modeling/SKILL.md +1 -0
  179. package/dist/agent-src/skills/typography-system/SKILL.md +138 -0
  180. package/dist/agent-src/skills/typography-system/evals/triggers.json +17 -0
  181. package/dist/agent-src/skills/upstream-contribute/SKILL.md +3 -3
  182. package/dist/agent-src/skills/verify-repair-loop/SKILL.md +209 -0
  183. package/dist/agent-src/skills/verify-repair-loop/evals/output-schema.yml +20 -0
  184. package/dist/agent-src/skills/verify-repair-loop/evals/triggers.json +17 -0
  185. package/dist/agent-src/templates/agent-settings.md +7 -0
  186. package/dist/agent-src/templates/contexts/knowledge-card.md +69 -0
  187. package/dist/agent-src/templates/contexts/lesson-card.md +73 -0
  188. package/dist/agent-src/templates/roadmaps.md +29 -1
  189. package/dist/agent-src/templates/scripts/README.md +6 -6
  190. package/dist/agent-src/templates/scripts/check_memory.ts +640 -0
  191. package/dist/agent-src/templates/scripts/check_memory_proposal.ts +351 -0
  192. package/dist/agent-src/templates/scripts/implement_ticket/__main__.ts +27 -0
  193. package/dist/agent-src/templates/scripts/memory_hash.ts +333 -0
  194. package/dist/agent-src/templates/scripts/memory_lookup.ts +1067 -0
  195. package/dist/agent-src/templates/scripts/memory_report.ts +846 -0
  196. package/dist/agent-src/templates/scripts/memory_signal.ts +422 -0
  197. package/dist/agent-src/templates/scripts/memory_status.ts +191 -0
  198. package/dist/agent-src/templates/scripts/pr_review_routing.ts +523 -0
  199. package/dist/agent-src/templates/scripts/pr_risk_review.ts +0 -0
  200. package/dist/agent-src/templates/scripts/telemetry/aggregator.ts +0 -0
  201. package/dist/agent-src/templates/scripts/telemetry/boundary.ts +164 -0
  202. package/dist/agent-src/templates/scripts/telemetry/engagement.ts +479 -0
  203. package/dist/agent-src/templates/scripts/telemetry/report_renderer.ts +394 -0
  204. package/dist/agent-src/templates/scripts/telemetry/settings.ts +210 -0
  205. package/dist/agent-src/templates/scripts/telemetry_record.ts +255 -0
  206. package/dist/agent-src/templates/scripts/telemetry_report.ts +189 -0
  207. package/dist/agent-src/templates/scripts/telemetry_status.ts +312 -0
  208. package/dist/agent-src/templates/scripts/tier_usage_report.ts +597 -0
  209. package/dist/agent-src/templates/scripts/work_engine/__main__.ts +14 -0
  210. package/dist/agent-src/templates/scripts/work_engine/_lib/agent_settings.ts +1118 -0
  211. package/dist/agent-src/templates/scripts/work_engine/_lib/user_global_paths.ts +329 -0
  212. package/dist/agent-src/templates/scripts/work_engine/cli.ts +206 -0
  213. package/dist/agent-src/templates/scripts/work_engine/cli_args.ts +249 -0
  214. package/dist/agent-src/templates/scripts/work_engine/delivery_state.ts +225 -0
  215. package/dist/agent-src/templates/scripts/work_engine/directives/backend/analyze.ts +125 -0
  216. package/dist/agent-src/templates/scripts/work_engine/directives/backend/implement.ts +189 -0
  217. package/dist/agent-src/templates/scripts/work_engine/directives/backend/index.ts +94 -0
  218. package/dist/agent-src/templates/scripts/work_engine/directives/backend/memory.ts +193 -0
  219. package/dist/agent-src/templates/scripts/work_engine/directives/backend/plan.ts +267 -0
  220. package/dist/agent-src/templates/scripts/work_engine/directives/backend/refine.ts +518 -0
  221. package/dist/agent-src/templates/scripts/work_engine/directives/backend/report.ts +379 -0
  222. package/dist/agent-src/templates/scripts/work_engine/directives/backend/test.ts +268 -0
  223. package/dist/agent-src/templates/scripts/work_engine/directives/backend/verify.ts +258 -0
  224. package/dist/agent-src/templates/scripts/work_engine/directives/index.ts +32 -0
  225. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/contract.ts +243 -0
  226. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/index.ts +108 -0
  227. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/stitch.ts +259 -0
  228. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/ui.ts +216 -0
  229. package/dist/agent-src/templates/scripts/work_engine/directives/ui/_passthrough.ts +40 -0
  230. package/dist/agent-src/templates/scripts/work_engine/directives/ui/app_spec.ts +241 -0
  231. package/dist/agent-src/templates/scripts/work_engine/directives/ui/apply.ts +216 -0
  232. package/dist/agent-src/templates/scripts/work_engine/directives/ui/audit.ts +506 -0
  233. package/dist/agent-src/templates/scripts/work_engine/directives/ui/design.ts +325 -0
  234. package/dist/agent-src/templates/scripts/work_engine/directives/ui/index.ts +102 -0
  235. package/dist/agent-src/templates/scripts/work_engine/directives/ui/polish.ts +462 -0
  236. package/dist/agent-src/templates/scripts/work_engine/directives/ui/review.ts +474 -0
  237. package/dist/agent-src/templates/scripts/work_engine/directives/ui/scaffold.ts +352 -0
  238. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/_skipped.ts +33 -0
  239. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/apply.ts +213 -0
  240. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/index.ts +111 -0
  241. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/refine.ts +126 -0
  242. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/report.ts +112 -0
  243. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/test.ts +164 -0
  244. package/dist/agent-src/templates/scripts/work_engine/dispatcher.ts +515 -0
  245. package/dist/agent-src/templates/scripts/work_engine/emitters.ts +119 -0
  246. package/dist/agent-src/templates/scripts/work_engine/errors.ts +24 -0
  247. package/dist/agent-src/templates/scripts/work_engine/hook_bootstrap.ts +104 -0
  248. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/_chat_history_base.ts +176 -0
  249. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/chat_history_append.ts +41 -0
  250. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/chat_history_halt_append.ts +89 -0
  251. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/decision_gate.ts +193 -0
  252. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/decision_trace.ts +304 -0
  253. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/directive_set_guard.ts +110 -0
  254. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/halt_surface_audit.ts +118 -0
  255. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/index.ts +17 -0
  256. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/memory_visibility.ts +161 -0
  257. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/state_shape_validation.ts +45 -0
  258. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/trace.ts +134 -0
  259. package/dist/agent-src/templates/scripts/work_engine/hooks/context.ts +94 -0
  260. package/dist/agent-src/templates/scripts/work_engine/hooks/events.ts +58 -0
  261. package/dist/agent-src/templates/scripts/work_engine/hooks/exceptions.ts +85 -0
  262. package/dist/agent-src/templates/scripts/work_engine/hooks/index.ts +27 -0
  263. package/dist/agent-src/templates/scripts/work_engine/hooks/registry.ts +66 -0
  264. package/dist/agent-src/templates/scripts/work_engine/hooks/runner.ts +90 -0
  265. package/dist/agent-src/templates/scripts/work_engine/hooks/settings.ts +260 -0
  266. package/dist/agent-src/templates/scripts/work_engine/input_builders.ts +260 -0
  267. package/dist/agent-src/templates/scripts/work_engine/intent/classify.ts +466 -0
  268. package/dist/agent-src/templates/scripts/work_engine/migration/v0_to_v1.ts +531 -0
  269. package/dist/agent-src/templates/scripts/work_engine/orchestration.ts +366 -0
  270. package/dist/agent-src/templates/scripts/work_engine/persona_policy.ts +97 -0
  271. package/dist/agent-src/templates/scripts/work_engine/resolvers/diff.ts +135 -0
  272. package/dist/agent-src/templates/scripts/work_engine/resolvers/file.ts +175 -0
  273. package/dist/agent-src/templates/scripts/work_engine/resolvers/prompt.ts +115 -0
  274. package/dist/agent-src/templates/scripts/work_engine/scoring/confidence.ts +415 -0
  275. package/dist/agent-src/templates/scripts/work_engine/scoring/decision_engine.ts +466 -0
  276. package/dist/agent-src/templates/scripts/work_engine/scoring/decision_trace.ts +298 -0
  277. package/dist/agent-src/templates/scripts/work_engine/scoring/memory_visibility.ts +444 -0
  278. package/dist/agent-src/templates/scripts/work_engine/stack/detect.ts +252 -0
  279. package/dist/agent-src/templates/scripts/work_engine/stack/runner.ts +745 -0
  280. package/dist/agent-src/templates/scripts/work_engine/state.ts +1151 -0
  281. package/dist/agent-src/templates/scripts/work_engine/state_io.ts +413 -0
  282. package/dist/agent-src/templates/tickets.md +120 -0
  283. package/dist/cli/commands/commands.js +2 -2
  284. package/dist/cli/commands/commands.js.map +1 -1
  285. package/dist/cli/commands/doctorShell.js +4 -22
  286. package/dist/cli/commands/doctorShell.js.map +1 -1
  287. package/dist/cli/commands/packs.js +1 -1
  288. package/dist/cli/commands/packs.js.map +1 -1
  289. package/dist/cli/commands/recordTriggerEval.js +179 -0
  290. package/dist/cli/commands/recordTriggerEval.js.map +1 -0
  291. package/dist/cli/commands/recordTriggerEval.test.js +113 -0
  292. package/dist/cli/commands/recordTriggerEval.test.js.map +1 -0
  293. package/dist/cli/commands/workspaces.js +1 -1
  294. package/dist/cli/commands/workspaces.js.map +1 -1
  295. package/dist/cli/main.js +22 -1
  296. package/dist/cli/main.js.map +1 -1
  297. package/dist/cli/python/knowledge_ingest.js +1048 -0
  298. package/dist/cli/python/knowledge_ingest.js.map +1 -0
  299. package/dist/cli/python/workspace_analytics.js +1085 -0
  300. package/dist/cli/python/workspace_analytics.js.map +1 -0
  301. package/dist/cli/python/workspace_crypto.js +544 -0
  302. package/dist/cli/python/workspace_crypto.js.map +1 -0
  303. package/dist/cli/python/workspace_documents.js +1216 -0
  304. package/dist/cli/python/workspace_documents.js.map +1 -0
  305. package/dist/cli/python/workspace_drive.js +574 -0
  306. package/dist/cli/python/workspace_drive.js.map +1 -0
  307. package/dist/cli/python/workspace_drive_health.js +628 -0
  308. package/dist/cli/python/workspace_drive_health.js.map +1 -0
  309. package/dist/cli/python/workspace_explain.js +765 -0
  310. package/dist/cli/python/workspace_explain.js.map +1 -0
  311. package/dist/cli/python/workspace_hosts.js +349 -0
  312. package/dist/cli/python/workspace_hosts.js.map +1 -0
  313. package/dist/cli/python/workspace_inbox.js +692 -0
  314. package/dist/cli/python/workspace_inbox.js.map +1 -0
  315. package/dist/cli/python/workspace_render.js +816 -0
  316. package/dist/cli/python/workspace_render.js.map +1 -0
  317. package/dist/cli/python/workspace_roles.js +487 -0
  318. package/dist/cli/python/workspace_roles.js.map +1 -0
  319. package/dist/cli/python/workspace_secrets.js +180 -0
  320. package/dist/cli/python/workspace_secrets.js.map +1 -0
  321. package/dist/cli/python/workspace_sessions.js +1079 -0
  322. package/dist/cli/python/workspace_sessions.js.map +1 -0
  323. package/dist/cli/python/workspace_skills.js +417 -0
  324. package/dist/cli/python/workspace_skills.js.map +1 -0
  325. package/dist/cli/registry.js +2 -0
  326. package/dist/cli/registry.js.map +1 -1
  327. package/dist/discovery/deprecation-report.md +1 -1
  328. package/dist/discovery/discovery-manifest.json +1174 -123
  329. package/dist/discovery/discovery-manifest.json.sha256 +1 -1
  330. package/dist/discovery/discovery-manifest.summary.md +9 -6
  331. package/dist/discovery/orphan-report.md +1 -1
  332. package/dist/discovery/packs.json +163 -15
  333. package/dist/discovery/trust-report.md +4 -4
  334. package/dist/discovery/workspaces.json +73 -12
  335. package/dist/install/install.mjs +13950 -0
  336. package/dist/mcp/registry-manifest.json +4 -4
  337. package/dist/router.json +1 -1
  338. package/dist/server/routes/wizard.js +50 -21
  339. package/dist/server/routes/wizard.js.map +1 -1
  340. package/dist/server/routes/workspace.js +44 -25
  341. package/dist/server/routes/workspace.js.map +1 -1
  342. package/dist/server/schemas/settings.js +15 -0
  343. package/dist/server/schemas/settings.js.map +1 -1
  344. package/docs/SKILL_CENSUS.md +344 -0
  345. package/docs/architecture/augment-projection.md +1 -1
  346. package/docs/architecture/multi-tool-projection.md +3 -3
  347. package/docs/architecture.md +37 -6
  348. package/docs/archive/CHANGELOG-7.0.0.md +419 -0
  349. package/docs/archive/CHANGELOG-pre-7.0.0.md +126 -0
  350. package/docs/benchmark.md +24 -27
  351. package/docs/capability-matrix.md +32 -0
  352. package/docs/catalog.md +50 -9
  353. package/docs/command-naming-audit.md +60 -0
  354. package/docs/contracts/STABILITY.md +32 -0
  355. package/docs/contracts/agents-md-tech-stack.md +1 -1
  356. package/docs/contracts/ai-council-config.md +22 -22
  357. package/docs/contracts/analysis-memory-loop.md +149 -0
  358. package/docs/contracts/benchmark-ab-contract.md +3 -3
  359. package/docs/contracts/branch-protection-policy.md +27 -0
  360. package/docs/contracts/brand-token-consumption.md +69 -0
  361. package/docs/contracts/command-clusters.md +2 -1
  362. package/docs/contracts/command-surface-tiers.md +13 -0
  363. package/docs/contracts/discovery-manifest.schema.json +24 -5
  364. package/docs/contracts/implement-ticket-flow.md +9 -9
  365. package/docs/contracts/install-layout.md +249 -0
  366. package/docs/contracts/kernel-membership.md +1 -1
  367. package/docs/contracts/linear-ai-rules-inclusion.md +2 -2
  368. package/docs/contracts/linter-structural-model.md +1 -1
  369. package/docs/contracts/mcp-discovery-phase-notice.md +1 -1
  370. package/docs/contracts/multi-tool-projection-fidelity.md +1 -1
  371. package/docs/contracts/namespace.md +2 -2
  372. package/docs/contracts/no-runtime-boundary.md +56 -0
  373. package/docs/contracts/package-self-orientation.md +24 -0
  374. package/docs/contracts/provider-lifecycle.md +3 -3
  375. package/docs/contracts/reasoning-discipline-protocol.md +83 -0
  376. package/docs/contracts/rule-classification.md +3 -3
  377. package/docs/contracts/skill-domains.md +1 -1
  378. package/docs/contracts/smoke-contracts.md +1 -1
  379. package/docs/contracts/surface-tiers.md +81 -0
  380. package/docs/contracts/ticket-bundle-format.md +228 -0
  381. package/docs/cookbook.md +152 -0
  382. package/docs/customization.md +12 -1
  383. package/docs/decisions/ADR-013-discovery-frontmatter-contract.md +16 -0
  384. package/docs/decisions/ADR-056-unvalidated-video-adapters-disposition.md +1 -1
  385. package/docs/decisions/ADR-059-render-resume-filesystem-as-state.md +1 -1
  386. package/docs/decisions/ADR-060-comfyui-sandbox-model.md +1 -1
  387. package/docs/decisions/ADR-061-corpus-grounding-layer.md +48 -1
  388. package/docs/decisions/ADR-096-analysis-workbench.md +110 -0
  389. package/docs/decisions/ADR-097-mission-recipe-privilege-boundary.md +121 -0
  390. package/docs/decisions/ADR-098-evidence-first-structure-discovery.md +154 -0
  391. package/docs/decisions/ADR-099-file-first-pattern-library.md +87 -0
  392. package/docs/decisions/ADR-100-global-knowledge-card-sharing.md +133 -0
  393. package/docs/decisions/ADR-101-ticket-bundle-emission.md +109 -0
  394. package/docs/decisions/ADR-102-ticket-handoff-paste-and-mcp.md +72 -0
  395. package/docs/decisions/ADR-103-global-knowledge-default-off-until-measured.md +92 -0
  396. package/docs/decisions/ADR-200-python-to-typescript-migration.md +193 -0
  397. package/docs/decisions/INDEX.md +9 -0
  398. package/docs/distribution/mcp-submission-checklist.md +3 -3
  399. package/docs/featured-commands.md +1 -1
  400. package/docs/featured-skills.md +1 -1
  401. package/docs/getting-started-by-role.md +2 -0
  402. package/docs/getting-started.md +2 -2
  403. package/docs/guidelines/agent-infra/failure-signatures.md +35 -0
  404. package/docs/guidelines/agent-infra/frontier-reasoning-operating-profile.md +5 -0
  405. package/docs/guidelines/agent-infra/size-and-scope.md +17 -0
  406. package/docs/guidelines/agent-infra/skill-quality-checklist.md +2 -2
  407. package/docs/guides/frontend-design-corpus-refresh.md +83 -0
  408. package/docs/guides/skill-preview.md +1 -1
  409. package/docs/hook-payload-capture.md +4 -4
  410. package/docs/mcp.md +1 -1
  411. package/docs/migration/consumer-template-consumption-model.md +145 -0
  412. package/docs/migration/divergences/README.md +55 -0
  413. package/docs/migration/divergences/_template.md +50 -0
  414. package/docs/migration/divergences/bench-stats-float-precision.md +72 -0
  415. package/docs/migration/divergences/mcp-telemetry-node-sqlite.md +61 -0
  416. package/docs/migration/divergences/pack-mcp-content-gzip-body.md +53 -0
  417. package/docs/migration/divergences/src-scripts-build_cloud_bundle.md +63 -0
  418. package/docs/migration/divergences/src-scripts-check_memory.md +91 -0
  419. package/docs/migration/divergences/src-scripts-inventory_abstraction_budget.md +65 -0
  420. package/docs/migration/divergences/src-scripts-lint_marketplace.md +57 -0
  421. package/docs/migration/divergences/src-scripts-lint_mcp_registry_manifest.md +70 -0
  422. package/docs/migration/divergences/src-scripts-spotcheck_thin_root.md +60 -0
  423. package/docs/migration/divergences/src-scripts-validate_agent_settings.md +58 -0
  424. package/docs/migration/node-floor.md +86 -0
  425. package/docs/migration/yaml-roundtrip-spike.md +163 -0
  426. package/docs/personas.md +6 -1
  427. package/docs/role-experiences.md +19 -0
  428. package/docs/setup/per-ide/windsurf.md +1 -1
  429. package/docs/skills-catalog.md +24 -3
  430. package/docs/threat-model.md +28 -0
  431. package/llms.txt +23 -2
  432. package/package.json +10 -15
  433. package/src/config/agent-settings.template.yml +64 -1
  434. package/src/config/discovery/packs.yml +31 -0
  435. package/src/config/discovery/unassigned-artefacts.yml +6 -0
  436. package/src/config/discovery/workspaces.yml +2 -2
  437. package/src/config/gitignore-block.txt +7 -0
  438. package/src/scripts/_cli/cmd_doctor.ts +2306 -0
  439. package/src/scripts/_cli/cmd_explain.ts +748 -0
  440. package/src/scripts/_cli/cmd_export.ts +375 -0
  441. package/src/scripts/_cli/cmd_migrate.ts +951 -0
  442. package/src/scripts/_cli/cmd_prune.ts +610 -0
  443. package/src/scripts/_cli/cmd_refresh.ts +530 -0
  444. package/src/scripts/_cli/cmd_settings_check.ts +407 -0
  445. package/src/scripts/_cli/cmd_settings_migrate.ts +344 -0
  446. package/src/scripts/_cli/cmd_sync.ts +381 -0
  447. package/src/scripts/_cli/cmd_uninstall.ts +833 -0
  448. package/src/scripts/_cli/cmd_update.ts +585 -0
  449. package/src/scripts/_cli/cmd_upgrade.ts +390 -0
  450. package/src/scripts/_cli/cmd_validate.ts +394 -0
  451. package/src/scripts/_cli/cmd_versions.ts +492 -0
  452. package/src/scripts/_cli/explain_last/assumptions.ts +114 -0
  453. package/src/scripts/_cli/explain_last/council.ts +197 -0
  454. package/src/scripts/_cli/explain_last/halt.ts +73 -0
  455. package/src/scripts/_cli/explain_last/index.ts +155 -0
  456. package/src/scripts/_cli/explain_last/inputs.ts +211 -0
  457. package/src/scripts/_cli/explain_last/memory.ts +231 -0
  458. package/src/scripts/_cli/explain_last/provider.ts +82 -0
  459. package/src/scripts/_cli/explain_last/render.ts +54 -0
  460. package/src/scripts/_cli/explain_last/route.ts +70 -0
  461. package/src/scripts/_cli/explain_last/scrubber.ts +138 -0
  462. package/src/scripts/_cli/explain_last/sections/assumptions.ts +51 -0
  463. package/src/scripts/_cli/explain_last/sections/council.ts +56 -0
  464. package/src/scripts/_cli/explain_last/sections/halt.ts +60 -0
  465. package/src/scripts/_cli/explain_last/sections/header.ts +50 -0
  466. package/src/scripts/_cli/explain_last/sections/index.ts +21 -0
  467. package/src/scripts/_cli/explain_last/sections/inputs.ts +63 -0
  468. package/src/scripts/_cli/explain_last/sections/memory.ts +124 -0
  469. package/src/scripts/_cli/explain_last/sections/pack.ts +42 -0
  470. package/src/scripts/_cli/explain_last/sections/provider.ts +51 -0
  471. package/src/scripts/_cli/explain_last/sections/route.ts +48 -0
  472. package/src/scripts/_cli/explain_last/state_loader.ts +119 -0
  473. package/src/scripts/_dispatch.bash +179 -163
  474. package/src/scripts/_lib/agent_settings.ts +1123 -0
  475. package/src/scripts/_lib/agent_src.ts +654 -0
  476. package/src/scripts/_lib/agents_overlay.ts +183 -0
  477. package/src/scripts/_lib/bench_ab_cache.ts +399 -0
  478. package/src/scripts/_lib/bench_ab_scoring.ts +352 -0
  479. package/src/scripts/_lib/bench_ab_scoring_v2.ts +751 -0
  480. package/src/scripts/_lib/bench_cost.ts +396 -0
  481. package/src/scripts/_lib/bench_quality.ts +237 -0
  482. package/src/scripts/_lib/bench_report.ts +255 -0
  483. package/src/scripts/_lib/bench_telegraph.ts +516 -0
  484. package/src/scripts/_lib/bench_telegraph_report.ts +272 -0
  485. package/src/scripts/_lib/changelog_eras.ts +456 -0
  486. package/src/scripts/_lib/claude_desktop_bundler.ts +324 -0
  487. package/src/scripts/_lib/cli_wrapper.ts +89 -0
  488. package/src/scripts/_lib/fs_atomic.ts +172 -0
  489. package/src/scripts/_lib/global_deploy_inventory.ts +639 -0
  490. package/src/scripts/_lib/install_layout.ts +87 -0
  491. package/src/scripts/_lib/install_regenerator.ts +157 -0
  492. package/src/scripts/_lib/installed_lock.ts +486 -0
  493. package/src/scripts/_lib/installed_tools.ts +518 -0
  494. package/src/scripts/_lib/json_pointers.ts +388 -0
  495. package/src/scripts/_lib/knowledge_global.ts +770 -0
  496. package/src/scripts/_lib/knowledge_global_promote.ts +453 -0
  497. package/src/scripts/_lib/knowledge_global_redaction.ts +448 -0
  498. package/src/scripts/_lib/link_crypto.ts +325 -0
  499. package/src/scripts/_lib/linked_projects.ts +613 -0
  500. package/src/scripts/_lib/model_tier.ts +65 -0
  501. package/src/scripts/_lib/module_detection.ts +275 -0
  502. package/src/scripts/_lib/node_sqlite.d.ts +32 -0
  503. package/src/scripts/_lib/pin_resolver.ts +264 -0
  504. package/src/scripts/_lib/py_random.ts +212 -0
  505. package/src/scripts/_lib/script_output.ts +147 -0
  506. package/src/scripts/_lib/security_lint.ts +623 -0
  507. package/src/scripts/_lib/surface_tiers.ts +127 -0
  508. package/src/scripts/_lib/token_count.ts +126 -0
  509. package/src/scripts/_lib/update_check.ts +297 -0
  510. package/src/scripts/_lib/user_global_paths.ts +329 -0
  511. package/src/scripts/_lib/value_ladder.ts +882 -0
  512. package/src/scripts/_lib/value_report.ts +617 -0
  513. package/src/scripts/_lib/zip_min.ts +175 -0
  514. package/src/scripts/adoption_report.ts +357 -0
  515. package/src/scripts/adoption_snapshot.ts +392 -0
  516. package/src/scripts/adoption_status.ts +424 -0
  517. package/src/scripts/adr/regenerate_index.ts +257 -0
  518. package/src/scripts/ai-image/adapters/flux.sh +45 -0
  519. package/src/scripts/ai-image/adapters/gemini-image.sh +45 -0
  520. package/src/scripts/ai-image/adapters/ideogram.sh +45 -0
  521. package/src/scripts/ai-image/adapters/recraft.sh +47 -0
  522. package/src/scripts/ai-video/adapters/comfyui.sh +3 -3
  523. package/src/scripts/ai-video/adapters/fal.sh +3 -3
  524. package/src/scripts/ai-video/adapters/gemini-veo.sh +3 -3
  525. package/src/scripts/ai-video/adapters/higgsfield.sh +3 -3
  526. package/src/scripts/ai-video/adapters/kling.sh +3 -3
  527. package/src/scripts/ai-video/adapters/musetalk.sh +2 -2
  528. package/src/scripts/ai-video/adapters/openai-images.sh +3 -3
  529. package/src/scripts/ai-video/adapters/replicate.sh +3 -3
  530. package/src/scripts/ai-video/adapters/sora.sh +3 -3
  531. package/src/scripts/ai-video/adapters/syncso.sh +3 -3
  532. package/src/scripts/ai-video/audio-adapters/allin1.sh +2 -2
  533. package/src/scripts/ai-video/audio-adapters/whisperx.sh +2 -2
  534. package/src/scripts/ai-video/lib/audio-adapter-contract.md +1 -1
  535. package/src/scripts/ai-video/lib/embed-provenance.sh +2 -2
  536. package/src/scripts/ai-video/lib/ingest-song.sh +2 -2
  537. package/src/scripts/ai-video/lib/parse-blueprint.sh +1 -1
  538. package/src/scripts/ai-video/lib/resume-scan.sh +2 -2
  539. package/src/scripts/ai-video/smoke-trace.sh +16 -7
  540. package/src/scripts/ai-video/stitch.sh +2 -2
  541. package/src/scripts/ai_council/_default_prices.ts +73 -0
  542. package/src/scripts/ai_council/advisors.ts +244 -0
  543. package/src/scripts/ai_council/airgap.ts +249 -0
  544. package/src/scripts/ai_council/budget_guard.ts +492 -0
  545. package/src/scripts/ai_council/bundler.ts +376 -0
  546. package/src/scripts/ai_council/cli_hints.ts +120 -0
  547. package/src/scripts/ai_council/clients.ts +2214 -0
  548. package/src/scripts/ai_council/compile_corpus.ts +681 -0
  549. package/src/scripts/ai_council/confidence_gate.ts +230 -0
  550. package/src/scripts/ai_council/config.ts +1729 -0
  551. package/src/scripts/ai_council/consensus.ts +551 -0
  552. package/src/scripts/ai_council/events_log.ts +327 -0
  553. package/src/scripts/ai_council/learn_low_impact_preview.ts +317 -0
  554. package/src/scripts/ai_council/low_impact.ts +1069 -0
  555. package/src/scripts/ai_council/low_impact_corpus.ts +662 -0
  556. package/src/scripts/ai_council/low_impact_intake.ts +222 -0
  557. package/src/scripts/ai_council/modes.ts +169 -0
  558. package/src/scripts/ai_council/necessity.ts +933 -0
  559. package/src/scripts/ai_council/orchestrator.ts +1689 -0
  560. package/src/scripts/ai_council/pricing.ts +267 -0
  561. package/src/scripts/ai_council/probation_gate.ts +282 -0
  562. package/src/scripts/ai_council/project_context.ts +308 -0
  563. package/src/scripts/ai_council/prompts.ts +600 -0
  564. package/src/scripts/ai_council/redact_low_impact_entry.ts +291 -0
  565. package/src/scripts/ai_council/replay.ts +314 -0
  566. package/src/scripts/ai_council/session.ts +558 -0
  567. package/src/scripts/ai_council/shadow_dispatch.ts +509 -0
  568. package/src/scripts/ai_council/solo_dispatch.ts +281 -0
  569. package/src/scripts/analysis_freshness.ts +343 -0
  570. package/src/scripts/annotate_discovery.ts +288 -0
  571. package/src/scripts/apply_modules_config.ts +537 -0
  572. package/src/scripts/audit_adr_coverage.ts +357 -0
  573. package/src/scripts/audit_auto_rules.ts +415 -0
  574. package/src/scripts/audit_cloud_compatibility.ts +608 -0
  575. package/src/scripts/audit_command_surface.ts +1227 -0
  576. package/src/scripts/audit_initial_context.ts +694 -0
  577. package/src/scripts/audit_likelihood.ts +434 -0
  578. package/src/scripts/audit_mcp_tools.ts +252 -0
  579. package/src/scripts/audit_overlap.ts +421 -0
  580. package/src/scripts/audit_skill_descriptions.ts +402 -0
  581. package/src/scripts/audit_skill_overlap.ts +576 -0
  582. package/src/scripts/audit_user_type_axis.ts +264 -0
  583. package/src/scripts/backfill_model_tier.ts +349 -0
  584. package/src/scripts/bench_ab_cache_dispatch.ts +126 -0
  585. package/src/scripts/bench_ab_clone.ts +610 -0
  586. package/src/scripts/bench_ab_diff.ts +609 -0
  587. package/src/scripts/bench_ab_integrity.ts +261 -0
  588. package/src/scripts/bench_ab_run.ts +417 -0
  589. package/src/scripts/bench_ab_task_runner.ts +1382 -0
  590. package/src/scripts/bench_ab_tracka_run.ts +436 -0
  591. package/src/scripts/bench_ab_v2_run.ts +585 -0
  592. package/src/scripts/bench_ab_v2_stats.ts +1018 -0
  593. package/src/scripts/bench_baseline_ready.ts +326 -0
  594. package/src/scripts/bench_condense_memory.ts +479 -0
  595. package/src/scripts/bench_drift_check.ts +503 -0
  596. package/src/scripts/bench_per_tool.ts +591 -0
  597. package/src/scripts/bench_rtk_savings.ts +710 -0
  598. package/src/scripts/bench_run.ts +509 -0
  599. package/src/scripts/bench_runner.ts +519 -0
  600. package/src/scripts/build_cloud_bundle.ts +692 -0
  601. package/src/scripts/build_discovery_manifest.ts +1371 -0
  602. package/src/scripts/build_linear_digest.ts +368 -0
  603. package/src/scripts/build_mcp_registry_manifest.ts +351 -0
  604. package/src/scripts/build_rule_trigger_matrix.ts +469 -0
  605. package/src/scripts/capture_showcase_session.ts +735 -0
  606. package/src/scripts/chat_history.ts +2301 -0
  607. package/src/scripts/check_always_budget.ts +694 -0
  608. package/src/scripts/check_artefact_checksums.ts +281 -0
  609. package/src/scripts/check_augment_description_cap.ts +133 -0
  610. package/src/scripts/check_augmentignore.ts +108 -0
  611. package/src/scripts/check_beta_review_markers.ts +234 -0
  612. package/src/scripts/check_bite_sized_granularity.ts +116 -0
  613. package/src/scripts/check_cluster_patterns.ts +285 -0
  614. package/src/scripts/check_command_count_messaging.ts +224 -0
  615. package/src/scripts/check_condensation.ts +900 -0
  616. package/src/scripts/check_condensed_paths.ts +414 -0
  617. package/src/scripts/check_context_paths.ts +388 -0
  618. package/src/scripts/check_council_config_location.ts +260 -0
  619. package/src/scripts/check_council_layout.ts +180 -0
  620. package/src/scripts/check_council_references.ts +345 -0
  621. package/src/scripts/check_discovery_determinism.ts +124 -0
  622. package/src/scripts/check_gate_paths.ts +230 -0
  623. package/src/scripts/check_iron_law_prominence.ts +298 -0
  624. package/src/scripts/check_kernel_rule_bundle.ts +242 -0
  625. package/src/scripts/check_knowledge_cards.ts +759 -0
  626. package/src/scripts/check_md_language.ts +291 -0
  627. package/src/scripts/check_memory.ts +845 -0
  628. package/src/scripts/check_memory_proposal.ts +351 -0
  629. package/src/scripts/check_module_management_neutral.ts +238 -0
  630. package/src/scripts/check_no_conflict_markers.ts +298 -0
  631. package/src/scripts/check_no_conflict_markers_allowlist.json +4 -0
  632. package/src/scripts/check_no_external_sources.ts +351 -0
  633. package/src/scripts/check_no_local_settings_committed.ts +69 -0
  634. package/src/scripts/check_no_new_legacy_path.ts +188 -0
  635. package/src/scripts/check_no_roadmap_refs.ts +304 -0
  636. package/src/scripts/check_one_off_location.ts +165 -0
  637. package/src/scripts/check_overlay_cascade_subdirs.ts +188 -0
  638. package/src/scripts/check_portability.ts +860 -0
  639. package/src/scripts/check_proposal.ts +0 -0
  640. package/src/scripts/check_public_catalog_links.ts +204 -0
  641. package/src/scripts/check_public_links.ts +357 -0
  642. package/src/scripts/check_references.ts +963 -0
  643. package/src/scripts/check_release_includes_discovery.ts +94 -0
  644. package/src/scripts/check_release_pr_shape.ts +222 -0
  645. package/src/scripts/check_release_published.ts +235 -0
  646. package/src/scripts/check_release_trunk_sync.ts +203 -0
  647. package/src/scripts/check_reply_consistency.ts +359 -0
  648. package/src/scripts/check_roadmap_trackable.ts +268 -0
  649. package/src/scripts/check_role_doc_links.ts +187 -0
  650. package/src/scripts/check_safety_floor_untouched.ts +160 -0
  651. package/src/scripts/check_skill_requires.ts +205 -0
  652. package/src/scripts/check_structural_breaking.ts +170 -0
  653. package/src/scripts/check_surface_tiers.ts +567 -0
  654. package/src/scripts/check_template_pin_drift.ts +222 -0
  655. package/src/scripts/check_test_coverage_diff.ts +235 -0
  656. package/src/scripts/check_token_optimizer_freshness.ts +183 -0
  657. package/src/scripts/check_trigger_evals.ts +375 -0
  658. package/src/scripts/check_update_banner.ts +143 -0
  659. package/src/scripts/ci_status.ts +0 -0
  660. package/src/scripts/ci_summary.ts +235 -0
  661. package/src/scripts/ci_time_ratio.ts +526 -0
  662. package/src/scripts/command_suggester/cooldown.ts +176 -0
  663. package/src/scripts/command_suggester/index.ts +41 -0
  664. package/src/scripts/command_suggester/loader.ts +205 -0
  665. package/src/scripts/command_suggester/match.ts +294 -0
  666. package/src/scripts/command_suggester/rank.ts +201 -0
  667. package/src/scripts/command_suggester/render.ts +122 -0
  668. package/src/scripts/command_suggester/sanitize.ts +114 -0
  669. package/src/scripts/command_suggester/settings.ts +186 -0
  670. package/src/scripts/command_suggester/types.ts +0 -0
  671. package/src/scripts/compile_router.ts +297 -0
  672. package/src/scripts/condense.sh +7 -1
  673. package/src/scripts/condense.ts +2035 -0
  674. package/src/scripts/condense_memory.ts +334 -0
  675. package/src/scripts/config/index.ts +15 -0
  676. package/src/scripts/config/packs.ts +310 -0
  677. package/src/scripts/config/presets.ts +369 -0
  678. package/src/scripts/config/profile_explain.ts +114 -0
  679. package/src/scripts/config/profiles.ts +277 -0
  680. package/src/scripts/config/session_profiles.ts +1064 -0
  681. package/src/scripts/context_hygiene_hook.ts +272 -0
  682. package/src/scripts/cost_by_conversation.ts +444 -0
  683. package/src/scripts/cost_summary.ts +407 -0
  684. package/src/scripts/council_cli.ts +2827 -0
  685. package/src/scripts/council_prune.ts +153 -0
  686. package/src/scripts/cross_repo_retrieve.ts +694 -0
  687. package/src/scripts/discovery_stats.ts +218 -0
  688. package/src/scripts/evidence_report.ts +580 -0
  689. package/src/scripts/external_sources_denylist.json +1 -0
  690. package/src/scripts/extract_audit_patterns.ts +394 -0
  691. package/src/scripts/first_run_gate_hook.ts +246 -0
  692. package/src/scripts/gen_discovery_baseline.ts +297 -0
  693. package/src/scripts/generate_capabilities_index.ts +496 -0
  694. package/src/scripts/generate_capability_matrix.ts +430 -0
  695. package/src/scripts/generate_catalog.ts +178 -0
  696. package/src/scripts/generate_command_flows.ts +316 -0
  697. package/src/scripts/generate_cookbook.ts +302 -0
  698. package/src/scripts/generate_index.ts +500 -0
  699. package/src/scripts/generate_ownership_matrix.ts +646 -0
  700. package/src/scripts/generate_pack_manifests.ts +1025 -0
  701. package/src/scripts/generate_role_experiences_catalog.ts +265 -0
  702. package/src/scripts/hermetic-install.sh +22 -11
  703. package/src/scripts/hook_manifest.yaml +24 -10
  704. package/src/scripts/hooks/augment-chat-history.sh +3 -10
  705. package/src/scripts/hooks/augment-context-hygiene.sh +3 -10
  706. package/src/scripts/hooks/augment-dispatcher.sh +3 -10
  707. package/src/scripts/hooks/augment-onboarding-gate.sh +3 -10
  708. package/src/scripts/hooks/augment-roadmap-progress.sh +3 -10
  709. package/src/scripts/hooks/block_no_verify.ts +413 -0
  710. package/src/scripts/hooks/cline-dispatcher.sh +3 -10
  711. package/src/scripts/hooks/cowork-dispatcher.sh +3 -14
  712. package/src/scripts/hooks/cursor-dispatcher.sh +3 -10
  713. package/src/scripts/hooks/dispatch_hook.ts +851 -0
  714. package/src/scripts/hooks/dispatch_issues.ts +226 -0
  715. package/src/scripts/hooks/envelope.ts +140 -0
  716. package/src/scripts/hooks/gemini-dispatcher.sh +3 -8
  717. package/src/scripts/hooks/replay_hook.ts +364 -0
  718. package/src/scripts/hooks/state_io.ts +293 -0
  719. package/src/scripts/hooks/windsurf-dispatcher.sh +3 -9
  720. package/src/scripts/hooks_doctor.ts +418 -0
  721. package/src/scripts/hooks_status.ts +292 -0
  722. package/src/scripts/injection_scan_hook.ts +285 -0
  723. package/src/scripts/install +36 -22
  724. package/src/scripts/install-hooks.sh +20 -14
  725. package/src/scripts/install.sh +38 -14
  726. package/src/scripts/install.ts +4515 -0
  727. package/src/scripts/inventory_abstraction_budget.ts +1104 -0
  728. package/src/scripts/inventory_frontmatter.ts +320 -0
  729. package/src/scripts/inventory_meta_layers.ts +516 -0
  730. package/src/scripts/iron_law_sha.ts +233 -0
  731. package/src/scripts/knowledge_global_cli.ts +1105 -0
  732. package/src/scripts/linked_projects_list.ts +310 -0
  733. package/src/scripts/lint_agent_security.ts +224 -0
  734. package/src/scripts/lint_agent_skill_names.ts +241 -0
  735. package/src/scripts/lint_agents_layout.ts +205 -0
  736. package/src/scripts/lint_agents_md.ts +294 -0
  737. package/src/scripts/lint_archived_skills.ts +309 -0
  738. package/src/scripts/lint_artefact_frontmatter.ts +359 -0
  739. package/src/scripts/lint_bench_ab.ts +319 -0
  740. package/src/scripts/lint_bench_corpus.ts +421 -0
  741. package/src/scripts/lint_command_flow_coverage.ts +231 -0
  742. package/src/scripts/lint_command_routing.ts +377 -0
  743. package/src/scripts/lint_command_tiers.ts +345 -0
  744. package/src/scripts/lint_command_verbs.ts +379 -0
  745. package/src/scripts/lint_commit_subjects.ts +243 -0
  746. package/src/scripts/lint_context_spine_usage.ts +198 -0
  747. package/src/scripts/lint_discovery_manifest.ts +540 -0
  748. package/src/scripts/lint_discovery_vocabulary.ts +393 -0
  749. package/src/scripts/lint_empty_roadmaps.ts +147 -0
  750. package/src/scripts/lint_eval_freshness.ts +335 -0
  751. package/src/scripts/lint_examples.ts +183 -0
  752. package/src/scripts/lint_explain_trace.ts +381 -0
  753. package/src/scripts/lint_featured_skills.ts +0 -0
  754. package/src/scripts/lint_flows.ts +701 -0
  755. package/src/scripts/lint_framework_leakage.ts +497 -0
  756. package/src/scripts/lint_framework_leakage_allowlist.json +8 -1
  757. package/src/scripts/lint_frontmatter_boilerplate.ts +356 -0
  758. package/src/scripts/lint_ghostwriter_source.ts +389 -0
  759. package/src/scripts/lint_global_paths.ts +420 -0
  760. package/src/scripts/lint_handoffs.ts +362 -0
  761. package/src/scripts/lint_hidden_unicode.ts +350 -0
  762. package/src/scripts/lint_hook_concern_budget.ts +319 -0
  763. package/src/scripts/lint_hook_manifest.ts +354 -0
  764. package/src/scripts/lint_instruction_smuggling.ts +173 -0
  765. package/src/scripts/lint_load_context.ts +371 -0
  766. package/src/scripts/lint_marketplace.ts +286 -0
  767. package/src/scripts/lint_marketplace_install_completeness.ts +309 -0
  768. package/src/scripts/lint_mcp_config_security.ts +225 -0
  769. package/src/scripts/lint_mcp_registry_manifest.ts +350 -0
  770. package/src/scripts/lint_media_policy_linkage.ts +224 -0
  771. package/src/scripts/lint_missions.ts +774 -0
  772. package/src/scripts/lint_model_tier_coverage.ts +151 -0
  773. package/src/scripts/lint_namespace.ts +295 -0
  774. package/src/scripts/lint_namespace_collisions.ts +203 -0
  775. package/src/scripts/lint_new_skill_gate.ts +462 -0
  776. package/src/scripts/lint_no_new_atomic_commands.ts +342 -0
  777. package/src/scripts/lint_one_off_age.ts +348 -0
  778. package/src/scripts/lint_orchestration_dsl.ts +377 -0
  779. package/src/scripts/lint_orchestrator_auto_detect.ts +177 -0
  780. package/src/scripts/lint_pack_boundaries.ts +366 -0
  781. package/src/scripts/lint_pack_dependencies.ts +541 -0
  782. package/src/scripts/lint_pack_first_win.ts +202 -0
  783. package/src/scripts/lint_persona_governance.ts +292 -0
  784. package/src/scripts/lint_positioning.ts +257 -0
  785. package/src/scripts/lint_profile_overlay_set_only.ts +324 -0
  786. package/src/scripts/lint_readme_jargon.ts +189 -0
  787. package/src/scripts/lint_readme_size.ts +73 -0
  788. package/src/scripts/lint_regression.ts +497 -0
  789. package/src/scripts/lint_roadmap_ci_steps.ts +252 -0
  790. package/src/scripts/lint_roadmap_complexity.ts +295 -0
  791. package/src/scripts/lint_roadmap_later_disposition.ts +357 -0
  792. package/src/scripts/lint_role_experiences.ts +410 -0
  793. package/src/scripts/lint_rule_interactions.ts +281 -0
  794. package/src/scripts/lint_rule_tiers.ts +169 -0
  795. package/src/scripts/lint_showcase_sessions.ts +254 -0
  796. package/src/scripts/lint_skill_frontmatter_safety.ts +279 -0
  797. package/src/scripts/lint_skill_originality.ts +586 -0
  798. package/src/scripts/lint_skill_originality_allowlist.json +20 -0
  799. package/src/scripts/lint_skill_tools.ts +320 -0
  800. package/src/scripts/lint_ticket_buildable.ts +1027 -0
  801. package/src/scripts/lint_topics_yaml.ts +203 -0
  802. package/src/scripts/lint_trust_coherence.ts +377 -0
  803. package/src/scripts/lint_value_dashboard.ts +314 -0
  804. package/src/scripts/lint_workflow_security.ts +637 -0
  805. package/src/scripts/lint_workflow_security_allowlist.json +20 -0
  806. package/src/scripts/lint_workspace_boundary.ts +248 -0
  807. package/src/scripts/mcp_parity_smoke.ts +638 -0
  808. package/src/scripts/mcp_render.ts +346 -0
  809. package/src/scripts/mcp_server/__main__.ts +28 -0
  810. package/src/scripts/mcp_server/catalog.ts +154 -0
  811. package/src/scripts/mcp_server/index.ts +24 -0
  812. package/src/scripts/mcp_server/metadata.ts +83 -0
  813. package/src/scripts/mcp_server/prompts.ts +711 -0
  814. package/src/scripts/mcp_server/resources.ts +343 -0
  815. package/src/scripts/mcp_server/server.ts +439 -0
  816. package/src/scripts/mcp_server/telemetry.ts +154 -0
  817. package/src/scripts/mcp_server/tools.ts +1031 -0
  818. package/src/scripts/mcp_setup.sh +25 -52
  819. package/src/scripts/mcp_telemetry_health.ts +362 -0
  820. package/src/scripts/mcp_telemetry_query.ts +371 -0
  821. package/src/scripts/mcp_telemetry_store.ts +422 -0
  822. package/src/scripts/measure_augment_budget.ts +453 -0
  823. package/src/scripts/measure_density.ts +618 -0
  824. package/src/scripts/measure_frugality_savings.ts +353 -0
  825. package/src/scripts/measure_markitdown_lift.ts +299 -0
  826. package/src/scripts/measure_patterns.ts +682 -0
  827. package/src/scripts/measure_projection_bytes.ts +425 -0
  828. package/src/scripts/measure_rule_budget.ts +627 -0
  829. package/src/scripts/measure_skill_reduction.ts +442 -0
  830. package/src/scripts/media/lib/adapter-common.sh +247 -0
  831. package/src/scripts/media/lib/adapter-contract.md +329 -0
  832. package/src/scripts/media/lib/fixtures/comfyui/result.json +1 -0
  833. package/src/scripts/media/lib/fixtures/fal/result.json +1 -0
  834. package/src/scripts/media/lib/fixtures/flux/asset-0001.png +0 -0
  835. package/src/scripts/media/lib/fixtures/flux/result.json +1 -0
  836. package/src/scripts/media/lib/fixtures/gemini-image/asset-0001.png +0 -0
  837. package/src/scripts/media/lib/fixtures/gemini-image/result.json +1 -0
  838. package/src/scripts/media/lib/fixtures/gemini-veo/result.json +1 -0
  839. package/src/scripts/media/lib/fixtures/higgsfield/result.json +1 -0
  840. package/src/scripts/media/lib/fixtures/ideogram/asset-0001.png +0 -0
  841. package/src/scripts/media/lib/fixtures/ideogram/result.json +1 -0
  842. package/src/scripts/media/lib/fixtures/kling/result.json +1 -0
  843. package/src/scripts/media/lib/fixtures/musetalk/result.json +1 -0
  844. package/src/scripts/media/lib/fixtures/openai-images/result.json +1 -0
  845. package/src/scripts/media/lib/fixtures/recraft/asset-0001.svg +1 -0
  846. package/src/scripts/media/lib/fixtures/recraft/result.json +1 -0
  847. package/src/scripts/media/lib/fixtures/replicate/result.json +1 -0
  848. package/src/scripts/media/lib/fixtures/sora/result.json +1 -0
  849. package/src/scripts/media/lib/fixtures/syncso/result.json +1 -0
  850. package/src/scripts/media/lib/load-config.sh +180 -0
  851. package/src/scripts/media/lib/redact.sh +85 -0
  852. package/src/scripts/memory_hash.ts +331 -0
  853. package/src/scripts/memory_lookup.ts +1278 -0
  854. package/src/scripts/memory_report.ts +845 -0
  855. package/src/scripts/memory_signal.ts +417 -0
  856. package/src/scripts/memory_status.ts +189 -0
  857. package/src/scripts/migrate_command_suggestions.ts +341 -0
  858. package/src/scripts/migrate_frontmatter_defaults.ts +539 -0
  859. package/src/scripts/migration_status.ts +301 -0
  860. package/src/scripts/mine_session.ts +645 -0
  861. package/src/scripts/minimal_safe_diff_hook.ts +355 -0
  862. package/src/scripts/move_artefact.ts +869 -0
  863. package/src/scripts/new_skill.ts +404 -0
  864. package/src/scripts/onboarding_gate_hook.ts +224 -0
  865. package/src/scripts/pack_dependency_allowlist.json +1 -1
  866. package/src/scripts/pack_mcp_content.ts +552 -0
  867. package/src/scripts/parity/README.md +140 -0
  868. package/src/scripts/parity/compare.ts +189 -0
  869. package/src/scripts/parity/coverage_diff.ts +199 -0
  870. package/src/scripts/parity/phase-manifest.json +93 -0
  871. package/src/scripts/parity/phase_gate.ts +270 -0
  872. package/src/scripts/parity/replay.ts +320 -0
  873. package/src/scripts/pattern_share.ts +363 -0
  874. package/src/scripts/plan_physical_move.ts +605 -0
  875. package/src/scripts/prediction-pool/poisson_sim.ts +537 -0
  876. package/src/scripts/prediction-pool/pool_winsim.ts +677 -0
  877. package/src/scripts/prediction-pool/score_ev.ts +546 -0
  878. package/src/scripts/print_required_checks.ts +249 -0
  879. package/src/scripts/probe_projection_fidelity.ts +468 -0
  880. package/src/scripts/probe_skill_registration.ts +787 -0
  881. package/src/scripts/profile_staleness_hook.ts +169 -0
  882. package/src/scripts/profile_use.ts +227 -0
  883. package/src/scripts/project_thin_rules.ts +387 -0
  884. package/src/scripts/propose_modules_config.ts +311 -0
  885. package/src/scripts/prototype_lint_contradictions.ts +414 -0
  886. package/src/scripts/prove_pack_extractable.ts +388 -0
  887. package/src/scripts/readme_linter.ts +913 -0
  888. package/src/scripts/redact_hook_capture.ts +325 -0
  889. package/src/scripts/refine_ticket_detect.ts +703 -0
  890. package/src/scripts/release.ts +1748 -0
  891. package/src/scripts/render_benchmark_md.ts +664 -0
  892. package/src/scripts/render_value_md.ts +506 -0
  893. package/src/scripts/repro/repro_marketplace_install_gap.sh +1 -1
  894. package/src/scripts/roadmap_progress_hook.ts +410 -0
  895. package/src/scripts/router_telemetry.ts +972 -0
  896. package/src/scripts/run.ts +98 -0
  897. package/src/scripts/run_skill_evals.ts +477 -0
  898. package/src/scripts/runtime_dispatcher.ts +586 -0
  899. package/src/scripts/runtime_handler.ts +231 -0
  900. package/src/scripts/runtime_registry.ts +394 -0
  901. package/src/scripts/schemas/command.schema.json +3 -2
  902. package/src/scripts/schemas/mission-catalog.schema.json +112 -0
  903. package/src/scripts/schemas/mission.schema.json +87 -0
  904. package/src/scripts/schemas/pack.schema.json +6 -0
  905. package/src/scripts/schemas/rule.schema.json +1 -0
  906. package/src/scripts/schemas/skill.schema.json +1 -0
  907. package/src/scripts/schemas/ticket-manifest.schema.json +35 -0
  908. package/src/scripts/schemas/ticket.schema.json +60 -0
  909. package/src/scripts/score_skill_selection.ts +570 -0
  910. package/src/scripts/security_audit_config.ts +423 -0
  911. package/src/scripts/skill_collision_clusters.ts +448 -0
  912. package/src/scripts/skill_discovery.ts +690 -0
  913. package/src/scripts/skill_linter.ts +4276 -0
  914. package/src/scripts/skill_overlap.ts +414 -0
  915. package/src/scripts/skill_preview.ts +548 -0
  916. package/src/scripts/skill_tools/audit_persona_coverage.ts +427 -0
  917. package/src/scripts/skill_tools/audit_user_type_coverage.ts +507 -0
  918. package/src/scripts/skill_tools/index.ts +28 -0
  919. package/src/scripts/skill_tools/run_block_d_eval.ts +373 -0
  920. package/src/scripts/skill_tools/score_skill_relevance.ts +475 -0
  921. package/src/scripts/skill_tools/suggest_skill_for_task.ts +288 -0
  922. package/src/scripts/skill_trigger_eval.ts +1046 -0
  923. package/src/scripts/skill_usage_collect.ts +465 -0
  924. package/src/scripts/skill_usage_report.ts +364 -0
  925. package/src/scripts/smoke/kernel.sh +4 -5
  926. package/src/scripts/smoke/router.sh +76 -76
  927. package/src/scripts/smoke/schema.sh +2 -2
  928. package/src/scripts/smoke/skills.sh +73 -52
  929. package/src/scripts/smoke_path_resolution.ts +194 -0
  930. package/src/scripts/smoke_quickstart.ts +224 -0
  931. package/src/scripts/snapshot_agent_outputs.ts +375 -0
  932. package/src/scripts/spotcheck_thin_root.ts +247 -0
  933. package/src/scripts/surface-tiers.yml +68 -0
  934. package/src/scripts/sync_agent_settings.ts +763 -0
  935. package/src/scripts/sync_github_metadata.ts +550 -0
  936. package/src/scripts/sync_gitignore.ts +630 -0
  937. package/src/scripts/sync_yaml_rt.ts +910 -0
  938. package/src/scripts/telegraph_stats.ts +447 -0
  939. package/src/scripts/tool_registry.ts +330 -0
  940. package/src/scripts/tools/adapter_errors.ts +93 -0
  941. package/src/scripts/tools/base_adapter.ts +147 -0
  942. package/src/scripts/tools/github_adapter.ts +229 -0
  943. package/src/scripts/tools/jira_adapter.ts +196 -0
  944. package/src/scripts/trigger_coverage.ts +251 -0
  945. package/src/scripts/update_counts.ts +284 -0
  946. package/src/scripts/update_prices.ts +219 -0
  947. package/src/scripts/validate_agent_settings.ts +265 -0
  948. package/src/scripts/validate_decision_engine.ts +366 -0
  949. package/src/scripts/validate_discovery_manifest.ts +160 -0
  950. package/src/scripts/validate_frontmatter.ts +1030 -0
  951. package/src/scripts/validate_pack_yaml.ts +0 -0
  952. package/src/scripts/validate_safe_paths.ts +164 -0
  953. package/src/scripts/validate_telegraph_carveouts.ts +485 -0
  954. package/src/scripts/verify_before_complete_hook.ts +306 -0
  955. package/src/scripts/verify_physical_move.ts +411 -0
  956. package/src/scripts/wrapper_freshness_hook.ts +179 -0
  957. package/dist/agent-src/scripts/archive_completed_roadmaps.py +0 -171
  958. package/dist/agent-src/scripts/update_roadmap_progress.py +0 -537
  959. package/dist/agent-src/skills/corpus-grounding/scripts/bm25_search.py +0 -212
  960. package/dist/agent-src/skills/corpus-grounding/scripts/decision_engine.py +0 -438
  961. package/dist/agent-src/skills/corpus-grounding/scripts/ground.py +0 -166
  962. package/dist/agent-src/skills/corpus-grounding/scripts/schema_validator.py +0 -160
  963. package/dist/agent-src/skills/design-tokens/scripts/tokens.py +0 -296
  964. package/dist/agent-src/skills/react-shadcn-ui/scripts/shadcn_add.py +0 -299
  965. package/dist/agent-src/skills/tailwind-engineer/scripts/tailwind_config_gen.py +0 -463
  966. package/dist/agent-src/templates/scripts/check_memory.py +0 -282
  967. package/dist/agent-src/templates/scripts/check_memory_proposal.py +0 -180
  968. package/dist/agent-src/templates/scripts/implement_ticket/__init__.py +0 -94
  969. package/dist/agent-src/templates/scripts/implement_ticket/__main__.py +0 -15
  970. package/dist/agent-src/templates/scripts/memory_hash.py +0 -75
  971. package/dist/agent-src/templates/scripts/memory_lookup.py +0 -436
  972. package/dist/agent-src/templates/scripts/memory_report.py +0 -314
  973. package/dist/agent-src/templates/scripts/memory_signal.py +0 -165
  974. package/dist/agent-src/templates/scripts/memory_status.py +0 -76
  975. package/dist/agent-src/templates/scripts/pr_review_routing.py +0 -340
  976. package/dist/agent-src/templates/scripts/pr_risk_review.py +0 -211
  977. package/dist/agent-src/templates/scripts/telemetry/__init__.py +0 -42
  978. package/dist/agent-src/templates/scripts/telemetry/aggregator.py +0 -169
  979. package/dist/agent-src/templates/scripts/telemetry/boundary.py +0 -171
  980. package/dist/agent-src/templates/scripts/telemetry/engagement.py +0 -297
  981. package/dist/agent-src/templates/scripts/telemetry/report_renderer.py +0 -197
  982. package/dist/agent-src/templates/scripts/telemetry/settings.py +0 -177
  983. package/dist/agent-src/templates/scripts/telemetry_record.py +0 -179
  984. package/dist/agent-src/templates/scripts/telemetry_report.py +0 -161
  985. package/dist/agent-src/templates/scripts/telemetry_status.py +0 -142
  986. package/dist/agent-src/templates/scripts/tier_usage_report.py +0 -183
  987. package/dist/agent-src/templates/scripts/work_engine/__init__.py +0 -58
  988. package/dist/agent-src/templates/scripts/work_engine/__main__.py +0 -9
  989. package/dist/agent-src/templates/scripts/work_engine/_lib/__init__.py +0 -7
  990. package/dist/agent-src/templates/scripts/work_engine/_lib/agent_settings.py +0 -840
  991. package/dist/agent-src/templates/scripts/work_engine/_lib/user_global_paths.py +0 -249
  992. package/dist/agent-src/templates/scripts/work_engine/cli.py +0 -195
  993. package/dist/agent-src/templates/scripts/work_engine/cli_args.py +0 -116
  994. package/dist/agent-src/templates/scripts/work_engine/delivery_state.py +0 -137
  995. package/dist/agent-src/templates/scripts/work_engine/directives/__init__.py +0 -33
  996. package/dist/agent-src/templates/scripts/work_engine/directives/backend/__init__.py +0 -98
  997. package/dist/agent-src/templates/scripts/work_engine/directives/backend/analyze.py +0 -98
  998. package/dist/agent-src/templates/scripts/work_engine/directives/backend/implement.py +0 -145
  999. package/dist/agent-src/templates/scripts/work_engine/directives/backend/memory.py +0 -136
  1000. package/dist/agent-src/templates/scripts/work_engine/directives/backend/plan.py +0 -175
  1001. package/dist/agent-src/templates/scripts/work_engine/directives/backend/refine.py +0 -396
  1002. package/dist/agent-src/templates/scripts/work_engine/directives/backend/report.py +0 -227
  1003. package/dist/agent-src/templates/scripts/work_engine/directives/backend/test.py +0 -180
  1004. package/dist/agent-src/templates/scripts/work_engine/directives/backend/verify.py +0 -170
  1005. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/__init__.py +0 -116
  1006. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/contract.py +0 -254
  1007. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/stitch.py +0 -229
  1008. package/dist/agent-src/templates/scripts/work_engine/directives/mixed/ui.py +0 -231
  1009. package/dist/agent-src/templates/scripts/work_engine/directives/ui/__init__.py +0 -113
  1010. package/dist/agent-src/templates/scripts/work_engine/directives/ui/_passthrough.py +0 -44
  1011. package/dist/agent-src/templates/scripts/work_engine/directives/ui/apply.py +0 -241
  1012. package/dist/agent-src/templates/scripts/work_engine/directives/ui/audit.py +0 -414
  1013. package/dist/agent-src/templates/scripts/work_engine/directives/ui/design.py +0 -335
  1014. package/dist/agent-src/templates/scripts/work_engine/directives/ui/polish.py +0 -513
  1015. package/dist/agent-src/templates/scripts/work_engine/directives/ui/review.py +0 -471
  1016. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/__init__.py +0 -119
  1017. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/_skipped.py +0 -37
  1018. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/apply.py +0 -165
  1019. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/refine.py +0 -66
  1020. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/report.py +0 -62
  1021. package/dist/agent-src/templates/scripts/work_engine/directives/ui_trivial/test.py +0 -115
  1022. package/dist/agent-src/templates/scripts/work_engine/dispatcher.py +0 -331
  1023. package/dist/agent-src/templates/scripts/work_engine/emitters.py +0 -68
  1024. package/dist/agent-src/templates/scripts/work_engine/errors.py +0 -19
  1025. package/dist/agent-src/templates/scripts/work_engine/hook_bootstrap.py +0 -91
  1026. package/dist/agent-src/templates/scripts/work_engine/hooks/__init__.py +0 -54
  1027. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/__init__.py +0 -35
  1028. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/_chat_history_base.py +0 -59
  1029. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/chat_history_append.py +0 -43
  1030. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/chat_history_halt_append.py +0 -41
  1031. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/decision_gate.py +0 -162
  1032. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/decision_trace.py +0 -163
  1033. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/directive_set_guard.py +0 -53
  1034. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/halt_surface_audit.py +0 -50
  1035. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/memory_visibility.py +0 -141
  1036. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/state_shape_validation.py +0 -52
  1037. package/dist/agent-src/templates/scripts/work_engine/hooks/builtin/trace.py +0 -84
  1038. package/dist/agent-src/templates/scripts/work_engine/hooks/context.py +0 -66
  1039. package/dist/agent-src/templates/scripts/work_engine/hooks/events.py +0 -44
  1040. package/dist/agent-src/templates/scripts/work_engine/hooks/exceptions.py +0 -79
  1041. package/dist/agent-src/templates/scripts/work_engine/hooks/registry.py +0 -60
  1042. package/dist/agent-src/templates/scripts/work_engine/hooks/runner.py +0 -73
  1043. package/dist/agent-src/templates/scripts/work_engine/hooks/settings.py +0 -196
  1044. package/dist/agent-src/templates/scripts/work_engine/input_builders.py +0 -163
  1045. package/dist/agent-src/templates/scripts/work_engine/intent/__init__.py +0 -47
  1046. package/dist/agent-src/templates/scripts/work_engine/intent/classify.py +0 -280
  1047. package/dist/agent-src/templates/scripts/work_engine/migration/__init__.py +0 -8
  1048. package/dist/agent-src/templates/scripts/work_engine/migration/v0_to_v1.py +0 -231
  1049. package/dist/agent-src/templates/scripts/work_engine/orchestration.py +0 -193
  1050. package/dist/agent-src/templates/scripts/work_engine/persona_policy.py +0 -85
  1051. package/dist/agent-src/templates/scripts/work_engine/resolvers/__init__.py +0 -22
  1052. package/dist/agent-src/templates/scripts/work_engine/resolvers/diff.py +0 -106
  1053. package/dist/agent-src/templates/scripts/work_engine/resolvers/file.py +0 -113
  1054. package/dist/agent-src/templates/scripts/work_engine/resolvers/prompt.py +0 -90
  1055. package/dist/agent-src/templates/scripts/work_engine/scoring/__init__.py +0 -14
  1056. package/dist/agent-src/templates/scripts/work_engine/scoring/confidence.py +0 -300
  1057. package/dist/agent-src/templates/scripts/work_engine/scoring/decision_engine.py +0 -351
  1058. package/dist/agent-src/templates/scripts/work_engine/scoring/decision_trace.py +0 -141
  1059. package/dist/agent-src/templates/scripts/work_engine/scoring/memory_visibility.py +0 -283
  1060. package/dist/agent-src/templates/scripts/work_engine/stack/__init__.py +0 -31
  1061. package/dist/agent-src/templates/scripts/work_engine/stack/detect.py +0 -187
  1062. package/dist/agent-src/templates/scripts/work_engine/stack/runner.py +0 -481
  1063. package/dist/agent-src/templates/scripts/work_engine/state.py +0 -694
  1064. package/dist/agent-src/templates/scripts/work_engine/state_io.py +0 -202
  1065. package/dist/cli/python/resolvePython.js +0 -38
  1066. package/dist/cli/python/resolvePython.js.map +0 -1
  1067. package/src/scripts/__pycache__/validate_frontmatter.cpython-312.pyc +0 -0
  1068. package/src/scripts/_archive/_backfill_skill_domains.py +0 -140
  1069. package/src/scripts/_archive/_bootstrap_tier_frontmatter.py +0 -151
  1070. package/src/scripts/_archive/_p43_bodies.py +0 -235
  1071. package/src/scripts/_archive/_p43_condense.py +0 -118
  1072. package/src/scripts/_archive/_p4_migrate.py +0 -199
  1073. package/src/scripts/_archive/_phase2_shim_helper.py +0 -109
  1074. package/src/scripts/_archive/_pilot_council_question.py +0 -57
  1075. package/src/scripts/_cli/__init__.py +0 -0
  1076. package/src/scripts/_cli/cmd_doctor.py +0 -1669
  1077. package/src/scripts/_cli/cmd_explain.py +0 -355
  1078. package/src/scripts/_cli/cmd_export.py +0 -157
  1079. package/src/scripts/_cli/cmd_migrate.py +0 -524
  1080. package/src/scripts/_cli/cmd_prune.py +0 -322
  1081. package/src/scripts/_cli/cmd_refresh.py +0 -179
  1082. package/src/scripts/_cli/cmd_settings_check.py +0 -171
  1083. package/src/scripts/_cli/cmd_settings_migrate.py +0 -147
  1084. package/src/scripts/_cli/cmd_sync.py +0 -166
  1085. package/src/scripts/_cli/cmd_uninstall.py +0 -476
  1086. package/src/scripts/_cli/cmd_update.py +0 -279
  1087. package/src/scripts/_cli/cmd_upgrade.py +0 -172
  1088. package/src/scripts/_cli/cmd_validate.py +0 -177
  1089. package/src/scripts/_cli/cmd_versions.py +0 -160
  1090. package/src/scripts/_cli/explain_last/__init__.py +0 -122
  1091. package/src/scripts/_cli/explain_last/assumptions.py +0 -59
  1092. package/src/scripts/_cli/explain_last/council.py +0 -105
  1093. package/src/scripts/_cli/explain_last/halt.py +0 -44
  1094. package/src/scripts/_cli/explain_last/inputs.py +0 -128
  1095. package/src/scripts/_cli/explain_last/memory.py +0 -94
  1096. package/src/scripts/_cli/explain_last/provider.py +0 -52
  1097. package/src/scripts/_cli/explain_last/render.py +0 -52
  1098. package/src/scripts/_cli/explain_last/route.py +0 -59
  1099. package/src/scripts/_cli/explain_last/scrubber.py +0 -105
  1100. package/src/scripts/_cli/explain_last/sections/__init__.py +0 -35
  1101. package/src/scripts/_cli/explain_last/sections/assumptions.py +0 -21
  1102. package/src/scripts/_cli/explain_last/sections/council.py +0 -27
  1103. package/src/scripts/_cli/explain_last/sections/halt.py +0 -31
  1104. package/src/scripts/_cli/explain_last/sections/header.py +0 -24
  1105. package/src/scripts/_cli/explain_last/sections/inputs.py +0 -27
  1106. package/src/scripts/_cli/explain_last/sections/memory.py +0 -21
  1107. package/src/scripts/_cli/explain_last/sections/pack.py +0 -16
  1108. package/src/scripts/_cli/explain_last/sections/provider.py +0 -26
  1109. package/src/scripts/_cli/explain_last/sections/route.py +0 -22
  1110. package/src/scripts/_cli/explain_last/state_loader.py +0 -76
  1111. package/src/scripts/_emit_domain_table.py +0 -35
  1112. package/src/scripts/_lib/__init__.py +0 -5
  1113. package/src/scripts/_lib/__pycache__/__init__.cpython-312.pyc +0 -0
  1114. package/src/scripts/_lib/__pycache__/agent_src.cpython-312.pyc +0 -0
  1115. package/src/scripts/_lib/agent_settings.py +0 -840
  1116. package/src/scripts/_lib/agent_src.py +0 -491
  1117. package/src/scripts/_lib/agents_overlay.py +0 -120
  1118. package/src/scripts/_lib/bench_ab_cache.py +0 -162
  1119. package/src/scripts/_lib/bench_ab_scoring.py +0 -209
  1120. package/src/scripts/_lib/bench_ab_scoring_v2.py +0 -227
  1121. package/src/scripts/_lib/bench_cost.py +0 -138
  1122. package/src/scripts/_lib/bench_quality.py +0 -118
  1123. package/src/scripts/_lib/bench_report.py +0 -149
  1124. package/src/scripts/_lib/bench_telegraph.py +0 -273
  1125. package/src/scripts/_lib/bench_telegraph_report.py +0 -151
  1126. package/src/scripts/_lib/changelog_eras.py +0 -330
  1127. package/src/scripts/_lib/claude_desktop_bundler.py +0 -238
  1128. package/src/scripts/_lib/cli_wrapper.py +0 -64
  1129. package/src/scripts/_lib/fs_atomic.py +0 -116
  1130. package/src/scripts/_lib/global_deploy_inventory.py +0 -312
  1131. package/src/scripts/_lib/install_regenerator.py +0 -134
  1132. package/src/scripts/_lib/installed_lock.py +0 -256
  1133. package/src/scripts/_lib/installed_tools.py +0 -381
  1134. package/src/scripts/_lib/json_pointers.py +0 -260
  1135. package/src/scripts/_lib/link_crypto.py +0 -206
  1136. package/src/scripts/_lib/linked_projects.py +0 -238
  1137. package/src/scripts/_lib/model_tier.py +0 -52
  1138. package/src/scripts/_lib/module_detection.py +0 -223
  1139. package/src/scripts/_lib/pin_resolver.py +0 -152
  1140. package/src/scripts/_lib/script_output.py +0 -144
  1141. package/src/scripts/_lib/security_lint.py +0 -228
  1142. package/src/scripts/_lib/token_count.py +0 -95
  1143. package/src/scripts/_lib/update_check.py +0 -207
  1144. package/src/scripts/_lib/user_global_paths.py +0 -249
  1145. package/src/scripts/_lib/value_ladder.py +0 -696
  1146. package/src/scripts/_lib/value_report.py +0 -455
  1147. package/src/scripts/_phase4_bucket.py +0 -210
  1148. package/src/scripts/_pilot_measure.py +0 -53
  1149. package/src/scripts/_tmp_scan_framework_leakage.py +0 -119
  1150. package/src/scripts/adoption_report.py +0 -195
  1151. package/src/scripts/adoption_snapshot.py +0 -219
  1152. package/src/scripts/adoption_status.py +0 -166
  1153. package/src/scripts/adr/regenerate_index.py +0 -79
  1154. package/src/scripts/ai-video/lib/adapter-common.sh +0 -231
  1155. package/src/scripts/ai-video/lib/adapter-contract.md +0 -329
  1156. package/src/scripts/ai-video/lib/fixtures/comfyui/result.json +0 -1
  1157. package/src/scripts/ai-video/lib/fixtures/fal/result.json +0 -1
  1158. package/src/scripts/ai-video/lib/fixtures/gemini-veo/result.json +0 -1
  1159. package/src/scripts/ai-video/lib/fixtures/higgsfield/result.json +0 -1
  1160. package/src/scripts/ai-video/lib/fixtures/kling/result.json +0 -1
  1161. package/src/scripts/ai-video/lib/fixtures/musetalk/result.json +0 -1
  1162. package/src/scripts/ai-video/lib/fixtures/openai-images/result.json +0 -1
  1163. package/src/scripts/ai-video/lib/fixtures/replicate/result.json +0 -1
  1164. package/src/scripts/ai-video/lib/fixtures/sora/result.json +0 -1
  1165. package/src/scripts/ai-video/lib/fixtures/syncso/result.json +0 -1
  1166. package/src/scripts/ai-video/lib/load-config.sh +0 -180
  1167. package/src/scripts/ai-video/lib/redact.sh +0 -85
  1168. package/src/scripts/ai_council/__init__.py +0 -40
  1169. package/src/scripts/ai_council/_default_prices.py +0 -50
  1170. package/src/scripts/ai_council/advisors.py +0 -148
  1171. package/src/scripts/ai_council/airgap.py +0 -165
  1172. package/src/scripts/ai_council/budget_guard.py +0 -202
  1173. package/src/scripts/ai_council/bundler.py +0 -263
  1174. package/src/scripts/ai_council/cli_hints.py +0 -123
  1175. package/src/scripts/ai_council/clients.py +0 -1385
  1176. package/src/scripts/ai_council/compile_corpus.py +0 -179
  1177. package/src/scripts/ai_council/confidence_gate.py +0 -156
  1178. package/src/scripts/ai_council/config.py +0 -1419
  1179. package/src/scripts/ai_council/consensus.py +0 -329
  1180. package/src/scripts/ai_council/events_log.py +0 -141
  1181. package/src/scripts/ai_council/learn_low_impact_preview.py +0 -252
  1182. package/src/scripts/ai_council/low_impact.py +0 -714
  1183. package/src/scripts/ai_council/low_impact_corpus.py +0 -466
  1184. package/src/scripts/ai_council/low_impact_intake.py +0 -163
  1185. package/src/scripts/ai_council/modes.py +0 -131
  1186. package/src/scripts/ai_council/necessity.py +0 -782
  1187. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_2a4_acceptance.py +0 -208
  1188. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_add_quiet.py +0 -149
  1189. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_budget_v2_audit.py +0 -206
  1190. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_context_layer_v1_estimate.py +0 -67
  1191. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_context_layer_v1_review.py +0 -292
  1192. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_followups_review.py +0 -259
  1193. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_inject_quiet_flag.py +0 -33
  1194. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_measure_v2.sh +0 -36
  1195. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_measure_verbosity.sh +0 -26
  1196. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_nondestructive_inline_audit.py +0 -209
  1197. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_per_task.sh +0 -41
  1198. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_phase4_dispatch_latency.py +0 -108
  1199. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_phase6_trigger_jaccard.py +0 -92
  1200. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_phase_2a_budget_rebalance.py +0 -257
  1201. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_phase_2a_post_revert.py +0 -197
  1202. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_rebalancing_audit.py +0 -149
  1203. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_roundtrip.py +0 -111
  1204. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_rule_hardening_v1.py +0 -251
  1205. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_silent_taskfiles.py +0 -98
  1206. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_structural_open_questions.py +0 -232
  1207. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_structural_optimization.py +0 -144
  1208. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_structural_v3_gaps.py +0 -252
  1209. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_structural_v3_review.py +0 -240
  1210. package/src/scripts/ai_council/one_off_archive/2026-05/_one_off_tier_retrofit.py +0 -180
  1211. package/src/scripts/ai_council/orchestrator.py +0 -1206
  1212. package/src/scripts/ai_council/pricing.py +0 -215
  1213. package/src/scripts/ai_council/probation_gate.py +0 -152
  1214. package/src/scripts/ai_council/project_context.py +0 -159
  1215. package/src/scripts/ai_council/prompts.py +0 -567
  1216. package/src/scripts/ai_council/redact_low_impact_entry.py +0 -155
  1217. package/src/scripts/ai_council/replay.py +0 -155
  1218. package/src/scripts/ai_council/session.py +0 -366
  1219. package/src/scripts/ai_council/shadow_dispatch.py +0 -235
  1220. package/src/scripts/ai_council/solo_dispatch.py +0 -226
  1221. package/src/scripts/annotate_discovery.py +0 -149
  1222. package/src/scripts/apply_modules_config.py +0 -290
  1223. package/src/scripts/audit_adr_coverage.py +0 -173
  1224. package/src/scripts/audit_auto_rules.py +0 -175
  1225. package/src/scripts/audit_cloud_compatibility.py +0 -362
  1226. package/src/scripts/audit_command_surface.py +0 -682
  1227. package/src/scripts/audit_initial_context.py +0 -237
  1228. package/src/scripts/audit_likelihood.py +0 -148
  1229. package/src/scripts/audit_mcp_tools.py +0 -146
  1230. package/src/scripts/audit_overlap.py +0 -145
  1231. package/src/scripts/audit_skill_descriptions.py +0 -180
  1232. package/src/scripts/audit_skill_overlap.py +0 -207
  1233. package/src/scripts/audit_user_type_axis.py +0 -140
  1234. package/src/scripts/backfill_model_tier.py +0 -184
  1235. package/src/scripts/bench_ab_cache_dispatch.py +0 -68
  1236. package/src/scripts/bench_ab_clone.py +0 -220
  1237. package/src/scripts/bench_ab_diff.py +0 -220
  1238. package/src/scripts/bench_ab_integrity.py +0 -143
  1239. package/src/scripts/bench_ab_run.py +0 -235
  1240. package/src/scripts/bench_ab_task_runner.py +0 -814
  1241. package/src/scripts/bench_ab_tracka_run.py +0 -202
  1242. package/src/scripts/bench_ab_v2_run.py +0 -247
  1243. package/src/scripts/bench_ab_v2_stats.py +0 -347
  1244. package/src/scripts/bench_baseline_ready.py +0 -108
  1245. package/src/scripts/bench_condense_memory.py +0 -168
  1246. package/src/scripts/bench_drift_check.py +0 -151
  1247. package/src/scripts/bench_per_tool.py +0 -216
  1248. package/src/scripts/bench_rtk_savings.py +0 -320
  1249. package/src/scripts/bench_run.py +0 -272
  1250. package/src/scripts/bench_runner.py +0 -158
  1251. package/src/scripts/build_cloud_bundle.py +0 -458
  1252. package/src/scripts/build_discovery_manifest.py +0 -757
  1253. package/src/scripts/build_linear_digest.py +0 -260
  1254. package/src/scripts/build_mcp_registry_manifest.py +0 -181
  1255. package/src/scripts/build_rule_trigger_matrix.py +0 -350
  1256. package/src/scripts/capture_showcase_session.py +0 -361
  1257. package/src/scripts/chat_history.py +0 -1799
  1258. package/src/scripts/check_always_budget.py +0 -532
  1259. package/src/scripts/check_artefact_checksums.py +0 -111
  1260. package/src/scripts/check_augment_description_cap.py +0 -79
  1261. package/src/scripts/check_augmentignore.py +0 -72
  1262. package/src/scripts/check_beta_review_markers.py +0 -127
  1263. package/src/scripts/check_bite_sized_granularity.py +0 -98
  1264. package/src/scripts/check_cluster_patterns.py +0 -206
  1265. package/src/scripts/check_command_count_messaging.py +0 -152
  1266. package/src/scripts/check_condensation.py +0 -375
  1267. package/src/scripts/check_condensed_paths.py +0 -231
  1268. package/src/scripts/check_context_paths.py +0 -202
  1269. package/src/scripts/check_council_layout.py +0 -125
  1270. package/src/scripts/check_council_references.py +0 -228
  1271. package/src/scripts/check_discovery_determinism.py +0 -70
  1272. package/src/scripts/check_gate_paths.py +0 -128
  1273. package/src/scripts/check_iron_law_prominence.py +0 -145
  1274. package/src/scripts/check_kernel_rule_bundle.py +0 -151
  1275. package/src/scripts/check_md_language.py +0 -161
  1276. package/src/scripts/check_memory.py +0 -429
  1277. package/src/scripts/check_memory_proposal.py +0 -182
  1278. package/src/scripts/check_module_management_neutral.py +0 -147
  1279. package/src/scripts/check_no_external_sources.py +0 -101
  1280. package/src/scripts/check_no_local_settings_committed.py +0 -51
  1281. package/src/scripts/check_no_new_legacy_path.py +0 -100
  1282. package/src/scripts/check_no_roadmap_refs.py +0 -155
  1283. package/src/scripts/check_one_off_location.py +0 -81
  1284. package/src/scripts/check_overlay_cascade_subdirs.py +0 -129
  1285. package/src/scripts/check_portability.py +0 -574
  1286. package/src/scripts/check_proposal.py +0 -269
  1287. package/src/scripts/check_public_catalog_links.py +0 -125
  1288. package/src/scripts/check_public_links.py +0 -185
  1289. package/src/scripts/check_references.py +0 -559
  1290. package/src/scripts/check_release_includes_discovery.py +0 -61
  1291. package/src/scripts/check_release_pr_shape.py +0 -123
  1292. package/src/scripts/check_release_published.py +0 -145
  1293. package/src/scripts/check_release_trunk_sync.py +0 -152
  1294. package/src/scripts/check_reply_consistency.py +0 -169
  1295. package/src/scripts/check_roadmap_trackable.py +0 -114
  1296. package/src/scripts/check_role_doc_links.py +0 -110
  1297. package/src/scripts/check_safety_floor_untouched.py +0 -125
  1298. package/src/scripts/check_skill_requires.py +0 -147
  1299. package/src/scripts/check_template_pin_drift.py +0 -129
  1300. package/src/scripts/check_test_coverage_diff.py +0 -180
  1301. package/src/scripts/check_token_optimizer_freshness.py +0 -146
  1302. package/src/scripts/check_update_banner.py +0 -86
  1303. package/src/scripts/ci_status.py +0 -301
  1304. package/src/scripts/ci_summary.py +0 -131
  1305. package/src/scripts/ci_time_ratio.py +0 -168
  1306. package/src/scripts/command_suggester/__init__.py +0 -51
  1307. package/src/scripts/command_suggester/cooldown.py +0 -132
  1308. package/src/scripts/command_suggester/loader.py +0 -73
  1309. package/src/scripts/command_suggester/match.py +0 -180
  1310. package/src/scripts/command_suggester/rank.py +0 -120
  1311. package/src/scripts/command_suggester/render.py +0 -86
  1312. package/src/scripts/command_suggester/sanitize.py +0 -113
  1313. package/src/scripts/command_suggester/settings.py +0 -127
  1314. package/src/scripts/command_suggester/types.py +0 -78
  1315. package/src/scripts/compile_router.py +0 -232
  1316. package/src/scripts/condense.py +0 -1919
  1317. package/src/scripts/condense_memory.py +0 -178
  1318. package/src/scripts/config/__init__.py +0 -9
  1319. package/src/scripts/config/packs.py +0 -157
  1320. package/src/scripts/config/presets.py +0 -224
  1321. package/src/scripts/config/profile_explain.py +0 -89
  1322. package/src/scripts/config/profiles.py +0 -191
  1323. package/src/scripts/config/session_profiles.py +0 -542
  1324. package/src/scripts/context_hygiene_hook.py +0 -181
  1325. package/src/scripts/cost_by_conversation.py +0 -78
  1326. package/src/scripts/cost_summary.py +0 -97
  1327. package/src/scripts/council_cli.py +0 -2571
  1328. package/src/scripts/council_prune.py +0 -81
  1329. package/src/scripts/cross_repo_retrieve.py +0 -172
  1330. package/src/scripts/discovery_stats.py +0 -70
  1331. package/src/scripts/extract_audit_patterns.py +0 -202
  1332. package/src/scripts/first_run_gate_hook.py +0 -179
  1333. package/src/scripts/gen_discovery_baseline.py +0 -127
  1334. package/src/scripts/generate_catalog.py +0 -116
  1335. package/src/scripts/generate_command_flows.py +0 -191
  1336. package/src/scripts/generate_index.py +0 -303
  1337. package/src/scripts/generate_ownership_matrix.py +0 -378
  1338. package/src/scripts/generate_pack_manifests.py +0 -340
  1339. package/src/scripts/hooks/__init__.py +0 -1
  1340. package/src/scripts/hooks/dispatch_hook.py +0 -461
  1341. package/src/scripts/hooks/dispatch_issues.py +0 -136
  1342. package/src/scripts/hooks/envelope.py +0 -98
  1343. package/src/scripts/hooks/replay_hook.py +0 -144
  1344. package/src/scripts/hooks/state_io.py +0 -145
  1345. package/src/scripts/hooks_doctor.py +0 -223
  1346. package/src/scripts/hooks_status.py +0 -157
  1347. package/src/scripts/injection_scan_hook.py +0 -145
  1348. package/src/scripts/install.py +0 -5258
  1349. package/src/scripts/inventory_abstraction_budget.py +0 -622
  1350. package/src/scripts/inventory_frontmatter.py +0 -164
  1351. package/src/scripts/inventory_meta_layers.py +0 -288
  1352. package/src/scripts/iron_law_sha.py +0 -107
  1353. package/src/scripts/linked_projects_list.py +0 -91
  1354. package/src/scripts/lint_agent_security.py +0 -112
  1355. package/src/scripts/lint_agent_skill_names.py +0 -150
  1356. package/src/scripts/lint_agents_layout.py +0 -197
  1357. package/src/scripts/lint_agents_md.py +0 -210
  1358. package/src/scripts/lint_archived_skills.py +0 -159
  1359. package/src/scripts/lint_artefact_frontmatter.py +0 -188
  1360. package/src/scripts/lint_bench_ab.py +0 -173
  1361. package/src/scripts/lint_bench_corpus.py +0 -255
  1362. package/src/scripts/lint_command_flow_coverage.py +0 -132
  1363. package/src/scripts/lint_command_routing.py +0 -160
  1364. package/src/scripts/lint_command_tiers.py +0 -216
  1365. package/src/scripts/lint_command_verbs.py +0 -206
  1366. package/src/scripts/lint_commit_subjects.py +0 -139
  1367. package/src/scripts/lint_context_spine_usage.py +0 -137
  1368. package/src/scripts/lint_discovery_manifest.py +0 -176
  1369. package/src/scripts/lint_discovery_vocabulary.py +0 -222
  1370. package/src/scripts/lint_empty_roadmaps.py +0 -80
  1371. package/src/scripts/lint_examples.py +0 -102
  1372. package/src/scripts/lint_explain_trace.py +0 -80
  1373. package/src/scripts/lint_featured_skills.py +0 -144
  1374. package/src/scripts/lint_flows.py +0 -215
  1375. package/src/scripts/lint_framework_leakage.py +0 -375
  1376. package/src/scripts/lint_frontmatter_boilerplate.py +0 -77
  1377. package/src/scripts/lint_ghostwriter_source.py +0 -242
  1378. package/src/scripts/lint_global_paths.py +0 -148
  1379. package/src/scripts/lint_handoffs.py +0 -217
  1380. package/src/scripts/lint_hidden_unicode.py +0 -132
  1381. package/src/scripts/lint_hook_concern_budget.py +0 -207
  1382. package/src/scripts/lint_hook_manifest.py +0 -217
  1383. package/src/scripts/lint_instruction_smuggling.py +0 -107
  1384. package/src/scripts/lint_load_context.py +0 -196
  1385. package/src/scripts/lint_marketplace.py +0 -180
  1386. package/src/scripts/lint_marketplace_install_completeness.py +0 -198
  1387. package/src/scripts/lint_mcp_config_security.py +0 -124
  1388. package/src/scripts/lint_mcp_registry_manifest.py +0 -69
  1389. package/src/scripts/lint_media_policy_linkage.py +0 -140
  1390. package/src/scripts/lint_model_tier_coverage.py +0 -73
  1391. package/src/scripts/lint_namespace.py +0 -135
  1392. package/src/scripts/lint_namespace_collisions.py +0 -103
  1393. package/src/scripts/lint_new_skill_gate.py +0 -144
  1394. package/src/scripts/lint_no_new_atomic_commands.py +0 -180
  1395. package/src/scripts/lint_one_off_age.py +0 -184
  1396. package/src/scripts/lint_orchestration_dsl.py +0 -217
  1397. package/src/scripts/lint_orchestrator_auto_detect.py +0 -111
  1398. package/src/scripts/lint_pack_boundaries.py +0 -147
  1399. package/src/scripts/lint_pack_dependencies.py +0 -137
  1400. package/src/scripts/lint_pack_first_win.py +0 -121
  1401. package/src/scripts/lint_persona_governance.py +0 -164
  1402. package/src/scripts/lint_positioning.py +0 -143
  1403. package/src/scripts/lint_profile_overlay_set_only.py +0 -179
  1404. package/src/scripts/lint_readme_jargon.py +0 -131
  1405. package/src/scripts/lint_readme_size.py +0 -33
  1406. package/src/scripts/lint_regression.py +0 -251
  1407. package/src/scripts/lint_roadmap_ci_steps.py +0 -186
  1408. package/src/scripts/lint_roadmap_complexity.py +0 -220
  1409. package/src/scripts/lint_role_experiences.py +0 -255
  1410. package/src/scripts/lint_rule_interactions.py +0 -170
  1411. package/src/scripts/lint_rule_tiers.py +0 -90
  1412. package/src/scripts/lint_showcase_sessions.py +0 -148
  1413. package/src/scripts/lint_skill_frontmatter_safety.py +0 -144
  1414. package/src/scripts/lint_skill_tools.py +0 -168
  1415. package/src/scripts/lint_topics_yaml.py +0 -89
  1416. package/src/scripts/lint_trust_coherence.py +0 -212
  1417. package/src/scripts/lint_value_dashboard.py +0 -218
  1418. package/src/scripts/lint_workspace_boundary.py +0 -122
  1419. package/src/scripts/mcp_parity_smoke.py +0 -316
  1420. package/src/scripts/mcp_render.py +0 -173
  1421. package/src/scripts/mcp_server/__init__.py +0 -19
  1422. package/src/scripts/mcp_server/__main__.py +0 -12
  1423. package/src/scripts/mcp_server/catalog.py +0 -125
  1424. package/src/scripts/mcp_server/metadata.py +0 -75
  1425. package/src/scripts/mcp_server/prompts.py +0 -442
  1426. package/src/scripts/mcp_server/requirements.txt +0 -4
  1427. package/src/scripts/mcp_server/resources.py +0 -201
  1428. package/src/scripts/mcp_server/server.py +0 -270
  1429. package/src/scripts/mcp_server/telemetry.py +0 -128
  1430. package/src/scripts/mcp_server/tools.py +0 -926
  1431. package/src/scripts/mcp_telemetry_health.py +0 -214
  1432. package/src/scripts/mcp_telemetry_query.py +0 -203
  1433. package/src/scripts/mcp_telemetry_store.py +0 -211
  1434. package/src/scripts/measure_augment_budget.py +0 -214
  1435. package/src/scripts/measure_density.py +0 -232
  1436. package/src/scripts/measure_frugality_savings.py +0 -164
  1437. package/src/scripts/measure_markitdown_lift.py +0 -127
  1438. package/src/scripts/measure_patterns.py +0 -376
  1439. package/src/scripts/measure_projection_bytes.py +0 -159
  1440. package/src/scripts/measure_rule_budget.py +0 -347
  1441. package/src/scripts/measure_skill_reduction.py +0 -102
  1442. package/src/scripts/memory_hash.py +0 -75
  1443. package/src/scripts/memory_lookup.py +0 -436
  1444. package/src/scripts/memory_report.py +0 -314
  1445. package/src/scripts/memory_signal.py +0 -165
  1446. package/src/scripts/memory_status.py +0 -76
  1447. package/src/scripts/migrate_command_suggestions.py +0 -151
  1448. package/src/scripts/migrate_frontmatter_defaults.py +0 -245
  1449. package/src/scripts/mine_session.py +0 -356
  1450. package/src/scripts/minimal_safe_diff_hook.py +0 -245
  1451. package/src/scripts/move_artefact.py +0 -143
  1452. package/src/scripts/new_skill.py +0 -148
  1453. package/src/scripts/onboarding_gate_hook.py +0 -142
  1454. package/src/scripts/pack_mcp_content.py +0 -293
  1455. package/src/scripts/plan_physical_move.py +0 -353
  1456. package/src/scripts/prediction-pool/poisson_sim.py +0 -167
  1457. package/src/scripts/prediction-pool/pool_winsim.py +0 -236
  1458. package/src/scripts/prediction-pool/score_ev.py +0 -188
  1459. package/src/scripts/print_required_checks.py +0 -196
  1460. package/src/scripts/probe_projection_fidelity.py +0 -202
  1461. package/src/scripts/probe_skill_registration.py +0 -413
  1462. package/src/scripts/profile_staleness_hook.py +0 -69
  1463. package/src/scripts/profile_use.py +0 -164
  1464. package/src/scripts/project_thin_rules.py +0 -168
  1465. package/src/scripts/propose_modules_config.py +0 -145
  1466. package/src/scripts/prototype_lint_contradictions.py +0 -150
  1467. package/src/scripts/prove_pack_extractable.py +0 -187
  1468. package/src/scripts/readme_linter.py +0 -589
  1469. package/src/scripts/redact_hook_capture.py +0 -148
  1470. package/src/scripts/refine_ticket_detect.py +0 -646
  1471. package/src/scripts/release.py +0 -1091
  1472. package/src/scripts/render_benchmark_md.py +0 -401
  1473. package/src/scripts/render_value_md.py +0 -347
  1474. package/src/scripts/requirements-evals.txt +0 -8
  1475. package/src/scripts/roadmap_progress_hook.py +0 -274
  1476. package/src/scripts/router_telemetry.py +0 -470
  1477. package/src/scripts/run_skill_evals.py +0 -185
  1478. package/src/scripts/runtime_dispatcher.py +0 -276
  1479. package/src/scripts/runtime_handler.py +0 -148
  1480. package/src/scripts/runtime_registry.py +0 -166
  1481. package/src/scripts/score_skill_selection.py +0 -198
  1482. package/src/scripts/security_audit_config.py +0 -153
  1483. package/src/scripts/setup_eval_venv.sh +0 -58
  1484. package/src/scripts/skill_collision_clusters.py +0 -162
  1485. package/src/scripts/skill_discovery.py +0 -254
  1486. package/src/scripts/skill_linter.py +0 -3694
  1487. package/src/scripts/skill_overlap.py +0 -204
  1488. package/src/scripts/skill_preview.py +0 -179
  1489. package/src/scripts/skill_tools/__init__.py +0 -22
  1490. package/src/scripts/skill_tools/audit_persona_coverage.py +0 -147
  1491. package/src/scripts/skill_tools/audit_user_type_coverage.py +0 -148
  1492. package/src/scripts/skill_tools/run_block_d_eval.py +0 -129
  1493. package/src/scripts/skill_tools/score_skill_relevance.py +0 -169
  1494. package/src/scripts/skill_tools/suggest_skill_for_task.py +0 -113
  1495. package/src/scripts/skill_trigger_eval.py +0 -682
  1496. package/src/scripts/skill_usage_collect.py +0 -191
  1497. package/src/scripts/skill_usage_report.py +0 -162
  1498. package/src/scripts/smoke_path_resolution.py +0 -93
  1499. package/src/scripts/smoke_quickstart.py +0 -144
  1500. package/src/scripts/snapshot_agent_outputs.py +0 -144
  1501. package/src/scripts/spotcheck_thin_root.py +0 -134
  1502. package/src/scripts/sync_agent_settings.py +0 -180
  1503. package/src/scripts/sync_github_metadata.py +0 -147
  1504. package/src/scripts/sync_gitignore.py +0 -291
  1505. package/src/scripts/sync_yaml_rt.py +0 -734
  1506. package/src/scripts/telegraph_stats.py +0 -119
  1507. package/src/scripts/tool_registry.py +0 -146
  1508. package/src/scripts/tools/__init__.py +0 -1
  1509. package/src/scripts/tools/adapter_errors.py +0 -63
  1510. package/src/scripts/tools/base_adapter.py +0 -91
  1511. package/src/scripts/tools/github_adapter.py +0 -128
  1512. package/src/scripts/tools/jira_adapter.py +0 -115
  1513. package/src/scripts/trigger_coverage.py +0 -129
  1514. package/src/scripts/update_counts.py +0 -199
  1515. package/src/scripts/update_prices.py +0 -125
  1516. package/src/scripts/validate_agent_settings.py +0 -124
  1517. package/src/scripts/validate_decision_engine.py +0 -136
  1518. package/src/scripts/validate_discovery_manifest.py +0 -94
  1519. package/src/scripts/validate_frontmatter.py +0 -647
  1520. package/src/scripts/validate_pack_yaml.py +0 -179
  1521. package/src/scripts/validate_safe_paths.py +0 -118
  1522. package/src/scripts/validate_telegraph_carveouts.py +0 -129
  1523. package/src/scripts/verify_before_complete_hook.py +0 -216
  1524. package/src/scripts/verify_physical_move.py +0 -185
  1525. package/src/scripts/wrapper_freshness_hook.py +0 -86
  1526. /package/dist/agent-src/skills/design-intelligence/data/{typography.csv → font-pairings-reference.csv} +0 -0
  1527. /package/src/scripts/{ai-video → media}/lib/fixtures/allin1/analysis.json +0 -0
  1528. /package/src/scripts/{ai-video → media}/lib/fixtures/comfyui/scene-0001.mp4 +0 -0
  1529. /package/src/scripts/{ai-video → media}/lib/fixtures/fal/scene-0001.mp4 +0 -0
  1530. /package/src/scripts/{ai-video → media}/lib/fixtures/gemini-veo/scene-0001.mp4 +0 -0
  1531. /package/src/scripts/{ai-video → media}/lib/fixtures/higgsfield/scene-0001.mp4 +0 -0
  1532. /package/src/scripts/{ai-video → media}/lib/fixtures/kling/scene-0001.mp4 +0 -0
  1533. /package/src/scripts/{ai-video → media}/lib/fixtures/musetalk/lipsync-0001.mp4 +0 -0
  1534. /package/src/scripts/{ai-video → media}/lib/fixtures/openai-images/scene-0001.png +0 -0
  1535. /package/src/scripts/{ai-video → media}/lib/fixtures/replicate/scene-0001.mp4 +0 -0
  1536. /package/src/scripts/{ai-video → media}/lib/fixtures/sora/scene-0001.mp4 +0 -0
  1537. /package/src/scripts/{ai-video → media}/lib/fixtures/syncso/lipsync-0001.mp4 +0 -0
  1538. /package/src/scripts/{ai-video → media}/lib/fixtures/whisperx/transcript.json +0 -0
  1539. /package/src/scripts/{ai-video → media}/lib/telemetry.sh +0 -0
@@ -0,0 +1,2214 @@
1
+ // External-AI clients for the council — byte-identical TypeScript twin of
2
+ // `src/scripts/ai_council/clients.py` (py2ts migration, ADR-200).
3
+ //
4
+ // Mirrors the contract from `scripts/skill_trigger_eval.py`:
5
+ // - Tokens come exclusively from `~/.event4u/agent-config/<provider>.key`
6
+ // (legacy `~/.config/agent-config/<provider>.key` is read as a fallback so
7
+ // pre-2.4 installs keep working until the user moves the files into the new
8
+ // namespace).
9
+ // - File mode must be exactly 0o600. Drift is a hard abort.
10
+ // - No environment-variable fallback. No keychain fallback.
11
+ // - Real SDKs (`anthropic`, `openai`) are *soft* dependencies — the module
12
+ // imports cleanly without them; only `ask()` requires them.
13
+ //
14
+ // Tests inject mock clients via the `client=` constructor argument and never
15
+ // hit the real API.
16
+ //
17
+ // Mode contract:
18
+ // - `billable=true` clients (AnthropicClient, OpenAIClient, GeminiClient,
19
+ // XAIClient, PerplexityClient) participate in the cost gate — projected USD
20
+ // spend is checked before each call.
21
+ // - `billable=false` clients (ManualClient, vendor-official CliClient
22
+ // subclasses — AnthropicCliClient, OpenAICliClient, GeminiCliClient) skip the
23
+ // USD cost gate entirely. Spend = $0 to us; provider-side limits are the
24
+ // user's concern.
25
+ // - `billable=true` CLI subclasses (XAICliClient, PerplexityCliClient) wrap
26
+ // community-maintained CLIs that consume the same API key as their `api`
27
+ // counterparts — they participate in the USD cost gate. `mode: cli` here is
28
+ // an ergonomic shortcut, not a billing change.
29
+ //
30
+ // CLI subclasses additionally consult the optional
31
+ // `cli_call_budget.max_calls_per_day.<provider>` quota with state persisted at
32
+ // `~/.event4u/agent-config/cli-calls.json` (daily UTC reset).
33
+ //
34
+ // TRANSPORT SEAM (TS-only, for tests): the Python original calls
35
+ // `subprocess.run` directly inside `CliClient.ask`. The twin routes that one
36
+ // call through the protected `_runSubprocess(cmd, stdinPayload)` instance
37
+ // method so a test subclass can stub the transport without live processes —
38
+ // it returns `{ returncode, stdout, stderr }` or throws a `SubprocessError`
39
+ // shaped like the Python exceptions (`timeout` / `file_not_found` / `os`).
40
+ // The default implementation shells out via `spawnSync`, matching
41
+ // `subprocess.run(..., capture_output=True, text=True, timeout=..., check=False)`.
42
+
43
+ import { spawnSync } from 'node:child_process';
44
+ import * as fs from 'node:fs';
45
+ import * as path from 'node:path';
46
+
47
+ import * as user_global_paths from '../_lib/user_global_paths.js';
48
+ import { appendEvent } from './events_log.js';
49
+
50
+ export const ANTHROPIC_KEY_FILENAME = 'anthropic.key';
51
+ export const OPENAI_KEY_FILENAME = 'openai.key';
52
+
53
+ // Canonical write target under the new namespace. Reads route via
54
+ // `_resolveKeyPath` so a key still sitting in the legacy
55
+ // `~/.config/agent-config/` tree keeps working.
56
+ export const ANTHROPIC_KEY_PATH = user_global_paths.write_target(ANTHROPIC_KEY_FILENAME);
57
+ export const OPENAI_KEY_PATH = user_global_paths.write_target(OPENAI_KEY_FILENAME);
58
+
59
+ /** Return the active key path, preferring the new namespace. */
60
+ function _resolveKeyPath(filename: string): string {
61
+ const found = user_global_paths.resolve_with_fallback(filename);
62
+ if (found !== null) {
63
+ return found;
64
+ }
65
+ return user_global_paths.write_target(filename);
66
+ }
67
+
68
+ export const DEFAULT_ANTHROPIC_MODEL = 'claude-sonnet-4-5';
69
+ export const DEFAULT_OPENAI_MODEL = 'gpt-4o';
70
+ export const DEFAULT_GEMINI_MODEL = 'gemini-2.5-pro';
71
+ export const DEFAULT_XAI_MODEL = 'grok-4';
72
+ export const DEFAULT_PERPLEXITY_MODEL = 'sonar-pro';
73
+
74
+ // OpenAI-API-compatible endpoints. xAI and Perplexity both expose the
75
+ // `/v1/chat/completions` shape, so their clients reuse the `openai` SDK with a
76
+ // custom `base_url`. Gemini has its own SDK (`google-genai`).
77
+ export const XAI_BASE_URL = 'https://api.x.ai/v1';
78
+ export const PERPLEXITY_BASE_URL = 'https://api.perplexity.ai';
79
+
80
+ // Per-call output budget when no caller-supplied value reaches `ask()`. The CLI
81
+ // resolves the live default from `ai_council.max_output_tokens` in
82
+ // `.agent-settings.yml`; this constant is only the abstract-base / direct-API
83
+ // fallback when nothing else is wired up.
84
+ export const DEFAULT_MAX_TOKENS = 2048;
85
+
86
+ // Expansion target when the user sets `max_output_tokens: 0` ("unlimited") in
87
+ // settings. Anthropic requires `max_tokens` to be a positive integer, so 0 is
88
+ // widened to this safe ceiling before the SDK call. Big enough for current
89
+ // frontier models (Sonnet/GPT-4o headroom ≥ 16k); raise explicitly in settings
90
+ // if a larger budget is genuinely needed.
91
+ export const UNLIMITED_TOKENS_FALLBACK = 16384;
92
+
93
+ // OpenAI reasoning models (o1, o3, o4 families) reject `max_tokens` and the
94
+ // `system` role; they require `max_completion_tokens` and accept only `user`
95
+ // (and `developer`) messages.
96
+ const _REASONING_PREFIXES: readonly string[] = ['o1', 'o3', 'o4'];
97
+
98
+ export function _is_reasoning_model(model: string): boolean {
99
+ const name = model.toLowerCase();
100
+ return _REASONING_PREFIXES.some((p) => name === p || name.startsWith(`${p}-`));
101
+ }
102
+
103
+ /** Raised when a provider key file violates the 0600 contract. */
104
+ export class KeyGateError extends Error {
105
+ constructor(message: string) {
106
+ super(message);
107
+ this.name = 'KeyGateError';
108
+ }
109
+ }
110
+
111
+ /** Raised when a CLI member cannot be constructed (binary missing, etc.). */
112
+ export class CliClientError extends Error {
113
+ constructor(message: string) {
114
+ super(message);
115
+ this.name = 'CliClientError';
116
+ }
117
+ }
118
+
119
+ /** Normalised output from a single council member (dataclass `CouncilResponse`). */
120
+ export class CouncilResponse {
121
+ provider: string;
122
+ model: string;
123
+ text: string;
124
+ input_tokens: number;
125
+ output_tokens: number;
126
+ latency_ms: number;
127
+ error: string | null;
128
+ metadata: Record<string, unknown>;
129
+
130
+ constructor(opts: {
131
+ provider: string;
132
+ model: string;
133
+ text: string;
134
+ input_tokens?: number;
135
+ output_tokens?: number;
136
+ latency_ms?: number;
137
+ error?: string | null;
138
+ metadata?: Record<string, unknown>;
139
+ }) {
140
+ this.provider = opts.provider;
141
+ this.model = opts.model;
142
+ this.text = opts.text;
143
+ this.input_tokens = opts.input_tokens ?? 0;
144
+ this.output_tokens = opts.output_tokens ?? 0;
145
+ this.latency_ms = opts.latency_ms ?? 0;
146
+ this.error = opts.error ?? null;
147
+ // Python dataclass uses `field(default_factory=dict)` — each instance
148
+ // gets its own fresh dict.
149
+ this.metadata = opts.metadata ?? {};
150
+ }
151
+ }
152
+
153
+ // ── monotonic clock seam ──────────────────────────────────────────────
154
+ // Python uses `time.monotonic()`. latency_ms is non-deterministic; tests
155
+ // normalise it. Exposed so tests can pin it if they choose.
156
+ let _monotonicSource: () => number = () => {
157
+ // performance.now() is monotonic milliseconds; the Python original is
158
+ // seconds, but we only ever compute `int((now - t0) * 1000)`, so feeding
159
+ // milliseconds here and dropping the *1000 keeps the same arithmetic.
160
+ return performance.now();
161
+ };
162
+
163
+ function _nowMs(): number {
164
+ return _monotonicSource();
165
+ }
166
+
167
+ function _elapsedMs(t0: number): number {
168
+ // Mirrors Python `int((time.monotonic() - t0) * 1000)` — here both sides are
169
+ // already in ms, so just truncate toward zero.
170
+ return Math.trunc(_nowMs() - t0);
171
+ }
172
+
173
+ /** Test seam: override the monotonic clock (ms). Returns the previous source. */
174
+ export function _setMonotonicSource(fn: () => number): () => number {
175
+ const prev = _monotonicSource;
176
+ _monotonicSource = fn;
177
+ return prev;
178
+ }
179
+
180
+ /** Shared 0600-gated key loader. Refuses anything outside the contract. */
181
+ function _loadKey(p: string, prefix: string, installScript: string): string {
182
+ if (!fs.existsSync(p)) {
183
+ throw new KeyGateError(
184
+ `Key not found at ${p}.\n` + ` Install it with: bash ${installScript}`,
185
+ );
186
+ }
187
+ const st = fs.statSync(p);
188
+ const mode = st.mode & 0o777;
189
+ if (mode !== 0o600) {
190
+ throw new KeyGateError(
191
+ `Unsafe permissions on ${p}: got ${_octRepr(mode)}, expected 0o600.\n` +
192
+ ` Fix: chmod 600 ${p}`,
193
+ );
194
+ }
195
+ const key = fs.readFileSync(p, { encoding: 'utf-8' }).trim();
196
+ if (!key) {
197
+ throw new KeyGateError(`${p} is empty.`);
198
+ }
199
+ if (!key.startsWith(prefix)) {
200
+ throw new KeyGateError(`${p} does not look like a ${_pyRepr(prefix)}-prefixed key.`);
201
+ }
202
+ return key;
203
+ }
204
+
205
+ export function load_anthropic_key(p: string | null = null): string {
206
+ const resolved = p !== null ? p : _resolveKeyPath(ANTHROPIC_KEY_FILENAME);
207
+ return _loadKey(resolved, 'sk-ant-', 'src/scripts/install_anthropic_key.sh');
208
+ }
209
+
210
+ export function load_openai_key(p: string | null = null): string {
211
+ const resolved = p !== null ? p : _resolveKeyPath(OPENAI_KEY_FILENAME);
212
+ return _loadKey(resolved, 'sk-', 'src/scripts/install_openai_key.sh');
213
+ }
214
+
215
+ // ── attribute-access helpers (mirror Python getattr) ──────────────────
216
+
217
+ /**
218
+ * Mirror Python `getattr(obj, name, default)` over the duck-typed mock /
219
+ * SDK-response objects the clients consume. Returns the attribute value, or
220
+ * `fallback` when the attribute is absent / object is null. Methods are
221
+ * returned bound so they can be invoked by the caller.
222
+ */
223
+ function _getattr(obj: unknown, name: string, fallback: unknown): unknown {
224
+ if (obj === null || obj === undefined) {
225
+ return fallback;
226
+ }
227
+ if (typeof obj === 'object' || typeof obj === 'function') {
228
+ const rec = obj as Record<string, unknown>;
229
+ if (name in rec) {
230
+ return rec[name];
231
+ }
232
+ // Walk the prototype chain for class-instance shaped mocks.
233
+ const val = (obj as Record<string, unknown>)[name];
234
+ if (val !== undefined) {
235
+ return val;
236
+ }
237
+ }
238
+ return fallback;
239
+ }
240
+
241
+ /** Abstract base for council members. */
242
+ export abstract class ExternalAIClient {
243
+ name = '';
244
+ model = '';
245
+ billable = true; // API-mode subclasses spend money; manual doesn't.
246
+ transport = 'api'; // "api" | "cli" | "manual" — surfaced in session manifest.
247
+ subscription_label = ''; // vendor-CLI label (e.g. "claude") for non-billable transports.
248
+
249
+ /**
250
+ * Send one independent query. Must never raise on network/API failure —
251
+ * return a `CouncilResponse` with `error` set instead. Other members should
252
+ * not be blocked by one failure.
253
+ */
254
+ abstract ask(
255
+ system_prompt: string,
256
+ user_prompt: string,
257
+ max_tokens?: number,
258
+ ): CouncilResponse;
259
+ }
260
+
261
+ /** Shared ctor-options shape for the API clients. */
262
+ interface ApiClientOptions {
263
+ model?: string | undefined;
264
+ client?: unknown;
265
+ api_key?: string | null | undefined;
266
+ }
267
+
268
+ /** Shared ctor-options shape for the CLI clients. */
269
+ interface CliClientOptions {
270
+ model?: string | undefined;
271
+ binary?: string | null | undefined;
272
+ timeout_seconds?: number | undefined;
273
+ max_calls_per_day?: number | null | undefined;
274
+ warn_at?: number | undefined;
275
+ cli_calls_path?: string | null | undefined;
276
+ }
277
+
278
+ /**
279
+ * Synchronous JSON POST via `curl` (no Node SDK, no Python). Matches the
280
+ * council's `spawnSync` transport model so the live-call clients stay
281
+ * synchronous. Returns the parsed response JSON (the provider HTTP APIs return
282
+ * exactly the SDK response shape `ask()` reads). Throws on transport failure or
283
+ * a non-2xx status so `ask()`'s catch surfaces it as a member error.
284
+ */
285
+ function _curlJsonPost(url: string, extraHeaders: string[], body: unknown): unknown {
286
+ const args = ['-sS', '-X', 'POST', url, '-H', 'content-type: application/json'];
287
+ for (const h of extraHeaders) {
288
+ args.push('-H', h);
289
+ }
290
+ args.push('-w', '\n%{http_code}', '--data-binary', '@-');
291
+ const r = spawnSync('curl', args, {
292
+ input: JSON.stringify(body),
293
+ encoding: 'utf8',
294
+ maxBuffer: 64 * 1024 * 1024,
295
+ timeout: 120_000,
296
+ });
297
+ if (r.error) {
298
+ throw new Error(`curl spawn failed: ${(r.error as Error).message}`);
299
+ }
300
+ if (r.status !== 0) {
301
+ throw new Error(`curl exited ${r.status ?? 'null'}: ${(r.stderr ?? '').toString().slice(0, 500)}`);
302
+ }
303
+ const out = (r.stdout ?? '').toString();
304
+ const nl = out.lastIndexOf('\n');
305
+ const httpCode = (nl >= 0 ? out.slice(nl + 1) : out).trim();
306
+ const bodyText = nl >= 0 ? out.slice(0, nl) : out;
307
+ if (!/^2\d\d$/.test(httpCode)) {
308
+ throw new Error(`HTTP ${httpCode}: ${bodyText.slice(0, 500)}`);
309
+ }
310
+ return JSON.parse(bodyText);
311
+ }
312
+
313
+ export class AnthropicClient extends ExternalAIClient {
314
+ override name = 'anthropic';
315
+ override billable = true;
316
+ private _client: unknown;
317
+
318
+ constructor(opts: ApiClientOptions = {}) {
319
+ super();
320
+ this.model = opts.model ?? DEFAULT_ANTHROPIC_MODEL;
321
+ if (opts.client !== undefined && opts.client !== null) {
322
+ this._client = opts.client;
323
+ return;
324
+ }
325
+ const api_key = opts.api_key ?? null;
326
+ if (api_key === null) {
327
+ throw new Error(
328
+ 'AnthropicClient requires explicit api_key or injected client. ' +
329
+ 'Use load_anthropic_key() — no env-var fallback.',
330
+ );
331
+ }
332
+ // Live transport: curl → Anthropic Messages API (synchronous, no Node
333
+ // SDK, no Python). The HTTP response is the same shape `ask()` reads
334
+ // (`content[0].text`, `usage.{input_tokens,output_tokens}`). Tests still
335
+ // inject a mock `client` above and never reach this path.
336
+ this._client = {
337
+ messages: {
338
+ create: (kwargs: Record<string, unknown>): unknown =>
339
+ _curlJsonPost(
340
+ 'https://api.anthropic.com/v1/messages',
341
+ [`x-api-key: ${api_key}`, 'anthropic-version: 2023-06-01'],
342
+ kwargs,
343
+ ),
344
+ },
345
+ };
346
+ }
347
+
348
+ override ask(
349
+ system_prompt: string,
350
+ user_prompt: string,
351
+ max_tokens: number = DEFAULT_MAX_TOKENS,
352
+ ): CouncilResponse {
353
+ const t0 = _nowMs();
354
+ let response: unknown;
355
+ try {
356
+ const messages = _getattr(this._client, 'messages', null);
357
+ const create = _getattr(messages, 'create', null) as
358
+ | ((kwargs: Record<string, unknown>) => unknown)
359
+ | null;
360
+ if (typeof create !== 'function') {
361
+ throw new TypeError("'messages.create' is not callable");
362
+ }
363
+ response = create.call(messages, {
364
+ model: this.model,
365
+ max_tokens,
366
+ system: system_prompt,
367
+ messages: [{ role: 'user', content: user_prompt }],
368
+ });
369
+ } catch (exc) {
370
+ return new CouncilResponse({
371
+ provider: this.name,
372
+ model: this.model,
373
+ text: '',
374
+ latency_ms: _elapsedMs(t0),
375
+ error: _excString(exc),
376
+ });
377
+ }
378
+ const latency_ms = _elapsedMs(t0);
379
+ let text = '';
380
+ const content = _getattr(response, 'content', null);
381
+ if (_pyTruthy(content)) {
382
+ const first = (content as unknown[])[0];
383
+ text = (_getattr(first, 'text', '') as string) || '';
384
+ }
385
+ const usage = _getattr(response, 'usage', null);
386
+ return new CouncilResponse({
387
+ provider: this.name,
388
+ model: this.model,
389
+ text,
390
+ input_tokens: usage ? (_getattr(usage, 'input_tokens', 0) as number) : 0,
391
+ output_tokens: usage ? (_getattr(usage, 'output_tokens', 0) as number) : 0,
392
+ latency_ms,
393
+ });
394
+ }
395
+ }
396
+
397
+ export class OpenAIClient extends ExternalAIClient {
398
+ override name = 'openai';
399
+ override billable = true;
400
+ private _client: unknown;
401
+
402
+ constructor(opts: ApiClientOptions = {}) {
403
+ super();
404
+ this.model = opts.model ?? DEFAULT_OPENAI_MODEL;
405
+ if (opts.client !== undefined && opts.client !== null) {
406
+ this._client = opts.client;
407
+ return;
408
+ }
409
+ const api_key = opts.api_key ?? null;
410
+ if (api_key === null) {
411
+ throw new Error(
412
+ 'OpenAIClient requires explicit api_key or injected client. ' +
413
+ 'Use load_openai_key() — no env-var fallback.',
414
+ );
415
+ }
416
+ // Live transport: curl → OpenAI Chat Completions API (synchronous, no
417
+ // Node SDK, no Python). The HTTP response is the same shape `ask()`
418
+ // reads (`choices[0].message.content`, `usage.{prompt_tokens,
419
+ // completion_tokens}`). Tests still inject a mock `client` above.
420
+ this._client = {
421
+ chat: {
422
+ completions: {
423
+ create: (kwargs: Record<string, unknown>): unknown =>
424
+ _curlJsonPost(
425
+ 'https://api.openai.com/v1/chat/completions',
426
+ [`authorization: Bearer ${api_key}`],
427
+ kwargs,
428
+ ),
429
+ },
430
+ },
431
+ };
432
+ }
433
+
434
+ override ask(
435
+ system_prompt: string,
436
+ user_prompt: string,
437
+ max_tokens: number = DEFAULT_MAX_TOKENS,
438
+ ): CouncilResponse {
439
+ const t0 = _nowMs();
440
+ const kwargs: Record<string, unknown> = { model: this.model };
441
+ if (_is_reasoning_model(this.model)) {
442
+ // o1/o3/o4 reasoning models reject `max_tokens` and `system` role.
443
+ kwargs['max_completion_tokens'] = max_tokens;
444
+ kwargs['messages'] = [
445
+ { role: 'user', content: `${system_prompt}\n\n---\n\n${user_prompt}` },
446
+ ];
447
+ } else {
448
+ kwargs['max_tokens'] = max_tokens;
449
+ kwargs['messages'] = [
450
+ { role: 'system', content: system_prompt },
451
+ { role: 'user', content: user_prompt },
452
+ ];
453
+ }
454
+ let response: unknown;
455
+ try {
456
+ const chat = _getattr(this._client, 'chat', null);
457
+ const completions = _getattr(chat, 'completions', null);
458
+ const create = _getattr(completions, 'create', null) as
459
+ | ((kwargs: Record<string, unknown>) => unknown)
460
+ | null;
461
+ if (typeof create !== 'function') {
462
+ throw new TypeError("'chat.completions.create' is not callable");
463
+ }
464
+ response = create.call(completions, kwargs);
465
+ } catch (exc) {
466
+ return new CouncilResponse({
467
+ provider: this.name,
468
+ model: this.model,
469
+ text: '',
470
+ latency_ms: _elapsedMs(t0),
471
+ error: _excString(exc),
472
+ });
473
+ }
474
+ const latency_ms = _elapsedMs(t0);
475
+ let text: unknown = '';
476
+ const choices = _getattr(response, 'choices', null);
477
+ if (_pyTruthy(choices)) {
478
+ const first = (choices as unknown[])[0];
479
+ const msg = _getattr(first, 'message', null);
480
+ text = msg ? _getattr(msg, 'content', '') : '';
481
+ }
482
+ const usage = _getattr(response, 'usage', null);
483
+ return new CouncilResponse({
484
+ provider: this.name,
485
+ model: this.model,
486
+ text: (text as string) || '',
487
+ input_tokens: usage ? (_getattr(usage, 'prompt_tokens', 0) as number) : 0,
488
+ output_tokens: usage ? (_getattr(usage, 'completion_tokens', 0) as number) : 0,
489
+ latency_ms,
490
+ });
491
+ }
492
+ }
493
+
494
+ // ── Gemini / xAI / Perplexity (Phase 0 — Step 6) ─────────────────────
495
+
496
+ /**
497
+ * Google Gemini via the `google-genai` SDK.
498
+ *
499
+ * Lazy-imports `google.genai` on first `ask()` so disabled members do not
500
+ * require the SDK to be installed. Tests inject a mock client shaped like
501
+ * `genai.Client(api_key=...)` — `self._client.models.generate_content(...)`
502
+ * returns an object with `.text` and
503
+ * `.usage_metadata.{prompt_token_count, candidates_token_count}`.
504
+ */
505
+ export class GeminiClient extends ExternalAIClient {
506
+ override name = 'gemini';
507
+ override billable = true;
508
+ private _client: unknown;
509
+
510
+ constructor(opts: ApiClientOptions = {}) {
511
+ super();
512
+ this.model = opts.model ?? DEFAULT_GEMINI_MODEL;
513
+ if (opts.client !== undefined && opts.client !== null) {
514
+ this._client = opts.client;
515
+ return;
516
+ }
517
+ const api_key = opts.api_key ?? null;
518
+ if (api_key === null) {
519
+ throw new Error(
520
+ 'GeminiClient requires explicit api_key or injected client. ' +
521
+ 'Use `api_key_ref: env:GEMINI_API_KEY` in ~/.event4u/agent-config/settings/.ai-council.yml.',
522
+ );
523
+ }
524
+ throw new Error('google-genai package not installed. `pip install google-genai`.');
525
+ }
526
+
527
+ override ask(
528
+ system_prompt: string,
529
+ user_prompt: string,
530
+ max_tokens: number = DEFAULT_MAX_TOKENS,
531
+ ): CouncilResponse {
532
+ const t0 = _nowMs();
533
+ const contents = `${system_prompt}\n\n---\n\n${user_prompt}`;
534
+ let response: unknown;
535
+ try {
536
+ const models = _getattr(this._client, 'models', null);
537
+ const generate = _getattr(models, 'generate_content', null) as
538
+ | ((kwargs: Record<string, unknown>) => unknown)
539
+ | null;
540
+ if (typeof generate !== 'function') {
541
+ throw new TypeError("'models.generate_content' is not callable");
542
+ }
543
+ response = generate.call(models, {
544
+ model: this.model,
545
+ contents,
546
+ config: { max_output_tokens: max_tokens },
547
+ });
548
+ } catch (exc) {
549
+ return new CouncilResponse({
550
+ provider: this.name,
551
+ model: this.model,
552
+ text: '',
553
+ latency_ms: _elapsedMs(t0),
554
+ error: _excString(exc),
555
+ });
556
+ }
557
+ const latency_ms = _elapsedMs(t0);
558
+ const text = (_getattr(response, 'text', '') as string) || '';
559
+ const usage = _getattr(response, 'usage_metadata', null);
560
+ return new CouncilResponse({
561
+ provider: this.name,
562
+ model: this.model,
563
+ text,
564
+ input_tokens: usage ? (_getattr(usage, 'prompt_token_count', 0) as number) : 0,
565
+ output_tokens: usage ? (_getattr(usage, 'candidates_token_count', 0) as number) : 0,
566
+ latency_ms,
567
+ });
568
+ }
569
+ }
570
+
571
+ /**
572
+ * Shared shape for OpenAI-API-compatible providers (xAI, Perplexity).
573
+ *
574
+ * Both vendors implement `/v1/chat/completions` and accept the `openai` Python
575
+ * SDK with a custom `base_url`. The reasoning-model branch from `OpenAIClient`
576
+ * is intentionally omitted — neither xAI nor Perplexity ships a reasoning model
577
+ * that requires `max_completion_tokens` as of 2026-05-14.
578
+ */
579
+ export class _OpenAICompatibleClient extends ExternalAIClient {
580
+ override billable = true;
581
+ base_url = '';
582
+ protected _client: unknown;
583
+
584
+ constructor(opts: { model: string; client?: unknown; api_key?: string | null | undefined }) {
585
+ super();
586
+ this.model = opts.model;
587
+ if (opts.client !== undefined && opts.client !== null) {
588
+ this._client = opts.client;
589
+ return;
590
+ }
591
+ const api_key = opts.api_key ?? null;
592
+ if (api_key === null) {
593
+ throw new Error(
594
+ `${this.constructor.name} requires explicit api_key or injected client.`,
595
+ );
596
+ }
597
+ throw new Error('openai package not installed. `pip install openai`.');
598
+ }
599
+
600
+ override ask(
601
+ system_prompt: string,
602
+ user_prompt: string,
603
+ max_tokens: number = DEFAULT_MAX_TOKENS,
604
+ ): CouncilResponse {
605
+ const t0 = _nowMs();
606
+ let response: unknown;
607
+ try {
608
+ const chat = _getattr(this._client, 'chat', null);
609
+ const completions = _getattr(chat, 'completions', null);
610
+ const create = _getattr(completions, 'create', null) as
611
+ | ((kwargs: Record<string, unknown>) => unknown)
612
+ | null;
613
+ if (typeof create !== 'function') {
614
+ throw new TypeError("'chat.completions.create' is not callable");
615
+ }
616
+ response = create.call(completions, {
617
+ model: this.model,
618
+ max_tokens,
619
+ messages: [
620
+ { role: 'system', content: system_prompt },
621
+ { role: 'user', content: user_prompt },
622
+ ],
623
+ });
624
+ } catch (exc) {
625
+ return new CouncilResponse({
626
+ provider: this.name,
627
+ model: this.model,
628
+ text: '',
629
+ latency_ms: _elapsedMs(t0),
630
+ error: _excString(exc),
631
+ });
632
+ }
633
+ const latency_ms = _elapsedMs(t0);
634
+ let text: unknown = '';
635
+ const choices = _getattr(response, 'choices', null);
636
+ if (_pyTruthy(choices)) {
637
+ const first = (choices as unknown[])[0];
638
+ const msg = _getattr(first, 'message', null);
639
+ text = msg ? _getattr(msg, 'content', '') : '';
640
+ }
641
+ const usage = _getattr(response, 'usage', null);
642
+ return new CouncilResponse({
643
+ provider: this.name,
644
+ model: this.model,
645
+ text: (text as string) || '',
646
+ input_tokens: usage ? (_getattr(usage, 'prompt_tokens', 0) as number) : 0,
647
+ output_tokens: usage ? (_getattr(usage, 'completion_tokens', 0) as number) : 0,
648
+ latency_ms,
649
+ });
650
+ }
651
+ }
652
+
653
+ /** xAI Grok via the OpenAI-compatible endpoint at api.x.ai/v1. */
654
+ export class XAIClient extends _OpenAICompatibleClient {
655
+ override name = 'xai';
656
+ override base_url = XAI_BASE_URL;
657
+
658
+ constructor(opts: ApiClientOptions = {}) {
659
+ super({ model: opts.model ?? DEFAULT_XAI_MODEL, client: opts.client, api_key: opts.api_key });
660
+ }
661
+ }
662
+
663
+ /** Perplexity via the OpenAI-compatible endpoint at api.perplexity.ai. */
664
+ export class PerplexityClient extends _OpenAICompatibleClient {
665
+ override name = 'perplexity';
666
+ override base_url = PERPLEXITY_BASE_URL;
667
+
668
+ constructor(opts: ApiClientOptions = {}) {
669
+ super({
670
+ model: opts.model ?? DEFAULT_PERPLEXITY_MODEL,
671
+ client: opts.client,
672
+ api_key: opts.api_key,
673
+ });
674
+ }
675
+ }
676
+
677
+ // ── CLI transport (step-1 Phase 1+) ──────────────────────────────────
678
+
679
+ export const CLI_CALLS_FILENAME = 'cli-calls.json';
680
+
681
+ // Default subprocess timeout (seconds) for a single CLI call. Long enough for
682
+ // the largest frontier models to think; short enough to surface a hung
683
+ // subprocess without freezing the council run.
684
+ export const DEFAULT_CLI_TIMEOUT_SECONDS = 120.0;
685
+
686
+ /** Return the canonical write target for the daily-quota counter. */
687
+ function _cliCallsStatePath(): string {
688
+ return user_global_paths.write_target(CLI_CALLS_FILENAME);
689
+ }
690
+
691
+ export function _today_utc_iso(): string {
692
+ // Python: datetime.now(timezone.utc).date().isoformat() → "YYYY-MM-DD".
693
+ const d = new Date();
694
+ const y = d.getUTCFullYear().toString().padStart(4, '0');
695
+ const m = (d.getUTCMonth() + 1).toString().padStart(2, '0');
696
+ const day = d.getUTCDate().toString().padStart(2, '0');
697
+ return `${y}-${m}-${day}`;
698
+ }
699
+
700
+ /** Return today's per-provider call counts. Empty dict on UTC rollover. */
701
+ export function load_cli_call_counts(p: string | null = null): Record<string, number> {
702
+ const target = p !== null ? p : _cliCallsStatePath();
703
+ if (!fs.existsSync(target)) {
704
+ return {};
705
+ }
706
+ let data: unknown;
707
+ try {
708
+ data = JSON.parse(fs.readFileSync(target, { encoding: 'utf-8' }));
709
+ } catch {
710
+ // json.JSONDecodeError / OSError
711
+ return {};
712
+ }
713
+ if (!_isPlainObject(data) || (data as Record<string, unknown>)['date'] !== _today_utc_iso()) {
714
+ return {};
715
+ }
716
+ const counts = (data as Record<string, unknown>)['counts'];
717
+ if (!_isPlainObject(counts)) {
718
+ return {};
719
+ }
720
+ const out: Record<string, number> = {};
721
+ for (const [k, v] of Object.entries(counts as Record<string, unknown>)) {
722
+ // Python: int(v) for k, v in counts.items() if isinstance(v, (int, str)).
723
+ // bool is an int subclass in Python and would pass; mirror by accepting
724
+ // booleans too. Floats are rejected (not int|str).
725
+ if (typeof v === 'number' && Number.isInteger(v)) {
726
+ out[String(k)] = _pyInt(v);
727
+ } else if (typeof v === 'boolean') {
728
+ out[String(k)] = v ? 1 : 0;
729
+ } else if (typeof v === 'string') {
730
+ out[String(k)] = _pyIntFromStr(v);
731
+ }
732
+ }
733
+ return out;
734
+ }
735
+
736
+ /** Increment today's call count for `provider`. Returns new total. */
737
+ export function record_cli_call(provider: string, p: string | null = null): number {
738
+ const target = p !== null ? p : _cliCallsStatePath();
739
+ const counts = load_cli_call_counts(target);
740
+ counts[provider] = (counts[provider] ?? 0) + 1;
741
+ fs.mkdirSync(path.dirname(target), { recursive: true });
742
+ fs.writeFileSync(
743
+ target,
744
+ _jsonDumpsIndent2({ date: _today_utc_iso(), counts }),
745
+ { encoding: 'utf-8' },
746
+ );
747
+ return counts[provider];
748
+ }
749
+
750
+ /**
751
+ * Reset the per-provider call counter (step-8 P1, `council quota --reset`).
752
+ *
753
+ * `provider=null` clears all providers (today's record). Otherwise only the
754
+ * named provider's count is removed; other providers and the UTC date marker
755
+ * are preserved. Returns the post-reset counts.
756
+ */
757
+ export function reset_cli_call_counts(
758
+ provider: string | null = null,
759
+ p: string | null = null,
760
+ ): Record<string, number> {
761
+ const target = p !== null ? p : _cliCallsStatePath();
762
+ let counts = load_cli_call_counts(target);
763
+ if (provider === null) {
764
+ counts = {};
765
+ } else {
766
+ delete counts[provider];
767
+ }
768
+ fs.mkdirSync(path.dirname(target), { recursive: true });
769
+ fs.writeFileSync(
770
+ target,
771
+ _jsonDumpsIndent2({ date: _today_utc_iso(), counts }),
772
+ { encoding: 'utf-8' },
773
+ );
774
+ return counts;
775
+ }
776
+
777
+ /**
778
+ * Build the pre-run quota summary line (step-8 P1, D1 + D4).
779
+ *
780
+ * Returns `[summary, warn_providers]` where `summary` is the formatted
781
+ * one-liner (empty string when no CLI member has a configured cap) and
782
+ * `warn_providers` is the subset whose `used / max_calls_per_day` ratio crossed
783
+ * `warn_at`. Uncapped providers (`max_calls_per_day is None`) are omitted from
784
+ * the summary entirely — they cannot exceed a threshold that does not exist.
785
+ */
786
+ export function quota_summary_line(
787
+ clients: CliClient[],
788
+ opts: { cli_calls_path?: string | null } = {},
789
+ ): [string, string[]] {
790
+ const cliCallsPath = opts.cli_calls_path ?? null;
791
+ // Python: [c for c in clients if getattr(c, "max_calls_per_day", None)]
792
+ // → truthy check (None or 0 both falsy).
793
+ const capped = clients.filter((c) => _pyTruthy(_getattr(c, 'max_calls_per_day', null)));
794
+ if (capped.length === 0) {
795
+ return ['', []];
796
+ }
797
+ const counts = load_cli_call_counts(cliCallsPath);
798
+ const parts: string[] = [];
799
+ const warn: string[] = [];
800
+ for (const c of capped) {
801
+ const name = _getattr(c, 'name', '?') as string;
802
+ const used = _pyInt(counts[name] ?? 0);
803
+ const limit = _pyInt((c.max_calls_per_day as number));
804
+ parts.push(`${name} ${used}/${limit}`);
805
+ const ratio = limit > 0 ? used / limit : 0.0;
806
+ const warnAt = Number(_getattr(c, 'warn_at', 0.8));
807
+ if (ratio >= warnAt) {
808
+ warn.push(name);
809
+ }
810
+ }
811
+ const prefix = warn.length > 0 ? '⚠️ ' : '';
812
+ return [`${prefix}council:quota · ${parts.join(' · ')}`, warn];
813
+ }
814
+
815
+ /** Transport-seam result, shaped like a finished `subprocess.run`. */
816
+ export interface SubprocessResult {
817
+ returncode: number;
818
+ stdout: string;
819
+ stderr: string;
820
+ }
821
+
822
+ /** Transport-seam error kinds, mirroring the Python exception branches. */
823
+ export type SubprocessErrorKind = 'timeout' | 'file_not_found' | 'os';
824
+
825
+ /** Typed transport error so a stub can model each Python exception branch. */
826
+ export class SubprocessError extends Error {
827
+ kind: SubprocessErrorKind;
828
+ osName: string;
829
+ constructor(kind: SubprocessErrorKind, osName = 'OSError') {
830
+ super(kind);
831
+ this.name = 'SubprocessError';
832
+ this.kind = kind;
833
+ this.osName = osName;
834
+ }
835
+ }
836
+
837
+ /**
838
+ * Shell-out council member — subscription-authed transport.
839
+ *
840
+ * Spawns a locally-installed provider CLI. Auth is delegated to the binary
841
+ * itself (Claude CLI, Codex CLI, Gemini CLI, etc. use the user's logged-in
842
+ * subscription session). Spend is $0 from this loader's perspective —
843
+ * `billable=false` keeps the USD cost gate from firing.
844
+ *
845
+ * Provider subscription quotas are guarded by the optional
846
+ * `cli_call_budget.max_calls_per_day.<provider>` config. Counter state lives at
847
+ * `~/.event4u/agent-config/cli-calls.json` and resets on UTC date rollover.
848
+ *
849
+ * Subclass contract:
850
+ * - `name`: provider key (`anthropic`, `openai`, `gemini`, …).
851
+ * - `default_binary`: executable name resolved via PATH when the member-level
852
+ * `binary:` field is not set.
853
+ * - `_build_command(system_prompt, user_prompt, max_tokens)`: return the argv.
854
+ * - `_parse_output(stdout, stderr)`: return a partial `CouncilResponse`.
855
+ *
856
+ * Construction validates the binary up front — a missing CLI fails fast with
857
+ * `CliClientError`.
858
+ *
859
+ * Stderr heuristics map known failure shapes to short error codes:
860
+ * - `auth_expired`, `timeout`, `cli_quota_exhausted`, `parse_failed`,
861
+ * `exit_<N>`.
862
+ */
863
+ export abstract class CliClient extends ExternalAIClient {
864
+ override billable = false;
865
+ override transport = 'cli';
866
+ default_binary = '';
867
+
868
+ timeout_seconds: number;
869
+ max_calls_per_day: number | null;
870
+ warn_at: number;
871
+ binary!: string;
872
+ protected _cli_calls_path: string | null;
873
+
874
+ static _AUTH_FAILURE_PATTERNS: readonly string[] = [
875
+ 'authentication',
876
+ 'unauthorized',
877
+ 'auth failed',
878
+ 'auth_error',
879
+ 'login',
880
+ 'not logged in',
881
+ 'session expired',
882
+ 'invalid credentials',
883
+ ];
884
+ static _TIMEOUT_PATTERNS: readonly string[] = ['timeout', 'timed out', 'deadline exceeded'];
885
+ static _QUOTA_PATTERNS: readonly string[] = [
886
+ 'rate limit',
887
+ 'rate_limit',
888
+ 'rate-limit',
889
+ 'quota exceeded',
890
+ 'too many requests',
891
+ '429',
892
+ 'usage limit',
893
+ ];
894
+
895
+ constructor(opts: CliClientOptions & { model: string }) {
896
+ super();
897
+ this.model = opts.model;
898
+ this.timeout_seconds = opts.timeout_seconds ?? DEFAULT_CLI_TIMEOUT_SECONDS;
899
+ this.max_calls_per_day = opts.max_calls_per_day ?? null;
900
+ this.warn_at = opts.warn_at ?? 0.8;
901
+ this._cli_calls_path = opts.cli_calls_path ?? null;
902
+ const binary = opts.binary ?? null;
903
+ if (binary !== null) {
904
+ this.binary = binary;
905
+ } else {
906
+ if (!this.default_binary) {
907
+ throw new CliClientError(
908
+ `${this.constructor.name}: no \`default_binary\` set on subclass; ` +
909
+ 'either fix the class or pass `binary=` explicitly.',
910
+ );
911
+ }
912
+ const resolved = _which(this.default_binary);
913
+ if (resolved === null) {
914
+ throw new CliClientError(
915
+ `${this.constructor.name}: binary ${_pyRepr(this.default_binary)} ` +
916
+ 'not found on PATH. Install the provider CLI or set ' +
917
+ `\`members.${this.name}.binary:\` in ~/.event4u/agent-config/settings/.ai-council.yml.`,
918
+ );
919
+ }
920
+ this.binary = resolved;
921
+ }
922
+ }
923
+
924
+ // ── subclass hooks ────────────────────────────────────────────
925
+
926
+ /** Return the argv list the subprocess should execute. */
927
+ protected abstract _build_command(
928
+ system_prompt: string,
929
+ user_prompt: string,
930
+ max_tokens: number,
931
+ ): string[];
932
+
933
+ /** Parse provider-specific stdout into a CouncilResponse. */
934
+ protected abstract _parse_output(stdout: string, stderr: string): CouncilResponse;
935
+
936
+ /** Return text to send on stdin, or `null` to inherit caller's stdin. */
937
+ protected _stdin_payload(system_prompt: string, user_prompt: string): string | null {
938
+ void system_prompt;
939
+ void user_prompt;
940
+ return null;
941
+ }
942
+
943
+ /**
944
+ * Transport seam (default impl). Mirror
945
+ * `subprocess.run(cmd, input=..., capture_output=True, text=True,
946
+ * timeout=..., check=False)`. Throws `SubprocessError` for the timeout /
947
+ * ENOENT / OSError branches so `ask()` can classify them exactly as the
948
+ * Python original does. Tests override this to inject canned output without
949
+ * spawning a process.
950
+ */
951
+ protected _runSubprocess(cmd: string[], stdinPayload: string | null): SubprocessResult {
952
+ const argv0 = cmd[0] ?? '';
953
+ const spawnOpts: Parameters<typeof spawnSync>[2] = {
954
+ encoding: 'utf-8',
955
+ timeout: Math.round(this.timeout_seconds * 1000),
956
+ };
957
+ if (stdinPayload !== null) {
958
+ spawnOpts.input = stdinPayload;
959
+ }
960
+ const r = spawnSync(argv0, cmd.slice(1), spawnOpts);
961
+ if (r.error) {
962
+ const err = r.error as NodeJS.ErrnoException;
963
+ if ((r as { signal?: string | null }).signal === 'SIGTERM' || err.code === 'ETIMEDOUT') {
964
+ throw new SubprocessError('timeout');
965
+ }
966
+ if (err.code === 'ENOENT') {
967
+ throw new SubprocessError('file_not_found');
968
+ }
969
+ throw new SubprocessError('os', err.code ?? 'OSError');
970
+ }
971
+ // spawnSync sets status=null + signal on timeout-kill on some platforms.
972
+ if ((r as { signal?: string | null }).signal === 'SIGTERM' && r.status === null) {
973
+ throw new SubprocessError('timeout');
974
+ }
975
+ return {
976
+ returncode: r.status ?? 0,
977
+ stdout: r.stdout != null ? String(r.stdout) : '',
978
+ stderr: r.stderr != null ? String(r.stderr) : '',
979
+ };
980
+ }
981
+
982
+ // ── ask() ──────────────────────────────────────────────────────
983
+
984
+ override ask(
985
+ system_prompt: string,
986
+ user_prompt: string,
987
+ max_tokens: number = DEFAULT_MAX_TOKENS,
988
+ ): CouncilResponse {
989
+ const t0 = _nowMs();
990
+
991
+ // 1. quota gate — local counter check before spawning anything.
992
+ if (this.max_calls_per_day !== null) {
993
+ const counts = load_cli_call_counts(this._cli_calls_path);
994
+ const used = counts[this.name] ?? 0;
995
+ if (used >= this.max_calls_per_day) {
996
+ // step-8 D3 — record the block on the persistent events log.
997
+ try {
998
+ appendEvent({
999
+ lens: '',
1000
+ invocation: '',
1001
+ action: 'block_quota',
1002
+ verdict: '',
1003
+ provider_caps: {
1004
+ [this.name]: { mode: 'cli', model: this.model },
1005
+ },
1006
+ original_ask: user_prompt,
1007
+ cli_calls_used: used,
1008
+ cli_calls_max: this.max_calls_per_day,
1009
+ });
1010
+ } catch {
1011
+ // never crash ask()
1012
+ }
1013
+ return new CouncilResponse({
1014
+ provider: this.name,
1015
+ model: this.model,
1016
+ text: '',
1017
+ latency_ms: _elapsedMs(t0),
1018
+ error: 'cli_quota_exhausted',
1019
+ metadata: {
1020
+ cli: true,
1021
+ cli_calls_used: used,
1022
+ cli_calls_max: this.max_calls_per_day,
1023
+ },
1024
+ });
1025
+ }
1026
+ }
1027
+
1028
+ // 2. build command + spawn.
1029
+ const cmd = this._build_command(system_prompt, user_prompt, max_tokens);
1030
+ const stdinPayload = this._stdin_payload(system_prompt, user_prompt);
1031
+ let proc: SubprocessResult;
1032
+ try {
1033
+ proc = this._runSubprocess(cmd, stdinPayload);
1034
+ } catch (exc) {
1035
+ if (exc instanceof SubprocessError) {
1036
+ if (exc.kind === 'timeout') {
1037
+ return new CouncilResponse({
1038
+ provider: this.name,
1039
+ model: this.model,
1040
+ text: '',
1041
+ latency_ms: _elapsedMs(t0),
1042
+ error: 'timeout',
1043
+ metadata: { cli: true, timeout_seconds: this.timeout_seconds },
1044
+ });
1045
+ }
1046
+ if (exc.kind === 'file_not_found') {
1047
+ return new CouncilResponse({
1048
+ provider: this.name,
1049
+ model: this.model,
1050
+ text: '',
1051
+ latency_ms: _elapsedMs(t0),
1052
+ error: 'binary_missing',
1053
+ metadata: { cli: true, binary: this.binary },
1054
+ });
1055
+ }
1056
+ // OSError
1057
+ return new CouncilResponse({
1058
+ provider: this.name,
1059
+ model: this.model,
1060
+ text: '',
1061
+ latency_ms: _elapsedMs(t0),
1062
+ error: `os_error: ${exc.osName}`,
1063
+ metadata: { cli: true },
1064
+ });
1065
+ }
1066
+ throw exc;
1067
+ }
1068
+
1069
+ // 3. record the call — even failures count against the quota so a broken
1070
+ // CLI cannot burn the whole budget in a tight loop.
1071
+ try {
1072
+ record_cli_call(this.name, this._cli_calls_path);
1073
+ } catch {
1074
+ // state-file write failure is non-fatal here.
1075
+ }
1076
+
1077
+ const latency_ms = _elapsedMs(t0);
1078
+
1079
+ // 4. non-zero exit → classify and bail.
1080
+ if (proc.returncode !== 0) {
1081
+ const code = (this.constructor as typeof CliClient)._classify_stderr(
1082
+ proc.stderr || '',
1083
+ proc.returncode,
1084
+ );
1085
+ return new CouncilResponse({
1086
+ provider: this.name,
1087
+ model: this.model,
1088
+ text: '',
1089
+ latency_ms,
1090
+ error: code,
1091
+ metadata: {
1092
+ cli: true,
1093
+ returncode: proc.returncode,
1094
+ stderr_tail: (proc.stderr || '').slice(-500),
1095
+ },
1096
+ });
1097
+ }
1098
+
1099
+ // 5. parse stdout via the subclass hook.
1100
+ let response: CouncilResponse;
1101
+ try {
1102
+ response = this._parse_output(proc.stdout || '', proc.stderr || '');
1103
+ } catch (exc) {
1104
+ return new CouncilResponse({
1105
+ provider: this.name,
1106
+ model: this.model,
1107
+ text: proc.stdout || '',
1108
+ latency_ms,
1109
+ error: `parse_failed: ${_excTypeName(exc)}`,
1110
+ metadata: { cli: true, stderr_tail: (proc.stderr || '').slice(-500) },
1111
+ });
1112
+ }
1113
+ response.latency_ms = latency_ms;
1114
+ const meta: Record<string, unknown> = { ...response.metadata };
1115
+ if (!('cli' in meta)) {
1116
+ meta['cli'] = true;
1117
+ }
1118
+ response.metadata = meta;
1119
+ return response;
1120
+ }
1121
+
1122
+ static _classify_stderr(stderr: string, returncode: number): string {
1123
+ const haystack = stderr.toLowerCase();
1124
+ if (this._AUTH_FAILURE_PATTERNS.some((p) => haystack.includes(p))) {
1125
+ return 'auth_expired';
1126
+ }
1127
+ if (this._TIMEOUT_PATTERNS.some((p) => haystack.includes(p))) {
1128
+ return 'timeout';
1129
+ }
1130
+ if (this._QUOTA_PATTERNS.some((p) => haystack.includes(p))) {
1131
+ return 'cli_quota_exhausted';
1132
+ }
1133
+ return `exit_${returncode}`;
1134
+ }
1135
+ }
1136
+
1137
+ /**
1138
+ * Claude via the official `claude` CLI (subscription-authed).
1139
+ *
1140
+ * Invokes `claude --print --output-format json` and consumes the structured
1141
+ * envelope: `{"result": str, "usage": {"input_tokens": int, "output_tokens":
1142
+ * int}, "session_id": str, ...}`. The prompt is piped on stdin so it never
1143
+ * collides with argv length limits.
1144
+ */
1145
+ export class AnthropicCliClient extends CliClient {
1146
+ override name = 'anthropic';
1147
+ override default_binary = 'claude';
1148
+ override subscription_label = 'claude-pro';
1149
+
1150
+ constructor(opts: CliClientOptions = {}) {
1151
+ super({
1152
+ model: opts.model ?? 'claude-sonnet-4-5',
1153
+ binary: opts.binary,
1154
+ timeout_seconds: opts.timeout_seconds,
1155
+ max_calls_per_day: opts.max_calls_per_day,
1156
+ warn_at: opts.warn_at,
1157
+ cli_calls_path: opts.cli_calls_path,
1158
+ });
1159
+ }
1160
+
1161
+ protected override _build_command(
1162
+ system_prompt: string,
1163
+ user_prompt: string,
1164
+ max_tokens: number,
1165
+ ): string[] {
1166
+ void user_prompt;
1167
+ void max_tokens;
1168
+ return [
1169
+ this.binary,
1170
+ '--print',
1171
+ '--output-format',
1172
+ 'json',
1173
+ '--model',
1174
+ this.model,
1175
+ '--append-system-prompt',
1176
+ system_prompt,
1177
+ ];
1178
+ }
1179
+
1180
+ protected override _stdin_payload(system_prompt: string, user_prompt: string): string | null {
1181
+ void system_prompt;
1182
+ return user_prompt;
1183
+ }
1184
+
1185
+ protected override _parse_output(stdout: string, stderr: string): CouncilResponse {
1186
+ void stderr;
1187
+ const envelope = _jsonLoads(stdout);
1188
+ if (!_isPlainObject(envelope)) {
1189
+ throw new ValueError('expected JSON object at the top level of claude CLI output');
1190
+ }
1191
+ const env = envelope as Record<string, unknown>;
1192
+ const text = _pyStr(_dictGet(env, 'result', '')).trim();
1193
+ let usage = _dictGet(env, 'usage', null);
1194
+ if (!_pyTruthy(usage)) {
1195
+ usage = {};
1196
+ }
1197
+ if (!_isPlainObject(usage)) {
1198
+ usage = {};
1199
+ }
1200
+ const usageObj = usage as Record<string, unknown>;
1201
+ const meta: Record<string, unknown> = {};
1202
+ const session_id = _dictGet(env, 'session_id', undefined);
1203
+ if (_pyTruthy(session_id)) {
1204
+ meta['session_id'] = _pyStr(session_id);
1205
+ }
1206
+ const total_cost = _dictGet(env, 'total_cost_usd', null);
1207
+ if (total_cost !== null && total_cost !== undefined) {
1208
+ meta['reported_cost_usd'] = total_cost;
1209
+ }
1210
+ const duration_ms = _dictGet(env, 'duration_ms', null);
1211
+ if (duration_ms !== null && duration_ms !== undefined) {
1212
+ meta['reported_duration_ms'] = duration_ms;
1213
+ }
1214
+ return new CouncilResponse({
1215
+ provider: this.name,
1216
+ model: this.model,
1217
+ text,
1218
+ input_tokens: _pyIntCoerce(_dictGet(usageObj, 'input_tokens', 0)),
1219
+ output_tokens: _pyIntCoerce(_dictGet(usageObj, 'output_tokens', 0)),
1220
+ metadata: meta,
1221
+ });
1222
+ }
1223
+ }
1224
+
1225
+ /**
1226
+ * OpenAI via the official `codex` CLI (subscription-authed).
1227
+ *
1228
+ * Invokes `codex exec --json <prompt>` and consumes the newline-delimited JSON
1229
+ * event stream. The user prompt rides on argv (Codex does not read prompts from
1230
+ * stdin in `exec` mode); the system prompt is passed via `--system` when
1231
+ * non-empty.
1232
+ *
1233
+ * Output shape: one JSON object per line. The terminal event has
1234
+ * `type == "item.completed"` with the final assistant message in
1235
+ * `item.content[0].text`; a separate `type == "turn.completed"` event carries
1236
+ * token usage in `usage.input_tokens` / `usage.output_tokens`.
1237
+ */
1238
+ export class OpenAICliClient extends CliClient {
1239
+ override name = 'openai';
1240
+ override default_binary = 'codex';
1241
+ override subscription_label = 'chatgpt-plus';
1242
+
1243
+ static override _AUTH_FAILURE_PATTERNS: readonly string[] = [
1244
+ ...CliClient._AUTH_FAILURE_PATTERNS,
1245
+ 'codex login',
1246
+ 'auth_required',
1247
+ '401',
1248
+ ];
1249
+
1250
+ constructor(opts: CliClientOptions = {}) {
1251
+ super({
1252
+ model: opts.model ?? 'gpt-5',
1253
+ binary: opts.binary,
1254
+ timeout_seconds: opts.timeout_seconds,
1255
+ max_calls_per_day: opts.max_calls_per_day,
1256
+ warn_at: opts.warn_at,
1257
+ cli_calls_path: opts.cli_calls_path,
1258
+ });
1259
+ }
1260
+
1261
+ protected override _build_command(
1262
+ system_prompt: string,
1263
+ user_prompt: string,
1264
+ max_tokens: number,
1265
+ ): string[] {
1266
+ void max_tokens;
1267
+ const cmd = [this.binary, 'exec', '--json', '--model', this.model];
1268
+ if (system_prompt) {
1269
+ cmd.push('--system', system_prompt);
1270
+ }
1271
+ cmd.push(user_prompt);
1272
+ return cmd;
1273
+ }
1274
+
1275
+ protected override _parse_output(stdout: string, stderr: string): CouncilResponse {
1276
+ void stderr;
1277
+ let text = '';
1278
+ let input_tokens = 0;
1279
+ let output_tokens = 0;
1280
+ const meta: Record<string, unknown> = {};
1281
+ for (let line of _splitlines(stdout)) {
1282
+ line = line.trim();
1283
+ if (!line) {
1284
+ continue;
1285
+ }
1286
+ let event: unknown;
1287
+ try {
1288
+ event = _jsonLoads(line);
1289
+ } catch (exc) {
1290
+ if (exc instanceof JSONDecodeError) {
1291
+ continue;
1292
+ }
1293
+ throw exc;
1294
+ }
1295
+ if (!_isPlainObject(event)) {
1296
+ continue;
1297
+ }
1298
+ const ev = event as Record<string, unknown>;
1299
+ const event_type = _dictGet(ev, 'type', undefined);
1300
+ if (event_type === 'item.completed') {
1301
+ let item = _dictGet(ev, 'item', null);
1302
+ if (!_pyTruthy(item)) {
1303
+ item = {};
1304
+ }
1305
+ if (_isPlainObject(item)) {
1306
+ const itemObj = item as Record<string, unknown>;
1307
+ let content = _dictGet(itemObj, 'content', null);
1308
+ if (!_pyTruthy(content)) {
1309
+ content = [];
1310
+ }
1311
+ if (Array.isArray(content)) {
1312
+ const chunks: string[] = [];
1313
+ for (const entry of content) {
1314
+ if (_isPlainObject(entry) && _pyTruthy(_dictGet(entry as Record<string, unknown>, 'text', undefined))) {
1315
+ chunks.push(_pyStr((entry as Record<string, unknown>)['text']));
1316
+ }
1317
+ }
1318
+ if (chunks.length > 0) {
1319
+ text = chunks.join('\n').trim();
1320
+ }
1321
+ }
1322
+ if (_pyTruthy(_dictGet(itemObj, 'id', undefined))) {
1323
+ meta['item_id'] = _pyStr(itemObj['id']);
1324
+ }
1325
+ }
1326
+ } else if (event_type === 'turn.completed') {
1327
+ let usage = _dictGet(ev, 'usage', null);
1328
+ if (!_pyTruthy(usage)) {
1329
+ usage = {};
1330
+ }
1331
+ if (_isPlainObject(usage)) {
1332
+ const usageObj = usage as Record<string, unknown>;
1333
+ input_tokens = _pyIntCoerce(_dictGet(usageObj, 'input_tokens', 0));
1334
+ output_tokens = _pyIntCoerce(_dictGet(usageObj, 'output_tokens', 0));
1335
+ }
1336
+ } else if (event_type === 'session.created') {
1337
+ if (_pyTruthy(_dictGet(ev, 'session_id', undefined))) {
1338
+ meta['session_id'] = _pyStr(ev['session_id']);
1339
+ }
1340
+ }
1341
+ }
1342
+ return new CouncilResponse({
1343
+ provider: this.name,
1344
+ model: this.model,
1345
+ text,
1346
+ input_tokens,
1347
+ output_tokens,
1348
+ metadata: meta,
1349
+ });
1350
+ }
1351
+ }
1352
+
1353
+ /**
1354
+ * Google Gemini via the official `gemini` CLI (free-tier subscription).
1355
+ *
1356
+ * Invokes `gemini --prompt <prompt> --output-format json` and consumes the
1357
+ * structured envelope: `{"response": str, "stats": {"models": {"<model>":
1358
+ * {"tokens": {"prompt": int, "candidates": int}}}}, ...}`. Prompt is piped on
1359
+ * stdin to dodge argv limits.
1360
+ */
1361
+ export class GeminiCliClient extends CliClient {
1362
+ override name = 'gemini';
1363
+ override default_binary = 'gemini';
1364
+ override subscription_label = 'gemini-pro';
1365
+
1366
+ static override _AUTH_FAILURE_PATTERNS: readonly string[] = [
1367
+ ...CliClient._AUTH_FAILURE_PATTERNS,
1368
+ 'interactive consent could not be obtained',
1369
+ 'please run `gemini`',
1370
+ 'oauth',
1371
+ ];
1372
+
1373
+ constructor(opts: CliClientOptions = {}) {
1374
+ super({
1375
+ model: opts.model ?? 'gemini-2.5-pro',
1376
+ binary: opts.binary,
1377
+ timeout_seconds: opts.timeout_seconds,
1378
+ max_calls_per_day: opts.max_calls_per_day,
1379
+ warn_at: opts.warn_at,
1380
+ cli_calls_path: opts.cli_calls_path,
1381
+ });
1382
+ }
1383
+
1384
+ protected override _build_command(
1385
+ system_prompt: string,
1386
+ user_prompt: string,
1387
+ max_tokens: number,
1388
+ ): string[] {
1389
+ void user_prompt;
1390
+ void max_tokens;
1391
+ const cmd = [this.binary, '--output-format', 'json', '--model', this.model];
1392
+ if (system_prompt) {
1393
+ cmd.push('--system', system_prompt);
1394
+ }
1395
+ return cmd;
1396
+ }
1397
+
1398
+ protected override _stdin_payload(system_prompt: string, user_prompt: string): string | null {
1399
+ void system_prompt;
1400
+ return user_prompt;
1401
+ }
1402
+
1403
+ protected override _parse_output(stdout: string, stderr: string): CouncilResponse {
1404
+ void stderr;
1405
+ const envelope = _jsonLoads(stdout);
1406
+ if (!_isPlainObject(envelope)) {
1407
+ throw new ValueError('expected JSON object at the top level of gemini CLI output');
1408
+ }
1409
+ const env = envelope as Record<string, unknown>;
1410
+ const text = _pyStr(_dictGet(env, 'response', '')).trim();
1411
+ let input_tokens = 0;
1412
+ let output_tokens = 0;
1413
+ let stats = _dictGet(env, 'stats', null);
1414
+ if (!_pyTruthy(stats)) {
1415
+ stats = {};
1416
+ }
1417
+ if (_isPlainObject(stats)) {
1418
+ let models = _dictGet(stats as Record<string, unknown>, 'models', null);
1419
+ if (!_pyTruthy(models)) {
1420
+ models = {};
1421
+ }
1422
+ if (_isPlainObject(models)) {
1423
+ const modelsObj = models as Record<string, unknown>;
1424
+ // gemini emits per-model token counts; pick the configured model
1425
+ // if present, else the first model in the envelope.
1426
+ let model_stats: unknown = modelsObj[this.model];
1427
+ if (!_isPlainObject(model_stats)) {
1428
+ model_stats = _firstDictValue(modelsObj) ?? {};
1429
+ }
1430
+ let tokens: unknown;
1431
+ if (_isPlainObject(model_stats)) {
1432
+ tokens = _dictGet(model_stats as Record<string, unknown>, 'tokens', null);
1433
+ if (!_pyTruthy(tokens)) {
1434
+ tokens = {};
1435
+ }
1436
+ } else {
1437
+ tokens = {};
1438
+ }
1439
+ if (_isPlainObject(tokens)) {
1440
+ const tokensObj = tokens as Record<string, unknown>;
1441
+ input_tokens = _pyIntCoerce(_dictGet(tokensObj, 'prompt', 0));
1442
+ output_tokens = _pyIntCoerce(_dictGet(tokensObj, 'candidates', 0));
1443
+ }
1444
+ }
1445
+ }
1446
+ const meta: Record<string, unknown> = {};
1447
+ let session_id = _dictGet(env, 'sessionId', undefined);
1448
+ if (!_pyTruthy(session_id)) {
1449
+ session_id = _dictGet(env, 'session_id', undefined);
1450
+ }
1451
+ if (_pyTruthy(session_id)) {
1452
+ meta['session_id'] = _pyStr(session_id);
1453
+ }
1454
+ return new CouncilResponse({
1455
+ provider: this.name,
1456
+ model: this.model,
1457
+ text,
1458
+ input_tokens,
1459
+ output_tokens,
1460
+ metadata: meta,
1461
+ });
1462
+ }
1463
+ }
1464
+
1465
+ /**
1466
+ * xAI Grok via the community `grok` CLI (Superagent project).
1467
+ *
1468
+ * Community-maintained wrapper around the xAI API — **not** an official
1469
+ * subscription transport. The CLI consumes `XAI_API_KEY` from its own
1470
+ * environment, so every call is paid per-token exactly as `XAIClient` (api
1471
+ * transport) would be. `mode: cli` here is an ergonomic shortcut; it does NOT
1472
+ * bypass the USD cost gate.
1473
+ *
1474
+ * Invokes `grok -p <prompt>`. Output is plain text — no JSON envelope.
1475
+ * `_parse_output` returns the trimmed stdout and estimates token counts
1476
+ * heuristically (chars / 4) for the audit-trail.
1477
+ */
1478
+ export class XAICliClient extends CliClient {
1479
+ override name = 'xai';
1480
+ override default_binary = 'grok';
1481
+ override billable = true; // community CLI consumes an API key — billable applies
1482
+
1483
+ static override _AUTH_FAILURE_PATTERNS: readonly string[] = [
1484
+ ...CliClient._AUTH_FAILURE_PATTERNS,
1485
+ 'xai_api_key',
1486
+ '401',
1487
+ 'unauthorized',
1488
+ ];
1489
+
1490
+ constructor(opts: CliClientOptions = {}) {
1491
+ super({
1492
+ model: opts.model ?? DEFAULT_XAI_MODEL,
1493
+ binary: opts.binary,
1494
+ timeout_seconds: opts.timeout_seconds,
1495
+ max_calls_per_day: opts.max_calls_per_day,
1496
+ warn_at: opts.warn_at,
1497
+ cli_calls_path: opts.cli_calls_path,
1498
+ });
1499
+ }
1500
+
1501
+ protected override _build_command(
1502
+ system_prompt: string,
1503
+ user_prompt: string,
1504
+ max_tokens: number,
1505
+ ): string[] {
1506
+ void system_prompt;
1507
+ void max_tokens;
1508
+ const cmd = [this.binary, '-p', user_prompt];
1509
+ if (this.model) {
1510
+ cmd.push('--model', this.model);
1511
+ }
1512
+ return cmd;
1513
+ }
1514
+
1515
+ protected override _parse_output(stdout: string, stderr: string): CouncilResponse {
1516
+ void stderr;
1517
+ const text = stdout.trim();
1518
+ // Plain-text CLIs surface no token usage — estimate from text length so
1519
+ // the audit trail and post-call tracker stay populated. chars / 4
1520
+ // mirrors `pricing.estimate_input_tokens`.
1521
+ const output_tokens = text ? Math.max(1, Math.trunc(_pyLen(text) / 4)) : 0;
1522
+ return new CouncilResponse({
1523
+ provider: this.name,
1524
+ model: this.model,
1525
+ text,
1526
+ input_tokens: 0,
1527
+ output_tokens,
1528
+ metadata: { cli_output_format: 'plain_text', tokens_estimated: true },
1529
+ });
1530
+ }
1531
+ }
1532
+
1533
+ /**
1534
+ * Perplexity via the community `perplexity` CLI (npm package).
1535
+ *
1536
+ * Community-maintained wrapper around the Perplexity API — **not** an official
1537
+ * subscription transport. The CLI consumes `PERPLEXITY_API_KEY` from its own
1538
+ * environment, so every call is paid per-token exactly as `PerplexityClient`
1539
+ * (api transport) would be. `mode: cli` here is an ergonomic shortcut; it does
1540
+ * NOT bypass the USD cost gate.
1541
+ *
1542
+ * Invokes `perplexity -p <prompt>`. Output is plain text — no JSON envelope.
1543
+ */
1544
+ export class PerplexityCliClient extends CliClient {
1545
+ override name = 'perplexity';
1546
+ override default_binary = 'perplexity';
1547
+ override billable = true; // community CLI consumes an API key — billable applies
1548
+
1549
+ static override _AUTH_FAILURE_PATTERNS: readonly string[] = [
1550
+ ...CliClient._AUTH_FAILURE_PATTERNS,
1551
+ 'perplexity_api_key',
1552
+ '401',
1553
+ 'unauthorized',
1554
+ ];
1555
+
1556
+ constructor(opts: CliClientOptions = {}) {
1557
+ super({
1558
+ model: opts.model ?? DEFAULT_PERPLEXITY_MODEL,
1559
+ binary: opts.binary,
1560
+ timeout_seconds: opts.timeout_seconds,
1561
+ max_calls_per_day: opts.max_calls_per_day,
1562
+ warn_at: opts.warn_at,
1563
+ cli_calls_path: opts.cli_calls_path,
1564
+ });
1565
+ }
1566
+
1567
+ protected override _build_command(
1568
+ system_prompt: string,
1569
+ user_prompt: string,
1570
+ max_tokens: number,
1571
+ ): string[] {
1572
+ void system_prompt;
1573
+ void max_tokens;
1574
+ const cmd = [this.binary, '-p', user_prompt];
1575
+ if (this.model) {
1576
+ cmd.push('--model', this.model);
1577
+ }
1578
+ return cmd;
1579
+ }
1580
+
1581
+ protected override _parse_output(stdout: string, stderr: string): CouncilResponse {
1582
+ void stderr;
1583
+ const text = stdout.trim();
1584
+ const output_tokens = text ? Math.max(1, Math.trunc(_pyLen(text) / 4)) : 0;
1585
+ return new CouncilResponse({
1586
+ provider: this.name,
1587
+ model: this.model,
1588
+ text,
1589
+ input_tokens: 0,
1590
+ output_tokens,
1591
+ metadata: { cli_output_format: 'plain_text', tokens_estimated: true },
1592
+ });
1593
+ }
1594
+ }
1595
+
1596
+ // ── Manual mode (Phase 2b) ───────────────────────────────────────────
1597
+
1598
+ export const MANUAL_END_MARKER = 'END'; // line containing only this terminates a paste block.
1599
+
1600
+ /** Minimal line-reading stream interface (mirrors Python `TextIO`). */
1601
+ export interface TextInputStream {
1602
+ /** Yield successive lines (each may keep its trailing "\n"), like iterating a Python file. */
1603
+ [Symbol.iterator](): Iterator<string>;
1604
+ /** Read a single line (with trailing newline), or '' at EOF — Python `readline()`. */
1605
+ readline(): string;
1606
+ }
1607
+
1608
+ /** Minimal write stream interface (mirrors Python `TextIO`). */
1609
+ export interface TextOutputStream {
1610
+ write(s: string): void;
1611
+ flush(): void;
1612
+ }
1613
+
1614
+ /**
1615
+ * Read lines from `stream` until a line equal to `marker` (after strip).
1616
+ *
1617
+ * Returns the joined body without the marker line. EOF before the marker is
1618
+ * treated as end-of-input — the body collected so far is returned.
1619
+ */
1620
+ export function _read_until_marker(stream: TextInputStream, marker: string): string {
1621
+ const body: string[] = [];
1622
+ for (const raw of stream) {
1623
+ const line = _rstripNewline(raw);
1624
+ if (line.trim() === marker) {
1625
+ break;
1626
+ }
1627
+ body.push(line);
1628
+ }
1629
+ return body.join('\n').trim();
1630
+ }
1631
+
1632
+ /**
1633
+ * Copy-paste council member — user is the transport.
1634
+ *
1635
+ * `ask()` renders the system prompt + artefact as one Markdown block, prints it
1636
+ * to stdout, and reads pasted replies from stdin. After each pasted reply,
1637
+ * surfaces a 1/2/3 menu (more · next · abort). Loops until the user picks 2 or
1638
+ * 3.
1639
+ *
1640
+ * Spend is $0 — `billable=false` makes the orchestrator skip the cost gate.
1641
+ *
1642
+ * Tests inject `stdin` / `stdout` streams. Production usage falls back to
1643
+ * process stdin / stdout.
1644
+ */
1645
+ export class ManualClient extends ExternalAIClient {
1646
+ override billable = false;
1647
+ override transport = 'manual';
1648
+ provider_label: string;
1649
+ private _stdin: TextInputStream;
1650
+ private _stdout: TextOutputStream;
1651
+ private _end_marker: string;
1652
+
1653
+ constructor(
1654
+ opts: {
1655
+ name?: string;
1656
+ model?: string;
1657
+ provider_label?: string;
1658
+ stdin?: TextInputStream | null;
1659
+ stdout?: TextOutputStream | null;
1660
+ end_marker?: string;
1661
+ } = {},
1662
+ ) {
1663
+ super();
1664
+ this.name = opts.name ?? 'manual';
1665
+ this.model = opts.model ?? 'manual';
1666
+ this.provider_label = opts.provider_label ?? 'your LLM web UI';
1667
+ this._stdin = opts.stdin ?? _defaultStdin();
1668
+ this._stdout = opts.stdout ?? _defaultStdout();
1669
+ this._end_marker = opts.end_marker ?? MANUAL_END_MARKER;
1670
+ }
1671
+
1672
+ override ask(
1673
+ system_prompt: string,
1674
+ user_prompt: string,
1675
+ max_tokens: number = DEFAULT_MAX_TOKENS,
1676
+ ): CouncilResponse {
1677
+ void max_tokens; // accepted for ABC parity
1678
+ const t0 = _nowMs();
1679
+ const rounds: string[] = [];
1680
+ let block = this._render_block(system_prompt, user_prompt, null);
1681
+ this._emit(block);
1682
+
1683
+ try {
1684
+ for (;;) {
1685
+ const reply = _read_until_marker(this._stdin, this._end_marker);
1686
+ rounds.push(reply);
1687
+ const choice = this._ask_menu(_pyLen(reply));
1688
+
1689
+ if (choice === '2') {
1690
+ // done with this member
1691
+ break;
1692
+ }
1693
+ if (choice === '3') {
1694
+ // abort the council run
1695
+ return new CouncilResponse({
1696
+ provider: this.name,
1697
+ model: this.model,
1698
+ text: '',
1699
+ latency_ms: _elapsedMs(t0),
1700
+ error: 'manual_aborted',
1701
+ metadata: { rounds: rounds.length, manual: true },
1702
+ });
1703
+ }
1704
+ // choice == "1": collect follow-up, re-emit context block.
1705
+ const follow_up = this._read_follow_up();
1706
+ if (!follow_up) {
1707
+ break; // empty follow-up → treat as "done with this member"
1708
+ }
1709
+ rounds.push(`[follow-up sent]\n${follow_up}`);
1710
+ block = this._render_block(system_prompt, user_prompt, follow_up);
1711
+ this._emit(block);
1712
+ }
1713
+ } catch (exc) {
1714
+ return new CouncilResponse({
1715
+ provider: this.name,
1716
+ model: this.model,
1717
+ text: rounds.join('\n\n'),
1718
+ latency_ms: _elapsedMs(t0),
1719
+ error: _excString(exc),
1720
+ metadata: { rounds: rounds.length, manual: true },
1721
+ });
1722
+ }
1723
+
1724
+ const text = rounds.join('\n\n---\n\n').trim();
1725
+ return new CouncilResponse({
1726
+ provider: this.name,
1727
+ model: this.model,
1728
+ text,
1729
+ latency_ms: _elapsedMs(t0),
1730
+ metadata: { rounds: rounds.length, manual: true },
1731
+ });
1732
+ }
1733
+
1734
+ // ── helpers ──────────────────────────────────────────────────────
1735
+
1736
+ private _emit(text: string): void {
1737
+ this._stdout.write(text);
1738
+ this._stdout.write('\n');
1739
+ this._stdout.flush();
1740
+ }
1741
+
1742
+ private _render_block(
1743
+ system_prompt: string,
1744
+ user_prompt: string,
1745
+ follow_up: string | null,
1746
+ ): string {
1747
+ const bar = '═'.repeat(67);
1748
+ const head =
1749
+ `${bar}\n` +
1750
+ `Manual council member: ${this.provider_label}\n` +
1751
+ 'Paste this block into the web UI · then paste the reply below.\n' +
1752
+ `${bar}`;
1753
+ let body: string;
1754
+ if (follow_up !== null) {
1755
+ body = `[Follow-up — paste this into the SAME chat thread]\n\n${follow_up}`;
1756
+ } else {
1757
+ body = `${system_prompt}\n\n---\n\n${user_prompt}`;
1758
+ }
1759
+ const tail =
1760
+ `${bar}\n` +
1761
+ `End your pasted reply with a line containing only: ${this._end_marker}\n` +
1762
+ `${bar}`;
1763
+ return `${head}\n\n${body}\n\n${tail}`;
1764
+ }
1765
+
1766
+ private _ask_menu(reply_chars: number): string {
1767
+ const prompt =
1768
+ `\nReply received (${reply_chars} chars). Now what?\n` +
1769
+ ' 1. More feedback for this member (continue this thread)\n' +
1770
+ ' 2. Done with this member, move to the next\n' +
1771
+ ' 3. Abort the council run\n\n' +
1772
+ 'Choose 1/2/3: ';
1773
+ this._stdout.write(prompt);
1774
+ this._stdout.flush();
1775
+ const line = this._stdin.readline().trim();
1776
+ if (line === '1' || line === '2' || line === '3') {
1777
+ return line;
1778
+ }
1779
+ // unknown input → treat as "next" so we never block forever in tests / piped runs.
1780
+ return '2';
1781
+ }
1782
+
1783
+ private _read_follow_up(): string {
1784
+ this._emit(
1785
+ `\nType your follow-up question, end with a line containing only: ${this._end_marker}`,
1786
+ );
1787
+ return _read_until_marker(this._stdin, this._end_marker);
1788
+ }
1789
+ }
1790
+
1791
+ // ── primitives ─────────────────────────────────────────────────────────
1792
+
1793
+ /** Python `ValueError`. */
1794
+ export class ValueError extends Error {
1795
+ constructor(message: string) {
1796
+ super(message);
1797
+ this.name = 'ValueError';
1798
+ }
1799
+ }
1800
+
1801
+ /** Python `json.JSONDecodeError`. */
1802
+ export class JSONDecodeError extends Error {
1803
+ constructor(message: string) {
1804
+ super(message);
1805
+ this.name = 'JSONDecodeError';
1806
+ }
1807
+ }
1808
+
1809
+ /** `json.loads` that raises `JSONDecodeError` (not the native SyntaxError). */
1810
+ function _jsonLoads(s: string): unknown {
1811
+ try {
1812
+ return JSON.parse(s);
1813
+ } catch (exc) {
1814
+ throw new JSONDecodeError(exc instanceof Error ? exc.message : String(exc));
1815
+ }
1816
+ }
1817
+
1818
+ /** True for a plain object (Python `isinstance(x, dict)`), not array / null. */
1819
+ function _isPlainObject(x: unknown): x is Record<string, unknown> {
1820
+ return typeof x === 'object' && x !== null && !Array.isArray(x);
1821
+ }
1822
+
1823
+ /** Mirror Python truthiness: '', 0, 0.0, false, null/undefined, [], {} are falsy. */
1824
+ function _pyTruthy(x: unknown): boolean {
1825
+ if (x === null || x === undefined) {
1826
+ return false;
1827
+ }
1828
+ if (typeof x === 'boolean') {
1829
+ return x;
1830
+ }
1831
+ if (typeof x === 'number') {
1832
+ return x !== 0;
1833
+ }
1834
+ if (typeof x === 'string') {
1835
+ return x.length > 0;
1836
+ }
1837
+ if (Array.isArray(x)) {
1838
+ return x.length > 0;
1839
+ }
1840
+ if (typeof x === 'object') {
1841
+ return Object.keys(x as Record<string, unknown>).length > 0;
1842
+ }
1843
+ return true;
1844
+ }
1845
+
1846
+ /** `dict.get(key, default)` over a duck-typed object. */
1847
+ function _dictGet(obj: Record<string, unknown>, key: string, fallback: unknown): unknown {
1848
+ return key in obj ? obj[key] : fallback;
1849
+ }
1850
+
1851
+ /** First dict-typed value in insertion order (mirrors `next(v for v ... if isinstance(v, dict))`). */
1852
+ function _firstDictValue(obj: Record<string, unknown>): Record<string, unknown> | undefined {
1853
+ for (const v of Object.values(obj)) {
1854
+ if (_isPlainObject(v)) {
1855
+ return v;
1856
+ }
1857
+ }
1858
+ return undefined;
1859
+ }
1860
+
1861
+ /** Python `str(x)`. */
1862
+ function _pyStr(x: unknown): string {
1863
+ if (x === null) {
1864
+ return 'None';
1865
+ }
1866
+ if (x === undefined) {
1867
+ // Defensive: undefined never reaches str() in the Python flow; treat as
1868
+ // empty per the `.get(..., "")` defaults that precede every call.
1869
+ return '';
1870
+ }
1871
+ if (typeof x === 'boolean') {
1872
+ return x ? 'True' : 'False';
1873
+ }
1874
+ return String(x);
1875
+ }
1876
+
1877
+ /**
1878
+ * Python `int(x or 0)` applied to JSON-loaded values where the source code does
1879
+ * `int(usage.get("k", 0) or 0)` — None/0/"" coerce to 0, ints pass, numeric
1880
+ * strings parse. Floats truncate toward zero.
1881
+ */
1882
+ function _pyIntCoerce(x: unknown): number {
1883
+ // `x or 0` first: falsy → 0.
1884
+ if (!_pyTruthy(x)) {
1885
+ return 0;
1886
+ }
1887
+ if (typeof x === 'number') {
1888
+ return Math.trunc(x);
1889
+ }
1890
+ if (typeof x === 'boolean') {
1891
+ return x ? 1 : 0;
1892
+ }
1893
+ if (typeof x === 'string') {
1894
+ return _pyIntFromStr(x);
1895
+ }
1896
+ // int() on a dict/list raises TypeError in Python — surface it.
1897
+ throw new TypeError(`int() argument must be a number or string, not ${typeof x}`);
1898
+ }
1899
+
1900
+ /** Python `int(value)` for an already-int-typed number (used in quota_summary_line). */
1901
+ function _pyInt(n: number): number {
1902
+ return Math.trunc(n);
1903
+ }
1904
+
1905
+ /** Python `int(str)` — strict base-10 with surrounding whitespace allowed. */
1906
+ function _pyIntFromStr(s: string): number {
1907
+ const t = s.trim();
1908
+ if (!/^[+-]?\d+$/.test(t)) {
1909
+ throw new ValueError(`invalid literal for int() with base 10: ${_pyRepr(s)}`);
1910
+ }
1911
+ return parseInt(t, 10);
1912
+ }
1913
+
1914
+ /** Python `len(str)` — Unicode code-point count, not UTF-16 units. */
1915
+ function _pyLen(s: string): number {
1916
+ let n = 0;
1917
+ for (const _ of s) {
1918
+ void _;
1919
+ n += 1;
1920
+ }
1921
+ return n;
1922
+ }
1923
+
1924
+ /** Python `str.rstrip("\n")` — strip only trailing newline chars. */
1925
+ function _rstripNewline(s: string): string {
1926
+ let end = s.length;
1927
+ while (end > 0 && s[end - 1] === '\n') {
1928
+ end -= 1;
1929
+ }
1930
+ return s.slice(0, end);
1931
+ }
1932
+
1933
+ /** Python `str.splitlines()` for the line shapes a CLI stream emits. */
1934
+ function _splitlines(s: string): string[] {
1935
+ if (s === '') {
1936
+ return [];
1937
+ }
1938
+ // Python splitlines() splits on \n, \r, \r\n (and more); CLI JSONL only
1939
+ // ever uses \n / \r\n. Match the universal-newline subset that matters here.
1940
+ const out: string[] = [];
1941
+ let cur = '';
1942
+ for (let i = 0; i < s.length; i += 1) {
1943
+ const ch = s[i];
1944
+ if (ch === '\n') {
1945
+ out.push(cur);
1946
+ cur = '';
1947
+ } else if (ch === '\r') {
1948
+ out.push(cur);
1949
+ cur = '';
1950
+ if (s[i + 1] === '\n') {
1951
+ i += 1;
1952
+ }
1953
+ } else {
1954
+ cur += ch;
1955
+ }
1956
+ }
1957
+ if (cur !== '') {
1958
+ out.push(cur);
1959
+ }
1960
+ return out;
1961
+ }
1962
+
1963
+ /** Render a value with Python `repr()` for the strings this module reprs. */
1964
+ function _pyRepr(x: unknown): string {
1965
+ if (typeof x === 'string') {
1966
+ // Python prefers single quotes unless the string has a single quote and
1967
+ // no double quote.
1968
+ const hasSingle = x.includes("'");
1969
+ const hasDouble = x.includes('"');
1970
+ const quote = hasSingle && !hasDouble ? '"' : "'";
1971
+ let out = quote;
1972
+ for (const ch of x) {
1973
+ if (ch === '\\') {
1974
+ out += '\\\\';
1975
+ } else if (ch === quote) {
1976
+ out += `\\${quote}`;
1977
+ } else if (ch === '\n') {
1978
+ out += '\\n';
1979
+ } else if (ch === '\r') {
1980
+ out += '\\r';
1981
+ } else if (ch === '\t') {
1982
+ out += '\\t';
1983
+ } else {
1984
+ out += ch;
1985
+ }
1986
+ }
1987
+ return out + quote;
1988
+ }
1989
+ return String(x);
1990
+ }
1991
+
1992
+ /** Python `oct(mode)` → "0o600" style string. */
1993
+ function _octRepr(mode: number): string {
1994
+ return `0o${mode.toString(8)}`;
1995
+ }
1996
+
1997
+ /** Build `f"{type(exc).__name__}: {exc}"` for caught errors. */
1998
+ function _excString(exc: unknown): string {
1999
+ return `${_excTypeName(exc)}: ${_excMessage(exc)}`;
2000
+ }
2001
+
2002
+ function _excTypeName(exc: unknown): string {
2003
+ if (exc instanceof KeyGateError) return 'KeyGateError';
2004
+ if (exc instanceof CliClientError) return 'CliClientError';
2005
+ if (exc instanceof ValueError) return 'ValueError';
2006
+ if (exc instanceof JSONDecodeError) return 'JSONDecodeError';
2007
+ if (exc instanceof TypeError) return 'TypeError';
2008
+ if (exc instanceof RangeError) return 'RangeError';
2009
+ if (exc instanceof Error) {
2010
+ return exc.name || 'Error';
2011
+ }
2012
+ return typeof exc;
2013
+ }
2014
+
2015
+ function _excMessage(exc: unknown): string {
2016
+ if (exc instanceof Error) {
2017
+ return exc.message;
2018
+ }
2019
+ return String(exc);
2020
+ }
2021
+
2022
+ /** `json.dumps(obj, indent=2)` — default ensure_ascii=True, insertion-order keys. */
2023
+ function _jsonDumpsIndent2(value: unknown): string {
2024
+ return _jsonDumpsIndented(value, 2, 0);
2025
+ }
2026
+
2027
+ function _jsonDumpsIndented(value: unknown, indent: number, level: number): string {
2028
+ if (value === null || value === undefined) {
2029
+ return 'null';
2030
+ }
2031
+ switch (typeof value) {
2032
+ case 'boolean':
2033
+ return value ? 'true' : 'false';
2034
+ case 'number':
2035
+ return _pyJsonNumber(value);
2036
+ case 'string':
2037
+ return _pyJsonStringAscii(value);
2038
+ case 'object':
2039
+ break;
2040
+ default:
2041
+ throw new TypeError(`Object of type ${typeof value} is not JSON serializable`);
2042
+ }
2043
+ const pad = ' '.repeat(indent * (level + 1));
2044
+ const closePad = ' '.repeat(indent * level);
2045
+ if (Array.isArray(value)) {
2046
+ if (value.length === 0) {
2047
+ return '[]';
2048
+ }
2049
+ const items = value.map((v) => pad + _jsonDumpsIndented(v, indent, level + 1));
2050
+ return `[\n${items.join(',\n')}\n${closePad}]`;
2051
+ }
2052
+ const obj = value as Record<string, unknown>;
2053
+ const keys = Object.keys(obj);
2054
+ if (keys.length === 0) {
2055
+ return '{}';
2056
+ }
2057
+ const items = keys.map(
2058
+ (k) => `${pad}${_pyJsonStringAscii(k)}: ${_jsonDumpsIndented(obj[k], indent, level + 1)}`,
2059
+ );
2060
+ return `{\n${items.join(',\n')}\n${closePad}}`;
2061
+ }
2062
+
2063
+ /** Render a number like Python `json.dumps` (int vs float; JS has one type). */
2064
+ function _pyJsonNumber(n: number): string {
2065
+ if (!Number.isFinite(n)) {
2066
+ if (Number.isNaN(n)) {
2067
+ return 'NaN';
2068
+ }
2069
+ return n > 0 ? 'Infinity' : '-Infinity';
2070
+ }
2071
+ return String(n);
2072
+ }
2073
+
2074
+ /** Escape a string like Python `json.dumps(..., ensure_ascii=True)` (default). */
2075
+ function _pyJsonStringAscii(s: string): string {
2076
+ let out = '"';
2077
+ for (const ch of s) {
2078
+ const code = ch.codePointAt(0) ?? 0;
2079
+ if (ch === '"') {
2080
+ out += '\\"';
2081
+ } else if (ch === '\\') {
2082
+ out += '\\\\';
2083
+ } else if (ch === '\b') {
2084
+ out += '\\b';
2085
+ } else if (ch === '\f') {
2086
+ out += '\\f';
2087
+ } else if (ch === '\n') {
2088
+ out += '\\n';
2089
+ } else if (ch === '\r') {
2090
+ out += '\\r';
2091
+ } else if (ch === '\t') {
2092
+ out += '\\t';
2093
+ } else if (code < 0x20) {
2094
+ out += `\\u${code.toString(16).padStart(4, '0')}`;
2095
+ } else if (code < 0x7f) {
2096
+ out += ch;
2097
+ } else if (code <= 0xffff) {
2098
+ out += `\\u${code.toString(16).padStart(4, '0')}`;
2099
+ } else {
2100
+ // Astral plane → surrogate pair, matching Python json.dumps default.
2101
+ const c = code - 0x10000;
2102
+ const hi = 0xd800 + (c >> 10);
2103
+ const lo = 0xdc00 + (c & 0x3ff);
2104
+ out += `\\u${hi.toString(16).padStart(4, '0')}\\u${lo.toString(16).padStart(4, '0')}`;
2105
+ }
2106
+ }
2107
+ return out + '"';
2108
+ }
2109
+
2110
+ /** Python `shutil.which(name)` — resolve an executable on PATH. */
2111
+ function _which(name: string): string | null {
2112
+ // Absolute / relative path with a separator: check directly (Python's which
2113
+ // checks the given path when it contains a dir component).
2114
+ if (name.includes(path.sep) || (path.sep !== '/' && name.includes('/'))) {
2115
+ return _isExecutable(name) ? name : null;
2116
+ }
2117
+ const pathEnv = process.env.PATH ?? '';
2118
+ if (!pathEnv) {
2119
+ return null;
2120
+ }
2121
+ const exts =
2122
+ process.platform === 'win32'
2123
+ ? (process.env.PATHEXT ?? '.COM;.EXE;.BAT;.CMD').split(';').filter(Boolean)
2124
+ : [''];
2125
+ for (const dir of pathEnv.split(path.delimiter)) {
2126
+ if (!dir) {
2127
+ continue;
2128
+ }
2129
+ for (const ext of exts) {
2130
+ const candidate = path.join(dir, name + ext);
2131
+ if (_isExecutable(candidate)) {
2132
+ return candidate;
2133
+ }
2134
+ }
2135
+ }
2136
+ return null;
2137
+ }
2138
+
2139
+ function _isExecutable(p: string): boolean {
2140
+ try {
2141
+ const st = fs.statSync(p);
2142
+ if (!st.isFile()) {
2143
+ return false;
2144
+ }
2145
+ if (process.platform === 'win32') {
2146
+ return true;
2147
+ }
2148
+ // Any execute bit set (mirrors os.access(p, X_OK) closely enough).
2149
+ return (st.mode & 0o111) !== 0;
2150
+ } catch {
2151
+ return false;
2152
+ }
2153
+ }
2154
+
2155
+ // ── default manual-mode streams (process stdin/stdout) ─────────────────
2156
+
2157
+ function _defaultStdout(): TextOutputStream {
2158
+ return {
2159
+ write(s: string): void {
2160
+ process.stdout.write(s);
2161
+ },
2162
+ flush(): void {
2163
+ // process.stdout is auto-flushing; no-op.
2164
+ },
2165
+ };
2166
+ }
2167
+
2168
+ function _defaultStdin(): TextInputStream {
2169
+ // Production manual mode reads from process.stdin synchronously line by
2170
+ // line via fd 0. Tests always inject a stream, so this is the fallback.
2171
+ let buffer: string | null = null;
2172
+ let pos = 0;
2173
+ function ensure(): string {
2174
+ if (buffer === null) {
2175
+ try {
2176
+ buffer = fs.readFileSync(0, { encoding: 'utf-8' });
2177
+ } catch {
2178
+ buffer = '';
2179
+ }
2180
+ }
2181
+ return buffer;
2182
+ }
2183
+ function nextLine(): string {
2184
+ const buf = ensure();
2185
+ if (pos >= buf.length) {
2186
+ return '';
2187
+ }
2188
+ const nl = buf.indexOf('\n', pos);
2189
+ if (nl === -1) {
2190
+ const line = buf.slice(pos);
2191
+ pos = buf.length;
2192
+ return line;
2193
+ }
2194
+ const line = buf.slice(pos, nl + 1);
2195
+ pos = nl + 1;
2196
+ return line;
2197
+ }
2198
+ return {
2199
+ readline(): string {
2200
+ return nextLine();
2201
+ },
2202
+ [Symbol.iterator](): Iterator<string> {
2203
+ return {
2204
+ next(): IteratorResult<string> {
2205
+ const line = nextLine();
2206
+ if (line === '') {
2207
+ return { done: true, value: undefined };
2208
+ }
2209
+ return { done: false, value: line };
2210
+ },
2211
+ };
2212
+ },
2213
+ };
2214
+ }