aprender-orchestrate 0.29.0

Sovereign AI orchestration: autonomous agents, ML serving, code analysis, and transpilation pipelines
Documentation
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! Experiment Tracking Frameworks Tree Visualization
//!
//! Displays comparison tree of Python experiment tracking frameworks
//! (MLflow, Weights & Biases, Neptune, etc.) and their PAIML Rust replacements.
//!
//! Core principle: Python experiment tracking is replaced by sovereign Rust alternatives.
//! No Python runtime dependencies permitted in production.

use serde::{Deserialize, Serialize};

/// Experiment tracking framework being compared
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ExperimentFramework {
    /// MLflow - Open source experiment tracking
    MLflow,
    /// Weights & Biases - Commercial experiment tracking
    WandB,
    /// Neptune.ai - Commercial ML metadata store
    Neptune,
    /// Comet ML - Commercial experiment tracking
    CometML,
    /// Sacred - Academic experiment tracking
    Sacred,
    /// DVC - Data Version Control with experiment tracking
    Dvc,
}

impl ExperimentFramework {
    /// Get the display name
    pub fn name(&self) -> &'static str {
        match self {
            ExperimentFramework::MLflow => "MLflow",
            ExperimentFramework::WandB => "Weights & Biases",
            ExperimentFramework::Neptune => "Neptune.ai",
            ExperimentFramework::CometML => "Comet ML",
            ExperimentFramework::Sacred => "Sacred",
            ExperimentFramework::Dvc => "DVC",
        }
    }

    /// Get the PAIML replacement
    pub fn replacement(&self) -> &'static str {
        match self {
            ExperimentFramework::MLflow => "Entrenar + Batuta",
            ExperimentFramework::WandB => "Entrenar + Trueno-Viz",
            ExperimentFramework::Neptune => "Entrenar",
            ExperimentFramework::CometML => "Entrenar + Presentar",
            ExperimentFramework::Sacred => "Entrenar",
            ExperimentFramework::Dvc => "Batuta + Trueno-DB",
        }
    }

    /// Get all frameworks
    pub fn all() -> Vec<ExperimentFramework> {
        vec![
            ExperimentFramework::MLflow,
            ExperimentFramework::WandB,
            ExperimentFramework::Neptune,
            ExperimentFramework::CometML,
            ExperimentFramework::Sacred,
            ExperimentFramework::Dvc,
        ]
    }
}

/// Integration type - all are replacements (Python eliminated)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IntegrationType {
    /// PAIML component fully replaces Python equivalent
    Replaces,
}

impl IntegrationType {
    /// Get the display code
    pub fn code(&self) -> &'static str {
        match self {
            IntegrationType::Replaces => "REP",
        }
    }
}

/// A component within a framework
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrameworkComponent {
    /// Component name
    pub name: String,
    /// Description
    pub description: String,
    /// PAIML replacement
    pub replacement: String,
    /// Sub-components
    pub sub_components: Vec<String>,
}

impl FrameworkComponent {
    fn new(name: &str, description: &str, replacement: &str) -> Self {
        Self {
            name: name.to_string(),
            description: description.to_string(),
            replacement: replacement.to_string(),
            sub_components: Vec::new(),
        }
    }

    fn with_subs(name: &str, description: &str, replacement: &str, subs: Vec<&str>) -> Self {
        Self {
            name: name.to_string(),
            description: description.to_string(),
            replacement: replacement.to_string(),
            sub_components: subs.into_iter().map(String::from).collect(),
        }
    }
}

/// A category of components
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrameworkCategory {
    /// Category name
    pub name: String,
    /// Components in this category
    pub components: Vec<FrameworkComponent>,
}

/// Integration mapping between Python and Rust
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IntegrationMapping {
    /// PAIML component
    pub paiml_component: String,
    /// Python component being replaced
    pub python_component: String,
    /// Integration type
    pub integration_type: IntegrationType,
    /// Category
    pub category: String,
}

