workflow

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 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 workflow holds a parsed workflow and what the engine plans from it: triggers, reach, plan items, runs and their idempotency keys.

Index

Constants

View Source
const (
	DefaultChainDepth = 3
	MaxChainDepth     = 5
)

The bounds of a chain of workflows triggering each other.

View Source
const (
	KindPlanned       = "workflow_planned"   // Key item id:decision, Data reason
	KindStepRan       = "workflow_step_ran"  // Key item id:step index
	KindStepRefused   = "workflow_refused"   // Key item id:attempt
	KindStepFailed    = "workflow_failed"    // Key item id:attempt
	KindItemDismissed = "workflow_dismissed" // Key item id
	KindItemDropped   = "workflow_dropped"   // Key item id:at, Data why
)
View Source
const EventIDFact = "trigger.event.id"

EventIDFact is the trigger fact an event workflow's key may name, so each event plans its own run.

View Source
const MaxAttempts = 3

MaxAttempts is how many failed runs make an item's key terminal.

Variables

ApprovalModes are the run approval's modes.

EngineKinds are the engine's own event kinds; a workflow's `on` may not name them.

View Source
var ErrWindow = errors.New("window is read only with mode delay, which needs one longer than zero")

ErrWindow refuses a window off the delay mode, or a delay with none.

Functions

func ActorKind added in v0.3.0

func ActorKind(actor string) string

ActorKind is the kind of actor an event's actor names, as "<kind>:<source>" writes it: "person" of "person:cockpit"; "" for an actor written otherwise.

func ChainData added in v0.3.0

func ChainData(data string, c Chain) string

ChainData is the stored data of an event that carries chain c: data itself for a zero chain.

func MarkNames added in v0.2.0

func MarkNames(p Predicate) []string

MarkNames are the marks p reads with handled(); a predicate lists them through a MarkNames() []string method.

func ReadsMarks added in v0.2.0

func ReadsMarks(ps ...Predicate) bool

ReadsMarks says whether one of ps reads the unit's marks, so a caller loads them only then.

func ValidApproval added in v0.2.0

func ValidApproval(m ApprovalMode, window time.Duration) error

ValidApproval refuses a mode outside ApprovalModes and a window off delay.

Types

type Action

type Action string
const (
	ActionApply   Action = "apply"
	ActionDismiss Action = "dismiss"
	ActionSkip    Action = "skip"
	ActionGuide   Action = "guide"
)

type Approval

type Approval struct {
	Mode   ApprovalMode
	Window time.Duration
	Rules  []ApprovalRule
}

Approval is a workflow's run approval: the first rule whose When holds, else Mode and Window.

func (Approval) Predicates added in v0.2.0

func (a Approval) Predicates() []Predicate

Predicates are a's rules' whens.

func (Approval) Validate added in v0.2.0

func (a Approval) Validate() error

Validate refuses a's mode or a rule's when ValidApproval does, or a rule with no When.

type ApprovalMode added in v0.2.0

type ApprovalMode string

ApprovalMode is how a workflow's matched item runs: at once, when its window ends unless a person stops it, or when a person applies it.

const (
	ApprovalRun   ApprovalMode = "run"
	ApprovalDelay ApprovalMode = "delay"
	ApprovalWait  ApprovalMode = "wait"
)

func ModeOf added in v0.3.0

func ModeOf(m gate.Mode) ApprovalMode

ModeOf is a gate's approval mode as a run's: send and run run it; dry_run, a draft gate's only, waits.

func (ApprovalMode) Releases added in v0.2.0

func (m ApprovalMode) Releases() bool

Releases says whether m runs an item with no person applying it.

type ApprovalRule added in v0.2.0

type ApprovalRule struct {
	When   Predicate
	Mode   ApprovalMode
	Window time.Duration
}

ApprovalRule is one [[approval.rule]]: its mode and window when its When holds. A matched wait rule also makes pending an item a guard refuses.

type Chain added in v0.3.0

type Chain struct {
	Depth     int      `json:"depth"`
	Workflows []string `json:"workflows,omitempty"`
}

Chain is the chain of workflows an event closes: Depth is how many workflows ran one after the other to cause it, Workflows their names in order. A person's event starts none: its chain is zero.

func SplitChain added in v0.3.0

func SplitChain(stored string) (string, Chain)

SplitChain reads stored event data as ChainData wrote it: the data and its chain, zero for data written with none.

func (Chain) Has added in v0.3.0

func (c Chain) Has(w string) bool

Has says whether workflow w is in c already.

