Skip to content

The Workflow Engine

James Dunkerley edited this page Nov 20, 2026 · 1 revision

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.

What a workflow is

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.

Builder and generated module

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 with ast, 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.

Steps and the registry

  • @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.steps entry point, whose value names a module.
  • discover_steps imports each announced module once per process and collects its top-level steps.
  • Workflow.run refuses 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

Running

Workflow.run(project=, **values):

  1. checks the project, the steps and the values;
  2. writes the module text to workflow-code/<sha256>.py in the object store;
  3. creates the record in the WorkflowRunStore, with the names and versions of the steps it needs;
  4. enqueues run_workflow_durably(run_id) on this process's own run queue;
  5. returns a WorkflowRun handle 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 RECORD step goes through the StepRecorder, which is DBOS.run_step named by the step key.
    • What DBOS records is a small JSON record.
    • The output itself is written by StepOutputs to workflow-run/<run_id>/<step key>/recorded/output.json, with each Network in 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 RECOMPUTE step is called directly.
  • Step statuses are batched and written when a RECORD step starts, when it ends and when the run ends, so a long solve shows as running.

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.

The solve step

solve does not solve inside the engine.

  • It calls Network.solve_into_handover with keys under workflow-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_durably on the solve queue, a solve child.
  • The step waits for it and returns a SolveResult with is_feasible, detail, objective, run_id and network.
  • With OnInfeasible.CONTINUE, an infeasible solve is a result and the step reads infeasible.
  • 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
Loading

Ownership, take-over and cancel

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.

Statuses

  • WorkflowRunStatus and StepStatus are in tz_ps_models/workflow_run.py.
  • A step is running, succeeded, infeasible, not_converged, failed or cancelled.
  • A cancel is a flag on the run, read as running, cancel requested until 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 --> [*]
Loading

Clone this wiki locally