impl IntegrationMapping {
    fn rep(paiml: &str, python: &str, category: &str) -> Self {
        Self {
            paiml_component: paiml.to_string(),
            python_component: python.to_string(),
            integration_type: IntegrationType::Replaces,
            category: category.to_string(),
        }
    }
}

/// Experiment tracking tree structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExperimentTree {
    /// Framework name
    pub framework: String,
    /// PAIML replacement
    pub replacement: String,
    /// Categories
    pub categories: Vec<FrameworkCategory>,
}

/// Build MLflow tree
pub fn build_mlflow_tree() -> ExperimentTree {
    ExperimentTree {
        framework: "MLflow".to_string(),
        replacement: "Entrenar + Batuta".to_string(),
        categories: vec![
            FrameworkCategory {
                name: "Experiment Tracking".to_string(),
                components: vec![
                    FrameworkComponent::with_subs(
                        "mlflow.start_run()",
                        "Run lifecycle management",
                        "Entrenar::ExperimentRun::new()",
                        vec!["log_param", "log_metric", "log_artifact"],
                    ),
                    FrameworkComponent::new(
                        "mlflow.log_params()",
                        "Hyperparameter logging",
                        "ExperimentRun::log_param()",
                    ),
                    FrameworkComponent::new(
                        "mlflow.log_metrics()",
                        "Metric logging",
                        "ExperimentRun::log_metric()",
                    ),
                    FrameworkComponent::new(
                        "mlflow.set_tags()",
                        "Run tagging",
                        "ExperimentRun::tags",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Model Registry".to_string(),
                components: vec![
                    FrameworkComponent::with_subs(
                        "mlflow.register_model()",
                        "Model versioning",
                        "SovereignDistribution",
                        vec!["stage_transitions", "model_versions"],
                    ),
                    FrameworkComponent::new(
                        "mlflow.pyfunc.log_model()",
                        "Model artifact storage",
                        "SovereignArtifact",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Artifact Storage".to_string(),
                components: vec![
                    FrameworkComponent::new(
                        "mlflow.log_artifact()",
                        "File storage",
                        "SovereignArtifact",
                    ),
                    FrameworkComponent::new(
                        "S3/Azure/GCS backends",
                        "Cloud storage",
                        "Batuta deploy (sovereign)",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Search & Query".to_string(),
                components: vec![
                    FrameworkComponent::new(
                        "mlflow.search_runs()",
                        "Run search",
                        "ExperimentStorage::list_runs()",
                    ),
                    FrameworkComponent::new(
                        "mlflow.search_experiments()",
                        "Experiment search",
                        "ExperimentStorage trait",
                    ),
                ],
            },
            FrameworkCategory {
                name: "GenAI / LLM".to_string(),
                components: vec![
                    FrameworkComponent::with_subs(
                        "mlflow.tracing",
                        "LLM trace capture",
                        "Realizar::trace()",
                        vec!["OpenAI", "LangChain", "LlamaIndex"],
                    ),
                    FrameworkComponent::new(
                        "mlflow.evaluate()",
                        "LLM evaluation",
                        "Entrenar::evaluate()",
                    ),
                    FrameworkComponent::new(
                        "Prompt Registry",
                        "Prompt versioning",
                        "Realizar::PromptTemplate",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Deployment".to_string(),
                components: vec![
                    FrameworkComponent::new(
                        "mlflow models serve",
                        "Model serving",
                        "Batuta serve (GGUF)",
                    ),
                    FrameworkComponent::new(
                        "MLflow Gateway",
                        "LLM gateway",
                        "Batuta serve + SpilloverRouter",
                    ),
                ],
            },
        ],
    }
}

/// Build Weights & Biases tree
pub fn build_wandb_tree() -> ExperimentTree {
    ExperimentTree {
        framework: "Weights & Biases".to_string(),
        replacement: "Entrenar + Trueno-Viz".to_string(),
        categories: vec![
            FrameworkCategory {
                name: "Experiment Tracking".to_string(),
                components: vec![
                    FrameworkComponent::new(
                        "wandb.init()",
                        "Run initialization",
                        "Entrenar::ExperimentRun::new()",
                    ),
                    FrameworkComponent::new(
                        "wandb.log()",
                        "Metric logging",
                        "ExperimentRun::log_metric()",
                    ),
                    FrameworkComponent::new(
                        "wandb.config",
                        "Hyperparameter config",
                        "ExperimentRun::hyperparameters",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Visualization".to_string(),
                components: vec![
                    FrameworkComponent::new("wandb.plot", "Custom plots", "Trueno-Viz::Chart"),
                    FrameworkComponent::new("Tables", "Data tables", "Trueno-Viz::DataGrid"),
                    FrameworkComponent::new(
                        "Media logging",
                        "Images/audio/video",
                        "Presentar::MediaView",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Sweeps".to_string(),
                components: vec![FrameworkComponent::with_subs(
                    "wandb.sweep()",
                    "Hyperparameter search",
                    "Entrenar::HyperparameterSearch",
                    vec!["grid", "random", "bayes"],
                )],
            },
            FrameworkCategory {
                name: "Artifacts".to_string(),
                components: vec![FrameworkComponent::new(
                    "wandb.Artifact",
                    "Dataset/model versioning",
                    "SovereignArtifact",
                )],
            },
        ],
    }
}

/// Build Neptune tree
pub fn build_neptune_tree() -> ExperimentTree {
    ExperimentTree {
        framework: "Neptune.ai".to_string(),
        replacement: "Entrenar".to_string(),
        categories: vec![
            FrameworkCategory {
                name: "Experiment Tracking".to_string(),
                components: vec![
                    FrameworkComponent::new(
                        "neptune.init_run()",
                        "Run initialization",
                        "Entrenar::ExperimentRun::new()",
                    ),
                    FrameworkComponent::new(
                        "run[\"metric\"].log()",
                        "Metric logging",
                        "ExperimentRun::log_metric()",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Metadata Store".to_string(),
                components: vec![
                    FrameworkComponent::new(
                        "Namespace hierarchy",
                        "Nested metadata",
                        "ExperimentRun::hyperparameters (JSON)",
                    ),
                    FrameworkComponent::new(
                        "System metrics",
                        "CPU/GPU/memory",
                        "EnergyMetrics + ComputeDevice",
                    ),
                ],
            },
        ],
    }
}

/// Build DVC tree
pub fn build_dvc_tree() -> ExperimentTree {
    ExperimentTree {
        framework: "DVC".to_string(),
        replacement: "Batuta + Trueno-DB".to_string(),
        categories: vec![
            FrameworkCategory {
                name: "Data Versioning".to_string(),
                components: vec![
                    FrameworkComponent::new(
                        "dvc add",
                        "Track data files",
                        "Trueno-DB::DatasetVersion",
                    ),
                    FrameworkComponent::new(
                        "dvc push/pull",
                        "Remote storage",
                        "SovereignDistribution",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Experiment Tracking".to_string(),
                components: vec![
                    FrameworkComponent::new("dvc exp run", "Run experiments", "Batuta orchestrate"),
                    FrameworkComponent::new(
                        "dvc exp show",
                        "Compare experiments",
                        "Batuta experiment tree",
                    ),
                    FrameworkComponent::new(
                        "dvc metrics",
                        "Metric tracking",
                        "ExperimentRun::metrics",
                    ),
                ],
            },
            FrameworkCategory {
                name: "Pipelines".to_string(),
                components: vec![
                    FrameworkComponent::new("dvc.yaml", "Pipeline definition", "batuta.toml"),
                    FrameworkComponent::new(
                        "dvc repro",
                        "Reproduce pipeline",
                        "Batuta transpile + validate",
                    ),
                ],
            },
        ],
    }
}

/// Build all integration mappings
pub fn build_integration_mappings() -> Vec<IntegrationMapping> {
    vec![
        // Experiment Tracking
        IntegrationMapping::rep(
            "Entrenar::ExperimentRun",
            "mlflow.start_run()",
            "Experiment Tracking",
        ),
        IntegrationMapping::rep(
            "ExperimentRun::log_metric()",
            "mlflow.log_metrics()",
            "Experiment Tracking",
        ),
        IntegrationMapping::rep(
            "ExperimentRun::log_param()",
            "mlflow.log_params()",
            "Experiment Tracking",
        ),
        IntegrationMapping::rep("ExperimentRun::tags", "mlflow.set_tags()", "Experiment Tracking"),
        IntegrationMapping::rep("Entrenar::ExperimentRun", "wandb.init()", "Experiment Tracking"),
        IntegrationMapping::rep(
            "Entrenar::ExperimentRun",
            "neptune.init_run()",
            "Experiment Tracking",
        ),
        // Model Registry
        IntegrationMapping::rep(
            "SovereignDistribution",
            "mlflow.register_model()",
            "Model Registry",
        ),
        IntegrationMapping::rep("SovereignArtifact", "mlflow.pyfunc.log_model()", "Model Registry"),
        IntegrationMapping::rep("SovereignArtifact", "wandb.Artifact", "Model Registry"),
        // Cost & Energy
        IntegrationMapping::rep("EnergyMetrics", "System metrics (wandb/neptune)", "Cost & Energy"),
        IntegrationMapping::rep("CostMetrics", "N/A (not in MLflow)", "Cost & Energy"),
        IntegrationMapping::rep(
            "CostPerformanceBenchmark",
            "N/A (Pareto frontier)",
            "Cost & Energy",
        ),
        IntegrationMapping::rep("ComputeDevice", "mlflow.system_metrics", "Cost & Energy"),
        // Visualization
        IntegrationMapping::rep("Trueno-Viz::Chart", "wandb.plot", "Visualization"),
        IntegrationMapping::rep("Trueno-Viz::DataGrid", "wandb.Table", "Visualization"),
        IntegrationMapping::rep("Presentar::Dashboard", "MLflow UI", "Visualization"),
        // LLM / GenAI
        IntegrationMapping::rep("Realizar::trace()", "mlflow.tracing", "LLM / GenAI"),
        IntegrationMapping::rep("Entrenar::evaluate()", "mlflow.evaluate()", "LLM / GenAI"),
        IntegrationMapping::rep(
            "Realizar::PromptTemplate",
            "MLflow Prompt Registry",
            "LLM / GenAI",
        ),
        // Deployment
        IntegrationMapping::rep("Batuta serve", "mlflow models serve", "Deployment"),
        IntegrationMapping::rep("SpilloverRouter", "MLflow Gateway", "Deployment"),
        IntegrationMapping::rep("SovereignDistribution", "MLflow Docker/K8s", "Deployment"),
        // Data Versioning
        IntegrationMapping::rep("Trueno-DB::DatasetVersion", "dvc add", "Data Versioning"),
        IntegrationMapping::rep("Batuta orchestrate", "dvc repro", "Data Versioning"),
        // Academic / Research
        IntegrationMapping::rep("ResearchArtifact", "N/A (ORCID/CRediT)", "Academic / Research"),
        IntegrationMapping::rep("CitationMetadata", "N/A (BibTeX/CFF)", "Academic / Research"),
        IntegrationMapping::rep("PreRegistration", "N/A (reproducibility)", "Academic / Research"),
    ]
}

/// Format a category's components as ASCII tree lines.
fn format_category_components(
    output: &mut String,
    category: &FrameworkCategory,
    cat_continuation: &str,
) {
    for (comp_idx, component) in category.components.iter().enumerate() {
        let is_last_comp = comp_idx == category.components.len() - 1;
        let comp_prefix = if is_last_comp { "└──" } else { "├──" };

        output.push_str(&format!(
            "{}{} {}{}\n",
            cat_continuation, comp_prefix, component.name, component.replacement
        ));

        if !component.sub_components.is_empty() {
            let sub_cont = if is_last_comp {
                format!("{}    ", cat_continuation)
            } else {
                format!("{}", cat_continuation)
            };
            for (sub_idx, sub) in component.sub_components.iter().enumerate() {
                let sub_prefix = if sub_idx == component.sub_components.len() - 1 {
                    "└──"
                } else {
                    "├──"
                };
                output.push_str(&format!("{}{} {}\n", sub_cont, sub_prefix, sub));
            }
        }
    }
}

/// Format a single framework tree as ASCII
pub fn format_framework_tree(tree: &ExperimentTree) -> String {
    let mut output = String::new();
    output.push_str(&format!("{} (Python) → {} (Rust)\n", tree.framework, tree.replacement));

    for (cat_idx, category) in tree.categories.iter().enumerate() {
        let is_last_cat = cat_idx == tree.categories.len() - 1;
        let cat_prefix = if is_last_cat { "└──" } else { "├──" };
        let cat_continuation = if is_last_cat { "    " } else { "" };

        output.push_str(&format!("{} {}\n", cat_prefix, category.name));
        format_category_components(&mut output, category, cat_continuation);
    }

    output
}

/// Format all frameworks
pub fn format_all_frameworks() -> String {
    let mut output = String::new();
    output.push_str("EXPERIMENT TRACKING FRAMEWORKS ECOSYSTEM\n");
    output.push_str("========================================\n\n");

    output.push_str(&format_framework_tree(&build_mlflow_tree()));
    output.push('\n');
    output.push_str(&format_framework_tree(&build_wandb_tree()));
    output.push('\n');
    output.push_str(&format_framework_tree(&build_neptune_tree()));
    output.push('\n');
    output.push_str(&format_framework_tree(&build_dvc_tree()));

    output.push_str(&format!(
        "\nSummary: {} Python frameworks replaced by sovereign Rust stack\n",
        ExperimentFramework::all().len()
    ));

    output
}

/// Format integration mappings
pub fn format_integration_mappings() -> String {
    let mappings = build_integration_mappings();
    let mut output = String::new();

    output.push_str("PAIML REPLACEMENTS FOR PYTHON EXPERIMENT TRACKING\n");
    output.push_str("=================================================\n\n");

    // Group by category
    let categories = [
        "Experiment Tracking",
        "Model Registry",
        "Cost & Energy",
        "Visualization",
        "LLM / GenAI",
        "Deployment",
        "Data Versioning",
        "Academic / Research",
    ];

    for category in categories {
        let cat_mappings: Vec<_> = mappings.iter().filter(|m| m.category == category).collect();

        if cat_mappings.is_empty() {
            continue;
        }

        output.push_str(&format!("{}\n", category.to_uppercase()));

        for (idx, mapping) in cat_mappings.iter().enumerate() {
            let is_last = idx == cat_mappings.len() - 1;
            let prefix = if is_last { "└──" } else { "├──" };

            output.push_str(&format!(
                "{} [{}] {}{}\n",
                prefix,
                mapping.integration_type.code(),
                mapping.paiml_component,
                mapping.python_component
            ));
        }
        output.push('\n');
    }

    output.push_str("Legend: [REP]=Replaces (Python eliminated)\n\n");
    output.push_str(&format!(
        "Summary: {} Python components replaced by sovereign Rust alternatives\n",
        mappings.len()
    ));
    output.push_str("         Zero Python dependencies in production\n");

    output
}

/// Format as JSON
pub fn format_json(framework: Option<ExperimentFramework>, integration: bool) -> String {
    if integration {
        let mappings = build_integration_mappings();
        serde_json::to_string_pretty(&mappings).unwrap_or_default()
    } else {
        match framework {
            Some(ExperimentFramework::MLflow) => {
                serde_json::to_string_pretty(&build_mlflow_tree()).unwrap_or_default()
            }
            Some(ExperimentFramework::WandB) => {
                serde_json::to_string_pretty(&build_wandb_tree()).unwrap_or_default()
            }
            Some(ExperimentFramework::Neptune) => {
                serde_json::to_string_pretty(&build_neptune_tree()).unwrap_or_default()
            }
            Some(ExperimentFramework::Dvc) => {
                serde_json::to_string_pretty(&build_dvc_tree()).unwrap_or_default()
            }
            Some(fw @ (ExperimentFramework::CometML | ExperimentFramework::Sacred)) => {
                // Minimal trees for these
                let tree = ExperimentTree {
                    framework: fw.name().to_string(),
                    replacement: fw.replacement().to_string(),
                    categories: vec![],
                };
                serde_json::to_string_pretty(&tree).unwrap_or_default()
            }
            None => {
                let trees = vec![
                    build_mlflow_tree(),
                    build_wandb_tree(),
                    build_neptune_tree(),
                    build_dvc_tree(),
                ];
                serde_json::to_string_pretty(&trees).unwrap_or_default()
            }
        }
    }
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
#[allow(non_snake_case)]
mod tests {
    use super::*;

    #[test]
    fn test_EXP_TREE_001_framework_names() {
        assert_eq!(ExperimentFramework::MLflow.name(), "MLflow");
        assert_eq!(ExperimentFramework::WandB.name(), "Weights & Biases");
        assert_eq!(ExperimentFramework::Neptune.name(), "Neptune.ai");
        assert_eq!(ExperimentFramework::Dvc.name(), "DVC");
    }

    #[test]
    fn test_EXP_TREE_002_framework_replacements() {
        assert_eq!(ExperimentFramework::MLflow.replacement(), "Entrenar + Batuta");
        assert_eq!(ExperimentFramework::WandB.replacement(), "Entrenar + Trueno-Viz");
        assert_eq!(ExperimentFramework::Neptune.replacement(), "Entrenar");
        assert_eq!(ExperimentFramework::Dvc.replacement(), "Batuta + Trueno-DB");
    }

    #[test]
    fn test_EXP_TREE_003_all_frameworks() {
        let all = ExperimentFramework::all();
        assert_eq!(all.len(), 6);
    }

    #[test]
    fn test_EXP_TREE_004_integration_type_code() {
        assert_eq!(IntegrationType::Replaces.code(), "REP");
    }

    #[test]
    fn test_EXP_TREE_005_mlflow_tree_structure() {
        let tree = build_mlflow_tree();
        assert_eq!(tree.framework, "MLflow");
        assert_eq!(tree.replacement, "Entrenar + Batuta");
        assert!(!tree.categories.is_empty());

        // Check experiment tracking category exists
        let exp_tracking = tree.categories.iter().find(|c| c.name == "Experiment Tracking");
        assert!(exp_tracking.is_some());
    }

    #[test]
    fn test_EXP_TREE_006_mlflow_has_genai() {
        let tree = build_mlflow_tree();
        let genai = tree.categories.iter().find(|c| c.name == "GenAI / LLM");
        assert!(genai.is_some());

        let genai = genai.expect("unexpected failure");
        assert!(genai.components.iter().any(|c| c.name == "mlflow.tracing"));
    }

    #[test]
    fn test_EXP_TREE_007_wandb_tree_structure() {
        let tree = build_wandb_tree();
        assert_eq!(tree.framework, "Weights & Biases");
        assert_eq!(tree.replacement, "Entrenar + Trueno-Viz");

        // Check visualization category
        let viz = tree.categories.iter().find(|c| c.name == "Visualization");
        assert!(viz.is_some());
    }

    #[test]
    fn test_EXP_TREE_008_neptune_tree_structure() {
        let tree = build_neptune_tree();
        assert_eq!(tree.framework, "Neptune.ai");
        assert_eq!(tree.replacement, "Entrenar");
    }

    #[test]
    fn test_EXP_TREE_009_dvc_tree_structure() {
        let tree = build_dvc_tree();
        assert_eq!(tree.framework, "DVC");
        assert_eq!(tree.replacement, "Batuta + Trueno-DB");

        // Check data versioning
        let data_ver = tree.categories.iter().find(|c| c.name == "Data Versioning");
        assert!(data_ver.is_some());
    }

    #[test]
    fn test_EXP_TREE_010_integration_mappings_count() {
        let mappings = build_integration_mappings();
        assert!(mappings.len() >= 20);
    }

    #[test]
    fn test_EXP_TREE_011_integration_mappings_categories() {
        let mappings = build_integration_mappings();
        let categories: std::collections::HashSet<_> =
            mappings.iter().map(|m| m.category.as_str()).collect();

        assert!(categories.contains("Experiment Tracking"));
        assert!(categories.contains("Model Registry"));
        assert!(categories.contains("Visualization"));
        assert!(categories.contains("LLM / GenAI"));
    }

    #[test]
    fn test_EXP_TREE_012_format_framework_tree() {
        let tree = build_mlflow_tree();
        let output = format_framework_tree(&tree);

        assert!(output.contains("MLflow (Python) → Entrenar + Batuta (Rust)"));
        assert!(output.contains("Experiment Tracking"));
        assert!(output.contains("mlflow.start_run()"));
    }

    #[test]
    fn test_EXP_TREE_013_format_all_frameworks() {
        let output = format_all_frameworks();

        assert!(output.contains("EXPERIMENT TRACKING FRAMEWORKS ECOSYSTEM"));
        assert!(output.contains("MLflow"));
        assert!(output.contains("Weights & Biases"));
        assert!(output.contains("Neptune.ai"));
        assert!(output.contains("DVC"));
        assert!(output.contains("Summary:"));
    }

    #[test]
    fn test_EXP_TREE_014_format_integration_mappings() {
        let output = format_integration_mappings();

        assert!(output.contains("PAIML REPLACEMENTS"));
        assert!(output.contains("[REP]"));
        assert!(output.contains("Entrenar::ExperimentRun"));
        assert!(output.contains("mlflow.start_run()"));
        assert!(output.contains("Legend:"));
    }

    #[test]
    fn test_EXP_TREE_015_json_output_single() {
        let json = format_json(Some(ExperimentFramework::MLflow), false);
        assert!(json.contains("MLflow"));
        assert!(json.contains("Entrenar"));

        // Verify it's valid JSON
        let parsed: Result<ExperimentTree, _> = serde_json::from_str(&json);
        assert!(parsed.is_ok());
    }

    #[test]
    fn test_EXP_TREE_016_json_output_all() {
        let json = format_json(None, false);

        // Verify it's valid JSON array
        let parsed: Result<Vec<ExperimentTree>, _> = serde_json::from_str(&json);
        assert!(parsed.is_ok());
        assert_eq!(parsed.expect("unexpected failure").len(), 4);
    }

    #[test]
    fn test_EXP_TREE_017_json_output_integration() {
        let json = format_json(None, true);

        // Verify it's valid JSON array
        let parsed: Result<Vec<IntegrationMapping>, _> = serde_json::from_str(&json);
        assert!(parsed.is_ok());
        assert!(parsed.expect("unexpected failure").len() >= 20);
    }

    #[test]
    fn test_EXP_TREE_018_all_mappings_have_replacements() {
        let mappings = build_integration_mappings();
        for mapping in &mappings {
            assert_eq!(mapping.integration_type, IntegrationType::Replaces);
            assert!(!mapping.paiml_component.is_empty());
            assert!(!mapping.python_component.is_empty());
        }
    }

    #[test]
    fn test_EXP_TREE_019_academic_features() {
        let mappings = build_integration_mappings();
        let academic: Vec<_> =
            mappings.iter().filter(|m| m.category == "Academic / Research").collect();

        assert!(academic.len() >= 3);
        assert!(academic.iter().any(|m| m.paiml_component.contains("ResearchArtifact")));
        assert!(academic.iter().any(|m| m.paiml_component.contains("CitationMetadata")));
    }

    #[test]
    fn test_EXP_TREE_020_cost_energy_features() {
        let mappings = build_integration_mappings();
        let cost_energy: Vec<_> =
            mappings.iter().filter(|m| m.category == "Cost & Energy").collect();

        assert!(cost_energy.len() >= 3);
        assert!(cost_energy.iter().any(|m| m.paiml_component.contains("EnergyMetrics")));
        assert!(cost_energy.iter().any(|m| m.paiml_component.contains("CostMetrics")));
    }
}