codegraph

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 29 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codegraph indexes the symbols and resolved call edges of a Go module and answers the queries an agent asks of a code graph: where a symbol is, who calls it, what it calls, and which tests reach it.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotConfigured = errors.New("codegraph: rules not configured; call Configure first")

ErrNotConfigured is returned by Build until Configure has been called.

Functions

func CacheDir

func CacheDir() (string, error)

CacheDir is the default directory for saved indexes.

func Configure

func Configure(data []byte) error

Configure sets the business rules from YAML the caller supplies (ranking weights, stopwords, result limits, path bounds). Build fails with ErrNotConfigured until it succeeds.

func RefTools

func RefTools(ix *Index) []mcp.Tool

RefTools are the reference and import tools. references covers Go only as far as the SSA form records non-call uses; imports_exports reads Go files.

func Tier2Tools

func Tier2Tools(ix *Index) []mcp.Tool

Tier2Tools are the path, neighbourhood and source tools. references and imports_exports are not served: they need non-call references and import statements, which the index does not store.

func Tier3Tools

func Tier3Tools(ix *Index) []mcp.Tool

Tier3Tools are the memory, workspace, risk and fetch tools. Notes are local (see package notes). Workspaces are one: this module. Options that only the hosted memory service has (profiles, sufficiency, grounding, time filters) are accepted and ignored. fetch takes a symbol name, not a repository:snapshot:node id, because this index has no snapshots.

func Tools

func Tools(ix *Index) []mcp.Tool

Default result limits, for the tools. Tools returns the code graph tools under stable names.

strict_calls and code_only are accepted for compatibility. The index holds only resolved calls and only code nodes, so both have no effect yet.

func Words

func Words(question string) []string

Words are the lowercased words of a question, without stopwords.

Types

type Hit

type Hit struct {
	Symbol Symbol
	Score  float64
	Body   string
}

Hit is a symbol that matched a question, with its source when requested.

type Index

type Index struct {
	Root    string // absolute module root
	Module  string
	Symbols map[string]Symbol
	// Missing lists languages found in the module whose language server is
	// not installed, so their symbols are absent.
	Missing []string

	// Notes is the module's local note store, used by the memory tools.
	Notes *notes.Store
	// contains filtered or unexported fields
}

Index is the code graph of one module.

func Build

func Build(dir string) (*Index, error)

Build indexes every function in dir. Go modules are analysed with SSA and VTA. Other languages found under dir are analysed by the language server that registry.yaml names for them, when one is installed.

func BuildCached

func BuildCached(dir, cacheDir string) (*Index, error)

BuildCached returns the index of dir, reusing the copy saved in cacheDir when no file under dir has changed since it was saved. Otherwise it builds the index and saves it there.

func (*Index) Affected

func (ix *Index) Affected(symbol string, depth, limit int) ([]Symbol, error)

Affected lists the symbols that depend on the target through callers, up to depth hops. Each symbol appears once, at its nearest distance.

func (*Index) Callees

func (ix *Index) Callees(symbol string, limit int) ([]Symbol, error)

Callees are the functions the symbol calls directly.

func (*Index) Callers

func (ix *Index) Callers(symbol string, limit int) ([]Symbol, error)

Callers are the functions that call the symbol directly.

func (*Index) Expand

func (ix *Index) Expand(names []string) map[string]string

Expand returns the source of each named symbol; names that do not resolve are reported with an empty body.

func (*Index) FileNeighbors

func (ix *Index) FileNeighbors(file string, limit int) (dependencies, dependents []string)

FileNeighbors are the files this file calls into (dependencies) and the files that call into it (dependents), from the call graph.

func (*Index) FileSymbols

func (ix *Index) FileSymbols(file string) []Symbol

FileSymbols are the symbols defined in a file, in source order.

func (*Index) Find

func (ix *Index) Find(term string, limit int) []Symbol

Find returns the symbols whose full name contains term, sorted.

func (*Index) ImpactRisk

func (ix *Index) ImpactRisk(target string, limit int) ([]map[string]any, []Symbol, error)

ImpactRisk ranks the files holding the target's direct callers and callees by how many of those symbols they hold, and lists the tests that reach the target.

func (*Index) ImportsExports

func (ix *Index) ImportsExports(file string) (imports, exports []string, err error)

ImportsExports lists the import paths and exported top-level names of a Go file under the module root. Other languages are not analysed.

func (*Index) Neighbourhood

func (ix *Index) Neighbourhood(seeds []string, depth, nodeLimit int) (nodes []string, edges [][2]string)

Neighbourhood is the set of symbols within depth hops of the seeds, and the call edges between them, capped at nodeLimit nodes.

func (*Index) Neighbours

func (ix *Index) Neighbours(target string, limit int) ([]Symbol, error)

Neighbours are the direct callers and callees of the target symbol.

func (*Index) Node

func (ix *Index) Node(symbol string, limit int) (Symbol, string, []Symbol, error)

Node is a symbol with its source and its direct callers and callees.

func (*Index) Query

func (ix *Index) Query(question string, k, budget int, skeleton bool) []Hit

Query returns the k best-matching symbols for question, with their source text cut to budget characters. skeleton drops the source.

func (*Index) RankFiles

func (ix *Index) RankFiles(question string, limit int) []Hit

RankFiles scores each file by the symbols in it that match question.

func (*Index) References

func (ix *Index) References(symbol string, limit int) ([]Reference, error)

References returns the non-call uses of a symbol, sorted by location.

func (*Index) ShortestPath

func (ix *Index) ShortestPath(src, tgt string) ([]string, error)

ShortestPath is the shortest connection between src and tgt when call edges are followed in either direction. It is nil when they are not connected.

func (*Index) Stats

func (ix *Index) Stats() (symbols, edges int)

Stats reports the number of symbols and call edges.

func (*Index) TestsFor

func (ix *Index) TestsFor(target string, limit int) ([]Symbol, error)

TestsFor are the test functions that reach the target, through any chain of callers. The target is a symbol name or a file path in the module.

func (*Index) Trace

func (ix *Index) Trace(src, tgt string, k int) ([][]string, error)

Limits for path and neighbourhood queries. Trace returns up to k simple directed call paths from src to tgt, shortest first. Each path lists symbol names from src to tgt.

type Reference

type Reference struct {
	Symbol string `json:"symbol"` // the referenced symbol
	From   string `json:"from"`   // the symbol whose body makes the reference
	File   string `json:"file"`
	Line   int    `json:"line"`
}

Reference is a use of a symbol that is not a direct call: the function is passed, stored, or captured as a value.

type Symbol

type Symbol struct {
	Name     string // full name, e.g. "example.com/m/pkg.(*T).Method" or "src/app.py::Login.run"
	File     string // path relative to the module root
	Line     int
	Language string // "go", or the registry id of a language server
	IsTest   bool   // defined in a test file
}

Symbol is one function or method in the module.

Jump to

Keyboard shortcuts

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