> ## Documentation Index
> Fetch the complete documentation index at: https://iii.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Compose

> The compose daemon and its worker surface: the compose::* functions, the worker-compose.yaml schema, and the environment every worker is started with.

Compose runs a group of workers as one project. The daemon reads a `worker-compose.yaml`, is able to
resolve, start, and stop each worker in dependency order, handles engine registration tasks, and
supervises running workers.

## The daemon

The daemon is itself a worker. It registers under the name `compose` and exposes the `compose::*`
functions, so every project operation is a standard [trigger](./triggers).

Bare `iii compose` is the command to start the compose worker/daemon. It reads `worker-compose.yaml`
in the working directory and starts only the compose worker.

`iii compose --up` is provided as a convenience. It starts the compose worker, the iii engine, and
the workers specified in `worker-compose.yaml`. It is approximately the equivalent of running `iii`,
`iii compose`, and `iii trigger compose::up` as separate commands.

`iii compose --up` is recommended for ease of development while more granular control and
independent operation of `iii` and `iii compose` daemons is suggested for production.

### Starting a project with the daemon

```text theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii compose [OPTIONS]
```

| Option                 | Description                                                                                                                                              |
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--engine <URL>`       | Existing engine WebSocket address. Overrides the compose file and III\_URL. The local default is used when none of them supplies a URL                   |
| `-n, --namespace <NS>` | Namespace this daemon answers `compose::*` in and applies to every project it loads. Several daemons attach to one engine; this is what tells them apart |
| `--up`                 | Serve with one project brought up first, starting its declared engine unless `--engine` selects an existing one                                          |
| `-f, --file <PATH>`    | The compose file. Only valid with `--up`. Defaults to `./worker-compose.yaml`, the same fallback `compose::up` uses when a call names no file            |

`Ctrl^C`, `SIGINT` and `SIGTERM` all gracefully stop the daemon, every worker run by the daemon, and
the iii engine if compose was started with `--up`. When `--up` has started the engine, every project
and worker stops before the engine process is stopped. `compose::stop` is the function equivalent of
this operation.

`compose::*` functions as documented below are the intended way to manage a running compose daemon.

### Compose logs

Compose logs stdout and stderr output from started workers to `$HOME/.iii/compose/namespace/`.

Logs are rotated every 10 MiB. Compose keeps up to 40 MiB of logs. Compose strips terminal control
sequences before persisting the engine output.

You can use the logs client for a recent snapshot or a live view:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii compose logs                         # last 100 lines from every worker
iii compose logs queue --tail 200        # one worker
iii compose logs queue --follow          # keep waiting for new output
iii compose logs queue --stream stderr   # only stderr
iii compose logs queue --namespace dev --engine ws://127.0.0.1:49134
```

| Option                 | Description                                                                            |
| ---------------------- | -------------------------------------------------------------------------------------- |
| `[WORKER]`             | Worker to read. Omit to read every worker in the project                               |
| `--engine <URL>`       | Existing engine WebSocket address. The compose file and III\_URL are used when omitted |
| `-n, --namespace <NS>` | Namespace of the Compose daemon that owns the project                                  |
| `-f, --file <PATH>`    | Compose file path on the daemon host. The daemon's default file is used when omitted   |
| `--tail <TAIL>`        | Number of recent lines to show before following new output \[default: 100]             |
| `-F, --follow`         | Continue waiting for new output until interrupted                                      |
| `--stream <STREAM>`    | Restrict output to one process stream \[possible values: stdout, stderr]               |

Each line is prefixed with the worker name, and stderr uses a bold prefix on a terminal.

### Running it in the background

Compose does not background or daemonize itself.

To accomplish this please use standard tooling

For example on most terminals (bash, zsh) you can run:

```bash theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii compose --namespace dev --engine ws://127.0.0.1:49134 >> ~/iii-compose.log 2>&1 &
```

Or on a server with systemd support you can use a unit file. This is a basic example:

```ini theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
[Unit]
Description=iii compose
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/iii compose --namespace prod --engine ws://127.0.0.1:49134
Restart=always

[Install]
WantedBy=multi-user.target
```

