Help us shape the future of Go! Take the 2026 Go Developer Survey

graph

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateCausalEdges

func CreateCausalEdges(db *store.DB, insight *model.Insight) (int, error)

CreateCausalEdges creates causal edges when either the new insight or a recent insight has causal signals and they share sufficient token overlap. Direction is inferred from which side has the causal keyword (MAGMA §3.3).

func CreateEntityEdges

func CreateEntityEdges(db *store.DB, insight *model.Insight) (int, error)

CreateEntityEdges creates entity co-occurrence edges between the new insight and existing insights that share the same entities.

func CreateSemanticEdges

func CreateSemanticEdges(db *store.DB, insight *model.Insight, embedCache EmbedCache) (int, error)

CreateSemanticEdges auto-creates semantic edges for insights with high embedding cosine similarity (MAGMA §3.2: cos(v_i, v_j) > θ_sim). If embedCache is non-nil, it is used instead of querying the database. Returns the number of edges created. Query and insert errors are returned so a surrounding SQL transaction can roll back.

func CreateTemporalEdge

func CreateTemporalEdge(db *store.DB, insight *model.Insight) (int, error)

CreateTemporalEdge creates a backbone temporal edge between the new insight and the most recent insight from the same source (MAGMA backbone chain), plus proximity edges to recent insights within a 24h window.

func ExtractEntities

func ExtractEntities(text string) []string

ExtractEntities extracts named entities from text using regex patterns and a technology dictionary for common terms regex would miss.

func ExtractEntitiesIndexed

func ExtractEntitiesIndexed(text string, knownEntities map[string]bool) []string

ExtractEntitiesIndexed extends ExtractEntities with a fourth extraction path: a wide-cast regex over capitalized tokens plus a tokenized scan over the rest of the text, whose candidates are admitted only when they appear in knownEntities. The index is used purely as a filter (never as a generator), so this stays read-only with respect to the entity store and never feeds its own output back into the index.

The fourth path exists because the default regex+dictionary paths short-circuit to an empty entity set on text whose only entities are single-segment CamelCase names (e.g., personal-project vocabulary) that are not in techDictionary. When such names already appear on prior insights — i.e., live in the entity index — the indexed extractor admits them on subsequent text without any dictionary edit.

When knownEntities is nil or empty, behavior is identical to ExtractEntities. Callers without DB access can pass nil to get the pre-existing semantics.

func HasCausalSignal

func HasCausalSignal(text string) bool

HasCausalSignal returns true if the text contains causal keywords.

func ResolveEntities

func ResolveEntities(content string, provided []string, mode EntityMode) []string

ResolveEntities returns the final entity list for a content/provided pair.

func ResolveEntitiesIndexed

func ResolveEntitiesIndexed(content string, provided []string, mode EntityMode, knownEntities map[string]bool) []string

ResolveEntitiesIndexed is the index-aware sibling of ResolveEntities. See ExtractEntitiesIndexed for the fourth-path semantics. A nil knownEntities map is identical to calling ResolveEntities.

func ValidEntityMode

func ValidEntityMode(mode EntityMode) bool

ValidEntityMode reports whether mode is a supported entity handling mode.

Types

type BFSNode

type BFSNode struct {
	Insight *model.Insight
	Hop     int
	ViaEdge *model.Edge
}

BFSNode represents a node discovered during BFS traversal.

func BFS

func BFS(db *store.DB, startID string, opts BFSOptions) []BFSNode

BFS performs breadth-first traversal from startID over the full graph. Pre-loads all active insights and edges to avoid N+1 queries. The start node is excluded from results. Only active (non-deleted) nodes are visited.

type BFSOptions

type BFSOptions struct {
	MaxDepth   int            // maximum hop distance from start
	MaxNodes   int            // maximum nodes to return (0 = unlimited)
	EdgeFilter model.EdgeType // filter by edge type (empty = all types)
}

BFSOptions controls BFS traversal behavior.

type CausalCandidate

type CausalCandidate struct {
	ID               string `json:"id"`
	Content          string `json:"content"`
	Category         string `json:"category"`
	Hop              int    `json:"hop"`                // graph distance (1 or 2)
	ViaEdge          string `json:"via_edge"`           // edge type that connected
	CausalSignal     string `json:"causal_signal"`      // keyword if found, else ""
	SuggestedSubType string `json:"suggested_sub_type"` // heuristic suggestion
}

CausalCandidate represents a potential causal link for Claude to evaluate.

func FindCausalCandidates

func FindCausalCandidates(db *store.DB, insight *model.Insight) []CausalCandidate

FindCausalCandidates returns insights that may have causal relationships with the given insight. Uses 2-hop BFS neighborhood traversal (MAGMA §3.3) to discover candidates through the existing graph structure, then annotates each with causal signal keywords as auxiliary labels for Claude to evaluate.

type EdgeStats

type EdgeStats struct {
	Temporal int `json:"temporal"`
	Entity   int `json:"entity"`
	Causal   int `json:"causal"`
	Semantic int `json:"semantic"`
}

EdgeStats tracks how many edges of each type were created.

type EmbedCache

type EmbedCache map[string][]float64

EmbedCache maps insight ID → deserialized embedding vector. When passed as nil, functions fall back to loading from the database.

type Engine

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

Engine orchestrates automatic edge creation when insights are stored.

func NewEngine

func NewEngine(db *store.DB, embedCache EmbedCache) *Engine

NewEngine creates a new graph edge engine. If embedCache is non-nil, it is reused for semantic operations instead of querying the database.

func NewEngineWithEntityMode

func NewEngineWithEntityMode(db *store.DB, embedCache EmbedCache, entityMode EntityMode) *Engine

NewEngineWithEntityMode creates a graph engine with configurable entity handling.

func NewEngineWithOptions

func NewEngineWithOptions(db *store.DB, embedCache EmbedCache, options EngineOptions) *Engine

NewEngineWithOptions creates a graph engine with explicit generation options.

func (*Engine) OnInsightCreated

func (e *Engine) OnInsightCreated(insight *model.Insight) (EdgeStats, error)

OnInsightCreated runs all edge generators for a newly created insight. It merges any pre-provided entities (e.g. from LLM) with regex-extracted ones. Query and insert errors are returned so a surrounding SQL transaction can roll back.

type EngineOptions

type EngineOptions struct {
	EntityMode   EntityMode
	TemporalMode TemporalMode
}

EngineOptions configures automatic edge generation.

type EntityMode

type EntityMode string

EntityMode controls how final insight entities are derived.

const (
	// EntityModeMerge keeps pre-provided entities and supplements them with auto extraction.
	EntityModeMerge EntityMode = "merge"
	// EntityModeProvided uses only pre-provided entities.
	EntityModeProvided EntityMode = "provided"
	// EntityModeAuto uses only auto-extracted entities.
	EntityModeAuto EntityMode = "auto"
)

type NeighborNode

type NeighborNode struct {
	ID       string `json:"id"`
	Content  string `json:"content"`
	Category string `json:"category"`
	Hop      int    `json:"hop"`      // 1 or 2
	ViaEdge  string `json:"via_edge"` // edge type that led here
}

NeighborNode represents a node discovered by BFS neighborhood traversal.

func GetNeighborhood

func GetNeighborhood(db *store.DB, nodeID string, maxHops int, maxNodes int) []NeighborNode

GetNeighborhood performs a BFS from nodeID up to maxHops, following all edge types (temporal, semantic, causal, entity). Returns up to maxNodes neighbor nodes, excluding the start node and soft-deleted nodes.

type SemanticCandidate

type SemanticCandidate struct {
	ID         string  `json:"id"`
	Content    string  `json:"content"`
	Category   string  `json:"category"`
	Similarity float64 `json:"similarity"`
	AutoLinked bool    `json:"auto_linked"`
}

SemanticCandidate represents a potential semantic link for Claude to evaluate. When AutoLinked is true, the edge was already created automatically (high confidence). When false, the candidate is in the review zone and needs LLM judgment.

func FindSemanticCandidates

func FindSemanticCandidates(db *store.DB, insight *model.Insight, embedCache EmbedCache) []SemanticCandidate

FindSemanticCandidates returns insights that are potential semantic matches for the given insight. When embeddings are available, uses cosine similarity (MAGMA-compliant); falls back to token overlap otherwise. If embedCache is non-nil, it is used instead of querying the database. These are candidates only — Claude evaluates and creates actual semantic edges via `mnemon link`.

type TemporalMode

type TemporalMode string

TemporalMode controls whether the engine creates temporal edges.

const (
	// TemporalEnabled creates temporal edges using the normal real-time write path.
	TemporalEnabled TemporalMode = "enabled"
	// TemporalDisabled skips temporal edges. Import paths can repair historical
	// temporal edges after all backdated insights are written.
	TemporalDisabled TemporalMode = "disabled"
)

Jump to

Keyboard shortcuts

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