claude-mycelium 2.0.0 → 2.2.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 (208) hide show
  1. package/.agent-meta/_inhibitors.ndjson +1287 -0
  2. package/.agent-meta/_quarantine.json +45 -0
  3. package/.agent-meta/config.json +9 -0
  4. package/.agent-meta/tasks/_active.json +4 -0
  5. package/.agent-meta/tasks/task_0657b028-05a0-4b0c-b0b9-a4eae3d66cd9.json +168 -0
  6. package/.claude/memory.db +0 -0
  7. package/.claude/settings.local.json +4 -1
  8. package/README.md +85 -233
  9. package/SECURITY.md +145 -0
  10. package/dist/agent/task-worker.d.ts +11 -0
  11. package/dist/agent/task-worker.d.ts.map +1 -0
  12. package/dist/agent/task-worker.js +173 -0
  13. package/dist/agent/task-worker.js.map +1 -0
  14. package/dist/agent/worker.d.ts +8 -0
  15. package/dist/agent/worker.d.ts.map +1 -0
  16. package/dist/agent/worker.js +97 -0
  17. package/dist/agent/worker.js.map +1 -0
  18. package/dist/bin.d.ts +7 -0
  19. package/dist/bin.d.ts.map +1 -0
  20. package/dist/bin.js +11 -0
  21. package/dist/bin.js.map +1 -0
  22. package/dist/cli/cost.d.ts +10 -0
  23. package/dist/cli/cost.d.ts.map +1 -0
  24. package/dist/cli/cost.js +163 -0
  25. package/dist/cli/cost.js.map +1 -0
  26. package/dist/cli/gc.d.ts +10 -0
  27. package/dist/cli/gc.d.ts.map +1 -0
  28. package/dist/cli/gc.js +108 -0
  29. package/dist/cli/gc.js.map +1 -0
  30. package/dist/cli/gradients.d.ts +10 -0
  31. package/dist/cli/gradients.d.ts.map +1 -0
  32. package/dist/cli/gradients.js +70 -0
  33. package/dist/cli/gradients.js.map +1 -0
  34. package/dist/cli/grow.d.ts +17 -0
  35. package/dist/cli/grow.d.ts.map +1 -0
  36. package/dist/cli/grow.js +373 -0
  37. package/dist/cli/grow.js.map +1 -0
  38. package/dist/cli/index.d.ts +17 -0
  39. package/dist/cli/index.d.ts.map +1 -0
  40. package/dist/cli/index.js +74 -0
  41. package/dist/cli/index.js.map +1 -0
  42. package/dist/cli/init.d.ts +11 -0
  43. package/dist/cli/init.d.ts.map +1 -0
  44. package/dist/cli/init.js +97 -0
  45. package/dist/cli/init.js.map +1 -0
  46. package/dist/cli/status.d.ts +10 -0
  47. package/dist/cli/status.d.ts.map +1 -0
  48. package/dist/cli/status.js +191 -0
  49. package/dist/cli/status.js.map +1 -0
  50. package/dist/coordination/file-locks.d.ts +42 -0
  51. package/dist/coordination/file-locks.d.ts.map +1 -0
  52. package/dist/coordination/file-locks.js +269 -0
  53. package/dist/coordination/file-locks.js.map +1 -0
  54. package/dist/coordination/index.d.ts +4 -0
  55. package/dist/coordination/index.d.ts.map +1 -1
  56. package/dist/coordination/index.js +4 -0
  57. package/dist/coordination/index.js.map +1 -1
  58. package/dist/coordination/inhibitors.d.ts +84 -0
  59. package/dist/coordination/inhibitors.d.ts.map +1 -0
  60. package/dist/coordination/inhibitors.js +290 -0
  61. package/dist/coordination/inhibitors.js.map +1 -0
  62. package/dist/coordination/process-manager.d.ts +73 -0
  63. package/dist/coordination/process-manager.d.ts.map +1 -0
  64. package/dist/coordination/process-manager.js +144 -0
  65. package/dist/coordination/process-manager.js.map +1 -0
  66. package/dist/core/agent-executor.d.ts +4 -1
  67. package/dist/core/agent-executor.d.ts.map +1 -1
  68. package/dist/core/agent-executor.js +38 -12
  69. package/dist/core/agent-executor.js.map +1 -1
  70. package/dist/core/change-applier.d.ts +29 -5
  71. package/dist/core/change-applier.d.ts.map +1 -1
  72. package/dist/core/change-applier.js +254 -24
  73. package/dist/core/change-applier.js.map +1 -1
  74. package/dist/core/signals/churn.d.ts.map +1 -1
  75. package/dist/core/signals/churn.js +6 -4
  76. package/dist/core/signals/churn.js.map +1 -1
  77. package/dist/core/signals/debt.d.ts.map +1 -1
  78. package/dist/core/signals/debt.js +4 -3
  79. package/dist/core/signals/debt.js.map +1 -1
  80. package/dist/cost/cost-tracker.d.ts.map +1 -1
  81. package/dist/cost/cost-tracker.js +2 -0
  82. package/dist/cost/cost-tracker.js.map +1 -1
  83. package/dist/gc/index.d.ts +17 -0
  84. package/dist/gc/index.d.ts.map +1 -0
  85. package/dist/gc/index.js +17 -0
  86. package/dist/gc/index.js.map +1 -0
  87. package/dist/gc/runner.d.ts +39 -0
  88. package/dist/gc/runner.d.ts.map +1 -0
  89. package/dist/gc/runner.js +277 -0
  90. package/dist/gc/runner.js.map +1 -0
  91. package/dist/gc/trace-compactor.d.ts +31 -0
  92. package/dist/gc/trace-compactor.d.ts.map +1 -0
  93. package/dist/gc/trace-compactor.js +162 -0
  94. package/dist/gc/trace-compactor.js.map +1 -0
  95. package/dist/index.d.ts +5 -1
  96. package/dist/index.d.ts.map +1 -1
  97. package/dist/index.js +6 -1
  98. package/dist/index.js.map +1 -1
  99. package/dist/prompts/index.d.ts +2 -1
  100. package/dist/prompts/index.d.ts.map +1 -1
  101. package/dist/prompts/index.js.map +1 -1
  102. package/dist/quarantine/explorer.d.ts +65 -0
  103. package/dist/quarantine/explorer.d.ts.map +1 -0
  104. package/dist/quarantine/explorer.js +175 -0
  105. package/dist/quarantine/explorer.js.map +1 -0
  106. package/dist/quarantine/index.d.ts +7 -0
  107. package/dist/quarantine/index.d.ts.map +1 -0
  108. package/dist/quarantine/index.js +7 -0
  109. package/dist/quarantine/index.js.map +1 -0
  110. package/dist/quarantine/manager.d.ts +75 -0
  111. package/dist/quarantine/manager.d.ts.map +1 -0
  112. package/dist/quarantine/manager.js +275 -0
  113. package/dist/quarantine/manager.js.map +1 -0
  114. package/dist/task/acceptance.d.ts +29 -0
  115. package/dist/task/acceptance.d.ts.map +1 -0
  116. package/dist/task/acceptance.js +228 -0
  117. package/dist/task/acceptance.js.map +1 -0
  118. package/dist/task/agent-coordinator.d.ts +40 -0
  119. package/dist/task/agent-coordinator.d.ts.map +1 -0
  120. package/dist/task/agent-coordinator.js +168 -0
  121. package/dist/task/agent-coordinator.js.map +1 -0
  122. package/dist/task/executor.d.ts +37 -0
  123. package/dist/task/executor.d.ts.map +1 -0
  124. package/dist/task/executor.js +462 -0
  125. package/dist/task/executor.js.map +1 -0
  126. package/dist/task/index.d.ts +12 -0
  127. package/dist/task/index.d.ts.map +1 -0
  128. package/dist/task/index.js +12 -0
  129. package/dist/task/index.js.map +1 -0
  130. package/dist/task/planner.d.ts +21 -0
  131. package/dist/task/planner.d.ts.map +1 -0
  132. package/dist/task/planner.js +253 -0
  133. package/dist/task/planner.js.map +1 -0
  134. package/dist/task/storage.d.ts +46 -0
  135. package/dist/task/storage.d.ts.map +1 -0
  136. package/dist/task/storage.js +266 -0
  137. package/dist/task/storage.js.map +1 -0
  138. package/dist/trace/trace-event.d.ts +2 -18
  139. package/dist/trace/trace-event.d.ts.map +1 -1
  140. package/dist/trace/trace-event.js +6 -6
  141. package/dist/trace/trace-event.js.map +1 -1
  142. package/dist/utils/file-utils.d.ts.map +1 -1
  143. package/dist/utils/file-utils.js +54 -15
  144. package/dist/utils/file-utils.js.map +1 -1
  145. package/docs/PHASE5_IMPLEMENTATION.md +237 -0
  146. package/docs/PHASES-3-7-COMPLETE.md +177 -0
  147. package/docs/PHASE_4_COMPLETE.md +135 -0
  148. package/docs/PHASE_7_DELIVERABLES.md +295 -0
  149. package/docs/PHASE_7_IMPLEMENTATION.md +306 -0
  150. package/docs/PHASE_7_SUMMARY.txt +195 -0
  151. package/docs/RELEASE-NOTES-v2.1.md +213 -0
  152. package/docs/ROADMAP.md +194 -107
  153. package/docs/SECURITY-AUDIT.md +387 -0
  154. package/docs/SNAPSHOT.md +59 -32
  155. package/docs/implementation/phase3-summary.md +220 -0
  156. package/package.json +27 -11
  157. package/src/agent/task-worker.ts +196 -0
  158. package/src/agent/worker.ts +111 -0
  159. package/src/bin.ts +13 -0
  160. package/src/cli/cost.ts +210 -0
  161. package/src/cli/gc.ts +138 -0
  162. package/src/cli/gradients.ts +97 -0
  163. package/src/cli/grow.ts +416 -0
  164. package/src/cli/index.ts +81 -0
  165. package/src/cli/init.ts +139 -0
  166. package/src/cli/status.ts +218 -0
  167. package/src/coordination/file-locks.ts +300 -0
  168. package/src/coordination/index.ts +4 -0
  169. package/src/coordination/inhibitors.ts +345 -0
  170. package/src/coordination/process-manager.ts +199 -0
  171. package/src/core/agent-executor.ts +37 -8
  172. package/src/core/signals/churn.ts +8 -5
  173. package/src/core/signals/debt.ts +4 -3
  174. package/src/cost/cost-tracker.ts +2 -0
  175. package/src/gc/index.ts +17 -0
  176. package/src/gc/runner.ts +314 -0
  177. package/src/gc/trace-compactor.ts +187 -0
  178. package/src/index.ts +7 -1
  179. package/src/prompts/index.ts +2 -1
  180. package/src/quarantine/explorer.ts +234 -0
  181. package/src/quarantine/index.ts +7 -0
  182. package/src/quarantine/manager.ts +336 -0
  183. package/src/task/acceptance.ts +267 -0
  184. package/src/task/agent-coordinator.ts +220 -0
  185. package/src/task/executor.ts +543 -0
  186. package/src/task/index.ts +38 -0
  187. package/src/task/planner.ts +294 -0
  188. package/src/task/storage.ts +332 -0
  189. package/src/trace/trace-event.ts +7 -26
  190. package/src/utils/file-utils.ts +61 -15
  191. package/tests/cli/gc.test.ts +206 -0
  192. package/tests/cli/init.test.ts +181 -0
  193. package/tests/cli/status.test.ts +282 -0
  194. package/tests/coordination/file-locks.test.ts +196 -0
  195. package/tests/coordination/inhibitors.test.ts +459 -0
  196. package/tests/coordination/integration.test.ts +195 -0
  197. package/tests/coordination/process-manager.test.ts +165 -0
  198. package/tests/gc/trace-compactor.test.ts +245 -0
  199. package/tests/integration/phase-7.test.ts +145 -0
  200. package/tests/quarantine/explorer.test.ts +381 -0
  201. package/tests/quarantine/manager.test.ts +399 -0
  202. package/tests/security/command-injection.test.ts +88 -0
  203. package/tests/security/path-traversal.test.ts +103 -0
  204. package/tests/task/acceptance.test.ts +411 -0
  205. package/tests/task/executor.test.ts +421 -0
  206. package/tests/task/planner.test.ts +359 -0
  207. package/tests/trace/trace-event.test.ts +62 -20
  208. package/tsconfig.json +2 -2
