Skip to content
danfry1Public

About

Durable workflow execution for TypeScript

Resources

Contributing

Security policy

Stars

43 stars

Watchers

0 watching

Forks

Repository files navigation

Reflow

Durable workflow execution for TypeScript. Define multi-step workflows with full type safety, automatic retries, and crash recovery via stale-run reclamation. Pause for days with .sleep() or wait on a webhook with .waitForEvent() — the process doesn't need to stay alive. Start on a SQLite file with no external services; move to PostgreSQL when workers span machines, with the same workflow code.

import { createWorkflow, createEngine } from 'reflow-ts'
import { SQLiteStorage } from 'reflow-ts/sqlite-node-builtin' // Node's built-in SQLite
import { z } from 'zod' // or valibot, arktype, etc.

const orderWorkflow = createWorkflow({
  name: 'order-fulfillment',
  input: z.object({ orderId: z.string(), amount: z.number() }),
})
  .step('charge', async ({ input, idempotencyKey }) => {
    // Steps run at least once; the key lets Stripe drop a repeated charge
    const charge = await stripe.charges.create({ amount: input.amount }, { idempotencyKey })
    return { chargeId: charge.id }
  })
  .step('fulfill', async ({ prev }) => {
    const shipment = await warehouse.ship(prev.chargeId)
    return { trackingNumber: shipment.tracking }
  })
  .step('notify', async ({ prev, input }) => {
    await email.send(input.orderId, `Shipped! Track: ${prev.trackingNumber}`)
  })
  .onFailure(async ({ error, stepName, input }) => {
    await alerts.send(`Order ${input.orderId} failed at ${stepName}: ${error.message}`)
  })

const storage = new SQLiteStorage('./workflows.db')
const engine = createEngine({ storage, workflows: [orderWorkflow] })
await engine.start() // Initializes storage and starts polling

// Type-safe: only accepts 'order-fulfillment' with the correct input shape
await engine.enqueue('order-fulfillment', { orderId: 'ORD_123', amount: 5000 })

The Problem

You have a multi-step operation — a signup, an import, an AI pipeline. You write it as a normal async function:

app.post('/signup', async (req, res) => {
  await createAccount(req.body)     // ✅ done
  await chargeStripe(req.body)      // ✅ done
  // 💥 process crashes, deploy happens, laptop sleeps
  await sendWelcomeEmail(req.body)  // ❌ never runs
})

Now the user is charged but never got their welcome email. Worse — you don't know which steps completed. Do you re-run everything? Then they get double-charged.

The usual fix is to build manual checkpoint logic: state columns, retry loops, deduplication. That's 200 lines of infrastructure code that's hard to test and easy to get wrong.

Reflow makes each step durable. If the process crashes after step 2 of 5, a new engine instance can reclaim the stale run after its lease expires and pick up at step 3. Active workers heartbeat their lease while they run, completed steps are never re-executed, and each step's output is persisted in SQLite or Postgres — no other services required.

Who Is This For?

Solo devs and small teams who need reliable multi-step workflows but don't want to run Temporal clusters or pay for cloud workflow services.

  • SaaS apps — Background jobs that must complete: signup flows, billing, provisioning
  • CLI tools — Long-running imports or migrations that should resume after interruption
  • AI pipelines — LLM calls that cost money — don't re-run a $0.05 call because the next step failed
Reflow Temporal Inngest
Infrastructure A SQLite file, or your Postgres Temporal Server + DB Cloud service
Type safety Full end-to-end Partial Partial
Setup npm install reflow-ts Cluster deployment Account + SDK
Best for Apps, CLIs, and AI agents, from one process to many machines Very large, multi-language systems Serverless

Don't use Reflow when:

  • You need workflows written in several languages, or event histories in the millions per run
  • You need sub-second pickup of work enqueued by a different process (it waits for the next poll)
  • You're already running Temporal or similar

Performance

Reflow's overhead is small — most of the cost in real workflows is your own step logic. The numbers below measure the engine and storage layers alone (trivial handlers, no-op input schema), processed end-to-end (enqueue → durable completion).

Storage Steps/run Runs/sec Steps/sec
in-memory 1 ~36,000 ~36,000
in-memory 5 ~25,000 ~124,000
SQLite (Bun) 1 ~4,600 ~4,600
SQLite (Bun) 5 ~2,600 ~13,000

2,000 runs/scenario, concurrency 25, Bun on an Apple Silicon laptop. Reproduce with bun run bench (benchmarks/). Numbers vary by machine and runtime; SQLite figures reflect durable, fsync-backed persistence in WAL mode.

Install

# Node.js 22.13+ or Bun — SQLite is built in, nothing else to install
npm install reflow-ts        # or: bun add reflow-ts

# Older Node (18.18+), or to use better-sqlite3
npm install reflow-ts better-sqlite3

# PostgreSQL, for workers on many machines
npm install reflow-ts pg

Then pick the storage adapter for your runtime. Each SQLite adapter takes a file path and behaves the same; only the driver differs.

// Node.js 22.13+ — built-in node:sqlite
import { SQLiteStorage } from 'reflow-ts/sqlite-node-builtin'
const storage = new SQLiteStorage('./reflow.db')

// Bun — built-in bun:sqlite
import { SQLiteStorage } from 'reflow-ts/sqlite-bun'
const storage = new SQLiteStorage('./reflow.db')

// Node.js 18.18+ — better-sqlite3
import { SQLiteStorage } from 'reflow-ts/sqlite-node'
const storage = new SQLiteStorage('./reflow.db')

// PostgreSQL
import { PostgresStorage } from 'reflow-ts/postgres'
const storage = new PostgresStorage({ connectionString: process.env.DATABASE_URL })

node:sqlite is still marked experimental by Node, which prints an ExperimentalWarning once at startup; it works unflagged from Node 22.13 and 23.4 (Node 22.5–22.12 need --experimental-sqlite). If you'd rather not see the warning, or you're on an older Node, use reflow-ts/sqlite-node with better-sqlite3. On Node 18–21, pin better-sqlite3@12: version 13 requires Node 22, and Reflow accepts better-sqlite3 >= 9.

Reflow uses Standard Schema for input validation, so you can bring any compatible library:

