Documentation
¶
Overview ¶
Package codegraph is X-Ray's code graph, run inside your own process.
It parses source code, Markdown and Kubernetes manifests into one graph of symbols, call edges, imports and document sections, embeds every node, and answers structural questions: where something is implemented, who calls it, what a change reaches, how risky it is. It is the engine the xray server and "xray graph" command use: Tree-sitter for parsing, an HNSW index and a trigram index for search, fused by reciprocal rank.
emb, _ := codegraph.NewEmbeddings(ctx, "http://localhost:8090", "nomic", 256, 8192)
g, err := codegraph.Build(ctx, emb, codegraph.Repo{Label: "myapp", Path: "./myapp"})
if err != nil { ... }
hits, _ := g.SearchGraph(ctx, "where do we retry payments?", xray.Limit(5))
impact, _ := g.Impact(ctx, []string{hits.Results[0].QN}, 2, 50)
g.UpdateGraph(ctx, "./myapp/pay/retry.go") // after the agent edits a file
g.Save(ctx, "myapp.graph.gob.gz") // later: codegraph.Load
Graph implements xray.CodeGraph, the interface the remote xray.Client also implements, so code written against it runs in process or against a server.
Parsing needs the Tree-sitter grammars, which are C, so Build, UpdateGraph and Reindex need a build with CGO_ENABLED=1 and a C compiler. A build without cgo can still Load a snapshot, search it, and serve it; Build returns ErrNoParser. Embeddings come from an OpenAI-compatible endpoint you run, or from your own Embedder.
Index ¶
- Variables
- type Embedder
- type Embeddings
- type Graph
- func (g *Graph) GraphStats(context.Context, ...xray.GraphOption) (json.RawMessage, error)
- func (g *Graph) Handler() http.Handler
- func (g *Graph) Impact(_ context.Context, seeds []string, depth, maxNodes int) (*xray.Impact, error)
- func (g *Graph) Labels() []string
- func (g *Graph) Node(_ context.Context, qn string) (*xray.Node, error)
- func (g *Graph) Reindex(ctx context.Context) error
- func (g *Graph) Risk(_ context.Context, qn string, depth, maxNodes int) (*xray.Risk, error)
- func (g *Graph) Save(ctx context.Context, path string) error
- func (g *Graph) SearchGraph(ctx context.Context, query string, opts ...xray.GraphOption) (*xray.SearchResult, error)
- func (g *Graph) SearchGraphDocs(ctx context.Context, query string, opts ...xray.GraphOption) (*xray.SearchResult, error)
- func (g *Graph) UpdateGraph(ctx context.Context, paths ...string) (*xray.UpdateResult, error)
- type LoadOption
- type Repo
Constants ¶
This section is empty.
Variables ¶
var ErrNoParser = errors.New("codegraph: this build has no Tree-sitter parser; build with CGO_ENABLED=1, or Load a snapshot")
ErrNoParser is returned by Build, UpdateGraph and Reindex in a build without cgo: the Tree-sitter parser is not linked in. Load a snapshot instead, or build with CGO_ENABLED=1.
Functions ¶
This section is empty.
Types ¶
type Embedder ¶
Embedder turns text into vectors. EmbedBatch is used when a graph is built, EmbedQuery once per search.
type Embeddings ¶
type Embeddings struct {
Embedder
// contains filtered or unexported fields
}
Embeddings is an Embedder together with the fingerprint a snapshot records: the model, the vector size and the input cap. A snapshot can only be loaded by a program whose embeddings carry the same fingerprint, because its vectors are meaningless in another space.
func CustomEmbeddings ¶
func CustomEmbeddings(e Embedder, model string, dims, maxInputChars int) *Embeddings
CustomEmbeddings wraps your own Embedder. model, dims and maxInputChars are recorded in snapshots and checked on load; dims must be the length of every vector the embedder returns.
func NewEmbeddings ¶
func NewEmbeddings(ctx context.Context, endpoint, model string, dims, maxInputChars int) (*Embeddings, error)
NewEmbeddings embeds through an OpenAI-compatible /v1/embeddings endpoint (llama.cpp, Ollama, vLLM, LM Studio or a hosted one). dims is the vector size to truncate to (256 suits nomic) and maxInputChars caps the text per node (8192 fits a 2048-token model).
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
Graph is an in-process code graph. It is safe for concurrent use.
func Build ¶
Build parses the repositories into a graph and embeds every node. It reads source files, Markdown and YAML under each path, skipping ignored directories and symlinked ones. Embedding is the slow part: a large repository makes one request per batch of nodes.
func Load ¶
func Load(ctx context.Context, path string, emb *Embeddings, opts ...LoadOption) (*Graph, error)
Load reads a snapshot written by Save or by "xray graph snapshot" and returns a graph over it. Nothing is parsed or embedded: the snapshot carries the graph, the vectors and the built HNSW index, so loading is fast and works in a build without cgo. The embeddings must match the snapshot's fingerprint.
func (*Graph) GraphStats ¶
func (g *Graph) GraphStats(context.Context, ...xray.GraphOption) (json.RawMessage, error)
GraphStats reports node and edge counts, languages and index state as JSON.
func (*Graph) Handler ¶
Handler serves the graph over MCP (streamable HTTP, at /mcp) and returns it as an http.Handler, so the program can offer its own graph to other agents: mux.Handle("/mcp", g.Handler()). The handler has no authentication; put it behind your own.
func (*Graph) Impact ¶
func (g *Graph) Impact(_ context.Context, seeds []string, depth, maxNodes int) (*xray.Impact, error)
Impact returns what a change to the seed symbols reaches over calls and imports. depth and maxNodes of 0 use 2 and 200.
func (*Graph) Save ¶
Save writes the graph, with its vectors and HNSW index, to a snapshot file that Load and "xray graph serve-snapshots" read. A snapshot holds one repository, so Save needs a graph built from one; build one Graph per repository to snapshot several.
func (*Graph) SearchGraph ¶
func (g *Graph) SearchGraph(ctx context.Context, query string, opts ...xray.GraphOption) (*xray.SearchResult, error)
SearchGraph finds code and manifests by meaning. OnMap names a repository label; without it every repository in the graph is searched.
func (*Graph) SearchGraphDocs ¶
func (g *Graph) SearchGraphDocs(ctx context.Context, query string, opts ...xray.GraphOption) (*xray.SearchResult, error)
SearchGraphDocs finds documentation prose by meaning: ADRs, READMEs, todos.
func (*Graph) UpdateGraph ¶
UpdateGraph re-indexes changed files. Only nodes whose text changed reach the embedder. A path outside every indexed repository comes back in Unowned and is never read. Needs a build with cgo for source files.
type LoadOption ¶
type LoadOption func(*loadConfig)
LoadOption configures Load.
func WithSnapshotKey ¶
func WithSnapshotKey(file string) LoadOption
WithSnapshotKey opens an age-encrypted snapshot with the identity in file (made by "xray graph keygen").