"""A live run's dependency graph (ADR 0019) and its applications and asset owners (ADR 0021) are built in code from the scan and the CMDB; the sample estate keeps the agent loop and the alz-dependency tools.""" import json from pathlib import Path from typing import Any, Optional import jsonschema from kyndryl_agent_runtime import ask_sub_agent from kyndryl_agent_runtime.request_cycle import Reply from app import applications, graph OUTPUT_SCHEMA = json.loads((Path(__file__).resolve().parents[1] / "schemas" / "output.json").read_text()) class GraphInvalid(RuntimeError): """The graph broke its output schema: a Reply can't be repaired by the output contract, so the step fails.""" async def build(payload: dict, ask) -> dict: result = graph.build(payload) result.update(await applications.group(payload, ask)) errors = sorted(jsonschema.Draft202012Validator(OUTPUT_SCHEMA).iter_errors(result), key=lambda e: list(e.path)) if errors: raise GraphInvalid(f"the dependency graph broke its output schema: {errors[0].message}") return result async def pre_process(payload: dict) -> Optional[Any]: if payload.get("provider", "sample") not in ("azure", "gcp"): return None # the Meridian sample estate: prompt.md and the alz-dependency tools, unchanged async def ask(prompt: str) -> str: return await ask_sub_agent("propose_application_groups", prompt) return Reply(json.dumps(await build(payload, ask), ensure_ascii=False))