Help us shape the future of Go! Take the 2026 Go Developer Survey

graph

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrGraphLocked = errors.New("graph is locked by another process (daemon running?); stop the daemon first")

ErrGraphLocked is returned when another process holds the graph lock.

Functions

func GodNodes

func GodNodes(g *Graph, topN int) []types.Node

GodNodes returns the topN nodes sorted by total degree (in + out). These are the most-connected concepts in the graph — architectural hubs.

func RunLeiden

func RunLeiden(g *Graph) (map[string]int, error)

RunLeiden runs Leiden community detection on the graph by delegating to the bundled Python script (scripts/leiden.py). Returns a map of node ID → community ID (0-indexed integers).

If Python or the script is unavailable, a clear error is returned (no panic). The script itself falls back to connected-components if graspologic is missing.

func SuggestedQuestions

func SuggestedQuestions(nodes []types.Node, edges []types.Edge) []string

SuggestedQuestions returns 4–5 question templates populated with the names of the top god nodes. These are questions the graph is uniquely positioned to answer through path-finding and community analysis.

func SurpriseEdges

func SurpriseEdges(g *Graph, topN int) []types.Edge

SurpriseEdges returns the topN edges that cross community boundaries, ranked by the product of the degrees of their endpoints (a proxy for betweenness centrality: high-degree nodes in different communities that are directly connected are architecturally "surprising").

Types

type Graph

type Graph struct {
	Directed      *simple.DirectedGraph
	NodeIndex     map[string]int64     // node ID string → gonum int64 ID
	NodeByID      map[int64]types.Node // gonum int64 ID → types.Node
	ResolvedEdges []types.Edge         // only edges where both endpoints resolved
	Nodes         []types.Node
}

Graph wraps a gonum directed graph with auxiliary indexes for fast lookup.

func Build

func Build(nodes []types.Node, edges []types.Edge) (*Graph, error)

Build constructs a Graph from the provided nodes and edges.

Edge resolution:

  • e.Source must match a node ID exactly (it is always in "<file>:<name>" format).
  • e.Target is first tried as an exact ID, then as a bare label via labelIndex.

Only edges where BOTH endpoints resolve to known nodes are stored in ResolvedEdges and added to the gonum graph. Dangling edges (targeting stdlib/external symbols) are silently dropped — they add noise and break Obsidian graph view by creating isolated satellite dots.

func (*Graph) ApplyCommunities

func (g *Graph) ApplyCommunities(partition map[string]int) map[int][]string

ApplyCommunities writes community IDs back onto the graph's node metadata and populates the community → []nodeID index for report generation.

func (*Graph) Communities

func (g *Graph) Communities() map[int][]string

Communities returns a map of communityID → list of node IDs (empty until clustering is run).

func (*Graph) Degree

func (g *Graph) Degree(nodeID string) int

Degree returns the total degree (in + out) of the node with the given ID string.

func (*Graph) EdgeCount

func (g *Graph) EdgeCount() int

EdgeCount returns the number of resolved edges in the graph.

func (*Graph) NodeCount

func (g *Graph) NodeCount() int

NodeCount returns the number of nodes in the graph.

func (*Graph) ToTypes

func (g *Graph) ToTypes() *types.Graph

ToTypes converts this internal Graph to the types.Graph for serialisation. Only resolved edges are exported — dangling references are excluded.

type GraphLock added in v1.3.0

type GraphLock struct {
	// contains filtered or unexported fields
}

GraphLock holds an exclusive advisory flock on ~/.vela/graph.lock so the daemon and vela extract cannot write graph.json concurrently.

func AcquireGraphLock added in v1.3.0

func AcquireGraphLock(lockPath string) (*GraphLock, error)

AcquireGraphLock opens the lock file and acquires a non-blocking exclusive flock. Returns ErrGraphLocked if another process already holds the lock.

func (*GraphLock) Release added in v1.3.0

func (l *GraphLock) Release()

Release unlocks and closes the lock file. Safe to call on a nil receiver.

type HealthMetrics added in v1.2.0

type HealthMetrics struct {
	Path        string `json:"path"`
	GeneratedAt string `json:"generated_at,omitempty"`

	// Size
	Nodes int `json:"nodes"`
	Edges int `json:"edges"`

	// Coverage — breakdowns
	NodesByKind     map[string]int `json:"nodes_by_kind"`
	EdgesByRelation map[string]int `json:"edges_by_relation"`

	// Quality / resolution
	BrokenEdges    int     `json:"broken_edges"`
	SelfLoops      int     `json:"self_loops"`
	DuplicateEdges int     `json:"duplicate_edges"`
	ResolutionRate float64 `json:"resolution_rate"` // 0..1

	// Confidence
	ConfidenceDist map[string]int `json:"confidence_dist"`
	ExtractedRate  float64        `json:"extracted_rate"` // 0..1

	// Degree
	AvgDegree     float64 `json:"avg_degree"`
	MedianDegree  int     `json:"median_degree"`
	P95Degree     int     `json:"p95_degree"`
	MaxDegree     int     `json:"max_degree"`
	IsolatedNodes int     `json:"isolated_nodes"`
	HubNodes      int     `json:"hub_nodes"`  // total degree >= 10
	LeafNodes     int     `json:"leaf_nodes"` // total degree == 1
	ConnectedPct  int     `json:"connected_pct"`

	// Communities
	Communities          int     `json:"communities"`
	LargestCommunitySize int     `json:"largest_community_size"`
	SingletonCommunities int     `json:"singleton_communities"`
	Modularity           float64 `json:"modularity"`

	// Top nodes (length fixed via TopN when loading)
	TopByOutDegree []NodeRank `json:"top_by_out_degree"`
}

HealthMetrics is the full set of graph-health signals computed from an on-disk graph.json. It is the single source of truth consumed by both `vela bench` and the TUI Graph Status screen.

func LoadHealthMetrics added in v1.2.0

func LoadHealthMetrics(path string, topN int) (HealthMetrics, error)

LoadHealthMetrics reads graph.json at path and computes every signal. topN controls the length of TopByOutDegree (5 is a reasonable default).

type NodeRank added in v1.2.0

type NodeRank struct {
	ID     string `json:"id"`
	Label  string `json:"label"`
	File   string `json:"file,omitempty"`
	Kind   string `json:"kind,omitempty"`
	OutDeg int    `json:"out_degree"`
	InDeg  int    `json:"in_degree"`
}

NodeRank is a compact row used in metric leaderboards (top-N by degree).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL