"""Delivering one JSON document to a caller that takes one output per step, as a KAB workflow step does, instead of each agent's files one by one.""" from __future__ import annotations import json from typing import Any import httpx from orchestration.main import create_app from tests.orchestration.test_sequence import ( ARCHITECTURE, BOTH, OPERATIONS, SETTINGS, ScriptedAgents, call, finished, struct, ) # What a KAB workflow step sends with every call. PLATFORM = {"workflow_id": "wf-1", "step_number": 1, "task_id": "t-1"} GRAPH = '{"elements": []}' def document(task: dict) -> dict[str, Any]: (artifact,) = task["artifacts"] assert artifact["name"] == "result.json" return json.loads("".join(part["text"] for part in artifact["parts"])) async def test_a_platform_step_is_given_one_document_of_the_agents_files(): agents = ScriptedAgents( {"architecture": [finished("a1", {"graph.json": GRAPH, "guide.md": "# Guide"})]} ) task, _ = await call( agents, [{"text": "A landing zone"}], metadata={**PLATFORM, "work": "architecture", "customer_case": "acme"}, ) assert task["status"]["state"] == "TASK_STATE_COMPLETED" result = document(task) assert result["status"] == "completed" assert result["work"] == "architecture" assert result["customer_case"] == "acme" # Every file as the agent delivered it, ready to be the next step's sources. assert result["files"]["graph.json"] == GRAPH assert result["files"]["guide.md"] == "# Guide" assert json.loads(result["files"]["execution.json"]) == { "status": "completed", "customer_case": "acme", "execution_id": "a1", "workflow_request": "A landing zone", "work": "architecture", } assert result["results"]["architecture"]["status"] == "completed" assert result["results"]["architecture"]["execution_id"] == "a1" assert set(result["results"]["architecture"]["files"]) == {"graph.json", "guide.md"} async def test_a_caller_that_asks_for_files_gets_them_one_by_one(): agents = ScriptedAgents({"architecture": [finished("a1", {"graph.json": GRAPH})]}) task, _ = await call( agents, [{"text": "A landing zone"}], metadata={**PLATFORM, "work": "architecture", "deliver": "files"}, ) assert [a["name"] for a in task["artifacts"]] == ["graph.json"] (sent,) = agents.received["architecture"] assert "deliver" not in struct(sent) async def test_any_caller_can_ask_for_the_document(): agents = ScriptedAgents({"architecture": [finished("a1", {"graph.json": GRAPH})]}) task, _ = await call( agents, [{"text": "A landing zone"}], metadata={"work": "architecture", "deliver": "document"}, ) assert document(task)["files"]["graph.json"] == GRAPH async def test_a_sequence_is_one_document_with_each_agents_results(): agents = ScriptedAgents( { "architecture": [finished("a1", {"graph.json": GRAPH})], "operations": [finished("o1", {"model.json": "{}"})], } ) task, _ = await call( agents, [{"text": "A landing zone"}], metadata={**PLATFORM, "work": "architecture_and_operations"}, ) result = document(task) assert result["status"] == "completed" assert set(result["results"]) == {"architecture", "operations"} assert set(result["results"]["architecture"]["files"]) == {"graph.json"} # The top-level files are the last agent's. assert set(result["files"]) == {"model.json", "execution.json"} assert json.loads(result["files"]["execution.json"])["work"] == "operations" async def test_a_sequence_stopped_by_warnings_still_delivers_what_was_done(): agents = ScriptedAgents( { "architecture": [ finished("a1", {"graph.json": GRAPH}, status="completed_with_warnings") ], "operations": [], } ) task, _ = await call( agents, [{"text": "A landing zone"}], metadata={**PLATFORM, "work": "architecture_and_operations"}, ) result = document(task) assert result["status"] == "stopped" assert result["not_started"] == ["operations"] assert result["results"]["architecture"]["status"] == "completed_with_warnings" assert result["files"]["graph.json"] == GRAPH async def test_sources_given_as_data_reach_the_agent_as_files(): # A platform step maps an earlier step's output into `sources`; whatever was # parsed on the way is sent on as text, the form an agent reads files in. agents = ScriptedAgents({"operations": [finished("o1", {"model.json": "{}"})]}) await call( agents, [{"text": "Operate it"}], metadata={ "work": "operations", "sources": {"graph.json": {"elements": []}, "guide.md": "# Guide"}, }, ) (sent,) = agents.received["operations"] sources = struct(sent)["sources"] assert json.loads(sources["graph.json"]) == {"elements": []} assert sources["guide.md"] == "# Guide" async def test_the_card_declares_the_document_and_the_choice_of_delivery(): app = create_app([ARCHITECTURE, OPERATIONS], SETTINGS, sequences=[BOTH]) async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://orchestration" ) as caller: skill = (await caller.get("/.well-known/agent-card.json")).json()["skills"][0] inputs = skill["inputSchema"]["properties"] assert inputs["deliver"]["enum"] == ["document", "files"] assert inputs["sources"]["type"] == "object" outputs = skill["outputSchema"]["properties"] assert {"status", "work", "customer_case", "files", "results"} <= set(outputs)