# Library API

Three entries: `fleetjobs` queues and follows jobs, `fleetjobs/schedule` holds the scheduler and the replay, and `fleetjobs/schema` holds the types. All are ES modules for Node 22.20 or later.

## `fleetjobs`

```ts
import { cancel, status, submit, wait } from "fleetjobs";

const id = await submit(["pnpm", "test"], { lane: "heavy" });
const exit = await wait(id, { output: { stdout: write, stderr: write } });
```

### `submit(argv, options?)`

Queues a command and returns the job id as a string. While a job with the same working directory and `argv` is queued or running, it returns that job's id and starts nothing.

| Option | Default | Meaning |
| --- | --- | --- |
| `cwd` | `process.cwd()` | The working directory |
| `lane` | `"heavy"` | `"heavy"` or `"light"` |
| `remote` | from `HEAVY_LOCAL` and `HEAVY_REMOTE`, else `"auto"` | `"auto"`, `"any"` or `"never"` |
| `blocking` | `false` | The caller waits for the job, so it leaves the queue after 10 minutes without a waiter, not 60 |

### `wait(id, options?)`

Resolves with the job's exit code once it is done. `options.output` is `{ stdout, stderr }`, two functions that receive each chunk of the job's output as a `Uint8Array`, from the start of the job. It rejects when the id names no job, and when the job's supervisor died before the job ended.

### `status()`

Resolves with a `Job[]`: the jobs that are queued or running, and the ones that ended in the last 10 minutes, oldest first.

### `cancel(id)`

Stops the job and resolves once its record is done. It does nothing for a job that already ended, and rejects for an unknown id.

## `fleetjobs/schedule`

```ts
import { schedule } from "fleetjobs/schedule";

const decisions = schedule(state, "default");
```

| Export | What it is |
| --- | --- |
| `schedule(state, policy)` | Pure. Takes a `QueueState` and `"bash"` or `"default"` and returns one `Decision` for each queued job |
| `SLOTS`, `HALF_LIFE` | The local slot names, and the half-life of the usage count in seconds |
| `replay(logs, options)` | Runs the scheduler over `Logs` and returns a `ReplayReport`, schema `fleetjobs/replay@1` |
| `REPLAY_SCHEMA` | `"fleetjobs/replay@1"` |
| `readLogs(files)`, `defaultLogFiles()` | Read log files, and list the ones `heavy replay` reads by default |
| `parseText(text, file)`, `parseJobs(text, file)` | Parse the bash heavy's text log, and `log/jobs.jsonl` |
| `emptyLogs()` | An empty `Logs` to parse into |

`ReplayOptions` is `{ policy, from?, to?, hosts? }`, where `from` and `to` are `YYYY-MM-DD` and `hosts` is a list of `{ name, slots }`. The report holds `parsed`, `assumptions`, `lanes` and `days`; run `heavy replay --json` to see one.

`schedule` reads no clock, disk or network: it uses `state.now`. A `Decision` is `{ id, start, slot?, finishAt? }` to start a job in `start`, which is `"local"` or a host name, or `{ id, wait }`, where `wait` says why the job waits.

## `fleetjobs/schema`

Types and one constant: `JOB_SCHEMA`, the string `"fleetjobs/job@1"`. The types are `Job`, `QueueState`, `Decision`, `Policy`, `Host`, `Mirror`, `Sinks`, `Transport` and `TrustEntry`. The `Job` fields are in [the job record](https://fleetjobs.loadsheet.dev/reference/job).

<!-- vitehub-docs:section-links -->
## In this section

- [Command line](https://fleetjobs.loadsheet.dev/raw/reference/cli.md)
- [Config file](https://fleetjobs.loadsheet.dev/raw/reference/config.md)
- [Environment variables](https://fleetjobs.loadsheet.dev/raw/reference/environment.md)
- [Library API](https://fleetjobs.loadsheet.dev/raw/reference/api.md) (this page)
- [Job record](https://fleetjobs.loadsheet.dev/raw/reference/job.md)

Next: [Job record](https://fleetjobs.loadsheet.dev/raw/reference/job.md)

All pages: [llms.txt](https://fleetjobs.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/fleetjobs/issues). Report a security vulnerability privately, never in a public issue.
