graph

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package graph provides observation relationship graphs for LEANN Phase 2.

This package implements graph-based selective recomputation where observation relationships (file overlap, semantic similarity, temporal proximity) form a graph structure. Hub nodes (high-degree observations) store embeddings, while leaf nodes recompute on-demand.

Index

Constants

View Source
const (
	// MinFileOverlapForEdge minimum file overlap ratio to create edge
	MinFileOverlapForEdge = 0.3

	// MaxEdgesPerNode prevents creating too many edges
	MaxEdgesPerNode = 20
)

Variables

View Source
var ErrGraphStoreNotConfigured = errors.New("graph store not configured")

ErrGraphStoreNotConfigured is returned by NoopGraphStore.Ping.

Functions

This section is empty.

Types

type AsyncGraphWriter

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

AsyncGraphWriter buffers relation edges and writes them to a GraphStore asynchronously. It never blocks the PostgreSQL write path.

func NewAsyncGraphWriter

func NewAsyncGraphWriter(store GraphStore) *AsyncGraphWriter

NewAsyncGraphWriter creates a writer that batches edges to the graph store.

func (*AsyncGraphWriter) Close

func (w *AsyncGraphWriter) Close()

Close drains the channel and waits for the writer goroutine to finish.

func (*AsyncGraphWriter) Enqueue

func (w *AsyncGraphWriter) Enqueue(relations []*models.ObservationRelation)

Enqueue adds edges derived from stored relations to the write buffer. Non-blocking: drops edges if the channel is full.

func (*AsyncGraphWriter) Stats

func (w *AsyncGraphWriter) Stats() (enqueued, written, dropped int64)

Stats returns write statistics.

type CSRGraph

type CSRGraph struct {
	RowPtr  []int32   // Node adjacency list pointers
	ColIdx  []int32   // Edge destination IDs
	Weights []float32 // Edge weights
	// contains filtered or unexported fields
}

CSRGraph represents a graph in Compressed Sparse Row format for memory efficiency

type Edge

type Edge struct {
	FromID   int64
	ToID     int64
	Relation RelationType
	Weight   float32 // 0.0-1.0, higher = stronger relationship
}

Edge represents a relationship between two observations

func DetectEdges

func DetectEdges(ctx context.Context, observations []*models.Observation) ([]Edge, error)

DetectEdges identifies relationships between observations

type GraphStats

type GraphStats struct {
	EdgeTypes    map[RelationType]int
	AvgDegree    float64
	MedianDegree float64
	NodeCount    int
	EdgeCount    int
	MaxDegree    int
	MinDegree    int
}

GraphStats contains graph statistics

type GraphStore

type GraphStore interface {
	// Ping checks connectivity to the graph backend.
	Ping(ctx context.Context) error

	// StoreEdge stores a single relation as a graph edge.
	StoreEdge(ctx context.Context, edge RelationEdge) error

	// StoreEdgesBatch stores multiple edges in a single operation.
	StoreEdgesBatch(ctx context.Context, edges []RelationEdge) error

	// GetNeighbors returns multi-hop neighbors of an observation.
	GetNeighbors(ctx context.Context, obsID int64, maxHops int, limit int) ([]Neighbor, error)

	// GetPath returns the shortest path between two observations as a list of IDs.
	GetPath(ctx context.Context, fromID, toID int64) ([]int64, error)

	// SyncFromRelations bulk-loads relations from PostgreSQL into the graph.
	SyncFromRelations(ctx context.Context, relations []*models.ObservationRelation) error

	// GetCluster returns observation IDs in the same cluster as the given node.
	// Uses BFS traversal up to maxNodes results.
	GetCluster(ctx context.Context, nodeID int64, maxNodes int) ([]int64, error)

	// Stats returns graph store statistics.
	Stats(ctx context.Context) (GraphStoreStats, error)

	// Close releases resources held by the graph store.
	Close() error
}

GraphStore provides persistent graph operations for observation relations. This interface uses models.RelationType (string), NOT graph.RelationType (int) which is an unrelated CSR-internal enum for in-memory edge detection.

func NewGraphStore

func NewGraphStore(cfg *config.Config) (GraphStore, error)

NewGraphStore creates a GraphStore based on configuration. Returns NoopGraphStore if no graph provider is configured.

type GraphStoreStats

type GraphStoreStats struct {
	NodeCount int
	EdgeCount int
	Provider  string
	Connected bool
}

GraphStoreStats contains graph backend statistics.

type Neighbor

