# Deploying new versions

> Change handler code without breaking invocations that are already running.

Source: https://cairnflow.dev/docs/develop/versioning

Invocations can run for minutes or months, so a deploy usually happens while some are in flight.
Cairnflow replays each of them against code, and the code must still match what the journal
recorded. This page covers how deployments are pinned, what counts as a breaking change, and how to
recover when something breaks.

## Invocations are pinned to their deployment

Registering an endpoint creates a new, immutable **deployment** and makes it the current revision of
every service it exposes.

- **New invocations** go to the current revision.
- **Running invocations stay on the deployment they started on**, for every later turn — even after
  the revision has moved on. A deploy never drags in-flight work onto new code by itself.

The safest way to ship a change is therefore to give each version its own URL:

1. Deploy the new code at a new URL (a new container, a versioned path, a new function alias).
2. Register it: `POST /deployments {"uri": "<new url>"}`. New invocations use it from now on.
3. Keep the old version running until no invocation is pinned to it.
   `GET /deployments/{id}` reports `pinnedInvocations`.
4. Remove the old deployment with `DELETE /deployments/{id}`. It answers `409` while the deployment
   is still a current revision or still has invocations pinned to it.

How you run several versions side by side depends on your platform — see
[Deployments](/docs/cloud/deployments).

## What breaks a running invocation

If you replace code **in place** (same URL), in-flight invocations replay against the new code.
On replay, the SDK checks each command against the journal: its **kind** (a step, a sleep, a
call, a state read...), its **name** (for `ctx.run`, the step name), and every other field it
recorded, such as a call's target service, handler, key and input. When they differ, the
invocation stops `paused` with code `570` (journal mismatch). It does not run the wrong code, and
it does not retry on its own: a retry would only hit the same mismatch.

| Change | Effect on an invocation already past that point |
|---|---|
| Rename a `ctx.run` step | Pauses with `570` when it reaches the step |
| Add, remove or reorder `ctx` commands before the point it reached | Pauses with `570` where the commands stop lining up |
| Change a call's target service, handler, key or input | Pauses with `570` at that call |
| Change the body of a step that already has a result | Nothing: the recorded result is used |
| Change the value written by `ctx.set`, a sleep's duration or a send's `delay` | Nothing: these values are recorded once and not compared |
| Add code after the last command it reached | Nothing: it runs the new code from there |

Not every semantic change is caught: what a recorded step returned is never checked against the
new code. When in doubt, deploy at a new URL.

Keep step names stable, and keep configuration (model IDs, prompt versions) out of them — put it in
[span attributes](/docs/develop/streams-logs#traces).

## Check before you switch

Replays can be tested without running any of the new code's side effects. Both checks are read-only:
no closure of the new code runs and nothing is written.

```sh
# Replay a sample of unfinished invocations of a service against a deployment.
curl -X POST "$CAIRN_URL/deployments/$NEW_DP/replay-check?service=OrderService&sample=20" \
  -H "authorization: Bearer $CAIRN_KEY"
# -> {"checked": ..., "matched": ..., "failures": ..., ...}

# Replay one invocation against a deployment.
curl -X POST "$CAIRN_URL/invocations/$ID/replay?deployment=$NEW_DP" \
  -H "authorization: Bearer $CAIRN_KEY"
# -> {"matches": true, "upto": 7}  or  {"matches": false, "mismatchAt": ..., "expected": ..., "got": ...}
```

To register a deployment without making it current, add `?activate=false`, run the checks, then move
each service yourself:

```sh
curl -X POST "$CAIRN_URL/deployments?activate=false" \
  -H "authorization: Bearer $CAIRN_KEY" -H "content-type: application/json" \
  -d '{"uri":"https://v2.example.com"}'

curl -X PUT "$CAIRN_URL/services/OrderService/revision" \
  -H "authorization: Bearer $CAIRN_KEY" -H "content-type: application/json" \
  -d '{"deployment": "dp_new", "expect": "dp_old"}'
```

`expect` makes the move conditional: it only happens if the current revision is still `dp_old`, so
two people rolling back at once cannot both win. The same call rolls back to an older deployment.

## Recovering a paused invocation

An invocation paused with `570` can continue in two ways:

- **Restore the code it expects** at the URL it is pinned to (for example, bring the old step name
  back), then `POST /invocations/{id}/resume`.
- **Move it to a deployment whose code matches** with
  `POST /invocations/{id}/migrate?deployment=<id>`. Migrate replays the invocation on the target
  first and refuses (`409`, with the report) if it does not match. `&force=true` skips that check.

Migrate is also the way to move a healthy invocation onto newer code on purpose.

## Long-running loops

An agent loop or a poller that runs forever keeps growing its journal, and every replay walks the
whole journal. `ctx.continueAsNew(input)` ends the current invocation and starts a new one for the
same target, with an empty journal and `input` as its input:

```ts
async loop(ctx: Context, s: { step: number; done: boolean }) {
  for (let i = 0; i < 50 && !s.done; i++) {
    s = await ctx.run(`step ${s.step}`, () => next(s));
  }
  if (!s.done) ctx.continueAsNew(s);
  return s;
}
```

- The line after `continueAsNew` never runs.
- The new invocation runs on the service's **current revision**, which makes it a natural upgrade
  point for long-lived loops.
- Callers waiting on the old invocation, and its idempotency key, move to the new one; they receive
  the final result of the chain. The old invocation ends with `{"continuedAsNew": "<new id>"}`.

## Handler options are part of the deployment

Retry policies, timeouts and other handler options are captured when a deployment is registered.
Changing them means registering a new deployment; invocations already pinned to the old one keep
the old options.

The same goes for the SDK's protocol version. If you roll a service back to an older SDK build at
the same URL without registering it again, cairnflow may speak a newer protocol version than that
build supports. The endpoint then answers `415` instead of running the handler, and the turn is
retried under the retry policy. Register the deployment again.
