"""Assembling an agent from settings, its own folder and a Workflow.""" import dataclasses from typing import Any import pytest from execution_engine.adk.fake_runtime import Call, FakeAgentRuntime from execution_engine.adk.runtime import NoDecider from execution_engine.agent import open_assembly from execution_engine.config import Settings from execution_engine.recorder import InMemoryRecorder from tests.toy.support import once_each, submits from tests.toy.workflow import ROOT, SHOPPING_LIST, TOY def settings(**overrides: Any) -> Settings: values: dict[str, Any] = { "CLOUD_PROVIDER": "toy", "CHECKPOINTER": "memory", "MCP_ENABLED": False, "ORCHESTRATOR_BUDGET": 9, "SPECIALIST_BUDGET": 4, } values.update(overrides) return Settings(_env_file=None, **values) # type: ignore[call-arg] def assembled(tmp_path, runtime, workflow=TOY, **overrides): return open_assembly( settings(**overrides), workflow, root=ROOT, runtime=runtime, recorder=InMemoryRecorder(), output_root=tmp_path, ) async def test_an_agent_is_its_folder_its_settings_and_its_workflow(tmp_path): runtime = FakeAgentRuntime(once_each()) async with assembled(tmp_path, runtime) as assembly: result = await assembly.engine.start("A picnic", pre_approved=True) assert result.status == "completed" assert assembly.engine.workflow is TOY assert assembly.bundle.name == "toy" assert assembly.roles["orchestrator"].budget == 9 assert assembly.roles["shopper"].budget == 4 assert assembly.mcp_gaps == {} # With no Jev key the router follows its playbook on rules alone, and no # Orchestrator model is run. assert isinstance(assembly.decider, NoDecider) assert "orchestrator" not in {run.role for run in runtime.runs} async def test_a_role_is_given_the_workflows_own_tool_on_its_working_copy(tmp_path): turns = once_each() turns["shopper"] = [[Call("tally"), *submits(SHOPPING_LIST)]] runtime = FakeAgentRuntime(turns) async with assembled(tmp_path, runtime) as assembly: await assembly.engine.start("A picnic", pre_approved=True) by_role = {run.role: run for run in runtime.runs} assert "tally" in by_role["shopper"].tools assert by_role["shopper"].results[0] == ("tally", "0 items so far.") # Only the role whose file names it. assert "tally" not in by_role["checker"].tools async def test_the_model_orchestrator_is_still_there_to_be_scripted(tmp_path): runtime = FakeAgentRuntime( {"orchestrator": [[Call("ask_user", {"questions": ["How many guests?"]})]]} ) async with assembled(tmp_path, runtime, ORCHESTRATOR="model") as assembly: result = await assembly.engine.start("A picnic") assert result.prompt["questions"][0]["question"] == "How many guests?" async def test_an_unknown_orchestrator_setting_is_refused(tmp_path): with pytest.raises(ValueError, match="ORCHESTRATOR must be router or model"): async with assembled(tmp_path, FakeAgentRuntime({}), ORCHESTRATOR="llm"): pass async def test_a_workflow_may_be_built_for_the_settings_and_the_bundle(tmp_path): seen = {} def build(given_settings, bundle): seen.update(provider=bundle.name, budget=given_settings.SPECIALIST_BUDGET) return dataclasses.replace(TOY, done="Packed.") async with assembled(tmp_path, FakeAgentRuntime({}), build) as assembly: assert assembly.engine.workflow.done == "Packed." assert seen == {"provider": "toy", "budget": 4} async def test_overlay_skills_come_from_the_agents_own_folder(tmp_path): runtime = FakeAgentRuntime(once_each()) async with assembled(tmp_path, runtime) as assembly: result = await assembly.engine.start( "A picnic", pre_approved=True, overlay_skills=["house-rules"] ) with pytest.raises(ValueError, match="Unknown overlay Skills: no-such-rules"): await assembly.engine.start( "A picnic", pre_approved=True, overlay_skills=["no-such-rules"] ) assert list(result.state["overlay_skills"]) == ["house-rules"] assert all( run.task.context["overlay_skills"] == ["house-rules"] for run in runtime.runs )