Repository navigation
feat(agents): let bundled plugins offer model routes - #4132
atishpatel wants to merge 4 commits into
Conversation
9aa3dc6 to
6743cca
Compare
wesbillman
left a comment
There was a problem hiding this comment.
🤖 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.
| const pick = await pickFor(job.trigger, agent, type); | ||
| const owned = await pickFor(job.trigger, agent, type); | ||
| const result: RoutedPick = | ||
| (await this.host.route?.( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖 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").
| automatic: onAutomatic(agent.config, type.id), | ||
| models: offered.models, | ||
| busy: state === "active" || state === "compacting", | ||
| last: key ? this.routedPicks.get(key) : undefined, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖 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.
| const { routed, ...pick } = result; | ||
| // The turn's activity says what its route chose, and why. | ||
| if (routed && !routed.kept) | ||
| job.work?.notice( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖 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}`; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖 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.
| </div> | ||
| )} | ||
| <ModelPicker | ||
| harness="codex" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖 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 }; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖 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".
| agent: { pubkey: agent.pubkey, name: agent.name }, | ||
| prompt: promptOf(trigger), | ||
| history: () => | ||
| (history ??= this.routeHistory(agent, trigger, place, signal)), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🤖 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".
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>
32095a5 to
12cd9a1
Compare
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? }:routesare listed beside Default in the Model menu.initialis the route new Custom agents start on.automaticis the route agents on AI setup Automatic run on.exclusivehides the harness's own models. The capability exists but nothing uses it yet.subscribere-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:history()of the thread's earlier messagesThe 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:
fallback. On Automatic agents it uses the recommended model instead.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
runner→Agents.routedPick→routeTurn). Claude Code and Codex reuse their current per-turn model and effort path, so neither runtime knows about routing.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.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.tscand Biome are clean.Docs:
docs/plugin-architecture.md(the API) anddocs/buzz-help.md(user-facing behavior).