-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_grouping.go
More file actions
552 lines (518 loc) · 15.2 KB
/
commit_grouping.go
File metadata and controls
552 lines (518 loc) · 15.2 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
package repomap
import (
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
)
// classifyFiles annotates each fileChange with derived metadata: language, type,
// IsConfig/IsArtifact/IsTest/IsDep flags. Call once after collectGitState so all
// downstream phases see the same classification.
func classifyFiles(files []fileChange) {
for i := range files {
f := &files[i]
f.Language = LanguageFor(filepath.Ext(f.Path))
f.IsTest = isTestFile(f.Path)
f.IsConfig = isConfigFile(f.Path)
f.IsArtifact = isArtifactFile(f.Path)
f.IsDep = depManager(f.Path) != ""
f.Type = inferType(f)
}
}
// isConfigFile matches .md / .yaml / .yml / .json / .toml / .env* / .cfg / .ini
// / .conf — anything that flows through the BLOCKING content-review gate.
func isConfigFile(path string) bool {
base := filepath.Base(path)
if strings.HasPrefix(base, ".env") {
return true
}
switch filepath.Ext(path) {
case ".md", ".yaml", ".yml", ".json", ".toml", ".cfg", ".ini", ".conf":
return true
}
return false
}
// isArtifactFile matches generated/session-artifact paths we'd rather gitignore
// than commit: AUDIT.md, REVIEW.md, *-output.md, *.log, *.tmp, dist/, build/.
var artifactPatterns = []*regexp.Regexp{
regexp.MustCompile(`(?i)^(audit|review|plan|notes|scratch)\.md$`),
regexp.MustCompile(`(?i)-output\.md$`),
regexp.MustCompile(`\.(log|tmp|bak|swp)$`),
regexp.MustCompile(`^(dist|build|coverage|node_modules)/`),
}
func isArtifactFile(path string) bool {
base := filepath.Base(path)
for _, re := range artifactPatterns {
if re.MatchString(base) || re.MatchString(path) {
return true
}
}
return false
}
// inferType maps a fileChange to a conventional-commit type. Priority:
// artifact > deps > test > docs/chore > feat (default).
func inferType(f *fileChange) string {
if f.IsArtifact {
return "artifact"
}
if f.IsDep {
return "deps"
}
if f.IsTest {
return "test"
}
if strings.HasSuffix(f.Path, ".md") {
// README, CHANGELOG, docs/, agents/, skills/ — all docs.
return "docs"
}
base := filepath.Base(f.Path)
if base == "Makefile" || base == "Dockerfile" || strings.HasPrefix(base, ".github") {
return "chore"
}
if strings.HasPrefix(f.Path, ".github/") || strings.HasPrefix(f.Path, "ci/") {
return "chore"
}
if f.IsConfig {
return "chore"
}
return "feat"
}
// buildGroups is the grouping entrypoint: builds the weighted edge graph,
// runs connected-components at the given threshold, and assembles CommitGroup
// records with rationale + confidence. `symbols` maps path → parsed symbols
// (may be nil if repomap couldn't parse that language).
func buildGroups(gs *gitState, symbols map[string]*FileSymbols, threshold float64) []CommitGroup {
if len(gs.Files) == 0 {
return nil
}
edges := buildEdges(gs, symbols)
clusters := connectedComponents(gs.Files, edges, threshold)
// Singletons are already their own clusters; the loop below turns each
// cluster into one CommitGroup.
groups := make([]CommitGroup, 0, len(clusters))
for i, cluster := range clusters {
grp := assembleGroup(cluster, gs, edges)
grp.ID = groupID(i)
groups = append(groups, grp)
}
// Deterministic order: by first-file path.
slices.SortFunc(groups, func(a, b CommitGroup) int {
if len(a.Files) == 0 {
return -1
}
if len(b.Files) == 0 {
return 1
}
return strings.Compare(a.Files[0], b.Files[0])
})
// Re-ID after sort so g1..gN matches display order.
for i := range groups {
groups[i].ID = groupID(i)
}
return groups
}
func groupID(i int) string {
return "g" + strconv.Itoa(i+1)
}
// buildEdges computes weighted edges between dirty files. Edge weight is the
// max across reasons (test-pair dominates co-change, etc.).
func buildEdges(gs *gitState, symbols map[string]*FileSymbols) []edge {
paths := make([]string, 0, len(gs.Files))
byPath := make(map[string]*fileChange, len(gs.Files))
for i := range gs.Files {
f := &gs.Files[i]
paths = append(paths, f.Path)
byPath[f.Path] = f
}
// Index imports by internal package → files that import it; plus internal
// packages → files that define them. A file imports another if their
// ImportPaths overlap.
//
// PHP/Java quirk: imports target FQCNs (e.g. `use Foo\Bar\Baz`) while
// ImportPath is the namespace only (`Foo\Bar`). Index additional keys of
// the form `ImportPath<sep><ClassName>` for each top-level class-like
// symbol so FQCN imports hit the defining file directly.
pkgFiles := make(map[string][]string) // ImportPath -> paths
fileImports := make(map[string][]string)
for path, fs := range symbols {
if fs == nil {
continue
}
if fs.ImportPath != "" {
pkgFiles[fs.ImportPath] = append(pkgFiles[fs.ImportPath], path)
if sep := fqcnSeparator(fs.Language); sep != "" {
for _, sym := range fs.Symbols {
if isTopLevelTypeKind(sym.Kind) && sym.Name != "" {
fqcn := fs.ImportPath + sep + sym.Name
pkgFiles[fqcn] = append(pkgFiles[fqcn], path)
}
}
}
}
fileImports[path] = fs.Imports
}
seen := make(map[string]edge) // key "A|B" with A<B
add := func(a, b string, weight float64, reason string) {
if a == b {
return
}
// Never link files that belong to different top-level plugins —
// cross-plugin co-change is incidental, not logical coupling.
if crossesPluginBoundary(a, b) {
return
}
if a > b {
a, b = b, a
}
k := a + "|" + b
cur, ok := seen[k]
if !ok || weight > cur.Weight {
seen[k] = edge{A: a, B: b, Weight: weight, Reason: reason}
}
}
// Edge 1: test-pair. foo.go ↔ foo_test.go, foo.ts ↔ foo.test.ts,
// src/bar.py ↔ tests/test_bar.py.
for _, a := range paths {
for _, b := range paths {
if a >= b {
continue
}
if isTestPair(a, b) {
add(a, b, WeightTestPair, "test-pair")
}
}
}
// Edge 2: symbol-dep via import-path overlap. If file A imports package P
// and file B lives in P, they co-change logically. A single weight
// (WeightSymbolDep = 0.8) applies across languages because the match is
// always exact-syntax: Go module path equality, PHP `use Foo\Bar\Baz` →
// FQCN-indexed file, Python dotted package from __init__.py walk, etc.
for path, imports := range fileImports {
for _, imp := range imports {
for _, t := range pkgFiles[imp] {
add(path, t, WeightSymbolDep, "symbol-dep")
}
}
}
// Edge 3: co-change. gitState.CoChange[a][b] counts co-commits in the
// last 500. Threshold of 3 filters out incidental pairings.
for a, inner := range gs.CoChange {
if byPath[a] == nil {
continue
}
for b, count := range inner {
if byPath[b] == nil || count < 3 {
continue
}
add(a, b, WeightCoChange, "co-change")
}
}
// Edge 4: path sibling. Same directory AND same inferred type — tie-breaker
// when nothing else links two files.
for _, a := range paths {
for _, b := range paths {
if a >= b {
continue
}
fa, fb := byPath[a], byPath[b]
if filepath.Dir(a) != filepath.Dir(b) {
continue
}
if fa.Type != fb.Type {
continue
}
add(a, b, WeightSibling, "sibling")
}
}
edges := make([]edge, 0, len(seen))
for _, e := range seen {
edges = append(edges, e)
}
return edges
}
// isTestPair detects canonical test/source pairings across languages.
func isTestPair(a, b string) bool {
ax, bx := filepath.Ext(a), filepath.Ext(b)
if ax != bx {
// Python: src/foo.py ↔ tests/test_foo.py still shares .py.
return false
}
// Normalize: one is the test, one is the source.
testA, testB := isTestFile(a), isTestFile(b)
if testA == testB {
return false
}
test, src := a, b
if testB {
test, src = b, a
}
baseT := strings.TrimSuffix(filepath.Base(test), ax)
baseS := strings.TrimSuffix(filepath.Base(src), ax)
// Go: foo_test.go ↔ foo.go
if ax == ".go" {
return strings.TrimSuffix(baseT, "_test") == baseS
}
// TS/JS: foo.test.ts / foo.spec.ts ↔ foo.ts
if ax == ".ts" || ax == ".tsx" || ax == ".js" || ax == ".jsx" {
for _, suf := range []string{".test", ".spec"} {
if strings.TrimSuffix(baseT, suf) == baseS {
return true
}
}
}
// Python: test_foo.py / foo_test.py ↔ foo.py
if ax == ".py" {
if strings.HasPrefix(baseT, "test_") && baseT[5:] == baseS {
return true
}
if strings.HasSuffix(baseT, "_test") && strings.TrimSuffix(baseT, "_test") == baseS {
return true
}
}
// Rust: tests/foo.rs ↔ src/foo.rs
if ax == ".rs" && baseT == baseS {
return true
}
return false
}
// connectedComponents runs union-find over edges above threshold and returns
// clusters (each cluster is a sorted slice of paths). All dirty files appear
// in exactly one cluster; files with no qualifying edges are singletons.
func connectedComponents(files []fileChange, edges []edge, threshold float64) [][]string {
parent := make(map[string]string, len(files))
for _, f := range files {
parent[f.Path] = f.Path
}
var find func(string) string
find = func(x string) string {
if parent[x] != x {
parent[x] = find(parent[x])
}
return parent[x]
}
union := func(a, b string) {
ra, rb := find(a), find(b)
if ra != rb {
parent[ra] = rb
}
}
for _, e := range edges {
if e.Weight < threshold {
continue
}
if _, ok := parent[e.A]; !ok {
continue
}
if _, ok := parent[e.B]; !ok {
continue
}
union(e.A, e.B)
}
buckets := make(map[string][]string)
for _, f := range files {
root := find(f.Path)
buckets[root] = append(buckets[root], f.Path)
}
// Deterministic cluster order: sort each cluster, then sort clusters by
// their first element.
clusters := make([][]string, 0, len(buckets))
for _, paths := range buckets {
slices.Sort(paths)
clusters = append(clusters, paths)
}
slices.SortFunc(clusters, func(a, b []string) int {
return strings.Compare(a[0], b[0])
})
return clusters
}
// assembleGroup picks the dominant type + scope for a cluster, drafts a
// rationale string from matching edges, scores confidence, and collects
// per-edge evidence for agent introspection.
func assembleGroup(paths []string, gs *gitState, edges []edge) CommitGroup {
byPath := make(map[string]*fileChange, len(gs.Files))
for i := range gs.Files {
byPath[gs.Files[i].Path] = &gs.Files[i]
}
// Dominant type by file count (tests roll into their dominant-type partner
// via the test-pair edge, so we count them as their source's type where possible).
typeCounts := make(map[string]int)
for _, p := range paths {
if f := byPath[p]; f != nil {
typeCounts[f.Type]++
}
}
domType := dominantType(typeCounts)
// Scope: deepest common directory among non-test files (so feat(search) not
// feat(internal)).
scope := commonScope(paths, byPath)
// Rationale: list reasons that connected this cluster (distinct).
reasons, evidence := clusterReasonsAndEvidence(paths, edges)
// Confidence heuristic:
// 1.0 base
// -0.2 if mixed types
// -0.1 per 5 files beyond 3
// -0.15 if cluster has no non-sibling edge (singletons sometimes land here)
// floor at 0.3
conf := 1.0
if len(typeCounts) > 1 {
conf -= 0.2
}
if len(paths) > 3 {
conf -= 0.1 * float64((len(paths)-3)/5+1)
}
if len(paths) > 1 && !hasStrongEdge(paths, edges) {
conf -= 0.15
}
if conf < 0.3 {
conf = 0.3
}
rationale := "singleton"
if len(reasons) > 0 {
rationale = strings.Join(reasons, "; ")
}
return CommitGroup{
Type: domType,
Scope: scope,
Verb: dominantVerb(paths, byPath),
Files: paths,
Rationale: rationale,
Confidence: conf,
Evidence: evidence,
// SuggestedMsg and Breaking filled in by commit_messages.go / caller.
}
}
func dominantType(counts map[string]int) string {
best, bestN := "chore", -1
// Preference order when tied: feat > fix > refactor > test > docs > deps > chore > artifact.
priority := map[string]int{
"feat": 7, "fix": 6, "refactor": 5, "test": 4, "docs": 3, "deps": 2, "chore": 1, "artifact": 0,
}
for t, n := range counts {
if n > bestN || (n == bestN && priority[t] > priority[best]) {
best, bestN = t, n
}
}
return best
}
// commonScope returns the deepest directory shared by all non-test files in
// the cluster, trimmed to a short conventional-commit scope (last segment).
func commonScope(paths []string, byPath map[string]*fileChange) string {
var nonTest []string
for _, p := range paths {
if f := byPath[p]; f != nil && !f.IsTest {
nonTest = append(nonTest, p)
}
}
if len(nonTest) == 0 {
nonTest = paths
}
dirs := make([]string, len(nonTest))
for i, p := range nonTest {
dirs[i] = filepath.Dir(p)
}
cp := commonDirPrefix(dirs)
if cp == "" || cp == "." {
return ""
}
// Conventional commits favor a short scope — use the deepest directory
// segment.
last := filepath.Base(cp)
// Skip uninformative scopes.
switch last {
case ".", "/", "src", "internal", "pkg", "lib", "cmd":
return ""
}
return last
}
// commonDirPrefix returns the deepest directory that prefixes every input
// directory.
func commonDirPrefix(dirs []string) string {
if len(dirs) == 0 {
return ""
}
prefix := strings.Split(dirs[0], string(filepath.Separator))
for _, d := range dirs[1:] {
parts := strings.Split(d, string(filepath.Separator))
n := len(prefix)
if len(parts) < n {
n = len(parts)
}
i := 0
for i < n && prefix[i] == parts[i] {
i++
}
prefix = prefix[:i]
if len(prefix) == 0 {
return ""
}
}
return strings.Join(prefix, string(filepath.Separator))
}
// clusterReasonsAndEvidence returns the distinct edge reasons for the cluster
// (stable order) and the full EdgeEvidence slice for agent introspection.
// It replaces the old clusterReasons function and is the single source of truth
// for both the Rationale string and the Evidence array on CommitGroup.
func clusterReasonsAndEvidence(paths []string, edges []edge) ([]string, []EdgeEvidence) {
inCluster := make(map[string]bool, len(paths))
for _, p := range paths {
inCluster[p] = true
}
seen := make(map[string]bool)
order := []string{"test-pair", "symbol-dep", "co-change", "sibling"}
var evidence []EdgeEvidence
for _, e := range edges {
if inCluster[e.A] && inCluster[e.B] {
seen[e.Reason] = true
evidence = append(evidence, EdgeEvidence{
A: e.A,
B: e.B,
Weight: e.Weight,
Reason: e.Reason,
})
}
}
var reasons []string
for _, r := range order {
if seen[r] {
reasons = append(reasons, r)
}
}
return reasons, evidence
}
// fqcnSeparator returns the FQCN separator for languages that import by
// fully-qualified class name (PHP uses `\`, Java uses `.`). Returns "" for
// languages whose imports already match the ImportPath directly.
func fqcnSeparator(lang string) string {
switch lang {
case "php":
return `\`
case "java":
return "."
}
return ""
}
// isTopLevelTypeKind reports whether a symbol kind represents a top-level
// type that can be targeted by a `use` / `import` statement. Methods and
// properties live inside a class and are never FQCN-imported directly.
func isTopLevelTypeKind(kind string) bool {
switch kind {
case "class", "interface", "trait", "enum", "type", "function":
return true
}
return false
}
// hasStrongEdge is true if any in-cluster edge has weight > 0.3 (i.e. at least
// one non-sibling reason).
func hasStrongEdge(paths []string, edges []edge) bool {
inCluster := make(map[string]bool, len(paths))
for _, p := range paths {
inCluster[p] = true
}
for _, e := range edges {
if inCluster[e.A] && inCluster[e.B] && e.Weight > WeightSibling {
return true
}
}
return false
}