@girardelli/architect-core 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/dist/src/core/analyzer.d.ts +42 -0
  2. package/dist/src/core/analyzer.js +431 -0
  3. package/dist/src/core/analyzer.js.map +1 -0
  4. package/dist/src/core/analyzers/forecast.d.ts +84 -0
  5. package/dist/src/core/analyzers/forecast.js +338 -0
  6. package/dist/src/core/analyzers/forecast.js.map +1 -0
  7. package/dist/src/core/analyzers/index.d.ts +9 -0
  8. package/dist/src/core/analyzers/index.js +7 -0
  9. package/dist/src/core/analyzers/index.js.map +1 -0
  10. package/dist/src/core/analyzers/temporal-scorer.d.ts +71 -0
  11. package/dist/src/core/analyzers/temporal-scorer.js +141 -0
  12. package/dist/src/core/analyzers/temporal-scorer.js.map +1 -0
  13. package/dist/src/core/anti-patterns.d.ts +28 -0
  14. package/dist/src/core/anti-patterns.js +264 -0
  15. package/dist/src/core/anti-patterns.js.map +1 -0
  16. package/dist/src/core/ast/ast-parser.interface.d.ts +20 -0
  17. package/dist/src/core/ast/ast-parser.interface.js +2 -0
  18. package/dist/src/core/ast/ast-parser.interface.js.map +1 -0
  19. package/dist/src/core/ast/path-resolver.d.ts +13 -0
  20. package/dist/src/core/ast/path-resolver.js +54 -0
  21. package/dist/src/core/ast/path-resolver.js.map +1 -0
  22. package/dist/src/core/ast/tree-sitter-parser.d.ts +10 -0
  23. package/dist/src/core/ast/tree-sitter-parser.js +142 -0
  24. package/dist/src/core/ast/tree-sitter-parser.js.map +1 -0
  25. package/dist/src/core/config.d.ts +11 -0
  26. package/dist/src/core/config.js +112 -0
  27. package/dist/src/core/config.js.map +1 -0
  28. package/dist/src/core/diagram.d.ts +9 -0
  29. package/dist/src/core/diagram.js +101 -0
  30. package/dist/src/core/diagram.js.map +1 -0
  31. package/dist/src/core/i18n.d.ts +14 -0
  32. package/dist/src/core/i18n.js +54 -0
  33. package/dist/src/core/i18n.js.map +1 -0
  34. package/dist/src/core/locales/en.d.ts +2 -0
  35. package/dist/src/core/locales/en.js +337 -0
  36. package/dist/src/core/locales/en.js.map +1 -0
  37. package/dist/src/core/locales/pt-BR.d.ts +172 -0
  38. package/dist/src/core/locales/pt-BR.js +337 -0
  39. package/dist/src/core/locales/pt-BR.js.map +1 -0
  40. package/dist/src/core/locales/types.d.ts +86 -0
  41. package/dist/src/core/locales/types.js +2 -0
  42. package/dist/src/core/locales/types.js.map +1 -0
  43. package/dist/src/core/plugin-loader.d.ts +11 -0
  44. package/dist/src/core/plugin-loader.js +67 -0
  45. package/dist/src/core/plugin-loader.js.map +1 -0
  46. package/dist/src/core/project-summarizer.d.ts +16 -0
  47. package/dist/src/core/project-summarizer.js +37 -0
  48. package/dist/src/core/project-summarizer.js.map +1 -0
  49. package/dist/src/core/refactor-engine.d.ts +18 -0
  50. package/dist/src/core/refactor-engine.js +87 -0
  51. package/dist/src/core/refactor-engine.js.map +1 -0
  52. package/dist/src/core/rules/barrel-optimizer.d.ts +13 -0
  53. package/dist/src/core/rules/barrel-optimizer.js +76 -0
  54. package/dist/src/core/rules/barrel-optimizer.js.map +1 -0
  55. package/dist/src/core/rules/dead-code-detector.d.ts +21 -0
  56. package/dist/src/core/rules/dead-code-detector.js +116 -0
  57. package/dist/src/core/rules/dead-code-detector.js.map +1 -0
  58. package/dist/src/core/rules/hub-splitter.d.ts +13 -0
  59. package/dist/src/core/rules/hub-splitter.js +117 -0
  60. package/dist/src/core/rules/hub-splitter.js.map +1 -0
  61. package/dist/src/core/rules/import-organizer.d.ts +13 -0
  62. package/dist/src/core/rules/import-organizer.js +84 -0
  63. package/dist/src/core/rules/import-organizer.js.map +1 -0
  64. package/dist/src/core/rules/module-grouper.d.ts +13 -0
  65. package/dist/src/core/rules/module-grouper.js +116 -0
  66. package/dist/src/core/rules/module-grouper.js.map +1 -0
  67. package/dist/src/core/rules-engine.d.ts +7 -0
  68. package/dist/src/core/rules-engine.js +89 -0
  69. package/dist/src/core/rules-engine.js.map +1 -0
  70. package/dist/src/core/scorer.d.ts +15 -0
  71. package/dist/src/core/scorer.js +165 -0
  72. package/dist/src/core/scorer.js.map +1 -0
  73. package/dist/src/core/summarizer/keyword-extractor.d.ts +6 -0
  74. package/dist/src/core/summarizer/keyword-extractor.js +38 -0
  75. package/dist/src/core/summarizer/keyword-extractor.js.map +1 -0
  76. package/dist/src/core/summarizer/module-inferrer.d.ts +11 -0
  77. package/dist/src/core/summarizer/module-inferrer.js +171 -0
  78. package/dist/src/core/summarizer/module-inferrer.js.map +1 -0
  79. package/dist/src/core/summarizer/package-reader.d.ts +3 -0
  80. package/dist/src/core/summarizer/package-reader.js +33 -0
  81. package/dist/src/core/summarizer/package-reader.js.map +1 -0
  82. package/dist/src/core/summarizer/purpose-inferrer.d.ts +8 -0
  83. package/dist/src/core/summarizer/purpose-inferrer.js +179 -0
  84. package/dist/src/core/summarizer/purpose-inferrer.js.map +1 -0
  85. package/dist/src/core/summarizer/readme-reader.d.ts +3 -0
  86. package/dist/src/core/summarizer/readme-reader.js +24 -0
  87. package/dist/src/core/summarizer/readme-reader.js.map +1 -0
  88. package/dist/src/core/types/architect-rules.d.ts +27 -0
  89. package/dist/src/core/types/architect-rules.js +2 -0
  90. package/dist/src/core/types/architect-rules.js.map +1 -0
  91. package/dist/src/core/types/core.d.ts +87 -0
  92. package/dist/src/core/types/core.js +2 -0
  93. package/dist/src/core/types/core.js.map +1 -0
  94. package/dist/src/core/types/infrastructure.d.ts +38 -0
  95. package/dist/src/core/types/infrastructure.js +2 -0
  96. package/dist/src/core/types/infrastructure.js.map +1 -0
  97. package/dist/src/core/types/plugin.d.ts +12 -0
  98. package/dist/src/core/types/plugin.js +2 -0
  99. package/dist/src/core/types/plugin.js.map +1 -0
  100. package/dist/src/core/types/rules.d.ts +53 -0
  101. package/dist/src/core/types/rules.js +2 -0
  102. package/dist/src/core/types/rules.js.map +1 -0
  103. package/dist/src/core/types/summarizer.d.ts +12 -0
  104. package/dist/src/core/types/summarizer.js +2 -0
  105. package/dist/src/core/types/summarizer.js.map +1 -0
  106. package/dist/src/infrastructure/git-cache.d.ts +6 -0
  107. package/dist/src/infrastructure/git-cache.js +41 -0
  108. package/dist/src/infrastructure/git-cache.js.map +1 -0
  109. package/dist/src/infrastructure/git-history.d.ts +112 -0
  110. package/dist/src/infrastructure/git-history.js +340 -0
  111. package/dist/src/infrastructure/git-history.js.map +1 -0
  112. package/dist/src/infrastructure/logger.d.ts +20 -0
  113. package/dist/src/infrastructure/logger.js +57 -0
  114. package/dist/src/infrastructure/logger.js.map +1 -0
  115. package/dist/src/infrastructure/scanner.d.ts +31 -0
  116. package/dist/src/infrastructure/scanner.js +334 -0
  117. package/dist/src/infrastructure/scanner.js.map +1 -0
  118. package/dist/tests/analyzers-integration.test.d.ts +7 -0
  119. package/dist/tests/analyzers-integration.test.js +140 -0
  120. package/dist/tests/analyzers-integration.test.js.map +1 -0
  121. package/dist/tests/anti-patterns.test.d.ts +1 -0
  122. package/dist/tests/anti-patterns.test.js +81 -0
  123. package/dist/tests/anti-patterns.test.js.map +1 -0
  124. package/dist/tests/ast-parser.test.d.ts +1 -0
  125. package/dist/tests/ast-parser.test.js +94 -0
  126. package/dist/tests/ast-parser.test.js.map +1 -0
  127. package/dist/tests/fixtures/monorepo/packages/app/src/index.d.ts +1 -0
  128. package/dist/tests/fixtures/monorepo/packages/app/src/index.js +9 -0
  129. package/dist/tests/fixtures/monorepo/packages/app/src/index.js.map +1 -0
  130. package/dist/tests/fixtures/monorepo/packages/core/src/index.d.ts +2 -0
  131. package/dist/tests/fixtures/monorepo/packages/core/src/index.js +11 -0
  132. package/dist/tests/fixtures/monorepo/packages/core/src/index.js.map +1 -0
  133. package/dist/tests/forecast.test.d.ts +7 -0
  134. package/dist/tests/forecast.test.js +380 -0
  135. package/dist/tests/forecast.test.js.map +1 -0
  136. package/dist/tests/git-history.test.d.ts +7 -0
  137. package/dist/tests/git-history.test.js +193 -0
  138. package/dist/tests/git-history.test.js.map +1 -0
  139. package/dist/tests/i18n.test.d.ts +1 -0
  140. package/dist/tests/i18n.test.js +39 -0
  141. package/dist/tests/i18n.test.js.map +1 -0
  142. package/dist/tests/monorepo-scan.test.d.ts +11 -0
  143. package/dist/tests/monorepo-scan.test.js +143 -0
  144. package/dist/tests/monorepo-scan.test.js.map +1 -0
  145. package/dist/tests/plugin-loader.test.d.ts +1 -0
  146. package/dist/tests/plugin-loader.test.js +31 -0
  147. package/dist/tests/plugin-loader.test.js.map +1 -0
  148. package/dist/tests/rules-engine.test.d.ts +1 -0
  149. package/dist/tests/rules-engine.test.js +112 -0
  150. package/dist/tests/rules-engine.test.js.map +1 -0
  151. package/dist/tests/scanner.test.d.ts +1 -0
  152. package/dist/tests/scanner.test.js +44 -0
  153. package/dist/tests/scanner.test.js.map +1 -0
  154. package/dist/tests/scorer.test.d.ts +1 -0
  155. package/dist/tests/scorer.test.js +610 -0
  156. package/dist/tests/scorer.test.js.map +1 -0
  157. package/dist/tests/temporal-scorer.test.d.ts +7 -0
  158. package/dist/tests/temporal-scorer.test.js +239 -0
  159. package/dist/tests/temporal-scorer.test.js.map +1 -0
  160. package/package.json +29 -0
  161. package/src/core/analyzer.ts +499 -0
  162. package/src/core/analyzers/forecast.ts +497 -0
  163. package/src/core/analyzers/index.ts +33 -0
  164. package/src/core/analyzers/temporal-scorer.ts +227 -0
  165. package/src/core/anti-patterns.ts +324 -0
  166. package/src/core/ast/ast-parser.interface.ts +21 -0
  167. package/src/core/ast/path-resolver.ts +61 -0
  168. package/src/core/ast/tree-sitter-parser.ts +158 -0
  169. package/src/core/config.ts +125 -0
  170. package/src/core/diagram.ts +129 -0
  171. package/src/core/i18n.ts +64 -0
  172. package/src/core/locales/en.ts +340 -0
  173. package/src/core/locales/pt-BR.ts +341 -0
  174. package/src/core/locales/types.ts +95 -0
  175. package/src/core/plugin-loader.ts +80 -0
  176. package/src/core/project-summarizer.ts +42 -0
  177. package/src/core/refactor-engine.ts +112 -0
  178. package/src/core/rules/barrel-optimizer.ts +99 -0
  179. package/src/core/rules/dead-code-detector.ts +134 -0
  180. package/src/core/rules/hub-splitter.ts +135 -0
  181. package/src/core/rules/import-organizer.ts +100 -0
  182. package/src/core/rules/module-grouper.ts +133 -0
  183. package/src/core/rules-engine.ts +100 -0
  184. package/src/core/scorer.ts +181 -0
  185. package/src/core/summarizer/keyword-extractor.ts +53 -0
  186. package/src/core/summarizer/module-inferrer.ts +194 -0
  187. package/src/core/summarizer/package-reader.ts +34 -0
  188. package/src/core/summarizer/purpose-inferrer.ts +197 -0
  189. package/src/core/summarizer/readme-reader.ts +24 -0
  190. package/src/core/types/architect-rules.ts +29 -0
  191. package/src/core/types/core.ts +94 -0
  192. package/src/core/types/infrastructure.ts +41 -0
  193. package/src/core/types/plugin.ts +19 -0
  194. package/src/core/types/rules.ts +51 -0
  195. package/src/core/types/summarizer.ts +8 -0
  196. package/src/infrastructure/git-cache.ts +52 -0
  197. package/src/infrastructure/git-history.ts +496 -0
  198. package/src/infrastructure/logger.ts +68 -0
  199. package/src/infrastructure/scanner.ts +349 -0
  200. package/tests/analyzers-integration.test.ts +174 -0
  201. package/tests/anti-patterns.test.ts +95 -0
  202. package/tests/ast-parser.test.ts +102 -0
  203. package/tests/fixtures/monorepo/package.json +6 -0
  204. package/tests/fixtures/monorepo/packages/app/package.json +12 -0
  205. package/tests/fixtures/monorepo/packages/app/src/index.ts +6 -0
  206. package/tests/fixtures/monorepo/packages/core/package.json +7 -0
  207. package/tests/fixtures/monorepo/packages/core/src/index.ts +7 -0
  208. package/tests/forecast.test.ts +504 -0
  209. package/tests/git-history.test.ts +254 -0
  210. package/tests/i18n.test.ts +47 -0
  211. package/tests/monorepo-scan.test.ts +170 -0
  212. package/tests/plugin-loader.test.ts +40 -0
  213. package/tests/rules-engine.test.ts +131 -0
  214. package/tests/scanner.test.ts +54 -0
  215. package/tests/scorer.test.ts +675 -0
  216. package/tests/temporal-scorer.test.ts +306 -0
  217. package/tsconfig.json +9 -0