type Neighbor struct {
	ObsID        int64
	Hops         int
	RelationType models.RelationType
}

Neighbor represents a graph neighbor found via multi-hop traversal.

type Node

type Node struct {
	Metadata    NodeMetadata
	LastAccess  time.Time
	StoredEmb   []float32 // Nil if recomputed on-demand
	ID          int64
	Degree      int // Number of edges (hub detection)
	AccessCount int
}

Node represents an observation in the graph

type NodeMetadata

type NodeMetadata struct {
	CreatedAt    time.Time
	Project      string
	Type         string
	Title        string
	IsSuperseded bool
}

NodeMetadata contains observation metadata

type NoopGraphStore

type NoopGraphStore struct{}

NoopGraphStore is a no-op implementation of GraphStore. It returns empty results for all queries and ErrGraphStoreNotConfigured for Ping. Used as fallback when no graph backend is configured.

func (*NoopGraphStore) Close

func (n *NoopGraphStore) Close() error

func (*NoopGraphStore) GetCluster added in v1.7.0

func (n *NoopGraphStore) GetCluster(_ context.Context, _ int64, _ int) ([]int64, error)

func (*NoopGraphStore) GetNeighbors

func (n *NoopGraphStore) GetNeighbors(_ context.Context, _ int64, _ int, _ int) ([]Neighbor, error)

func (*NoopGraphStore) GetPath

func (n *NoopGraphStore) GetPath(_ context.Context, _, _ int64) ([]int64, error)

func (*NoopGraphStore) Ping

func (n *NoopGraphStore) Ping(_ context.Context) error

func (*NoopGraphStore) Stats

func (*NoopGraphStore) StoreEdge

func (n *NoopGraphStore) StoreEdge(_ context.Context, _ RelationEdge) error

func (*NoopGraphStore) StoreEdgesBatch

func (n *NoopGraphStore) StoreEdgesBatch(_ context.Context, _ []RelationEdge) error

func (*NoopGraphStore) SyncFromRelations

func (n *NoopGraphStore) SyncFromRelations(_ context.Context, _ []*models.ObservationRelation) error

type ObservationGraph

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

ObservationGraph manages the observation relationship graph

func BuildFromObservations

func BuildFromObservations(ctx context.Context, observations []*models.Observation) (*ObservationGraph, error)

BuildFromObservations constructs a graph from a list of observations

func NewObservationGraph

func NewObservationGraph() *ObservationGraph

NewObservationGraph creates a new empty observation graph

func (*ObservationGraph) AddEdge

func (g *ObservationGraph) AddEdge(edge Edge)

AddEdge adds an edge to the graph

func (*ObservationGraph) AddNode

func (g *ObservationGraph) AddNode(node *Node)

AddNode adds or updates a node in the graph

func (*ObservationGraph) BuildCSR

func (g *ObservationGraph) BuildCSR() error

BuildCSR converts edge list to CSR format for efficient traversal

func (*ObservationGraph) FindHubs

func (g *ObservationGraph) FindHubs(percentile float64) []int64

FindHubs identifies hub nodes (high degree) in the graph

func (*ObservationGraph) GetNeighbors

func (g *ObservationGraph) GetNeighbors(nodeID int64) ([]int64, []float32, error)

GetNeighbors returns neighboring nodes and their edge weights

func (*ObservationGraph) GetNode

func (g *ObservationGraph) GetNode(nodeID int64) (*Node, error)

GetNode retrieves a node by ID

func (*ObservationGraph) Stats

func (g *ObservationGraph) Stats() GraphStats

Stats returns graph statistics

type RelationEdge

type RelationEdge struct {
	SourceID     int64
	TargetID     int64
	RelationType models.RelationType // string: "causes", "fixes", etc.
	Confidence   float64
}

RelationEdge represents a typed, weighted edge between two observations.

type RelationType

type RelationType int

RelationType defines the type of relationship between observations

const (
	// RelationFileOverlap indicates observations reference overlapping files
	RelationFileOverlap RelationType = iota
	// RelationSemantic indicates high semantic similarity (cosine > 0.85)
	RelationSemantic
	// RelationTemporal indicates observations from same session
	RelationTemporal
	// RelationConcept indicates shared concept tags
	RelationConcept
)

func (RelationType) String

func (r RelationType) String() string

String returns a human-readable representation of RelationType

Directories

Path Synopsis
Package falkordb implements graph.GraphStore against FalkorDB (Redis module).
Package falkordb implements graph.GraphStore against FalkorDB (Redis module).

Jump to

Keyboard shortcuts

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