ExAgent.Coordination.Flow (ExAgent v3.0.0)

View Source

A bounded, trusted host router or fan-out/fan-in.

Stability — Candidate.

A router's versioned :select callback returns {:ok, id} for one declared branch or {:error, reason}. A parallel flow admits every declared branch, with at most :max_concurrency live branch workers. Router and parallel branches receive the root input. A map flow runs a single versioned branch template for each item of the manifest passed to run/3. All branches may delegate through ordinary agent tools. Portable outcomes follow definition order for static branches and manifest order for map branches.

:failure_policy is :fail_fast or :collect. Collect includes confirmed failures in the ordered outcomes; uncertain IO always blocks continuation. A versioned optional :merge callback receives the ordered portable outcomes. Without one, that list is the result. Selection and merge are admitted and checkpointed host phases: a crash after admission requires explicit recovery and never replays an uncertain callback.

For a confirmed failure of selection or merge, RunError.partial.error_phase is :select or :merge. Restore preparation failures use :prepare. A failed branch returns its own live reason, as a sequence step does. A terminal resume reads the phase and portable error from the record.

Selection and merge are pure trusted host callbacks. Put external effects in agent tools, where intent/outcome and approval are journaled. Their explicit versions identify the host code; change a version when its behavior changes.

There are at most 32 branches, concurrency is 1..32 (default 4), and the merged result and each branch result have separate portable JSON limits of 65,536 bytes (:max_result_bytes and :max_branch_result_bytes, which may be lowered). These limits do not guarantee admission of every branch/delegation depth into the existing 8 MiB journal. The complete portable definition, including its fingerprint, must also fit in 65,536 JSON bytes at construction. Ordinary scope authority and checkpoint admission apply.

Options for new/1

OptionContract
:id, :versionRequired strings identifying the trusted definition.
:kindRequired. Use :router to select one branch, :parallel to run every branch or :map to run a template for each input item.
:stepsRouter and parallel only. Required list of 1 to 32 steps with distinct IDs, defined as in ExAgent.Coordination.Composition.new/1 without input mappers.
:templateMap only. Required single step descriptor without an input mapper. Its versioned references identify the agent, policy, model and output contract.
:max_itemsMap only. Maximum manifest length, from 1 to 32. Defaults to 32.
:selectRouter-only function of the root input. Return {:ok, step_id} or {:error, reason}.
:select_versionRequired version string for the router's selector.
:mergeOptional function of the ordered branch outcomes. Return {:ok, json_value} or {:error, reason}. Defaults to returning the list of outcomes.
:merge_versionRequired version string when supplying :merge.
:max_concurrencyMaximum live branch workers, from 1 to 32. Defaults to 4.
:failure_policy:fail_fast stops admitting branches on a confirmed failure and is the default for routers and parallel flows. :collect includes confirmed failures in the ordered outcomes and is the default for map flows.
:max_result_bytes, :max_branch_result_bytesJSON byte caps for the merged result and each branch result. Both accept 1 through 65,536 and default to 65,536.

Summary

Functions

Return the portable identity after validating the trusted definition and its fingerprint.

Construct a bounded router, parallel flow or map flow. See the module's options table. Invalid definitions raise ArgumentError.

Resume from an exact continuation reference or read a confirmed terminal result. Use the :continuation returned by a successful recovery or reconciliation, or read the current reference with ExAgent.Continuation.get/2. A stale or invalid reference gives a run error with reason :continuation_conflict. A matching revision that cannot resume gives {:continuation_not_ready, status}.

Execute a flow using the supplied continuation configuration.

Compare a stored identity with the trusted Flow definition.

Types

t()

@opaque t()

Functions

binding(definition)

@spec binding(t()) :: map()

Return the portable identity after validating the trusted definition and its fingerprint.

new(opts)

@spec new(keyword()) :: t()

Construct a bounded router, parallel flow or map flow. See the module's options table. Invalid definitions raise ArgumentError.

resume(definition, reference, opts \\ [])

Resume from an exact continuation reference or read a confirmed terminal result. Use the :continuation returned by a successful recovery or reconciliation, or read the current reference with ExAgent.Continuation.get/2. A stale or invalid reference gives a run error with reason :continuation_conflict. A matching revision that cannot resume gives {:continuation_not_ready, status}.

run(definition, input, opts \\ [])

@spec run(t(), term(), keyword()) ::
  {:ok, ExAgent.Coordination.result()} | {:error, ExAgent.RunError.t()}

Execute a flow using the supplied continuation configuration.

For :map, pass an ordered list of %{id: id, input: value} items or their string-keyed equivalents. IDs are unique UTF-8 strings of 1 to 512 bytes. The manifest may be empty and must fit in 65,536 bytes of JSON. Each string input becomes the prompt verbatim. Other JSON values become canonical JSON text. The Flow commits the manifest when it creates the continuation. The result's steps and default output follow the manifest order and use its IDs. Set :step_options using the template's ID to apply them to every item.

validate_binding(d, stored)

@spec validate_binding(t(), term()) ::
  :ok | {:error, :invalid_composition_binding | :composition_definition_changed}

Compare a stored identity with the trusted Flow definition.