@girardelli/architect 2.2.0 → 5.0.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 (212) hide show
  1. package/README.md +105 -116
  2. package/architect-run.sh +431 -0
  3. package/assets/banner-v3.html +561 -0
  4. package/dist/agent-generator/context-enricher.d.ts +58 -0
  5. package/dist/agent-generator/context-enricher.d.ts.map +1 -0
  6. package/dist/agent-generator/context-enricher.js +613 -0
  7. package/dist/agent-generator/context-enricher.js.map +1 -0
  8. package/dist/agent-generator/domain-inferrer.d.ts +52 -0
  9. package/dist/agent-generator/domain-inferrer.d.ts.map +1 -0
  10. package/dist/agent-generator/domain-inferrer.js +585 -0
  11. package/dist/agent-generator/domain-inferrer.js.map +1 -0
  12. package/dist/agent-generator/framework-detector.d.ts +40 -0
  13. package/dist/agent-generator/framework-detector.d.ts.map +1 -0
  14. package/dist/agent-generator/framework-detector.js +611 -0
  15. package/dist/agent-generator/framework-detector.js.map +1 -0
  16. package/dist/agent-generator/index.d.ts +47 -0
  17. package/dist/agent-generator/index.d.ts.map +1 -0
  18. package/dist/agent-generator/index.js +545 -0
  19. package/dist/agent-generator/index.js.map +1 -0
  20. package/dist/agent-generator/stack-detector.d.ts +14 -0
  21. package/dist/agent-generator/stack-detector.d.ts.map +1 -0
  22. package/dist/agent-generator/stack-detector.js +124 -0
  23. package/dist/agent-generator/stack-detector.js.map +1 -0
  24. package/dist/agent-generator/templates/core/agents.d.ts +17 -0
  25. package/dist/agent-generator/templates/core/agents.d.ts.map +1 -0
  26. package/dist/agent-generator/templates/core/agents.js +1256 -0
  27. package/dist/agent-generator/templates/core/agents.js.map +1 -0
  28. package/dist/agent-generator/templates/core/architecture-rules.d.ts +7 -0
  29. package/dist/agent-generator/templates/core/architecture-rules.d.ts.map +1 -0
  30. package/dist/agent-generator/templates/core/architecture-rules.js +274 -0
  31. package/dist/agent-generator/templates/core/architecture-rules.js.map +1 -0
  32. package/dist/agent-generator/templates/core/general-rules.d.ts +8 -0
  33. package/dist/agent-generator/templates/core/general-rules.d.ts.map +1 -0
  34. package/dist/agent-generator/templates/core/general-rules.js +301 -0
  35. package/dist/agent-generator/templates/core/general-rules.js.map +1 -0
  36. package/dist/agent-generator/templates/core/hooks-generator.d.ts +21 -0
  37. package/dist/agent-generator/templates/core/hooks-generator.d.ts.map +1 -0
  38. package/dist/agent-generator/templates/core/hooks-generator.js +233 -0
  39. package/dist/agent-generator/templates/core/hooks-generator.js.map +1 -0
  40. package/dist/agent-generator/templates/core/index-md.d.ts +7 -0
  41. package/dist/agent-generator/templates/core/index-md.d.ts.map +1 -0
  42. package/dist/agent-generator/templates/core/index-md.js +246 -0
  43. package/dist/agent-generator/templates/core/index-md.js.map +1 -0
  44. package/dist/agent-generator/templates/core/orchestrator.d.ts +8 -0
  45. package/dist/agent-generator/templates/core/orchestrator.d.ts.map +1 -0
  46. package/dist/agent-generator/templates/core/orchestrator.js +422 -0
  47. package/dist/agent-generator/templates/core/orchestrator.js.map +1 -0
  48. package/dist/agent-generator/templates/core/preflight.d.ts +8 -0
  49. package/dist/agent-generator/templates/core/preflight.d.ts.map +1 -0
  50. package/dist/agent-generator/templates/core/preflight.js +213 -0
  51. package/dist/agent-generator/templates/core/preflight.js.map +1 -0
  52. package/dist/agent-generator/templates/core/quality-gates.d.ts +11 -0
  53. package/dist/agent-generator/templates/core/quality-gates.d.ts.map +1 -0
  54. package/dist/agent-generator/templates/core/quality-gates.js +254 -0
  55. package/dist/agent-generator/templates/core/quality-gates.js.map +1 -0
  56. package/dist/agent-generator/templates/core/security-rules.d.ts +7 -0
  57. package/dist/agent-generator/templates/core/security-rules.d.ts.map +1 -0
  58. package/dist/agent-generator/templates/core/security-rules.js +528 -0
  59. package/dist/agent-generator/templates/core/security-rules.js.map +1 -0
  60. package/dist/agent-generator/templates/core/skills-generator.d.ts +19 -0
  61. package/dist/agent-generator/templates/core/skills-generator.d.ts.map +1 -0
  62. package/dist/agent-generator/templates/core/skills-generator.js +546 -0
  63. package/dist/agent-generator/templates/core/skills-generator.js.map +1 -0
  64. package/dist/agent-generator/templates/core/workflow-fix-bug.d.ts +7 -0
  65. package/dist/agent-generator/templates/core/workflow-fix-bug.d.ts.map +1 -0
  66. package/dist/agent-generator/templates/core/workflow-fix-bug.js +237 -0
  67. package/dist/agent-generator/templates/core/workflow-fix-bug.js.map +1 -0
  68. package/dist/agent-generator/templates/core/workflow-new-feature.d.ts +8 -0
  69. package/dist/agent-generator/templates/core/workflow-new-feature.d.ts.map +1 -0
  70. package/dist/agent-generator/templates/core/workflow-new-feature.js +321 -0
  71. package/dist/agent-generator/templates/core/workflow-new-feature.js.map +1 -0
  72. package/dist/agent-generator/templates/core/workflow-review.d.ts +7 -0
  73. package/dist/agent-generator/templates/core/workflow-review.d.ts.map +1 -0
  74. package/dist/agent-generator/templates/core/workflow-review.js +104 -0
  75. package/dist/agent-generator/templates/core/workflow-review.js.map +1 -0
  76. package/dist/agent-generator/templates/domain/index.d.ts +22 -0
  77. package/dist/agent-generator/templates/domain/index.d.ts.map +1 -0
  78. package/dist/agent-generator/templates/domain/index.js +1176 -0
  79. package/dist/agent-generator/templates/domain/index.js.map +1 -0
  80. package/dist/agent-generator/templates/stack/index.d.ts +8 -0
  81. package/dist/agent-generator/templates/stack/index.d.ts.map +1 -0
  82. package/dist/agent-generator/templates/stack/index.js +695 -0
  83. package/dist/agent-generator/templates/stack/index.js.map +1 -0
  84. package/dist/agent-generator/templates/template-helpers.d.ts +75 -0
  85. package/dist/agent-generator/templates/template-helpers.d.ts.map +1 -0
  86. package/dist/agent-generator/templates/template-helpers.js +726 -0
  87. package/dist/agent-generator/templates/template-helpers.js.map +1 -0
  88. package/dist/agent-generator/types.d.ts +196 -0
  89. package/dist/agent-generator/types.d.ts.map +1 -0
  90. package/dist/agent-generator/types.js +27 -0
  91. package/dist/agent-generator/types.js.map +1 -0
  92. package/dist/analyzer.d.ts +5 -0
  93. package/dist/analyzer.d.ts.map +1 -1
  94. package/dist/analyzer.js +46 -5
  95. package/dist/analyzer.js.map +1 -1
  96. package/dist/analyzers/forecast.d.ts +85 -0
  97. package/dist/analyzers/forecast.d.ts.map +1 -0
  98. package/dist/analyzers/forecast.js +337 -0
  99. package/dist/analyzers/forecast.js.map +1 -0
  100. package/dist/analyzers/git-cache.d.ts +7 -0
  101. package/dist/analyzers/git-cache.d.ts.map +1 -0
  102. package/dist/analyzers/git-cache.js +41 -0
  103. package/dist/analyzers/git-cache.js.map +1 -0
  104. package/dist/analyzers/git-history.d.ts +113 -0
  105. package/dist/analyzers/git-history.d.ts.map +1 -0
  106. package/dist/analyzers/git-history.js +333 -0
  107. package/dist/analyzers/git-history.js.map +1 -0
  108. package/dist/analyzers/index.d.ts +10 -0
  109. package/dist/analyzers/index.d.ts.map +1 -0
  110. package/dist/analyzers/index.js +7 -0
  111. package/dist/analyzers/index.js.map +1 -0
  112. package/dist/analyzers/temporal-scorer.d.ts +72 -0
  113. package/dist/analyzers/temporal-scorer.d.ts.map +1 -0
  114. package/dist/analyzers/temporal-scorer.js +140 -0
  115. package/dist/analyzers/temporal-scorer.js.map +1 -0
  116. package/dist/anti-patterns.d.ts +7 -0
  117. package/dist/anti-patterns.d.ts.map +1 -1
  118. package/dist/anti-patterns.js +25 -6
  119. package/dist/anti-patterns.js.map +1 -1
  120. package/dist/cli.d.ts +2 -3
  121. package/dist/cli.d.ts.map +1 -1
  122. package/dist/cli.js +275 -113
  123. package/dist/cli.js.map +1 -1
  124. package/dist/config.d.ts +6 -0
  125. package/dist/config.d.ts.map +1 -1
  126. package/dist/config.js +48 -11
  127. package/dist/config.js.map +1 -1
  128. package/dist/html-reporter.d.ts +3 -1
  129. package/dist/html-reporter.d.ts.map +1 -1
  130. package/dist/html-reporter.js +248 -12
  131. package/dist/html-reporter.js.map +1 -1
  132. package/dist/index.d.ts +16 -3
  133. package/dist/index.d.ts.map +1 -1
  134. package/dist/index.js +63 -4
  135. package/dist/index.js.map +1 -1
  136. package/dist/project-summarizer.d.ts +38 -0
  137. package/dist/project-summarizer.d.ts.map +1 -0
  138. package/dist/project-summarizer.js +463 -0
  139. package/dist/project-summarizer.js.map +1 -0
  140. package/dist/refactor-reporter.js +1 -1
  141. package/dist/scanner.d.ts +8 -2
  142. package/dist/scanner.d.ts.map +1 -1
  143. package/dist/scanner.js +153 -113
  144. package/dist/scanner.js.map +1 -1
  145. package/dist/scorer.d.ts.map +1 -1
  146. package/dist/scorer.js +24 -11
  147. package/dist/scorer.js.map +1 -1
  148. package/dist/types.d.ts +29 -0
  149. package/dist/types.d.ts.map +1 -1
  150. package/package.json +12 -3
  151. package/src/agent-generator/context-enricher.ts +672 -0
  152. package/src/agent-generator/domain-inferrer.ts +635 -0
  153. package/src/agent-generator/framework-detector.ts +669 -0
  154. package/src/agent-generator/index.ts +634 -0
  155. package/src/agent-generator/stack-detector.ts +115 -0
  156. package/src/agent-generator/templates/core/agents.ts +1296 -0
  157. package/src/agent-generator/templates/core/architecture-rules.ts +287 -0
  158. package/src/agent-generator/templates/core/general-rules.ts +306 -0
  159. package/src/agent-generator/templates/core/hooks-generator.ts +242 -0
  160. package/src/agent-generator/templates/core/index-md.ts +260 -0
  161. package/src/agent-generator/templates/core/orchestrator.ts +459 -0
  162. package/src/agent-generator/templates/core/preflight.ts +215 -0
  163. package/src/agent-generator/templates/core/quality-gates.ts +256 -0
  164. package/src/agent-generator/templates/core/security-rules.ts +543 -0
  165. package/src/agent-generator/templates/core/skills-generator.ts +585 -0
  166. package/src/agent-generator/templates/core/workflow-fix-bug.ts +239 -0
  167. package/src/agent-generator/templates/core/workflow-new-feature.ts +323 -0
  168. package/src/agent-generator/templates/core/workflow-review.ts +106 -0
  169. package/src/agent-generator/templates/domain/index.ts +1201 -0
  170. package/src/agent-generator/templates/stack/index.ts +705 -0
  171. package/src/agent-generator/templates/template-helpers.ts +776 -0
  172. package/src/agent-generator/types.ts +232 -0
  173. package/src/analyzer.ts +51 -5
  174. package/src/analyzers/forecast.ts +496 -0
  175. package/src/analyzers/git-cache.ts +52 -0
  176. package/src/analyzers/git-history.ts +488 -0
  177. package/src/analyzers/index.ts +33 -0
  178. package/src/analyzers/temporal-scorer.ts +227 -0
  179. package/src/anti-patterns.ts +29 -6
  180. package/src/cli.ts +316 -117
  181. package/src/config.ts +52 -11
  182. package/src/html-reporter.ts +263 -13
  183. package/src/index.ts +93 -10
  184. package/src/project-summarizer.ts +521 -0
  185. package/src/refactor-reporter.ts +1 -1
  186. package/src/scanner.ts +136 -90
  187. package/src/scorer.ts +26 -11
  188. package/src/types.ts +27 -0
  189. package/tests/agent-generator.test.ts +427 -0
  190. package/tests/analyzers-integration.test.ts +174 -0
  191. package/tests/architect-adapter-enrichment.test.ts +9 -0
  192. package/tests/context-enricher.test.ts +971 -0
  193. package/tests/fixtures/monorepo/package.json +6 -0
  194. package/tests/fixtures/monorepo/packages/app/package.json +12 -0
  195. package/tests/fixtures/monorepo/packages/app/src/index.ts +6 -0
  196. package/tests/fixtures/monorepo/packages/core/package.json +7 -0
  197. package/tests/fixtures/monorepo/packages/core/src/index.ts +7 -0
  198. package/tests/forecast.test.ts +509 -0
  199. package/tests/framework-detector.test.ts +1172 -0
  200. package/tests/git-history.test.ts +254 -0
  201. package/tests/monorepo-scan.test.ts +170 -0
  202. package/tests/scanner.test.ts +7 -8
  203. package/tests/scorer.test.ts +594 -0
  204. package/tests/stack-detector.test.ts +241 -0
  205. package/tests/template-generation.test.ts +706 -0
  206. package/tests/template-helpers.test.ts +1152 -0
  207. package/tests/temporal-scorer.test.ts +307 -0
  208. package/dist/agent-generator.d.ts +0 -106
  209. package/dist/agent-generator.d.ts.map +0 -1
  210. package/dist/agent-generator.js +0 -1398
  211. package/dist/agent-generator.js.map +0 -1
  212. package/src/agent-generator.ts +0 -1526
