@aiready/pattern-detect 0.7.9 → 0.7.12

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.
@@ -0,0 +1,503 @@
1
+ // src/index.ts
2
+ import { readFileContent } from "@aiready/core";
3
+
4
+ // src/detector.ts
5
+ import { estimateTokens } from "@aiready/core";
6
+ function categorizePattern(code) {
7
+ const lower = code.toLowerCase();
8
+ 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")) {
9
+ return "api-handler";
10
+ }
11
+ if (lower.includes("validate") || lower.includes("schema") || lower.includes("zod") || lower.includes("yup") || lower.includes("if") && lower.includes("throw")) {
12
+ return "validator";
13
+ }
14
+ if (lower.includes("return (") || lower.includes("jsx") || lower.includes("component") || lower.includes("props")) {
15
+ return "component";
16
+ }
17
+ if (lower.includes("class ") || lower.includes("this.")) {
18
+ return "class-method";
19
+ }
20
+ if (lower.includes("return ") && !lower.includes("this") && !lower.includes("new ")) {
21
+ return "utility";
22
+ }
23
+ if (lower.includes("function") || lower.includes("=>")) {
24
+ return "function";
25
+ }
26
+ return "unknown";
27
+ }
28
+ function extractCodeBlocks(content, minLines) {
29
+ const lines = content.split("\n");
30
+ const blocks = [];
31
+ let currentBlock = [];
32
+ let blockStart = 0;
33
+ let braceDepth = 0;
34
+ let inFunction = false;
35
+ for (let i = 0; i < lines.length; i++) {
36
+ const line = lines[i];
37
+ const trimmed = line.trim();
38
+ 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))) {
39
+ inFunction = true;
40
+ blockStart = i;
41
+ }
42
+ for (const char of line) {
43
+ if (char === "{") braceDepth++;
44
+ if (char === "}") braceDepth--;
45
+ }
46
+ if (inFunction) {
47
+ currentBlock.push(line);
48
+ }
49
+ if (inFunction && braceDepth === 0 && currentBlock.length >= minLines) {
50
+ const blockContent = currentBlock.join("\n");
51
+ const linesOfCode = currentBlock.filter(
52
+ (l) => l.trim() && !l.trim().startsWith("//")
53
+ ).length;
54
+ blocks.push({
55
+ content: blockContent,
56
+ startLine: blockStart + 1,
57
+ endLine: i + 1,
58
+ patternType: categorizePattern(blockContent),
59
+ linesOfCode
60
+ });
61
+ currentBlock = [];
62
+ inFunction = false;
63
+ } else if (inFunction && braceDepth === 0) {
64
+ currentBlock = [];
65
+ inFunction = false;
66
+ }
67
+ }
68
+ return blocks;
69
+ }
70
+ function normalizeCode(code) {
71
+ 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();
72
+ }
73
+ function jaccardSimilarity(tokens1, tokens2) {
74
+ const set1 = new Set(tokens1);
75
+ const set2 = new Set(tokens2);
76
+ let intersection = 0;
77
+ for (const token of set1) {
78
+ if (set2.has(token)) intersection++;
79
+ }
80
+ const union = set1.size + set2.size - intersection;
81
+ return union === 0 ? 0 : intersection / union;
82
+ }
83
+ async function detectDuplicatePatterns(files, options) {
84
+ const {
85
+ minSimilarity,
86
+ minLines,
87
+ batchSize = 100,
88
+ approx = true,
89
+ minSharedTokens = 8,
90
+ maxCandidatesPerBlock = 100,
91
+ streamResults = false
92
+ } = options;
93
+ const duplicates = [];
94
+ const maxComparisons = approx ? Infinity : 5e5;
95
+ const allBlocks = files.flatMap(
96
+ (file) => extractCodeBlocks(file.content, minLines).map((block) => ({
97
+ content: block.content,
98
+ startLine: block.startLine,
99
+ endLine: block.endLine,
100
+ file: file.file,
101
+ normalized: normalizeCode(block.content),
102
+ patternType: block.patternType,
103
+ tokenCost: estimateTokens(block.content),
104
+ linesOfCode: block.linesOfCode
105
+ }))
106
+ );
107
+ console.log(`Extracted ${allBlocks.length} code blocks for analysis`);
108
+ if (!approx && allBlocks.length > 500) {
109
+ console.log(`\u26A0\uFE0F Using --no-approx mode with ${allBlocks.length} blocks may be slow (O(B\xB2) complexity).`);
110
+ console.log(` Consider using approximate mode (default) for better performance.`);
111
+ }
112
+ const stopwords = /* @__PURE__ */ new Set([
113
+ "return",
114
+ "const",
115
+ "let",
116
+ "var",
117
+ "function",
118
+ "class",
119
+ "new",
120
+ "if",
121
+ "else",
122
+ "for",
123
+ "while",
124
+ "async",
125
+ "await",
126
+ "try",
127
+ "catch",
128
+ "switch",
129
+ "case",
130
+ "default",
131
+ "import",
132
+ "export",
133
+ "from",
134
+ "true",
135
+ "false",
136
+ "null",
137
+ "undefined",
138
+ "this"
139
+ ]);
140
+ const tokenize = (norm) => norm.split(/[\s(){}\[\];,\.]+/).filter((t) => t && t.length >= 3 && !stopwords.has(t.toLowerCase()));
141
+ const blockTokens = allBlocks.map((b) => tokenize(b.normalized));
142
+ const invertedIndex = /* @__PURE__ */ new Map();
143
+ if (approx) {
144
+ for (let i = 0; i < blockTokens.length; i++) {
145
+ for (const tok of blockTokens[i]) {
146
+ let arr = invertedIndex.get(tok);
147
+ if (!arr) {
148
+ arr = [];
149
+ invertedIndex.set(tok, arr);
150
+ }
151
+ arr.push(i);
152
+ }
153
+ }
154
+ }
155
+ const totalComparisons = approx ? void 0 : allBlocks.length * (allBlocks.length - 1) / 2;
156
+ if (totalComparisons !== void 0) {
157
+ console.log(`Processing ${totalComparisons.toLocaleString()} comparisons in batches...`);
158
+ } else {
159
+ console.log(`Using approximate candidate selection to reduce comparisons...`);
160
+ }
161
+ let comparisonsProcessed = 0;
162
+ let comparisonsBudgetExhausted = false;
163
+ const startTime = Date.now();
164
+ for (let i = 0; i < allBlocks.length; i++) {
165
+ if (maxComparisons && comparisonsProcessed >= maxComparisons) {
166
+ comparisonsBudgetExhausted = true;
167
+ break;
168
+ }
169
+ if (i % batchSize === 0 && i > 0) {
170
+ const elapsed = ((Date.now() - startTime) / 1e3).toFixed(1);
171
+ const duplicatesFound = duplicates.length;
172
+ if (totalComparisons !== void 0) {
173
+ const progress = (comparisonsProcessed / totalComparisons * 100).toFixed(1);
174
+ const remaining = totalComparisons - comparisonsProcessed;
175
+ const rate = comparisonsProcessed / parseFloat(elapsed);
176
+ const eta = remaining > 0 ? (remaining / rate).toFixed(0) : 0;
177
+ console.log(` ${progress}% (${comparisonsProcessed.toLocaleString()}/${totalComparisons.toLocaleString()} comparisons, ${elapsed}s elapsed, ~${eta}s remaining, ${duplicatesFound} duplicates)`);
178
+ } else {
179
+ console.log(` Processed ${i.toLocaleString()}/${allBlocks.length} blocks (${elapsed}s elapsed, ${duplicatesFound} duplicates)`);
180
+ }
181
+ await new Promise((resolve) => setImmediate(resolve));
182
+ }
183
+ const block1 = allBlocks[i];
184
+ let candidates = null;
185
+ if (approx) {
186
+ const counts = /* @__PURE__ */ new Map();
187
+ const block1Tokens = new Set(blockTokens[i]);
188
+ const block1Size = block1Tokens.size;
189
+ const rareTokens = blockTokens[i].filter((tok) => {
190
+ const blocksWithToken = invertedIndex.get(tok)?.length || 0;
191
+ return blocksWithToken < allBlocks.length * 0.1;
192
+ });
193
+ for (const tok of rareTokens) {
194
+ const ids = invertedIndex.get(tok);
195
+ if (!ids) continue;
196
+ for (const j of ids) {
197
+ if (j <= i) continue;
198
+ if (allBlocks[j].file === block1.file) continue;
199
+ counts.set(j, (counts.get(j) || 0) + 1);
200
+ }
201
+ }
202
+ candidates = Array.from(counts.entries()).filter(([j, shared]) => {
203
+ const block2Tokens = blockTokens[j];
204
+ const block2Size = block2Tokens.length;
205
+ const minSize = Math.min(block1Size, block2Size);
206
+ const sharedPercentage = shared / minSize;
207
+ return shared >= minSharedTokens && sharedPercentage >= 0.3;
208
+ }).sort((a, b) => b[1] - a[1]).slice(0, Math.min(maxCandidatesPerBlock, 5)).map(([j, shared]) => ({ j, shared }));
209
+ }
210
+ if (approx && candidates) {
211
+ for (const { j } of candidates) {
212
+ if (!approx && maxComparisons !== Infinity && comparisonsProcessed >= maxComparisons) {
213
+ console.log(`\u26A0\uFE0F Comparison safety limit reached (${maxComparisons.toLocaleString()} comparisons in --no-approx mode).`);
214
+ console.log(` This prevents excessive runtime on large repos. Consider using approximate mode (default) or --min-lines to reduce blocks.`);
215
+ break;
216
+ }
217
+ comparisonsProcessed++;
218
+ const block2 = allBlocks[j];
219
+ const similarity = jaccardSimilarity(blockTokens[i], blockTokens[j]);
220
+ if (similarity >= minSimilarity) {
221
+ const duplicate = {
222
+ file1: block1.file,
223
+ file2: block2.file,
224
+ line1: block1.startLine,
225
+ line2: block2.startLine,
226
+ endLine1: block1.endLine,
227
+ endLine2: block2.endLine,
228
+ similarity,
229
+ snippet: block1.content.split("\n").slice(0, 5).join("\n") + "\n...",
230
+ patternType: block1.patternType,
231
+ tokenCost: block1.tokenCost + block2.tokenCost,
232
+ linesOfCode: block1.linesOfCode
233
+ };
234
+ duplicates.push(duplicate);
235
+ if (streamResults) {
236
+ console.log(`
237
+ \u2705 Found: ${duplicate.patternType} ${Math.round(similarity * 100)}% similar`);
238
+ console.log(` ${duplicate.file1}:${duplicate.line1}-${duplicate.endLine1} \u21D4 ${duplicate.file2}:${duplicate.line2}-${duplicate.endLine2}`);
239
+ console.log(` Token cost: ${duplicate.tokenCost.toLocaleString()}`);
240
+ }
241
+ }
242
+ }
243
+ } else {
244
+ for (let j = i + 1; j < allBlocks.length; j++) {
245
+ if (maxComparisons && comparisonsProcessed >= maxComparisons) break;
246
+ comparisonsProcessed++;
247
+ const block2 = allBlocks[j];
248
+ if (block1.file === block2.file) continue;
249
+ const similarity = jaccardSimilarity(blockTokens[i], blockTokens[j]);
250
+ if (similarity >= minSimilarity) {
251
+ const duplicate = {
252
+ file1: block1.file,
253
+ file2: block2.file,
254
+ line1: block1.startLine,
255
+ line2: block2.startLine,
256
+ endLine1: block1.endLine,
257
+ endLine2: block2.endLine,
258
+ similarity,
259
+ snippet: block1.content.split("\n").slice(0, 5).join("\n") + "\n...",
260
+ patternType: block1.patternType,
261
+ tokenCost: block1.tokenCost + block2.tokenCost,
262
+ linesOfCode: block1.linesOfCode
263
+ };
264
+ duplicates.push(duplicate);
265
+ if (streamResults) {
266
+ console.log(`
267
+ \u2705 Found: ${duplicate.patternType} ${Math.round(similarity * 100)}% similar`);
268
+ console.log(` ${duplicate.file1}:${duplicate.line1}-${duplicate.endLine1} \u21D4 ${duplicate.file2}:${duplicate.line2}-${duplicate.endLine2}`);
269
+ console.log(` Token cost: ${duplicate.tokenCost.toLocaleString()}`);
270
+ }
271
+ }
272
+ }
273
+ }
274
+ }
275
+ if (comparisonsBudgetExhausted) {
276
+ console.log(`\u26A0\uFE0F Comparison budget exhausted (${maxComparisons.toLocaleString()} comparisons). Use --max-comparisons to increase.`);
277
+ }
278
+ return duplicates.sort(
279
+ (a, b) => b.similarity - a.similarity || b.tokenCost - a.tokenCost
280
+ );
281
+ }
282
+
283
+ // src/index.ts
284
+ function getRefactoringSuggestion(patternType, similarity) {
285
+ const baseMessages = {
286
+ "api-handler": "Extract common middleware or create a base handler class",
287
+ validator: "Consolidate validation logic into shared schema validators (Zod/Yup)",
288
+ utility: "Move to a shared utilities file and reuse across modules",
289
+ "class-method": "Consider inheritance or composition to share behavior",
290
+ component: "Extract shared logic into a custom hook or HOC",
291
+ function: "Extract into a shared helper function",
292
+ unknown: "Extract common logic into a reusable module"
293
+ };
294
+ const urgency = similarity > 0.95 ? " (CRITICAL: Nearly identical code)" : similarity > 0.9 ? " (HIGH: Very similar, refactor soon)" : "";
295
+ return baseMessages[patternType] + urgency;
296
+ }
297
+ async function getSmartDefaults(directory, userOptions) {
298
+ if (userOptions.useSmartDefaults === false) {
299
+ return {
300
+ rootDir: directory,
301
+ minSimilarity: 0.6,
302
+ minLines: 8,
303
+ batchSize: 100,
304
+ approx: true,
305
+ minSharedTokens: 12,
306
+ maxCandidatesPerBlock: 5,
307
+ streamResults: false,
308
+ severity: "all",
309
+ includeTests: false
310
+ };
311
+ }
312
+ const scanOptions = {
313
+ rootDir: directory,
314
+ include: userOptions.include || ["**/*.{ts,tsx,js,jsx,py,java}"],
315
+ exclude: userOptions.exclude || [
316
+ "**/node_modules/**",
317
+ "**/dist/**",
318
+ "**/build/**",
319
+ "**/coverage/**",
320
+ "**/.git/**",
321
+ "**/.turbo/**"
322
+ ]
323
+ };
324
+ const { scanFiles: scanFiles2 } = await import("@aiready/core");
325
+ const files = await scanFiles2(scanOptions);
326
+ const estimatedBlocks = files.length * 3;
327
+ const maxCandidatesPerBlock = Math.max(3, Math.min(10, Math.floor(3e4 / estimatedBlocks)));
328
+ const minSimilarity = Math.min(0.75, 0.5 + estimatedBlocks / 1e4 * 0.25);
329
+ const minLines = Math.max(6, Math.min(12, 6 + Math.floor(estimatedBlocks / 2e3)));
330
+ const minSharedTokens = Math.max(10, Math.min(20, 10 + Math.floor(estimatedBlocks / 2e3)));
331
+ const batchSize = estimatedBlocks > 1e3 ? 200 : 100;
332
+ const severity = estimatedBlocks > 5e3 ? "high" : "all";
333
+ let defaults = {
334
+ rootDir: directory,
335
+ minSimilarity,
336
+ minLines,
337
+ batchSize,
338
+ approx: true,
339
+ minSharedTokens,
340
+ maxCandidatesPerBlock,
341
+ streamResults: false,
342
+ severity,
343
+ includeTests: false
344
+ };
345
+ const result = { ...defaults };
346
+ for (const [key, value] of Object.entries(defaults)) {
347
+ if (key in userOptions && userOptions[key] !== void 0) {
348
+ result[key] = userOptions[key];
349
+ }
350
+ }
351
+ return result;
352
+ }
353
+ function logConfiguration(config, estimatedBlocks) {
354
+ console.log("\u{1F4CB} Configuration:");
355
+ console.log(` Repository size: ~${estimatedBlocks} code blocks`);
356
+ console.log(` Similarity threshold: ${config.minSimilarity}`);
357
+ console.log(` Minimum lines: ${config.minLines}`);
358
+ console.log(` Approximate mode: ${config.approx ? "enabled" : "disabled"}`);
359
+ console.log(` Max candidates per block: ${config.maxCandidatesPerBlock}`);
360
+ console.log(` Min shared tokens: ${config.minSharedTokens}`);
361
+ console.log(` Severity filter: ${config.severity}`);
362
+ console.log(` Include tests: ${config.includeTests}`);
363
+ console.log("");
364
+ }
365
+ async function analyzePatterns(options) {
366
+ const smartDefaults = await getSmartDefaults(options.rootDir || ".", options);
367
+ const finalOptions = { ...smartDefaults, ...options };
368
+ const {
369
+ minSimilarity = 0.4,
370
+ minLines = 5,
371
+ batchSize = 100,
372
+ approx = true,
373
+ minSharedTokens = 8,
374
+ maxCandidatesPerBlock = 100,
375
+ streamResults = false,
376
+ severity = "all",
377
+ includeTests = false,
378
+ ...scanOptions
379
+ } = finalOptions;
380
+ const { scanFiles: scanFiles2 } = await import("@aiready/core");
381
+ const files = await scanFiles2(scanOptions);
382
+ const estimatedBlocks = files.length * 3;
383
+ logConfiguration(finalOptions, estimatedBlocks);
384
+ const results = [];
385
+ const fileContents = await Promise.all(
386
+ files.map(async (file) => ({
387
+ file,
388
+ content: await readFileContent(file)
389
+ }))
390
+ );
391
+ const duplicates = await detectDuplicatePatterns(fileContents, {
392
+ minSimilarity,
393
+ minLines,
394
+ batchSize,
395
+ approx,
396
+ minSharedTokens,
397
+ maxCandidatesPerBlock,
398
+ streamResults
399
+ });
400
+ for (const file of files) {
401
+ const fileDuplicates = duplicates.filter(
402
+ (dup) => dup.file1 === file || dup.file2 === file
403
+ );
404
+ const issues = fileDuplicates.map((dup) => {
405
+ const otherFile = dup.file1 === file ? dup.file2 : dup.file1;
406
+ const severity2 = dup.similarity > 0.95 ? "critical" : dup.similarity > 0.9 ? "major" : "minor";
407
+ return {
408
+ type: "duplicate-pattern",
409
+ severity: severity2,
410
+ message: `${dup.patternType} pattern ${Math.round(dup.similarity * 100)}% similar to ${otherFile} (${dup.tokenCost} tokens wasted)`,
411
+ location: {
412
+ file,
413
+ line: dup.file1 === file ? dup.line1 : dup.line2
414
+ },
415
+ suggestion: getRefactoringSuggestion(dup.patternType, dup.similarity)
416
+ };
417
+ });
418
+ let filteredIssues = issues;
419
+ if (severity !== "all") {
420
+ const severityMap = {
421
+ critical: ["critical"],
422
+ high: ["critical", "major"],
423
+ medium: ["critical", "major", "minor"]
424
+ };
425
+ const allowedSeverities = severityMap[severity] || ["critical", "major", "minor"];
426
+ filteredIssues = issues.filter((issue) => allowedSeverities.includes(issue.severity));
427
+ }
428
+ const totalTokenCost = fileDuplicates.reduce(
429
+ (sum, dup) => sum + dup.tokenCost,
430
+ 0
431
+ );
432
+ results.push({
433
+ fileName: file,
434
+ issues: filteredIssues,
435
+ metrics: {
436
+ tokenCost: totalTokenCost,
437
+ consistencyScore: Math.max(0, 1 - fileDuplicates.length * 0.1)
438
+ }
439
+ });
440
+ }
441
+ return { results, duplicates, files };
442
+ }
443
+ function generateSummary(results) {
444
+ const allIssues = results.flatMap((r) => r.issues);
445
+ const totalTokenCost = results.reduce(
446
+ (sum, r) => sum + (r.metrics.tokenCost || 0),
447
+ 0
448
+ );
449
+ const patternsByType = {
450
+ "api-handler": 0,
451
+ validator: 0,
452
+ utility: 0,
453
+ "class-method": 0,
454
+ component: 0,
455
+ function: 0,
456
+ unknown: 0
457
+ };
458
+ allIssues.forEach((issue) => {
459
+ const match = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
460
+ if (match) {
461
+ const type = match[1];
462
+ patternsByType[type] = (patternsByType[type] || 0) + 1;
463
+ }
464
+ });
465
+ const topDuplicates = allIssues.slice(0, 10).map((issue) => {
466
+ const similarityMatch = issue.message.match(/(\d+)% similar/);
467
+ const tokenMatch = issue.message.match(/\((\d+) tokens/);
468
+ const typeMatch = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
469
+ const fileMatch = issue.message.match(/similar to (.+?) \(/);
470
+ return {
471
+ files: [
472
+ {
473
+ path: issue.location.file,
474
+ startLine: issue.location.line,
475
+ endLine: 0
476
+ // Not available from Issue
477
+ },
478
+ {
479
+ path: fileMatch?.[1] || "unknown",
480
+ startLine: 0,
481
+ // Not available from Issue
482
+ endLine: 0
483
+ // Not available from Issue
484
+ }
485
+ ],
486
+ similarity: similarityMatch ? parseInt(similarityMatch[1]) / 100 : 0,
487
+ patternType: typeMatch?.[1] || "unknown",
488
+ tokenCost: tokenMatch ? parseInt(tokenMatch[1]) : 0
489
+ };
490
+ });
491
+ return {
492
+ totalPatterns: allIssues.length,
493
+ totalTokenCost,
494
+ patternsByType,
495
+ topDuplicates
496
+ };
497
+ }
498
+
499
+ export {
500
+ detectDuplicatePatterns,
501
+ analyzePatterns,
502
+ generateSummary
503
+ };
package/dist/cli.js CHANGED
@@ -212,7 +212,13 @@ async function detectDuplicatePatterns(files, options) {
212
212
  let candidates = null;
213
213
  if (approx) {
214
214
  const counts = /* @__PURE__ */ new Map();
215
- for (const tok of blockTokens[i]) {
215
+ const block1Tokens = new Set(blockTokens[i]);
216
+ const block1Size = block1Tokens.size;
217
+ const rareTokens = blockTokens[i].filter((tok) => {
218
+ const blocksWithToken = invertedIndex.get(tok)?.length || 0;
219
+ return blocksWithToken < allBlocks.length * 0.1;
220
+ });
221
+ for (const tok of rareTokens) {
216
222
  const ids = invertedIndex.get(tok);
217
223
  if (!ids) continue;
218
224
  for (const j of ids) {
@@ -221,7 +227,13 @@ async function detectDuplicatePatterns(files, options) {
221
227
  counts.set(j, (counts.get(j) || 0) + 1);
222
228
  }
223
229
  }
224
- candidates = Array.from(counts.entries()).filter(([, shared]) => shared >= minSharedTokens).sort((a, b) => b[1] - a[1]).slice(0, maxCandidatesPerBlock).map(([j, shared]) => ({ j, shared }));
230
+ candidates = Array.from(counts.entries()).filter(([j, shared]) => {
231
+ const block2Tokens = blockTokens[j];
232
+ const block2Size = block2Tokens.length;
233
+ const minSize = Math.min(block1Size, block2Size);
234
+ const sharedPercentage = shared / minSize;
235
+ return shared >= minSharedTokens && sharedPercentage >= 0.3;
236
+ }).sort((a, b) => b[1] - a[1]).slice(0, Math.min(maxCandidatesPerBlock, 5)).map(([j, shared]) => ({ j, shared }));
225
237
  }
226
238
  if (approx && candidates) {
227
239
  for (const { j } of candidates) {
@@ -314,12 +326,12 @@ async function getSmartDefaults(directory, userOptions) {
314
326
  if (userOptions.useSmartDefaults === false) {
315
327
  return {
316
328
  rootDir: directory,
317
- minSimilarity: 0.4,
318
- minLines: 5,
329
+ minSimilarity: 0.6,
330
+ minLines: 8,
319
331
  batchSize: 100,
320
332
  approx: true,
321
- minSharedTokens: 8,
322
- maxCandidatesPerBlock: 100,
333
+ minSharedTokens: 12,
334
+ maxCandidatesPerBlock: 5,
323
335
  streamResults: false,
324
336
  severity: "all",
325
337
  includeTests: false
@@ -340,12 +352,12 @@ async function getSmartDefaults(directory, userOptions) {
340
352
  const { scanFiles: scanFiles2 } = await import("@aiready/core");
341
353
  const files = await scanFiles2(scanOptions);
342
354
  const estimatedBlocks = files.length * 3;
343
- const maxCandidatesPerBlock = Math.max(10, Math.min(100, Math.floor(8e4 / estimatedBlocks)));
344
- const minSimilarity = Math.min(0.65, 0.4 + estimatedBlocks / 15e3 * 0.25);
345
- const minLines = Math.max(5, Math.min(10, 5 + Math.floor(estimatedBlocks / 3e3)));
346
- const minSharedTokens = Math.max(8, Math.min(15, 8 + Math.floor(estimatedBlocks / 4e3)));
347
- const batchSize = estimatedBlocks > 2e3 ? 300 : 150;
348
- const severity = estimatedBlocks > 8e3 ? "high" : "all";
355
+ const maxCandidatesPerBlock = Math.max(3, Math.min(10, Math.floor(3e4 / estimatedBlocks)));
356
+ const minSimilarity = Math.min(0.75, 0.5 + estimatedBlocks / 1e4 * 0.25);
357
+ const minLines = Math.max(6, Math.min(12, 6 + Math.floor(estimatedBlocks / 2e3)));
358
+ const minSharedTokens = Math.max(10, Math.min(20, 10 + Math.floor(estimatedBlocks / 2e3)));
359
+ const batchSize = estimatedBlocks > 1e3 ? 200 : 100;
360
+ const severity = estimatedBlocks > 5e3 ? "high" : "all";
349
361
  let defaults = {
350
362
  rootDir: directory,
351
363
  minSimilarity,
@@ -518,7 +530,7 @@ var import_fs = require("fs");
518
530
  var import_path = require("path");
519
531
  var import_core3 = require("@aiready/core");
520
532
  var program = new import_commander.Command();
521
- 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").argument("<directory>", "Directory to analyze").option("-s, --similarity <number>", "Minimum similarity score (0-1)").option("-l, --min-lines <number>", "Minimum lines to consider").option("--batch-size <number>", "Batch size for comparisons").option("--no-approx", "Disable approximate candidate selection (faster on small repos, slower on large)").option("--min-shared-tokens <number>", "Minimum shared tokens to consider a candidate").option("--max-candidates <number>", "Maximum candidates per block").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("--severity <level>", "Filter by severity: critical|high|medium|all").option("--include-tests", "Include test files in analysis (excluded by default)").option("--max-results <number>", "Maximum number of results to show in console output").option(
533
+ 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("--severity <level>", "Filter by severity: critical|high|medium|all. Default: all").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(
522
534
  "-o, --output <format>",
523
535
  "Output format: console, json, html",
524
536
  "console"
@@ -670,6 +682,20 @@ program.name("aiready-patterns").description("Detect duplicate patterns in your
670
682
  }
671
683
  if (totalIssues === 0) {
672
684
  console.log(import_chalk.default.green("\n\u2728 Great! No duplicate patterns detected.\n"));
685
+ console.log(import_chalk.default.yellow("\u{1F4A1} If you expected to find duplicates, try adjusting parameters:"));
686
+ console.log(import_chalk.default.dim(" \u2022 Lower similarity threshold: --similarity 0.3"));
687
+ console.log(import_chalk.default.dim(" \u2022 Reduce minimum lines: --min-lines 3"));
688
+ console.log(import_chalk.default.dim(" \u2022 Include test files: --include-tests"));
689
+ console.log(import_chalk.default.dim(" \u2022 Lower shared tokens threshold: --min-shared-tokens 5"));
690
+ console.log("");
691
+ }
692
+ if (totalIssues > 0 && totalIssues < 5) {
693
+ console.log(import_chalk.default.yellow("\n\u{1F4A1} Few results found. To find more duplicates, try:"));
694
+ console.log(import_chalk.default.dim(" \u2022 Lower similarity threshold: --similarity 0.3"));
695
+ console.log(import_chalk.default.dim(" \u2022 Reduce minimum lines: --min-lines 3"));
696
+ console.log(import_chalk.default.dim(" \u2022 Include test files: --include-tests"));
697
+ console.log(import_chalk.default.dim(" \u2022 Lower shared tokens threshold: --min-shared-tokens 5"));
698
+ console.log("");
673
699
  }
674
700
  console.log(import_chalk.default.cyan(divider));
675
701
  if (totalIssues > 0) {
package/dist/cli.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  analyzePatterns,
4
4
  generateSummary
5
- } from "./chunk-S2KQFII2.mjs";
5
+ } from "./chunk-GSJFORRO.mjs";
6
6
 
7
7
  // src/cli.ts
8
8
  import { Command } from "commander";
@@ -11,7 +11,7 @@ import { writeFileSync } from "fs";
11
11
  import { join } from "path";
12
12
  import { loadConfig, mergeConfigWithDefaults } from "@aiready/core";
13
13
  var program = new Command();
14
- 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").argument("<directory>", "Directory to analyze").option("-s, --similarity <number>", "Minimum similarity score (0-1)").option("-l, --min-lines <number>", "Minimum lines to consider").option("--batch-size <number>", "Batch size for comparisons").option("--no-approx", "Disable approximate candidate selection (faster on small repos, slower on large)").option("--min-shared-tokens <number>", "Minimum shared tokens to consider a candidate").option("--max-candidates <number>", "Maximum candidates per block").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("--severity <level>", "Filter by severity: critical|high|medium|all").option("--include-tests", "Include test files in analysis (excluded by default)").option("--max-results <number>", "Maximum number of results to show in console output").option(
14
+ 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("--severity <level>", "Filter by severity: critical|high|medium|all. Default: all").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(
15
15
  "-o, --output <format>",
16
16
  "Output format: console, json, html",
17
17
  "console"
@@ -163,6 +163,20 @@ program.name("aiready-patterns").description("Detect duplicate patterns in your
163
163
  }
164
164
  if (totalIssues === 0) {
165
165
  console.log(chalk.green("\n\u2728 Great! No duplicate patterns detected.\n"));
166
+ console.log(chalk.yellow("\u{1F4A1} If you expected to find duplicates, try adjusting parameters:"));
167
+ console.log(chalk.dim(" \u2022 Lower similarity threshold: --similarity 0.3"));
168
+ console.log(chalk.dim(" \u2022 Reduce minimum lines: --min-lines 3"));
169
+ console.log(chalk.dim(" \u2022 Include test files: --include-tests"));
170
+ console.log(chalk.dim(" \u2022 Lower shared tokens threshold: --min-shared-tokens 5"));
171
+ console.log("");
172
+ }
173
+ if (totalIssues > 0 && totalIssues < 5) {
174
+ console.log(chalk.yellow("\n\u{1F4A1} Few results found. To find more duplicates, try:"));
175
+ console.log(chalk.dim(" \u2022 Lower similarity threshold: --similarity 0.3"));
176
+ console.log(chalk.dim(" \u2022 Reduce minimum lines: --min-lines 3"));
177
+ console.log(chalk.dim(" \u2022 Include test files: --include-tests"));
178
+ console.log(chalk.dim(" \u2022 Lower shared tokens threshold: --min-shared-tokens 5"));
179
+ console.log("");
166
180
  }
167
181
  console.log(chalk.cyan(divider));
168
182
  if (totalIssues > 0) {
package/dist/index.d.mts CHANGED
@@ -61,6 +61,10 @@ interface PatternSummary {
61
61
  tokenCost: number;
62
62
  }>;
63
63
  }
64
+ /**
65
+ * Determine smart defaults based on repository size estimation
66
+ */
67
+ declare function getSmartDefaults(directory: string, userOptions: Partial<PatternDetectOptions>): Promise<PatternDetectOptions>;
64
68
  declare function analyzePatterns(options: PatternDetectOptions): Promise<{
65
69
  results: AnalysisResult[];
66
70
  duplicates: DuplicatePattern[];
@@ -71,4 +75,4 @@ declare function analyzePatterns(options: PatternDetectOptions): Promise<{
71
75
  */
72
76
  declare function generateSummary(results: AnalysisResult[]): PatternSummary;
73
77
 
74
- export { type DuplicatePattern, type PatternDetectOptions, type PatternSummary, type PatternType, analyzePatterns, detectDuplicatePatterns, generateSummary };
78
+ export { type DuplicatePattern, type PatternDetectOptions, type PatternSummary, type PatternType, analyzePatterns, detectDuplicatePatterns, generateSummary, getSmartDefaults };