# autopilot-on: reasoning and detail

<!-- toc -->
- [Why doctor gates arming (step 1)](#why-doctor-gates-arming-step-1)
- [Why a picker rather than a label alone](#why-a-picker-rather-than-a-label-alone)
- [Source queries (step 4)](#source-queries-step-4)
- [Why the JQL is run before saving](#why-the-jql-is-run-before-saving)
- [Keeping the Mac awake (step 5)](#keeping-the-mac-awake-step-5)
- [Config keys not written (step 6)](#config-keys-not-written-step-6)
- [The menu bar indicator (step 7)](#the-menu-bar-indicator-step-7)
- [Probe warnings (step 8)](#probe-warnings-step-8)
- [Rendering the plist (step 8)](#rendering-the-plist-step-8)
- [What `autopilot-control.mjs on` does](#what-autopilot-controlmjs-on-does)
- [Why the report says what it says (step 9)](#why-the-report-says-what-it-says-step-9)
<!-- /toc -->

Read by `/multi-agent:autopilot-on` when a step needs its reasoning explained,
when Jira is selected as a source (step 4), or when the probe or launchd reports
a problem (step 8).

## Why doctor gates arming (step 1)

Arming unattended work on an install that `doctor` calls broken produces a queue
of failures, one per item.

## Why a picker rather than a label alone

Push rights usually reach far more repos than you own, many of them other
people's. A label is a filter; anyone who can open an issue in a repo you happen
to have push on could add one. The gate is the repo selection, and it is per
machine.

Both halves of the repo list are shown because silently dropping the
`unavailable` rows looks exactly like a permissions problem, and the user goes
hunting for a token fault that does not exist.

There is no `add-repo` / `remove-repo` command because two entry points to one
list is two lists; re-running the picker is how repos are added and removed.

## Source queries (step 4)

| Source | Query | Extra condition |
|---|---|---|
| GitHub | `gh issue list --repo <r> --label <label> --assignee @me --state open` | **assigned to you** (`githubAssignee`, `"*"` for any), so a label an issue form applies is not enough |
| Jira | `assignee = currentUser() AND labels = "<label>" AND resolution = EMPTY AND status not in (Done, Closed, Cancelled)` | **assigned to you**, so a label somebody else adds is not enough |

A repo on the analysis queue is queried a second time per source, with the
analysis label in place of the development one and the same assignee rule, so
the second queue never widens who can give the machine work. `jiraJql` is the
development search only: a repo with a custom JQL still picks analysis work up
by label.

### Why analysis is opted into and not defaulted

The two queues deliver different things - one opens a pull request, the other
writes a document - and both spend a run. A repo selected in step 3 said yes to
development work and to nothing else, so the analysis pass runs only for a repo
that said yes to it as well (`analysisQueue`, which naming an analysis label
also sets). A default here would quietly start producing documents in every
repo the picker ever touched, which is the opposite of what selecting a repo
was understood to mean.

Two Jira caveats to say out loud when Jira is selected: labels are one global
namespace across the whole instance and anyone can write them, so on a shared
instance prefer a qualified name (`agent-queue-<team>`); and the Labels field has
to be on the edit screen for the issue types in play. An instance where neither
holds is a `jiraJql` config change - a saved filter, a component - not a
redesign.

## Why the JQL is run before saving

A JQL that returns nothing is not an error and is usually correct on day one
(nothing is labelled yet), but a JQL that errors is a config that will be silent
forever - the Labels field missing from an edit screen looks exactly like an
empty queue from the outside. "0 items, query valid" and "query rejected" are
reported differently for that reason.

## Keeping the Mac awake (step 5)

While the mode is on, a second launchd agent, `com.multi-agent.autopilot-awake`,
runs `/usr/bin/caffeinate -s -i` from **Start** until `autopilot-off`. Without it
the machine can sleep between ticks, and launchd does not wake it for the next
one: the queue stops with nothing to say so. `-s` holds system sleep off on AC
power; `-i` holds idle sleep off on battery too. Display sleep does not lock the
keychain; system sleep can, which is the part `-s -i` prevents regardless of the
display answer.

## Config keys not written (step 6)

Keys the schema reserves and nothing reads (`askOnLowMaturity`, `reportStatus`,
`sendItemKeys`) are not written. A config written by an older version may carry
`depthRouter` or one of those; the schema still accepts them and nothing reads
them, so drop them when rewriting the file.

## The menu bar indicator (step 7)

Built from source on demand, never shipped as a binary: the package contains only
text, and a compiled artifact in it would need signing and notarization to be
distributable. The compiled binary is the one thing an update does not replace,
so the runner rebuilds it on the first tick after `autopilot-menubar.swift`
becomes newer than `bin/menubar`. A machine that is already on picks up a new
indicator without running this command again.

## Probe warnings (step 8)

`PROBE_WARNING=` lines do not block: a login keychain that locks on sleep or
after a timeout is readable now and locked for the tick after the Mac slept,
which every tick checks again and refuses with the same message.
`features/autopilot-operations.md` has the probe's rules.

## Rendering the plist (step 8)

Run before the Start / Configure only question:

```bash
. "$HOME/.claude/lib/autopilot-state.sh"
TPL=$(ma_ap_asset templates/multi-agent-autopilot.plist.template) || {
  echo "BLOCKED: the plist template is not installed on this host - re-run the installer"; exit 1; }
DEST="$HOME/Library/LaunchAgents/com.multi-agent.autopilot.plist"
INTERVAL=$(bash "$HOME/.claude/scripts/autopilot-status.sh" --json | jq -r '.scanIntervalSeconds // 120')
sed -e "s|{{NODE}}|$(command -v node)|g" \
    -e "s|{{TICK}}|$HOME/.claude/scripts/autopilot-runner.mjs|g" \
    -e "s|{{LABEL}}|com.multi-agent.autopilot|g" \
    -e "s|{{ROOT}}|$HOME/.claude/autopilot|g" \
    -e "s|{{INTERVAL}}|$INTERVAL|g" \
    -e "s|{{HOME}}|$HOME|g" \
    -e "s|{{CLAUDE}}|$(command -v claude || true)|g" \
    "$TPL" > "$DEST"
```

`ma_ap_asset` resolves the template rather than a literal
`$HOME/.claude/templates/...`, because on a Copilot-only or Codex-only machine
that tree may not exist. The state root stays shared on purpose - two CLIs on one
machine must see one queue, not two.

`command -v node` rather than the bare word: launchd does not read a login
shell, so its `PATH` is the short one in the plist's own `EnvironmentVariables`,
and a Homebrew or nvm node that is not on that path would make every tick fail
with a message nobody is watching for. `claude` is captured the same way, and
the plist's `PATH` carries `~/.local/bin` as well, which is where the native
installer puts it; the runner tries the captured path first, then that `PATH`.

## What `autopilot-control.mjs on` does

The same script the office panel's `POST /v1/autopilot/on` calls. It keeps
exactly the named repos in `config.json`, probes their credentials again
(`autopilot-arming.mjs probeCredentials`), renders
`templates/multi-agent-autopilot.plist.template` with its seven placeholders
(NODE, TICK, LABEL, ROOT, INTERVAL, HOME, CLAUDE), loads it with `launchctl
bootstrap` (falling back to `load`) and installs the awake agent with the saved
`awake.display` answer. `--configure-only` saves the selection and installs
neither.

## Why the report says what it says (step 9)

"Configure only" lets the queue be watched with `autopilot-status` for a while
before anything runs. Both runs are worktrees and per-repo concurrency is 1, so
the queue skips a repo you are working in rather than competing for
`.git/index.lock`.

Each of the five items is something a user has asked about after the fact.
launchd loads at **login**, not boot, and that is correct: the login keychain is
what unlocks the tokens, so a run before login could not reach Jira or GitHub
anyway.
