# Station definition

A station's handler is the default export of `src/stations/<CODE>__<name>/station.js`:

```js
import { defineStation } from '@assemblyline/lib-factory';
import { z } from 'zod';

export default defineStation({
  code: 'IN1',
  configSchema: z.object({ approval_limit: z.number().default(5000) }),
  execute: async (config, ctx) => ({ checked: 12 }),
  isolate: false,
});
```

How to write one: [Write a station](/docs/guides/write-a-station).

## `defineStation(definition)`

| Field | Type | Required | Default | What it does |
|---|---|---|---|---|
| `code` | string | yes | | The station's short code. The factory session finds the handler by it. |
| `configSchema` | zod schema | no | none | Parses the run's config before `execute`: fills defaults, drops keys it doesn't name, fails the run when the config doesn't fit. Without it, `execute` gets the config as stored. |
| `execute(config, ctx)` | async function | yes | | The work. `config` is the parsed config; `ctx` is the [station context](/docs/reference/station-context). Its return value is the run's output, stored on the work record as `output`. It must be JSON. |
| `isolate` | boolean | no | `false` | `true` runs each run in its own child process, so the station's CPU work or a crash can't stall or end the factory session. |

`defineStation` returns the definition and throws when:

- `code` is missing: `defineStation: code is required`
- `execute` is not a function: `defineStation: execute must be a function`
- the definition has an `enabled` field. A station is turned on and off on the Orchestrator (`al-orc stations edit IN1 --enabled false`), not in code.

A station's name, description, line, conveyor, schedule, switches and config live in its definition on the Orchestrator: see [al-orc](/docs/reference/al-orc).

## The config check

When `configSchema` rejects the config, the run fails before `execute` runs, with one line per field:

```text
[IN1] Invalid config:
  - approval_limit: Invalid input: expected number, received string
```

Before the check, the handler prints the raw config to the session's log: `[IN1] Config: {…}`.

## An isolated run

With `isolate: true`, the factory session still claims and reports the run. A child process imports `station.js`, runs the same handler with the same `ctx`, sends back the output or the error, and exits.

- A crash in the child fails only that run, as retryable: `Isolated run ended without a result (exit code 3)`, with the child's exit code or signal.
- Each run starts Node and imports the station's dependencies again, and module-level caches start empty.
- A local run (`npx factory stations run`) always runs in-process.

## The folder

| Part | Rule |
|---|---|
| Folder | `src/stations/<CODE>__<name>/`, one per station |
| Handler | `station.js` in that folder; its default export is `defineStation(…)` |
| Code | The part of the folder name before `__` equals the handler's `code`, case included |
| Other files | Not imported by discovery. `station.js` imports what it needs. |

## What discovery refuses

`discoverStations()` imports each folder's `station.js` in name order and keys the handler by `code`. It throws, and the factory session doesn't start, when:

| Case | Error |
|---|---|
| The default export isn't `defineStation(…)` | `<file>: the default export is not defineStation(...)` |
| The code doesn't match the folder | `<file>: code 'IN2' doesn't match its folder 'IN1__check'` |
| Two folders hold the same code | `Two stations with code 'IN1': …` |

A folder with no `station.js` is skipped. A run of a station the session has no handler for fails at once, without a retry: `Unknown station: IN1`.

## `createStationHandler(definition, options)`

The function a factory session calls for a run, exported for tests. It parses the config with `configSchema`, runs `execute` and returns its output.

```js
const output = await createStationHandler(definition, { local: true })({
  station: { code: 'IN1', config: { approval_limit: 1000 } },
  work_record: { id: 'local-test' },
});
```

| Option | Default | What it does |
|---|---|---|
| `local` | `false` | The attach calls write files under `.factory/work_records/` and never call the Orchestrator |

How to test with it: [Run and test a station](/docs/guides/test-a-station).
