# Fix runs

> Fix runs, for `@nexus fix this`: Gateway fixes one GitHub issue in a throwaway cloud sandbox on Nexus's provider account and opens the pull request as the person's agent. The run reads the issue and the repository through the GitHub connector, runs the coding agent's turns through Gateway, and runs every command in the sandbox with no network and no credential. It opens the pull request with `github/pull_create` through the person's agents' writes, so only an agent link starts one. A run is a job: its state and its pull request are read at `GET /v1/cloud/runs/{id}`. The sandbox is charged as cloud compute. Rust only.

## Create cloud run

`POST /v1/cloud/runs`

Start a fix run for one issue of a GitHub repository, as the calling agent link. Gateway reads the issue and the repository at its default branch through the GitHub connector, runs the coding agent in a cloud sandbox, and opens a pull request that closes the issue with `github/pull_create`. The organization must have agent writes on with `github.pull_create` on its allowlist, and the person their own agents' writes on; otherwise the run fails with `write_not_granted`, `chat_writes_off` or `operation_not_allowed` and opens nothing. Send an `Idempotency-Key`: the run's id is derived from it, so a resend answers the run the first send started.

Permission: `cloud.runs.use` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `Idempotency-Key` | header | string | no | Send the same key again to retry safely: the request runs once. See [Idempotency keys](/docs/concepts/idempotency). |

### Request body (application/json, required)

- `repo` (string, required): `https://github.com/<owner>/<name>`
- `issue` (integer, required): The issue's number [min 1]

### Responses

- `200` The run a send with this key started (application/json, CloudRun)
  - `id` (string, required): The run's job id (`job_…`)
  - `state` ("accepted" | "running" | "done" | "failed" | "cancelled", required)
  - `repo` (string, required)
  - `issue` (integer, required)
  - `pullRequest` (object | null, required): The pull request the run opened, once it is `done`
    - `number` (integer, required)
    - `url` (string, required)
  - `error` (object | null, required): Why the run failed, once it `failed`: `write_not_granted`, `chat_writes_off` or `operation_not_allowed` (the pull request write), `no_change`, `agent_stopped`, `repo_too_large`, `link_ended`, `budget_exceeded` and others
    - `code` (string, required)
    - `message` (string, required)
  - `createdAt` (string (date-time), required)
  - `updatedAt` (string (date-time), required)
- `202` Accepted: the run waits for the job worker (application/json, CloudRun)
  - `id` (string, required): The run's job id (`job_…`)
  - `state` ("accepted" | "running" | "done" | "failed" | "cancelled", required)
  - `repo` (string, required)
  - `issue` (integer, required)
  - `pullRequest` (object | null, required): The pull request the run opened, once it is `done`
    - `number` (integer, required)
    - `url` (string, required)
  - `error` (object | null, required): Why the run failed, once it `failed`: `write_not_granted`, `chat_writes_off` or `operation_not_allowed` (the pull request write), `no_change`, `agent_stopped`, `repo_too_large`, `link_ended`, `budget_exceeded` and others
    - `code` (string, required)
    - `message` (string, required)
  - `createdAt` (string (date-time), required)
  - `updatedAt` (string (date-time), required)
- `400` The body is not `{ repo, issue }`, `repo` is not a GitHub repository's URL (`invalid_repo`), `issue` is 0 (`invalid_issue`), or the `Idempotency-Key` is missing.
- `402` Nothing covers 15 minutes of the run's sandbox (`budget_exceeded`), or the organization is past due (`payment_past_due`). The refusal is recorded.
- `403` The credential is not an agent link (`agent_link_required`): a person's own key reads runs but never starts one. Or the link does not hold `cloud.runs.use` (`role_required`), or the organization may not run cloud compute (`cloud_not_enabled`).
- `422` This `Idempotency-Key` named another run (`idempotency_key_reused`).
- `429` The person has two runs going (`fix_runs_busy`); try again after `Retry-After`.
- `503` Fix runs are off on this Gateway (`fix_runs_unavailable`): they need `NEXUS_FIX_RUNS=on`, a provider account (`BOAT_API_KEY`), the job worker (`NEXUS_JOB_WORKER`) and a database. The route still mounts.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | `invalid_request_error` | A field is missing or not valid; the message names it |
| 400 |  | The body is not `{ repo, issue }`, `repo` is not a GitHub repository's URL (`invalid_repo`), `issue` is 0 (`invalid_issue`), or the `Idempotency-Key` is missing. |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 402 | `budget_exceeded` | Nothing covers 15 minutes of the run's sandbox (`budget_exceeded`), or the organization is past due (`payment_past_due`). The refusal is recorded. |
| 403 | `role_required` | Your role does not have this permission |
| 409 | `idempotency_in_progress` | A request with the same Idempotency-Key is still running |
| 409 | `idempotency_interrupted` | The first request with this Idempotency-Key was cut off and may have run in part |
| 422 |  | This `Idempotency-Key` named another run (`idempotency_key_reused`). |
| 422 | `idempotency_key_reused` | The Idempotency-Key was used with a different request |
| 429 |  | The person has two runs going (`fix_runs_busy`); try again after `Retry-After`. |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |
| 503 |  | Fix runs are off on this Gateway (`fix_runs_unavailable`): they need `NEXUS_FIX_RUNS=on`, a provider account (`BOAT_API_KEY`), the job worker (`NEXUS_JOB_WORKER`) and a database. The route still mounts. |

### Example

```sh
curl -X POST "$NEXUS_URL/v1/cloud/runs" \
  -H "Authorization: Bearer $NEXUS_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{
    "repo": "https://github.com/nexus-demo/hello-factory",
    "issue": 1
  }'
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#post%3A%2Fv1%2Fcloud%2Fruns)

## Get cloud run

`GET /v1/cloud/runs/{id}`

A fix run of the caller's person: its state, its pull request once it is `done`, and why it failed once it `failed`. Another person's run is a 404, as a missing one is.

Permission: `cloud.runs.use` (Every member).

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `id` | path | string | yes | The run's id (`job_…`). |

### Responses

- `200` The run (application/json, CloudRun)
  - `id` (string, required): The run's job id (`job_…`)
  - `state` ("accepted" | "running" | "done" | "failed" | "cancelled", required)
  - `repo` (string, required)
  - `issue` (integer, required)
  - `pullRequest` (object | null, required): The pull request the run opened, once it is `done`
    - `number` (integer, required)
    - `url` (string, required)
  - `error` (object | null, required): Why the run failed, once it `failed`: `write_not_granted`, `chat_writes_off` or `operation_not_allowed` (the pull request write), `no_change`, `agent_stopped`, `repo_too_large`, `link_ended`, `budget_exceeded` and others
    - `code` (string, required)
    - `message` (string, required)
  - `createdAt` (string (date-time), required)
  - `updatedAt` (string (date-time), required)
- `404` No such run of the caller's person (`not_found`).
- `503` Fix runs are off on this Gateway (`fix_runs_unavailable`): they need `NEXUS_FIX_RUNS=on`, a provider account (`BOAT_API_KEY`), the job worker (`NEXUS_JOB_WORKER`) and a database. The route still mounts.

### Errors

| Status | Code | When |
| --- | --- | --- |
| 401 | `authentication_error` | No key, or a key Gateway does not accept |
| 403 | `role_required` | Your role does not have this permission |
| 404 | `not_found` | Nothing matches the path in your organization |
| 404 |  | No such run of the caller's person (`not_found`). |
| 429 | `rate_limit_error` | Too many requests from this key in a minute |
| 503 |  | Fix runs are off on this Gateway (`fix_runs_unavailable`): they need `NEXUS_FIX_RUNS=on`, a provider account (`BOAT_API_KEY`), the job worker (`NEXUS_JOB_WORKER`) and a database. The route still mounts. |

### Example

```sh
curl "$NEXUS_URL/v1/cloud/runs/<id>" \
  -H "Authorization: Bearer $NEXUS_API_KEY"
```

Try it: [API Explorer](/app?next=%2Fapi-explorer#get%3A%2Fv1%2Fcloud%2Fruns%2F%7Bid%7D)
