forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaf_adapter.py
More file actions
631 lines (523 loc) · 23.1 KB
/
maf_adapter.py
File metadata and controls
631 lines (523 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Microsoft Agent Framework (MAF) Governance Adapter
Bridges the Agent OS governance toolkit into MAF's native middleware system.
Four composable middleware layers enforce policy, capability guards, audit
trails, and rogue-agent detection at every level of the agent stack:
- GovernancePolicyMiddleware (AgentMiddleware): Declarative policy enforcement
- CapabilityGuardMiddleware (FunctionMiddleware): Tool allow/deny lists
- AuditTrailMiddleware (AgentMiddleware): Tamper-proof audit logging
- RogueDetectionMiddleware (FunctionMiddleware): Behavioral anomaly detection
Each middleware works independently and can be composed in any combination.
Usage::
from agent_framework import Agent
from agent_os.integrations.maf_adapter import create_governance_middleware
middleware = create_governance_middleware(
policy_directory="policies/",
allowed_tools=["web_search", "file_read"],
enable_rogue_detection=True,
)
agent = Agent(
name="researcher",
instructions="You are a research assistant.",
middleware=middleware,
)
"""
from __future__ import annotations
import logging
import time
from pathlib import Path
from typing import Any, Awaitable, Callable
from agent_os.policies import PolicyDecision, PolicyEvaluator
from agentmesh.governance import AuditEntry, AuditLog
from agent_sre.anomaly import RiskLevel, RogueAgentDetector, RogueDetectorConfig
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Conditional MAF imports — fall back to local stubs when agent_framework
# is not installed so the module remains importable for testing / linting.
# ---------------------------------------------------------------------------
try:
from agent_framework import (
AgentContext,
AgentMiddleware,
AgentResponse,
FunctionInvocationContext,
FunctionMiddleware,
Message,
MiddlewareTermination,
)
except ImportError: # pragma: no cover
logger.debug(
"agent_framework is not installed; MAF middleware classes will use "
"protocol-only base stubs."
)
class AgentMiddleware: # type: ignore[no-redef]
"""Stub base class when agent_framework is absent."""
class FunctionMiddleware: # type: ignore[no-redef]
"""Stub base class when agent_framework is absent."""
class AgentContext: # type: ignore[no-redef]
"""Stub for type hints."""
class FunctionInvocationContext: # type: ignore[no-redef]
"""Stub for type hints."""
class AgentResponse: # type: ignore[no-redef]
def __init__(self, *, messages: list[Any] | None = None) -> None:
self.messages = messages or []
class Message: # type: ignore[no-redef]
def __init__(self, role: str, contents: list[str] | None = None) -> None:
self.role = role
self.contents = contents or []
@property
def text(self) -> str:
return str(self.contents[0]) if self.contents else ""
class MiddlewareTermination(Exception): # type: ignore[no-redef]
"""Local fallback when agent_framework is not installed."""
# ═══════════════════════════════════════════════════════════════════════════
# 1. GovernancePolicyMiddleware
# ═══════════════════════════════════════════════════════════════════════════
class GovernancePolicyMiddleware(AgentMiddleware):
"""AgentMiddleware that evaluates declarative governance policies.
Intercepts every agent invocation, builds a context dict from the
incoming messages and agent metadata, and evaluates it against loaded
:class:`~agent_os.policies.PolicyEvaluator` rules. Denied requests
are short-circuited with an ``AgentResponse`` explaining the
violation and a ``MiddlewareTermination`` is raised.
Args:
evaluator: Pre-configured :class:`PolicyEvaluator` with loaded
policy documents.
audit_log: Optional :class:`AuditLog` for recording decisions.
"""
def __init__(
self,
evaluator: PolicyEvaluator,
audit_log: AuditLog | None = None,
) -> None:
self.evaluator = evaluator
self.audit_log = audit_log
async def process(
self,
context: AgentContext,
call_next: Callable[[], Awaitable[None]],
) -> None:
"""Evaluate governance policy before agent execution."""
agent_name = getattr(context.agent, "name", "unknown")
# Extract the last user message text (handle empty conversations).
last_message_text = ""
messages: list[Any] = getattr(context, "messages", None) or []
if messages:
last_msg = messages[-1]
# Message.text is the MAF accessor; fall back to str()
last_message_text = (
getattr(last_msg, "text", None) or str(last_msg)
)
# Build context dict for the policy evaluator.
eval_context: dict[str, Any] = {
"agent": agent_name,
"message": last_message_text,
"timestamp": time.time(),
"stream": getattr(context, "stream", False),
"message_count": len(messages),
}
decision: PolicyDecision = self.evaluator.evaluate(eval_context)
# Persist the decision in the MAF metadata for downstream middleware.
metadata: dict[str, Any] = getattr(context, "metadata", {})
metadata["governance_decision"] = decision
if not decision.allowed:
logger.info(
"Policy DENY for agent '%s': %s (rule=%s)",
agent_name,
decision.reason,
decision.matched_rule,
)
# Set a user-visible response explaining the denial.
context.result = AgentResponse(
messages=[
Message(
"assistant",
[f"⛔ Policy violation: {decision.reason}"],
)
]
)
if self.audit_log:
self.audit_log.log(
event_type="policy_violation",
agent_did=agent_name,
action="deny",
data={
"reason": decision.reason,
"matched_rule": decision.matched_rule,
"message_preview": last_message_text[:200],
},
outcome="denied",
policy_decision=decision.action,
)
raise MiddlewareTermination(decision.reason)
# Policy allowed — log and continue the pipeline.
logger.debug(
"Policy ALLOW for agent '%s' (rule=%s)",
agent_name,
decision.matched_rule,
)
if self.audit_log:
self.audit_log.log(
event_type="policy_evaluation",
agent_did=agent_name,
action="allow",
data={
"matched_rule": decision.matched_rule,
"message_preview": last_message_text[:200],
},
outcome="success",
policy_decision=decision.action,
)
await call_next()
# ═══════════════════════════════════════════════════════════════════════════
# 2. CapabilityGuardMiddleware
# ═══════════════════════════════════════════════════════════════════════════
class CapabilityGuardMiddleware(FunctionMiddleware):
"""FunctionMiddleware that enforces tool allow/deny lists.
Each tool invocation is checked against explicit ``allowed_tools``
and ``denied_tools`` lists. If a tool is not permitted, the
function result is set to an error string and
``MiddlewareTermination`` is raised.
When both ``allowed_tools`` and ``denied_tools`` are provided,
``denied_tools`` takes precedence (a tool in both lists is denied).
Args:
allowed_tools: Whitelist of permitted tool names. If ``None``,
all tools are allowed unless explicitly denied.
denied_tools: Blacklist of forbidden tool names.
audit_log: Optional :class:`AuditLog` for recording tool
invocations.
"""
def __init__(
self,
allowed_tools: list[str] | None = None,
denied_tools: list[str] | None = None,
audit_log: AuditLog | None = None,
) -> None:
self.allowed_tools = allowed_tools
self.denied_tools = denied_tools
self.audit_log = audit_log
def _is_denied(self, tool_name: str) -> bool:
"""Return ``True`` if *tool_name* should be blocked."""
# Explicit deny list takes precedence.
if self.denied_tools and tool_name in self.denied_tools:
return True
# If an allow list is set, anything not in it is denied.
if self.allowed_tools is not None and tool_name not in self.allowed_tools:
return True
return False
async def process(
self,
context: FunctionInvocationContext,
call_next: Callable[[], Awaitable[None]],
) -> None:
"""Guard tool invocations against capability policy."""
func_name = getattr(
getattr(context, "function", None), "name", "unknown"
)
if self._is_denied(func_name):
logger.info("Capability DENY: tool '%s' blocked by policy", func_name)
context.result = (
f"⛔ Tool '{func_name}' is not permitted by governance policy"
)
if self.audit_log:
self.audit_log.log(
event_type="tool_blocked",
agent_did="capability-guard",
action="deny",
resource=func_name,
data={"tool": func_name},
outcome="denied",
)
raise MiddlewareTermination(
f"Tool '{func_name}' is not permitted by governance policy"
)
# Tool is allowed — log start, execute, log completion.
if self.audit_log:
self.audit_log.log(
event_type="tool_invocation",
agent_did="capability-guard",
action="start",
resource=func_name,
data={"tool": func_name},
outcome="success",
)
logger.debug("Capability ALLOW: invoking tool '%s'", func_name)
await call_next()
# Log completion with a truncated result summary.
result_summary = str(getattr(context, "result", ""))[:500]
if self.audit_log:
self.audit_log.log(
event_type="tool_invocation",
agent_did="capability-guard",
action="complete",
resource=func_name,
data={
"tool": func_name,
"result_preview": result_summary,
},
outcome="success",
)
# ═══════════════════════════════════════════════════════════════════════════
# 3. AuditTrailMiddleware
# ═══════════════════════════════════════════════════════════════════════════
class AuditTrailMiddleware(AgentMiddleware):
"""AgentMiddleware that records tamper-proof audit entries.
Wraps every agent invocation with pre- and post-execution audit
entries, capturing timing information and the execution outcome.
The resulting :class:`AuditEntry` ID is stored in
``context.metadata["audit_entry_id"]`` for downstream correlation.
Args:
audit_log: :class:`AuditLog` instance for recording entries.
agent_did: Optional decentralised identifier for the agent.
Defaults to the MAF agent name when not provided.
"""
def __init__(
self,
audit_log: AuditLog,
agent_did: str | None = None,
) -> None:
self.audit_log = audit_log
self.agent_did = agent_did
async def process(
self,
context: AgentContext,
call_next: Callable[[], Awaitable[None]],
) -> None:
"""Record pre/post execution audit entries with timing."""
agent_name = getattr(context.agent, "name", "unknown")
did = self.agent_did or agent_name
messages: list[Any] = getattr(context, "messages", None) or []
metadata: dict[str, Any] = getattr(context, "metadata", {})
# Pre-execution audit entry.
start_entry: AuditEntry = self.audit_log.log(
event_type="agent_invocation",
agent_did=did,
action="start",
data={
"agent_name": agent_name,
"message_count": len(messages),
"stream": getattr(context, "stream", False),
},
outcome="success",
)
# Store the entry ID for downstream middleware / callers.
metadata["audit_entry_id"] = start_entry.entry_id
start_time = time.time()
outcome = "success"
error_detail: str | None = None
try:
await call_next()
except Exception as exc:
outcome = "error"
error_detail = f"{type(exc).__name__}: {exc}"
raise
finally:
elapsed = time.time() - start_time
# Post-execution audit entry.
self.audit_log.log(
event_type="agent_invocation",
agent_did=did,
action="complete",
data={
"agent_name": agent_name,
"elapsed_seconds": round(elapsed, 4),
"start_entry_id": start_entry.entry_id,
**({"error": error_detail} if error_detail else {}),
},
outcome=outcome,
)
logger.debug(
"Audit: agent '%s' completed in %.3fs (outcome=%s)",
agent_name,
elapsed,
outcome,
)
# ═══════════════════════════════════════════════════════════════════════════
# 4. RogueDetectionMiddleware
# ═══════════════════════════════════════════════════════════════════════════
class RogueDetectionMiddleware(FunctionMiddleware):
"""FunctionMiddleware that detects rogue agent behaviour.
Feeds every tool invocation into a
:class:`~agent_sre.anomaly.RogueAgentDetector` and checks the
resulting risk assessment. High-risk agents are blocked with a
``MiddlewareTermination``; medium-risk invocations proceed with a
warning logged to the audit trail.
Args:
detector: Pre-configured :class:`RogueAgentDetector`.
agent_id: Identifier for the agent being monitored.
capability_profile: Optional dict mapping ``"allowed_tools"``
to a list of expected tool names. Registered with the
detector on construction.
audit_log: Optional :class:`AuditLog` for recording detections.
"""
def __init__(
self,
detector: RogueAgentDetector,
agent_id: str,
capability_profile: dict[str, Any] | None = None,
audit_log: AuditLog | None = None,
) -> None:
self.detector = detector
self.agent_id = agent_id
self.audit_log = audit_log
# Register the expected capability profile if provided.
if capability_profile and "allowed_tools" in capability_profile:
self.detector.register_capability_profile(
agent_id,
capability_profile["allowed_tools"],
)
async def process(
self,
context: FunctionInvocationContext,
call_next: Callable[[], Awaitable[None]],
) -> None:
"""Assess rogue risk before allowing tool execution."""
func_name = getattr(
getattr(context, "function", None), "name", "unknown"
)
now = time.time()
# Feed the observation into the detector's analyzers.
self.detector.record_action(
agent_id=self.agent_id,
action=func_name,
tool_name=func_name,
timestamp=now,
)
# Produce a composite risk assessment.
assessment = self.detector.assess(self.agent_id, timestamp=now)
if assessment.quarantine_recommended:
logger.warning(
"Rogue QUARANTINE for agent '%s': risk=%s score=%.2f",
self.agent_id,
assessment.risk_level.value,
assessment.composite_score,
)
context.result = (
f"⛔ Agent '{self.agent_id}' has been quarantined due to "
f"anomalous behaviour (risk={assessment.risk_level.value}, "
f"score={assessment.composite_score:.2f})"
)
if self.audit_log:
self.audit_log.log(
event_type="rogue_detection",
agent_did=self.agent_id,
action="quarantine",
resource=func_name,
data=assessment.to_dict(),
outcome="denied",
)
raise MiddlewareTermination(
f"Agent '{self.agent_id}' quarantined: "
f"risk={assessment.risk_level.value}"
)
# Log a warning for MEDIUM or above but allow execution.
if assessment.risk_level in (RiskLevel.MEDIUM, RiskLevel.HIGH):
logger.warning(
"Rogue WARNING for agent '%s': risk=%s score=%.2f "
"(tool=%s)",
self.agent_id,
assessment.risk_level.value,
assessment.composite_score,
func_name,
)
if self.audit_log:
self.audit_log.log(
event_type="rogue_detection",
agent_did=self.agent_id,
action="warning",
resource=func_name,
data=assessment.to_dict(),
outcome="success",
)
await call_next()
# ═══════════════════════════════════════════════════════════════════════════
# Convenience factory
# ═══════════════════════════════════════════════════════════════════════════
def create_governance_middleware(
policy_directory: str | Path | None = None,
allowed_tools: list[str] | None = None,
denied_tools: list[str] | None = None,
agent_id: str = "default-agent",
enable_rogue_detection: bool = True,
audit_log: AuditLog | None = None,
) -> list:
"""Create a complete governance middleware stack for a MAF agent.
Assembles and returns an ordered list of middleware instances ready
to pass directly to a MAF ``Agent(middleware=...)`` constructor.
The stack is built bottom-up:
1. :class:`AuditTrailMiddleware` (if *audit_log* provided)
2. :class:`GovernancePolicyMiddleware` (if *policy_directory* provided)
3. :class:`CapabilityGuardMiddleware` (if allow/deny lists provided)
4. :class:`RogueDetectionMiddleware` (if *enable_rogue_detection*)
Args:
policy_directory: Path to a directory of YAML policy files.
When provided, a :class:`PolicyEvaluator` is created and
loaded with all ``*.yaml`` / ``*.yml`` files found.
allowed_tools: Whitelist of permitted tool names.
denied_tools: Blacklist of forbidden tool names.
agent_id: Identifier for the agent (used by audit and rogue
detection).
enable_rogue_detection: Whether to include the
:class:`RogueDetectionMiddleware`.
audit_log: Shared :class:`AuditLog` instance. When ``None``,
a fresh in-memory log is created if any auditing middleware
is needed.
Returns:
List of middleware instances in recommended execution order.
Example::
from agent_framework import Agent
from agent_os.integrations.maf_adapter import create_governance_middleware
stack = create_governance_middleware(
policy_directory="policies/",
allowed_tools=["search", "read_file"],
agent_id="my-researcher",
)
agent = Agent(name="researcher", middleware=stack)
"""
stack: list[Any] = []
# Determine whether we need an audit log for any layer.
needs_audit = (
audit_log is not None
or policy_directory is not None
or allowed_tools is not None
or denied_tools is not None
or enable_rogue_detection
)
if needs_audit and audit_log is None:
audit_log = AuditLog()
# 1. Audit trail (outermost — captures everything).
if audit_log is not None:
stack.append(AuditTrailMiddleware(audit_log=audit_log, agent_did=agent_id))
# 2. Governance policy enforcement.
if policy_directory is not None:
evaluator = PolicyEvaluator()
evaluator.load_policies(policy_directory)
stack.append(
GovernancePolicyMiddleware(evaluator=evaluator, audit_log=audit_log)
)
# 3. Capability guard.
if allowed_tools is not None or denied_tools is not None:
stack.append(
CapabilityGuardMiddleware(
allowed_tools=allowed_tools,
denied_tools=denied_tools,
audit_log=audit_log,
)
)
# 4. Rogue detection (innermost — closest to actual tool execution).
if enable_rogue_detection:
detector = RogueAgentDetector(config=RogueDetectorConfig())
capability_profile: dict[str, Any] | None = None
if allowed_tools:
capability_profile = {"allowed_tools": allowed_tools}
stack.append(
RogueDetectionMiddleware(
detector=detector,
agent_id=agent_id,
capability_profile=capability_profile,
audit_log=audit_log,
)
)
return stack