loop

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package loop defines the core loop aggregate types. A loop is a fold over an append-only typed event log:

(S, Event) → (S, []Action)

The Transition function is pure: no I/O, no clock reads, no global rand. The runtime injects time, IDs, and randomness via the Context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// Iteration is the current iteration number (1-based).
	Iteration int
	// Now is the time of the last event, injected by the runtime.
	// This makes transitions deterministic: replaying the same event log
	// with the same transition always produces the same state.
	Now time.Time
	// Rand is a deterministic pseudo-random number generator seeded from
	// the LoopStarted seed XOR the current sequence number.
	// Same seed + same sequence → same random sequence.
	Rand *rand.Rand
	// LoopID is the unique identifier of this loop instance.
	LoopID event.LoopID
}

Context is the deterministic execution context passed to every Transition call. It contains everything a transition may observe — and nothing it must not.

Purity contract: transitions must NOT read the system clock, use global rand, or perform any I/O. All non-determinism is injected here by the runtime.

func NewContext

func NewContext(p NewContextParams) Context

NewContext constructs a Context from the given params. The Rand field is seeded deterministically from seed XOR seq, ensuring the same (seed, seq) always yields the same random sequence.

type Definition

type Definition[S, R any] struct {
	// Name is a human-readable identifier for this loop type.
	Name string
	// InitState returns the initial state for a new loop instance.
	// Called once per Start; not called on Resume (state is loaded from store).
	InitState func() S
	// Transition is the pure state-transition function.
	Transition Transition[S]
}

Definition is the static description of a loop: its name, initial state, and how it transitions. R is the result type produced via action.Finish.

type NewContextParams

type NewContextParams struct {
	// Iteration is the current 1-based iteration number.
	Iteration int
	// Now is the time from the most recently processed event.
	Now time.Time
	// LoopID is the unique loop instance identifier.
	LoopID event.LoopID
	// Seed is the entropy seed from the LoopStarted event.
	Seed int64
	// Seq is the current event sequence number.
	// Combined with Seed to produce the per-event deterministic rand source.
	Seq uint64
}

NewContextParams holds the inputs for constructing a deterministic Context.

type Transition

type Transition[S any] func(tc Context, state S, ev event.Envelope) (S, []action.Action, error)

Transition is the pure state-transition function of a loop. Given a context, current state, and the next event, it returns:

  • the new state
  • zero or more actions for the runtime to execute
  • an error (which causes the loop to enter Failed outcome)

Transition must be pure: no I/O, no clock, no global rand.

Directories

Path Synopsis
Package outcome defines the sealed set of terminal outcomes for a loop.
Package outcome defines the sealed set of terminal outcomes for a loop.

Jump to

Keyboard shortcuts

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