Documentation
¶
Overview ¶
Package graph provides a knowledge graph layer for instinct relationships, dependency tracking, and graph-based queries using in-memory structures. It replaces the shell's jq-based graph layer (graph.sh) with O(1) lookups via adjacency lists, supporting 5 node types and 16 edge types.
Index ¶
- type Edge
- type EdgeType
- type Graph
- func (g *Graph) AddEdge(source, target string, relType EdgeType, weight float64, createdAt string) (*Edge, string, error)
- func (g *Graph) AddNode(node Node) error
- func (g *Graph) DetectCycles() [][]string
- func (g *Graph) EdgeCount() int
- func (g *Graph) Export() (*JSONFile, error)
- func (g *Graph) ExportJSON() ([]byte, error)
- func (g *Graph) GetNode(id string) *Node
- func (g *Graph) Import(jf *JSONFile) error
- func (g *Graph) ImportJSON(data []byte) error
- func (g *Graph) LoadFromStore(store *storage.Store, path string) error
- func (g *Graph) Neighbors(nodeID string, direction string, filterRel EdgeType, minWeight float64) (*NeighborsResult, error)
- func (g *Graph) Neighbors2Hop(nodeID string, direction string, filterRel EdgeType, minWeight float64) ([]Neighbor, error)
- func (g *Graph) NodeCount() int
- func (g *Graph) NodesByType(nodeType NodeType) []*Node
- func (g *Graph) Reach(startID string, maxHops int, minWeight float64) (*ReachResult, error)
- func (g *Graph) RemoveEdge(source, target string, relType EdgeType) error
- func (g *Graph) RemoveNode(id string) error
- func (g *Graph) SaveToStore(store *storage.Store, path string) error
- func (g *Graph) ShortestPath(from, to string) ([]string, error)
- type JSONFile
- type Neighbor
- type NeighborsResult
- type Node
- type NodeType
- type ReachNode
- type ReachResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Edge ¶
type Edge struct {
ID string `json:"edge_id"`
Source string `json:"source"`
Target string `json:"target"`
Relationship EdgeType `json:"relationship"`
Weight float64 `json:"weight"`
CreatedAt string `json:"created_at"`
}
Edge represents a directed relationship between two nodes. JSON field names match the shell graph.sh format exactly.
type EdgeType ¶
type EdgeType string
EdgeType represents the kind of relationship between two nodes.
const ( // Learning-to-Instinct edges EdgePromotedFrom EdgeType = "promoted_from" EdgeDerivedFrom EdgeType = "derived_from" // Instinct-to-Instinct edges (matching shell graph.sh) EdgeReinforces EdgeType = "reinforces" EdgeContradicts EdgeType = "contradicts" EdgeExtends EdgeType = "extends" EdgeSupersedes EdgeType = "supersedes" EdgeRelated EdgeType = "related" // Instinct-to-Queen edges EdgePromotedTo EdgeType = "promoted_to" // Phase-to-Learning/Instinct edges EdgeProduced EdgeType = "produced" EdgeOriginated EdgeType = "originated" // Colony-to-Phase edges EdgeContains EdgeType = "contains" EdgeColonyInstinct EdgeType = "colony_instinct" // Cross-type edges EdgeDependsOn EdgeType = "depends_on" EdgeInfluenced EdgeType = "influenced" EdgeConflictsWith EdgeType = "conflicts_with" EdgeSupersedesPhase EdgeType = "supersedes_phase" )
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
Graph is an in-memory directed graph with typed nodes and edges. All operations are goroutine-safe via sync.RWMutex.
func (*Graph) AddEdge ¶
func (g *Graph) AddEdge(source, target string, relType EdgeType, weight float64, createdAt string) (*Edge, string, error)
AddEdge creates a directed edge between source and target nodes. If source or target nodes do not exist, they are auto-created with inferred types. If an edge with the same source+target+relationship already exists, the weight is updated. Returns the edge, a status string ("created" or "updated"), and any error.
func (*Graph) AddNode ¶
AddNode adds a node to the graph. Returns an error if a node with the same ID but different type exists. Adding a node with the same ID and same type is idempotent (no error).
func (*Graph) DetectCycles ¶
DetectCycles finds all directed cycles in the graph using DFS. Each cycle is represented as a slice of node IDs forming the cycle. A self-loop (a -> a) is reported as [a].
func (*Graph) ExportJSON ¶
ExportJSON serializes the graph to JSON bytes.
func (*Graph) Import ¶
Import loads nodes and edges from a JSONFile into the graph. Existing graph data is replaced.
func (*Graph) ImportJSON ¶
ImportJSON deserializes the graph from JSON bytes.
func (*Graph) LoadFromStore ¶
LoadFromStore loads the graph from a JSON file via storage.Store. Supports both Go format (with nodes) and shell format (edges only). Shell format auto-creates nodes from edge source/target with inferred types.
func (*Graph) Neighbors ¶
func (g *Graph) Neighbors(nodeID string, direction string, filterRel EdgeType, minWeight float64) (*NeighborsResult, error)
Neighbors returns nodes connected to nodeID with the given direction filter. direction can be "out", "in", or "both". filterRel filters by relationship type (empty string means no filter). minWeight excludes edges with weight below the threshold (0 means no filter). Results are sorted by ID for deterministic output.
func (*Graph) Neighbors2Hop ¶
func (g *Graph) Neighbors2Hop(nodeID string, direction string, filterRel EdgeType, minWeight float64) ([]Neighbor, error)
Neighbors2Hop returns unique nodes reachable within 2 hops from nodeID. Results are sorted by ID for deterministic output and tagged with hop count.
func (*Graph) NodesByType ¶
NodesByType returns all nodes of the given type.
func (*Graph) Reach ¶
Reach returns all nodes reachable from startID within maxHops using BFS. maxHops is clamped to 3 (matching shell MAX_HOPS=3). minWeight excludes edges with weight below the threshold (0 means no filter). The start node is NOT included in results (matching shell behavior).
func (*Graph) RemoveEdge ¶
RemoveEdge removes a single edge by source, target, and relationship type.
func (*Graph) RemoveNode ¶
RemoveNode removes a node and all edges connected to it (both directions).
func (*Graph) SaveToStore ¶
SaveToStore saves the graph to a JSON file via storage.Store. Uses atomic write via store.SaveJSON for crash safety.
type JSONFile ¶
type JSONFile struct {
Version string `json:"version"`
Nodes []Node `json:"nodes"`
Edges []Edge `json:"edges"`
}
JSONFile represents the graph in the shell-compatible JSON format.
type Neighbor ¶
type Neighbor struct {
ID string `json:"id"`
Relationship EdgeType `json:"relationship"`
Weight float64 `json:"weight"`
Direction string `json:"direction"` // "out" or "in"
Hop int `json:"hop,omitempty"`
}
Neighbor represents a node connected to a query node.
type NeighborsResult ¶
NeighborsResult holds the result of a neighbor query.
type ReachResult ¶
type ReachResult struct {
Reachable []ReachNode `json:"reachable"`
Count int `json:"count"`
HopsSearched int `json:"hops_searched"`
}
ReachResult holds the result of a reachability query.