@@ -0,0 +1,416 @@
1
+ /**
2
+ * Grow CLI Command - Interactive Mycelium Growth Interface
3
+ *
4
+ * Interactive terminal for spawning agents to improve your codebase
5
+ * Usage: npx claude-mycelium grow
6
+ */
7
+
8
+ import { Command } from 'commander';
9
+ import prompts from 'prompts';
10
+ import chalk from 'chalk';
11
+ import ora from 'ora';
12
+ import { executeAgent } from '../core/agent-executor.js';
13
+ import { calculateGradient } from '../core/gradient.js';
14
+ import { planTask, executeTask } from '../task/index.js';
15
+ import { storeTask } from '../task/storage.js';
16
+ import type { Mode, Task } from '../types/index.js';
17
+ import { randomUUID } from 'crypto';
18
+
19
+ interface ParsedTask {
20
+ type: 'spawn' | 'analyze' | 'task' | 'help' | 'exit';
21
+ mode?: Mode;
22
+ file?: string;
23
+ description?: string;
24
+ }
25
+
26
+ /**
27
+ * Parse natural language task into action
28
+ */
29
+ function parseTask(input: string): ParsedTask {
30
+ const lower = input.toLowerCase().trim();
31
+
32
+ if (lower === 'help' || lower === '?') {
33
+ return { type: 'help' };
34
+ }
35
+
36
+ if (lower === 'exit' || lower === 'quit' || lower === 'q') {
37
+ return { type: 'exit' };
38
+ }
39
+
40
+ if (lower.startsWith('analyze ') || lower.startsWith('check ')) {
41
+ const file = lower.split(/\s+/).slice(1).join(' ');
42
+ return { type: 'analyze', file };
43
+ }
44
+
45
+ // Check if input has a file path - if so, use quick spawn commands
46
+ const file = extractFilePath(input);
47
+
48
+ if (file) {
49
+ // File-specific quick commands
50
+ if (lower.includes('fix') || lower.includes('bug') || lower.includes('error')) {
51
+ return { type: 'spawn', mode: 'error_reducer', file };
52
+ }
53
+
54
+ if (lower.includes('complex') || lower.includes('simplify') || lower.includes('reduce')) {
55
+ return { type: 'spawn', mode: 'complexity_reducer', file };
56
+ }
57
+
58
+ if (lower.includes('clean') || lower.includes('debt') || lower.includes('lint')) {
59
+ return { type: 'spawn', mode: 'debt_payer', file };
60
+ }
61
+
62
+ if (lower.includes('stabil') || lower.includes('churn')) {
63
+ return { type: 'spawn', mode: 'stabilizer', file };
64
+ }
65
+
66
+ // Has file but no recognized keywords - analyze it
67
+ return { type: 'analyze', file };
68
+ }
69
+
70
+ // No file path - treat as general task (like "implement MCP", "add health check", etc.)
71
+ // Keywords that indicate a general task
72
+ if (lower.includes('implement') ||
73
+ lower.includes('create') ||
74
+ lower.includes('add') ||
75
+ lower.includes('build') ||
76
+ lower.includes('make') ||
77
+ lower.includes('develop') ||
78
+ lower.includes('write') ||
79
+ lower.startsWith('let\'s')) {
80
+ return { type: 'task', description: input };
81
+ }
82
+
83
+ return { type: 'help' };
84
+ }
85
+
86
+ /**
87
+ * Extract file path from natural language
88
+ */
89
+ function extractFilePath(input: string): string | undefined {
90
+ const words = input.split(/\s+/);
91
+ const fileWord = words.find(
92
+ (w) => w.includes('/') || w.endsWith('.ts') || w.endsWith('.js') || w.endsWith('.tsx') || w.endsWith('.jsx')
93
+ );
94
+ return fileWord;
95
+ }
96
+
97
+ /**
98
+ * Show help message
99
+ */
100
+ function showHelp() {
101
+ console.log('');
102
+ console.log(chalk.bold.green('🍄 Claude Mycelium - Interactive Growth'));
103
+ console.log('');
104
+ console.log(chalk.bold('General Tasks (Multi-Step):'));
105
+ console.log('');
106
+ console.log(chalk.cyan(' "implement MCP server"') + ' - Plan and execute multi-step task');
107
+ console.log(chalk.cyan(' "add health check endpoint"') + ' - Create feature with tests');
108
+ console.log(chalk.cyan(' "create authentication system"') + ' - Build complete feature');
109
+ console.log('');
110
+ console.log(chalk.bold('File-Specific Commands (Quick):'));
111
+ console.log('');
112
+ console.log(chalk.cyan(' "fix bugs in src/api.ts"') + ' - Spawn error_reducer agent');
113
+ console.log(chalk.cyan(' "reduce complexity in src/utils.ts"') + ' - Spawn complexity_reducer agent');
114
+ console.log(chalk.cyan(' "clean up src/legacy.ts"') + ' - Spawn debt_payer agent');
115
+ console.log(chalk.cyan(' "stabilize src/volatile.ts"') + ' - Spawn stabilizer agent');
116
+ console.log(chalk.cyan(' "analyze src/app.ts"') + ' - Show gradient analysis');
117
+ console.log('');
118
+ console.log(chalk.dim(' Type "exit" to quit'));
119
+ console.log('');
120
+ }
121
+
122
+ /**
123
+ * Start interactive grow session
124
+ */
125
+ export async function startGrowSession() {
126
+ console.clear();
127
+ console.log(chalk.bold.green('🍄 Mycelium Network Active'));
128
+ console.log(chalk.dim('Growing intelligence through autonomous agents'));
129
+ console.log('');
130
+
131
+ showHelp();
132
+
133
+ let running = true;
134
+
135
+ while (running) {
136
+ try {
137
+ const response = await prompts({
138
+ type: 'text',
139
+ name: 'task',
140
+ message: chalk.green('🌱 What would you like to grow?'),
141
+ validate: (value) => value.length > 0 || 'Please enter a task',
142
+ });
143
+
144
+ if (!response.task) {
145
+ // User cancelled with Ctrl+C
146
+ running = false;
147
+ break;
148
+ }
149
+
150
+ const task = parseTask(response.task);
151
+
152
+ if (task.type === 'help') {
153
+ showHelp();
154
+ continue;
155
+ }
156
+
157
+ if (task.type === 'exit') {
158
+ running = false;
159
+ break;
160
+ }
161
+
162
+ if (task.type === 'analyze') {
163
+ if (!task.file) {
164
+ console.log(chalk.yellow('⚠️ Please specify a file to analyze'));
165
+ continue;
166
+ }
167
+
168
+ const spinner = ora(`Analyzing ${task.file}...`).start();
169
+ try {
170
+ const gradient = await calculateGradient(task.file);
171
+ spinner.succeed(chalk.green(`Analysis complete for ${task.file}`));
172
+
173
+ console.log('');
174
+ console.log(chalk.bold('📊 Gradient Analysis:'));
175
+ console.log(chalk.dim('─'.repeat(50)));
176
+ console.log(`${chalk.bold('Overall Score:')} ${gradient.score.toFixed(3)}`);
177
+ console.log(`${chalk.bold('Dominant Signal:')} ${gradient.dominantSignal.name}`);
178
+ console.log('');
179
+ console.log(chalk.dim('Signal Breakdown:'));
180
+ console.log(` Complexity: ${gradient.signals.complexity.toFixed(3)}`);
181
+ console.log(` Churn: ${gradient.signals.churn.toFixed(3)}`);
182
+ console.log(` Debt: ${gradient.signals.debt.toFixed(3)}`);
183
+ console.log(` Error Rate: ${gradient.signals.error_rate.toFixed(3)}`);
184
+ console.log(` Centrality: ${gradient.signals.centrality.toFixed(3)}`);
185
+ console.log('');
186
+ } catch (error) {
187
+ spinner.fail(chalk.red('Analysis failed'));
188
+ console.error(chalk.red(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
189
+ }
190
+ continue;
191
+ }
192
+
193
+ if (task.type === 'task') {
194
+ if (!task.description) {
195
+ console.log(chalk.yellow('⚠️ Could not determine task description'));
196
+ continue;
197
+ }
198
+
199
+ const spinner = ora('Planning task...').start();
200
+ try {
201
+ // Create task object
202
+ const newTask: Task = {
203
+ id: randomUUID(),
204
+ description: task.description,
205
+ status: 'planning',
206
+ created_at: new Date().toISOString(),
207
+ acceptance_criteria: [],
208
+ steps_completed: 0,
209
+ steps_total: 0,
210
+ files_created: [],
211
+ files_modified: [],
212
+ traces: [],
213
+ };
214
+
215
+ spinner.text = 'Generating task plan...';
216
+ const plan = await planTask(newTask);
217
+
218
+ newTask.plan = plan;
219
+ newTask.steps_total = plan.steps.length;
220
+ newTask.status = 'in_progress';
221
+
222
+ await storeTask(newTask);
223
+
224
+ spinner.succeed(chalk.green('Task plan generated'));
225
+ console.log('');
226
+ console.log(chalk.bold('📋 Task Plan:'));
227
+ console.log(chalk.dim('─'.repeat(50)));
228
+ console.log(`${chalk.bold('Summary:')} ${plan.summary}`);
229
+ console.log(`${chalk.bold('Steps:')} ${plan.steps.length}`);
230
+ console.log(`${chalk.bold('Complexity:')} ${plan.estimated_complexity}`);
231
+ if (plan.risks.length > 0) {
232
+ console.log(`${chalk.bold('Risks:')}`);
233
+ plan.risks.forEach(risk => console.log(chalk.yellow(` ⚠️ ${risk}`)));
234
+ }
235
+ console.log('');
236
+
237
+ // Show steps
238
+ console.log(chalk.bold('Steps:'));
239
+ plan.steps.forEach(step => {
240
+ console.log(` ${step.order}. ${step.description}`);
241
+ console.log(` ${chalk.dim(`File: ${step.target_file}, Mode: ${step.mode}`)}`);
242
+ });
243
+ console.log('');
244
+
245
+ const executeSpinner = ora('Executing task...').start();
246
+ const executedTask = await executeTask(newTask);
247
+
248
+ if (executedTask.status === 'completed') {
249
+ executeSpinner.succeed(chalk.green('Task completed successfully'));
250
+ console.log('');
251
+ console.log(chalk.bold('✅ Results:'));
252
+ console.log(chalk.dim('─'.repeat(50)));
253
+ console.log(`${chalk.bold('Task ID:')} ${executedTask.id}`);
254
+ console.log(`${chalk.bold('Steps:')} ${executedTask.steps_completed}/${executedTask.steps_total}`);
255
+ console.log(`${chalk.bold('Files Created:')} ${executedTask.files_created.length}`);
256
+ console.log(`${chalk.bold('Files Modified:')} ${executedTask.files_modified.length}`);
257
+ console.log('');
258
+
259
+ if (executedTask.files_created.length > 0) {
260
+ console.log(chalk.bold('Created:'));
261
+ executedTask.files_created.forEach(f => console.log(` ${chalk.green('+')} ${f}`));
262
+ }
263
+ if (executedTask.files_modified.length > 0) {
264
+ console.log(chalk.bold('Modified:'));
265
+ executedTask.files_modified.forEach(f => console.log(` ${chalk.yellow('~')} ${f}`));
266
+ }
267
+ console.log('');
268
+ console.log(chalk.green('💡 Task completed. Run tests to verify.'));
269
+ } else {
270
+ executeSpinner.fail(chalk.red('Task execution failed'));
271
+ console.error(chalk.red(`Error: ${executedTask.error || 'Unknown error'}`));
272
+ }
273
+
274
+ await storeTask(executedTask);
275
+ console.log('');
276
+ } catch (error) {
277
+ spinner.fail(chalk.red('Task execution failed'));
278
+ console.error(chalk.red(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
279
+ console.log('');
280
+ }
281
+ continue;
282
+ }
283
+
284
+ if (task.type === 'spawn') {
285
+ if (!task.file || !task.mode) {
286
+ console.log(chalk.yellow('⚠️ Could not determine file and mode from task'));
287
+ continue;
288
+ }
289
+
290
+ const spinner = ora(`Spawning ${task.mode} agent for ${task.file}...`).start();
291
+ try {
292
+ const result = await executeAgent(task.file, task.mode, { dryRun: false });
293
+
294
+ spinner.succeed(chalk.green(`Agent completed successfully`));
295
+ console.log('');
296
+ console.log(chalk.bold('✅ Results:'));
297
+ console.log(chalk.dim('─'.repeat(50)));
298
+ console.log(`${chalk.bold('File:')} ${task.file}`);
299
+ console.log(`${chalk.bold('Mode:')} ${task.mode}`);
300
+ console.log(`${chalk.bold('Changes:')} ${result.changes} modifications`);
301
+ console.log(`${chalk.bold('Cost:')} $${result.cost.toFixed(4)}`);
302
+ console.log(`${chalk.bold('Efficiency:')} ${result.trace?.efficiency.toFixed(3) || 'N/A'}`);
303
+ console.log('');
304
+
305
+ if (result.changes && result.changes.length > 0) {
306
+ console.log(chalk.green('💡 Changes have been applied. Run tests to verify.'));
307
+ } else {
308
+ console.log(chalk.yellow('ℹ️ No changes were needed.'));
309
+ }
310
+ console.log('');
311
+ } catch (error) {
312
+ spinner.fail(chalk.red('Agent execution failed'));
313
+ console.error(chalk.red(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
314
+ console.log('');
315
+ }
316
+ continue;
317
+ }
318
+ } catch (error) {
319
+ if (error instanceof Error && error.message.includes('User force closed')) {
320
+ running = false;
321
+ break;
322
+ }
323
+ console.error(chalk.red(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
324
+ }
325
+ }
326
+
327
+ console.log('');
328
+ console.log(chalk.green('🍄 Mycelium network dormant. Growth complete.'));
329
+ console.log('');
330
+ }
331
+
332
+ /**
333
+ * Commander.js command definition
334
+ */
335
+ const command = new Command()
336
+ .name('grow')
337
+ .description('Start interactive mycelium growth interface')
338
+ .option('--task <description>', 'Run a single task and exit')
339
+ .action(async (options: { task?: string }) => {
340
+ if (options.task) {
341
+ // Non-interactive mode - single task
342
+ const task = parseTask(options.task);
343
+
344
+ if (task.type === 'task' && task.description) {
345
+ const spinner = ora('Planning task...').start();
346
+ try {
347
+ // Create and execute task
348
+ const newTask: Task = {
349
+ id: randomUUID(),
350
+ description: task.description,
351
+ status: 'planning',
352
+ created_at: new Date().toISOString(),
353
+ acceptance_criteria: [],
354
+ steps_completed: 0,
355
+ steps_total: 0,
356
+ files_created: [],
357
+ files_modified: [],
358
+ traces: [],
359
+ };
360
+
361
+ const plan = await planTask(newTask);
362
+ newTask.plan = plan;
363
+ newTask.steps_total = plan.steps.length;
364
+ newTask.status = 'in_progress';
365
+ await storeTask(newTask);
366
+
367
+ spinner.text = 'Executing task...';
368
+ const executedTask = await executeTask(newTask);
369
+ await storeTask(executedTask);
370
+
371
+ if (executedTask.status === 'completed') {
372
+ spinner.succeed(chalk.green('Task completed'));
373
+ console.log(`Steps: ${executedTask.steps_completed}/${executedTask.steps_total}, Created: ${executedTask.files_created.length}, Modified: ${executedTask.files_modified.length}`);
374
+ } else {
375
+ spinner.fail(chalk.red('Task failed'));
376
+ console.error(chalk.red(`Error: ${executedTask.error || 'Unknown error'}`));
377
+ process.exit(1);
378
+ }
379
+ } catch (error) {
380
+ spinner.fail(chalk.red('Task failed'));
381
+ console.error(chalk.red(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
382
+ process.exit(1);
383
+ }
384
+ } else if (task.type === 'spawn' && task.file && task.mode) {
385
+ const spinner = ora(`Spawning ${task.mode} agent...`).start();
386
+ try {
387
+ const result = await executeAgent(task.file, task.mode, { dryRun: false });
388
+ spinner.succeed(chalk.green('Agent completed'));
389
+ console.log(`Changes: ${result.changes}, Cost: $${result.cost.toFixed(4)}, Efficiency: ${result.trace?.efficiency.toFixed(3) || 'N/A'}`);
390
+ } catch (error) {
391
+ spinner.fail(chalk.red('Agent failed'));
392
+ console.error(chalk.red(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
393
+ process.exit(1);
394
+ }
395
+ } else if (task.type === 'analyze' && task.file) {
396
+ const spinner = ora(`Analyzing ${task.file}...`).start();
397
+ try {
398
+ const gradient = await calculateGradient(task.file);
399
+ spinner.succeed(chalk.green('Analysis complete'));
400
+ console.log(`Score: ${gradient.score.toFixed(3)}, Dominant: ${gradient.dominantSignal.name}`);
401
+ } catch (error) {
402
+ spinner.fail(chalk.red('Analysis failed'));
403
+ console.error(chalk.red(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`));
404
+ process.exit(1);
405
+ }
406
+ } else {
407
+ console.log(chalk.yellow('Could not parse task. Use interactive mode for help.'));
408
+ process.exit(1);
409
+ }
410
+ } else {
411
+ // Interactive TUI mode
412
+ await startGrowSession();
413
+ }
414
+ });
415
+
416
+ export default command;
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Claude Mycelium CLI
3
+ *
4
+ * Main CLI entry point using Commander.js
5
+ * Coordinates all CLI subcommands
6
+ */
7
+
8
+ import { Command } from 'commander';
9
+ import { readFileSync } from 'fs';
10
+ import { resolve } from 'path';
11
+ import { fileURLToPath } from 'url';
12
+ import { dirname } from 'path';
13
+
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ const __dirname = dirname(__filename);
16
+
17
+ // Read package.json for version
18
+ let version = '2.0.0';
19
+ try {
20
+ const pkgPath = resolve(__dirname, '../../package.json');
21
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')) as { version?: string };
22
+ if (pkg.version) {
23
+ version = pkg.version;
24
+ }
25
+ } catch {
26
+ // Fallback to default version
27
+ }
28
+
29
+ export const program = new Command();
30
+
31
+ program
32
+ .name('claude-mycelium')
33
+ .description('Intelligent automated code improvement system')
34
+ .version(version)
35
+ .option('-v, --verbose', 'Enable verbose logging')
36
+ .option('--config <path>', 'Path to config file')
37
+ .hook('preAction', (thisCommand) => {
38
+ // Handle global options
39
+ if (thisCommand.optsWithGlobals().verbose) {
40
+ process.env.DEBUG = '1';
41
+ }
42
+ if (thisCommand.optsWithGlobals().config) {
43
+ process.env.MYCELIUM_CONFIG = thisCommand.optsWithGlobals().config;
44
+ }
45
+ });
46
+
47
+ /**
48
+ * Import and register subcommands
49
+ */
50
+ export async function registerCommands() {
51
+ // Dynamically import subcommands
52
+ const { default: gradients } = await import('./gradients.js');
53
+ const { default: cost } = await import('./cost.js');
54
+ const { default: status } = await import('./status.js');
55
+ const { default: init } = await import('./init.js');
56
+ const { default: gc } = await import('./gc.js');
57
+ const { default: grow } = await import('./grow.js');
58
+
59
+ program.addCommand(gradients);
60
+ program.addCommand(cost);
61
+ program.addCommand(status);
62
+ program.addCommand(init);
63
+ program.addCommand(gc);
64
+ program.addCommand(grow);
65
+ }
66
+
67
+ /**
68
+ * Parse and execute CLI
69
+ */
70
+ export async function execute(argv?: string[]) {
71
+ await registerCommands();
72
+ return program.parseAsync(argv || process.argv);
73
+ }
74
+
75
+ // Auto-execute when run as script
76
+ if (import.meta.url === `file://${process.argv[1]}`) {
77
+ execute().catch((error) => {
78
+ console.error('CLI Error:', error);
79
+ process.exit(1);
80
+ });
81
+ }
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Init CLI Command
3
+ *
4
+ * Initialize .agent-meta/ directory structure
5
+ * Create default configuration files
6
+ * Per Phase 7 specification §15
7
+ */
8
+
9
+ import { Command } from 'commander';
10
+ import chalk from 'chalk';
11
+ import * as fs from 'fs/promises';
12
+ import * as path from 'path';
13
+ import { fileExists } from '../utils/file-utils.js';
14
+
15
+ const command = new Command()
16
+ .name('init')
17
+ .description('Initialize .agent-meta/ in current directory')
18
+ .option('--force', 'Reinitialize even if exists')
19
+ .action(async (options: any) => {
20
+ try {
21
+ await initCommand(options.force);
22
+ } catch (error) {
23
+ const message = error instanceof Error ? error.message : 'Unknown error';
24
+ console.error(chalk.red(`Error: ${message}`));
25
+ process.exit(1);
26
+ }
27
+ });
28
+
29
+ /**
30
+ * Initialize .agent-meta/ directory and configuration
31
+ */
32
+ async function initCommand(force: boolean = false): Promise<void> {
33
+ const metaDir = '.agent-meta';
34
+
35
+ // Check if already initialized
36
+ if (fileExists(metaDir) && !force) {
37
+ console.log(
38
+ chalk.yellow('Already initialized. Use --force to reinitialize.')
39
+ );
40
+ return;
41
+ }
42
+
43
+ // Create directory structure
44
+ await fs.mkdir(metaDir, { recursive: true });
45
+ await fs.mkdir(path.join(metaDir, 'traces'), { recursive: true });
46
+ await fs.mkdir(path.join(metaDir, 'tasks'), { recursive: true });
47
+ await fs.mkdir(path.join(metaDir, 'locks'), { recursive: true });
48
+
49
+ // Create initial config file
50
+ await fs.writeFile(
51
+ path.join(metaDir, 'config.json'),
52
+ JSON.stringify(
53
+ {
54
+ version: '2.0.0',
55
+ created_at: new Date().toISOString(),
56
+ error_provider: 'file',
57
+ error_file: '.agent-meta/_errors.json',
58
+ ci_command: 'npm test && npm run lint',
59
+ spawn_count: 0,
60
+ last_gc_at_spawn: 0,
61
+ },
62
+ null,
63
+ 2
64
+ ) + '\n'
65
+ );
66
+
67
+ // Create quarantine file
68
+ await fs.writeFile(
69
+ path.join(metaDir, '_quarantine.json'),
70
+ JSON.stringify(
71
+ {
72
+ updated_at: new Date().toISOString(),
73
+ entries: [],
74
+ },
75
+ null,
76
+ 2
77
+ ) + '\n'
78
+ );
79
+
80
+ // Create inhibitors file
81
+ await fs.writeFile(path.join(metaDir, '_inhibitors.ndjson'), '');
82
+
83
+ // Create errors file
84
+ await fs.writeFile(
85
+ path.join(metaDir, '_errors.json'),
86
+ JSON.stringify(
87
+ {
88
+ updated_at: new Date().toISOString(),
89
+ errors: [],
90
+ },
91
+ null,
92
+ 2
93
+ ) + '\n'
94
+ );
95
+
96
+ // Create GC log file
97
+ await fs.writeFile(path.join(metaDir, '_gc.log'), '');
98
+
99
+ // Add to .gitignore if needed
100
+ const gitignorePath = '.gitignore';
101
+ const lockPattern = '.agent-meta/locks/';
102
+
103
+ try {
104
+ const gitignore = await fs.readFile(gitignorePath, 'utf-8');
105
+ if (!gitignore.includes(lockPattern)) {
106
+ await fs.appendFile(
107
+ gitignorePath,
108
+ `\n# Claude Mycelium locks (ephemeral)\n${lockPattern}\n`
109
+ );
110
+ }
111
+ } catch {
112
+ // No .gitignore, create one
113
+ await fs.writeFile(
114
+ gitignorePath,
115
+ `# Claude Mycelium locks (ephemeral)\n${lockPattern}\n`
116
+ );
117
+ }
118
+
119
+ // Success message
120
+ console.log('');
121
+ console.log(chalk.green('✓ Initialized .agent-meta/'));
122
+ console.log(
123
+ chalk.dim(' - Created directory structure (traces, tasks, locks)')
124
+ );
125
+ console.log(chalk.dim(' - Created config.json'));
126
+ console.log(chalk.dim(' - Created _quarantine.json'));
127
+ console.log(chalk.dim(' - Created _inhibitors.ndjson'));
128
+ console.log(chalk.dim(' - Created _errors.json'));
129
+ console.log(chalk.dim(' - Created _gc.log'));
130
+ console.log(chalk.dim(' - Updated .gitignore'));
131
+ console.log('');
132
+ console.log(chalk.bold('Next steps:'));
133
+ console.log(' 1. Set ANTHROPIC_API_KEY environment variable');
134
+ console.log(' 2. Run: npx claude-mycelium gradients ./src');
135
+ console.log(' 3. Check system status: npx claude-mycelium status');
136
+ console.log('');
137
+ }
138
+
139
+ export default command;