@@ -0,0 +1,51 @@
1
+ import { ArchitectureScore, AnalysisReport } from './core.js';
2
+
3
+ export interface RefactoringPlan {
4
+ timestamp: string;
5
+ projectPath: string;
6
+ currentScore: ArchitectureScore;
7
+ estimatedScoreAfter: { overall: number; breakdown: Record<string, number> };
8
+ steps: RefactorStep[];
9
+ totalOperations: number;
10
+ tier1Steps: number;
11
+ tier2Steps: number;
12
+ }
13
+
14
+ export interface RefactorStep {
15
+ id: number;
16
+ tier: 1 | 2;
17
+ rule: string;
18
+ priority: 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW';
19
+ title: string;
20
+ description: string;
21
+ rationale: string;
22
+ operations: FileOperation[];
23
+ scoreImpact: { metric: string; before: number; after: number }[];
24
+ codePreview?: string;
25
+ aiPrompt?: string;
26
+ }
27
+
28
+ export interface FileOperation {
29
+ type: 'CREATE' | 'MOVE' | 'MODIFY' | 'DELETE';
30
+ path: string;
31
+ newPath?: string;
32
+ content?: string;
33
+ diff?: string;
34
+ description: string;
35
+ }
36
+
37
+ export interface CodeSymbol {
38
+ name: string;
39
+ type: 'function' | 'class' | 'variable' | 'import' | 'export';
40
+ startLine: number;
41
+ endLine: number;
42
+ lines: number;
43
+ dependencies: string[];
44
+ usedBy: string[];
45
+ }
46
+
47
+ export interface RefactorRule {
48
+ name: string;
49
+ tier: 1 | 2;
50
+ analyze(report: AnalysisReport, projectPath: string): RefactorStep[];
51
+ }
@@ -0,0 +1,8 @@
1
+ export interface ProjectSummary {
2
+ description: string;
3
+ purpose: string;
4
+ modules: { name: string; files: number; description: string }[];
5
+ techStack: string[];
6
+ entryPoints: string[];
7
+ keywords: string[];
8
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Git History Cache — Serialize/deserialize GitHistoryReport
3
+ */
4
+
5
+ import * as fs from 'fs';
6
+ import * as path from 'path';
7
+ import type { GitHistoryReport } from './git-history.js';
8
+
9
+ export function saveToCache(
10
+ report: GitHistoryReport,
11
+ projectPath: string,
12
+ cacheDir = '.architect-cache',
13
+ ): void {
14
+ const dir = path.join(projectPath, cacheDir);
15
+ fs.mkdirSync(dir, { recursive: true });
16
+
17
+ const serializable = {
18
+ ...report,
19
+ modules: report.modules.map(m => ({
20
+ ...m,
21
+ files: m.files.map(f => ({
22
+ ...f,
23
+ authors: Array.from(f.authors),
24
+ lastModified: f.lastModified.toISOString(),
25
+ })),
26
+ })),
27
+ hotspots: report.hotspots.map(f => ({
28
+ ...f,
29
+ authors: Array.from(f.authors),
30
+ lastModified: f.lastModified.toISOString(),
31
+ })),
32
+ };
33
+
34
+ fs.writeFileSync(path.join(dir, 'git-history.json'), JSON.stringify(serializable, null, 2));
35
+ }
36
+
37
+ export function loadFromCache(
38
+ projectPath: string,
39
+ cacheDir = '.architect-cache',
40
+ maxAgeMs = 3600000,
41
+ ): GitHistoryReport | null {
42
+ const cachePath = path.join(projectPath, cacheDir, 'git-history.json');
43
+ if (!fs.existsSync(cachePath)) return null;
44
+
45
+ try {
46
+ const raw = JSON.parse(fs.readFileSync(cachePath, 'utf-8'));
47
+ if (Date.now() - new Date(raw.analyzedAt).getTime() > maxAgeMs) return null;
48
+ return raw;
49
+ } catch {
50
+ return null;
51
+ }
52
+ }
@@ -0,0 +1,496 @@
1
+ /**
2
+ * Git History Analyzer — Temporal analysis of codebase evolution
3
+ *
4
+ * Reads git log to build velocity vectors, churn rates, and hotspot maps.
5
+ * Enables Architect v4's predictive capabilities.
6
+ *
7
+ * Key metrics per module:
8
+ * - Commit frequency (commits/week rolling 4-week average)
9
+ * - Churn rate (lines added + deleted per commit)
10
+ * - Author diversity (bus factor proxy)
11
+ * - Change coupling (files that change together)
12
+ *
13
+ * @author Camilo Girardelli — Girardelli Tecnologia
14
+ * @license MIT
15
+ */
16
+
17
+ import { exec } from 'child_process';
18
+ import { promisify } from 'util';
19
+
20
+ import { logger } from './logger.js';
21
+
22
+ const execAsync = promisify(exec);
23
+
24
+ // ═══════════════════════════════════════════════════════════════
25
+ // TYPES
26
+ // ═══════════════════════════════════════════════════════════════
27
+
28
+ export interface GitCommit {
29
+ hash: string;
30
+ author: string;
31
+ date: Date;
32
+ message: string;
33
+ files: FileChange[];
34
+ }
35
+
36
+ export interface FileChange {
37
+ path: string;
38
+ additions: number;
39
+ deletions: number;
40
+ }
41
+
42
+ export interface FileHistory {
43
+ path: string;
44
+ commits: number;
45
+ totalAdditions: number;
46
+ totalDeletions: number;
47
+ churnRate: number; // (additions + deletions) / commits
48
+ authors: Set<string>;
49
+ busFactor: number; // unique authors count
50
+ lastModified: Date;
51
+ weeklyCommitRate: number; // commits/week (rolling 4-week)
52
+ isHotspot: boolean;
53
+ }
54
+
55
+ export interface ModuleHistory {
56
+ modulePath: string;
57
+ files: FileHistory[];
58
+ aggregateCommits: number;
59
+ aggregateChurn: number;
60
+ avgWeeklyRate: number;
61
+ topHotspots: FileHistory[];
62
+ velocityVector: VelocityVector;
63
+ busFactor: number;
64
+ }
65
+
66
+ export interface VelocityVector {
67
+ /** Commit rate trend: positive = accelerating, negative = decelerating */
68
+ commitAcceleration: number;
69
+ /** Churn trend: positive = increasing complexity, negative = stabilizing */
70
+ churnTrend: number;
71
+ /** Overall direction: "accelerating" | "stable" | "decelerating" */
72
+ direction: 'accelerating' | 'stable' | 'decelerating';
73
+ }
74
+
75
+ export interface ChangeCoupling {
76
+ fileA: string;
77
+ fileB: string;
78
+ cochangeCount: number;
79
+ confidence: number; // cochangeCount / max(commitsA, commitsB)
80
+ }
81
+
82
+ export interface GitHistoryReport {
83
+ projectPath: string;
84
+ analyzedAt: string;
85
+ periodWeeks: number;
86
+ totalCommits: number;
87
+ totalAuthors: number;
88
+ modules: ModuleHistory[];
89
+ hotspots: FileHistory[];
90
+ changeCouplings: ChangeCoupling[];
91
+ commitTimeline: WeeklySnapshot[];
92
+ }
93
+
94
+ export interface WeeklySnapshot {
95
+ weekStart: string; // ISO date
96
+ commits: number;
97
+ churn: number;
98
+ activeFiles: number;
99
+ }
100
+
101
+ export interface GitAnalyzerConfig {
102
+ /** How many weeks of history to analyze (default: 24) */
103
+ periodWeeks?: number;
104
+ /** Rolling window for averages (default: 4 weeks) */
105
+ rollingWindowWeeks?: number;
106
+ /** Churn threshold to flag as hotspot (default: 500 lines) */
107
+ hotspotChurnThreshold?: number;
108
+ /** Minimum co-change count for coupling (default: 3) */
109
+ couplingMinCochanges?: number;
110
+ /** Path to cache dir (default: .architect-cache/) */
111
+ cacheDir?: string;
112
+ }
113
+
114
+ const DEFAULT_CONFIG: Required<GitAnalyzerConfig> = {
115
+ periodWeeks: 24,
116
+ rollingWindowWeeks: 4,
117
+ hotspotChurnThreshold: 500,
118
+ couplingMinCochanges: 3,
119
+ cacheDir: '.architect-cache',
120
+ };
121
+
122
+ // ═══════════════════════════════════════════════════════════════
123
+ // GIT HISTORY ANALYZER
124
+ // ═══════════════════════════════════════════════════════════════
125
+
126
+ export class GitHistoryAnalyzer {
127
+ private config: Required<GitAnalyzerConfig>;
128
+
129
+ constructor(config?: GitAnalyzerConfig) {
130
+ this.config = { ...DEFAULT_CONFIG, ...config };
131
+ }
132
+
133
+ /**
134
+ * Analyze git history for the project at the given path.
135
+ * Returns a comprehensive GitHistoryReport.
136
+ */
137
+ async analyze(projectPath: string): Promise<GitHistoryReport> {
138
+ logger.debug('Starting GitHistory analysis', { projectPath, periodWeeks: this.config.periodWeeks });
139
+
140
+ await this.validateGitRepo(projectPath);
141
+
142
+ const sinceDate = this.getSinceDate();
143
+ const commits = await this.parseGitLog(projectPath, sinceDate);
144
+
145
+ logger.debug('Git history parsed', { commitCount: commits.length });
146
+ const fileHistories = this.buildFileHistories(commits);
147
+ const modules = this.groupByModule(fileHistories);
148
+ const hotspots = this.detectHotspots(fileHistories);
149
+ const changeCouplings = this.detectChangeCoupling(commits);
150
+ const timeline = this.buildTimeline(commits);
151
+
152
+ const allAuthors = new Set<string>();
153
+ commits.forEach(c => allAuthors.add(c.author));
154
+
155
+ return {
156
+ projectPath,
157
+ analyzedAt: new Date().toISOString(),
158
+ periodWeeks: this.config.periodWeeks,
159
+ totalCommits: commits.length,
160
+ totalAuthors: allAuthors.size,
161
+ modules,
162
+ hotspots,
163
+ changeCouplings,
164
+ commitTimeline: timeline,
165
+ };
166
+ }
167
+
168
+ // ── Git Log Parsing ──
169
+
170
+ private async parseGitLog(projectPath: string, since: string): Promise<GitCommit[]> {
171
+ const cmd = `git log --format='%H|%an|%aI|%s' --numstat --since="${since}" -- .`;
172
+
173
+ let output: string;
174
+ try {
175
+ const { stdout } = await execAsync(cmd, {
176
+ cwd: projectPath,
177
+ encoding: 'utf-8',
178
+ maxBuffer: 10 * 1024 * 1024, // 10MB buffer for large repos
179
+ });
180
+ output = stdout;
181
+ } catch (error) {
182
+ logger.warn('Failed to parse git log. History features will be bypassed.', { error, cmd });
183
+ return [];
184
+ }
185
+
186
+ return this.parseLogOutput(output);
187
+ }
188
+
189
+ private parseLogOutput(output: string): GitCommit[] {
190
+ const commits: GitCommit[] = [];
191
+ const lines = output.trim().split('\n');
192
+
193
+ let current: GitCommit | null = null;
194
+
195
+ for (const line of lines) {
196
+ if (!line.trim()) {
197
+ // Blank lines separate numstat blocks, but also appear between
198
+ // the commit header and its numstat output. Only finalize a commit
199
+ // on blank line if it already has file changes (otherwise it's just
200
+ // the gap between header and numstat).
201
+ if (current && current.files.length > 0) {
202
+ commits.push(current);
203
+ current = null;
204
+ }
205
+ continue;
206
+ }
207
+
208
+ // Commit header: hash|author|date|message
209
+ if (line.includes('|') && !line.startsWith('\t') && /^[0-9a-f]{7,}/.test(line)) {
210
+ if (current) commits.push(current);
211
+ const parts = line.split('|');
212
+ if (parts.length >= 4) {
213
+ current = {
214
+ hash: parts[0],
215
+ author: parts[1],
216
+ date: new Date(parts[2]),
217
+ message: parts.slice(3).join('|'),
218
+ files: [],
219
+ };
220
+ }
221
+ continue;
222
+ }
223
+
224
+ // Numstat line: additions\tdeletions\tfilepath
225
+ if (current && /^\d+\t\d+\t/.test(line)) {
226
+ const [add, del, filePath] = line.split('\t');
227
+ if (filePath && !filePath.includes('{') /* skip renames */) {
228
+ current.files.push({
229
+ path: filePath,
230
+ additions: parseInt(add, 10) || 0,
231
+ deletions: parseInt(del, 10) || 0,
232
+ });
233
+ }
234
+ }
235
+ }
236
+
237
+ if (current) commits.push(current);
238
+ return commits;
239
+ }
240
+
241
+ // ── File History Building ──
242
+
243
+ private buildFileHistories(commits: GitCommit[]): Map<string, FileHistory> {
244
+ const histories = new Map<string, FileHistory>();
245
+ const now = new Date();
246
+ const windowMs = this.config.rollingWindowWeeks * 7 * 24 * 60 * 60 * 1000;
247
+ const windowStart = new Date(now.getTime() - windowMs);
248
+
249
+ for (const commit of commits) {
250
+ for (const file of commit.files) {
251
+ if (!histories.has(file.path)) {
252
+ histories.set(file.path, {
253
+ path: file.path,
254
+ commits: 0,
255
+ totalAdditions: 0,
256
+ totalDeletions: 0,
257
+ churnRate: 0,
258
+ authors: new Set(),
259
+ busFactor: 0,
260
+ lastModified: commit.date,
261
+ weeklyCommitRate: 0,
262
+ isHotspot: false,
263
+ });
264
+ }
265
+
266
+ const h = histories.get(file.path)!;
267
+ h.commits++;
268
+ h.totalAdditions += file.additions;
269
+ h.totalDeletions += file.deletions;
270
+ h.authors.add(commit.author);
271
+
272
+ if (commit.date > h.lastModified) {
273
+ h.lastModified = commit.date;
274
+ }
275
+ }
276
+ }
277
+
278
+ // Calculate derived metrics
279
+ const recentCommits = commits.filter(c => c.date >= windowStart);
280
+ const weekCount = Math.max(this.config.rollingWindowWeeks, 1);
281
+
282
+ for (const [filePath, h] of histories) {
283
+ h.churnRate = h.commits > 0
284
+ ? (h.totalAdditions + h.totalDeletions) / h.commits
285
+ : 0;
286
+ h.busFactor = h.authors.size;
287
+
288
+ // Weekly commit rate from rolling window
289
+ const recentFileCommits = recentCommits.filter(
290
+ c => c.files.some(f => f.path === filePath)
291
+ ).length;
292
+ h.weeklyCommitRate = recentFileCommits / weekCount;
293
+
294
+ // Hotspot: high churn + high frequency
295
+ h.isHotspot = (h.totalAdditions + h.totalDeletions) >= this.config.hotspotChurnThreshold
296
+ && h.weeklyCommitRate >= 1;
297
+ }
298
+
299
+ return histories;
300
+ }
301
+
302
+ // ── Module Grouping ──
303
+
304
+ private groupByModule(fileHistories: Map<string, FileHistory>): ModuleHistory[] {
305
+ const moduleMap = new Map<string, FileHistory[]>();
306
+
307
+ for (const [filePath, history] of fileHistories) {
308
+ const modulePath = this.getModulePath(filePath);
309
+ if (!moduleMap.has(modulePath)) {
310
+ moduleMap.set(modulePath, []);
311
+ }
312
+ moduleMap.get(modulePath)!.push(history);
313
+ }
314
+
315
+ return Array.from(moduleMap.entries()).map(([modulePath, files]) => {
316
+ const aggregateCommits = files.reduce((s, f) => s + f.commits, 0);
317
+ const aggregateChurn = files.reduce((s, f) => s + f.totalAdditions + f.totalDeletions, 0);
318
+ const avgWeeklyRate = files.reduce((s, f) => s + f.weeklyCommitRate, 0) / Math.max(files.length, 1);
319
+ const allAuthors = new Set<string>();
320
+ files.forEach(f => f.authors.forEach(a => allAuthors.add(a)));
321
+
322
+ const topHotspots = files
323
+ .filter(f => f.isHotspot)
324
+ .sort((a, b) => (b.totalAdditions + b.totalDeletions) - (a.totalAdditions + a.totalDeletions))
325
+ .slice(0, 5);
326
+
327
+ return {
328
+ modulePath,
329
+ files,
330
+ aggregateCommits,
331
+ aggregateChurn,
332
+ avgWeeklyRate,
333
+ topHotspots,
334
+ velocityVector: this.calculateVelocity(files),
335
+ busFactor: allAuthors.size,
336
+ };
337
+ }).sort((a, b) => b.aggregateChurn - a.aggregateChurn);
338
+ }
339
+
340
+ // ── Velocity Calculation ──
341
+
342
+ private calculateVelocity(files: FileHistory[]): VelocityVector {
343
+ if (files.length === 0) {
344
+ return { commitAcceleration: 0, churnTrend: 0, direction: 'stable' };
345
+ }
346
+
347
+ // Sort files by last modified (most recent first)
348
+ const sorted = [...files].sort((a, b) => b.lastModified.getTime() - a.lastModified.getTime());
349
+
350
+ // Split into halves: recent vs older
351
+ const mid = Math.floor(sorted.length / 2) || 1;
352
+ const recentHalf = sorted.slice(0, mid);
353
+ const olderHalf = sorted.slice(mid);
354
+
355
+ const recentAvgRate = recentHalf.reduce((s, f) => s + f.weeklyCommitRate, 0) / recentHalf.length;
356
+ const olderAvgRate = olderHalf.length > 0
357
+ ? olderHalf.reduce((s, f) => s + f.weeklyCommitRate, 0) / olderHalf.length
358
+ : recentAvgRate;
359
+
360
+ const recentAvgChurn = recentHalf.reduce((s, f) => s + f.churnRate, 0) / recentHalf.length;
361
+ const olderAvgChurn = olderHalf.length > 0
362
+ ? olderHalf.reduce((s, f) => s + f.churnRate, 0) / olderHalf.length
363
+ : recentAvgChurn;
364
+
365
+ const commitAcceleration = olderAvgRate > 0
366
+ ? ((recentAvgRate - olderAvgRate) / olderAvgRate) * 100
367
+ : 0;
368
+
369
+ const churnTrend = olderAvgChurn > 0
370
+ ? ((recentAvgChurn - olderAvgChurn) / olderAvgChurn) * 100
371
+ : 0;
372
+
373
+ let direction: VelocityVector['direction'] = 'stable';
374
+ if (commitAcceleration > 15) direction = 'accelerating';
375
+ else if (commitAcceleration < -15) direction = 'decelerating';
376
+
377
+ return {
378
+ commitAcceleration: Math.round(commitAcceleration * 10) / 10,
379
+ churnTrend: Math.round(churnTrend * 10) / 10,
380
+ direction,
381
+ };
382
+ }
383
+
384
+ // ── Hotspot Detection ──
385
+
386
+ private detectHotspots(fileHistories: Map<string, FileHistory>): FileHistory[] {
387
+ return Array.from(fileHistories.values())
388
+ .filter(f => f.isHotspot)
389
+ .sort((a, b) => (b.totalAdditions + b.totalDeletions) - (a.totalAdditions + a.totalDeletions))
390
+ .slice(0, 20);
391
+ }
392
+
393
+ // ── Change Coupling Detection ──
394
+
395
+ private detectChangeCoupling(commits: GitCommit[]): ChangeCoupling[] {
396
+ const cochangeMap = new Map<string, number>();
397
+ const fileCommitCount = new Map<string, number>();
398
+
399
+ for (const commit of commits) {
400
+ const files = commit.files.map(f => f.path).sort();
401
+
402
+ for (const f of files) {
403
+ fileCommitCount.set(f, (fileCommitCount.get(f) || 0) + 1);
404
+ }
405
+
406
+ // Pairwise co-change counting (limit to 10 files per commit to avoid explosion)
407
+ const limited = files.slice(0, 10);
408
+ for (let i = 0; i < limited.length; i++) {
409
+ for (let j = i + 1; j < limited.length; j++) {
410
+ const key = `${limited[i]}|||${limited[j]}`;
411
+ cochangeMap.set(key, (cochangeMap.get(key) || 0) + 1);
412
+ }
413
+ }
414
+ }
415
+
416
+ const couplings: ChangeCoupling[] = [];
417
+ for (const [key, count] of cochangeMap) {
418
+ if (count < this.config.couplingMinCochanges) continue;
419
+
420
+ const [fileA, fileB] = key.split('|||');
421
+ const maxCommits = Math.max(
422
+ fileCommitCount.get(fileA) || 1,
423
+ fileCommitCount.get(fileB) || 1,
424
+ );
425
+
426
+ couplings.push({
427
+ fileA,
428
+ fileB,
429
+ cochangeCount: count,
430
+ confidence: Math.round((count / maxCommits) * 100) / 100,
431
+ });
432
+ }
433
+
434
+ return couplings
435
+ .sort((a, b) => b.confidence - a.confidence)
436
+ .slice(0, 50);
437
+ }
438
+
439
+ // ── Timeline Building ──
440
+
441
+ private buildTimeline(commits: GitCommit[]): WeeklySnapshot[] {
442
+ if (commits.length === 0) return [];
443
+
444
+ const snapshots = new Map<string, WeeklySnapshot>();
445
+ const toMonday = (d: Date): string => {
446
+ d.setHours(0, 0, 0, 0);
447
+ d.setDate(d.getDate() - ((d.getDay() + 6) % 7));
448
+ return d.toISOString().split('T')[0];
449
+ };
450
+ const now = new Date();
451
+
452
+ for (let w = 0; w < this.config.periodWeeks; w++) {
453
+ const key = toMonday(new Date(now.getTime() - w * 604800000));
454
+ if (!snapshots.has(key)) {
455
+ snapshots.set(key, { weekStart: key, commits: 0, churn: 0, activeFiles: 0 });
456
+ }
457
+ }
458
+
459
+ for (const commit of commits) {
460
+ const key = toMonday(new Date(commit.date));
461
+ const snap = snapshots.get(key);
462
+ if (snap) {
463
+ snap.commits++;
464
+ snap.churn += commit.files.reduce((s, f) => s + f.additions + f.deletions, 0);
465
+ snap.activeFiles += commit.files.length;
466
+ }
467
+ }
468
+
469
+ return Array.from(snapshots.values()).sort((a, b) => a.weekStart.localeCompare(b.weekStart));
470
+ }
471
+
472
+ // ── Utilities ──
473
+
474
+ private async validateGitRepo(projectPath: string): Promise<void> {
475
+ try {
476
+ await execAsync('git rev-parse --is-inside-work-tree', {
477
+ cwd: projectPath,
478
+ encoding: 'utf-8',
479
+ });
480
+ } catch {
481
+ throw new Error(`Not a git repository: ${projectPath}`);
482
+ }
483
+ }
484
+
485
+ private getSinceDate(): string {
486
+ const d = new Date();
487
+ d.setDate(d.getDate() - (this.config.periodWeeks * 7));
488
+ return d.toISOString().split('T')[0];
489
+ }
490
+
491
+ private getModulePath(filePath: string): string {
492
+ const parts = filePath.split('/');
493
+ // Use first directory as module, or "root" if no directory
494
+ return parts.length > 1 ? parts[0] : 'root';
495
+ }
496
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Structured Logger for Architect
3
+ * Provides leveled logging that respects CLI verbosity and environment.
4
+ */
5
+
6
+ export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
7
+
8
+ class Logger {
9
+ private isVerbose = false;
10
+ private isJson = false;
11
+
12
+ setup(options: { verbose?: boolean; json?: boolean }): void {
13
+ if (options.verbose !== undefined) this.isVerbose = options.verbose;
14
+ if (options.json !== undefined) this.isJson = options.json;
15
+ }
16
+
17
+ debug(message: string, meta?: Record<string, any>): void {
18
+ if (!this.isVerbose) return;
19
+ this.log('debug', message, meta);
20
+ }
21
+
22
+ info(message: string, meta?: Record<string, any>): void {
23
+ if (!this.isVerbose) return; // Info behaves like verbose debug by default unless needed
24
+ this.log('info', message, meta);
25
+ }
26
+
27
+ warn(message: string, meta?: Record<string, any>): void {
28
+ this.log('warn', message, meta);
29
+ }
30
+
31
+ error(message: string, error?: Error | unknown, meta?: Record<string, any>): void {
32
+ const errorMeta = error instanceof Error
33
+ ? { errorMessage: error.message, stack: this.isVerbose ? error.stack : undefined, ...meta }
34
+ : { rawError: String(error), ...meta };
35
+
36
+ this.log('error', message, errorMeta);
37
+ }
38
+
39
+ private log(level: LogLevel, message: string, meta?: Record<string, any>): void {
40
+ if (this.isJson) {
41
+ // In JSON mode, output minimal structured error logic inside stderr so it doesn't break stdout pipes.
42
+ process.stderr.write(JSON.stringify({ level, message, timestamp: new Date().toISOString(), ...meta }) + '\n');
43
+ return;
44
+ }
45
+
46
+ // Human-readable CLI formatting
47
+ const timestamp = new Date().toISOString().split('T')[1].slice(0, 12);
48
+ const color = {
49
+ debug: '\x1b[38;5;240m', // gray
50
+ info: '\x1b[38;5;33m', // blue
51
+ warn: '\x1b[38;5;208m', // orange
52
+ error: '\x1b[38;5;196m', // red
53
+ }[level];
54
+
55
+ const reset = '\x1b[0m';
56
+ const dim = '\x1b[2m';
57
+
58
+ let msg = `[${timestamp}] ${color}● ${level.toUpperCase().padEnd(5)}${reset} ${message}`;
59
+
60
+ if (meta && Object.keys(meta).length > 0) {
61
+ msg += ` ${dim}${JSON.stringify(meta)}${reset}`;
62
+ }
63
+
64
+ process.stderr.write(msg + '\n');
65
+ }
66
+ }
67
+
68
+ export const logger = new Logger();