func (Chain) Then added in v0.3.0

func (c Chain) Then(w string) Chain

Then is c continued by workflow w.

type Cron

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

Cron is a five-field schedule ("0 9 * * 1-5") read in the location of the time it is asked about. When both day fields are restricted, a day matches either, as in cron(8).

func ParseCron

func ParseCron(src string) (*Cron, error)

func (*Cron) Next

func (c *Cron) Next(after time.Time) time.Time

Next is the first firing strictly after after; zero when none comes within years.

func (*Cron) Prev

func (c *Cron) Prev(at time.Time) time.Time

Prev is the last firing at or before at; zero when none came within years.

type Decision

type Decision string
const (
	DecisionRun     Decision = "run"
	DecisionPending Decision = "pending"
	DecisionBlocked Decision = "blocked"
	DecisionSkipped Decision = "skipped"
	// DecisionDeferred is an item whose step started asynchronously: it runs
	// again at its Deadline until a run is done. No person decides it.
	DecisionDeferred Decision = "deferred"
)

type Event

type Event struct {
	ID              int64 // events.id
	Unit            unit.Unit
	Kind, Key, Data string
	// Actor is who recorded it, "<actor>:<source>" for a verb; Chain the
	// chain of workflows it closes.
	Actor string
	Chain Chain
	At    time.Time
}

type Fact

type Fact struct {
	Name  string
	Value any
}

type Facts

type Facts map[string]any

Facts are a unit's facts by dotted name; values are bool, int, string, []string, time.Duration or time.Time, or a Temporal a provider reports, which the engine resolves to one of those before a predicate reads it. "<slot>.repo" is "owner/repo" or "" and is what the pause's Repos reads.

func Resolve added in v0.3.0

func Resolve(f Facts, now time.Time) (Facts, time.Time)

Resolve is f with every Temporal resolved at now, and the earliest deadline among them after now; zero when none has one. f is returned as it is when it holds no Temporal, and never written.

type Gain

type Gain struct{ MemoryBytes, DiskBytes int64 }

type Item

type Item struct {
	ID       string
	Key      Key
	Workflow string
	Unit     unit.Unit
	Decision Decision
	Reason   string
	Steps    []Step
	Gain     Gain
	Why      Why
	Default  Outcome
	Deadline time.Time
	Guidance string // set by Act(guide) only
	// Marks are the marks a done or dismissed run records, by full name, from
	// the facts the item was planned with.
	Marks    map[string]mark.Value
	Attempts int
	Error    string
	Observe  bool
	At       time.Time
}

type Key

type Key struct {
	Workflow string
	Unit     unit.Unit
	Value    string // sha256 hex of the sorted key values, first 16 chars
}

Key is the idempotency key: workflow, unit and the key facts' values (for a schedule trigger, plus the firing time).

func NewKey

func NewKey(w Workflow, u unit.Unit, f Facts, why Why) Key

func (Key) ID

func (k Key) ID() string

ID is "<workflow>/<provider>/<path-escaped-key>/<value>".

type Limits

type Limits struct {
	PerRun, PerDay, PerUnitPerDay int
	Cooldown                      time.Duration
}

type Outcome

type Outcome string

Outcome is what a pending item does when its window ends unanswered.

const (
	OutcomeRun  Outcome = "run"
	OutcomeWait Outcome = "wait"
)

type Predicate

type Predicate interface {
	Match(v Vars) (bool, error)
	Reads() []string
}

Predicate is a compiled `when`; Reads are the facts it names, for the why.

func All

func All(ps ...Predicate) Predicate

All is a predicate that matches when every one of ps does; a nil one matches.

type Reach

type Reach string
const (
	ReachLocal   Reach = "local"
	ReachPrivate Reach = "private"
	ReachVisible Reach = "visible"
)

type Refused

type Refused struct{ Err error }

Refused is a predicate that never matches and says why on every match.

func (Refused) Match

func (r Refused) Match(Vars) (bool, error)

func (Refused) Reads

func (Refused) Reads() []string

type Run

type Run struct {
	Key      Key
	Status   RunStatus
	Attempts int
	Error    string
	At       time.Time
}

type RunStatus

type RunStatus string
const (
	RunDone      RunStatus = "done"
	RunDismissed RunStatus = "dismissed"
	RunFailed    RunStatus = "failed"
	RunRefused   RunStatus = "refused"
)

type Step

type Step struct {
	Use   string         // a name of builtin.Steps
	Reach Reach          // from the vocabulary, never from the manifest
	Args  map[string]any // e.g. {"intent": "review", "stage": "follow-up"}
}

type Temporal added in v0.3.0

type Temporal struct {
	Kind        TemporalKind
	Time        time.Time
	Threshold   time.Duration
	WorkingDays bool
}

Temporal is a fact whose value changes with the clock, which a source reports instead of a value it would have to report again as time passes. The engine resolves it with its own clock whenever it reads the facts. A Temporal whose Time is zero resolves to its type's zero and has no deadline.

func (Temporal) Deadline added in v0.3.0

func (t Temporal) Deadline(now time.Time) (time.Time, bool)

Deadline is the instant after now at which t's value next changes; false when it never does: a TemporalSince changes continuously, and the other kinds change once.

func (Temporal) Value added in v0.3.0

func (t Temporal) Value(now time.Time) any

Value is t at now: a time.Duration for TemporalSince, a bool otherwise.

type TemporalKind added in v0.3.0

type TemporalKind int

TemporalKind is how a Temporal fact reads the clock.

const (
	// TemporalSince is the duration from Time to now.
	TemporalSince TemporalKind = iota + 1
	// TemporalAt is true from Time on.
	TemporalAt
	// TemporalAfter is true once more than Threshold has passed since
	// Time, counted in working days when WorkingDays.
	TemporalAfter
)

type Trigger

type Trigger struct {
	Kind     TriggerKind
	On       []string // event kinds, Kind == TriggerEvent
	Schedule *Cron    // Kind == TriggerSchedule
	// Actors are the kinds of actor whose events plan the workflow, every
	// one when empty; Depth the longest chain an event it reacts to may
	// close, DefaultChainDepth when 0. Both read with Kind == TriggerEvent.
	Actors []string
	Depth  int
}

func (Trigger) Fires added in v0.3.0

func (t Trigger) Fires(w string, ev Event) bool

Fires says whether an event of the trigger's kinds plans workflow w: its actor is one the trigger listens to, its chain is shorter than the trigger's depth, and w is not in that chain already.

type TriggerKind

type TriggerKind string
const (
	TriggerSweep    TriggerKind = "sweep"
	TriggerEvent    TriggerKind = "event"
	TriggerSchedule TriggerKind = "schedule"
)

type Vars

type Vars struct {
	Facts   Facts             // the unit's slot ("pr.state", "session.idle_for")
	Trigger Facts             // "trigger.kind", "trigger.event.kind", "trigger.event.key", "trigger.event.data", "trigger.fired_at"
	Config  Facts             // "config.cleanup.idle", ...
	Marks   map[string]string // the unit's handled marks by name, which handled() reads
	Now     time.Time
}

type Why

type Why struct {
	Workflow string
	Trigger  TriggerKind
	Event    *Event
	FiredAt  time.Time
	Matched  []Fact // the predicate's Reads with their values, sorted by name
}

type Workflow

type Workflow struct {
	Name, Summary string
	Provider      unit.ProviderID
	Slot          string // the provider's fact slot: "pr" for github, else the provider id
	Trigger       Trigger
	When          Predicate
	Steps         []Step
	Limits        Limits
	// Observe plans items pending and never runs them by itself.
	Observe bool
	Order   int
	Key     []string // fact names; empty means the slot's fingerprint fact
	// Marks are the facts whose values a done or dismissed item records as
	// the unit's marks, by the mark's name under the workflow (mark.Of).
	Marks map[string]string
	// Approval is the approval of its runs' plan gates.
	Approval Approval
	// Gates are the approval of the gates its gate.open steps open, by
	// kind, every kind but draft and plan.
	Gates   map[gate.Kind]Approval
	Source  string // manifest path
	Bundled bool
	// Overrides are the files of a bundled workflow a user directory replaces.
	Overrides []string
	// DraftApproval is the approval of the drafts its steps and turns
	// propose, from its [gates.approval]; nil when that names none for a
	// draft gate.
	DraftApproval *draft.Policy
	// Withdraw is the manifest's [drafts.withdraw] when, over the subject's facts
	// and each draft's (the draft slot): an open draft the workflow proposed
	// is withdrawn when it matches. Nil withdraws none.
	Withdraw Predicate
	// WithdrawReason is the Reason of a draft Withdraw withdraws: assigned
	// or moot.
	WithdrawReason string
}

func (Workflow) Visible

func (w Workflow) Visible() bool

Visible says whether a step of the workflow acts in the user's name where others see it.

Jump to

Keyboard shortcuts

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