# 13. Choose an engine

> **Status: partly implemented.** Routing has expanded in two layers:
> (1) the CLI router (`coax/lib/agent/engine/route.coax`) asks Gemini Decision
> 1 first, then local Elephant or mammoth-1 (44/44 on the 44-message CLI set);
> and (2) the unified **System One Front v2** (`openagents.router.v2alpha1`
> `RoutePlan` in [`proto/openagents/router/v2alpha1/router.proto`](../../proto/openagents/router/v2alpha1/router.proto)
> and [`coax/lib/agent/router/`](../../coax/lib/agent/router/README.md), `#14056`)
> routes across all 8 OpenAgents capability packs, compute tiers
> (`local_inkling`, `cloud_fast`, `cloud_deep`, `remote_station`), and opt-in
> external CLI delegation (`#14071`), scoring **66/66** on the tuned set and
> **30/31** on the held-out set (2026-11-14). See
> [System One Front v2 (`RoutePlan`)](#system-one-front-v2-routeplan) and
> [The door order](#the-door-order).

The labelled accuracy below measures agreement with a routing policy, not
which engine produces the best patch. The [repository pilot](../../docs/coder/repository-pilot.md)
compares fixed engines and automatic routing on the same tasks, with
provider failures separate from coding failures. A returned probability is
a model score; calibration is not established by 22 correct labels.

Coder picks an engine, a model and an effort for each task. The choice is a
typed decision from System One, not a keyword rule.

## The request

[coax/lib/agent/engine/route.coax](../../coax/lib/agent/engine/route.coax),
the router every agent owner shares, sends the task and this machine's
environment as the state:

```json
{"message":"fix the failing test","environment":{"os":"macOS","app":"Coder in the terminal","agents":{"claude_code":{"installed":true},"codex":{"installed":false}},"local_model_server":false}}
```

An agent counts as installed when its CLI is in a `PATH` directory. A local
model server counts when `PSIONIC_BASE_URL` is set, so the route makes no
probe of its own.

It asks three typed questions:

| Question | Type | Answers |
| --- | --- | --- |
| `engine` | choice | `claude`, `codex`, `micro`, `psionic`, each with a label that says what it is for |
| `difficulty` | score | trivial, easy, moderate, hard, very hard |
| `effort` | choice | `low`, `medium`, `high` |

The labels split Claude Code and Codex by the job's shape: work that spans
files or needs understanding first goes to Claude Code, and quick contained
jobs go to Codex. They don't split by whether the task mentions code. A
plain question or small talk goes to Microcoder. The unified delegation router
([`coax/lib/agent/router/`](../../coax/lib/agent/router/README.md)) builds on
these same shape-based labels; earlier labels that split by "code" sent
refactors and one-line edits near 50/50.

Each key comes from its variable (`GEMINI_API_KEY`, `BASETEN_API_KEY`), or
from its file under `~/work/.secrets/` (`openagents-gemini-provider.env`,
`baseten.env`), into curl's environment only (`SoClient.key_value` in
`coax/lib/systemone/transport.coax`). It never appears in Coder's own
environment, curl's arguments or a process listing.

## What runs

The decision is the session's second line, and the transcript says what
ran and why:

```text
› what is 17 times 23? Reply with only the number.
  session 20261102-070956-632
  Engine: micro (0.96) · trivial · effort low, chosen by System One
● 391
  └ finished · 3.7s
```

```json
{"kind":"route","ok":true,"engine":"micro","probability":0.936,"difficulty":0,"effort":"low","ran":"microcoder"}
```

The live state also includes `eligible_engines`, separately from installation.
Coder runs the decision only when it can:

- Candidates must be installed/configured, permitted by `CODER_ROUTE_ENGINES`
  and outside a recent typed provider-failure cooldown. The exact allowlist
  names are `claude,codex,micro,psionic`; empty permits all, invalid lists
  permit none. This restricts automatic routing, not an explicit engine choice.
- Below a probability of 0.5, or without a usable decision, the first eligible
  engine runs in Claude, Codex, Microcoder, Psionic order. The log says why.
- A model-selected engine outside that eligible set cannot dispatch.
- With no eligible engine, the run fails before inference. Its route and failed
  ending are logged; the interactive composer history retains the task for correction.
- `--engine` overrides routing and cooldowns, allowing an intentional retry.

Typed terminal provider failures persist a local cooldown of at least 60 seconds.
A rejected Claude rate-limit event can extend it to the provider's reported
reset (bounded to seven days). This is observed failure evidence, not a health
probe or a claim that quota remains. No failed task is automatically resent
elsewhere: it may already have produced effects. See the
[observability contract](../../docs/protocol/coder-observability-v1.md) for
records, conservative storage limits and reset behavior.

`coder route "TASK"` prints the decision alone, with `--json` for scripts.

## Effort

The decision's effort goes to the engine that runs:

- Claude Code gets `--effort=low`, `--effort=medium` or `--effort=high`.
- Codex gets it as the turn's `effort`.
- Microcoder already asks for its own effort in each step.

Coder passes the effort only when System One is sure (probability 0.5 or
more). Otherwise the engine keeps its own default. `--effort low|medium|high`
sets it yourself and wins over the decision. The model stays the one you
pass with `--model`, or the engine's default: the decision doesn't pick one.

## In the terminal UI

The terminal UI keeps the agent you picked: `--engine`, `/engine` or
Shift-Tab. Pass `--engine auto`, or type `/engine auto`, to let System One
pick instead. It asks at the first message of each conversation, switches to
the pick, and prints the same route line as `coder run`:

```text
› what is 2+2? answer with just the number
  session 20261102-080710-579
  Engine: micro (0.93) · trivial · effort low, chosen by System One
● 4
  └ finished · 3.1s
```

When System One isn't sure, or there is no decision, the current agent runs
and the route line says why. Later messages in the conversation stay with
that agent, so its session keeps its context.

## The door order

Since 2026-11-13 (#13473) the route asks Gemini Decision 1 first when a
Gemini key is available. Without one, Elephant at the configured System One
URL answers, else mammoth-1, and the route line's note says why. When Gemini
fails for a reason other than a rule of the request or a safety block, the
same fallback answers. When the consent ledger has no grant and a local
Elephant is configured, Elephant answers without one. A `"door"` in
router.json, or `CODER_ROUTE_DOOR`, names one door (`gemini-decision-1`,
`elephant-1` or `mammoth-1`), and only that door is asked. On the labelled
set Gemini routes 44 of 44, the same as mammoth-1, a little slower
(`coder/tui/route-door-eval.coax`,
[the comparison](../../docs/research/2026-11-13-gemini-decision-1.md)).

## The Decisions API

The server's `/api/v1/systemone` has answered `"model":"auto"` with Gemini
Decision 1 first and elephant-1 behind it since 2026-11-13. Before that, as
of 2026-11-02, the path didn't answer:

- `https://openagents.com/api/v1/decisions` returns 404.
- `https://openagents.com/api/v1/systemone` is a NIP-98-signed mesh route
  to elephant-1 Pylon providers (`coax/web/api/mesh-client.sh`). With the
  accelerators off, it has no providers to answer.

Coder calls the doors directly through `coax/lib/systemone`, the same
request the desktop Lab sends.

## System One Front v2 (`RoutePlan`)

With `oa-worker` owning primary turns across local Inkling, OpenAgents Cloud,
and capability packs (`#14053`, `#14056`, `#14071`), routing generalized from
picking one of four coding CLIs into `openagents.router.v2alpha1`
([`proto/openagents/router/v2alpha1/router.proto`](../../proto/openagents/router/v2alpha1/router.proto))
implemented in [`coax/lib/agent/router/`](../../coax/lib/agent/router/README.md):

- **Code decides eligibility first (`policy.coax`):** stale station reports
  (>2 min), keep-clear flags, disk floors, max agents, logged-out accounts, and
  opt-in external delegation gates (`#14071`: external CLIs `claude`, `codex`,
  `antigravity`, `cursor` are ineligible unless the user explicitly enables
  external harness delegation or names them) are enforced deterministically in
  Coax before any model call.
- **Typed `RoutePlan` questions (`ask.coax`, `decide.coax`):** selects
  `primary_pack`, `secondary_pack`, `compute_tier` (`local_inkling`,
  `cloud_fast`, `cloud_deep`, `remote_station`), `explicit_delegate`,
  `difficulty`, `effort`, `continue_active`, and `missing_capability` (Episode
  289 P9 authoring trigger).
- **Verified labelled sets (`replay-check.coax`):** **66/66** wants right on
  the 33-task tuned set and **30/31** on the 20-task held-out set against
  recorded Gemini Decision 1 exchanges (`fixtures/google/`,
  `fixtures/google-held/`), plus 30,000 policy fuzz mixes and 2,048 seeded
  codec goldens (`wire-goldens.coax`).

## Checks

### Local Elephant and desktop routing (2026-11-04)

OpenAgents Desktop replaces manual agent/model/effort dropdowns with this router.
Shared `~/.openagents/coder/router.json`, with schema `coder.router.v1`, selects
a System One `url` and explicit `remote` boolean. The desktop's default is
keyless local Elephant at `http://127.0.0.1:8793/v1/systemone`; it reuses the
already-resident server. `PSIONIC_SYSTEMONE_URL` overrides the URL. Remote
Pylon fleet/proxy doors require `remote:true` (or `CODER_ROUTE_REMOTE=1`) and
the existing `system-one` session grant. A remote HTTPS door may read
`PSIONIC_SYSTEMONE_KEY` through curl's environment. The public NIP-98 endpoint
is a distinct authenticated transport, not a bearer-HTTP drop-in.

Without a Gemini key, a config or an override, the hosted Mammoth path above remains.
A configured Elephant failure never falls through to it. The eligible default
still works, and the route record says why a decision was unavailable or unsure.
Configured Elephant uses compact typed questions, an eight-second timeout and
UTF-8-safe projections: 768 current-task bytes and 256 recent-context bytes.
The worker receives the complete task and fork history. `fork --engine auto`
reroutes each follow-up; ordinary forks and TUI conversation routing retain their
existing behavior. Explicit `--engine`, `--model` and `--effort` still override.

The initial automatic Claude profile maps trivial to Haiku, easy/moderate to
Sonnet, hard/very hard to Opus. Codex keeps its configured model and uses the
decided effort. No absent or cooled-down engine can become eligible from a
model answer. New local/Pylon routing records identify `coder.route.v2` and its
source/model profile; the legacy record and System One wire are unchanged.
See [the proto](../../proto/openagents/coder/route/v2/route.proto) and
[desktop instructions](../../apps/openagents-desktop/README.md).

Finite live probes distinguished a typo (Codex/low) from a multi-service
refactor (Claude/high/Opus). Native automatic first/follow-up chats preserved
context. The router policy killed all 88 mutants with zero survivors; 432
Coder checks passed. These probes do not replace the held-out Mammoth accuracy
record below, and do not establish broad Elephant routing quality. No Pylons
were advertised in the current CLI discovery; the desktop uses local Elephant.

```sh
coax test coder/tui/coder.coax           # the request's state and questions, the decision's text
python3 coder/tui/smoke.py               # offline: no key, so the default runs and the log says why;
                                         # --effort reaches a stand-in claude, and bad values are refused
python3 coder/tui/route_eval.py          # live: the held-out half on mammoth-1
python3 coder/tui/route_eval.py train    # live: the tuning half
```

```text
held: 22/22 right on mammoth-1, mean probability of the right picks 0.95
train: 22/22 right on mammoth-1, mean probability of the right picks 0.92
```

[route-labels.tsv](../../coder/tui/tests/route-labels.tsv) is the labelled set:
44 messages, 22 to tune and 22 held out, 16 for Claude Code, 16 for Codex
and 12 for Microcoder. It is the desktop Lab's set with its labels renamed.
No message in it needs the psionic engine, so that answer has no check yet.

## Dependencies

- [Chapters 9–12](PLAN.md#ii-engines): the engines.
- [Chapter 16](PLAN.md#iii-many-tasks): System One decisions as a tool.

## Done means

- [x] The held-out half routes at a recorded accuracy on a named decision
  model: 22/22 on mammoth-1, 2026-11-02.
- [x] `coder run "TASK"` with no `--engine` prints the decision and runs
  it.
- [x] The decision's effort reaches the engine: Claude Code's `--effort`
  and Codex's turn `effort`; `--effort` overrides it. The model stays the
  person's.
- [x] The terminal UI routes on request: `--engine auto` and `/engine auto`.
- [ ] The question goes through the Decisions API, Clef first. Blocked
  until the API serves; see [The Decisions API](#the-decisions-api).

## Sources

- `coax/lib/agent/router/` (`decide.coax`, `policy.coax`, `ask.coax`,
  `wire.coax`, `station.coax`, `eval.coax`, `replay-check.coax`,
  `wire-goldens.coax`) and `proto/openagents/router/v2alpha1/router.proto`.
- `coax/lib/agent/engine/route.coax`, `route-policy.coax`, `availability.coax` and
  `availability-policy.coax` (the CLI router); `coder/tui/route.coax`
  (Coder's consent gate around it), `coder/tui/route-labels.tsv`,
  `coder/tui/route_eval.py`.
- `coax/lib/systemone/` (`request.coax`, `answers.coax`, `transport.coax`).
