graph

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 5 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 implements the AgentGraph normalized node and edge models and the in-memory graph store.

Index

Constants

This section is empty.

Variables

AllEdgeTypes is the canonical list of edge types.

AllNodeTypes is the canonical ordered list of node types.

DefaultEdgeRisk maps edge types to intrinsic risk (0-100) when an edge does not carry an explicit risk. Dangerous capabilities are rated according to the PRD's dangerous-capability table.

Functions

func IsValidEdgeType

func IsValidEdgeType(t EdgeType) bool

IsValidEdgeType reports whether t is a known edge type.

func IsValidNodeType

func IsValidNodeType(t NodeType) bool

IsValidNodeType reports whether t is a known node type.

func PrivilegeRank

func PrivilegeRank(level string) int

PrivilegeRank returns an ordered rank for a privilege level string.

func SaveSnapshotFile added in v0.2.0

func SaveSnapshotFile(g *Graph, path string) error

SaveSnapshotFile writes the graph to a JSON file.

Types

type Assembler added in v0.2.0

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

Assembler collects nodes and edges from multiple sources (YAML config, connectors) and builds a single graph. Edge endpoints may be declared in any order: validation happens once, at Build time. This lets a static config reference node IDs that a connector will discover later.

func NewAssembler added in v0.2.0

func NewAssembler() *Assembler

NewAssembler returns an empty assembler.

func (*Assembler) AddEdge added in v0.2.0

func (a *Assembler) AddEdge(e *Edge)

AddEdge registers an edge for deferred validation.

func (*Assembler) AddNode added in v0.2.0

func (a *Assembler) AddNode(n *Node) error

AddNode registers a node. Later registrations for the same ID update the existing node (connector metadata can enrich a config-declared node).

func (*Assembler) Build added in v0.2.0

func (a *Assembler) Build() (*Graph, error)

Build validates all collected relationships and constructs the graph.

func (*Assembler) EdgeCount added in v0.2.0

func (a *Assembler) EdgeCount() int

EdgeCount returns the number of edges registered so far.

func (*Assembler) NodeCount added in v0.2.0

func (a *Assembler) NodeCount() int

NodeCount returns the number of distinct nodes registered so far.

type Edge

type Edge struct {
	Source       string         `json:"source" yaml:"source"`
	Target       string         `json:"target" yaml:"target"`
	Type         EdgeType       `json:"type" yaml:"type"`
	Confidence   float64        `json:"confidence" yaml:"confidence"`
	Risk         int            `json:"risk,omitempty" yaml:"risk,omitempty"`
	Metadata     map[string]any `json:"metadata,omitempty" yaml:"metadata,omitempty"`
	Provenance   Provenance     `json:"provenance,omitempty" yaml:"provenance,omitempty"`
	FirstSeen    time.Time      `json:"first_seen,omitempty" yaml:"first_seen,omitempty"`
	LastSeen     time.Time      `json:"last_seen,omitempty" yaml:"last_seen,omitempty"`
	LastVerified time.Time      `json:"last_verified,omitempty" yaml:"last_verified,omitempty"`
}

Edge is the normalized graph edge (PRD section 36) with provenance (section 41) and freshness fields (section 42).

func (*Edge) EffectiveRisk

func (e *Edge) EffectiveRisk() int

EffectiveRisk returns the edge risk, falling back to the intrinsic default risk of its type.

func (*Edge) Validate

func (e *Edge) Validate() error

Validate checks edge invariants.

type EdgeType

type EdgeType string

EdgeType enumerates relationship categories between nodes.

const (
	EdgeUses            EdgeType = "USES"
	EdgeCanCall         EdgeType = "CAN_CALL"
	EdgeHasPermission   EdgeType = "HAS_PERMISSION"
	EdgeCanRead         EdgeType = "CAN_READ"
	EdgeCanWrite        EdgeType = "CAN_WRITE"
	EdgeCanExecute      EdgeType = "CAN_EXECUTE"
	EdgeCanModify       EdgeType = "CAN_MODIFY"
	EdgeCanAssume       EdgeType = "CAN_ASSUME"
	EdgeCanImpersonate  EdgeType = "CAN_IMPERSONATE"
	EdgeAuthenticatesAs EdgeType = "AUTHENTICATES_AS"
	EdgeHasSecret       EdgeType = "HAS_SECRET"
	EdgeContainsSecret  EdgeType = "CONTAINS_SECRET"
	EdgeCanAccess       EdgeType = "CAN_ACCESS"
	EdgeCanAdmin        EdgeType = "CAN_ADMIN"
	EdgeDeploysTo       EdgeType = "DEPLOYS_TO"
	EdgeTriggers        EdgeType = "TRIGGERS"
	EdgeOwns            EdgeType = "OWNS"
	EdgeMemberOf        EdgeType = "MEMBER_OF"
	EdgeConnectedTo     EdgeType = "CONNECTED_TO"
	EdgeTrusts          EdgeType = "TRUSTS"
	EdgeExposedTo       EdgeType = "EXPOSED_TO"
)

