autonomous-flow-daemon 1.1.0 → 1.6.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 (55) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.ko.md +124 -164
  3. package/README.md +99 -170
  4. package/package.json +11 -5
  5. package/src/adapters/index.ts +246 -35
  6. package/src/cli.ts +71 -1
  7. package/src/commands/benchmark.ts +187 -0
  8. package/src/commands/diagnose.ts +56 -14
  9. package/src/commands/doctor.ts +243 -0
  10. package/src/commands/evolution.ts +107 -0
  11. package/src/commands/fix.ts +22 -2
  12. package/src/commands/hooks.ts +136 -0
  13. package/src/commands/mcp.ts +129 -0
  14. package/src/commands/restart.ts +14 -0
  15. package/src/commands/score.ts +164 -96
  16. package/src/commands/start.ts +74 -15
  17. package/src/commands/stats.ts +103 -0
  18. package/src/commands/status.ts +157 -0
  19. package/src/commands/stop.ts +23 -4
  20. package/src/commands/sync.ts +253 -20
  21. package/src/commands/vaccine.ts +177 -0
  22. package/src/constants.ts +25 -1
  23. package/src/core/boast.ts +27 -12
  24. package/src/core/db.ts +74 -3
  25. package/src/core/evolution.ts +215 -0
  26. package/src/core/hologram/engine.ts +71 -0
  27. package/src/core/hologram/fallback.ts +11 -0
  28. package/src/core/hologram/incremental.ts +227 -0
  29. package/src/core/hologram/py-extractor.ts +132 -0
  30. package/src/core/hologram/ts-extractor.ts +320 -0
  31. package/src/core/hologram/types.ts +25 -0
  32. package/src/core/hologram.ts +64 -236
  33. package/src/core/hook-manager.ts +259 -0
  34. package/src/core/i18n/messages.ts +43 -0
  35. package/src/core/immune.ts +8 -123
  36. package/src/core/log-rotate.ts +33 -0
  37. package/src/core/log-utils.ts +38 -0
  38. package/src/core/lru-map.ts +61 -0
  39. package/src/core/notify.ts +27 -19
  40. package/src/core/rule-engine.ts +287 -0
  41. package/src/core/semantic-diff.ts +432 -0
  42. package/src/core/telemetry.ts +94 -0
  43. package/src/core/vaccine-registry.ts +212 -0
  44. package/src/core/workspace.ts +28 -0
  45. package/src/core/yaml-minimal.ts +176 -0
  46. package/src/daemon/client.ts +34 -6
  47. package/src/daemon/event-batcher.ts +108 -0
  48. package/src/daemon/guards.ts +13 -0
  49. package/src/daemon/http-routes.ts +293 -0
  50. package/src/daemon/mcp-handler.ts +270 -0
  51. package/src/daemon/server.ts +439 -353
  52. package/src/daemon/types.ts +100 -0
  53. package/src/daemon/workspace-map.ts +92 -0
  54. package/src/platform.ts +23 -2
  55. package/src/version.ts +15 -0
@@ -1,6 +1,13 @@
1
1
  import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
2
2
  import { join, dirname } from "path";
3
3
  import { resolveHookCommand } from "../platform";
4
+ import {
5
+ readHooksFile,
6
+ writeHooksFile,
7
+ mergeHooks,
8
+ getAfdDesiredHooks,
9
+ KNOWN_AFD_HOOKS,
10
+ } from "../core/hook-manager";
4
11
 