bun add zod        # or
bun add valibot    # or
bun add arktype    # or any Standard Schema-compatible library

Core Concepts

Workflows

A workflow is a named sequence of steps with a validated input schema. Any Standard Schema-compatible library works (Zod, Valibot, ArkType, etc.).

const workflow = createWorkflow({
  name: 'send-welcome',
  input: z.object({ userId: z.string(), email: z.email() }),
})
  .step('create-account', async ({ input }) => {
    // input is typed as { userId: string, email: string }
    return { accountId: await createAccount(input.userId) }
  })
  .step('send-email', async ({ prev, input, signal }) => {
    // prev is typed as { accountId: string }
    // input is still available
    // signal is aborted on cancellation / timeout
    await sendEmail(input.email, `Welcome! Your account: ${prev.accountId}`, { signal })
  })

Each .step() receives:

  • input — the validated workflow input (same for every step)
  • prev — the return value of the previous step (undefined for the first step)
  • steps — typed access to all previously completed step results by name (e.g. steps.charge.chargeId)
  • signal — an AbortSignal that is aborted when the run is cancelled, its lease is lost, or the step times out
  • complete(value?) — finish the workflow early, skipping remaining steps (optionally persist a final value)
  • runId, attempt — the run's id, and which attempt at this step this is (from 1)
  • idempotencyKey — stable for this step of this run (${runId}:${stepName}); pass it to payment and email APIs so a step that reruns after a crash isn't charged or sent twice

The builder is immutable — each .step() returns a new workflow instance, so you can safely branch:

const base = createWorkflow({ name: 'base', input: z.object({}) })
const withLogging = base.step('log', async () => { /* ... */ })
const withMetrics = base.step('metric', async () => { /* ... */ })
// base, withLogging, and withMetrics are all independent

Workflows as Functions (beta)

The same workflow can be written as one async function — new in 0.11, so its API may still change in a minor release. Each step call is durable, and the code between them is plain TypeScript, so branches, loops over data, running steps together, and recovering from a failure need nothing new:

const fulfilOrder = createWorkflow({
  name: 'fulfil-order',
  input: z.object({ orderId: z.string(), items: z.array(z.string()) }),
  events: { shipped: z.object({ tracking: z.string() }) },
}, async ({ input, step }) => {
  const payment = await step.run('charge', ({ idempotencyKey }) => charge(input.orderId, idempotencyKey))

  // One step per item, all at once, however many items there are
  await Promise.all(input.items.map((item) => step.run(`print-${item}`, () => printer.order(item))))

  const shipped = await step.waitForEvent('shipped', { timeout: '10d' }) // null if it times out
  if (!shipped) return await step.run('chase', () => support.chase(input.orderId))

  try {
    await step.run('invoice', () => accounting.invoice(input.orderId))
  } catch (error) {
    if (!(error instanceof StepFailedError)) throw error
    await step.run('refund', () => refund(payment.id))
  }
  return { tracking: shipped.tracking }
})

When a run resumes, after a sleep, a wait, a backoff, or a crash, the function runs again from the top. Each step that already finished returns its saved result without running, so only code between steps runs again. Keep requests, writes, and anything time- or random-dependent inside step.run(). Steps are matched by name, so give each one a name of its own, such as `print-${item}` in a loop. Retries, timeouts, caching, hooks, the dashboard, and storage all work as they do for chained steps. See Workflows as Functions.

Engine

The engine connects workflows to storage and handles execution.

const storage = new SQLiteStorage('./workflows.db')
const engine = createEngine({ storage, workflows: [orderWorkflow, emailWorkflow] })

// start() initializes storage and begins polling
await engine.start(1000) // poll every 1000ms (default)

// Enqueue a run
const run = await engine.enqueue('order-fulfillment', { orderId: 'ORD_1', amount: 100 })
// run.id is a unique identifier for this run

// Stop polling (waits for in-flight work to finish)
await engine.stop()

By default, claimed runs use a 30_000ms lease. If a worker crashes and stops updating a run, a later tick() can reclaim it after that lease expires:

const engine = createEngine({
  storage,
  workflows: [orderWorkflow],
  runLeaseDurationMs: 30_000,
  heartbeatIntervalMs: 10_000,
})

The engine heartbeats active runs while they execute so long-running steps do not get reclaimed before they finish.

enqueue() is fully type-safe — it only accepts registered workflow names and their corresponding input types:

engine.enqueue('order-fulfillment', { orderId: 'x', amount: 1 }) // OK
engine.enqueue('order-fulfillment', { wrong: 'shape' })          // Type error
engine.enqueue('nonexistent', {})                                 // Type error

If callers may retry enqueue(), give the run an idempotency key:

const run = await engine.enqueue(
  'order-fulfillment',
  { orderId: 'ORD_1', amount: 100 },
  { idempotencyKey: 'checkout:ORD_1' },
)

Reusing the same idempotency key for the same workflow returns the existing run instead of creating a duplicate. Reusing it with different input throws.

Retry

Steps can be configured with automatic retry and backoff:

.step('call-api', {
  retry: {
    maxAttempts: 5,
    backoff: 'exponential', // or 'linear'
    initialDelayMs: 200,    // 200ms, 400ms, 800ms, 1600ms...
  },
  handler: async ({ input }) => {
    const response = await fetch(`https://api.example.com/${input.id}`)
    if (!response.ok) throw new Error(`API error: ${response.status}`)
    return await response.json()
  },
})

Without retry config, a failing step immediately fails the entire workflow run.

On a started engine, the wait between attempts is durable: the step is saved as retrying and the run sleeps with its lease and concurrency slot released, so a long backoff holds no worker and survives a restart. An engine driven only by tick() (and testEngine) waits in process instead.

Caching Steps

A step can reuse the result an earlier run computed, rather than running again — for a paid API or an LLM call that separate runs would repeat for the same data:

.step('summary', {
  cache: '24h', // reused when the input and earlier steps' outputs match
  handler: async ({ prev }) => ({ text: await llm.summarize(prev.html) }),
})

Pass { ttl, key, version } to choose what counts as the same (key: ({ input }) => input.userId) and to discard results from older code (version: 2). Only completed steps are cached. The caching guide has the details.

Failure Handling

