unit

package
v2.39.1-notes-metadata Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: AGPL-3.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package unit tracks named units of work and the dependencies between them.

A Manager holds two independent kinds of dependency. Status dependencies (AddDependency, IsReady) wait for a prerequisite to reach one exact Status and back coder exp sync. Conditional dependencies (AddConditionalDependency, Evaluate, WaitForDecision) wait for a prerequisite's Outcome to satisfy a Requirement and back declarative script ordering. "Conditional dependency" is the same thing the script ordering RFC calls an "outcome-aware dependency".

A Manager instance is the isolation boundary between callers. Unit IDs are not namespaced, so callers that must not see each other's units, such as the agent socket API and a script execution, use separate instances.

Cycle detection is per graph. A status edge in one direction and a conditional edge in the other form a loop neither check sees.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidRequirement     = xerrors.New("invalid dependency requirement")
	ErrInvalidOutcome         = xerrors.New("invalid unit outcome")
	ErrSameOutcomeAlreadySet  = xerrors.New("same outcome already set")
	ErrOutcomeAlreadyTerminal = xerrors.New("unit outcome is already terminal")
	ErrConflictingRequirement = xerrors.New("conflicting dependency requirement")
)
View Source
var (
	ErrUnitIDRequired           = xerrors.New("unit name is required")
	ErrUnitNotFound             = xerrors.New("unit not found")
	ErrUnitAlreadyRegistered    = xerrors.New("unit already registered")
	ErrCannotUpdateOtherUnit    = xerrors.New("cannot update other unit's status")
	ErrDependenciesNotSatisfied = xerrors.New("unit dependencies not satisfied")
	ErrSameStatusAlreadySet     = xerrors.New("same status already set")
	ErrCycleDetected            = xerrors.New("cycle detected")
	ErrFailedToAddDependency    = xerrors.New("failed to add dependency")
)

Functions

This section is empty.

Types

type ConditionalDependency

type ConditionalDependency struct {
	Unit           ID
	DependsOn      ID
	Requirement    Requirement
	CurrentOutcome Outcome
}

ConditionalDependency is one unmet conditional dependency as seen at evaluation time.

type Decision

type Decision string

Decision is the manager's answer about one unit. It describes what the caller should do next. The manager never records an outcome on its own.

const (
	// DecisionWaiting means some prerequisite is neither satisfied nor
	// impossible yet.
	DecisionWaiting Decision = "waiting"
	// DecisionRunnable means every prerequisite is satisfied.
	DecisionRunnable Decision = "runnable"
	// DecisionSkip means a success prerequisite can no longer succeed. The
	// caller must record OutcomeSkipped for the unit. Until it does, the
	// unit's outcome stays as it was and the unit's own dependents keep
	// waiting.
	DecisionSkip Decision = "skip"
)

type Dependency added in v2.29.0

type Dependency struct {
	Unit           ID
	DependsOn      ID
	RequiredStatus Status
	CurrentStatus  Status
	IsSatisfied    bool
}

Dependency represents a dependency relationship between units.

type Edge

type Edge[EdgeType, VertexType comparable] struct {
	From VertexType
	To   VertexType
	Edge EdgeType
}

Edge is a convenience type for representing an edge in the graph. It encapsulates the from and to vertices and the edge type itself.

type Evaluation

type Evaluation struct {
	Decision          Decision
	UnmetDependencies []ConditionalDependency
}

Evaluation is the decision for a unit plus its unmet conditional dependencies, sorted by DependsOn.

type Graph

type Graph[EdgeType, VertexType comparable] struct {
	// contains filtered or unexported fields
}

Graph is a directed acyclic graph with typed edges. Forward and reverse adjacency are both indexed. Wanting and being wanted by other units are related but different concepts that have different graph traversal implications when Units update their status.

The graph stores edge types to represent different relationships between units, allowing for domain-specific semantics beyond simple connectivity.

func (*Graph[EdgeType, VertexType]) AddEdge

func (g *Graph[EdgeType, VertexType]) AddEdge(from, to VertexType, edge EdgeType) error

AddEdge adds an edge to the graph. It initializes the graph on first use and rejects edges that would create a cycle.

func (*Graph[EdgeType, VertexType]) GetForwardAdjacentVertices

func (g *Graph[EdgeType, VertexType]) GetForwardAdjacentVertices(from VertexType) []Edge[EdgeType, VertexType]

GetForwardAdjacentVertices returns all the edges that originate from the given vertex.

func (*Graph[EdgeType, VertexType]) GetReverseAdjacentVertices

func (g *Graph[EdgeType, VertexType]) GetReverseAdjacentVertices(to VertexType) []Edge[EdgeType, VertexType]

GetReverseAdjacentVertices returns all the edges that terminate at the given vertex.

func (*Graph[EdgeType, VertexType]) ToDOT

func (g *Graph[EdgeType, VertexType]) ToDOT(name string) (string, error)

ToDOT exports the graph to DOT format for visualization. Vertices are written by their numeric IDs, and nodes and edges are sorted by ID.

type ID added in v2.29.0

type ID string

ID provides a type narrowed representation of the unique identifier of a unit.

type Manager added in v2.29.0

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

Manager provides reactive dependency tracking over a Graph. It manages Unit registration, dependency relationships, and status updates with automatic recalculation of readiness when dependencies are satisfied.

func NewManager added in v2.29.0