5
12
  export interface HarnessSchema {
6
13
  configFiles: string[];
@@ -15,6 +22,9 @@ export interface EcosystemAdapter {
15
22
  getHarnessSchema(): HarnessSchema;
16
23
  injectHooks?(cwd: string): { injected: boolean; message: string };
17
24
  configureStatusLine?(cwd: string): { configured: boolean; message: string };
25
+ registerMcp?(cwd: string): { registered: boolean; message: string };
26
+ removeHooks?(cwd: string): { removed: boolean; message: string };
27
+ unregisterMcp?(cwd: string): { removed: boolean; message: string };
18
28
  }
19
29
 
20
30
  const AFD_HOOK_MARKER = "afd-auto-heal";
@@ -49,45 +59,27 @@ export const ClaudeCodeAdapter: EcosystemAdapter = {
49
59
  },
50
60
  injectHooks(cwd: string): { injected: boolean; message: string } {
51
61
  const hooksPath = join(cwd, ".claude", "hooks.json");
52
- const hookCommand = resolveHookCommand();
53
-
54
- const newHook: HookEntry = {
55
- id: AFD_HOOK_MARKER,
56
- matcher: "",
57
- command: hookCommand,
58
- };
59
-
60
- let config: HooksConfig;
61
- if (existsSync(hooksPath)) {
62
- try {
63
- config = JSON.parse(readFileSync(hooksPath, "utf-8"));
64
- } catch {
65
- config = { hooks: {} };
66
- }
67
- } else {
68
- mkdirSync(dirname(hooksPath), { recursive: true });
69
- config = { hooks: {} };
70
- }
62
+ const config = readHooksFile(hooksPath);
71
63
 
72
64
  if (!config.hooks || Array.isArray(config.hooks) || typeof config.hooks !== "object") {
73
65
  config.hooks = {};
74
66
  }
75
67
  if (!config.hooks.PreToolUse) config.hooks.PreToolUse = [];
76
68
 
77
- // Check if already injected
78
- const existing = config.hooks.PreToolUse.find(
79
- (h: HookEntry) => h.id === AFD_HOOK_MARKER
80
- );
81
- if (existing) {
82
- // Update command in case path changed
83
- existing.command = hookCommand;
84
- writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
85
- return { injected: false, message: "Auto-heal hook already present (updated)" };
86
- }
69
+ const before = config.hooks.PreToolUse.length;
70
+ const result = mergeHooks(config.hooks.PreToolUse, getAfdDesiredHooks());
71
+ config.hooks.PreToolUse = result.merged;
72
+ writeHooksFile(hooksPath, config);
87
73
 
88
- config.hooks.PreToolUse.push(newHook);
89
- writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
90
- return { injected: true, message: "Auto-heal hook injected into PreToolUse" };
74
+ const added = result.changes.added.length > 0;
75
+ const after = config.hooks.PreToolUse.length;
76
+ if (added) {
77
+ return { injected: true, message: "Auto-heal hook injected into PreToolUse" };
78
+ }
79
+ return {
80
+ injected: false,
81
+ message: `Auto-heal hook already present (${after} hook${after !== 1 ? "s" : ""} total, ordering: afd → omc → user)`,
82
+ };
91
83
  },
92
84
  configureStatusLine(cwd: string): { configured: boolean; message: string } {
93
85
  const settingsPath = join(cwd, ".claude", "settings.local.json");
@@ -121,24 +113,243 @@ export const ClaudeCodeAdapter: EcosystemAdapter = {
121
113
  writeFileSync(settingsPath, JSON.stringify(settings, null, 2), "utf-8");
122
114
  return { configured: true, message: "Status line configured with afd integration" };
123
115
  },
116
+ registerMcp(cwd: string): { registered: boolean; message: string } {
117
+ const mcpPath = join(cwd, ".mcp.json");
118
+ const serverScript = "src/daemon/server.ts";
119
+ const expectedArgs = ["run", serverScript, "--mcp"];
120
+
121
+ let config: Record<string, unknown>;
122
+ if (existsSync(mcpPath)) {
123
+ try {
124
+ config = JSON.parse(readFileSync(mcpPath, "utf-8"));
125
+ } catch {
126
+ config = {};
127
+ }
128
+ } else {
129
+ config = {};
130
+ }
131
+
132
+ const mcpServers = (config.mcpServers ?? {}) as Record<string, unknown>;
133
+ const existing = mcpServers.afd as { command?: string; args?: string[] } | undefined;
134
+
135
+ if (existing?.command === "bun" &&
136
+ JSON.stringify(existing.args) === JSON.stringify(expectedArgs)) {
137
+ return { registered: false, message: "MCP server already registered in .mcp.json" };
138
+ }
139
+
140
+ mcpServers.afd = {
141
+ command: "bun",
142
+ args: expectedArgs,
143
+ };
144
+ config.mcpServers = mcpServers;
145
+ writeFileSync(mcpPath, JSON.stringify(config, null, 2), "utf-8");
146
+ return { registered: true, message: "MCP server 'afd' registered in .mcp.json" };
147
+ },
148
+ removeHooks(cwd: string): { removed: boolean; message: string } {
149
+ const hooksPath = join(cwd, ".claude", "hooks.json");
150
+ if (!existsSync(hooksPath)) {
151
+ return { removed: false, message: "No hooks file found" };
152
+ }
153
+ try {
154
+ const config: HooksConfig = JSON.parse(readFileSync(hooksPath, "utf-8"));
155
+ const arr = config.hooks?.PreToolUse;
156
+ if (!arr) return { removed: false, message: "No PreToolUse hooks" };
157
+
158
+ // Only remove hooks that are in the canonical KNOWN_AFD_HOOKS set.
159
+ // User hooks with an `afd-` prefix (e.g., afd-read-gate) are preserved.
160
+ const before = arr.length;
161
+ config.hooks!.PreToolUse = arr.filter(
162
+ (h: HookEntry) => !KNOWN_AFD_HOOKS.has(h.id ?? "")
163
+ );
164
+ const removed = before - config.hooks!.PreToolUse.length;
165
+ if (removed === 0) return { removed: false, message: "No afd-managed hooks found" };
166
+
167
+ writeHooksFile(hooksPath, config);
168
+ return { removed: true, message: `Removed ${removed} afd-managed hook${removed !== 1 ? "s" : ""} from PreToolUse` };
169
+ } catch {
170
+ return { removed: false, message: "Failed to parse hooks file" };
171
+ }
172
+ },
173
+ unregisterMcp(cwd: string): { removed: boolean; message: string } {
174
+ const mcpPath = join(cwd, ".mcp.json");
175
+ if (!existsSync(mcpPath)) {
176
+ return { removed: false, message: "No .mcp.json found" };
177
+ }
178
+ try {
179
+ const config = JSON.parse(readFileSync(mcpPath, "utf-8"));
180
+ const servers = config.mcpServers as Record<string, unknown> | undefined;
181
+ if (!servers?.afd) return { removed: false, message: "afd not in .mcp.json" };
182
+ delete servers.afd;
183
+ if (Object.keys(servers).length === 0) delete config.mcpServers;
184
+ writeFileSync(mcpPath, JSON.stringify(config, null, 2), "utf-8");
185
+ return { removed: true, message: "MCP server 'afd' removed from .mcp.json" };
186
+ } catch {
187
+ return { removed: false, message: "Failed to parse .mcp.json" };
188
+ }
189
+ },
124
190
  };
125
191
 
126
192
  export const CursorAdapter: EcosystemAdapter = {
127
193
  name: "Cursor",
128
194
  detect(cwd: string): boolean {
129
- return existsSync(join(cwd, ".cursorrules"));
195
+ return existsSync(join(cwd, ".cursorrules")) || existsSync(join(cwd, ".cursor"));
130
196
  },
131
197
  getHarnessSchema(): HarnessSchema {
132
198
  return {
133
199
  configFiles: [".cursorrules", ".cursor/settings.json"],
134
200
  ignoreFile: ".cursorignore",
135
201
  rulesFile: ".cursorrules",
136
- hooksFile: null,
202
+ hooksFile: ".cursor/hooks.json",
203
+ };
204
+ },
205
+ injectHooks(cwd: string): { injected: boolean; message: string } {
206
+ // Cursor supports hooks via .cursor/hooks.json (same format as Claude Code)
207
+ const hooksPath = join(cwd, ".cursor", "hooks.json");
208
+ const hookCommand = resolveHookCommand();
209
+
210
+ const newHook: HookEntry = {
211
+ id: AFD_HOOK_MARKER,
212
+ matcher: "",
213
+ command: hookCommand,
214
+ };
215
+
216
+ let config: HooksConfig;
217
+ if (existsSync(hooksPath)) {
218
+ try {
219
+ config = JSON.parse(readFileSync(hooksPath, "utf-8"));
220
+ } catch {
221
+ config = { hooks: {} };
222
+ }
223
+ } else {
224
+ mkdirSync(join(cwd, ".cursor"), { recursive: true });
225
+ config = { hooks: {} };
226
+ }
227
+
228
+ if (!config.hooks || Array.isArray(config.hooks) || typeof config.hooks !== "object") {
229
+ config.hooks = {};
230
+ }
231
+ if (!config.hooks.PreToolUse) config.hooks.PreToolUse = [];
232
+
233
+ const existing = config.hooks.PreToolUse.find((h: HookEntry) => h.id === AFD_HOOK_MARKER);
234
+ if (existing) {
235
+ existing.command = hookCommand;
236
+ writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
237
+ return { injected: false, message: "Cursor: auto-heal hook already present (updated)" };
238
+ }
239
+
240
+ config.hooks.PreToolUse.push(newHook);
241
+ writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
242
+ return { injected: true, message: "Cursor: auto-heal hook injected" };
243
+ },
244
+ };
245
+
246
+ export const WindsurfAdapter: EcosystemAdapter = {
247
+ name: "Windsurf",
248
+ detect(cwd: string): boolean {
249
+ return existsSync(join(cwd, ".windsurfrules")) || existsSync(join(cwd, ".windsurf"));
250
+ },
251
+ getHarnessSchema(): HarnessSchema {
252
+ return {
253
+ configFiles: [".windsurfrules", ".windsurf/settings.json"],
254
+ ignoreFile: ".windsurfignore",
255
+ rulesFile: ".windsurfrules",
256
+ hooksFile: ".windsurf/hooks.json",
257
+ };
258
+ },
259
+ injectHooks(cwd: string): { injected: boolean; message: string } {
260
+ const hooksPath = join(cwd, ".windsurf", "hooks.json");
261
+ const hookCommand = resolveHookCommand();
262
+
263
+ const newHook: HookEntry = {
264
+ id: AFD_HOOK_MARKER,
265
+ matcher: "",
266
+ command: hookCommand,
267
+ };
268
+
269
+ let config: HooksConfig;
270
+ if (existsSync(hooksPath)) {
271
+ try {
272
+ config = JSON.parse(readFileSync(hooksPath, "utf-8"));
273
+ } catch {
274
+ config = { hooks: {} };
275
+ }
276
+ } else {
277
+ mkdirSync(join(cwd, ".windsurf"), { recursive: true });
278
+ config = { hooks: {} };
279
+ }
280
+
281
+ if (!config.hooks || Array.isArray(config.hooks) || typeof config.hooks !== "object") {
282
+ config.hooks = {};
283
+ }
284
+ if (!config.hooks.PreToolUse) config.hooks.PreToolUse = [];
285
+
286
+ const existing = config.hooks.PreToolUse.find((h: HookEntry) => h.id === AFD_HOOK_MARKER);
287
+ if (existing) {
288
+ existing.command = hookCommand;
289
+ writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
290
+ return { injected: false, message: "Windsurf: auto-heal hook already present (updated)" };
291
+ }
292
+
293
+ config.hooks.PreToolUse.push(newHook);
294
+ writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
295
+ return { injected: true, message: "Windsurf: auto-heal hook injected" };
296
+ },
297
+ };
298
+
299
+ export const CodexAdapter: EcosystemAdapter = {
300
+ name: "Codex",
301
+ detect(cwd: string): boolean {
302
+ return existsSync(join(cwd, "codex.md")) || existsSync(join(cwd, ".codex"));
303
+ },
304
+ getHarnessSchema(): HarnessSchema {
305
+ return {
306
+ configFiles: ["codex.md", ".codex/settings.json"],
307
+ ignoreFile: ".codexignore",
308
+ rulesFile: "codex.md",
309
+ hooksFile: ".codex/hooks.json",
137
310
  };
138
311
  },
312
+ injectHooks(cwd: string): { injected: boolean; message: string } {
313
+ const hooksPath = join(cwd, ".codex", "hooks.json");
314
+ const hookCommand = resolveHookCommand();
315
+
316
+ const newHook: HookEntry = {
317
+ id: AFD_HOOK_MARKER,
318
+ matcher: "",
319
+ command: hookCommand,
320
+ };
321
+
322
+ let config: HooksConfig;
323
+ if (existsSync(hooksPath)) {
324
+ try {
325
+ config = JSON.parse(readFileSync(hooksPath, "utf-8"));
326
+ } catch {
327
+ config = { hooks: {} };
328
+ }
329
+ } else {
330
+ mkdirSync(join(cwd, ".codex"), { recursive: true });
331
+ config = { hooks: {} };
332
+ }
333
+
334
+ if (!config.hooks || Array.isArray(config.hooks) || typeof config.hooks !== "object") {
335
+ config.hooks = {};
336
+ }
337
+ if (!config.hooks.PreToolUse) config.hooks.PreToolUse = [];
338
+
339
+ const existing = config.hooks.PreToolUse.find((h: HookEntry) => h.id === AFD_HOOK_MARKER);
340
+ if (existing) {
341
+ existing.command = hookCommand;
342
+ writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
343
+ return { injected: false, message: "Codex: auto-heal hook already present (updated)" };
344
+ }
345
+
346
+ config.hooks.PreToolUse.push(newHook);
347
+ writeFileSync(hooksPath, JSON.stringify(config, null, 2), "utf-8");
348
+ return { injected: true, message: "Codex: auto-heal hook injected" };
349
+ },
139
350
  };
140
351
 
141
- const adapters: EcosystemAdapter[] = [ClaudeCodeAdapter, CursorAdapter];
352
+ const adapters: EcosystemAdapter[] = [ClaudeCodeAdapter, CursorAdapter, WindsurfAdapter, CodexAdapter];
142
353
 
143
354
  export interface DetectionResult {
144
355
  adapter: EcosystemAdapter;
package/src/cli.ts CHANGED
@@ -2,18 +2,34 @@
2
2
  import { Command } from "commander";
3
3
  import { startCommand } from "./commands/start";
4
4
  import { stopCommand } from "./commands/stop";
5
+ import { restartCommand } from "./commands/restart";
6
+ import { statusCommand } from "./commands/status";
5
7
  import { scoreCommand } from "./commands/score";
6
8
  import { fixCommand } from "./commands/fix";
7
9
  import { syncCommand } from "./commands/sync";
8
10
  import { diagnoseCommand } from "./commands/diagnose";
11
+ import { doctorCommand } from "./commands/doctor";
12
+
13
+ import { vaccineCommand } from "./commands/vaccine";
9
14
  import { langCommand } from "./commands/lang";
15
+ import { evolutionCommand } from "./commands/evolution";
16
+ import { mcpCommand } from "./commands/mcp";
17
+ import { statsCommand } from "./commands/stats";
18
+ import { hooksCommand } from "./commands/hooks";
19
+ import { benchmarkCommand } from "./commands/benchmark";
20
+ import { APP_VERSION } from "./version";
21
+ import { trackCliCommand } from "./core/telemetry";
10
22
 
11
23
  const program = new Command();
12
24
 
13
25
  program
14
26
  .name("afd")
15
27
  .description("Autonomous Flow Daemon - The Immune System for AI Workflows")
16
- .version("1.1.0");
28
+ .version(APP_VERSION);
29
+
30
+ program.hook("preAction", (thisCommand) => {
31
+ trackCliCommand(thisCommand.name());
32
+ });
17
33
 
18
34
  program
19
35
  .command("start")
@@ -24,8 +40,19 @@ program
24
40
  program
25
41
  .command("stop")
26
42
  .description("Stop the afd daemon")
43
+ .option("--clean", "Remove all injected hooks and MCP registrations")
27
44
  .action(stopCommand);
28
45
 
46
+ program
47
+ .command("restart")
48
+ .description("Restart the afd daemon (stop + start)")
49
+ .action(restartCommand);
50
+
51
+ program
52
+ .command("status")
53
+ .description("Quick health check — daemon, hooks, defenses, quarantine")
54
+ .action(statusCommand);
55
+
29
56
  program
30
57
  .command("score")
31
58
  .description("Show current diagnostic stats from the daemon")
@@ -39,8 +66,17 @@ program
39
66
  program
40
67
  .command("sync")
41
68
  .description("Synchronize AI agent configs across team")
69
+ .option("--push", "Push local antibodies to team vaccine store")
70
+ .option("--pull", "Pull antibodies from team vaccine store")
71
+ .option("--remote <url>", "Remote vaccine store URL (future)")
42
72
  .action(syncCommand);
43
73
 
74
+ program
75
+ .command("doctor")
76
+ .description("Deep health analysis with recommendations and auto-fix")
77
+ .option("--fix", "Auto-fix detected issues")
78
+ .action(doctorCommand);
79
+
44
80
  program
45
81
  .command("diagnose")
46
82
  .description("Run headless diagnosis (used by auto-heal hooks)")
@@ -48,10 +84,44 @@ program
48
84
  .option("--auto-heal", "Auto-apply patches for known antibodies")
49
85
  .action(diagnoseCommand);
50
86
 
87
+ program
88
+ .command("vaccine [subcommand] [arg]")
89
+ .description("Vaccine registry: list, search, install, publish")
90
+ .action(vaccineCommand);
91
+
92
+ program
93
+ .command("evolution")
94
+ .description("Self-Evolution: analyze quarantined failures and generate lessons for AI agents")
95
+ .action(evolutionCommand);
96
+
97
+ program
98
+ .command("mcp [subcommand]")
99
+ .description("MCP server management (install)")
100
+ .action(mcpCommand);
101
+
51
102
  program
52
103
  .command("lang [language]")
53
104
  .description("Show or change display language (en, ko)")
54
105
  .option("--list", "Show all supported languages")
55
106
  .action(langCommand);
56
107
 
108
+ program
109
+ .command("stats")
110
+ .description("Feature usage telemetry dashboard (developer-only)")
111
+ .option("--days <n>", "Number of days to aggregate", "7")
112
+ .action(statsCommand);
113
+
114
+ program
115
+ .command("hooks [subcommand]")
116
+ .description("Hook Manager: inspect and sync hook ordering (afd → omc → user)")
117
+ .action(hooksCommand);
118
+
119
+ program
120
+ .command("benchmark")
121
+ .description("Hologram AST compression benchmark across all source files")
122
+ .option("--sort <key>", "Sort by: savings (default), size, name")
123
+ .option("--top <n>", "Show only top N files")
124
+ .option("--json", "Output raw JSON for programmatic use")
125
+ .action(benchmarkCommand);
126
+
57
127
  program.parse();
@@ -0,0 +1,187 @@
1
+ import { readFileSync, readdirSync, statSync } from "node:fs";
2
+ import { join, relative } from "node:path";
3
+ import { generateHologram } from "../core/hologram";
4
+ import { getSystemLanguage } from "../core/locale";
5
+
6
+ // ── ANSI helpers ──
7
+ const C = {
8
+ reset: "\x1b[0m",
9
+ bold: "\x1b[1m",
10
+ dim: "\x1b[2m",
11
+ red: "\x1b[31m",
12
+ green: "\x1b[32m",
13
+ yellow: "\x1b[33m",
14
+ cyan: "\x1b[36m",
15
+ white: "\x1b[37m",
16
+ };
17
+
18
+ interface FileResult {
19
+ path: string;
20
+ originalLines: number;
21
+ hologramLines: number;
22
+ originalChars: number;
23
+ hologramChars: number;
24
+ savings: number;
25
+ }
26
+
27
+ function collectTsFiles(dir: string, base: string): string[] {
28
+ const results: string[] = [];
29
+ const skipDirs = new Set(["node_modules", ".git", ".afd", "dist", "coverage", ".omc"]);
30
+
31
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
32
+ if (entry.isDirectory()) {
33
+ if (!skipDirs.has(entry.name)) {
34
+ results.push(...collectTsFiles(join(dir, entry.name), base));
35
+ }
36
+ } else if (/\.(ts|tsx|js|jsx)$/.test(entry.name) && !entry.name.endsWith(".d.ts")) {
37
+ results.push(join(dir, entry.name));
38
+ }
39
+ }
40
+ return results;
41
+ }
42
+
43
+ function formatChars(n: number): string {
44
+ if (n < 1000) return `${n}`;
45
+ if (n < 1_000_000) return `${(n / 1000).toFixed(1)}K`;
46
+ return `${(n / 1_000_000).toFixed(1)}M`;
47
+ }
48
+
49
+ function savingsColor(pct: number): string {
50
+ if (pct >= 80) return C.green;
51
+ if (pct >= 50) return C.yellow;
52
+ return C.red;
53
+ }
54
+
55
+ function padRight(s: string, w: number): string {
56
+ const stripped = s.replace(/\x1b\[[0-9;]*m/g, "");
57
+ return s + " ".repeat(Math.max(0, w - stripped.length));
58
+ }
59
+
60
+ function padLeft(s: string, w: number): string {
61
+ const stripped = s.replace(/\x1b\[[0-9;]*m/g, "");
62
+ return " ".repeat(Math.max(0, w - stripped.length)) + s;
63
+ }
64
+
65
+ export async function benchmarkCommand(options: { sort?: string; top?: string; json?: boolean }) {
66
+ const lang = getSystemLanguage();
67
+ const ko = lang === "ko";
68
+ const cwd = process.cwd();
69
+ const files = collectTsFiles(join(cwd, "src"), cwd);
70
+
71
+ if (files.length === 0) {
72
+ console.error(`${C.red}[afd] ${ko ? "src/ 디렉토리에 TS/JS 파일이 없습니다." : "No TS/JS files found in src/."}${C.reset}`);
73
+ process.exit(1);
74
+ }
75
+
76
+ const results: FileResult[] = [];
77
+ const startTime = performance.now();
78
+
79
+ for (const filePath of files) {
80
+ try {
81
+ const source = readFileSync(filePath, "utf-8");
82
+ const { hologram, originalLength, hologramLength, savings } = await generateHologram(filePath, source);
83
+ results.push({
84
+ path: relative(cwd, filePath),
85
+ originalLines: source.split("\n").length,
86
+ hologramLines: hologram.split("\n").length,
87
+ originalChars: originalLength,
88
+ hologramChars: hologramLength,
89
+ savings: Math.round(savings * 10) / 10,
90
+ });
91
+ } catch {
92
+ // Skip files that fail to parse
93
+ }
94
+ }
95
+
96
+ const elapsed = Math.round(performance.now() - startTime);
97
+
98
+ // Sort
99
+ const sortKey = options.sort ?? "savings";
100
+ results.sort((a, b) => {
101
+ if (sortKey === "size") return b.originalChars - a.originalChars;
102
+ if (sortKey === "name") return a.path.localeCompare(b.path);
103
+ return b.savings - a.savings; // default: savings desc
104
+ });
105
+
106
+ const limit = options.top ? parseInt(options.top, 10) : results.length;
107
+ const display = results.slice(0, limit);
108
+
109
+ // JSON output
110
+ if (options.json) {
111
+ const totalOriginal = results.reduce((s, r) => s + r.originalChars, 0);
112
+ const totalHologram = results.reduce((s, r) => s + r.hologramChars, 0);
113
+ console.log(JSON.stringify({
114
+ files: results.length,
115
+ totalOriginalChars: totalOriginal,
116
+ totalHologramChars: totalHologram,
117
+ totalSavedChars: totalOriginal - totalHologram,
118
+ overallCompression: totalOriginal > 0 ? Math.round((1 - totalHologram / totalOriginal) * 1000) / 10 : 0,
119
+ estimatedTokensSaved: Math.round((totalOriginal - totalHologram) / 4),
120
+ elapsedMs: elapsed,
121
+ results: results.map(r => ({
122
+ path: r.path,
123
+ originalLines: r.originalLines,
124
+ hologramLines: r.hologramLines,
125
+ originalChars: r.originalChars,
126
+ hologramChars: r.hologramChars,
127
+ savings: r.savings,
128
+ })),
129
+ }, null, 2));
130
+ return;
131
+ }
132
+
133
+ // ── Header ──
134
+ console.log();
135
+ console.log(`${C.bold}${C.cyan} ╔══════════════════════════════════════════════════════════════╗${C.reset}`);
136
+ console.log(`${C.bold}${C.cyan} ║ ${ko ? "홀로그램 AST 압축 벤치마크" : "Hologram AST Compression Benchmark"} ║${C.reset}`);
137
+ console.log(`${C.bold}${C.cyan} ╚══════════════════════════════════════════════════════════════╝${C.reset}`);
138
+ console.log();
139
+
140
+ // ── Table Header ──
141
+ const colFile = ko ? "파일" : "File";
142
+ const colLines = ko ? "원본줄" : "Lines";
143
+ const colHolo = ko ? "홀로줄" : "Holo";
144
+ const colOrig = ko ? "원본" : "Original";
145
+ const colComp = ko ? "압축" : "Compressed";
146
+ const colSave = ko ? "절감률" : "Savings";
147
+
148
+ console.log(
149
+ ` ${C.dim}${padRight(colFile, 40)} ${padLeft(colLines, 6)} ${padLeft(colHolo, 6)} ${padLeft(colOrig, 8)} ${padLeft(colComp, 8)} ${padLeft(colSave, 8)}${C.reset}`
150
+ );
151
+ console.log(` ${C.dim}${"─".repeat(80)}${C.reset}`);
152
+
153
+ // ── Rows ──
154
+ for (const r of display) {
155
+ const sc = savingsColor(r.savings);
156
+ const savingsStr = `${sc}${r.savings.toFixed(1)}%${C.reset}`;
157
+ console.log(
158
+ ` ${padRight(r.path, 40)} ${padLeft(String(r.originalLines), 6)} ${padLeft(String(r.hologramLines), 6)} ${padLeft(formatChars(r.originalChars), 8)} ${padLeft(formatChars(r.hologramChars), 8)} ${padLeft(savingsStr, 8)}`
159
+ );
160
+ }
161
+
162
+ if (limit < results.length) {
163
+ console.log(` ${C.dim}... ${ko ? `외 ${results.length - limit}개 파일` : `and ${results.length - limit} more files`}${C.reset}`);
164
+ }
165
+
166
+ // ── Summary ──
167
+ const totalOriginal = results.reduce((s, r) => s + r.originalChars, 0);
168
+ const totalHologram = results.reduce((s, r) => s + r.hologramChars, 0);
169
+ const totalSaved = totalOriginal - totalHologram;
170
+ const overallPct = totalOriginal > 0 ? Math.round((1 - totalHologram / totalOriginal) * 1000) / 10 : 0;
171
+ const estimatedTokens = Math.round(totalSaved / 4);
172
+ const high = results.filter(r => r.savings >= 70).length;
173
+
174
+ console.log();
175
+ console.log(` ${C.dim}${"─".repeat(80)}${C.reset}`);
176
+ console.log(` ${C.bold}${ko ? "요약" : "Summary"}${C.reset}`);
177
+ console.log();
178
+ console.log(` ${ko ? "분석 파일" : "Files analyzed"} ${C.bold}${results.length}${C.reset}`);
179
+ console.log(` ${ko ? "전체 압축률" : "Overall compression"} ${C.bold}${savingsColor(overallPct)}${overallPct}%${C.reset}`);
180
+ console.log(` ${ko ? "원본 크기" : "Original size"} ${C.bold}${formatChars(totalOriginal)}${C.reset} ${C.dim}(${(totalOriginal / 1024).toFixed(0)} KB)${C.reset}`);
181
+ console.log(` ${ko ? "압축 크기" : "Compressed size"} ${C.bold}${formatChars(totalHologram)}${C.reset} ${C.dim}(${(totalHologram / 1024).toFixed(0)} KB)${C.reset}`);
182
+ console.log(` ${ko ? "절약 크기" : "Saved"} ${C.bold}${C.green}${formatChars(totalSaved)}${C.reset} ${C.dim}(${(totalSaved / 1024).toFixed(0)} KB)${C.reset}`);
183
+ console.log(` ${ko ? "추정 토큰 절약" : "Est. tokens saved"} ${C.bold}${C.green}~${estimatedTokens.toLocaleString()}${C.reset}`);
184
+ console.log(` ${ko ? "70%+ 압축 파일" : "70%+ compression"} ${C.bold}${high}${C.reset}/${results.length} ${C.dim}(${Math.round(high / results.length * 100)}%)${C.reset}`);
185
+ console.log(` ${ko ? "처리 시간" : "Elapsed"} ${C.dim}${elapsed}ms${C.reset}`);
186
+ console.log();
187
+ }