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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
a2a.py — Agent-to-Agent protocol adapters.
|
| 3 |
+
|
| 4 |
+
Implements the A2A standard for Purpose Agent:
|
| 5 |
+
- AgentCard: declares agent capabilities, schemas, and endpoints
|
| 6 |
+
- A2AClient: delegate tasks to remote agents
|
| 7 |
+
- publish_card(): expose local agents as A2A-compatible services
|
| 8 |
+
|
| 9 |
+
Security:
|
| 10 |
+
- Endpoint allowlist (only approved remote agents)
|
| 11 |
+
- Schema validation on delegation and response
|
| 12 |
+
- Timeout + circuit breaker for unreachable peers
|
| 13 |
+
- Trust tier system (local > verified > unknown)
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import json
|
| 18 |
+
import logging
|
| 19 |
+
import time
|
| 20 |
+
import uuid
|
| 21 |
+
from dataclasses import dataclass, field
|
| 22 |
+
from enum import Enum
|
| 23 |
+
from typing import Any
|
| 24 |
+
|
| 25 |
+
logger = logging.getLogger(__name__)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TrustTier(str, Enum):
|
| 29 |
+
"""Trust level for remote agents."""
|
| 30 |
+
LOCAL = "local" # Same system, full trust
|
| 31 |
+
VERIFIED = "verified" # Authenticated, schema-validated
|
| 32 |
+
UNKNOWN = "unknown" # No verification, sandboxed
|
| 33 |
+
BLOCKED = "blocked" # Explicitly denied
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@dataclass
|
| 37 |
+
class AgentCapability:
|
| 38 |
+
"""A single capability declared by an agent."""
|
| 39 |
+
name: str
|
| 40 |
+
description: str
|
| 41 |
+
input_schema: dict[str, Any] = field(default_factory=dict)
|
| 42 |
+
output_schema: dict[str, Any] = field(default_factory=dict)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@dataclass
|
| 46 |
+
class AgentCard:
|
| 47 |
+
"""
|
| 48 |
+
A2A Agent Card — declares an agent's identity, capabilities, and endpoint.
|
| 49 |
+
|
| 50 |
+
This is what gets published for other agents to discover and delegate to.
|
| 51 |
+
"""
|
| 52 |
+
agent_id: str = field(default_factory=lambda: uuid.uuid4().hex[:12])
|
| 53 |
+
name: str = ""
|
| 54 |
+
description: str = ""
|
| 55 |
+
version: str = "1.0"
|
| 56 |
+
endpoint: str = "" # URL or local reference
|
| 57 |
+
capabilities: list[AgentCapability] = field(default_factory=list)
|
| 58 |
+
trust_tier: TrustTier = TrustTier.UNKNOWN
|
| 59 |
+
metadata: dict[str, Any] = field(default_factory=dict)
|
| 60 |
+
created_at: float = field(default_factory=time.time)
|
| 61 |
+
|
| 62 |
+
def to_dict(self) -> dict[str, Any]:
|
| 63 |
+
return {
|
| 64 |
+
"agent_id": self.agent_id,
|
| 65 |
+
"name": self.name,
|
| 66 |
+
"description": self.description,
|
| 67 |
+
"version": self.version,
|
| 68 |
+
"endpoint": self.endpoint,
|
| 69 |
+
"capabilities": [
|
| 70 |
+
{"name": c.name, "description": c.description,
|
| 71 |
+
"input_schema": c.input_schema, "output_schema": c.output_schema}
|
| 72 |
+
for c in self.capabilities
|
| 73 |
+
],
|
| 74 |
+
"trust_tier": self.trust_tier.value,
|
| 75 |
+
"metadata": self.metadata,
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
@classmethod
|
| 79 |
+
def from_dict(cls, d: dict[str, Any]) -> "AgentCard":
|
| 80 |
+
caps = [AgentCapability(**c) for c in d.get("capabilities", [])]
|
| 81 |
+
return cls(
|
| 82 |
+
agent_id=d.get("agent_id", ""),
|
| 83 |
+
name=d.get("name", ""),
|
| 84 |
+
description=d.get("description", ""),
|
| 85 |
+
version=d.get("version", "1.0"),
|
| 86 |
+
endpoint=d.get("endpoint", ""),
|
| 87 |
+
capabilities=caps,
|
| 88 |
+
trust_tier=TrustTier(d.get("trust_tier", "unknown")),
|
| 89 |
+
metadata=d.get("metadata", {}),
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
def to_json(self) -> str:
|
| 93 |
+
return json.dumps(self.to_dict(), indent=2)
|
| 94 |
+
|
| 95 |
+
def has_capability(self, name: str) -> bool:
|
| 96 |
+
return any(c.name == name for c in self.capabilities)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@dataclass
|
| 100 |
+
class A2ATaskResult:
|
| 101 |
+
"""Result from a delegated A2A task."""
|
| 102 |
+
success: bool
|
| 103 |
+
output: dict[str, Any] = field(default_factory=dict)
|
| 104 |
+
error: str | None = None
|
| 105 |
+
duration_s: float = 0.0
|
| 106 |
+
agent_id: str = ""
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class A2AClient:
|
| 110 |
+
"""
|
| 111 |
+
Client for delegating tasks to remote agents via A2A protocol.
|
| 112 |
+
|
| 113 |
+
Usage:
|
| 114 |
+
client = A2AClient()
|
| 115 |
+
client.register_peer(card)
|
| 116 |
+
|
| 117 |
+
result = client.delegate(
|
| 118 |
+
agent_id="remote_coder",
|
| 119 |
+
task="Write a fibonacci function",
|
| 120 |
+
timeout_s=30.0,
|
| 121 |
+
)
|
| 122 |
+
"""
|
| 123 |
+
|
| 124 |
+
def __init__(self, allowlist: list[str] | None = None):
|
| 125 |
+
self._peers: dict[str, AgentCard] = {}
|
| 126 |
+
self._allowlist = set(allowlist) if allowlist else None # None = allow all registered
|
| 127 |
+
self._circuit_breaker: dict[str, int] = {} # agent_id → consecutive failures
|
| 128 |
+
self._max_failures = 3
|
| 129 |
+
|
| 130 |
+
def register_peer(self, card: AgentCard) -> None:
|
| 131 |
+
"""Register a remote agent as a potential delegate."""
|
| 132 |
+
if card.trust_tier == TrustTier.BLOCKED:
|
| 133 |
+
logger.warning(f"A2A: rejected blocked agent '{card.name}'")
|
| 134 |
+
return
|
| 135 |
+
self._peers[card.agent_id] = card
|
| 136 |
+
logger.info(f"A2A: registered peer '{card.name}' ({card.agent_id}) trust={card.trust_tier.value}")
|
| 137 |
+
|
| 138 |
+
def delegate(
|
| 139 |
+
self,
|
| 140 |
+
agent_id: str,
|
| 141 |
+
task: str,
|
| 142 |
+
input_data: dict[str, Any] | None = None,
|
| 143 |
+
timeout_s: float = 30.0,
|
| 144 |
+
) -> A2ATaskResult:
|
| 145 |
+
"""
|
| 146 |
+
Delegate a task to a remote agent.
|
| 147 |
+
|
| 148 |
+
Returns A2ATaskResult with success/failure and output.
|
| 149 |
+
"""
|
| 150 |
+
card = self._peers.get(agent_id)
|
| 151 |
+
if not card:
|
| 152 |
+
return A2ATaskResult(success=False, error=f"Unknown agent: {agent_id}")
|
| 153 |
+
|
| 154 |
+
# Allowlist check
|
| 155 |
+
if self._allowlist and agent_id not in self._allowlist:
|
| 156 |
+
return A2ATaskResult(success=False, error=f"Agent '{agent_id}' not in allowlist")
|
| 157 |
+
|
| 158 |
+
# Circuit breaker
|
| 159 |
+
if self._circuit_breaker.get(agent_id, 0) >= self._max_failures:
|
| 160 |
+
return A2ATaskResult(
|
| 161 |
+
success=False,
|
| 162 |
+
error=f"Circuit breaker open for '{card.name}' (>{self._max_failures} failures)",
|
| 163 |
+
agent_id=agent_id,
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
t0 = time.time()
|
| 167 |
+
try:
|
| 168 |
+
result = self._execute_delegation(card, task, input_data or {}, timeout_s)
|
| 169 |
+
# Reset circuit breaker on success
|
| 170 |
+
self._circuit_breaker[agent_id] = 0
|
| 171 |
+
result.duration_s = time.time() - t0
|
| 172 |
+
result.agent_id = agent_id
|
| 173 |
+
return result
|
| 174 |
+
except Exception as e:
|
| 175 |
+
# Increment circuit breaker
|
| 176 |
+
self._circuit_breaker[agent_id] = self._circuit_breaker.get(agent_id, 0) + 1
|
| 177 |
+
return A2ATaskResult(
|
| 178 |
+
success=False,
|
| 179 |
+
error=f"Delegation failed: {e}",
|
| 180 |
+
duration_s=time.time() - t0,
|
| 181 |
+
agent_id=agent_id,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
def _execute_delegation(
|
| 185 |
+
self, card: AgentCard, task: str, input_data: dict, timeout_s: float
|
| 186 |
+
) -> A2ATaskResult:
|
| 187 |
+
"""
|
| 188 |
+
Execute the actual delegation. Protocol skeleton.
|
| 189 |
+
|
| 190 |
+
Real implementation requires HTTP client for remote agents,
|
| 191 |
+
or direct function call for local agents.
|
| 192 |
+
"""
|
| 193 |
+
# For local trust tier, try direct invocation
|
| 194 |
+
if card.trust_tier == TrustTier.LOCAL and card.endpoint.startswith("local:"):
|
| 195 |
+
# Future: invoke local agent directly
|
| 196 |
+
pass
|
| 197 |
+
|
| 198 |
+
# Protocol skeleton — real transport needs purpose-agent[a2a] extra
|
| 199 |
+
return A2ATaskResult(
|
| 200 |
+
success=False,
|
| 201 |
+
error=f"A2A transport not available. Install: pip install purpose-agent[a2a]. "
|
| 202 |
+
f"Would delegate to: {card.endpoint}",
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
def list_peers(self) -> list[AgentCard]:
|
| 206 |
+
"""List all registered peer agents."""
|
| 207 |
+
return list(self._peers.values())
|
| 208 |
+
|
| 209 |
+
def find_capable(self, capability: str) -> list[AgentCard]:
|
| 210 |
+
"""Find peers that declare a specific capability."""
|
| 211 |
+
return [c for c in self._peers.values() if c.has_capability(capability)]
|
| 212 |
+
|
| 213 |
+
@property
|
| 214 |
+
def peer_count(self) -> int:
|
| 215 |
+
return len(self._peers)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def publish_card(
|
| 219 |
+
name: str,
|
| 220 |
+
description: str,
|
| 221 |
+
capabilities: list[dict[str, str]],
|
| 222 |
+
endpoint: str = "",
|
| 223 |
+
) -> AgentCard:
|
| 224 |
+
"""
|
| 225 |
+
Create and return an AgentCard for a local Purpose Agent.
|
| 226 |
+
|
| 227 |
+
Usage:
|
| 228 |
+
card = publish_card(
|
| 229 |
+
name="code_reviewer",
|
| 230 |
+
description="Reviews Python code for bugs and style",
|
| 231 |
+
capabilities=[
|
| 232 |
+
{"name": "code_review", "description": "Review Python code"},
|
| 233 |
+
{"name": "security_audit", "description": "Check for security issues"},
|
| 234 |
+
],
|
| 235 |
+
endpoint="local:code_reviewer",
|
| 236 |
+
)
|
| 237 |
+
"""
|
| 238 |
+
caps = [AgentCapability(name=c["name"], description=c.get("description", "")) for c in capabilities]
|
| 239 |
+
return AgentCard(
|
| 240 |
+
name=name,
|
| 241 |
+
description=description,
|
| 242 |
+
capabilities=caps,
|
| 243 |
+
endpoint=endpoint,
|
| 244 |
+
trust_tier=TrustTier.LOCAL,
|
| 245 |
+
)
|