magi-cli 0.270.1

Blind multi-agent implementation competition: N agents implement, M judges rank blind, deliberate, vote privately, winner survives double review + E2E gate
Documentation
# Agent CLIs, sessions and seats

Conventions for this area of magi. `AGENTS.md` indexes these files: read this
one before changing the code it covers.

## Editing-seat filesystem scope

`agent::invoke` grants repository editing nodes their actual shared Git
directory plus the `questions` and `notifications` stores, through the backend's
supported directory mechanism (agy's `--add-dir`; a codex editing seat runs
without a sandbox and needs none, see "codex is the only seat whose read-only
mode is enforced"). Keep this policy centralized for initial turns,
resumes, fallbacks and waiter delivery. Never widen a read-only or deputy seat
to the entire state root. Deputies/chats retain explicit grants. For read-only
seats in repositories registered through `[coordination]`, the launcher holds the
real exclusive lock through `repo_lock::SeatLease` without widening their
sandbox. All chat turns (`queue::CHAT_NODE`), including consultation delivery
and waiter-style continuations through `talk::turn`, bypass `SeatLease` and the
per-repository gate: conversation must not wait behind repository work sessions.
Transcript-only title generation (`talk-title`) uses the same scheduling bypass,
with a separate read-only seat, no chat control bridge and no shared session.
This bypass does not change their filesystem grants or sandbox policy. A fair per-repository
gate admits concurrent readers or one editor;
editors take their own OS lock after readers have released theirs. External
reader-lock contention waits before starting the CLI or its budget, never as
an agent failure. `seat_process::Running` transfers cancelled processes and
coordination to an independent cleanup thread and releases only after the CLI
and discovered tool descendants terminate. Registered seats currently require
Unix containment; other platforms fail closed before spawning.
Coordination is disabled without `[coordination]`. Machine and repository layers
may set `contract_file`, `contract_token`, and `lock_file`; all three are required
after normal layer merging and teravars rendering. Names are plain files inside
the Git common directory, never paths or names containing `..`; the trimmed
contract contents must match the configured token. A missing configured contract
leaves the repository unregistered. Pass this policy explicitly through
`Invocation` and `SeatLease`, including resumes and launcher operations; never
infer it from discovered files or a task-local wait policy. Existing external
protocols require those three keys in the operator's machine config.
Editing-seat directory grants permit the
repository's lock contract but do not acquire or shorten that lock.
Backend enforcement limits, host verification and
the held-task recovery procedure are in `docs/seat-write-scope.md`.

## Agent CLIs are the only backend

magi drives subscription CLIs (`claude -p`, `opencode run`, `agy -p`,
`codex exec`) and never an HTTP API. That is a product decision, not an
unfinished one: the CLIs carry
the operator's own plan and expose the agent's whole tool loop. **Do not add an
API-key path**, and do not add a `reqwest`-shaped dependency — `examples/smoke.rs`
deliberately exercises filesystem and serialization instead of a TLS handshake
because there is no handshake to exercise.

## Gemini CLI is deliberately unsupported

`AgentKind` has no `Gemini` variant. Google retired the standalone client for
individual accounts ("This client is no longer supported for Gemini Code Assist
for individuals") in favour of Antigravity, which is the `agy` binary and the
`antigravity` kind. Anyone re-adding a Gemini adapter is adding dead code; the
escape hatch for any other CLI is `kind = "command"`.

## Session mechanics are per-CLI and verified by hand

Multi-turn nodes (deliberation, the review loop) depend on each seat keeping one
CLI conversation. The mechanics were established empirically, not from
docs, and each is asserted in `src/agent.rs` tests:

| CLI | open | resume | trap |
|---|---|---|---|
| `claude` | `--session-id <uuid>` | `--resume <uuid>` | the uuid must be RFC 4122 v4 or the CLI rejects it (`src/rng.rs` mints it); an expired OAuth session requires re-authentication before resuming |
| `opencode` | `--format json` → `sessionID` | `-s <id>` | nothing to resume until a turn reported an id |
| `agy` | `--output-format json` → `conversation_id` | `--conversation <id>` | print mode defaults to a **5 minute** timeout; `--print-timeout` must track the node budget. `--disable-slash-commands` silently disables `--mode`, so magi never passes it |
| `codex` | `exec --json` → `thread.started.thread_id` | `exec … resume <id>` | `resume` is a **subcommand** and rejects every `exec` option that follows it (`unexpected argument '--sandbox'`), so magi emits all options first and the subcommand last. The prompt goes on stdin, which codex reads only when the prompt argument is `-`. The answer is the **last** `item.completed` whose item is an `agent_message`: earlier ones narrate the tool loop |
| `omp` | `-p --mode=json` → the `id` on its `"type":"session"` line | `-p … --resume <id>` | a turn that ends on a **tool call** emits **no** `agent_end` line, so the answer has to be the last non-empty assistant text block across `agent_end` / `turn_end` / `message_end` — reading only `agent_end` silently discards a complete review. The prompt goes on stdin. `--continue` opens a *new* session, so it is never used |

`agent::has_session` is the single place that decides whether a follow-up prompt
may rely on memory. If it says no, the node re-sends full context. Never assume
a resume worked.

## `[roles]` synthesizer / chatter / conductor take a fallback chain

Each accepts a string (unchanged) or an ordered array of ids
(`config::AgentChoice`, untagged). `agent::pick_chain` resolves it: unset or
empty is `agent::pick`'s default order; a duplicate id keeps its first
appearance; an unknown or uninstalled id is skipped with a `tracing::warn`;
nothing resolving is an error naming the role. `agent::chain_advances` is the
one place that decides to move on (error, quota - judged apart from `usable`
- or an unusable answer).

- **Each id is tried at most once per call.** That is the whole retry bound:
  a plain `for`, never a loop back. An exhausted chain ends as the last
  single failed seat would (`Ok(None)` for the synthesizer, the first
  agent's failure note for chat, the same error for the conductor).
- **A fallback agent gets a fresh `SeatState`**, never the previous agent's
  seat renamed, so `agent::has_session` is false and full context is
  re-sent. The conductor falls back only through the call and the parse of
  its reply, never through `apply`, which would file queue changes twice.
- **A chat's fallback persists.** `talk.agent` switches to the agent that
  answered (with a magi note in the transcript); quota coming back does not
  move it home, an operator's agent switch does. Only a talk whose agent is
  in `[roles] chatter`'s chain falls back; an explicit `--agent` is a chain
  of one. A turn can now cost up to N x the turn timeout.
- **`[roles] fixer` is a chain too** (`config::Roles::fixer`,
  `ResolvedRoles::fixer: Option<Vec<AgentSpec>>`, resolved by `agent::pick_chain`
  only when set: unset stays `None` and means the winner's own implementer, as
  before). `src/fixer.rs` holds what the five consumers share - the review loop's
  fix round, the operator-selected fix, the gate-fix round, `land::fix_round` and
  `rebase::rebase_with_fixer`: `fixer::attempts` (the order and each entry's seat
  key), `seat_for` (a fresh `SeatState` for another agent, session id minted from
  the agent too) and `failure_of`. Each consumer is a plain forward `for`, each id
  at most once, advancing only through `agent::output_advances` (the `Ok` half of
  `chain_advances`); only the **last** attempt's outcome reaches the old `match`,
  so an exhausted chain ends with the single fixer's wording, one `QuotaLoss` and
  the same refund. An intermediate quota is a handover record only.
- **A fixer fallback is sticky within a run and never persisted.** The start is
  read from `RunState::handovers`: the last handover on a `fixer::FIX_NODES` node
  (`fix`, `gate-fix`, `land`, `rebase`) whose `to` is on the chain. Every consumer
  records under exactly one of those names - a name nobody records sends every
  round back to the first agent and re-bills its quota. A call runs from the
  start to the end of the chain, forward only (no wrap), so the last entry's
  failure ends it. Only the chain's first entry may sit in the winner's
  `impl-<label>` seat; a fallback that is the winner's author takes the `fix`
  seat, fresh.
  `FixRecord` / `GateFixRecord` / `RebaseFixRecord` `.agent` is the agent that
  answered; `run::SCHEMA` is unchanged. In a rebase round the round is spent and
  saved before the first call; a chain advance only rewrites the record's `agent`.
- `[roles]` roster roles (implementers, judges, reviewers, advisors) are
  untouched.

## opencode has no read-only mode

`--auto` gates **every** permission in opencode, reads included. A judge or
reviewer seat spawned without it cannot even open its own prompt file and drops
out with *"the user rejected permission to use this specific tool call"* — which
is exactly what happened on the first live run, silently costing a seat on the
panel. So magi always passes `--auto` for `kind = "opencode"`, and read-only-ness
for those seats rests on the prompt plus the fact that judge worktrees are
deleted after the tally and reviewer worktrees are `reset --hard` to the commit
under review every round. Do not "fix" this by withholding `--auto`.

## codex is the only seat whose read-only mode is enforced

`--sandbox read-only` is refused by the CLI, not by the prompt, so a codex
judge or reviewer cannot write even if it decides to. **Unattended read-only
seats never get `--dangerously-bypass-approvals-and-sandbox`** (judge,
reviewer, advisor, conductor, dupes, title ...): it would throw away
the one enforced guarantee in the roster. Neither does a deputy, which may
write only its stores: it keeps `--sandbox workspace-write` with explicit
`writable_roots`.

**Codex repository editing seats do get the bypass** — implementer, fixer,
grafter, gate fixer, land and rebase resolvers, and a chat turn in a
repository that opted in with `[talk] allow_write` (`agent::repository_writer`),
on every initial turn, resume, fallback and waiter delivery to such a seat.
Owner decision 2026-10-27: they run without the OS sandbox, matching claude's
`bypassPermissions`, because codex's macOS seatbelt sandbox refuses temporary
directories under `$TMPDIR` and aborts Chrome, so magi's own browser and
integration tests could not run and editing tasks ended as no-ops. The flag
replaces `--sandbox` / `approval_policy` / `writable_roots` and sits before
`resume`. Consequence accepted: an editing seat can write outside its worktree
and reach the network; what limits it is the prompt, the worktree discipline
and the launcher's own checks, exactly as for claude.
`codex_editing_seats_bypass_the_sandbox_on_initial_and_resume` pins which roles
get it and `codex_is_sandboxed_reads_stdin_and_puts_resume_last` that the rest
do not.

Sandboxed unattended seats also pass `-c approval_policy="never"`: a seat that stops to
ask blocks until its node timeout kills it, and there is nobody at the
terminal during a run.

## omp is opencode's shape, and its answer is not where the stream looks

`kind = "omp"` reaches DeepSeek (its models ship in `omp`'s own catalog) and
`gpt`-class models through the same CLI, but it has **no read-only mode**: like
opencode, `--auto-approve` gates every permission, reads included, and a
non-interactive seat without it cannot open its prompt file at all. So magi
always passes it, and read-only-ness for those seats rests on the prompt plus
the worktree discipline named above — never on the flag. `--dangerously-bypass-
approvals-and-sandbox` appears nowhere for omp (codex's editing-seat exception does not extend to it).

The extraction is the part that will be got wrong by anyone reading the event
names instead of the stream. `omp -p --mode=json` emits `agent_end` **only** for
a run that quiesces on a message turn; a turn that ends on a tool call
(`stopReason: "toolUse"`) — which is what the review seats here routinely do —
ends with no `agent_end` line at all, and the answer is in `message_end` /
`turn_end` instead. A first hand-written wrapper keyed on `agent_end` and
silently discarded three complete reviews; `omp_takes_the_answer_without_an_agent_end_line`
exists so that cannot come back. The answer is the **last** non-empty assistant
text block, because earlier ones narrate the tool loop (sometimes as a bare
`.`). `--continue` opens a *new* session rather than the stored one, so resume is
always `--resume <captured id>`.

## Agents are users of this CLI

`agent::invoke` exports `MAGI_RUN` and `MAGI_NODE`, and `magi task add` reads
them to attribute a filed task to the seat that filed it. Nothing else may set
those variables, which is what makes "most of the backlog was filed by agents" a
measurement rather than a claim. `Invocation` carries `run`/`node` purely for
this; they must not influence behaviour.

## Jev for the English check is a role, never an HTTP client

`[roles] language_judge` names an agent (normally a `kind = "command"` wrapper
around Jev) that `github_text::judge_language` asks whether a pull request
title and its prose are English. Unset spawns nothing; a missing agent, a spawn
failure, a non-zero exit, a timeout (15 s for the whole call), a quota hit or a
reply that is not `{"title_english": bool, "body_english": bool}` all yield
`None`, and the built-in heuristics decide as before. No API key and no HTTP
dependency, so offline tests and CI are unaffected.

- The same role also writes the one description rewrite: `graph::Runner::rewrite_pr_text` first resumes the winning implementer's conversation when one exists, then falls back to `agent::pick_chain` over this role (unset = default pick), skipping the implementer's agent, in a separate `github-text-rewrite` seat created only when its turn comes. It advances on error, quota, unusable output or a reply with no `TITLE:` line, so an implementer that is out of quota (runs 7861 / 344a: agy, a week from its reset) does not end the rewrite. A classification-only wrapper therefore ends in the owner question. No handover is recorded; the result is re-checked by the gate.
- The wrapper receives the prompt through `{prompt_file}` and prints the JSON
  object on stdout (a code fence or earlier log lines are tolerated).
- Only prose goes out (code, quotes and `<details>` are dropped) and only after
  `scrub`; sensitive-data detection always runs on the original text.
- A decision replaces the ASCII default only (ASCII text passes without one).
  "Not English" always stands; "English" still has to clear the non-ASCII share floor.
- Wired into `graph::Runner::guarded_pr_message` (first check, the rewrite's
  re-check and the final `prepare_with`); `bump.rs` and `land.rs` still use the
  heuristics. The event log records which source decided, never the text.