graph

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 13 Imported by: 0 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 graph owns resource identity resolution and canonical hashing (GRAPH-001). It is deliberately dependency-light: it imports only the contract package, never orm, api, or configstore, so it can be used by every layer that must address a resource identically.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDepth = errors.New("graph: impact MaxDepth must be 1..20")

ErrInvalidDepth is a typed error for an out-of-range MaxDepth.

Functions

func CanonicalHash

func CanonicalHash(v any) string

CanonicalHash returns the SHA-256 hex digest of v marshalled to canonical JSON. Map keys are sorted recursively, struct fields follow declared order, and slices keep element order. The result is therefore stable across equivalent inputs that differ only in map key ordering.

func DescriptorHash

func DescriptorHash(d contract.ResourceDescriptor) string

DescriptorHash returns the canonical hash of a resource descriptor's content — namespace, name, version, kind, fields, and dependencies — excluding the Hash field and lifecycle timestamps. This is the value stored on ResourceDescriptor.Hash.

Types

type BoundedAnalyzer

type BoundedAnalyzer interface {
	ImpactPage(ctx context.Context, q ImpactQuery, maxNodes int, cursor string) (ImpactPage, error)
}

BoundedAnalyzer is the bounded, paginated impact surface (GRAPH-005). maxNodes caps the total dependent set; cursor resumes from a prior page offset.

func NewBoundedAnalyzer

func NewBoundedAnalyzer(inner ImpactAnalyzer) BoundedAnalyzer

NewBoundedAnalyzer returns a bounded analyzer over inner.

type CapabilityGraph

type CapabilityGraph interface {
	DeclareConsumer(consumer contract.ResourceRef, cap CapabilityID) error
	ConsumersOf(cap CapabilityID) ([]contract.ResourceRef, error)
	DegradeSet(cap CapabilityID) (ImpactResult, error)
}

CapabilityGraph records which resources consume which capabilities and computes the degrade set when a capability is removed or fails.

func NewCapabilityGraph

func NewCapabilityGraph(deps DependencyGraph) CapabilityGraph

NewCapabilityGraph returns a capability graph. deps supplies the resource dependency graph used to compute transitive degradation.

type CapabilityID

type CapabilityID struct {
	Namespace string
	Name      string
	Version   int
}

CapabilityID is the stable identity of a capability (GRAPH-004). Namespace is the capability category ("sms", "storage", "events"); it is not a tenant.

func (CapabilityID) String

func (c CapabilityID) String() string

type DependencyGraph

type DependencyGraph interface {
	AddEdge(ctx context.Context, e Edge) error
	EdgesFrom(ref contract.ResourceRef) ([]Edge, error)
	EdgesTo(ref contract.ResourceRef) ([]Edge, error)
	TopologicalOrder(namespace string) ([]contract.ResourceRef, error)
}

DependencyGraph is the queryable edge store for explainable dependencies (GRAPH-002). Implementations enforce cycle-free registration.

type DependentPath

type DependentPath struct {
	Resource contract.ResourceRef
	Path     []Edge
	Depth    int
}

DependentPath is one transitive dependent and the edge chain from the target to it. Path[i].From depends on Path[i].To; Path[0].To is the target's direct dependency edge target, so Path reads from the queried resource outward.

type Edge

type Edge struct {
	From contract.ResourceRef `json:"from"`
	To   contract.ResourceRef `json:"to"`
	Kind EdgeKind             `json:"kind"`
	Via  string               `json:"via,omitempty"`
}

Edge is one explainable dependency: From depends on To (declared via Via).

type EdgeKind

type EdgeKind string

EdgeKind classifies the relationship an edge represents. The value is stable and used for telemetry labels and impact classification.

const (
	EdgeLink        EdgeKind = "link"
	EdgeTable       EdgeKind = "table"
	EdgeComputed    EdgeKind = "computed"
	EdgeLinkedField EdgeKind = "linked_field"
	EdgeWorkflow    EdgeKind = "workflow"
	EdgeCapability  EdgeKind = "capability"
)

type ErrCycleDetected

type ErrCycleDetected struct {
	Path []contract.ResourceRef
}

ErrCycleDetected is a typed error returned when adding an edge would close a dependency cycle. Path carries the full cycle (From → … → From) so callers can explain the rejection without re-deriving it.

func (*ErrCycleDetected) Error

