@eduardbar/drift 1.3.0 → 1.4.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 (168) hide show
  1. package/.gga +50 -0
  2. package/.github/actions/drift-review/README.md +60 -0
  3. package/.github/actions/drift-review/action.yml +131 -0
  4. package/.github/actions/drift-scan/README.md +28 -32
  5. package/.github/actions/drift-scan/action.yml +78 -14
  6. package/.github/workflows/review-pr.yml +34 -41
  7. package/AGENTS.md +75 -251
  8. package/CHANGELOG.md +28 -0
  9. package/README.md +148 -41
  10. package/dist/benchmark.d.ts +1 -1
  11. package/dist/benchmark.js +71 -52
  12. package/dist/cli.js +243 -8
  13. package/dist/config.js +16 -2
  14. package/dist/diff.js +42 -50
  15. package/dist/doctor.d.ts +5 -0
  16. package/dist/doctor.js +133 -0
  17. package/dist/format.d.ts +17 -0
  18. package/dist/format.js +45 -0
  19. package/dist/guard-types.d.ts +57 -0
  20. package/dist/guard-types.js +2 -0
  21. package/dist/guard.d.ts +14 -0
  22. package/dist/guard.js +239 -0
  23. package/dist/index.d.ts +10 -3
  24. package/dist/index.js +4 -1
  25. package/dist/init.d.ts +15 -0
  26. package/dist/init.js +273 -0
  27. package/dist/map-cycles.d.ts +2 -0
  28. package/dist/map-cycles.js +34 -0
  29. package/dist/map-svg.d.ts +19 -0
  30. package/dist/map-svg.js +97 -0
  31. package/dist/map.js +78 -138
  32. package/dist/metrics.js +70 -55
  33. package/dist/output-metadata.d.ts +13 -0
  34. package/dist/output-metadata.js +17 -0
  35. package/dist/plugins-capabilities.d.ts +4 -0
  36. package/dist/plugins-capabilities.js +21 -0
  37. package/dist/plugins-messages.d.ts +10 -0
  38. package/dist/plugins-messages.js +16 -0
  39. package/dist/plugins-rules.d.ts +9 -0
  40. package/dist/plugins-rules.js +137 -0
  41. package/dist/plugins.d.ts +1 -1
  42. package/dist/plugins.js +45 -142
  43. package/dist/reporter-constants.d.ts +16 -0
  44. package/dist/reporter-constants.js +39 -0
  45. package/dist/reporter.d.ts +3 -3
  46. package/dist/reporter.js +35 -55
  47. package/dist/review.d.ts +2 -1
  48. package/dist/review.js +2 -1
  49. package/dist/rules/phase3-configurable.js +23 -15
  50. package/dist/saas/constants.d.ts +15 -0
  51. package/dist/saas/constants.js +48 -0
  52. package/dist/saas/dashboard.d.ts +8 -0
  53. package/dist/saas/dashboard.js +132 -0
  54. package/dist/saas/errors.d.ts +19 -0
  55. package/dist/saas/errors.js +37 -0
  56. package/dist/saas/helpers.d.ts +21 -0
  57. package/dist/saas/helpers.js +110 -0
  58. package/dist/saas/ingest.d.ts +3 -0
  59. package/dist/saas/ingest.js +249 -0
  60. package/dist/saas/organization.d.ts +5 -0
  61. package/dist/saas/organization.js +82 -0
  62. package/dist/saas/plan-change.d.ts +10 -0
  63. package/dist/saas/plan-change.js +15 -0
  64. package/dist/saas/store.d.ts +21 -0
  65. package/dist/saas/store.js +159 -0
  66. package/dist/saas/types.d.ts +191 -0
  67. package/dist/saas/types.js +2 -0
  68. package/dist/saas.d.ts +8 -218
  69. package/dist/saas.js +7 -761
  70. package/dist/sarif.d.ts +74 -0
  71. package/dist/sarif.js +122 -0
  72. package/dist/trust-advanced.d.ts +14 -0
  73. package/dist/trust-advanced.js +65 -0
  74. package/dist/trust-kpi-fs.d.ts +3 -0
  75. package/dist/trust-kpi-fs.js +141 -0
  76. package/dist/trust-kpi-parse.d.ts +7 -0
  77. package/dist/trust-kpi-parse.js +186 -0
  78. package/dist/trust-kpi-types.d.ts +16 -0
  79. package/dist/trust-kpi-types.js +2 -0
  80. package/dist/trust-kpi.d.ts +1 -3
  81. package/dist/trust-kpi.js +6 -266
  82. package/dist/trust-policy.d.ts +32 -0
  83. package/dist/trust-policy.js +160 -0
  84. package/dist/trust-render.d.ts +9 -0
  85. package/dist/trust-render.js +54 -0
  86. package/dist/trust-scoring.d.ts +9 -0
  87. package/dist/trust-scoring.js +208 -0
  88. package/dist/trust.d.ts +4 -32
  89. package/dist/trust.js +29 -432
  90. package/dist/types/app.d.ts +30 -0
  91. package/dist/types/app.js +2 -0
  92. package/dist/types/config.d.ts +25 -0
  93. package/dist/types/config.js +2 -0
  94. package/dist/types/core.d.ts +100 -0
  95. package/dist/types/core.js +2 -0
  96. package/dist/types/diff.d.ts +55 -0
  97. package/dist/types/diff.js +2 -0
  98. package/dist/types/plugin.d.ts +41 -0
  99. package/dist/types/plugin.js +2 -0
  100. package/dist/types/trust.d.ts +120 -0
  101. package/dist/types/trust.js +2 -0
  102. package/dist/types.d.ts +8 -365
  103. package/docs/release-notes-draft.md +40 -0
  104. package/docs/rules-catalog.md +49 -0
  105. package/docs/trust-core-release-checklist.md +37 -5
  106. package/package.json +3 -2
  107. package/packages/vscode-drift/src/code-actions.ts +1 -1
  108. package/schemas/drift-ai-output.v1.json +162 -0
  109. package/schemas/drift-report.v1.json +151 -0
  110. package/schemas/drift-trust.v1.json +131 -0
  111. package/scripts/smoke-repo.mjs +394 -0
  112. package/src/benchmark.ts +75 -53
  113. package/src/cli.ts +285 -13
  114. package/src/config.ts +19 -2
  115. package/src/diff.ts +57 -48
  116. package/src/doctor.ts +173 -0
  117. package/src/format.ts +81 -0
  118. package/src/guard-types.ts +64 -0
  119. package/src/guard.ts +324 -0
  120. package/src/index.ts +35 -0
  121. package/src/init.ts +298 -0
  122. package/src/map-cycles.ts +38 -0
  123. package/src/map-svg.ts +124 -0
  124. package/src/map.ts +111 -142
  125. package/src/metrics.ts +78 -59
  126. package/src/output-metadata.ts +30 -0
  127. package/src/plugins-capabilities.ts +36 -0
  128. package/src/plugins-messages.ts +35 -0
  129. package/src/plugins-rules.ts +296 -0
  130. package/src/plugins.ts +76 -283
  131. package/src/reporter-constants.ts +46 -0
  132. package/src/reporter.ts +64 -65
  133. package/src/review.ts +4 -2
  134. package/src/rules/phase3-configurable.ts +39 -26
  135. package/src/saas/constants.ts +56 -0
  136. package/src/saas/dashboard.ts +172 -0
  137. package/src/saas/errors.ts +45 -0
  138. package/src/saas/helpers.ts +140 -0
  139. package/src/saas/ingest.ts +278 -0
  140. package/src/saas/organization.ts +99 -0
  141. package/src/saas/plan-change.ts +19 -0
  142. package/src/saas/store.ts +172 -0
  143. package/src/saas/types.ts +216 -0
  144. package/src/saas.ts +49 -1031
  145. package/src/sarif.ts +232 -0
  146. package/src/trust-advanced.ts +99 -0
  147. package/src/trust-kpi-fs.ts +169 -0
  148. package/src/trust-kpi-parse.ts +219 -0
  149. package/src/trust-kpi-types.ts +19 -0
  150. package/src/trust-kpi.ts +8 -316
  151. package/src/trust-policy.ts +246 -0
  152. package/src/trust-render.ts +61 -0
  153. package/src/trust-scoring.ts +231 -0
  154. package/src/trust.ts +62 -576
  155. package/src/types/app.ts +30 -0
  156. package/src/types/config.ts +27 -0
  157. package/src/types/core.ts +105 -0
  158. package/src/types/diff.ts +61 -0
  159. package/src/types/plugin.ts +46 -0
  160. package/src/types/trust.ts +134 -0
  161. package/src/types.ts +78 -409
  162. package/tests/cli-sarif.test.ts +92 -0
  163. package/tests/format.test.ts +157 -0
  164. package/tests/new-features.test.ts +10 -2
  165. package/tests/phase1-init-doctor-guard.test.ts +199 -0
  166. package/tests/sarif.test.ts +160 -0
  167. package/tests/trust-kpi.test.ts +31 -4
  168. package/tests/trust.test.ts +18 -0
