Raxol.Workflow.Node behaviour (Raxol v2.6.0)

View Source

Node descriptors for Raxol.Workflow.Graph.

A node is one of three shapes:

  • FunctionNode -- a 1-arity function (state -> result)
  • BehaviourNode -- a module implementing this module's behaviour
  • TypedNode -- a struct with a Raxol.Workflow.Node.Executor protocol implementation. Mirrors the Raxol.Core.Runtime.Directive pattern: lets external packages register typed nodes (e.g. %Raxol.ACP.Workflow.Node.CreateMemo{...}) with custom telemetry and validation.

Node functions return one of:

  • {:ok, state} -- proceed with the new state
  • {:effects, [directive], state} -- emit directives and proceed
  • {:interrupt, value} -- pause the run for human-in-the-loop resume
  • {:error, reason} -- fail (handled by the workflow's failure policy)

FunctionNodes also accept an optional 1-arity compensation function that runs in reverse order under failure_policy: :compensate.

Summary

Callbacks

Called by the runtime's failure policy when failure_policy: :compensate is set and the run errors after this node already succeeded. Receives the current state (potentially modified by later compensations) and returns either an updated state via {:ok, new_state} or an error via {:error, reason}. Compensation errors are surfaced through node.compensated telemetry but do not displace the run's original failure reason. Default is a no-op that leaves the state untouched.

Called once at run start to set up node-private state.

Called once per visit to the node. Returns the next state, an interrupt, an error, or a state plus a list of Raxol.Core.Runtime.Directive structs to dispatch through the runtime's existing effect pipeline.

Functions

Construct a BehaviourNode.

Construct a FunctionNode.

Construct a FunctionNode with an optional compensation function.

Return the node's id.

Construct a TypedNode.

Types

id()

@type id() :: atom() | binary()

result()

@type result() ::
  {:ok, state()}
  | {:effects, [struct()], state()}
  | {:interrupt, any()}
  | {:error, any()}

state()

@type state() :: any()

t()

Callbacks

compensate(state, opts)

(optional)
@callback compensate(state :: state(), opts :: keyword()) ::
  {:ok, state()} | {:error, any()}

Called by the runtime's failure policy when failure_policy: :compensate is set and the run errors after this node already succeeded. Receives the current state (potentially modified by later compensations) and returns either an updated state via {:ok, new_state} or an error via {:error, reason}. Compensation errors are surfaced through node.compensated telemetry but do not displace the run's original failure reason. Default is a no-op that leaves the state untouched.

init(opts)

(optional)
@callback init(opts :: keyword()) :: state()

Called once at run start to set up node-private state.

Optional. Default returns the run's initial state unchanged.

run(state, opts)

@callback run(state :: state(), opts :: keyword()) :: result()

Called once per visit to the node. Returns the next state, an interrupt, an error, or a state plus a list of Raxol.Core.Runtime.Directive structs to dispatch through the runtime's existing effect pipeline.

Functions

behaviour(id, module, opts \\ [])

@spec behaviour(id(), module(), keyword()) :: Raxol.Workflow.Node.BehaviourNode.t()

Construct a BehaviourNode.

function(id, fun)

@spec function(id(), (state() -> result())) :: Raxol.Workflow.Node.FunctionNode.t()

Construct a FunctionNode.

function(id, fun, compensate_fun)

@spec function(id(), (state() -> result()), (state() ->
                                         {:ok, state()} | {:error, any()})) ::
  Raxol.Workflow.Node.FunctionNode.t()

Construct a FunctionNode with an optional compensation function.

The compensation runs in reverse order under failure_policy: :compensate when the run fails after this node succeeded. It must be a 1-arity function returning {:ok, new_state} | {:error, reason}.

id(arg1)

@spec id(t()) :: id()

Return the node's id.

typed(id, struct)

@spec typed(
  id(),
  struct()
) :: Raxol.Workflow.Node.TypedNode.t()

Construct a TypedNode.