> For the complete documentation index, see [llms.txt](https://www.health-samurai.io/docs/interbox/llms.txt).
> Use it to discover all available pages before guessing URLs.

---
# Updating the engine

Interbox releases two artifacts cut from the same commit and versioned in
lockstep: the engine image (`healthsamurai/interbox` on Docker Hub) and the
SDK (`@health-samurai/interbox` on npm) your workspace is built against.
Updating means moving the image to a new tag and keeping the workspace's SDK in
a compatible range. Database migrations run on boot. A migration that builds an
index on a large delivery queue (`sender_item`) makes that boot take longer, in
proportion to the backlog. Moving to the per-pipeline `retention` rebuilds the
index of finished sender items (`sender_item_done`) the same way. That table holds
at most the retention window (7 days by default) of items, and the build blocks
inserts into it, which only an older engine still running during a rolling deploy
would notice.

`INTERBOX_DONE_ITEMS_RETENTION_DAYS` is removed and ignored. Every pipeline keeps
finished items and stored delivery files for 7 days unless it sets `retention`. A
deployment that relied on a longer value must declare `retention` on its
pipelines and deploy that workspace with the engine, because the first sweep after
boot applies 7 days.

Before updating, read the
[Release notes](../release-notes.md) of
every version between yours and the target: a change you must act on first, such
as a new required Postgres extension or environment variable, is listed at the
top of its release under **Upgrade Notes**.

> Updating the engine (this page) is separate from updating your pipelines. A
> pipeline change is a workspace edit with no image change: the workspace service
> builds and publishes it, and the replicas load it. See
> [The workspace](../deploy/workspace.md).
>
> Moving a 1.x installation to 2.0 is a cutover of its own. The workspace is
> rewritten, and a one-off script moves the 1.x message history and Aidbox
> fingerprints into a 2.0 pipeline while every engine is stopped. Follow
> [Upgrading from 1.x](from-1x.md) for it.

## Upgrading from 2.4.x or older

From 2.5.0 every ready replica ingests, one replica processes, and a workspace
service builds the workspace for the replicas ([Deployment](../deploy/deployment.md#replicas)).

- **Move the image and chart 0.4.0 together.** Chart 0.4.0 adds the
  [workspace service](../deploy/deployment.md#the-workspace-service). An engine from 2.5.0
  loads only a build the workspace service has published, so under an older
  chart it stays unready. An older engine ignores the `workspace` argument, so
  under chart 0.4.0 the workspace service's pod runs as one more engine of the
  older version. A deployment whose chart version floats takes 0.4.0 as soon as
  it is released, and runs that way until its image moves to 2.5.0.
- **Under Docker Compose, add the workspace service.** Run a second service from
  the same image with the command `workspace`, the same environment and the
  same workspace mount as the main service, and set
  `INTERBOX_WORKSPACE_SERVICE_URL` on the main service to its port 3001.
- **Rolling updates are the chart's default.** Chart 0.4.0 uses `RollingUpdate`
  and routes to every ready replica. For an image tagged 2.5.0 or later the
  chart's readiness probe uses `/api/ready` ([Readiness](../deploy/deployment.md#readiness)), so
  a new pod takes traffic only once its listeners are up. With a digest pin or
  a tag such as `edge`, set the probe's path to `/api/ready` yourself.
- **The first rollout from 2.4.x hands processing over last.** Engines up to 2.4.x
  run without the active lease. A 2.5.0 pod ingests as soon as it is ready, but
  takes the lease only once no older engine has written a heartbeat in the last
  6 seconds or still holds a lease on a unit or item. Until then it logs a
  warning naming each such engine and its version.
- **There is no way back to 2.4.x.** 2.5.0 drops the tables 2.4.x replicas
  coordinate through, so a 2.4.x engine does not boot on the upgraded database.
  During a rolling update the 2.4.x pods stop their workers, MLLP listeners
  included, about 10 seconds after the first 2.5.0 pod migrates, and intake
  waits until a 2.5.0 pod has loaded a published build. A sending system resends what
  got no acknowledgement.
- **Mount data on every replica.** A replica runs the published build from its
  own cache, so data the workspace reads that is not in its repository, and
  every folder a file-drop source reads, must be mounted on each replica.
- **Remove `ownership` from `defineSource` calls.** The SDK no longer declares
  it, and every `defineSource` source runs on the active replica only.
- **Remove `INTERBOX_RUNTIME_ROLLBACK_INTENT`, `INTERBOX_RUNTIME_CONFIG_DIR` and
  `INTERBOX_WORKSPACE_INSTANCE`.** The engine no longer reads them.

## Move the image tag

The image tag is the engine version. The template's Compose file and the Helm
chart default to the moving `edge` tag. For production, pin a released semver
(for example `2.5.3`).

For Docker Compose, set `INTERBOX_VERSION` in `.env`, then pull and recreate:

```sh
# .env
INTERBOX_VERSION=2.5.3
```

```sh
docker compose pull
docker compose up -d
```

For Kubernetes (Helm), bump `image.tag` (or the chart's `appVersion`) and
upgrade:

```sh
helm upgrade --install interbox healthsamurai/interbox \
  -n interbox --reuse-values --set image.tag=2.5.3
```

Under `RollingUpdate`, the chart's default, the new pod loads the published
build and ingests beside the old one, then takes the active lease when the old
pod stops. An update pauses processing for seconds and keeps intake open; see
[Replicas](../deploy/deployment.md#replicas). The value keys and pinning options (digest pin,
`pullPolicy`) are in the [chart's README](https://github.com/HealthSamurai/helm-charts/tree/main/interbox).

During a rolling update, pods of the old and the new release run side by side
on the new schema. Each release's migrations keep the previous release working,
so the old pods keep ingesting, and the old active replica keeps processing,
until they stop. Your own workspace changes follow the same rule while the
replicas switch builds:
[Changes the previous revision must survive](../deploy/workspace.md#changes-the-previous-revision-must-survive).

## Keep the workspace SDK compatible

This is the one update step unique to Interbox. Keep the workspace's SDK in the
same npm caret (`^`) bucket as the engine image: the same major version or,
while on `0.x`, the same minor. So `1.3.0` ↔ `1.9.2` are compatible, and `1.x` ↔
`2.x` and `0.4` ↔ `0.5` are not.

The workspace service checks this before it publishes a build, and each
replica checks it again when it loads one, by comparing the engine's version
with the `SDK_VERSION` on the `ChainRegistry` your workspace's entry exports:

- **Different bucket.** The workspace is refused, with a message naming both
  versions and the `interbox pin` or `INTERBOX_VERSION` change that fixes it. A
  version that cannot be parsed is refused the same way. The workspace service
  does not publish a refused build, and the replicas keep the build they run.
- **Same bucket, workspace SDK on a newer minor than the engine.** The workspace
  loads, and the engine logs one warning per load naming both versions. A feature
  the newer SDK added can still fail when it is first used, so move the engine up
  or pin the SDK down.
- **Same bucket, workspace SDK older or equal.** The workspace loads.

An SDK release whose `ChainRegistry` has no `SDK_VERSION` cannot be compared. Such
a workspace loads, and the engine logs a warning on each load until the SDK is
upgraded.

The dashboard's Instances page shows the engine's own version. A workspace
written for 1.x, whose pipelines and mappers come from the package root's
`pipeline()` and `defineMapper`, fails to load with a message that names each of
them:

```text
the workspace registers pipeline "adt" and mapper "v2-to-fhir" with the 1.x DSL,
the package root's `pipeline()` and `defineMapper`, which this 2.0 engine does
not run: it runs only chain pipelines, declared with `pipeline` from
"@health-samurai/interbox/chain" and exported from the workspace entry as
`ChainRegistry`. See concepts/pipelines.md
```

A workspace that already exports `ChainRegistry` but still declares a pipeline
or a mapper the 1.x way fails the same way. Rewrite them in the chain DSL, as
[Pipelines](../concepts/pipelines.md) describes, before you move to 2.0, and see
[Upgrading from 1.x](from-1x.md) for the rest of that move.

A replica that has no build loaded and fails to load the published one stays
unready and runs no pipeline; its API still answers. A replica that already
runs a build keeps it when a newer one fails to load.

Within a compatible bucket (a patch or minor bump on `2.x`) the workspace needs
no change: move the image tag. When you cross the boundary, align the
workspace's SDK and redeploy it:

```sh
interbox pin 2.5.3      # rewrites @health-samurai/interbox to ^2.5.3 in package.json
bun install
```

The template's `bun.lock` needs Bun 1.4 or newer for that `bun install`; see
[Quick start](../get-started/quick-start.md).

`interbox pin` with no argument reads `$INTERBOX_VERSION`, so one variable can
pin both the image tag and the SDK dependency. See [CLI](../reference/cli.md).

To pull the public template's changes into your workspace copy, see
[Updating a workspace from the template](workspace-template.md).

## What happens on boot

On every start, an update included, the container:

1. ensures its `interbox` database exists (unless you tell it the database is
   pre-created);
2. applies any new migrations, idempotent and forward-only;
3. starts the engine and API;
4. loads the current published build, runs its workspace migrations, prepares
   the pipelines' storage and opens its listeners, and from then on reports
   ready;
5. takes the [active lease](../deploy/deployment.md#replicas) and starts processing, or keeps
   ingesting as a standby replica while another replica holds the lease.

The workspace service's container runs the first two steps too, then starts
the workspace service, which builds the workspace and publishes it.

Each engine migration runs in its own transaction. During a rolling update the
old pods keep writing, so a migration that cannot get a table lock soon gives
way: it rolls back, logs `a migration gave way to the database's other sessions
and will retry`, and tries again. If a migration fails, the container stops
with an error that names it, the migrations before it stay applied, and the
next start continues from the failed one. Workspace migrations run the same
way when a replica loads a build; see
[How an update reaches the replicas](../deploy/workspace.md#how-an-update-reaches-the-replicas).

A 2.0 engine that finds rows left in the 1.x tables `interbox.hl7v2_in` or
`interbox.resource_hash` logs a warning naming `bun scripts/import-1x`: 2.0
does not process them. A migration drops the 1.x tables if both are empty when
it runs. It runs once, so tables that the import empties later stay; see
[Upgrading from 1.x](from-1x.md#once-the-cutover-is-final).

## What happens on a stop

In a rolling update the new pod ingests as soon as it is ready. The old pod
stops processing and releases the active lease, which another ready pod takes,
then closes its listeners. When a listener closes, MLLP sending systems must
reconnect and retry, and the Service sends the new connection to a ready pod. In-flight
messages have the stop grace below to persist and ACK. After a lost ACK, a
resend is a new input unless the upstream protocol or workspace supplies a
durable duplicate identity.

On a stop the engine takes no new work and gives what it has started a grace
period to finish: 10 seconds by default, set in milliseconds by
`INTERBOX_STOP_GRACE_MS`. When the grace period ends, the engine abandons work
still running without counting a failed attempt, and picks it up again after the
restart. A
worker stuck in a long synchronous step cannot be interrupted: it is killed
instead, and its work counts one failed attempt.

The container forwards `SIGTERM` and `SIGINT` to the engine and API and waits
for both processes to exit. If either process exits on its own, the container
stops the other and preserves the first process's exit status.

The engine stops its processing workers first and releases the active lease,
then stops its ingest workers, which hold the listeners. Each of the two stops
takes up to the grace period, then a 2-second margin for workers to release
what they hold, then up to 10 seconds for their threads to close. The engine
then takes up to 1.5 seconds to write its last live metrics. The whole stop
takes up to twice the grace period plus 25.5 seconds: 45.5 seconds with the
default. So the orchestrator's termination grace period
(`terminationGracePeriodSeconds` in Kubernetes, `stop_grace_period` in Docker
Compose) must exceed twice `INTERBOX_STOP_GRACE_MS` plus 25.5 seconds: at least
46 seconds with the default. With a shorter period, the engine is killed before
it has released what it held. A stop that arrives while the engine switches to a
new build waits for the switch to finish first, so it can take longer.

Under the Helm chart, `preStopDelaySeconds` (5 by default) runs inside that period, so
add it: at least 51 seconds with the defaults. The chart's default
`terminationGracePeriodSeconds` of 30 and the workspace template's `stop_grace_period`
of 15 seconds are below this, so raise them.

## Rolling back

Interbox migrations are forward-only. The chain holds only up-migrations, and
there is no rollback command. If an update applied a schema change, moving the
image tag back runs the older engine against the newer schema, which it may not
expect. So:

- **Prefer rolling forward.** Deploy a newer image tag that fixes the problem. A
  forward move keeps migrations additive, while reverting runs an engine against
  a schema it may not expect.
- Roll the image tag back only when the version gap applied no migration (e.g. a
  same-series revert), or restore the database from backup alongside it.
- Test upgrades in staging first, especially across a major version, where the
  workspace SDK moves too.
- After a 1.x import, a 1.x image no longer sees the moved rows: going back to
  1.x means restoring the backup taken before the cutover. See
  [Upgrading from 1.x](from-1x.md#rolling-back).
