294 lines
9.6 KiB
Python
294 lines
9.6 KiB
Python
"""The A2A server: tasks map onto Executions, driven over JSON-RPC with a scripted runtime."""
|
|
|
|
import uuid
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from langgraph.checkpoint.memory import InMemorySaver
|
|
from starlette.testclient import TestClient
|
|
|
|
from app.adk.fake_runtime import Call, FakeAgentRuntime
|
|
from app.adk.roles import load_roles
|
|
from app.agent import Assembly, ExecutionEngine
|
|
from app.config import get_settings
|
|
from app.main import create_app
|
|
from app.providers import load_provider
|
|
from app.recorder import InMemoryRecorder
|
|
from app.workflows.deliverables import DEPENDENCY_GRAPH, DeliverableKind, PhaseId
|
|
|
|
AGENTS_DIR = Path(__file__).parents[2] / "agents"
|
|
K = DeliverableKind
|
|
LEGACY_GENERATE_KEYS = {
|
|
"execution_id",
|
|
"session_id",
|
|
"status",
|
|
"validation_passed",
|
|
"total_loop_iterations",
|
|
"current_phase",
|
|
"artifacts",
|
|
"active_skills",
|
|
}
|
|
|
|
|
|
def submission(kind: DeliverableKind) -> list[Call]:
|
|
spec = DEPENDENCY_GRAPH[kind]
|
|
body = "\n".join(f"## {section}\nDetails." for section in spec.sections)
|
|
return [
|
|
Call(
|
|
"submit_deliverable",
|
|
{
|
|
"content": f"# {kind}\n{body}\n",
|
|
"attachments": {name: f"# {name}\n" for name in spec.attachments},
|
|
},
|
|
)
|
|
]
|
|
|
|
|
|
def complete_run_turns() -> dict:
|
|
def delegate(*kinds):
|
|
return Call("delegate", {"kinds": [kind.value for kind in kinds]})
|
|
|
|
def approve(phase):
|
|
return Call("request_approval", {"phase": phase.value, "summary": "done"})
|
|
|
|
return {
|
|
"orchestrator": [
|
|
[delegate(K.REQUIREMENTS)],
|
|
[delegate(K.TECHNICAL_DECOMPOSITION)],
|
|
[approve(PhaseId.REQUIREMENTS_DISCOVERY), delegate(K.PRODUCT_SELECTION)],
|
|
[delegate(K.ARCHITECTURE_DIAGRAM, K.DESIGN_RECOMMENDATIONS)],
|
|
[delegate(K.ARCHITECTURE_DESCRIPTION, K.DEPLOYMENT_GUIDANCE)],
|
|
[approve(PhaseId.SOLUTION_ARCHITECTURE), delegate(K.VALIDATION_RESULTS)],
|
|
[approve(PhaseId.SOLUTION_VALIDATION), delegate(K.SOLUTION_GUIDE)],
|
|
[approve(PhaseId.PACKAGING), Call("finish")],
|
|
],
|
|
**{
|
|
spec.specialist: [submission(kind)]
|
|
for kind, spec in DEPENDENCY_GRAPH.items()
|
|
},
|
|
}
|
|
|
|
|
|
def client_for(tmp_path: Path, turns: dict) -> tuple[TestClient, FakeAgentRuntime]:
|
|
bundle = load_provider("gcp")
|
|
roles = load_roles(AGENTS_DIR, bundle)
|
|
runtime = FakeAgentRuntime(turns)
|
|
|
|
@asynccontextmanager
|
|
async def fake_assembly(settings):
|
|
engine = ExecutionEngine(
|
|
runtime=runtime,
|
|
roles=roles,
|
|
recorder=InMemoryRecorder(),
|
|
checkpointer=InMemorySaver(),
|
|
output_root=tmp_path,
|
|
)
|
|
yield Assembly(engine, bundle, roles, {"compute": "API not enabled"})
|
|
|
|
return TestClient(create_app(get_settings(), fake_assembly)), runtime
|
|
|
|
|
|
def send(
|
|
client: TestClient, parts: list[dict], task: dict | None = None, metadata=None
|
|
):
|
|
message = {"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 = client.post(
|
|
"/a2a",
|
|
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
|
|
|
|
|
|
def test_agent_card_is_served_at_current_and_legacy_paths(tmp_path):
|
|
client, _ = client_for(tmp_path, {})
|
|
with client:
|
|
for path in ("/.well-known/agent-card.json", "/.well-known/agent.json"):
|
|
card = client.get(path).json()
|
|
assert card["name"] == get_settings().AGENT_NAME
|
|
assert card["supportedInterfaces"][0]["url"].endswith("/a2a")
|
|
assert "solution_architecture" in card["skills"][0]["description"]
|
|
|
|
|
|
def test_question_pauses_the_task_and_a_text_reply_resumes_it(tmp_path):
|
|
turns = {
|
|
"orchestrator": [
|
|
[Call("ask_user", {"questions": ["What is the RTO?"]})],
|
|
lambda task: [
|
|
Call(
|
|
"ask_user",
|
|
{
|
|
"questions": [
|
|
f"Heard: {task.context['conversation'][-1]['text']}"
|
|
]
|
|
},
|
|
)
|
|
],
|
|
]
|
|
}
|
|
client, _ = client_for(tmp_path, turns)
|
|
with client:
|
|
task = send(client, [{"text": "Design regional ingestion"}])
|
|
|
|
assert task["status"]["state"] == "TASK_STATE_INPUT_REQUIRED"
|
|
text, data = status_parts(task)
|
|
assert "What is the RTO?" in text
|
|
assert data == {
|
|
"prompt": {
|
|
"questions": [{"from": "orchestrator", "question": "What is the RTO?"}]
|
|
}
|
|
}
|
|
|
|
task = send(client, [{"text": "One hour"}], task)
|
|
|
|
text, _ = status_parts(task)
|
|
assert "Heard: One hour" in text
|
|
|
|
|
|
def test_approval_counts_only_as_structured_data(tmp_path):
|
|
turns = complete_run_turns()
|
|
turns["orchestrator"][2] = [
|
|
Call("request_approval", {"phase": "requirements_discovery", "summary": "done"})
|
|
]
|
|
turns["orchestrator"][3:] = [
|
|
[
|
|
Call(
|
|
"request_approval",
|
|
{"phase": "requirements_discovery", "summary": "again"},
|
|
)
|
|
],
|
|
[Call("ask_user", {"questions": ["Approved; what next?"]})],
|
|
]
|
|
client, _ = client_for(tmp_path, turns)
|
|
with client:
|
|
task = send(client, [{"text": "Design ingestion"}])
|
|
text, data = status_parts(task)
|
|
assert data["prompt"]["approval"]["phase"] == "requirements_discovery"
|
|
assert '"decision": "approve"' in text
|
|
|
|
task = send(client, [{"text": "approve"}], task)
|
|
_, data = status_parts(task)
|
|
assert data["prompt"]["approval"]["phase"] == "requirements_discovery"
|
|
|
|
task = send(
|
|
client,
|
|
[
|
|
{
|
|
"data": {
|
|
"approval": {
|
|
"phase": "requirements_discovery",
|
|
"decision": "approve",
|
|
}
|
|
}
|
|
}
|
|
],
|
|
task,
|
|
)
|
|
text, _ = status_parts(task)
|
|
assert "Approved; what next?" in text
|
|
|
|
|
|
def test_pre_approved_task_completes_with_guide_and_code_artifacts(tmp_path):
|
|
client, _ = client_for(tmp_path, complete_run_turns())
|
|
with client:
|
|
task = send(
|
|
client, [{"text": "Design ingestion"}], metadata={"pre_approved": True}
|
|
)
|
|
|
|
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
|
|
names = {artifact["name"] for artifact in task["artifacts"]}
|
|
assert {
|
|
"solution-architecture-guide.md",
|
|
"terraform/main.tf",
|
|
"architecture.mmd",
|
|
} <= names
|
|
|
|
|
|
def test_engine_failure_fails_the_task(tmp_path):
|
|
client, _ = client_for(tmp_path, {"orchestrator": []})
|
|
with client:
|
|
task = send(client, [{"text": "Design ingestion"}])
|
|
|
|
assert task["status"]["state"] == "TASK_STATE_FAILED"
|
|
assert "ScriptExhausted" in status_parts(task)[0]
|
|
|
|
|
|
def test_v0_3_clients_are_accepted(tmp_path):
|
|
client, _ = client_for(
|
|
tmp_path, {"orchestrator": [[Call("ask_user", {"questions": ["Region?"]})]]}
|
|
)
|
|
with client:
|
|
response = client.post(
|
|
"/a2a",
|
|
json={
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"method": "message/send",
|
|
"params": {
|
|
"message": {
|
|
"messageId": "m1",
|
|
"role": "user",
|
|
"kind": "message",
|
|
"parts": [{"kind": "text", "text": "Design ingestion"}],
|
|
}
|
|
},
|
|
},
|
|
)
|
|
|
|
result = response.json()["result"]
|
|
assert result["status"]["state"] == "input-required"
|
|
|
|
|
|
def test_generate_runs_pre_approved_and_keeps_the_legacy_response_keys(tmp_path):
|
|
client, _ = client_for(tmp_path, complete_run_turns())
|
|
with client:
|
|
response = client.post("/generate", json={"request": "Design ingestion"})
|
|
|
|
body = response.json()
|
|
assert response.status_code == 200, body
|
|
assert LEGACY_GENERATE_KEYS <= set(body)
|
|
assert body["status"] == "success"
|
|
assert body["validation_passed"] is True
|
|
assert body["artifacts"]["terraform_code"] == "# terraform/main.tf\n"
|
|
assert body["artifacts"]["source_discovery_doc"] == "Details."
|
|
assert "google-cloud-solution-architecture" in body["active_skills"]
|
|
assert (tmp_path / "deliverables" / "executions" / body["execution_id"]).is_dir()
|
|
|
|
|
|
@pytest.mark.parametrize("payload", [{}, {"request": " "}])
|
|
def test_generate_requires_a_request(tmp_path, payload):
|
|
client, _ = client_for(tmp_path, {})
|
|
with client:
|
|
response = client.post("/generate", json=payload)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
def test_health_reports_mcp_discovery_gaps(tmp_path):
|
|
client, _ = client_for(tmp_path, {})
|
|
with client:
|
|
body = client.get("/health").json()
|
|
|
|
assert body["status"] == "healthy"
|
|
assert body["mcp_discovery_gaps"] == {"compute": "API not enabled"}
|