claude-mycelium 2.0.0 → 2.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 (189) 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/.claude/memory.db +0 -0
  5. package/.claude/settings.local.json +4 -1
  6. package/README.md +81 -235
  7. package/SECURITY.md +145 -0
  8. package/dist/agent/worker.d.ts +8 -0
  9. package/dist/agent/worker.d.ts.map +1 -0
  10. package/dist/agent/worker.js +97 -0
  11. package/dist/agent/worker.js.map +1 -0
  12. package/dist/bin.d.ts +7 -0
  13. package/dist/bin.d.ts.map +1 -0
  14. package/dist/bin.js +11 -0
  15. package/dist/bin.js.map +1 -0
  16. package/dist/cli/cost.d.ts +10 -0
  17. package/dist/cli/cost.d.ts.map +1 -0
  18. package/dist/cli/cost.js +163 -0
  19. package/dist/cli/cost.js.map +1 -0
  20. package/dist/cli/gc.d.ts +10 -0
  21. package/dist/cli/gc.d.ts.map +1 -0
  22. package/dist/cli/gc.js +108 -0
  23. package/dist/cli/gc.js.map +1 -0
  24. package/dist/cli/gradients.d.ts +10 -0
  25. package/dist/cli/gradients.d.ts.map +1 -0
  26. package/dist/cli/gradients.js +69 -0
  27. package/dist/cli/gradients.js.map +1 -0
  28. package/dist/cli/index.d.ts +17 -0
  29. package/dist/cli/index.d.ts.map +1 -0
  30. package/dist/cli/index.js +72 -0
  31. package/dist/cli/index.js.map +1 -0
  32. package/dist/cli/init.d.ts +11 -0
  33. package/dist/cli/init.d.ts.map +1 -0
  34. package/dist/cli/init.js +97 -0
  35. package/dist/cli/init.js.map +1 -0
  36. package/dist/cli/status.d.ts +10 -0
  37. package/dist/cli/status.d.ts.map +1 -0
  38. package/dist/cli/status.js +191 -0
  39. package/dist/cli/status.js.map +1 -0
  40. package/dist/coordination/file-locks.d.ts +42 -0
  41. package/dist/coordination/file-locks.d.ts.map +1 -0
  42. package/dist/coordination/file-locks.js +269 -0
  43. package/dist/coordination/file-locks.js.map +1 -0
  44. package/dist/coordination/index.d.ts +4 -0
  45. package/dist/coordination/index.d.ts.map +1 -1
  46. package/dist/coordination/index.js +4 -0
  47. package/dist/coordination/index.js.map +1 -1
  48. package/dist/coordination/inhibitors.d.ts +84 -0
  49. package/dist/coordination/inhibitors.d.ts.map +1 -0
  50. package/dist/coordination/inhibitors.js +290 -0
  51. package/dist/coordination/inhibitors.js.map +1 -0
  52. package/dist/coordination/process-manager.d.ts +73 -0
  53. package/dist/coordination/process-manager.d.ts.map +1 -0
  54. package/dist/coordination/process-manager.js +144 -0
  55. package/dist/coordination/process-manager.js.map +1 -0
  56. package/dist/core/agent-executor.d.ts.map +1 -1
  57. package/dist/core/agent-executor.js +28 -10
  58. package/dist/core/agent-executor.js.map +1 -1
  59. package/dist/core/change-applier.d.ts +29 -5
  60. package/dist/core/change-applier.d.ts.map +1 -1
  61. package/dist/core/change-applier.js +254 -24
  62. package/dist/core/change-applier.js.map +1 -1
  63. package/dist/core/signals/churn.d.ts.map +1 -1
  64. package/dist/core/signals/churn.js +6 -4
  65. package/dist/core/signals/churn.js.map +1 -1
  66. package/dist/core/signals/debt.d.ts.map +1 -1
  67. package/dist/core/signals/debt.js +4 -3
  68. package/dist/core/signals/debt.js.map +1 -1
  69. package/dist/cost/cost-tracker.d.ts.map +1 -1
  70. package/dist/cost/cost-tracker.js +2 -0
  71. package/dist/cost/cost-tracker.js.map +1 -1
  72. package/dist/gc/index.d.ts +17 -0
  73. package/dist/gc/index.d.ts.map +1 -0
  74. package/dist/gc/index.js +17 -0
  75. package/dist/gc/index.js.map +1 -0
  76. package/dist/gc/runner.d.ts +39 -0
  77. package/dist/gc/runner.d.ts.map +1 -0
  78. package/dist/gc/runner.js +277 -0
  79. package/dist/gc/runner.js.map +1 -0
  80. package/dist/gc/trace-compactor.d.ts +31 -0
  81. package/dist/gc/trace-compactor.d.ts.map +1 -0
  82. package/dist/gc/trace-compactor.js +162 -0
  83. package/dist/gc/trace-compactor.js.map +1 -0
  84. package/dist/index.d.ts +5 -1
  85. package/dist/index.d.ts.map +1 -1
  86. package/dist/index.js +6 -1
  87. package/dist/index.js.map +1 -1
  88. package/dist/prompts/index.d.ts +2 -1
  89. package/dist/prompts/index.d.ts.map +1 -1
  90. package/dist/prompts/index.js.map +1 -1
  91. package/dist/quarantine/explorer.d.ts +65 -0
  92. package/dist/quarantine/explorer.d.ts.map +1 -0
  93. package/dist/quarantine/explorer.js +175 -0
  94. package/dist/quarantine/explorer.js.map +1 -0
  95. package/dist/quarantine/index.d.ts +7 -0
  96. package/dist/quarantine/index.d.ts.map +1 -0
  97. package/dist/quarantine/index.js +7 -0
  98. package/dist/quarantine/index.js.map +1 -0
  99. package/dist/quarantine/manager.d.ts +75 -0
  100. package/dist/quarantine/manager.d.ts.map +1 -0
  101. package/dist/quarantine/manager.js +275 -0
  102. package/dist/quarantine/manager.js.map +1 -0
  103. package/dist/task/acceptance.d.ts +29 -0
  104. package/dist/task/acceptance.d.ts.map +1 -0
  105. package/dist/task/acceptance.js +228 -0
  106. package/dist/task/acceptance.js.map +1 -0
  107. package/dist/task/executor.d.ts +30 -0
  108. package/dist/task/executor.d.ts.map +1 -0
  109. package/dist/task/executor.js +429 -0
  110. package/dist/task/executor.js.map +1 -0
  111. package/dist/task/index.d.ts +12 -0
  112. package/dist/task/index.d.ts.map +1 -0
  113. package/dist/task/index.js +12 -0
  114. package/dist/task/index.js.map +1 -0
  115. package/dist/task/planner.d.ts +21 -0
  116. package/dist/task/planner.d.ts.map +1 -0
  117. package/dist/task/planner.js +253 -0
  118. package/dist/task/planner.js.map +1 -0
  119. package/dist/task/storage.d.ts +46 -0
  120. package/dist/task/storage.d.ts.map +1 -0
  121. package/dist/task/storage.js +266 -0
  122. package/dist/task/storage.js.map +1 -0
  123. package/dist/trace/trace-event.d.ts +2 -18
  124. package/dist/trace/trace-event.d.ts.map +1 -1
  125. package/dist/trace/trace-event.js +6 -6
  126. package/dist/trace/trace-event.js.map +1 -1
  127. package/dist/utils/file-utils.d.ts.map +1 -1
  128. package/dist/utils/file-utils.js +54 -15
  129. package/dist/utils/file-utils.js.map +1 -1
  130. package/docs/PHASE5_IMPLEMENTATION.md +237 -0
  131. package/docs/PHASES-3-7-COMPLETE.md +177 -0
  132. package/docs/PHASE_4_COMPLETE.md +135 -0
  133. package/docs/PHASE_7_DELIVERABLES.md +295 -0
  134. package/docs/PHASE_7_IMPLEMENTATION.md +306 -0
  135. package/docs/PHASE_7_SUMMARY.txt +195 -0
  136. package/docs/RELEASE-NOTES-v2.1.md +213 -0
  137. package/docs/ROADMAP.md +64 -57
  138. package/docs/SECURITY-AUDIT.md +387 -0
  139. package/docs/SNAPSHOT.md +59 -32
  140. package/docs/implementation/phase3-summary.md +220 -0
  141. package/package.json +19 -11
  142. package/src/agent/worker.ts +111 -0
  143. package/src/bin.ts +13 -0
  144. package/src/cli/cost.ts +210 -0
  145. package/src/cli/gc.ts +138 -0
  146. package/src/cli/gradients.ts +95 -0
  147. package/src/cli/index.ts +79 -0
  148. package/src/cli/init.ts +139 -0
  149. package/src/cli/status.ts +218 -0
  150. package/src/coordination/file-locks.ts +300 -0
  151. package/src/coordination/index.ts +4 -0
  152. package/src/coordination/inhibitors.ts +345 -0
  153. package/src/coordination/process-manager.ts +199 -0
  154. package/src/core/agent-executor.ts +20 -4
  155. package/src/core/signals/churn.ts +8 -5
  156. package/src/core/signals/debt.ts +4 -3
  157. package/src/cost/cost-tracker.ts +2 -0
  158. package/src/gc/index.ts +17 -0
  159. package/src/gc/runner.ts +314 -0
  160. package/src/gc/trace-compactor.ts +187 -0
  161. package/src/index.ts +7 -1
  162. package/src/prompts/index.ts +2 -1
  163. package/src/quarantine/explorer.ts +234 -0
  164. package/src/quarantine/index.ts +7 -0
  165. package/src/quarantine/manager.ts +336 -0
  166. package/src/task/acceptance.ts +267 -0
  167. package/src/task/executor.ts +538 -0
  168. package/src/task/index.ts +38 -0
  169. package/src/task/planner.ts +294 -0
  170. package/src/task/storage.ts +332 -0
  171. package/src/trace/trace-event.ts +7 -26
  172. package/src/utils/file-utils.ts +61 -15
  173. package/tests/cli/gc.test.ts +206 -0
  174. package/tests/cli/init.test.ts +181 -0
  175. package/tests/cli/status.test.ts +282 -0
  176. package/tests/coordination/file-locks.test.ts +196 -0
  177. package/tests/coordination/inhibitors.test.ts +459 -0
  178. package/tests/coordination/integration.test.ts +195 -0
  179. package/tests/coordination/process-manager.test.ts +165 -0
  180. package/tests/gc/trace-compactor.test.ts +245 -0
  181. package/tests/integration/phase-7.test.ts +145 -0
  182. package/tests/quarantine/explorer.test.ts +381 -0
  183. package/tests/quarantine/manager.test.ts +399 -0
  184. package/tests/security/command-injection.test.ts +88 -0
  185. package/tests/security/path-traversal.test.ts +103 -0
  186. package/tests/task/acceptance.test.ts +411 -0
  187. package/tests/task/executor.test.ts +421 -0
  188. package/tests/task/planner.test.ts +359 -0
  189. package/tsconfig.json +2 -2
