Skip to content

feat(agents): let bundled plugins offer model routes - #4132

Open
atishpatel wants to merge 4 commits into
mainfrom
auto-model-picker
Open

atishpatel wants to merge 4 commits into
mainfrom
auto-model-picker

Conversation

@atishpatel

Copy link
Copy Markdown
Contributor

Summary

Bundled plugins can now offer model routes: Model menu choices such as "Auto Balanced" that pick a concrete model and thinking level for each new turn. This PR adds the host side for Claude Code and Codex. A route does nothing until a bundled plugin registers a router.

  • Plugin API. ctx.agents.registerModelRouter(router) is for bundled plugins only. For each harness, routes(harness) returns { routes, initial?, automatic?, exclusive? }:

    • routes are listed beside Default in the Model menu.
    • initial is the route new Custom agents start on.
    • automatic is the route agents on AI setup Automatic run on.
    • exclusive hides the harness's own models. The capability exists but nothing uses it yet.
    • subscribe re-reads the routes when the plugin's settings change.
  • Saving. An agent on a route saves its model as route:<id>. Claude Code and Codex never pass that id to their CLIs. Outside a turn, a route resolves to the harness default.

  • Per-turn selection. When a turn starts and the conversation is idle, the host calls router.select() with:

    • the prompt
    • a lazy history() of the thread's earlier messages
    • the harness's own models, with their thinking levels
    • an abort signal

    The router answers { model, effort?, rationale? }. The turn's activity line shows the route, the model it chose and the rationale.

  • Steering keeps the cache. A message that arrives while the conversation is active or compacting is never routed. It reuses that conversation's last routed pick, so Claude doesn't switch models mid-turn and Codex doesn't queue a new turn with a different model.

  • Failure handling:

    • An explicit per-message pick wins.
    • A router error, an unlisted model or a 30-second timeout uses the route's fallback. On Automatic agents it uses the recommended model instead.
    • A saved route whose plugin is off runs on the harness default and shows as "Unavailable route".
  • UI. Routes come first in the Model menu, in both the select and the family menu, with their description. While a route is selected, Thinking shows Auto and is disabled. Summaries and AI setup's "Runs on" name the route. Switching from Automatic to Custom starts on the Automatic route when there is one.

Non-goals: goose support, and any routing policy. The policy belongs in the plugin.

Design notes

  • Routing happens host-side at the existing per-message pick point (runner → Agents.routedPick → routeTurn). Claude Code and Codex reuse their current per-turn model and effort path, so neither runtime knows about routing.
  • The router is always given the harness's own models. The Model list that pickers and the composer see is route-wrapped.
  • Routed picks are remembered per conversation (LRU, 512 entries), keyed like session places, so a steer can reuse its conversation's pick.

Testing

  • bin/pnpm exec vitest run src/features/agents src/bundled/claude-code src/bundled/codex src/bundled/agents: all pass. Two timing-sensitive tests timed out under full-suite load (usage.test.ts, transcript.test.ts) and pass when run alone.
  • New tests:
    • model-routes.test.ts: listing and ownership, exclusive, idle select, steer reuse, explicit pick, fallback on error, unlisted model and timeout, Automatic, plugin off, cancellation.
    • ModelPicker.test.tsx: routes beside Default, exclusive, unavailable route.
    • service.test.ts: an idle turn is routed and a steer keeps its pick; routers can come only from bundled plugins.
  • tsc and Biome are clean.
  • Deferred: the desktop app hasn't been run end to end against a real router yet. This PR stays a draft until that's done and a human has tested it.

Docs: docs/plugin-architecture.md (the API) and docs/buzz-help.md (user-facing behavior).

@atishpatel
atishpatel force-pushed the auto-model-picker branch 4 times, most recently from 9aa3dc6 to 6743cca Compare November 22, 2026 19:26
@atishpatel
atishpatel marked this pull request as ready for review November 23, 2026 15:03
@atishpatel
atishpatel requested a review from a team as a code owner November 23, 2026 15:03

