codegraph

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDir

func ParseDir(dir, pkgPath string, store *Store) error

ParseDir parses all .go files under dir, adding nodes and edges to store. pkgPath is the module-qualified import path for the package (used to build NodeIDs).

Types

type Edge

type Edge struct {
	From NodeID
	To   NodeID
	Kind EdgeKind
}

Edge represents a directed relationship.

type EdgeKind

type EdgeKind int

EdgeKind classifies a directed relationship between two nodes.

const (
	EdgeCalls EdgeKind = iota
	EdgeDefines
	EdgeImplements
	EdgeImports
)

func (EdgeKind) String

func (k EdgeKind) String() string

String returns the uppercase name of the kind (e.g. "CALLS", "DEFINES"). EdgeKind uses uppercase to distinguish it visually from node/kind names in serialized output and log messages; consumers must compare case-sensitively.

type Index

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

Index is the primary entry point for the codegraph. It manages incremental parsing and exposes query methods.

func NewIndex

func NewIndex(pkgPath string) *Index

NewIndex returns an empty Index for the given module-qualified package path.

func (*Index) Callees

func (idx *Index) Callees(symID NodeID) []*Node

Callees returns all nodes that symID has a CALLS edge to.

func (*Index) Callers

func (idx *Index) Callers(symID NodeID) []*Node

Callers returns all nodes that have a CALLS edge pointing to symID.

func (*Index) Impact

func (idx *Index) Impact(symID NodeID) []*Node

Impact performs a reverse-BFS from symID over CALLS edges and returns all nodes that transitively call symID (i.e., would be affected by a change).

func (*Index) Lookup

func (idx *Index) Lookup(id NodeID) *Node

Lookup returns the Node with the given id, or nil if not present. It holds idx.mu.RLock for both the store dereference and the map lookup, preventing a data race with a concurrent Rebuild that atomically swaps idx.store.

func (*Index) Rebuild

func (idx *Index) Rebuild(root string) error

Rebuild walks root, re-parses files whose content hash changed, and removes nodes/edges for files that disappeared. It is safe to call repeatedly.

func (*Index) Search

func (idx *Index) Search(name string) []*Node

Search returns all nodes whose Name matches the query (exact match first, then prefix matches when no exact match is found).

func (*Index) Store

func (idx *Index) Store() *Store

Store returns a snapshot pointer to the underlying Store at the instant of the call. The pointer is valid for READ-ONLY use only. It carries no durability guarantee: a concurrent Rebuild may atomically replace idx.store at any time after Store() returns, making the returned pointer stale. Callers must NOT retain the pointer across yield points and must NOT call AddNode / AddEdge through it; doing so introduces a data race. Use the Index query methods (Search, Callers, Callees, Impact, Lookup) for safe access.

type LatentInjector

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

LatentInjector renders the top-N most central symbols from an Index into a compact markdown snippet suitable for injection into the dynamic context window (i.e., AFTER prompt.DynamicContextBoundary).

func NewLatentInjector

func NewLatentInjector(idx *Index, topN int) *LatentInjector

NewLatentInjector creates a LatentInjector that will render the top-N symbols by PageRank centrality.

func (*LatentInjector) Render

func (li *LatentInjector) Render() string

Render returns a markdown-formatted symbol table of the most central nodes. The output is intended for placement AFTER prompt.DynamicContextBoundary.

type Node

type Node struct {
	ID      NodeID
	Kind    NodeKind
	Name    string
	File    string
	Line    int
	Snippet string // first line of declaration
}

Node represents a single named symbol.

func RankByPageRank

func RankByPageRank(s *Store, iterations int) []*Node

RankByPageRank runs `iterations` steps of PageRank over the CALLS edge set and returns nodes sorted descending by score. A sensible value for iterations is 20–100 for small graphs; higher values cost more but converge more tightly. When iterations is 0 or negative the loop is skipped and every node receives an equal score of 1/n, with ties broken arbitrarily by the final sort.

type NodeID

type NodeID string

NodeID is a fully-qualified symbol identifier: "<pkg-path>.<Name>". Defined as a distinct type (not an alias) to prevent raw strings from being passed where a NodeID is expected.

type NodeKind

type NodeKind int

NodeKind classifies a symbol in the graph.

const (
	KindFunc NodeKind = iota
	KindType
	KindInterface
)

func (NodeKind) String

func (k NodeKind) String() string

String returns the lowercase name of the kind (e.g. "func", "type"). NodeKind uses lowercase to match conventional Go source spelling.

type Store

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

Store is the in-memory graph. The adjacency maps are unexported to ensure all mutations go through AddEdge, which maintains Out/In consistency and deduplication.

func NewStore

func NewStore() *Store

NewStore returns an empty Store with all internal maps initialised.

func (*Store) AddEdge

func (s *Store) AddEdge(e Edge)

AddEdge inserts an edge (deduplication by From+To+Kind).

func (*Store) AddNode

func (s *Store) AddNode(n *Node)

AddNode inserts or replaces a node.

func (*Store) AllNodes

func (s *Store) AllNodes() []*Node

AllNodes returns a slice of all nodes in the store (order unspecified).

func (*Store) InEdges

func (s *Store) InEdges(to NodeID) []Edge

InEdges returns a copy of the incoming edges to the given node. Returns nil when the node has no incoming edges. Callers may freely modify the returned slice without affecting store internals.

func (*Store) Node

func (s *Store) Node(id NodeID) *Node

Node returns the node with the given ID, or nil if not present.

func (*Store) OutEdges

func (s *Store) OutEdges(from NodeID) []Edge

OutEdges returns a copy of the outgoing edges from the given node. Returns nil when the node has no outgoing edges. Callers may freely modify the returned slice without affecting store internals.

Jump to

Keyboard shortcuts

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