-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcopilot_governed.py
More file actions
322 lines (252 loc) · 10.1 KB
/
copilot_governed.py
File metadata and controls
322 lines (252 loc) · 10.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
#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
GitHub Copilot under Agent-OS Governance - Real World Example
This example shows how to run an AI coding assistant (like GitHub Copilot)
under Agent-OS governance, allowing all file system operations without
prompting but logging everything for audit.
Usage:
python copilot_governed.py
"""
import asyncio
import os
import sys
import tempfile
from pathlib import Path
from datetime import datetime
# Add the control-plane module to path
sys.path.insert(0, str(Path(__file__).parent.parent / "modules" / "control-plane" / "src"))
from agent_control_plane.kernel_space import KernelSpace, SyscallRequest, SyscallType
from agent_control_plane.policy_engine import PolicyEngine
from agent_control_plane.flight_recorder import FlightRecorder
# =============================================================================
# REAL FILE SYSTEM EXECUTORS (Not Mocks!)
# =============================================================================
def real_file_read(path: str) -> str:
"""Actually read a file from disk."""
with open(path, 'r', encoding='utf-8') as f:
return f.read()
def real_file_write(path: str, content: str) -> int:
"""Actually write content to a file."""
# Create parent directories if needed
Path(path).parent.mkdir(parents=True, exist_ok=True)
with open(path, 'w', encoding='utf-8') as f:
bytes_written = f.write(content)
return bytes_written
def real_file_delete(path: str) -> bool:
"""Actually delete a file."""
if os.path.exists(path):
os.remove(path)
return True
return False
def real_file_list(path: str) -> list:
"""Actually list directory contents."""
if os.path.isdir(path):
return os.listdir(path)
return []
def real_file_exists(path: str) -> bool:
"""Check if file exists."""
return os.path.exists(path)
def real_file_mkdir(path: str) -> bool:
"""Create directory."""
Path(path).mkdir(parents=True, exist_ok=True)
return True
# =============================================================================
# COPILOT GOVERNANCE SETUP
# =============================================================================
def create_copilot_governance(workspace_path: str, audit_db: str = "copilot_audit.db"):
"""
Create a governance setup for GitHub Copilot.
This allows ALL file operations within the workspace without prompting,
but logs everything for audit purposes.
Args:
workspace_path: The root directory where Copilot can operate
audit_db: Path to SQLite audit database
Returns:
Tuple of (kernel, agent_context)
"""
# 1. Create Policy Engine - Allow all file operations for copilot
policy = PolicyEngine()
# Allow all file operations for the copilot agent
policy.add_constraint("github-copilot", [
"file_read",
"file_write",
"file_delete",
"file_list",
"file_exists",
"file_mkdir",
# Add more as needed
"code_search",
"code_analyze",
])
# 2. Create Flight Recorder for audit trail
recorder = FlightRecorder(
db_path=audit_db,
enable_batching=False, # Immediate writes for demo
)
# 3. Create Kernel with governance
kernel = KernelSpace(
policy_engine=policy,
flight_recorder=recorder,
)
# 4. Register REAL file system executors (not mocks!)
kernel.register_tool("file_read", real_file_read)
kernel.register_tool("file_write", real_file_write)
kernel.register_tool("file_delete", real_file_delete)
kernel.register_tool("file_list", real_file_list)
kernel.register_tool("file_exists", real_file_exists)
kernel.register_tool("file_mkdir", real_file_mkdir)
# 5. Create agent context
ctx = kernel.create_agent_context("github-copilot")
print(f"✓ Governance initialized for workspace: {workspace_path}")
print(f"✓ Audit log: {audit_db}")
print(f"✓ Registered tools: {kernel.list_tools()}")
return kernel, ctx, recorder
# =============================================================================
# GOVERNED OPERATIONS
# =============================================================================
async def governed_read(kernel, ctx, path: str) -> str:
"""Read a file through the governed kernel."""
request = SyscallRequest(
syscall=SyscallType.SYS_EXEC,
args={"tool": "file_read", "args": {"path": path}},
)
result = await kernel.syscall(request, ctx)
if result.success:
return result.return_value
else:
raise Exception(f"Read failed: {result.error_message}")
async def governed_write(kernel, ctx, path: str, content: str) -> int:
"""Write a file through the governed kernel."""
request = SyscallRequest(
syscall=SyscallType.SYS_EXEC,
args={"tool": "file_write", "args": {"path": path, "content": content}},
)
result = await kernel.syscall(request, ctx)
if result.success:
return result.return_value
else:
raise Exception(f"Write failed: {result.error_message}")
async def governed_list(kernel, ctx, path: str) -> list:
"""List directory through the governed kernel."""
request = SyscallRequest(
syscall=SyscallType.SYS_EXEC,
args={"tool": "file_list", "args": {"path": path}},
)
result = await kernel.syscall(request, ctx)
if result.success:
return result.return_value
else:
raise Exception(f"List failed: {result.error_message}")
async def governed_delete(kernel, ctx, path: str) -> bool:
"""Delete a file through the governed kernel."""
request = SyscallRequest(
syscall=SyscallType.SYS_EXEC,
args={"tool": "file_delete", "args": {"path": path}},
)
result = await kernel.syscall(request, ctx)
if result.success:
return result.return_value
else:
raise Exception(f"Delete failed: {result.error_message}")
# =============================================================================
# DEMO: Simulate Copilot Operations
# =============================================================================
async def demo_copilot_session(workspace: str):
"""
Demonstrate a Copilot session with full governance.
All operations go through the kernel:
- Policy is checked (allow-list)
- Actions are logged to FlightRecorder
- Real file operations happen
"""
print("\n" + "="*60)
print("GitHub Copilot under Agent-OS Governance - LIVE DEMO")
print("="*60 + "\n")
# Setup governance
audit_db = os.path.join(workspace, "audit.db")
kernel, ctx, recorder = create_copilot_governance(workspace, audit_db)
print("\n--- Starting Copilot Session ---\n")
# 1. Create a new Python file
py_file = os.path.join(workspace, "hello.py")
code = '''#!/usr/bin/env python3
"""Generated by GitHub Copilot under Agent-OS governance."""
def greet(name: str) -> str:
"""Return a greeting message."""
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("World"))
'''
print(f"1. Writing {py_file}...")
bytes_written = await governed_write(kernel, ctx, py_file, code)
print(f" ✓ Wrote {bytes_written} bytes")
# 2. Read it back
print(f"\n2. Reading {py_file}...")
content = await governed_read(kernel, ctx, py_file)
print(f" ✓ Read {len(content)} characters")
print(f" Preview: {content[:50]}...")
# 3. Create a README
readme = os.path.join(workspace, "README.md")
readme_content = f"""# Demo Project
Generated by GitHub Copilot under Agent-OS governance.
## Files
- hello.py - A simple greeting script
## Governance
All file operations were:
- Logged to FlightRecorder (audit.db)
- Checked against PolicyEngine
- Executed through KernelSpace
Generated at: {datetime.now().isoformat()}
"""
print(f"\n3. Writing {readme}...")
await governed_write(kernel, ctx, readme, readme_content)
print(" ✓ README.md created")
# 4. List the workspace
print(f"\n4. Listing {workspace}...")
files = await governed_list(kernel, ctx, workspace)
print(f" ✓ Found {len(files)} items: {files}")
# 5. Show audit log
print("\n--- Audit Log (FlightRecorder) ---\n")
recorder.flush()
logs = recorder.query_logs(agent_id="github-copilot", limit=10)
for log in logs:
verdict = log.get('policy_verdict', 'unknown')
tool = log.get('tool_name', 'unknown')
emoji = "✓" if verdict == "allowed" else "✗"
print(f" {emoji} [{verdict}] {tool}")
# 6. Verify integrity
print("\n--- Integrity Check (Hash Chain) ---\n")
integrity = recorder.verify_integrity()
if integrity.get("valid"):
print(f" ✓ Audit log integrity verified ({integrity.get('total_entries')} entries)")
else:
print(f" ✗ Integrity check failed: {integrity.get('error')}")
# 7. Show statistics
print("\n--- Kernel Metrics ---\n")
metrics = kernel.metrics
print(f" Syscalls: {metrics.syscall_count}")
print(f" Policy checks: {metrics.policy_checks}")
print(f" Policy violations: {metrics.policy_violations}")
print(f" Active agents: {metrics.active_agents}")
# Cleanup - close recorder
recorder.close()
print("\n" + "="*60)
print("Demo complete! All operations were governed and logged.")
print("="*60 + "\n")
return True
# =============================================================================
# MAIN
# =============================================================================
async def main():
# Create a temporary workspace
with tempfile.TemporaryDirectory(prefix="copilot_governed_") as workspace:
print(f"Workspace: {workspace}")
success = await demo_copilot_session(workspace)
if success:
print("\n✓ All tests passed! Agent-OS governance is REAL and WORKING.")
else:
print("\n✗ Some tests failed.")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())