graph

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package graph provides graph traversal algorithms for code analysis.

This package implements callers/callees/path queries using BFS traversal with support for depth limits, cycle detection, and multiple output formats.

Architecture

┌─────────────────────────────────────────────────────────────────────────┐
│                      Graph Query Flow                                    │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐                  │
│  │ Symbol      │───▶│   Resolve   │───▶│   BFS       │                  │
│  │ Input       │    │   Symbol    │    │  Traversal  │                  │
│  └─────────────┘    └─────────────┘    └─────────────┘                  │
│                                               │                          │
│                                               ▼                          │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐                  │
│  │  Output     │◀───│  Format     │◀───│  Results    │                  │
│  │  (tree/json)│    │  Results    │    │  (paths)    │                  │
│  └─────────────┘    └─────────────┘    └─────────────┘                  │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘

Thread Safety

Query operations are safe for concurrent use with the same MemoryIndex.

Index

Constants

View Source
const (
	ExitSuccess = 0 // Query successful (even if no results)
	ExitError   = 1 // Error (no index, invalid symbol, etc.)
	ExitBadArgs = 2 // Invalid arguments
)

Exit codes for graph commands.

View Source
const (
	DefaultMaxDepth   = 50
	DefaultMaxResults = 1000
	DefaultTimeout    = 10 // seconds
)

Default limits.

View Source
const APIVersion = "1.0"

API version for JSON output.

Variables

View Source
var (
	// Index errors
	ErrIndexNotFound = errors.New("index not found: run 'aleutian init' first")
	ErrIndexStale    = errors.New("index is stale: consider running 'aleutian init'")

	// Symbol errors
	ErrSymbolNotFound  = errors.New("symbol not found")
	ErrSymbolAmbiguous = errors.New("symbol is ambiguous")

	// Query errors
	ErrNoResults     = errors.New("no results found")
	ErrDepthExceeded = errors.New("maximum depth exceeded")
	ErrTimeout       = errors.New("query timed out")
)

Sentinel errors for graph queries.

Functions

This section is empty.

Types

type AmbiguousSymbolError

type AmbiguousSymbolError struct {
	Input   string
	Matches []SymbolMatch
}

AmbiguousSymbolError provides details about ambiguous symbol matches.

func (*AmbiguousSymbolError) Error

func (e *AmbiguousSymbolError) Error() string

Error implements the error interface.

func (*AmbiguousSymbolError) Unwrap

func (e *AmbiguousSymbolError) Unwrap() error

Unwrap returns the sentinel error.

type CallResult

type CallResult struct {
	// Symbol information
	SymbolID   string `json:"symbol_id"`
	SymbolName string `json:"symbol_name"`
	Kind       string `json:"kind"`
	FilePath   string `json:"file_path"`
	Line       int    `json:"line"`

	// Depth from the query target (1 = direct caller/callee).
	Depth int `json:"depth"`

	// Path from root to this symbol (for tree output).
	Path []string `json:"path,omitempty"`
}

CallResult represents a single caller or callee result.

type OutputFormat

type OutputFormat string

OutputFormat specifies the output format for query results.

const (
	FormatTree    OutputFormat = "tree"
	FormatFlat    OutputFormat = "flat"
	FormatPaths   OutputFormat = "paths"
	FormatColumns OutputFormat = "columns"
)

type PathNode

type PathNode struct {
	SymbolID   string `json:"symbol_id"`
	SymbolName string `json:"symbol_name"`
	FilePath   string `json:"file_path"`
	Line       int    `json:"line"`
}

PathNode represents a symbol in a path.

type PathQueryResult

type PathQueryResult struct {
	APIVersion string `json:"api_version"`
	Query      string `json:"query"`
	From       string `json:"from"`
	To         string `json:"to"`

	// Paths found (may be multiple if --all)
	Paths []PathResult `json:"paths"`

	// Stats
	PathFound bool `json:"path_found"`
	PathCount int  `json:"path_count"`
	Truncated bool `json:"truncated,omitempty"`
	MaxDepth  int  `json:"max_depth"`

	// Errors/warnings
	Warnings []string `json:"warnings,omitempty"`
}

PathQueryResult holds results of a path query.

func NewPathQueryResult

func NewPathQueryResult(from, to string) *PathQueryResult

NewPathQueryResult creates a new PathQueryResult with defaults.

type PathResult

type PathResult struct {
	// Symbols in the path, from source to target.
	Symbols []PathNode `json:"symbols"`

	// Length of the path (number of hops).
	Length int `json:"length"`
}

PathResult represents a path between two symbols.

type Querier

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

Querier provides graph query operations.

Description

Querier wraps a MemoryIndex and provides callers/callees/path queries using BFS traversal with depth limits and cycle detection.

Thread Safety

Querier is safe for concurrent use.

func NewQuerier

func NewQuerier(index *initializer.MemoryIndex) *Querier