func NewManager() *Manager

NewManager creates a new Manager instance.

func (*Manager) AddConditionalDependency

func (m *Manager) AddConditionalDependency(unit ID, dependsOn ID, requirement Requirement) error

AddConditionalDependency makes unit wait for dependsOn to reach the given requirement. Both units must be registered first. These dependencies are independent of the ones added by AddDependency. Re-adding an edge with the same requirement does nothing; with a different requirement it is an error.

func (*Manager) AddDependency added in v2.29.0

func (m *Manager) AddDependency(unit ID, dependsOn ID, requiredStatus Status) error

AddDependency adds a dependency relationship between units. The unit depends on the dependsOn unit reaching the requiredStatus.

func (*Manager) Evaluate

func (m *Manager) Evaluate(id ID) (Evaluation, error)

Evaluate returns the current decision for a unit. The decision describes the unit's prerequisites, not the unit itself: a unit whose own outcome is terminal still answers from its edges. Evaluate never changes the unit's outcome. On DecisionSkip the caller records OutcomeSkipped itself.

func (*Manager) ExportDOT added in v2.29.0

func (m *Manager) ExportDOT(name string) (string, error)

ExportDOT exports the dependency graph to DOT format for visualization.

func (*Manager) GetAllDependencies added in v2.29.0

func (m *Manager) GetAllDependencies(unit ID) ([]Dependency, error)

GetAllDependencies returns all dependencies for a unit, both satisfied and unsatisfied.

func (*Manager) GetGraph added in v2.29.0

func (m *Manager) GetGraph() *Graph[Status, ID]

GetGraph returns the underlying graph for visualization and debugging. This should be used carefully as it exposes the internal graph structure.

func (*Manager) GetUnmetDependencies added in v2.29.0

func (m *Manager) GetUnmetDependencies(unit ID) ([]Dependency, error)

GetUnmetDependencies returns a list of unsatisfied dependencies for a unit.

func (*Manager) IsReady added in v2.29.0

func (m *Manager) IsReady(id ID) (bool, error)

func (*Manager) ListUnits added in v2.35.0

func (m *Manager) ListUnits() []Unit

ListUnits returns a snapshot of all registered units and their current status.

func (*Manager) Register added in v2.29.0

func (m *Manager) Register(id ID) error

Register adds a unit to the manager if it is not already registered. If a Unit is already registered (per the ID field), it is not updated.

func (*Manager) Unit added in v2.29.0

func (m *Manager) Unit(id ID) (Unit, error)

Unit fetches a unit from the manager. If the unit does not exist, it returns the Unit zero-value as a placeholder unit, because units may depend on other units that have not yet been created.

func (*Manager) UpdateOutcome

func (m *Manager) UpdateOutcome(unit ID, outcome Outcome) error

UpdateOutcome records a unit's outcome and wakes every WaitForDecision caller. A terminal outcome cannot be changed, because dependents may already have been released or skipped because of it.

func (*Manager) UpdateStatus added in v2.29.0

func (m *Manager) UpdateStatus(unit ID, newStatus Status) error

UpdateStatus updates a unit's status and recalculates readiness for affected dependents.

func (*Manager) WaitForDecision

func (m *Manager) WaitForDecision(ctx context.Context, id ID) (Evaluation, error)

WaitForDecision blocks until the unit is runnable or must be skipped, or ctx is done. It has no deadline of its own and never treats elapsed time as satisfaction. Only the caller's ctx can end the wait.

type Outcome

type Outcome string

Outcome is a unit's progress and final result for conditional dependencies. It is independent of Status.

const (
	OutcomeNotRegistered Outcome = ""
	OutcomePending       Outcome = "pending"
	OutcomeRunning       Outcome = "running"
	OutcomeSucceeded     Outcome = "succeeded"
	OutcomeFailed        Outcome = "failed"
	OutcomeTimedOut      Outcome = "timed_out"
	OutcomeSkipped       Outcome = "skipped"
	OutcomeCanceled      Outcome = "canceled"
)

func (Outcome) String

func (o Outcome) String() string

func (Outcome) Terminal

func (o Outcome) Terminal() bool

Terminal reports whether the outcome can no longer change. It says nothing about whether a dependency is satisfied: canceled is terminal and satisfies nothing. Dependency logic uses Requirement.satisfiedBy and Requirement.impossible instead.

type Requirement

type Requirement string

Requirement is what a conditional dependency needs from its prerequisite.

const (
	RequirementSuccess    Requirement = "success"
	RequirementCompletion Requirement = "completion"
)

type Status added in v2.29.0

type Status string

Status represents the status of a unit.

const (
	StatusNotRegistered Status = ""
	StatusPending       Status = "pending"
	StatusStarted       Status = "started"
	StatusComplete      Status = "completed"
)

Status constants for dependency tracking.

func (Status) String added in v2.29.0

func (s Status) String() string

type Unit added in v2.29.0

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

Unit represents a point-in-time snapshot of a vertex in the dependency graph. Units may depend on other units, or be depended on by other units. The unit struct is not aware of updates made to the dependency graph after it is initialized and should not be cached.

func (Unit) ID added in v2.29.0

func (u Unit) ID() ID

func (Unit) Outcome

func (u Unit) Outcome() Outcome

func (Unit) Status added in v2.29.0

func (u Unit) Status() Status

Jump to

Keyboard shortcuts

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