kailash 0.1.5__py3-none-any.whl → 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. kailash/__init__.py +1 -1
  2. kailash/access_control.py +740 -0
  3. kailash/api/__main__.py +6 -0
  4. kailash/api/auth.py +668 -0
  5. kailash/api/custom_nodes.py +285 -0
  6. kailash/api/custom_nodes_secure.py +377 -0
  7. kailash/api/database.py +620 -0
  8. kailash/api/studio.py +915 -0
  9. kailash/api/studio_secure.py +893 -0
  10. kailash/mcp/__init__.py +53 -0
  11. kailash/mcp/__main__.py +13 -0
  12. kailash/mcp/ai_registry_server.py +712 -0
  13. kailash/mcp/client.py +447 -0
  14. kailash/mcp/client_new.py +334 -0
  15. kailash/mcp/server.py +293 -0
  16. kailash/mcp/server_new.py +336 -0
  17. kailash/mcp/servers/__init__.py +12 -0
  18. kailash/mcp/servers/ai_registry.py +289 -0
  19. kailash/nodes/__init__.py +4 -2
  20. kailash/nodes/ai/__init__.py +2 -0
  21. kailash/nodes/ai/a2a.py +714 -67
  22. kailash/nodes/ai/intelligent_agent_orchestrator.py +31 -37
  23. kailash/nodes/ai/iterative_llm_agent.py +1280 -0
  24. kailash/nodes/ai/llm_agent.py +324 -1
  25. kailash/nodes/ai/self_organizing.py +5 -6
  26. kailash/nodes/base.py +15 -2
  27. kailash/nodes/base_async.py +45 -0
  28. kailash/nodes/base_cycle_aware.py +374 -0
  29. kailash/nodes/base_with_acl.py +338 -0
  30. kailash/nodes/code/python.py +135 -27
  31. kailash/nodes/data/readers.py +16 -6
  32. kailash/nodes/data/writers.py +16 -6
  33. kailash/nodes/logic/__init__.py +8 -0
  34. kailash/nodes/logic/convergence.py +642 -0
  35. kailash/nodes/logic/loop.py +153 -0
  36. kailash/nodes/logic/operations.py +187 -27
  37. kailash/nodes/mixins/__init__.py +11 -0
  38. kailash/nodes/mixins/mcp.py +228 -0
  39. kailash/nodes/mixins.py +387 -0
  40. kailash/runtime/__init__.py +2 -1
  41. kailash/runtime/access_controlled.py +458 -0
  42. kailash/runtime/local.py +106 -33
  43. kailash/runtime/parallel_cyclic.py +529 -0
  44. kailash/sdk_exceptions.py +90 -5
  45. kailash/security.py +845 -0
  46. kailash/tracking/manager.py +38 -15
  47. kailash/tracking/models.py +1 -1
  48. kailash/tracking/storage/filesystem.py +30 -2
  49. kailash/utils/__init__.py +8 -0
  50. kailash/workflow/__init__.py +18 -0
  51. kailash/workflow/convergence.py +270 -0
  52. kailash/workflow/cycle_analyzer.py +768 -0
  53. kailash/workflow/cycle_builder.py +573 -0
  54. kailash/workflow/cycle_config.py +709 -0
  55. kailash/workflow/cycle_debugger.py +760 -0
  56. kailash/workflow/cycle_exceptions.py +601 -0
  57. kailash/workflow/cycle_profiler.py +671 -0
  58. kailash/workflow/cycle_state.py +338 -0
  59. kailash/workflow/cyclic_runner.py +985 -0
  60. kailash/workflow/graph.py +500 -39
  61. kailash/workflow/migration.py +768 -0
  62. kailash/workflow/safety.py +365 -0
  63. kailash/workflow/templates.py +744 -0
  64. kailash/workflow/validation.py +693 -0
  65. {kailash-0.1.5.dist-info → kailash-0.2.0.dist-info}/METADATA +256 -12
  66. kailash-0.2.0.dist-info/RECORD +125 -0
  67. kailash/nodes/mcp/__init__.py +0 -11
  68. kailash/nodes/mcp/client.py +0 -554
  69. kailash/nodes/mcp/resource.py +0 -682
  70. kailash/nodes/mcp/server.py +0 -577
  71. kailash-0.1.5.dist-info/RECORD +0 -88
  72. {kailash-0.1.5.dist-info → kailash-0.2.0.dist-info}/WHEEL +0 -0
  73. {kailash-0.1.5.dist-info → kailash-0.2.0.dist-info}/entry_points.txt +0 -0
  74. {kailash-0.1.5.dist-info → kailash-0.2.0.dist-info}/licenses/LICENSE +0 -0
  75. {kailash-0.1.5.dist-info → kailash-0.2.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,760 @@
1
+ """
2
+ Comprehensive Debugging and Introspection for Cyclic Workflows.
3
+
4
+ This module provides extensive debugging and introspection capabilities for
5
+ cyclic workflows, enabling developers to understand cycle behavior, analyze
6
+ performance characteristics, and diagnose issues during both development and
7
+ production phases with detailed execution tracking and rich analytics.
8
+
9
+ Examples:
10
+ Basic debugging setup:
11
+
12
+ >>> debugger = CycleDebugger(
13
+ ... debug_level="detailed",
14
+ ... enable_profiling=True
15
+ ... )
16
+ >>> # Start cycle debugging
17
+ >>> trace = debugger.start_cycle(
18
+ ... "optimization_cycle",
19
+ ... "workflow_1",
20
+ ... max_iterations=100
21
+ ... )
22
+
23
+ Iteration tracking:
24
+
25
+ >>> # Track each iteration
26
+ >>> iteration = debugger.start_iteration(trace, input_data)
27
+ >>> # ... cycle execution ...
28
+ >>> debugger.end_iteration(
29
+ ... trace, iteration, output_data,
30
+ ... convergence_value=0.05,
31
+ ... node_executions=["processor", "evaluator"]
32
+ ... )
33
+
34
+ Analysis and reporting:
35
+
36
+ >>> # Complete cycle
37
+ >>> debugger.end_cycle(
38
+ ... trace, converged=True,
39
+ ... termination_reason="convergence",
40
+ ... convergence_iteration=15
41
+ ... )
42
+ >>> # Generate comprehensive report
43
+ >>> report = debugger.generate_report(trace)
44
+ >>> # Export for external analysis
45
+ >>> debugger.export_trace(trace, "debug_output.json", "json")
46
+ """
47
+
48
+ import json
49
+ import logging
50
+ from dataclasses import dataclass, field
51
+ from datetime import datetime
52
+ from typing import Any, Dict, List, Optional, Tuple
53
+
54
+ logger = logging.getLogger(__name__)
55
+
56
+
57
+ @dataclass
58
+ class CycleIteration:
59
+ """
60
+ Represents a single iteration within a cycle execution.
61
+
62
+ This class captures all relevant information about a cycle iteration,
63
+ including input/output data, execution time, memory usage, and any
64
+ errors that occurred during execution.
65
+
66
+ Attributes:
67
+ iteration_number: The iteration count (starting from 1).
68
+ start_time: When this iteration began execution.
69
+ end_time: When this iteration completed.
70
+ execution_time: Duration in seconds.
71
+ input_data: Input data for this iteration.
72
+ output_data: Output data from this iteration.
73
+ memory_usage_mb: Memory usage in megabytes.
74
+ cpu_usage_percent: CPU usage percentage.
75
+ convergence_value: Convergence metric if available.
76
+ error: Error message if iteration failed.
77
+ node_executions: List of nodes executed in this iteration.
78
+ """
79
+
80
+ iteration_number: int
81
+ start_time: datetime
82
+ end_time: Optional[datetime] = None
83
+ execution_time: Optional[float] = None
84
+ input_data: Dict[str, Any] = field(default_factory=dict)
85
+ output_data: Optional[Dict[str, Any]] = None
86
+ memory_usage_mb: Optional[float] = None
87
+ cpu_usage_percent: Optional[float] = None
88
+ convergence_value: Optional[float] = None
89
+ error: Optional[str] = None
90
+ node_executions: List[str] = field(default_factory=list)
91
+
92
+ def complete(self, output_data: Dict[str, Any], convergence_value: Optional[float] = None):
93
+ """
94
+ Mark iteration as complete with output data.
95
+
96
+ Args:
97
+ output_data: The output data from this iteration.
98
+ convergence_value: Convergence metric if available.
99
+ """
100
+ self.end_time = datetime.now()
101
+ self.execution_time = (self.end_time - self.start_time).total_seconds()
102
+ self.output_data = output_data
103
+ self.convergence_value = convergence_value
104
+
105
+ def fail(self, error: str):
106
+ """
107
+ Mark iteration as failed with error message.
108
+
109
+ Args:
110
+ error: Error message describing the failure.
111
+ """
112
+ self.end_time = datetime.now()
113
+ self.execution_time = (self.end_time - self.start_time).total_seconds()
114
+ self.error = error
115
+
116
+ def is_completed(self) -> bool:
117
+ """Check if iteration has completed (successfully or with error).
118
+
119
+ Returns:
120
+ True if iteration has completed.
121
+ """
122
+ return self.end_time is not None
123
+
124
+ def is_successful(self) -> bool:
125
+ """Check if iteration completed successfully.
126
+
127
+ Returns:
128
+ True if iteration completed without error.
129
+ """
130
+ return self.end_time is not None and self.error is None
131
+
132
+ def to_dict(self) -> Dict[str, Any]:
133
+ """Convert iteration to dictionary for serialization.
134
+
135
+ Returns:
136
+ Dictionary representation of the iteration.
137
+ """
138
+ return {
139
+ "iteration_number": self.iteration_number,
140
+ "start_time": self.start_time.isoformat(),
141
+ "end_time": self.end_time.isoformat() if self.end_time else None,
142
+ "execution_time": self.execution_time,
143
+ "memory_usage_mb": self.memory_usage_mb,
144
+ "cpu_usage_percent": self.cpu_usage_percent,
145
+ "convergence_value": self.convergence_value,
146
+ "error": self.error,
147
+ "node_executions": self.node_executions,
148
+ "input_size": len(str(self.input_data)) if self.input_data else 0,
149
+ "output_size": len(str(self.output_data)) if self.output_data else 0
150
+ }
151
+
152
+
153
+ @dataclass
154
+ class CycleExecutionTrace:
155
+ """
156
+ Complete execution trace for a cycle, containing all iterations and metadata.
157
+
158
+ This class provides a comprehensive record of cycle execution, including
159
+ all iterations, overall statistics, convergence analysis, and performance
160
+ metrics. It serves as the primary data structure for cycle debugging.
161
+
162
+ Attributes:
163
+ cycle_id: Unique identifier for the cycle.
164
+ workflow_id: Parent workflow identifier.
165
+ start_time: When cycle execution began.
166
+ end_time: When cycle execution completed.
167
+ total_execution_time: Total duration in seconds.
168
+ iterations: All iterations executed.
169
+ converged: Whether cycle converged successfully.
170
+ convergence_iteration: Iteration where convergence occurred.
171
+ termination_reason: Why the cycle terminated.
172
+ max_iterations_configured: Configured iteration limit.
173
+ timeout_configured: Configured timeout limit.
174
+ convergence_condition: Configured convergence condition.
175
+ """
176
+
177
+ cycle_id: str
178
+ workflow_id: str
179
+ start_time: datetime
180
+ end_time: Optional[datetime] = None
181
+ total_execution_time: Optional[float] = None
182
+ iterations: List[CycleIteration] = field(default_factory=list)
183
+ converged: bool = False
184
+ convergence_iteration: Optional[int] = None
185
+ termination_reason: str = "unknown"
186
+ max_iterations_configured: Optional[int] = None
187
+ timeout_configured: Optional[float] = None
188
+ convergence_condition: Optional[str] = None
189
+ memory_peak_mb: Optional[float] = None
190
+ cpu_peak_percent: Optional[float] = None
191
+
192
+ def add_iteration(self, iteration: CycleIteration):
193
+ """
194
+ Add an iteration to the trace.
195
+
196
+ Args:
197
+ iteration: The iteration to add.
198
+ """
199
+ self.iterations.append(iteration)
200
+
201
+ # Update peak metrics
202
+ if iteration.memory_usage_mb and (not self.memory_peak_mb or iteration.memory_usage_mb > self.memory_peak_mb):
203
+ self.memory_peak_mb = iteration.memory_usage_mb
204
+
205
+ if iteration.cpu_usage_percent and (not self.cpu_peak_percent or iteration.cpu_usage_percent > self.cpu_peak_percent):
206
+ self.cpu_peak_percent = iteration.cpu_usage_percent
207
+
208
+ def complete(self, converged: bool, termination_reason: str, convergence_iteration: Optional[int] = None):
209
+ """
210
+ Mark cycle execution as complete.
211
+
212
+ Args:
213
+ converged: Whether the cycle converged successfully.
214
+ termination_reason: Why the cycle terminated.
215
+ convergence_iteration: Iteration where convergence occurred.
216
+ """
217
+ self.end_time = datetime.now()
218
+ self.total_execution_time = (self.end_time - self.start_time).total_seconds()
219
+ self.converged = converged
220
+ self.termination_reason = termination_reason
221
+ self.convergence_iteration = convergence_iteration
222
+
223
+ def get_statistics(self) -> Dict[str, Any]:
224
+ """
225
+ Get comprehensive statistics about the cycle execution.
226
+
227
+ Returns:
228
+ Statistics including timing, convergence, and performance metrics.
229
+
230
+ Examples:
231
+ >>> stats = trace.get_statistics()
232
+ >>> print(f"Average iteration time: {stats['avg_iteration_time']:.3f}s")
233
+ """
234
+ if not self.iterations:
235
+ return {
236
+ "total_iterations": 0,
237
+ "avg_iteration_time": 0.0,
238
+ "min_iteration_time": 0.0,
239
+ "max_iteration_time": 0.0,
240
+ "convergence_rate": 0.0,
241
+ "efficiency_score": 0.0
242
+ }
243
+
244
+ # Calculate timing statistics
245
+ iteration_times = [iter.execution_time for iter in self.iterations if iter.execution_time is not None]
246
+
247
+ stats = {
248
+ "total_iterations": len(self.iterations),
249
+ "successful_iterations": len([iter for iter in self.iterations if iter.is_successful()]),
250
+ "failed_iterations": len([iter for iter in self.iterations if iter.error is not None]),
251
+ "avg_iteration_time": sum(iteration_times) / len(iteration_times) if iteration_times else 0.0,
252
+ "min_iteration_time": min(iteration_times) if iteration_times else 0.0,
253
+ "max_iteration_time": max(iteration_times) if iteration_times else 0.0,
254
+ "total_execution_time": self.total_execution_time or 0.0,
255
+ "converged": self.converged,
256
+ "convergence_iteration": self.convergence_iteration,
257
+ "termination_reason": self.termination_reason,
258
+ "memory_peak_mb": self.memory_peak_mb,
259
+ "cpu_peak_percent": self.cpu_peak_percent
260
+ }
261
+
262
+ # Calculate convergence rate (how quickly it converged relative to max_iterations)
263
+ if self.converged and self.convergence_iteration and self.max_iterations_configured:
264
+ stats["convergence_rate"] = self.convergence_iteration / self.max_iterations_configured
265
+ else:
266
+ stats["convergence_rate"] = 1.0 if self.converged else 0.0
267
+
268
+ # Calculate efficiency score (0-1, higher is better)
269
+ if self.max_iterations_configured and len(self.iterations) > 0:
270
+ iteration_efficiency = 1.0 - (len(self.iterations) / self.max_iterations_configured)
271
+ time_efficiency = 1.0 if not self.timeout_configured else max(0.0, 1.0 - (self.total_execution_time or 0) / self.timeout_configured)
272
+ convergence_bonus = 0.2 if self.converged else 0.0
273
+ stats["efficiency_score"] = min(1.0, iteration_efficiency * 0.5 + time_efficiency * 0.3 + convergence_bonus)
274
+ else:
275
+ stats["efficiency_score"] = 0.5 if self.converged else 0.0
276
+
277
+ return stats
278
+
279
+ def get_convergence_trend(self) -> List[Tuple[int, Optional[float]]]:
280
+ """
281
+ Get convergence values over iterations for trend analysis.
282
+
283
+ Returns:
284
+ List[Tuple[int, Optional[float]]]: List of (iteration_number, convergence_value) pairs
285
+
286
+ Side Effects:
287
+ None - this is a pure calculation method
288
+
289
+ Example:
290
+ >>> trend = trace.get_convergence_trend()
291
+ >>> for iteration, value in trend:
292
+ ... print(f"Iteration {iteration}: {value}")
293
+ """
294
+ return [(iter.iteration_number, iter.convergence_value) for iter in self.iterations]
295
+
296
+ def to_dict(self) -> Dict[str, Any]:
297
+ """Convert trace to dictionary for serialization."""
298
+ return {
299
+ "cycle_id": self.cycle_id,
300
+ "workflow_id": self.workflow_id,
301
+ "start_time": self.start_time.isoformat(),
302
+ "end_time": self.end_time.isoformat() if self.end_time else None,
303
+ "total_execution_time": self.total_execution_time,
304
+ "iterations": [iter.to_dict() for iter in self.iterations],
305
+ "converged": self.converged,
306
+ "convergence_iteration": self.convergence_iteration,
307
+ "termination_reason": self.termination_reason,
308
+ "max_iterations_configured": self.max_iterations_configured,
309
+ "timeout_configured": self.timeout_configured,
310
+ "convergence_condition": self.convergence_condition,
311
+ "memory_peak_mb": self.memory_peak_mb,
312
+ "cpu_peak_percent": self.cpu_peak_percent,
313
+ "statistics": self.get_statistics()
314
+ }
315
+
316
+
317
+ class CycleDebugger:
318
+ """
319
+ Comprehensive debugging tool for cyclic workflow execution.
320
+
321
+ This class provides real-time debugging capabilities for cycles, including
322
+ iteration tracking, performance monitoring, convergence analysis, and
323
+ detailed execution tracing. It integrates with the cycle execution system
324
+ to provide insights into cycle behavior and performance.
325
+
326
+ Design Philosophy:
327
+ Provides non-intrusive debugging that doesn't affect cycle performance
328
+ in production. Offers multiple levels of debugging detail from basic
329
+ tracking to comprehensive profiling with rich analytics.
330
+
331
+ Upstream Dependencies:
332
+ - Used by CyclicWorkflowExecutor when debug mode is enabled
333
+ - Integrates with cycle configuration and execution systems
334
+
335
+ Downstream Consumers:
336
+ - Debug reports and analysis tools
337
+ - Performance optimization recommendations
338
+ - Cycle visualization and monitoring dashboards
339
+
340
+ Usage Patterns:
341
+ 1. Real-time debugging during development
342
+ 2. Performance profiling for optimization
343
+ 3. Production monitoring for cycle health
344
+ 4. Post-execution analysis for troubleshooting
345
+
346
+ Example:
347
+ >>> debugger = CycleDebugger(debug_level="detailed")
348
+ >>> trace = debugger.start_cycle("optimization", "workflow_1")
349
+ >>>
350
+ >>> # During cycle execution
351
+ >>> iteration = debugger.start_iteration(trace, input_data)
352
+ >>> debugger.end_iteration(iteration, output_data)
353
+ >>>
354
+ >>> # After cycle completion
355
+ >>> debugger.end_cycle(trace, converged=True, reason="convergence")
356
+ >>> report = debugger.generate_report(trace)
357
+ """
358
+
359
+ def __init__(self, debug_level: str = "basic", enable_profiling: bool = False):
360
+ """
361
+ Initialize cycle debugger.
362
+
363
+ Args:
364
+ debug_level (str): Level of debugging detail ("basic", "detailed", "verbose")
365
+ enable_profiling (bool): Whether to enable detailed profiling
366
+
367
+ Side Effects:
368
+ Configures logging and profiling settings
369
+ """
370
+ self.debug_level = debug_level
371
+ self.enable_profiling = enable_profiling
372
+ self.active_traces: Dict[str, CycleExecutionTrace] = {}
373
+
374
+ # Configure logging based on debug level
375
+ if debug_level == "verbose":
376
+ logger.setLevel(logging.DEBUG)
377
+ elif debug_level == "detailed":
378
+ logger.setLevel(logging.INFO)
379
+ else:
380
+ logger.setLevel(logging.WARNING)
381
+
382
+ def start_cycle(
383
+ self,
384
+ cycle_id: str,
385
+ workflow_id: str,
386
+ max_iterations: Optional[int] = None,
387
+ timeout: Optional[float] = None,
388
+ convergence_condition: Optional[str] = None
389
+ ) -> CycleExecutionTrace:
390
+ """
391
+ Start debugging a new cycle execution.
392
+
393
+ Creates a new execution trace and begins tracking cycle execution
394
+ with all configured debugging features enabled.
395
+
396
+ Args:
397
+ cycle_id (str): Unique identifier for the cycle
398
+ workflow_id (str): Parent workflow identifier
399
+ max_iterations (Optional[int]): Configured iteration limit
400
+ timeout (Optional[float]): Configured timeout limit
401
+ convergence_condition (Optional[str]): Convergence condition expression
402
+
403
+ Returns:
404
+ CycleExecutionTrace: New trace object for tracking execution
405
+
406
+ Side Effects:
407
+ Creates new trace and adds to active_traces
408
+ Logs cycle start event
409
+
410
+ Example:
411
+ >>> trace = debugger.start_cycle("opt_cycle", "workflow_1", max_iterations=100)
412
+ """
413
+ trace = CycleExecutionTrace(
414
+ cycle_id=cycle_id,
415
+ workflow_id=workflow_id,
416
+ start_time=datetime.now(),
417
+ max_iterations_configured=max_iterations,
418
+ timeout_configured=timeout,
419
+ convergence_condition=convergence_condition
420
+ )
421
+
422
+ self.active_traces[cycle_id] = trace
423
+
424
+ logger.info(
425
+ f"Started debugging cycle '{cycle_id}' in workflow '{workflow_id}' "
426
+ f"with max_iterations={max_iterations}, timeout={timeout}"
427
+ )
428
+
429
+ return trace
430
+
431
+ def start_iteration(
432
+ self,
433
+ trace: CycleExecutionTrace,
434
+ input_data: Dict[str, Any],
435
+ iteration_number: Optional[int] = None
436
+ ) -> CycleIteration:
437
+ """
438
+ Start debugging a new cycle iteration.
439
+
440
+ Creates a new iteration object and begins tracking execution time,
441
+ resource usage, and other iteration-specific metrics.
442
+
443
+ Args:
444
+ trace (CycleExecutionTrace): Parent cycle trace
445
+ input_data (Dict[str, Any]): Input data for this iteration
446
+ iteration_number (Optional[int]): Iteration number (auto-calculated if None)
447
+
448
+ Returns:
449
+ CycleIteration: New iteration object for tracking
450
+
451
+ Side Effects:
452
+ Creates new iteration and adds to trace
453
+ Begins resource monitoring if profiling enabled
454
+
455
+ Example:
456
+ >>> iteration = debugger.start_iteration(trace, {"value": 10})
457
+ """
458
+ if iteration_number is None:
459
+ iteration_number = len(trace.iterations) + 1
460
+
461
+ iteration = CycleIteration(
462
+ iteration_number=iteration_number,
463
+ start_time=datetime.now(),
464
+ input_data=input_data.copy() if self.debug_level in ["detailed", "verbose"] else {}
465
+ )
466
+
467
+ # Add profiling data if enabled
468
+ if self.enable_profiling:
469
+ try:
470
+ import psutil
471
+ process = psutil.Process()
472
+ iteration.memory_usage_mb = process.memory_info().rss / 1024 / 1024
473
+ iteration.cpu_usage_percent = process.cpu_percent()
474
+ except ImportError:
475
+ logger.warning("psutil not available for profiling. Install with: pip install psutil")
476
+
477
+ if self.debug_level == "verbose":
478
+ logger.debug(
479
+ f"Started iteration {iteration_number} for cycle '{trace.cycle_id}' "
480
+ f"with input keys: {list(input_data.keys())}"
481
+ )
482
+
483
+ return iteration
484
+
485
+ def end_iteration(
486
+ self,
487
+ trace: CycleExecutionTrace,
488
+ iteration: CycleIteration,
489
+ output_data: Dict[str, Any],
490
+ convergence_value: Optional[float] = None,
491
+ node_executions: Optional[List[str]] = None
492
+ ):
493
+ """
494
+ Complete iteration tracking with output data and metrics.
495
+
496
+ Finalizes iteration tracking by recording output data, convergence
497
+ metrics, and final resource usage measurements.
498
+
499
+ Args:
500
+ trace (CycleExecutionTrace): Parent cycle trace
501
+ iteration (CycleIteration): Iteration object to complete
502
+ output_data (Dict[str, Any]): Output data from iteration
503
+ convergence_value (Optional[float]): Convergence metric if available
504
+ node_executions (Optional[List[str]]): List of executed nodes
505
+
506
+ Side Effects:
507
+ Completes iteration and adds to trace
508
+ Updates peak resource usage in trace
509
+ Logs iteration completion
510
+
511
+ Example:
512
+ >>> debugger.end_iteration(trace, iteration, {"result": 20}, convergence_value=0.05)
513
+ """
514
+ iteration.complete(output_data.copy() if self.debug_level in ["detailed", "verbose"] else {}, convergence_value)
515
+
516
+ if node_executions:
517
+ iteration.node_executions = node_executions
518
+
519
+ # Update profiling data if enabled
520
+ if self.enable_profiling:
521
+ try:
522
+ import psutil
523
+ process = psutil.Process()
524
+ end_memory = process.memory_info().rss / 1024 / 1024
525
+ end_cpu = process.cpu_percent()
526
+
527
+ # Use the higher value for this iteration
528
+ iteration.memory_usage_mb = max(iteration.memory_usage_mb or 0, end_memory)
529
+ iteration.cpu_usage_percent = max(iteration.cpu_usage_percent or 0, end_cpu)
530
+ except ImportError:
531
+ pass # Already warned during start_iteration
532
+
533
+ trace.add_iteration(iteration)
534
+
535
+ if self.debug_level in ["detailed", "verbose"]:
536
+ logger.info(
537
+ f"Completed iteration {iteration.iteration_number} for cycle '{trace.cycle_id}' "
538
+ f"in {iteration.execution_time:.3f}s, convergence={convergence_value}"
539
+ )
540
+
541
+ def end_cycle(
542
+ self,
543
+ trace: CycleExecutionTrace,
544
+ converged: bool,
545
+ termination_reason: str,
546
+ convergence_iteration: Optional[int] = None
547
+ ):
548
+ """
549
+ Complete cycle tracking with final results and analysis.
550
+
551
+ Finalizes cycle execution tracking and generates comprehensive
552
+ statistics and analysis for the complete cycle execution.
553
+
554
+ Args:
555
+ trace (CycleExecutionTrace): Cycle trace to complete
556
+ converged (bool): Whether the cycle converged successfully
557
+ termination_reason (str): Why the cycle terminated
558
+ convergence_iteration (Optional[int]): Iteration where convergence occurred
559
+
560
+ Side Effects:
561
+ Completes trace and removes from active_traces
562
+ Logs cycle completion with statistics
563
+
564
+ Example:
565
+ >>> debugger.end_cycle(trace, converged=True, termination_reason="convergence", convergence_iteration=15)
566
+ """
567
+ trace.complete(converged, termination_reason, convergence_iteration)
568
+
569
+ # Remove from active traces
570
+ if trace.cycle_id in self.active_traces:
571
+ del self.active_traces[trace.cycle_id]
572
+
573
+ stats = trace.get_statistics()
574
+ logger.info(
575
+ f"Completed cycle '{trace.cycle_id}' in {trace.total_execution_time:.3f}s "
576
+ f"with {stats['total_iterations']} iterations, "
577
+ f"converged={converged}, efficiency={stats['efficiency_score']:.2f}"
578
+ )
579
+
580
+ def generate_report(self, trace: CycleExecutionTrace) -> Dict[str, Any]:
581
+ """
582
+ Generate comprehensive debugging report for a cycle execution.
583
+
584
+ Creates a detailed report including execution statistics, performance
585
+ analysis, convergence trends, and optimization recommendations based
586
+ on the complete cycle execution trace.
587
+
588
+ Args:
589
+ trace (CycleExecutionTrace): Completed cycle trace to analyze
590
+
591
+ Returns:
592
+ Dict[str, Any]: Comprehensive debugging report
593
+
594
+ Side Effects:
595
+ None - this is a pure analysis method
596
+
597
+ Example:
598
+ >>> report = debugger.generate_report(trace)
599
+ >>> print(f"Efficiency score: {report['performance']['efficiency_score']}")
600
+ """
601
+ stats = trace.get_statistics()
602
+ convergence_trend = trace.get_convergence_trend()
603
+
604
+ # Analyze convergence pattern
605
+ convergence_analysis = self._analyze_convergence(convergence_trend)
606
+
607
+ # Generate optimization recommendations
608
+ recommendations = self._generate_recommendations(trace, stats)
609
+
610
+ # Create performance summary
611
+ performance = {
612
+ "efficiency_score": stats["efficiency_score"],
613
+ "avg_iteration_time": stats["avg_iteration_time"],
614
+ "convergence_rate": stats["convergence_rate"],
615
+ "resource_usage": {
616
+ "memory_peak_mb": trace.memory_peak_mb,
617
+ "cpu_peak_percent": trace.cpu_peak_percent
618
+ }
619
+ }
620
+
621
+ report = {
622
+ "cycle_info": {
623
+ "cycle_id": trace.cycle_id,
624
+ "workflow_id": trace.workflow_id,
625
+ "execution_time": trace.total_execution_time,
626
+ "converged": trace.converged,
627
+ "termination_reason": trace.termination_reason
628
+ },
629
+ "statistics": stats,
630
+ "performance": performance,
631
+ "convergence_analysis": convergence_analysis,
632
+ "recommendations": recommendations,
633
+ "trace_data": trace.to_dict() if self.debug_level == "verbose" else None
634
+ }
635
+
636
+ return report
637
+
638
+ def _analyze_convergence(self, convergence_trend: List[Tuple[int, Optional[float]]]) -> Dict[str, Any]:
639
+ """Analyze convergence pattern from trend data."""
640
+ if not convergence_trend or all(value is None for _, value in convergence_trend):
641
+ return {"pattern": "no_data", "analysis": "No convergence data available"}
642
+
643
+ # Filter out None values
644
+ valid_points = [(iter_num, value) for iter_num, value in convergence_trend if value is not None]
645
+
646
+ if len(valid_points) < 2:
647
+ return {"pattern": "insufficient_data", "analysis": "Insufficient convergence data for analysis"}
648
+
649
+ # Analyze trend
650
+ values = [value for _, value in valid_points]
651
+
652
+ # Check for improvement pattern
653
+ if len(values) >= 3:
654
+ improving_count = sum(1 for i in range(1, len(values)) if values[i] < values[i-1])
655
+ improvement_ratio = improving_count / (len(values) - 1)
656
+
657
+ if improvement_ratio > 0.8:
658
+ pattern = "steady_improvement"
659
+ analysis = "Convergence is steadily improving"
660
+ elif improvement_ratio > 0.5:
661
+ pattern = "gradual_improvement"
662
+ analysis = "Convergence is gradually improving with some fluctuation"
663
+ elif improvement_ratio < 0.2:
664
+ pattern = "plateau"
665
+ analysis = "Convergence has plateaued - may need different approach"
666
+ else:
667
+ pattern = "unstable"
668
+ analysis = "Convergence is unstable - check algorithm parameters"
669
+ else:
670
+ pattern = "limited_data"
671
+ analysis = "Limited data for pattern analysis"
672
+
673
+ return {
674
+ "pattern": pattern,
675
+ "analysis": analysis,
676
+ "initial_value": values[0] if values else None,
677
+ "final_value": values[-1] if values else None,
678
+ "improvement": values[0] - values[-1] if len(values) >= 2 else None,
679
+ "data_points": len(valid_points)
680
+ }
681
+
682
+ def _generate_recommendations(self, trace: CycleExecutionTrace, stats: Dict[str, Any]) -> List[str]:
683
+ """Generate optimization recommendations based on execution analysis."""
684
+ recommendations = []
685
+
686
+ # Efficiency recommendations
687
+ if stats["efficiency_score"] < 0.5:
688
+ recommendations.append("Consider reducing max_iterations or improving convergence condition")
689
+
690
+ # Convergence recommendations
691
+ if not trace.converged:
692
+ if stats["total_iterations"] >= (trace.max_iterations_configured or 0):
693
+ recommendations.append("Cycle reached max_iterations without converging - increase limit or improve algorithm")
694
+ else:
695
+ recommendations.append("Cycle terminated early - check for errors or timeout issues")
696
+
697
+ # Performance recommendations
698
+ if stats["avg_iteration_time"] > 1.0:
699
+ recommendations.append("Average iteration time is high - consider optimizing node performance")
700
+
701
+ if trace.memory_peak_mb and trace.memory_peak_mb > 1000:
702
+ recommendations.append("High memory usage detected - consider data streaming or chunking")
703
+
704
+ # Convergence pattern recommendations
705
+ convergence_trend = trace.get_convergence_trend()
706
+ convergence_analysis = self._analyze_convergence(convergence_trend)
707
+
708
+ if convergence_analysis["pattern"] == "plateau":
709
+ recommendations.append("Convergence plateaued - try different learning rate or algorithm parameters")
710
+ elif convergence_analysis["pattern"] == "unstable":
711
+ recommendations.append("Unstable convergence - reduce learning rate or add regularization")
712
+
713
+ # Success recommendations
714
+ if trace.converged and stats["efficiency_score"] > 0.8:
715
+ recommendations.append("Excellent cycle performance - consider using as template for similar workflows")
716
+
717
+ return recommendations
718
+
719
+ def export_trace(self, trace: CycleExecutionTrace, filepath: str, format: str = "json"):
720
+ """
721
+ Export cycle trace to file for external analysis.
722
+
723
+ Args:
724
+ trace (CycleExecutionTrace): Trace to export
725
+ filepath (str): Output file path
726
+ format (str): Export format ("json", "csv")
727
+
728
+ Side Effects:
729
+ Creates file at specified path with trace data
730
+
731
+ Example:
732
+ >>> debugger.export_trace(trace, "cycle_debug.json", "json")
733
+ """
734
+ trace_data = trace.to_dict()
735
+
736
+ if format == "json":
737
+ with open(filepath, 'w') as f:
738
+ json.dump(trace_data, f, indent=2)
739
+ elif format == "csv":
740
+ import csv
741
+ with open(filepath, 'w', newline='') as f:
742
+ writer = csv.writer(f)
743
+
744
+ # Write header
745
+ writer.writerow(["iteration", "execution_time", "memory_mb", "cpu_percent", "convergence", "error"])
746
+
747
+ # Write iteration data
748
+ for iteration in trace.iterations:
749
+ writer.writerow([
750
+ iteration.iteration_number,
751
+ iteration.execution_time,
752
+ iteration.memory_usage_mb,
753
+ iteration.cpu_usage_percent,
754
+ iteration.convergence_value,
755
+ iteration.error or ""
756
+ ])
757
+ else:
758
+ raise ValueError(f"Unsupported export format: {format}")
759
+
760
+ logger.info(f"Exported cycle trace to {filepath} in {format} format")