Workflow Module¶
workflow
¶
Workflow graph types and decorator — the public facade.
The vocabulary itself lives in functualize._types.workflow so that boot and
discovery can read declarations without an internal layer importing the public
surface; this module is the user-facing re-export, mirroring how
functualize.job fronts _types.job_declaration.
Public API
- workflow: Decorator for declaring multi-step workflows.
- Step: A node that runs a registered job.
- Gate: A node that pauses for input.
- AgentStep: A node performed by an agent, via a registered executor.
- Tool: A job a gate offers, with gate-fixed arguments narrowed away.
- Edge: Directed connection between two workflow nodes.
- ConditionalEdge: Branching connection based on runtime condition.
- END: Sentinel marking workflow termination.
- FromStep: A read of this walk's recorded result for one step or gate —
on a step's or the epilogue's parameter, or bound to a gate tool's
argument (
Tool(read_file, allowed=FromStep(...))). - ChoiceDecision: A gate's declared decision (
Gate(decide=...)) — which field a decision provider fills, from which step's result, and the thresholds a proposal must clear. Provisional: outside the names 1.0 promises to keep.
END = _EndSentinel()
module-attribute
¶
__all__ = ['workflow', 'AgentStep', 'ChoiceDecision', 'ConditionalEdge', 'Edge', 'END', 'FromStep', 'Gate', 'Loop', 'Notification', 'Notify', 'OnFailure', 'Step', 'Tool', '_EndSentinel']
module-attribute
¶
ChoiceDecision(field, instructions, options, state, accept_at, min_margin=0.0, model=None, fallback=None)
dataclass
¶
A gate's declared decision: who fills which field, and when it counts.
Declared on Gate(decide=...). The gate hands the recorded result of
state's step to a decision provider as the text to decide about, and
accepts the proposed option only when its probability reaches
accept_at and leads the runner-up by at least min_margin; anything
less blocks the gate for a person. Whether options match the awaited
field is checked by the gate, which knows the field; this class checks its
own ranges only.
fallback names the option taken when no proposal clears the
thresholds: the gate still completes, on the declared answer rather than a
proposed one. A default on the decided field itself is the same outcome
reached by accident, which is why the gate refuses it.
field
instance-attribute
¶
instructions
instance-attribute
¶
options
instance-attribute
¶
state
instance-attribute
¶
accept_at
instance-attribute
¶
min_margin = 0.0
class-attribute
instance-attribute
¶
model = None
class-attribute
instance-attribute
¶
fallback = None
class-attribute
instance-attribute
¶
__post_init__()
¶
Source code in src/functualize/_types/decision.py
FromStep(step)
¶
A read of this walk's recorded result for one node (resolved Q20).
FromStep and :class:FromJob answer different questions, and the
difference is why this is a separate name rather than a flag:
============ =============================== =========================
FromJob in a signature FromStep
============ =============================== =========================
may run it yes — it is a dependency edge never
resolves in fingerprints, or a scope this walk's nodes
upstream may not have run yet has already run
============ =============================== =========================
The node is a Step or a Gate — a gate's record is its answer — and
the read is written in one of three positions:
- a step job's parameter,
Annotated[T, FromStep("node")]; - the epilogue body's parameter, the same annotation on the
@workflowfunction itself; -
a gate tool's bound argument,
Tool(job, arg=FromStep("node"))::Gate( name="review", awaits=Decision, tools=[Tool(read_file, allowed=FromStep("setup-vfs"))], )
The agent may call read_file, but allowed is fixed to whatever
setup-vfs returned in this walk — so the tool is scoped to exactly
those files and a call outside them is inexpressible rather than refused.
Boot refuses a name that is not a node of the workflow, and a step or gate
reader naming a node the graph does not order before it — including a
gate's binding naming its own gate; an earlier gate is allowed and reads
that gate's answer. The epilogue may read any node, because it runs after
END. At run time a node a conditional branch skipped yields the
parameter's declared default, and with none the run fails with
:class:~functualize._types.errors.FromStepUnavailableError naming the
node, the parameter and the reader — as does a job run outside a walk,
including one invoked from inside a step. An explicit argument wins over
the read.
Why not FromJob here. In a gate-tool binding run=True is not
merely unused, it is unmeaningful: the agent invokes the tool on demand,
outside the graph's ordering, and the referenced step has already run
because the graph ordered it before the gate. Running it from here would
execute it outside the walk's step recording, so the walker would not know
it happened. Reusing FromJob would have made its natural spelling mean
one thing in a signature and another in a binding — the "same declaration,
different path" divergence this codebase has repeatedly paid for.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
str | Callable[..., Any]
|
The step's registered job name, or the decorated function.
Positional-only, matching :class: |
required |
Source code in src/functualize/_types/from_job.py
AgentStep(name, instructions, executor=None, tools=(), requires=frozenset(), time_budget_s=None)
dataclass
¶
A workflow node performed by an agent, not by a registered job.
The third node kind, and the first whose execution is not a local
function call: it runs somewhere else, it can take arbitrarily long, it can
fail in ways a try/except around a callable does not describe, and it
can be refused — which is the point. A step that declares something its
executor cannot honour refuses before the walk starts, rather than running
with the constraint silently dropped.
Nothing about the agent is declared here. The port says what the engine needs; how an implementation talks to a model, an MCP client or a terminal is the implementation's business.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Graph key for this node — the address it is recorded under, and the name a refusal reports. |
instructions |
str
|
What the step asks the agent to do. Required, and not allowed to be blank: an agent step with nothing to ask cannot be serviced by anyone. |
executor |
str | None
|
The registered executor that services this step, by name.
|
tools |
Sequence[str]
|
The tool allowlist this step declares. Declaring any tool
implies :attr: |
requires |
frozenset[AgentCapability]
|
Capabilities the step needs honoured, on top of the implied
ones. Widening a declaration is explicit; narrowing it is not
possible — |
time_budget_s |
float | None
|
The step's active-time budget in seconds, when it
declares one. Declaring one implies
:attr: |
name
instance-attribute
¶
instructions
instance-attribute
¶
executor = None
class-attribute
instance-attribute
¶
tools = ()
class-attribute
instance-attribute
¶
requires = frozenset()
class-attribute
instance-attribute
¶
time_budget_s = None
class-attribute
instance-attribute
¶
__post_init__()
¶
Source code in src/functualize/_types/workflow.py
ConditionalEdge(source, condition, targets)
dataclass
¶
Branching connection where the target depends on a runtime condition.
The chosen key is recorded per scope on first evaluation and replayed on resume, so a walk that pauses cannot resume down a different branch than the one it paused on (§D.7).
Attributes:
| Name | Type | Description |
|---|---|---|
source |
str
|
Name of the source node. |
condition |
Callable[..., str]
|
Callable returning a key into |
targets |
dict[str, str | _EndSentinel]
|
Mapping of condition keys to node names or |
source
instance-attribute
¶
condition
instance-attribute
¶
targets
instance-attribute
¶
__post_init__()
¶
Source code in src/functualize/_types/workflow.py
Edge(source, target=(lambda: END)())
dataclass
¶
Unconditional directed connection between two workflow nodes.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
str
|
Name of the source node. |
target |
str | _EndSentinel
|
Name of the target node, or |
source
instance-attribute
¶
target = field(default_factory=(lambda: END))
class-attribute
instance-attribute
¶
__post_init__()
¶
Source code in src/functualize/_types/workflow.py
Gate(name, awaits, tools=(), strategy=None, decide=None)
dataclass
¶
A workflow node that pauses for input.
When the walker reaches a gate it records a BLOCKED position, publishes
awaits's JSON schema, and stops. Depositing valid input resumes the
walk; the deposited payload is available to the epilogue body.
The answer is the gate's record in the walk. Read it with
:class:~functualize._types.from_job.FromStep — on a later step's
parameter, on the epilogue body's
(approval: Annotated[Approval, FromStep("approve")], rebuilt into the
annotated type), or as a later gate's tool binding.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Graph key for this node, and the address used to resume it. |
awaits |
type[BaseModel]
|
Pydantic model describing the input the gate requires. |
tools |
Sequence[ToolRef]
|
Jobs an external agent may run while resolving this gate — a
permission, not a hint, enforced at MCP dispatch. Each entry is a
job name, a decorated function, or a :class: |
strategy |
str | None
|
Preferred resolution strategy. One of |
decide |
ChoiceDecision | None
|
A :class: |
name
instance-attribute
¶
awaits
instance-attribute
¶
tools = ()
class-attribute
instance-attribute
¶
strategy = None
class-attribute
instance-attribute
¶
decide = None
class-attribute
instance-attribute
¶
__post_init__()
¶
Source code in src/functualize/_types/workflow.py
Loop(source, target, max_iterations, condition=None)
dataclass
¶
A back-edge that closes a cycle, with the bound that makes it legal.
A cycle declared with ordinary :class:Edge\ s is refused at decoration
time (workflow-graph-semantics/T1), because the walk prunes nodes it has
already visited and such a graph therefore ran its cycle a single time, in
silence. Loop is the declaration that says how many times, which is the
one thing the graph could not previously express.
max_iterations has no default on purpose. Every value anyone would
pick as one is wrong for somebody: too low silently truncates work, too
high turns a runaway condition into a long outage instead of a quick
refusal. Writing the bound is the point of the type.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
str
|
The node the back-edge leaves — the end of the repeated body. |
target |
str
|
The node it returns to — the start of the repeated body. |
max_iterations |
int
|
How many times the body may run in total, counting the
first pass. |
condition |
Callable[..., bool] | None
|
Called with the source node's return value; going round
again requires a true answer. |
source
instance-attribute
¶
target
instance-attribute
¶
max_iterations
instance-attribute
¶
condition = None
class-attribute
instance-attribute
¶
__post_init__()
¶
Source code in src/functualize/_types/workflow.py
Notification(to, scope_id, workflow, status, node=None)
dataclass
¶
What a notifier is handed. A target, and what happened.
Five fields and no envelope. There is no message id, no correlation key, no priority and no retry count, because each of those is the first field of a broker and this is not one (N8). A provider that needs an id makes one; a provider that needs a retry owns it.
Attributes:
| Name | Type | Description |
|---|---|---|
to |
str
|
The declaration's |
scope_id |
str
|
The walk this is about. |
workflow |
str | None
|
The job name, when the scope records one. |
status |
str
|
The scope status that fired it. |
node |
str | None
|
Where the walk stopped, when it stopped somewhere — the failed node, or the gate it is blocked at. None for a walk that finished. |
Notify(on, to, provider=None)
dataclass
¶
Tell somebody when a walk ends in a given state.
An effect, and a deliberately modest one: the record that it fired is committed to the scope's branch store before the provider is called, so a crash can lose a notification but can never send it twice. At-most-once, in the direction that matters — a resumed workflow must not page the on-call again for a failure they have already seen.
This is not the transactional outbox and does not ride it. The outbox
(ADR-034) exists for an effect whose delivery must not be lost: its
intent commits with the state change and an EffectReceiver is delivered
at least once, deduplicating on the effect's idempotency key. A Notify
makes the opposite trade — it would rather drop one than repeat one — so
it goes straight to the registered notifier, once, with no redelivery.
When a notification must survive a crash, it is an outbox receiver, not a
Notify.
Not a bus and not a broker.
tois opaque to the engine: it is handed to the provider verbatim and nothing here parses, matches or routes on it. The momenttobecomes load-bearing routing, you own a broker — and then retries, fan-out and a dead-letter queue follow, none of which this is. A target, and an effect.
Attributes:
| Name | Type | Description |
|---|---|---|
on |
str
|
The scope status to fire on — one of |
to |
str
|
Where the notification goes, in whatever spelling the provider understands. An address, a channel, a task list, a URL. |
provider |
str | None
|
Which registered notifier delivers it. None means the single registered one, and is an error when there is more than one — a notification that silently picked a deliverer would be the worst kind of working. |
on
instance-attribute
¶
to
instance-attribute
¶
provider = None
class-attribute
instance-attribute
¶
key
property
¶
What "this notification already fired" is recorded under.
The declaration's own content, not its position in a list: a workflow
that gains a second Notify must not make the first one fire again by
shifting its index.
__post_init__()
¶
Source code in src/functualize/_types/workflow.py
OnFailure(source, target=(lambda: END)(), when=None)
dataclass
¶
Where control goes when a step raises.
Without one, a raising step stops the walk and the scope is marked
failed — that is the behaviour every workflow has today and OnFailure
does not change it. It adds a declared alternative, and only for the node
it names.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
str
|
The node whose failure this routes. |
target |
str | _EndSentinel
|
Where to continue, or |
when |
Callable[[BaseException], bool] | None
|
Called with the exception the step raised; routing requires a
true answer. |
The chosen route is recorded and read back on replay, never
re-evaluated — the property ConditionalEdge already has, for a sharper
reason. _choice_for puts it as "calling it and discarding the answer
would still run whatever side effects it has", and a failure predicate is
exactly the kind that pages somebody.
Step(job, effecting=False)
dataclass
¶
A workflow node that runs a registered job.
Attributes:
| Name | Type | Description |
|---|---|---|
job |
str | Callable[..., Any]
|
The job to run — its registered name, or the decorated function itself. Nothing else: a step does not define behavior, it points at behavior that is already declared and independently runnable. |
effecting |
bool
|
This step does something the world remembers — charges a card, sends a mail, files a ticket. An in-flight effecting step is never re-run without a decision, and is exactly-once once that decision is made; a raise is the job's own account, even if its effect already happened. Default What the flag buys is a record of intent: before the job runs,
the walk commits an
|
job
instance-attribute
¶
effecting = False
class-attribute
instance-attribute
¶
name
property
¶
The graph key for this node (the referenced job's name).
__post_init__()
¶
Source code in src/functualize/_types/workflow.py
Tool(job, /, **bound)
¶
A job offered at a gate, with some of its arguments fixed by the gate.
A tool is a registered job — there is no second kind of callable thing in
this framework. What a Tool adds is narrowing: arguments pinned here
are removed from the schema the agent is shown, so a forbidden call is not
merely refused, it is inexpressible::
Gate(
name="approval",
awaits=RefundDecision,
tools=[order_history, Tool(issue_refund, cap_cents=5_000)],
)
The agent sees issue_refund(order_id, amount_cents) and never learns
cap_cents exists. Passing it anyway is an error rather than a silent
override: an agent that believes it set a value and did not is worse off
than one told no.
This has to live at the gate rather than on the job, because it is a
property of the usage. The same issue_refund may be capped at $50 in a
self-serve workflow and uncapped in a supervisor one, and its signature
cannot know which workflow is calling.
A bare job reference stays legal wherever no narrowing is wanted —
tools=[order_history] is not worth a wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job
|
str | Callable[..., Any]
|
The job to offer — its registered name or the decorated function.
Positional-only, so a job with a parameter named |
required |
**bound
|
Any
|
Arguments fixed by this gate. |
{}
|
Source code in src/functualize/_types/workflow.py
workflow(*, steps, edges, notify=())
¶
Decorator registering a function as a declarative workflow.
Validates the workflow graph at decoration time and attaches the frozen
declaration to the decorated function. The function's own body is the
workflow's epilogue: it runs when the walk reaches END.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
Sequence[Step | Gate | AgentStep]
|
Workflow nodes — |
required |
edges
|
Sequence[Edge | ConditionalEdge]
|
List of Edge or ConditionalEdge objects defining connections. |
required |
notify
|
Sequence[Notify]
|
Notifications to fire when the walk ends in a declared state.
A separate argument rather than an entry in |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., Any]], Callable[..., Any]]
|
A decorator that attaches the workflow definition to the function. |
Callable[[Callable[..., Any]], Callable[..., Any]]
|
Identity-preserving: |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a list entry is not a workflow node or edge type. |
ValueError
|
If the graph contains duplicate node names or unknown node references in edges. |
Source code in src/functualize/workflow/_decorator.py
Overview¶
The functualize.workflow module provides graph-based workflow execution with multi-step job orchestration, branching logic, and conditional execution paths.
Module location: src/functualize/workflow/
from functualize.workflow import (
workflow,
Step,
Gate,
AgentStep,
Edge,
ConditionalEdge,
OnFailure,
END,
Notify,
Notification,
)
@workflow Decorator¶
The @workflow decorator transforms a function into a multi-step workflow. The decorated function should return a workflow graph description.
from functualize.workflow import workflow, Step, Edge, END
@workflow(
steps=[
Step(step1),
Step(step2),
],
edges=[
Edge(source="step1", target="step2"),
Edge(source="step2", target=END),
],
)
def multi_step_job(config, rc):
"""Define a workflow with multiple steps."""
rc.log("Workflow complete")
Step¶
A workflow node that runs a registered job. A step wraps a reference to a job — its registered name or the decorated function itself. It carries no behavior of its own; the job's @job declaration (deps, guards, caching) is what runs.
from functualize.workflow import Step
step = Step("validate_data")
step = Step(validate_data) # callable reference
| Attribute | Type | Description |
|---|---|---|
job |
str \| Callable |
The registered job name or decorated function |
name (property) |
str |
Graph key for this node (the referenced job's normalized name) |
Note
Step only takes a job argument. There are no action or name
constructor parameters — the job declaration already describes what runs.
AgentStep¶
A workflow node performed by an agent, through a registered executor, rather than by a registered job. The third node kind, and the first whose execution is not a local function call: it runs somewhere else, can take arbitrarily long, and can be refused.
from functualize.workflow import AgentStep
AgentStep(
"draft",
instructions="Draft the release notes from the fetched changelog.",
tools=["read_file"],
time_budget_s=120,
)
| Attribute | Type | Description |
|---|---|---|
name |
str |
Graph key for this node — the address it is recorded under, and the name a refusal reports |
instructions |
str |
What the step asks the agent to do. Required, and may not be blank |
executor |
str \| None |
The registered executor that services this step. None means the only registered executor; with two registered, a step naming none is refused rather than guessed at |
tools |
Sequence[str] |
The tool allowlist. Declaring any tool implies enforces_tool_allowlist. An empty sequence declares no constraint, which is not the same statement as "no tools" |
requires |
frozenset[AgentCapability] |
Capabilities needed on top of the implied ones. Widening is explicit; narrowing is not possible |
time_budget_s |
float \| None |
Active-time budget in seconds. Declaring one implies preserves_active_time_budget |
Refused, never degraded
An executor that cannot honour a required capability makes the step refuse before the walk starts. Running it with the constraint dropped would produce a workflow that appears to have restricted tools it left wide open.
See Workflows → Agent steps for executors, capabilities and a worked graph.
Edge¶
Represents a directed connection between two workflow steps.
from functualize.workflow import Edge, END
# source and target name workflow nodes
edge = Edge(source="start", target="process")
final_edge = Edge(source="process", target=END)
| Attribute | Type | Description |
|---|---|---|
source |
str |
Name of the source node |
target |
str \| END |
Name of the target node, or END to terminate |
Note
Edge takes source and target as keyword arguments naming nodes,
not Step objects.
ConditionalEdge¶
Represents a branching connection based on a runtime condition.
from functualize.workflow import ConditionalEdge, END
# Maps condition keys to target nodes
conditional = ConditionalEdge(
source="check",
condition=lambda: "success" if all_green() else "failure",
targets={
"success": "success_handler",
"failure": "failure_handler",
},
)
| Attribute | Type | Description |
|---|---|---|
source |
str |
Name of the source node |
condition |
Callable |
Returns a key into targets |
targets |
dict[str, str \| END] |
Mapping of condition keys to node names or END |
OnFailure¶
Where control goes when a step raises. Without one, a raising step stops the walk and the scope is marked failed — the behaviour every workflow has by default, and OnFailure does not change it. It adds a declared alternative, and only for the node it names.
from functualize.workflow import OnFailure, END
OnFailure(
source="deploy",
target="rollback",
when=lambda exc: isinstance(exc, DeploymentError),
)
| Attribute | Type | Description |
|---|---|---|
source |
str |
The node whose failure this routes. |
target |
str \| END |
Where to continue, or END to finish the walk without marking it failed — a cleanup path that succeeds is a success. |
when |
Callable[[BaseException], bool] \| None |
Called with the exception the step raised; routing requires a true answer. None routes every failure — the honest spelling of a catch-all. |
The chosen route is recorded and read back on replay
Never re-evaluated on resume, for the same reason ConditionalEdge's choice isn't: calling when again and discarding the answer would still run whatever side effects it has, and a failure predicate is exactly the kind that pages somebody.
END¶
A sentinel value marking the end of a workflow. Used in workflow graph definitions to indicate the termination point.
from functualize.workflow import workflow, Step, Edge, END
@workflow(
steps=[Step("work")],
edges=[Edge(source="work", target=END)],
)
def simple_workflow(config, rc):
rc.log("Work done")
Notification¶
What a registered notifier is handed when a Notify declaration fires — a target, and what happened. Five fields and no envelope: there is no message id, correlation key, priority, or retry count, because Notify declares a target and an effect, not a broker.
from functualize.workflow import Notification
@dataclass(frozen=True)
class Notification:
to: str
scope_id: str
workflow: str | None
status: str
node: str | None = None
| Attribute | Type | Description |
|---|---|---|
to |
str |
The Notify declaration's to, verbatim — never parsed by the engine. |
scope_id |
str |
The workflow walk this notification is about. |
workflow |
str \| None |
The job name, when the scope records one. |
status |
str |
The scope status that fired the notification. |
node |
str \| None |
Where the walk stopped, when it stopped somewhere — the failed node, or the gate it is blocked at. None for a walk that finished. |
A plugin implementing a notifier receives one Notification per fired Notify(on=..., to=..., provider=...) declaration on @workflow(..., notify=[...]). Delivery is at-most-once: the record that a notification fired is committed before the provider is called, so a crash can lose a notification but never send it twice.
Effect Receivers¶
A gate's acceptance can carry an effect to the outside world. On a store whose
profile declares durable_outbox=True (SQLite), the accepted candidate, its
GateDecision and the effect intent commit in one transaction, and the
intent is handed to the receiver registered for its topic — gate.accepted —
at least once. Exactly-once application is the receiver's, via the
idempotency_key:
from functualize.plugin import EffectReceiver, EffectView
class GateAuditor:
"""Receives each accepted gate, applying every key exactly once."""
topic = "gate.accepted"
def __init__(self) -> None:
self._applied: set[str] = set()
def receive(self, effect: EffectView) -> None:
if effect.idempotency_key in self._applied:
return # at-least-once delivery: a redelivery is a no-op
self._applied.add(effect.idempotency_key)
... # apply the effect
def register(app) -> None:
app.extensions.register_effect_receiver(GateAuditor())
| Member | Type | Description |
|---|---|---|
topic |
str |
The topic this receiver consumes — "gate.accepted". One receiver per topic; a second for the same topic raises ValueError. |
receive(effect) |
Callable |
Apply the effect. Returning acknowledges it; raising reschedules it with a backoff. May be called more than once for the same intent — deduplicate on effect.idempotency_key. |
EffectView carries the intent's identity: effect_id, topic, payload,
idempotency_key, attempts, and the claim fields. The payload of a
gate.accepted intent is {"request_id", "scope_id", "gate", "accepted_id",
"policy_id", "policy_version"}.
Registration is a plugin's declaration that this configuration needs a
durable outbox: it must happen before boot selects the store (a later
registration raises SubstrateInstallError, having missed the boot drain),
and boot refuses a selected store whose profile lacks durable_outbox,
naming runtime_store.url and the receivers' topics.
A runnable receiver — key-deduplicating, registered as a plugin, tested
against a real answered gate — lives in
examples/plugins/effect_receiver/.
Internal Location¶
Workflow types live in functualize._types.workflow:
_types/workflow.py— Step, Gate, AgentStep, Edge, ConditionalEdge, OnFailure, Loop, Notify, Notification, END, WorkflowShape, WorkflowDeclarationworkflow/_decorator.py—@workflowdecorator and execution engineworkflow/_validation.py— Graph validation and cycle detection
Internal API
Modules under functualize.workflow._* are implementation details. Import types from functualize.workflow instead.