Fathym
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. Keys are step names; each step returns the next step (or END). Preferred for anything with branching.
  • .Execute() - direct imperative control. Simpler for tight loops.
const BuildAndDeploy = Workflow('BuildAndDeploy', 'Build, test, deploy')
  .Input(z.object({ repo: z.string() }))
  .State(z.object({ artifact: z.string().optional() }))
  .Flow({
    [START]: () => 'Clone',
    Clone: async (ctx) => {
      /* ... */ return 'Build';
    },
    Build: async (ctx) => {
      ctx.SetState({ artifact: 'out.tar' });
      return 'Test';
    },
    Test: () => 'Deploy',
    Deploy: () => END,
  });

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

Gates - approval points

Wrap a step in Wait(...) to pause for a human decision before continuing - the confidence layer in workflow form.

Deploy: () => Wait(HumanApproval({ approvers: ["ops-lead"] }), "Release"),

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

Parallel execution

Parallel(steps, finalize) runs steps concurrently and gathers them before moving on.

Build: () => Parallel([BuildLinux, BuildMac, BuildWin], "Bundle"),

Persistence and resume

.Persistence({ Enabled: true, Store: "redis", KeyPrefix: "deploy:" }) makes a long-running workflow durable - it survives restarts and can resume where it left off.

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 this page