"""A request names work kept earlier instead of carrying its files (ADR-0004, phase 2): `sources_from` is a link from an earlier document, `latest/`, or an Execution's id in the request's Customer case.""" from __future__ import annotations import json from typing import Any from tests.orchestration.test_links import PLATFORM, Harness, document from tests.orchestration.test_sequence import ScriptedAgents, finished, struct GRAPH = '{"elements": []}' def designed(**more: Any) -> ScriptedAgents: """An architecture agent that has designed once, and an operations agent ready.""" turns: dict[str, list[Any]] = { "architecture": [finished("a1", {"graph.json": GRAPH, "guide.md": "# Guide"})], "operations": [finished("o1", {"model.json": "{}"})], } turns.update(more) return ScriptedAgents(turns) async def design(harness: Harness, case: str = "acme") -> dict[str, Any]: return document( await harness.send( "A landing zone", {**PLATFORM, "work": "architecture", "customer_case": case}, ) ) def not_started(task: dict[str, Any]) -> str: assert task["status"]["state"] == "TASK_STATE_COMPLETED" parts = task["status"]["message"]["parts"] data = next(p["data"] for p in parts if "data" in p) assert data["status"] == "not_started" return " ".join(p.get("text", "") for p in parts) def sent_sources(harness: Harness) -> dict[str, Any]: (message,) = harness.agents.received["operations"] metadata = struct(message) assert "sources_from" not in metadata return metadata async def test_the_latest_work_of_the_case_is_the_agents_sources(tmp_path): harness = Harness(tmp_path, designed()) await design(harness) await harness.send( "Operate it", { **PLATFORM, "work": "operations", "customer_case": "acme", "sources_from": "latest/architecture", }, ) sent = sent_sources(harness) assert sent["sources"]["graph.json"] == GRAPH assert sent["sources"]["guide.md"] == "# Guide" assert json.loads(sent["sources"]["execution.json"]) == { "status": "completed", "customer_case": "acme", "execution_id": "a1", "workflow_request": "A landing zone", "work": "architecture", } async def test_a_link_from_an_earlier_document_names_the_work_and_its_case(tmp_path): # A platform step maps an earlier step's `record` into `sources_from`. harness = Harness(tmp_path, designed()) earlier = await design(harness) await harness.send( "Operate it", {**PLATFORM, "work": "operations", "sources_from": earlier["record"]}, ) sent = sent_sources(harness) assert sent["customer_case"] == "acme" assert sent["sources"]["graph.json"] == GRAPH async def test_a_latest_link_is_followed_to_the_newest_completed_work(tmp_path): harness = Harness( tmp_path, designed( architecture=[ finished("a1", {"graph.json": "1"}), finished("a2", {"graph.json": "2"}), ] ), ) first = await design(harness) await design(harness) await harness.send( "Operate it", {**PLATFORM, "work": "operations", "sources_from": first["latest"]}, ) assert sent_sources(harness)["sources"]["graph.json"] == "2" async def test_an_execution_of_the_case_is_named_by_its_id(tmp_path): harness = Harness(tmp_path, designed()) await design(harness) await harness.send( "Operate it", { **PLATFORM, "work": "operations", "customer_case": "acme", "sources_from": "a1", }, ) assert sent_sources(harness)["sources"]["graph.json"] == GRAPH async def test_a_link_that_was_changed_starts_nothing(tmp_path): harness = Harness(tmp_path, designed()) earlier = await design(harness) task = await harness.send( "Operate it", { **PLATFORM, "work": "operations", "sources_from": earlier["record"].replace("/a1?", "/a2?"), }, ) assert "not valid" in not_started(task) assert harness.agents.received["operations"] == [] async def test_a_link_of_another_tenant_starts_nothing(tmp_path): harness = Harness(tmp_path, designed()) earlier = await design(harness) task = await harness.send( "Operate it", { **PLATFORM, "tenant_id": "t2", "work": "operations", "sources_from": earlier["record"], }, ) assert "tenant" in not_started(task) assert harness.agents.received["operations"] == [] async def test_work_of_another_customer_case_starts_nothing(tmp_path): harness = Harness(tmp_path, designed()) earlier = await design(harness) task = await harness.send( "Operate it", { **PLATFORM, "work": "operations", "customer_case": "other", "sources_from": earlier["record"], }, ) assert "acme" in not_started(task) async def test_nothing_kept_to_build_on_starts_nothing(tmp_path): harness = Harness(tmp_path, designed()) task = await harness.send( "Operate it", { **PLATFORM, "work": "operations", "customer_case": "acme", "sources_from": "latest/architecture", }, ) assert "architecture" in not_started(task) assert harness.agents.received["operations"] == [] async def test_work_that_ended_with_warnings_is_not_built_on(tmp_path): harness = Harness( tmp_path, designed( architecture=[ finished("a1", {"graph.json": GRAPH}, status="completed_with_warnings") ] ), ) await design(harness) task = await harness.send( "Operate it", { **PLATFORM, "work": "operations", "customer_case": "acme", "sources_from": "a1", }, ) assert "completed_with_warnings" in not_started(task) async def test_sources_and_sources_from_together_start_nothing(tmp_path): harness = Harness(tmp_path, designed()) await design(harness) task = await harness.send( "Operate it", { **PLATFORM, "work": "operations", "customer_case": "acme", "sources_from": "a1", "sources": {"x.json": "{}"}, }, ) assert "sources" in not_started(task) async def test_a_sequence_gives_the_next_agent_what_was_kept(tmp_path): harness = Harness(tmp_path, designed()) await harness.send( "A landing zone", {**PLATFORM, "work": "architecture_and_operations", "customer_case": "acme"}, ) sources = sent_sources(harness)["sources"] assert sources["graph.json"] == GRAPH assert json.loads(sources["execution.json"])["execution_id"] == "a1" async def test_the_card_offers_sources_from(tmp_path): harness = Harness(tmp_path, ScriptedAgents({})) async with harness.client() as caller: skill = (await caller.get("/.well-known/agent-card.json")).json()["skills"][0] assert skill["inputSchema"]["properties"]["sources_from"]["type"] == "string"