type Graph

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

Graph is an in-memory directed multigraph with adjacency indexes. It is safe for read-only concurrent use after construction.

func FromSnapshot added in v0.2.0

func FromSnapshot(s *Snapshot) (*Graph, error)

FromSnapshot builds a graph from a snapshot.

func LoadSnapshotFile added in v0.2.0

func LoadSnapshotFile(path string) (*Graph, error)

LoadSnapshotFile reads a graph from a JSON file.

func New

func New() *Graph

New returns an empty graph.

func (*Graph) AddEdge

func (g *Graph) AddEdge(e *Edge) error

AddEdge validates and inserts an edge. Both endpoints must already exist. Duplicate edges (same source, target, and type) are ignored so that repeated connector runs are idempotent.

func (*Graph) AddNode

func (g *Graph) AddNode(n *Node) error

AddNode inserts a node. Adding a duplicate ID returns an error to prevent silent overwrites between connectors.

func (*Graph) EdgeBetween

func (g *Graph) EdgeBetween(source, target string) []*Edge

EdgeBetween returns the edges from source to target, if any.

func (*Graph) EdgeCount

func (g *Graph) EdgeCount() int

EdgeCount returns the number of edges.

func (*Graph) Edges

func (g *Graph) Edges() []*Edge

Edges returns all edges in insertion order.

func (*Graph) HasEdge

func (g *Graph) HasEdge(source, target string, t EdgeType) bool

HasEdge reports whether an edge of the given type exists.

func (*Graph) InEdges

func (g *Graph) InEdges(id string) []*Edge

InEdges returns all edges entering a node.

func (*Graph) MarshalJSON added in v0.2.0

func (g *Graph) MarshalJSON() ([]byte, error)

MarshalJSON renders the graph as a snapshot.

func (*Graph) Node

func (g *Graph) Node(id string) (*Node, bool)

Node returns the node with the given ID.

func (*Graph) NodeCount

func (g *Graph) NodeCount() int

NodeCount returns the number of nodes.

func (*Graph) Nodes

func (g *Graph) Nodes() []*Node

Nodes returns all nodes sorted by ID for deterministic output.

func (*Graph) NodesByType

func (g *Graph) NodesByType(t NodeType) []*Node

NodesByType returns all nodes of a given type sorted by ID.

func (*Graph) OutEdges

func (g *Graph) OutEdges(id string) []*Edge

OutEdges returns all edges leaving a node.

func (*Graph) RemoveEdge

func (g *Graph) RemoveEdge(source, target string, t EdgeType)

RemoveEdge removes the edge from source to target with the given type.

func (*Graph) Snapshot added in v0.2.0

func (g *Graph) Snapshot() *Snapshot

Snapshot returns the graph in serializable form.

func (*Graph) UpsertNode

func (g *Graph) UpsertNode(n *Node) error

UpsertNode inserts a node or replaces an existing one with the same ID.

type Node

type Node struct {
	ID          string         `json:"id" yaml:"id"`
	Type        NodeType       `json:"type" yaml:"type"`
	Name        string         `json:"name" yaml:"name"`
	Provider    string         `json:"provider,omitempty" yaml:"provider,omitempty"`
	Criticality int            `json:"criticality,omitempty" yaml:"criticality,omitempty"`
	CrownJewel  bool           `json:"crown_jewel,omitempty" yaml:"crown_jewel,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty" yaml:"metadata,omitempty"`
}

Node is the normalized graph node (PRD section 35).

func (*Node) Validate

func (n *Node) Validate() error

Validate checks node invariants.

type NodeType

type NodeType string

NodeType enumerates all node categories modeled by AgentGraph.

const (
	NodeAIAgent       NodeType = "AI_AGENT"
	NodeMCPServer     NodeType = "MCP_SERVER"
	NodeTool          NodeType = "TOOL"
	NodeIdentity      NodeType = "IDENTITY"
	NodeSecret        NodeType = "SECRET"
	NodeRepository    NodeType = "REPOSITORY"
	NodeCIPipeline    NodeType = "CI_PIPELINE"
	NodeCloudRole     NodeType = "CLOUD_ROLE"
	NodeCloudResource NodeType = "CLOUD_RESOURCE"
	NodeDatabase      NodeType = "DATABASE"
	NodeHost          NodeType = "HOST"
	NodeAPI           NodeType = "API"
	NodeDataset       NodeType = "DATASET"
)

type Provenance

type Provenance struct {
	Connector    string    `json:"connector,omitempty" yaml:"connector,omitempty"`
	ObservedAt   time.Time `json:"observed_at,omitempty" yaml:"observed_at,omitempty"`
	SourceObject string    `json:"source_object,omitempty" yaml:"source_object,omitempty"`
}

Provenance records where a relationship was observed.

type Snapshot added in v0.2.0

type Snapshot struct {
	Nodes []*Node `json:"nodes"`
	Edges []*Edge `json:"edges"`
}

Snapshot is the JSON representation of a complete graph, used for --save/--graph persistence and the web API.

Jump to

Keyboard shortcuts

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