@@ -0,0 +1,488 @@
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 { execSync } from 'child_process';
18
+ import * as path from 'path';
19
+ import * as fs from 'fs';
20
+
21
+ // ═══════════════════════════════════════════════════════════════
22
+ // TYPES
23
+ // ═══════════════════════════════════════════════════════════════
24
+
25
+ export interface GitCommit {
26
+ hash: string;
27
+ author: string;
28
+ date: Date;
29
+ message: string;
30
+ files: FileChange[];
31
+ }
32
+
33
+ export interface FileChange {
34
+ path: string;
35
+ additions: number;
36
+ deletions: number;
37
+ }
38
+
39
+ export interface FileHistory {
40
+ path: string;
41
+ commits: number;
42
+ totalAdditions: number;
43
+ totalDeletions: number;
44
+ churnRate: number; // (additions + deletions) / commits
45
+ authors: Set<string>;
46
+ busFactor: number; // unique authors count
47
+ lastModified: Date;
48
+ weeklyCommitRate: number; // commits/week (rolling 4-week)
49
+ isHotspot: boolean;
50
+ }
51
+
52
+ export interface ModuleHistory {
53
+ modulePath: string;
54
+ files: FileHistory[];
55
+ aggregateCommits: number;
56
+ aggregateChurn: number;
57
+ avgWeeklyRate: number;
58
+ topHotspots: FileHistory[];
59
+ velocityVector: VelocityVector;
60
+ busFactor: number;
61
+ }
62
+
63
+ export interface VelocityVector {
64
+ /** Commit rate trend: positive = accelerating, negative = decelerating */
65
+ commitAcceleration: number;
66
+ /** Churn trend: positive = increasing complexity, negative = stabilizing */
67
+ churnTrend: number;
68
+ /** Overall direction: "accelerating" | "stable" | "decelerating" */
69
+ direction: 'accelerating' | 'stable' | 'decelerating';
70
+ }
71
+
72
+ export interface ChangeCoupling {
73
+ fileA: string;
74
+ fileB: string;
75
+ cochangeCount: number;
76
+ confidence: number; // cochangeCount / max(commitsA, commitsB)
77
+ }
78
+
79
+ export interface GitHistoryReport {
80
+ projectPath: string;
81
+ analyzedAt: string;
82
+ periodWeeks: number;
83
+ totalCommits: number;
84
+ totalAuthors: number;
85
+ modules: ModuleHistory[];
86
+ hotspots: FileHistory[];
87
+ changeCouplings: ChangeCoupling[];
88
+ commitTimeline: WeeklySnapshot[];
89
+ }
90
+
91
+ export interface WeeklySnapshot {
92
+ weekStart: string; // ISO date
93
+ commits: number;
94
+ churn: number;
95
+ activeFiles: number;
96
+ }
97
+
98
+ export interface GitAnalyzerConfig {
99
+ /** How many weeks of history to analyze (default: 24) */
100
+ periodWeeks?: number;
101
+ /** Rolling window for averages (default: 4 weeks) */
102
+ rollingWindowWeeks?: number;
103
+ /** Churn threshold to flag as hotspot (default: 500 lines) */
104
+ hotspotChurnThreshold?: number;
105
+ /** Minimum co-change count for coupling (default: 3) */
106
+ couplingMinCochanges?: number;
107
+ /** Path to cache dir (default: .architect-cache/) */
108
+ cacheDir?: string;
109
+ }
110
+
111
+ const DEFAULT_CONFIG: Required<GitAnalyzerConfig> = {
112
+ periodWeeks: 24,
113
+ rollingWindowWeeks: 4,
114
+ hotspotChurnThreshold: 500,
115
+ couplingMinCochanges: 3,
116
+ cacheDir: '.architect-cache',
117
+ };
118
+
119
+ // ═══════════════════════════════════════════════════════════════
120
+ // GIT HISTORY ANALYZER
121
+ // ═══════════════════════════════════════════════════════════════
122
+
123
+ export class GitHistoryAnalyzer {
124
+ private config: Required<GitAnalyzerConfig>;
125
+
126
+ constructor(config?: GitAnalyzerConfig) {
127
+ this.config = { ...DEFAULT_CONFIG, ...config };
128
+ }
129
+
130
+ /**
131
+ * Analyze git history for the project at the given path.
132
+ * Returns a comprehensive GitHistoryReport.
133
+ */
134
+ analyze(projectPath: string): GitHistoryReport {
135
+ this.validateGitRepo(projectPath);
136
+
137
+ const sinceDate = this.getSinceDate();
138
+ const commits = this.parseGitLog(projectPath, sinceDate);
139
+ const fileHistories = this.buildFileHistories(commits);
140
+ const modules = this.groupByModule(fileHistories);
141
+ const hotspots = this.detectHotspots(fileHistories);
142
+ const changeCouplings = this.detectChangeCoupling(commits);
143
+ const timeline = this.buildTimeline(commits);
144
+
145
+ const allAuthors = new Set<string>();
146
+ commits.forEach(c => allAuthors.add(c.author));
147
+
148
+ return {
149
+ projectPath,
150
+ analyzedAt: new Date().toISOString(),
151
+ periodWeeks: this.config.periodWeeks,
152
+ totalCommits: commits.length,
153
+ totalAuthors: allAuthors.size,
154
+ modules,
155
+ hotspots,
156
+ changeCouplings,
157
+ commitTimeline: timeline,
158
+ };
159
+ }
160
+
161
+ // ── Git Log Parsing ──
162
+
163
+ private parseGitLog(projectPath: string, since: string): GitCommit[] {
164
+ const cmd = `git log --format='%H|%an|%aI|%s' --numstat --since="${since}" -- .`;
165
+
166
+ let output: string;
167
+ try {
168
+ output = execSync(cmd, {
169
+ cwd: projectPath,
170
+ encoding: 'utf-8',
171
+ maxBuffer: 10 * 1024 * 1024, // 10MB buffer for large repos
172
+ });
173
+ } catch {
174
+ return [];
175
+ }
176
+
177
+ return this.parseLogOutput(output);
178
+ }
179
+
180
+ private parseLogOutput(output: string): GitCommit[] {
181
+ const commits: GitCommit[] = [];
182
+ const lines = output.trim().split('\n');
183
+
184
+ let current: GitCommit | null = null;
185
+
186
+ for (const line of lines) {
187
+ if (!line.trim()) {
188
+ // Blank lines separate numstat blocks, but also appear between
189
+ // the commit header and its numstat output. Only finalize a commit
190
+ // on blank line if it already has file changes (otherwise it's just
191
+ // the gap between header and numstat).
192
+ if (current && current.files.length > 0) {
193
+ commits.push(current);
194
+ current = null;
195
+ }
196
+ continue;
197
+ }
198
+
199
+ // Commit header: hash|author|date|message
200
+ if (line.includes('|') && !line.startsWith('\t') && /^[0-9a-f]{7,}/.test(line)) {
201
+ if (current) commits.push(current);
202
+ const parts = line.split('|');
203
+ if (parts.length >= 4) {
204
+ current = {
205
+ hash: parts[0],
206
+ author: parts[1],
207
+ date: new Date(parts[2]),
208
+ message: parts.slice(3).join('|'),
209
+ files: [],
210
+ };
211
+ }
212
+ continue;
213
+ }
214
+
215
+ // Numstat line: additions\tdeletions\tfilepath
216
+ if (current && /^\d+\t\d+\t/.test(line)) {
217
+ const [add, del, filePath] = line.split('\t');
218
+ if (filePath && !filePath.includes('{') /* skip renames */) {
219
+ current.files.push({
220
+ path: filePath,
221
+ additions: parseInt(add, 10) || 0,
222
+ deletions: parseInt(del, 10) || 0,
223
+ });
224
+ }
225
+ }
226
+ }
227
+
228
+ if (current) commits.push(current);
229
+ return commits;
230
+ }
231
+
232
+ // ── File History Building ──
233
+
234
+ private buildFileHistories(commits: GitCommit[]): Map<string, FileHistory> {
235
+ const histories = new Map<string, FileHistory>();
236
+ const now = new Date();
237
+ const windowMs = this.config.rollingWindowWeeks * 7 * 24 * 60 * 60 * 1000;
238
+ const windowStart = new Date(now.getTime() - windowMs);
239
+
240
+ for (const commit of commits) {
241
+ for (const file of commit.files) {
242
+ if (!histories.has(file.path)) {
243
+ histories.set(file.path, {
244
+ path: file.path,
245
+ commits: 0,
246
+ totalAdditions: 0,
247
+ totalDeletions: 0,
248
+ churnRate: 0,
249
+ authors: new Set(),
250
+ busFactor: 0,
251
+ lastModified: commit.date,
252
+ weeklyCommitRate: 0,
253
+ isHotspot: false,
254
+ });
255
+ }
256
+
257
+ const h = histories.get(file.path)!;
258
+ h.commits++;
259
+ h.totalAdditions += file.additions;
260
+ h.totalDeletions += file.deletions;
261
+ h.authors.add(commit.author);
262
+
263
+ if (commit.date > h.lastModified) {
264
+ h.lastModified = commit.date;
265
+ }
266
+ }
267
+ }
268
+
269
+ // Calculate derived metrics
270
+ const recentCommits = commits.filter(c => c.date >= windowStart);
271
+ const weekCount = Math.max(this.config.rollingWindowWeeks, 1);
272
+
273
+ for (const [filePath, h] of histories) {
274
+ h.churnRate = h.commits > 0
275
+ ? (h.totalAdditions + h.totalDeletions) / h.commits
276
+ : 0;
277
+ h.busFactor = h.authors.size;
278
+
279
+ // Weekly commit rate from rolling window
280
+ const recentFileCommits = recentCommits.filter(
281
+ c => c.files.some(f => f.path === filePath)
282
+ ).length;
283
+ h.weeklyCommitRate = recentFileCommits / weekCount;
284
+
285
+ // Hotspot: high churn + high frequency
286
+ h.isHotspot = (h.totalAdditions + h.totalDeletions) >= this.config.hotspotChurnThreshold
287
+ && h.weeklyCommitRate >= 1;
288
+ }
289
+
290
+ return histories;
291
+ }
292
+
293
+ // ── Module Grouping ──
294
+
295
+ private groupByModule(fileHistories: Map<string, FileHistory>): ModuleHistory[] {
296
+ const moduleMap = new Map<string, FileHistory[]>();
297
+
298
+ for (const [filePath, history] of fileHistories) {
299
+ const modulePath = this.getModulePath(filePath);
300
+ if (!moduleMap.has(modulePath)) {
301
+ moduleMap.set(modulePath, []);
302
+ }
303
+ moduleMap.get(modulePath)!.push(history);
304
+ }
305
+
306
+ return Array.from(moduleMap.entries()).map(([modulePath, files]) => {
307
+ const aggregateCommits = files.reduce((s, f) => s + f.commits, 0);
308
+ const aggregateChurn = files.reduce((s, f) => s + f.totalAdditions + f.totalDeletions, 0);
309
+ const avgWeeklyRate = files.reduce((s, f) => s + f.weeklyCommitRate, 0) / Math.max(files.length, 1);
310
+ const allAuthors = new Set<string>();
311
+ files.forEach(f => f.authors.forEach(a => allAuthors.add(a)));
312
+
313
+ const topHotspots = files
314
+ .filter(f => f.isHotspot)
315
+ .sort((a, b) => (b.totalAdditions + b.totalDeletions) - (a.totalAdditions + a.totalDeletions))
316
+ .slice(0, 5);
317
+
318
+ return {
319
+ modulePath,
320
+ files,
321
+ aggregateCommits,
322
+ aggregateChurn,
323
+ avgWeeklyRate,
324
+ topHotspots,
325
+ velocityVector: this.calculateVelocity(files),
326
+ busFactor: allAuthors.size,
327
+ };
328
+ }).sort((a, b) => b.aggregateChurn - a.aggregateChurn);
329
+ }
330
+
331
+ // ── Velocity Calculation ──
332
+
333
+ private calculateVelocity(files: FileHistory[]): VelocityVector {
334
+ if (files.length === 0) {
335
+ return { commitAcceleration: 0, churnTrend: 0, direction: 'stable' };
336
+ }
337
+
338
+ // Sort files by last modified (most recent first)
339
+ const sorted = [...files].sort((a, b) => b.lastModified.getTime() - a.lastModified.getTime());
340
+
341
+ // Split into halves: recent vs older
342
+ const mid = Math.floor(sorted.length / 2) || 1;
343
+ const recentHalf = sorted.slice(0, mid);
344
+ const olderHalf = sorted.slice(mid);
345
+
346
+ const recentAvgRate = recentHalf.reduce((s, f) => s + f.weeklyCommitRate, 0) / recentHalf.length;
347
+ const olderAvgRate = olderHalf.length > 0
348
+ ? olderHalf.reduce((s, f) => s + f.weeklyCommitRate, 0) / olderHalf.length
349
+ : recentAvgRate;
350
+
351
+ const recentAvgChurn = recentHalf.reduce((s, f) => s + f.churnRate, 0) / recentHalf.length;
352
+ const olderAvgChurn = olderHalf.length > 0
353
+ ? olderHalf.reduce((s, f) => s + f.churnRate, 0) / olderHalf.length
354
+ : recentAvgChurn;
355
+
356
+ const commitAcceleration = olderAvgRate > 0
357
+ ? ((recentAvgRate - olderAvgRate) / olderAvgRate) * 100
358
+ : 0;
359
+
360
+ const churnTrend = olderAvgChurn > 0
361
+ ? ((recentAvgChurn - olderAvgChurn) / olderAvgChurn) * 100
362
+ : 0;
363
+
364
+ let direction: VelocityVector['direction'] = 'stable';
365
+ if (commitAcceleration > 15) direction = 'accelerating';
366
+ else if (commitAcceleration < -15) direction = 'decelerating';
367
+
368
+ return {
369
+ commitAcceleration: Math.round(commitAcceleration * 10) / 10,
370
+ churnTrend: Math.round(churnTrend * 10) / 10,
371
+ direction,
372
+ };
373
+ }
374
+
375
+ // ── Hotspot Detection ──
376
+
377
+ private detectHotspots(fileHistories: Map<string, FileHistory>): FileHistory[] {
378
+ return Array.from(fileHistories.values())
379
+ .filter(f => f.isHotspot)
380
+ .sort((a, b) => (b.totalAdditions + b.totalDeletions) - (a.totalAdditions + a.totalDeletions))
381
+ .slice(0, 20);
382
+ }
383
+
384
+ // ── Change Coupling Detection ──
385
+
386
+ private detectChangeCoupling(commits: GitCommit[]): ChangeCoupling[] {
387
+ const cochangeMap = new Map<string, number>();
388
+ const fileCommitCount = new Map<string, number>();
389
+
390
+ for (const commit of commits) {
391
+ const files = commit.files.map(f => f.path).sort();
392
+
393
+ for (const f of files) {
394
+ fileCommitCount.set(f, (fileCommitCount.get(f) || 0) + 1);
395
+ }
396
+
397
+ // Pairwise co-change counting (limit to 10 files per commit to avoid explosion)
398
+ const limited = files.slice(0, 10);
399
+ for (let i = 0; i < limited.length; i++) {
400
+ for (let j = i + 1; j < limited.length; j++) {
401
+ const key = `${limited[i]}|||${limited[j]}`;
402
+ cochangeMap.set(key, (cochangeMap.get(key) || 0) + 1);
403
+ }
404
+ }
405
+ }
406
+
407
+ const couplings: ChangeCoupling[] = [];
408
+ for (const [key, count] of cochangeMap) {
409
+ if (count < this.config.couplingMinCochanges) continue;
410
+
411
+ const [fileA, fileB] = key.split('|||');
412
+ const maxCommits = Math.max(
413
+ fileCommitCount.get(fileA) || 1,
414
+ fileCommitCount.get(fileB) || 1,
415
+ );
416
+
417
+ couplings.push({
418
+ fileA,
419
+ fileB,
420
+ cochangeCount: count,
421
+ confidence: Math.round((count / maxCommits) * 100) / 100,
422
+ });
423
+ }
424
+
425
+ return couplings
426
+ .sort((a, b) => b.confidence - a.confidence)
427
+ .slice(0, 50);
428
+ }
429
+
430
+ // ── Timeline Building ──
431
+
432
+ private buildTimeline(commits: GitCommit[]): WeeklySnapshot[] {
433
+ if (commits.length === 0) return [];
434
+
435
+ const snapshots = new Map<string, WeeklySnapshot>();
436
+ const toMonday = (d: Date): string => {
437
+ d.setHours(0, 0, 0, 0);
438
+ d.setDate(d.getDate() - ((d.getDay() + 6) % 7));
439
+ return d.toISOString().split('T')[0];
440
+ };
441
+ const now = new Date();
442
+
443
+ for (let w = 0; w < this.config.periodWeeks; w++) {
444
+ const key = toMonday(new Date(now.getTime() - w * 604800000));
445
+ if (!snapshots.has(key)) {
446
+ snapshots.set(key, { weekStart: key, commits: 0, churn: 0, activeFiles: 0 });
447
+ }
448
+ }
449
+
450
+ for (const commit of commits) {
451
+ const key = toMonday(new Date(commit.date));
452
+ const snap = snapshots.get(key);
453
+ if (snap) {
454
+ snap.commits++;
455
+ snap.churn += commit.files.reduce((s, f) => s + f.additions + f.deletions, 0);
456
+ snap.activeFiles += commit.files.length;
457
+ }
458
+ }
459
+
460
+ return Array.from(snapshots.values()).sort((a, b) => a.weekStart.localeCompare(b.weekStart));
461
+ }
462
+
463
+ // ── Utilities ──
464
+
465
+ private validateGitRepo(projectPath: string): void {
466
+ try {
467
+ execSync('git rev-parse --is-inside-work-tree', {
468
+ cwd: projectPath,
469
+ encoding: 'utf-8',
470
+ stdio: 'pipe',
471
+ });
472
+ } catch {
473
+ throw new Error(`Not a git repository: ${projectPath}`);
474
+ }
475
+ }
476
+
477
+ private getSinceDate(): string {
478
+ const d = new Date();
479
+ d.setDate(d.getDate() - (this.config.periodWeeks * 7));
480
+ return d.toISOString().split('T')[0];
481
+ }
482
+
483
+ private getModulePath(filePath: string): string {
484
+ const parts = filePath.split('/');
485
+ // Use first directory as module, or "root" if no directory
486
+ return parts.length > 1 ? parts[0] : 'root';
487
+ }
488
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Architect v4.0 Analyzers — Temporal & Predictive
3
+ */
4
+
5
+ export { GitHistoryAnalyzer } from './git-history.js';
6
+ export type {
7
+ GitCommit,
8
+ FileChange,
9
+ FileHistory,
10
+ ModuleHistory,
11
+ VelocityVector,
12
+ ChangeCoupling,
13
+ GitHistoryReport,
14
+ WeeklySnapshot,
15
+ GitAnalyzerConfig,
16
+ } from './git-history.js';
17
+
18
+ export { TemporalScorer } from './temporal-scorer.js';
19
+ export type {
20
+ Trend,
21
+ TemporalScore,
22
+ TemporalReport,
23
+ TemporalScorerConfig,
24
+ } from './temporal-scorer.js';
25
+
26
+ export { ForecastEngine } from './forecast.js';
27
+ export type {
28
+ PreAntiPatternType,
29
+ PreAntiPattern,
30
+ ModuleForecast,
31
+ WeatherForecast,
32
+ ForecastConfig,
33
+ } from './forecast.js';