|
| 1 | +"""Base adapter contracts for framework-specific integrations. |
| 2 | +
|
| 3 | +An adapter's job: |
| 4 | +
|
| 5 | +1. Detect whether it can handle a given agent object. |
| 6 | +2. Attach hooks to that agent (framework-specific). |
| 7 | +3. Publish events to a policy evaluator when those hooks fire. |
| 8 | +
|
| 9 | +The evaluator subscribes to events and runs policy checks; it never |
| 10 | +knows or cares which adapter fired the event. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from abc import ABC, abstractmethod |
| 16 | +from typing import Any |
| 17 | +from uuid import uuid4 |
| 18 | + |
| 19 | +from .evaluator import EvaluatorProtocol |
| 20 | + |
| 21 | + |
| 22 | +class BaseAdapter(ABC): |
| 23 | + """Base class for framework-specific governance adapters.""" |
| 24 | + |
| 25 | + #: Higher value = more specific = inserted earlier in the registry. |
| 26 | + #: Plugin authors should set this above ``0`` on adapters that target |
| 27 | + #: a narrower agent type than another already-registered adapter, so |
| 28 | + #: the specific one wins ``can_handle`` resolution regardless of the |
| 29 | + #: order in which plugins happen to be imported. Among adapters with |
| 30 | + #: the same priority, registration order is preserved (stable). |
| 31 | + priority: int = 0 |
| 32 | + |
| 33 | + #: Set to True on a catch-all adapter that should always sort last in |
| 34 | + #: the registry. The registry uses this flag (not the class name or |
| 35 | + #: :attr:`priority`) to keep the fallback in last position when new |
| 36 | + #: adapters register. |
| 37 | + is_fallback: bool = False |
| 38 | + |
| 39 | + @property |
| 40 | + def name(self) -> str: |
| 41 | + """Return adapter name for logging.""" |
| 42 | + return self.__class__.__name__ |
| 43 | + |
| 44 | + @abstractmethod |
| 45 | + def can_handle(self, agent: Any) -> bool: |
| 46 | + """Return True if this adapter knows how to hook into this agent type.""" |
| 47 | + |
| 48 | + @abstractmethod |
| 49 | + def attach( |
| 50 | + self, |
| 51 | + agent: Any, |
| 52 | + agent_id: str, |
| 53 | + session_id: str, |
| 54 | + evaluator: EvaluatorProtocol, |
| 55 | + ) -> Any: |
| 56 | + """Attach governance hooks to the agent. |
| 57 | +
|
| 58 | + Args: |
| 59 | + agent: The agent to govern. |
| 60 | + agent_id: Unique identifier for the agent. |
| 61 | + session_id: Session identifier for tracing. |
| 62 | + evaluator: Policy evaluator implementing |
| 63 | + :class:`EvaluatorProtocol`. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + A governed proxy (or the original agent with hooks installed). |
| 67 | + """ |
| 68 | + |
| 69 | + def detach(self, governed: Any) -> Any: |
| 70 | + """Detach governance and return the original agent. |
| 71 | +
|
| 72 | + Default implementation uses the public :attr:`GovernedAgentBase.unwrapped` |
| 73 | + contract; non-proxy adapters that return the original agent from |
| 74 | + :meth:`attach` get back ``governed`` unchanged. |
| 75 | + """ |
| 76 | + return getattr(governed, "unwrapped", governed) |
| 77 | + |
| 78 | + def _generate_trace_id(self) -> str: |
| 79 | + """Generate a trace ID for governance events.""" |
| 80 | + return str(uuid4()) |
| 81 | + |
| 82 | + |
| 83 | +class GovernedAgentBase: |
| 84 | + """Base class for governed agent proxies. |
| 85 | +
|
| 86 | + Provides common functionality for all governed agents: |
| 87 | +
|
| 88 | + - Stores reference to original agent |
| 89 | + - Forwards unknown attributes to original agent |
| 90 | + - Tracks governance metadata |
| 91 | + """ |
| 92 | + |
| 93 | + def __init__( |
| 94 | + self, |
| 95 | + agent: Any, |
| 96 | + adapter: BaseAdapter, |
| 97 | + agent_id: str, |
| 98 | + session_id: str, |
| 99 | + evaluator: EvaluatorProtocol, |
| 100 | + ) -> None: |
| 101 | + """Initialize with the wrapped agent and governance metadata.""" |
| 102 | + self._agent = agent |
| 103 | + self._adapter = adapter |
| 104 | + self._agent_id = agent_id |
| 105 | + self._session_id = session_id |
| 106 | + self._evaluator = evaluator |
| 107 | + self._trace_id = adapter._generate_trace_id() |
| 108 | + |
| 109 | + @property |
| 110 | + def unwrapped(self) -> Any: |
| 111 | + """Get the original unwrapped agent.""" |
| 112 | + return self._agent |
| 113 | + |
| 114 | + def __getattr__(self, name: str) -> Any: |
| 115 | + """Forward attribute access to the original agent.""" |
| 116 | + return getattr(self._agent, name) |
0 commit comments