@wesbillman wesbillman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Star Lord recommends changes at 6743cca after a source-only review. Separately, please confirm core-maintainer agreement on the bundled-only prompt/history routing API and shared model controls before merging the host changes.

Comment thread src/features/agents/runner.ts Outdated
const pick = await pickFor(job.trigger, agent, type);
const owned = await pickFor(job.trigger, agent, type);
const result: RoutedPick =
(await this.host.route?.(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Check mention authority before invoking the router. Claude and Codex refuse unauthorized mentions inside their delivery paths, after this callback has already received the prompt and can read thread history. A refused request can also hold the mention queue while selection runs. Move selection behind the same authority check as the turn.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Fixed in 32095a5. The agents service now checks that a mention came from the agent's owner (with the same owner attestation the harnesses use, including workflow-marked mentions) before it builds a route selector. Anyone else's mention gets no route, so the router never sees its prompt. Covered in service.test.ts ("lets the harness ask a model route as its turn starts, once").

Comment thread src/features/agents/service.ts Outdated
automatic: onAutomatic(agent.config, type.id),
models: offered.models,
busy: state === "active" || state === "compacting",
last: key ? this.routedPicks.get(key) : undefined,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Resolve the route where the harness decides to start or steer. The session can become idle or busy after this sample. Even when the sample is accurate, an explicit pick can start a turn without replacing the cached route choice. A later follow-up can carry that older pick, causing Codex to queue a separate turn or Claude to send a model change during the active turn. Resolve only on the harness branch that starts a turn, including deliveries deferred by compaction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Fixed in 32095a5. The runner no longer chooses up front. Delivery now carries a lazy, memoized route() that each harness calls only where the delivery starts a turn: Claude in sessions.submit when its process isn't busy, after any compaction wait; Codex as its start() job runs, so a queued or compaction-deferred request asks when it actually starts; Goose/ACP before an idle session's level check, or as the session pumps a queued turn, including a steer that falls back to its own turn. A steer never calls it and runs on the running turn's model. The sampled busy flag and the routed-pick cache are gone. Tests cover Claude (new turn vs steer), Codex and Goose.

Comment thread src/features/agents/runner.ts Outdated
const { routed, ...pick } = result;
// The turn's activity says what its route chose, and why.
if (routed && !routed.kept)
job.work?.notice(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Honor the selected model on standalone wakes. The runner passes both picks to runOnce, but Claude and Codex disregard them in their community and ephemeral paths. A timer or watch can run the harness default after the router chooses another model. Pass the picks through both paths or limit routing to supported deliveries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Fixed in 32095a5. A watch or timer that runs as a standalone turn (unaddressed, no anchor, type has runOnce) is no longer routed. The harness starts it on its default, as before routes existed.

export const ROUTE_TIMEOUT_MS = 30_000;

/** The model value an agent saves for route `id`. */
export const routeModel = (id: string) => `${ROUTE_PREFIX}${id}`;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Preserve compatibility with older clients that sync agent settings. Saving this token changes the existing settings.model string for all devices. A client at the base revision accepts it as a provider model. Codex then refuses the turn because the provider catalog lacks the token, and Claude passes it to --model. Store route metadata using a backward-compatible representation that leaves older clients a valid model or default.

@atishpatel atishpatel Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Keeping route:<id> in model for now to avoid increasing PR complexity, by the owner's call: Instead adding a env var flag for enabling auto model routing.

Comment thread src/bundled/codex/tabs.tsx Outdated
</div>
)}
<ModelPicker
harness="codex"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Offer routes on execution targets only when they can select a model. Both harnesses offer routes on remote-bound agents, but their remote previews expose only the saved model. After saving a route, selection filters out that placeholder and has no target models against which to validate a pick or its fallback. Load the target catalog or mark routing unavailable for targets without one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Fixed in f467799: remote-bound and hosted agents get no routes in their Model menus or Automatic, and routedPick never routes their turns.

// listed ones to pick, so the picker stays.
// A saved route is the current choice; the agents service lists it.
if (routeOf(settings.model))
return { current: settings.model, models: offered, sendDefaults: true };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 · Preserve Codex’s default when a saved route disappears. When the current route is missing from the catalog, the composer falls through to the first row and sends it as a model override. That row can be a different plugin’s route or a concrete model other than the default. Expose the actual fallback to the composer or omit a model tag when using the unavailable route’s fallback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Fixed in 32095a5. Codex's model preview treats a saved route as current only while a router offers it. Otherwise it shows Codex's default model, which is what the turn runs on, and the preview cache key includes that so it refreshes when routes appear or go. Test: "shows a saved route no router offers as Codex's default".

Comment thread src/features/agents/service.ts Outdated
agent: { pubkey: agent.pubkey, name: agent.name },
prompt: promptOf(trigger),
history: () =>
(history ??= this.routeHistory(agent, trigger, place, signal)),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 · Make route selection cancellable at the request. Activity shows this request as working while it waits up to 30 seconds here, before a Stop handler is registered. The history read also captures the outer signal, so it can outlive the router timeout. Register Stop during selection and give the history read the selection’s cancellation signal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Fixed in 32095a5. While the route chooses, the runner registers its own Stop. Stop aborts the selection (its signal is the turn's lifetime plus that Stop), calls the harness's stop if it has offered one, and the delivery sends nothing because route() answers undefined. Once the choice is made, the harness's own Stop is restored. The thread history read now takes the selection's signal, runs at most once, and is aborted when the choice returns. Tests: "lets the owner stop a model route while it chooses, running nothing" and "reads the thread once, ending the read with the choice".

atishpatel and others added 4 commits November 23, 2026 12:30
A bundled plugin can now register a model router with
ctx.agents.registerModelRouter. For each harness it offers routes (such as
"Auto Balanced") that appear beside Default in the Model menu, an optional
initial route for new Custom agents, an optional route for Automatic
agents, and an exclusive switch that hides the harness's own models.

An agent on a route saves its model as route:<id>. For each turn started
while the conversation is idle, the host asks the router for a model and
thinking level, given the prompt, the thread's earlier messages and the
harness's own models. A steering message reuses the conversation's last
pick, so a running turn's prompt cache is kept. An explicit per-message
pick wins; a failure, a 30-second timeout or an unlisted model uses the
route's fallback, and a route whose plugin is off runs on the default.
The turn's activity says which model the route chose and why.

Claude Code and Codex support routes: route:<id> never reaches their CLIs,
Thinking shows Auto, and summaries name the route.

Co-authored-by: Worker Bee (Opus) <4a3bc7fd57a7b2c126e4f3d3462dd16f375242e1c176b4b3f25ca93e3edb6a84@buzz.block.builderlab.xyz>
Signed-off-by: Atish Patel <atishpatel2012@gmail.com>
Goose agents can save a plugin's model route, as Claude Code and Codex
agents can. The process starts on the provider's default, each turn runs
on the route's pick from the provider's own models, Thinking shows Auto,
and Automatic agents use the harness's Automatic route.

Signed-off-by: Atish Patel <atishpatel2012@gmail.com>
A bound or hosted agent runs on its target's models, which this device
can't list, so its Model menus offer no routes and its turns are never
routed.

Signed-off-by: Atish Patel <atishpatel2012@gmail.com>
The harness now asks a lazy Delivery.route only where a delivery starts
a new turn (Claude when its process is idle, Codex as its job starts,
Goose as its session starts or pumps a queued turn), never for a steer.
The owner's mention authority is checked before the router sees the
prompt, standalone watch and timer wakes are not routed, Stop works
while the route chooses, and the thread history read ends with the
choice. Codex shows a saved route no router offers as its default.

Co-authored-by: Worker Bee (Opus) <4a3bc7fd57a7b2c126e4f3d3462dd16f375242e1c176b4b3f25ca93e3edb6a84@buzz.block.builderlab.xyz>
Signed-off-by: Atish Patel <atishpatel2012@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants