empathy-framework 4.6.2__py3-none-any.whl → 4.6.3__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 (53) hide show
  1. {empathy_framework-4.6.2.dist-info → empathy_framework-4.6.3.dist-info}/METADATA +1 -1
  2. {empathy_framework-4.6.2.dist-info → empathy_framework-4.6.3.dist-info}/RECORD +53 -20
  3. {empathy_framework-4.6.2.dist-info → empathy_framework-4.6.3.dist-info}/WHEEL +1 -1
  4. empathy_os/__init__.py +1 -1
  5. empathy_os/cli.py +361 -32
  6. empathy_os/config/xml_config.py +8 -3
  7. empathy_os/core.py +37 -4
  8. empathy_os/leverage_points.py +2 -1
  9. empathy_os/memory/short_term.py +45 -1
  10. empathy_os/meta_workflows/agent_creator 2.py +254 -0
  11. empathy_os/meta_workflows/builtin_templates 2.py +567 -0
  12. empathy_os/meta_workflows/cli_meta_workflows 2.py +1551 -0
  13. empathy_os/meta_workflows/form_engine 2.py +304 -0
  14. empathy_os/meta_workflows/intent_detector 2.py +298 -0
  15. empathy_os/meta_workflows/pattern_learner 2.py +754 -0
  16. empathy_os/meta_workflows/session_context 2.py +398 -0
  17. empathy_os/meta_workflows/template_registry 2.py +229 -0
  18. empathy_os/meta_workflows/workflow 2.py +980 -0
  19. empathy_os/models/token_estimator.py +16 -9
  20. empathy_os/models/validation.py +7 -1
  21. empathy_os/orchestration/pattern_learner 2.py +699 -0
  22. empathy_os/orchestration/real_tools 2.py +938 -0
  23. empathy_os/orchestration/real_tools.py +4 -2
  24. empathy_os/socratic/__init__ 2.py +273 -0
  25. empathy_os/socratic/ab_testing 2.py +969 -0
  26. empathy_os/socratic/blueprint 2.py +532 -0
  27. empathy_os/socratic/cli 2.py +689 -0
  28. empathy_os/socratic/collaboration 2.py +1112 -0
  29. empathy_os/socratic/domain_templates 2.py +916 -0
  30. empathy_os/socratic/embeddings 2.py +734 -0
  31. empathy_os/socratic/engine 2.py +729 -0
  32. empathy_os/socratic/explainer 2.py +663 -0
  33. empathy_os/socratic/feedback 2.py +767 -0
  34. empathy_os/socratic/forms 2.py +624 -0
  35. empathy_os/socratic/generator 2.py +716 -0
  36. empathy_os/socratic/llm_analyzer 2.py +635 -0
  37. empathy_os/socratic/mcp_server 2.py +751 -0
  38. empathy_os/socratic/session 2.py +306 -0
  39. empathy_os/socratic/storage 2.py +635 -0
  40. empathy_os/socratic/storage.py +2 -1
  41. empathy_os/socratic/success 2.py +719 -0
  42. empathy_os/socratic/visual_editor 2.py +812 -0
  43. empathy_os/socratic/web_ui 2.py +925 -0
  44. empathy_os/tier_recommender.py +5 -2
  45. empathy_os/workflow_commands.py +11 -6
  46. empathy_os/workflows/base.py +1 -1
  47. empathy_os/workflows/batch_processing 2.py +310 -0
  48. empathy_os/workflows/release_prep_crew 2.py +968 -0
  49. empathy_os/workflows/test_coverage_boost_crew 2.py +848 -0
  50. empathy_os/workflows/test_maintenance.py +3 -2
  51. {empathy_framework-4.6.2.dist-info → empathy_framework-4.6.3.dist-info}/entry_points.txt +0 -0
  52. {empathy_framework-4.6.2.dist-info → empathy_framework-4.6.3.dist-info}/licenses/LICENSE +0 -0
  53. {empathy_framework-4.6.2.dist-info → empathy_framework-4.6.3.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,754 @@
1
+ """Pattern learning from historical meta-workflow executions.
2
+
3
+ Analyzes saved execution results to generate insights and recommendations
4
+ for optimizing future workflows.
5
+
6
+ Hybrid Storage:
7
+ - File-based storage: Persistent, human-readable execution results
8
+ - Memory-based storage: Rich semantic queries, relationship modeling
9
+
10
+ Created: 2026-01-17
11
+ Purpose: Self-optimizing meta-workflows through pattern analysis
12
+ """
13
+
14
+ import logging
15
+ from collections import defaultdict
16
+ from pathlib import Path
17
+ from typing import TYPE_CHECKING, Any
18
+
19
+ from empathy_os.meta_workflows.models import PatternInsight
20
+ from empathy_os.meta_workflows.workflow import list_execution_results, load_execution_result
21
+
22
+ if TYPE_CHECKING:
23
+ from empathy_os.memory.unified import UnifiedMemory
24
+
25
+ logger = logging.getLogger(__name__)
26
+
27
+
28
+ class PatternLearner:
29
+ """Analyzes historical workflow executions to generate insights.
30
+
31
+ Learns patterns from past executions to recommend optimizations
32
+ for future workflows.
33
+
34
+ Hybrid Architecture:
35
+ - Files: Persistent storage of execution results
36
+ - Memory: Rich semantic queries and relationship modeling
37
+
38
+ Attributes:
39
+ executions_dir: Directory where execution results are stored
40
+ memory: Optional UnifiedMemory instance for enhanced querying
41
+ """
42
+
43
+ def __init__(
44
+ self,
45
+ executions_dir: str | None = None,
46
+ memory: "UnifiedMemory | None" = None,
47
+ ):
48
+ """Initialize pattern learner with hybrid storage.
49
+
50
+ Args:
51
+ executions_dir: Directory for execution results
52
+ (default: .empathy/meta_workflows/executions/)
53
+ memory: Optional UnifiedMemory instance for enhanced querying
54
+ If provided, insights will be stored in both files and memory
55
+ """
56
+ if executions_dir is None:
57
+ executions_dir = str(
58
+ Path.home() / ".empathy" / "meta_workflows" / "executions"
59
+ )
60
+ self.executions_dir = Path(executions_dir)
61
+ self.memory = memory
62
+
63
+ logger.info(
64
+ f"Pattern learner initialized: {self.executions_dir}",
65
+ extra={"memory_enabled": memory is not None},
66
+ )
67
+
68
+ def analyze_patterns(
69
+ self, template_id: str | None = None, min_confidence: float = 0.5
70
+ ) -> list[PatternInsight]:
71
+ """Analyze patterns from historical executions.
72
+
73
+ Args:
74
+ template_id: Optional template ID to filter by
75
+ min_confidence: Minimum confidence threshold (0.0-1.0)
76
+
77
+ Returns:
78
+ List of pattern insights
79
+ """
80
+ # Load all execution results
81
+ run_ids = list_execution_results(storage_dir=str(self.executions_dir))
82
+
83
+ if not run_ids:
84
+ logger.warning("No execution results found")
85
+ return []
86
+
87
+ # Filter by template if specified
88
+ results = []
89
+ for run_id in run_ids:
90
+ try:
91
+ result = load_execution_result(
92
+ run_id, storage_dir=str(self.executions_dir)
93
+ )
94
+ if template_id is None or result.template_id == template_id:
95
+ results.append(result)
96
+ except Exception as e:
97
+ logger.warning(f"Failed to load result {run_id}: {e}")
98
+
99
+ if not results:
100
+ logger.warning(f"No results found for template: {template_id}")
101
+ return []
102
+
103
+ logger.info(f"Analyzing {len(results)} execution(s)")
104
+
105
+ # Generate insights
106
+ insights = []
107
+
108
+ # 1. Agent count patterns
109
+ insights.extend(self._analyze_agent_counts(results))
110
+
111
+ # 2. Tier performance patterns
112
+ insights.extend(self._analyze_tier_performance(results))
113
+
114
+ # 3. Cost patterns
115
+ insights.extend(self._analyze_costs(results))
116
+
117
+ # 4. Common failures
118
+ insights.extend(self._analyze_failures(results))
119
+
120
+ # Filter by confidence
121
+ insights = [i for i in insights if i.confidence >= min_confidence]
122
+
123
+ logger.info(f"Generated {len(insights)} insights")
124
+
125
+ return insights
126
+
127
+ def _analyze_agent_counts(self, results: list) -> list[PatternInsight]:
128
+ """Analyze patterns in agent counts.
129
+
130
+ Args:
131
+ results: List of workflow results
132
+
133
+ Returns:
134
+ List of insights about agent counts
135
+ """
136
+ insights = []
137
+
138
+ agent_counts = [len(r.agents_created) for r in results]
139
+
140
+ if not agent_counts:
141
+ return insights
142
+
143
+ avg_count = sum(agent_counts) / len(agent_counts)
144
+ min_count = min(agent_counts)
145
+ max_count = max(agent_counts)
146
+
147
+ # Calculate confidence based on sample size
148
+ confidence = min(len(results) / 10.0, 1.0)
149
+
150
+ insights.append(
151
+ PatternInsight(
152
+ insight_type="agent_count",
153
+ description=f"Average {avg_count:.1f} agents per workflow (range: {min_count}-{max_count})",
154
+ confidence=confidence,
155
+ data={
156
+ "average": avg_count,
157
+ "min": min_count,
158
+ "max": max_count,
159
+ "counts": agent_counts,
160
+ },
161
+ sample_size=len(results),
162
+ )
163
+ )
164
+
165
+ return insights
166
+
167
+ def _analyze_tier_performance(self, results: list) -> list[PatternInsight]:
168
+ """Analyze tier performance patterns.
169
+
170
+ Args:
171
+ results: List of workflow results
172
+
173
+ Returns:
174
+ List of insights about tier performance
175
+ """
176
+ insights = []
177
+
178
+ # Track success rate by agent role and tier
179
+ tier_stats = defaultdict(lambda: {"success": 0, "total": 0, "costs": []})
180
+
181
+ for result in results:
182
+ for agent_result in result.agent_results:
183
+ key = f"{agent_result.role}:{agent_result.tier_used}"
184
+ tier_stats[key]["total"] += 1
185
+ if agent_result.success:
186
+ tier_stats[key]["success"] += 1
187
+ tier_stats[key]["costs"].append(agent_result.cost)
188
+
189
+ # Generate insights for agents with enough data
190
+ for key, stats in tier_stats.items():
191
+ if stats["total"] >= 3: # Minimum 3 samples
192
+ role, tier = key.split(":")
193
+ success_rate = stats["success"] / stats["total"]
194
+ avg_cost = sum(stats["costs"]) / len(stats["costs"])
195
+
196
+ confidence = min(stats["total"] / 10.0, 1.0)
197
+
198
+ insights.append(
199
+ PatternInsight(
200
+ insight_type="tier_performance",
201
+ description=f"{role} succeeds {success_rate:.0%} at {tier} tier (avg cost: ${avg_cost:.2f})",
202
+ confidence=confidence,
203
+ data={
204
+ "role": role,
205
+ "tier": tier,
206
+ "success_rate": success_rate,
207
+ "avg_cost": avg_cost,
208
+ "total_runs": stats["total"],
209
+ },
210
+ sample_size=stats["total"],
211
+ )
212
+ )
213
+
214
+ return insights
215
+
216
+ def _analyze_costs(self, results: list) -> list[PatternInsight]:
217
+ """Analyze cost patterns.
218
+
219
+ Args:
220
+ results: List of workflow results
221
+
222
+ Returns:
223
+ List of insights about costs
224
+ """
225
+ insights = []
226
+
227
+ if not results:
228
+ return insights
229
+
230
+ total_costs = [r.total_cost for r in results]
231
+ avg_cost = sum(total_costs) / len(total_costs)
232
+ min_cost = min(total_costs)
233
+ max_cost = max(total_costs)
234
+
235
+ # Calculate cost by tier
236
+ tier_costs = defaultdict(list)
237
+ for result in results:
238
+ for agent_result in result.agent_results:
239
+ tier_costs[agent_result.tier_used].append(agent_result.cost)
240
+
241
+ tier_breakdown = {}
242
+ for tier, costs in tier_costs.items():
243
+ tier_breakdown[tier] = {
244
+ "avg": sum(costs) / len(costs),
245
+ "total": sum(costs),
246
+ "count": len(costs),
247
+ }
248
+
249
+ confidence = min(len(results) / 10.0, 1.0)
250
+
251
+ insights.append(
252
+ PatternInsight(
253
+ insight_type="cost_analysis",
254
+ description=f"Average workflow cost ${avg_cost:.2f} (range: ${min_cost:.2f}-${max_cost:.2f})",
255
+ confidence=confidence,
256
+ data={
257
+ "average": avg_cost,
258
+ "min": min_cost,
259
+ "max": max_cost,
260
+ "tier_breakdown": tier_breakdown,
261
+ },
262
+ sample_size=len(results),
263
+ )
264
+ )
265
+
266
+ return insights
267
+
268
+ def _analyze_failures(self, results: list) -> list[PatternInsight]:
269
+ """Analyze failure patterns.
270
+
271
+ Args:
272
+ results: List of workflow results
273
+
274
+ Returns:
275
+ List of insights about failures
276
+ """
277
+ insights = []
278
+
279
+ failed_agents = defaultdict(int)
280
+ total_agents = defaultdict(int)
281
+
282
+ for result in results:
283
+ for agent_result in result.agent_results:
284
+ total_agents[agent_result.role] += 1
285
+ if not agent_result.success:
286
+ failed_agents[agent_result.role] += 1
287
+
288
+ # Find agents with failures
289
+ for role, failure_count in failed_agents.items():
290
+ if failure_count > 0:
291
+ total = total_agents[role]
292
+ failure_rate = failure_count / total
293
+
294
+ confidence = min(total / 10.0, 1.0)
295
+
296
+ insights.append(
297
+ PatternInsight(
298
+ insight_type="failure_analysis",
299
+ description=f"{role} fails {failure_rate:.0%} of the time ({failure_count}/{total})",
300
+ confidence=confidence,
301
+ data={
302
+ "role": role,
303
+ "failure_count": failure_count,
304
+ "total_runs": total,
305
+ "failure_rate": failure_rate,
306
+ },
307
+ sample_size=total,
308
+ )
309
+ )
310
+
311
+ return insights
312
+
313
+ def get_recommendations(
314
+ self, template_id: str, min_confidence: float = 0.7
315
+ ) -> list[str]:
316
+ """Get actionable recommendations for a template.
317
+
318
+ Args:
319
+ template_id: Template ID to get recommendations for
320
+ min_confidence: Minimum confidence for recommendations
321
+
322
+ Returns:
323
+ List of recommendation strings
324
+ """
325
+ insights = self.analyze_patterns(
326
+ template_id=template_id, min_confidence=min_confidence
327
+ )
328
+
329
+ recommendations = []
330
+
331
+ for insight in insights:
332
+ if insight.insight_type == "tier_performance":
333
+ role = insight.data["role"]
334
+ tier = insight.data["tier"]
335
+ success_rate = insight.data["success_rate"]
336
+
337
+ if success_rate >= 0.9:
338
+ recommendations.append(
339
+ f"✓ {role} works well at {tier} tier ({success_rate:.0%} success)"
340
+ )
341
+ elif success_rate < 0.6:
342
+ recommendations.append(
343
+ f"⚠ {role} struggles at {tier} tier ({success_rate:.0%} success) - consider upgrading tier"
344
+ )
345
+
346
+ elif insight.insight_type == "cost_analysis":
347
+ avg_cost = insight.data["average"]
348
+ recommendations.append(
349
+ f"💰 Expected workflow cost: ${avg_cost:.2f}"
350
+ )
351
+
352
+ elif insight.insight_type == "failure_analysis":
353
+ role = insight.data["role"]
354
+ failure_rate = insight.data["failure_rate"]
355
+ if failure_rate > 0.3:
356
+ recommendations.append(
357
+ f"🔧 {role} needs attention ({failure_rate:.0%} failure rate)"
358
+ )
359
+
360
+ return recommendations
361
+
362
+ def generate_analytics_report(
363
+ self, template_id: str | None = None
364
+ ) -> dict[str, Any]:
365
+ """Generate comprehensive analytics report.
366
+
367
+ Args:
368
+ template_id: Optional template ID to filter by
369
+
370
+ Returns:
371
+ Dictionary with analytics data
372
+ """
373
+ insights = self.analyze_patterns(template_id=template_id, min_confidence=0.0)
374
+
375
+ # Group insights by type
376
+ insights_by_type = defaultdict(list)
377
+ for insight in insights:
378
+ insights_by_type[insight.insight_type].append(insight)
379
+
380
+ # Load all results for summary stats
381
+ run_ids = list_execution_results(storage_dir=str(self.executions_dir))
382
+ results = []
383
+ for run_id in run_ids:
384
+ try:
385
+ result = load_execution_result(
386
+ run_id, storage_dir=str(self.executions_dir)
387
+ )
388
+ if template_id is None or result.template_id == template_id:
389
+ results.append(result)
390
+ except Exception:
391
+ continue
392
+
393
+ # Calculate summary statistics
394
+ total_runs = len(results)
395
+ successful_runs = sum(1 for r in results if r.success)
396
+ total_cost = sum(r.total_cost for r in results)
397
+ total_agents = sum(len(r.agents_created) for r in results)
398
+
399
+ report = {
400
+ "summary": {
401
+ "total_runs": total_runs,
402
+ "successful_runs": successful_runs,
403
+ "success_rate": successful_runs / total_runs if total_runs > 0 else 0,
404
+ "total_cost": total_cost,
405
+ "avg_cost_per_run": total_cost / total_runs if total_runs > 0 else 0,
406
+ "total_agents_created": total_agents,
407
+ "avg_agents_per_run": total_agents / total_runs if total_runs > 0 else 0,
408
+ },
409
+ "insights": {
410
+ insight_type: [i.to_dict() for i in insights_list]
411
+ for insight_type, insights_list in insights_by_type.items()
412
+ },
413
+ "recommendations": self.get_recommendations(template_id)
414
+ if template_id
415
+ else [],
416
+ }
417
+
418
+ return report
419
+
420
+ # =========================================================================
421
+ # MEMORY INTEGRATION
422
+ # =========================================================================
423
+
424
+ def store_execution_in_memory(self, result: "MetaWorkflowResult") -> str | None:
425
+ """Store execution result in memory for semantic querying.
426
+
427
+ This stores execution insights in long-term memory IN ADDITION to
428
+ file-based storage. Memory enables rich semantic queries like:
429
+ - "Find workflows that succeeded with test coverage >80%"
430
+ - "Show me all workflows that used progressive tier escalation"
431
+
432
+ Args:
433
+ result: MetaWorkflowResult to store
434
+
435
+ Returns:
436
+ Pattern ID if stored successfully, None otherwise
437
+ """
438
+ if not self.memory:
439
+ logger.debug("Memory not available, skipping memory storage")
440
+ return None
441
+
442
+ try:
443
+ # Calculate tier distribution
444
+ tier_counts = defaultdict(int)
445
+ for agent_result in result.agent_results:
446
+ tier_counts[agent_result.tier_used] += 1
447
+
448
+ # Create rich metadata for semantic querying
449
+ metadata = {
450
+ "run_id": result.run_id,
451
+ "template_id": result.template_id,
452
+ "success": result.success,
453
+ "total_cost": result.total_cost,
454
+ "total_duration": result.total_duration,
455
+ "agents_created": len(result.agents_created),
456
+ "agents_succeeded": sum(1 for a in result.agent_results if a.success),
457
+ "tier_distribution": dict(tier_counts),
458
+ "form_responses": result.form_responses.responses,
459
+ "timestamp": result.timestamp,
460
+ "error": result.error,
461
+ }
462
+
463
+ # Create searchable content
464
+ content = f"""Meta-workflow execution: {result.template_id}
465
+ Run ID: {result.run_id}
466
+ Status: {'SUCCESS' if result.success else 'FAILED'}
467
+ Agents created: {len(result.agents_created)}
468
+ Total cost: ${result.total_cost:.2f}
469
+ Duration: {result.total_duration:.1f}s
470
+
471
+ Agents:
472
+ {self._format_agents_for_content(result)}
473
+
474
+ Form Responses:
475
+ {self._format_responses_for_content(result.form_responses.responses)}
476
+ """
477
+
478
+ # Store in long-term memory
479
+ storage_result = self.memory.persist_pattern(
480
+ content=content,
481
+ pattern_type="meta_workflow_execution",
482
+ classification="INTERNAL", # Workflow metadata is internal
483
+ auto_classify=False,
484
+ metadata=metadata,
485
+ )
486
+
487
+ if storage_result:
488
+ pattern_id = storage_result.get("pattern_id")
489
+ logger.info(
490
+ f"Execution stored in memory: {pattern_id}",
491
+ extra={
492
+ "run_id": result.run_id,
493
+ "template_id": result.template_id,
494
+ },
495
+ )
496
+ return pattern_id
497
+
498
+ return None
499
+
500
+ except Exception as e:
501
+ logger.error(f"Failed to store execution in memory: {e}")
502
+ return None
503
+
504
+ def _format_agents_for_content(self, result: "MetaWorkflowResult") -> str:
505
+ """Format agents for searchable content."""
506
+ lines = []
507
+ for agent_result in result.agent_results:
508
+ status = "✅" if agent_result.success else "❌"
509
+ lines.append(
510
+ f"- {status} {agent_result.role} (tier: {agent_result.tier_used}, "
511
+ f"cost: ${agent_result.cost:.2f})"
512
+ )
513
+ return "\n".join(lines)
514
+
515
+ def _format_responses_for_content(self, responses: dict) -> str:
516
+ """Format form responses for searchable content."""
517
+ lines = []
518
+ for key, value in responses.items():
519
+ lines.append(f"- {key}: {value}")
520
+ return "\n".join(lines)
521
+
522
+ def search_executions_by_context(
523
+ self,
524
+ query: str,
525
+ template_id: str | None = None,
526
+ limit: int = 10,
527
+ ) -> list["MetaWorkflowResult"]:
528
+ """Search executions using semantic memory queries.
529
+
530
+ This provides richer querying than file-based search:
531
+ - Natural language queries
532
+ - Semantic similarity matching
533
+ - Cross-template pattern recognition
534
+
535
+ Args:
536
+ query: Natural language search query
537
+ e.g., "workflows with high test coverage"
538
+ template_id: Optional filter by template
539
+ limit: Maximum results to return
540
+
541
+ Returns:
542
+ List of matching MetaWorkflowResult objects
543
+
544
+ Example:
545
+ >>> learner.search_executions_by_context(
546
+ ... "successful workflows with test coverage > 80%",
547
+ ... limit=5
548
+ ... )
549
+ """
550
+ if not self.memory:
551
+ logger.warning("Memory not available, falling back to file-based search")
552
+ return self._search_executions_files(query, template_id, limit)
553
+
554
+ try:
555
+ # Search memory patterns
556
+ patterns = self.memory.search_patterns(
557
+ query=query,
558
+ pattern_type="meta_workflow_execution",
559
+ limit=limit,
560
+ )
561
+
562
+ # Convert to MetaWorkflowResult objects
563
+ results = []
564
+ for pattern in patterns:
565
+ metadata = pattern.get("metadata", {})
566
+ run_id = metadata.get("run_id")
567
+
568
+ if run_id:
569
+ # Filter by template if specified
570
+ if template_id and metadata.get("template_id") != template_id:
571
+ continue
572
+
573
+ # Load full result from files
574
+ try:
575
+ result = load_execution_result(
576
+ run_id, storage_dir=str(self.executions_dir)
577
+ )
578
+ results.append(result)
579
+ except FileNotFoundError:
580
+ logger.warning(f"Result file not found for run_id: {run_id}")
581
+ continue
582
+
583
+ return results
584
+
585
+ except Exception as e:
586
+ logger.error(f"Memory search failed: {e}")
587
+ return self._search_executions_files(query, template_id, limit)
588
+
589
+ def _search_executions_files(
590
+ self,
591
+ query: str,
592
+ template_id: str | None,
593
+ limit: int,
594
+ ) -> list["MetaWorkflowResult"]:
595
+ """Fallback file-based search when memory is unavailable."""
596
+ # Simple keyword search in file-based storage
597
+ results = []
598
+ run_ids = list_execution_results(storage_dir=str(self.executions_dir))
599
+
600
+ for run_id in run_ids[:limit]:
601
+ try:
602
+ result = load_execution_result(run_id, storage_dir=str(self.executions_dir))
603
+
604
+ # Filter by template
605
+ if template_id and result.template_id != template_id:
606
+ continue
607
+
608
+ # Simple keyword matching
609
+ result_json = result.to_json().lower()
610
+ if query.lower() in result_json:
611
+ results.append(result)
612
+
613
+ except Exception as e:
614
+ logger.warning(f"Failed to load result {run_id}: {e}")
615
+ continue
616
+
617
+ return results[:limit]
618
+
619
+ def get_smart_recommendations(
620
+ self,
621
+ template_id: str,
622
+ form_response: "FormResponse | None" = None,
623
+ min_confidence: float = 0.7,
624
+ ) -> list[str]:
625
+ """Get context-aware recommendations enhanced by memory.
626
+
627
+ Combines statistical pattern analysis with semantic memory queries
628
+ to provide more intelligent recommendations.
629
+
630
+ Args:
631
+ template_id: Template ID to get recommendations for
632
+ form_response: Optional form responses for context-aware suggestions
633
+ min_confidence: Minimum confidence threshold
634
+
635
+ Returns:
636
+ List of recommendation strings
637
+
638
+ Example:
639
+ >>> recommendations = learner.get_smart_recommendations(
640
+ ... "python_package_publish",
641
+ ... form_response=response,
642
+ ... min_confidence=0.7
643
+ ... )
644
+ """
645
+ # Get base recommendations from statistical analysis
646
+ base_recs = self.get_recommendations(template_id, min_confidence)
647
+
648
+ # If no memory, return base recommendations
649
+ if not self.memory or not form_response:
650
+ return base_recs
651
+
652
+ # Enhance with memory-based context
653
+ try:
654
+ # Find similar past executions
655
+ query = f"Successful workflows for {template_id}"
656
+ if form_response:
657
+ # Add context from form responses
658
+ key_responses = []
659
+ for key, value in form_response.responses.items():
660
+ key_responses.append(f"{key}={value}")
661
+ query += f" with {', '.join(key_responses[:3])}"
662
+
663
+ similar_executions = self.search_executions_by_context(
664
+ query=query,
665
+ template_id=template_id,
666
+ limit=5,
667
+ )
668
+
669
+ # Generate memory-enhanced recommendations
670
+ if similar_executions:
671
+ success_rate = sum(
672
+ 1 for e in similar_executions if e.success
673
+ ) / len(similar_executions)
674
+
675
+ if success_rate >= 0.8:
676
+ base_recs.insert(
677
+ 0,
678
+ f"📊 {len(similar_executions)} similar workflows found "
679
+ f"with {success_rate:.0%} success rate",
680
+ )
681
+
682
+ # Add tier recommendations from similar executions
683
+ tier_usage = defaultdict(int)
684
+ for execution in similar_executions:
685
+ for agent_result in execution.agent_results:
686
+ tier_usage[agent_result.tier_used] += 1
687
+
688
+ if tier_usage:
689
+ most_common_tier = max(tier_usage.items(), key=lambda x: x[1])[0]
690
+ base_recs.append(
691
+ f"💡 Similar workflows typically use '{most_common_tier}' tier"
692
+ )
693
+
694
+ except Exception as e:
695
+ logger.error(f"Failed to enhance recommendations with memory: {e}")
696
+
697
+ return base_recs
698
+
699
+
700
+ # =============================================================================
701
+ # Helper functions
702
+ # =============================================================================
703
+
704
+
705
+ def print_analytics_report(report: dict[str, Any]) -> None:
706
+ """Print analytics report in human-readable format.
707
+
708
+ Args:
709
+ report: Analytics report dictionary
710
+ """
711
+ print("\n" + "=" * 70)
712
+ print("META-WORKFLOW ANALYTICS REPORT")
713
+ print("=" * 70)
714
+
715
+ # Summary
716
+ summary = report["summary"]
717
+ print("\n## Summary")
718
+ print(f"\n Total Runs: {summary['total_runs']}")
719
+ print(f" Successful: {summary['successful_runs']} ({summary['success_rate']:.0%})")
720
+ print(f" Total Cost: ${summary['total_cost']:.2f}")
721
+ print(f" Avg Cost/Run: ${summary['avg_cost_per_run']:.2f}")
722
+ print(f" Total Agents: {summary['total_agents_created']}")
723
+ print(f" Avg Agents/Run: {summary['avg_agents_per_run']:.1f}")
724
+
725
+ # Recommendations
726
+ if report.get("recommendations"):
727
+ print("\n## Recommendations")
728
+ print()
729
+ for rec in report["recommendations"]:
730
+ print(f" {rec}")
731
+
732
+ # Insights by type
733
+ insights = report.get("insights", {})
734
+
735
+ if insights.get("tier_performance"):
736
+ print("\n## Tier Performance")
737
+ print()
738
+ for insight in insights["tier_performance"]:
739
+ print(f" • {insight['description']}")
740
+ print(f" Confidence: {insight['confidence']:.0%} (n={insight['sample_size']})")
741
+
742
+ if insights.get("cost_analysis"):
743
+ print("\n## Cost Analysis")
744
+ print()
745
+ for insight in insights["cost_analysis"]:
746
+ print(f" • {insight['description']}")
747
+
748
+ if insights.get("failure_analysis"):
749
+ print("\n## Failure Analysis")
750
+ print()
751
+ for insight in insights["failure_analysis"]:
752
+ print(f" • {insight['description']}")
753
+
754
+ print("\n" + "=" * 70 + "\n")