"""Published Terraform modules are opt-in per Execution (iteration 8). Infrastructure code is plain resources by default. An Execution that opts in is given the modules Skill and the module lookup, and only then is a module call from an approved source accepted. """ import json import pytest from langgraph.checkpoint.memory import InMemorySaver from app.adk.fake_runtime import FakeAgentRuntime, sample_submission from app.adk.roles import load_roles from app.adk.runtime import NoDecider, RoleSpec from app.agent import AGENTS_DIR, ExecutionEngine from app.nodes.router import Router, Thresholds from app.providers import load_provider from app.recorder import InMemoryRecorder from app.tools.local import TerraformRunner from app.tools.modules import MODULE_TOOL, MODULES_OPTED_IN, NOT_OPTED_IN from app.workflows.deliverables import DEPENDENCY_GRAPH from app.workflows.deliverables import DeliverableKind as K AUTHOR = DEPENDENCY_GRAPH[K.DEPLOYMENT_GUIDANCE].specialist CASE = "acme" MODULE_CALL = """ module "network" { source = "approved-modules/network/cloud" version = "1.2.3" } """ ROLES = { "orchestrator": RoleSpec("orchestrator", "Unused.", budget=40), **{ spec.specialist: RoleSpec( spec.specialist, "Produce.", budget=15, skills=("base",), tools=(MODULE_TOOL,) if spec.specialist == AUTHOR else (), modules_skill="modules-skill" if spec.specialist == AUTHOR else None, ) for spec in DEPENDENCY_GRAPH.values() }, } class Recording(FakeAgentRuntime): def __init__(self, turns): super().__init__(turns) self.seen: dict[str, dict] = {} async def run(self, role, task, tools): self.seen[role.name] = { "skills": role.skills, "role_tools": role.tools, "context": task.context.get("terraform_modules"), "opted_in": MODULES_OPTED_IN.get(), } return await super().run(role, task, tools) def engine(tmp_path, **options) -> tuple[ExecutionEngine, Recording]: runtime = Recording( { spec.specialist: [[sample_submission(kind)]] * 2 for kind, spec in DEPENDENCY_GRAPH.items() } ) built = ExecutionEngine( runtime=runtime, roles=ROLES, recorder=InMemoryRecorder(), checkpointer=InMemorySaver(), output_root=tmp_path, orchestrator=Router(NoDecider(), None, Thresholds()), **options, ) return built, runtime def manifest(tmp_path, execution_id: str) -> dict: folder = tmp_path / "deliverables" / "cases" / CASE / "executions" / execution_id return json.loads((folder / "execution.json").read_text()) async def test_by_default_code_is_plain_resources(tmp_path): built, runtime = engine(tmp_path, modules_available=True) await built.start("Ingest", customer_case=CASE, pre_approved=True, execution_id="a") author = runtime.seen[AUTHOR] assert author["skills"] == ("base",) assert MODULE_TOOL not in author["role_tools"] assert author["context"] is False and author["opted_in"] is False assert manifest(tmp_path, "a")["terraform_modules"] is False async def test_an_execution_that_opts_in_is_given_the_skill_and_the_lookup(tmp_path): built, runtime = engine(tmp_path, modules_available=True) await built.start( "Ingest", customer_case=CASE, pre_approved=True, execution_id="a", terraform_modules=True, ) author = runtime.seen[AUTHOR] assert author["skills"] == ("base", "modules-skill") assert MODULE_TOOL in author["role_tools"] assert author["context"] is True and author["opted_in"] is True # A role that writes no code is given neither. assert runtime.seen["requirements-analyst"]["skills"] == ("base",) assert manifest(tmp_path, "a")["terraform_modules"] is True async def test_the_deployment_may_make_modules_the_default(tmp_path): built, runtime = engine(tmp_path, modules_available=True, default_modules=True) await built.start("Ingest", customer_case=CASE, pre_approved=True, execution_id="a") assert runtime.seen[AUTHOR]["opted_in"] is True await built.start( "Ingest", customer_case=CASE, pre_approved=True, execution_id="b", terraform_modules=False, ) assert runtime.seen[AUTHOR]["opted_in"] is False async def test_opting_in_is_refused_when_the_provider_approves_no_sources(tmp_path): built, runtime = engine(tmp_path) with pytest.raises(ValueError, match="no approved module sources"): await built.start("Ingest", pre_approved=True, terraform_modules=True) with pytest.raises(ValueError, match="true or false"): await built.start("Ingest", pre_approved=True, terraform_modules="yes") assert runtime.seen == {} async def test_a_change_request_does_as_its_baseline_did(tmp_path): built, runtime = engine(tmp_path, modules_available=True) await built.start( "Ingest", customer_case=CASE, pre_approved=True, execution_id="v1", terraform_modules=True, ) runtime.seen.clear() # sample submissions change nothing, so the Change request ends with warnings; # what matters is which way it started. await built.start( "Retention changes.", customer_case=CASE, pre_approved=True, execution_id="v2", baseline="v1", ) assert built is not None state = (await built.interaction("v2")).state assert state["terraform_modules"] is True async def test_a_module_call_is_refused_unless_the_execution_opted_in(): runner = TerraformRunner( binary="no-such-binary", module_namespaces=("approved-modules",) ) files = {"terraform/main.tf": MODULE_CALL} token = MODULES_OPTED_IN.set(False) try: assert (await runner.validate(files)).message == NOT_OPTED_IN # Plain resources are untouched by the switch. plain = await runner.validate({"terraform/main.tf": "locals {}"}) assert not plain.message.startswith("Refused") finally: MODULES_OPTED_IN.reset(token) token = MODULES_OPTED_IN.set(True) try: opted = await runner.validate(files) assert not opted.message.startswith("Refused") finally: MODULES_OPTED_IN.reset(token) def test_the_gcp_bundle_names_its_modules_skill_for_the_roles_that_run_terraform(): bundle = load_provider("gcp") roles = load_roles(AGENTS_DIR, bundle) assert bundle.terraform_module_namespaces == ("terraform-google-modules",) with_skill = {name for name, role in roles.items() if role.modules_skill} assert with_skill == {"iac-author", "validator"} # The Skill is not among any role's Skills until an Execution opts in. assert all( bundle.terraform_modules_skill not in role.skills for role in roles.values() ) def test_the_bundle_lists_modules_to_watch_from_its_approved_namespaces(): bundle = load_provider("gcp") assert bundle.terraform_modules_watch for module in bundle.terraform_modules_watch: namespace, _, provider = module.split("/") assert namespace in bundle.terraform_module_namespaces and provider == "google"