@aiready/pattern-detect 0.11.20 β†’ 0.11.24

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.
package/README.md CHANGED
@@ -7,21 +7,36 @@ Finds semantically similar but syntactically different code patterns that waste
7
7
  ## πŸ›οΈ Architecture
8
8
 
9
9
  ```
10
- 🎯 USER
11
- β”‚
12
- β–Ό
13
- πŸŽ›οΈ CLI (orchestrator)
14
- β”‚
15
- β–Ό
16
- 🏒 HUB (core)
17
- β”‚
18
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
19
- β–Ό β–Ό β–Ό β–Ό
20
- β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” πŸ“¦ CONTEXT πŸ”§ CONSIST πŸ“š DOC
21
- β”‚ πŸ“Š PATTERN β”‚ β¬… YOU ARE HERE ENCY DRIFT
22
- β”‚ DETECT β”‚ ANALYZER
23
- β”‚ βœ… Ready β”‚ βœ… Ready βœ… Ready πŸ”œ Soon
24
- β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
10
+ 🎯 USER
11
+ β”‚
12
+ β–Ό
13
+ πŸŽ›οΈ CLI (orchestrator)
14
+ β”‚
15
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
16
+ β”‚ β”‚
17
+ β–Ό β–Ό
18
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
19
+ β”‚πŸŽ¨ VIS- β”‚ β”‚ ANALY- β”‚
20
+ β”‚UALIZER β”‚ β”‚ SIS β”‚
21
+ β”‚βœ… Readyβ”‚ β”‚ SPOKES β”‚
22
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
23
+ β”‚ β”‚
24
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
25
+ β”‚ β–Ό β–Ό β–Ό
26
+ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
27
+ β”‚ β”‚πŸ“Š PAT- β”‚ β”‚πŸ“¦ CON- β”‚ β”‚πŸ”§ CON- β”‚
28
+ β”‚ β”‚TERN β”‚ β”‚TEXT β”‚ β”‚SISTENCYβ”‚
29
+ β”‚ β”‚DETECT β”‚ β”‚ANALYZERβ”‚ β”‚ β”‚
30
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
31
+ β”‚ β”‚βœ… Readyβ”‚ β”‚βœ… Readyβ”‚ β”‚βœ… Readyβ”‚
32
+ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜
33
+ β”‚ β”‚ β”‚ β”‚
34
+ β”‚ β”‚ ← YOU ARE HERE β”€β”€β”˜ β”‚
35
+ β”‚ β”‚ β”‚
36
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
37
+ β”‚
38
+ β–Ό
39
+ 🏒 HUB (@aiready/core)
25
40
  ```
26
41
 
27
42
  ## 🌍 Language Support
