stage

package
v0.0.0-...-dfadc02 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package stage defines the classification contract. A pipeline is an ordered cascade of Stages: each record exits at the first stage confident enough to decide it, so cheap stages shield expensive ones.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnclassified = errors.New("stage: unclassified")

ErrUnclassified is returned by Classify when the stage cannot decide on a category for the record. The cascade then hands the record to the next stage; a record unclassified by every stage is routed for review.

Functions

func ValidateNames

func ValidateNames(stages []Stage) error

ValidateNames rejects a cascade whose stages are not individually identifiable: every Name must be non-empty and unique, so stats, logs, and per-stage policy can key on it unambiguously. A nil stage, including a typed-nil pointer, is rejected rather than dereferenced.

func ValidateResult

func ValidateResult(name string, c domain.Classification) error

ValidateResult guards the boundary between a stage and the cascade: a stage that returns no error is promising a well-formed Classification. When it is not (empty category, or confidence outside [0, 1]), it is reported as a stage Error so the cascade fails fast instead of emitting a malformed decision.

Types

type Error

type Error struct {
	// Stage is the stage that failed.
	Stage string
	// Err is the underlying cause.
	Err error
}

Error reports that a stage itself broke, as opposed to ErrUnclassified, which just asks the cascade to try the next stage. Stage names the stage that failed and Unwrap gives the cause, so callers can tell which stage failed and why with errors.As and errors.Is.

func (*Error) Error

func (e *Error) Error() string

Error implements error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the cause so errors.Is and errors.As reach through.

type Gate

type Gate float64

Gate is a minimum confidence. A decision scoring below it is treated as undecided, so the cascade escalates to the next stage. The zero Gate admits everything, which turns gating off; deterministic stages emit 1, so they always pass.

func NewGate

func NewGate(min float64) (Gate, error)

NewGate returns a Gate for min, or an error if min is outside [0, 1].

func (Gate) Admits

func (g Gate) Admits(confidence float64) bool

Admits reports whether a decision with this confidence clears the gate.

type Gated

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

Gated wraps a Stage with its own confidence gate: a valid decision that clears the gate is returned unchanged, a valid one below it becomes ErrUnclassified so the cascade escalates. A malformed decision is passed through untouched so the cascade's contract check still rejects it rather than having it masked as an escalation.

func WithGate

func WithGate(inner Stage, gate Gate) *Gated

WithGate wraps inner so a decision below gate escalates instead of winning.

func (*Gated) Classify

func (g *Gated) Classify(ctx context.Context, r domain.Record) (domain.Classification, error)

Classify runs the wrapped stage and escalates a below-gate decision.

func (*Gated) Name

func (g *Gated) Name() string

Name implements Stage, reporting the wrapped stage's name.

type Named

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

Named wraps a Stage to report a different name, so one stage type can appear more than once in a cascade under distinct config-declared ids without colliding on the name that stats, logs, and validation key on.

func WithName

func WithName(inner Stage, name string) *Named

WithName wraps inner so Name reports name.

func (*Named) Classify

func (n *Named) Classify(ctx context.Context, r domain.Record) (domain.Classification, error)

Classify implements Stage, delegating to the wrapped stage.

func (*Named) Name

func (n *Named) Name() string

Name implements Stage.

type Stage

type Stage interface {
	// Name identifies the stage in classifications, logs, and metrics.
	Name() string
	// Classify assigns a category to the record, or returns
	// ErrUnclassified when this stage cannot decide. It must not mutate
	// the record, which a concurrent engine shares with later consumers.
	Classify(ctx context.Context, r domain.Record) (domain.Classification, error)
}

Stage classifies records. Implementations range from deterministic rule matching to in-process model inference to remote API calls.

type Static

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

Static is a Stage that classifies by exact-match lookup of the record's payload, for tests and wiring examples. Records whose payload is not in the table are unclassified.

func NewStatic

func NewStatic(name string, categories map[string]string) *Static

NewStatic returns a Stage named name that maps an exact payload string to a category with full confidence.

func (*Static) Classify

func (s *Static) Classify(ctx context.Context, r domain.Record) (domain.Classification, error)

Classify implements Stage.

func (*Static) Name

func (s *Static) Name() string

Name implements Stage.

Directories

Path Synopsis
Package mock implements a stage.Stage that stands in for a model: it emits declared confidences below 1.0 so gates, escalation, and review routing can be exercised end to end before a real model stage exists.
Package mock implements a stage.Stage that stands in for a model: it emits declared confidences below 1.0 so gates, escalation, and review routing can be exercised end to end before a real model stage exists.
Package rules implements stage.Stage with deterministic, YAML-defined matching: ordered rules of payload and field matchers mapped to categories.
Package rules implements stage.Stage with deterministic, YAML-defined matching: ordered rules of payload and field matchers mapped to categories.
Package schema implements a stage.Stage that validates a structured record's decoded Fields against a declared shape.
Package schema implements a stage.Stage that validates a structured record's decoded Fields against a declared shape.

Jump to

Keyboard shortcuts

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