Skip to main content

API Reference: Flow Instance Methods (Flow)

The Flow interface represents an executable, composable workflow pipeline in Siglo. Instances are created using flow(...steps). A Flow encapsulates sequential step execution, deadline timeouts, versioning, network egress perimeters, live event monitoring, execution hooks, saga rollbacks, durable event resumption, and AST graph visualization.

Table of Contents


The Flow Interface


Execution Methods

flow.run(param?, options?)

Executes the workflow from the first step through to completion, propagating accumulated context from step to step and returning a Promise resolving to the final output.

Signature

Parameters

  • param: TInput (optional) — Initial POJO input parameters passed to the first step.
  • options: ExecutionOptions (optional) — Runtime options such as signal, timeout, services, pojo, durable, namespace, stepExecutionHook, and idempotencyKey.

Returns

A Promise resolving to TOutput, which contains:
  • All fields accumulated across step outputs.
  • $steps: Typed record containing outputs of every named step (e.g. result.$steps.fetchUser).

Example


Configuration Methods

flow.timeout(ms)

Configures an overall workflow-level SLA deadline. If the total elapsed execution duration across all steps exceeds ms, execution is cancelled and a FlowTimeoutException is thrown.

Signature

Chainable

Returns the flow instance for fluent chaining.

Behavior

  • Overridden at runtime if options.timeout is supplied to .run().
  • If a timeout occurs, any in-flight steps receive an abort signal from the internal cancellation controller.

Example


flow.version(version)

Declares the schema version of the workflow (e.g. 'v1', 'v2', 2). Used in durable executions to detect version mismatches when restoring suspended checkpoints.

Signature

Chainable

Returns the flow instance for fluent chaining.

Behavior

When a durable workflow resumes:
  • The declared flow.workflowVersion is compared against the version stored in the checkpoint.
  • If they differ and no migration function was supplied in options.migrations, a WorkflowVersionMismatchException is thrown.

Example


flow.networkPolicy(policy?)

Configures the workflow-level network egress policy perimeter. The flow policy supersedes all individual step policies: steps can only narrow permissions within what the flow permits. Outbound requests are intercepted across globalThis.fetch, node:http/node:https, and raw node:net.Socket. Loopback addresses and local Unix sockets are permitted by default.

Signature

Chainable

Returns the flow instance for fluent chaining.

Parameters

  • policy: NetworkPolicyConfig (optional)
    • allowedHosts: (string | RegExp)[] (optional) — Outer permissible hosts or wildcard patterns.
    • defaultPolicy: 'allow' | 'deny' (optional, default: 'deny') — Outer perimeter default policy when policy is active. (When no policy is declared on flow or step, network enforcement is inactive).
    • allowLocalhost: boolean (optional, default: false) — Loopback exemption (localhost, 127.0.0.1, ::1).
    • allowUnixSockets: string[] (optional) — Explicit permitted Unix domain socket filesystem paths.

Precedence and Narrowing Rules

  1. Outer Envelope: The flow’s allowedHosts defines the outer boundary. If a flow only allows ['api.stripe.com'], a step cannot allow api.github.com.
  2. Step Narrowing: A step can choose to permit only a subset of the flow’s allowed hosts or call .denyNetwork().
  3. Execution Options Override: Network policy can also be supplied dynamically per execution run via flow.run(payload, { networkPolicy: { ... } }).

Example


flow.monitor(emitter)

Attaches a live monitoring event emitter or callback listener directly to the workflow. The monitor receives real-time execution lifecycle events (workflow:start, step:start, step:complete, step:fail, workflow:complete).

Signature

Example


flow.hook(hook)

Attaches a StepExecutionHook to the workflow instance, intercepting step executions within an asynchronous context (AsyncLocalStorage).

Signature

Example


Lifecycle and Resumption Methods

flow.resume(correlationKeyOrStackId, payload?, options?)

Resumes a suspended durable workflow instance that paused at a waitForEvent or wait step.

Signature

Parameters

  • correlationKeyOrStackId: string — Either the correlation key specified during waitForEvent or the execution stackId.
  • eventPayload: any (optional) — Data injected from the external event (e.g. webhook body). Merged into context before resumed steps execute.
  • options: ExecutionOptions (optional) — Runtime options including schema migrations.

Example


flow.compensate(stackId, options?)

Manually triggers saga compensation rollbacks for an execution stack in reverse order of execution (LIFO).

Signature

Parameters

  • stackId: string — The execution stack ID whose registered compensations should run.
  • options: CompensationRunOptions (optional) — Optional compensation execution options (e.g. timeoutMs, stopOnError).

Example


Graph and Export Methods

flow.toGraph()

Compiles the workflow into a JSON-serializable WorkflowNode AST structure representing the hierarchical topology of steps and combinators.

flow.inspect()

Analyzes the workflow graph and returns structural metrics including totalNodes, entryNodeId, exitNodeIds, nodes, and directed edges. Note that run() builds an AST for monitoring but does not run assertValidGraph automatically; validation is an explicit utility.

flow.toMermaid(options?)

Exports the workflow topology as a Mermaid flowchart diagram with policy badges for timeouts, retries, sagas, and waits.

Signature

Example


flow.toDot(options?)

Exports the workflow topology as a Graphviz DOT diagram string.

flow.toJSON()

Serializes the complete workflow AST into a formatted JSON string.

Properties

flow.steps

Returns the flattened array of Step execution units contained in the flow.

flow.timeoutMs (Property)

Read-only number reflecting the workflow-level timeout configured via .timeout(ms).

flow.workflowVersion

Read-only string or number reflecting the workflow schema version configured via .version(v).

flow.networkPolicyConfig

Read-only object reflecting the configured outer network egress policy for the workflow.

flow.monitorEmitter (Property)

Read-only reference to the active WorkflowEventEmitter or listener callback registered via .monitor().

flow.stepExecutionHook

Read-only reference to the registered StepExecutionHook attached via .hook().

Composing Flows Within Flows

Flows are first-class execution units and can be nested seamlessly within other flows: