# Run next: pin a queued job

`kinhin jobs --queued` (also `kinhin queue`) and the Fleet tab's **Queued work** card list the jobs waiting in your
forges' queues, first in, first out; see [Requeue and skip queued jobs](https://kinhin.dev/guide/requeue-skip.md). **Run next** pins one of
them.

## What kinhin controls

The forge owns the queue. GitHub, GitLab and the others decide which job a free runner takes, and kinhin cannot move a
job up in that order. What kinhin does decide is **which runtime gets the next VM**: each pass it counts the queued
jobs per runtime and starts the VM for the runtime with the most unmet demand.

**Run next** pins one job. While a pinned job waits, the scale plan gives the next VM to that job's runtime ahead of the
runtime with the most demand, as long as that runtime can take one. Two limits to know:

- The runner that comes up registers with the forge like any other, and the forge hands it whichever matching job it
  picks. A pin makes the right kind of VM appear sooner; it does not promise that this exact job runs first.
- A pin never exceeds a runtime's capacity cap, the fleet-wide cap or a runtime that is backing off after failures.

Pins are **ordered**. The first pinned job's runtime gets the next VM, then the second's, and so on; a pinned job
whose runtime has no unmet demand, is at its cap or is backing off is skipped for that VM. A new pin goes last. The
order is a priority among your pinned jobs, not the forge's queue order.

A pin lasts until its job leaves the queue (it started, finished or was skipped) or the daemon restarts. A forge
outage keeps the pin: an unreadable queue does not count as "the job started".

## CLI

```
kinhin queue                 # the queued jobs; the ID column is what the next two commands take
kinhin queue next ID         # pin a job to run next (it lists first, marked ▲ next); a new pin goes last
kinhin queue next ID --at N  # pin it at place N among the pinned jobs (1 = first), or move it there
kinhin queue clear ID        # clear the pin
```

`ID` is the id in the list, or any unique prefix of it. Both commands talk to the running daemon. `next` exits 2 for an
id that names no queued job (it may have left the queue since you listed). `clear` exits 0 for a job that is not pinned
or has left the queue, since there is nothing to clear, and exits 2 for an empty or ambiguous id; an id that is exactly a
queued job's id only ever clears that job's pin, never another pin that merely starts with it (`github#7` leaves a
pinned `github#77` alone, pinned or not); other ids match a single pin by prefix. Both exit 4 when no daemon runs.

## Fleet tab

Each row of the **Queued work** card has a **Run next** button. A pinned job moves to the top with a **Next** badge and a
**Clear** button, and a note says the forge still picks which job a runner takes. With more than one pinned job each
shows its place (▲1, ▲2) and a drag handle: drag a pinned row onto another to reorder, or drop an unpinned job onto a
pinned row to pin it there. **Move up** and **Move down** (the row's context menu and its VoiceOver actions) do the same
without a pointer. It works in daemon mode too, because
the pin travels over RPC; Requeue and Skip still need forge access.

The design is in `design/kinhin-app.fig` (Fleet page, "Queued jobs" frames).

## Wire

The queue entries in `status` (`queuedJobs`) gain optional `id`, `pool` and `pinned` fields, so older daemons and
clients keep working. `pin_queued_job` takes `{"id": "...", "index": 0}` (`index` is the 0-based place among the pins, optional; an older daemon ignores it) and `unpin_queued_job` takes `{"id": "..."}`; and are serialized like the other
mutating methods.
