Agent Zero AI framework
Cache parsed global model presets
Alessandro1effda46824d
diff --git a/plugins/_model_config/AGENTS.md b/plugins/_model_config/AGENTS.mdindex b535fd3ea..8cef7ba97 100644--- a/plugins/_model_config/AGENTS.md+++ b/plugins/_model_config/AGENTS.md@@ -18,6 +18,7 @@- `Default` is the first global preset and cannot be deleted or renamed. It owns the complete main, utility, and embedding baseline; its Vision Model slot is optional.- Preset definitions are global. Global, project, agent-profile, and project/profile plugin configs persist only `model_preset`; chats may persist a preset reference as their explicit override.- Preserve scoped plugin resolution order and fall back invalid or missing scope/chat references to `Default`.+- `get_presets()` caches the parsed global collection in `model_presets(plugins)`, keyed by the stat signature of the user, fallback, and legacy config files, and returns deep copies. Missing files allow caching; read or parse failures return uncached fallback results so recovery needs no file edit. `save_presets` and `reset_presets` clear it; other writers must change those files or clear the area.- Project Settings `llm` payloads are owned here through the generic `helpers.projects` project extension-data hooks; keep project helper code agnostic to `_model_config` paths, presets, and inheritance rules.- Keep provider metadata and API-key checks safe around secrets.- Check API-key readiness only for the effective model configuration; unused global presets must not produce Welcome-screen warnings.diff --git a/plugins/_model_config/helpers/model_config.py b/plugins/_model_config/helpers/model_config.pyindex 0aa73a37f..bf071ce5a 100644--- a/plugins/_model_config/helpers/model_config.py+++ b/plugins/_model_config/helpers/model_config.py@@ -2,7 +2,7 @@ import osfrom copy import deepcopyimport models-from helpers import defer, plugins, files+from helpers import cache, defer, plugins, filesfrom helpers.extension import call_extensions_asyncfrom helpers import yaml as yaml_helperfrom helpers.providers import get_provider_config, get_providers@@ -10,6 +10,7 @@ from helpers.providers import get_provider_config, get_providersPRESETS_FILE = "presets.yaml"FALLBACK_PRESETS_FILE = "mode_presets_fallback.yaml"PROVIDER_METADATA_FILE = "provider_metadata.yaml"+PRESETS_CACHE_AREA = "model_presets(plugins)"DEFAULT_PRESET_NAME = "Default"DEFAULT_VISION_TIMEOUT_SECONDS = 300DEFAULT_VISION_MAX_TOKENS = 2000@@ -228,11 +229,13 @@ def save_project_llm_settings(project_name: str, llm_data: object) -> None:)-def _load_presets_from_path(path: str) -> list | None:+def _load_presets_from_path(path: str, *, raise_on_error: bool = False) -> list | None:if files.exists(path):try:data = yaml_helper.loads(files.read_file(path))except Exception:+ if raise_on_error:+ raisereturn Noneif isinstance(data, list):return data@@ -365,7 +368,7 @@ def normalize_config_for_save(config: dict) -> dict:return cleaned-def _legacy_default_preset() -> dict | None:+def _legacy_default_preset(*, raise_on_error: bool = False) -> dict | None:"""Build Default from a pre-v2 global config when startup migration has not run."""path = plugins.determine_plugin_asset_path("_model_config", "", "", plugins.CONFIG_FILE_NAME@@ -375,6 +378,8 @@ def _legacy_default_preset() -> dict | None:try:raw = files.read_file_json(path)except Exception:+ if raise_on_error:+ raisereturn Noneif not isinstance(raw, dict) or not any(section in raw for section in PRESET_SLOT_CONFIG_SECTIONS.values()@@ -388,23 +393,25 @@ def parse_preset_collection(text: str) -> list:return validate_presets(yaml_helper.loads(text))-def _fallback_presets() -> list:+def _fallback_presets(*, raise_on_error: bool = False) -> list:path = _get_fallback_presets_path()if not files.exists(path):return []try:return parse_preset_collection(files.read_file(path))except Exception:+ if raise_on_error:+ raisereturn []-def _ensure_default_preset(presets: list) -> list:+def _ensure_default_preset(presets: list, *, raise_on_error: bool = False) -> list:result = [deepcopy(preset) for preset in presets if isinstance(preset, dict)]- legacy_default = _legacy_default_preset()+ legacy_default = _legacy_default_preset(raise_on_error=raise_on_error)bundled_default = next((deepcopy(preset)- for preset in _fallback_presets()+ for preset in _fallback_presets(raise_on_error=raise_on_error)if isinstance(preset, dict)and str(preset.get("name") or "").strip().casefold()== DEFAULT_PRESET_NAME.casefold()@@ -446,13 +453,46 @@ def get_presets(project_name: str | None = None) -> list:if project_name:return get_project_presets(project_name)- path = _get_presets_path()- presets = _load_presets_from_path(path)+ # config resolution reads presets many times per model call, parse once per file version+ signature = _presets_signature()+ presets = cache.get(PRESETS_CACHE_AREA, signature)+ if presets is None:+ try:+ presets = _load_global_presets(raise_on_error=True)+ except Exception:+ # Preserve fallback behavior without caching a failed read.+ return _load_global_presets()+ cache.add(PRESETS_CACHE_AREA, signature, presets)+ return deepcopy(presets)+++def _load_global_presets(*, raise_on_error: bool = False) -> list:+ presets = _load_presets_from_path(_get_presets_path(), raise_on_error=raise_on_error)if presets is not None:- return _ensure_default_preset(presets)+ return _ensure_default_preset(presets, raise_on_error=raise_on_error)# Fall back to the repository-shipped offline collection.- return _ensure_default_preset(_fallback_presets())+ return _ensure_default_preset(+ _fallback_presets(raise_on_error=raise_on_error), raise_on_error=raise_on_error+ )+++def _presets_signature() -> tuple:+ paths = (+ _get_presets_path(),+ _get_fallback_presets_path(),+ plugins.determine_plugin_asset_path(+ "_model_config", "", "", plugins.CONFIG_FILE_NAME+ ),+ )+ signature = []+ for path in paths:+ try:+ stat = os.stat(path)+ signature.append((path, stat.st_mtime_ns, stat.st_size))+ except OSError:+ signature.append((path, None, None))+ return tuple(signature)def get_project_presets(project_name: str) -> list:@@ -484,6 +524,7 @@ def save_presets(presets: list, project_name: str | None = None) -> None:cleaned = validate_presets(presets)path = _get_presets_path(project_name)files.write_file(path, yaml_helper.dumps(cleaned))+ cache.clear(PRESETS_CACHE_AREA)def update_preset_from_config(name: str, config: dict) -> dict:@@ -519,6 +560,7 @@ def reset_presets(project_name: str | None = None) -> list:path = _get_presets_path(project_name)if os.path.exists(path):os.remove(path)+ cache.clear(PRESETS_CACHE_AREA)return get_presets()diff --git a/tests/test_model_config_project_presets.py b/tests/test_model_config_project_presets.pyindex bde58525c..d78579b2d 100644--- a/tests/test_model_config_project_presets.py+++ b/tests/test_model_config_project_presets.py@@ -1,6 +1,7 @@from __future__ import annotationsimport json+import osimport sysimport threadingimport types@@ -277,6 +278,82 @@ def test_global_presets_require_immutable_default_and_save_behavior(monkeypatch,assert model_config.reset_presets()[0]["name"] == "Default"+def test_global_presets_parse_once_until_a_source_file_changes(monkeypatch, tmp_path):+ _prepare_a0_tree(monkeypatch, tmp_path)++ from plugins._model_config.helpers import model_config++ calls = []+ load = model_config._load_global_presets+ monkeypatch.setattr(+ model_config, "_load_global_presets", lambda **kwargs: calls.append(1) or load(**kwargs)+ )++ model_config.get_presets()[0]["name"] = "mutated"+ assert model_config.get_presets()[0]["name"] == "Default"+ assert len(calls) == 1++ model_config.save_presets(+ [+ model_config.get_presets()[0],+ {"name": "Added", "chat": {"provider": "openai", "name": "gpt-test"}},+ ]+ )+ assert [p["name"] for p in model_config.get_presets()] == ["Default", "Added"]+ assert len(calls) == 2++ # out-of-band edits invalidate through the file signature+ path = tmp_path / "usr" / "plugins" / "_model_config" / "presets.yaml"+ path.write_text(path.read_text(encoding="utf-8").replace("Added", "Edited"), encoding="utf-8")+ os.utime(path, ns=(1, 1))+ assert [p["name"] for p in model_config.get_presets()] == ["Default", "Edited"]+ assert len(calls) == 3++[email protected]("source", ["presets", "fallback", "legacy"])+def test_global_presets_recover_after_source_read_failure(monkeypatch, tmp_path, source):+ _prepare_a0_tree(monkeypatch, tmp_path)++ from helpers import cache, files+ from plugins._model_config.helpers import model_config++ user_dir = tmp_path / "usr" / "plugins" / "_model_config"+ user_dir.mkdir()+ paths = {+ "presets": user_dir / "presets.yaml",+ "fallback": tmp_path / "plugins" / "_model_config" / "mode_presets_fallback.yaml",+ "legacy": user_dir / "config.json",+ }+ paths["presets"].write_text(+ "- name: Default\n- name: Custom\n chat: {provider: openai, name: custom-chat}\n",+ encoding="utf-8",+ )+ paths["legacy"].write_text(+ json.dumps({"utility_model": {"provider": "openai", "name": "legacy-utility"}}),+ encoding="utf-8",+ )+ expected = model_config.get_presets()+ assert expected[0]["chat"]["name"] == "default-chat"+ assert expected[0]["utility"]["name"] == "legacy-utility"+ assert expected[1]["name"] == "Custom"+ cache.clear(model_config.PRESETS_CACHE_AREA)++ reader_name = "read_file_json" if source == "legacy" else "read_file"+ read = getattr(files, reader_name)++ def fail_read(path, *args, **kwargs):+ if Path(path) == paths[source]:+ raise PermissionError("temporary read failure")+ return read(path, *args, **kwargs)++ with monkeypatch.context() as failing:+ failing.setattr(files, reader_name, fail_read)+ assert model_config.get_presets() != expected++ # Recovery must not require editing the files or clearing the cache.+ assert model_config.get_presets() == expected++def test_project_scope_selects_global_presets_only(monkeypatch, tmp_path):_prepare_a0_tree(monkeypatch, tmp_path)