<Warning>
  Use `Type=simple`. `Type=notify` waits for an `sd_notify` readiness message, which compose does
  not send.
</Warning>

## The `compose::*` functions

These are the functions that control the compose worker and are the canonical way of interacting
with it and making basic changes to the `worker-compose.yaml` file.

| Function            | Takes                                                       | Returns                                                                    |
| ------------------- | ----------------------------------------------------------- | -------------------------------------------------------------------------- |
| `compose::up`       | `file`, `container`                                         | An operation result.                                                       |
| `compose::down`     | `file`, `container`                                         | An operation result.                                                       |
| `compose::status`   | `file`                                                      | The project's namespace, file, state directory, daemon pid, worker states. |
| `compose::logs`     | `file`, `container`, `cursors`, `tail`, `stream`, `wait_ms` | Bounded stdout/stderr entries and one cursor per worker.                   |
| `compose::list`     | nothing                                                     | The daemon name, its namespace, its pid, and every project it holds.       |
| `compose::validate` | `file`                                                      | A validation report.                                                       |
| `compose::add`      | `file`, `workers`                                           | What the edit did, changed workers restarted, and new workers started.     |
| `compose::remove`   | `file`, `worker`                                            | The worker removed, its targeted stop, and the idempotent `up`.            |
| `compose::restart`  | `file`, `container`                                         | The `down` and the `up`, or one worker's restart.                          |
| `compose::update`   | `file`, `worker`                                            | Both versions, and the restart that followed.                              |
| `compose::stop`     | nothing                                                     | The daemon name, its pid, and the projects it is about to stop.            |
| `compose::schema`   | `function_id`                                               | Request/response JSON Schemas, descriptions, timeouts, and retry safety.   |

`file` is not required. Left out, it falls back to a `worker-compose.yaml` in the daemon's own
working directory; without one, the call fails with `NO_COMPOSE_FILE`. A relative `file` path is
considered relative to the daemon's directory, not the caller's working directory. You may pass an
absolute path as well.

```bash theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii trigger compose::up      --namespace dev file=./worker-compose.yaml
iii trigger compose::up      --namespace dev file=./worker-compose.yaml container=api
iii trigger compose::status  --namespace dev file=./worker-compose.yaml
iii trigger compose::logs    --namespace dev file=./worker-compose.yaml worker=api tail=100
iii trigger compose::down    --namespace dev file=./worker-compose.yaml
iii trigger compose::list    --namespace dev
iii trigger compose::add     --namespace dev file=./worker-compose.yaml worker=database worker=web
iii trigger compose::remove  --namespace dev file=./worker-compose.yaml worker=state
iii trigger compose::restart --namespace dev file=./worker-compose.yaml
iii trigger compose::schema  --namespace dev function_id=compose::up
iii trigger compose::stop    --namespace dev
```

### Starting a project

`compose::up` starts every worker in the compose file, in dependency order. Workers that are already
ready stay as they are.

| Field       | Description                                                         |
| ----------- | ------------------------------------------------------------------- |
| `file`      | The project to start.                                               |
| `container` | Starts that worker and the workers it depends on, and nothing else. |

#### compose::up failures

A project that fails to start due to a project-related issue ends the command with
`PROJECT_DID_NOT_START`. Partial starts are rolled back in reverse dependency order.

### Stopping a project

`compose::down` stops the project in reverse dependency order.

| Field       | Description                                                            |
| ----------- | ---------------------------------------------------------------------- |
| `file`      | The project to stop.                                                   |
| `container` | Stops that worker and the workers that depend on it, and nothing else. |

### Adding workers

`compose::add worker=state worker=./workers/api` declares one or more workers in the compose file
and reconciles the project once. On the CLI, repeat `worker=` for each worker. Each value takes a
registry package name (`state`), a package name with a version (`state@0.21.4`), or a directory
(`./workers/api`).

A JSON payload for this function can use a single list: `{ "workers": ["database", "web"] }`.

