-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatureDetector.ts
More file actions
1104 lines (981 loc) · 39.5 KB
/
Copy pathfeatureDetector.ts
File metadata and controls
1104 lines (981 loc) · 39.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type {
FileAnalysis,
ScannedFile,
EntityGraph,
RelationInfo,
} from "../analysis/index.js";
import { classifyFileRole, isTechnicalFeatureSource, isDocumentationMeta, type FileRole } from "../analysis/index.js";
import type {
DatabaseInfo,
RouteInfo,
CapabilityInfo,
} from "../detectors/index.js";
import { detectFrontendPageFeatures, detectClientRouteFeatures } from "../detectors/index.js";
import type { FileGraph } from "../graph/dependencyGraph.js";
import { countReferences, isArchitectureSource } from "../graph/index.js";
import {
projectFeatureCandidates,
reconcileFeatureCandidates,
type FeatureCandidate,
type FeatureCandidateSource,
type FeatureCluster,
} from "./featureCandidates.js";
import { FEATURE_SIGNALS, hasAiProviderUrl, isAiProviderImport } from "../registry/index.js";
export type FeatureInfo = {
name: string;
purpose: string;
files: string[];
entryPoint?: string;
entryPoints: string[];
businessFlow: string[];
searchTerms: string[];
confidence: "high" | "medium" | "low";
evidence: string[];
};
export type DetectFeaturesResult = {
features: FeatureInfo[];
mergeDecisions: Array<{ candidateIds: [string, string]; outcome: string; anchors: Array<{ type: string; value: string }> }>;
rejectedCandidateIds: string[];
};
export type AuthSemanticRole = "auth-config" | "guard" | "provider" | "consumer";
// ---------------------------------------------------------------------------
// ROLE_FEATURES — only user-visible features, not architectural layers.
//
// Excluded intentionally:
// "api-handler", "service", "middleware", "repository", "ui-component"
// → architectural implementation concerns, not domain features
// "ai-integration"
// → detected via FEATURE_SIGNALS (import-based), role-based too noisy
// "documentation"
// → re-added with strict meta-file filtering via isDocumentationEvidence()
//
// FEATURE_SIGNALS (vocabulary-driven feature detection) lives in
// ../registry/index.js — one file per domain under ../registry/.
// ---------------------------------------------------------------------------
const ROLE_FEATURES: Array<{
role: FileRole;
name: string;
purpose: string;
terms: string[];
}> = [
{
role: "documentation",
name: "Documentation",
purpose: "Contains project documentation, API references, and developer guides.",
terms: ["documentation", "docs", "guide", "reference", "api", "wiki"]
},
{
role: "landing-ui",
name: "Web Landing",
purpose: "Contains public-facing landing page and marketing UI components.",
terms: ["web", "landing", "marketing", "hero", "ui", "homepage"]
},
{
role: "cli-command",
name: "CLI Commands",
purpose: "Contains CLI entry points and command handlers.",
terms: ["cli", "command", "bin", "argv", "commander", "yargs"]
},
];
// ---------------------------------------------------------------------------
// Infrastructure entity names — auth provider internals and ORM bookkeeping
// entities that should never appear as standalone domain features.
//
// These are excluded from entityGraphToFeatures regardless of their relations.
//
// Rule: an entity is infrastructure if it exists solely to support an
// external system (auth provider, ORM, payment processor) and has no
// business logic of its own visible to the application domain.
//
// "Account" is excluded here because in NextAuth / Lucia schemas it
// represents an OAuth provider link record, not a user-facing account.
// Projects that genuinely use "Account" as a domain entity (e.g. billing
// accounts, bank accounts) will still detect it via FEATURE_SIGNALS or
// capabilitiesToFeatures — so excluding it here is safe.
// ---------------------------------------------------------------------------
const INFRASTRUCTURE_ENTITY_NAMES = new Set([
// NextAuth / Lucia / Better-Auth internals
"Account",
"Session",
"VerificationToken",
"VerificationCode",
"Authenticator",
// Generic auth infrastructure
"PasswordResetToken",
"RefreshToken",
"OAuthToken",
"OAuthAccount",
// Audit / system tables
"AuditLog",
"ActivityLog",
"EventLog",
]);
// ---------------------------------------------------------------------------
// Documentation evidence filter
// ---------------------------------------------------------------------------
function isDocumentationEvidence(filePath: string): boolean {
const normalized = filePath.toLowerCase();
const filename = normalized.split("/").at(-1) ?? normalized;
if (/(^|\/)\.github(\/|$)/.test(normalized)) return false;
if (isDocumentationMeta(filename)) return false;
return (
/(^|\/)docs?(\/|$)/.test(normalized)
|| /(^|\/)wiki(\/|$)/.test(normalized)
|| filename === "readme.md"
|| /\.(guide|tutorial|reference)\.(md|mdx)$/.test(filename)
|| /(openapi|swagger)\.(json|yaml|yml)$/.test(filename)
);
}
// ---------------------------------------------------------------------------
// Feature evidence file filter
//
// Excludes files that are generated artifacts or migration history —
// these contain domain keywords (table/column names) that cause false
// positive feature detection without representing actual implementation.
// ---------------------------------------------------------------------------
function isFeatureEvidenceFile(path: string): boolean {
const lower = path.toLowerCase();
return !(
/\/prisma\/migrations\//.test(lower)
|| /\/migrations\//.test(lower)
|| /\.sql$/.test(lower)
|| /\/generated\//.test(lower)
|| /\.generated\./.test(lower)
|| /\/prisma\/schema\.prisma$/.test(lower)
|| /^prisma\/schema\.prisma$/.test(lower)
);
}
// ---------------------------------------------------------------------------
// Registry self-exclusion
//
// Files under ../registry/ *define* the detection vocabulary. They are not
// evidence of a real feature in the scanned project — without this check a
// file like registry/email.ts would self-match the "email" term it describes.
// ---------------------------------------------------------------------------
function isRegistryFile(path: string): boolean {
return /\/analyzers\/registry\//.test(path.toLowerCase());
}
// ---------------------------------------------------------------------------
// FEATURE_FILE_PRIORITIES
// ---------------------------------------------------------------------------
const FEATURE_FILE_PRIORITIES: Record<string, RegExp[]> = {
Documentation: [
/(^|\/)readme\.md$/,
/(^|\/)contributing\.md$/,
/(^|\/)changelog\.md$/,
/(^|\/)license(\.md)?$/,
/(^|\/)docs\/index\.md$/,
/(^|\/)docs\//,
],
Authentication: [
/(^|\/)src\/auth\.[cm]?[jt]sx?$/,
/(^|\/)auth\.[cm]?[jt]sx?$/,
/\/auth\/config\.[cm]?[jt]sx?$/,
/(^|\/)middleware\.[cm]?[jt]sx?$/,
/\/auth\/middleware\.[cm]?[jt]sx?$/,
/\/api\/auth\//,
/\/api\/.*\/(login|register|logout)\.[cm]?[jt]sx?$/,
/\/providers?\/auth[^/]*\.[cm]?[jt]sx?$/,
/\/context\/auth[^/]*\.[cm]?[jt]sx?$/,
],
Payments: [
/\/lib\/stripe\.[cm]?[jt]sx?$/,
/\/lib\/payment[^/]*\.[cm]?[jt]sx?$/,
/\/api\/.*webhook[^/]*\.[cm]?[jt]sx?$/,
/\/api\/.*checkout[^/]*\.[cm]?[jt]sx?$/,
/\/api\/.*payment[^/]*\.[cm]?[jt]sx?$/,
],
"AI Integration": [
/\/lib\/ai\.[cm]?[jt]sx?$/,
/\/ai\/provider\.[cm]?[jt]sx?$/,
/\/ai\/client\.[cm]?[jt]sx?$/,
/\/lib\/openai\.[cm]?[jt]sx?$/,
/\/lib\/groq\.[cm]?[jt]sx?$/,
/\/ai\/prompts?\.[cm]?[jt]sx?$/,
/\/ai\/completion\.[cm]?[jt]sx?$/,
],
Email: [
/\/lib\/email[^/]*\.[cm]?[jt]sx?$/,
/\/lib\/mailer[^/]*\.[cm]?[jt]sx?$/,
/\/emails?\//,
/\/templates?\//,
],
"File Upload": [
/\/lib\/upload[^/]*\.[cm]?[jt]sx?$/,
/\/lib\/storage[^/]*\.[cm]?[jt]sx?$/,
/\/lib\/cloudinary[^/]*\.[cm]?[jt]sx?$/,
/\/api\/.*upload[^/]*\.[cm]?[jt]sx?$/,
],
"Background Jobs": [
/\/lib\/queue[^/]*\.[cm]?[jt]sx?$/,
/\/lib\/worker[^/]*\.[cm]?[jt]sx?$/,
/\/workers?\//,
/\/jobs?\//,
/\/queues?\//,
],
Caching: [
/\/lib\/redis\.[cm]?[jt]sx?$/,
/\/lib\/cache[^/]*\.[cm]?[jt]sx?$/,
/\/cache\//,
],
"CLI Commands": [
/\/src\/index\.[cm]?[jt]sx?$/,
/\/bin\//,
/\/commands?\/index\.[cm]?[jt]sx?$/,
/\/commands?\//,
],
"Web Landing": [
/\/pages\/index\.(astro|tsx?|jsx?)$/,
/\/app\/page\.(tsx?|jsx?)$/,
/\/landing\//,
/(hero|pricing|features?section)[^/]*\.(astro|tsx?|jsx?|vue|svelte)$/,
],
Testing: [
/\/(vitest|jest)\.config\.[cm]?[jt]sx?$/,
/\/test-utils?\.[cm]?[jt]sx?$/,
/\/setup\.(test|spec)\.[cm]?[jt]sx?$/,
],
Search: [
/\/lib\/search[^/]*\.[cm]?[jt]sx?$/,
/\/lib\/meilisearch[^/]*\.[cm]?[jt]sx?$/,
/\/lib\/algolia[^/]*\.[cm]?[jt]sx?$/,
],
"Logging & Monitoring": [
/\/lib\/logger[^/]*\.[cm]?[jt]sx?$/,
/\/lib\/sentry[^/]*\.[cm]?[jt]sx?$/,
/\/instrumentation\.[cm]?[jt]sx?$/,
/\/sentry\.(client|server|edge)\.[cm]?[jt]sx?$/,
],
};
// ---------------------------------------------------------------------------
// Entry point scoring
//
// Lower score = better entry point candidate.
// Generic utility/helper files score >= ENTRY_POINT_EXCLUDE_THRESHOLD (excluded).
// ---------------------------------------------------------------------------
const ENTRY_POINT_EXCLUDE_THRESHOLD = 90;
export type FileTier = "primary" | "supporting" | "reference" | "excluded";
export function classifyFileTier(path: string): FileTier {
const lower = path.toLowerCase();
if (/\/(prisma\/)?migrations?\//.test(lower)) return "excluded";
if (/\.sql$/.test(lower)) return "excluded";
if (/\/generated\//.test(lower) || /\.generated\./.test(lower)) return "excluded";
if (/\.(lock|log|map)$/.test(lower)) return "excluded";
if (/(^|\/)schema\.prisma$/.test(lower)) return "reference";
if (/\.(config|conf)\.[cm]?[jt]s$/.test(lower)) return "reference";
if (/\/(api|routes?)\//.test(lower)) return "primary";
if (/\.(service|usecase|action|route|handler)\.[cm]?[jt]sx?$/.test(lower)) return "primary";
if (/\/(hooks?|stores?)\//.test(lower)) return "primary";
if (/\.[cm]?[jt]sx?$/.test(lower)) return "supporting";
return "reference";
}
function scoreEntryPointRelevance(file: string, _context: string): number {
const lower = file.toLowerCase();
if (/\/(utils?|helpers?|constants?|types?|shared)\.[cm]?[jt]sx?$/.test(lower)) return 100;
if (/\/(index)\.[cm]?[jt]sx?$/.test(lower) && !/\/(api|routes?|commands?)\//.test(lower)) return 95;
if (/\.(d\.ts)$/.test(lower)) return 100;
if (/\/(route|handler)\.[cm]?[jt]sx?$/.test(lower)) return 5;
if (/\/api\//.test(lower)) return 10;
if (/\.(service|usecase|action)\.[cm]?[jt]sx?$/.test(lower)) return 20;
if (/\/services?\//.test(lower)) return 25;
if (/\/(commands?|bin)\//.test(lower)) return 15;
if (/\/(pages?|app)\/.+\/(page|layout)\.[cm]?[jt]sx?$/.test(lower)) return 30;
if (/\/components?\//.test(lower)) return 50;
if (/\/lib\//.test(lower)) return 60;
return 70;
}
// ---------------------------------------------------------------------------
// Main entry
// ---------------------------------------------------------------------------
export function detectFeatures(
files: ScannedFile[],
analyses: Record<string, FileAnalysis>,
routes: RouteInfo[],
database?: DatabaseInfo,
entityGraph?: EntityGraph,
capabilities?: CapabilityInfo[],
fileGraph?: FileGraph
): DetectFeaturesResult {
const candidates: FeatureCandidate[] = [];
const scopedFiles = files.filter((file) => isArchitectureSource(file.path));
// --- ROLE_FEATURES ---
for (const definition of ROLE_FEATURES) {
const evidence = scopedFiles
.filter((file) =>
classifyFileRole(file.path) === definition.role
|| (definition.name === "CLI Commands"
&& /(^|\/)src\/index\.[cm]?[jt]s$/.test(file.path.toLowerCase()))
)
.filter((file) => isFeatureEvidenceFile(file.path))
.filter((file) =>
definition.role !== "documentation" || isDocumentationEvidence(file.path)
)
.map((file) => file.path)
.sort((left, right) =>
featureFilePriority(definition.name, left) - featureFilePriority(definition.name, right)
|| left.localeCompare(right)
)
.slice(0, 12);
if (evidence.length > 0) {
candidates.push(toFeatureCandidate(
"registry",
`role-${definition.role}`,
createFeatureInfo(
definition.name,
evidence,
definition.terms,
definition.purpose,
analyses
)
));
}
}
// --- FEATURE_SIGNALS ---
// Pre-filter once here — avoids redundant isFeatureEvidenceFile calls inside each signal loop
// Registry files (../registry/**) are excluded: they *define* the vocabulary,
// so their paths/content would otherwise self-match every signal they describe.
const technicalFiles = scopedFiles
.filter((file) => isTechnicalFeatureSource(file.path))
.filter((file) => isFeatureEvidenceFile(file.path))
.filter((file) => !isRegistryFile(file.path));
for (const signal of FEATURE_SIGNALS) {
const evidence = technicalFiles
.filter((file) => matchesSignal(file, analyses[file.path], signal.terms, signal.importOnly))
.map((file) => file.path)
.sort((left, right) =>
featureFilePriority(signal.name, left) - featureFilePriority(signal.name, right)
|| left.localeCompare(right)
)
.slice(0, 5);
const primaryEvidence = evidence.filter(
(f) => classifyFileTier(f) === "primary" || classifyFileTier(f) === "supporting"
);
if (signal.minimumDistinctFiles && primaryEvidence.length < signal.minimumDistinctFiles) {
continue;
}
if (evidence.length > 0) {
candidates.push(toFeatureCandidate("registry", `signal-${signal.name}`, createFeatureInfo(
signal.name,
evidence,
signal.terms,
signal.purpose,
analyses
)));
}
}
// Database and API Routes are architectural concerns, not domain features.
// Database info lives in snapshot.database, routes in snapshot.routes.
if (capabilities && capabilities.length > 0) {
for (const feature of capabilitiesToFeatures(capabilities)) {
if (feature !== null) {
candidates.push(toFeatureCandidate("capability", "route-capability", feature, routes));
}
}
}
if (entityGraph && entityGraph.entityNames.length > 0) {
for (const feature of entityGraphToFeatures(entityGraph, scopedFiles)) {
candidates.push(toFeatureCandidate("entity", `entity-${feature.name}`, feature, routes));
}
}
if (fileGraph) {
for (const feature of detectFrontendPageFeatures(routes, fileGraph, analyses, scopedFiles)) {
candidates.push(toFeatureCandidate("frontend-page", "file-page", feature, routes));
}
for (const feature of detectClientRouteFeatures(scopedFiles, fileGraph, analyses)) {
candidates.push(toFeatureCandidate("client-route", "client-route", feature, routes));
}
}
const fileReferenceCounts = fileGraph ? countReferences(fileGraph) : {};
const reconciliation = reconcileFeatureCandidates(candidates, fileReferenceCounts);
const features = projectFeatureCandidates(reconciliation.clusters);
const enriched = enrichAuthenticationFeature(features, reconciliation.clusters, scopedFiles, analyses)
.sort((left, right) => left.name.localeCompare(right.name));
return {
features: enriched,
mergeDecisions: reconciliation.mergeDecisions ?? [],
rejectedCandidateIds: reconciliation.rejectedCandidateIds
};
}
function toFeatureCandidate(
source: FeatureCandidateSource,
ruleId: string,
feature: FeatureInfo,
routes: RouteInfo[] = []
): FeatureCandidate {
const entityNames = feature.name.endsWith(" Management")
? [feature.name.slice(0, -" Management".length)]
: [];
const routePaths = routes
.filter((route) => feature.files.includes(route.file))
.map((route) => route.path)
.sort();
return {
id: `${source}:${ruleId}:${normalizeCandidateSubject(feature.name)}`,
label: feature.name,
source,
evidence: [{
ruleId,
source,
files: [...feature.evidence].sort(),
routePaths,
entityNames,
detail: feature.purpose,
reliability: source === "entity" ? "high" : "medium",
}],
files: [...feature.files].sort(),
routePaths,
entityNames,
conclusionConfidence: feature.confidence,
projection: feature,
};
}
function normalizeCandidateSubject(value: string): string {
return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
}
// ---------------------------------------------------------------------------
// capabilitiesToFeatures
//
// Capabilities with no resolvable entry points are dropped — they represent
// route patterns detected without backing implementation files, which
// produces low-quality features (empty criticalFiles, misleading names).
// ---------------------------------------------------------------------------
function capabilitiesToFeatures(capabilities: CapabilityInfo[]): Array<FeatureInfo | null> {
return capabilities.map((cap) => {
const terms = [cap.kind, ...cap.entities.map((e) => e.toLowerCase())];
const scoredEvidence = cap.evidence
.map((file) => ({ file, score: scoreEntryPointRelevance(file, cap.kind) }))
.sort((a, b) => a.score - b.score);
const entryPoints = scoredEvidence
.filter((e) => e.score < ENTRY_POINT_EXCLUDE_THRESHOLD)
.filter((e) => {
const tier = classifyFileTier(e.file);
return tier === "primary" || tier === "supporting";
})
.slice(0, 2)
.map((e) => e.file);
// Drop capabilities with no resolvable entry points and non-high confidence.
// These are weak detections: route pattern matched but no backing file found.
if (entryPoints.length === 0 && cap.confidence !== "high") return null;
return {
name: cap.name,
purpose: purposeFromCapability(cap),
files: cap.evidence,
entryPoints,
businessFlow: [],
searchTerms: [...new Set(terms)],
confidence: cap.confidence,
evidence: cap.evidence
};
});
}
function purposeFromCapability(cap: CapabilityInfo): string {
const entityList = cap.entities.length > 0 ? cap.entities.join(", ") : "resources";
switch (cap.kind) {
case "crud": return `Handles create, read, update, and delete operations for ${entityList}.`;
case "sharing": return `Handles content sharing via public links and share tokens.`;
case "collaboration": return `Handles team collaboration, workspaces, and member management.`;
case "discovery": return `Handles public content discovery and browsing.`;
case "publishing": return `Handles content publishing and visibility management.`;
case "social": return `Handles social interactions like likes, favorites, and reactions.`;
case "file-management": return `Handles file uploads, storage, and media management.`;
case "real-time": return `Handles real-time events, websockets, and live updates.`;
case "search": return `Handles full-text search and content filtering.`;
case "reporting": return `Handles usage statistics, analytics, and reporting.`;
default: return `Handles ${cap.kind} operations for ${entityList}.`;
}
}
// ---------------------------------------------------------------------------
// entityGraphToFeatures
//
// Entity ownership model:
//
// TRUE CHILD — entity with a single parent via one-to-many AND no own
// children (leaf node), OR has a child-like name suffix.
// Skipped as standalone feature; mentioned in parent's purpose.
// Example: ChecklistItem (owned by Message, no children, suffix "Item")
//
// STANDALONE — entity with multiple parents, or entity that itself owns
// other entities (intermediate node). Gets its own feature.
// Example: Room (owned by User, but owns Message[])
// Example: Message (owned by User+Room, but owns ChecklistItem[])
//
// OWNED — non-infra, non-true-child entities this entity owns via
// one-to-many. Shown in purpose string.
//
// PEER — many-to-many associations. Shown as "associates with X".
//
// Infrastructure entities (auth provider internals, ORM bookkeeping) are
// excluded entirely via INFRASTRUCTURE_ENTITY_NAMES.
// ---------------------------------------------------------------------------
// Suffixes that semantically indicate a sub-item of a parent entity.
// Used as a tiebreaker when an entity has a single parent and no children.
const TRUE_CHILD_SUFFIXES = /(?:Item|Entry|Detail|Line|Row|Part|Step|Variant|Option)$/;
/**
* isTrueChildEntity — determines if an entity should be skipped as standalone.
*
* An entity is a true child if ALL of:
* 1. Exactly ONE parent owns it via one-to-many (single exclusive owner)
* 2. It has NO outgoing one-to-many of its own (leaf node)
* OR its name has a child-like suffix (Item, Entry, Detail, etc.)
*/
function isTrueChildEntity(entityName: string, relations: RelationInfo[]): boolean {
const parents = relations.filter((r) => r.to === entityName && r.kind === "one-to-many");
if (parents.length === 0) return false;
if (parents.length > 1) return false;
const hasOwnChildren = relations.some(
(r) => r.from === entityName && r.kind === "one-to-many"
);
if (!hasOwnChildren) return true;
if (TRUE_CHILD_SUFFIXES.test(entityName)) return true;
return false;
}
function entityFileTierScore(path: string): number {
if (/\.(prisma)$/.test(path)) return 50;
if (/\/(migrations?)\//.test(path)) return 100;
return 0;
}
function findEntityFiles(entityName: string, files: ScannedFile[]): string[] {
const lowerName = entityName.toLowerCase();
const nameSegments = splitNameToSegments(lowerName);
const EXCLUDED_EXTS = /\.(sql|lock|log)(\.[^/]+)?$/i;
const isNonSourceDoc = (p: string) =>
/\.md$/i.test(p) && !/\/docs\//.test(p.toLowerCase());
return files
.filter((f) => isFeatureEvidenceFile(f.path))
.filter((f) => {
const lowerPath = f.path.toLowerCase();
return !EXCLUDED_EXTS.test(lowerPath) && !isNonSourceDoc(lowerPath);
})
.filter((f) => {
const lowerPath = f.path.toLowerCase();
const pathSegments = lowerPath.split(/[/\\]/);
return pathSegments.some((segment) => {
const fileStem = segment.replace(/\.[^/.]+$/, "");
if (fileStem === lowerName) return true;
if (fileStem.includes(lowerName) && fileStem.length >= lowerName.length + 1) return true;
const segParts = splitNameToSegments(fileStem);
return nameSegments.some((ns) => segParts.includes(ns));
});
})
.map((f) => f.path)
.sort((a, b) => {
const tierDiff = entityFileTierScore(a) - entityFileTierScore(b);
if (tierDiff !== 0) return tierDiff;
return a.localeCompare(b);
})
.slice(0, 5);
}
function splitNameToSegments(name: string): string[] {
return name
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter(Boolean);
}
function entityGraphToFeatures(entityGraph: EntityGraph, files: ScannedFile[] = []): FeatureInfo[] {
if (entityGraph.source === "empty") return [];
// Deduplicate relations across the graph
const relations: RelationInfo[] = [];
const seenRelKeys = new Set<string>();
for (const entity of entityGraph.entities) {
for (const r of entity.relations) {
const key = `${r.from}→${r.to}:${r.kind}`;
if (!seenRelKeys.has(key)) {
seenRelKeys.add(key);
relations.push(r);
}
}
}
const trueChildNames = new Set<string>(
entityGraph.entities
.map((e) => e.name)
.filter((name) => isTrueChildEntity(name, relations))
);
const features: FeatureInfo[] = [];
const meaningfulEntities = (entityGraph.source === "prisma"
? entityGraph.entities.filter((e) =>
relations.some((r) => r.from === e.name || r.to === e.name)
)
: entityGraph.entities)
.filter((e) => !trueChildNames.has(e.name) && !INFRASTRUCTURE_ENTITY_NAMES.has(e.name))
// entity dengan implementasi nyata (sourceFiles dari route-hint/SQL) diprioritaskan
// di atas entity yang cuma eksis di schema tanpa file custom
.sort((a, b) => (b.sourceFiles?.length ?? 0) - (a.sourceFiles?.length ?? 0));
for (const entity of meaningfulEntities.slice(0, 8)) {
const ownedNames = relations
.filter((r) => r.from === entity.name && r.kind === "one-to-many")
.map((r) => r.to)
.filter((n) => !INFRASTRUCTURE_ENTITY_NAMES.has(n) && !trueChildNames.has(n));
const oneToOneOwned = relations
.filter((r) => r.from === entity.name && r.kind === "one-to-one")
.map((r) => r.to)
.filter((n) => !INFRASTRUCTURE_ENTITY_NAMES.has(n) && !trueChildNames.has(n));
const allOwned = [...new Set([...ownedNames, ...oneToOneOwned])];
const peerNames = relations
.filter((r) =>
r.kind === "many-to-many"
&& (r.from === entity.name || r.to === entity.name)
)
.map((r) => r.from === entity.name ? r.to : r.from)
.filter((n) => !INFRASTRUCTURE_ENTITY_NAMES.has(n) && !trueChildNames.has(n) && n !== entity.name);
const purpose = buildEntityPurpose(entity.name, allOwned, peerNames);
const searchTerms = [
entity.name.toLowerCase(),
...allOwned.map((n) => n.toLowerCase()),
...peerNames.map((n) => n.toLowerCase()),
"management", "crud"
].filter((v, i, arr) => arr.indexOf(v) === i);
const entityFiles = entity.sourceFiles ?? findEntityFiles(entity.name, files);
if (entityFiles.length === 0) continue;
const entryPoints = entityFiles
.map((file) => ({ file, score: scoreEntryPointRelevance(file, entity.name.toLowerCase()) }))
.sort((a, b) => a.score - b.score)
.filter((e) => e.score < ENTRY_POINT_EXCLUDE_THRESHOLD)
.filter((e) => {
const tier = classifyFileTier(e.file);
return tier === "primary" || tier === "supporting";
})
.slice(0, 2)
.map((e) => e.file);
let confidence: FeatureInfo["confidence"] = entityGraph.source === "prisma" ? "high" : "medium";
if (entityFiles.length === 1) {
const tier = classifyFileTier(entityFiles[0]);
if (tier === "reference") confidence = "low";
}
features.push({
name: `${entity.name} Management`,
purpose,
files: entityFiles,
entryPoints,
businessFlow: [],
searchTerms,
confidence,
evidence: entityFiles
});
}
return features;
}
function buildEntityPurpose(
entityName: string,
owned: string[],
peers: string[]
): string {
if (owned.length > 0 && peers.length > 0) {
return `Manages ${entityName} (including ${owned.join(", ")}) and its associations with ${peers.join(", ")}.`;
}
if (owned.length > 0) {
return `Manages ${entityName} and its owned items: ${owned.join(", ")}.`;
}
if (peers.length > 0) {
return `Manages ${entityName} and its associations with ${peers.join(", ")}.`;
}
return `Manages ${entityName} data and operations.`;
}
// ---------------------------------------------------------------------------
// Confidence calculation
// ---------------------------------------------------------------------------
function calculateFeatureConfidence(
evidence: string[],
analyses: Record<string, FileAnalysis>
): FeatureInfo["confidence"] {
if (evidence.length === 0) return "low";
const highQualityCount = evidence.filter(
(path) => analyses[path]?.confidence === "high"
).length;
if (highQualityCount >= 2) return "high";
if (highQualityCount >= 1 || evidence.length >= 2) return "medium";
return "low";
}
function createFeatureInfo(
name: string,
evidence: string[],
terms: string[],
purpose = `Identifies ${name.toLowerCase()} capability in the project.`,
analyses: Record<string, FileAnalysis> = {}
): FeatureInfo {
const files = evidence.filter((item) => item.includes("/") || /\.[A-Za-z0-9]+$/.test(item));
const entryPoints = files
.map((file) => ({ file, score: scoreEntryPointRelevance(file, name.toLowerCase()) }))
.sort((a, b) => a.score - b.score)
.filter((e) => e.score < ENTRY_POINT_EXCLUDE_THRESHOLD)
.filter((e) => {
const tier = classifyFileTier(e.file);
return tier === "primary" || tier === "supporting";
})
.slice(0, 2)
.map((e) => e.file);
return {
name,
purpose,
files,
businessFlow: [],
entryPoints,
searchTerms: [...new Set(terms.map((term) => term.toLowerCase()))],
confidence: calculateFeatureConfidence(evidence, analyses),
evidence
};
}
/**
* mergeFeature — wrapper untuk backward compat internal usage.
* Delegasi ke similarity-based mergeIntoFeatureList dari featureMerge.ts.
*
* Alasan tidak hapus function ini: masih dipanggil di beberapa tempat di file ini.
* mergeIntoFeatureList menggantikan logika lama yang pakai normalizeFeatureName.
*/
// ---------------------------------------------------------------------------
// matchesSignal
//
// AI Integration: import-only by default — see ../registry/ai-providers.ts for the
// provider vocabulary and the fetch()-URL fallback rules.
//
// All other signals: path matching first, then import matching.
// ---------------------------------------------------------------------------
function matchesSignal(
file: ScannedFile,
analysis: FileAnalysis | undefined,
terms: string[],
importOnly?: boolean
): boolean {
if (importOnly) {
if (analysis?.imports.some(isAiProviderImport)) return true;
if (hasAiProviderUrl(file.content)) return true;
return classifyFileRole(file.path) === "ai-integration";
}
const path = file.path.toLowerCase();
if (terms.some((term) => matchesPathTerm(path, term))) return true;
if (analysis) {
return terms.some((term) =>
analysis.imports.some((specifier) => matchesImportTerm(specifier, term))
);
}
return false;
}
/**
* matchesImportTerm — segment-aware import specifier matching.
*
* Prevents false positives like "author" matching the "auth" signal.
*
* Rules:
* - Scoped packages: exact match on package name after @org/
* e.g. "@auth/core" matches "auth", "@prisma/client" matches "prisma"
* - Unscoped packages: exact match on first segment
* e.g. "next-auth" matches "auth" (via segment boundary), "author" does NOT
* - Local imports: use the same segment-boundary regex as path matching
* e.g. "./author" does NOT match "auth", "./auth/config" DOES match "auth"
*/
function matchesImportTerm(specifier: string, term: string): boolean {
const lower = specifier.toLowerCase();
// Scoped package: @org/name — match against the name segment
const scopedMatch = lower.match(/^@[^/]+\/(.+)$/);
if (scopedMatch) {
const packageName = scopedMatch[1];
// Exact segment match: "auth" matches "next-auth" (contains "-auth")
// but "auth" does NOT match "author" (different segment)
return getOrCompilePattern(term).test(packageName)
|| packageName === term
|| packageName.split("-").includes(term);
}
// Unscoped package or local path: use segment-boundary matching
// This reuses the same regex that protects path matching
return getOrCompilePattern(term).test(lower)
|| lower.split("/").some((segment) => segment === term);
}
const regexCache = new Map<string, RegExp>();
function getOrCompilePattern(term: string): RegExp {
if (regexCache.has(term)) return regexCache.get(term)!;
const pattern = new RegExp(`(?:^|[/._-])${escapeRegex(term)}(?:[/._-]|$)`);
regexCache.set(term, pattern);
return pattern;
}
function matchesPathTerm(path: string, term: string): boolean {
// All terms ≤7 chars use word-boundary matching to prevent partial matches.
//
// ≤3 chars: "ai" must not match "detail", "tailwind", "email"
// 4-7 chars: "search" must not match "SearchSurah.tsx" (component name, not a path segment)
// "upload" must not match "LoginUploadWidget.tsx"
// ≥8 chars: substring is fine — long terms are specific enough (e.g. "elasticsearch")
if (term.length <= 7) {
return getOrCompilePattern(term).test(path);
}
return path.includes(term);
}
function escapeRegex(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
// ---------------------------------------------------------------------------
// Authentication enrichment
// ---------------------------------------------------------------------------
function enrichAuthenticationFeature(
features: FeatureInfo[],
clusters: FeatureCluster[],
files: ScannedFile[],
analyses: Record<string, FileAnalysis>
): FeatureInfo[] {
const authFiles = collectAuthenticationFeatureFiles(files, analyses);
if (authFiles.length === 0) return features;
const authCluster = clusters.find((cluster) =>
cluster.candidates.some((c) => c.source === "registry" && c.label === "Authentication")
);
const targetName = authCluster?.canonicalLabel ?? "Authentication";
const existingTarget = features.find((f) => f.name === targetName);
if (existingTarget) {
return features.map((feature) =>
feature.name === targetName
? {
...feature,
files: orderAuthenticationFiles([...new Set([...feature.files, ...authFiles])]),
evidence: orderAuthenticationFiles([...new Set([...feature.evidence, ...authFiles])]),
confidence: calculateFeatureConfidence(
[...new Set([...feature.evidence, ...authFiles])],
analyses
)
}
: feature
);
}
return [
...features,
createFeatureInfo(targetName, authFiles, [
"auth", "authentication", "login", "session", "jwt", "next-auth"
], undefined, analyses)
];
}
function collectAuthenticationFeatureFiles(
files: ScannedFile[],
analyses: Record<string, FileAnalysis>
): string[] {
return orderAuthenticationFiles(
files
.filter((file) => isArchitectureSource(file.path))
.filter((file) => isTechnicalFeatureSource(file.path))
.filter((file) => isFeatureEvidenceFile(file.path))
.filter((file) => !isAnalyzerImplementationFile(file.path))
.filter((file) => classifyFileRole(file.path) !== "ai-integration")
.filter((file) => {
const analysis = analyses[file.path];
const imports = analysis?.imports ?? extractImportsFallback(file.content);
const symbols = analysis
? analysis.symbols.map((s) => s.name)
: extractSymbolsFallback(file.content);
const role = detectAuthenticationSemanticRole(file.path, symbols, imports, file.content);
return role !== null && role !== "consumer";
})
.map((file) => file.path)
);
}
function isAnalyzerImplementationFile(path: string): boolean {
const normalized = path.toLowerCase();
return (
/(^|\/)(analyzers?|detectors?)\//.test(normalized)
|| /(^|\/)[^/]+(?:analyzer|detector)\.[cm]?[jt]sx?$/.test(normalized)
);
}
export function detectAuthenticationSemanticRole(
path: string,
symbols: string[],
imports: string[],
content = ""
): AuthSemanticRole | null {
const normalizedPath = path.toLowerCase();
const text = `${normalizedPath} ${symbols.join(" ")} ${imports.join(" ")} ${content}`.toLowerCase();
const normalizedImports = imports.map((s) => s.toLowerCase());
const hasAuthImport = normalizedImports.some((s) =>
/(^|[/@-])(auth|next-auth|auth0|clerk|lucia|better-auth|passport|kinde)([/.-]|$)/.test(s)
|| /supabase.*auth/.test(s)
|| /firebase\/auth/.test(s)
);
const hasAuthSymbol = symbols.some((s) =>
/\b(auth|session|login|register|signin|signout|jwt|token|credential)\b/i.test(s)
);
const hasAuthPath = /(^|[/._-])(auth|session|login|register|signin|signout)([/._-]|$)/.test(normalizedPath);