forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhibernation_and_time_travel_demo.py
More file actions
408 lines (339 loc) · 15.1 KB
/
hibernation_and_time_travel_demo.py
File metadata and controls
408 lines (339 loc) · 15.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Example: Agent Hibernation and Time-Travel Debugging
This demonstrates the two new features:
1. Agent Hibernation - "Serverless Agents" that save state to disk when idle
2. Time-Travel Debugging - Replay agent history for debugging and analysis
"""
from agent_control_plane import (
AgentControlPlane,
create_standard_agent,
HibernationConfig,
HibernationFormat,
TimeTravelConfig,
)
from agent_control_plane.agent_kernel import ActionType
from agent_control_plane.flight_recorder import FlightRecorder
import time
from datetime import datetime
def example_agent_hibernation():
"""
Demonstrate Agent Hibernation feature.
Problem: Agents sitting idle in memory cost money or RAM.
Solution: Serialize agent state to disk and wake only when needed.
Result: "Serverless Agents" - Scale by Subtraction.
"""
print("=" * 60)
print("EXAMPLE 1: Agent Hibernation (Serverless Agents)")
print("=" * 60)
print()
# Create control plane with hibernation enabled
hibernation_config = HibernationConfig(
enabled=True,
idle_timeout_seconds=5, # Short timeout for demo
storage_path="/tmp/agent_hibernation_demo",
format=HibernationFormat.JSON # Use JSON for human-readable state files
)
control_plane = AgentControlPlane(
enable_hibernation=True,
hibernation_config=hibernation_config
)
# Create an agent
agent_context = create_standard_agent(control_plane, "demo-agent-001")
print(f"✓ Created agent: {agent_context.agent_id}")
print(f" Session ID: {agent_context.session_id}")
print()
# Execute some actions
print("Executing actions...")
result1 = control_plane.execute_action(
agent_context,
ActionType.FILE_READ,
{"path": "/data/sample.txt"}
)
print(f" • File read: {result1['success']}")
result2 = control_plane.execute_action(
agent_context,
ActionType.API_CALL,
{"url": "https://api.example.com/data", "method": "GET"}
)
print(f" • API call: {result2['success']}")
print()
# Record activity
control_plane.record_agent_activity(agent_context.agent_id)
# Wait for agent to become idle
print(f"Waiting {hibernation_config.idle_timeout_seconds} seconds for agent to become idle...")
time.sleep(hibernation_config.idle_timeout_seconds + 1)
print()
# Hibernate the agent
print("Hibernating idle agent...")
metadata = control_plane.hibernate_agent(
agent_context.agent_id,
agent_context,
caas_pointer="context://agent-001/session-123", # Example caas pointer
additional_state={"custom_data": "example"}
)
print(f"✓ Agent hibernated successfully")
print(f" • State file: {metadata.state_file_path}")
print(f" • Size: {metadata.state_size_bytes} bytes")
print(f" • Format: {metadata.format.value}")
print(f" • Context pointer (caas): {metadata.context_pointer}")
print()
# Check hibernation status
is_hibernated = control_plane.is_agent_hibernated(agent_context.agent_id)
print(f"✓ Agent hibernation status: {is_hibernated}")
print()
# Get hibernation statistics
stats = control_plane.get_hibernation_statistics()
print("Hibernation Statistics:")
print(f" • Total hibernated agents: {stats['total_hibernated_agents']}")
print(f" • Total state size: {stats['total_state_size_mb']:.2f} MB")
print(f" • Storage path: {stats['storage_path']}")
print()
# Simulate receiving a message for the hibernated agent
print("Simulating incoming message for hibernated agent...")
print("Waking agent from hibernation...")
restored_state = control_plane.wake_agent(agent_context.agent_id)
print(f"✓ Agent woken successfully")
print(f" • Session ID: {restored_state['session_id']}")
print(f" • Created at: {restored_state['created_at']}")
print(f" • Context pointer (caas): {restored_state['caas_pointer']}")
print(f" • Custom data: {restored_state['additional_state']}")
print()
# Check hibernation status again
is_hibernated = control_plane.is_agent_hibernated(agent_context.agent_id)
print(f"✓ Agent hibernation status after wake: {is_hibernated}")
print()
print("✅ Agent Hibernation demo complete!")
print(" Agents can now be 'serverless' - no idle cost!")
print()
def example_time_travel_debugging():
"""
Demonstrate Time-Travel Debugging feature.
Problem: Need to understand and debug agent behavior after the fact.
Solution: Replay agent history from audit logs (amb + emk).
Result: Complete observability - "Re-run the last 5 minutes exactly as it happened."
"""
print("=" * 60)
print("EXAMPLE 2: Time-Travel Debugging")
print("=" * 60)
print()
# Create control plane with time-travel enabled
flight_recorder = FlightRecorder(db_path="/tmp/time_travel_demo.db")
time_travel_config = TimeTravelConfig(
enabled=True,
enable_state_snapshots=True,
snapshot_interval_seconds=10
)
control_plane = AgentControlPlane(
enable_time_travel=True,
time_travel_config=time_travel_config
)
# Attach FlightRecorder to kernel for audit logging
control_plane.kernel.audit_logger = flight_recorder
control_plane.time_travel_debugger.flight_recorder = flight_recorder
# Create an agent
agent_context = create_standard_agent(control_plane, "debug-agent-001")
print(f"✓ Created agent: {agent_context.agent_id}")
print()
# Execute a series of actions (simulating agent behavior)
print("Executing actions to build history...")
actions = [
("File read", ActionType.FILE_READ, {"path": "/data/config.json"}),
("API call", ActionType.API_CALL, {"url": "https://api.example.com/users", "method": "GET"}),
("Database query", ActionType.DATABASE_QUERY, {"query": "SELECT * FROM users"}),
("File write", ActionType.FILE_WRITE, {"path": "/data/output.txt", "content": "Result data"}),
]
for idx, (name, action_type, params) in enumerate(actions, 1):
result = control_plane.execute_action(agent_context, action_type, params)
print(f" {idx}. {name}: {result['success']}")
# Capture state snapshot after each action
control_plane.capture_agent_state_snapshot(
agent_context.agent_id,
agent_context,
metadata={"action": name, "step": idx}
)
time.sleep(0.5) # Small delay between actions
print()
# Now demonstrate time-travel debugging
print("Time-Travel Debugging: Replaying last 1 minute of agent history...")
print()
# Create a replay session
replay_session = control_plane.replay_agent_history(
agent_context.agent_id,
minutes=1
)
print(f"✓ Created replay session: {replay_session.session_id}")
print(f" • Agent: {replay_session.agent_id}")
print(f" • Time range: {replay_session.start_time} to {replay_session.end_time}")
print(f" • Total events: {len(replay_session.events)}")
print(f" • Mode: {replay_session.mode.value}")
print()
# Get replay summary
summary = control_plane.get_replay_summary(replay_session.session_id)
print("Replay Summary:")
print(f" • Duration: {summary['time_range']['duration_seconds']:.1f} seconds")
print(f" • Total events: {summary['total_events']}")
print(f" • Event breakdown:")
for event_type, count in summary['event_type_breakdown'].items():
print(f" - {event_type}: {count}")
print()
# Replay events with a callback
print("Replaying events:")
def replay_callback(event):
"""Callback function called for each replayed event"""
print(f" [{event.timestamp.strftime('%H:%M:%S')}] {event.event_type.value}")
print(f" Tool: {event.data.get('tool_name')}")
print(f" Verdict: {event.data.get('policy_verdict')}")
if event.data.get('result'):
print(f" Result: {event.data.get('result')[:50]}...")
print()
# Replay the history
from agent_control_plane.time_travel_debugger import ReplayMode
# Create a step-by-step replay session
step_session = control_plane.time_travel_debugger.create_replay_session(
agent_context.agent_id,
replay_session.start_time,
replay_session.end_time,
mode=ReplayMode.STEP_BY_STEP
)
print(f"Step-by-step replay (press Enter to advance):")
print()
# Replay first 3 events step by step
for i in range(min(3, len(step_session.events))):
event = control_plane.time_travel_debugger.next_step(step_session.session_id)
if event:
print(f"Step {i+1}:")
replay_callback(event)
# In a real scenario, you'd wait for user input here
# input("Press Enter for next step...")
# Get progress
progress = control_plane.time_travel_debugger.get_session_progress(step_session.session_id)
print(f"Replay Progress: {progress['progress_percent']:.1f}% complete")
print(f" ({progress['current_index']} of {progress['total_events']} events)")
print()
# Get time-travel statistics
tt_stats = control_plane.get_time_travel_statistics()
print("Time-Travel Statistics:")
print(f" • Active replay sessions: {tt_stats['active_replay_sessions']}")
print(f" • Total state snapshots: {tt_stats['total_state_snapshots']}")
print(f" • Agents with snapshots: {tt_stats['agents_with_snapshots']}")
print()
# Export replay session for analysis
print("Exporting replay session for analysis...")
export_data = control_plane.time_travel_debugger.export_replay_session(
replay_session.session_id
)
print(f"✓ Export complete ({len(export_data)} bytes)")
print(f" Sample: {export_data[:100]}...")
print()
print("✅ Time-Travel Debugging demo complete!")
print(" You can now replay and debug agent behavior!")
print()
def example_combined_features():
"""
Demonstrate using both hibernation and time-travel together.
This shows how the two features complement each other:
- Hibernation reduces idle cost
- Time-travel lets you debug what happened before hibernation
"""
print("=" * 60)
print("EXAMPLE 3: Combined Features")
print("=" * 60)
print()
# Setup
flight_recorder = FlightRecorder(db_path="/tmp/combined_demo.db")
control_plane = AgentControlPlane(
enable_hibernation=True,
enable_time_travel=True,
hibernation_config=HibernationConfig(
idle_timeout_seconds=3,
storage_path="/tmp/combined_demo_hibernation"
),
time_travel_config=TimeTravelConfig(
enabled=True,
enable_state_snapshots=True
)
)
control_plane.kernel.audit_logger = flight_recorder
control_plane.time_travel_debugger.flight_recorder = flight_recorder
agent_context = create_standard_agent(control_plane, "combined-agent-001")
print(f"✓ Agent created: {agent_context.agent_id}")
print()
# Do some work
print("Agent performing work...")
for i in range(3):
result = control_plane.execute_action(
agent_context,
ActionType.FILE_READ,
{"path": f"/data/file{i}.txt"}
)
control_plane.capture_agent_state_snapshot(
agent_context.agent_id,
agent_context,
metadata={"iteration": i}
)
time.sleep(0.5)
print("✓ Work completed")
print()
# Hibernate due to inactivity
print("Agent becomes idle...")
time.sleep(4)
hibernated = control_plane.hibernate_idle_agents()
if hibernated:
print(f"✓ Hibernated {len(hibernated)} idle agent(s): {hibernated}")
else:
# Manually hibernate for demo
print("✓ Manually hibernating agent for demo...")
control_plane.hibernate_agent(agent_context.agent_id, agent_context)
hibernated = [agent_context.agent_id]
print()
# Later, debug what the agent did before hibernation
print("Debugging agent behavior before hibernation...")
replay_session = control_plane.replay_agent_history(
agent_context.agent_id,
minutes=1
)
summary = control_plane.get_replay_summary(replay_session.session_id)
print(f"✓ Found {summary['total_events']} events before hibernation")
print()
# Wake agent for new work
if hibernated:
print("New message arrives, waking agent...")
restored = control_plane.wake_agent(agent_context.agent_id)
print(f"✓ Agent restored and ready for new work")
print()
print("✅ Combined features demo complete!")
print(" Serverless agents + complete debugging = Perfect!")
print()
if __name__ == "__main__":
print("\n")
print("╔════════════════════════════════════════════════════════════╗")
print("║ Agent Control Plane - Advanced Features Demo ║")
print("║ ║")
print("║ 1. Agent Hibernation (Serverless Agents) ║")
print("║ 2. Time-Travel Debugging ║")
print("║ 3. Combined Features ║")
print("╚════════════════════════════════════════════════════════════╝")
print("\n")
try:
# Run all examples
example_agent_hibernation()
print("\n" + "─" * 60 + "\n")
example_time_travel_debugging()
print("\n" + "─" * 60 + "\n")
example_combined_features()
print("\n")
print("╔════════════════════════════════════════════════════════════╗")
print("║ All demos completed successfully! 🎉 ║")
print("║ ║")
print("║ Key Takeaways: ║")
print("║ • Hibernation = No idle cost (Serverless Agents) ║")
print("║ • Time-Travel = Complete debugging & observability ║")
print("║ • Together = Production-ready agent infrastructure ║")
print("╚════════════════════════════════════════════════════════════╝")
print("\n")
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()