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

graph

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package graph is the in-memory code graph: nodes keyed by qualified name, edges, and the adjacency indexes that power callers/callees/impact queries. It is rebuilt from scratch on each service start, so there is no persistence layer here — at ~10^3-10^4 nodes a full re-parse is cheap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisplayTitle

func DisplayTitle(n *Node) string

Dump exports the whole graph as titles + edges for a force-directed UI. It emits an edge only when BOTH endpoints are real nodes — the same reasoning as ImpactRadius skipping non-node call targets: unresolved bare symbols (e.g. a common "Get") aren't nodes, and rendering them as phantom hubs would merge unrelated call sites and clutter the layout. Read-only; takes the read lock. DisplayTitle is a node's name as every surface prints it: qualified by its parent when it has one. A child's bare Name is not unique — sibling sets repeat it under every parent — so an unqualified title renders distinct nodes as duplicates. Single definition of the rule: index.nodeTitle calls this.

Types

type DumpEdge

type DumpEdge struct {
	Source string `json:"source"`
	Target string `json:"target"`
	Kind   string `json:"kind"`
}

DumpEdge is one directed edge in a GraphDump. Both endpoints are guaranteed to be real nodes in the same dump (see Dump).

type DumpNode

type DumpNode struct {
	QN    string `json:"qn"`
	Repo  string `json:"repo,omitempty"`
	Kind  string `json:"kind"`
	Title string `json:"title"`
	File  string `json:"file"`
	Line  int    `json:"line"`
	Lang  string `json:"lang,omitempty"`
}

DumpNode is one node in a GraphDump — identity + placement, no body.

type Edge

type Edge struct {
	Kind   string
	Source string
	Target string
	File   string
	Line   int
	// Extra is the extractor's hints for the edge (parser.EdgeInfo.Extra),
	// carried through linking so a reader of the graph sees what the extractor
	// recorded — a compose env key's inline value, a SQL reference's kind —
	// rather than only its endpoints. Shared with the parsed result; read-only.
	Extra map[string]string
}

Edge is a stored directed relationship.

type Graph

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

Graph holds the whole code graph. It is read-mostly after Build; the mutex guards the rare full rebuild.

func New

func New() *Graph

New returns an empty graph.

func (*Graph) AttrKeysByKind

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

AttrKeysByKind reports, per node kind, the attribute keys its nodes carry and how many carry each. Kinds whose nodes carry no attribute (every parsed code kind) are absent, so a code map's stats say nothing new. It is the vocabulary a caller needs BEFORE asking a catalog map to filter or order by an attribute: the team map was ordered by "experience" when the key was "years", and nothing short of reading a node back with get_graph_node could have said so.

func (*Graph) Build

func (g *Graph) Build(repos ...RepoGraph) LinkStats

Build atomically swaps in a graph assembled from one or more repositories, running cross-file call resolution so calls edges point at qualified nodes instead of bare names. Lock-free readers see the old graph until the swap completes. It returns resolution stats.

func (*Graph) Callees

func (g *Graph) Callees(qn string, relation string) []Edge

Callees returns outgoing edges of a node, optionally filtered by relation.

func (*Graph) Callers

func (g *Graph) Callers(qn string, relation string) []Edge

Callers returns incoming edges of a node, optionally filtered by relation.

func (*Graph) Dump

func (g *Graph) Dump() GraphDump

func (*Graph) Get

func (g *Graph) Get(qn string) (*Node, bool)

Get returns a node by qualified name.

func (*Graph) ImpactRadius

func (g *Graph) ImpactRadius(seeds []string, maxDepth, maxNodes int) []ImpactNode

ImpactRadius runs a bidirectional BFS from the given seed qualified names over dependency edge kinds (calls/imports/inherits/implements/references — see impactEdgeKinds), up to maxDepth, capped at maxNodes. This is the in-memory equivalent of code-review-graph's recursive-CTE blast-radius query.

func (*Graph) Nodes

func (g *Graph) Nodes() []*Node

Nodes returns all nodes in stable order (callers must not mutate).

func (*Graph) NodesForFile

func (g *Graph) NodesForFile(file string) []*Node

NodesForFile returns all nodes declared in the given file path.

func (*Graph) Risk

func (g *Graph) Risk(qn string, maxDepth, maxNodes int) RiskReport

Risk scores the change risk of one symbol. Thresholds are deliberately simple (caller count + module spread, not a weighted model) — the goal is a quick triage signal, not a precise estimate.

func (*Graph) SetVec

func (g *Graph) SetVec(qn string, vec []float32)

SetVec attaches an embedding to a node by qn.

func (*Graph) Stats

func (g *Graph) Stats() Stats

type GraphDump

type GraphDump struct {
	Nodes []DumpNode `json:"nodes"`
	Edges []DumpEdge `json:"edges"`
}

GraphDump is the lean whole-graph export for visualization: node titles + edges, with no node bodies (Body/Content are omitted — the UI lazy-fetches a single node's full content via /node on click). At ~10^3 nodes this serializes to a few hundred KB, small enough for the UI to fetch once and lay out locally.

type ImpactNode

type ImpactNode struct {
	QN    string
	Depth int
}

ImpactNode is a node reached by an impact-radius BFS, with its distance.

type LinkStats

type LinkStats struct {
	CallEdges     int
	ResolvedCalls int // rewritten to a qualified node QN
	ExternalCalls int // stdlib / third-party / unresolvable (kept as-is)
	ResolvedRatio float64
	// ResolvedByRule splits ResolvedCalls by the linker rule that produced each
	// edge (pkg, chain, recvPkg, recvType, bare — see link.go), so a build
	// shows which declared evidence its edges rest on.
	ResolvedByRule map[string]int
	// SQLRefs counts SQL references edges resolved to a declared schema object;
	// DroppedSQLRefs those that named no single declaration (see sqllink.go).
	SQLRefs        int
	DroppedSQLRefs int
	// TypeRefs counts declared type-name references (Java fields, parameters,
	// locals); ResolvedTypeRefs those resolved to a class (see typelink.go).
	TypeRefs         int
	ResolvedTypeRefs int
}

LinkStats reports how call-edge resolution went, for observability.

type Node

type Node struct {
	parser.NodeInfo
	Repo string    // owning repository label (multi-repo serving)
	QN   string    // repo-qualified name (cached); unique across repos
	Vec  []float32 // embedding, nil until indexed
}

Node is a stored code symbol plus its embedding (set after indexing).

type RepoGraph

type RepoGraph struct {
	Repo  string
	Nodes []parser.NodeInfo
	Edges []parser.EdgeInfo
}

RepoGraph is one repository's parsed nodes and edges, fed to Build.

type RiskReport

type RiskReport struct {
	QN            string
	Level         string // LOW, MEDIUM, HIGH
	DirectCallers int
	ModuleSpread  int
	ImpactedCount int
	Modules       []string
}

RiskReport buckets a symbol's change risk into a coarse LOW/MEDIUM/HIGH signal, derived from its direct caller count and how many distinct modules (directories) its blast radius reaches. It is a heuristic, not a precise cost model — it exists so a caller gets a signal instead of having to eyeball a raw impact list themselves.

type Stats

type Stats struct {
	Nodes    int
	Edges    int
	ByKind   map[string]int
	ByEdge   map[string]int
	Embedded int
}

Stats reports node/edge counts by kind.

func (Stats) DependencyEdges

func (s Stats) DependencyEdges() int

DependencyEdges counts the edges impact and risk traverse (impactEdgeKinds). Zero means the map is structure-only — contains edges alone, as in a document collection — and a blast radius is not defined for it.

Jump to

Keyboard shortcuts

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