claude-flow-novice 2.15.11 → 2.16.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 (741) hide show
  1. package/.claude/cfn-extras/agents/cfn-v3-coordinator.md +517 -0
  2. package/.claude/cfn-extras/skills/GOOGLE_SHEETS_SKILLS_README.md +1 -1
  3. package/.claude/cfn-extras/skills/google-sheets-api-coordinator/SKILL.md +1 -1
  4. package/.claude/cfn-extras/skills/google-sheets-formula-builder/SKILL.md +1 -1
  5. package/.claude/cfn-extras/skills/google-sheets-progress/SKILL.md +1 -1
  6. package/.claude/commands/CFN_LOOP_FRONTEND.md +1 -1
  7. package/.claude/commands/cfn-loop-cli.md +214 -442
  8. package/.claude/commands/cfn-loop-frontend.md +1 -1
  9. package/.claude/commands/cfn-loop-task.md +2 -2
  10. package/.claude/commands/cfn-loop-trigger.md +114 -0
  11. package/.claude/commands/deprecated/cfn-loop.md +2 -2
  12. package/.claude/hooks/cfn-invoke-post-edit-ts.sh +100 -0
  13. package/.claude/hooks/cfn-invoke-post-edit-ts.sh.backup +78 -0
  14. package/.claude/hooks/cfn-invoke-post-edit.sh +53 -5
  15. package/.claude/hooks/cfn-invoke-post-edit.sh.backup +87 -0
  16. package/.claude/hooks/cfn-invoke-pre-edit-ts.sh +116 -0
  17. package/.claude/hooks/cfn-invoke-pre-edit-ts.sh.backup +94 -0
  18. package/.claude/hooks/cfn-invoke-pre-edit.sh +22 -0
  19. package/.claude/hooks/cfn-invoke-pre-edit.sh.backup +88 -0
  20. package/.claude/hooks/cfn-post-edit.config.json +9 -2
  21. package/.claude/root-claude-distribute/CFN-CLAUDE.md +1 -1
  22. package/.claude/skills/cfn-agent-spawning/SKILL.md +48 -1
  23. package/.claude/skills/cfn-agent-spawning/SKILL.md.backup +135 -0
  24. package/.claude/skills/cfn-agent-spawning/TYPESCRIPT_MIGRATION.md +567 -0
  25. package/.claude/skills/cfn-agent-spawning/check-dependencies.sh +22 -0
  26. package/.claude/skills/{cfn-redis-coordination/check-dependencies.sh → cfn-agent-spawning/check-dependencies.sh.backup} +3 -5
  27. package/.claude/skills/cfn-agent-spawning/get-agent-provider-env.sh +22 -0
  28. package/.claude/skills/cfn-agent-spawning/get-agent-provider-env.sh.backup +127 -0
  29. package/.claude/skills/cfn-agent-spawning/parse-agent-provider.sh +22 -0
  30. package/.claude/skills/cfn-agent-spawning/parse-agent-provider.sh.backup +59 -0
  31. package/.claude/skills/cfn-agent-spawning/spawn-agent-wrapper.sh +63 -0
  32. package/.claude/skills/cfn-agent-spawning/spawn-agent-wrapper.sh.backup +41 -0
  33. package/.claude/skills/cfn-agent-spawning/spawn-agent.sh +26 -1
  34. package/.claude/skills/cfn-agent-spawning/spawn-templates.sh +22 -0
  35. package/.claude/skills/cfn-agent-spawning/spawn-templates.sh.backup +613 -0
  36. package/.claude/skills/cfn-agent-spawning/spawn-worker.sh +22 -0
  37. package/.claude/skills/cfn-agent-spawning/spawn-worker.sh.backup +176 -0
  38. package/.claude/skills/cfn-backlog-management/SKILL.md +1 -1
  39. package/.claude/skills/cfn-loop-orchestration/.backups/unknown/1763619700_33aff4a69b99159e4e849107ebc4d09f/metadata.json +8 -0
  40. package/.claude/skills/cfn-loop-orchestration/.backups/unknown/1763619700_33aff4a69b99159e4e849107ebc4d09f/original +271 -0
  41. package/.claude/skills/cfn-loop-orchestration/.backups/unknown/1763619700_33aff4a69b99159e4e849107ebc4d09f/revert.sh +7 -0
  42. package/.claude/skills/cfn-loop-orchestration/.backups/unknown/1763671642_06496e8c399a79db08167cc00ed4b31e/metadata.json +8 -0
  43. package/.claude/skills/cfn-loop-orchestration/.backups/unknown/1763671642_06496e8c399a79db08167cc00ed4b31e/original +325 -0
  44. package/.claude/skills/cfn-loop-orchestration/.backups/unknown/1763671642_06496e8c399a79db08167cc00ed4b31e/revert.sh +7 -0
  45. package/.claude/skills/cfn-loop-orchestration/CLI_IMPLEMENTATION_SUMMARY.md +330 -0
  46. package/.claude/skills/cfn-loop-orchestration/CONFIGURATION_IMPROVEMENTS.md +318 -0
  47. package/.claude/skills/cfn-loop-orchestration/CONTEXT_LOOKUP_MIGRATION.md +308 -0
  48. package/.claude/skills/cfn-loop-orchestration/CONTEXT_LOOKUP_QUICK_START.md +378 -0
  49. package/.claude/skills/cfn-loop-orchestration/E2E_VALIDATION_REPORT.md +262 -0
  50. package/.claude/skills/cfn-loop-orchestration/IMPLEMENTATION_SUMMARY.md +319 -519
  51. package/.claude/skills/cfn-loop-orchestration/NORTH_STAR_E2E_REPORT.md +299 -0
  52. package/.claude/skills/cfn-loop-orchestration/NORTH_STAR_EXECUTION_SUMMARY.md +403 -0
  53. package/.claude/skills/cfn-loop-orchestration/NORTH_STAR_INDEX.md +323 -0
  54. package/.claude/skills/cfn-loop-orchestration/SKILL.md +159 -48
  55. package/.claude/skills/cfn-loop-orchestration/SPAWN_AGENTS_IMPLEMENTATION.md +188 -0
  56. package/.claude/skills/cfn-loop-orchestration/TEST_COVERAGE_REPORT.md +335 -0
  57. package/.claude/skills/cfn-loop-orchestration/TEST_COVERAGE_SUMMARY.md +456 -0
  58. package/.claude/skills/cfn-loop-orchestration/TYPESCRIPT_INTEGRATION_REPORT.md +709 -0
  59. package/.claude/skills/cfn-loop-orchestration/TYPESCRIPT_INTEGRATION_SUMMARY.md +257 -0
  60. package/.claude/skills/cfn-loop-orchestration/VALIDATION_REPORT.md +572 -0
  61. package/.claude/skills/cfn-loop-orchestration/VALIDATION_SUMMARY.txt +196 -0
  62. package/.claude/skills/cfn-loop-orchestration/VALIDATOR_MODULE_GUIDE.md +526 -0
  63. package/.claude/skills/cfn-loop-orchestration/archive/legacy-bash/README.md +167 -0
  64. package/.claude/skills/cfn-loop-orchestration/archive/legacy-bash/orchestrate-enhanced.sh +548 -0
  65. package/.claude/skills/cfn-loop-orchestration/{orchestrate-wrapper.sh → archive/legacy-bash/orchestrate-wrapper.sh} +11 -1
  66. package/.claude/skills/cfn-loop-orchestration/archive/legacy-bash/orchestrate.sh +182 -0
  67. package/.claude/skills/cfn-loop-orchestration/e2e-validation-fixed.js +240 -0
  68. package/.claude/skills/cfn-loop-orchestration/e2e-validation.js +213 -0
  69. package/.claude/skills/cfn-loop-orchestration/package-lock.json +3 -0
  70. package/.claude/skills/cfn-loop-orchestration/package.json +4 -0
  71. package/.claude/skills/cfn-loop-orchestration/run-north-star-e2e.ts +210 -0
  72. package/.claude/skills/cfn-loop-orchestration/src/cli/orchestrator-cli.ts +396 -0
  73. package/.claude/skills/cfn-loop-orchestration/src/helpers/CONFIDENCE_AGGREGATOR.md +564 -0
  74. package/.claude/skills/cfn-loop-orchestration/src/helpers/CONFIDENCE_AGGREGATOR_QUICK_REF.md +241 -0
  75. package/.claude/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_IMPLEMENTATION.md +375 -0
  76. package/.claude/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_QUICK_REFERENCE.md +362 -0
  77. package/.claude/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_README.md +307 -0
  78. package/.claude/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_USAGE_GUIDE.md +508 -0
  79. package/.claude/skills/cfn-loop-orchestration/src/helpers/confidence-aggregator.ts +473 -0
  80. package/.claude/skills/cfn-loop-orchestration/src/helpers/consensus.ts +1 -1
  81. package/.claude/skills/cfn-loop-orchestration/src/helpers/context-injector.ts +349 -0
  82. package/.claude/skills/cfn-loop-orchestration/src/helpers/context-lookup.ts +486 -0
  83. package/.claude/skills/cfn-loop-orchestration/src/helpers/deliverable-verifier.ts +6 -2
  84. package/.claude/skills/cfn-loop-orchestration/src/helpers/gate-check.ts +1 -1
  85. package/.claude/skills/cfn-loop-orchestration/src/helpers/product-owner-decision.ts +316 -0
  86. package/.claude/skills/cfn-loop-orchestration/src/helpers/spawn-agents.ts +357 -0
  87. package/.claude/skills/cfn-loop-orchestration/src/helpers/validator.ts +276 -0
  88. package/.claude/skills/cfn-loop-orchestration/src/index.ts +2 -0
  89. package/.claude/skills/cfn-loop-orchestration/src/orchestrate.ts +743 -2
  90. package/.claude/skills/cfn-loop-orchestration/src/types.ts +56 -0
  91. package/.claude/skills/cfn-loop-orchestration/test-cli.sh +92 -0
  92. package/.claude/skills/cfn-loop-orchestration/test-typescript-integration.sh +442 -0
  93. package/.claude/skills/cfn-loop-orchestration/tests/agent-spawner.test.ts +124 -0
  94. package/.claude/skills/cfn-loop-orchestration/tests/confidence-aggregator.test.ts +604 -0
  95. package/.claude/skills/cfn-loop-orchestration/tests/context-injector.test.ts +561 -0
  96. package/.claude/skills/cfn-loop-orchestration/tests/context-lookup.test.ts +661 -0
  97. package/.claude/skills/cfn-loop-orchestration/tests/deliverable-verifier.test.ts +2 -2
  98. package/.claude/skills/cfn-loop-orchestration/tests/gate-check-edge-cases.test.ts +422 -0
  99. package/.claude/skills/cfn-loop-orchestration/tests/gate-checker.test.ts +276 -0
  100. package/.claude/skills/cfn-loop-orchestration/tests/logger.test.ts +291 -0
  101. package/.claude/skills/cfn-loop-orchestration/tests/north-star-e2e.test.ts +334 -0
  102. package/.claude/skills/cfn-loop-orchestration/tests/redis-coordinator.test.ts +321 -0
  103. package/.claude/skills/cfn-loop-orchestration/tests/spawn-agents.test.ts +284 -0
  104. package/.claude/skills/cfn-loop-orchestration/tests/validator.test.ts +643 -0
  105. package/.claude/skills/cfn-loop-validation/IMPLEMENTATION_SUMMARY.md +672 -0
  106. package/.claude/skills/cfn-loop-validation/INDEX.md +531 -0
  107. package/.claude/skills/cfn-loop-validation/README_TYPESCRIPT.md +454 -0
  108. package/.claude/skills/cfn-loop-validation/SKILL.md +48 -1
  109. package/.claude/skills/cfn-loop-validation/SKILL.md.backup +353 -0
  110. package/.claude/skills/cfn-loop-validation/SKILL_TYPESCRIPT.md +782 -0
  111. package/.claude/skills/cfn-loop-validation/VAPOR_DETECTION_EXAMPLES.md +598 -0
  112. package/.claude/skills/cfn-loop-validation/check-dependencies.sh +22 -0
  113. package/{claude-assets/skills/cfn-redis-coordination/check-dependencies.sh → .claude/skills/cfn-loop-validation/check-dependencies.sh.backup} +4 -5
  114. package/.claude/skills/cfn-loop-validation/detect-vapor.sh +59 -0
  115. package/.claude/skills/cfn-loop-validation/detect-vapor.sh.backup +37 -0
  116. package/.claude/skills/cfn-loop-validation/dist/.tsbuildinfo +1 -0
  117. package/.claude/skills/cfn-loop-validation/dist/cli/detect-vapor.d.ts +14 -0
  118. package/.claude/skills/cfn-loop-validation/dist/cli/detect-vapor.d.ts.map +1 -0
  119. package/.claude/skills/cfn-loop-validation/dist/cli/detect-vapor.js +185 -0
  120. package/.claude/skills/cfn-loop-validation/dist/cli/detect-vapor.js.map +1 -0
  121. package/.claude/skills/cfn-loop-validation/dist/cli/validate-deliverables.d.ts +14 -0
  122. package/.claude/skills/cfn-loop-validation/dist/cli/validate-deliverables.d.ts.map +1 -0
  123. package/.claude/skills/cfn-loop-validation/dist/cli/validate-deliverables.js +176 -0
  124. package/.claude/skills/cfn-loop-validation/dist/cli/validate-deliverables.js.map +1 -0
  125. package/.claude/skills/cfn-loop-validation/dist/cli/validate-gate.d.ts +19 -0
  126. package/.claude/skills/cfn-loop-validation/dist/cli/validate-gate.d.ts.map +1 -0
  127. package/.claude/skills/cfn-loop-validation/dist/cli/validate-gate.js +123 -0
  128. package/.claude/skills/cfn-loop-validation/dist/cli/validate-gate.js.map +1 -0
  129. package/.claude/skills/cfn-loop-validation/dist/types.d.ts +156 -0
  130. package/.claude/skills/cfn-loop-validation/dist/types.d.ts.map +1 -0
  131. package/.claude/skills/cfn-loop-validation/dist/types.js +66 -0
  132. package/.claude/skills/cfn-loop-validation/dist/types.js.map +1 -0
  133. package/.claude/skills/cfn-loop-validation/dist/validator.d.ts +85 -0
  134. package/.claude/skills/cfn-loop-validation/dist/validator.d.ts.map +1 -0
  135. package/.claude/skills/cfn-loop-validation/dist/validator.js +411 -0
  136. package/.claude/skills/cfn-loop-validation/dist/validator.js.map +1 -0
  137. package/.claude/skills/cfn-loop-validation/orchestrate-cfn-loop.sh +22 -0
  138. package/.claude/skills/cfn-loop-validation/orchestrate-cfn-loop.sh.backup +252 -0
  139. package/.claude/skills/cfn-loop-validation/package.json +93 -0
  140. package/.claude/skills/cfn-loop-validation/src/cli/detect-vapor.ts +177 -0
  141. package/.claude/skills/cfn-loop-validation/src/cli/validate-deliverables.ts +161 -0
  142. package/.claude/skills/cfn-loop-validation/src/cli/validate-gate.ts +139 -0
  143. package/.claude/skills/cfn-loop-validation/src/types.ts +215 -0
  144. package/.claude/skills/cfn-loop-validation/src/validator.ts +503 -0
  145. package/.claude/skills/cfn-loop-validation/tests/validator.test.ts +537 -0
  146. package/.claude/skills/{cfn-redis-coordination → cfn-loop-validation}/tsconfig.json +34 -31
  147. package/.claude/skills/cfn-loop-validation/validate-deliverables.sh +59 -0
  148. package/.claude/skills/cfn-loop-validation/validate-deliverables.sh.backup +37 -0
  149. package/.claude/skills/cfn-loop-validation/validate-gate.sh +63 -0
  150. package/.claude/skills/cfn-loop-validation/validate-gate.sh.backup +41 -0
  151. package/.claude/skills/cfn-loop-validation/validate-iteration.sh +22 -0
  152. package/.claude/skills/cfn-loop-validation/validate-iteration.sh.backup +134 -0
  153. package/.claude/skills/cfn-product-owner-decision/SKILL.md +479 -147
  154. package/.claude/skills/cfn-product-owner-decision/TYPESCRIPT_IMPLEMENTATION.md +653 -0
  155. package/.claude/skills/cfn-product-owner-decision/{execute-decision.sh → archive/legacy-bash/execute-decision.sh} +24 -2
  156. package/.claude/skills/pre-edit-backup/SKILL.md +324 -0
  157. package/.claude/skills/pre-edit-backup/SKILL.md.backup +277 -0
  158. package/.claude/skills/pre-edit-backup/backup.sh +22 -0
  159. package/.claude/skills/pre-edit-backup/backup.sh.backup +107 -0
  160. package/claude-assets/agents/cfn-dev-team/analysts/root-cause-analyst.md +2 -2
  161. package/claude-assets/agents/cfn-dev-team/architecture/base-template-generator.md +1 -1
  162. package/claude-assets/agents/cfn-dev-team/coordinators/cfn-frontend-coordinator.md +3 -2
  163. package/claude-assets/agents/cfn-dev-team/coordinators/consensus-builder.md +1 -0
  164. package/claude-assets/agents/cfn-dev-team/coordinators/handoff-coordinator.md +2 -1
  165. package/claude-assets/agents/cfn-dev-team/coordinators/multi-sprint-coordinator.md +1 -0
  166. package/claude-assets/agents/cfn-dev-team/dev-ops/devops-engineer.md +11 -1
  167. package/claude-assets/agents/cfn-dev-team/dev-ops/docker-specialist.md +58 -35
  168. package/claude-assets/agents/cfn-dev-team/dev-ops/github-commit-agent.md +2 -2
  169. package/claude-assets/agents/cfn-dev-team/dev-ops/kubernetes-specialist.md +47 -37
  170. package/claude-assets/agents/cfn-dev-team/developers/api-gateway-specialist.md +18 -18
  171. package/claude-assets/agents/cfn-dev-team/developers/backend-developer.md +40 -58
  172. package/claude-assets/agents/cfn-dev-team/developers/data/data-engineer.md +19 -21
  173. package/claude-assets/agents/cfn-dev-team/developers/database/database-architect.md +20 -29
  174. package/claude-assets/agents/cfn-dev-team/developers/frontend/mobile-dev.md +15 -19
  175. package/claude-assets/agents/cfn-dev-team/developers/frontend/react-frontend-engineer.md +15 -10
  176. package/claude-assets/agents/cfn-dev-team/developers/frontend/typescript-specialist.md +16 -11
  177. package/claude-assets/agents/cfn-dev-team/developers/frontend/ui-designer.md +16 -26
  178. package/claude-assets/agents/cfn-dev-team/developers/graphql-specialist.md +18 -22
  179. package/claude-assets/agents/cfn-dev-team/developers/rust-developer.md +17 -21
  180. package/claude-assets/agents/cfn-dev-team/documentation/pseudocode.md +1 -1
  181. package/claude-assets/agents/cfn-dev-team/product-owners/accessibility-advocate-persona.md +1 -1
  182. package/claude-assets/agents/cfn-dev-team/product-owners/cto-agent.md +1 -1
  183. package/claude-assets/agents/cfn-dev-team/product-owners/power-user-persona.md +1 -1
  184. package/claude-assets/agents/cfn-dev-team/product-owners/product-owner.md +1 -5
  185. package/claude-assets/agents/cfn-dev-team/reviewers/code-reviewer.md +20 -51
  186. package/claude-assets/agents/cfn-dev-team/reviewers/quality/code-quality-validator.md +22 -71
  187. package/claude-assets/agents/cfn-dev-team/reviewers/quality/perf-analyzer.md +21 -64
  188. package/claude-assets/agents/cfn-dev-team/reviewers/quality/performance-benchmarker.md +22 -67
  189. package/claude-assets/agents/cfn-dev-team/reviewers/quality/security-specialist.md +24 -68
  190. package/claude-assets/agents/cfn-dev-team/testers/api-testing-specialist.md +8 -36
  191. package/claude-assets/agents/cfn-dev-team/testers/chaos-engineering-specialist.md +9 -38
  192. package/claude-assets/agents/cfn-dev-team/testers/contract-tester.md +17 -55
  193. package/claude-assets/agents/cfn-dev-team/testers/e2e/playwright-tester.md +1 -1
  194. package/claude-assets/agents/cfn-dev-team/testers/integration-tester.md +18 -56
  195. package/claude-assets/agents/cfn-dev-team/testers/interaction-tester.md +9 -37
  196. package/claude-assets/agents/cfn-dev-team/testers/load-testing-specialist.md +18 -56
  197. package/claude-assets/agents/cfn-dev-team/testers/mutation-testing-specialist.md +18 -49
  198. package/claude-assets/agents/cfn-dev-team/testers/playwright-tester.md +8 -37
  199. package/claude-assets/agents/cfn-dev-team/testers/tester.md +7 -27
  200. package/claude-assets/agents/cfn-dev-team/testers/unit/tdd-london-unit-swarm.md +1 -1
  201. package/claude-assets/agents/cfn-dev-team/utility/agent-builder.md +11 -0
  202. package/claude-assets/agents/cfn-dev-team/utility/analyst.md +13 -29
  203. package/claude-assets/agents/cfn-dev-team/utility/claude-code-expert.md +1 -1
  204. package/claude-assets/agents/cfn-dev-team/utility/code-booster.md +13 -13
  205. package/claude-assets/agents/cfn-dev-team/utility/context-curator.md +7 -2
  206. package/claude-assets/agents/cfn-dev-team/utility/epic-creator.md +6 -11
  207. package/claude-assets/agents/cfn-dev-team/utility/memory-leak-specialist.md +121 -715
  208. package/claude-assets/agents/cfn-dev-team/utility/researcher.md +13 -22
  209. package/claude-assets/agents/cfn-dev-team/utility/z-ai-specialist.md +147 -573
  210. package/claude-assets/agents/custom/cfn-docker-expert.md +103 -0
  211. package/claude-assets/agents/custom/cfn-loops-cli-expert.md +438 -0
  212. package/claude-assets/agents/custom/cfn-redis-operations.md +529 -529
  213. package/claude-assets/agents/custom/cfn-system-expert.md +1 -1
  214. package/claude-assets/agents/custom/trigger-dev-expert.md +369 -0
  215. package/claude-assets/agents/docker-team/micro-sprint-planner.md +747 -747
  216. package/claude-assets/agents/project-only-agents/npm-package-specialist.md +1 -1
  217. package/claude-assets/cfn-extras/agents/cfn-v3-coordinator.md +517 -0
  218. package/claude-assets/cfn-extras/skills/GOOGLE_SHEETS_SKILLS_README.md +1 -1
  219. package/claude-assets/cfn-extras/skills/google-sheets-api-coordinator/SKILL.md +1 -1
  220. package/claude-assets/cfn-extras/skills/google-sheets-formula-builder/SKILL.md +1 -1
  221. package/claude-assets/cfn-extras/skills/google-sheets-progress/SKILL.md +1 -1
  222. package/claude-assets/commands/CFN_LOOP_FRONTEND.md +1 -1
  223. package/claude-assets/commands/cfn-loop-cli.md +214 -442
  224. package/claude-assets/commands/cfn-loop-frontend.md +1 -1
  225. package/claude-assets/commands/cfn-loop-task.md +2 -2
  226. package/claude-assets/commands/cfn-loop-trigger.md +114 -0
  227. package/claude-assets/commands/deprecated/cfn-loop.md +2 -2
  228. package/claude-assets/hooks/GIT-HOOKS-USAGE-EXAMPLES.md +116 -0
  229. package/claude-assets/hooks/README-GIT-HOOKS.md +443 -0
  230. package/claude-assets/hooks/SKILL.md +518 -0
  231. package/claude-assets/hooks/SKILL.md.backup +471 -0
  232. package/claude-assets/hooks/cfn-invoke-post-edit-ts.sh +100 -0
  233. package/claude-assets/hooks/cfn-invoke-post-edit-ts.sh.backup +78 -0
  234. package/claude-assets/hooks/cfn-invoke-post-edit.sh +53 -5
  235. package/claude-assets/hooks/cfn-invoke-post-edit.sh.backup +87 -0
  236. package/claude-assets/hooks/cfn-invoke-pre-edit-ts.sh +116 -0
  237. package/claude-assets/hooks/cfn-invoke-pre-edit-ts.sh.backup +94 -0
  238. package/claude-assets/hooks/cfn-invoke-pre-edit.sh +22 -0
  239. package/claude-assets/hooks/cfn-invoke-pre-edit.sh.backup +88 -0
  240. package/claude-assets/hooks/cfn-post-edit.config.json +9 -2
  241. package/claude-assets/hooks/install-git-hooks.sh +243 -0
  242. package/claude-assets/hooks/subagent-start.sh +98 -0
  243. package/claude-assets/hooks/subagent-stop.sh +93 -0
  244. package/claude-assets/hooks/validators/credential-scanner.sh +172 -0
  245. package/claude-assets/root-claude-distribute/CFN-CLAUDE.md +1 -1
  246. package/claude-assets/skills/cfn-agent-selection-with-fallback/DELIVERABLES.md +409 -0
  247. package/claude-assets/skills/cfn-agent-selection-with-fallback/IMPLEMENTATION_SUMMARY.md +396 -0
  248. package/claude-assets/skills/cfn-agent-selection-with-fallback/INTEGRATION_GUIDE.md +308 -0
  249. package/claude-assets/skills/cfn-agent-selection-with-fallback/QUICK_REFERENCE.md +239 -0
  250. package/claude-assets/skills/cfn-agent-selection-with-fallback/SKILL.md +107 -1
  251. package/claude-assets/skills/cfn-agent-selection-with-fallback/SKILL.md.backup +302 -0
  252. package/claude-assets/skills/cfn-agent-selection-with-fallback/TYPESCRIPT_MIGRATION.md +295 -0
  253. package/claude-assets/skills/cfn-agent-selection-with-fallback/dist/agent-selector.cjs +297 -0
  254. package/claude-assets/skills/cfn-agent-selection-with-fallback/dist/agent-selector.js +297 -0
  255. package/claude-assets/skills/cfn-agent-selection-with-fallback/dist/cli.cjs +96 -0
  256. package/claude-assets/skills/cfn-agent-selection-with-fallback/dist/cli.js +96 -0
  257. package/claude-assets/skills/cfn-agent-selection-with-fallback/select-agents-ts.sh +45 -0
  258. package/claude-assets/skills/cfn-agent-selection-with-fallback/select-agents-ts.sh.backup +23 -0
  259. package/claude-assets/skills/cfn-agent-selection-with-fallback/select-agents.sh +22 -0
  260. package/claude-assets/skills/cfn-agent-selection-with-fallback/select-agents.sh.backup +173 -0
  261. package/claude-assets/skills/cfn-agent-selection-with-fallback/src/agent-selector.test.ts +357 -0
  262. package/claude-assets/skills/cfn-agent-selection-with-fallback/src/agent-selector.ts +350 -0
  263. package/claude-assets/skills/cfn-agent-selection-with-fallback/src/cli.ts +74 -0
  264. package/claude-assets/skills/cfn-agent-selection-with-fallback/task-classifier.sh +22 -0
  265. package/claude-assets/skills/cfn-agent-selection-with-fallback/task-classifier.sh.backup +71 -0
  266. package/claude-assets/skills/cfn-agent-selection-with-fallback/tsconfig.json +18 -0
  267. package/claude-assets/skills/cfn-agent-spawning/SKILL.md +48 -1
  268. package/claude-assets/skills/cfn-agent-spawning/SKILL.md.backup +135 -0
  269. package/claude-assets/skills/cfn-agent-spawning/TYPESCRIPT_MIGRATION.md +567 -0
  270. package/claude-assets/skills/cfn-agent-spawning/check-dependencies.sh +22 -0
  271. package/claude-assets/skills/cfn-agent-spawning/check-dependencies.sh.backup +30 -0
  272. package/claude-assets/skills/cfn-agent-spawning/get-agent-provider-env.sh +22 -0
  273. package/claude-assets/skills/cfn-agent-spawning/get-agent-provider-env.sh.backup +127 -0
  274. package/claude-assets/skills/cfn-agent-spawning/parse-agent-provider.sh +22 -0
  275. package/claude-assets/skills/cfn-agent-spawning/parse-agent-provider.sh.backup +59 -0
  276. package/claude-assets/skills/cfn-agent-spawning/spawn-agent-wrapper.sh +63 -0
  277. package/claude-assets/skills/cfn-agent-spawning/spawn-agent-wrapper.sh.backup +41 -0
  278. package/claude-assets/skills/cfn-agent-spawning/spawn-agent.sh +26 -1
  279. package/claude-assets/skills/cfn-agent-spawning/spawn-templates.sh +22 -0
  280. package/claude-assets/skills/cfn-agent-spawning/spawn-templates.sh.backup +613 -0
  281. package/claude-assets/skills/cfn-agent-spawning/spawn-worker.sh +22 -0
  282. package/claude-assets/skills/cfn-agent-spawning/spawn-worker.sh.backup +176 -0
  283. package/claude-assets/skills/cfn-backlog-management/SKILL.md +1 -1
  284. package/claude-assets/skills/cfn-coordination/agent-completion.sh.backup +36 -0
  285. package/claude-assets/skills/cfn-coordination/coordination-signal.sh.backup +36 -0
  286. package/claude-assets/skills/cfn-coordination/coordination-wait.sh.backup +36 -0
  287. package/claude-assets/skills/cfn-dependency-ingestion/README.md +101 -0
  288. package/claude-assets/skills/cfn-dependency-ingestion/SKILL.md +397 -0
  289. package/claude-assets/skills/cfn-dependency-ingestion/build.sh +23 -0
  290. package/claude-assets/skills/cfn-dependency-ingestion/dist/ingest-dependencies.js +478 -0
  291. package/claude-assets/skills/cfn-dependency-ingestion/ingest-dependencies.sh +295 -0
  292. package/claude-assets/skills/cfn-dependency-ingestion/ingest.sh +237 -0
  293. package/claude-assets/skills/cfn-dependency-ingestion/manifests/cli-mode-dependencies.txt +73 -0
  294. package/claude-assets/skills/cfn-dependency-ingestion/manifests/shared-dependencies.txt +57 -0
  295. package/claude-assets/skills/cfn-dependency-ingestion/manifests/trigger-dev-dependencies.txt +82 -0
  296. package/claude-assets/skills/cfn-dependency-ingestion/manifests/trigger-mode-dependencies.txt +80 -0
  297. package/claude-assets/skills/cfn-dependency-ingestion/src/ingest-dependencies.ts +563 -0
  298. package/claude-assets/skills/cfn-environment-sanitization/sanitize-environment.sh +14 -4
  299. package/claude-assets/skills/cfn-loop-orchestration/.backups/unknown/1763619700_33aff4a69b99159e4e849107ebc4d09f/metadata.json +8 -0
  300. package/claude-assets/skills/cfn-loop-orchestration/.backups/unknown/1763619700_33aff4a69b99159e4e849107ebc4d09f/original +271 -0
  301. package/claude-assets/skills/cfn-loop-orchestration/.backups/unknown/1763619700_33aff4a69b99159e4e849107ebc4d09f/revert.sh +7 -0
  302. package/claude-assets/skills/cfn-loop-orchestration/.backups/unknown/1763671642_06496e8c399a79db08167cc00ed4b31e/metadata.json +8 -0
  303. package/claude-assets/skills/cfn-loop-orchestration/.backups/unknown/1763671642_06496e8c399a79db08167cc00ed4b31e/original +325 -0
  304. package/claude-assets/skills/cfn-loop-orchestration/.backups/unknown/1763671642_06496e8c399a79db08167cc00ed4b31e/revert.sh +7 -0
  305. package/claude-assets/skills/cfn-loop-orchestration/CLI_IMPLEMENTATION_SUMMARY.md +330 -0
  306. package/claude-assets/skills/cfn-loop-orchestration/CONFIGURATION_IMPROVEMENTS.md +318 -0
  307. package/claude-assets/skills/cfn-loop-orchestration/CONTEXT_LOOKUP_MIGRATION.md +308 -0
  308. package/claude-assets/skills/cfn-loop-orchestration/CONTEXT_LOOKUP_QUICK_START.md +378 -0
  309. package/claude-assets/skills/cfn-loop-orchestration/E2E_VALIDATION_REPORT.md +262 -0
  310. package/claude-assets/skills/cfn-loop-orchestration/IMPLEMENTATION_SUMMARY.md +319 -519
  311. package/claude-assets/skills/cfn-loop-orchestration/NORTH_STAR_E2E_REPORT.md +299 -0
  312. package/claude-assets/skills/cfn-loop-orchestration/NORTH_STAR_EXECUTION_SUMMARY.md +403 -0
  313. package/claude-assets/skills/cfn-loop-orchestration/NORTH_STAR_INDEX.md +323 -0
  314. package/claude-assets/skills/cfn-loop-orchestration/SKILL.md +159 -48
  315. package/claude-assets/skills/cfn-loop-orchestration/SPAWN_AGENTS_IMPLEMENTATION.md +188 -0
  316. package/claude-assets/skills/cfn-loop-orchestration/TEST_COVERAGE_REPORT.md +335 -0
  317. package/claude-assets/skills/cfn-loop-orchestration/TEST_COVERAGE_SUMMARY.md +456 -0
  318. package/claude-assets/skills/cfn-loop-orchestration/TYPESCRIPT_INTEGRATION_REPORT.md +709 -0
  319. package/claude-assets/skills/cfn-loop-orchestration/TYPESCRIPT_INTEGRATION_SUMMARY.md +257 -0
  320. package/claude-assets/skills/cfn-loop-orchestration/VALIDATION_REPORT.md +572 -0
  321. package/claude-assets/skills/cfn-loop-orchestration/VALIDATION_SUMMARY.txt +196 -0
  322. package/claude-assets/skills/cfn-loop-orchestration/VALIDATOR_MODULE_GUIDE.md +526 -0
  323. package/claude-assets/skills/cfn-loop-orchestration/archive/legacy-bash/README.md +167 -0
  324. package/claude-assets/skills/cfn-loop-orchestration/archive/legacy-bash/orchestrate-enhanced.sh +548 -0
  325. package/claude-assets/skills/cfn-loop-orchestration/{orchestrate-wrapper.sh → archive/legacy-bash/orchestrate-wrapper.sh} +11 -1
  326. package/claude-assets/skills/cfn-loop-orchestration/archive/legacy-bash/orchestrate.sh +182 -0
  327. package/claude-assets/skills/cfn-loop-orchestration/e2e-validation-fixed.js +240 -0
  328. package/claude-assets/skills/cfn-loop-orchestration/e2e-validation.js +213 -0
  329. package/claude-assets/skills/cfn-loop-orchestration/package-lock.json +3 -0
  330. package/claude-assets/skills/cfn-loop-orchestration/package.json +4 -0
  331. package/claude-assets/skills/cfn-loop-orchestration/run-north-star-e2e.ts +210 -0
  332. package/claude-assets/skills/cfn-loop-orchestration/src/cli/orchestrator-cli.ts +396 -0
  333. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/CONFIDENCE_AGGREGATOR.md +564 -0
  334. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/CONFIDENCE_AGGREGATOR_QUICK_REF.md +241 -0
  335. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_IMPLEMENTATION.md +375 -0
  336. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_QUICK_REFERENCE.md +362 -0
  337. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_README.md +307 -0
  338. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/CONTEXT_INJECTOR_USAGE_GUIDE.md +508 -0
  339. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/confidence-aggregator.ts +473 -0
  340. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/consensus.ts +1 -1
  341. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/context-injector.ts +349 -0
  342. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/context-lookup.ts +486 -0
  343. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/deliverable-verifier.ts +6 -2
  344. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/gate-check.ts +1 -1
  345. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/product-owner-decision.ts +316 -0
  346. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/spawn-agents.ts +357 -0
  347. package/claude-assets/skills/cfn-loop-orchestration/src/helpers/validator.ts +276 -0
  348. package/claude-assets/skills/cfn-loop-orchestration/src/index.ts +2 -0
  349. package/claude-assets/skills/cfn-loop-orchestration/src/orchestrate.ts +743 -2
  350. package/claude-assets/skills/cfn-loop-orchestration/src/types.ts +56 -0
  351. package/claude-assets/skills/cfn-loop-orchestration/test-cli.sh +92 -0
  352. package/claude-assets/skills/cfn-loop-orchestration/test-typescript-integration.sh +442 -0
  353. package/claude-assets/skills/cfn-loop-orchestration/tests/agent-spawner.test.ts +124 -0
  354. package/claude-assets/skills/cfn-loop-orchestration/tests/confidence-aggregator.test.ts +604 -0
  355. package/claude-assets/skills/cfn-loop-orchestration/tests/context-injector.test.ts +561 -0
  356. package/claude-assets/skills/cfn-loop-orchestration/tests/context-lookup.test.ts +661 -0
  357. package/claude-assets/skills/cfn-loop-orchestration/tests/deliverable-verifier.test.ts +2 -2
  358. package/claude-assets/skills/cfn-loop-orchestration/tests/gate-check-edge-cases.test.ts +422 -0
  359. package/claude-assets/skills/cfn-loop-orchestration/tests/gate-checker.test.ts +276 -0
  360. package/claude-assets/skills/cfn-loop-orchestration/tests/logger.test.ts +291 -0
  361. package/claude-assets/skills/cfn-loop-orchestration/tests/north-star-e2e.test.ts +334 -0
  362. package/claude-assets/skills/cfn-loop-orchestration/tests/redis-coordinator.test.ts +321 -0
  363. package/claude-assets/skills/cfn-loop-orchestration/tests/spawn-agents.test.ts +284 -0
  364. package/claude-assets/skills/cfn-loop-orchestration/tests/validator.test.ts +643 -0
  365. package/claude-assets/skills/cfn-loop-output-processing/.eslintrc.json +33 -0
  366. package/claude-assets/skills/cfn-loop-output-processing/DELIVERY_SUMMARY.txt +462 -0
  367. package/claude-assets/skills/cfn-loop-output-processing/DEPRECATION_NOTICE.md +183 -0
  368. package/claude-assets/skills/cfn-loop-output-processing/EXAMPLES.md +609 -0
  369. package/claude-assets/skills/cfn-loop-output-processing/IMPLEMENTATION_SUMMARY.md +418 -0
  370. package/claude-assets/skills/cfn-loop-output-processing/INDEX.md +531 -0
  371. package/claude-assets/skills/cfn-loop-output-processing/MIGRATION.md +362 -0
  372. package/claude-assets/skills/cfn-loop-output-processing/README.md +114 -0
  373. package/claude-assets/skills/cfn-loop-output-processing/SKILL.md +633 -0
  374. package/{.claude/skills/cfn-docker-redis-coordination → claude-assets/skills/cfn-loop-output-processing}/jest.config.js +7 -15
  375. package/claude-assets/skills/cfn-loop-output-processing/package.json +50 -0
  376. package/claude-assets/skills/cfn-loop-output-processing/src/cli/process-loop2.ts +195 -0
  377. package/claude-assets/skills/cfn-loop-output-processing/src/cli/process-loop3.ts +157 -0
  378. package/claude-assets/skills/cfn-loop-output-processing/src/output-processor.ts +632 -0
  379. package/claude-assets/skills/cfn-loop-output-processing/tests/output-processor.test.ts +617 -0
  380. package/claude-assets/skills/{cfn-docker-redis-coordination → cfn-loop-output-processing}/tsconfig.json +16 -7
  381. package/claude-assets/skills/cfn-loop-validation/IMPLEMENTATION_SUMMARY.md +672 -0
  382. package/claude-assets/skills/cfn-loop-validation/INDEX.md +531 -0
  383. package/claude-assets/skills/cfn-loop-validation/README_TYPESCRIPT.md +454 -0
  384. package/claude-assets/skills/cfn-loop-validation/SKILL.md +48 -1
  385. package/claude-assets/skills/cfn-loop-validation/SKILL.md.backup +353 -0
  386. package/claude-assets/skills/cfn-loop-validation/SKILL_TYPESCRIPT.md +782 -0
  387. package/claude-assets/skills/cfn-loop-validation/VAPOR_DETECTION_EXAMPLES.md +598 -0
  388. package/claude-assets/skills/cfn-loop-validation/check-dependencies.sh +22 -0
  389. package/claude-assets/skills/cfn-loop-validation/check-dependencies.sh.backup +31 -0
  390. package/claude-assets/skills/cfn-loop-validation/detect-vapor.sh +59 -0
  391. package/claude-assets/skills/cfn-loop-validation/detect-vapor.sh.backup +37 -0
  392. package/claude-assets/skills/cfn-loop-validation/dist/.tsbuildinfo +1 -0
  393. package/claude-assets/skills/cfn-loop-validation/dist/cli/detect-vapor.d.ts +14 -0
  394. package/claude-assets/skills/cfn-loop-validation/dist/cli/detect-vapor.d.ts.map +1 -0
  395. package/claude-assets/skills/cfn-loop-validation/dist/cli/detect-vapor.js +185 -0
  396. package/claude-assets/skills/cfn-loop-validation/dist/cli/detect-vapor.js.map +1 -0
  397. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-deliverables.d.ts +14 -0
  398. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-deliverables.d.ts.map +1 -0
  399. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-deliverables.js +176 -0
  400. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-deliverables.js.map +1 -0
  401. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-gate.d.ts +19 -0
  402. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-gate.d.ts.map +1 -0
  403. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-gate.js +123 -0
  404. package/claude-assets/skills/cfn-loop-validation/dist/cli/validate-gate.js.map +1 -0
  405. package/claude-assets/skills/cfn-loop-validation/dist/types.d.ts +156 -0
  406. package/claude-assets/skills/cfn-loop-validation/dist/types.d.ts.map +1 -0
  407. package/claude-assets/skills/cfn-loop-validation/dist/types.js +66 -0
  408. package/claude-assets/skills/cfn-loop-validation/dist/types.js.map +1 -0
  409. package/claude-assets/skills/cfn-loop-validation/dist/validator.d.ts +85 -0
  410. package/claude-assets/skills/cfn-loop-validation/dist/validator.d.ts.map +1 -0
  411. package/claude-assets/skills/cfn-loop-validation/dist/validator.js +411 -0
  412. package/claude-assets/skills/cfn-loop-validation/dist/validator.js.map +1 -0
  413. package/claude-assets/skills/cfn-loop-validation/orchestrate-cfn-loop.sh +22 -0
  414. package/claude-assets/skills/cfn-loop-validation/orchestrate-cfn-loop.sh.backup +252 -0
  415. package/claude-assets/skills/cfn-loop-validation/package.json +93 -0
  416. package/claude-assets/skills/cfn-loop-validation/src/cli/detect-vapor.ts +177 -0
  417. package/claude-assets/skills/cfn-loop-validation/src/cli/validate-deliverables.ts +161 -0
  418. package/claude-assets/skills/cfn-loop-validation/src/cli/validate-gate.ts +139 -0
  419. package/claude-assets/skills/cfn-loop-validation/src/types.ts +215 -0
  420. package/claude-assets/skills/cfn-loop-validation/src/validator.ts +503 -0
  421. package/claude-assets/skills/cfn-loop-validation/tests/validator.test.ts +537 -0
  422. package/claude-assets/skills/{cfn-redis-coordination → cfn-loop-validation}/tsconfig.json +34 -31
  423. package/claude-assets/skills/cfn-loop-validation/validate-deliverables.sh +59 -0
  424. package/claude-assets/skills/cfn-loop-validation/validate-deliverables.sh.backup +37 -0
  425. package/claude-assets/skills/cfn-loop-validation/validate-gate.sh +63 -0
  426. package/claude-assets/skills/cfn-loop-validation/validate-gate.sh.backup +41 -0
  427. package/claude-assets/skills/cfn-loop-validation/validate-iteration.sh +22 -0
  428. package/claude-assets/skills/cfn-loop-validation/validate-iteration.sh.backup +134 -0
  429. package/claude-assets/skills/cfn-product-owner-decision/SKILL.md +479 -147
  430. package/claude-assets/skills/cfn-product-owner-decision/TYPESCRIPT_IMPLEMENTATION.md +653 -0
  431. package/claude-assets/skills/cfn-product-owner-decision/{execute-decision.sh → archive/legacy-bash/execute-decision.sh} +24 -2
  432. package/claude-assets/skills/cfn-provider-routing/README.md +129 -0
  433. package/claude-assets/skills/cfn-provider-routing/SKILL.md +215 -0
  434. package/claude-assets/skills/cfn-provider-routing/resolve-provider-model.ts +223 -0
  435. package/claude-assets/skills/docker-build/build.sh +1 -1
  436. package/claude-assets/skills/pre-edit-backup/SKILL.md +324 -0
  437. package/claude-assets/skills/pre-edit-backup/SKILL.md.backup +277 -0
  438. package/claude-assets/skills/pre-edit-backup/backup.sh +22 -0
  439. package/claude-assets/skills/pre-edit-backup/backup.sh.backup +107 -0
  440. package/dist/agent/skill-mcp-selector.js +2 -1
  441. package/dist/agent/skill-mcp-selector.js.map +1 -1
  442. package/dist/api/auth-endpoints.js +415 -0
  443. package/dist/api/auth-endpoints.js.map +1 -0
  444. package/dist/api/task-endpoints.js +562 -0
  445. package/dist/api/task-endpoints.js.map +1 -0
  446. package/dist/backend/server.js +418 -0
  447. package/dist/backend/server.js.map +1 -0
  448. package/dist/cfn-loop/product-owner/decision-parser.js +356 -0
  449. package/dist/cfn-loop/product-owner/decision-parser.js.map +1 -0
  450. package/dist/cfn-loop/product-owner/index.js +1 -0
  451. package/dist/cfn-loop/product-owner/index.js.map +1 -1
  452. package/dist/cli/agent-command.js +1 -1
  453. package/dist/cli/agent-command.js.map +1 -1
  454. package/dist/cli/agent-completion.js +273 -0
  455. package/dist/cli/agent-completion.js.map +1 -0
  456. package/dist/cli/agent-executor.js +470 -26
  457. package/dist/cli/agent-executor.js.map +1 -1
  458. package/dist/cli/agent-prompt-builder.js +83 -48
  459. package/dist/cli/agent-prompt-builder.js.map +1 -1
  460. package/dist/cli/agent-spawn.js +7 -4
  461. package/dist/cli/agent-spawn.js.map +1 -1
  462. package/dist/cli/agent-spawner.js +546 -0
  463. package/dist/cli/agent-spawner.js.map +1 -0
  464. package/dist/cli/agent-token-manager.js +2 -1
  465. package/dist/cli/agent-token-manager.js.map +1 -1
  466. package/dist/cli/anthropic-client.js +127 -14
  467. package/dist/cli/anthropic-client.js.map +1 -1
  468. package/dist/cli/cfn-context.js +2 -1
  469. package/dist/cli/cfn-context.js.map +1 -1
  470. package/dist/cli/cfn-metrics.js +2 -1
  471. package/dist/cli/cfn-metrics.js.map +1 -1
  472. package/dist/cli/cfn-redis.js +2 -1
  473. package/dist/cli/cfn-redis.js.map +1 -1
  474. package/dist/cli/cli-agent-context.js +2 -0
  475. package/dist/cli/cli-agent-context.js.map +1 -1
  476. package/dist/cli/config-manager.js +90 -356
  477. package/dist/cli/config-manager.js.map +1 -1
  478. package/dist/cli/conversation-fork-cleanup.js +2 -1
  479. package/dist/cli/conversation-fork-cleanup.js.map +1 -1
  480. package/dist/cli/conversation-fork.js +2 -1
  481. package/dist/cli/conversation-fork.js.map +1 -1
  482. package/dist/cli/coordination/agent-messaging.js +415 -0
  483. package/dist/cli/coordination/agent-messaging.js.map +1 -0
  484. package/dist/cli/coordination/wait-for-threshold.js +232 -0
  485. package/dist/cli/coordination/wait-for-threshold.js.map +1 -0
  486. package/dist/cli/index.js +11 -0
  487. package/dist/cli/index.js.map +1 -1
  488. package/dist/cli/iteration-history.js +2 -1
  489. package/dist/cli/iteration-history.js.map +1 -1
  490. package/dist/cli/parse-decision-cli.js +268 -0
  491. package/dist/cli/parse-decision-cli.js.map +1 -0
  492. package/dist/cli/post-edit-hook.js +83 -0
  493. package/dist/cli/post-edit-hook.js.map +1 -0
  494. package/dist/cli/pre-edit-hook.js +77 -0
  495. package/dist/cli/pre-edit-hook.js.map +1 -0
  496. package/dist/cli/process-lifecycle.js +5 -1
  497. package/dist/cli/process-lifecycle.js.map +1 -1
  498. package/dist/cli/spawn-agent-cli.js +244 -0
  499. package/dist/cli/spawn-agent-cli.js.map +1 -0
  500. package/dist/coordination/coordination-wrapper.js +383 -0
  501. package/dist/coordination/coordination-wrapper.js.map +1 -0
  502. package/dist/coordination/redis-waiting-mode.js +4 -0
  503. package/dist/coordination/redis-waiting-mode.js.map +1 -1
  504. package/dist/coordination/store-success-criteria.js +68 -0
  505. package/dist/coordination/store-success-criteria.js.map +1 -0
  506. package/dist/coordination/store-task-context.js +65 -0
  507. package/dist/coordination/store-task-context.js.map +1 -0
  508. package/dist/hooks/backup-manager.js +273 -0
  509. package/dist/hooks/backup-manager.js.map +1 -0
  510. package/dist/hooks/post-edit-validator.js +388 -0
  511. package/dist/hooks/post-edit-validator.js.map +1 -0
  512. package/dist/integration/index.js +19 -0
  513. package/dist/integration/index.js.map +1 -0
  514. package/dist/integration/task-mode-adapter.js +297 -0
  515. package/dist/integration/task-mode-adapter.js.map +1 -0
  516. package/dist/integration/trigger-dev-client.js +253 -0
  517. package/dist/integration/trigger-dev-client.js.map +1 -0
  518. package/dist/integration/trigger-dev-webhooks.js +362 -0
  519. package/dist/integration/trigger-dev-webhooks.js.map +1 -0
  520. package/dist/lib/artifact-registry.js +4 -0
  521. package/dist/lib/artifact-registry.js.map +1 -1
  522. package/dist/lib/connection-pool.js +390 -0
  523. package/dist/lib/connection-pool.js.map +1 -0
  524. package/dist/lib/environment-contract.js +258 -0
  525. package/dist/lib/environment-contract.js.map +1 -0
  526. package/dist/lib/path-validator.js +14 -5
  527. package/dist/lib/path-validator.js.map +1 -1
  528. package/dist/lib/query-optimizer.js +388 -0
  529. package/dist/lib/query-optimizer.js.map +1 -0
  530. package/dist/lib/redis-queue-manager.js +5 -1
  531. package/dist/lib/redis-queue-manager.js.map +1 -1
  532. package/dist/lib/result-cache.js +285 -0
  533. package/dist/lib/result-cache.js.map +1 -0
  534. package/dist/mcp/auth-middleware.js +2 -1
  535. package/dist/mcp/auth-middleware.js.map +1 -1
  536. package/dist/mcp/playwright-mcp-server-auth.js +2 -1
  537. package/dist/mcp/playwright-mcp-server-auth.js.map +1 -1
  538. package/dist/middleware/authentication.js +317 -0
  539. package/dist/middleware/authentication.js.map +1 -0
  540. package/dist/services/authentication.js +669 -0
  541. package/dist/services/authentication.js.map +1 -0
  542. package/dist/services/session-management.js +436 -0
  543. package/dist/services/session-management.js.map +1 -0
  544. package/dist/services/skill-deployment.js +8 -6
  545. package/dist/services/skill-deployment.js.map +1 -1
  546. package/dist/services/user-service.js +710 -0
  547. package/dist/services/user-service.js.map +1 -0
  548. package/dist/types/trigger-dev-events.d.js +10 -0
  549. package/dist/types/trigger-dev-events.d.js.map +1 -0
  550. package/docs/README.md +240 -0
  551. package/package.json +15 -4
  552. package/scripts/build-agent-image.sh +1 -1
  553. package/scripts/compare-workflow-performance.sh +556 -0
  554. package/scripts/cost-allocation-tracker.sh +632 -0
  555. package/scripts/docker-rebuild-all-agents.sh +2 -2
  556. package/scripts/migrate-to-optimized-workflows.sh +438 -0
  557. package/scripts/organize-docs.sh +338 -0
  558. package/scripts/reorganize-tests.sh +280 -0
  559. package/scripts/trigger-dev-setup.sh +279 -0
  560. package/tests/README.md +45 -0
  561. package/.claude/commands/cost-savings-status.md +0 -34
  562. package/.claude/commands/metrics-summary.md +0 -58
  563. package/.claude/skills/cfn-docker-redis-coordination/MIGRATION_SUMMARY.md +0 -348
  564. package/.claude/skills/cfn-docker-redis-coordination/README.md +0 -294
  565. package/.claude/skills/cfn-docker-redis-coordination/SKILL.md +0 -435
  566. package/.claude/skills/cfn-docker-redis-coordination/coordinate.sh +0 -650
  567. package/.claude/skills/cfn-docker-redis-coordination/coordinate.sh.backup-1763145142 +0 -641
  568. package/.claude/skills/cfn-docker-redis-coordination/package-lock.json +0 -5259
  569. package/.claude/skills/cfn-docker-redis-coordination/package.json +0 -40
  570. package/.claude/skills/cfn-docker-redis-coordination/src/coordinator.ts +0 -801
  571. package/.claude/skills/cfn-docker-redis-coordination/src/index.ts +0 -42
  572. package/.claude/skills/cfn-docker-redis-coordination/src/types.ts +0 -351
  573. package/.claude/skills/cfn-docker-redis-coordination/tests/coordinator.test.ts +0 -1464
  574. package/.claude/skills/cfn-docker-redis-coordination/tsconfig.json +0 -30
  575. package/.claude/skills/cfn-loop-orchestration/helpers/auto-tune-timeouts.sh +0 -228
  576. package/.claude/skills/cfn-loop-orchestration/helpers/consensus-ts.sh +0 -104
  577. package/.claude/skills/cfn-loop-orchestration/helpers/consensus.sh +0 -94
  578. package/.claude/skills/cfn-loop-orchestration/helpers/context-injection.sh +0 -142
  579. package/.claude/skills/cfn-loop-orchestration/helpers/context-lookup.sh +0 -359
  580. package/.claude/skills/cfn-loop-orchestration/helpers/deliverable-verifier-ts.sh +0 -123
  581. package/.claude/skills/cfn-loop-orchestration/helpers/deliverable-verifier.sh +0 -71
  582. package/.claude/skills/cfn-loop-orchestration/helpers/gate-check.sh +0 -56
  583. package/.claude/skills/cfn-loop-orchestration/helpers/iteration-manager-ts.sh +0 -89
  584. package/.claude/skills/cfn-loop-orchestration/helpers/iteration-manager.sh +0 -87
  585. package/.claude/skills/cfn-loop-orchestration/helpers/orchestrate-ts.sh +0 -104
  586. package/.claude/skills/cfn-loop-orchestration/helpers/parse-test-results.sh +0 -56
  587. package/.claude/skills/cfn-loop-orchestration/helpers/spawn-agents.sh +0 -290
  588. package/.claude/skills/cfn-loop-orchestration/helpers/timeout-calculator-ts.sh +0 -47
  589. package/.claude/skills/cfn-loop-orchestration/helpers/timeout-calculator.sh +0 -51
  590. package/.claude/skills/cfn-loop-orchestration/orchestrate.sh +0 -1345
  591. package/.claude/skills/cfn-redis-coordination/AGENT_LOGGING.md +0 -280
  592. package/.claude/skills/cfn-redis-coordination/BZPOPMIN_FIX_SUMMARY.md +0 -209
  593. package/.claude/skills/cfn-redis-coordination/CENTRALIZED_REDIS_WRAPPER.md +0 -319
  594. package/.claude/skills/cfn-redis-coordination/agent-log.sh.bak +0 -124
  595. package/.claude/skills/cfn-redis-coordination/config.json +0 -61
  596. package/.claude/skills/cfn-redis-coordination/demos/phase4-wake-queue-test-report.md +0 -82
  597. package/.claude/skills/cfn-redis-coordination/demos/test-bzpopmin-fix.sh +0 -274
  598. package/.claude/skills/cfn-redis-coordination/demos/test-cancel-swarm.sh +0 -0
  599. package/.claude/skills/cfn-redis-coordination/docs/migration/PHASE_3_REDIS_COORDINATION_COMPLETION_REPORT.md +0 -553
  600. package/.claude/skills/cfn-redis-coordination/jest.config.js +0 -23
  601. package/.claude/skills/cfn-redis-coordination/package-lock.json +0 -5272
  602. package/.claude/skills/cfn-redis-coordination/package.json +0 -45
  603. package/.claude/skills/cfn-redis-coordination/src/agent-logger.ts +0 -446
  604. package/.claude/skills/cfn-redis-coordination/src/agent-recovery.ts +0 -454
  605. package/.claude/skills/cfn-redis-coordination/src/completion-reporter.ts +0 -396
  606. package/.claude/skills/cfn-redis-coordination/src/context-manager.ts +0 -327
  607. package/.claude/skills/cfn-redis-coordination/src/index.ts +0 -82
  608. package/.claude/skills/cfn-redis-coordination/src/mode-detector.ts +0 -155
  609. package/.claude/skills/cfn-redis-coordination/src/redis/redis-client.ts +0 -305
  610. package/.claude/skills/cfn-redis-coordination/src/redis/redis-functions.ts +0 -283
  611. package/.claude/skills/cfn-redis-coordination/src/redis-client.ts +0 -654
  612. package/.claude/skills/cfn-redis-coordination/src/result-collector.ts +0 -437
  613. package/.claude/skills/cfn-redis-coordination/src/swarm-manager.ts +0 -494
  614. package/.claude/skills/cfn-redis-coordination/src/task-analyzer.ts +0 -404
  615. package/.claude/skills/cfn-redis-coordination/src/task-executor.ts +0 -423
  616. package/.claude/skills/cfn-redis-coordination/src/types.ts +0 -235
  617. package/.claude/skills/cfn-redis-coordination/src/waiting-coordinator.ts +0 -587
  618. package/.claude/skills/cfn-redis-coordination/store-success-criteria.sh +0 -85
  619. package/.claude/skills/cfn-redis-coordination/test-connection-attempts.js +0 -70
  620. package/.claude/skills/cfn-redis-coordination/test-mode-simple.js +0 -121
  621. package/.claude/skills/cfn-redis-coordination/test-redis-check.js +0 -84
  622. package/.claude/skills/cfn-redis-coordination/test-task-mode-redis.cjs +0 -391
  623. package/.claude/skills/cfn-redis-coordination/tests/coordination.test.ts +0 -788
  624. package/.claude/skills/cfn-redis-coordination/update-all-scripts.sh +0 -67
  625. package/claude-assets/agents/cfn-dev-team/coordinators/cfn-v3-coordinator.md +0 -980
  626. package/claude-assets/agents/cfn-dev-team/dev-ops/monitoring-specialist.md +0 -759
  627. package/claude-assets/agents/custom/test-mcp-access.md +0 -24
  628. package/claude-assets/agents/typescript-specialist.md +0 -280
  629. package/claude-assets/commands/cost-savings-status.md +0 -34
  630. package/claude-assets/commands/metrics-summary.md +0 -58
  631. package/claude-assets/skills/cfn-docker-redis-coordination/MIGRATION_SUMMARY.md +0 -348
  632. package/claude-assets/skills/cfn-docker-redis-coordination/README.md +0 -294
  633. package/claude-assets/skills/cfn-docker-redis-coordination/SKILL.md +0 -435
  634. package/claude-assets/skills/cfn-docker-redis-coordination/coordinate.sh +0 -650
  635. package/claude-assets/skills/cfn-docker-redis-coordination/coordinate.sh.backup-1763145142 +0 -641
  636. package/claude-assets/skills/cfn-docker-redis-coordination/jest.config.js +0 -37
  637. package/claude-assets/skills/cfn-docker-redis-coordination/package-lock.json +0 -5259
  638. package/claude-assets/skills/cfn-docker-redis-coordination/package.json +0 -40
  639. package/claude-assets/skills/cfn-docker-redis-coordination/src/coordinator.ts +0 -801
  640. package/claude-assets/skills/cfn-docker-redis-coordination/src/index.ts +0 -42
  641. package/claude-assets/skills/cfn-docker-redis-coordination/src/types.ts +0 -351
  642. package/claude-assets/skills/cfn-docker-redis-coordination/tests/coordinator.test.ts +0 -1464
  643. package/claude-assets/skills/cfn-loop-orchestration/helpers/auto-tune-timeouts.sh +0 -228
  644. package/claude-assets/skills/cfn-loop-orchestration/helpers/consensus-ts.sh +0 -104
  645. package/claude-assets/skills/cfn-loop-orchestration/helpers/consensus.sh +0 -94
  646. package/claude-assets/skills/cfn-loop-orchestration/helpers/context-injection.sh +0 -142
  647. package/claude-assets/skills/cfn-loop-orchestration/helpers/context-lookup.sh +0 -359
  648. package/claude-assets/skills/cfn-loop-orchestration/helpers/deliverable-verifier-ts.sh +0 -123
  649. package/claude-assets/skills/cfn-loop-orchestration/helpers/deliverable-verifier.sh +0 -71
  650. package/claude-assets/skills/cfn-loop-orchestration/helpers/gate-check.sh +0 -56
  651. package/claude-assets/skills/cfn-loop-orchestration/helpers/iteration-manager-ts.sh +0 -89
  652. package/claude-assets/skills/cfn-loop-orchestration/helpers/iteration-manager.sh +0 -87
  653. package/claude-assets/skills/cfn-loop-orchestration/helpers/orchestrate-ts.sh +0 -104
  654. package/claude-assets/skills/cfn-loop-orchestration/helpers/parse-test-results.sh +0 -56
  655. package/claude-assets/skills/cfn-loop-orchestration/helpers/spawn-agents.sh +0 -290
  656. package/claude-assets/skills/cfn-loop-orchestration/helpers/timeout-calculator-ts.sh +0 -47
  657. package/claude-assets/skills/cfn-loop-orchestration/helpers/timeout-calculator.sh +0 -51
  658. package/claude-assets/skills/cfn-loop-orchestration/orchestrate.sh +0 -1345
  659. package/claude-assets/skills/cfn-redis-cleanup/cleanup-redis.sh +0 -130
  660. package/claude-assets/skills/cfn-redis-coordination/AGENT_LOGGING.md +0 -280
  661. package/claude-assets/skills/cfn-redis-coordination/BZPOPMIN_FIX_SUMMARY.md +0 -209
  662. package/claude-assets/skills/cfn-redis-coordination/CENTRALIZED_REDIS_WRAPPER.md +0 -319
  663. package/claude-assets/skills/cfn-redis-coordination/agent-log.sh.bak +0 -124
  664. package/claude-assets/skills/cfn-redis-coordination/config.json +0 -61
  665. package/claude-assets/skills/cfn-redis-coordination/demos/phase4-wake-queue-test-report.md +0 -82
  666. package/claude-assets/skills/cfn-redis-coordination/demos/test-bzpopmin-fix.sh +0 -274
  667. package/claude-assets/skills/cfn-redis-coordination/demos/test-cancel-swarm.sh +0 -0
  668. package/claude-assets/skills/cfn-redis-coordination/docs/migration/PHASE_3_REDIS_COORDINATION_COMPLETION_REPORT.md +0 -553
  669. package/claude-assets/skills/cfn-redis-coordination/jest.config.js +0 -23
  670. package/claude-assets/skills/cfn-redis-coordination/package-lock.json +0 -5272
  671. package/claude-assets/skills/cfn-redis-coordination/package.json +0 -45
  672. package/claude-assets/skills/cfn-redis-coordination/src/agent-logger.ts +0 -446
  673. package/claude-assets/skills/cfn-redis-coordination/src/agent-recovery.ts +0 -454
  674. package/claude-assets/skills/cfn-redis-coordination/src/completion-reporter.ts +0 -396
  675. package/claude-assets/skills/cfn-redis-coordination/src/context-manager.ts +0 -327
  676. package/claude-assets/skills/cfn-redis-coordination/src/index.ts +0 -82
  677. package/claude-assets/skills/cfn-redis-coordination/src/mode-detector.ts +0 -155
  678. package/claude-assets/skills/cfn-redis-coordination/src/redis/redis-client.ts +0 -305
  679. package/claude-assets/skills/cfn-redis-coordination/src/redis/redis-functions.ts +0 -283
  680. package/claude-assets/skills/cfn-redis-coordination/src/redis-client.ts +0 -654
  681. package/claude-assets/skills/cfn-redis-coordination/src/result-collector.ts +0 -437
  682. package/claude-assets/skills/cfn-redis-coordination/src/swarm-manager.ts +0 -494
  683. package/claude-assets/skills/cfn-redis-coordination/src/task-analyzer.ts +0 -404
  684. package/claude-assets/skills/cfn-redis-coordination/src/task-executor.ts +0 -423
  685. package/claude-assets/skills/cfn-redis-coordination/src/types.ts +0 -235
  686. package/claude-assets/skills/cfn-redis-coordination/src/waiting-coordinator.ts +0 -587
  687. package/claude-assets/skills/cfn-redis-coordination/store-success-criteria.sh +0 -85
  688. package/claude-assets/skills/cfn-redis-coordination/test-connection-attempts.js +0 -70
  689. package/claude-assets/skills/cfn-redis-coordination/test-mode-simple.js +0 -121
  690. package/claude-assets/skills/cfn-redis-coordination/test-redis-check.js +0 -84
  691. package/claude-assets/skills/cfn-redis-coordination/test-task-mode-redis.cjs +0 -391
  692. package/claude-assets/skills/cfn-redis-coordination/tests/coordination.test.ts +0 -788
  693. package/claude-assets/skills/cfn-redis-coordination/update-all-scripts.sh +0 -67
  694. package/claude-assets/skills/cfn-redis-data-extraction/SKILL.md +0 -442
  695. package/claude-assets/skills/cfn-redis-data-extraction/extract.sh +0 -306
  696. package/dist/coordination/index.js +0 -25
  697. package/dist/coordination/index.js.map +0 -1
  698. package/docs/BUG_19_MEMORY_LEAK_TASK_MODE.md +0 -405
  699. package/docs/MEMORY_CLEANUP_GUIDE.md +0 -358
  700. package/docs/MEMORY_LEAK_FIX_SUMMARY.md +0 -322
  701. package/docs/REDIS_CLEANUP_EXECUTIVE_SUMMARY.md +0 -319
  702. package/docs/REDIS_CLEANUP_VERIFICATION_REPORT.md +0 -574
  703. package/tests/test-memory-leak-task-mode.sh +0 -435
  704. /package/.claude/skills/cfn-loop-orchestration/{inject-loop-context.sh → archive/legacy-bash/inject-loop-context.sh} +0 -0
  705. /package/.claude/skills/cfn-loop-orchestration/{monitor-execution.sh → archive/legacy-bash/monitor-execution.sh} +0 -0
  706. /package/.claude/skills/cfn-redis-coordination/{agent-log.sh → agent-log.sh.backup} +0 -0
  707. /package/.claude/skills/cfn-redis-coordination/{agent-recovery.sh → agent-recovery.sh.backup} +0 -0
  708. /package/.claude/skills/cfn-redis-coordination/{analyze-task-complexity.sh → analyze-task-complexity.sh.backup} +0 -0
  709. /package/.claude/skills/cfn-redis-coordination/bash-wrappers/{store-context.sh → store-context.sh.backup} +0 -0
  710. /package/.claude/skills/cfn-redis-coordination/{cancel-swarm.sh → cancel-swarm.sh.backup} +0 -0
  711. /package/.claude/skills/cfn-redis-coordination/{cfn-loop-exec.sh → cfn-loop-exec.sh.backup} +0 -0
  712. /package/.claude/skills/cfn-redis-coordination/{cfn-loop-relaunch.sh → cfn-loop-relaunch.sh.backup} +0 -0
  713. /package/.claude/skills/cfn-redis-coordination/{collect-confidence-scores.sh → collect-confidence-scores.sh.backup} +0 -0
  714. /package/.claude/skills/cfn-redis-coordination/{collect-results.sh → collect-results.sh.backup} +0 -0
  715. /package/.claude/skills/cfn-redis-coordination/{complete-swarm.sh → complete-swarm.sh.backup} +0 -0
  716. /package/.claude/skills/cfn-redis-coordination/{get-context.sh → get-context.sh.backup} +0 -0
  717. /package/.claude/skills/cfn-redis-coordination/{get-success-criteria.sh → get-success-criteria.sh.backup} +0 -0
  718. /package/.claude/skills/cfn-redis-coordination/{invoke-waiting-mode.sh → invoke-waiting-mode.sh.backup} +0 -0
  719. /package/.claude/skills/cfn-redis-coordination/{redis-cli-wrapper.sh → redis-cli-wrapper.sh.backup} +0 -0
  720. /package/.claude/skills/cfn-redis-coordination/{redis-functions.sh → redis-functions.sh.backup} +0 -0
  721. /package/.claude/skills/cfn-redis-coordination/{report-completion.sh → report-completion.sh.backup} +0 -0
  722. /package/.claude/skills/cfn-redis-coordination/{store-context.sh → store-context.sh.backup} +0 -0
  723. /package/claude-assets/skills/cfn-loop-orchestration/{inject-loop-context.sh → archive/legacy-bash/inject-loop-context.sh} +0 -0
  724. /package/claude-assets/skills/cfn-loop-orchestration/{monitor-execution.sh → archive/legacy-bash/monitor-execution.sh} +0 -0
  725. /package/claude-assets/skills/cfn-redis-coordination/{agent-log.sh → agent-log.sh.backup} +0 -0
  726. /package/claude-assets/skills/cfn-redis-coordination/{agent-recovery.sh → agent-recovery.sh.backup} +0 -0
  727. /package/claude-assets/skills/cfn-redis-coordination/{analyze-task-complexity.sh → analyze-task-complexity.sh.backup} +0 -0
  728. /package/claude-assets/skills/cfn-redis-coordination/bash-wrappers/{store-context.sh → store-context.sh.backup} +0 -0
  729. /package/claude-assets/skills/cfn-redis-coordination/{cancel-swarm.sh → cancel-swarm.sh.backup} +0 -0
  730. /package/claude-assets/skills/cfn-redis-coordination/{cfn-loop-exec.sh → cfn-loop-exec.sh.backup} +0 -0
  731. /package/claude-assets/skills/cfn-redis-coordination/{cfn-loop-relaunch.sh → cfn-loop-relaunch.sh.backup} +0 -0
  732. /package/claude-assets/skills/cfn-redis-coordination/{collect-confidence-scores.sh → collect-confidence-scores.sh.backup} +0 -0
  733. /package/claude-assets/skills/cfn-redis-coordination/{collect-results.sh → collect-results.sh.backup} +0 -0
  734. /package/claude-assets/skills/cfn-redis-coordination/{complete-swarm.sh → complete-swarm.sh.backup} +0 -0
  735. /package/claude-assets/skills/cfn-redis-coordination/{get-context.sh → get-context.sh.backup} +0 -0
  736. /package/claude-assets/skills/cfn-redis-coordination/{get-success-criteria.sh → get-success-criteria.sh.backup} +0 -0
  737. /package/claude-assets/skills/cfn-redis-coordination/{invoke-waiting-mode.sh → invoke-waiting-mode.sh.backup} +0 -0
  738. /package/claude-assets/skills/cfn-redis-coordination/{redis-cli-wrapper.sh → redis-cli-wrapper.sh.backup} +0 -0
  739. /package/claude-assets/skills/cfn-redis-coordination/{redis-functions.sh → redis-functions.sh.backup} +0 -0
  740. /package/claude-assets/skills/cfn-redis-coordination/{report-completion.sh → report-completion.sh.backup} +0 -0
  741. /package/claude-assets/skills/cfn-redis-coordination/{store-context.sh → store-context.sh.backup} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/cli/agent-executor.ts"],"sourcesContent":["/**\r\n * Agent Executor\r\n *\r\n * Executes CLI-spawned agents by:\r\n * 1. Checking custom routing configuration (z.ai vs Anthropic)\r\n * 2. Invoking the appropriate API\r\n * 3. Managing agent lifecycle and output\r\n */\r\n\r\nimport { spawn } from 'child_process';\r\nimport { exec } from 'child_process';\r\nimport { promisify } from 'util';\r\nimport { AgentDefinition } from './agent-definition-parser.js';\r\nimport { TaskContext, getAgentId } from './agent-prompt-builder.js';\r\nimport { buildCLIAgentSystemPrompt, loadContextFromEnv } from './cli-agent-context.js';\r\nimport {\r\n loadMessages,\r\n storeMessage,\r\n getCurrentFork,\r\n formatMessagesForAPI,\r\n type Message\r\n} from './conversation-fork.js';\r\nimport { convertToolNames } from './tool-definitions.js';\r\nimport fs from 'fs/promises';\r\nimport path from 'path';\r\nimport os from 'os';\r\nimport { execSync } from 'child_process';\r\n\r\nconst execAsync = promisify(exec);\r\n\r\n/**\r\n * Detect project root dynamically\r\n * Uses PROJECT_ROOT env var if set, otherwise tries git, falls back to cwd\r\n */\r\nfunction getProjectRoot(): string {\r\n // 1. Check environment variable\r\n if (process.env.PROJECT_ROOT) {\r\n return process.env.PROJECT_ROOT;\r\n }\r\n\r\n // 2. Try git rev-parse (most reliable)\r\n try {\r\n const gitRoot = execSync('git rev-parse --show-toplevel', {\r\n encoding: 'utf-8',\r\n cwd: process.cwd(),\r\n stdio: ['pipe', 'pipe', 'ignore']\r\n }).trim();\r\n if (gitRoot) {\r\n return gitRoot;\r\n }\r\n } catch {\r\n // Fall through to next method\r\n }\r\n\r\n // 3. Fall back to current working directory\r\n return process.cwd();\r\n}\r\n\r\nconst projectRoot = getProjectRoot();\r\n\r\n// Bug #6 Fix: Read Redis connection parameters from process.env\r\n// ENV-001: Standardized environment variable naming (REDIS_PASSWORD for all deployments)\r\nconst redisHost = process.env.CFN_REDIS_HOST || 'cfn-redis';\r\nconst redisPort = process.env.CFN_REDIS_PORT || '6379';\r\nconst redisPassword = process.env.CFN_REDIS_PASSWORD || process.env.REDIS_PASSWORD || '';\r\n\r\nexport interface AgentExecutionResult {\r\n success: boolean;\r\n agentId: string;\r\n output?: string;\r\n error?: string;\r\n exitCode: number;\r\n}\r\n\r\n/**\r\n * Parse context string into environment variables\r\n *\r\n * Parses context string like \"TASK_ID='xyz' MODE='mvp' MAX_ITERATIONS=5\"\r\n * and sets the values as environment variables.\r\n *\r\n * Supports:\r\n * - Single quoted: KEY='value with spaces'\r\n * - Double quoted: KEY=\"value with spaces\"\r\n * - Unquoted: KEY=value (no spaces in value)\r\n *\r\n * @param contextString - Context string from --context parameter\r\n * @returns Object with parsed key-value pairs\r\n */\r\nfunction parseContextToEnv(contextString: string | undefined): Record<string, string> {\r\n if (!contextString) return {};\r\n\r\n const envVars: Record<string, string> = {};\r\n\r\n // Match KEY='value' or KEY=\"value\" or KEY=value patterns\r\n // For quoted values: capture everything between quotes\r\n // For unquoted values: capture until next space or end of string\r\n const regex = /(\\w+)=(?:(['\"])([^\\2]*?)\\2|([^\\s]+))/g;\r\n let match;\r\n\r\n while ((match = regex.exec(contextString)) !== null) {\r\n const [, key, , quotedValue, unquotedValue] = match;\r\n const value = quotedValue !== undefined ? quotedValue : unquotedValue;\r\n envVars[key] = value;\r\n // Also set in process.env for current process\r\n process.env[key] = value;\r\n }\r\n\r\n return envVars;\r\n}\r\n\r\n/**\r\n * Extract confidence score from agent output\r\n * Looks for patterns like:\r\n * - \"confidence: 0.85\"\r\n * - \"Confidence: 0.90\"\r\n * - \"confidence score: 0.95\"\r\n * - \"self-confidence: 0.88\"\r\n */\r\nfunction extractConfidence(output: string | undefined): number {\r\n if (!output) return 0.85;\r\n\r\n // Try multiple patterns\r\n const patterns = [\r\n /confidence:\\s*([0-9.]+)/i,\r\n /confidence\\s+score:\\s*([0-9.]+)/i,\r\n /self-confidence:\\s*([0-9.]+)/i,\r\n /my\\s+confidence:\\s*([0-9.]+)/i,\r\n ];\r\n\r\n for (const pattern of patterns) {\r\n const match = output.match(pattern);\r\n if (match && match[1]) {\r\n const score = parseFloat(match[1]);\r\n if (score >= 0 && score <= 1) {\r\n return score;\r\n }\r\n }\r\n }\r\n\r\n // Default to 0.85 if not found\r\n return 0.85;\r\n}\r\n\r\n/**\r\n * Execute CFN Loop protocol after agent completes work\r\n *\r\n * Steps:\r\n * 1. Signal completion to orchestrator\r\n * 2. Report confidence score\r\n * 3. Enter waiting mode (if iterations enabled)\r\n */\r\nasync function executeCFNProtocol(\r\n taskId: string,\r\n agentId: string,\r\n output: string | undefined,\r\n iteration: number,\r\n enableIterations: boolean = false,\r\n maxIterations: number = 10\r\n): Promise<void> {\r\n console.log(`\\n[CFN Protocol] Starting for agent ${agentId}`);\r\n console.log(`[CFN Protocol] Task ID: ${taskId}, Iteration: ${iteration}`);\r\n\r\n try {\r\n // Step 1: Signal completion\r\n console.log('[CFN Protocol] Step 1: Signaling completion...');\r\n const authFlag = redisPassword ? `-a \"${redisPassword}\"` : '';\r\n await execAsync(`redis-cli -h \"${redisHost}\" -p \"${redisPort}\" ${authFlag} lpush \"swarm:${taskId}:${agentId}:done\" \"complete\"`);\r\n console.log('[CFN Protocol] ✓ Completion signaled');\r\n\r\n // Step 2: Extract and report confidence\r\n const confidence = extractConfidence(output);\r\n console.log(`[CFN Protocol] Step 2: Reporting confidence (${confidence})...`);\r\n\r\n const reportCmd = `./.claude/skills/cfn-redis-coordination/report-completion.sh \\\r\n --task-id \"${taskId}\" \\\r\n --agent-id \"${agentId}\" \\\r\n --confidence ${confidence} \\\r\n --iteration ${iteration}`;\r\n\r\n await execAsync(reportCmd);\r\n console.log('[CFN Protocol] ✓ Confidence reported');\r\n\r\n // Step 3: Exit cleanly (BUG #18 FIX - removed waiting mode)\r\n // Orchestrator will spawn appropriate specialist agent for next iteration\r\n // This enables adaptive agent specialization based on feedback type\r\n console.log('[CFN Protocol] Step 3: Exiting cleanly (iteration complete)');\r\n console.log('[CFN Protocol] Protocol complete\\n');\r\n } catch (error) {\r\n console.error('[CFN Protocol] Error:', error);\r\n throw error;\r\n }\r\n}\r\n\r\n/**\r\n * Check if custom routing (z.ai) is enabled\r\n */\r\nasync function isCustomRoutingEnabled(): Promise<boolean> {\r\n // Check environment variable\r\n if (process.env.CLAUDE_API_PROVIDER === 'zai') {\r\n return true;\r\n }\r\n\r\n // Check config file (.claude/config/api-provider.json)\r\n try {\r\n const configPath = path.join(projectRoot, '.claude', 'config', 'api-provider.json');\r\n const config = JSON.parse(await fs.readFile(configPath, 'utf-8'));\r\n return config.provider === 'zai' || config.provider === 'z.ai';\r\n } catch {\r\n return false;\r\n }\r\n}\r\n\r\n/**\r\n * Get API provider configuration\r\n */\r\nasync function getAPIProvider(): Promise<'anthropic' | 'zai'> {\r\n const customEnabled = await isCustomRoutingEnabled();\r\n return customEnabled ? 'zai' : 'anthropic';\r\n}\r\n\r\n/**\r\n * Execute agent using direct API calls\r\n */\r\nasync function executeViaAPI(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n const agentId = getAgentId(definition, context);\r\n\r\n console.log(`[agent-executor] Executing agent via API: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log('');\r\n\r\n // BUG #24 FIX: Parse and inject context environment variables\r\n // This enables CLI-spawned agents to access TASK_ID, MODE, etc. in Bash tool\r\n if (context.context) {\r\n console.log(`[agent-executor] Parsing context: ${context.context}`);\r\n const contextEnv = parseContextToEnv(context.context);\r\n console.log(`[agent-executor] Injected env vars: ${Object.keys(contextEnv).join(', ')}`);\r\n }\r\n\r\n try {\r\n // Check for conversation fork (Sprint 4 enhancement)\r\n const forkId = process.env.FORK_ID || await getCurrentFork(context.taskId || '', agentId);\r\n const iteration = context.iteration || 1;\r\n\r\n let systemPrompt: string;\r\n let messages: Array<{role: string, content: string}> = [];\r\n\r\n if (forkId && iteration > 1) {\r\n // Continue from fork (iterations 2+)\r\n console.log(`[agent-executor] Continuing from fork: ${forkId}`);\r\n\r\n // Load fork messages\r\n const forkMessages = await loadMessages(context.taskId || '', agentId, forkId);\r\n console.log(`[agent-executor] Loaded ${forkMessages.length} messages from fork`);\r\n\r\n // Extract system prompt from first message (it's always the system message)\r\n // The fork messages are assistant/user pairs, we need to add system separately\r\n systemPrompt = forkMessages[0]?.content || '';\r\n\r\n // Format remaining messages for API\r\n messages = formatMessagesForAPI(forkMessages.slice(1));\r\n\r\n // Add new user message with feedback\r\n messages.push({\r\n role: 'user',\r\n content: prompt\r\n });\r\n\r\n console.log(`[agent-executor] Fork continuation: ${messages.length} messages`);\r\n } else {\r\n // New conversation (iteration 1)\r\n console.log('[agent-executor] Starting new conversation');\r\n console.log('[agent-executor] Building system prompt with context...');\r\n\r\n const contextOptions = loadContextFromEnv();\r\n contextOptions.agentType = definition.name;\r\n if (context.taskId) contextOptions.taskId = context.taskId;\r\n if (context.iteration) contextOptions.iteration = context.iteration;\r\n\r\n systemPrompt = await buildCLIAgentSystemPrompt(contextOptions);\r\n console.log('[agent-executor] System prompt built successfully');\r\n\r\n // Initial user message\r\n messages = [{\r\n role: 'user',\r\n content: prompt\r\n }];\r\n }\r\n\r\n console.log('');\r\n\r\n // Dynamic import to avoid bundling issues\r\n const { executeAgentAPI } = await import('./anthropic-client.js');\r\n\r\n // Convert agent tool names to Anthropic API format\r\n const tools = definition.tools && definition.tools.length > 0\r\n ? convertToolNames(definition.tools)\r\n : undefined;\r\n\r\n const result = await executeAgentAPI(\r\n definition.name,\r\n agentId,\r\n definition.model,\r\n prompt,\r\n systemPrompt,\r\n messages.length > 1 ? messages : undefined,\r\n undefined, // maxTokens (use default)\r\n tools // Pass converted tools\r\n );\r\n\r\n // Store messages in conversation history (for future forking)\r\n if (context.taskId) {\r\n // Store user message\r\n const userMessage: Message = {\r\n role: 'user',\r\n content: prompt,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, userMessage);\r\n\r\n // Store assistant response\r\n if (result.output) {\r\n const assistantMessage: Message = {\r\n role: 'assistant',\r\n content: result.output,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, assistantMessage);\r\n }\r\n\r\n console.log(`[agent-executor] Stored messages for iteration ${iteration}`);\r\n\r\n // Execute CFN Loop protocol (signal completion, report confidence, enter waiting mode)\r\n // Iterations are enabled for CFN Loop tasks (indicated by presence of taskId)\r\n try {\r\n const maxIterations = 10; // Default max iterations\r\n const enableIterations = true; // Enable iterations for all CFN Loop tasks\r\n\r\n await executeCFNProtocol(\r\n context.taskId,\r\n agentId,\r\n result.output,\r\n iteration,\r\n enableIterations,\r\n maxIterations\r\n );\r\n } catch (error) {\r\n console.error('[agent-executor] CFN Protocol execution failed:', error);\r\n // Don't fail the entire agent execution if CFN protocol fails\r\n // This allows agents to complete even if Redis coordination has issues\r\n }\r\n }\r\n\r\n return {\r\n success: result.success,\r\n agentId,\r\n output: result.output,\r\n error: result.error,\r\n exitCode: result.success ? 0 : 1,\r\n };\r\n } catch (error) {\r\n console.error('[agent-executor] API execution failed:', error);\r\n return {\r\n success: false,\r\n agentId,\r\n error: error instanceof Error ? error.message : String(error),\r\n exitCode: 1,\r\n };\r\n }\r\n}\r\n\r\n/**\r\n * Execute agent using shell script (fallback/simulation)\r\n */\r\nasync function executeViaScript(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n const agentId = getAgentId(definition, context);\r\n\r\n // BUG #24 FIX: Parse and inject context environment variables\r\n if (context.context) {\r\n console.log(`[agent-executor] Parsing context: ${context.context}`);\r\n const contextEnv = parseContextToEnv(context.context);\r\n console.log(`[agent-executor] Injected env vars: ${Object.keys(contextEnv).join(', ')}`);\r\n }\r\n\r\n // Write prompt to temporary file\r\n const tmpDir = os.tmpdir();\r\n const promptFile = path.join(tmpDir, `agent-${agentId}-${Date.now()}.md`);\r\n await fs.writeFile(promptFile, prompt, 'utf-8');\r\n\r\n console.log(`[agent-executor] Executing agent via script: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log(`[agent-executor] Prompt file: ${promptFile}`);\r\n\r\n return new Promise((resolve) => {\r\n const scriptPath = path.join(projectRoot, '.claude', 'skills', 'agent-execution', 'execute-agent.sh');\r\n\r\n // Build environment variables - WHITELIST ONLY APPROACH\r\n // SECURITY FIX: Do not use ...process.env spread which exposes ALL variables including secrets\r\n // Instead, explicitly whitelist safe variables to pass to spawned process\r\n const safeEnvVars = [\r\n 'CFN_REDIS_HOST',\r\n 'CFN_REDIS_PORT',\r\n 'CFN_REDIS_PASSWORD', // CRITICAL: Required for Redis authentication\r\n 'CFN_REDIS_URL',\r\n 'REDIS_PASSWORD', // Fallback for Redis password\r\n 'CFN_MEMORY_BUDGET',\r\n 'CFN_API_HOST',\r\n 'CFN_API_PORT',\r\n 'CFN_LOG_LEVEL',\r\n 'CFN_LOG_FORMAT',\r\n 'CFN_CONTAINER_MODE',\r\n 'CFN_DOCKER_SOCKET',\r\n 'CFN_NETWORK_NAME',\r\n 'CFN_CUSTOM_ROUTING',\r\n 'CFN_DEFAULT_PROVIDER',\r\n 'NODE_ENV',\r\n 'PATH',\r\n 'HOME',\r\n 'PWD' // Required for working directory context\r\n ];\r\n\r\n const env: Record<string, string> = {};\r\n\r\n // Add whitelisted variables\r\n for (const key of safeEnvVars) {\r\n const value = process.env[key];\r\n if (value !== undefined) {\r\n env[key] = value;\r\n }\r\n }\r\n\r\n // Add API key only when explicitly needed (with strict validation)\r\n if (process.env.ANTHROPIC_API_KEY) {\r\n if (process.env.ANTHROPIC_API_KEY.match(/^sk-[a-zA-Z0-9-]+$/)) {\r\n env.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;\r\n }\r\n }\r\n\r\n // Add agent execution context (safe values)\r\n env.AGENT_TYPE = definition.name;\r\n env.AGENT_ID = agentId;\r\n env.AGENT_MODEL = definition.model;\r\n env.AGENT_TOOLS = definition.tools.join(',');\r\n env.TASK_ID = context.taskId || '';\r\n env.ITERATION = String(context.iteration || 1);\r\n env.MODE = context.mode || 'cli';\r\n env.PROMPT_FILE = promptFile;\r\n\r\n // Check if execute script exists\r\n fs.access(scriptPath)\r\n .then(() => {\r\n // Use execution script\r\n const proc = spawn('bash', [scriptPath], { env, stdio: 'inherit' });\r\n\r\n proc.on('exit', (code) => {\r\n resolve({\r\n success: code === 0,\r\n agentId,\r\n exitCode: code || 0,\r\n });\r\n });\r\n\r\n proc.on('error', (err) => {\r\n resolve({\r\n success: false,\r\n agentId,\r\n error: err.message,\r\n exitCode: 1,\r\n });\r\n });\r\n })\r\n .catch(() => {\r\n // Fallback: Print prompt\r\n console.log('\\n=== Agent Prompt ===');\r\n console.log(prompt.substring(0, 500) + '...');\r\n console.log('\\n[agent-executor] Execution script not found');\r\n console.log('[agent-executor] Using simulation mode\\n');\r\n\r\n resolve({\r\n success: true,\r\n agentId,\r\n output: prompt,\r\n exitCode: 0,\r\n });\r\n });\r\n });\r\n}\r\n\r\n/**\r\n * Main agent execution function\r\n */\r\nexport async function executeAgent(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext,\r\n options: {\r\n method?: 'auto' | 'api' | 'script';\r\n } = {}\r\n): Promise<AgentExecutionResult> {\r\n const method = options.method || 'auto';\r\n\r\n // Auto-select execution method\r\n if (method === 'auto') {\r\n // Try API execution first, fallback to script if API key not available\r\n try {\r\n return await executeViaAPI(definition, prompt, context);\r\n } catch (error) {\r\n if (error instanceof Error && error.message.includes('API key not found')) {\r\n console.log('[agent-executor] API key not found, using script fallback');\r\n return executeViaScript(definition, prompt, context);\r\n }\r\n throw error;\r\n }\r\n }\r\n\r\n if (method === 'api') {\r\n return executeViaAPI(definition, prompt, context);\r\n }\r\n\r\n return executeViaScript(definition, prompt, context);\r\n}\r\n\r\n/**\r\n * Write agent output to file for debugging\r\n */\r\nexport async function saveAgentOutput(\r\n agentId: string,\r\n output: string,\r\n outputDir: string = '.claude/tmp/agent-output'\r\n): Promise<string> {\r\n await fs.mkdir(outputDir, { recursive: true });\r\n\r\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-');\r\n const filename = `${agentId}-${timestamp}.txt`;\r\n const filepath = path.join(outputDir, filename);\r\n\r\n await fs.writeFile(filepath, output, 'utf-8');\r\n\r\n return filepath;\r\n}\r\n"],"names":["spawn","exec","promisify","getAgentId","buildCLIAgentSystemPrompt","loadContextFromEnv","loadMessages","storeMessage","getCurrentFork","formatMessagesForAPI","convertToolNames","fs","path","os","execSync","execAsync","getProjectRoot","process","env","PROJECT_ROOT","gitRoot","encoding","cwd","stdio","trim","projectRoot","redisHost","CFN_REDIS_HOST","redisPort","CFN_REDIS_PORT","redisPassword","CFN_REDIS_PASSWORD","REDIS_PASSWORD","parseContextToEnv","contextString","envVars","regex","match","key","quotedValue","unquotedValue","value","undefined","extractConfidence","output","patterns","pattern","score","parseFloat","executeCFNProtocol","taskId","agentId","iteration","enableIterations","maxIterations","console","log","authFlag","confidence","reportCmd","error","isCustomRoutingEnabled","CLAUDE_API_PROVIDER","configPath","join","config","JSON","parse","readFile","provider","getAPIProvider","customEnabled","executeViaAPI","definition","prompt","context","name","model","contextEnv","Object","keys","forkId","FORK_ID","systemPrompt","messages","forkMessages","length","content","slice","push","role","contextOptions","agentType","executeAgentAPI","tools","result","userMessage","timestamp","Date","toISOString","assistantMessage","success","exitCode","Error","message","String","executeViaScript","tmpDir","tmpdir","promptFile","now","writeFile","Promise","resolve","scriptPath","safeEnvVars","ANTHROPIC_API_KEY","AGENT_TYPE","AGENT_ID","AGENT_MODEL","AGENT_TOOLS","TASK_ID","ITERATION","MODE","mode","PROMPT_FILE","access","then","proc","on","code","err","catch","substring","executeAgent","options","method","includes","saveAgentOutput","outputDir","mkdir","recursive","replace","filename","filepath"],"mappings":"AAAA;;;;;;;CAOC,GAED,SAASA,KAAK,QAAQ,gBAAgB;AACtC,SAASC,IAAI,QAAQ,gBAAgB;AACrC,SAASC,SAAS,QAAQ,OAAO;AAEjC,SAAsBC,UAAU,QAAQ,4BAA4B;AACpE,SAASC,yBAAyB,EAAEC,kBAAk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GACD,OAAO,eAAe+D,gBACpBvF,OAAe,EACfP,MAAc,EACd+F,YAAoB,0BAA0B;IAE9C,MAAMhI,GAAGiI,KAAK,CAACD,WAAW;QAAEE,WAAW;IAAK;IAE5C,MAAM5C,YAAY,IAAIC,OAAOC,WAAW,GAAG2C,OAAO,CAAC,SAAS;IAC5D,MAAMC,WAAW,GAAG5F,QAAQ,CAAC,EAAE8C,UAAU,IAAI,CAAC;IAC9C,MAAM+C,WAAWpI,KAAKoD,IAAI,CAAC2E,WAAWI;IAEtC,MAAMpI,GAAGoG,SAAS,CAACiC,UAAUpG,QAAQ;IAErC,OAAOoG;AACT"}
1
+ {"version":3,"sources":["../../src/cli/agent-executor.ts"],"sourcesContent":["/**\r\n * Agent Executor\r\n *\r\n * Executes CLI-spawned agents by:\r\n * 1. Checking custom routing configuration (z.ai vs Anthropic)\r\n * 2. Invoking the appropriate API\r\n * 3. Managing agent lifecycle and output\r\n */\r\n\r\nimport { spawn } from 'child_process';\r\nimport { exec } from 'child_process';\r\nimport { promisify } from 'util';\r\nimport { createClient, RedisClientType } from 'redis';\r\nimport { AgentDefinition } from './agent-definition-parser.js';\r\nimport { TaskContext, getAgentId } from './agent-prompt-builder.js';\r\nimport { buildCLIAgentSystemPrompt, loadContextFromEnv } from './cli-agent-context.js';\r\nimport {\r\n loadMessages,\r\n storeMessage,\r\n getCurrentFork,\r\n formatMessagesForAPI,\r\n type Message\r\n} from './conversation-fork.js';\r\nimport { convertToolNames } from './tool-definitions.js';\r\nimport { AgentCommandProcessor, type AgentCommand } from './coordination/agent-messaging.js';\r\nimport fs from 'fs/promises';\r\nimport fsSync from 'fs';\r\nimport path from 'path';\r\nimport os from 'os';\r\nimport { execSync } from 'child_process';\r\n\r\nconst execAsync = promisify(exec);\r\n\r\n// DEBUG: File-based logging for background agents (stdio: 'ignore' masks errors)\r\nconst AGENT_ID = process.env.AGENT_ID || 'unknown';\r\nconst LOG_FILE = `/tmp/cfn-agent-${AGENT_ID}.log`;\r\nfunction debugLog(message: string, data?: any) {\r\n const timestamp = new Date().toISOString();\r\n const logEntry = data\r\n ? `${timestamp} [${AGENT_ID}] ${message} ${JSON.stringify(data)}\\n`\r\n : `${timestamp} [${AGENT_ID}] ${message}\\n`;\r\n try {\r\n fsSync.appendFileSync(LOG_FILE, logEntry);\r\n } catch (err) {\r\n // Ignore logging errors\r\n }\r\n}\r\n\r\ndebugLog('=== Agent Executor Started ===');\r\n\r\n/**\r\n * Detect project root dynamically\r\n * Uses PROJECT_ROOT env var if set, otherwise tries git, falls back to cwd\r\n */\r\nfunction getProjectRoot(): string {\r\n // 1. Check environment variable\r\n if (process.env.PROJECT_ROOT) {\r\n return process.env.PROJECT_ROOT;\r\n }\r\n\r\n // 2. Try git rev-parse (most reliable)\r\n try {\r\n const gitRoot = execSync('git rev-parse --show-toplevel', {\r\n encoding: 'utf-8',\r\n cwd: process.cwd(),\r\n stdio: ['pipe', 'pipe', 'ignore']\r\n }).trim();\r\n if (gitRoot) {\r\n return gitRoot;\r\n }\r\n } catch {\r\n // Fall through to next method\r\n }\r\n\r\n // 3. Fall back to current working directory\r\n return process.cwd();\r\n}\r\n\r\nconst projectRoot = getProjectRoot();\r\n\r\n// Bug #6 Fix: Read Redis connection parameters from process.env\r\n// ENV-001: Standardized environment variable naming (REDIS_PASSWORD for all deployments)\r\n// ROOT CAUSE #2 FIX: Don't fall back to REDIS_PASSWORD from parent env (CLI mode has no password)\r\n// FIX: Default to 'localhost' for CLI mode (host execution), not 'cfn-redis' (Docker)\r\n// CLI agents run on the host and connect to Redis at localhost:6379\r\n// Docker agents should have CFN_REDIS_HOST explicitly set to their container network hostname\r\nconst redisHost = process.env.CFN_REDIS_HOST || 'localhost';\r\nconst redisPort = process.env.CFN_REDIS_PORT || '6379';\r\nconst redisPassword = process.env.CFN_REDIS_PASSWORD || '';\r\n\r\n/**\r\n * Validate task ID format to prevent command injection\r\n * Allows: optional namespace prefix (e.g., \"cli:\", \"task:\"), alphanumeric, hyphens, underscores, dots\r\n * Pattern: /^([a-z]+:)?[a-zA-Z0-9_.-]+$/\r\n * - Optional prefix: lowercase letters followed by colon\r\n * - Main ID: alphanumeric, hyphens, underscores, dots\r\n * @param taskId - The task ID to validate\r\n * @throws Error if taskId is invalid\r\n */\r\nfunction validateTaskId(taskId: string): void {\r\n if (!taskId || !/^([a-z]+:)?[a-zA-Z0-9_.-]+$/.test(taskId)) {\r\n throw new Error(`Invalid task ID format: \"${taskId}\". Must contain optional namespace prefix (e.g., \"cli:\") and alphanumeric characters, hyphens, underscores, or dots.`);\r\n }\r\n}\r\n\r\n/**\r\n * Validate agent ID format to prevent command injection\r\n * Allows: alphanumeric, hyphens, underscores\r\n * @param agentId - The agent ID to validate\r\n * @throws Error if agentId is invalid\r\n */\r\nfunction validateAgentId(agentId: string): void {\r\n if (!agentId || !/^[a-zA-Z0-9_-]+$/.test(agentId)) {\r\n throw new Error(`Invalid agent ID format: \"${agentId}\". Must contain only alphanumeric characters, hyphens, and underscores.`);\r\n }\r\n}\r\n\r\n/**\r\n * Create a Redis client with proper connection handling\r\n * Uses environment variables for connection configuration\r\n * @returns Promise<RedisClientType> - Connected Redis client\r\n */\r\nasync function createRedisClient(): Promise<RedisClientType> {\r\n debugLog('createRedisClient: Starting Redis connection attempt');\r\n debugLog('Redis config', { host: redisHost, port: redisPort, hasPassword: !!redisPassword });\r\n console.error('[DEBUG] createRedisClient: Starting Redis connection...');\r\n console.error(`[DEBUG] Redis config: host=${redisHost}, port=${redisPort}, hasPassword=${!!redisPassword}`);\r\n\r\n const portNum = parseInt(redisPort, 10);\r\n debugLog(`createRedisClient: Creating client for ${redisHost}:${portNum}`);\r\n\r\n const client = createClient({\r\n socket: {\r\n host: redisHost,\r\n port: portNum,\r\n reconnectStrategy: (retries) => Math.min(retries * 50, 500),\r\n connectTimeout: 5000,\r\n },\r\n password: redisPassword || undefined,\r\n });\r\n\r\n client.on('error', (err: Error) => {\r\n debugLog('Redis Client Error event triggered', { error: err.message, stack: err.stack });\r\n console.error('[DEBUG] Redis Client Error:', err);\r\n });\r\n\r\n debugLog('createRedisClient: Client created, calling connect()');\r\n console.error('[DEBUG] createRedisClient: Client created, attempting connection...');\r\n\r\n await client.connect();\r\n\r\n debugLog('createRedisClient: ✓ Connected successfully');\r\n console.error('[DEBUG] createRedisClient: ✓ Connected successfully');\r\n return client;\r\n}\r\n\r\nexport interface AgentExecutionResult {\r\n success: boolean;\r\n agentId: string;\r\n output?: string;\r\n error?: string;\r\n exitCode: number;\r\n}\r\n\r\n/**\r\n * Parse context string into environment variables\r\n *\r\n * Parses context string like \"TASK_ID='xyz' MODE='mvp' MAX_ITERATIONS=5\"\r\n * and sets the values as environment variables.\r\n *\r\n * Supports:\r\n * - Single quoted: KEY='value with spaces'\r\n * - Double quoted: KEY=\"value with spaces\"\r\n * - Unquoted: KEY=value (no spaces in value)\r\n *\r\n * @param contextString - Context string from --context parameter\r\n * @returns Object with parsed key-value pairs\r\n */\r\nfunction parseContextToEnv(contextString: string | undefined): Record<string, string> {\r\n if (!contextString) return {};\r\n\r\n const envVars: Record<string, string> = {};\r\n\r\n // Match KEY='value' or KEY=\"value\" or KEY=value patterns\r\n // For quoted values: capture everything between quotes\r\n // For unquoted values: capture until next space or end of string\r\n const regex = /(\\w+)=(?:(['\"])([^\\2]*?)\\2|([^\\s]+))/g;\r\n let match;\r\n\r\n while ((match = regex.exec(contextString)) !== null) {\r\n const [, key, , quotedValue, unquotedValue] = match;\r\n const value = quotedValue !== undefined ? quotedValue : unquotedValue;\r\n envVars[key] = value;\r\n // Also set in process.env for current process\r\n process.env[key] = value;\r\n }\r\n\r\n return envVars;\r\n}\r\n\r\n/**\r\n * Extract confidence score from agent output\r\n * Looks for patterns like:\r\n * - \"confidence: 0.85\"\r\n * - \"Confidence: 0.90\"\r\n * - \"confidence score: 0.95\"\r\n * - \"self-confidence: 0.88\"\r\n */\r\nfunction extractConfidence(output: string | undefined): number {\r\n if (!output) return 0.85;\r\n\r\n // Try multiple patterns\r\n const patterns = [\r\n /confidence:\\s*([0-9.]+)/i,\r\n /confidence\\s+score:\\s*([0-9.]+)/i,\r\n /self-confidence:\\s*([0-9.]+)/i,\r\n /my\\s+confidence:\\s*([0-9.]+)/i,\r\n ];\r\n\r\n for (const pattern of patterns) {\r\n const match = output.match(pattern);\r\n if (match && match[1]) {\r\n const score = parseFloat(match[1]);\r\n if (score >= 0 && score <= 1) {\r\n return score;\r\n }\r\n }\r\n }\r\n\r\n // Default to 0.85 if not found\r\n return 0.85;\r\n}\r\n\r\n/**\r\n * Execute CFN Loop protocol after agent completes work\r\n *\r\n * Steps:\r\n * 1. Signal completion to orchestrator and Main Chat via Redis (includes confidence)\r\n * 2. Exit cleanly (orchestrator spawns next agent if needed)\r\n *\r\n * Note: Confidence is included in the completion signal JSON payload.\r\n * The deprecated report-completion.sh script has been removed (Bug #4 fix).\r\n */\r\nasync function executeCFNProtocol(\r\n taskId: string,\r\n agentId: string,\r\n output: string | undefined,\r\n iteration: number,\r\n enableIterations: boolean = false,\r\n maxIterations: number = 10\r\n): Promise<void> {\r\n console.error('[DEBUG] executeCFNProtocol: ENTRY POINT REACHED');\r\n console.log(`\\n[CFN Protocol] Starting for agent ${agentId}`);\r\n console.log(`[CFN Protocol] Task ID: ${taskId}, Iteration: ${iteration}`);\r\n\r\n // SECURITY FIX: Validate inputs to prevent command injection\r\n console.error('[DEBUG] executeCFNProtocol: Validating taskId and agentId...');\r\n validateTaskId(taskId);\r\n validateAgentId(agentId);\r\n console.error('[DEBUG] executeCFNProtocol: Validation passed');\r\n\r\n let redisClient: RedisClientType | null = null;\r\n\r\n try {\r\n // Step 1: Signal completion using Redis client (NOT shell commands)\r\n debugLog('executeCFNProtocol: Starting Step 1 (Redis signaling)', { taskId, agentId, iteration });\r\n console.error('[DEBUG] executeCFNProtocol: Starting Step 1 (Redis signaling)...');\r\n console.log('[CFN Protocol] Step 1: Signaling completion...');\r\n\r\n debugLog('executeCFNProtocol: Calling createRedisClient()');\r\n redisClient = await createRedisClient();\r\n debugLog('executeCFNProtocol: Redis client obtained successfully');\r\n console.error('[DEBUG] executeCFNProtocol: Redis client obtained');\r\n\r\n // Signal to orchestrator (CFN Loop coordination) - using parameterized Redis call\r\n const orchestratorKey = `swarm:${taskId}:${agentId}:done`;\r\n debugLog('executeCFNProtocol: Sending orchestrator signal', { key: orchestratorKey });\r\n console.error(`[DEBUG] executeCFNProtocol: Sending orchestrator signal to key: swarm:${taskId}:${agentId}:done`);\r\n\r\n await redisClient.lPush(orchestratorKey, 'complete');\r\n\r\n debugLog('executeCFNProtocol: Orchestrator signal sent successfully');\r\n console.error('[DEBUG] executeCFNProtocol: Orchestrator signal sent successfully');\r\n console.log('[CFN Protocol] ✓ Orchestrator signal sent');\r\n\r\n // Signal to Main Chat (CLI mode coordination - correct key format) - using parameterized Redis call\r\n const mainChatKey = `cfn:cli:${taskId}:completion`;\r\n debugLog('executeCFNProtocol: Preparing Main Chat signal', { key: mainChatKey });\r\n console.error(`[DEBUG] executeCFNProtocol: Preparing Main Chat signal for key: cfn:cli:${taskId}:completion`);\r\n\r\n const confidence = extractConfidence(output);\r\n const agentMetadata = JSON.stringify({\r\n agentId,\r\n taskId,\r\n status: 'completed',\r\n iteration,\r\n confidence,\r\n });\r\n debugLog('executeCFNProtocol: Agent metadata prepared', { metadata: agentMetadata, confidence });\r\n\r\n await redisClient.lPush(mainChatKey, agentMetadata);\r\n\r\n debugLog('executeCFNProtocol: Main Chat signal sent successfully');\r\n console.error('[DEBUG] executeCFNProtocol: Main Chat signal sent successfully');\r\n console.log('[CFN Protocol] ✓ Main Chat signal sent');\r\n console.log(`[CFN Protocol] ✓ Confidence reported: ${confidence}`);\r\n\r\n // Step 2: Exit cleanly (BUG #18 FIX - removed waiting mode)\r\n // Orchestrator will spawn appropriate specialist agent for next iteration\r\n // This enables adaptive agent specialization based on feedback type\r\n debugLog('executeCFNProtocol: Step 2 - Exiting cleanly');\r\n console.error('[DEBUG] executeCFNProtocol: Step 2 - Exiting cleanly...');\r\n console.log('[CFN Protocol] Step 2: Exiting cleanly (iteration complete)');\r\n console.log('[CFN Protocol] Protocol complete\\n');\r\n debugLog('executeCFNProtocol: ✓ PROTOCOL COMPLETED SUCCESSFULLY');\r\n console.error('[DEBUG] executeCFNProtocol: ✓ PROTOCOL COMPLETED SUCCESSFULLY');\r\n } catch (error) {\r\n const errorDetails = error instanceof Error ? {\r\n message: error.message,\r\n stack: error.stack,\r\n name: error.name\r\n } : { error: String(error) };\r\n debugLog('executeCFNProtocol: ERROR CAUGHT', errorDetails);\r\n console.error('[DEBUG] executeCFNProtocol: ERROR CAUGHT:', error);\r\n console.error('[CFN Protocol] Error:', error);\r\n throw error;\r\n } finally {\r\n // Always close the Redis connection\r\n debugLog('executeCFNProtocol: FINALLY block - cleaning up Redis client');\r\n console.error('[DEBUG] executeCFNProtocol: FINALLY block - cleaning up Redis client...');\r\n if (redisClient) {\r\n await redisClient.quit();\r\n debugLog('executeCFNProtocol: Redis client closed successfully');\r\n console.error('[DEBUG] executeCFNProtocol: Redis client closed');\r\n } else {\r\n debugLog('executeCFNProtocol: No Redis client to close');\r\n }\r\n debugLog('executeCFNProtocol: FINALLY block complete');\r\n console.error('[DEBUG] executeCFNProtocol: FINALLY block complete');\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Bidirectional Messaging Support (v2.0)\r\n// ============================================================================\r\n\r\n/**\r\n * Agent state that can be modified by Main Chat commands\r\n */\r\ninterface AgentRuntimeState {\r\n paused: boolean;\r\n abortRequested: boolean;\r\n redirectContext?: string;\r\n lastCommandTime?: string;\r\n}\r\n\r\n/**\r\n * Start command processor for bidirectional messaging with Main Chat\r\n *\r\n * Enables Main Chat to send commands to running agents:\r\n * - status: Request agent status update\r\n * - redirect: Redirect agent to new task context\r\n * - abort: Request clean agent abort\r\n * - pause: Pause agent for N seconds\r\n */\r\nfunction startCommandProcessor(\r\n taskId: string,\r\n agentId: string,\r\n state: AgentRuntimeState\r\n): AgentCommandProcessor | null {\r\n // Only start if Redis is available and we have valid IDs\r\n if (!taskId || !agentId) {\r\n debugLog('startCommandProcessor: Skipping - missing taskId or agentId');\r\n return null;\r\n }\r\n\r\n try {\r\n debugLog('startCommandProcessor: Creating processor', { taskId, agentId });\r\n\r\n const processor = new AgentCommandProcessor({\r\n taskId,\r\n agentId,\r\n redisHost: redisHost,\r\n redisPort: parseInt(redisPort, 10),\r\n redisPassword: redisPassword || undefined\r\n });\r\n\r\n // Register redirect handler - updates agent context\r\n processor.onCommand('redirect', async (command: AgentCommand) => {\r\n debugLog('Command received: redirect', command.payload);\r\n console.log(`[agent-msg] Received redirect command`);\r\n\r\n if (command.payload?.newTask) {\r\n state.redirectContext = command.payload.newTask as string;\r\n state.lastCommandTime = new Date().toISOString();\r\n console.log(`[agent-msg] New context: ${state.redirectContext}`);\r\n }\r\n\r\n return {\r\n agentId,\r\n taskId,\r\n status: 'running',\r\n timestamp: new Date().toISOString(),\r\n currentStep: 'Acknowledged redirect',\r\n metadata: { redirectedTo: state.redirectContext }\r\n };\r\n });\r\n\r\n // Register custom status handler with more detail\r\n processor.onCommand('status', async (_command: AgentCommand) => {\r\n debugLog('Command received: status request');\r\n console.log(`[agent-msg] Received status request`);\r\n\r\n return {\r\n agentId,\r\n taskId,\r\n status: state.paused ? 'paused' : (state.abortRequested ? 'aborting' : 'running'),\r\n timestamp: new Date().toISOString(),\r\n metadata: {\r\n paused: state.paused,\r\n abortRequested: state.abortRequested,\r\n redirectContext: state.redirectContext,\r\n lastCommandTime: state.lastCommandTime\r\n }\r\n };\r\n });\r\n\r\n // Register abort handler\r\n processor.onCommand('abort', async (_command: AgentCommand) => {\r\n debugLog('Command received: abort request');\r\n console.log(`[agent-msg] Received abort request - will exit after current task`);\r\n state.abortRequested = true;\r\n state.lastCommandTime = new Date().toISOString();\r\n\r\n return {\r\n agentId,\r\n taskId,\r\n status: 'aborting',\r\n timestamp: new Date().toISOString()\r\n };\r\n });\r\n\r\n // Start processor in background (non-blocking)\r\n processor.start(3).catch(err => {\r\n debugLog('Command processor error', { error: err.message });\r\n console.error('[agent-msg] Command processor error:', err);\r\n });\r\n\r\n debugLog('startCommandProcessor: Started successfully');\r\n console.log(`[agent-msg] Command processor started for ${agentId}`);\r\n\r\n return processor;\r\n } catch (err) {\r\n debugLog('startCommandProcessor: Failed to start', { error: err instanceof Error ? err.message : String(err) });\r\n console.error('[agent-msg] Failed to start command processor:', err);\r\n return null;\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Provider Configuration\r\n// ============================================================================\r\n\r\n/**\r\n * Supported provider types for API routing\r\n */\r\ntype ProviderType = 'anthropic' | 'zai' | 'kimi' | 'openrouter';\r\n\r\n/**\r\n * Check if custom routing (non-Anthropic) is enabled\r\n *\r\n * BUG FIX: Now checks both CLAUDE_API_PROVIDER (legacy) and PROVIDER (from --provider flag)\r\n */\r\nasync function isCustomRoutingEnabled(): Promise<boolean> {\r\n // Check environment variables - support both conventions\r\n const envProvider = process.env.CLAUDE_API_PROVIDER || process.env.PROVIDER;\r\n\r\n if (envProvider && envProvider !== 'anthropic') {\r\n debugLog('isCustomRoutingEnabled: Custom provider from env', { provider: envProvider });\r\n return true;\r\n }\r\n\r\n // Check config file (.claude/config/api-provider.json)\r\n try {\r\n const configPath = path.join(projectRoot, '.claude', 'config', 'api-provider.json');\r\n const config = JSON.parse(await fs.readFile(configPath, 'utf-8'));\r\n const isCustom = config.provider && config.provider !== 'anthropic';\r\n debugLog('isCustomRoutingEnabled: Config file result', { provider: config.provider, isCustom });\r\n return isCustom;\r\n } catch {\r\n return false;\r\n }\r\n}\r\n\r\n/**\r\n * Get API provider configuration\r\n *\r\n * Resolution order:\r\n * 1. CLAUDE_API_PROVIDER env var (legacy)\r\n * 2. PROVIDER env var (from CLI --provider flag)\r\n * 3. Config file\r\n * 4. Default to Z.ai (cost-effective fallback)\r\n *\r\n * BUG FIX: Previously only returned 'anthropic' | 'zai', now supports all providers\r\n * BUG FIX: Previously defaulted to Anthropic, now defaults to Z.ai\r\n */\r\nasync function getAPIProvider(): Promise<ProviderType> {\r\n // Check environment variables\r\n const envProvider = process.env.CLAUDE_API_PROVIDER || process.env.PROVIDER;\r\n\r\n if (envProvider) {\r\n const normalized = envProvider.toLowerCase() as ProviderType;\r\n if (['anthropic', 'zai', 'z.ai', 'kimi', 'openrouter'].includes(normalized)) {\r\n const result = normalized === 'z.ai' ? 'zai' : normalized;\r\n debugLog('getAPIProvider: Resolved from env', { envProvider, result });\r\n console.error(`[agent-executor] Provider from env: ${result}`);\r\n return result as ProviderType;\r\n }\r\n }\r\n\r\n // Check config file\r\n try {\r\n const configPath = path.join(projectRoot, '.claude', 'config', 'api-provider.json');\r\n const config = JSON.parse(await fs.readFile(configPath, 'utf-8'));\r\n if (config.provider) {\r\n const normalized = config.provider.toLowerCase();\r\n if (['anthropic', 'zai', 'z.ai', 'kimi', 'openrouter'].includes(normalized)) {\r\n const result = normalized === 'z.ai' ? 'zai' : normalized;\r\n debugLog('getAPIProvider: Resolved from config', { configProvider: config.provider, result });\r\n console.error(`[agent-executor] Provider from config: ${result}`);\r\n return result as ProviderType;\r\n }\r\n }\r\n } catch {\r\n // Config file doesn't exist, use default\r\n }\r\n\r\n // Default to Z.ai (cost-effective fallback)\r\n debugLog('getAPIProvider: Using default', { result: 'zai' });\r\n console.error('[agent-executor] Provider defaulting to: zai');\r\n return 'zai';\r\n}\r\n\r\n/**\r\n * Execute agent using direct API calls\r\n */\r\nasync function executeViaAPI(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n debugLog('executeViaAPI: FUNCTION ENTRY', { agentType: definition.name });\r\n console.error('[DEBUG] executeViaAPI: FUNCTION ENTRY');\r\n const agentId = getAgentId(definition, context);\r\n\r\n debugLog('executeViaAPI: Agent initialized', {\r\n agentId,\r\n taskId: context.taskId,\r\n iteration: context.iteration,\r\n hasContext: !!context.context\r\n });\r\n console.error(`[DEBUG] executeViaAPI: Generated agentId=${agentId}`);\r\n console.error(`[DEBUG] executeViaAPI: context.taskId=${context.taskId}, context.iteration=${context.iteration}`);\r\n\r\n console.log(`[agent-executor] Executing agent via API: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log('');\r\n\r\n // BUG #24 FIX: Parse and inject context environment variables\r\n // This enables CLI-spawned agents to access TASK_ID, MODE, etc. in Bash tool\r\n if (context.context) {\r\n console.log(`[agent-executor] Parsing context: ${context.context}`);\r\n const contextEnv = parseContextToEnv(context.context);\r\n console.log(`[agent-executor] Injected env vars: ${Object.keys(contextEnv).join(', ')}`);\r\n }\r\n\r\n // v2.0: Initialize runtime state for bidirectional messaging\r\n const runtimeState: AgentRuntimeState = {\r\n paused: false,\r\n abortRequested: false\r\n };\r\n\r\n // v2.0: Start command processor for Main Chat communication\r\n let commandProcessor: AgentCommandProcessor | null = null;\r\n if (context.taskId) {\r\n commandProcessor = startCommandProcessor(context.taskId, agentId, runtimeState);\r\n }\r\n\r\n try {\r\n // Check for conversation fork (Sprint 4 enhancement)\r\n const forkId = process.env.FORK_ID || await getCurrentFork(context.taskId || '', agentId);\r\n const iteration = context.iteration || 1;\r\n\r\n let systemPrompt: string;\r\n let messages: Array<{role: string, content: string}> = [];\r\n\r\n if (forkId && iteration > 1) {\r\n // Continue from fork (iterations 2+)\r\n console.log(`[agent-executor] Continuing from fork: ${forkId}`);\r\n\r\n // Load fork messages\r\n const forkMessages = await loadMessages(context.taskId || '', agentId, forkId);\r\n console.log(`[agent-executor] Loaded ${forkMessages.length} messages from fork`);\r\n\r\n // Extract system prompt from first message (it's always the system message)\r\n // The fork messages are assistant/user pairs, we need to add system separately\r\n systemPrompt = forkMessages[0]?.content || '';\r\n\r\n // Format remaining messages for API\r\n messages = formatMessagesForAPI(forkMessages.slice(1));\r\n\r\n // Add new user message with feedback\r\n messages.push({\r\n role: 'user',\r\n content: prompt\r\n });\r\n\r\n console.log(`[agent-executor] Fork continuation: ${messages.length} messages`);\r\n } else {\r\n // New conversation (iteration 1)\r\n console.log('[agent-executor] Starting new conversation');\r\n console.log('[agent-executor] Building system prompt with context...');\r\n\r\n const contextOptions = loadContextFromEnv();\r\n contextOptions.agentType = definition.name;\r\n if (context.taskId) contextOptions.taskId = context.taskId;\r\n if (context.iteration) contextOptions.iteration = context.iteration;\r\n\r\n systemPrompt = await buildCLIAgentSystemPrompt(contextOptions);\r\n console.log('[agent-executor] System prompt built successfully');\r\n\r\n // Initial user message\r\n messages = [{\r\n role: 'user',\r\n content: prompt\r\n }];\r\n }\r\n\r\n console.log('');\r\n\r\n // Dynamic import to avoid bundling issues\r\n const { executeAgentAPI } = await import('./anthropic-client.js');\r\n\r\n // Convert agent tool names to Anthropic API format\r\n debugLog('[TOOL DEBUG] definition.tools check', {\r\n hasTools: !!definition.tools,\r\n toolsLength: definition.tools?.length || 0,\r\n toolNames: definition.tools || []\r\n });\r\n console.error(`[TOOL DEBUG] definition.tools: ${JSON.stringify(definition.tools)}`);\r\n\r\n const tools = definition.tools && definition.tools.length > 0\r\n ? convertToolNames(definition.tools)\r\n : undefined;\r\n\r\n debugLog('[TOOL DEBUG] converted tools', {\r\n hasConvertedTools: !!tools,\r\n convertedToolsCount: tools?.length || 0,\r\n convertedToolNames: tools?.map(t => t.name) || []\r\n });\r\n console.error(`[TOOL DEBUG] Converted tools: ${tools?.map(t => t.name).join(', ') || 'NONE'}`);\r\n\r\n const result = await executeAgentAPI(\r\n definition.name,\r\n agentId,\r\n definition.model,\r\n prompt,\r\n systemPrompt,\r\n messages.length > 1 ? messages : undefined,\r\n undefined, // maxTokens (use default)\r\n tools // Pass converted tools\r\n );\r\n\r\n // Store messages in conversation history (for future forking)\r\n if (context.taskId) {\r\n // Store user message\r\n const userMessage: Message = {\r\n role: 'user',\r\n content: prompt,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, userMessage);\r\n\r\n // Store assistant response\r\n if (result.output) {\r\n const assistantMessage: Message = {\r\n role: 'assistant',\r\n content: result.output,\r\n iteration,\r\n timestamp: new Date().toISOString()\r\n };\r\n await storeMessage(context.taskId, agentId, assistantMessage);\r\n }\r\n\r\n console.log(`[agent-executor] Stored messages for iteration ${iteration}`);\r\n\r\n // Execute CFN Loop protocol (signal completion, report confidence, enter waiting mode)\r\n // Iterations are enabled for CFN Loop tasks (indicated by presence of taskId)\r\n debugLog('agent-executor: About to execute CFN Protocol', {\r\n taskId: context.taskId,\r\n agentId,\r\n iteration,\r\n outputLength: result.output.length\r\n });\r\n console.error('[DEBUG] agent-executor: About to execute CFN Protocol...');\r\n console.error(`[DEBUG] agent-executor: taskId=${context.taskId}, agentId=${agentId}, iteration=${iteration}`);\r\n try {\r\n const maxIterations = 10; // Default max iterations\r\n const enableIterations = true; // Enable iterations for all CFN Loop tasks\r\n\r\n debugLog('agent-executor: Calling executeCFNProtocol', { maxIterations, enableIterations });\r\n console.error('[DEBUG] agent-executor: Calling executeCFNProtocol...');\r\n await executeCFNProtocol(\r\n context.taskId,\r\n agentId,\r\n result.output,\r\n iteration,\r\n enableIterations,\r\n maxIterations\r\n );\r\n debugLog('agent-executor: ✓ executeCFNProtocol returned successfully');\r\n console.error('[DEBUG] agent-executor: ✓ executeCFNProtocol returned successfully');\r\n } catch (error) {\r\n const errorDetails = error instanceof Error ? {\r\n message: error.message,\r\n stack: error.stack\r\n } : { error: String(error) };\r\n debugLog('agent-executor: ✗ executeCFNProtocol threw error', errorDetails);\r\n console.error('[DEBUG] agent-executor: ✗ executeCFNProtocol threw error:', error);\r\n console.error('[agent-executor] CFN Protocol execution failed:', error);\r\n // Don't fail the entire agent execution if CFN protocol fails\r\n // This allows agents to complete even if Redis coordination has issues\r\n }\r\n console.error('[DEBUG] agent-executor: CFN Protocol section complete');\r\n }\r\n\r\n return {\r\n success: result.success,\r\n agentId,\r\n output: result.output,\r\n error: result.error,\r\n exitCode: result.success ? 0 : 1,\r\n };\r\n } catch (error) {\r\n console.error('[agent-executor] API execution failed:', error);\r\n return {\r\n success: false,\r\n agentId,\r\n error: error instanceof Error ? error.message : String(error),\r\n exitCode: 1,\r\n };\r\n } finally {\r\n // v2.0: Clean up command processor\r\n if (commandProcessor) {\r\n debugLog('executeViaAPI: Stopping command processor');\r\n console.log('[agent-msg] Stopping command processor');\r\n await commandProcessor.stop();\r\n }\r\n }\r\n}\r\n\r\n/**\r\n * Execute agent using shell script (fallback/simulation)\r\n */\r\nasync function executeViaScript(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext\r\n): Promise<AgentExecutionResult> {\r\n const agentId = getAgentId(definition, context);\r\n\r\n // BUG #24 FIX: Parse and inject context environment variables\r\n if (context.context) {\r\n console.log(`[agent-executor] Parsing context: ${context.context}`);\r\n const contextEnv = parseContextToEnv(context.context);\r\n console.log(`[agent-executor] Injected env vars: ${Object.keys(contextEnv).join(', ')}`);\r\n }\r\n\r\n // Write prompt to temporary file\r\n const tmpDir = os.tmpdir();\r\n const promptFile = path.join(tmpDir, `agent-${agentId}-${Date.now()}.md`);\r\n await fs.writeFile(promptFile, prompt, 'utf-8');\r\n\r\n console.log(`[agent-executor] Executing agent via script: ${definition.name}`);\r\n console.log(`[agent-executor] Agent ID: ${agentId}`);\r\n console.log(`[agent-executor] Model: ${definition.model}`);\r\n console.log(`[agent-executor] Prompt file: ${promptFile}`);\r\n\r\n return new Promise((resolve) => {\r\n const scriptPath = path.join(projectRoot, '.claude', 'skills', 'agent-execution', 'execute-agent.sh');\r\n\r\n // Build environment variables - WHITELIST ONLY APPROACH\r\n // SECURITY FIX: Do not use ...process.env spread which exposes ALL variables including secrets\r\n // Instead, explicitly whitelist safe variables to pass to spawned process\r\n const safeEnvVars = [\r\n 'CFN_REDIS_HOST',\r\n 'CFN_REDIS_PORT',\r\n 'CFN_REDIS_PASSWORD', // CRITICAL: Required for Redis authentication\r\n 'CFN_REDIS_URL',\r\n 'REDIS_PASSWORD', // Fallback for Redis password\r\n 'CFN_MEMORY_BUDGET',\r\n 'CFN_API_HOST',\r\n 'CFN_API_PORT',\r\n 'CFN_LOG_LEVEL',\r\n 'CFN_LOG_FORMAT',\r\n 'CFN_CONTAINER_MODE',\r\n 'CFN_DOCKER_SOCKET',\r\n 'CFN_NETWORK_NAME',\r\n 'CFN_CUSTOM_ROUTING',\r\n 'CFN_DEFAULT_PROVIDER',\r\n 'NODE_ENV',\r\n 'PATH',\r\n 'HOME',\r\n 'PWD' // Required for working directory context\r\n ];\r\n\r\n const env: Record<string, string> = {};\r\n\r\n // Add whitelisted variables\r\n for (const key of safeEnvVars) {\r\n const value = process.env[key];\r\n if (value !== undefined) {\r\n env[key] = value;\r\n }\r\n }\r\n\r\n // Add API key only when explicitly needed (with strict validation)\r\n if (process.env.ANTHROPIC_API_KEY) {\r\n if (process.env.ANTHROPIC_API_KEY.match(/^sk-[a-zA-Z0-9-]+$/)) {\r\n env.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;\r\n }\r\n }\r\n\r\n // Add agent execution context (safe values)\r\n env.AGENT_TYPE = definition.name;\r\n env.AGENT_ID = agentId;\r\n env.AGENT_MODEL = definition.model;\r\n env.AGENT_TOOLS = definition.tools.join(',');\r\n env.TASK_ID = context.taskId || '';\r\n env.ITERATION = String(context.iteration || 1);\r\n env.MODE = context.mode || 'cli';\r\n env.PROMPT_FILE = promptFile;\r\n\r\n // Check if execute script exists\r\n fs.access(scriptPath)\r\n .then(() => {\r\n // Use execution script\r\n const proc = spawn('bash', [scriptPath], { env, stdio: 'inherit' });\r\n\r\n proc.on('exit', (code) => {\r\n resolve({\r\n success: code === 0,\r\n agentId,\r\n exitCode: code || 0,\r\n });\r\n });\r\n\r\n proc.on('error', (err) => {\r\n resolve({\r\n success: false,\r\n agentId,\r\n error: err.message,\r\n exitCode: 1,\r\n });\r\n });\r\n })\r\n .catch(() => {\r\n // Fallback: Print prompt\r\n console.log('\\n=== Agent Prompt ===');\r\n console.log(prompt.substring(0, 500) + '...');\r\n console.log('\\n[agent-executor] Execution script not found');\r\n console.log('[agent-executor] Using simulation mode\\n');\r\n\r\n resolve({\r\n success: true,\r\n agentId,\r\n output: prompt,\r\n exitCode: 0,\r\n });\r\n });\r\n });\r\n}\r\n\r\n/**\r\n * Main agent execution function\r\n */\r\nexport async function executeAgent(\r\n definition: AgentDefinition,\r\n prompt: string,\r\n context: TaskContext,\r\n options: {\r\n method?: 'auto' | 'api' | 'script';\r\n } = {}\r\n): Promise<AgentExecutionResult> {\r\n const method = options.method || 'auto';\r\n\r\n debugLog('executeAgent: Starting execution', {\r\n agentType: definition.name,\r\n method,\r\n taskId: context.taskId,\r\n iteration: context.iteration,\r\n agentId: context.agentId || 'not-set'\r\n });\r\n\r\n // Auto-select execution method\r\n if (method === 'auto') {\r\n // Try API execution first, fallback to script if API key not available\r\n try {\r\n debugLog('executeAgent: Attempting API execution');\r\n return await executeViaAPI(definition, prompt, context);\r\n } catch (error) {\r\n if (error instanceof Error && error.message.includes('API key not found')) {\r\n debugLog('executeAgent: API key not found, falling back to script');\r\n console.log('[agent-executor] API key not found, using script fallback');\r\n return executeViaScript(definition, prompt, context);\r\n }\r\n debugLog('executeAgent: API execution error', { error: error instanceof Error ? error.message : String(error) });\r\n throw error;\r\n }\r\n }\r\n\r\n if (method === 'api') {\r\n debugLog('executeAgent: Using API method');\r\n return executeViaAPI(definition, prompt, context);\r\n }\r\n\r\n debugLog('executeAgent: Using script method');\r\n return executeViaScript(definition, prompt, context);\r\n}\r\n\r\n/**\r\n * Write agent output to file for debugging\r\n */\r\nexport async function saveAgentOutput(\r\n agentId: string,\r\n output: string,\r\n outputDir: string = '.claude/tmp/agent-output'\r\n): Promise<string> {\r\n await fs.mkdir(outputDir, { recursive: true });\r\n\r\n const timestamp = new Date().toISOString().replace(/[:.]/g, '-');\r\n const filename = `${agentId}-${timestamp}.txt`;\r\n const filepath = path.join(outputDir, filename);\r\n\r\n await fs.writeFile(filepath, output, 'utf-8');\r\n\r\n return filepath;\r\n}\r\n\r\n/**\r\n * Main entry point when run as a script via tsx\r\n */\r\nasync function main() {\r\n // Parse command line arguments\r\n const args = process.argv.slice(2);\r\n let agentType: string | undefined;\r\n\r\n for (let i = 0; i < args.length; i++) {\r\n if (args[i] === '--agent-type' && i + 1 < args.length) {\r\n agentType = args[i + 1];\r\n break;\r\n }\r\n }\r\n\r\n if (!agentType) {\r\n console.error('[agent-executor] ERROR: --agent-type is required');\r\n process.exit(1);\r\n }\r\n\r\n // Load agent definition\r\n const { parseAgentDefinition } = await import('./agent-definition-parser.js');\r\n const definition = await parseAgentDefinition(agentType);\r\n\r\n if (!definition) {\r\n console.error(`[agent-executor] ERROR: Agent type not found: ${agentType}`);\r\n process.exit(1);\r\n }\r\n\r\n // Build task context from environment variables\r\n const context: TaskContext = {\r\n taskId: process.env.TASK_ID,\r\n iteration: process.env.ITERATION ? parseInt(process.env.ITERATION, 10) : 1,\r\n mode: process.env.MODE || 'cli',\r\n agentId: process.env.AGENT_ID,\r\n context: process.env.CONTEXT\r\n };\r\n\r\n // Get prompt from environment variable or use default\r\n const prompt = process.env.PROMPT || `Execute your assigned task for ${agentType}.\r\n\r\nYou are part of a CFN Loop workflow. Review any broadcast messages, complete your work, and report your confidence score.`;\r\n\r\n console.log(`[agent-executor] Starting agent: ${agentType}`);\r\n console.log(`[agent-executor] Task ID: ${context.taskId || 'none'}`);\r\n console.log(`[agent-executor] Iteration: ${context.iteration}`);\r\n console.log(`[agent-executor] Prompt source: ${process.env.PROMPT ? 'PROMPT env var' : 'default'}`);\r\n\r\n // Execute agent\r\n const result = await executeAgent(definition, prompt, context);\r\n\r\n // Exit with appropriate code\r\n if (!result.success) {\r\n console.error(`[agent-executor] Agent execution failed: ${result.error || 'unknown error'}`);\r\n process.exit(result.exitCode || 1);\r\n }\r\n\r\n console.log('[agent-executor] Agent execution completed successfully');\r\n process.exit(0);\r\n}\r\n\r\n// Run main if executed as a script\r\nif (import.meta.url === `file://${process.argv[1]}`) {\r\n main().catch((error) => {\r\n console.error('[agent-executor] Fatal error:', error);\r\n process.exit(1);\r\n });\r\n}\r\n"],"names":["spawn","exec","promisify","createClient","getAgentId","buildCLIAgentSystemPrompt","loadContextFromEnv","loadMessages","storeMessage","getCurrentFork","formatMessagesForAPI","convertToolNames","AgentCommandProcessor","fs","fsSync","path","os","execSync","execAsync","AGENT_ID","process","env","LOG_FILE","debugLog","message","data","timestamp","Date","toISOString","logEntry","JSON","stringify","appendFileSync","err","getProjectRoot","PROJECT_ROOT","gitRoot","encoding","cwd","stdio","trim","projectRoot","redisHost","CFN_REDIS_HOST","redisPort","CFN_REDIS_PORT","redisPassword","CFN_REDIS_PASSWORD","validateTaskId","taskId","test","Error","validateAgentId","agentId","createRedisClient","host","port","hasPassword","console","error","portNum","parseInt","client","socket","reconnectStrategy","retries","Math","min","connectTimeout","password","undefined","on","stack","connect","parseContextToEnv","contextString","envVars","regex","match","key","quotedValue","unquotedValue","value","extractConfidence","output","patterns","pattern","score","parseFloat","executeCFNProtocol","iteration","enableIterations","maxIterations","log","redisClient","orchestratorKey","lPush","mainChatKey","confidence","agentMetadata","status","metadata","errorDetails","name","String","quit","startCommandProcessor","state","processor","onCommand","command","payload","newTask","redirectContext","lastCommandTime","currentStep","redirectedTo","_command","paused","abortRequested","start","catch","isCustomRoutingEnabled","envProvider","CLAUDE_API_PROVIDER","PROVIDER","provider","configPath","join","config","parse","readFile","isCustom","getAPIProvider","normalized","toLowerCase","includes","result","configProvider","executeViaAPI","definition","prompt","context","agentType","hasContext","model","contextEnv","Object","keys","runtimeState","commandProcessor","forkId","FORK_ID","systemPrompt","messages","forkMessages","length","content","slice","push","role","contextOptions","executeAgentAPI","hasTools","tools","toolsLength","toolNames","hasConvertedTools","convertedToolsCount","convertedToolNames","map","t","userMessage","assistantMessage","outputLength","success","exitCode","stop","executeViaScript","tmpDir","tmpdir","promptFile","now","writeFile","Promise","resolve","scriptPath","safeEnvVars","ANTHROPIC_API_KEY","AGENT_TYPE","AGENT_MODEL","AGENT_TOOLS","TASK_ID","ITERATION","MODE","mode","PROMPT_FILE","access","then","proc","code","substring","executeAgent","options","method","saveAgentOutput","outputDir","mkdir","recursive","replace","filename","filepath","main","args","argv","i","exit","parseAgentDefinition","CONTEXT","PROMPT","url"],"mappings":"AAAA;;;;;;;CAOC,GAED,SAASA,KAAK,QAAQ,gBAAgB;AACtC,SAASC,IAAI,QAAQ,gBAAgB;AACrC,SAASC,SAAS,QAAQ,OAAO;AACjC,SAASC,YAAY,QAAyB,QAAQ;AAEtD,SAAsBC,UAAU,QAAQ,4BAA4B;AACpE,SAASC,yBAAyB,EAAEC,kBAAkB,QAAQ,yBAAyB;AACvF,SACEC,YAAY,EACZC,YAAY,EACZC,cAAc,EACdC,oBAAoB,QAEf,yBAAyB;AAChC,SAASC,gBAAgB,QAAQ,wBAAwB;AACzD,SAASC,qBAAqB,QAA2B,oCAAoC;AAC7F,OAAOC,QAAQ,cAAc;AAC7B,OAAOC,YAAY,KAAK;AACxB,OAAOC,UAAU,OAAO;AACxB,OAAOC,QAAQ,KAAK;AACpB,SAASC,QAAQ,QAAQ,gBAAgB;AAEzC,MAAMC,YAAYhB,UAAUD;AAE5B,iFAAiF;AACjF,MAAMkB,WAAWC,QAAQC,GAAG,CAACF,QAAQ,IAAI;AACzC,MAAMG,WAAW,CAAC,eAAe,EAAEH,SAAS,IAAI,CAAC;AACjD,SAASI,SAASC,OAAe,EAAEC,IAAU;IAC3C,MAAMC,YAAY,IAAIC,OAAOC,WAAW;IACxC,MAAMC,WAAWJ,OACb,GAAGC,UAAU,EAAE,EAAEP,SAAS,EAAE,EAAEK,QAAQ,CAAC,EAAEM,KAAKC,SAAS,CAACN,MAAM,EAAE,CAAC,GACjE,GAAGC,UAAU,EAAE,EAAEP,SAAS,EAAE,EAAEK,QAAQ,EAAE,CAAC;IAC7C,IAAI;QACFV,OAAOkB,cAAc,CAACV,UAAUO;IAClC,EAAE,OAAOI,KAAK;IACZ,wBAAwB;IAC1B;AACF;AAEAV,SAAS;AAET;;;CAGC,GACD,SAASW;IACP,gCA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ACnDF,OAAO3F,KAAK,CAAC,CAAC9D;QACZD,QAAQC,KAAK,CAAC,iCAAiCA;QAC/CvC,QAAQoM,IAAI,CAAC;IACf;AACF"}
@@ -11,69 +11,96 @@
11
11
  */ import { loadIterationHistory, formatIterationHistory } from './iteration-history.js';
12
12
  import { SkillLoader } from './skill-loader.js';
13
13
  /**
14
- * Build CFN Loop protocol instructions
15
- */ function buildCFNLoopProtocol(taskId, agentId) {
14
+ * Build CLI Mode Redis Completion Protocol for CLI Mode
15
+ */ function buildCLIModeProtocol(taskId, agentId) {
16
16
  return `
17
- ## CFN Loop Redis Completion Protocol
17
+ ## CLI Mode Redis Completion Protocol
18
18
 
19
- You are participating in a CFN Loop workflow. Follow this protocol EXACTLY:
19
+ You are running in CLI Mode with Main Chat coordination. Follow this protocol EXACTLY:
20
20
 
21
21
  ### Step 1: Complete Your Work
22
22
  Execute your assigned task (implementation, review, testing, etc.)
23
23
 
24
- ### Step 2: Calculate Confidence Score
25
- Assess your work quality and assign a confidence score (0.0-1.0):
26
- - 0.90-1.0: Excellent, production-ready
27
- - 0.75-0.89: Good, minor issues possible
28
- - 0.50-0.74: Acceptable, needs review
29
- - 0.0-0.49: Poor, significant issues
24
+ ### Step 2: Signal Completion to Main Chat
25
+ Send a Redis signal to notify Main Chat that you're finished:
30
26
 
31
- ### Step 3: Report Completion to Redis
32
27
  \`\`\`bash
33
- ./.claude/skills/cfn-redis-coordination/report-completion.sh \\
34
- --task-id "${taskId}" \\
35
- --agent-id "${agentId}" \\
36
- --confidence [YOUR_CONFIDENCE_SCORE] \\
37
- --iteration \${ITERATION:-1}
38
- \`\`\`
28
+ # Use Node.js for Redis communication
29
+ node -e "
30
+ const { createClient } = require('redis');
31
+ const signal = {
32
+ agentId: '${agentId}',
33
+ taskId: '${taskId}',
34
+ status: 'completed',
35
+ timestamp: new Date().toISOString(),
36
+ provider: process.env.PROVIDER || 'unknown',
37
+ model: process.env.MODEL || 'unknown',
38
+ confidence: 0.90, // Replace with your actual confidence
39
+ metadata: {
40
+ iteration: process.env.ITERATION || 1,
41
+ mode: process.env.MODE || 'standard'
42
+ }
43
+ };
44
+
45
+ (async () => {
46
+ const client = createClient({ url: 'redis://localhost:6379' });
47
+ await client.connect();
39
48
 
40
- This script automatically:
41
- - Signals completion via \`swarm:${taskId}:${agentId}:done\`
42
- - Stores confidence in \`swarm:${taskId}:${agentId}:confidence\`
43
- - Creates result hash in \`swarm:${taskId}:${agentId}:result\`
44
- - Updates SQLite persistence layer
49
+ const signalKey = \`cfn:cli:\${process.env.TASK_ID}:completion\`;
50
+ await client.lPush(signalKey, JSON.stringify(signal));
51
+
52
+ console.log(\`✅ Completion signal sent to Main Chat via Redis\`);
53
+ await client.disconnect();
54
+ })();
55
+ "
56
+ \`\`\`
45
57
 
46
- ### Step 4: Exit Cleanly
47
- After reporting, exit immediately. DO NOT enter waiting mode.
58
+ ### Step 3: Exit Cleanly
59
+ After sending the signal, exit immediately. Main Chat is waiting for your Redis signal.
48
60
 
49
- The orchestrator will:
50
- - Collect confidence scores from all agents
51
- - Run gate check (≥0.75 threshold)
52
- - Spawn validators if gate passes
53
- - Spawn fresh agents for iteration N+1 if needed
61
+ **Why This Protocol:**
62
+ - Main Chat uses Redis BLPOP to wait for your completion signal
63
+ - Enables simple 2-layer coordination (Main Chat → CLI agents)
64
+ - No complex orchestrator needed for CLI mode
65
+ - Supports different AI providers via environment variables
54
66
 
55
67
  **Environment Variables Available:**
56
68
  - TASK_ID: ${taskId}
57
69
  - AGENT_ID: ${agentId}
58
- - ITERATION: Current iteration number (default: 1)
59
- - CONFIDENCE_SCORE: Your final confidence assessment
70
+ - PROVIDER: AI provider (zai, kimi, anthropic, etc.)
71
+ - MODEL: Specific model being used
72
+ - ITERATION: Current iteration number
73
+ - MODE: Execution mode (mvp, standard, enterprise)
60
74
 
61
- **Why This Matters:**
62
- - Enables zero-token coordination (orchestrator uses Redis BLPOP)
63
- - Supports adaptive agent specialization (spawn different specialist for iteration N+1)
64
- - Prevents memory leaks (agents exit after reporting)
65
- - Confidence scores drive gate checks and consensus validation
75
+ **Main Chat Workflow:**
76
+ 1. Spawns you via CLI with specific provider/model
77
+ 2. Waits via \`redis-cli BLPOP cfn:cli:${taskId}:completion\`
78
+ 3. Processes your completion signal when received
79
+ 4. Continues with next task or spawns additional agents
66
80
 
67
- **CRITICAL:** Report completion before exiting. Orchestrator is waiting for your signal.
81
+ **CRITICAL:** Send Redis signal before exiting. Main Chat cannot proceed without your completion signal.
68
82
  `;
69
83
  }
84
+ /**
85
+ * Parse shell variable format into JSON object
86
+ * Example: "WORKSPACE='/tmp/test' MODE='standard'" -> {WORKSPACE: '/tmp/test', MODE: 'standard'}
87
+ */ function parseShellVariables(shellContext) {
88
+ const jsonObj = {};
89
+ const regex = /([A-Z_]+)='([^']*)'/g;
90
+ let match;
91
+ while((match = regex.exec(shellContext)) !== null){
92
+ jsonObj[match[1]] = match[2];
93
+ }
94
+ return jsonObj;
95
+ }
70
96
  /**
71
97
  * Parse and enrich JSON context into natural language instructions
72
98
  */ function enrichJSONContext(jsonObj) {
73
99
  const sections = [];
74
- // Extract task description
75
- if (jsonObj.task) {
76
- sections.push(`**Task:** ${jsonObj.task}`);
100
+ // Extract task description (support both 'task' and 'taskDescription' keys)
101
+ const taskText = jsonObj.task || jsonObj.taskDescription;
102
+ if (taskText) {
103
+ sections.push(`**Task:** ${taskText}`);
77
104
  }
78
105
  // Parse files - convert comma-separated string to bullet list
79
106
  if (jsonObj.files) {
@@ -102,9 +129,10 @@ The orchestrator will:
102
129
  if (jsonObj.batch) {
103
130
  sections.push(`\n**Batch:** ${jsonObj.batch}`);
104
131
  }
105
- // Add directory context
106
- if (jsonObj.directory) {
107
- sections.push(`\n**Working Directory:** ${jsonObj.directory}`);
132
+ // Add directory context - support 'directory', 'WORKSPACE', and 'workspace' keys
133
+ const workspacePath = jsonObj.directory || jsonObj.WORKSPACE || jsonObj.workspace;
134
+ if (workspacePath) {
135
+ sections.push(`\n**Working Directory:** ${workspacePath}`);
108
136
  }
109
137
  // Add acceptance criteria
110
138
  if (jsonObj.acceptanceCriteria) {
@@ -129,10 +157,17 @@ The orchestrator will:
129
157
  */ function buildTaskDescription(agentType, context) {
130
158
  let desc = '';
131
159
  if (context.context) {
132
- // Try to parse as JSON first
160
+ // Try to parse context in multiple formats
133
161
  let contextStr = context.context.trim();
134
- // Check if context looks like JSON
135
- if (contextStr.startsWith('{') && contextStr.endsWith('}') || contextStr.startsWith('[') && contextStr.endsWith(']')) {
162
+ // Parse shell variables BEFORE attempting JSON parse
163
+ if (contextStr.includes('=') && !contextStr.startsWith('{')) {
164
+ const jsonObj = parseShellVariables(contextStr);
165
+ desc = enrichJSONContext(jsonObj);
166
+ // Add instruction footer for structured tasks
167
+ if (jsonObj.files || jsonObj.deliverables) {
168
+ desc += '\n\n**Process each item systematically and report confidence when complete.**';
169
+ }
170
+ } else if (contextStr.startsWith('{') && contextStr.endsWith('}') || contextStr.startsWith('[') && contextStr.endsWith(']')) {
136
171
  try {
137
172
  const jsonObj = JSON.parse(contextStr);
138
173
  desc = enrichJSONContext(jsonObj);
@@ -333,9 +368,9 @@ ${env.join('\n')}
333
368
  console.warn(`[agent-prompt-builder] Skill loading failed: ${skillError}`);
334
369
  // Continue without skills
335
370
  }
336
- // 5. CFN Loop protocol (ALWAYS inject when taskId present - enables Redis coordination)
371
+ // 5. CLI Mode protocol (ALWAYS inject when taskId present - enables Redis coordination)
337
372
  if (context.taskId && agentId) {
338
- sections.push(buildCFNLoopProtocol(context.taskId, agentId));
373
+ sections.push(buildCLIModeProtocol(context.taskId, agentId));
339
374
  sections.push('');
340
375
  }
341
376
  // 6. Environment context
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/cli/agent-prompt-builder.ts"],"sourcesContent":["/**\r\n * Agent Prompt Builder\r\n *\r\n * Builds comprehensive prompts for CLI-spawned agents by combining:\r\n * - Agent definition (YAML + markdown)\r\n * - Task context (taskId, iteration, mode)\r\n * - CFN Loop protocol (if applicable)\r\n * - Iteration history (Sprint 3 - Phase 2)\r\n * - Environment variables\r\n * - Skills (Phase 5 - Skills Database integration)\r\n */\r\n\r\nimport { AgentDefinition, hasCFNLoopProtocol } from './agent-definition-parser.js';\r\nimport { loadIterationHistory, formatIterationHistory } from './iteration-history.js';\r\nimport { SkillLoader, Skill } from './skill-loader.js';\r\n\r\nexport interface TaskContext {\r\n taskId?: string;\r\n iteration?: number;\r\n context?: string;\r\n mode?: string;\r\n priority?: number;\r\n parentTaskId?: string;\r\n agentId?: string;\r\n keywords?: string; // Phase 5: For skill filtering\r\n phase?: string; // Phase 5: CFN Loop phase (loop1, loop2, loop3)\r\n}\r\n\r\n/**\r\n * Build CFN Loop protocol instructions\r\n */\r\nfunction buildCFNLoopProtocol(taskId: string, agentId: string): string {\r\n return `\r\n## CFN Loop Redis Completion Protocol\r\n\r\nYou are participating in a CFN Loop workflow. Follow this protocol EXACTLY:\r\n\r\n### Step 1: Complete Your Work\r\nExecute your assigned task (implementation, review, testing, etc.)\r\n\r\n### Step 2: Calculate Confidence Score\r\nAssess your work quality and assign a confidence score (0.0-1.0):\r\n- 0.90-1.0: Excellent, production-ready\r\n- 0.75-0.89: Good, minor issues possible\r\n- 0.50-0.74: Acceptable, needs review\r\n- 0.0-0.49: Poor, significant issues\r\n\r\n### Step 3: Report Completion to Redis\r\n\\`\\`\\`bash\r\n./.claude/skills/cfn-redis-coordination/report-completion.sh \\\\\r\n --task-id \"${taskId}\" \\\\\r\n --agent-id \"${agentId}\" \\\\\r\n --confidence [YOUR_CONFIDENCE_SCORE] \\\\\r\n --iteration \\${ITERATION:-1}\r\n\\`\\`\\`\r\n\r\nThis script automatically:\r\n- Signals completion via \\`swarm:${taskId}:${agentId}:done\\`\r\n- Stores confidence in \\`swarm:${taskId}:${agentId}:confidence\\`\r\n- Creates result hash in \\`swarm:${taskId}:${agentId}:result\\`\r\n- Updates SQLite persistence layer\r\n\r\n### Step 4: Exit Cleanly\r\nAfter reporting, exit immediately. DO NOT enter waiting mode.\r\n\r\nThe orchestrator will:\r\n- Collect confidence scores from all agents\r\n- Run gate check (≥0.75 threshold)\r\n- Spawn validators if gate passes\r\n- Spawn fresh agents for iteration N+1 if needed\r\n\r\n**Environment Variables Available:**\r\n- TASK_ID: ${taskId}\r\n- AGENT_ID: ${agentId}\r\n- ITERATION: Current iteration number (default: 1)\r\n- CONFIDENCE_SCORE: Your final confidence assessment\r\n\r\n**Why This Matters:**\r\n- Enables zero-token coordination (orchestrator uses Redis BLPOP)\r\n- Supports adaptive agent specialization (spawn different specialist for iteration N+1)\r\n- Prevents memory leaks (agents exit after reporting)\r\n- Confidence scores drive gate checks and consensus validation\r\n\r\n**CRITICAL:** Report completion before exiting. Orchestrator is waiting for your signal.\r\n`;\r\n}\r\n\r\n/**\r\n * Parse and enrich JSON context into natural language instructions\r\n */\r\nfunction enrichJSONContext(jsonObj: any): string {\r\n const sections: string[] = [];\r\n\r\n // Extract task description\r\n if (jsonObj.task) {\r\n sections.push(`**Task:** ${jsonObj.task}`);\r\n }\r\n\r\n // Parse files - convert comma-separated string to bullet list\r\n if (jsonObj.files) {\r\n const fileList = typeof jsonObj.files === 'string'\r\n ? jsonObj.files.split(',').map(f => f.trim()).filter(f => f)\r\n : Array.isArray(jsonObj.files) ? jsonObj.files : [];\r\n\r\n if (fileList.length > 0) {\r\n sections.push('\\n**Files to process:**');\r\n fileList.forEach(file => sections.push(`- ${file}`));\r\n }\r\n }\r\n\r\n // Add requirements/deliverables\r\n if (jsonObj.requirements) {\r\n const reqs = Array.isArray(jsonObj.requirements) ? jsonObj.requirements : [jsonObj.requirements];\r\n sections.push('\\n**Requirements:**');\r\n reqs.forEach((req, i) => sections.push(`${i + 1}. ${req}`));\r\n }\r\n\r\n if (jsonObj.deliverables) {\r\n const delivs = Array.isArray(jsonObj.deliverables) ? jsonObj.deliverables : [jsonObj.deliverables];\r\n sections.push('\\n**Deliverables:**');\r\n delivs.forEach(deliv => sections.push(`- ${deliv}`));\r\n }\r\n\r\n // Add batch information\r\n if (jsonObj.batch) {\r\n sections.push(`\\n**Batch:** ${jsonObj.batch}`);\r\n }\r\n\r\n // Add directory context\r\n if (jsonObj.directory) {\r\n sections.push(`\\n**Working Directory:** ${jsonObj.directory}`);\r\n }\r\n\r\n // Add acceptance criteria\r\n if (jsonObj.acceptanceCriteria) {\r\n const criteria = Array.isArray(jsonObj.acceptanceCriteria)\r\n ? jsonObj.acceptanceCriteria\r\n : [jsonObj.acceptanceCriteria];\r\n sections.push('\\n**Acceptance Criteria:**');\r\n criteria.forEach(criterion => sections.push(`- ${criterion}`));\r\n }\r\n\r\n // Add explicit instructions if present\r\n if (jsonObj.instructions) {\r\n sections.push('\\n**Instructions:**');\r\n const instrs = Array.isArray(jsonObj.instructions) ? jsonObj.instructions : [jsonObj.instructions];\r\n instrs.forEach((instr, i) => sections.push(`${i + 1}. ${instr}`));\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Build task description from context\r\n */\r\nfunction buildTaskDescription(agentType: string, context: TaskContext): string {\r\n let desc = '';\r\n\r\n if (context.context) {\r\n // Try to parse as JSON first\r\n let contextStr = context.context.trim();\r\n\r\n // Check if context looks like JSON\r\n if ((contextStr.startsWith('{') && contextStr.endsWith('}')) ||\r\n (contextStr.startsWith('[') && contextStr.endsWith(']'))) {\r\n try {\r\n const jsonObj = JSON.parse(contextStr);\r\n desc = enrichJSONContext(jsonObj);\r\n\r\n // Add instruction footer for structured tasks\r\n if (jsonObj.files || jsonObj.deliverables) {\r\n desc += '\\n\\n**Process each item systematically and report confidence when complete.**';\r\n }\r\n } catch (e) {\r\n // Not valid JSON, treat as plain text\r\n desc = context.context;\r\n }\r\n } else {\r\n // Plain text context\r\n desc = context.context;\r\n }\r\n } else {\r\n desc = `Execute task as ${agentType} agent`;\r\n }\r\n\r\n // Add metadata fields\r\n if (context.taskId) {\r\n desc += `\\n\\n**Task ID:** ${context.taskId}`;\r\n }\r\n\r\n if (context.iteration) {\r\n desc += `\\n**Iteration:** ${context.iteration}`;\r\n }\r\n\r\n if (context.mode) {\r\n desc += `\\n**Mode:** ${context.mode}`;\r\n }\r\n\r\n if (context.priority) {\r\n desc += `\\n**Priority:** ${context.priority}`;\r\n }\r\n\r\n if (context.parentTaskId) {\r\n desc += `\\n**Parent Task:** ${context.parentTaskId}`;\r\n }\r\n\r\n return desc;\r\n}\r\n\r\n/**\r\n * Build environment context section\r\n */\r\nfunction buildEnvironmentContext(context: TaskContext): string {\r\n const env: string[] = [];\r\n\r\n if (context.taskId) env.push(`TASK_ID=${context.taskId}`);\r\n if (context.iteration) env.push(`ITERATION=${context.iteration}`);\r\n if (context.mode) env.push(`MODE=${context.mode}`);\r\n if (context.priority) env.push(`PRIORITY=${context.priority}`);\r\n if (context.parentTaskId) env.push(`PARENT_TASK_ID=${context.parentTaskId}`);\r\n\r\n // Docker workspace detection\r\n const isDockerEnv = process.env.DOCKER_AGENT === 'true' || process.env.WORKSPACE_ROOT;\r\n const workspaceRoot = process.env.WORKSPACE_ROOT || '/workspace';\r\n\r\n if (isDockerEnv) {\r\n env.push(`WORKSPACE_ROOT=${workspaceRoot}`);\r\n }\r\n\r\n // Always include Docker context if detected, even if env array is empty\r\n if (isDockerEnv && env.length === 0) {\r\n env.push(`WORKSPACE_ROOT=${workspaceRoot}`);\r\n }\r\n\r\n if (env.length === 0 && !isDockerEnv) return '';\r\n\r\n let contextText = `\r\n## Environment Variables\r\n\r\n\\`\\`\\`bash\r\n${env.join('\\n')}\r\n\\`\\`\\`\r\n`;\r\n\r\n // Add Docker workspace notice if detected\r\n if (isDockerEnv) {\r\n contextText += `\r\n\r\n## Docker Container Environment\r\n\r\n**CRITICAL:** You are running inside a Docker container.\r\n\r\n- **Working Directory:** \\`${workspaceRoot}\\`\r\n- **File Paths:** All file operations use \\`${workspaceRoot}/\\` prefix\r\n- **Example:** To read \\`src/file.ts\\`, use \\`${workspaceRoot}/src/file.ts\\`\r\n\r\n**DO NOT** use paths from your training data or Main Chat context. Use \\`${workspaceRoot}/\\` for all file operations.\r\n`;\r\n }\r\n\r\n return contextText;\r\n}\r\n\r\n/**\r\n * Load skills for agent (Phase 5: Skills Database Integration)\r\n */\r\nasync function loadSkillsForAgent(\r\n agentType: string,\r\n context: TaskContext\r\n): Promise<Skill[]> {\r\n // Feature flag check\r\n if (process.env.CFN_SKILLS_DATABASE !== 'true') {\r\n return [];\r\n }\r\n\r\n try {\r\n const dbPath = process.env.CFN_SKILLS_DB_PATH || './.claude/skills-database/skills.db';\r\n const skillLoader = new SkillLoader(dbPath, {\r\n enableCache: true,\r\n cacheMaxSize: 100,\r\n cacheTTL: 60000 // 1 minute\r\n });\r\n\r\n // Extract keywords from context for filtering\r\n const keywords = context.keywords || context.context?.toLowerCase() || '';\r\n\r\n const skills = await skillLoader.loadSkillsForAgent(agentType, {\r\n taskId: context.taskId,\r\n keywords,\r\n phase: context.phase,\r\n mode: context.mode,\r\n iteration: context.iteration\r\n });\r\n\r\n skillLoader.close();\r\n return skills;\r\n } catch (error) {\r\n console.warn(`[agent-prompt-builder] Failed to load skills: ${error}`);\r\n return [];\r\n }\r\n}\r\n\r\n/**\r\n * Format skills for prompt injection\r\n */\r\nfunction formatSkillsForPrompt(skills: Skill[]): string {\r\n if (skills.length === 0) {\r\n return '';\r\n }\r\n\r\n const sections: string[] = [];\r\n\r\n sections.push('## Applicable Skills');\r\n sections.push('');\r\n sections.push('The following skills have been loaded based on your agent type and task context:');\r\n sections.push('');\r\n\r\n for (const skill of skills) {\r\n const approvalBadge = skill.approvalLevel === 'auto' ? '✓' :\r\n skill.approvalLevel === 'escalate' ? '⚠' : '✋';\r\n sections.push(`### ${skill.name} (v${skill.version}) [${approvalBadge} ${skill.approvalLevel}]`);\r\n sections.push('');\r\n if (skill.content) {\r\n sections.push(skill.content);\r\n } else {\r\n sections.push(`*Skill content not available*`);\r\n }\r\n sections.push('');\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Build complete prompt for agent execution (async for iteration history + skills)\r\n */\r\nexport async function buildAgentPrompt(\r\n definition: AgentDefinition,\r\n context: TaskContext\r\n): Promise<string> {\r\n // Use explicit agent ID if provided, otherwise generate from name + iteration\r\n const agentId = context.agentId || `${definition.name}-${context.iteration || 1}`;\r\n const startTime = Date.now();\r\n\r\n const sections: string[] = [];\r\n\r\n // 1. Agent definition header\r\n sections.push(`# Agent: ${definition.name}`);\r\n sections.push('');\r\n sections.push(definition.description);\r\n sections.push('');\r\n\r\n // 2. Task description\r\n sections.push('## Task');\r\n sections.push('');\r\n sections.push(buildTaskDescription(definition.name, context));\r\n sections.push('');\r\n\r\n // 3. Iteration history (Sprint 3 - Phase 2)\r\n // Load and format previous iterations if iteration > 1\r\n if (context.taskId && context.iteration && context.iteration > 1) {\r\n try {\r\n const history = await loadIterationHistory(context.taskId, agentId, context.iteration);\r\n const historyText = formatIterationHistory(history, context.iteration);\r\n sections.push(historyText);\r\n sections.push('');\r\n } catch (err) {\r\n console.warn(`[agent-prompt-builder] Failed to load iteration history:`, err);\r\n // Continue without history\r\n }\r\n }\r\n\r\n // 4. Agent definition content (from markdown file)\r\n sections.push('## Agent Definition');\r\n sections.push('');\r\n sections.push(definition.content);\r\n sections.push('');\r\n\r\n // 4a. Load and inject skills (Phase 5: Skills Database Integration)\r\n try {\r\n const skills = await loadSkillsForAgent(definition.type || definition.name, context);\r\n\r\n if (skills.length > 0) {\r\n const skillsText = formatSkillsForPrompt(skills);\r\n sections.push(skillsText);\r\n sections.push('');\r\n\r\n // Log skill usage for analytics (Phase 5)\r\n if (process.env.CFN_SKILLS_DATABASE === 'true' && context.taskId) {\r\n try {\r\n const dbPath = process.env.CFN_SKILLS_DB_PATH || './.claude/skills-database/skills.db';\r\n const skillLoader = new SkillLoader(dbPath);\r\n\r\n await skillLoader.logSkillUsage({\r\n agentId: agentId,\r\n agentType: definition.type || definition.name,\r\n skillIds: skills.map(s => s.id).filter(id => id > 0), // Exclude bootstrap skills (negative IDs)\r\n taskId: context.taskId,\r\n phase: context.phase,\r\n loadedAt: new Date(),\r\n executionTimeMs: Date.now() - startTime\r\n });\r\n\r\n skillLoader.close();\r\n } catch (logError) {\r\n console.warn(`[agent-prompt-builder] Failed to log skill usage: ${logError}`);\r\n }\r\n }\r\n }\r\n } catch (skillError) {\r\n console.warn(`[agent-prompt-builder] Skill loading failed: ${skillError}`);\r\n // Continue without skills\r\n }\r\n\r\n // 5. CFN Loop protocol (ALWAYS inject when taskId present - enables Redis coordination)\r\n if (context.taskId && agentId) {\r\n sections.push(buildCFNLoopProtocol(context.taskId, agentId));\r\n sections.push('');\r\n }\r\n\r\n // 6. Environment context\r\n const envContext = buildEnvironmentContext(context);\r\n if (envContext) {\r\n sections.push(envContext);\r\n sections.push('');\r\n }\r\n\r\n // 7. Execution instructions\r\n sections.push('## Execution Instructions');\r\n sections.push('');\r\n sections.push('1. Read and understand the task requirements');\r\n if (context.iteration && context.iteration > 1) {\r\n sections.push('2. Review iteration history and feedback from validators');\r\n sections.push('3. Address specific feedback points from previous iteration');\r\n sections.push('4. Execute your core responsibilities as defined above');\r\n } else {\r\n sections.push('2. Execute your core responsibilities as defined above');\r\n sections.push('3. Follow any protocol steps (CFN Loop, validation hooks, etc.)');\r\n }\r\n sections.push('4. Provide clear, concise output');\r\n sections.push('5. Report confidence score if applicable');\r\n sections.push('');\r\n\r\n // 7a. Pre-edit backup requirement (MANDATORY)\r\n sections.push('## Pre-Edit Backup Protocol (MANDATORY)');\r\n sections.push('');\r\n sections.push('**BEFORE ANY Edit/Write/MultiEdit operation, you MUST create a backup:**');\r\n sections.push('');\r\n sections.push('```bash');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"$FILE_TO_EDIT\" --agent-id \"${agentId}\")`);\r\n sections.push('```');\r\n sections.push('');\r\n sections.push('**Why:** Enables safe file revert without git operations during parallel sessions.');\r\n sections.push('**Location:** `.backups/[agent-id]/[timestamp]_[hash]/`');\r\n sections.push('**Retention:** 24h TTL (configurable)');\r\n sections.push('');\r\n sections.push('**Complete Edit Workflow:**');\r\n sections.push('```bash');\r\n sections.push('# 1. Pre-Edit: Create backup');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"src/file.ts\" --agent-id \"${agentId}\")`);\r\n sections.push('');\r\n sections.push('# 2. Edit: Perform file modification');\r\n sections.push('Edit: file_path=\"src/file.ts\" old_string=\"...\" new_string=\"...\"');\r\n sections.push('');\r\n sections.push('# 3. Post-Edit: Validate changes');\r\n sections.push(`./.claude/hooks/cfn-invoke-post-edit.sh \"src/file.ts\" --agent-id \"${agentId}\"`);\r\n sections.push('```');\r\n sections.push('');\r\n\r\n // 8. Tool reminder\r\n if (definition.tools && definition.tools.length > 0) {\r\n sections.push('## Available Tools');\r\n sections.push('');\r\n sections.push(`You have access to: ${definition.tools.join(', ')}`);\r\n sections.push('');\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Extract agent ID from context\r\n * If agentId is explicitly provided in context, use it; otherwise generate from name + iteration\r\n */\r\nexport function getAgentId(definition: AgentDefinition, context: TaskContext): string {\r\n if (context.agentId) {\r\n return context.agentId;\r\n }\r\n return `${definition.name}-${context.iteration || 1}`;\r\n}\r\n\r\n/**\r\n * Build system prompt for agent (optional, for structured agent behavior)\r\n */\r\nexport function buildSystemPrompt(definition: AgentDefinition): string {\r\n return `You are ${definition.name}, a specialized AI agent.\r\n\r\nType: ${definition.type || 'specialist'}\r\nModel: ${definition.model}\r\nTools: ${definition.tools.join(', ')}\r\n\r\nFollow your agent definition exactly and complete assigned tasks with high quality.`;\r\n}\r\n"],"names":["loadIterationHistory","formatIterationHistory","SkillLoader","buildCFNLoopProtocol","taskId","agentId","enrichJSONContext","jsonObj","sections","task","push","files","fileList","split","map","f","trim","filter","Array","isArray","length","forEach","file","requirements","reqs","req","i","deliverables","delivs","deliv","batch","directory","acceptanceCriteria","criteria","criterion","instructions","instrs","instr","join","buildTaskDescription","agentType","context","desc","contextStr","startsWith","endsWith","JSON","parse","e","iteration","mode","priority","parentTaskId","buildEnvironmentContext","env","isDockerEnv","process","DOCKER_AGENT","WORKSPACE_ROOT","workspaceRoot","contextText","loadSkillsForAgent","CFN_SKILLS_DATABASE","dbPath","CFN_SKILLS_DB_PATH","skillLoader","enableCache","cacheMaxSize","cacheTTL","keywords","toLowerCase","skills","phase","close","error","console","warn","formatSkillsForPrompt","skill","approvalBadge","approvalLevel","name","version","content","buildAgentPrompt","definition","startTime","Date","now","description","history","historyText","err","type","skillsText","logSkillUsage","skillIds","s","id","loadedAt","executionTimeMs","logError","skillError","envContext","tools","getAgentId","buildSystemPrompt","model"],"mappings":"AAAA;;;;;;;;;;CAUC,GAGD,SAASA,oBAAoB,EAAEC,sBAAsB,QAAQ,yBAAyB;AACtF,SAASC,WAAW,QAAe,oBAAoB;AAcvD;;CAEC,GACD,SAASC,qBAAqBC,MAAc,EAAEC,OAAe;IAC3D,OAAO,CAAC;;;;;;;;;;;;;;;;;;aAkBG,EAAED,OAAO;cACR,EAAEC,QAAQ;;;;;;iCAMS,EAAED,OAAO,CAAC,EAAEC,QAAQ;+BACtB,EAAED,OAAO,CAAC,EAAEC,QAAQ;iCAClB,EAAED,OAAO,CAAC,EAAEC,QAAQ;;;;;;;;;;;;;WAa1C,EAAED,OAAO;YACR,EAAEC,QAAQ;;;;;;;;;;;AAWtB,CAAC;AACD;AAEA;;CAEC,GACD,SAASC,kBAAkBC,OAAY;IACrC,MAAMC,WAAqB,EAAE;IAE7B,2BAA2B;IAC3B,IAAID,QAAQE,IAAI,EAAE;QAChBD,SAASE,IAAI,CAAC,CAAC,UAAU,EAAEH,QAAQE,IAAI,EAAE;IAC3C;IAEA,8DAA8D;IAC9D,IAAIF,QAAQI,KAAK,EAAE;QACjB,MAAMC,WAAW,OAAOL,QAAQI,KAAK,KAAK,WACtCJ,QAAQI,KAAK,CAACE,KAAK,CAAC,KAAKC,GAAG,CAACC,CAAAA,IAAKA,EAAEC,IAAI,IAAIC,MAAM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AI,CAAC,MAAM;;mFAE8C,CAAC;AACpF"}
1
+ {"version":3,"sources":["../../src/cli/agent-prompt-builder.ts"],"sourcesContent":["/**\r\n * Agent Prompt Builder\r\n *\r\n * Builds comprehensive prompts for CLI-spawned agents by combining:\r\n * - Agent definition (YAML + markdown)\r\n * - Task context (taskId, iteration, mode)\r\n * - CFN Loop protocol (if applicable)\r\n * - Iteration history (Sprint 3 - Phase 2)\r\n * - Environment variables\r\n * - Skills (Phase 5 - Skills Database integration)\r\n */\r\n\r\nimport { AgentDefinition, hasCFNLoopProtocol } from './agent-definition-parser.js';\r\nimport { loadIterationHistory, formatIterationHistory } from './iteration-history.js';\r\nimport { SkillLoader, Skill } from './skill-loader.js';\r\n\r\nexport interface TaskContext {\r\n taskId?: string;\r\n iteration?: number;\r\n context?: string;\r\n mode?: string;\r\n priority?: number;\r\n parentTaskId?: string;\r\n agentId?: string;\r\n keywords?: string; // Phase 5: For skill filtering\r\n phase?: string; // Phase 5: CFN Loop phase (loop1, loop2, loop3)\r\n}\r\n\r\n/**\r\n * Build CLI Mode Redis Completion Protocol for CLI Mode\r\n */\r\nfunction buildCLIModeProtocol(taskId: string, agentId: string): string {\r\n return `\r\n## CLI Mode Redis Completion Protocol\r\n\r\nYou are running in CLI Mode with Main Chat coordination. Follow this protocol EXACTLY:\r\n\r\n### Step 1: Complete Your Work\r\nExecute your assigned task (implementation, review, testing, etc.)\r\n\r\n### Step 2: Signal Completion to Main Chat\r\nSend a Redis signal to notify Main Chat that you're finished:\r\n\r\n\\`\\`\\`bash\r\n# Use Node.js for Redis communication\r\nnode -e \"\r\nconst { createClient } = require('redis');\r\nconst signal = {\r\n agentId: '${agentId}',\r\n taskId: '${taskId}',\r\n status: 'completed',\r\n timestamp: new Date().toISOString(),\r\n provider: process.env.PROVIDER || 'unknown',\r\n model: process.env.MODEL || 'unknown',\r\n confidence: 0.90, // Replace with your actual confidence\r\n metadata: {\r\n iteration: process.env.ITERATION || 1,\r\n mode: process.env.MODE || 'standard'\r\n }\r\n};\r\n\r\n(async () => {\r\n const client = createClient({ url: 'redis://localhost:6379' });\r\n await client.connect();\r\n\r\n const signalKey = \\`cfn:cli:\\${process.env.TASK_ID}:completion\\`;\r\n await client.lPush(signalKey, JSON.stringify(signal));\r\n\r\n console.log(\\`✅ Completion signal sent to Main Chat via Redis\\`);\r\n await client.disconnect();\r\n})();\r\n\"\r\n\\`\\`\\`\r\n\r\n### Step 3: Exit Cleanly\r\nAfter sending the signal, exit immediately. Main Chat is waiting for your Redis signal.\r\n\r\n**Why This Protocol:**\r\n- Main Chat uses Redis BLPOP to wait for your completion signal\r\n- Enables simple 2-layer coordination (Main Chat → CLI agents)\r\n- No complex orchestrator needed for CLI mode\r\n- Supports different AI providers via environment variables\r\n\r\n**Environment Variables Available:**\r\n- TASK_ID: ${taskId}\r\n- AGENT_ID: ${agentId}\r\n- PROVIDER: AI provider (zai, kimi, anthropic, etc.)\r\n- MODEL: Specific model being used\r\n- ITERATION: Current iteration number\r\n- MODE: Execution mode (mvp, standard, enterprise)\r\n\r\n**Main Chat Workflow:**\r\n1. Spawns you via CLI with specific provider/model\r\n2. Waits via \\`redis-cli BLPOP cfn:cli:${taskId}:completion\\`\r\n3. Processes your completion signal when received\r\n4. Continues with next task or spawns additional agents\r\n\r\n**CRITICAL:** Send Redis signal before exiting. Main Chat cannot proceed without your completion signal.\r\n`;\r\n}\r\n\r\n/**\r\n * Parse shell variable format into JSON object\r\n * Example: \"WORKSPACE='/tmp/test' MODE='standard'\" -> {WORKSPACE: '/tmp/test', MODE: 'standard'}\r\n */\r\nfunction parseShellVariables(shellContext: string): any {\r\n const jsonObj: any = {};\r\n const regex = /([A-Z_]+)='([^']*)'/g;\r\n let match;\r\n while ((match = regex.exec(shellContext)) !== null) {\r\n jsonObj[match[1]] = match[2];\r\n }\r\n return jsonObj;\r\n}\r\n\r\n/**\r\n * Parse and enrich JSON context into natural language instructions\r\n */\r\nfunction enrichJSONContext(jsonObj: any): string {\r\n const sections: string[] = [];\r\n\r\n // Extract task description (support both 'task' and 'taskDescription' keys)\r\n const taskText = jsonObj.task || jsonObj.taskDescription;\r\n if (taskText) {\r\n sections.push(`**Task:** ${taskText}`);\r\n }\r\n\r\n // Parse files - convert comma-separated string to bullet list\r\n if (jsonObj.files) {\r\n const fileList = typeof jsonObj.files === 'string'\r\n ? jsonObj.files.split(',').map(f => f.trim()).filter(f => f)\r\n : Array.isArray(jsonObj.files) ? jsonObj.files : [];\r\n\r\n if (fileList.length > 0) {\r\n sections.push('\\n**Files to process:**');\r\n fileList.forEach(file => sections.push(`- ${file}`));\r\n }\r\n }\r\n\r\n // Add requirements/deliverables\r\n if (jsonObj.requirements) {\r\n const reqs = Array.isArray(jsonObj.requirements) ? jsonObj.requirements : [jsonObj.requirements];\r\n sections.push('\\n**Requirements:**');\r\n reqs.forEach((req, i) => sections.push(`${i + 1}. ${req}`));\r\n }\r\n\r\n if (jsonObj.deliverables) {\r\n const delivs = Array.isArray(jsonObj.deliverables) ? jsonObj.deliverables : [jsonObj.deliverables];\r\n sections.push('\\n**Deliverables:**');\r\n delivs.forEach(deliv => sections.push(`- ${deliv}`));\r\n }\r\n\r\n // Add batch information\r\n if (jsonObj.batch) {\r\n sections.push(`\\n**Batch:** ${jsonObj.batch}`);\r\n }\r\n\r\n // Add directory context - support 'directory', 'WORKSPACE', and 'workspace' keys\r\n const workspacePath = jsonObj.directory || jsonObj.WORKSPACE || jsonObj.workspace;\r\n if (workspacePath) {\r\n sections.push(`\\n**Working Directory:** ${workspacePath}`);\r\n }\r\n\r\n // Add acceptance criteria\r\n if (jsonObj.acceptanceCriteria) {\r\n const criteria = Array.isArray(jsonObj.acceptanceCriteria)\r\n ? jsonObj.acceptanceCriteria\r\n : [jsonObj.acceptanceCriteria];\r\n sections.push('\\n**Acceptance Criteria:**');\r\n criteria.forEach(criterion => sections.push(`- ${criterion}`));\r\n }\r\n\r\n // Add explicit instructions if present\r\n if (jsonObj.instructions) {\r\n sections.push('\\n**Instructions:**');\r\n const instrs = Array.isArray(jsonObj.instructions) ? jsonObj.instructions : [jsonObj.instructions];\r\n instrs.forEach((instr, i) => sections.push(`${i + 1}. ${instr}`));\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Build task description from context\r\n */\r\nfunction buildTaskDescription(agentType: string, context: TaskContext): string {\r\n let desc = '';\r\n\r\n if (context.context) {\r\n // Try to parse context in multiple formats\r\n let contextStr = context.context.trim();\r\n\r\n // Parse shell variables BEFORE attempting JSON parse\r\n if (contextStr.includes('=') && !contextStr.startsWith('{')) {\r\n const jsonObj = parseShellVariables(contextStr);\r\n desc = enrichJSONContext(jsonObj);\r\n\r\n // Add instruction footer for structured tasks\r\n if (jsonObj.files || jsonObj.deliverables) {\r\n desc += '\\n\\n**Process each item systematically and report confidence when complete.**';\r\n }\r\n }\r\n // Check if context looks like JSON\r\n else if ((contextStr.startsWith('{') && contextStr.endsWith('}')) ||\r\n (contextStr.startsWith('[') && contextStr.endsWith(']'))) {\r\n try {\r\n const jsonObj = JSON.parse(contextStr);\r\n desc = enrichJSONContext(jsonObj);\r\n\r\n // Add instruction footer for structured tasks\r\n if (jsonObj.files || jsonObj.deliverables) {\r\n desc += '\\n\\n**Process each item systematically and report confidence when complete.**';\r\n }\r\n } catch (e) {\r\n // Not valid JSON, treat as plain text\r\n desc = context.context;\r\n }\r\n } else {\r\n // Plain text context\r\n desc = context.context;\r\n }\r\n } else {\r\n desc = `Execute task as ${agentType} agent`;\r\n }\r\n\r\n // Add metadata fields\r\n if (context.taskId) {\r\n desc += `\\n\\n**Task ID:** ${context.taskId}`;\r\n }\r\n\r\n if (context.iteration) {\r\n desc += `\\n**Iteration:** ${context.iteration}`;\r\n }\r\n\r\n if (context.mode) {\r\n desc += `\\n**Mode:** ${context.mode}`;\r\n }\r\n\r\n if (context.priority) {\r\n desc += `\\n**Priority:** ${context.priority}`;\r\n }\r\n\r\n if (context.parentTaskId) {\r\n desc += `\\n**Parent Task:** ${context.parentTaskId}`;\r\n }\r\n\r\n return desc;\r\n}\r\n\r\n/**\r\n * Build environment context section\r\n */\r\nfunction buildEnvironmentContext(context: TaskContext): string {\r\n const env: string[] = [];\r\n\r\n if (context.taskId) env.push(`TASK_ID=${context.taskId}`);\r\n if (context.iteration) env.push(`ITERATION=${context.iteration}`);\r\n if (context.mode) env.push(`MODE=${context.mode}`);\r\n if (context.priority) env.push(`PRIORITY=${context.priority}`);\r\n if (context.parentTaskId) env.push(`PARENT_TASK_ID=${context.parentTaskId}`);\r\n\r\n // Docker workspace detection\r\n const isDockerEnv = process.env.DOCKER_AGENT === 'true' || process.env.WORKSPACE_ROOT;\r\n const workspaceRoot = process.env.WORKSPACE_ROOT || '/workspace';\r\n\r\n if (isDockerEnv) {\r\n env.push(`WORKSPACE_ROOT=${workspaceRoot}`);\r\n }\r\n\r\n // Always include Docker context if detected, even if env array is empty\r\n if (isDockerEnv && env.length === 0) {\r\n env.push(`WORKSPACE_ROOT=${workspaceRoot}`);\r\n }\r\n\r\n if (env.length === 0 && !isDockerEnv) return '';\r\n\r\n let contextText = `\r\n## Environment Variables\r\n\r\n\\`\\`\\`bash\r\n${env.join('\\n')}\r\n\\`\\`\\`\r\n`;\r\n\r\n // Add Docker workspace notice if detected\r\n if (isDockerEnv) {\r\n contextText += `\r\n\r\n## Docker Container Environment\r\n\r\n**CRITICAL:** You are running inside a Docker container.\r\n\r\n- **Working Directory:** \\`${workspaceRoot}\\`\r\n- **File Paths:** All file operations use \\`${workspaceRoot}/\\` prefix\r\n- **Example:** To read \\`src/file.ts\\`, use \\`${workspaceRoot}/src/file.ts\\`\r\n\r\n**DO NOT** use paths from your training data or Main Chat context. Use \\`${workspaceRoot}/\\` for all file operations.\r\n`;\r\n }\r\n\r\n return contextText;\r\n}\r\n\r\n/**\r\n * Load skills for agent (Phase 5: Skills Database Integration)\r\n */\r\nasync function loadSkillsForAgent(\r\n agentType: string,\r\n context: TaskContext\r\n): Promise<Skill[]> {\r\n // Feature flag check\r\n if (process.env.CFN_SKILLS_DATABASE !== 'true') {\r\n return [];\r\n }\r\n\r\n try {\r\n const dbPath = process.env.CFN_SKILLS_DB_PATH || './.claude/skills-database/skills.db';\r\n const skillLoader = new SkillLoader(dbPath, {\r\n enableCache: true,\r\n cacheMaxSize: 100,\r\n cacheTTL: 60000 // 1 minute\r\n });\r\n\r\n // Extract keywords from context for filtering\r\n const keywords = context.keywords || context.context?.toLowerCase() || '';\r\n\r\n const skills = await skillLoader.loadSkillsForAgent(agentType, {\r\n taskId: context.taskId,\r\n keywords,\r\n phase: context.phase,\r\n mode: context.mode,\r\n iteration: context.iteration\r\n });\r\n\r\n skillLoader.close();\r\n return skills;\r\n } catch (error) {\r\n console.warn(`[agent-prompt-builder] Failed to load skills: ${error}`);\r\n return [];\r\n }\r\n}\r\n\r\n/**\r\n * Format skills for prompt injection\r\n */\r\nfunction formatSkillsForPrompt(skills: Skill[]): string {\r\n if (skills.length === 0) {\r\n return '';\r\n }\r\n\r\n const sections: string[] = [];\r\n\r\n sections.push('## Applicable Skills');\r\n sections.push('');\r\n sections.push('The following skills have been loaded based on your agent type and task context:');\r\n sections.push('');\r\n\r\n for (const skill of skills) {\r\n const approvalBadge = skill.approvalLevel === 'auto' ? '✓' :\r\n skill.approvalLevel === 'escalate' ? '⚠' : '✋';\r\n sections.push(`### ${skill.name} (v${skill.version}) [${approvalBadge} ${skill.approvalLevel}]`);\r\n sections.push('');\r\n if (skill.content) {\r\n sections.push(skill.content);\r\n } else {\r\n sections.push(`*Skill content not available*`);\r\n }\r\n sections.push('');\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Build complete prompt for agent execution (async for iteration history + skills)\r\n */\r\nexport async function buildAgentPrompt(\r\n definition: AgentDefinition,\r\n context: TaskContext\r\n): Promise<string> {\r\n // Use explicit agent ID if provided, otherwise generate from name + iteration\r\n const agentId = context.agentId || `${definition.name}-${context.iteration || 1}`;\r\n const startTime = Date.now();\r\n\r\n const sections: string[] = [];\r\n\r\n // 1. Agent definition header\r\n sections.push(`# Agent: ${definition.name}`);\r\n sections.push('');\r\n sections.push(definition.description);\r\n sections.push('');\r\n\r\n // 2. Task description\r\n sections.push('## Task');\r\n sections.push('');\r\n sections.push(buildTaskDescription(definition.name, context));\r\n sections.push('');\r\n\r\n // 3. Iteration history (Sprint 3 - Phase 2)\r\n // Load and format previous iterations if iteration > 1\r\n if (context.taskId && context.iteration && context.iteration > 1) {\r\n try {\r\n const history = await loadIterationHistory(context.taskId, agentId, context.iteration);\r\n const historyText = formatIterationHistory(history, context.iteration);\r\n sections.push(historyText);\r\n sections.push('');\r\n } catch (err) {\r\n console.warn(`[agent-prompt-builder] Failed to load iteration history:`, err);\r\n // Continue without history\r\n }\r\n }\r\n\r\n // 4. Agent definition content (from markdown file)\r\n sections.push('## Agent Definition');\r\n sections.push('');\r\n sections.push(definition.content);\r\n sections.push('');\r\n\r\n // 4a. Load and inject skills (Phase 5: Skills Database Integration)\r\n try {\r\n const skills = await loadSkillsForAgent(definition.type || definition.name, context);\r\n\r\n if (skills.length > 0) {\r\n const skillsText = formatSkillsForPrompt(skills);\r\n sections.push(skillsText);\r\n sections.push('');\r\n\r\n // Log skill usage for analytics (Phase 5)\r\n if (process.env.CFN_SKILLS_DATABASE === 'true' && context.taskId) {\r\n try {\r\n const dbPath = process.env.CFN_SKILLS_DB_PATH || './.claude/skills-database/skills.db';\r\n const skillLoader = new SkillLoader(dbPath);\r\n\r\n await skillLoader.logSkillUsage({\r\n agentId: agentId,\r\n agentType: definition.type || definition.name,\r\n skillIds: skills.map(s => s.id).filter(id => id > 0), // Exclude bootstrap skills (negative IDs)\r\n taskId: context.taskId,\r\n phase: context.phase,\r\n loadedAt: new Date(),\r\n executionTimeMs: Date.now() - startTime\r\n });\r\n\r\n skillLoader.close();\r\n } catch (logError) {\r\n console.warn(`[agent-prompt-builder] Failed to log skill usage: ${logError}`);\r\n }\r\n }\r\n }\r\n } catch (skillError) {\r\n console.warn(`[agent-prompt-builder] Skill loading failed: ${skillError}`);\r\n // Continue without skills\r\n }\r\n\r\n // 5. CLI Mode protocol (ALWAYS inject when taskId present - enables Redis coordination)\r\n if (context.taskId && agentId) {\r\n sections.push(buildCLIModeProtocol(context.taskId, agentId));\r\n sections.push('');\r\n }\r\n\r\n // 6. Environment context\r\n const envContext = buildEnvironmentContext(context);\r\n if (envContext) {\r\n sections.push(envContext);\r\n sections.push('');\r\n }\r\n\r\n // 7. Execution instructions\r\n sections.push('## Execution Instructions');\r\n sections.push('');\r\n sections.push('1. Read and understand the task requirements');\r\n if (context.iteration && context.iteration > 1) {\r\n sections.push('2. Review iteration history and feedback from validators');\r\n sections.push('3. Address specific feedback points from previous iteration');\r\n sections.push('4. Execute your core responsibilities as defined above');\r\n } else {\r\n sections.push('2. Execute your core responsibilities as defined above');\r\n sections.push('3. Follow any protocol steps (CFN Loop, validation hooks, etc.)');\r\n }\r\n sections.push('4. Provide clear, concise output');\r\n sections.push('5. Report confidence score if applicable');\r\n sections.push('');\r\n\r\n // 7a. Pre-edit backup requirement (MANDATORY)\r\n sections.push('## Pre-Edit Backup Protocol (MANDATORY)');\r\n sections.push('');\r\n sections.push('**BEFORE ANY Edit/Write/MultiEdit operation, you MUST create a backup:**');\r\n sections.push('');\r\n sections.push('```bash');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"$FILE_TO_EDIT\" --agent-id \"${agentId}\")`);\r\n sections.push('```');\r\n sections.push('');\r\n sections.push('**Why:** Enables safe file revert without git operations during parallel sessions.');\r\n sections.push('**Location:** `.backups/[agent-id]/[timestamp]_[hash]/`');\r\n sections.push('**Retention:** 24h TTL (configurable)');\r\n sections.push('');\r\n sections.push('**Complete Edit Workflow:**');\r\n sections.push('```bash');\r\n sections.push('# 1. Pre-Edit: Create backup');\r\n sections.push(`BACKUP_PATH=$(./.claude/hooks/cfn-invoke-pre-edit.sh \"src/file.ts\" --agent-id \"${agentId}\")`);\r\n sections.push('');\r\n sections.push('# 2. Edit: Perform file modification');\r\n sections.push('Edit: file_path=\"src/file.ts\" old_string=\"...\" new_string=\"...\"');\r\n sections.push('');\r\n sections.push('# 3. Post-Edit: Validate changes');\r\n sections.push(`./.claude/hooks/cfn-invoke-post-edit.sh \"src/file.ts\" --agent-id \"${agentId}\"`);\r\n sections.push('```');\r\n sections.push('');\r\n\r\n // 8. Tool reminder\r\n if (definition.tools && definition.tools.length > 0) {\r\n sections.push('## Available Tools');\r\n sections.push('');\r\n sections.push(`You have access to: ${definition.tools.join(', ')}`);\r\n sections.push('');\r\n }\r\n\r\n return sections.join('\\n');\r\n}\r\n\r\n/**\r\n * Extract agent ID from context\r\n * If agentId is explicitly provided in context, use it; otherwise generate from name + iteration\r\n */\r\nexport function getAgentId(definition: AgentDefinition, context: TaskContext): string {\r\n if (context.agentId) {\r\n return context.agentId;\r\n }\r\n return `${definition.name}-${context.iteration || 1}`;\r\n}\r\n\r\n/**\r\n * Build system prompt for agent (optional, for structured agent behavior)\r\n */\r\nexport function buildSystemPrompt(definition: AgentDefinition): string {\r\n return `You are ${definition.name}, a specialized AI agent.\r\n\r\nType: ${definition.type || 'specialist'}\r\nModel: ${definition.model}\r\nTools: ${definition.tools.join(', ')}\r\n\r\nFollow your agent definition exactly and complete assigned tasks with high quality.`;\r\n}\r\n"],"names":["loadIterationHistory","formatIterationHistory","SkillLoader","buildCLIModeProtocol","taskId","agentId","parseShellVariables","shellContext","jsonObj","regex","match","exec","enrichJSONContext","sections","taskText","task","taskDescription","push","files","fileList","split","map","f","trim","filter","Array","isArray","length","forEach","file","requirements","reqs","req","i","deliverables","delivs","deliv","batch","workspacePath","directory","WORKSPACE","workspace","acceptanceCriteria","criteria","criterion","instructions","instrs","instr","join","buildTaskDescription","agentType","context","desc","contextStr","includes","startsWith","endsWith","JSON","parse","e","iteration","mode","priority","parentTaskId","buildEnvironmentContext","env","isDockerEnv","process","DOCKER_AGENT","WORKSPACE_ROOT","workspaceRoot","contextText","loadSkillsForAgent","CFN_SKILLS_DATABASE","dbPath","CFN_SKILLS_DB_PATH","skillLoader","enableCache","cacheMaxSize","cacheTTL","keywords","toLowerCase","skills","phase","close","error","console","warn","formatSkillsForPrompt","skill","approvalBadge","approvalLevel","name","version","content","buildAgentPrompt","definition","startTime","Date","now","description","history","historyText","err","type","skillsText","logSkillUsage","skillIds","s","id","loadedAt","executionTimeMs","logError","skillError","envContext","tools","getAgentId","buildSystemPrompt","model"],"mappings":"AAAA;;;;;;;;;;CAUC,GAGD,SAASA,oBAAoB,EAAEC,sBAAsB,QAAQ,yBAAyB;AACtF,SAASC,WAAW,QAAe,oBAAoB;AAcvD;;CAEC,GACD,SAASC,qBAAqBC,MAAc,EAAEC,OAAe;IAC3D,OAAO,CAAC;;;;;;;;;;;;;;;;YAgBE,EAAEA,QAAQ;WACX,EAAED,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAmCT,EAAEA,OAAO;YACR,EAAEC,QAAQ;;;;;;;;uCAQiB,EAAED,OAAO;;;;;AAKhD,CAAC;AACD;AAEA;;;CAGC,GACD,SAASE,oBAAoBC,YAAoB;IAC/C,MAAMC,UAAe,CAAC;IACtB,MAAMC,QAAQ;IACd,IAAIC;IACJ,MAAO,AAACA,CAAAA,QAAQD,MAAME,IAAI,CAACJ,aAAY,MAAO,KAAM;QAClDC,OAAO,CAACE,KAAK,CAAC,EAAE,CAAC,GAAGA,KAAK,CAAC,EAAE;IAC9B;IACA,OAAOF;AACT;AAEA;;CAEC,GACD,SAASI,kBAAkBJ,OAAY;IACrC,MAAMK,WAAqB,EAAE;IAE7B,4EAA4E;IAC5E,MAAMC,WAAWN,QAAQO,IAAI,IAAIP,QAAQQ,eAAe;IACxD,IAAIF,UAAU;QACZD,SAASI,IAAI,CAAC,CAAC,UAAU,EAAEH,UAAU;IACvC;IAEA,8DAA8D;IAC9D,IAAIN,QAAQU,KAAK,EAAE;QACjB,MAAMC,WAAW,OAAOX,QAAQU,KAAK,KAAK,WACtCV,QAAQU,KA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EA;;;CAGC,GACD,OAAO,SAASoE,WAAWpB,UAA2B,EAAE7C,OAAoB;IAC1E,IAAIA,QAAQ9C,OAAO,EAAE;QACnB,OAAO8C,QAAQ9C,OAAO;IACxB;IACA,OAAO,GAAG2F,WAAWJ,IAAI,CAAC,CAAC,EAAEzC,QAAQS,SAAS,IAAI,GAAG;AACvD;AAEA;;CAEC,GACD,OAAO,SAASyD,kBAAkBrB,UAA2B;IAC3D,OAAO,CAAC,QAAQ,EAAEA,WAAWJ,IAAI,CAAC;;MAE9B,EAAEI,WAAWQ,IAAI,IAAI,aAAa;OACjC,EAAER,WAAWsB,KAAK,CAAC;OACnB,EAAEtB,WAAWmB,KAAK,CAACnE,IAAI,CAAC,MAAM;;mFAE8C,CAAC;AACpF"}
@@ -15,7 +15,8 @@
15
15
  * Validates all parameters that will be passed to execFileSync()
16
16
  */ /**
17
17
  * Validates taskId format to prevent command injection attacks
18
- * Pattern: alphanumeric, underscore, hyphen only, 1-64 chars
18
+ * Pattern: optional namespace prefix (e.g., "cli:", "task:"), alphanumeric, underscore, hyphen, dot (max 64 chars)
19
+ * Examples: "task-123", "cli:task-456", "orchestrator:batch-789"
19
20
  */ function validateTaskId(taskId) {
20
21
  if (typeof taskId !== 'string' || taskId.length === 0) {
21
22
  return {
@@ -23,11 +24,12 @@
23
24
  error: 'Task ID must be a non-empty string'
24
25
  };
25
26
  }
26
- const taskIdPattern = /^[a-zA-Z0-9_-]{1,64}$/;
27
+ // Allow optional namespace prefix (lowercase letters + colon), followed by alphanumeric/underscore/hyphen/dot
28
+ const taskIdPattern = /^([a-z]+:)?[a-zA-Z0-9_.-]{1,64}$/;
27
29
  if (!taskIdPattern.test(taskId)) {
28
30
  return {
29
31
  valid: false,
30
- error: 'Invalid task ID format - must contain only alphanumeric characters, underscores, and hyphens (max 64 chars)'
32
+ error: 'Invalid task ID format - must contain optional namespace prefix (e.g., "cli:") and alphanumeric characters, underscores, hyphens, or dots (max 64 chars)'
31
33
  };
32
34
  }
33
35
  return {
@@ -221,7 +223,8 @@
221
223
  if (taskId) {
222
224
  // SECURITY FIX (CVSS 8.9): Use safe parameter validation and execFileSync()
223
225
  // Prevent command injection by validating all parameters BEFORE execution
224
- const redisHost = process.env.CFN_REDIS_HOST || 'cfn-redis';
226
+ // FIX: Default to 'localhost' for CLI mode (host execution), not 'cfn-redis' (Docker)
227
+ const redisHost = process.env.CFN_REDIS_HOST || 'localhost';
225
228
  const redisPort = process.env.CFN_REDIS_PORT || '6379';
226
229
  // Validate Redis connection parameters
227
230
  const hostValidation = validateRedisHost(redisHost);