Apply `clai2` `/model settings` edits to the running model's next request mid-turn

Merged
MP
2mo ago
+127-17

Why we make these changes

Editing /model settings for the active model while a turn runs (for example, raising max output tokens or switching reasoning effort during a long tool call) only applied to the next prompt. The running turn kept the settings it captured at start. This change applies those edits to the running turn from its next model request.

Approach

I looked at three options:

  • Mutate a shared settings object in place. CLAI2 passes a plain ModelSettings dict to agent.run. Core keeps that reference and merges it before each request, so changing it in place would technically work. It is not safe, though: /model settings saves from a menu worker thread (run_worker → asyncio.to_thread) while core merges that same dict on the event loop. Clearing and refilling it races with that merge and depends on core never copying the dict.
  • Reload or rebuild the agent. A running agent.run has already captured its settings. Rebuilding StockAgent only affects later runs and would not help a supplied --agent.
  • Use core's per-request settings callable (chosen). agent.run(model_settings=...) accepts Callable[[RunContext], ModelSettings], and core resolves it before every model request. This is the supported extension point, so no core or harness change is needed.

CommandContext.live_model_settings(model) validates the saved settings once at turn start, which keeps the existing "Invalid saved model settings" turn failure. It then returns a callable that reads the store again before each request. Every write path (ModelSettingsSource apply and reset, custom params, /fast) is picked up without per-call-site notification. If a later read fails validation (for example, a value another build wrote to the shared DB), the callable keeps the last valid settings instead of failing the running turn. The foreground turn and /fork child sessions both use it. Session.model_settings widens to core's AgentModelSettings so it can hold the callable.

This applies only to per-model settings. /model, run.tool_retries, and run.request_limit still wait for the turn to end, as SessionSettings documents.

New public surface

  • CommandContext.live_model_settings(model).
  • Session.model_settings now also accepts a callable (core's AgentModelSettings), which it passes through unchanged to agent.run.

User-visible behavior

  • Before: during a turn, open /model settings, set Max Output Tokens to 42 for the running model, and close it. The rest of the turn still sent the old settings.
  • After: the turn's next model request (after the current tool call returns) sends max_tokens=42. Resetting a field mid-turn removes it from the next request the same way.
  • README, PLUGINS.md, and docs/harness/clai2.md now describe this.

Verification

  • End-to-end regression test: drives chat through the live prompt, opens a during_turn command while a tool blocks, and saves through ModelSettingsSource.apply on the menu worker thread. It asserts the turn's requests saw [{'seed': 1}, {'seed': 1, 'max_tokens': 42}]. The test fails on main with [{'seed': 1}, {'seed': 1}].
  • Unit test: covers re-reading after a save, keeping the last valid settings after an invalid read, and raising up front for invalid settings.
  • tests/clai2: 2582 passed, 2 skipped. Ruff format and check pass, and Pyright over src/pydantic_clai2 and tests/clai2 reports 0 errors. Both touched test files are 100% covered, and the new live_model_settings branches are fully covered.
  • Manual check in a fresh tmux window: ran clai2 --agent with a streaming FunctionModel that echoes its model_settings and calls a 20 s tool, with an isolated XDG_CONFIG_HOME. While the tool ran I opened /model settings, set Max Output Tokens to 42, and closed the menu. Step 2 of the same turn printed {'max_tokens': 42, 'seed': 1}. Resetting the field mid-turn in a later run dropped it from step 2.

What changes for existing users

Per-model settings edited during a turn now affect the rest of that turn, not just the next prompt. The success message stays "Applies when this model is selected.", which is still accurate for non-active models. Code that reads Session.model_settings after the shell sets it now gets a callable rather than a dict.

Checklist

  • No breaking changes to the stable Pydantic AI API in accordance with the version policy; only pre-1.0 CLAI2 changes.
  • No permitted compatibility impact label needed: the stable API is unchanged.
  • PR title is fit for the release changelog.