# The factory command

`factory` ships with lib-factory, so every factory repo has it after `npm install`, at the lib-factory version the repo pins. Run it from the factory repo's root, the folder that holds `src/stations/`.

```text
Usage: factory <command> [options]

Run this from a factory repo's root.

Commands:
  factory stations list                    List the stations in src/stations
  factory stations run <CODE>              Run one station locally: first move its conveyor.from
                                           bins into its input/, then run it with its config
                                           from the Orchestrator. Output: .factory/runs/<CODE>.json
      -e, --empty                          run with an empty config
      --set <key=value>                    change one config key for this run; repeatable.
                                           The value is read as JSON (true, 2, "x"), else text

Options:
  -h, --help       Show this help
  -v, --version    Show the lib-factory version

A local run is real: anything the station calls or writes happens. Attachments and reports
are written to .factory/work_records/ and never sent to the Orchestrator.
```

How to use it while you build a station: [Run and test a station](/docs/guides/test-a-station).

## `npx factory stations list`

Lists the handlers in `src/stations/`, by code and file. It reads the folder, not the Orchestrator.

## `npx factory stations run <CODE>`

| Flag | What it does |
|---|---|
| `--set key=value` | Changes one top-level config key for this run. Repeatable; a later flag for the same key wins. The value is read as JSON (`true`, `2`, `"x"`, `[1,2]`), and kept as text when it isn't JSON. |
| `-e`, `--empty` | Doesn't read the station from the Orchestrator: the config is `{}` plus any `--set` keys, and there is no conveyor pass |

A run, in order:

1. Loads `.env` from the factory root.
2. Reads the station's definition from the Orchestrator, with the key in `.env`: its `config` and `conveyor`. A station not defined there fails with `IN1 is not defined in the orchestrator`; pass `--empty` to run it anyway.
3. Runs one conveyor pass for the station: the workpieces in its `conveyor.from` bins move into its `input/`, with no wait for recently changed ones.
4. Runs the handler in this process, as a factory session would: the config goes through `configSchema`, then `execute` runs. `isolate: true` is ignored.
5. Prints the output and saves it.

The run is real: anything the station calls, sends or writes happens, and its workpieces move through the bins.

| Where | What |
|---|---|
| The terminal | `Station:`, `Config:` (`orchestrator` or `empty (--empty)`, then each `--set`), `Conveyed: N into IN1/input`, the station's log, and its output |
| `.factory/runs/<CODE>.json` | The station's output |
| `.factory/work_records/local-wr-<ms>/` | `report.md`, each document and file it attached, and `workpieces.json`. Nothing is sent to the Orchestrator. |
| `.factory/stations/` | The station's bins, the same ones a factory session uses |

The task the handler gets has `work_record.id` `local-wr-<milliseconds>` and `org_id` `local`.

## Exit codes

| Code | When |
|---|---|
| `0` | The station ran, or `--help` or `--version` was given |
| `1` | No command, an unknown command or flag, no `src/stations/` in the current folder, an unknown station, an Orchestrator error, or the station threw |