Attach an onFailure handler for compensation logic (saga pattern):

const workflow = createWorkflow({ name: 'transfer', input: schema })
  .step('debit', async ({ input }) => {
    return await debitAccount(input.from, input.amount)
  })
  .step('credit', async ({ input }) => {
    return await creditAccount(input.to, input.amount)
  })
  .onFailure(async ({ error, stepName, input }) => {
    if (stepName === 'credit') {
      // Debit succeeded but credit failed — reverse the debit
      await creditAccount(input.from, input.amount)
    }
    await notifyOps(`Transfer failed at ${stepName}: ${error.message}`)
  })

Steps Context

Each step handler receives a typed steps object with access to all previously completed step results by name. No need to forward data through prev across intermediate steps:

const workflow = createWorkflow({ name: 'pipeline', input: schema })
  .step('fetch', async ({ input }) => {
    return { url: input.url, body: await fetchPage(input.url) }
  })
  .step('parse', async ({ prev }) => {
    return { title: extractTitle(prev.body), links: extractLinks(prev.body) }
  })
  .step('save', async ({ steps }) => {
    // Access any previous step directly — no forwarding needed
    await save(steps.fetch.url, steps.parse.title, steps.parse.links)
  })

The steps object is a frozen, deep-cloned snapshot — mutations to prev in one step will never affect what later steps see through steps.

Early Completion

A step can finish the workflow early by calling complete(), skipping all remaining steps:

const workflow = createWorkflow({ name: 'conditional', input: schema })
  .step('check', async ({ input, complete }) => {
    if (!input.eligible) {
      return complete({ reason: 'ineligible' })
    }
    return { eligible: true }
  })
  .step('process', async ({ prev }) => {
    // Only runs if check didn't call complete()
    return await doWork(prev)
  })

The optional value passed to complete() is persisted as the step result and visible via getRunStatus(). Early completion is crash-safe — if the engine crashes after saving the step but before marking the run completed, recovery will detect the early-complete marker and finish the run without re-executing later steps.

Parallel Steps

Run multiple independent steps concurrently with .parallel(). Each branch is a named handler that runs at the same time as its siblings; the next step receives a merged record of all branch outputs as prev:

const pipeline = createWorkflow({ name: 'pipeline', input: z.object({ url: z.string() }) })
  .step('fetch', async ({ input }) => ({ body: await fetchPage(input.url) }))
  .parallel({
    summary: async ({ prev }) => ({ text: await summarize(prev.body) }),
    keywords: async ({ prev }) => ({ tags: await extractKeywords(prev.body) }),
    images: async ({ prev }) => ({ urls: await extractImages(prev.body) }),
  })
  .step('save', async ({ prev }) => {
    // prev is fully typed: { summary: { text }, keywords: { tags }, images: { urls } }
    await save(prev.summary.text, prev.keywords.tags, prev.images.urls)
  })

Branches accept the same { retry, timeoutMs, handler } config form as .step(), so each branch can have its own retry and timeout policy:

.parallel({
  flaky: {
    retry: { maxAttempts: 3, backoff: 'exponential', initialDelayMs: 100 },
    handler: async () => await callFlakyApi(),
  },
  stable: async () => await callStableApi(),
})

Semantics:

  • Fail-fast. When one branch fails (after exhausting its own retries), siblings receive signal.abort() and the run is marked failed. onRunFailed and onFailure report the branch that actually caused the failure, not a sibling aborted by the abort propagation.
  • Crash recovery is per-branch. If the engine crashes after some branches have persisted their results, recovery skips those branches and only re-runs the missing ones. Side effects in already-completed branches do not fire twice.
  • No complete() inside a branch. Calling complete() from a parallel branch throws ParallelCompleteError — early completion is only meaningful in sequential context.
  • Each branch must be idempotent. Like sequential steps, a branch may run multiple times across crash recoveries before its result is persisted.
  • steps is a frozen snapshot. All sibling branches see the same steps view taken before the group started; they cannot observe each other's outputs mid-flight.

Durable Sleep

Pause a workflow between steps for a duration that survives process exit, deploy, or crash:

const trial = createWorkflow({ name: 'trial', input: z.object({ userId: z.string() }) })
  .step('start', async ({ input }) => activate(input.userId))
  .sleep('wait-two-weeks', '14d')
  .step('convert', async ({ input }) => chargeOrExpire(input.userId))

The run is persisted as sleeping with its lease released, so the process does not need to stay alive — any engine instance reclaims and resumes it once the time elapses. prev passes through unchanged. Durations are milliseconds or a unit-suffixed string ('500ms', '30s', '24h', '7d').

The wake target is stored the first time the sleep is reached, so a run that crashes and is reclaimed ten times still waits the duration you asked for — not ten times it.

Waiting for External Events

Suspend a workflow until something outside it happens — a webhook, an approval, a human action:

const refund = createWorkflow({ name: 'refund', input: z.object({ orderId: z.string() }) })
  .step('request', async ({ input }) => notifyReviewer(input.orderId))
  .waitForEvent('approval', {
    schema: z.object({ approved: z.boolean(), reviewer: z.string() }),
    timeoutMs: 48 * 60 * 60 * 1000,
  })
  .step('settle', async ({ prev }) => (prev.approved ? issueRefund() : close()))

// From your webhook handler, possibly in a different process:
await engine.sendEvent(runId, 'approval', { approved: true, reviewer: 'alice' })

Like sleep, the run is persisted (as waiting) with its lease released and resumes on any instance. The payload — validated against schema, if given — becomes the next step's prev.

Delivery is order-independent: an event sent before the run reaches the wait is buffered durably and consumed when it gets there, so you don't have to race your webhook against your workflow. With timeoutMs, the run fails with WaitTimeoutError if nothing arrives in time.

Conditional Steps

Skip a step based on the run's state with a when predicate:

const checkout = createWorkflow({ name: 'checkout', input: z.object({ premium: z.boolean() }) })
  .step('base', async () => ({ tier: 'base' as const }))
  .step('upgrade', {
    when: ({ input }) => input.premium,
    handler: async () => ({ tier: 'premium' as const }),
  })
  .step('finalize', async ({ prev }) => prev.tier)