func (e *ErrCycleDetected) Error() string

type ErrQueryBudgetExceeded

type ErrQueryBudgetExceeded struct {
	Limit    int
	Observed int
}

ErrQueryBudgetExceeded is a typed overrun error carrying the limit and the observed node count.

func (*ErrQueryBudgetExceeded) Error

func (e *ErrQueryBudgetExceeded) Error() string

type ImpactAnalyzer

type ImpactAnalyzer interface {
	Impact(ctx context.Context, q ImpactQuery) (ImpactResult, error)
}

ImpactAnalyzer computes transitive dependency impact.

func NewImpactAnalyzer

func NewImpactAnalyzer(g DependencyGraph) ImpactAnalyzer

NewImpactAnalyzer returns an analyzer over g.

type ImpactPage

type ImpactPage struct {
	Dependents []DependentPath
	NextCursor string
	Truncated  bool
	Stats      QueryStats
}

ImpactPage is one cursor-paginated page of an impact query.

type ImpactQuery

type ImpactQuery struct {
	Target   contract.ResourceRef
	MaxDepth int        // required, 1..20
	Kinds    []EdgeKind // empty means all kinds
}

ImpactQuery asks "what transitively depends on Target, out to MaxDepth, via these edge kinds". It is the programmatic form of the destructive-change dependency report (GRAPH-003).

type ImpactResult

type ImpactResult struct {
	Dependents []DependentPath
	Truncated  bool // traversal hit MaxDepth with more nodes remaining
}

ImpactResult reports transitive dependents with explanatory paths.

type Memory

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

Memory is an in-memory, concurrency-safe ResourceRegistry (GRAPH-001). It enforces namespace isolation and monotonic per-(namespace, name) versioning. It is the reference implementation against which the SQL-backed registry (backed by _kora_resource) is contract-tested.

func NewMemory

func NewMemory() *Memory

NewMemory returns an empty in-memory registry.

func (*Memory) List

func (m *Memory) List(namespace string, kind contract.ResourceKind) ([]contract.ResourceDescriptor, error)

List returns all descriptors in namespace, optionally filtered by kind (an empty kind matches all kinds). Results are ordered by (name, version).

func (*Memory) Register

Register stores d under its namespaced identity. Re-registering an existing (namespace, name) bumps the version; re-registering the exact same (namespace, name, version) with an identical content hash is idempotent and returns the existing ref unchanged. A version collision with a different hash is a typed conflict.

func (*Memory) Resolve

Resolve returns the descriptor for ref. A Version of 0 resolves the latest registered version for that (namespace, name); otherwise the version must match exactly.

type MemoryGraph

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

MemoryGraph is an in-memory, concurrency-safe DependencyGraph (GRAPH-002). It rejects self- and mutual-dependency cycles with a path-bearing typed error and provides deterministic topological ordering (dependencies before dependents). It is the reference implementation for the SQL-backed edge store, mirroring Memory (GRAPH-001).

func NewMemoryGraph

func NewMemoryGraph() *MemoryGraph

NewMemoryGraph returns an empty in-memory dependency graph.

func (*MemoryGraph) AddEdge

func (g *MemoryGraph) AddEdge(ctx context.Context, e Edge) error

AddEdge registers e after proving it does not close a cycle. Adding an edge whose To can already reach From would create From → … → To → … → From.

func (*MemoryGraph) EdgesFrom

func (g *MemoryGraph) EdgesFrom(ref contract.ResourceRef) ([]Edge, error)

EdgesFrom returns all outgoing edges for ref (things ref depends on).

func (*MemoryGraph) EdgesTo

func (g *MemoryGraph) EdgesTo(ref contract.ResourceRef) ([]Edge, error)

EdgesTo returns all incoming edges for ref (things that depend on ref).

func (*MemoryGraph) TopologicalOrder

func (g *MemoryGraph) TopologicalOrder(namespace string) ([]contract.ResourceRef, error)

TopologicalOrder returns the resources in namespace in dependency order: dependencies (To) precede dependents (From). Deterministic for a fixed graph; ties break by resource name then version.

type QueryStats

type QueryStats struct {
	NodesVisited int
	DurationMs   int64
	CacheHit     bool
}

QueryStats reports the cost of an impact query (GRAPH-005).

Jump to

Keyboard shortcuts

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