"""Composition root: build the Execution engine from settings and the provider bundle. The only place concrete runtimes, tools, checkpointers and recorders are chosen. """ from __future__ import annotations from collections.abc import AsyncIterator from contextlib import asynccontextmanager from dataclasses import dataclass from pathlib import Path from app.agents.roles import load_roles from app.agents.runtime import AgentRuntime, RoleSpec from app.config import Settings from app.execution.checkpointing import open_checkpointer from app.execution.engine import ExecutionEngine from app.execution.recorder import DatabaseRecorder, ExecutionRecorder from app.execution.toolkit import make_tool_factory from app.providers import ProviderBundle, load_provider from app.tools.local import TerraformRunner, local_tools from app.tools.mcp import McpCatalog, auth_from_config AGENTS_DIR = Path(__file__).resolve().parent.parent / "agents" @dataclass(frozen=True) class Assembly: engine: ExecutionEngine bundle: ProviderBundle roles: dict[str, RoleSpec] mcp_gaps: dict[str, str] @asynccontextmanager async def open_assembly( settings: Settings, *, runtime: AgentRuntime | None = None, recorder: ExecutionRecorder | None = None, output_root: Path | None = None, ) -> AsyncIterator[Assembly]: bundle = load_provider(settings.CLOUD_PROVIDER) roles = load_roles( AGENTS_DIR, bundle, orchestrator_budget=settings.ORCHESTRATOR_BUDGET, specialist_budget=settings.SPECIALIST_BUDGET, include_preview_mcp=settings.MCP_ENABLE_PREVIEW, ) if runtime is None: from app.agents.adk_runtime import AdkAgentRuntime runtime = AdkAgentRuntime( skill_dirs={name: meta.path for name, meta in bundle.skills.items()} ) catalog = McpCatalog() if settings.MCP_ENABLED: used = {server_id for role in roles.values() for server_id in role.mcp_servers} catalog = await McpCatalog.load( [bundle.mcp_servers[server_id] for server_id in sorted(used)], auth_from_config(bundle.mcp_auth, settings.MCP_QUOTA_PROJECT), timeout=settings.MCP_TIMEOUT_SECONDS, ) runner = TerraformRunner( binary=settings.TERRAFORM_BINARY, timeout_seconds=settings.TERRAFORM_TIMEOUT_SECONDS, plugin_cache_dir=settings.TF_PLUGIN_CACHE_DIR, ) offered = [name for names in bundle.tools.values() for name in names] tool_factory = make_tool_factory( runtime, roles, bundle.pillar_skills, local_tools=local_tools(offered, runner), catalog=catalog, ) async with open_checkpointer(settings) as checkpointer: engine = ExecutionEngine( runtime=runtime, roles=roles, recorder=recorder or DatabaseRecorder(), checkpointer=checkpointer, output_root=output_root or settings.BASE_DIR, max_revisions=settings.MAX_REVISIONS, max_orchestrator_turns=settings.MAX_ORCHESTRATOR_TURNS, tool_factory=tool_factory, ) yield Assembly(engine, bundle, roles, dict(catalog.gaps))