When the predicate returns false the step is skipped, prev passes through unchanged, and the skip is persisted — so it is never re-evaluated on a crash-recovery replay against state that may have changed since.

The types follow the skip rather than assuming the step ran: prev widens to a union including the passed-through value, and the step's entry in steps becomes optional. Reading steps.upgrade.tier is a compile error, not a runtime surprise.

Run Status

Query the status of any run and its step results:

const run = await engine.enqueue('order-fulfillment', { orderId: 'ORD_1', amount: 100 })

// Later...
const info = await engine.getRunStatus(run.id)
if (info) {
  info.run.status     // 'pending' | 'running' | 'sleeping' | 'waiting' | 'completed' | 'failed' | 'cancelled'
  info.run.output     // a completed run's final output
  info.run.error      // a failed run's { stepName, message, code }
  info.run.waitingFor // the event a waiting run is waiting for
  info.run.startedAt  // first claimed; finishedAt once it is done
  info.steps          // StepResult[] — each step's output, error, attempts, and start time
}

await engine.countRuns()               // { pending: 3, running: 1, ..., failed: 2 }
await engine.prune({ olderThan: '30d' }) // delete runs that finished over 30 days ago

Dashboard

See what your runs are doing — what is waiting and on what, what failed and why, where each run spent its time:

npx reflow-ts dashboard ./reflow.db   # read-only, on 127.0.0.1

The CLI opens SQLite files; for Postgres, mount the dashboard in your app as below.

Or serve it from your own app, behind a password or your own sign-in, with cancel, resume, and live updates. Nothing is hosted elsewhere; your run data stays on your server.

import { basicAuth, createDashboard } from 'reflow-ts/dashboard'

const dashboard = createDashboard({
  storage,
  engine,
  authorize: basicAuth({ username: 'admin', password: process.env.REFLOW_DASHBOARD_PASSWORD }),
})
Bun.serve({ port: 4400, fetch: dashboard }) // or Hono, Next.js, toNodeHandler() for Express

The dashboard guide covers mounting it in each server, using your app's own sign-in, and opening a production database over SSH.

Hooks

Add observability with lifecycle hooks:

const engine = createEngine({
  storage,
  workflows: [orderWorkflow],
  hooks: {
    onRunStart: ({ runId, workflow }) => { /* ... */ },
    onStepStart: ({ runId, workflow, stepName }) => { /* ... */ },
    onStepComplete: ({ runId, workflow, stepName, output, attempts }) => {
      console.log(`Step ${stepName} completed in ${attempts} attempt(s)`)
    },
    onRunComplete: ({ runId, workflow, output }) => {
      // `output` is the workflow's final result
      metrics.increment('workflow.completed', { workflow })
    },
    onRunFailed: ({ runId, workflow, stepName, error }) => {
      alerting.notify(`${workflow} failed at ${stepName}: ${error.message}`)
    },
    onError: (error) => {
      // Background failures (poll cycles, scheduled enqueues) and errors thrown by
      // hooks, stream consumers, or onFailure, wrapped in HookError
      console.error('Engine error:', error)
    },
  },
})

Hooks may be synchronous or async. An async hook is awaited before the engine moves on, so you can use it to flush a metric, persist an audit row, or apply backpressure. A hook that throws (or rejects) never affects engine state: the error is reported to onError as a HookError, so a broken observer can't fail a workflow. onStepSkipped also fires when a step's when predicate returns false.

Streaming Results

Hooks are push-based callbacks. When you'd rather pull results — to apply backpressure, rate-limit, or pipe completions into a producer/consumer loop — use engine.stream(). It returns an async iterable of EngineEvents:

const engine = createEngine({ storage, workflows: [importWorkflow] })
await engine.start()

for await (const event of engine.stream()) {
  if (event.type === 'runComplete') {
    await pipeline.push(event.output) // process each result as it lands
  }
}

Each call to engine.stream() returns an independent stream. The event is a discriminated union (runStart, stepStart, stepSkipped, stepComplete, runComplete, runFailed), so TypeScript narrows event.output, event.error, etc. once you check event.type.

Backpressure. By default the stream buffers without bound and never slows the engine. Pass bufferSize to pace the engine against a slow consumer — the engine pauses once the buffer is full and resumes as you pull:

// The engine won't start the next unit of work until you consume the last one.
for await (const event of engine.stream({ bufferSize: 1 })) {
  await slowlyHandle(event)
}

Cleanup is automatic. Breaking out of the loop (or await using stream = engine.stream()) unsubscribes from the engine, and engine.stop() ends every open stream so consumer loops terminate cleanly.

Step Timeouts

Prevent steps from hanging indefinitely:

.step('call-external-api', {
  timeoutMs: 5000, // Fail after 5 seconds
  handler: async ({ input }) => {
    return await fetch(`https://slow-api.example.com/${input.id}`)
  },
})

Timeouts can also be set via the retry config:

.step('flaky-service', {
  retry: {
    maxAttempts: 3,
    backoff: 'exponential',
    initialDelayMs: 500,
    timeoutMs: 10000, // Each attempt times out after 10s
  },
  handler: async ({ input }) => { /* ... */ },
})

Step-level timeoutMs takes precedence over retry.timeoutMs.

Concurrency

By default, the engine processes one run at a time. Set concurrency to execute several runs at once:

const engine = createEngine({
  storage,
  workflows: [orderWorkflow],
  concurrency: 5, // Execute up to 5 runs at once (default: 1)
})

With concurrency: 5, the engine keeps up to 5 runs executing at once. Slots are independent: when a run finishes, its slot is refilled straight away, so one slow run never holds the others idle. Steps within a single run still execute sequentially.

To cap one workflow across every process sharing the storage — for a rate-limited API, say — set concurrency on the workflow:

const sync = createWorkflow({ name: 'sync-crm', input: schema, concurrency: 2 })

Runs past the limit wait their turn as pending. All the built-in adapters support it; a custom adapter must set supportsConcurrencyLimits, or createEngine throws ConfigError.

Run Cancellation

Cancel any run that hasn't finished — pending, running, sleeping, or waiting:

const run = await engine.enqueue('order-fulfillment', { orderId: 'ORD_1', amount: 100 })

const cancelled = await engine.cancel(run.id)
// true if cancelled, false if it doesn't exist or has already finished

