claude-flow-guidance-implementation 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/.agents/README.md +38 -0
  2. package/.agents/config.toml +298 -0
  3. package/.agents/skills/memory-management/SKILL.md +126 -0
  4. package/.agents/skills/memory-management/scripts/memory-backup.sh +16 -0
  5. package/.agents/skills/memory-management/scripts/memory-consolidate.sh +11 -0
  6. package/.agents/skills/security-audit/SKILL.md +135 -0
  7. package/.agents/skills/security-audit/scripts/cve-remediate.sh +16 -0
  8. package/.agents/skills/security-audit/scripts/security-scan.sh +33 -0
  9. package/.agents/skills/sparc-methodology/SKILL.md +118 -0
  10. package/.agents/skills/sparc-methodology/scripts/sparc-init.sh +21 -0
  11. package/.agents/skills/sparc-methodology/scripts/sparc-review.sh +18 -0
  12. package/.agents/skills/swarm-orchestration/SKILL.md +114 -0
  13. package/.agents/skills/swarm-orchestration/scripts/swarm-monitor.sh +8 -0
  14. package/.agents/skills/swarm-orchestration/scripts/swarm-start.sh +14 -0
  15. package/.claude/agents/analysis/analyze-code-quality.md +179 -0
  16. package/.claude/agents/analysis/code-analyzer.md +210 -0
  17. package/.claude/agents/analysis/code-review/analyze-code-quality.md +179 -0
  18. package/.claude/agents/architecture/system-design/arch-system-design.md +155 -0
  19. package/.claude/agents/consensus/README.md +253 -0
  20. package/.claude/agents/consensus/byzantine-coordinator.md +63 -0
  21. package/.claude/agents/consensus/crdt-synchronizer.md +997 -0
  22. package/.claude/agents/consensus/gossip-coordinator.md +63 -0
  23. package/.claude/agents/consensus/performance-benchmarker.md +851 -0
  24. package/.claude/agents/consensus/quorum-manager.md +823 -0
  25. package/.claude/agents/consensus/raft-manager.md +63 -0
  26. package/.claude/agents/consensus/security-manager.md +622 -0
  27. package/.claude/agents/core/coder.md +266 -0
  28. package/.claude/agents/core/planner.md +168 -0
  29. package/.claude/agents/core/researcher.md +190 -0
  30. package/.claude/agents/core/reviewer.md +326 -0
  31. package/.claude/agents/core/tester.md +319 -0
  32. package/.claude/agents/custom/test-long-runner.md +44 -0
  33. package/.claude/agents/data/ml/data-ml-model.md +193 -0
  34. package/.claude/agents/development/backend/dev-backend-api.md +142 -0
  35. package/.claude/agents/development/dev-backend-api.md +345 -0
  36. package/.claude/agents/devops/ci-cd/ops-cicd-github.md +164 -0
  37. package/.claude/agents/documentation/api-docs/docs-api-openapi.md +174 -0
  38. package/.claude/agents/dual-mode/README.md +94 -0
  39. package/.claude/agents/dual-mode/codex-coordinator.md +224 -0
  40. package/.claude/agents/dual-mode/codex-worker.md +211 -0
  41. package/.claude/agents/dual-mode/dual-orchestrator.md +291 -0
  42. package/.claude/agents/flow-nexus/app-store.md +88 -0
  43. package/.claude/agents/flow-nexus/authentication.md +69 -0
  44. package/.claude/agents/flow-nexus/challenges.md +81 -0
  45. package/.claude/agents/flow-nexus/neural-network.md +88 -0
  46. package/.claude/agents/flow-nexus/payments.md +83 -0
  47. package/.claude/agents/flow-nexus/sandbox.md +76 -0
  48. package/.claude/agents/flow-nexus/swarm.md +76 -0
  49. package/.claude/agents/flow-nexus/user-tools.md +96 -0
  50. package/.claude/agents/flow-nexus/workflow.md +84 -0
  51. package/.claude/agents/github/code-review-swarm.md +538 -0
  52. package/.claude/agents/github/github-modes.md +173 -0
  53. package/.claude/agents/github/issue-tracker.md +319 -0
  54. package/.claude/agents/github/multi-repo-swarm.md +553 -0
  55. package/.claude/agents/github/pr-manager.md +191 -0
  56. package/.claude/agents/github/project-board-sync.md +509 -0
  57. package/.claude/agents/github/release-manager.md +367 -0
  58. package/.claude/agents/github/release-swarm.md +583 -0
  59. package/.claude/agents/github/repo-architect.md +398 -0
  60. package/.claude/agents/github/swarm-issue.md +573 -0
  61. package/.claude/agents/github/swarm-pr.md +428 -0
  62. package/.claude/agents/github/sync-coordinator.md +452 -0
  63. package/.claude/agents/github/workflow-automation.md +635 -0
  64. package/.claude/agents/goal/agent.md +816 -0
  65. package/.claude/agents/goal/code-goal-planner.md +446 -0
  66. package/.claude/agents/goal/goal-planner.md +168 -0
  67. package/.claude/agents/hive-mind/collective-intelligence-coordinator.md +130 -0
  68. package/.claude/agents/hive-mind/queen-coordinator.md +203 -0
  69. package/.claude/agents/hive-mind/scout-explorer.md +242 -0
  70. package/.claude/agents/hive-mind/swarm-memory-manager.md +193 -0
  71. package/.claude/agents/hive-mind/worker-specialist.md +217 -0
  72. package/.claude/agents/optimization/README.md +250 -0
  73. package/.claude/agents/optimization/benchmark-suite.md +665 -0
  74. package/.claude/agents/optimization/load-balancer.md +431 -0
  75. package/.claude/agents/optimization/performance-monitor.md +672 -0
  76. package/.claude/agents/optimization/resource-allocator.md +674 -0
  77. package/.claude/agents/optimization/topology-optimizer.md +808 -0
  78. package/.claude/agents/payments/agentic-payments.md +126 -0
  79. package/.claude/agents/sona/sona-learning-optimizer.md +74 -0
  80. package/.claude/agents/sparc/architecture.md +472 -0
  81. package/.claude/agents/sparc/pseudocode.md +318 -0
  82. package/.claude/agents/sparc/refinement.md +525 -0
  83. package/.claude/agents/sparc/specification.md +276 -0
  84. package/.claude/agents/specialized/mobile/spec-mobile-react-native.md +225 -0
  85. package/.claude/agents/sublinear/consensus-coordinator.md +338 -0
  86. package/.claude/agents/sublinear/matrix-optimizer.md +185 -0
  87. package/.claude/agents/sublinear/pagerank-analyzer.md +299 -0
  88. package/.claude/agents/sublinear/performance-optimizer.md +368 -0
  89. package/.claude/agents/sublinear/trading-predictor.md +246 -0
  90. package/.claude/agents/swarm/README.md +190 -0
  91. package/.claude/agents/swarm/adaptive-coordinator.md +396 -0
  92. package/.claude/agents/swarm/hierarchical-coordinator.md +327 -0
  93. package/.claude/agents/swarm/mesh-coordinator.md +392 -0
  94. package/.claude/agents/templates/automation-smart-agent.md +205 -0
  95. package/.claude/agents/templates/coordinator-swarm-init.md +105 -0
  96. package/.claude/agents/templates/github-pr-manager.md +177 -0
  97. package/.claude/agents/templates/implementer-sparc-coder.md +259 -0
  98. package/.claude/agents/templates/memory-coordinator.md +187 -0
  99. package/.claude/agents/templates/migration-plan.md +746 -0
  100. package/.claude/agents/templates/orchestrator-task.md +139 -0
  101. package/.claude/agents/templates/performance-analyzer.md +199 -0
  102. package/.claude/agents/templates/sparc-coordinator.md +183 -0
  103. package/.claude/agents/testing/production-validator.md +395 -0
  104. package/.claude/agents/testing/tdd-london-swarm.md +244 -0
  105. package/.claude/agents/testing/unit/tdd-london-swarm.md +244 -0
  106. package/.claude/agents/testing/validation/production-validator.md +395 -0
  107. package/.claude/agents/v3/database-specialist.yaml +21 -0
  108. package/.claude/agents/v3/index.yaml +17 -0
  109. package/.claude/agents/v3/project-coordinator.yaml +15 -0
  110. package/.claude/agents/v3/python-specialist.yaml +21 -0
  111. package/.claude/agents/v3/test-architect.yaml +20 -0
  112. package/.claude/agents/v3/typescript-specialist.yaml +21 -0
  113. package/.claude/agents/v3/v3-integration-architect.md +346 -0
  114. package/.claude/agents/v3/v3-memory-specialist.md +318 -0
  115. package/.claude/agents/v3/v3-performance-engineer.md +397 -0
  116. package/.claude/agents/v3/v3-queen-coordinator.md +98 -0
  117. package/.claude/agents/v3/v3-security-architect.md +174 -0
  118. package/.claude/commands/analysis/COMMAND_COMPLIANCE_REPORT.md +54 -0
  119. package/.claude/commands/analysis/README.md +9 -0
  120. package/.claude/commands/analysis/bottleneck-detect.md +162 -0
  121. package/.claude/commands/analysis/performance-bottlenecks.md +59 -0
  122. package/.claude/commands/analysis/performance-report.md +25 -0
  123. package/.claude/commands/analysis/token-efficiency.md +45 -0
  124. package/.claude/commands/analysis/token-usage.md +25 -0
  125. package/.claude/commands/automation/README.md +9 -0
  126. package/.claude/commands/automation/auto-agent.md +122 -0
  127. package/.claude/commands/automation/self-healing.md +106 -0
  128. package/.claude/commands/automation/session-memory.md +90 -0
  129. package/.claude/commands/automation/smart-agents.md +73 -0
  130. package/.claude/commands/automation/smart-spawn.md +25 -0
  131. package/.claude/commands/automation/workflow-select.md +25 -0
  132. package/.claude/commands/claude-flow-help.md +103 -0
  133. package/.claude/commands/claude-flow-memory.md +107 -0
  134. package/.claude/commands/claude-flow-swarm.md +205 -0
  135. package/.claude/commands/github/README.md +11 -0
  136. package/.claude/commands/github/code-review-swarm.md +514 -0
  137. package/.claude/commands/github/code-review.md +25 -0
  138. package/.claude/commands/github/github-modes.md +147 -0
  139. package/.claude/commands/github/github-swarm.md +121 -0
  140. package/.claude/commands/github/issue-tracker.md +292 -0
  141. package/.claude/commands/github/issue-triage.md +25 -0
  142. package/.claude/commands/github/multi-repo-swarm.md +519 -0
  143. package/.claude/commands/github/pr-enhance.md +26 -0
  144. package/.claude/commands/github/pr-manager.md +170 -0
  145. package/.claude/commands/github/project-board-sync.md +471 -0
  146. package/.claude/commands/github/release-manager.md +338 -0
  147. package/.claude/commands/github/release-swarm.md +544 -0
  148. package/.claude/commands/github/repo-analyze.md +25 -0
  149. package/.claude/commands/github/repo-architect.md +367 -0
  150. package/.claude/commands/github/swarm-issue.md +482 -0
  151. package/.claude/commands/github/swarm-pr.md +285 -0
  152. package/.claude/commands/github/sync-coordinator.md +301 -0
  153. package/.claude/commands/github/workflow-automation.md +442 -0
  154. package/.claude/commands/hooks/README.md +11 -0
  155. package/.claude/commands/hooks/overview.md +58 -0
  156. package/.claude/commands/hooks/post-edit.md +117 -0
  157. package/.claude/commands/hooks/post-task.md +112 -0
  158. package/.claude/commands/hooks/pre-edit.md +113 -0
  159. package/.claude/commands/hooks/pre-task.md +111 -0
  160. package/.claude/commands/hooks/session-end.md +118 -0
  161. package/.claude/commands/hooks/setup.md +103 -0
  162. package/.claude/commands/monitoring/README.md +9 -0
  163. package/.claude/commands/monitoring/agent-metrics.md +25 -0
  164. package/.claude/commands/monitoring/agents.md +44 -0
  165. package/.claude/commands/monitoring/real-time-view.md +25 -0
  166. package/.claude/commands/monitoring/status.md +46 -0
  167. package/.claude/commands/monitoring/swarm-monitor.md +25 -0
  168. package/.claude/commands/optimization/README.md +9 -0
  169. package/.claude/commands/optimization/auto-topology.md +62 -0
  170. package/.claude/commands/optimization/cache-manage.md +25 -0
  171. package/.claude/commands/optimization/parallel-execute.md +25 -0
  172. package/.claude/commands/optimization/parallel-execution.md +50 -0
  173. package/.claude/commands/optimization/topology-optimize.md +25 -0
  174. package/.claude/commands/sparc/analyzer.md +52 -0
  175. package/.claude/commands/sparc/architect.md +53 -0
  176. package/.claude/commands/sparc/ask.md +97 -0
  177. package/.claude/commands/sparc/batch-executor.md +54 -0
  178. package/.claude/commands/sparc/code.md +89 -0
  179. package/.claude/commands/sparc/coder.md +54 -0
  180. package/.claude/commands/sparc/debug.md +83 -0
  181. package/.claude/commands/sparc/debugger.md +54 -0
  182. package/.claude/commands/sparc/designer.md +53 -0
  183. package/.claude/commands/sparc/devops.md +109 -0
  184. package/.claude/commands/sparc/docs-writer.md +80 -0
  185. package/.claude/commands/sparc/documenter.md +54 -0
  186. package/.claude/commands/sparc/innovator.md +54 -0
  187. package/.claude/commands/sparc/integration.md +83 -0
  188. package/.claude/commands/sparc/mcp.md +117 -0
  189. package/.claude/commands/sparc/memory-manager.md +54 -0
  190. package/.claude/commands/sparc/optimizer.md +54 -0
  191. package/.claude/commands/sparc/orchestrator.md +132 -0
  192. package/.claude/commands/sparc/post-deployment-monitoring-mode.md +83 -0
  193. package/.claude/commands/sparc/refinement-optimization-mode.md +83 -0
  194. package/.claude/commands/sparc/researcher.md +54 -0
  195. package/.claude/commands/sparc/reviewer.md +54 -0
  196. package/.claude/commands/sparc/security-review.md +80 -0
  197. package/.claude/commands/sparc/sparc-modes.md +174 -0
  198. package/.claude/commands/sparc/sparc.md +111 -0
  199. package/.claude/commands/sparc/spec-pseudocode.md +80 -0
  200. package/.claude/commands/sparc/supabase-admin.md +348 -0
  201. package/.claude/commands/sparc/swarm-coordinator.md +54 -0
  202. package/.claude/commands/sparc/tdd.md +54 -0
  203. package/.claude/commands/sparc/tester.md +54 -0
  204. package/.claude/commands/sparc/tutorial.md +79 -0
  205. package/.claude/commands/sparc/workflow-manager.md +54 -0
  206. package/.claude/helpers/hook-handler.cjs +168 -0
  207. package/.claude/helpers/intelligence.cjs +197 -0
  208. package/.claude/helpers/memory.js +83 -0
  209. package/.claude/helpers/post-commit +16 -0
  210. package/.claude/helpers/pre-commit +26 -0
  211. package/.claude/helpers/router.js +66 -0
  212. package/.claude/helpers/session.js +127 -0
  213. package/.claude/settings.json +250 -0
  214. package/.claude/skills/agentdb-advanced/SKILL.md +550 -0
  215. package/.claude/skills/agentdb-learning/SKILL.md +545 -0
  216. package/.claude/skills/agentdb-memory-patterns/SKILL.md +339 -0
  217. package/.claude/skills/agentdb-optimization/SKILL.md +509 -0
  218. package/.claude/skills/agentdb-vector-search/SKILL.md +339 -0
  219. package/.claude/skills/github-code-review/SKILL.md +1140 -0
  220. package/.claude/skills/github-multi-repo/SKILL.md +874 -0
  221. package/.claude/skills/github-project-management/SKILL.md +1277 -0
  222. package/.claude/skills/github-release-management/SKILL.md +1081 -0
  223. package/.claude/skills/github-workflow-automation/SKILL.md +1065 -0
  224. package/.claude/skills/hooks-automation/SKILL.md +1201 -0
  225. package/.claude/skills/pair-programming/SKILL.md +1202 -0
  226. package/.claude/skills/reasoningbank-agentdb/SKILL.md +446 -0
  227. package/.claude/skills/reasoningbank-intelligence/SKILL.md +201 -0
  228. package/.claude/skills/skill-builder/.claude-flow/metrics/agent-metrics.json +1 -0
  229. package/.claude/skills/skill-builder/.claude-flow/metrics/performance.json +87 -0
  230. package/.claude/skills/skill-builder/.claude-flow/metrics/task-metrics.json +10 -0
  231. package/.claude/skills/skill-builder/SKILL.md +910 -0
  232. package/.claude/skills/sparc-methodology/SKILL.md +1115 -0
  233. package/.claude/skills/stream-chain/SKILL.md +563 -0
  234. package/.claude/skills/swarm-advanced/SKILL.md +973 -0
  235. package/.claude/skills/swarm-orchestration/SKILL.md +179 -0
  236. package/.claude/skills/v3-cli-modernization/SKILL.md +872 -0
  237. package/.claude/skills/v3-core-implementation/SKILL.md +797 -0
  238. package/.claude/skills/v3-ddd-architecture/SKILL.md +442 -0
  239. package/.claude/skills/v3-integration-deep/SKILL.md +241 -0
  240. package/.claude/skills/v3-mcp-optimization/SKILL.md +777 -0
  241. package/.claude/skills/v3-memory-unification/SKILL.md +174 -0
  242. package/.claude/skills/v3-performance-optimization/SKILL.md +390 -0
  243. package/.claude/skills/v3-security-overhaul/SKILL.md +82 -0
  244. package/.claude/skills/v3-swarm-coordination/SKILL.md +340 -0
  245. package/.claude/skills/verification-quality/SKILL.md +649 -0
  246. package/.claude/statusline.mjs +109 -0
  247. package/.claude/statusline.sh +375 -0
  248. package/.claude-flow/CAPABILITIES.md +403 -0
  249. package/.claude-flow/config.yaml +43 -0
  250. package/.claude-flow/metrics/learning.json +17 -0
  251. package/.claude-flow/metrics/swarm-activity.json +18 -0
  252. package/.claude-flow/metrics/v3-progress.json +26 -0
  253. package/.claude-flow/security/audit-status.json +8 -0
  254. package/.mcp.json +20 -0
  255. package/AGENTS.md +145 -0
  256. package/CLAUDE.md +188 -0
  257. package/LICENSE +21 -0
  258. package/README.md +115 -0
  259. package/bin/cf-guidance-impl.mjs +74 -0
  260. package/package.json +23 -0
  261. package/scaffold/.claude/helpers/hook-handler.cjs +576 -0
  262. package/scaffold/docs/guidance-control-plane.md +441 -0
  263. package/scaffold/docs/guidance-implementation-guide.md +424 -0
  264. package/scaffold/scripts/analyze-guidance.js +176 -0
  265. package/scaffold/scripts/guidance-ab-benchmark.js +40 -0
  266. package/scaffold/scripts/guidance-autopilot.js +473 -0
  267. package/scaffold/scripts/guidance-codex-bridge.js +337 -0
  268. package/scaffold/scripts/guidance-integrations.js +587 -0
  269. package/scaffold/scripts/guidance-runtime.js +133 -0
  270. package/scaffold/scripts/scaffold-guidance.js +122 -0
  271. package/scaffold/src/guidance/advanced-runtime.js +658 -0
  272. package/scaffold/src/guidance/content-aware-executor.js +70 -0
  273. package/scaffold/src/guidance/phase1-runtime.js +151 -0
  274. package/src/default-settings.mjs +111 -0
  275. package/src/installer.mjs +415 -0
