Agent Zero AI framework
fix(delegation): preserve scoped subagent model presets
Alessandrofc1582ca1e73
diff --git a/tests/test_subagent_profiles.py b/tests/test_subagent_profiles.pyindex 8e9d17a54..fb02dae10 100644--- a/tests/test_subagent_profiles.py+++ b/tests/test_subagent_profiles.py@@ -1,6 +1,8 @@from __future__ import annotationsfrom datetime import datetime, timezone+import json+from pathlib import Pathfrom types import SimpleNamespaceimport pytest@@ -207,6 +209,100 @@ async def test_call_subordinate_reset_false_reuses_numbered_child(monkeypatch) -assert second.additional == first.additional+ "project_name,project_preset,profile_project",+ [+ ("", None, ""),+ ("demo", None, ""),+ ("demo", "Default", ""),+ ("demo", "Project", ""),+ ("demo", "Project", "demo"),+ ],+ ids=["no-project", "project-inherits", "project-default", "project-preset", "project-profile"],+)[email protected]("preset", [None, "Default", "Cheap", "Missing"])+ "override",+ [None, {"preset_name": "Expensive"}, {"provider": "test", "name": "Expensive"}],+)+def test_subordinate_preserves_scoped_model_preset(+ monkeypatch, tmp_path, project_name, project_preset, profile_project, preset, override+) -> None:+ import tools.call_subordinate as call_subordinate+ from helpers import cache, files, plugins+ from plugins._model_config.helpers import model_config++ monkeypatch.setattr(files, "_base_dir", str(tmp_path))+ monkeypatch.setattr(cache, "_cache", {})+ monkeypatch.setattr(AgentContext, "_contexts", {})+ monkeypatch.setattr(persist_chat, "save_tmp_chat", lambda _context: None)+ monkeypatch.setattr(+ call_subordinate,+ "initialize_agent",+ lambda override_settings=None: AgentConfig(+ mcp_servers="", profile=override_settings["agent_profile"]+ ),+ )++ plugin_source = Path(__file__).resolve().parents[1] / "plugins" / "_model_config"+ for filename in ("plugin.yaml", "hooks.py", "default_config.yaml"):+ files.write_file(+ files.get_abs_path("plugins", "_model_config", filename),+ (plugin_source / filename).read_text(encoding="utf-8"),+ )+ files.write_file(+ files.get_abs_path("usr/plugins/_model_config/presets.yaml"),+ json.dumps([+ {+ "name": name,+ **{+ slot: {"provider": "test", "name": name}+ for slot in ("chat", "utility", "embedding")+ },+ }+ for name in ("Default", "Cheap", "Project", "Expensive")+ ]),+ )+ for profile in ("agent0", "developer"):+ files.write_file(files.get_abs_path("agents", profile, "agent.yaml"), "{}")+ if project_name:+ projects.create_project(project_name, {"title": "Demo"})+ for project, profile, selection in (+ (project_name, "", project_preset),+ (profile_project, "developer", preset),+ ):+ if selection is not None:+ files.write_file(+ plugins.determine_plugin_asset_path("_model_config", project, profile, "config.json"),+ json.dumps({"model_preset": selection}),+ )++ parent = Agent(0, AgentConfig(mcp_servers="", profile="agent0"))+ if project_name:+ projects.activate_project(parent.context.id, project_name, mark_dirty=False)+ parent.context.set_data("chat_model_override", override)+ child = call_subordinate.get_or_create_subordinate(+ parent, profile="developer", reset=True+ )++ configured = preset if profile_project and preset else project_preset or preset+ configured = configured if configured in {"Cheap", "Project"} else "Default"+ expected = "Expensive" if configured == "Default" and override else configured+ assert child.config.profile == "developer"+ assert projects.get_context_project_name(child.context) == (project_name or None)+ assert model_config.get_configured_preset_name(child) == configured+ assert model_config.get_chat_model_config(child)["name"] == expected+ assert child.context.get_data("chat_model_override") == (+ override if configured == "Default" else None+ )+ assert parent.context.get_data("chat_model_override") == override++ child.context.set_data("chat_model_override", {"preset_name": "Expensive"})+ resumed = call_subordinate.get_or_create_subordinate(parent, context_id=child.context.id)+ assert resumed is child+ assert model_config.get_chat_model_config(resumed)["name"] == "Expensive"++def test_subordinate_tree_numbers_each_generation(monkeypatch) -> None:import tools.call_subordinate as call_subordinatediff --git a/tools/call_subordinate.py b/tools/call_subordinate.pyindex be40b4d03..cfe2aac4b 100644--- a/tools/call_subordinate.py+++ b/tools/call_subordinate.py@@ -4,6 +4,7 @@ from helpers.errors import RepairableExceptionfrom helpers.tool import Tool, Responsefrom initialize import initialize_agentfrom extensions.python.hist_add_tool_result import _90_save_tool_call_file as save_tool_call_file+from plugins._model_config.helpers.model_config import DEFAULT_PRESET_NAME, get_configured_preset_nameSUBORDINATES_DATA_KEY = "_subordinates"@@ -172,7 +173,7 @@ def get_or_create_subordinate(if project:projects.activate_project(context.id, project, mark_dirty=False)model_override = parent.context.get_data("chat_model_override")- if model_override:+ if model_override and get_configured_preset_name(subordinate) == DEFAULT_PRESET_NAME:context.set_data("chat_model_override", model_override)_register_subordinate(parent, subordinate, slot)diff --git a/tools/call_subordinate.py.dox.md b/tools/call_subordinate.py.dox.mdindex 9eef419d0..c6c2dc827 100644--- a/tools/call_subordinate.py.dox.md+++ b/tools/call_subordinate.py.dox.md@@ -35,7 +35,8 @@- Child context IDs are accepted only when their persisted parent context, parent agent number, and child depth match the caller.- Supplying a different profile for an existing child without creating a fresh child raises `RepairableException` instead of silently changing its profile.- Active parallel children cannot be continued concurrently; await or cancel their job first.-- Child contexts inherit the caller's project and selected chat-model override, are saved before execution and again on exit, and remain reusable after model/API failures.+- Child contexts inherit the caller's project. After project activation, copy the caller's chat-model override only when the child's resolved scoped preset is `Default` (including missing/invalid selections); non-default scoped presets keep their configured models. Continuing an existing child preserves its chat override.+- Child contexts are saved before execution and again on exit, and remain reusable after model/API failures.- The direct tool result includes `context_id`; parallel job snapshots expose the same stable child ID separately from their per-invocation job ID.- Legacy args `agent_id` for `context_id` and `agent_profile` for `profile` still resolve; prompts document only canonical names.- Existing same-context linear subordinates remain reusable for saved-chat compatibility, but new children use child contexts and a private per-parent registry.@@ -55,6 +56,7 @@## Verification- Run targeted tool and prompt-contract tests for changed behavior; smoke-test agent execution when no focused test exists.+- `test_subordinate_preserves_scoped_model_preset` exercises real scoped config files and project activation with absent, default, custom, and invalid presets plus named/raw chat overrides; continued children retain their own override.- Related tests observed by source search:- `tests/test_default_prompt_budget.py`- `tests/test_subagent_profiles.py`