relation

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package relation implements the interaction relation — the primary artifact of extraction (schema/SPEC.md). A flat typed relation of rows plus a companion node table; the algorithm graph and the site feed are pure deterministic projections; canonical form (sorted rows, SHA-256) is defined once, here.

The builder is the schema-discipline chokepoint: unknown kinds, undeclared or missing attributes, illegal enum values, ID collisions, case-only ID clashes, and rows referencing absent nodes are all hard errors. Nothing is coerced, defaulted, or skipped.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalFormOf

func CanonicalFormOf(nodes []Node, rows []Row) []byte

CanonicalFormOf renders an arbitrary node/row set canonically, sorting both (the inputs are not modified). The fix verifier uses this to compare span-masked variants, where masking changes the sort order.

Types

type Builder

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

Builder accumulates nodes and rows, validating each addition against the vocabulary. Insertion order is irrelevant: Build sorts everything into canonical order, and rows may be added before the nodes they reference (referential integrity is checked at Build).

func NewBuilder

func NewBuilder() *Builder

NewBuilder returns an empty builder.

func (*Builder) AddNode

func (b *Builder) AddNode(n Node) error

AddNode validates and inserts a node. Hard errors: unknown kind, invalid ID, attribute violations, ID collision, case-only ID clash.

func (*Builder) AddRow

func (b *Builder) AddRow(r Row) error

AddRow validates and appends a row. Hard errors: unknown kind, invalid IDs, empty file, inverted span, attribute violations. Referential integrity and src/dst kind constraints are checked at Build.

func (*Builder) Build

func (b *Builder) Build() (*Relation, error)

Build freezes the builder into a Relation. Hard errors: a row whose src or dst is not in the node table, or whose src/dst node kind violates the row kind's declared src_kinds/dst_kinds.

type Edge

type Edge struct {
	Src string
	Dst string
}

Edge is one algorithm-graph edge: a distinct (src, dst) pair, as serialized IDs.

type Node

type Node struct {
	Kind  vocab.NodeKind
	ID    NodeID
	Attrs map[string]Value
}

Node is one node-table entry: (node_kind, id, attrs).

type NodeID

type NodeID struct {
	// Lang is the profile prefix: "py" | "go" | "ts".
	Lang string
	// Member is the workspace member name, or "_" for a single-project
	// (non-workspace) scan.
	Member string
	// Module is the module's logical name (SPEC.md section 2.2).
	Module string
	// Chain is the container chain from module scope inward; empty for
	// module-kind nodes.
	Chain []Segment
}

NodeID is the hierarchical qualified name of a node (schema/SPEC.md section 2). Location is never part of identity. The serialized form is

<lang>:<member>:<module>:<container-chain>

Serialized IDs are opaque strings for consumers; the structured form is authoritative.

func (NodeID) String

func (id NodeID) String() string

String returns the serialized ID. Reserved characters inside a segment are percent-encoded (SPEC.md section 2.1 lists %, :, ., #, whitespace; the tilde is additionally escaped so the anonymous-segment structure <hint>~<ordinal>~<fp8> is unambiguous — see BUILDLOG.md).

func (NodeID) Validate

func (id NodeID) Validate() error

Validate checks the structural invariants of an ID. Extractors construct IDs; the relation builder rejects invalid ones as hard errors.

type Relation

type Relation struct {
	Nodes []Node
	Rows  []Row
}

Relation is the frozen, canonically ordered relation: the node table sorted by (node_kind, id) and the rows sorted by the total key (row_kind, src_id, dst_id, file, span_start, span_end, attr-tuple).

func (*Relation) AlgorithmGraph

func (rel *Relation) AlgorithmGraph(kind vocab.RowKind) []Edge

AlgorithmGraph projects the distinct (src, dst) pairs of one row kind, in canonical (sorted) order. Algorithms (Tarjan SCC, reachability) consume this; they never see spans or attributes.

func (*Relation) CanonicalForm

func (rel *Relation) CanonicalForm() []byte

CanonicalForm renders the relation deterministically: the version line, the sorted node table, then the sorted rows, one record per line, every free-form field percent-escaped. Equal relations produce equal bytes.

func (*Relation) Hash

func (rel *Relation) Hash() [32]byte

Hash is the SHA-256 of the canonical form — the one hash every projection inherits determinism from.

func (*Relation) SiteFeed

func (rel *Relation) SiteFeed(kind vocab.RowKind) []Row

SiteFeed projects the rows of one row kind with their spans and attributes, in canonical order — the input for findings and fixes.

type Row

type Row struct {
	Kind  vocab.RowKind
	Src   NodeID
	Dst   NodeID
	File  string
	Span  Span
	Attrs map[string]Value
}

Row is one interaction-relation row: (row_kind, src_node, dst_node, file, span, attrs).

type Segment

type Segment struct {
	// Name is the unit's name; for anonymous units it is the name hint, or
	// "anon" when no hint is syntactically derivable.
	Name string
	// Overload is the 0-based source-order index among same-name siblings in
	// the same container (SPEC.md section 2.4). Serialized as #<n>, omitted
	// for 0.
	Overload int
	// Anonymous marks a synthesized segment (SPEC.md section 2.3):
	// <name-hint|anon>~<ordinal>~<fp8>.
	Anonymous bool
	// Ordinal is the 0-based source-order index among same-hint anonymous
	// siblings in the same parent. Anonymous only.
	Ordinal int
	// Fingerprint is the first 8 hex chars of SHA-256 over the unit's
	// normalized signature text. Anonymous only. It exists so ordinal drift
	// is detectable, never silently misattributed.
	Fingerprint string
}

Segment is one element of the container chain.

type Span

type Span struct {
	Start uint32
	End   uint32
}

Span is a byte range over the file's LF-normalized UTF-8 bytes (schema/SPEC.md section 3). Line/column are derived at output time, never stored as truth.

type Value

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

Value is one typed attribute value. Enum-typed attributes carry their value as a string; legality against the vocabulary enum is checked by the relation builder.

func BoolValue

func BoolValue(b bool) Value

BoolValue constructs a boolean value.

func IntValue

func IntValue(i int64) Value

IntValue constructs an integer value.

func StringValue

func StringValue(s string) Value

StringValue constructs a string (or enum) value.

func (Value) AsBool

func (v Value) AsBool() (bool, bool)

AsBool returns the boolean content; the boolean is false for non-booleans.

func (Value) AsInt

func (v Value) AsInt() (int64, bool)

AsInt returns the integer content; the boolean is false for non-integers.

func (Value) AsString

func (v Value) AsString() (string, bool)

AsString returns the string content; the boolean is false for non-strings.

func (Value) Kind

func (v Value) Kind() ValueKind

Kind returns the value's type.

type ValueKind

type ValueKind int

ValueKind is the type of an attribute value.

const (
	ValueString ValueKind = iota
	ValueInt
	ValueBool
)

Jump to

Keyboard shortcuts

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