@@ -0,0 +1,415 @@
1
+ import { cpSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { dirname, resolve, relative } from 'node:path';
3
+ import { spawnSync } from 'node:child_process';
4
+
5
+ import {
6
+ GUIDANCE_ENV_DEFAULTS,
7
+ GUIDANCE_HOOKS_DEFAULTS,
8
+ GUIDANCE_PACKAGE_DEPS,
9
+ GUIDANCE_PACKAGE_SCRIPTS,
10
+ } from './default-settings.mjs';
11
+
12
+ const GUIDANCE_CODEX_CONFIG_BLOCK = [
13
+ '# =============================================================================',
14
+ '# Guidance Codex Bridge',
15
+ '# =============================================================================',
16
+ '',
17
+ '[guidance_codex]',
18
+ 'enabled = true',
19
+ 'script = "scripts/guidance-codex-bridge.js"',
20
+ 'hook_handler = ".claude/helpers/hook-handler.cjs"',
21
+ 'run_claude_flow_cli_hooks = true',
22
+ '',
23
+ '[guidance_codex.commands]',
24
+ 'status = "npm run guidance:codex:status"',
25
+ 'pre_command = "npm run guidance:codex:pre-command -- --command \\"<bash command>\\""',
26
+ 'pre_edit = "npm run guidance:codex:pre-edit -- --file <path>"',
27
+ 'pre_task = "npm run guidance:codex:pre-task -- --description \\"<task description>\\""',
28
+ 'post_edit = "npm run guidance:codex:post-edit -- --file <path>"',
29
+ 'post_task = "npm run guidance:codex:post-task -- --task-id <id> --status completed"',
30
+ 'session_start = "npm run guidance:codex:session-start"',
31
+ 'session_end = "npm run guidance:codex:session-end"',
32
+ '',
33
+ ].join('\n');
34
+
35
+ const GUIDANCE_CODEX_AGENTS_BLOCK = [
36
+ '## Guidance Lifecycle Wiring (Codex)',
37
+ '',
38
+ 'Codex does not expose Claude Code-style event-command hook maps in `config.toml`.',
39
+ 'This project uses an explicit bridge script:',
40
+ '',
41
+ '- `scripts/guidance-codex-bridge.js` -> dispatches lifecycle events to:',
42
+ ' - `.claude/helpers/hook-handler.cjs` (enforcement path)',
43
+ ' - optional `npx @claude-flow/cli@latest hooks ...` telemetry calls',
44
+ '',
45
+ 'Primary commands:',
46
+ '',
47
+ '```bash',
48
+ 'npm run guidance:codex:session-start',
49
+ 'npm run guidance:codex:pre-task -- --description "Implement feature X"',
50
+ 'npm run guidance:codex:pre-command -- --command "git status"',
51
+ 'npm run guidance:codex:pre-edit -- --file src/example.ts',
52
+ 'npm run guidance:codex:post-edit -- --file src/example.ts',
53
+ 'npm run guidance:codex:post-task -- --task-id task-123 --status completed',
54
+ 'npm run guidance:codex:session-end',
55
+ '```',
56
+ '',
57
+ 'Control flags:',
58
+ '- `--skip-cf-hooks` skips secondary `@claude-flow/cli` hook invocations',
59
+ '- `GUIDANCE_CODEX_SKIP_CF_HOOKS=1` disables secondary invocations globally',
60
+ '',
61
+ ].join('\n');
62
+
63
+ function ensureDir(path) {
64
+ mkdirSync(path, { recursive: true });
65
+ }
66
+
67
+ function readJson(path, fallback = {}) {
68
+ if (!existsSync(path)) return fallback;
69
+ try {
70
+ return JSON.parse(readFileSync(path, 'utf-8'));
71
+ } catch {
72
+ return fallback;
73
+ }
74
+ }
75
+
76
+ function writeJson(path, value) {
77
+ ensureDir(dirname(path));
78
+ writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`);
79
+ }
80
+
81
+ function writeText(path, value) {
82
+ ensureDir(dirname(path));
83
+ writeFileSync(path, value);
84
+ }
85
+
86
+ function sameMatcher(a, b) {
87
+ return String(a ?? '') === String(b ?? '');
88
+ }
89
+
90
+ function mergeHookBlocks(existingBlocks, incomingBlocks) {
91
+ const blocks = Array.isArray(existingBlocks) ? [...existingBlocks] : [];
92
+ for (const incoming of incomingBlocks) {
93
+ const index = blocks.findIndex((block) => sameMatcher(block.matcher, incoming.matcher));
94
+ if (index < 0) {
95
+ blocks.push(incoming);
96
+ continue;
97
+ }
98
+
99
+ const current = blocks[index];
100
+ const currentHooks = Array.isArray(current.hooks) ? [...current.hooks] : [];
101
+ for (const incomingHook of incoming.hooks ?? []) {
102
+ const exists = currentHooks.some(
103
+ (hook) => hook.type === incomingHook.type && hook.command === incomingHook.command
104
+ );
105
+ if (!exists) currentHooks.push(incomingHook);
106
+ }
107
+
108
+ blocks[index] = { ...current, hooks: currentHooks };
109
+ }
110
+ return blocks;
111
+ }
112
+
113
+ function ensureGitIgnoreLine(targetRepo, line) {
114
+ const gitignorePath = resolve(targetRepo, '.gitignore');
115
+ const existing = existsSync(gitignorePath) ? readFileSync(gitignorePath, 'utf-8') : '';
116
+ const lines = existing.split(/\r?\n/).filter(Boolean);
117
+ if (!lines.includes(line)) lines.push(line);
118
+ writeText(gitignorePath, `${lines.join('\n')}\n`);
119
+ }
120
+
121
+ function writeMissingStub(filePath, content) {
122
+ if (!existsSync(filePath)) writeText(filePath, content);
123
+ }
124
+
125
+ function run(cmd, args, cwd) {
126
+ return spawnSync(cmd, args, {
127
+ cwd,
128
+ encoding: 'utf-8',
129
+ });
130
+ }
131
+
132
+ function appendBlockIfMissing(path, marker, block, fallback = '') {
133
+ const existing = existsSync(path) ? readFileSync(path, 'utf-8') : fallback;
134
+ if (existing.includes(marker)) return false;
135
+ const trimmed = existing.trimEnd();
136
+ const prefix = trimmed ? `${trimmed}\n\n` : '';
137
+ writeText(path, `${prefix}${block.trim()}\n`);
138
+ return true;
139
+ }
140
+
141
+ export function installIntoRepo({ toolkitRoot, targetRepo, force = false, installDeps = false }) {
142
+ const target = resolve(targetRepo);
143
+ const scaffold = resolve(toolkitRoot, 'scaffold');
144
+
145
+ if (!existsSync(target)) {
146
+ throw new Error(`Target repo does not exist: ${target}`);
147
+ }
148
+
149
+ if (!existsSync(scaffold)) {
150
+ throw new Error(`Toolkit scaffold not found: ${scaffold}`);
151
+ }
152
+
153
+ // Copy scaffold files into target.
154
+ cpSync(scaffold, target, {
155
+ recursive: true,
156
+ force,
157
+ errorOnExist: false,
158
+ });
159
+
160
+ // Merge package.json scripts + dependencies.
161
+ const packagePath = resolve(target, 'package.json');
162
+ const packageJson = readJson(packagePath, {
163
+ name: 'project',
164
+ version: '1.0.0',
165
+ private: true,
166
+ type: 'module',
167
+ scripts: {},
168
+ dependencies: {},
169
+ });
170
+
171
+ packageJson.type = packageJson.type || 'module';
172
+ packageJson.scripts = packageJson.scripts || {};
173
+ for (const [name, cmd] of Object.entries(GUIDANCE_PACKAGE_SCRIPTS)) {
174
+ if (!(name in packageJson.scripts)) {
175
+ packageJson.scripts[name] = cmd;
176
+ }
177
+ }
178
+
179
+ packageJson.dependencies = packageJson.dependencies || {};
180
+ for (const [dep, version] of Object.entries(GUIDANCE_PACKAGE_DEPS)) {
181
+ if (!(dep in packageJson.dependencies)) {
182
+ packageJson.dependencies[dep] = version;
183
+ }
184
+ }
185
+
186
+ writeJson(packagePath, packageJson);
187
+
188
+ // Merge .claude/settings.json hooks and env.
189
+ const settingsPath = resolve(target, '.claude/settings.json');
190
+ const settings = readJson(settingsPath, {});
191
+ settings.env = settings.env || {};
192
+ for (const [key, value] of Object.entries(GUIDANCE_ENV_DEFAULTS)) {
193
+ if (!(key in settings.env)) settings.env[key] = value;
194
+ }
195
+
196
+ settings.hooks = settings.hooks || {};
197
+ for (const [event, blocks] of Object.entries(GUIDANCE_HOOKS_DEFAULTS)) {
198
+ settings.hooks[event] = mergeHookBlocks(settings.hooks[event], blocks);
199
+ }
200
+
201
+ writeJson(settingsPath, settings);
202
+
203
+ const agentsConfigPath = resolve(target, '.agents/config.toml');
204
+ const codexConfigAdded = appendBlockIfMissing(
205
+ agentsConfigPath,
206
+ '[guidance_codex]',
207
+ GUIDANCE_CODEX_CONFIG_BLOCK
208
+ );
209
+
210
+ const agentsDocPath = resolve(target, 'AGENTS.md');
211
+ const codexAgentsDocAdded = appendBlockIfMissing(
212
+ agentsDocPath,
213
+ '## Guidance Lifecycle Wiring (Codex)',
214
+ GUIDANCE_CODEX_AGENTS_BLOCK,
215
+ '# Project\n\n'
216
+ );
217
+
218
+ // Ensure local guidance file and gitignore entries.
219
+ writeMissingStub(
220
+ resolve(target, 'CLAUDE.local.md'),
221
+ [
222
+ '# Local Guidance Experiments',
223
+ '',
224
+ 'Add experimental local-only rules here in parseable guidance style.',
225
+ 'Autopilot can promote winning local rules into CLAUDE.md with ADRs.',
226
+ '',
227
+ '- [local-example-rule] Prefer safe/validated command patterns (high) #implementation @engineering priority:80',
228
+ '',
229
+ ].join('\n')
230
+ );
231
+ ensureGitIgnoreLine(target, 'CLAUDE.local.md');
232
+
233
+ const summary = {
234
+ target,
235
+ copiedFrom: relative(target, scaffold),
236
+ filesInstalled: [
237
+ '.claude/helpers/hook-handler.cjs',
238
+ 'scripts/guidance-integrations.js',
239
+ 'scripts/guidance-runtime.js',
240
+ 'scripts/guidance-codex-bridge.js',
241
+ 'scripts/guidance-autopilot.js',
242
+ 'scripts/guidance-ab-benchmark.js',
243
+ 'scripts/scaffold-guidance.js',
244
+ 'scripts/analyze-guidance.js',
245
+ 'src/guidance/phase1-runtime.js',
246
+ 'src/guidance/advanced-runtime.js',
247
+ 'src/guidance/content-aware-executor.js',
248
+ 'docs/guidance-control-plane.md',
249
+ 'docs/guidance-implementation-guide.md',
250
+ ],
251
+ packageUpdated: packagePath,
252
+ settingsUpdated: settingsPath,
253
+ agentsConfigUpdated: agentsConfigPath,
254
+ agentsDocUpdated: agentsDocPath,
255
+ codexConfigAdded,
256
+ codexAgentsDocAdded,
257
+ installDeps,
258
+ };
259
+
260
+ if (installDeps) {
261
+ const npmInstall = run('npm', ['install'], target);
262
+ summary.installExitCode = npmInstall.status;
263
+ summary.installStdout = npmInstall.stdout.split('\n').slice(-8).join('\n');
264
+ summary.installStderr = npmInstall.stderr.split('\n').slice(-8).join('\n');
265
+ if (npmInstall.status !== 0) {
266
+ throw new Error(`npm install failed in ${target}:\n${npmInstall.stderr}`);
267
+ }
268
+ }
269
+
270
+ return summary;
271
+ }
272
+
273
+ export function verifyRepo({ targetRepo }) {
274
+ const target = resolve(targetRepo);
275
+ const requiredFiles = [
276
+ '.claude/helpers/hook-handler.cjs',
277
+ 'scripts/guidance-integrations.js',
278
+ 'scripts/guidance-runtime.js',
279
+ 'scripts/guidance-codex-bridge.js',
280
+ 'src/guidance/phase1-runtime.js',
281
+ '.claude/settings.json',
282
+ 'package.json',
283
+ ];
284
+
285
+ const checks = [];
286
+ for (const relPath of requiredFiles) {
287
+ const full = resolve(target, relPath);
288
+ checks.push({ path: relPath, exists: existsSync(full) });
289
+ }
290
+
291
+ const syntaxChecks = [];
292
+ const checkFiles = [
293
+ '.claude/helpers/hook-handler.cjs',
294
+ 'scripts/guidance-integrations.js',
295
+ 'scripts/guidance-runtime.js',
296
+ 'scripts/guidance-codex-bridge.js',
297
+ ];
298
+
299
+ for (const relPath of checkFiles) {
300
+ const full = resolve(target, relPath);
301
+ if (!existsSync(full)) {
302
+ syntaxChecks.push({ path: relPath, ok: false, reason: 'missing' });
303
+ continue;
304
+ }
305
+ const result = run('node', ['--check', full], target);
306
+ syntaxChecks.push({
307
+ path: relPath,
308
+ ok: result.status === 0,
309
+ stderr: result.stderr.trim(),
310
+ });
311
+ }
312
+
313
+ const smokeInput = '{"tool_input":{"command":"git status"}}';
314
+ const smoke = run(
315
+ 'bash',
316
+ [
317
+ '-lc',
318
+ `printf '%s' '${smokeInput}' | node .claude/helpers/hook-handler.cjs pre-bash`,
319
+ ],
320
+ target
321
+ );
322
+
323
+ const smokeCodex = run(
324
+ 'node',
325
+ ['scripts/guidance-codex-bridge.js', 'status', '--skip-cf-hooks'],
326
+ target
327
+ );
328
+
329
+ const passed =
330
+ checks.every((check) => check.exists) &&
331
+ syntaxChecks.every((check) => check.ok) &&
332
+ smoke.status === 0 &&
333
+ smokeCodex.status === 0;
334
+
335
+ return {
336
+ target,
337
+ passed,
338
+ files: checks,
339
+ syntaxChecks,
340
+ smoke: {
341
+ exitCode: smoke.status,
342
+ stdout: smoke.stdout.trim(),
343
+ stderr: smoke.stderr.trim(),
344
+ },
345
+ smokeCodex: {
346
+ exitCode: smokeCodex.status,
347
+ stdout: smokeCodex.stdout.trim(),
348
+ stderr: smokeCodex.stderr.trim(),
349
+ },
350
+ };
351
+ }
352
+
353
+ export function initRepo({
354
+ toolkitRoot,
355
+ targetRepo,
356
+ force = false,
357
+ installDeps = false,
358
+ dual = true,
359
+ skipCfInit = false,
360
+ verify = true,
361
+ }) {
362
+ const target = resolve(targetRepo);
363
+ if (!existsSync(target)) {
364
+ throw new Error(`Target repo does not exist: ${target}`);
365
+ }
366
+
367
+ let claudeFlowInit = {
368
+ skipped: true,
369
+ reason: '--skip-cf-init',
370
+ };
371
+
372
+ if (!skipCfInit) {
373
+ const initArgs = ['@claude-flow/cli@latest', 'init'];
374
+ if (dual) initArgs.push('--dual');
375
+
376
+ const initResult = run('npx', initArgs, target);
377
+ claudeFlowInit = {
378
+ skipped: false,
379
+ command: `npx ${initArgs.join(' ')}`,
380
+ exitCode: initResult.status,
381
+ stdout: initResult.stdout.trim().split('\n').slice(-12).join('\n'),
382
+ stderr: initResult.stderr.trim().split('\n').slice(-12).join('\n'),
383
+ };
384
+
385
+ if (initResult.status !== 0) {
386
+ throw new Error(
387
+ `claude-flow init failed in ${target}:\n${claudeFlowInit.stderr || claudeFlowInit.stdout}`
388
+ );
389
+ }
390
+ }
391
+
392
+ const install = installIntoRepo({
393
+ toolkitRoot,
394
+ targetRepo: target,
395
+ force,
396
+ installDeps,
397
+ });
398
+
399
+ let verifyReport = null;
400
+ if (verify) {
401
+ verifyReport = verifyRepo({ targetRepo: target });
402
+ if (!verifyReport.passed) {
403
+ throw new Error(
404
+ `Guidance wiring verification failed in ${target}. Run cf-guidance-impl verify --target ${target} for details.`
405
+ );
406
+ }
407
+ }
408
+
409
+ return {
410
+ target,
411
+ claudeFlowInit,
412
+ install,
413
+ verify: verifyReport,
414
+ };
415
+ }