Cancellation aborts the current step's AbortSignal immediately and prevents later steps from starting. If a handler ignores the signal, its underlying work may continue outside Reflow, but the run remains cancelled. engine.resume(runId) re-queues a failed or cancelled run from the step where it stopped.

If your step handler cooperates with the provided AbortSignal, cancellation can stop it immediately:

.step('fetch-profile', async ({ input, signal }) => {
  const response = await fetch(`https://api.example.com/users/${input.userId}`, { signal })
  return await response.json()
})

Scheduled Workflows

Enqueue workflows on a recurring interval:

// Enqueue a cleanup workflow every hour
const key = await engine.schedule('cleanup', { olderThanDays: 30 }, 60 * 60 * 1000)

// Stop it, for the whole fleet
await engine.unschedule(key)

Fires on a fixed interval or a cron expression:

await engine.schedule('cleanup', input, { every: '1h' })
await engine.schedule('report',  input, { cron: '0 9 * * 1-5' })  // 09:00 weekdays, UTC

Schedules are stored, not held in memory, so they survive a restart or a deploy — any engine instance running that workflow picks up the next firing. Registering the same key again updates that schedule in place (preserving its cadence unless the interval changed), so calling this unconditionally at startup on every worker is the intended usage.

Each due firing is claimed atomically, so a schedule shared by N instances still produces one run per interval, not N. A worker only claims schedules for workflows it has registered. Occurrences missed while the fleet was down are skipped, not backfilled.

Crash Recovery

Reflow automatically resumes workflows from the last completed step. If your process crashes after step 2 of 5, a later engine instance can reclaim the stale running run after runLeaseDurationMs and continue at step 3 — completed steps are never re-executed.

// Process crashes here after 'charge' completed but before 'fulfill'
// On restart, the engine claims the run and skips 'charge'
await engine.start()

Storage

Reflow ships with three SQLite adapters, one per driver, a PostgreSQL adapter, and an in-memory adapter for tests:

SQLiteStorage — for Bun runtime. Uses the built-in bun:sqlite module with zero native dependencies.

import { SQLiteStorage } from 'reflow-ts/sqlite-bun'

const storage = new SQLiteStorage('./workflows.db')

SQLiteStorage — for Node.js. Uses better-sqlite3 (native addon). Persists to disk, uses WAL mode.

import { SQLiteStorage } from 'reflow-ts/sqlite-node'

const storage = new SQLiteStorage('./workflows.db')

SQLiteStorage — for Node.js with no native dependencies, using the built-in node:sqlite module (Node ≥ 22.13 without a flag). The Node equivalent of the Bun adapter.

import { SQLiteStorage } from 'reflow-ts/sqlite-node-builtin'

const storage = new SQLiteStorage('./workflows.db')

PostgresStorage — for workers on many machines. Needs the pg package. Tables live in their own schema (reflow by default) and migrate themselves on initialize().

import { PostgresStorage } from 'reflow-ts/postgres'

const storage = new PostgresStorage({ connectionString: process.env.DATABASE_URL })
// or share your app's pool: new PostgresStorage({ pool })

MemoryStorage — used internally by the test helper. For custom use, import from reflow-ts/test.

import { MemoryStorage } from 'reflow-ts/test'

You can implement your own adapter by conforming to the StorageAdapter interface:

interface StorageAdapter {
  initialize(): Promise<void>
  createRun(run: WorkflowRun): Promise<CreateRunResult>
  claimNextRun(workflowNames: readonly string[], staleBefore?: number, limits?: ReadonlyMap<string, number>): Promise<ClaimedRun | null>
  readonly supportsConcurrencyLimits?: boolean
  heartbeatRun(runId: string, leaseId: string): Promise<boolean>
  sleepRun(runId: string, leaseId: string, wakeAt: number): Promise<boolean>
  waitRun(runId: string, leaseId: string, eventName: string | readonly string[], wakeAt: number | null): Promise<boolean>
  deliverEvent(runId: string, eventName: string, payload: PersistedValue): Promise<boolean>
  takeEvent(runId: string, eventName: string): Promise<{ payload: PersistedValue } | null>
  consumeEvent?(step: Omit<StepResult, 'output'>, leaseId: string, eventName?: string): Promise<ConsumeEventResult>
  readonly supportsEventNames?: boolean
  getCachedStep?(key: string): Promise<CachedStep | null>
  putCachedStep?(entry: CachedStep): Promise<void>
  deleteExpiredCachedSteps?(now: number): Promise<number>
  getRun(runId: string): Promise<WorkflowRun | null>
  listRuns(filter?: ListRunsFilter): Promise<WorkflowRun[]>
  requeueRun(runId: string, options?: RequeueOptions): Promise<boolean>
  readonly supportsKeepFailed?: boolean
  getStepResults(runId: string): Promise<StepResult[]>
  getStepResultsForRuns?(runIds: readonly string[]): Promise<Map<string, StepResult[]>>
  runActivity?(filter: RunActivityFilter): Promise<RunActivity>
  saveStepResult(result: StepResult, leaseId?: string): Promise<boolean>
  completeRun(runId: string, leaseId: string, output: PersistedValue): Promise<boolean>
  failRun(runId: string, leaseId: string, error: RunError): Promise<boolean>
  cancelRun(runId: string): Promise<boolean>
  countRuns(filter?: CountRunsFilter): Promise<RunCounts>
  deleteRuns(filter: DeleteRunsFilter): Promise<number>
  upsertSchedule(schedule: WorkflowSchedule): Promise<WorkflowSchedule>
  claimDueSchedule(workflowNames: readonly string[], now: number): Promise<WorkflowSchedule | null>
  deleteSchedule(key: string): Promise<boolean>
  listSchedules(): Promise<WorkflowSchedule[]>
  close(): void
}

Each method's contract is in the Storage API reference, and reflow-ts/conformance checks an adapter against it.

Persisted workflow input and step output must be plain data: objects, arrays, strings, numbers, booleans, null, undefined, and Date.

Testing

Reflow includes a test helper that runs a workflow to completion in memory and returns typed results:

import { testEngine } from 'reflow-ts/test'

const te = testEngine({ workflows: [orderWorkflow] })

