# Debug a run

Start from the work record: its status tells you where to look. A run that failed has its error on the work record and the whole story in the factory session's log. A run that never started is waiting for a session at its line's location.

This page is about runs. If the run completed but a workpiece landed in `failed/`, see [Find out why a workpiece failed](/docs/guides/handle-failures).

## Find the run

```bash
al-orc work-records list --station IN1 --status failed
al-orc work-records show <id> --output --activity
```

`--output` shows the config the run was given and what the station returned, `--activity` the run's events, such as retries, and `--report` the report your station attached. `--all` shows everything.

| Status | What happened | Look at |
|---|---|---|
| `pending` | No factory session has claimed the run. After 30 minutes the run becomes `timed_out`. | Is a session running at the line's location? `al-orc factory-sessions list --location acme-office`, `al-control procs list` |
| `running` | The station is running now. After 4 hours the run becomes `timed_out`. | The session log |
| `failed` | The station threw on its first try and on 3 retries, or the session had no handler for it. See [Throw errors from a station](/docs/guides/station-errors). | The error on the work record, then the session log |
| `completed` | The station finished. Workpieces may still have failed. | `al-control lines status IN -f .` and [Find out why a workpiece failed](/docs/guides/handle-failures) |

All statuses are in the [work record object reference](https://orchestrator.assemblyline.sh/docs/objects/work-record).

## Read the session log

The log is the session's output: the terminal you ran `npm start` in, or `al-control procs logs -f .` under pm2. Every line has one of these shapes:

| Line | Comes from |
|---|---|
| `--- wr:t7Qx9mPa2Lc start` … `done` or `failed` | One try of one run, opened and closed. The id is the work record's. |
| `[IN1] …` | Inside that station: its `Config`, then whatever your code prints |
| `[Factory] …` | The session itself: start-up, files it wrote, attachments sent, poll errors, a station's error |
| `[conveyor] …` | The conveyor: what it moved into each station's `input`, and any workpiece it couldn't move |
| `[bin-watcher] …`, `[intake-watcher] …` | The watchers starting runs, and their warnings |

One try of a run that failed, with the conveyor and another station running at the same time:

```text
--- wr:t7Qx9mPa2Lc start
[IN2] Config: {"ledger_url":"https://ledger.example.com"}
[conveyor] IN1 from IN0/output: moved 3
[bin-watcher] triggered IN1 (work_record_id=K3vn8sWq0Rb)
--- wr:K3vn8sWq0Rb start
[IN1] Config: {"approval_limit":5000}
--- wr:K3vn8sWq0Rb done
[Factory] wr:t7Qx9mPa2Lc [IN2] error: ledger_env: missing LEDGER_API_KEY (see .env.example)
--- wr:t7Qx9mPa2Lc failed
```

- **Search by the work record id** (`grep wr:t7Qx9mPa2Lc`) to pull one run out of the log. A run that is retried shows one block per try.
- **Blocks can interleave.** The session runs different stations at the same time, so read each line's prefix to know which station printed it.
- **`Config`** is exactly what your station received, before its schema filled in defaults. When a station behaves as if a setting is wrong, start there.

## Usual causes

| You see | Cause | Fix |
|---|---|---|
| The run stays `pending` | No session is running at the line's location, or the session's `FACTORY_LOCATION` differs from the line's `location` | Start the session, or correct one of the two. See [Run a factory session](/docs/guides/run-a-factory-session). |
| `No handler for station` and `Unknown station` | The session didn't find that station's handler | Check the folder and `code` in `src/stations/`, and restart the session. See [How a session finds your stations](/docs/guides/run-a-factory-session#how-a-session-finds-your-stations). |
| `Invalid config` | The station's config doesn't match its `configSchema` | The error lists each bad field. Compare it with the `Config` line, and see [Write a station](/docs/guides/write-a-station). |
| `Missing required environment variables` | The session's `.env` lacks a setting | See [Give stations their credentials](/docs/guides/environment-and-secrets) |
| `is served by another factory session` | A session already runs at this location | Stop the other one, or give this one its own location |
| `Poll failed: 401` | The API key or secret in `.env` is wrong or revoked | Create a new key in the Orchestrator's settings |

## Run the station on your machine

To try a fix without waiting for the next run, run the station with the config in its station file:

```bash
npx factory stations run IN2
```

It runs for real, the same code the session runs. See [Run and test a station](/docs/guides/test-a-station).