@@ -0,0 +1,210 @@
1
+ /**
2
+ * Cost CLI Command
3
+ *
4
+ * Display spending report with daily summaries
5
+ * Show efficiency trends
6
+ */
7
+
8
+ import { Command } from 'commander';
9
+ import chalk from 'chalk';
10
+ import * as fs from 'fs/promises';
11
+ import * as path from 'path';
12
+ import type { TraceEvent, DailyCostSummary } from '../types/index.js';
13
+
14
+ const command = new Command()
15
+ .name('cost')
16
+ .description('Show spending report and efficiency trends')
17
+ .option('--days <number>', 'Show data for last N days', '30')
18
+ .option('--json', 'Output as JSON')
19
+ .action(async (options: any) => {
20
+ try {
21
+ const days = parseInt(options.days, 10) || 30;
22
+ const summary = await generateCostSummary(days);
23
+
24
+ if (options.json) {
25
+ console.log(JSON.stringify(summary, null, 2));
26
+ return;
27
+ }
28
+
29
+ // Colorized output
30
+ console.log('');
31
+ console.log(chalk.bold('Cost Report (Last ' + days + ' days)'));
32
+ console.log(chalk.dim('─'.repeat(80)));
33
+
34
+ // Overall totals
35
+ console.log(chalk.bold('Overall:'));
36
+ console.log(
37
+ ` Total cost: ${chalk.yellow('$' + summary.total_usd.toFixed(2))}`
38
+ );
39
+ console.log(
40
+ ` Total tokens in: ${summary.total_tokens_in.toLocaleString()}`
41
+ );
42
+ console.log(
43
+ ` Total tokens out: ${summary.total_tokens_out.toLocaleString()}`
44
+ );
45
+ console.log(
46
+ ` Avg cost per run: ${chalk.yellow('$' + summary.avg_cost_per_run.toFixed(4))}`
47
+ );
48
+ console.log(
49
+ ` Avg cost per improvement: ${chalk.yellow(
50
+ '$' + summary.avg_cost_per_improvement.toFixed(4)
51
+ )}`
52
+ );
53
+
54
+ // Trend
55
+ const trendColor =
56
+ summary.cost_efficiency_trend === 'improving'
57
+ ? 'green'
58
+ : summary.cost_efficiency_trend === 'stable'
59
+ ? 'yellow'
60
+ : 'red';
61
+ console.log(
62
+ ` Trend: ${chalk[trendColor](summary.cost_efficiency_trend)}`
63
+ );
64
+
65
+ console.log(chalk.dim('─'.repeat(80)));
66
+
67
+ // Daily breakdown
68
+ if (summary.daily.length > 0) {
69
+ console.log(chalk.bold('Daily Breakdown:'));
70
+ console.log(
71
+ chalk.gray(
72
+ `${'Date'.padEnd(12)} ${'Runs'.padStart(6)} ${'Cost'.padStart(12)} ${'Eff.'.padStart(8)}`
73
+ )
74
+ );
75
+
76
+ for (const day of summary.daily.slice(-7)) {
77
+ const costStr = ('$' + day.total_cost_usd.toFixed(2)).padStart(12);
78
+ const effStr = (day.avg_efficiency.toFixed(3)).padStart(8);
79
+ console.log(
80
+ `${day.date.padEnd(12)} ${String(day.runs).padStart(6)} ${costStr} ${effStr}`
81
+ );
82
+ }
83
+ }
84
+
85
+ console.log('');
86
+ } catch (error) {
87
+ const message = error instanceof Error ? error.message : 'Unknown error';
88
+ console.error(chalk.red(`Error: ${message}`));
89
+ process.exit(1);
90
+ }
91
+ });
92
+
93
+ /**
94
+ * Generate cost summary from trace events
95
+ */
96
+ async function generateCostSummary(days: number) {
97
+ const tracesDir = '.agent-meta/traces';
98
+ const now = Date.now();
99
+ const cutoffMs = days * 24 * 60 * 60 * 1000;
100
+
101
+ let traceFiles: string[] = [];
102
+ try {
103
+ const entries = await fs.readdir(tracesDir);
104
+ traceFiles = entries.filter(f => f.endsWith('.ndjson'));
105
+ } catch {
106
+ // Traces directory doesn't exist
107
+ }
108
+
109
+ const events: TraceEvent[] = [];
110
+
111
+ // Collect all trace events from last N days
112
+ for (const file of traceFiles) {
113
+ const filePath = path.join(tracesDir, file);
114
+
115
+ try {
116
+ const content = await fs.readFile(filePath, 'utf-8');
117
+ const lines = content.split('\n').filter(Boolean);
118
+
119
+ for (const line of lines) {
120
+ try {
121
+ const event = JSON.parse(line) as TraceEvent;
122
+ const ageMs = now - new Date(event.timestamp).getTime();
123
+
124
+ if (ageMs < cutoffMs) {
125
+ events.push(event);
126
+ }
127
+ } catch {
128
+ // Skip malformed lines
129
+ }
130
+ }
131
+ } catch {
132
+ // File read error
133
+ }
134
+ }
135
+
136
+ // Calculate daily summaries
137
+ const byDate = new Map<string, TraceEvent[]>();
138
+
139
+ for (const event of events) {
140
+ const date = new Date(event.timestamp).toISOString().split('T')[0];
141
+ if (!byDate.has(date)) {
142
+ byDate.set(date, []);
143
+ }
144
+ byDate.get(date)!.push(event);
145
+ }
146
+
147
+ const daily: DailyCostSummary[] = [];
148
+ let totalCost = 0;
149
+ let totalTokensIn = 0;
150
+ let totalTokensOut = 0;
151
+ let totalEfficiency = 0;
152
+ let improvementCount = 0;
153
+
154
+ for (const [date, dayEvents] of Array.from(byDate).sort()) {
155
+ const cost = dayEvents.reduce((sum, e) => sum + e.cost.estimated_usd, 0);
156
+ const tokensIn = dayEvents.reduce((sum, e) => sum + e.cost.tokens_in, 0);
157
+ const tokensOut = dayEvents.reduce((sum, e) => sum + e.cost.tokens_out, 0);
158
+ const avgEff = dayEvents.reduce((sum, e) => sum + e.efficiency, 0) / dayEvents.length;
159
+ const improvements = dayEvents.filter(e => e.gradient_delta > 0).length;
160
+
161
+ const dayData: DailyCostSummary = {
162
+ date,
163
+ runs: dayEvents.length,
164
+ total_cost_usd: cost,
165
+ total_tokens_in: tokensIn,
166
+ total_tokens_out: tokensOut,
167
+ avg_efficiency: avgEff,
168
+ cost_per_improvement: improvements > 0 ? cost / improvements : cost,
169
+ };
170
+
171
+ daily.push(dayData);
172
+ totalCost += cost;
173
+ totalTokensIn += tokensIn;
174
+ totalTokensOut += tokensOut;
175
+ totalEfficiency += avgEff;
176
+ improvementCount += improvements;
177
+ }
178
+
179
+ // Calculate trend (compare first half vs second half)
180
+ let trend: 'improving' | 'stable' | 'degrading' = 'stable';
181
+ if (daily.length >= 2) {
182
+ const mid = Math.floor(daily.length / 2);
183
+ const firstHalf = daily.slice(0, mid);
184
+ const secondHalf = daily.slice(mid);
185
+
186
+ const firstAvgEff =
187
+ firstHalf.reduce((sum, d) => sum + d.avg_efficiency, 0) / firstHalf.length;
188
+ const secondAvgEff =
189
+ secondHalf.reduce((sum, d) => sum + d.avg_efficiency, 0) / secondHalf.length;
190
+
191
+ if (secondAvgEff > firstAvgEff * 1.1) {
192
+ trend = 'improving';
193
+ } else if (secondAvgEff < firstAvgEff * 0.9) {
194
+ trend = 'degrading';
195
+ }
196
+ }
197
+
198
+ return {
199
+ total_usd: totalCost,
200
+ total_tokens_in: totalTokensIn,
201
+ total_tokens_out: totalTokensOut,
202
+ avg_cost_per_run: events.length > 0 ? totalCost / events.length : 0,
203
+ avg_cost_per_improvement:
204
+ improvementCount > 0 ? totalCost / improvementCount : totalCost,
205
+ cost_efficiency_trend: trend,
206
+ daily,
207
+ };
208
+ }
209
+
210
+ export default command;
package/src/cli/gc.ts ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * GC CLI Command
3
+ *
4
+ * Manual garbage collection trigger
5
+ * Show GC report and statistics
6
+ */
7
+
8
+ import { Command } from 'commander';
9
+ import chalk from 'chalk';
10
+ import { runGC, readGCLog } from '../gc/runner.js';
11
+
12
+ const command = new Command()
13
+ .name('gc')
14
+ .description('Run garbage collection manually')
15
+ .option('--json', 'Output as JSON')
16
+ .option('--history', 'Show GC history')
17
+ .action(async (options: any) => {
18
+ try {
19
+ if (options.history) {
20
+ await showGCHistory(options.json);
21
+ return;
22
+ }
23
+
24
+ await runGarbageCollection(options.json);
25
+ } catch (error) {
26
+ const message = error instanceof Error ? error.message : 'Unknown error';
27
+ console.error(chalk.red(`Error: ${message}`));
28
+ process.exit(1);
29
+ }
30
+ });
31
+
32
+ /**
33
+ * Run garbage collection and show report
34
+ */
35
+ async function runGarbageCollection(json: boolean = false): Promise<void> {
36
+ if (!json) {
37
+ console.log('');
38
+ console.log(chalk.bold('Running garbage collection...'));
39
+ }
40
+
41
+ const report = await runGC();
42
+
43
+ if (json) {
44
+ console.log(JSON.stringify(report, null, 2));
45
+ return;
46
+ }
47
+
48
+ // Display report
49
+ console.log(chalk.green('✓ GC completed'));
50
+ console.log('');
51
+
52
+ console.log(chalk.bold('Report:'));
53
+ console.log(` Timestamp: ${report.timestamp}`);
54
+ console.log(` Duration: ${report.duration_ms}ms`);
55
+ console.log(` Size before: ${report.size_before_mb.toFixed(2)} MB`);
56
+ console.log(` Size after: ${report.size_after_mb.toFixed(2)} MB`);
57
+
58
+ if (report.size_before_mb > 0) {
59
+ const saved = report.size_before_mb - report.size_after_mb;
60
+ const percent = ((saved / report.size_before_mb) * 100).toFixed(1);
61
+ console.log(
62
+ ` Space saved: ${chalk.green(saved.toFixed(2) + ' MB')} (${percent}%)`
63
+ );
64
+ }
65
+
66
+ console.log('');
67
+ console.log(chalk.bold('Actions:'));
68
+ console.log(` Deleted items: ${report.deleted}`);
69
+ console.log(` Compacted files: ${report.compacted}`);
70
+
71
+ if (report.errors.length > 0) {
72
+ console.log('');
73
+ console.log(chalk.yellow('Warnings:'));
74
+ for (const error of report.errors) {
75
+ console.log(` - ${error}`);
76
+ }
77
+ }
78
+
79
+ console.log('');
80
+ }
81
+
82
+ /**
83
+ * Show GC history
84
+ */
85
+ async function showGCHistory(json: boolean = false): Promise<void> {
86
+ const reports = await readGCLog();
87
+
88
+ if (json) {
89
+ console.log(JSON.stringify(reports, null, 2));
90
+ return;
91
+ }
92
+
93
+ if (reports.length === 0) {
94
+ console.log(chalk.yellow('No GC history found'));
95
+ return;
96
+ }
97
+
98
+ console.log('');
99
+ console.log(chalk.bold('GC History'));
100
+ console.log(chalk.dim('─'.repeat(80)));
101
+ console.log(
102
+ chalk.gray(
103
+ `${'Timestamp'.padEnd(25)} ${'Duration'.padStart(10)} ${'Deleted'.padStart(10)} ${'Space'.padStart(15)}`
104
+ )
105
+ );
106
+ console.log(chalk.dim('─'.repeat(80)));
107
+
108
+ for (const report of reports.slice(-10)) {
109
+ const date = new Date(report.timestamp).toISOString().split('T')[0];
110
+ const time = new Date(report.timestamp).toISOString().split('T')[1].substring(0, 5);
111
+ const timestamp = `${date} ${time}`.padEnd(25);
112
+ const duration = `${report.duration_ms}ms`.padStart(10);
113
+ const deleted = String(report.deleted).padStart(10);
114
+ const saved = (report.size_before_mb - report.size_after_mb).toFixed(2);
115
+ const space = `${saved}MB`.padStart(15);
116
+
117
+ console.log(`${timestamp} ${duration} ${deleted} ${space}`);
118
+ }
119
+
120
+ console.log(chalk.dim('─'.repeat(80)));
121
+
122
+ // Summary statistics
123
+ const totalDeleted = reports.reduce((sum, r) => sum + r.deleted, 0);
124
+ const totalSaved = reports.reduce(
125
+ (sum, r) => sum + (r.size_before_mb - r.size_after_mb),
126
+ 0
127
+ );
128
+ const avgDuration =
129
+ reports.reduce((sum, r) => sum + r.duration_ms, 0) / reports.length;
130
+
131
+ console.log(chalk.bold('Summary (all runs):'));
132
+ console.log(` Total items deleted: ${totalDeleted}`);
133
+ console.log(` Total space saved: ${totalSaved.toFixed(2)} MB`);
134
+ console.log(` Average duration: ${avgDuration.toFixed(0)}ms`);
135
+ console.log('');
136
+ }
137
+
138
+ export default command;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Gradients CLI Command
3
+ *
4
+ * Display gradient scores for all files
5
+ * Sorted by score descending with colorized output
6
+ */
7
+
8
+ import { Command } from 'commander';
9
+ import chalk from 'chalk';
10
+ import glob from 'fast-glob';
11
+ import { calculateGradientBatch } from '../core/gradient.js';
12
+
13
+ const command = new Command()
14
+ .name('gradients')
15
+ .description('Show gradient scores for all files')
16
+ .argument('[path]', 'Root path to analyze', '.')
17
+ .option('--json', 'Output as JSON')
18
+ .option('--limit <number>', 'Show top N files', '20')
19
+ .action(async (rootPath: string, options: any) => {
20
+ try {
21
+ // Find all TypeScript files (not .d.ts)
22
+ const files = await glob('**/*.ts', {
23
+ cwd: rootPath,
24
+ ignore: ['node_modules/**', 'dist/**', '**/*.d.ts', '**/*.test.ts', '**/*.spec.ts'],
25
+ });
26
+
27
+ if (files.length === 0) {
28
+ console.log(chalk.yellow('No TypeScript files found'));
29
+ return;
30
+ }
31
+
32
+ const gradientMap = await calculateGradientBatch(files);
33
+ const gradients = Array.from(gradientMap.values());
34
+
35
+ // Sort by score descending
36
+ const sorted = [...gradients].sort((a, b) => b.score - a.score);
37
+
38
+ // Apply limit
39
+ const limit = parseInt(options.limit, 10) || 20;
40
+ const limited = sorted.slice(0, limit);
41
+
42
+ if (options.json) {
43
+ console.log(JSON.stringify(limited, null, 2));
44
+ return;
45
+ }
46
+
47
+ // Colorized output
48
+ console.log('');
49
+ console.log(chalk.bold('Gradient Scores (sorted by impact)'));
50
+ console.log(chalk.dim('─'.repeat(80)));
51
+ console.log(
52
+ chalk.gray(
53
+ `${String('File').padEnd(50)} ${'Score'.padStart(10)} ${'Signal'.padStart(15)}`
54
+ )
55
+ );
56
+ console.log(chalk.dim('─'.repeat(80)));
57
+
58
+ for (const gradient of limited) {
59
+ const scoreColor = gradient.score > 0.7 ? 'red' : gradient.score > 0.4 ? 'yellow' : 'green';
60
+ const score = gradient.score.toFixed(3);
61
+ const signal = gradient.dominantSignal.name;
62
+
63
+ console.log(
64
+ `${gradient.file.substring(0, 50).padEnd(50)} ${chalk[scoreColor](
65
+ score.padStart(10)
66
+ )} ${signal.padStart(15)}`
67
+ );
68
+ }
69
+
70
+ console.log(chalk.dim('─'.repeat(80)));
71
+ console.log(
72
+ chalk.dim(`Showing ${limited.length} of ${gradients.length} files`)
73
+ );
74
+ console.log('');
75
+
76
+ // Summary
77
+ const highImpact = sorted.filter(g => g.score > 0.7).length;
78
+ const mediumImpact = sorted.filter(g => g.score > 0.4 && g.score <= 0.7).length;
79
+ const lowImpact = sorted.filter(g => g.score <= 0.4).length;
80
+
81
+ console.log(chalk.bold('Summary:'));
82
+ console.log(
83
+ ` ${chalk.red(`High impact: ${highImpact}`)} | ${chalk.yellow(
84
+ `Medium: ${mediumImpact}`
85
+ )} | ${chalk.green(`Low: ${lowImpact}`)}`
86
+ );
87
+ console.log('');
88
+ } catch (error) {
89
+ const message = error instanceof Error ? error.message : 'Unknown error';
90
+ console.error(chalk.red(`Error: ${message}`));
91
+ process.exit(1);
92
+ }
93
+ });
94
+
95
+ export default command;
@@ -0,0 +1,79 @@
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
+
58
+ program.addCommand(gradients);
59
+ program.addCommand(cost);
60
+ program.addCommand(status);
61
+ program.addCommand(init);
62
+ program.addCommand(gc);
63
+ }
64
+
65
+ /**
66
+ * Parse and execute CLI
67
+ */
68
+ export async function execute(argv?: string[]) {
69
+ await registerCommands();
70
+ return program.parseAsync(argv || process.argv);
71
+ }
72
+
73
+ // Auto-execute when run as script
74
+ if (import.meta.url === `file://${process.argv[1]}`) {
75
+ execute().catch((error) => {
76
+ console.error('CLI Error:', error);
77
+ process.exit(1);
78
+ });
79
+ }
@@ -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;