---
title: "Workflows"
description: "Author durable multi-step workflows with retries, sleeps, signal waits, and idempotent starts."
---

> Documentation Index
> Fetch the complete documentation index at: https://buildrforge.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Workflows

While a tool call completes within a single sandbox execution, a **workflow** is durable extension code that Forge keeps in its database, so it can run across hours or days, sleep between steps, and automatically retry steps that fail.

This section covers:

- [Steps and replay](https://buildrforge.com/docs/extensions/workflows/steps-and-replay/): how a run replays its body, and the rules for `step.do`, `step.sleep`, and `step.sleepUntil`.
- [Signals](https://buildrforge.com/docs/extensions/workflows/signals/): how a run waits for an answer, and how tools and members answer it.
- [Runs](https://buildrforge.com/docs/extensions/workflows/runs/): how tools and members start runs, and how members inspect and stop them.
- [State machines](https://buildrforge.com/docs/extensions/workflows/state-machines/): how to drive a state machine from a workflow, with Procore's submittal workflow as a worked example.

## Workflow declarations in `index.ts`

Import `workflow` (and optionally `WorkflowStepError`) from `@buildr/sdk` and include your workflows under `workflows` in `defineExtension`:

```ts
import {
  boolean,
  defineExtension,
  integer,
  literal,
  object,
  string,
  tool,
  workflow,
  WorkflowStepError,
} from "@buildr/sdk";

const syncVendorCatalog = workflow(
  {
    description: "Imports vendor records in stages with a cooldown check",
    input: object({
      batchId: string(),
      delaySeconds: integer().optional(),
    }),
    output: object({
      importedCount: integer(),
      verified: boolean(),
    }),
    signals: {
      approval: object({ decision: literal("approve", "reject") }),
    },
  },
  async ({ runId, step }, { batchId, delaySeconds = 60 }) => {
    const importedCount = await step.do(
      "import-batch",
      async (s) => {
        const page = (await s.forge.customRecords.listRepoCustomRecordsAtBranch({
          repo_id: s.repo.id,
          branch: s.branch,
          model_api_name: "vendor_standards__vendor",
        })) as { data: ReadonlyArray<unknown> };
        return page.data.length;
      },
      {
        retries: { limit: 3, delay: "10 seconds", backoff: "exponential" },
        timeout: "20 seconds",
      },
    );

    await step.sleep("wait-before-verification", `${delaySeconds} seconds`);

    const verified = await step.do("verify-import", async (s) => {
      const branch = (await s.forge.branches.getRepoBranch({
        repo_id: s.repo.id,
        branch: s.branch,
      })) as { head_commit_id: string };
      return branch.head_commit_id !== "";
    });

    return { importedCount, verified };
  },
);

const startCatalogSync = tool(
  {
    description: "Starts the vendor catalog workflow for a batch",
    input: object({ batchId: string() }),
    output: object({ runId: string(), created: boolean() }),
  },
  async (ctx, { batchId }) =>
    ctx.workflows.start("syncVendorCatalog", { batchId }, { key: `batch-${batchId}` }),
);

export default defineExtension(
  {},
  {
    tools: { startCatalogSync },
    workflows: { syncVendorCatalog },
  },
);
```

Source: https://buildrforge.com/docs/extensions/workflows/index.mdx
