forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrewai_safe_mode.py
More file actions
435 lines (363 loc) · 15.7 KB
/
crewai_safe_mode.py
File metadata and controls
435 lines (363 loc) · 15.7 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
#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
🛡️ Agent OS Safe Mode Demo for CrewAI
=======================================
This script demonstrates Agent OS blocking dangerous operations
from a CrewAI agent in real-time.
What happens:
1. A CrewAI agent tries to delete files
2. Agent OS intercepts the operation
3. The kernel BLOCKS the action and logs the violation
4. Your data stays safe ✓
Run:
pip install agent-os-kernel crewai
python crewai_safe_mode.py
Perfect for recording a 15-second GIF showing "Access Denied"!
"""
import os
import sys
from datetime import datetime
from typing import Any
# ============================================================================
# ANSI Colors for Terminal Output (the visual "wow" factor)
# ============================================================================
class Colors:
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'
BOLD = '\033[1m'
RESET = '\033[0m'
def print_banner():
"""Print the Agent OS banner"""
print(f"""
{Colors.CYAN}{Colors.BOLD}
╔═══════════════════════════════════════════════════════════╗
║ ║
║ 🛡️ AGENT OS - Safe Mode Demo for CrewAI ║
║ ║
║ Kernel-level safety for autonomous AI agents ║
║ ║
╚═══════════════════════════════════════════════════════════╝
{Colors.RESET}""")
def print_section(title: str):
print(f"\n{Colors.BLUE}{Colors.BOLD}{'─' * 60}{Colors.RESET}")
print(f"{Colors.BLUE}{Colors.BOLD} {title}{Colors.RESET}")
print(f"{Colors.BLUE}{Colors.BOLD}{'─' * 60}{Colors.RESET}\n")
def print_blocked(action: str, reason: str):
"""Print a blocked action with dramatic effect"""
print(f"""
{Colors.RED}{Colors.BOLD}
╔═══════════════════════════════════════════════════════════╗
║ 🚫 ACCESS DENIED - POLICY VIOLATION ║
╠═══════════════════════════════════════════════════════════╣
║ ║
║ Action: {action:<47} ║
║ Reason: {reason:<47} ║
║ Status: BLOCKED BY KERNEL ║
║ ║
╚═══════════════════════════════════════════════════════════╝
{Colors.RESET}""")
def print_allowed(action: str):
"""Print an allowed action"""
print(f"{Colors.GREEN} ✅ ALLOWED:{Colors.RESET} {action}")
def print_log(level: str, message: str):
"""Print a kernel log message"""
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
color = {
"INFO": Colors.CYAN,
"WARN": Colors.YELLOW,
"ERROR": Colors.RED,
"BLOCK": Colors.RED + Colors.BOLD,
}.get(level, Colors.WHITE)
print(f" {Colors.WHITE}[{timestamp}]{Colors.RESET} {color}[{level}]{Colors.RESET} {message}")
# ============================================================================
# Agent OS Kernel - The Safety Layer
# ============================================================================
class SafetyPolicy:
"""
Defines what operations are allowed/blocked.
This is the "kernel" that protects your system.
"""
# Dangerous file patterns
BLOCKED_FILE_OPS = [
"rm -rf",
"rmdir",
"unlink",
"shutil.rmtree",
"os.remove",
"os.unlink",
"pathlib.Path.unlink",
"DELETE FROM",
"DROP TABLE",
"TRUNCATE",
]
# Dangerous commands
BLOCKED_COMMANDS = [
"sudo",
"chmod 777",
"curl | bash",
"wget | sh",
"eval(",
"exec(",
"__import__",
]
# Restricted paths
RESTRICTED_PATHS = [
"/",
"/etc",
"/usr",
"/var",
"/home",
"/root",
"C:\\Windows",
"C:\\Program Files",
"~",
]
@classmethod
def check_action(cls, action: str, params: dict = None) -> tuple[bool, str]:
"""
Check if an action is allowed.
Returns (allowed, reason).
"""
action_lower = action.lower()
params = params or {}
# Check for blocked file operations
for blocked in cls.BLOCKED_FILE_OPS:
if blocked.lower() in action_lower:
return False, f"Destructive file operation detected: {blocked}"
# Check for blocked commands
for blocked in cls.BLOCKED_COMMANDS:
if blocked.lower() in action_lower:
return False, f"Dangerous command detected: {blocked}"
# Check for restricted paths
path = params.get("path", "") or params.get("file", "") or ""
for restricted in cls.RESTRICTED_PATHS:
if path.startswith(restricted):
return False, f"Access to restricted path: {restricted}"
return True, "Action permitted"
class AgentOSKernel:
"""
The Agent OS Kernel - intercepts all agent actions.
This is a simplified version showing the core concept:
- Every action goes through the kernel
- The kernel checks against policies
- Violations are BLOCKED, not just logged
"""
def __init__(self, agent_id: str = "crew-agent"):
self.agent_id = agent_id
self.audit_log = []
self.blocked_count = 0
self.allowed_count = 0
print_log("INFO", f"Kernel initialized for agent: {agent_id}")
print_log("INFO", "Safety policies loaded: file_ops, commands, paths")
def execute(self, action: str, params: dict = None) -> Any:
"""
Execute an action through the kernel.
The kernel:
1. Intercepts the action
2. Checks against policies
3. Blocks or allows
4. Logs everything
"""
params = params or {}
# Log the attempt
print_log("INFO", f"Agent requested: {action}")
# Check policy
allowed, reason = SafetyPolicy.check_action(action, params)
# Record in audit log
self.audit_log.append({
"timestamp": datetime.now().isoformat(),
"agent_id": self.agent_id,
"action": action,
"params": params,
"allowed": allowed,
"reason": reason,
})
if not allowed:
self.blocked_count += 1
print_log("BLOCK", f"DENIED: {reason}")
print_blocked(action[:45], reason[:45])
raise PermissionError(f"🛡️ Agent OS: {reason}")
self.allowed_count += 1
print_allowed(action)
return f"Executed: {action}"
def get_stats(self) -> dict:
"""Get kernel statistics"""
return {
"agent_id": self.agent_id,
"total_requests": len(self.audit_log),
"allowed": self.allowed_count,
"blocked": self.blocked_count,
"violation_rate": f"{(self.blocked_count / max(1, len(self.audit_log))) * 100:.1f}%"
}
# ============================================================================
# Simulated CrewAI Agent (for demo without actual CrewAI dependency)
# ============================================================================
class SimulatedCrewAIAgent:
"""
Simulates a CrewAI agent that tries to perform dangerous operations.
In a real scenario, this would be an actual CrewAI agent.
The Agent OS kernel wraps it the same way.
"""
def __init__(self, name: str, role: str, kernel: AgentOSKernel):
self.name = name
self.role = role
self.kernel = kernel
print_log("INFO", f"Created agent: {name} ({role})")
def execute_task(self, task: str) -> str:
"""
Execute a task - ALL actions go through the kernel.
"""
print(f"\n{Colors.YELLOW} 🤖 Agent '{self.name}' executing: {task}{Colors.RESET}")
# Simulate the agent "thinking" and choosing actions
if "clean" in task.lower() or "delete" in task.lower():
# Agent decides to delete files (DANGEROUS!)
actions = [
("os.remove('/tmp/cache/*')", {"path": "/tmp/cache"}),
("shutil.rmtree('/var/log/old')", {"path": "/var/log/old"}),
("rm -rf /home/user/Downloads/*", {"path": "/home/user/Downloads"}),
]
elif "analyze" in task.lower():
# Agent decides to read data (SAFE)
actions = [
("read_file('data.csv')", {"path": "data.csv"}),
("pandas.read_csv('report.csv')", {"path": "report.csv"}),
]
elif "deploy" in task.lower():
# Agent tries dangerous deploy commands
actions = [
("sudo systemctl restart app", {}),
("chmod 777 /var/www", {"path": "/var/www"}),
]
else:
# Default safe actions
actions = [
("print('Hello from agent')", {}),
("calculate_metrics()", {}),
]
results = []
for action, params in actions:
try:
result = self.kernel.execute(action, params)
results.append(result)
except PermissionError as e:
results.append(f"BLOCKED: {e}")
return "\n".join(results)
class SimulatedCrew:
"""
Simulates a CrewAI Crew with multiple agents.
"""
def __init__(self, agents: list, kernel: AgentOSKernel):
self.agents = agents
self.kernel = kernel
def kickoff(self, inputs: dict = None) -> str:
"""Run the crew - simulates CrewAI's kickoff()"""
print_section("CREW KICKOFF")
print_log("INFO", f"Starting crew with {len(self.agents)} agents")
results = []
for agent in self.agents:
task = inputs.get("task", "default task") if inputs else "default task"
try:
result = agent.execute_task(task)
results.append(f"{agent.name}: {result}")
except Exception as e:
results.append(f"{agent.name}: ERROR - {e}")
return "\n\n".join(results)
# ============================================================================
# Main Demo
# ============================================================================
def run_demo():
"""
Run the complete demo showing Agent OS protecting against dangerous ops.
"""
print_banner()
# Initialize the kernel
print_section("INITIALIZING AGENT OS KERNEL")
kernel = AgentOSKernel(agent_id="demo-crew")
# Create agents (simulating CrewAI)
print_section("CREATING CREWAI AGENTS")
cleanup_agent = SimulatedCrewAIAgent(
name="Cleanup Bot",
role="System Cleaner",
kernel=kernel
)
analyst_agent = SimulatedCrewAIAgent(
name="Data Analyst",
role="Analytics",
kernel=kernel
)
deploy_agent = SimulatedCrewAIAgent(
name="Deploy Bot",
role="DevOps",
kernel=kernel
)
# Create the crew
crew = SimulatedCrew(
agents=[cleanup_agent, analyst_agent, deploy_agent],
kernel=kernel
)
# ========================================================================
# DEMO 1: Cleanup task (BLOCKED - tries to delete files)
# ========================================================================
print_section("DEMO 1: CLEANUP TASK (Agent tries to delete files)")
print(f"{Colors.MAGENTA} Task: 'Clean up old files and free disk space'{Colors.RESET}")
try:
result = cleanup_agent.execute_task("Clean up old cache files")
except Exception as e:
print(f"\n{Colors.RED} Crew task failed safely: {e}{Colors.RESET}")
# ========================================================================
# DEMO 2: Analysis task (ALLOWED - safe read operations)
# ========================================================================
print_section("DEMO 2: ANALYSIS TASK (Safe read operations)")
print(f"{Colors.MAGENTA} Task: 'Analyze sales data'{Colors.RESET}")
try:
result = analyst_agent.execute_task("Analyze Q4 sales data")
print(f"\n{Colors.GREEN} ✅ Task completed successfully!{Colors.RESET}")
except Exception as e:
print(f"\n{Colors.RED} Task failed: {e}{Colors.RESET}")
# ========================================================================
# DEMO 3: Deploy task (BLOCKED - tries sudo/chmod)
# ========================================================================
print_section("DEMO 3: DEPLOY TASK (Agent tries privilege escalation)")
print(f"{Colors.MAGENTA} Task: 'Deploy new version to production'{Colors.RESET}")
try:
result = deploy_agent.execute_task("Deploy app to production")
except Exception as e:
print(f"\n{Colors.RED} Crew task failed safely: {e}{Colors.RESET}")
# ========================================================================
# Final Statistics
# ========================================================================
print_section("KERNEL STATISTICS")
stats = kernel.get_stats()
print(f"""
{Colors.CYAN} 📊 Agent OS Kernel Report
─────────────────────────────────────────
Agent ID: {stats['agent_id']}
Total Requests: {stats['total_requests']}
✅ Allowed: {stats['allowed']}
🚫 Blocked: {stats['blocked']}
Violation Rate: {stats['violation_rate']}
─────────────────────────────────────────{Colors.RESET}
""")
print(f"""
{Colors.GREEN}{Colors.BOLD}
╔═══════════════════════════════════════════════════════════╗
║ ║
║ ✅ DEMO COMPLETE - Your system stayed safe! ║
║ ║
║ Agent OS blocked {stats['blocked']} dangerous operations ║
║ while allowing {stats['allowed']} safe ones. ║
║ ║
║ This is kernel-level safety, not prompt engineering. ║
║ ║
╚═══════════════════════════════════════════════════════════╝
{Colors.RESET}""")
print(f"\n{Colors.CYAN} 🔗 Learn more: https://github.com/microsoft/agent-governance-toolkit{Colors.RESET}\n")
if __name__ == "__main__":
run_demo()