const result = await te.run('order-fulfillment', { orderId: 'test', amount: 100 })

result.status              // 'completed' | 'failed'
result.steps.charge.output // { chargeId: string } — fully typed
result.steps.charge.status // 'completed' | 'failed'
result.steps.charge.error  // string | null
// a step with `when` that did not run: { status: 'skipped', output: null, error: null }

Use it in your test suite:

import { describe, it, expect } from 'vitest'
import { testEngine } from 'reflow-ts/test'

describe('order workflow', () => {
  it('charges and fulfills', async () => {
    const te = testEngine({ workflows: [orderWorkflow] })
    const result = await te.run('order-fulfillment', { orderId: 'ORD_1', amount: 100 })

    expect(result.status).toBe('completed')
    expect(result.steps.charge.output.chargeId).toBeTruthy()
    expect(result.steps.fulfill.output.trackingNumber).toBeTruthy()
  })
})

run() executes one pass, so a workflow that sleeps or waits for an event throws TestRunIncompleteError; test those with a real engine and waitForRun().

Type Safety

Reflow tracks types through the entire workflow chain:

  • Workflow name is a string literal type ('order-fulfillment', not string)
  • Input is validated by your schema library and inferred at the type level
  • Step chaining — each step's prev is typed as the return value of the previous step
  • Engine — enqueue() only accepts registered workflow names with matching input
  • Test engine — run() returns typed step results keyed by step name
// These are all compile-time errors, not runtime surprises:
engine.enqueue('typo', {})                    // 'typo' is not a registered workflow
engine.enqueue('order-fulfillment', {})       // missing required fields
workflow.step('x', async ({ prev }) => {
  prev.nonexistent                            // property doesn't exist on prev
})

Error Handling

Every error Reflow throws extends ReflowError, so a single instanceof check catches them all, and carries a stable literal code. (The one exception: a schema whose validation is async is rejected with a plain TypeError.) Subclasses carry structured context in typed fields — no message parsing needed.

Prefer code over instanceof for branching. It's a closed union, so a switch over it can be checked for exhaustiveness, and it keeps working across bundling, duplicate copies of the package in a dependency tree, and realm boundaries — all cases where instanceof silently fails.

if (error instanceof ReflowError) {
  switch (error.code) {
    case 'STEP_TIMEOUT':       return retryLater()
    case 'WAIT_TIMEOUT':       return escalate(error.message)
    case 'VALIDATION':         return badRequest(error.message)
    case 'WORKFLOW_NOT_FOUND': return notFound()
    default:                   return report(error)
  }
}

Switching on code narrows the code, not the class; to read a subclass's fields, such as timeoutMs or issues, check it with instanceof as below.

Errors also implement toJSON(), so JSON.stringify(error) emits the discriminant, the structured context, and a flattened cause chain — no custom log serializer needed.

import {
  ReflowError,
  WorkflowNotFoundError,
  ValidationError,
  IdempotencyConflictError,
  StepTimeoutError,
} from 'reflow-ts'

try {
  await engine.enqueue('nonexistent', {})
} catch (error) {
  if (error instanceof WorkflowNotFoundError) {
    console.log(error.workflowName) // 'nonexistent'
  }
  if (error instanceof ValidationError) {
    console.log(error.issues) // [{ message: '...', path: [...] }]
  }
  if (error instanceof ReflowError) {
    // Catch-all for any Reflow error
  }
}

In hooks, you can identify timeout failures:

hooks: {
  onRunFailed: ({ error }) => {
    if (error instanceof StepTimeoutError) {
      console.log(`Timed out after ${error.timeoutMs}ms`)
    }
  },
}

Available error classes:

Error code Thrown when Structured properties
ReflowError — Base class for all errors code
ConfigError CONFIG Invalid engine, retry, or schedule config —
WorkflowNotFoundError WORKFLOW_NOT_FOUND enqueue() / schedule() with unknown name workflowName
DuplicateWorkflowError DUPLICATE_WORKFLOW Same workflow registered twice workflowName
DuplicateStepError DUPLICATE_STEP .step() reuses an existing name workflowName, stepName
ValidationError VALIDATION Input fails schema validation issues
IdempotencyConflictError IDEMPOTENCY_CONFLICT Same idempotency key with different input workflowName, idempotencyKey
SerializationError SERIALIZATION Input or step output contains data that can't be persisted (NaN, functions, etc.) path
StepTimeoutError STEP_TIMEOUT Step exceeds timeoutMs timeoutMs
RunCancelledError RUN_CANCELLED Run cancelled via engine.cancel() runId
RunNotFoundError RUN_NOT_FOUND waitForRun() with an unknown run id runId
LeaseExpiredError LEASE_EXPIRED Worker lost its lease on a run runId
RunSuspendedError RUN_SUSPENDED A workflow written as a function is suspended to sleep or wait; its pending operations reject with it runId
WaitTimeoutError WAIT_TIMEOUT A waitForEvent step's timeoutMs elapsed eventName, timeoutMs
ParallelCompleteError PARALLEL_COMPLETE complete() called inside a parallel branch stepName
StorageError STORAGE A storage backend operation failed; driver error on cause operation, cause
StepFailedError STEP_FAILED A step exhausted its retries with no error of its own. In a workflow written as a function, what step.run() rejects with once every attempt has failed, the handler's error as its cause stepName, attempts
HookError HOOK A hook, stream consumer, or onFailure threw. Delivered to onError, never into a run source, cause
ThrownValueError THROWN_VALUE User code threw a non-Error value value, cause
TestRunIncompleteError TEST_RUN_INCOMPLETE testEngine().run() on a workflow that sleeps or waits runId, status
InternalError INTERNAL An invariant was violated — a bug in reflow-ts —

API Reference

createWorkflow(config)

Creates a new workflow builder. Pass a function as a second argument to write the workflow as a function instead (below).

Parameter Type Description
config.name string Unique workflow name (becomes a literal type)
config.input StandardSchemaV1 Any Standard Schema-compatible schema for input validation
config.concurrency number Optional. Most runs of this workflow running at once, across every process sharing the storage
config.label (input) => string Optional. Names a run in the dashboard, such as an order number

Returns a Workflow with .step(), .parallel(), .sleep(), .waitForEvent(), .onFailure(), and .parseInput() methods.

