# Worker state machine

> The finite state machine of a Link Loom worker: its seven states, the nine events that move it, the hook each transition runs and what a workflow step uses.

Source: https://docs.sommatic.ai/en/workflows/lifecycle/worker-states/ (updated 2026-11-26)

Every worker instance, and so every node step, is driven by the `WorkerStateMachine` of the Link Loom SDK: a
deterministic state machine moved only by commands, with no timers or background loops. Each transition runs one hook
of the worker; if the hook throws, the instance goes to `CRASHED`.

Diagram: The Link Loom worker state machine in three runs: a workflow step (activate in the background, stop), the same step when activateBackground throws, and a worker activated in the foreground, suspended and stopped.

## States

| State               | Meaning                                                      |
| ------------------- | ------------------------------------------------------------ |
| _(none)_            | Before `CREATE`: the instance does not exist yet             |
| `INACTIVE`          | Created and prepared; no active work                         |
| `ACTIVE_FOREGROUND` | Active with foreground semantics                             |
| `ACTIVE_BACKGROUND` | Active with background semantics: where a node does its work |
| `SUSPENDED`         | Resources released, minimal footprint; can resume            |
| `TERMINATING`       | Graceful shutdown in progress                                |
| `TERMINATED`        | Final: the instance is removed                               |
| `CRASHED`           | Final: a hook threw during a transition                      |

## Events and transitions

| Event                 | From                                | To                  | Hook it runs                       |
| --------------------- | ----------------------------------- | ------------------- | ---------------------------------- |
| `CREATE`              | _(none)_                            | `INACTIVE`          | `setup`                            |
| `ACTIVATE_BACKGROUND` | `INACTIVE`                          | `ACTIVE_BACKGROUND` | `activateBackground`               |
| `ACTIVATE_FOREGROUND` | `INACTIVE`                          | `ACTIVE_FOREGROUND` | `activateForeground`               |
| `DEACTIVATE`          | `ACTIVE_*`                          | `INACTIVE`          | `deactivate`                       |
| `SUSPEND`             | `INACTIVE`, `ACTIVE_*`              | `SUSPENDED`         | `suspend`                          |
| `RESUME`              | `SUSPENDED`                         | `INACTIVE`          | `resume`                           |
| `STOP`                | `INACTIVE`, `ACTIVE_*`, `SUSPENDED` | `TERMINATING`       | `terminate` (skipped with `force`) |
| `STOP`                | `TERMINATING`                       | `TERMINATED`        | none                               |
| `STOP`                | `CRASHED`, `TERMINATED`             | `TERMINATED`        | none: idempotent cleanup           |
| `SIGNAL`              | any non-final state                 | the same state      | `handleSignal`                     |
| `FAIL`                | any non-final state                 | `CRASHED`           | none                               |

Any other event in any other state is rejected with `Invalid transition from "<state>" on "<event>"`.

## A workflow step

A node step uses a short path through the machine, always the same: `CREATE`, `ACTIVATE_BACKGROUND`, `STOP` and `STOP` again. The figure above plays it, then the same step when its worker crashes, and then the foreground and suspended states the SDK also supports.

If `activateBackground` throws, the path ends in `CRASHED` instead, and the cleanup `STOP` turns it into `TERMINATED`
without running any hook. Foreground activation, suspension, resumption and signals exist in the SDK but are not used by
workflow nodes.

## Rules of the machine

- **One instance, one step.** The engine never reuses a worker instance: every step starts at `CREATE`.
- **A throwing hook crashes the instance.** The state becomes `CRASHED`, the error is logged with the transition and
  thrown again to the engine (see [When a node fails](https://docs.sommatic.ai/en/workflows/lifecycle/failures.md)).
- **No retries inside the machine.** Retrying is a policy of the node (`execution` settings), not of the state machine.
- **Every transition is logged** as `<worker>:<alias> <from> --<event>-> <to>`, which is how a step can be followed in
  the service logs.

## Next steps

- [Drafts and versions](https://docs.sommatic.ai/en/workflows/lifecycle/versions.md): From draft to an immutable version, and which mode runs what.
- [Runs and their statuses](https://docs.sommatic.ai/en/workflows/lifecycle/runs.md): The statuses of a run and of its result.
- [How a node runs](https://docs.sommatic.ai/en/workflows/lifecycle/node-execution.md): Each node is a Link Loom worker: spawn, activate, stop.
- [When a node fails](https://docs.sommatic.ai/en/workflows/lifecycle/failures.md): Error port or crash, and how each one ends the run.
- [Isolation and sandboxes](https://docs.sommatic.ai/en/workflows/lifecycle/isolation.md): Workers, expression contexts and code sandboxes.
