# Dependencies & startup order

`depends_on` gates a service's startup on the state of other services. This is the difference between a wall of panes that all launch at once and a stack that comes up in the right order.

```yaml
services:
  api:
    command: "./run-api"
    depends_on:
      - name: postgres
        condition: healthy
      - name: migrate
        condition: completed

  postgres:
    command: "postgres -D ./pgdata"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -q"]

  migrate:
    command: "./migrate"
    restart: "no"
```

Here `api` starts only once `postgres` is **healthy** and `migrate` has **completed** successfully.

## Conditions

Each dependency is either a bare service id or an object with a `condition`:

| Condition | Waits until the dependency… | Requires |
|---|---|---|
| `started` | has been started (process spawned) | — |
| `healthy` | reports healthy | a [`healthcheck`](https://romnn.github.io/micromux/docs/configuration/healthchecks/) on the dependency |
| `completed` | exited **successfully** (status 0) | a service that terminates |

A bare string is shorthand for the `started` condition, or `completed` when the dependency is a [job](https://romnn.github.io/micromux/docs/configuration/services/#jobs):

```yaml
depends_on:
  - postgres          # same as { name: postgres, condition: started }
  - name: redis
    condition: healthy
```

> [!NOTE]
> The `healthy` condition only makes sense if the dependency defines a `healthcheck` — without one, a service has no health to report. Likewise `completed` is for one-shot services (migrations, seeders) that exit, not long-lived ones.

## How gating shows up

A service waiting on its dependencies sits in the **blocked** state until they are satisfied, then transitions to starting. In the TUI a blocked service is visibly distinct, so a stack that's mid-startup — or stuck behind an unhealthy dependency — is obvious at a glance. This holds on a restart too: a dependent that has already run once reports **blocked**, not **exited**, while it waits for its gate to come back.

Its log pane says the same thing. A service that is waiting has no process to produce output, so micromux writes the transition into the log itself:

```text
======== waiting for postgres to become healthy ========
```

If a dependency never becomes healthy (its probe keeps failing), the dependent stays blocked. Open the [healthcheck pane](https://romnn.github.io/micromux/docs/tui/) (`H`) on the dependency, or run `micromux ctl health <id>`, to see why.

### Dependencies that cannot recover

Some blocks are permanent: a dependency exited and its [restart policy](https://romnn.github.io/micromux/docs/configuration/restart-policies/) will not start it again (`restart: "no"`, or `on-failure` with its retries spent), or it is disabled or retired, or it is waited on as `healthy` without a healthcheck. Micromux marks a dependent blocked by such a dependency as failed, transitively: if `api` waits for `worker`, which waits for a `seed` that exited with status 1 under `restart: "no"`, both `worker` and `api` name `seed` as the cause.

```text
======== dependency `seed` exited with code 1 and will not restart, so it cannot complete successfully ========
```

`micromux ctl ls` shows the cause as `failed_dependency=seed(exited,exit_code=1)`, and [`micromux ctl wait`](https://romnn.github.io/micromux/docs/agent-control/control-plane/#waiting-from-a-script) and the MCP waits fail at once instead of running into their timeout. Restarting or enabling the dependency clears the failure (after giving it a healthcheck, for the last case), and its dependents start as soon as it meets its condition.

## Restarts respect dependencies

Because restarts go through the [control plane](https://romnn.github.io/micromux/docs/agent-control/control-plane/), restarting a service — from the TUI, `micromux ctl`, or an agent — re-applies its gating. This is why restarting *through* micromux is more correct than killing and re-running a process by hand.
