-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_execution_engine.py
More file actions
600 lines (471 loc) · 18.5 KB
/
debug_execution_engine.py
File metadata and controls
600 lines (471 loc) · 18.5 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
#!/usr/bin/env python3
"""Debug script for Milestone 2.5: Execution Engine.
This script walks through each component step-by-step.
Run with: python debug_execution_engine.py
Recommended: Set breakpoints in your IDE at the marked locations.
"""
import asyncio
import sys
from datetime import datetime, timezone
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
# Enable verbose logging
import logging
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s | %(name)s | %(levelname)s | %(message)s"
)
print("=" * 80)
print("MILESTONE 2.5 DEBUG: Execution Engine Deep Dive")
print("=" * 80)
# =============================================================================
# PART 1: Understanding the Schemas (Data Models)
# =============================================================================
def debug_part1_schemas():
"""Debug Part 1: Understand ExecutionStatus and ExecutionResult."""
print("\n" + "=" * 80)
print("PART 1: SCHEMAS - ExecutionStatus & ExecutionResult")
print("=" * 80)
# BREAKPOINT 1: Set breakpoint here
from src.agents.schemas import ExecutionResult, ExecutionStatus
# --- ExecutionStatus Enum ---
print("\n📦 ExecutionStatus Enum:")
print("-" * 40)
# This enum tracks the lifecycle of an action
for status in ExecutionStatus:
print(f" {status.name} = '{status.value}'")
# State machine visualization:
print("\n📊 State Machine:")
print(
"""
PENDING → RUNNING → SUCCESS
↓
FAILED → ROLLED_BACK
"""
)
# --- ExecutionResult Model ---
print("\n📦 ExecutionResult Model:")
print("-" * 40)
# BREAKPOINT 2: Step into ExecutionResult creation
#
# TO DEBUG: Set breakpoint on the next line (line 70)
# If breakpoint doesn't stop:
# 1. Make sure you're running in DEBUG mode (F5), not just running (F9)
# 2. Check .vscode/launch.json exists (I created it for you)
# 3. Try the simple version: debug_execution_engine_simple.py
# 4. Or add: import pdb; pdb.set_trace() before this line
result = ExecutionResult(
action_id="test-action-001",
status=ExecutionStatus.SUCCESS,
started_at=datetime.now(timezone.utc),
completed_at=datetime.now(timezone.utc),
)
print(f" action_id: {result.action_id}")
print(f" status: {result.status}")
print(f" started_at: {result.started_at}")
print(f" completed_at: {result.completed_at}")
print(f" error_message: {result.error_message}")
print(f" rollback_status: {result.rollback_status}")
# --- Failed Result with Rollback ---
print("\n📦 ExecutionResult with Rollback:")
print("-" * 40)
failed_result = ExecutionResult(
action_id="test-action-002",
status=ExecutionStatus.FAILED,
started_at=datetime.now(timezone.utc),
completed_at=datetime.now(timezone.utc),
error_message="Connection timeout",
rollback_status=ExecutionStatus.SUCCESS,
rollback_completed_at=datetime.now(timezone.utc),
)
print(f" status: {failed_result.status}")
print(f" error_message: {failed_result.error_message}")
print(f" rollback_status: {failed_result.rollback_status}")
print("\n✅ Part 1 Complete: You now understand the data models")
return result, failed_result
# =============================================================================
# PART 2: Understanding the Executors
# =============================================================================
async def debug_part2_executors():
"""Debug Part 2: Understand how Action Executors work."""
print("\n" + "=" * 80)
print("PART 2: EXECUTORS - How Actions Are Executed")
print("=" * 80)
from src.agents.executor import (
RestartServiceExecutor,
ScaleServiceExecutor,
get_executor,
)
from src.agents.schemas import ExecutionStatus, RemediationAction
# --- Executor Factory ---
print("\n📦 Executor Factory (get_executor):")
print("-" * 40)
# BREAKPOINT 3: Step into get_executor
restart_executor = get_executor("restart_service")
scale_executor = get_executor("scale_service")
print(f" restart_service → {type(restart_executor).__name__}")
print(f" scale_service → {type(scale_executor).__name__}")
# --- Create Test Action ---
print("\n📦 Creating Test Action:")
print("-" * 40)
action = RemediationAction(
action_id="debug-action-001",
action_type="restart_service",
target="web-server-01",
parameters={},
estimated_downtime_seconds=30,
risk_level="low",
approved=True,
)
print(f" action_id: {action.action_id}")
print(f" action_type: {action.action_type}")
print(f" target: {action.target}")
print(f" risk_level: {action.risk_level}")
# --- Execute Action ---
print("\n📦 Executing Action:")
print("-" * 40)
# BREAKPOINT 4: Step into execute() method
# This is where the action is "performed" (mocked)
executor = get_executor(action.action_type)
# Force success for predictable debugging
import random
original_random = random.random
random.random = lambda: 0.5 # Always succeed (< 0.9)
try:
result = await executor.execute(action)
print(f" Execution Status: {result.status}")
print(f" Started At: {result.started_at}")
print(f" Completed At: {result.completed_at}")
if result.status == ExecutionStatus.SUCCESS:
print(" ✅ Action executed successfully")
else:
print(f" ❌ Action failed: {result.error_message}")
finally:
random.random = original_random
# --- Validate Action ---
print("\n📦 Validating Action:")
print("-" * 40)
# BREAKPOINT 5: Step into validate() method
is_valid = await executor.validate(action)
print(f" Action is valid: {is_valid}")
# Invalid action type
action.action_type = "invalid_type"
is_valid = await executor.validate(action)
print(f" After changing type to 'invalid_type': {is_valid}")
print("\n✅ Part 2 Complete: You now understand how executors work")
return result
# =============================================================================
# PART 3: Understanding Rollback Logic
# =============================================================================
async def debug_part3_rollback():
"""Debug Part 3: Understand when and how rollback happens."""
print("\n" + "=" * 80)
print("PART 3: ROLLBACK - Safety Mechanism")
print("=" * 80)
from unittest.mock import MagicMock
from src.agents.rollback import execute_rollback, should_rollback, validate_rollback
from src.agents.schemas import ExecutionResult, ExecutionStatus, RemediationAction
# --- Create Mock Config ---
config = MagicMock()
config.agent.enable_rollback_on_failure = True
config.agent.rollback_on_timeout = True
config.agent.atomic_high_risk_actions = True
# --- should_rollback() Decision Logic ---
print("\n📦 should_rollback() - Decision Logic:")
print("-" * 40)
# BREAKPOINT 6: Step into should_rollback()
# Case 1: High-risk action failure → Always rollback
high_risk_action = RemediationAction(
action_id="high-risk-001",
action_type="rollback_deployment",
target="api-service",
parameters={},
estimated_downtime_seconds=300,
risk_level="high",
)
failed_result = ExecutionResult(
action_id="high-risk-001",
status=ExecutionStatus.FAILED,
started_at=datetime.now(timezone.utc),
completed_at=datetime.now(timezone.utc),
error_message="Deployment failed",
)
should_rb = should_rollback(high_risk_action, failed_result, config)
print(f" High-risk + Failed → Should rollback: {should_rb}")
# Case 2: Success → Never rollback
success_result = ExecutionResult(
action_id="success-001",
status=ExecutionStatus.SUCCESS,
started_at=datetime.now(timezone.utc),
completed_at=datetime.now(timezone.utc),
)
should_rb = should_rollback(high_risk_action, success_result, config)
print(f" High-risk + Success → Should rollback: {should_rb}")
# Case 3: Low-risk + Failed → Check config
low_risk_action = RemediationAction(
action_id="low-risk-001",
action_type="restart_service",
target="web-server",
parameters={},
estimated_downtime_seconds=30,
risk_level="low",
)
should_rb = should_rollback(low_risk_action, failed_result, config)
print(f" Low-risk + Failed → Should rollback: {should_rb}")
# --- Execute Rollback ---
print("\n📦 execute_rollback() - Undo Action:")
print("-" * 40)
# BREAKPOINT 7: Step into execute_rollback()
scale_action = RemediationAction(
action_id="scale-001",
action_type="scale_service",
target="cache-service",
parameters={"instance_count": 2},
estimated_downtime_seconds=60,
risk_level="low",
)
scale_failed = ExecutionResult(
action_id="scale-001",
status=ExecutionStatus.FAILED,
started_at=datetime.now(timezone.utc),
completed_at=datetime.now(timezone.utc),
error_message="Insufficient resources",
)
rolled_back = await execute_rollback(scale_action, scale_failed, config)
print(f" Original status: {scale_failed.status}")
print(f" Rollback status: {rolled_back.rollback_status}")
print(f" Rollback completed at: {rolled_back.rollback_completed_at}")
# --- Validate Rollback ---
print("\n📦 validate_rollback() - Check Success:")
print("-" * 40)
# BREAKPOINT 8: Step into validate_rollback()
is_valid = validate_rollback(scale_action, rolled_back)
print(f" Rollback was successful: {is_valid}")
print("\n✅ Part 3 Complete: You now understand rollback logic")
# =============================================================================
# PART 4: Understanding the Execute Node
# =============================================================================
async def debug_part4_execute_node():
"""Debug Part 4: Understand how execute_node integrates with LangGraph."""
print("\n" + "=" * 80)
print("PART 4: EXECUTE NODE - LangGraph Integration")
print("=" * 80)
from src.agents.nodes.execute import execute_node
from src.agents.schemas import (
AnalysisResult,
DetectionResult,
ExecutionStatus,
IncidentMetrics,
RemediationAction,
RemediationPlan,
create_initial_state,
)
# --- Create Full State ---
print("\n📦 Creating Agent State with Approved Plan:")
print("-" * 40)
# BREAKPOINT 9: Understand the state structure
metrics = [
IncidentMetrics(
metric_name="cpu_usage_percent",
current_value=95.0,
baseline_value=70.0,
deviation_score=3.5,
labels={"service": "web-server"},
)
]
state = create_initial_state("debug-incident-001", metrics)
# Add detection result
state["detection_result"] = DetectionResult(
is_anomaly=True,
confidence=0.90,
detection_method="z_score",
threshold_used=3.0,
)
# Add analysis result
state["analysis_result"] = AnalysisResult(
root_cause="High CPU usage due to memory leak in web-server",
confidence=0.85,
evidence=["CPU spiked from 70% to 95%"],
affected_services=["web-server"],
token_usage=450,
)
# Add approved remediation plan
plan = RemediationPlan(
actions=[
RemediationAction(
action_id="action-1",
action_type="restart_service",
target="web-server",
parameters={},
estimated_downtime_seconds=30,
risk_level="low",
approved=True, # ← IMPORTANT: Must be approved
)
],
total_estimated_downtime_seconds=30,
)
plan.approved_at = datetime.now(timezone.utc)
plan.approved_by = "debug-user@example.com"
state["remediation_plan"] = plan
state["human_approval"] = True # ← IMPORTANT: Must be True
print(f" incident_id: {state['incident_id']}")
print(f" detection_result.is_anomaly: {state['detection_result'].is_anomaly}")
print(
f" analysis_result.root_cause: {state['analysis_result'].root_cause[:50]}..."
)
print(f" remediation_plan.actions: {len(plan.actions)} action(s)")
print(f" human_approval: {state['human_approval']}")
# --- Execute Node ---
print("\n📦 Running execute_node():")
print("-" * 40)
# BREAKPOINT 10: Step into execute_node
# This is where all the magic happens
# Force success for predictable debugging
import random
original_random = random.random
random.random = lambda: 0.5
try:
result_state = await execute_node(state)
print(f" Current node: {result_state['current_node']}")
print(f" Iteration count: {result_state['iteration_count']}")
updated_plan = result_state["remediation_plan"]
print(f" Execution started at: {updated_plan.execution_started_at}")
print(f" Execution completed at: {updated_plan.execution_completed_at}")
print("\n Execution Results:")
for er in updated_plan.execution_results:
print(f" - {er.action_id}: {er.status}")
if er.error_message:
print(f" Error: {er.error_message}")
if er.rollback_status:
print(f" Rollback: {er.rollback_status}")
# Check execution log
print("\n Execution Log:")
for log in result_state["execution_log"]:
print(
f" - {log.node_name}: success={log.success}, duration={log.duration_seconds:.3f}s"
)
finally:
random.random = original_random
print("\n✅ Part 4 Complete: You now understand the execute node")
return result_state
# =============================================================================
# PART 5: Understanding Graph Integration
# =============================================================================
async def debug_part5_graph():
"""Debug Part 5: Understand how execute integrates in the full graph."""
print("\n" + "=" * 80)
print("PART 5: GRAPH INTEGRATION - Full Flow")
print("=" * 80)
from src.agents.approval import can_execute
from src.agents.graph import create_agent_graph, should_execute
from src.agents.schemas import (
IncidentMetrics,
RemediationAction,
RemediationPlan,
create_initial_state,
)
# --- should_execute() Routing Decision ---
print("\n📦 should_execute() - Routing Decision:")
print("-" * 40)
# Create state without approval
metrics = [
IncidentMetrics(
metric_name="memory_usage",
current_value=95.0,
baseline_value=70.0,
deviation_score=3.5,
)
]
state = create_initial_state("debug-graph-001", metrics)
# Add unapproved plan
plan = RemediationPlan(
actions=[
RemediationAction(
action_id="action-1",
action_type="restart_service",
target="cache-service",
parameters={},
estimated_downtime_seconds=30,
risk_level="low",
approved=False, # ← Not approved
)
],
total_estimated_downtime_seconds=30,
)
state["remediation_plan"] = plan
state["human_approval"] = False
# BREAKPOINT 11: Step into should_execute and can_execute
print(f" Plan approved: {plan.is_fully_approved()}")
print(f" human_approval: {state['human_approval']}")
print(f" can_execute(): {can_execute(state)}")
route = should_execute(state)
print(f" should_execute() returns: '{route}'")
# Now approve and try again
print("\n After approval:")
state["remediation_plan"].actions[0].approved = True
state["remediation_plan"].approved_at = datetime.now(timezone.utc)
state["remediation_plan"].approved_by = "admin"
state["human_approval"] = True
print(f" Plan approved: {state['remediation_plan'].is_fully_approved()}")
print(f" human_approval: {state['human_approval']}")
print(f" can_execute(): {can_execute(state)}")
route = should_execute(state)
print(f" should_execute() returns: '{route}'")
# --- Graph Structure ---
print("\n📦 Graph Structure:")
print("-" * 40)
# BREAKPOINT 12: Examine the compiled graph
graph = create_agent_graph()
print(" Entry point: detect")
print(" Flow:")
print(" detect → [should_analyze] → analyze")
print(" analyze → [should_respond] → respond")
print(" respond → [should_execute] → execute (if approved)")
print(" respond → END (if not approved)")
print(" execute → END")
print("\n✅ Part 5 Complete: You now understand graph integration")
# =============================================================================
# MAIN - Run All Debug Parts
# =============================================================================
async def main():
"""Run all debug parts."""
print("\n🚀 Starting Milestone 2.5 Debug Session")
print("=" * 80)
# Part 1: Schemas (synchronous)
debug_part1_schemas()
# Part 2: Executors (async)
await debug_part2_executors()
# Part 3: Rollback (async)
await debug_part3_rollback()
# Part 4: Execute Node (async)
await debug_part4_execute_node()
# Part 5: Graph Integration (async)
await debug_part5_graph()
print("\n" + "=" * 80)
print("🎉 DEBUG SESSION COMPLETE")
print("=" * 80)
print(
"""
Key Breakpoints to Set in Your IDE:
1. src/agents/schemas.py:
- Line ~40: ExecutionStatus enum definition
- Line ~170: ExecutionResult class
2. src/agents/executor.py:
- Line ~105: RestartServiceExecutor.execute()
- Line ~380: get_executor() factory
- Line ~400: execute_remediation_plan()
3. src/agents/rollback.py:
- Line ~30: should_rollback()
- Line ~65: execute_rollback()
4. src/agents/nodes/execute.py:
- Line ~45: execute_node() - main entry point
- Line ~70: asyncio.wait_for() - timeout handling
5. src/agents/graph.py:
- Line ~100: should_execute() - routing decision
- Line ~180: create_agent_graph() - graph construction
Happy debugging! 🔍
"""
)
if __name__ == "__main__":
asyncio.run(main())