NewQuerier creates a new Querier with the given index.

Description

Creates a Querier for executing graph queries against the provided index.

Inputs

  • index: The MemoryIndex to query. Must not be nil.

Outputs

  • *Querier: The querier instance.

Thread Safety

The returned Querier is safe for concurrent use.

func (*Querier) FindCallees

func (q *Querier) FindCallees(ctx context.Context, symbolInput string, cfg QueryConfig) (*QueryResult, error)

FindCallees finds all symbols called by the target symbol.

Description

Uses BFS traversal to find all callees up to the specified depth. Direct callees are at depth 1, their callees at depth 2, etc.

Inputs

  • ctx: Context for cancellation. Must not be nil.
  • symbolInput: Symbol name/ID/file:line to find callees for.
  • cfg: Query configuration.

Outputs

  • *QueryResult: Query results with callees.
  • error: Non-nil if symbol resolution fails.

Thread Safety

Safe for concurrent use.

func (*Querier) FindCallers

func (q *Querier) FindCallers(ctx context.Context, symbolInput string, cfg QueryConfig) (*QueryResult, error)

FindCallers finds all symbols that call the target symbol.

Description

Uses BFS traversal to find all callers up to the specified depth. Direct callers are at depth 1, their callers at depth 2, etc.

Inputs

  • ctx: Context for cancellation. Must not be nil.
  • symbolInput: Symbol name/ID/file:line to find callers for.
  • cfg: Query configuration.

Outputs

  • *QueryResult: Query results with callers.
  • error: Non-nil if symbol resolution fails.

Thread Safety

Safe for concurrent use.

func (*Querier) FindPath

func (q *Querier) FindPath(ctx context.Context, fromInput, toInput string, cfg QueryConfig, findAll bool, maxPaths int) (*PathQueryResult, error)

FindPath finds paths between two symbols using bidirectional BFS.

Description

Uses bidirectional BFS for efficient path finding. Expands from both the source and target simultaneously until they meet.

Inputs

  • ctx: Context for cancellation. Must not be nil.
  • fromInput: Source symbol name/ID.
  • toInput: Target symbol name/ID.
  • cfg: Query configuration.
  • findAll: If true, find all paths (not just shortest).
  • maxPaths: Maximum number of paths to return.

Outputs

  • *PathQueryResult: Query results with paths.
  • error: Non-nil if symbol resolution fails.

Thread Safety

Safe for concurrent use.

type QueryConfig

type QueryConfig struct {
	// MaxDepth limits traversal depth. 0 = unlimited (up to DefaultMaxDepth).
	MaxDepth int

	// MaxResults limits number of results. 0 = unlimited (up to DefaultMaxResults).
	MaxResults int

	// IncludeTests includes test files in results.
	IncludeTests bool

	// IncludeStdlib includes standard library calls.
	IncludeStdlib bool

	// Exact requires exact symbol match (no fuzzy).
	Exact bool

	// FailIfEmpty returns error if no results found.
	FailIfEmpty bool
}

QueryConfig holds configuration for graph queries.

func DefaultQueryConfig

func DefaultQueryConfig() QueryConfig

DefaultQueryConfig returns configuration with sensible defaults.

type QueryResult

type QueryResult struct {
	APIVersion string `json:"api_version"`
	Query      string `json:"query"`
	Symbol     string `json:"symbol"`

	// Results
	Results []CallResult `json:"results"`

	// Counts
	DirectCount     int `json:"direct_count"`
	TransitiveCount int `json:"transitive_count"`
	TotalCount      int `json:"total_count"`

	// Query info
	MaxDepthUsed int  `json:"max_depth_used"`
	Truncated    bool `json:"truncated,omitempty"`

	// Errors/warnings
	Warnings []string `json:"warnings,omitempty"`
}

QueryResult holds the results of a graph query.

func NewQueryResult

func NewQueryResult(query, symbol string) *QueryResult

NewQueryResult creates a new QueryResult with defaults.

type SymbolMatch

type SymbolMatch struct {
	ID       string
	Name     string
	FilePath string
	Line     int
}

SymbolMatch represents a potential symbol match.

type SymbolNotFoundError

type SymbolNotFoundError struct {
	Input       string
	Suggestions []string
}

SymbolNotFoundError provides details about a missing symbol.

func (*SymbolNotFoundError) Error

func (e *SymbolNotFoundError) Error() string

Error implements the error interface.

func (*SymbolNotFoundError) Unwrap

func (e *SymbolNotFoundError) Unwrap() error

Unwrap returns the sentinel error.

type SymbolResolver

type SymbolResolver interface {
	// ResolveSymbol resolves a symbol input to a Symbol.
	// Returns nil if not found, error on ambiguous match.
	ResolveSymbol(input string, exact bool) (*initializer.Symbol, error)
}

SymbolResolver resolves symbol names to Symbol objects.

Jump to

Keyboard shortcuts

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