Let capabilities set the model and settings in `clai2` by passing only the user's own choices to `agent.run`

Merged
DO
2mo ago
+785-49

clai2 always passed model= to agent.run(), even when the user never chose a model. It also passed its GPT-6/GPT-5.6 family defaults, merged with the saved /model_settings overrides, as run-level model_settings. Run-level values beat agent- and capability-level ones, so anything configured below run level was silently overridden. That includes Logfire AgentControl, plugin capabilities, and capabilities on an --agent. Now only what the user explicitly chose goes to run(), and CLAI's own defaults sit below it.

New public surface: none (internal Session attributes only).

What moved where

  • Default model: a model counts as chosen when it is in Settings.model_fields_set: from -m, CLAI_MODEL, the project file, a saved /set model or /model, or a fork's @model. A chosen model is still passed to run(). An unchosen default (openai-codex:gpt-6-astra) becomes the stock agent's own model, so a capability's get_model() takes precedence. Names still resolve through CLAI's existing resolver first (Codex auth, OpenRouter, vLLM, Copilot, plugin providers), now via an outermost run-level ResolveModelId. Names it returns unchanged go on to agent and plugin resolvers and then to infer_model, as before.
  • Family defaults: now an outermost run capability that resolves per request from the request's model name (ctx.model_id). It is merged before every other capability, and the defaults follow a model that a capability switches to.
  • Run-level settings: only the saved overrides, and only for requests on the model they were saved for. Codex-only settings are not sent to a model a capability selected. Like main's mid-turn /model settings (#9831), both the overrides and the defaults' view of what was saved are read again before each request.
  • I didn't touch the /model, /add_model or /model_settings command and menu code. The only change near it: CommandContext.validate/reset_setting now dump with exclude_unset, so /set and reset preserve which fields were actually chosen.

User-visible behavior

 pydantic_clai2/runtime/_session.py :: Session.prompt()
+├─ Session._bind()  # unchosen default → stock agent's own model; family defaults → outermost capability
-└─ Agent.run(model=<chosen or CLAI default>, model_settings=<family defaults + saved overrides>)
+└─ Agent.run(model=<chosen model only>, model_settings=<saved overrides, on their model only>)
    └─ … unchanged machinery …
-      └─ capability get_model() / get_model_settings()  → overridden by run-level values
+      └─ capability get_model() / get_model_settings()  → take effect over CLAI's defaults

Verification: tests/clai2/test_run_model_layers.py

  • test_effective_settings_unchanged compares what the model receives before and after the change: the old run(model_settings=context.model_settings(name)) against a real shell turn now. It covers OpenAI reasoning (including OpenRouter overrides), Anthropic, Codex as the default and chosen (including fast mode with custom params), and Copilot, on both the stock and a supplied agent. Equal in every case.
  • Further tests cover capability settings beating defaults but not saved overrides, a capability model replacing the default, a chosen model (saved or -m) beating a capability, saved settings staying with their model, model-name resolution order, /set/reset keeping the model unchosen, and forks.

What changes for existing users: nothing, unless a capability selects a model or supplies overlapping settings. Such a capability now takes effect over CLAI's default model and family defaults; the user's chosen model and saved settings still win. Agent-level model_settings stay beneath the family defaults, as before.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

Opened by Claude (Douwe's coding agent).

Part of #9781