> For the complete documentation index, see [llms.txt](https://docs.codna.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.codna.ai/reference/adaptive-concurrency.md). Agents can also search and read this documentation through its [MCP server](https://docs.codna.ai/~gitbook/mcp).

# CI fleet gate

codna ci keeps a self-hosted CI runner fleet inside a chosen share of one machine. A job asks for a slot before it does real work. The gate never fails a build.

If you run your own CI runners, several of them usually share one machine, and that machine often has someone working on it. A CI system has no way to express "use most of this box, but not all of it": a provider's own concurrency setting is scoped to one repository, so two repositories can each stay inside their own limit and still fill the machine together.

`codna ci` is that missing limit. A job asks for a slot before it does real work, and gets one only while the machine is inside the share you chose.

```bash
codna ci status
```

```
fleet   : 2/12 jobs in flight
machine : 16 cores · load 8.4 · free 46.1GB
budget  : share 0.75 · load limit 12.0
state   : ~/.codna/ci-admission
  - ci/parity-tests @ runner-2
  - ci/unit-tests @ runner-4
```

Occupancy is counted **per machine, across every repository**, so the limit holds however many projects share the fleet. It is entirely capacity-shaped: nothing in it knows your repository, your language, or what your jobs do.

* **Any language.** The gate reads no source and runs no build.
* **Linux and macOS runners.** It uses only the Python standard library.
* **Any CI provider.** It is a command, not a plugin. `codna ci admit` works from any job on any runner.

Where the machine cannot be read (free memory is not available on every platform), the gate treats the value as unknown, and an unknown never holds a job back.

## Commands

| Command                                             | Meaning                                                                                                                                                                                                                         |
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `codna ci admit --job-class CLASS [--meta K=V ...]` | Wait for fleet capacity before a CI job does real work. `--job-class` is a stable identity for this kind of job (e.g. `ci/unit-tests`). Prints the lease path and, on GitHub Actions, writes it to `$GITHUB_OUTPUT` as `lease`. |
| `codna ci release [--lease PATH]`                   | Release a fleet slot and record the job's duration. `--lease` defaults to `$CODNA_CI_LEASE`.                                                                                                                                    |
| `codna ci status`                                   | Show the fleet's capacity budget and what is running.                                                                                                                                                                           |

`python -m codna.ci_admission admit|release|status` is the same command in module form.

## Setting your share

| Variable                      | Purpose                                                           | Default |
| ----------------------------- | ----------------------------------------------------------------- | ------- |
| `CODNA_CI_CPU_SHARE`          | Fraction of the machine CI may use. `0.75` leaves a quarter free. | `0.75`  |
| `CODNA_CI_RESERVE_CORES`      | Alternative phrasing: always keep this many cores free.           | unset   |
| `CODNA_CI_MAX_JOBS`           | Exact job limit, overriding the derivation above.                 | derived |
| `CODNA_CI_MIN_FREE_MEMORY_GB` | Stop adding jobs below this much free memory.                     | `8`     |
| `CODNA_CI_MAX_WAIT_S`         | How long a job may wait for a slot before starting anyway.        | `120`   |
| `CODNA_CI_ADMISSION`          | `off` disables the gate entirely.                                 | `on`    |

Set `CODNA_CI_CPU_SHARE` and `CODNA_CI_RESERVE_CORES` together and the stricter one wins. Each is a request for headroom, so neither is quietly ignored.

## In a workflow

Add it as the first step of a job, and release the slot when the job ends:

```yaml
      - name: Fleet admission
        id: fleet
        run: codna ci admit --job-class 'ci/unit-tests'

      # ... the job's real work ...

      - name: Release fleet slot
        if: always()
        run: codna ci release --lease "${{ steps.fleet.outputs.lease }}"
```

`admit` writes the lease path to `$GITHUB_OUTPUT` as `lease`, so a later step reads it as `steps.<id>.outputs.lease`. Outside GitHub Actions, pass the printed path with `--lease` or export it as `CODNA_CI_LEASE`.

{% hint style="info" %}
The gate never fails a build. If it cannot reach its own state, or a job waits longer than `CODNA_CI_MAX_WAIT_S`, the job starts anyway and the reason is printed in the step log. A limit that can break CI is worse than no limit.
{% endhint %}

Two behaviours worth knowing, because both are deliberate:

* **An idle fleet always starts a job immediately.** When the machine is over its budget the gate stops the fleet *growing* rather than blocking outright. Otherwise a workstation that runs warm all day would delay every job and then admit it anyway.
* **A slot is never lost.** A runner holds one slot at a time, so a cancelled or killed job releases its slot as soon as that runner picks up its next job, with no cleanup step required.

## See also

* [CLI Reference](https://docs.codna.ai/reference/cli.md) — the `codna ci` synopsis.
* [Configuration](https://docs.codna.ai/reference/configuration.md) — every variable and its default.
* [GitHub Action](https://docs.codna.ai/guides/github-action.md) — running Codna itself from CI.