createWorkflow(config, handler)

Creates a workflow written as a function. Takes the same name, input, label, and concurrency, plus:

Parameter Type Description
config.events Record<string, StandardSchemaV1> Optional. Events step.waitForEvent() may wait for, with the schema sendEvent validates each payload against
config.onFailure ({ error, stepName, input }) => Promise<void> Optional. Called once a run has failed; stepName is empty when the function's own code threw
config.maxOperations number Optional. The most operations one run may start (default 10,000); past it the run fails with ConfigError
handler ({ input, step, runId }) => Promise<T> The workflow. Its return value is the run's output
Operation Resolves to Description
step.run(name, handler | config) The handler's result A durable step. config takes handler, retry ({ maxAttempts, backoff?, initialDelay? }), timeout, cache (with key: () => value); durations are milliseconds or strings such as '30s'. The handler gets { signal, runId, attempt, idempotencyKey }. Rejects with StepFailedError (its cause is the handler's error) once every attempt has failed, and replays that rejection on resume. Any other error — storage, an unsaveable output, a misconfigured call — fails the run however the function catches it
step.sleep(name, duration) undefined Durable pause, in milliseconds or '30s' / '7d'
step.sleepUntil(name, time) undefined Durable pause until a Date or epoch time
step.waitForEvent(name, { event?, timeout? }) The payload, or null on timeout Waits for a declared event — event, when the wait is named apart from it to wait for the same event again, or else name. Several may be pending at once

Operations started together (Promise.all) run concurrently; the run suspends only once every operation in flight is sleeping, waiting, or backing off. On resume, saved results are handed back in the order the operations first finished, so Promise.race between them picks the same winner. What the function returns is typed (InferOutput<typeof workflow>, and output from testEngine().run()). When a run stops part-way — suspended, cancelled, or its lease lost — the operations still pending never settle, so a catch never sees it, and a finally around them does not run.

workflow.step(name, handler | config)

Adds a step to the workflow. Accepts either a handler function or a config object.

Handler function form:

.step('name', async ({ input, prev, steps, signal, complete, idempotencyKey }) => {
  return { result: 'value' }
})

Step context:

Field Type Description
input TInput Validated workflow input (same for every step)
prev TPrev Return value of the previous step (undefined for the first step)
steps TStepsSoFar Typed record of all previously completed step results by name
signal AbortSignal Aborted on cancellation, lease loss, or step timeout
complete (value?) => never Finish the workflow early, skipping remaining steps
runId string The run's id
attempt number Which attempt at this step this is, from 1. Restarts from 1 after a crash for parallel branches and on engines driven only by tick()
idempotencyKey string ${runId}:${stepName}, stable across retries and crash recovery. Pass it to APIs that deduplicate, since a step interrupted by a crash runs again

Config object form:

Parameter Type Description
handler (ctx) => Promise<T> Step handler. Receives the step context above
when ({ input, prev, steps }) => boolean Optional. Runs the step only when it returns true; when skipped, prev passes through unchanged. Sequential steps only
cache number | string | { ttl, key?, version? } Optional. Reuse the step's result from an earlier run for ttl, keyed on { input, prev, steps } or what key({ input, prev, steps }) returns; bump version when the step's code changes. Sequential steps only
retry RetryConfig Optional retry configuration (see below)
timeoutMs number Optional timeout per attempt in milliseconds

RetryConfig:

Parameter Type Description
maxAttempts number Maximum number of attempts, at least 1. Without retry, a step gets one attempt
backoff 'linear' | 'exponential' Backoff strategy between retries
initialDelayMs number Base delay in milliseconds (default: 1000)
timeoutMs number Timeout per attempt. Step-level timeoutMs takes precedence

workflow.sleep(name, duration)

Durably pauses the workflow for duration (milliseconds, or '500ms' / '30s' / '24h' / '7d'). The run is persisted as sleeping with its lease released and resumes on any engine instance once the time elapses. prev passes through unchanged. name must be unique within the workflow.

workflow.waitForEvent(name, options?)

Durably suspends the workflow until engine.sendEvent(runId, name, payload) delivers a matching event. options.schema validates the payload on delivery; options.timeoutMs fails the run with WaitTimeoutError if nothing arrives in time. The payload becomes the next step's prev. Events delivered before the run reaches the wait are buffered and consumed when it gets there.

workflow.parallel(branches)

Adds a group of concurrent steps. branches is a record of { branchName: handler | config }. All branches run at the same time; the next step's prev is { [branchName]: output } (merged across all branches).

.parallel({
  a: async ({ prev }) => ({ x: prev.value * 2 }),
  b: {
    retry: { maxAttempts: 3, backoff: 'linear' },
    timeoutMs: 5000,
    handler: async () => await someCall(),
  },
})

Branch names share the same namespace as .step() names — duplicate names across .step() and .parallel() throw DuplicateStepError.

workflow.onFailure(handler)

Attaches a failure handler for compensation logic. Receives { error, stepName, input }. Called when a step fails after exhausting all retry attempts.

createEngine(config)

Creates an engine that executes workflows.

Parameter Type Default Description
config.storage StorageAdapter required Storage backend
config.workflows Workflow[] required Workflows to register
config.hooks EngineHooks undefined Lifecycle hooks (onRunStart, onStepStart, onStepSkipped, onStepComplete, onRunComplete, onRunFailed, onError). May be sync or async (awaited)
config.concurrency number 1 Maximum runs executing at once
config.runLeaseDurationMs number 30000 How long a claimed run stays running before another engine may reclaim it
config.heartbeatIntervalMs number leaseDuration / 3 How often the active worker renews its lease

Returns an Engine with the methods below.

engine.start(pollIntervalMs?)

Initializes storage and starts the engine. Work this instance enqueues, resumes, or sends an event to starts at once, and a finished run's slot is refilled immediately; every pollIntervalMs (default: 1000) the engine also checks storage for work from elsewhere — other processes, elapsed sleeps, due schedules, stale runs. Call this once at startup, then enqueue() work as it arrives.

engine.stop()

Stops polling, aborts in-flight runs (left running, to be reclaimed by another engine), ends open streams, and waits for in-flight work to settle. Durable schedules stay registered, since other workers still serve them; use unschedule() to remove one. Returns a Promise<void>.