@@ -0,0 +1,231 @@
1
+ import { RULE_WEIGHTS } from './analyzer.js'
2
+ import type { DriftDiff, DriftReport, MergeRiskLevel, TrustDiffContext, TrustFixPriority, TrustReason } from './types.js'
3
+
4
+ const ARCHITECTURE_RULES = new Set([
5
+ 'circular-dependency',
6
+ 'layer-violation',
7
+ 'cross-boundary-import',
8
+ 'controller-no-db',
9
+ 'service-no-http',
10
+ ])
11
+
12
+ const RULE_SUGGESTIONS: Record<string, string> = {
13
+ 'circular-dependency': 'Break cycles first to reduce hidden merge blast radius.',
14
+ 'layer-violation': 'Fix layer violations to keep architecture boundaries enforceable.',
15
+ 'high-complexity': 'Split branch-heavy functions before adding more logic.',
16
+ 'deep-nesting': 'Flatten control flow with early returns.',
17
+ 'large-file': 'Split monolithic files by responsibility before merge.',
18
+ 'large-function': 'Extract smaller functions to reduce review complexity.',
19
+ 'catch-swallow': 'Handle or rethrow swallowed errors to avoid silent failures.',
20
+ 'debug-leftover': 'Remove debug leftovers from production paths.',
21
+ 'semantic-duplication': 'Consolidate duplicated logic to prevent divergent fixes.',
22
+ 'dead-file': 'Delete or wire dead files to avoid stale merge artifacts.',
23
+ }
24
+
25
+ const SYSTEMIC_RULES = new Set([
26
+ 'circular-dependency',
27
+ 'layer-violation',
28
+ 'cross-boundary-import',
29
+ 'unused-export',
30
+ 'unused-dependency',
31
+ 'dead-file',
32
+ 'semantic-duplication',
33
+ 'controller-no-db',
34
+ 'service-no-http',
35
+ ])
36
+
37
+ const TRUST_LOW_MIN = 80
38
+ const TRUST_MEDIUM_MIN = 60
39
+ const TRUST_HIGH_MIN = 40
40
+
41
+ const DRIFT_PRESSURE_FACTOR = 0.55
42
+ const ERROR_IMPACT_CAP = 22
43
+ const ERROR_IMPACT_FACTOR = 4
44
+ const ARCHITECTURE_IMPACT_CAP = 24
45
+ const ARCHITECTURE_IMPACT_FACTOR = 6
46
+ const HOTSPOT_IMPACT_CAP = 25
47
+ const HOTSPOT_IMPACT_FACTOR = 0.25
48
+ const WORST_FILE_IMPACT_CAP = 15
49
+ const WORST_FILE_IMPACT_FACTOR = 0.15
50
+ const TOP_REASONS_LIMIT = 4
51
+
52
+ const EFFORT_LOW_MAX_WEIGHT = 6
53
+ const EFFORT_MEDIUM_MAX_WEIGHT = 12
54
+
55
+ const SCORE_REGRESSION_PENALTY_FACTOR = 2
56
+ const NEW_ISSUE_PENALTY_FACTOR = 3
57
+ const CHURN_FILE_THRESHOLD = 15
58
+ const CHURN_PENALTY = 4
59
+ const PENALTY_CAP = 30
60
+ const RESOLVED_ISSUE_BONUS_FACTOR = 2
61
+ const BONUS_CAP = 20
62
+
63
+ const SEVERITY_ERROR_SCORE = 4
64
+ const SEVERITY_WARNING_SCORE = 2
65
+ const SEVERITY_INFO_SCORE = 1
66
+ const PRIORITY_OCCURRENCE_FACTOR = 2
67
+ const SYSTEMIC_CONFIDENCE_BOOST = 2
68
+ const CONFIDENCE_HIGH_MIN = 12
69
+ const CONFIDENCE_MEDIUM_MIN = 7
70
+
71
+ const DEFAULT_WEIGHT_CONFIG = { severity: 'warning' as const, weight: EFFORT_LOW_MAX_WEIGHT }
72
+ const SEVERITY_BOOST_ERROR = 25
73
+ const SEVERITY_BOOST_WARNING = 12
74
+ const SEVERITY_BOOST_INFO = 4
75
+ const SYSTEMIC_PRIORITY_BOOST = 25
76
+ const TRUST_GAIN_MAX = 30
77
+ const TRUST_GAIN_MIN = 3
78
+ const TRUST_GAIN_DIVISOR = 4
79
+ const FIX_PRIORITIES_LIMIT = 5
80
+
81
+ export const TOP_REASONS_SLICE = TOP_REASONS_LIMIT
82
+
83
+ export function clamp(value: number, min: number, max: number): number {
84
+ return Math.max(min, Math.min(max, value))
85
+ }
86
+
87
+ export function toMergeRisk(trustScore: number): MergeRiskLevel {
88
+ if (trustScore >= TRUST_LOW_MIN) return 'LOW'
89
+ if (trustScore >= TRUST_MEDIUM_MIN) return 'MEDIUM'
90
+ if (trustScore >= TRUST_HIGH_MIN) return 'HIGH'
91
+ return 'CRITICAL'
92
+ }
93
+
94
+ export function computeReasons(report: DriftReport): TrustReason[] {
95
+ const architectureIssues = Object.entries(report.summary.byRule)
96
+ .filter(([rule]) => ARCHITECTURE_RULES.has(rule))
97
+ .reduce((sum, [, count]) => sum + count, 0)
98
+
99
+ const worstHotspot = report.maintenanceRisk.hotspots[0]
100
+ const reasons: TrustReason[] = [
101
+ {
102
+ label: 'Drift score pressure',
103
+ detail: `Repository drift score is ${report.totalScore}/100.`,
104
+ impact: Math.round(report.totalScore * DRIFT_PRESSURE_FACTOR),
105
+ },
106
+ {
107
+ label: 'Error-level issues',
108
+ detail: `${report.summary.errors} error issue(s) increase merge volatility.`,
109
+ impact: Math.min(ERROR_IMPACT_CAP, report.summary.errors * ERROR_IMPACT_FACTOR),
110
+ },
111
+ {
112
+ label: 'Architecture signals',
113
+ detail: `${architectureIssues} architecture-related issue(s) detected.`,
114
+ impact: Math.min(ARCHITECTURE_IMPACT_CAP, architectureIssues * ARCHITECTURE_IMPACT_FACTOR),
115
+ },
116
+ {
117
+ label: 'Maintenance hotspots',
118
+ detail: `Maintenance risk is ${report.maintenanceRisk.level.toUpperCase()} (${report.maintenanceRisk.score}/100).`,
119
+ impact: Math.min(HOTSPOT_IMPACT_CAP, Math.round(report.maintenanceRisk.score * HOTSPOT_IMPACT_FACTOR)),
120
+ },
121
+ {
122
+ label: 'Highest-risk file',
123
+ detail: worstHotspot
124
+ ? `${worstHotspot.file} has hotspot risk ${worstHotspot.risk}/100.`
125
+ : 'No hotspot concentration detected.',
126
+ impact: worstHotspot ? Math.min(WORST_FILE_IMPACT_CAP, Math.round(worstHotspot.risk * WORST_FILE_IMPACT_FACTOR)) : 0,
127
+ },
128
+ ]
129
+
130
+ return reasons
131
+ .filter((reason) => reason.impact > 0)
132
+ .sort((a, b) => b.impact - a.impact)
133
+ .slice(0, TOP_REASONS_LIMIT)
134
+ }
135
+
136
+ function effortFromWeight(weight: number): 'low' | 'medium' | 'high' {
137
+ if (weight <= EFFORT_LOW_MAX_WEIGHT) return 'low'
138
+ if (weight <= EFFORT_MEDIUM_MAX_WEIGHT) return 'medium'
139
+ return 'high'
140
+ }
141
+
142
+ export function computeDiffContext(diff: DriftDiff): TrustDiffContext {
143
+ const scoreRegressionPenalty = Math.max(0, diff.totalDelta) * SCORE_REGRESSION_PENALTY_FACTOR
144
+ const newIssuePenalty = diff.newIssuesCount * NEW_ISSUE_PENALTY_FACTOR
145
+ const churnPenalty = diff.files.length >= CHURN_FILE_THRESHOLD ? CHURN_PENALTY : 0
146
+ const penalty = clamp(scoreRegressionPenalty + newIssuePenalty + churnPenalty, 0, PENALTY_CAP)
147
+
148
+ const scoreImprovementBonus = Math.max(0, -diff.totalDelta) * SCORE_REGRESSION_PENALTY_FACTOR
149
+ const resolvedIssueBonus = diff.resolvedIssuesCount * RESOLVED_ISSUE_BONUS_FACTOR
150
+ const bonus = clamp(scoreImprovementBonus + resolvedIssueBonus, 0, BONUS_CAP)
151
+
152
+ const netImpact = penalty - bonus
153
+ const status = netImpact > 0 ? 'regressed' : netImpact < 0 ? 'improved' : 'neutral'
154
+
155
+ return {
156
+ baseRef: diff.baseRef,
157
+ status,
158
+ scoreDelta: diff.totalDelta,
159
+ newIssues: diff.newIssuesCount,
160
+ resolvedIssues: diff.resolvedIssuesCount,
161
+ filesChanged: diff.files.length,
162
+ penalty,
163
+ bonus,
164
+ netImpact,
165
+ }
166
+ }
167
+
168
+ function confidenceFromPrioritySignals(
169
+ occurrences: number,
170
+ severity: 'error' | 'warning' | 'info',
171
+ systemic: boolean,
172
+ ): 'low' | 'medium' | 'high' {
173
+ const severityScore = severity === 'error' ? SEVERITY_ERROR_SCORE : severity === 'warning' ? SEVERITY_WARNING_SCORE : SEVERITY_INFO_SCORE
174
+ const systemicScore = systemic ? SYSTEMIC_CONFIDENCE_BOOST : 0
175
+ const score = occurrences * PRIORITY_OCCURRENCE_FACTOR + severityScore + systemicScore
176
+
177
+ if (score >= CONFIDENCE_HIGH_MIN) return 'high'
178
+ if (score >= CONFIDENCE_MEDIUM_MIN) return 'medium'
179
+ return 'low'
180
+ }
181
+
182
+ export function computeFixPriorities(report: DriftReport, advancedMode = false): TrustFixPriority[] {
183
+ const ordered = Object.entries(report.summary.byRule)
184
+ .map(([rule, occurrences]) => {
185
+ const weightConfig = RULE_WEIGHTS[rule] ?? DEFAULT_WEIGHT_CONFIG
186
+ const severityBoost = weightConfig.severity === 'error' ? SEVERITY_BOOST_ERROR : weightConfig.severity === 'warning' ? SEVERITY_BOOST_WARNING : SEVERITY_BOOST_INFO
187
+ const systemic = SYSTEMIC_RULES.has(rule)
188
+ const systemicBoost = advancedMode && systemic ? SYSTEMIC_PRIORITY_BOOST : 0
189
+ const priorityScore = occurrences * weightConfig.weight + severityBoost + systemicBoost
190
+ const confidence = confidenceFromPrioritySignals(occurrences, weightConfig.severity, systemic)
191
+ const explanation = advancedMode
192
+ ? systemic
193
+ ? 'System-level rule that propagates risk across multiple teams and modules.'
194
+ : 'Local rule with contained impact; treat as team-level cleanup after systemic fixes.'
195
+ : undefined
196
+
197
+ return {
198
+ rule,
199
+ severity: weightConfig.severity,
200
+ occurrences,
201
+ systemic,
202
+ priorityScore,
203
+ estimatedTrustGain: Math.min(TRUST_GAIN_MAX, Math.max(TRUST_GAIN_MIN, Math.round(priorityScore / TRUST_GAIN_DIVISOR))),
204
+ effort: effortFromWeight(weightConfig.weight),
205
+ suggestion: RULE_SUGGESTIONS[rule] ?? 'Address this rule in the highest-scored files first.',
206
+ confidence,
207
+ explanation,
208
+ }
209
+ })
210
+ .sort((a, b) => b.priorityScore - a.priorityScore)
211
+ .slice(0, FIX_PRIORITIES_LIMIT)
212
+
213
+ return ordered.map((item, index) => ({
214
+ rank: index + 1,
215
+ rule: item.rule,
216
+ severity: item.severity,
217
+ occurrences: item.occurrences,
218
+ estimated_trust_gain: item.estimatedTrustGain,
219
+ effort: item.effort,
220
+ suggestion: item.suggestion,
221
+ ...(advancedMode ? { confidence: item.confidence, explanation: item.explanation, systemic: item.systemic } : {}),
222
+ }))
223
+ }
224
+
225
+ export function buildDiffRegressionReason(diffContext: TrustDiffContext): TrustReason {
226
+ return {
227
+ label: 'Diff regression signals',
228
+ detail: `Against ${diffContext.baseRef}: score delta ${diffContext.scoreDelta >= 0 ? '+' : ''}${diffContext.scoreDelta}, +${diffContext.newIssues} new issue(s), -${diffContext.resolvedIssues} resolved.`,
229
+ impact: diffContext.netImpact,
230
+ }
231
+ }