---
title: "Deploy Code Changes"
description: "What a deploy changes for open Flows, how to deploy changed Flow code while Flows are still open, with a worked example, and how to update Steps that already wait with Time Travel."
canonical_url: "https://docs.superdurable.io/production-operations/deploy-code-changes/"
source: "docs/content/concepts/change-flow-code.mdx"
locale: "en"
---

> Index of every page for AI agents: https://docs.superdurable.io/llms.txt

# Deploy Code Changes

Flows can stay open for days or months, so new code meets Flows that started
under old code. This page shows how to change Flow code safely, what runs
again when a Worker stops, and how to update Steps that already wait.

<a id="what-a-deploy-changes"></a>

## What a deploy changes

- **A Step runs the code of the Worker that takes it.** Dex runs no earlier
  Step again. A Flow that waits in a Step runs the `Execute` of the Worker
  that takes it when its conditions are met.
- **During a rolling deploy, old and new Workers both take calls.** Dex routes
  by Flow type, so an old Worker still runs every Step and RPC it registers,
  with its old code. A Worker that lacks a Step type or RPC declines it, and
  Dex hands the call to another Worker without using up the attempt. See
  [Routing and availability](https://docs.superdurable.io/reference/server/workers.md#routing-and-availability).
- **A Step that already waits keeps its old conditions.** New `WaitFor` code
  applies to Step executions that start after the deploy. To satisfy an
  existing condition, publish to its Channel or skip its Timer. To install new
  conditions, start a new Flow or
  [time travel the run](https://docs.superdurable.io/production-operations/deploy-code-changes.md#apply-new-waitfor-code) to that Step's `WaitFor`.

<a id="you-deploy-new-code"></a>

<a id="a-worker-crashes-or-stops"></a>

## When a Worker stops

A deploy stops Workers, and a Worker can also crash. A graceful stop lets
running tasks finish within the drain period. An interrupted attempt fails
when Dex detects the disconnect or a timeout, and an eligible Worker can take
its retry if the retry policy allows. Steps whose results Dex committed stay
finished, and a waiting Step holds no Worker. So give each external call an
[idempotency key](https://docs.superdurable.io/concepts/step.md#idempotency-keys).
[Attempts and Worker failures](https://docs.superdurable.io/reference/server/workers.md#attempts-and-worker-failures)
covers detection, graceful stops, and long Steps.

> **Diagram**: A Worker crashes during a SYNC Step, shown over four lanes: Dex, Worker A, Worker B, and a payment service. Worker A finishes Reserve, and Dex saves its result with the move to Charge. Worker A starts Charge attempt 1, calls the payment service with the key charge-order-1042, and stops before Dex saves the result of attempt 1. Dex fails attempt 1 when it detects the disconnect or a deadline expires, and the retry policy allows another attempt. Worker B runs Charge attempt 2 on the same snapshot with the same key, and the payment service drops the repeat. Dex saves Charge's result with the move to Ship, Ship is ready, and Reserve does not run again.

<a id="versioning-flow-code"></a>

## Versioning Flow code

Dex decides a Flow's next Step from its saved state, without running earlier
Step code again. So Dex never fails a Flow because its earlier Steps ran under
different code.
Open Flows still meet the new code: changed business rules, missing Step
implementations, renamed RPCs, or incompatible data.

<a id="lock-business-behavior-at-flow-start"></a>

### Lock business behavior at Flow start

When a rule change must not affect open Flows, store a business version when
the Flow starts, in an Attribute or the start input. Branch on that stored
value, not on the Worker build that serves the call. Open Flows keep the old
behavior, and new Flows get the new one. Use this for any change to a decision,
price, retry policy, deadline, or other customer-visible meaning.

<a id="versioning-example"></a>

### Example: add a fee to Charge

The order Flow from [Order Processing](https://docs.superdurable.io/examples/order-processing.md) charges the
order amount. A new rule adds a handling fee, and open orders keep the old
price.

1. **Record the version at start.** The new start handler sets the Attribute
   `pricing-version` to 2. Flows that started earlier have no value, which
   means version 1.
2. **Branch on the stored value.** `Charge` charges the amount for version 1
   and the amount plus the fee for version 2. A Flow that already waits in
   `Ship` never runs `Charge` again.
3. **Add a Step type for a changed contract.** If the refund needs a new input
   field that old Flows never stored, add a Step type such as `RefundWithFee`
   and go to it only from version 2. Keep `Refund` for version 1.
4. **Deploy both, then start version 2.** One Worker build holds `Charge` with
   the branch, `Refund`, and `RefundWithFee`. Wait until it replaces the old
   build before the start handler writes version 2, because an old Worker
   still runs its old `Charge`. During the rollout, an older Worker hands
   `RefundWithFee` calls to a newer one.
5. **Retire the old path.** When no open Flow has version 1, remove the
   version 1 branch and `Refund`. Search for such Flows first. If
   `pricing-version` is an indexed Attribute, the search can filter on it.

The stored Attribute picks the path whatever Worker build serves the Flow, so
Step code needs no version marker.

<a id="evolve-data-and-contracts-additively"></a>

### Evolve data and contracts additively

Add optional fields. Code that reads a payload gives a safe default to a field
that older runs did not set, and ignores unknown fields. Existing field values
keep their meaning. For an incompatible payload or output, add a new Step type
or RPC name instead of changing the old one.

A newer Worker that cannot decode a stored input or Attribute fails the
attempt like any Step error, and every retry fails the same way. Watch Step
failures after each deploy, as
[Upgrades and compatibility](https://docs.superdurable.io/reference/server/upgrades.md#decoding-errors-after-a-deploy)
shows. Fix the code, then rerun the Step with
[Time Travel](https://docs.superdurable.io/production-operations/operate-and-debug.md#time-travel).

<a id="keep-old-definitions-until-old-flows-finish"></a>

### Keep old definitions until old Flows finish

Keep a Flow type, Step type, or RPC registered under its name while an open
Flow can still call it, through retries and any approved Time Travel. Deploy
the new definition alongside the old one. New Flows reach it through a decision
that branches on the stored version, or through new code that starts a new
Flow type.

Before you remove the old definition, confirm that no running, waiting,
retrying, or recoverable terminal Flow needs it. Start with a search by Flow
type. `--all` reads every page of results:

```bash
dexcli flow search --all --query 'FlowType = "OldFlowType"'
```

Stop new starts that can take the old path. Account for search lag, check the
candidate runs directly, and record which runs are approved for retirement.
Remove the old code only when no candidate run remains or every remaining run
is approved.

<a id="check-before-you-deploy"></a>

### Check before you deploy

Test a new Worker build against open Flows:

1. **List the open Flows of the type.** Search for running Flows and for the
   Step types they wait in. Active-Step search is off by default, so
   `ActiveStepTypes` finds only Flows that turn it on.

   ```bash
   dexcli flow search --all --query 'FlowType = "OrderFlow" AND ExecutionStatus = "Running"'
   dexcli flow search --all --query 'FlowType = "OrderFlow" AND ActiveStepTypes = "Ship"'
   ```

2. **Confirm the new build registers every Step type and RPC those Flows can
   reach.** If no Worker has one, a Step waits in the queue and an RPC fails.

3. **Test the new build against Flows started by the old one.** Use a
   [server for tests](https://docs.superdurable.io/reference/cli.md#test-your-application). The
   Quick Start's restart test has the shape to copy:
   1. Start a Worker from the old build. Start Flows and wait until each
      reaches the Step you care about: wait for an earlier Step to complete,
      or inspect the run.
   2. Stop that Worker gracefully.
   3. Start a Worker from the new build against the same Dex Server. Skip
      Timers with `skip-timer`, send the messages the Flows wait for, and wait
      for each Flow to finish.
   4. Check each Flow's result and Attributes, and that the new build raised
      no decoding error.

<a id="apply-new-waitfor-code"></a>

## Update waiting Steps with Time Travel

To make a Step that already waits use new `WaitFor` code,
[time travel](https://docs.superdurable.io/production-operations/operate-and-debug.md#time-travel) its run to that Step
execution, before its `WaitFor`. The Worker that takes it runs the new
`WaitFor`, which returns the new Wait. The Flow ID and Run ID stay the same.

An operator or a deploy job does this for each affected run:

1. Wait until every Worker that serves the Flow runs the new build.
2. Find candidate runs, such as with the
   [`ActiveStepTypes`](https://docs.superdurable.io/reference/server/search.md#query-fields) search field.
   A match is only a candidate. It works only when the Flow turns on
   active-Step search, it does not say whether the Step waits or runs a
   method, and search results can lag.
3. Before each time travel, check the run's current Run ID and run attempt.
   Confirm in its open Step executions that the exact Step execution, such as
   `Review-2`, still waits. Use the exact execution ID, because a Step-type
   selector can choose an earlier execution of the same type.
4. Review what the run did after that Step execution was created. Skip a run
   that went beyond the approved scope.
5. Time travel the run to that Step execution's `WaitFor`. Then check that the
   Step waits on the new Wait.

Like every Time Travel, this is a privileged change to the run, so it needs
authorization. The
point is when that Step execution was created, so it must still be inside the
run's history window.

Time Travel restores the whole run, not one Step. Work after the point can run
again, including parallel Steps and their external calls. Changed code can also
change the later path and the Step execution numbers. Earlier external effects
remain. Use it only when the rollback scope is approved and repeating that
work is safe.

Choose whether later input applies again. If it should, enable write reapply
explicitly. Dex applies eligible input before `WaitFor` runs, so check that the
new Wait still listens to every Channel whose messages must count. The default
differs between Dex Web, the CLI, and a Client; see
[Time travel dialog fields](https://docs.superdurable.io/reference/web/dex-web.md#time-travel-dialog) and the
[CLI reference](https://docs.superdurable.io/reference/cli.md#time-travel-and-clone-fork).

The new `WaitFor` declares its Timers again. So a deadline measured from a
business time must come from a stored value, as
[Business deadlines](https://docs.superdurable.io/reference/server/data-primitive-semantics.md#timer-business-deadline)
shows. A Flow timeout that was still pending at the point restarts with its
full duration.

## Next

- [Operate and Debug](https://docs.superdurable.io/production-operations/operate-and-debug.md): find a Flow, fix one that is stuck or failed, and recover it with Time Travel.
- [Server Failover](https://docs.superdurable.io/production-operations/server-failover.md): what runs again when a Dex Server node fails over.
- [Run a server for tests](https://docs.superdurable.io/reference/cli.md#test-your-application): test a new build against Flows started by the old one.
- [Time travel](https://docs.superdurable.io/reference/server/client-apis.md#time-travel) in the server reference: the point types and how to [read open Step executions](https://docs.superdurable.io/reference/server/client-apis.md#read-open-step-executions).
- [Step guarantees](https://docs.superdurable.io/concepts/step.md#guarantees)