| Field     | Description                                    |
| --------- | ---------------------------------------------- |
| `file`    | The project to edit.                           |
| `workers` | The canonical JSON list of workers to declare. |
| `worker`  | One worker. Repeatable on the CLI.             |

An package without a version specified will pin to the latest available version (ex. `0.23.1`).

Workers whose declarations did not change remain running. Existing workers whose resolved versions
changed restart in place, and newly declared workers start through the normal dependency plan.

### Removing a worker

`compose::remove worker=state` removes the named worker and every reference to it. The changes are
validated before a worker is removed. Compose then stops only that worker and runs an idempotent
`up`.

| Field    | Description           |
| -------- | --------------------- |
| `file`   | The project to edit.  |
| `worker` | The worker to remove. |

Removal does not resolve the registry graph or remove other workers that were added with this
worker. Those remain declared until they are removed explicitly.

### Restarting one worker

`compose::restart worker=state` stops that specified worker and starts it again. All other workers,
including dependencies, are left unchanged.

`compose::restart` without a worker argument restarts the entire compose project. It is
approximately the equivalent of `compose::down` followed by `compose::up`.

| Field    | Description             |
| -------- | ----------------------- |
| `file`   | The project to restart. |
| `worker` | The worker to restart.  |

### Updating a worker

`compose::update worker=state` updates a worker to either the current `latest` version when no
version is specified or to the target version when it is.

If the requested version is already installed this operation is treated as a NOOP.

| Field    | Description                                |
| -------- | ------------------------------------------ |
| `file`   | The project to edit.                       |
| `worker` | The worker spec: `name` or `name@version`. |

```text theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
worker=state            the version the registry calls latest
worker=state@0.21.4     that version, which is also how a downgrade is spelled
```

The worker has to be declared already in order to be updated, and it has to be a `package://`. Use
[`compose::add`](#adding-workers) to add new workers.

Workers specified with `path://` are not versioned, any updates to these workers will be reflected
the next time the worker is restarted.

<Note>Unlike `compose::restart worker=state`, an update restarts the whole project.</Note>

### Checking status

`compose::status` reports each declared worker with its `state`, its `pid`, an `owned` flag, its
rotating `log_path`, and `last_error` when there is one. `owned` is `false` for a worker this daemon
has knowledge of but does not manage (ie. was not started by the compose daemon).

#### Worker states

| State      | Meaning                                                    |
| ---------- | ---------------------------------------------------------- |
| `starting` | Spawned. The engine has not registered it yet.             |
| `ready`    | Registered in the engine under `(namespace, container)`.   |
| `failed`   | Exited without being asked to, or one of its hooks failed. |
| `stopped`  | Stopped by this daemon.                                    |

### Viewing logs

`compose::logs` returns recent stdout and stderr for the workers of one project.

| Field     | Description                                                                          |
| --------- | ------------------------------------------------------------------------------------ |
| `file`    | The project to read.                                                                 |
| `worker`  | One worker. Returns every compose-orchestrated worker when omitted.                  |
| `tail`    | Recent lines returned when no cursor is sent. Default 100, maximum 1000.             |
| `cursors` | The `cursor` from the last response, keyed by worker. The call continues from there. |
| `stream`  | `stdout` or `stderr`. Omit for both.                                                 |
| `wait_ms` | Wait this many milliseconds for new output. Maximum 5000.                            |

The response holds one entry per worker with `container`, `entries`, `cursor`, and `truncated`. Each
entry in `entries` has `stream` and `message`. A `cursor` has `generation` and `offset`. `truncated`
is `true` when the cursor sent is older than the retained archives.

```bash theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii trigger compose::logs --namespace dev worker=api tail=200
iii trigger compose::logs --namespace dev worker=api stream=stderr wait_ms=5000
```

### Listing projects

`compose::list` returns the daemon name, its namespace, its pid, and other project information.

### Validating a file

`compose::validate` validates a compose file and is intended for package develpment work. It takes
`file`. Validation is offline, so `package://` workers are reported under `deferred_packages` and
not resolved.

#### Validation reports

| Field               | Type   | Description                                               |
| ------------------- | ------ | --------------------------------------------------------- |
| `namespace`         | string | Namespace the project's workers register in.              |
| `start_order`       | array  | Worker names in dependency order.                         |
| `deferred_packages` | array  | `package://` workers, which need the registry to resolve. |

### Stopping the daemon

`compose::stop` stops the compose project, all associated workers, and optionally the engine if
started with (`--up`). The compose daemon will also exit. Before exiting the daemon will return its
name, pid, and the projects it is about to stop.

<Warning>
  There is no `compose::start` equivalent to `compose::stop`. Stopping a compose daemon means it
  must be restarted from the server it is running on.
</Warning>

### Viewing schema

`compose::schema` takes a `function_id` argument. With no `function_id`, it returns every
`compose::*` schema. Pass a function id to return the schema for a given function\_id. The pseudo-id
`worker-compose.yaml` returns the file's JSON Schema as `request` and a complete small example as
`response`.

Each entry holds `function_id`, `description`, `request`, `response`, `default_timeout_ms`, and
`idempotent`. The same schemas, descriptions, and metadata are also published through
`engine::functions::info`.

```bash theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii trigger compose::schema --namespace dev
iii trigger compose::schema --namespace dev function_id=compose::up
iii trigger compose::schema --namespace dev function_id=worker-compose.yaml
```

## Namespaces

Namespaces are used to allow advanced architectures that require more than one running copy of a
given worker, multi-tenancy, some multi-agent workflows, and various isolation schemes between
different parts of a iii application.

Namespaces are arbitrary and their usage depends largely on the given usecase. They do not prescribe
a specific way of constructing your iii application.

The two primary points where Namespaces are used are during Worker registration via Compose and
during Function and Trigger interactions. All have ways of declaring which namespace to use.

### Precedence

| Value                    | Sets                                                                          |
| ------------------------ | ----------------------------------------------------------------------------- |
| `-n, --namespace <NS>`   | The daemon namespace, and the project namespace of every project it loads.    |
| `namespace:` in the file | The daemon namespace when `--namespace` is absent, and the project namespace. |
| Neither                  | `default`.                                                                    |

`namespace` is commonly defined in `worker-compose.yaml` but can be overriden on compose daemon
startup with the `--namespace` flag.

Likewise, compose's own `compose::*` functions will exist within the same declared namespace.

### What a namespace may hold

A valid namespace is made up of the lowercase characters `a-z`, `0-9`, `-` and `_`. All other
characters are not permitted and will result in an `INVALID_NAMESPACE` error; including uppercase
letters. To prevent naming conflicts there is no coercion of invalid namespaces to valid namespaces.

### One daemon to a namespace

Two compose daemons with different namespaces can share an engine. However, a second daemon claiming
a namespace that is already served is refused at registration with `DAEMON_ALREADY_SERVING`.

<Warning>
  Only one Compose daemon can serve a namespace on a iii engine. Set `namespace:` in the compose
  file or pass `--namespace` when several daemons must share one iii engine.
</Warning>

## `worker-compose.yaml`

Below is an example of version 1 of a worker compose file. Unknown keys and duplicate keys are
errors. Durations can specify a unit such as: `500ms`, `30s`, `2m`.

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
namespace: shop
startup_timeout: 60s
stop_timeout: 10s
engine:
  url: ws://127.0.0.1:49134
  workers:
    configuration:
      adapter:
        name: fs
        config:
          directory: ./config
containers:
  database_1: # Worker names do not need to match package names
    worker: package://database
    version: "0.21.4"
  api:
    worker: path://./workers/api
    start_after: [database_1]
    config_name: shop-api
    config_override:
      log_level: debug
    env_file: [./.env] # specify .envs, later in list overwrites earlier declarations
    environment:
      RUST_LOG: info # specify environment variables directly
      MY_ENV: ${MY_ENV:-defaultValue} # assign an environment variable from the execution environment
    scripts:
      pre_run: npm run migrate
      pre_run_timeout: 2m
      run: npm start
      post_run: ./scripts/cleanup.sh
  state:
    worker: package://state
    version: "0.21.4"
```

### Top-level fields

| Field             | Type   | Default | Description                                                                                   |
| ----------------- | ------ | ------- | --------------------------------------------------------------------------------------------- |
| `namespace`       | string | absent  | Namespace the project's workers register in. A project that declares none lands in `default`. |
| `startup_timeout` | string | `60s`   | Readiness budget for every worker. A worker may override it.                                  |
| `stop_timeout`    | string | `10s`   | Grace between the polite stop and the forced kill.                                            |
| `engine`          | map    | absent  | Present when this Compose invocation owns and configures the engine.                          |
| `containers`      | map    | empty   | Project workers. May be empty only when `engine:` is present.                                 |

### Engine fields

| Field                             | Type    | Default                | Description                                                                              |
| --------------------------------- | ------- | ---------------------- | ---------------------------------------------------------------------------------------- |
| `url`                             | string  | `ws://127.0.0.1:49134` | Managed engine endpoint used by Compose and its workers.                                 |
| `registration_namespace_grace_ms` | integer | engine default         | Namespace-registration grace passed to the engine.                                       |
| `workers`                         | map     | empty                  | Direct configs for engine-owned workers. Values must be mappings; use `{}` for defaults. |

Allowed worker keys are `configuration`, `iii-worker-manager`, `iii-http-functions`, `iii-stream`,
and `iii-sandbox`. Use `#instance` for another instance of an allowed type, for example
`iii-worker-manager#rbac`. Internal `iii-engine-functions`, `iii-telemetry`, and `iii-observability`
are injected and must not be declared.

<Note>
  These workers are always engine managed and started so this method of operating these workers is
  an exception to the typical way for other workers. Changes to these workers requires an engine
  restart.
</Note>

### Worker fields

Each key under `containers` is the name the worker registers under.

| Field             | Type           | Default              | Description                                                                                                |
| ----------------- | -------------- | -------------------- | ---------------------------------------------------------------------------------------------------------- |
| `worker`          | string         | required             | `path://<dir>` or `package://<name>`. A package may name its registry: `package://<registry-host>/<name>`. |
| `version`         | string         | absent               | Version range. Required for `package://`.                                                                  |
| `start_after`     | array          | empty                | Workers that must start first (ie. a worker dependency). Self-dependencies and cycles are rejected.        |
| `config_name`     | string         | absent               | The [configuration worker](./configuration) entry this worker owns.                                        |
| `config_override` | mapping        | absent               | Merged on top of the fetched configuration.                                                                |
| `working_dir`     | path           | the worker directory | Resolved against the compose file's directory.                                                             |
| `environment`     | map            | empty                | Environment variables for this worker.                                                                     |
| `env_file`        | array of paths | empty                | Read at start time, in declaration order. A later file wins on conflicting entries.                        |
| `startup_timeout` | string         | the file's value     | Readiness budget for this worker.                                                                          |
| `scripts`         | mapping        | absent               | See below.                                                                                                 |

`path://` directories resolve against the compose file's directory. A missing directory fails with
`MISSING_WORKER_DIRECTORY`, and a missing `env_file` fails with `MISSING_ENV_FILE` during
validation.

A path worker normally runs as a host process. A non-empty `runtime.base_image` in its
`iii.worker.yaml` selects a local VM instead. The worker's `scripts.install` and start command then
run inside that image, and compose keeps the VM state inside the project. An invalid image reference
fails the start instead of falling back to the host or to another image.

### Worker kinds

Worker packages can be released in multiple different "kinds". A kind compose cannot run fails with
`UNSUPPORTED_PACKAGE_KIND`.

| Kind     | How it runs                                                                     |
| -------- | ------------------------------------------------------------------------------- |
| `binary` | A child process on the host.                                                    |
| `bundle` | A VM. The start command is the bundle's own `scripts.start`, read in the guest. |
| `path`   | A path to a worker stored locally on disk.                                      |

A bundle's VM is booted by `iii-worker`, which the installer ships beside `iii` and which needs
glibc on Linux. Compose runs it as a process rather than linking it, so the engine stays portable; a
bundle worker on a machine without `iii-worker` fails saying so, and every other worker kind is
unaffected.

Bundles need a VM, and windows has none: a bundle worker there fails with `BUNDLE_NEEDS_A_VM` before
anything is downloaded. Run compose under WSL, where the VM has KVM to run on. Every other worker
kind runs on windows as it always has.

Bundle support can be refused machine-wide with `III_BUNDLE_WORKERS_DISABLED=1`, which compose
honours.

### Scripts

| Field             | Type   | Default | Description                                         |
| ----------------- | ------ | ------- | --------------------------------------------------- |
| `pre_run`         | string | absent  | Runs to completion before the worker is spawned.    |
| `pre_run_timeout` | string | `60s`   | Budget for `pre_run`. Rejected without a `pre_run`. |
| `run`             | string | absent  | Start command. Rejected for `package://` workers.   |
| `post_run`        | string | absent  | Runs after the worker's exit is confirmed.          |

Both hooks run with the worker's environment, working directory, and their own process group. A
`post_run` runs after the worker stops but before the compose daemon exits. The top-level
`stop_timeout` argument is a global timer for a compose daemon to stop. If this time is exceeded all
scripts will be exited along with the compose daemon.

A path worker's start command is `scripts.run` in `worker-compose.yaml`, or `scripts.start` in the
worker's own `iii.worker.yaml` when `run` is absent. A worker with neither fails with
`MISSING_START_COMMAND`.

### Configuration precedence

Lowest to highest: the configuration a package ships, the entry in the configuration worker, then
`config_override`. The merged result is written to an owner-only file and its path is passed to the
worker as `III_CONFIG`. A worker that declares `config_name` does not start when the fetch fails;
the error is `CONFIG_FETCH_FAILED`.

## The worker environment

A worker's environment is defined by the following sources:

1. A host baseline. On Unix: `PATH`, `HOME`, `USER`, `LOGNAME`, `SHELL`, `TERM`, `TMPDIR`, `TZ`,
   `LANG`, `LC_ALL`. Windows adds the variables the platform needs, such as `SystemRoot`, `COMSPEC`,
   and `PATHEXT`.
2. The worker's `env_file` entries, then its `environment` map.
3. The reserved variables, which the daemon owns.

| Variable                | Value                                                                         |
| ----------------------- | ----------------------------------------------------------------------------- |
| `III_URL`               | The engine address the daemon is connected to.                                |
| `III_NAMESPACE`         | The project's namespace.                                                      |
| `III_COMPOSE_NAMESPACE` | The supervising Compose daemon's namespace for explicit `compose::*` routing. |
| `III_COMPOSE_FILE`      | Canonical path of the compose file that owns this worker.                     |
| `III_COMPOSE_DIR`       | Canonical directory that contains the owning compose file.                    |
| `III_WORKER_NAME`       | The key under `containers`.                                                   |
| `III_CONFIG`            | Path to the resolved configuration file. Absent when there is none.           |
| `III_CONFIG_NAME`       | The configuration entry the worker owns. Absent when it declares none.        |

Declaring a reserved variable in `environment` or an `env_file` fails with `RESERVED_ENV_OVERRIDE`.

### Expanding values from environment variables

Variables can be expanded with `${VAR}`. References expand in any value, not only in `environment`.
For example:

```yaml theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
containers:
  queue:
    worker: path://${WORKERS_DIR}/queue
    environment:
      RUST_LOG: ${RUST_LOG:-info}
```

| Written        | Means                                                                                           |
| -------------- | ----------------------------------------------------------------------------------------------- |
| `${VAR}`       | The value from compose's own environment. Unset, the file is refused with `UNDEFINED_VARIABLE`. |
| `${VAR:-text}` | The value, or `text` when it is unset. `${VAR:-}` makes it optional and empty.                  |
| `$VAR`         | Nothing. A bare name is left alone, so a `scripts.run` holding `$PWD` still reaches the shell.  |
| `$${VAR}`      | A literal `${VAR}`.                                                                             |

<Warning>
  `config_override` is never expanded, all values are treated as literals.

  ```yaml theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
  config_override:
    # Reaches the worker exactly as written.
    api_key: ${ANTHROPIC_API_KEY}
  ```
</Warning>

## Build registry packages

```text theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii compose build [-f, --file <PATH>]
```

| Option              | Description                                                                               |
| ------------------- | ----------------------------------------------------------------------------------------- |
| `-f, --file <PATH>` | Compose file whose registry packages should be downloaded \[default: worker-compose.yaml] |

`build` reads and validates the compose file, then downloads every `package://` worker into the same
cache used by `compose::up`. The file defaults to `./worker-compose.yaml`. The command does not
connect to an engine, start a worker, or run lifecycle hooks. Local `path://` workers and
engine-managed workers need no registry download and are skipped.

```bash theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii compose build --file worker-compose.yaml
iii compose --up --file worker-compose.yaml
```

The cache is shared under `~/.iii/compose/packages`, or under `III_COMPOSE_STATE_DIR` when that
variable is set. A later `compose::up` reuses a valid cached artifact if it exists.

## Readiness

A worker is considered up when the engine reports a worker of that name in the project's namespace.
Compose polls `engine::workers::list` every 200 ms until the worker's `startup_timeout` runs out.

| Outcome                                                        | Code                               |
| -------------------------------------------------------------- | ---------------------------------- |
| The worker never appeared.                                     | `STARTUP_TIMEOUT`                  |
| The process exited while compose was waiting.                  | `CHILD_EXITED_BEFORE_REGISTRATION` |
| It registered in `default` instead of the project's namespace. | `WORKER_IGNORED_NAMESPACE`         |
| It registered under a different name in the right namespace.   | `WORKER_NAME_MISMATCH`             |
| Its functions landed outside the project's namespace.          | `FUNCTIONS_IN_WRONG_NAMESPACE`     |
| A worker already held that name in the namespace.              | `CONTAINER_NAME_TAKEN`             |

After a worker is ready, the daemon checks it every 250 ms. A worker that exits takes its transitive
dependents down with it and is recorded as `failed`. Automatic restarts are not performed. When the
engine connection drops and comes back, every running worker gets its `startup_timeout` to register
again.

Dependency shutdown is dependent upon when a shutdown happens:

| When                      | What comes down                                                                                                                  |
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| During `up`               | The operation ends. Everything it started is stopped in reverse order, and workers later in the start order are never attempted. |
| After the worker is ready | Its transitive dependents. Everything else keeps running.                                                                        |

## Where compose keeps state

Compose state is stored under `~/.iii/compose`, or under `$III_COMPOSE_STATE_DIR` when that is set.

| Path                        | Contents                                                                                                            |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| `<ns>/<project>/state.json` | One project's child records. Owner-only.                                                                            |
| `<ns>/<project>/config/`    | Resolved configuration files.                                                                                       |
| `<ns>/<project>/logs/`      | Rotating stdout and stderr for every project worker.                                                                |
| `<ns>/<project>/vm/`        | VM state for bundle and local-image workers, one directory each, plus the config each one publishes into its guest. |
| `packages/`                 | Installed `package://` artefacts, shared by projects.                                                               |

`<ns>` is the daemon's namespace. `<project>` is derived from the compose file's canonical path + a
short hash to prevent project name collisions.

```bash theme={"theme":{"light":"catppuccin-latte","dark":"dark-plus"}}
iii trigger compose::status --namespace dev file=./worker-compose.yaml
# ... "state_dir": "/home/you/.iii/compose/dev/shop-3f2a1b9c"
ls /home/you/.iii/compose/dev/shop-3f2a1b9c/logs/
```

## Error codes

Compose can output the following error codes:

| Area                | Codes                                                                                                                                                                                                                                                                                                                                                           |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Compose file        | `COMPOSE_FILE_UNREADABLE`, `INVALID_COMPOSE_FILE`, `EMPTY_CONTAINERS`, `INVALID_DURATION`, `UNKNOWN_DEPENDENCY`, `SELF_DEPENDENCY`, `DEPENDENCY_CYCLE`, `UNSUPPORTED_WORKER_SOURCE`, `UNDEFINED_VARIABLE`, `INVALID_REFERENCE`, `UNTERMINATED_REFERENCE`                                                                                                        |
| Worker declaration  | `MISSING_VERSION_FOR_PACKAGE`, `RUN_NOT_ALLOWED_FOR_PACKAGE`, `NOT_A_PACKAGE_CONTAINER`, `PRE_RUN_TIMEOUT_WITHOUT_PRE_RUN`, `RESERVED_ENV_OVERRIDE`, `MISSING_ENV_FILE`                                                                                                                                                                                         |
| Worker resolution   | `MISSING_WORKER_DIRECTORY`, `MISSING_START_COMMAND`, `INVALID_MANIFEST`, `INVALID_WORKER_SPEC`, `WORKER_SOURCE_CHANGED`, `ENGINE_WORKER_IS_BUILTIN`                                                                                                                                                                                                             |
| Packages            | `REGISTRY_UNREACHABLE`, `PACKAGE_NOT_RESOLVED`, `PACKAGE_NOT_INSTALLED`, `PACKAGE_DOWNLOAD_FAILED`, `PACKAGE_DIGEST_MISMATCH`, `PACKAGE_ARTIFACT_EMPTY`, `REGISTRY_NAME_REFUSED`, `UNSUPPORTED_PACKAGE_KIND`, `UNSUPPORTED_PLATFORM`, `BUNDLE_NEEDS_A_VM`                                                                                                       |
| Start and readiness | `SPAWN_FAILED`, `HOOK_SPAWN_FAILED`, `HOOK_FAILED`, `HOOK_TIMEOUT`, `STARTUP_TIMEOUT`, `CHILD_EXITED_BEFORE_REGISTRATION`, `WORKER_IGNORED_NAMESPACE`, `WORKER_NAME_MISMATCH`, `FUNCTIONS_IN_WRONG_NAMESPACE`, `CONTAINER_NAME_TAKEN`, `CONFIG_FETCH_FAILED`, `CONFIG_PUBLISH_FAILED`, `ENGINE_CALL_FAILED`, `PROJECT_DID_NOT_START`                            |
| Managed engine      | `ENGINE_SECTION_REQUIRES_MANAGED_START`, `ENGINE_ALREADY_OWNED`, `ENGINE_RESTART_REQUIRED`, `ENGINE_WORKER_IS_INJECTED`, `UNSUPPORTED_ENGINE_WORKER`, `INVALID_ENGINE_WORKER_CONFIG`, `INVALID_MANAGED_ENGINE_URL`, `MANAGED_ENGINE_ENDPOINT_MISMATCH`, `MANAGED_ENGINE_LISTENER_UNAVAILABLE`, `ENGINE_SPAWN_FAILED`, `ENGINE_STARTUP_TIMEOUT`, `ENGINE_EXITED` |
| Daemon and project  | `NO_COMPOSE_FILE`, `WRONG_DAEMON`, `INVALID_NAMESPACE`, `UNKNOWN_CONTAINER`, `UNKNOWN_PROJECT`, `INVALID_STATE_FILE`, `STATE_DIR_UNAVAILABLE`, `DAEMON_ALREADY_SERVING`, `DAEMON_NAMESPACE_TAKEN`, `IO_ERROR`                                                                                                                                                   |
| Command line        | `FILE_REQUIRES_UP`, `BUILD_CONFLICTS_WITH_SERVE_OPTIONS`                                                                                                                                                                                                                                                                                                        |

## Related

<Note>
  For why compose is a worker see [Understanding iii / Compose](../understanding-iii/compose). For
  understanding namespaces see [Understanding iii / Namespaces](../understanding-iii/namespaces).
  For the `iii compose` entry in the command tree, see the [CLI
  reference](../cli-reference/index#iii-compose).
</Note>
