forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrewai_governed.py
More file actions
70 lines (56 loc) · 3.19 KB
/
crewai_governed.py
File metadata and controls
70 lines (56 loc) · 3.19 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
CrewAI Crew with Governance Middleware — Quickstart
====================================================
pip install agent-governance-toolkit[full] crewai
python examples/quickstart/crewai_governed.py
Shows a real policy violation being caught, then a compliant run succeeding,
with a printed audit trail.
"""
from __future__ import annotations
import sys
from datetime import datetime
from pathlib import Path
_REPO_ROOT = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(_REPO_ROOT / "packages" / "agent-os" / "src"))
from agent_os.integrations import CrewAIKernel
from agent_os.integrations.base import GovernancePolicy, PolicyViolationError
# ── 1. Define a governance policy ─────────────────────────────────────────
policy = GovernancePolicy(
name="crewai-demo-policy",
blocked_patterns=["DROP TABLE", "rm -rf"], # dangerous SQL/shell commands
max_tool_calls=3,
require_human_approval=False,
)
kernel = CrewAIKernel(policy=policy)
ctx = kernel.create_context("crewai-demo-crew")
audit: list[dict] = []
print("=" * 60)
print(" CrewAI Crew — Governance Quickstart")
print("=" * 60)
# ── 2. Policy violation: blocked content pattern ───────────────────────────
print("\n[1] Crew task with a dangerous SQL injection pattern …")
allowed, reason = kernel.pre_execute(ctx, "Execute: DROP TABLE users")
if not allowed:
print(f" 🚫 BLOCKED — {reason}")
audit.append({"ts": datetime.now().isoformat(), "task": "DROP TABLE users", "status": "BLOCKED"})
# ── 3. Policy violation: call budget exhausted ────────────────────────────
print("\n[2] Exhausting the call budget …")
ctx.call_count = policy.max_tool_calls # simulate budget consumed
allowed, reason = kernel.pre_execute(ctx, "Summarise quarterly reports")
if not allowed:
print(f" 🚫 BLOCKED — {reason}")
audit.append({"ts": datetime.now().isoformat(), "task": "summarise reports", "status": "BLOCKED"})
ctx.call_count = 0 # reset for next check
# ── 4. Compliant task succeeds ────────────────────────────────────────────
print("\n[3] Safe crew task passes policy check …")
allowed, reason = kernel.pre_execute(ctx, "Summarise the quarterly financial reports")
if allowed:
print(" ✅ ALLOWED — policy check passed")
audit.append({"ts": datetime.now().isoformat(), "task": "summarise reports", "status": "ALLOWED"})
# ── 5. Audit trail ────────────────────────────────────────────────────────
print("\n── Audit Trail ──────────────────────────────────────────")
for i, entry in enumerate(audit, 1):
print(f" [{i}] {entry['ts']} task={entry['task']!r} status={entry['status']}")
print("\n🎉 CrewAI governance demo complete.")