engine.tick()

Claims up to concurrency pending or stale runs and executes them in parallel. Useful for CLI tools or tests where you want explicit control instead of polling. If you use tick() without start(), call storage.initialize() first.

engine.stream(options?)

Returns a pull-based ResultStream — an AsyncIterableIterator<EngineEvent> (and AsyncDisposable) of execution events. Each call returns an independent stream; iterate it with for await. EngineEvent is a discriminated union on type: runStart, stepStart, stepSkipped, stepComplete ({ output, attempts }), runComplete ({ output }), and runFailed ({ stepName, error }) — all carrying runId and workflow.

Option Type Default Description
bufferSize number Infinity Max events buffered before the engine pauses (backpressure). Set to 0 for strict rendezvous delivery, or another non-negative integer to allow that many buffered events

Breaking out of the loop, disposing via await using, or calling engine.stop() unsubscribes the stream automatically.

engine.enqueue(name, input, options?)

Submits a workflow run. Type-safe - only accepts registered workflow names with their corresponding input types. Returns the created WorkflowRun.

Option Type Description
idempotencyKey string Prevents duplicate runs. Same key + same input returns the existing run. Same key + different input throws
delay number | string Start the run later: milliseconds or a duration such as '10m'
startAt Date | number Start the run at this time; a time already past starts it now. Not together with delay

A delayed run is stored as sleeping until it starts, so it survives restarts and can be cancelled.

engine.cancel(runId)

Cancels any run that hasn't finished (pending, running, sleeping, or waiting). Returns true if cancelled, false if the run doesn't exist or has already finished. Aborts the current step's AbortSignal immediately.

engine.resume(runId)

Re-queues a failed or cancelled run, resuming from the step where it stopped; completed steps are not run again. Returns false if the run isn't resumable.

engine.listRuns(filter?)

Returns runs newest first, filtered by status and/or workflow. limit defaults to 100; page with before and beforeId from the last run of the previous page.

engine.sendEvent(runId, name, payload)

Delivers an external event to a run waiting on waitForEvent(name). Returns false if the run does not exist or has already finished; throws if the workflow has no such event step or the payload fails validation.

engine.schedule(name, input, spec, options?)

Registers a durable recurring schedule. Returns a Promise<string> resolving to the schedule's key, for later removal with engine.unschedule(key).

spec is a number of milliseconds, { every } (ms or a duration string), or { cron } (five fields, evaluated in UTC; malformed or unsatisfiable expressions throw at registration). The schedule is persisted, so it survives restarts and deploys. Registering the same key again updates it in place and preserves the existing cadence unless the recurrence changed. Due firings are claimed atomically, so N instances sharing a schedule still produce one run per interval. options.key overrides the identity, which otherwise defaults to a hash of the workflow name, interval, and input. Missed occurrences are skipped rather than backfilled.

engine.unschedule(key)

Removes a durable schedule. Returns Promise<boolean> — false if no such schedule existed. Because schedules are shared, this stops it for every instance.

engine.listSchedules()

Returns Promise<readonly WorkflowSchedule[]> — every registered schedule, ordered by key, each with its nextRunAt.

engine.getRunStatus(runId)

Returns { run, steps } with the run's current status and all step results, or null if not found. The run records startedAt, finishedAt, output, error, wakeAt, and waitingFor.

engine.waitForRun(runId, { signal?, pollIntervalMs? })

Resolves with the run once it is completed, failed, or cancelled. Immediate for runs finishing on this engine; runs finished elsewhere are noticed by polling storage. Pass AbortSignal.timeout(ms) as signal for a deadline. pollIntervalMs defaults to 1000. Works on an engine that was never started; throws RunNotFoundError for an unknown run.

engine.countRuns(filter?)

Returns how many runs are in each status, optionally for one workflow.

engine.prune({ olderThan, statuses? })

Deletes finished runs that finished longer ago than olderThan (ms or a duration string like '30d'), with their steps and events. Never touches a run that can still progress. Returns how many were deleted.

testEngine(config)

Creates a test engine with in-memory storage. Accepts { workflows } and returns an async run(name, input) that executes the run in one pass and returns its typed step results. Throws TestRunIncompleteError if the workflow sleeps or waits.

storageConformanceCases — reflow-ts/conformance

The behavioural contract the built-in adapters are held to, as a list of { name, appliesTo?(storage), run(storage) } cases. Run them against a custom StorageAdapter to validate it, skipping a case whose appliesTo returns false. Each case takes a freshly initialized, empty adapter and throws on failure, with no test-framework dependency.

SQLiteStorage(path, options?) — Bun

SQLite storage adapter for Bun runtime. Uses the built-in bun:sqlite module — no native dependencies. WAL mode and transactional claiming.

SQLiteStorage(path, options?) — Node.js

SQLite storage adapter for Node.js. Uses better-sqlite3. WAL mode and transactional claiming.

SQLiteStorage(path, options?) — Node.js built-in

SQLite storage adapter for Node.js using the built-in node:sqlite module — no native dependencies. Requires Node ≥ 22.5 (no flag needed from Node 22.13 and 23.4; earlier releases need --experimental-sqlite). WAL mode and transactional claiming. Import from reflow-ts/sqlite-node-builtin.

For every SQLite adapter, options.readOnly opens the file read-only, as the dashboard CLI does.

PostgresStorage(options) — reflow-ts/postgres

PostgreSQL storage adapter, for workers on many machines. Requires the pg package. Pass { connectionString, poolConfig? } to have Reflow create and own a pool, or { pool } to share yours (close() then leaves it open). schema names the Postgres schema for Reflow's tables (default reflow; lowercase letters, digits, and underscores). Runs are claimed with FOR UPDATE SKIP LOCKED, and migrations run under an advisory lock on initialize().

Contributors

Thanks to everyone who has opened an issue or a pull request. Each one has shaped reflow.

brianjenkins94
brianjenkins94

5 issues, 1 pull request
shishantbiswas
shishantbiswas

1 issue
mmgoodnow
mmgoodnow

1 pull request

License

MIT

About

Durable workflow execution for TypeScript

Resources

Contributing

Security policy

Stars

43 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages