Files
zeekayandHanzo Dev 1cd0cd7644 semantic: the pipeline, end to end
The first commit was the shape. This is the implementation: every stage of the
pipeline, the graph and its measures, the type system over it, entailment,
deduplication and conflict resolution, the decision layer, the stores and the
export formats. Twenty-six thousand lines of Go and sixteen thousand of tests,
against no dependency but the standard library.

Where the Python reaches for torch, spacy or a database driver, the Go states
an interface and reports what is missing rather than pretending. A caller can
tell "this build cannot serve that" from "nobody has heard of it", which is the
difference between a port and a stub. The deterministic paths are complete and
are what the tests exercise; the model-shaped ones are one adapter away.

Porting a bug is not parity, so several places now disagree with the original
on purpose. The JSON repair that closed a truncated reply counted each bracket
kind separately and closed them backwards on the one input it exists for; it
now works off a stack and ignores brackets inside strings. The entity patterns
joined words with \s, so a heading ran into the sentence beneath it and became
one name while the real relation was lost; a name is separated by horizontal
space now. Eigenvector centrality ran plain power iteration, which on a
bipartite graph swings between the two ends of a sign-paired eigenvalue forever
and reported every node in a star as equally central; it iterates with A+I,
which moves no eigenvector and settles. Similarity summed its weighted parts in
map order, and float addition is not associative, so the same comparison scored
differently between runs and duplicate detection returned a different answer
each time. An inferred ontology named object-property endpoints by raw source
type and data-property endpoints by class, so the schema referred to classes it
did not contain. A property with no IRI of its own was minted as a class and
came back Pascal-cased, renaming the term on the way out of the document.

The command built a pipeline with every stage nil and panicked on each
invocation. It is now the composition the package documents, and Run reports a
missing ingester instead of dereferencing it.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-08 12:57:59 -07:00

114 lines
2.9 KiB
Go

package kg
import (
"context"
"fmt"
"sync"
"github.com/hanzoai/semantic"
"github.com/hanzoai/semantic/store"
)
// Mem keeps a graph in memory and serves it through store.Graph and
// store.Triple, so a caller with no database still has somewhere to put a
// graph. Writes go through the same merge as Add: the same assertion twice is
// one edge. It is safe for concurrent use, and its zero value is an empty
// store ready to write to.
type Mem struct {
mu sync.RWMutex
g *Graph
}
var (
_ store.Graph = (*Mem)(nil)
_ store.Triple = (*Mem)(nil)
)
// Keep serves an already built graph. Mem takes it over: later writes through
// Mem change it, so a caller that wants the original untouched passes a Sub of
// everything.
func Keep(g *Graph) *Mem { return &Mem{g: g} }
// Graph is the graph Mem holds, for the measures in this package. It is the
// live graph, not a copy, and reading it while another goroutine writes is
// the caller's business to order.
func (m *Mem) Graph() *Graph {
m.mu.Lock()
defer m.mu.Unlock()
return m.graph()
}
func (m *Mem) graph() *Graph {
if m.g == nil {
m.g = Build(nil)
}
return m.g
}
// Node records a node and its properties.
func (m *Mem) Node(ctx context.Context, id string, props map[string]any) error {
if err := ctx.Err(); err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
if !m.graph().Set(id, props) {
return fmt.Errorf("kg: node %q has no name", id)
}
return nil
}
// Edge records a relation between two nodes, creating either that the graph
// has not heard of.
func (m *Mem) Edge(ctx context.Context, from, to, label string) error {
return m.Assert(ctx, from, label, to)
}
// Out lists the nodes an edge points at from this one.
func (m *Mem) Out(ctx context.Context, id string) ([]string, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
m.mu.RLock()
defer m.mu.RUnlock()
if m.g == nil {
return nil, nil
}
return m.g.Out(id), nil
}
// Assert records one triple.
func (m *Mem) Assert(ctx context.Context, s, p, o string) error {
if err := ctx.Err(); err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
if !m.graph().Add(semantic.Triple{Subject: s, Predicate: p, Object: o}) {
return fmt.Errorf("kg: incomplete triple (%q, %q, %q)", s, p, o)
}
return nil
}
// Match returns the asserted triples matching a pattern, first asserted
// first. An empty subject, predicate or object stands for any, so Match with
// three empty strings returns everything.
func (m *Mem) Match(ctx context.Context, s, p, o string) ([][3]string, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
m.mu.RLock()
defer m.mu.RUnlock()
if m.g == nil {
return nil, nil
}
s, p, o = Fold(s), Fold(p), Fold(o)
var out [][3]string
for _, k := range m.g.eord {
if (s == "" || k.from == s) && (p == "" || k.label == p) && (o == "" || k.to == o) {
out = append(out, [3]string{k.from, m.g.edge[k].Label, k.to})
}
}
return out, nil
}