feat: working orhcestration engine
This commit is contained in:
0
tests/orchestration/__init__.py
Normal file
0
tests/orchestration/__init__.py
Normal file
162
tests/orchestration/test_document.py
Normal file
162
tests/orchestration/test_document.py
Normal file
@@ -0,0 +1,162 @@
|
||||
"""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)
|
||||
300
tests/orchestration/test_relay.py
Normal file
300
tests/orchestration/test_relay.py
Normal file
@@ -0,0 +1,300 @@
|
||||
"""The Orchestration engine in front of a real agent: the Execution engine on the toy
|
||||
Workflow, scripted. Both run in this process, joined by httpx's ASGI transport."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import uuid
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from execution_engine.adk.fake_runtime import Call
|
||||
from execution_engine.adk.roles import load_roles
|
||||
from execution_engine.agent import Assembly
|
||||
from execution_engine.config import Settings
|
||||
from execution_engine.main import create_app as create_agent
|
||||
from execution_engine.providers import load_provider
|
||||
from orchestration.agents import Agent
|
||||
from orchestration.config import OrchestrationSettings
|
||||
from orchestration.main import create_app
|
||||
from tests.toy.support import make_engine, once_each
|
||||
from tests.toy.workflow import PLANNING, ROOT, TOY
|
||||
|
||||
PICNIC_URL = "http://picnic"
|
||||
PICNIC = Agent(work="picnic", url=PICNIC_URL, description="Plans picnics.")
|
||||
PARTY = Agent(work="party", url="http://party", description="Plans parties.")
|
||||
SETTINGS = OrchestrationSettings(_env_file=None, HEARTBEAT_SECONDS=3600) # type: ignore[call-arg]
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def picnic_agent(
|
||||
tmp_path: Path, turns: dict[str, list[Any]]
|
||||
) -> AsyncIterator[Any]:
|
||||
settings = Settings( # type: ignore[call-arg]
|
||||
_env_file=None,
|
||||
CLOUD_PROVIDER="toy",
|
||||
AGENT_NAME="picnic-agent",
|
||||
A2A_PUBLIC_URL=PICNIC_URL,
|
||||
)
|
||||
bundle = load_provider("toy", ROOT)
|
||||
roles = load_roles(ROOT / "agents", bundle, TOY)
|
||||
engine, runtime = make_engine(tmp_path, turns, roles=roles)
|
||||
|
||||
@asynccontextmanager
|
||||
async def assembly(given: Settings) -> AsyncIterator[Assembly]:
|
||||
yield Assembly(engine, bundle, roles, {})
|
||||
|
||||
app = create_agent(TOY, settings, assembly)
|
||||
async with app.router.lifespan_context(app):
|
||||
yield app, runtime
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def orchestration(
|
||||
agent_app: Any, agents: list[Agent]
|
||||
) -> AsyncIterator[httpx.AsyncClient]:
|
||||
"""A caller's client to the Orchestration engine, whose agents are served by
|
||||
`agent_app` at the picnic agent's address."""
|
||||
to_agents = httpx.AsyncClient(
|
||||
mounts={PICNIC_URL: httpx.ASGITransport(app=agent_app)}
|
||||
)
|
||||
app = create_app(agents, SETTINGS, httpx_client=to_agents)
|
||||
async with (
|
||||
to_agents,
|
||||
httpx.AsyncClient(
|
||||
transport=httpx.ASGITransport(app=app), base_url="http://orchestration"
|
||||
) as caller,
|
||||
):
|
||||
yield caller
|
||||
|
||||
|
||||
async def send(
|
||||
caller: httpx.AsyncClient,
|
||||
parts: list[dict],
|
||||
task: dict | None = None,
|
||||
metadata: dict | None = None,
|
||||
path: str = "/a2a",
|
||||
) -> dict:
|
||||
message: dict[str, Any] = {
|
||||
"messageId": str(uuid.uuid4()),
|
||||
"role": "ROLE_USER",
|
||||
"parts": parts,
|
||||
}
|
||||
if task:
|
||||
message.update(taskId=task["id"], contextId=task["contextId"])
|
||||
if metadata:
|
||||
message["metadata"] = metadata
|
||||
response = await caller.post(
|
||||
path,
|
||||
json={
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "SendMessage",
|
||||
"params": {"message": message},
|
||||
},
|
||||
headers={"A2A-Version": "1.0"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
body = response.json()
|
||||
assert "error" not in body, body
|
||||
return body["result"]["task"]
|
||||
|
||||
|
||||
def status_parts(task: dict) -> tuple[str, dict | None]:
|
||||
parts = task["status"]["message"]["parts"]
|
||||
text = "\n".join(part["text"] for part in parts if "text" in part)
|
||||
data = next((part["data"] for part in parts if "data" in part), None)
|
||||
return text, data
|
||||
|
||||
|
||||
async def test_the_agents_questions_and_approvals_are_relayed(tmp_path):
|
||||
turns = once_each()
|
||||
turns["menu-writer"] = [
|
||||
[Call("needs_input", {"questions": ["Any allergies?"]})],
|
||||
*turns["menu-writer"],
|
||||
]
|
||||
async with (
|
||||
picnic_agent(tmp_path, turns) as (agent_app, runtime),
|
||||
orchestration(agent_app, [PICNIC, PARTY]) as caller,
|
||||
):
|
||||
task = await send(
|
||||
caller, [{"text": "A picnic for six"}], metadata={"work": "picnic"}
|
||||
)
|
||||
|
||||
assert task["status"]["state"] == "TASK_STATE_INPUT_REQUIRED"
|
||||
text, data = status_parts(task)
|
||||
assert "Any allergies?" in text
|
||||
assert data == {
|
||||
"prompt": {"questions": [{"from": "menu", "question": "Any allergies?"}]}
|
||||
}
|
||||
|
||||
task = await send(caller, [{"text": "No nuts"}], task)
|
||||
|
||||
_, data = status_parts(task)
|
||||
assert data is not None
|
||||
assert data["prompt"]["approval"]["phase"] == PLANNING
|
||||
assert runtime.runs[1].task.context["answers"][-1]["reply"] == "No nuts"
|
||||
|
||||
approval = {"approval": {"phase": PLANNING, "decision": "approve"}}
|
||||
task = await send(caller, [{"data": approval}], task)
|
||||
|
||||
_, data = status_parts(task)
|
||||
assert data is not None
|
||||
assert data["prompt"]["approval"]["phase"] == "checking"
|
||||
|
||||
|
||||
async def test_a_pre_approved_request_completes_with_the_agents_files(tmp_path):
|
||||
async with (
|
||||
picnic_agent(tmp_path, once_each()) as (agent_app, _),
|
||||
orchestration(agent_app, [PICNIC, PARTY]) as caller,
|
||||
):
|
||||
task = await send(
|
||||
caller,
|
||||
[{"text": "A picnic"}],
|
||||
metadata={"work": "picnic", "pre_approved": True, "customer_case": "park"},
|
||||
path="/",
|
||||
)
|
||||
|
||||
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
|
||||
text, data = status_parts(task)
|
||||
assert text == "The picnic is planned."
|
||||
assert data == {"status": "completed", "warnings": []}
|
||||
names = {artifact["name"] for artifact in task["artifacts"]}
|
||||
assert names == {"picnic-plan.md", "menu.json", "shopping.json", "verdicts.json"}
|
||||
# The metadata went to the agent as it came, the Customer case with it.
|
||||
assert (tmp_path / "deliverables" / "cases" / "park").is_dir()
|
||||
|
||||
|
||||
async def test_a_request_naming_no_work_starts_nothing_and_says_how(tmp_path):
|
||||
async with (
|
||||
picnic_agent(tmp_path, once_each()) as (agent_app, runtime),
|
||||
orchestration(agent_app, [PICNIC, PARTY]) as caller,
|
||||
):
|
||||
task = await send(caller, [{"text": "Something nice on Saturday"}])
|
||||
|
||||
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
|
||||
text, data = status_parts(task)
|
||||
assert "picnic" in text and "party" in text
|
||||
assert data == {"status": "not_started", "work": ["picnic", "party"]}
|
||||
assert runtime.runs == []
|
||||
|
||||
|
||||
async def test_the_card_offers_the_work_each_agent_does():
|
||||
app = create_app([PICNIC, PARTY], SETTINGS)
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.ASGITransport(app=app), base_url="http://orchestration"
|
||||
) as caller:
|
||||
for path in ("/.well-known/agent-card.json", "/.well-known/agent.json"):
|
||||
card = (await caller.get(path)).json()
|
||||
skill = card["skills"][0]
|
||||
assert card["supportedInterfaces"][0]["url"].endswith("/a2a")
|
||||
assert skill["inputArrives"] == "as_fields"
|
||||
work = skill["inputSchema"]["properties"]["work"]
|
||||
assert work["enum"] == ["picnic", "party"]
|
||||
assert "Plans picnics." in work["description"]
|
||||
assert (await caller.get("/health")).json()["agents"] == ["picnic", "party"]
|
||||
|
||||
|
||||
class SlowAgent:
|
||||
"""An agent that works for a while, then completes: what the operating model
|
||||
agent does for half an hour without a word."""
|
||||
|
||||
delay = 0.3
|
||||
|
||||
async def for_agent(self, agent: Agent) -> Any:
|
||||
return self
|
||||
|
||||
def forget(self, agent: Agent) -> None:
|
||||
pass
|
||||
|
||||
async def send_message(self, request: Any) -> AsyncIterator[Any]:
|
||||
from a2a.helpers.proto_helpers import new_text_part
|
||||
from a2a.types import Message, Role, StreamResponse, Task, TaskState, TaskStatus
|
||||
|
||||
await asyncio.sleep(self.delay)
|
||||
done = Message(
|
||||
message_id="done", role=Role.ROLE_AGENT, parts=[new_text_part("Done.")]
|
||||
)
|
||||
yield StreamResponse(
|
||||
task=Task(
|
||||
id="theirs",
|
||||
context_id="ctx",
|
||||
status=TaskStatus(state=TaskState.TASK_STATE_COMPLETED, message=done),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def test_a_silent_agent_is_reported_as_still_working():
|
||||
settings = OrchestrationSettings(_env_file=None, HEARTBEAT_SECONDS=0.05) # type: ignore[call-arg]
|
||||
app = create_app([PICNIC], settings, clients=SlowAgent()) # type: ignore[arg-type]
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.ASGITransport(app=app), base_url="http://orchestration"
|
||||
) as caller:
|
||||
response = await caller.post(
|
||||
"/a2a",
|
||||
json={
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "SendStreamingMessage",
|
||||
"params": {
|
||||
"message": {
|
||||
"messageId": "m1",
|
||||
"role": "ROLE_USER",
|
||||
"parts": [{"text": "A picnic"}],
|
||||
}
|
||||
},
|
||||
},
|
||||
headers={"A2A-Version": "1.0"},
|
||||
)
|
||||
|
||||
events = [
|
||||
json.loads(line.removeprefix("data:"))
|
||||
for line in response.text.splitlines()
|
||||
if line.startswith("data:")
|
||||
]
|
||||
states = [
|
||||
event["result"]["statusUpdate"]["status"]
|
||||
for event in events
|
||||
if "statusUpdate" in event.get("result", {})
|
||||
]
|
||||
beats = [s for s in states if "still working" in str(s.get("message", ""))]
|
||||
assert len(beats) >= 2
|
||||
assert states[-1]["state"] == "TASK_STATE_COMPLETED"
|
||||
|
||||
|
||||
class RecordingAgent(SlowAgent):
|
||||
"""An agent that records the message it was sent and completes at once."""
|
||||
|
||||
delay = 0.0
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.received: list[Any] = []
|
||||
|
||||
async def send_message(self, request: Any) -> AsyncIterator[Any]:
|
||||
self.received.append(request.message)
|
||||
async for event in super().send_message(request):
|
||||
yield event
|
||||
|
||||
|
||||
async def test_the_request_field_is_the_text_the_agent_is_sent():
|
||||
# A KAB step sends every field as metadata and, as the message text, all of
|
||||
# them again as one JSON document. The agent is sent the request as its text,
|
||||
# whichever agent it is; the fields stay in the metadata.
|
||||
agent = RecordingAgent()
|
||||
app = create_app([PICNIC], SETTINGS, clients=agent) # type: ignore[arg-type]
|
||||
fields = {"work": "picnic", "request": "A picnic for nine", "pre_approved": True}
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.ASGITransport(app=app), base_url="http://orchestration"
|
||||
) as caller:
|
||||
await send(caller, [{"text": json.dumps(fields)}], metadata=fields)
|
||||
|
||||
(message,) = agent.received
|
||||
assert [part.text for part in message.parts] == ["A picnic for nine"]
|
||||
assert message.metadata["request"] == "A picnic for nine"
|
||||
assert message.metadata["pre_approved"] is True
|
||||
assert "work" not in message.metadata
|
||||
61
tests/orchestration/test_routing.py
Normal file
61
tests/orchestration/test_routing.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""Which agent a request is for: what the caller named, else the only agent, else none."""
|
||||
|
||||
import pytest
|
||||
|
||||
from orchestration.agents import Agent, load_agents
|
||||
from orchestration.routing import route
|
||||
|
||||
PICNIC = Agent(work="picnic", url="http://picnic", description="Plans picnics.")
|
||||
PARTY = Agent(work="party", url="http://party", description="Plans parties.")
|
||||
|
||||
|
||||
def test_the_work_the_caller_names_settles_it():
|
||||
decision = route({"work": "party"}, [PICNIC, PARTY])
|
||||
|
||||
assert decision.agent == PARTY
|
||||
|
||||
|
||||
def test_with_one_agent_and_nothing_named_that_agent_takes_it():
|
||||
decision = route({}, [PICNIC])
|
||||
|
||||
assert decision.agent == PICNIC
|
||||
|
||||
|
||||
def test_with_several_agents_and_nothing_named_nothing_starts():
|
||||
decision = route({}, [PICNIC, PARTY])
|
||||
|
||||
assert decision.agent is None
|
||||
assert "picnic" in decision.reason and "party" in decision.reason
|
||||
|
||||
|
||||
def test_work_no_agent_does_is_refused_by_name():
|
||||
decision = route({"work": "wedding"}, [PICNIC, PARTY])
|
||||
|
||||
assert decision.agent is None
|
||||
assert "wedding" in decision.reason
|
||||
|
||||
|
||||
def test_agents_are_read_from_a_file(tmp_path):
|
||||
path = tmp_path / "agents.yaml"
|
||||
path.write_text(
|
||||
"agents:\n"
|
||||
" - work: picnic\n"
|
||||
" url: http://picnic/\n"
|
||||
" description: Plans picnics.\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
assert load_agents(path) == [PICNIC]
|
||||
|
||||
|
||||
def test_two_agents_for_one_kind_of_work_are_refused(tmp_path):
|
||||
path = tmp_path / "agents.yaml"
|
||||
path.write_text(
|
||||
"agents:\n"
|
||||
" - {work: picnic, url: http://a}\n"
|
||||
" - {work: picnic, url: http://b}\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="picnic"):
|
||||
load_agents(path)
|
||||
287
tests/orchestration/test_sequence.py
Normal file
287
tests/orchestration/test_sequence.py
Normal file
@@ -0,0 +1,287 @@
|
||||
"""A sequence of agents (ADR-0003, step F4): each agent's files go to the next as
|
||||
`sources`, under one Customer case, with every agent's prompts relayed."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import uuid
|
||||
from collections.abc import AsyncIterator
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
from a2a.helpers.proto_helpers import new_data_part, new_text_part
|
||||
from a2a.types import (
|
||||
Artifact,
|
||||
Message,
|
||||
Role,
|
||||
StreamResponse,
|
||||
Task,
|
||||
TaskState,
|
||||
TaskStatus,
|
||||
)
|
||||
|
||||
from orchestration.agents import Agent, Sequence, load_routes
|
||||
from orchestration.config import OrchestrationSettings
|
||||
from orchestration.main import create_app
|
||||
from orchestration.routing import route
|
||||
|
||||
ARCHITECTURE = Agent(
|
||||
work="architecture", url="http://architecture", description="Designs."
|
||||
)
|
||||
OPERATIONS = Agent(work="operations", url="http://operations", description="Operates.")
|
||||
BOTH = Sequence(
|
||||
work="architecture_and_operations",
|
||||
steps=(ARCHITECTURE, OPERATIONS),
|
||||
description="Designs, then operates.",
|
||||
)
|
||||
SETTINGS = OrchestrationSettings(_env_file=None, HEARTBEAT_SECONDS=3600) # type: ignore[call-arg]
|
||||
|
||||
|
||||
def finished(
|
||||
task_id: str, files: dict[str, str], status: str = "completed"
|
||||
) -> StreamResponse:
|
||||
message = Message(
|
||||
message_id=str(uuid.uuid4()),
|
||||
role=Role.ROLE_AGENT,
|
||||
parts=[
|
||||
new_text_part("Done."),
|
||||
new_data_part({"status": status, "warnings": []}),
|
||||
],
|
||||
)
|
||||
return StreamResponse(
|
||||
task=Task(
|
||||
id=task_id,
|
||||
context_id=f"ctx-{task_id}",
|
||||
status=TaskStatus(state=TaskState.TASK_STATE_COMPLETED, message=message),
|
||||
artifacts=[
|
||||
Artifact(
|
||||
artifact_id=f"{task_id}-{name}",
|
||||
name=name,
|
||||
parts=[new_text_part(text)],
|
||||
)
|
||||
for name, text in files.items()
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def asking(task_id: str) -> StreamResponse:
|
||||
message = Message(
|
||||
message_id=str(uuid.uuid4()),
|
||||
role=Role.ROLE_AGENT,
|
||||
parts=[new_text_part("Who operates it?")],
|
||||
)
|
||||
return StreamResponse(
|
||||
task=Task(
|
||||
id=task_id,
|
||||
context_id=f"ctx-{task_id}",
|
||||
status=TaskStatus(
|
||||
state=TaskState.TASK_STATE_INPUT_REQUIRED, message=message
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class ScriptedAgents:
|
||||
"""Stand-ins for the agents: each answers its turns in order and records what
|
||||
it was sent."""
|
||||
|
||||
def __init__(self, turns: dict[str, list[StreamResponse]]) -> None:
|
||||
self.turns = {work: list(events) for work, events in turns.items()}
|
||||
self.received: dict[str, list[Message]] = {work: [] for work in turns}
|
||||
self._current = ""
|
||||
|
||||
async def for_agent(self, agent: Agent) -> Any:
|
||||
self._current = agent.work
|
||||
return _Client(self, agent.work)
|
||||
|
||||
def forget(self, agent: Agent) -> None:
|
||||
pass
|
||||
|
||||
|
||||
class _Client:
|
||||
def __init__(self, agents: ScriptedAgents, work: str) -> None:
|
||||
self._agents, self._work = agents, work
|
||||
|
||||
async def send_message(self, request: Any) -> AsyncIterator[StreamResponse]:
|
||||
self._agents.received[self._work].append(request.message)
|
||||
yield self._agents.turns[self._work].pop(0)
|
||||
|
||||
|
||||
async def call(
|
||||
agents: ScriptedAgents,
|
||||
parts: list[dict],
|
||||
metadata: dict | None = None,
|
||||
task: dict | None = None,
|
||||
app: Any = None,
|
||||
) -> tuple[dict, Any]:
|
||||
app = app or create_app(
|
||||
[ARCHITECTURE, OPERATIONS],
|
||||
SETTINGS,
|
||||
clients=agents, # type: ignore[arg-type]
|
||||
sequences=[BOTH],
|
||||
)
|
||||
message: dict[str, Any] = {
|
||||
"messageId": str(uuid.uuid4()),
|
||||
"role": "ROLE_USER",
|
||||
"parts": parts,
|
||||
}
|
||||
if metadata:
|
||||
message["metadata"] = metadata
|
||||
if task:
|
||||
message.update(taskId=task["id"], contextId=task["contextId"])
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.ASGITransport(app=app), base_url="http://orchestration"
|
||||
) as caller:
|
||||
response = await caller.post(
|
||||
"/a2a",
|
||||
json={
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "SendMessage",
|
||||
"params": {"message": message},
|
||||
},
|
||||
headers={"A2A-Version": "1.0"},
|
||||
)
|
||||
body = response.json()
|
||||
assert "error" not in body, body
|
||||
return body["result"]["task"], app
|
||||
|
||||
|
||||
def struct(message: Message) -> dict[str, Any]:
|
||||
from google.protobuf import json_format
|
||||
|
||||
return json_format.MessageToDict(message.metadata)
|
||||
|
||||
|
||||
def test_a_sequence_is_named_like_any_work():
|
||||
decision = route(
|
||||
{"work": "architecture_and_operations"}, [ARCHITECTURE, OPERATIONS], [BOTH]
|
||||
)
|
||||
|
||||
assert decision.agents == (ARCHITECTURE, OPERATIONS)
|
||||
|
||||
|
||||
def test_sequences_are_read_with_the_agents(tmp_path):
|
||||
path = tmp_path / "agents.yaml"
|
||||
path.write_text(
|
||||
"agents:\n"
|
||||
" - {work: architecture, url: http://architecture, description: Designs.}\n"
|
||||
" - {work: operations, url: http://operations, description: Operates.}\n"
|
||||
"sequences:\n"
|
||||
" - work: architecture_and_operations\n"
|
||||
" steps: [architecture, operations]\n"
|
||||
" description: Designs, then operates.\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
agents, sequences = load_routes(path)
|
||||
|
||||
assert agents == [ARCHITECTURE, OPERATIONS]
|
||||
assert sequences == [BOTH]
|
||||
|
||||
|
||||
async def test_the_first_agents_files_are_the_next_agents_sources():
|
||||
agents = ScriptedAgents(
|
||||
{
|
||||
"architecture": [
|
||||
finished("a1", {"graph.json": "{}", "guide.md": "# Guide"})
|
||||
],
|
||||
"operations": [finished("o1", {"model.json": "{}"})],
|
||||
}
|
||||
)
|
||||
task, _ = await call(
|
||||
agents,
|
||||
[{"text": "A landing zone"}],
|
||||
metadata={
|
||||
"work": "architecture_and_operations",
|
||||
"request": "A landing zone",
|
||||
"customer_case": "acme",
|
||||
"pre_approved": True,
|
||||
"scope": "architecture: spec",
|
||||
"operations": {"scope": "operations: overlay"},
|
||||
},
|
||||
)
|
||||
|
||||
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
|
||||
# Every agent's files reach the caller, each marked with the work it is of.
|
||||
relayed = {(a["name"], a["metadata"]["work"]) for a in task["artifacts"]}
|
||||
assert relayed == {
|
||||
("graph.json", "architecture"),
|
||||
("guide.md", "architecture"),
|
||||
("model.json", "operations"),
|
||||
}
|
||||
|
||||
(first,) = agents.received["architecture"]
|
||||
sent = struct(first)
|
||||
assert sent["scope"] == "architecture: spec"
|
||||
assert "operations" not in sent and "work" not in sent
|
||||
|
||||
(second,) = agents.received["operations"]
|
||||
sent = struct(second)
|
||||
assert [part.text for part in second.parts] == ["A landing zone"]
|
||||
assert sent["customer_case"] == "acme"
|
||||
assert sent["pre_approved"] is True
|
||||
# Its own fields, not the first agent's spec.
|
||||
assert sent["scope"] == "operations: overlay"
|
||||
sources = sent["sources"]
|
||||
assert sources["graph.json"] == "{}" and sources["guide.md"] == "# Guide"
|
||||
record = json.loads(sources["execution.json"])
|
||||
assert record == {
|
||||
"status": "completed",
|
||||
"customer_case": "acme",
|
||||
"execution_id": "a1",
|
||||
"workflow_request": "A landing zone",
|
||||
"work": "architecture",
|
||||
}
|
||||
|
||||
|
||||
async def test_an_agent_that_ends_with_warnings_starts_nothing_after_it():
|
||||
agents = ScriptedAgents(
|
||||
{
|
||||
"architecture": [
|
||||
finished("a1", {"graph.json": "{}"}, status="completed_with_warnings")
|
||||
],
|
||||
"operations": [],
|
||||
}
|
||||
)
|
||||
task, _ = await call(
|
||||
agents,
|
||||
[{"text": "A landing zone"}],
|
||||
metadata={"work": "architecture_and_operations"},
|
||||
)
|
||||
|
||||
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
|
||||
text = " ".join(p.get("text", "") for p in task["status"]["message"]["parts"])
|
||||
assert "warnings" in text and "operations" in text
|
||||
data = next(p["data"] for p in task["status"]["message"]["parts"] if "data" in p)
|
||||
assert data == {
|
||||
"status": "stopped",
|
||||
"after": "architecture",
|
||||
"not_started": ["operations"],
|
||||
}
|
||||
assert agents.received["operations"] == []
|
||||
|
||||
|
||||
async def test_the_next_agents_questions_are_relayed_and_answered():
|
||||
agents = ScriptedAgents(
|
||||
{
|
||||
"architecture": [finished("a1", {"graph.json": "{}"})],
|
||||
"operations": [asking("o1"), finished("o1", {"model.json": "{}"})],
|
||||
}
|
||||
)
|
||||
task, app = await call(
|
||||
agents,
|
||||
[{"text": "A landing zone"}],
|
||||
metadata={"work": "architecture_and_operations"},
|
||||
)
|
||||
|
||||
assert task["status"]["state"] == "TASK_STATE_INPUT_REQUIRED"
|
||||
assert "Who operates it?" in task["status"]["message"]["parts"][0]["text"]
|
||||
|
||||
task, _ = await call(agents, [{"text": "A service provider"}], task=task, app=app)
|
||||
|
||||
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
|
||||
reply = agents.received["operations"][-1]
|
||||
assert [part.text for part in reply.parts] == ["A service provider"]
|
||||
assert reply.task_id == "o1"
|
||||
Reference in New Issue
Block a user