control

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package control is a generic adaptive-control primitive: it reads signals from named sources, aggregates them into a vector, and drives a mode FSM whose transitions are gated by threshold expressions.

"AI director" is one application — Left 4 Dead / Vermintide / Helldivers monitor party stress, transition between calm/build/peak modes, and schedule horde events. The same mechanism shows up in:

  • k8s autoscaler — monitor load metrics, transition cluster size.
  • Alert manager — monitor incident signal, transition severity.
  • Dynamic pricing — read demand, transition price tier.
  • Trading regime detection — read volatility, transition risk mode.

Domain-free by design — the mode names and signal sources are supplied by the caller.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aggregator

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

Aggregator samples a named set of sources and returns the combined vector. Safe for concurrent use.

func NewAggregator

func NewAggregator(sources map[string]Source) *Aggregator

NewAggregator constructs an Aggregator from a name → Source map.

func (*Aggregator) AddSource

func (a *Aggregator) AddSource(name string, s Source)

AddSource registers (or replaces) a source by name.

func (*Aggregator) Vector

func (a *Aggregator) Vector(ctx context.Context, now time.Time) map[string]Signal

Vector samples every source and returns the resulting map.

type Comparison

type Comparison uint8

Comparison enumerates the ordering operators a Threshold can apply.

const (
	// CmpGT — value > threshold.
	CmpGT Comparison = iota
	// CmpGE — value >= threshold.
	CmpGE
	// CmpLT — value < threshold.
	CmpLT
	// CmpLE — value <= threshold.
	CmpLE
	// CmpEQ — value == threshold (use sparingly with floats).
	CmpEQ
)

type Controller

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

Controller drives a Mode FSM from the aggregator's signal vector. Safe for concurrent use.

func NewController

func NewController(agg *Aggregator, modes []Mode, initial string) *Controller

NewController constructs a Controller. `initial` must be one of the Mode names.

func (*Controller) Mode

func (c *Controller) Mode() string

Mode returns the current mode name.

func (*Controller) SetClock

func (c *Controller) SetClock(f func() time.Time)

SetClock overrides the wall clock; used in tests.

func (*Controller) Tick

func (c *Controller) Tick(ctx context.Context, now time.Time)

Tick samples the aggregator, runs the current mode's OnTick, and evaluates transitions.

type Mode

type Mode struct {
	Name        string
	OnEnter     func(ctx context.Context)
	OnTick      func(ctx context.Context, vec map[string]Signal)
	Transitions []ModeTransition
	// MinDwell, if > 0, prevents leaving this mode until at least
	// MinDwell has elapsed since entering. Used to suppress thrashing
	// when signals oscillate near a threshold.
	MinDwell time.Duration
}

Mode is one control-loop mode. OnEnter fires when the controller transitions to this mode; OnTick fires every Tick while this mode is active. Transitions are evaluated after OnTick; the first one whose Conditions all hold wins.

type ModeTransition

type ModeTransition struct {
	To         string
	Conditions []Threshold
}

ModeTransition is one outgoing edge from a Mode. All Conditions must hold for the transition to fire (AND).

type Signal

type Signal float64

Signal is a scalar reading from a single source at a point in time.

type Source

type Source interface {
	Sample(ctx context.Context, now time.Time) Signal
}

Source produces a Signal for an Aggregator.

type SourceFunc

type SourceFunc func(ctx context.Context, now time.Time) Signal

SourceFunc adapts a function to the Source interface.

func (SourceFunc) Sample

func (f SourceFunc) Sample(ctx context.Context, now time.Time) Signal

Sample implements Source.

type Threshold

type Threshold struct {
	Signal string
	Cmp    Comparison
	Value  Signal
}

Threshold is a single comparison on a named signal.

func (Threshold) Eval

func (t Threshold) Eval(vec map[string]Signal) bool

Eval reports whether the threshold holds against the supplied vector. A missing signal returns false.

Jump to

Keyboard shortcuts

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