-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcrossrepo.go
More file actions
99 lines (88 loc) · 2.78 KB
/
Copy pathcrossrepo.go
File metadata and controls
99 lines (88 loc) · 2.78 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
// Package crossrepo provides an explainer that summarizes the cross-repo
// dependency edges synthesized by internal/linkers/crossrepo into
// human-readable insights for the LLM context and insights.json output.
package crossrepo
import (
"context"
"fmt"
"sort"
"strings"
"github.com/enola-labs/enola/internal/facts"
)
// CrossRepoExplainer turns synthetic cross_repo dependency facts into insights.
type CrossRepoExplainer struct{}
// New creates a new CrossRepoExplainer.
func New() *CrossRepoExplainer {
return &CrossRepoExplainer{}
}
func (e *CrossRepoExplainer) Name() string {
return "crossrepo"
}
// Explain reads the cross_repo dependency facts the linker already added to the
// store and emits one summarizing insight describing how the repositories
// depend on each other. It returns nothing for single-repo snapshots.
func (e *CrossRepoExplainer) Explain(ctx context.Context, store *facts.Store) ([]facts.Insight, error) {
deps, _ := store.QueryAdvanced(facts.QueryOpts{
Kind: facts.KindDependency,
Prop: "type",
PropValue: "cross_repo",
Limit: 500,
})
if len(deps) == 0 {
return nil, nil
}
sort.Slice(deps, func(i, j int) bool { return deps[i].Name < deps[j].Name })
evidence := make([]facts.Evidence, 0, len(deps))
var lines []string
for _, d := range deps {
detail := edgeDetail(d)
evidence = append(evidence, facts.Evidence{
Fact: d.Name,
Detail: detail,
})
lines = append(lines, d.Name+" ("+detail+")")
}
insight := facts.Insight{
Title: fmt.Sprintf("Cross-repo dependencies (%d edges)", len(deps)),
Description: "These service-to-service dependencies span repositories: " +
strings.Join(lines, "; ") + ". Traverse from a repo label (service node) " +
"to follow requests across repos.",
Confidence: 0.9,
Evidence: evidence,
}
return []facts.Insight{insight}, nil
}
// edgeDetail renders a short description of what justifies a cross-repo edge.
func edgeDetail(d facts.Fact) string {
var parts []string
if v, ok := d.Props["via"].([]string); ok && len(v) > 0 {
parts = append(parts, "via "+strings.Join(v, "+"))
}
if n := propInt(d, "endpoint_count"); n > 0 {
parts = append(parts, fmt.Sprintf("%d endpoint(s)", n))
}
if n := propInt(d, "import_count"); n > 0 {
parts = append(parts, fmt.Sprintf("%d import(s)", n))
}
if n := propInt(d, "symbol_count"); n > 0 {
parts = append(parts, fmt.Sprintf("%d shared symbol(s)", n))
}
if len(parts) == 0 {
return "cross-repo dependency"
}
return strings.Join(parts, ", ")
}
// propInt reads an int-valued prop, tolerating the float64 form that survives a
// JSON round-trip through facts.jsonl.
func propInt(d facts.Fact, key string) int {
if d.Props == nil {
return 0
}
switch v := d.Props[key].(type) {
case int:
return v
case float64:
return int(v)
}
return 0
}