Agent Zero AI framework
Clear stale preset kwargs on model switches
Alessandrod6e8f06ec002
diff --git a/plugins/_model_config/AGENTS.md b/plugins/_model_config/AGENTS.mdindex be2979411..faacef90f 100644--- a/plugins/_model_config/AGENTS.md+++ b/plugins/_model_config/AGENTS.md@@ -16,6 +16,7 @@- Preserve global, project, agent, and chat override resolution order.- Keep provider metadata and API-key checks safe around secrets.- Coordinate OAuth-backed providers with `_oauth` instead of hardcoding provider-specific auth here.+- Applying a model preset may inherit durable tuning such as context windows and rate limits, but must replace or clear per-slot `kwargs` so provider-specific extra params never leak across model providers.## Work Guidancediff --git a/plugins/_model_config/helpers/model_config.py b/plugins/_model_config/helpers/model_config.pyindex e01430304..045e915c8 100644--- a/plugins/_model_config/helpers/model_config.py+++ b/plugins/_model_config/helpers/model_config.py@@ -16,6 +16,7 @@ PRESET_SLOT_CONFIG_SECTIONS = {"utility": "utility_model","embedding": "embedding_model",}+MODEL_SLOT_PRESET_REPLACE_FIELDS = {"kwargs"}IMPLICIT_PRESET_SLOT_DEFAULTS = {"utility": {"ctx_length": 128000,@@ -283,6 +284,17 @@ def _deep_merge_dict(base: dict, override: dict) -> dict:return result+def _replace_preset_model_slot_fields(base: dict, override: dict, result: dict) -> dict:+ """Clear or replace provider-specific fields that must not leak across presets."""+ for key in MODEL_SLOT_PRESET_REPLACE_FIELDS:+ if key in override:+ value = override.get(key)+ result[key] = deepcopy(value) if isinstance(value, dict) else {}+ elif key in base:+ result[key] = {}+ return result++def _slot_has_identity(slot_config: dict) -> bool:return bool(slot_config.get("provider") or slot_config.get("name"))@@ -342,7 +354,8 @@ def _merge_model_slot()if not strip_api_key and not str(cleaned.get("api_key") or "").strip():cleaned.pop("api_key", None)- return _deep_merge_dict(base_slot if isinstance(base_slot, dict) else {}, cleaned)+ base = base_slot if isinstance(base_slot, dict) else {}+ return _replace_preset_model_slot_fields(base, cleaned, _deep_merge_dict(base, cleaned))def build_config_from_preset(@@ -356,8 +369,8 @@ def build_config_from_preset(Presets are intentionally partial: omitted fields inherit from the currentconfig, so selecting a preset does not reset tuned values such as context- windows, rate limits, or nested kwargs unless the preset explicitly defines- them.+ windows or rate limits. Provider-specific kwargs are replaced when present+ and cleared when omitted so stale params do not leak between providers."""config = (normalize_config_for_save(base_config)diff --git a/plugins/_model_config/webui/model-config-store.js b/plugins/_model_config/webui/model-config-store.jsindex 0468c7569..d23f200a0 100644--- a/plugins/_model_config/webui/model-config-store.js+++ b/plugins/_model_config/webui/model-config-store.js@@ -73,6 +73,7 @@ const IMPLICIT_PRESET_SLOT_DEFAULTS = {kwargs: {},},};+const PRESET_REPLACE_FIELDS = new Set(['kwargs']);function presetDefaultValuesEqual(value, defaultValue) {if (typeof defaultValue === 'number') return Number(value) === defaultValue;@@ -118,6 +119,14 @@ export function mergeModelSlot(baseSlot, presetSlot, stripApiKey = true, slotKeyresult[key] = clonePlain(value);}}+ for (const key of PRESET_REPLACE_FIELDS) {+ if (Object.prototype.hasOwnProperty.call(clean, key)) {+ const value = clean[key];+ result[key] = value && typeof value === 'object' && !Array.isArray(value) ? clonePlain(value) : {};+ } else if (Object.prototype.hasOwnProperty.call(baseSlot || {}, key)) {+ result[key] = {};+ }+ }return result;}diff --git a/tests/test_model_config_project_presets.py b/tests/test_model_config_project_presets.pyindex 18ce5128c..571ae5563 100644--- a/tests/test_model_config_project_presets.py+++ b/tests/test_model_config_project_presets.py@@ -247,7 +247,7 @@ def test_project_save_copies_selected_preset_to_scoped_model_config(monkeypatch,assert "_model_config" not in project_json-def test_preset_application_deep_merges_model_slots(monkeypatch, tmp_path):+def test_preset_application_preserves_tuning_but_replaces_kwargs(monkeypatch, tmp_path):_prepare_a0_tree(monkeypatch, tmp_path)from plugins._model_config.helpers import model_config@@ -296,19 +296,57 @@ def test_preset_application_deep_merges_model_slots(monkeypatch, tmp_path):assert config["chat_model"]["name"] == "claude-research"assert config["chat_model"]["ctx_length"] == 200000- assert config["chat_model"]["kwargs"] == {- "temperature": 0.2,- "routing": {"order": ["a", "b"], "priority": "quality"},- }+ assert config["chat_model"]["kwargs"] == {"routing": {"priority": "quality"}}assert config["utility_model"]["name"] == "utility-research"assert config["utility_model"]["ctx_length"] == 200000assert config["utility_model"]["ctx_input"] == 0.4- assert config["utility_model"]["kwargs"] == {- "temperature": 0.1,- "routing": {"order": ["fast"], "timeout": 30},- }+ assert config["utility_model"]["kwargs"] == {"routing": {"timeout": 30}}assert config["embedding_model"]["name"] == "text-embedding-3-large"- assert config["embedding_model"]["kwargs"] == {"device": "cpu", "batch_size": 16}+ assert config["embedding_model"]["kwargs"] == {}+++def test_preset_application_clears_stale_kwargs_when_preset_omits_them(+ monkeypatch,+ tmp_path,+):+ _prepare_a0_tree(monkeypatch, tmp_path)++ from plugins._model_config.helpers import model_config++ base_config = {+ "chat_model": {+ "provider": "openrouter",+ "name": "openai/gpt-5.4",+ "ctx_length": 200000,+ "kwargs": {"temperature": 0, "extra_headers": {"x-old": "true"}},+ },+ "utility_model": {+ "provider": "openrouter",+ "name": "openai/gpt-5.4-mini",+ "ctx_length": 128000,+ "kwargs": {"temperature": 0},+ },+ }+ preset = {+ "name": "Codex",+ "chat": {+ "provider": "codex_oauth",+ "name": "gpt-5.1-codex",+ },+ "utility": {+ "provider": "codex_oauth",+ "name": "gpt-5.1-codex-mini",+ },+ }++ config = model_config.build_config_from_preset(preset, base_config)++ assert config["chat_model"]["name"] == "gpt-5.1-codex"+ assert config["chat_model"]["ctx_length"] == 200000+ assert config["chat_model"]["kwargs"] == {}+ assert config["utility_model"]["name"] == "gpt-5.1-codex-mini"+ assert config["utility_model"]["ctx_length"] == 128000+ assert config["utility_model"]["kwargs"] == {}def test_preset_application_inherits_optional_slots(monkeypatch, tmp_path):@@ -341,7 +379,10 @@ def test_preset_application_inherits_optional_slots(monkeypatch, tmp_path):assert config["embedding_model"] == base_config["embedding_model"]-def test_legacy_utility_preset_defaults_do_not_override_tuned_config(monkeypatch, tmp_path):+def test_legacy_utility_preset_defaults_preserve_tuning_but_clear_kwargs(+ monkeypatch,+ tmp_path,+):_prepare_a0_tree(monkeypatch, tmp_path)from plugins._model_config.helpers import model_config@@ -390,7 +431,7 @@ def test_legacy_utility_preset_defaults_do_not_override_tuned_config(monkeypatchassert utility["rl_requests"] == 12assert utility["rl_input"] == 34000assert utility["rl_output"] == 56000- assert utility["kwargs"] == {"temperature": 0.1}+ assert utility["kwargs"] == {}def test_preset_override_preserves_configured_utility_context(monkeypatch, tmp_path):