# Writing pipelines with an AI agent

> What to give an AI agent so it writes correct pipeline files, and the loop it should follow.
> **Available** in repositories branchd reads pipeline files for. It is switching repositories on one at a time: until yours is, it keeps running its `.forge-ci.yaml` or `.xeonr-ci.yaml`. See [What is available](https://docs.branchd.run/branchd/pipelines#what-is-available).

> **Not yet available** (cli 0.39.0): `branchd ci lint` checking every event a file lists (as the push check does) and `branchd run start --pipeline … --input …` are in the next CLI release. `ci lint` and `ci plan` from 0.33.0 read pipeline files; `branchd upgrade` updates.

## Give it three things

1. **The complete reference, one file**:
   `https://docs.xeonr.io/branchd/pipelines/reference.md`: every key,
   rule and limit in plain Markdown. For worked examples too, every page of
   the section in one fetch:
   `https://docs.xeonr.io/branchd/pipelines/llms-full.txt` (index:
   `…/pipelines/llms.txt`).
2. **The schema**: `https://docs.xeonr.io/schemas/branchd/pipeline.json` (and
   `step.json`, `packages.json`).
3. **The commands** it may run: `branchd ci lint` and `branchd ci plan`, from
   the repository's root.

A prompt that works:

```text
Write .branchd/pipelines/ci.yaml for this repository. Read
https://docs.xeonr.io/branchd/pipelines/llms-full.txt first and use only
keys it documents (the reference alone is …/pipelines/reference.md); the schema is
https://docs.xeonr.io/schemas/branchd/pipeline.json. After each change, run
`branchd ci lint` and fix every refusal it prints, then run
`branchd ci plan` for a push to main and for a pull request
(`branchd ci plan --event pull_request`) and check that the jobs I expect run.
Do not push until lint passes.
```

## The loop

1. **Write** the file, starting from the closest guide's example. Every
   example names its path on its first line.
2. **`branchd ci lint`**: compiles every pipeline file for a push to the
   default branch, to `stage`, to a feature branch, a `v` tag, a pull request,
   a scheduled run and a manual run of each pipeline that has one, then each
   file for every event its `on:` lists (as branchd checks a push). Exit code 1
   means at least one case was refused; each refusal names the file and line,
   and most say what to write instead
   ([Refusals](https://docs.branchd.run/branchd/pipelines/errors)).
3. **`branchd ci plan`**: lists every job, whether it runs for that ref and
   event, and why. `--ref <branch or tag>`, `--event push|pull_request|schedule|manual|trigger`,
   `--pipeline <name>` and `--input k=v` choose the case. `--json` prints it for a program to read.
4. **Push.** branchd compiles the changed files again for every event; a
   file the push breaks refuses the push with `file:line:column: message`.
   Fix it and push again; do not use `-o ci.allow-invalid`. Then read the
   run: `branchd run watch <n>`, then
   `branchd run failure <n>` if it fails.

## Things agents get wrong

- **GitHub Actions habits.** There is no `actions/checkout` (the checkout is
  automatic), no `runs-on: ubuntu-latest` (jobs name an `image:`), no
  `github.*` context (use `branch`, `sha`, `event`, `pr.number`), no
  `hashFiles()` (use `cache.files`), and no `${{ secrets.X }}` (list the secret
  under the job's `secrets:` and read `$X`).
- **GitLab CI habits.** `stages`, `script`, `rules`, `variables` and `extends`
  are refused, each with its replacement in the message.
- **`if:` on outputs.** `if:` is decided before the run starts and cannot read
  `steps.*` or `needs.*` outputs.
- **A need on a job that does not run.** Give both jobs the same `if:`, or mark
  the need `optional: true`.
- **`.yml`.** Files end in `.yaml`.
- **`${{ secrets.X }}` and `github.token`.** The job token is
  `$BRANCHD_TOKEN`, and it reads only its own repository; ask for more with
  `permissions:`.
- **A typed step among `run:` steps** needs `isolated: true` and a
  `workspace:`.
- **Assuming the CLI is in the job.** It is not; use `curl` and `git` with
  `$BRANCHD_TOKEN`.

Source: https://docs.branchd.run/branchd/pipelines/ai-agents
