Repository navigation
The Workflow Engine
Home · Architecture · Execution
In short
- A workflow is a modeller's repeatable script over networks: load, change, solve, loop until a condition, save.
- It is built with a fluent API, stored as a generated Python module, and run step by step as a durable job.
- A step is either recorded (its output is kept and replayed) or recomputed.
- A run uses only steps that an entry point announces, and reaches only a process that has its step packages.
A workflow is a modeller's repeatable script over networks: load, change, solve, loop until a condition, save. The design is docs/specs/2026-10-22-workflow-fluent-api-design.md.
Workflow(name) builds a frozen graph through chained calls, each of which checks the whole graph:
| Call | Adds |
|---|---|
add_variable(name, type, default=) |
a plain variable |
add_network_variable(name, framework=) |
a network the run pins to a version when it starts |
add_step(step_id, function, *args, **kwargs) |
a step call; with no input named it reads the previous step's output |
add_condition(id, condition, then=Branch(), otherwise=Branch()) |
a branch |
add_loop_until(id, body=LoopBody(), max_passes=, on_limit=) |
a loop, whose body calls set_finished_when(condition, value=)
|
Arguments refer to other values with Var("name"), Output("step id").field and LoopInput().
The graph is not what runs. build_module_text generates a Python module:
- a header comment and the imports;
- one entry function, whose parameters are the variables;
- one call per step, carrying
id="<step id>"; - one pass function per loop, called through
loop_until.
Writing and reading it:
-
Workflow.write(path)saves the module. -
Workflow.load_module(path)reads it back withast, never by importing it. - It rebuilds the graph with the builder's own calls, generates the module again, and compares the syntax trees.
- It refuses anything else with
NotGeneratedCodeError. - The parser accepts an import only from a module that a step entry point announces.
-
@step(name=, version=, recovery=)marks a function as a step. - A call with
id=is a step of the active run. A call without is a plain call, which is how a step is tested. - A step returns a
Network, a plain value, or a frozen pydantic model of these. - A run uses only steps announced under the
tz_ps_workflow.stepsentry point, whose value names a module. -
discover_stepsimports each announced module once per process and collects its top-level steps. -
Workflow.runrefuses a step from a module that failed to import, from an unannounced module, or with a name two packages announce.
| Recovery | On a replay | Built-in steps (tz_ps_workflow/steps.py) |
|---|---|---|
RECORD (the default) |
answered from its record; the function does not run |
load, solve, save
|
RECOMPUTE |
runs again |
check_predicate, update_component, add_constraint, update_constraint, remove_constraint
|
Workflow.run(project=, **values):
- checks the project, the steps and the values;
- writes the module text to
workflow-code/<sha256>.pyin the object store; - creates the record in the
WorkflowRunStore, with the names and versions of the steps it needs; - enqueues
run_workflow_durably(run_id)on this process's own run queue; - returns a
WorkflowRunhandle at once.
WorkflowEngine.run reads everything from storage, on the first attempt too:
- the record;
- the module, by its hash;
- a strict parse (
parse_generated_text) that accepts only text equal, byte for byte, to what the generator writes.
Only then does it execute the module. A missing, changed or foreign module fails the run with a note and runs no step.
The engine runs the entry function in a thread, and each step call goes through _engine.py:
- Before each step it reads the cancel flag from the store.
- A
RECORDstep goes through theStepRecorder, which isDBOS.run_stepnamed by the step key.- What DBOS records is a small JSON record.
- The output itself is written by
StepOutputstoworkflow-run/<run_id>/<step key>/recorded/output.json, with eachNetworkin it as a handover beside it. - Nothing is pickled.
- On a replay, the engine reads the output back through the step's return type.
- A
RECOMPUTEstep is called directly. - Step statuses are batched and written when a
RECORDstep starts, when it ends and when the run ends, so a long solve shows asrunning.
A step key has one part for each loop around the step, as raise-loop/2/solve. A loop has a status row of its own.
solve does not solve inside the engine.
- It calls
Network.solve_into_handoverwith keys underworkflow-run/<run_id>/<step key>/. - The idempotency key is
workflow:<run_id>:<step key>, so a replay gets the same run. - That is an ordinary handover run: a run record,
solve_durablyon the solve queue, a solve child. - The step waits for it and returns a
SolveResultwithis_feasible,detail,objective,run_idandnetwork. - With
OnInfeasible.CONTINUE, an infeasible solve is a result and the step readsinfeasible. - Every other failed solve raises
SolveFailedError.
The sequence shows a workflow run with a load, a solve and a save.
sequenceDiagram
participant U as Caller
participant WS as WorkflowRunStore
participant OS as Object store
participant Q as Run queue of this process
participant E as WorkflowEngine
participant R as Runner
participant S as Solve workflow and child
U->>OS: write generated module by hash
U->>WS: create run as queued
U->>Q: enqueue run_workflow_durably
Q->>E: run with a DBOS step recorder
E->>OS: read module, strict parse, execute
E->>WS: mark_running
E->>E: load, a RECORD step
E->>OS: write recorded output of load
E->>E: update_component, a RECOMPUTE step
E->>WS: read cancel flag
E->>WS: record steps, solve running
E->>OS: write input handover of the step
E->>R: solve as a handover run
R->>S: solve_durably
S->>OS: publish writes the target handover
S-->>E: outcome
E->>OS: write recorded output of solve
E->>E: save, a RECORD step
E->>WS: record outcome succeeded
Ownership and take-over
- A process takes workflow runs only from its own queue, so a run reaches a process that has its step packages.
- When a run's owner stops beating, another process that has every step package claims the run in the store. The claim is a write guarded on the old owner.
- That process then moves the run's workflow to its own queue.
- A process without a package leaves a note naming it.
- The engine records a run's end only as the owner it started with.
Cancel
-
run.cancel()sets the flag, and the run stops before its next step, or after its last when the cancel came during it. - A solve already running finishes and keeps its output.
Reading a run
-
get_workflow_run(run_id)gives a handle in any process. - Over HTTP, a workflow run can be read but not started or cancelled.
-
WorkflowRunStatusandStepStatusare intz_ps_models/workflow_run.py. - A step is
running,succeeded,infeasible,not_converged,failedorcancelled. - A cancel is a flag on the run, read as
running, cancel requesteduntil the run stops.
The diagram shows every transition of a workflow run.
stateDiagram-v2
[*] --> queued: create
queued --> running: mark_running
queued --> failed: not submitted or stored code cannot run
running --> succeeded: entry function returned
running --> not_converged: loop reached its pass limit
running --> failed: a step raised
running --> cancelled: cancel read before a step or after the last
succeeded --> [*]
not_converged --> [*]
failed --> [*]
cancelled --> [*]
Using the app
Architecture
- Overview
- Python API
- Domain model
- Backend core
- Composition root and config
- Expression language internals
- Storage
- Execution
- Imports and exports
- Workflow engine
- HTTP API and contracts
- Frontend and desktop
- Build, CI and release
Contributing