@@ -0,0 +1,1072 @@
1
+ // src/index.ts
2
+ import { readFileContent } from "@aiready/core";
3
+
4
+ // src/detector.ts
5
+ import { estimateTokens } from "@aiready/core";
6
+
7
+ // src/context-rules.ts
8
+ var CONTEXT_RULES = [
9
+ // Test Fixtures - Intentional duplication for test isolation
10
+ {
11
+ name: "test-fixtures",
12
+ detect: (file, code) => {
13
+ const isTestFile = file.includes(".test.") || file.includes(".spec.") || file.includes("__tests__") || file.includes("/test/") || file.includes("/tests/");
14
+ const hasTestFixtures = code.includes("beforeAll") || code.includes("afterAll") || code.includes("beforeEach") || code.includes("afterEach") || code.includes("setUp") || code.includes("tearDown");
15
+ return isTestFile && hasTestFixtures;
16
+ },
17
+ severity: "info",
18
+ reason: "Test fixture duplication is intentional for test isolation",
19
+ suggestion: "Consider if shared test setup would improve maintainability without coupling tests"
20
+ },
21
+ // Email/Document Templates - Often intentionally similar for consistency
22
+ {
23
+ name: "templates",
24
+ detect: (file, code) => {
25
+ const isTemplate = file.includes("/templates/") || file.includes("-template") || file.includes("/email-templates/") || file.includes("/emails/");
26
+ const hasTemplateContent = (code.includes("return") || code.includes("export")) && (code.includes("html") || code.includes("subject") || code.includes("body"));
27
+ return isTemplate && hasTemplateContent;
28
+ },
29
+ severity: "minor",
30
+ reason: "Template duplication may be intentional for maintainability and branding consistency",
31
+ suggestion: "Extract shared structure only if templates become hard to maintain"
32
+ },
33
+ // E2E/Integration Test Page Objects - Test independence
34
+ {
35
+ name: "e2e-page-objects",
36
+ detect: (file, code) => {
37
+ const isE2ETest = file.includes("e2e/") || file.includes("/e2e/") || file.includes(".e2e.") || file.includes("/playwright/") || file.includes("playwright/") || file.includes("/cypress/") || file.includes("cypress/") || file.includes("/integration/") || file.includes("integration/");
38
+ const hasPageObjectPatterns = code.includes("page.") || code.includes("await page") || code.includes("locator") || code.includes("getBy") || code.includes("selector") || code.includes("click(") || code.includes("fill(");
39
+ return isE2ETest && hasPageObjectPatterns;
40
+ },
41
+ severity: "minor",
42
+ reason: "E2E test duplication ensures test independence and reduces coupling",
43
+ suggestion: "Consider page object pattern only if duplication causes maintenance issues"
44
+ },
45
+ // Configuration Files - Often necessarily similar by design
46
+ {
47
+ name: "config-files",
48
+ detect: (file) => {
49
+ return file.endsWith(".config.ts") || file.endsWith(".config.js") || file.includes("jest.config") || file.includes("vite.config") || file.includes("webpack.config") || file.includes("rollup.config") || file.includes("tsconfig");
50
+ },
51
+ severity: "minor",
52
+ reason: "Configuration files often have similar structure by design",
53
+ suggestion: "Consider shared config base only if configurations become hard to maintain"
54
+ },
55
+ // Type Definitions - Duplication for type safety and module independence
56
+ {
57
+ name: "type-definitions",
58
+ detect: (file, code) => {
59
+ const isTypeFile = file.endsWith(".d.ts") || file.includes("/types/");
60
+ const hasTypeDefinitions = code.includes("interface ") || code.includes("type ") || code.includes("enum ");
61
+ return isTypeFile && hasTypeDefinitions;
62
+ },
63
+ severity: "info",
64
+ reason: "Type duplication may be intentional for module independence and type safety",
65
+ suggestion: "Extract to shared types package only if causing maintenance burden"
66
+ },
67
+ // Migration Scripts - One-off scripts that are similar by nature
68
+ {
69
+ name: "migration-scripts",
70
+ detect: (file) => {
71
+ return file.includes("/migrations/") || file.includes("/migrate/") || file.includes(".migration.");
72
+ },
73
+ severity: "info",
74
+ reason: "Migration scripts are typically one-off and intentionally similar",
75
+ suggestion: "Duplication is acceptable for migration scripts"
76
+ },
77
+ // Mock Data - Test data intentionally duplicated
78
+ {
79
+ name: "mock-data",
80
+ detect: (file, code) => {
81
+ const isMockFile = file.includes("/mocks/") || file.includes("/__mocks__/") || file.includes("/fixtures/") || file.includes(".mock.") || file.includes(".fixture.");
82
+ const hasMockData = code.includes("mock") || code.includes("Mock") || code.includes("fixture") || code.includes("stub") || code.includes("export const");
83
+ return isMockFile && hasMockData;
84
+ },
85
+ severity: "info",
86
+ reason: "Mock data duplication is expected for comprehensive test coverage",
87
+ suggestion: "Consider shared factories only for complex mock generation"
88
+ }
89
+ ];
90
+ function calculateSeverity(file1, file2, code, similarity, linesOfCode) {
91
+ for (const rule of CONTEXT_RULES) {
92
+ if (rule.detect(file1, code) || rule.detect(file2, code)) {
93
+ return {
94
+ severity: rule.severity,
95
+ reason: rule.reason,
96
+ suggestion: rule.suggestion,
97
+ matchedRule: rule.name
98
+ };
99
+ }
100
+ }
101
+ if (similarity >= 0.95 && linesOfCode >= 30) {
102
+ return {
103
+ severity: "critical",
104
+ reason: "Large nearly-identical code blocks waste tokens and create maintenance burden",
105
+ suggestion: "Extract to shared utility module immediately"
106
+ };
107
+ } else if (similarity >= 0.95 && linesOfCode >= 15) {
108
+ return {
109
+ severity: "major",
110
+ reason: "Nearly identical code should be consolidated",
111
+ suggestion: "Move to shared utility file"
112
+ };
113
+ } else if (similarity >= 0.85) {
114
+ return {
115
+ severity: "major",
116
+ reason: "High similarity indicates significant duplication",
117
+ suggestion: "Extract common logic to shared function"
118
+ };
119
+ } else if (similarity >= 0.7) {
120
+ return {
121
+ severity: "minor",
122
+ reason: "Moderate similarity detected",
123
+ suggestion: "Consider extracting shared patterns if code evolves together"
124
+ };
125
+ } else {
126
+ return {
127
+ severity: "minor",
128
+ reason: "Minor similarity detected",
129
+ suggestion: "Monitor but refactoring may not be worthwhile"
130
+ };
131
+ }
132
+ }
133
+ function getSeverityLabel(severity) {
134
+ const labels = {
135
+ critical: "\u{1F534} CRITICAL",
136
+ major: "\u{1F7E1} MAJOR",
137
+ minor: "\u{1F535} MINOR",
138
+ info: "\u2139\uFE0F INFO"
139
+ };
140
+ return labels[severity];
141
+ }
142
+ function filterBySeverity(duplicates, minSeverity) {
143
+ const severityOrder = ["info", "minor", "major", "critical"];
144
+ const minIndex = severityOrder.indexOf(minSeverity);
145
+ if (minIndex === -1) return duplicates;
146
+ return duplicates.filter((dup) => {
147
+ const dupIndex = severityOrder.indexOf(dup.severity);
148
+ return dupIndex >= minIndex;
149
+ });
150
+ }
151
+
152
+ // src/detector.ts
153
+ function categorizePattern(code) {
154
+ const lower = code.toLowerCase();
155
+ if (lower.includes("request") && lower.includes("response") || lower.includes("router.") || lower.includes("app.get") || lower.includes("app.post") || lower.includes("express") || lower.includes("ctx.body")) {
156
+ return "api-handler";
157
+ }
158
+ if (lower.includes("validate") || lower.includes("schema") || lower.includes("zod") || lower.includes("yup") || lower.includes("if") && lower.includes("throw")) {
159
+ return "validator";
160
+ }
161
+ if (lower.includes("return (") || lower.includes("jsx") || lower.includes("component") || lower.includes("props")) {
162
+ return "component";
163
+ }
164
+ if (lower.includes("class ") || lower.includes("this.")) {
165
+ return "class-method";
166
+ }
167
+ if (lower.includes("return ") && !lower.includes("this") && !lower.includes("new ")) {
168
+ return "utility";
169
+ }
170
+ if (lower.includes("function") || lower.includes("=>")) {
171
+ return "function";
172
+ }
173
+ return "unknown";
174
+ }
175
+ function extractCodeBlocks(content, minLines) {
176
+ const lines = content.split("\n");
177
+ const blocks = [];
178
+ let currentBlock = [];
179
+ let blockStart = 0;
180
+ let braceDepth = 0;
181
+ let inFunction = false;
182
+ for (let i = 0; i < lines.length; i++) {
183
+ const line = lines[i];
184
+ const trimmed = line.trim();
185
+ if (!inFunction && (trimmed.includes("function ") || trimmed.includes("=>") || trimmed.includes("async ") || /^(export\s+)?(async\s+)?function\s+/.test(trimmed) || /^(export\s+)?const\s+\w+\s*=\s*(async\s*)?\(/.test(trimmed))) {
186
+ inFunction = true;
187
+ blockStart = i;
188
+ }
189
+ for (const char of line) {
190
+ if (char === "{") braceDepth++;
191
+ if (char === "}") braceDepth--;
192
+ }
193
+ if (inFunction) {
194
+ currentBlock.push(line);
195
+ }
196
+ if (inFunction && braceDepth === 0 && currentBlock.length >= minLines) {
197
+ const blockContent = currentBlock.join("\n");
198
+ const linesOfCode = currentBlock.filter(
199
+ (l) => l.trim() && !l.trim().startsWith("//")
200
+ ).length;
201
+ blocks.push({
202
+ content: blockContent,
203
+ startLine: blockStart + 1,
204
+ endLine: i + 1,
205
+ patternType: categorizePattern(blockContent),
206
+ linesOfCode
207
+ });
208
+ currentBlock = [];
209
+ inFunction = false;
210
+ } else if (inFunction && braceDepth === 0) {
211
+ currentBlock = [];
212
+ inFunction = false;
213
+ }
214
+ }
215
+ return blocks;
216
+ }
217
+ function normalizeCode(code) {
218
+ if (!code) {
219
+ return "";
220
+ }
221
+ return code.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "").replace(/"[^"]*"/g, '"STR"').replace(/'[^']*'/g, "'STR'").replace(/`[^`]*`/g, "`STR`").replace(/\b\d+\b/g, "NUM").replace(/\s+/g, " ").trim();
222
+ }
223
+ function jaccardSimilarity(tokens1, tokens2) {
224
+ const set1 = new Set(tokens1);
225
+ const set2 = new Set(tokens2);
226
+ let intersection = 0;
227
+ for (const token of set1) {
228
+ if (set2.has(token)) intersection++;
229
+ }
230
+ const union = set1.size + set2.size - intersection;
231
+ return union === 0 ? 0 : intersection / union;
232
+ }
233
+ async function detectDuplicatePatterns(files, options) {
234
+ const {
235
+ minSimilarity,
236
+ minLines,
237
+ batchSize = 100,
238
+ approx = true,
239
+ minSharedTokens = 8,
240
+ maxCandidatesPerBlock = 100,
241
+ streamResults = false
242
+ } = options;
243
+ const duplicates = [];
244
+ const maxComparisons = approx ? Infinity : 5e5;
245
+ const allBlocks = files.flatMap(
246
+ (file) => extractCodeBlocks(file.content, minLines).filter((block) => block.content && block.content.trim().length > 0).map((block) => ({
247
+ content: block.content,
248
+ startLine: block.startLine,
249
+ endLine: block.endLine,
250
+ file: file.file,
251
+ normalized: normalizeCode(block.content),
252
+ patternType: block.patternType,
253
+ tokenCost: estimateTokens(block.content),
254
+ linesOfCode: block.linesOfCode
255
+ }))
256
+ );
257
+ console.log(`Extracted ${allBlocks.length} code blocks for analysis`);
258
+ const pythonFiles = files.filter((f) => f.file.toLowerCase().endsWith(".py"));
259
+ if (pythonFiles.length > 0) {
260
+ const { extractPythonPatterns } = await import("./python-extractor-BGKGX6BK.mjs");
261
+ const patterns = await extractPythonPatterns(pythonFiles.map((f) => f.file));
262
+ const pythonBlocks = patterns.filter((p) => p.code && p.code.trim().length > 0).map((p) => ({
263
+ content: p.code,
264
+ startLine: p.startLine,
265
+ endLine: p.endLine,
266
+ file: p.file,
267
+ normalized: normalizeCode(p.code),
268
+ patternType: p.type,
269
+ tokenCost: estimateTokens(p.code),
270
+ linesOfCode: p.endLine - p.startLine + 1
271
+ }));
272
+ allBlocks.push(...pythonBlocks);
273
+ console.log(`Added ${pythonBlocks.length} Python patterns`);
274
+ }
275
+ if (!approx && allBlocks.length > 500) {
276
+ console.log(`\u26A0\uFE0F Using --no-approx mode with ${allBlocks.length} blocks may be slow (O(B\xB2) complexity).`);
277
+ console.log(` Consider using approximate mode (default) for better performance.`);
278
+ }
279
+ const stopwords = /* @__PURE__ */ new Set([
280
+ "return",
281
+ "const",
282
+ "let",
283
+ "var",
284
+ "function",
285
+ "class",
286
+ "new",
287
+ "if",
288
+ "else",
289
+ "for",
290
+ "while",
291
+ "async",
292
+ "await",
293
+ "try",
294
+ "catch",
295
+ "switch",
296
+ "case",
297
+ "default",
298
+ "import",
299
+ "export",
300
+ "from",
301
+ "true",
302
+ "false",
303
+ "null",
304
+ "undefined",
305
+ "this"
306
+ ]);
307
+ const tokenize = (norm) => norm.split(/[\s(){}\[\];,\.]+/).filter((t) => t && t.length >= 3 && !stopwords.has(t.toLowerCase()));
308
+ const blockTokens = allBlocks.map((b) => tokenize(b.normalized));
309
+ const invertedIndex = /* @__PURE__ */ new Map();
310
+ if (approx) {
311
+ for (let i = 0; i < blockTokens.length; i++) {
312
+ for (const tok of blockTokens[i]) {
313
+ let arr = invertedIndex.get(tok);
314
+ if (!arr) {
315
+ arr = [];
316
+ invertedIndex.set(tok, arr);
317
+ }
318
+ arr.push(i);
319
+ }
320
+ }
321
+ }
322
+ const totalComparisons = approx ? void 0 : allBlocks.length * (allBlocks.length - 1) / 2;
323
+ if (totalComparisons !== void 0) {
324
+ console.log(`Processing ${totalComparisons.toLocaleString()} comparisons in batches...`);
325
+ } else {
326
+ console.log(`Using approximate candidate selection to reduce comparisons...`);
327
+ }
328
+ let comparisonsProcessed = 0;
329
+ let comparisonsBudgetExhausted = false;
330
+ const startTime = Date.now();
331
+ for (let i = 0; i < allBlocks.length; i++) {
332
+ if (maxComparisons && comparisonsProcessed >= maxComparisons) {
333
+ comparisonsBudgetExhausted = true;
334
+ break;
335
+ }
336
+ if (i % batchSize === 0 && i > 0) {
337
+ const elapsed = ((Date.now() - startTime) / 1e3).toFixed(1);
338
+ const duplicatesFound = duplicates.length;
339
+ if (totalComparisons !== void 0) {
340
+ const progress = (comparisonsProcessed / totalComparisons * 100).toFixed(1);
341
+ const remaining = totalComparisons - comparisonsProcessed;
342
+ const rate = comparisonsProcessed / parseFloat(elapsed);
343
+ const eta = remaining > 0 ? (remaining / rate).toFixed(0) : 0;
344
+ console.log(` ${progress}% (${comparisonsProcessed.toLocaleString()}/${totalComparisons.toLocaleString()} comparisons, ${elapsed}s elapsed, ~${eta}s remaining, ${duplicatesFound} duplicates)`);
345
+ } else {
346
+ console.log(` Processed ${i.toLocaleString()}/${allBlocks.length} blocks (${elapsed}s elapsed, ${duplicatesFound} duplicates)`);
347
+ }
348
+ await new Promise((resolve) => setImmediate(resolve));
349
+ }
350
+ const block1 = allBlocks[i];
351
+ let candidates = null;
352
+ if (approx) {
353
+ const counts = /* @__PURE__ */ new Map();
354
+ const block1Tokens = new Set(blockTokens[i]);
355
+ const block1Size = block1Tokens.size;
356
+ const rareTokens = blockTokens[i].filter((tok) => {
357
+ const blocksWithToken = invertedIndex.get(tok)?.length || 0;
358
+ return blocksWithToken < allBlocks.length * 0.1;
359
+ });
360
+ for (const tok of rareTokens) {
361
+ const ids = invertedIndex.get(tok);
362
+ if (!ids) continue;
363
+ for (const j of ids) {
364
+ if (j <= i) continue;
365
+ if (allBlocks[j].file === block1.file) continue;
366
+ counts.set(j, (counts.get(j) || 0) + 1);
367
+ }
368
+ }
369
+ candidates = Array.from(counts.entries()).filter(([j, shared]) => {
370
+ const block2Tokens = blockTokens[j];
371
+ const block2Size = block2Tokens.length;
372
+ const minSize = Math.min(block1Size, block2Size);
373
+ const sharedPercentage = shared / minSize;
374
+ return shared >= minSharedTokens && sharedPercentage >= 0.3;
375
+ }).sort((a, b) => b[1] - a[1]).slice(0, Math.min(maxCandidatesPerBlock, 5)).map(([j, shared]) => ({ j, shared }));
376
+ }
377
+ if (approx && candidates) {
378
+ for (const { j } of candidates) {
379
+ if (!approx && maxComparisons !== Infinity && comparisonsProcessed >= maxComparisons) {
380
+ console.log(`\u26A0\uFE0F Comparison safety limit reached (${maxComparisons.toLocaleString()} comparisons in --no-approx mode).`);
381
+ console.log(` This prevents excessive runtime on large repos. Consider using approximate mode (default) or --min-lines to reduce blocks.`);
382
+ break;
383
+ }
384
+ comparisonsProcessed++;
385
+ const block2 = allBlocks[j];
386
+ const similarity = jaccardSimilarity(blockTokens[i], blockTokens[j]);
387
+ if (similarity >= minSimilarity) {
388
+ const { severity, reason, suggestion, matchedRule } = calculateSeverity(
389
+ block1.file,
390
+ block2.file,
391
+ block1.content,
392
+ similarity,
393
+ block1.linesOfCode
394
+ );
395
+ const duplicate = {
396
+ file1: block1.file,
397
+ file2: block2.file,
398
+ line1: block1.startLine,
399
+ line2: block2.startLine,
400
+ endLine1: block1.endLine,
401
+ endLine2: block2.endLine,
402
+ similarity,
403
+ snippet: block1.content.split("\n").slice(0, 5).join("\n") + "\n...",
404
+ patternType: block1.patternType,
405
+ tokenCost: block1.tokenCost + block2.tokenCost,
406
+ linesOfCode: block1.linesOfCode,
407
+ severity,
408
+ reason,
409
+ suggestion,
410
+ matchedRule
411
+ };
412
+ duplicates.push(duplicate);
413
+ if (streamResults) {
414
+ console.log(`
415
+ \u2705 Found: ${duplicate.patternType} ${Math.round(similarity * 100)}% similar`);
416
+ console.log(` ${duplicate.file1}:${duplicate.line1}-${duplicate.endLine1} \u21D4 ${duplicate.file2}:${duplicate.line2}-${duplicate.endLine2}`);
417
+ console.log(` Token cost: ${duplicate.tokenCost.toLocaleString()}`);
418
+ }
419
+ }
420
+ }
421
+ } else {
422
+ for (let j = i + 1; j < allBlocks.length; j++) {
423
+ if (maxComparisons && comparisonsProcessed >= maxComparisons) break;
424
+ comparisonsProcessed++;
425
+ const block2 = allBlocks[j];
426
+ if (block1.file === block2.file) continue;
427
+ const similarity = jaccardSimilarity(blockTokens[i], blockTokens[j]);
428
+ if (similarity >= minSimilarity) {
429
+ const { severity, reason, suggestion, matchedRule } = calculateSeverity(
430
+ block1.file,
431
+ block2.file,
432
+ block1.content,
433
+ similarity,
434
+ block1.linesOfCode
435
+ );
436
+ const duplicate = {
437
+ file1: block1.file,
438
+ file2: block2.file,
439
+ line1: block1.startLine,
440
+ line2: block2.startLine,
441
+ endLine1: block1.endLine,
442
+ endLine2: block2.endLine,
443
+ similarity,
444
+ snippet: block1.content.split("\n").slice(0, 5).join("\n") + "\n...",
445
+ patternType: block1.patternType,
446
+ tokenCost: block1.tokenCost + block2.tokenCost,
447
+ linesOfCode: block1.linesOfCode,
448
+ severity,
449
+ reason,
450
+ suggestion,
451
+ matchedRule
452
+ };
453
+ duplicates.push(duplicate);
454
+ if (streamResults) {
455
+ console.log(`
456
+ \u2705 Found: ${duplicate.patternType} ${Math.round(similarity * 100)}% similar`);
457
+ console.log(` ${duplicate.file1}:${duplicate.line1}-${duplicate.endLine1} \u21D4 ${duplicate.file2}:${duplicate.line2}-${duplicate.endLine2}`);
458
+ console.log(` Token cost: ${duplicate.tokenCost.toLocaleString()}`);
459
+ }
460
+ }
461
+ }
462
+ }
463
+ }
464
+ if (comparisonsBudgetExhausted) {
465
+ console.log(`\u26A0\uFE0F Comparison budget exhausted (${maxComparisons.toLocaleString()} comparisons). Use --max-comparisons to increase.`);
466
+ }
467
+ return duplicates.sort(
468
+ (a, b) => b.similarity - a.similarity || b.tokenCost - a.tokenCost
469
+ );
470
+ }
471
+
472
+ // src/grouping.ts
473
+ function normalizeFilePair(file1, file2) {
474
+ return file1 < file2 ? `${file1}::${file2}` : `${file2}::${file1}`;
475
+ }
476
+ function rangesOverlap(start1, end1, start2, end2, tolerance = 5) {
477
+ return start1 <= end2 + tolerance && start2 <= end1 + tolerance;
478
+ }
479
+ function groupDuplicatesByFilePair(duplicates) {
480
+ const groups = /* @__PURE__ */ new Map();
481
+ for (const dup of duplicates) {
482
+ const key = normalizeFilePair(dup.file1, dup.file2);
483
+ if (!groups.has(key)) {
484
+ groups.set(key, []);
485
+ }
486
+ groups.get(key).push(dup);
487
+ }
488
+ const result = [];
489
+ for (const [filePair, groupDups] of groups.entries()) {
490
+ const deduplicated = deduplicateOverlappingRanges(groupDups);
491
+ const totalTokenCost = deduplicated.reduce((sum, d) => sum + d.tokenCost, 0);
492
+ const averageSimilarity = deduplicated.reduce((sum, d) => sum + d.similarity, 0) / deduplicated.length;
493
+ const maxSimilarity = Math.max(...deduplicated.map((d) => d.similarity));
494
+ const severity = getHighestSeverity(deduplicated.map((d) => d.severity));
495
+ const patternType = getMostCommonPatternType(deduplicated);
496
+ const lineRanges = deduplicated.map((d) => ({
497
+ file1: { start: d.line1, end: d.endLine1 },
498
+ file2: { start: d.line2, end: d.endLine2 }
499
+ }));
500
+ result.push({
501
+ filePair,
502
+ duplicates: deduplicated,
503
+ totalTokenCost,
504
+ averageSimilarity,
505
+ maxSimilarity,
506
+ severity,
507
+ patternType,
508
+ occurrences: deduplicated.length,
509
+ lineRanges
510
+ });
511
+ }
512
+ return result.sort((a, b) => b.totalTokenCost - a.totalTokenCost);
513
+ }
514
+ function deduplicateOverlappingRanges(duplicates) {
515
+ if (duplicates.length === 0) return [];
516
+ const sorted = [...duplicates].sort((a, b) => {
517
+ if (a.line1 !== b.line1) return a.line1 - b.line1;
518
+ return b.similarity - a.similarity;
519
+ });
520
+ const result = [];
521
+ let current = null;
522
+ for (const dup of sorted) {
523
+ if (!current) {
524
+ current = dup;
525
+ result.push(dup);
526
+ continue;
527
+ }
528
+ const overlapsFile1 = rangesOverlap(
529
+ current.line1,
530
+ current.endLine1,
531
+ dup.line1,
532
+ dup.endLine1
533
+ );
534
+ const overlapsFile2 = rangesOverlap(
535
+ current.line2,
536
+ current.endLine2,
537
+ dup.line2,
538
+ dup.endLine2
539
+ );
540
+ if (overlapsFile1 && overlapsFile2) {
541
+ current = {
542
+ ...current,
543
+ endLine1: Math.max(current.endLine1, dup.endLine1),
544
+ endLine2: Math.max(current.endLine2, dup.endLine2),
545
+ tokenCost: Math.max(current.tokenCost, dup.tokenCost)
546
+ };
547
+ result[result.length - 1] = current;
548
+ } else {
549
+ current = dup;
550
+ result.push(dup);
551
+ }
552
+ }
553
+ return result;
554
+ }
555
+ function createRefactorClusters(duplicates) {
556
+ const clusters = /* @__PURE__ */ new Map();
557
+ for (const dup of duplicates) {
558
+ const clusterId = identifyCluster(dup);
559
+ if (!clusters.has(clusterId)) {
560
+ clusters.set(clusterId, []);
561
+ }
562
+ clusters.get(clusterId).push(dup);
563
+ }
564
+ const result = [];
565
+ for (const [clusterId, clusterDups] of clusters.entries()) {
566
+ if (clusterDups.length < 2) continue;
567
+ const files = getUniqueFiles(clusterDups);
568
+ const totalTokenCost = clusterDups.reduce((sum, d) => sum + d.tokenCost, 0);
569
+ const averageSimilarity = clusterDups.reduce((sum, d) => sum + d.similarity, 0) / clusterDups.length;
570
+ const severity = getHighestSeverity(clusterDups.map((d) => d.severity));
571
+ const patternType = getMostCommonPatternType(clusterDups);
572
+ const clusterInfo = getClusterInfo(clusterId, patternType, files.length);
573
+ result.push({
574
+ id: clusterId,
575
+ name: clusterInfo.name,
576
+ files,
577
+ patternType,
578
+ severity,
579
+ totalTokenCost,
580
+ averageSimilarity,
581
+ duplicateCount: clusterDups.length,
582
+ suggestion: clusterInfo.suggestion,
583
+ reason: clusterInfo.reason
584
+ });
585
+ }
586
+ return result.sort((a, b) => b.totalTokenCost - a.totalTokenCost);
587
+ }
588
+ function identifyCluster(dup) {
589
+ const file1 = dup.file1.toLowerCase();
590
+ const file2 = dup.file2.toLowerCase();
591
+ if ((file1.includes("/blog/") || file1.startsWith("blog/") || file1.includes("/articles/") || file1.startsWith("articles/")) && (file2.includes("/blog/") || file2.startsWith("blog/") || file2.includes("/articles/") || file2.startsWith("articles/"))) {
592
+ return "blog-seo-boilerplate";
593
+ }
594
+ if ((file1.includes("/components/") || file1.startsWith("components/")) && (file2.includes("/components/") || file2.startsWith("components/")) && dup.patternType === "component") {
595
+ const component1 = extractComponentName(dup.file1);
596
+ const component2 = extractComponentName(dup.file2);
597
+ console.log(`Component check: ${dup.file1} -> ${component1}, ${dup.file2} -> ${component2}`);
598
+ if (component1 && component2 && areSimilarComponents(component1, component2)) {
599
+ const category = getComponentCategory(component1);
600
+ console.log(`Creating cluster: component-${category}`);
601
+ return `component-${category}`;
602
+ }
603
+ }
604
+ if ((file1.includes("/e2e/") || file1.startsWith("e2e/") || file1.includes(".e2e.")) && (file2.includes("/e2e/") || file2.startsWith("e2e/") || file2.includes(".e2e."))) {
605
+ return "e2e-test-patterns";
606
+ }
607
+ if (dup.patternType === "api-handler") {
608
+ return "api-handlers";
609
+ }
610
+ if (dup.patternType === "validator") {
611
+ return "validators";
612
+ }
613
+ if ((file1.includes("/scripts/") || file1.startsWith("scripts/") || file1.includes("/infra/") || file1.startsWith("infra/")) && (file2.includes("/scripts/") || file2.startsWith("scripts/") || file2.includes("/infra/") || file2.startsWith("infra/"))) {
614
+ return "infrastructure-scripts";
615
+ }
616
+ return `${dup.patternType}-patterns`;
617
+ }
618
+ function extractComponentName(filePath) {
619
+ const match = filePath.match(/[/\\]?([A-Z][a-zA-Z0-9]*)\.(tsx|jsx|ts|js)$/);
620
+ return match ? match[1] : null;
621
+ }
622
+ function areSimilarComponents(name1, name2) {
623
+ const category1 = getComponentCategory(name1);
624
+ const category2 = getComponentCategory(name2);
625
+ return category1 === category2;
626
+ }
627
+ function getComponentCategory(name) {
628
+ name = name.toLowerCase();
629
+ if (name.includes("button") || name.includes("btn")) return "button";
630
+ if (name.includes("card")) return "card";
631
+ if (name.includes("modal") || name.includes("dialog")) return "modal";
632
+ if (name.includes("form")) return "form";
633
+ if (name.includes("input") || name.includes("field")) return "input";
634
+ if (name.includes("table") || name.includes("grid")) return "table";
635
+ if (name.includes("nav") || name.includes("menu")) return "navigation";
636
+ if (name.includes("header") || name.includes("footer")) return "layout";
637
+ return "misc";
638
+ }
639
+ function getUniqueFiles(duplicates) {
640
+ const files = /* @__PURE__ */ new Set();
641
+ for (const dup of duplicates) {
642
+ files.add(dup.file1);
643
+ files.add(dup.file2);
644
+ }
645
+ return Array.from(files).sort();
646
+ }
647
+ function getHighestSeverity(severities) {
648
+ const order = {
649
+ critical: 4,
650
+ major: 3,
651
+ minor: 2,
652
+ info: 1
653
+ };
654
+ let highest = "info";
655
+ let highestValue = 0;
656
+ for (const severity of severities) {
657
+ if (order[severity] > highestValue) {
658
+ highestValue = order[severity];
659
+ highest = severity;
660
+ }
661
+ }
662
+ return highest;
663
+ }
664
+ function getMostCommonPatternType(duplicates) {
665
+ const counts = /* @__PURE__ */ new Map();
666
+ for (const dup of duplicates) {
667
+ counts.set(dup.patternType, (counts.get(dup.patternType) || 0) + 1);
668
+ }
669
+ let mostCommon = "unknown";
670
+ let maxCount = 0;
671
+ for (const [type, count] of counts.entries()) {
672
+ if (count > maxCount) {
673
+ maxCount = count;
674
+ mostCommon = type;
675
+ }
676
+ }
677
+ return mostCommon;
678
+ }
679
+ function getClusterInfo(clusterId, patternType, fileCount) {
680
+ const templates = {
681
+ "blog-seo-boilerplate": {
682
+ name: `Blog SEO Boilerplate (${fileCount} files)`,
683
+ suggestion: "Create BlogPageLayout component with SEO schema generator, breadcrumb component, and metadata helpers",
684
+ reason: "SEO boilerplate duplication increases maintenance burden and schema consistency risk"
685
+ },
686
+ "e2e-test-patterns": {
687
+ name: `E2E Test Patterns (${fileCount} files)`,
688
+ suggestion: "Extract page object helpers and common test utilities (waitFor, fillForm, etc.)",
689
+ reason: "Test helper extraction improves maintainability while preserving test independence"
690
+ },
691
+ "api-handlers": {
692
+ name: `API Handler Patterns (${fileCount} files)`,
693
+ suggestion: "Extract common middleware, error handling, and response formatting",
694
+ reason: "API handler duplication leads to inconsistent error handling and response formats"
695
+ },
696
+ "validators": {
697
+ name: `Validator Patterns (${fileCount} files)`,
698
+ suggestion: "Consolidate into shared schema validators (Zod/Yup) with reusable rules",
699
+ reason: "Validator duplication causes inconsistent validation and harder maintenance"
700
+ },
701
+ "infrastructure-scripts": {
702
+ name: `Infrastructure Scripts (${fileCount} files)`,
703
+ suggestion: "Extract common CLI parsing, file I/O, and error handling utilities",
704
+ reason: "Script duplication is often acceptable for one-off tasks, but common patterns can be shared"
705
+ },
706
+ "component-button": {
707
+ name: `Button Component Variants (${fileCount} files)`,
708
+ suggestion: "Create unified Button component with variant props",
709
+ reason: "Multiple button variants should share base styles and behavior"
710
+ },
711
+ "component-card": {
712
+ name: `Card Component Variants (${fileCount} files)`,
713
+ suggestion: "Create unified Card component with composition pattern",
714
+ reason: "Card variants should share layout structure and styling"
715
+ },
716
+ "component-modal": {
717
+ name: `Modal Component Variants (${fileCount} files)`,
718
+ suggestion: "Create base Modal component with customizable content",
719
+ reason: "Modal variants should share overlay, animation, and accessibility logic"
720
+ }
721
+ };
722
+ if (templates[clusterId]) {
723
+ return templates[clusterId];
724
+ }
725
+ return {
726
+ name: `${patternType} Cluster (${fileCount} files)`,
727
+ suggestion: `Extract common ${patternType} patterns into shared utilities`,
728
+ reason: `Multiple similar ${patternType} patterns detected across ${fileCount} files`
729
+ };
730
+ }
731
+ function filterClustersByImpact(clusters, minTokenCost = 1e3, minFileCount = 3) {
732
+ return clusters.filter(
733
+ (cluster) => cluster.totalTokenCost >= minTokenCost || cluster.files.length >= minFileCount
734
+ );
735
+ }
736
+
737
+ // src/scoring.ts
738
+ import {
739
+ calculateMonthlyCost,
740
+ calculateProductivityImpact,
741
+ DEFAULT_COST_CONFIG
742
+ } from "@aiready/core";
743
+ function calculatePatternScore(duplicates, totalFilesAnalyzed, costConfig) {
744
+ const totalDuplicates = duplicates.length;
745
+ const totalTokenCost = duplicates.reduce((sum, d) => sum + d.tokenCost, 0);
746
+ const highImpactDuplicates = duplicates.filter(
747
+ (d) => d.tokenCost > 1e3 || d.similarity > 0.7
748
+ ).length;
749
+ if (totalFilesAnalyzed === 0) {
750
+ return {
751
+ toolName: "pattern-detect",
752
+ score: 100,
753
+ rawMetrics: { totalDuplicates: 0, totalTokenCost: 0, highImpactDuplicates: 0, totalFilesAnalyzed: 0 },
754
+ factors: [],
755
+ recommendations: []
756
+ };
757
+ }
758
+ const duplicatesPerFile = totalDuplicates / totalFilesAnalyzed * 100;
759
+ const tokenWastePerFile = totalTokenCost / totalFilesAnalyzed;
760
+ const duplicatesPenalty = Math.min(60, duplicatesPerFile * 0.6);
761
+ const tokenPenalty = Math.min(40, tokenWastePerFile / 125);
762
+ const highImpactPenalty = highImpactDuplicates > 0 ? Math.min(15, highImpactDuplicates * 2 - 5) : -5;
763
+ const score = 100 - duplicatesPenalty - tokenPenalty - highImpactPenalty;
764
+ const finalScore = Math.max(0, Math.min(100, Math.round(score)));
765
+ const factors = [
766
+ {
767
+ name: "Duplication Density",
768
+ impact: -Math.round(duplicatesPenalty),
769
+ description: `${duplicatesPerFile.toFixed(1)} duplicates per 100 files`
770
+ },
771
+ {
772
+ name: "Token Waste",
773
+ impact: -Math.round(tokenPenalty),
774
+ description: `${Math.round(tokenWastePerFile)} tokens wasted per file`
775
+ }
776
+ ];
777
+ if (highImpactDuplicates > 0) {
778
+ factors.push({
779
+ name: "High-Impact Patterns",
780
+ impact: -Math.round(highImpactPenalty),
781
+ description: `${highImpactDuplicates} high-impact duplicates (>1000 tokens or >70% similar)`
782
+ });
783
+ } else {
784
+ factors.push({
785
+ name: "No High-Impact Patterns",
786
+ impact: 5,
787
+ description: "No severe duplicates detected"
788
+ });
789
+ }
790
+ const recommendations = [];
791
+ if (highImpactDuplicates > 0) {
792
+ const estimatedImpact = Math.min(15, highImpactDuplicates * 3);
793
+ recommendations.push({
794
+ action: `Deduplicate ${highImpactDuplicates} high-impact pattern${highImpactDuplicates > 1 ? "s" : ""}`,
795
+ estimatedImpact,
796
+ priority: "high"
797
+ });
798
+ }
799
+ if (totalDuplicates > 10 && duplicatesPerFile > 20) {
800
+ const estimatedImpact = Math.min(10, Math.round(duplicatesPenalty * 0.3));
801
+ recommendations.push({
802
+ action: "Extract common patterns into shared utilities",
803
+ estimatedImpact,
804
+ priority: "medium"
805
+ });
806
+ }
807
+ if (tokenWastePerFile > 2e3) {
808
+ const estimatedImpact = Math.min(8, Math.round(tokenPenalty * 0.4));
809
+ recommendations.push({
810
+ action: "Consolidate duplicated logic to reduce AI context waste",
811
+ estimatedImpact,
812
+ priority: totalTokenCost > 1e4 ? "high" : "medium"
813
+ });
814
+ }
815
+ const cfg = { ...DEFAULT_COST_CONFIG, ...costConfig };
816
+ const estimatedMonthlyCost = calculateMonthlyCost(totalTokenCost, cfg);
817
+ const issues = duplicates.map((d) => ({
818
+ severity: d.severity === "critical" ? "critical" : d.severity === "major" ? "major" : "minor"
819
+ }));
820
+ const productivityImpact = calculateProductivityImpact(issues);
821
+ return {
822
+ toolName: "pattern-detect",
823
+ score: finalScore,
824
+ rawMetrics: {
825
+ totalDuplicates,
826
+ totalTokenCost,
827
+ highImpactDuplicates,
828
+ totalFilesAnalyzed,
829
+ duplicatesPerFile: Math.round(duplicatesPerFile * 10) / 10,
830
+ tokenWastePerFile: Math.round(tokenWastePerFile),
831
+ // Business value metrics
832
+ estimatedMonthlyCost,
833
+ estimatedDeveloperHours: productivityImpact.totalHours
834
+ },
835
+ factors,
836
+ recommendations
837
+ };
838
+ }
839
+
840
+ // src/index.ts
841
+ function getRefactoringSuggestion(patternType, similarity) {
842
+ const baseMessages = {
843
+ "api-handler": "Extract common middleware or create a base handler class",
844
+ validator: "Consolidate validation logic into shared schema validators (Zod/Yup)",
845
+ utility: "Move to a shared utilities file and reuse across modules",
846
+ "class-method": "Consider inheritance or composition to share behavior",
847
+ component: "Extract shared logic into a custom hook or HOC",
848
+ function: "Extract into a shared helper function",
849
+ unknown: "Extract common logic into a reusable module"
850
+ };
851
+ const urgency = similarity > 0.95 ? " (CRITICAL: Nearly identical code)" : similarity > 0.9 ? " (HIGH: Very similar, refactor soon)" : "";
852
+ return baseMessages[patternType] + urgency;
853
+ }
854
+ async function getSmartDefaults(directory, userOptions) {
855
+ if (userOptions.useSmartDefaults === false) {
856
+ return {
857
+ rootDir: directory,
858
+ minSimilarity: 0.6,
859
+ minLines: 8,
860
+ batchSize: 100,
861
+ approx: true,
862
+ minSharedTokens: 12,
863
+ maxCandidatesPerBlock: 5,
864
+ streamResults: false,
865
+ severity: "all",
866
+ includeTests: false
867
+ };
868
+ }
869
+ const scanOptions = {
870
+ rootDir: directory,
871
+ include: userOptions.include || ["**/*.{ts,tsx,js,jsx,py,java}"],
872
+ exclude: userOptions.exclude
873
+ };
874
+ const { scanFiles: scanFiles2 } = await import("@aiready/core");
875
+ const files = await scanFiles2(scanOptions);
876
+ const estimatedBlocks = files.length * 3;
877
+ const maxCandidatesPerBlock = Math.max(3, Math.min(10, Math.floor(3e4 / estimatedBlocks)));
878
+ const minSimilarity = Math.min(0.75, 0.5 + estimatedBlocks / 1e4 * 0.25);
879
+ const minLines = Math.max(6, Math.min(12, 6 + Math.floor(estimatedBlocks / 2e3)));
880
+ const minSharedTokens = Math.max(10, Math.min(20, 10 + Math.floor(estimatedBlocks / 2e3)));
881
+ const batchSize = estimatedBlocks > 1e3 ? 200 : 100;
882
+ const severity = estimatedBlocks > 5e3 ? "high" : "all";
883
+ let defaults = {
884
+ rootDir: directory,
885
+ minSimilarity,
886
+ minLines,
887
+ batchSize,
888
+ approx: true,
889
+ minSharedTokens,
890
+ maxCandidatesPerBlock,
891
+ streamResults: false,
892
+ severity,
893
+ includeTests: false
894
+ };
895
+ const result = { ...defaults };
896
+ for (const [key, value] of Object.entries(defaults)) {
897
+ if (key in userOptions && userOptions[key] !== void 0) {
898
+ result[key] = userOptions[key];
899
+ }
900
+ }
901
+ return result;
902
+ }
903
+ function logConfiguration(config, estimatedBlocks) {
904
+ if (config.suppressToolConfig) return;
905
+ console.log("\u{1F4CB} Configuration:");
906
+ console.log(` Repository size: ~${estimatedBlocks} code blocks`);
907
+ console.log(` Similarity threshold: ${config.minSimilarity}`);
908
+ console.log(` Minimum lines: ${config.minLines}`);
909
+ console.log(` Approximate mode: ${config.approx ? "enabled" : "disabled"}`);
910
+ console.log(` Max candidates per block: ${config.maxCandidatesPerBlock}`);
911
+ console.log(` Min shared tokens: ${config.minSharedTokens}`);
912
+ console.log(` Severity filter: ${config.severity}`);
913
+ console.log(` Include tests: ${config.includeTests}`);
914
+ console.log("");
915
+ }
916
+ async function analyzePatterns(options) {
917
+ const smartDefaults = await getSmartDefaults(options.rootDir || ".", options);
918
+ const finalOptions = { ...smartDefaults, ...options };
919
+ const {
920
+ minSimilarity = 0.4,
921
+ minLines = 5,
922
+ batchSize = 100,
923
+ approx = true,
924
+ minSharedTokens = 8,
925
+ maxCandidatesPerBlock = 100,
926
+ streamResults = false,
927
+ severity = "all",
928
+ includeTests = false,
929
+ groupByFilePair = true,
930
+ createClusters = true,
931
+ minClusterTokenCost = 1e3,
932
+ minClusterFiles = 3,
933
+ ...scanOptions
934
+ } = finalOptions;
935
+ const { scanFiles: scanFiles2 } = await import("@aiready/core");
936
+ const files = await scanFiles2(scanOptions);
937
+ const estimatedBlocks = files.length * 3;
938
+ logConfiguration(finalOptions, estimatedBlocks);
939
+ const results = [];
940
+ const fileContents = await Promise.all(
941
+ files.map(async (file) => ({
942
+ file,
943
+ content: await readFileContent(file)
944
+ }))
945
+ );
946
+ const duplicates = await detectDuplicatePatterns(fileContents, {
947
+ minSimilarity,
948
+ minLines,
949
+ batchSize,
950
+ approx,
951
+ minSharedTokens,
952
+ maxCandidatesPerBlock,
953
+ streamResults
954
+ });
955
+ for (const file of files) {
956
+ const fileDuplicates = duplicates.filter(
957
+ (dup) => dup.file1 === file || dup.file2 === file
958
+ );
959
+ const issues = fileDuplicates.map((dup) => {
960
+ const otherFile = dup.file1 === file ? dup.file2 : dup.file1;
961
+ const severity2 = dup.similarity > 0.95 ? "critical" : dup.similarity > 0.9 ? "major" : "minor";
962
+ return {
963
+ type: "duplicate-pattern",
964
+ severity: severity2,
965
+ message: `${dup.patternType} pattern ${Math.round(dup.similarity * 100)}% similar to ${otherFile} (${dup.tokenCost} tokens wasted)`,
966
+ location: {
967
+ file,
968
+ line: dup.file1 === file ? dup.line1 : dup.line2
969
+ },
970
+ suggestion: getRefactoringSuggestion(dup.patternType, dup.similarity)
971
+ };
972
+ });
973
+ let filteredIssues = issues;
974
+ if (severity !== "all") {
975
+ const severityMap = {
976
+ critical: ["critical"],
977
+ high: ["critical", "major"],
978
+ medium: ["critical", "major", "minor"]
979
+ };
980
+ const allowedSeverities = severityMap[severity] || ["critical", "major", "minor"];
981
+ filteredIssues = issues.filter((issue) => allowedSeverities.includes(issue.severity));
982
+ }
983
+ const totalTokenCost = fileDuplicates.reduce(
984
+ (sum, dup) => sum + dup.tokenCost,
985
+ 0
986
+ );
987
+ results.push({
988
+ fileName: file,
989
+ issues: filteredIssues,
990
+ metrics: {
991
+ tokenCost: totalTokenCost,
992
+ consistencyScore: Math.max(0, 1 - fileDuplicates.length * 0.1)
993
+ }
994
+ });
995
+ }
996
+ let groups;
997
+ let clusters;
998
+ if (groupByFilePair) {
999
+ groups = groupDuplicatesByFilePair(duplicates);
1000
+ }
1001
+ if (createClusters) {
1002
+ const allClusters = createRefactorClusters(duplicates);
1003
+ clusters = filterClustersByImpact(allClusters, minClusterTokenCost, minClusterFiles);
1004
+ }
1005
+ return { results, duplicates, files, groups, clusters };
1006
+ }
1007
+ function generateSummary(results) {
1008
+ const allIssues = results.flatMap((r) => r.issues);
1009
+ const totalTokenCost = results.reduce(
1010
+ (sum, r) => sum + (r.metrics.tokenCost || 0),
1011
+ 0
1012
+ );
1013
+ const patternsByType = {
1014
+ "api-handler": 0,
1015
+ validator: 0,
1016
+ utility: 0,
1017
+ "class-method": 0,
1018
+ component: 0,
1019
+ function: 0,
1020
+ unknown: 0
1021
+ };
1022
+ allIssues.forEach((issue) => {
1023
+ const match = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
1024
+ if (match) {
1025
+ const type = match[1];
1026
+ patternsByType[type] = (patternsByType[type] || 0) + 1;
1027
+ }
1028
+ });
1029
+ const topDuplicates = allIssues.slice(0, 10).map((issue) => {
1030
+ const similarityMatch = issue.message.match(/(\d+)% similar/);
1031
+ const tokenMatch = issue.message.match(/\((\d+) tokens/);
1032
+ const typeMatch = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
1033
+ const fileMatch = issue.message.match(/similar to (.+?) \(/);
1034
+ return {
1035
+ files: [
1036
+ {
1037
+ path: issue.location.file,
1038
+ startLine: issue.location.line,
1039
+ endLine: 0
1040
+ // Not available from Issue
1041
+ },
1042
+ {
1043
+ path: fileMatch?.[1] || "unknown",
1044
+ startLine: 0,
1045
+ // Not available from Issue
1046
+ endLine: 0
1047
+ // Not available from Issue
1048
+ }
1049
+ ],
1050
+ similarity: similarityMatch ? parseInt(similarityMatch[1]) / 100 : 0,
1051
+ patternType: typeMatch?.[1] || "unknown",
1052
+ tokenCost: tokenMatch ? parseInt(tokenMatch[1]) : 0
1053
+ };
1054
+ });
1055
+ return {
1056
+ totalPatterns: allIssues.length,
1057
+ totalTokenCost,
1058
+ patternsByType,
1059
+ topDuplicates
1060
+ };
1061
+ }
1062
+
1063
+ export {
1064
+ calculateSeverity,
1065
+ getSeverityLabel,
1066
+ filterBySeverity,
1067
+ detectDuplicatePatterns,
1068
+ calculatePatternScore,
1069
+ getSmartDefaults,
1070
+ analyzePatterns,
1071
+ generateSummary
1072
+ };
package/dist/cli.js CHANGED
@@ -173,7 +173,7 @@ var init_python_extractor = __esm({
173
173
  var import_commander = require("commander");
174
174
 
175
175
  // src/index.ts
176
- var import_core3 = require("@aiready/core");
176
+ var import_core4 = require("@aiready/core");
177
177
 
178
178
  // src/detector.ts
179
179
  var import_core2 = require("@aiready/core");
@@ -899,6 +899,9 @@ function filterClustersByImpact(clusters, minTokenCost = 1e3, minFileCount = 3)
899
899
  );
900
900
  }
901
901
 
902
+ // src/scoring.ts
903
+ var import_core3 = require("@aiready/core");
904
+
902
905
  // src/index.ts
903
906
  function getRefactoringSuggestion(patternType, similarity) {
904
907
  const baseMessages = {
@@ -1002,7 +1005,7 @@ async function analyzePatterns(options) {
1002
1005
  const fileContents = await Promise.all(
1003
1006
  files.map(async (file) => ({
1004
1007
  file,
1005
- content: await (0, import_core3.readFileContent)(file)
1008
+ content: await (0, import_core4.readFileContent)(file)
1006
1009
  }))
1007
1010
  );
1008
1011
  const duplicates = await detectDuplicatePatterns(fileContents, {
@@ -1126,7 +1129,7 @@ function generateSummary(results) {
1126
1129
  var import_chalk = __toESM(require("chalk"));
1127
1130
  var import_fs = require("fs");
1128
1131
  var import_path = require("path");
1129
- var import_core4 = require("@aiready/core");
1132
+ var import_core5 = require("@aiready/core");
1130
1133
  var program = new import_commander.Command();
1131
1134
  program.name("aiready-patterns").description("Detect duplicate patterns in your codebase").version("0.1.0").addHelpText("after", "\nCONFIGURATION:\n Supports config files: aiready.json, aiready.config.json, .aiready.json, .aireadyrc.json, aiready.config.js, .aireadyrc.js\n CLI options override config file settings\n\nPARAMETER TUNING:\n If you get too few results: decrease --similarity, --min-lines, or --min-shared-tokens\n If analysis is too slow: increase --min-lines, --min-shared-tokens, or decrease --max-candidates\n If you get too many false positives: increase --similarity or --min-lines\n\nEXAMPLES:\n aiready-patterns . # Basic analysis with smart defaults\n aiready-patterns . --similarity 0.3 --min-lines 3 # More sensitive detection\n aiready-patterns . --max-candidates 50 --no-approx # Slower but more thorough\n aiready-patterns . --output json > report.json # JSON export").argument("<directory>", "Directory to analyze").option("-s, --similarity <number>", "Minimum similarity score (0-1). Lower = more results, higher = fewer but more accurate. Default: 0.4").option("-l, --min-lines <number>", "Minimum lines to consider. Lower = more results, higher = faster analysis. Default: 5").option("--batch-size <number>", "Batch size for comparisons. Higher = faster but more memory. Default: 100").option("--no-approx", "Disable approximate candidate selection. Slower but more thorough on small repos").option("--min-shared-tokens <number>", "Minimum shared tokens to consider a candidate. Higher = faster, fewer results. Default: 8").option("--max-candidates <number>", "Maximum candidates per block. Higher = more thorough but slower. Default: 100").option("--no-stream-results", "Disable incremental output (default: enabled)").option("--include <patterns>", "File patterns to include (comma-separated)").option("--exclude <patterns>", "File patterns to exclude (comma-separated)").option("--min-severity <level>", "Minimum severity to show: critical|major|minor|info. Default: minor").option("--exclude-test-fixtures", "Exclude test fixture duplication (beforeAll/afterAll)").option("--exclude-templates", "Exclude template file duplication").option("--include-tests", "Include test files in analysis (excluded by default)").option("--max-results <number>", "Maximum number of results to show in console output. Default: 10").option("--no-group-by-file-pair", "Disable grouping duplicates by file pair").option("--no-create-clusters", "Disable creating refactor clusters").option("--min-cluster-tokens <number>", "Minimum token cost for cluster reporting. Default: 1000").option("--min-cluster-files <number>", "Minimum files for cluster reporting. Default: 3").option("--show-raw-duplicates", "Show raw duplicates instead of grouped view").option(
1132
1135
  "-o, --output <format>",
@@ -1135,7 +1138,7 @@ program.name("aiready-patterns").description("Detect duplicate patterns in your
1135
1138
  ).option("--output-file <path>", "Output file path (for json/html)").action(async (directory, options) => {
1136
1139
  console.log(import_chalk.default.blue("\u{1F50D} Analyzing patterns...\n"));
1137
1140
  const startTime = Date.now();
1138
- const config = await (0, import_core4.loadConfig)(directory);
1141
+ const config = await (0, import_core5.loadConfig)(directory);
1139
1142
  const defaults = {
1140
1143
  minSimilarity: 0.4,
1141
1144
  minLines: 5,
@@ -1157,7 +1160,7 @@ program.name("aiready-patterns").description("Detect duplicate patterns in your
1157
1160
  minClusterFiles: 3,
1158
1161
  showRawDuplicates: false
1159
1162
  };
1160
- const mergedConfig = (0, import_core4.mergeConfigWithDefaults)(config, defaults);
1163
+ const mergedConfig = (0, import_core5.mergeConfigWithDefaults)(config, defaults);
1161
1164
  const finalOptions = {
1162
1165
  rootDir: directory,
1163
1166
  minSimilarity: options.similarity ? parseFloat(options.similarity) : mergedConfig.minSimilarity,
@@ -1216,7 +1219,7 @@ program.name("aiready-patterns").description("Detect duplicate patterns in your
1216
1219
  clusters: clusters || [],
1217
1220
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
1218
1221
  };
1219
- const outputPath = (0, import_core4.resolveOutputPath)(
1222
+ const outputPath = (0, import_core5.resolveOutputPath)(
1220
1223
  options.outputFile,
1221
1224
  `pattern-report-${(/* @__PURE__ */ new Date()).toISOString().split("T")[0]}.json`,
1222
1225
  directory
@@ -1232,7 +1235,7 @@ program.name("aiready-patterns").description("Detect duplicate patterns in your
1232
1235
  }
1233
1236
  if (options.output === "html") {
1234
1237
  const html = generateHTMLReport(summary, results);
1235
- const outputPath = (0, import_core4.resolveOutputPath)(
1238
+ const outputPath = (0, import_core5.resolveOutputPath)(
1236
1239
  options.outputFile,
1237
1240
  `pattern-report-${(/* @__PURE__ */ new Date()).toISOString().split("T")[0]}.html`,
1238
1241
  directory
package/dist/cli.mjs CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  analyzePatterns,
4
4
  filterBySeverity,
5
5
  generateSummary
6
- } from "./chunk-MJWBS6SM.mjs";
6
+ } from "./chunk-WKBCNITM.mjs";
7
7
 
8
8
  // src/cli.ts
9
9
  import { Command } from "commander";
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { ToolScoringOutput, ScanOptions, AnalysisResult } from '@aiready/core';
1
+ import { CostConfig, ToolScoringOutput, ScanOptions, AnalysisResult } from '@aiready/core';
2
2
 
3
3
  /**
4
4
  * Context-aware severity detection for duplicate patterns
@@ -108,8 +108,12 @@ interface RefactorCluster {
108
108
  * - Number of duplicates per file
109
109
  * - Token waste per file
110
110
  * - High-impact duplicates (>1000 tokens or >70% similarity)
111
+ *
112
+ * Includes business value metrics:
113
+ * - Estimated monthly cost of token waste
114
+ * - Estimated developer hours to fix
111
115
  */
112
- declare function calculatePatternScore(duplicates: DuplicatePattern[], totalFilesAnalyzed: number): ToolScoringOutput;
116
+ declare function calculatePatternScore(duplicates: DuplicatePattern[], totalFilesAnalyzed: number, costConfig?: Partial<CostConfig>): ToolScoringOutput;
113
117
 
114
118
  interface PatternDetectOptions extends ScanOptions {
115
119
  minSimilarity?: number;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ToolScoringOutput, ScanOptions, AnalysisResult } from '@aiready/core';
1
+ import { CostConfig, ToolScoringOutput, ScanOptions, AnalysisResult } from '@aiready/core';
2
2
 
3
3
  /**
4
4
  * Context-aware severity detection for duplicate patterns
@@ -108,8 +108,12 @@ interface RefactorCluster {
108
108
  * - Number of duplicates per file
109
109
  * - Token waste per file
110
110
  * - High-impact duplicates (>1000 tokens or >70% similarity)
111
+ *
112
+ * Includes business value metrics:
113
+ * - Estimated monthly cost of token waste
114
+ * - Estimated developer hours to fix
111
115
  */
112
- declare function calculatePatternScore(duplicates: DuplicatePattern[], totalFilesAnalyzed: number): ToolScoringOutput;
116
+ declare function calculatePatternScore(duplicates: DuplicatePattern[], totalFilesAnalyzed: number, costConfig?: Partial<CostConfig>): ToolScoringOutput;
113
117
 
114
118
  interface PatternDetectOptions extends ScanOptions {
115
119
  minSimilarity?: number;
package/dist/index.js CHANGED
@@ -182,7 +182,7 @@ __export(index_exports, {
182
182
  getSmartDefaults: () => getSmartDefaults
183
183
  });
184
184
  module.exports = __toCommonJS(index_exports);
185
- var import_core3 = require("@aiready/core");
185
+ var import_core4 = require("@aiready/core");
186
186
 
187
187
  // src/detector.ts
188
188
  var import_core2 = require("@aiready/core");
@@ -918,7 +918,8 @@ function filterClustersByImpact(clusters, minTokenCost = 1e3, minFileCount = 3)
918
918
  }
919
919
 
920
920
  // src/scoring.ts
921
- function calculatePatternScore(duplicates, totalFilesAnalyzed) {
921
+ var import_core3 = require("@aiready/core");
922
+ function calculatePatternScore(duplicates, totalFilesAnalyzed, costConfig) {
922
923
  const totalDuplicates = duplicates.length;
923
924
  const totalTokenCost = duplicates.reduce((sum, d) => sum + d.tokenCost, 0);
924
925
  const highImpactDuplicates = duplicates.filter(
@@ -990,6 +991,12 @@ function calculatePatternScore(duplicates, totalFilesAnalyzed) {
990
991
  priority: totalTokenCost > 1e4 ? "high" : "medium"
991
992
  });
992
993
  }
994
+ const cfg = { ...import_core3.DEFAULT_COST_CONFIG, ...costConfig };
995
+ const estimatedMonthlyCost = (0, import_core3.calculateMonthlyCost)(totalTokenCost, cfg);
996
+ const issues = duplicates.map((d) => ({
997
+ severity: d.severity === "critical" ? "critical" : d.severity === "major" ? "major" : "minor"
998
+ }));
999
+ const productivityImpact = (0, import_core3.calculateProductivityImpact)(issues);
993
1000
  return {
994
1001
  toolName: "pattern-detect",
995
1002
  score: finalScore,
@@ -999,7 +1006,10 @@ function calculatePatternScore(duplicates, totalFilesAnalyzed) {
999
1006
  highImpactDuplicates,
1000
1007
  totalFilesAnalyzed,
1001
1008
  duplicatesPerFile: Math.round(duplicatesPerFile * 10) / 10,
1002
- tokenWastePerFile: Math.round(tokenWastePerFile)
1009
+ tokenWastePerFile: Math.round(tokenWastePerFile),
1010
+ // Business value metrics
1011
+ estimatedMonthlyCost,
1012
+ estimatedDeveloperHours: productivityImpact.totalHours
1003
1013
  },
1004
1014
  factors,
1005
1015
  recommendations
@@ -1109,7 +1119,7 @@ async function analyzePatterns(options) {
1109
1119
  const fileContents = await Promise.all(
1110
1120
  files.map(async (file) => ({
1111
1121
  file,
1112
- content: await (0, import_core3.readFileContent)(file)
1122
+ content: await (0, import_core4.readFileContent)(file)
1113
1123
  }))
1114
1124
  );
1115
1125
  const duplicates = await detectDuplicatePatterns(fileContents, {
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  generateSummary,
8
8
  getSeverityLabel,
9
9
  getSmartDefaults
10
- } from "./chunk-MJWBS6SM.mjs";
10
+ } from "./chunk-WKBCNITM.mjs";
11
11
  export {
12
12
  analyzePatterns,
13
13
  calculatePatternScore,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiready/pattern-detect",
3
- "version": "0.11.20",
3
+ "version": "0.11.24",
4
4
  "description": "Semantic duplicate pattern detection for AI-generated code - finds similar implementations that waste AI context tokens",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -45,7 +45,7 @@
45
45
  "dependencies": {
46
46
  "commander": "^14.0.0",
47
47
  "chalk": "^5.3.0",
48
- "@aiready/core": "0.9.20"
48
+ "@aiready/core": "0.9.25"
49
49
  },
50
50
  "devDependencies": {
51
51
  "tsup": "^8.3.5",