Docs menu

Workflow Patterns

A Workflow is a sequence of steps with evolving state - branching, looping, and composition at each step, brought as one whole. It carries durable state and can pause for approval, run steps in parallel, and resume after interruption.

Two execution models

  • .Flow() - a declarative graph. Each step returns the next operation to run (or END), and the step named after that operation runs when it finishes. Preferred for anything with branching.
  • .Execute() - direct imperative control. Simpler for tight loops.
import { END, START, Workflow } from 'jsr:@fathym/steward/workflows';

const BuildAndDeploy = Workflow('BuildAndDeploy', 'Build, test, deploy')
  .Input(z.object({ repo: z.string() }))
  .State(z.object({ artifact: z.string().optional() }))
  .Operations({ Clone: CloneOp, Build: BuildOp, Test: TestOp, Deploy: DeployOp })
  .Flow({
    [START]: (ctx) => ctx.Operations.Clone({ repo: ctx.Input.repo }),
    Clone: (ctx) => ctx.Operations.Build({ path: ctx.StepResults.Clone!.path }),
    Build: (ctx) => {
      ctx.SetState({ artifact: ctx.StepResults.Build!.artifact });
      return ctx.Operations.Test({ path: ctx.StepResults.Clone!.path });
    },
    Test: (ctx) =>
      ctx.StepResults.Test!.passed
        ? ctx.Operations.Deploy({ artifact: ctx.StepResults.Build!.artifact })
        : END,
    Deploy: () => END,
  });

ctx.StepResults accumulates each step's typed output; ctx.SetState(...) evolves the workflow's state.

Gates - approval points

Wrap the next operation in Wait(gate, next) to pause for a human decision before it runs - the confidence layer in workflow form.

Test: (ctx) => Wait(
  HumanApproval({ Approvers: ['ops-lead'] }),
  ctx.Operations.Deploy({ artifact: ctx.StepResults.Build!.artifact }),
),

Production deploys gate; staging auto-deploys. Nothing consequential proceeds without the decision.

Parallel execution

Parallel(...targets) runs its targets concurrently and waits for all of them. The targets are the workflow's operations:

Build: (ctx) => Parallel(
  ctx.Operations.BuildLinux(),
  ctx.Operations.BuildMac(),
  ctx.Operations.BuildWin(),
),

Persistence and resume

.Persistence({ Backend: 'deno-kv', KeyPrefix: ['deploy'] }) makes a long-running workflow durable - it survives restarts and can resume where it left off. Backend is 'deno-kv' or 'memory'.

Agents in flows

Flow functions can reach ctx.Agents (set up via .Agents({})); await the handle, use it, and always Close().

Running workflows

const handle = await WorkflowRunner(BuildAndDeploy).Run({ repo: 'acme/app' });
await handle.Pause();
await handle.Resume();
await handle.Stop();

A Workflow returns a handle; the caller controls its lifecycle. Workflows can also be registered in a Steward for discovery.


On fathym.com: Holonic →

On this page