batuta 0.7.3

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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
//! Recipe registrations for the Cookbook
//!
//! This module contains all recipe definitions, split from mod.rs for file size compliance.

use super::Recipe;

/// Register all recipes in the cookbook
pub fn register_all(cookbook: &mut super::Cookbook) {
    register_wasm_recipes(cookbook);
    register_ml_recipes(cookbook);
    register_transpilation_recipes(cookbook);
    register_distributed_recipes(cookbook);
    register_quality_recipes(cookbook);
    register_speech_recipes(cookbook);
    super::recipes_more::register_training_recipes(cookbook);
    super::recipes_more::register_data_recipes(cookbook);
    super::recipes_more::register_registry_recipes(cookbook);
    super::recipes_more::register_rag_recipes(cookbook);
    super::recipes_more::register_viz_recipes(cookbook);
    super::recipes_more::register_rlhf_recipes(cookbook);
}

// =========================================================================
// WASM Recipes
// =========================================================================

fn register_wasm_recipes(cookbook: &mut super::Cookbook) {
    // Zero-JS WASM Architecture
    cookbook.add(
            Recipe::new("wasm-zero-js", "Zero-JS WASM Application")
                .with_problem("Build interactive web apps with pure Rust/WASM, eliminating JavaScript entirely")
                .with_components(vec!["simular", "trueno", "web-sys", "wasm-bindgen"])
                .with_tags(vec!["wasm", "zero-js", "web", "canvas", "animation"])
                .with_code(r##"// Cargo.toml
[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = [
    "Window", "Document", "HtmlCanvasElement",
    "CanvasRenderingContext2d", "Element"
]}
console_error_panic_hook = "0.1"

// src/lib.rs
use std::cell::RefCell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;

struct AppState {
    canvas: web_sys::HtmlCanvasElement,
    ctx: web_sys::CanvasRenderingContext2d,
    frame: u32,
}

impl AppState {
    fn tick(&mut self) {
        self.frame += 1;
        self.render();
    }

    fn render(&self) {
        let w = self.canvas.width() as f64;
        let h = self.canvas.height() as f64;

        // Clear
        self.ctx.set_fill_style_str("#0a0a1a");
        self.ctx.fill_rect(0.0, 0.0, w, h);

        // Draw
        self.ctx.set_fill_style_str("#4ecdc4");
        self.ctx.begin_path();
        let _ = self.ctx.arc(w/2.0, h/2.0, 50.0, 0.0, std::f64::consts::PI * 2.0);
        self.ctx.fill();
    }
}

fn request_animation_frame(f: &Closure<dyn FnMut()>) {
    web_sys::window().expect("no global window")
        .request_animation_frame(f.as_ref().unchecked_ref()).expect("raf failed");
}

#[wasm_bindgen(js_name = initApp)]
pub fn init_app() -> Result<(), JsValue> {
    console_error_panic_hook::set_once();

    let window = web_sys::window().expect("no window");
    let document = window.document().expect("no document");
    let canvas = document.get_element_by_id("canvas")
        .expect("no canvas")
        .dyn_into::<web_sys::HtmlCanvasElement>()?;

    let ctx = canvas.get_context("2d")?.expect("no 2d context")
        .dyn_into::<web_sys::CanvasRenderingContext2d>()?;

    let state = Rc::new(RefCell::new(AppState { canvas, ctx, frame: 0 }));

    // Animation loop
    let f: Rc<RefCell<Option<Closure<dyn FnMut()>>>> = Rc::new(RefCell::new(None));
    let g = Rc::clone(&f);
    let state_clone = Rc::clone(&state);

    *g.borrow_mut() = Some(Closure::new(move || {
        state_clone.borrow_mut().tick();
        request_animation_frame(f.borrow().as_ref().expect("closure not set"));
    }));

    request_animation_frame(g.borrow().as_ref().expect("closure not set"));
    Ok(())
}

// index.html - ONE LINE OF JAVASCRIPT
// <script type="module">import init, { initApp } from './pkg/app.js'; init().then(initApp);</script>
"##)
                .with_related(vec!["wasm-event-handling", "wasm-canvas-rendering"])
                .with_test_code(r#"#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    #[test]
    fn test_frame_increment() {
        let mut frame = 0u32;
        frame += 1;
        assert_eq!(frame, 1);
    }

    #[test]
    fn test_circle_radius_positive() {
        let radius = 50.0_f64;
        assert!(radius > 0.0);
    }

    #[test]
    fn test_canvas_center_calculation() {
        let w = 800_u32;
        let h = 600_u32;
        let center_x = (w as f64) / 2.0;
        let center_y = (h as f64) / 2.0;
        assert_eq!(center_x, 400.0);
        assert_eq!(center_y, 300.0);
    }
}"#),
        );

    // WASM Event Handling
    cookbook.add(
            Recipe::new("wasm-event-handling", "WASM Event Handling")
                .with_problem("Handle DOM events (click, input, keypress) in pure Rust")
                .with_components(vec!["simular", "web-sys", "wasm-bindgen"])
                .with_tags(vec!["wasm", "events", "dom", "closure"])
                .with_code(r#"use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;

fn setup_button<F>(document: &web_sys::Document, id: &str, mut callback: F) -> Result<(), JsValue>
where
    F: FnMut() + 'static,
{
    if let Some(btn) = document.get_element_by_id(id) {
        let closure = Closure::wrap(Box::new(move |_: web_sys::Event| {
            callback();
        }) as Box<dyn FnMut(_)>);

        btn.add_event_listener_with_callback("click", closure.as_ref().unchecked_ref())?;
        closure.forget(); // Prevent closure from being dropped
    }
    Ok(())
}

fn setup_slider(document: &web_sys::Document, id: &str, state: Rc<RefCell<AppState>>) -> Result<(), JsValue> {
    if let Some(slider) = document.get_element_by_id(id) {
        let closure = Closure::wrap(Box::new(move |e: web_sys::Event| {
            if let Some(target) = e.target() {
                if let Some(input) = target.dyn_ref::<web_sys::HtmlInputElement>() {
                    if let Ok(value) = input.value().parse::<u32>() {
                        state.borrow_mut().set_speed(value);
                    }
                }
            }
        }) as Box<dyn FnMut(_)>);

        slider.add_event_listener_with_callback("input", closure.as_ref().unchecked_ref())?;
        closure.forget();
    }
    Ok(())
}
"#)
                .with_related(vec!["wasm-zero-js", "wasm-canvas-rendering"])
                .with_test_code(r#"#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use std::cell::RefCell;
    use std::rc::Rc;

    #[test]
    fn test_closure_state_capture() {
        let counter = Rc::new(RefCell::new(0u32));
        let counter_clone = Rc::clone(&counter);
        let increment = move || { *counter_clone.borrow_mut() += 1; };
        increment();
        assert_eq!(*counter.borrow(), 1);
    }

    #[test]
    fn test_input_value_parsing() {
        let input_value = "42";
        let parsed = input_value.parse::<u32>();
        assert_eq!(parsed.expect("unexpected failure"), 42);
    }

    #[test]
    fn test_speed_state_update() {
        let mut speed = 0_u32;
        let new_value = 10_u32;
        speed = new_value;
        assert_eq!(speed, 10);
    }
}"#),
        );

    // WASM Canvas Rendering
    cookbook.add(
        Recipe::new("wasm-canvas-rendering", "WASM Canvas 2D Rendering")
            .with_problem("Render graphics to HTML5 Canvas from Rust/WASM")
            .with_components(vec!["simular", "web-sys"])
            .with_tags(vec!["wasm", "canvas", "graphics", "rendering"])
            .with_code(
                r##"use web_sys::CanvasRenderingContext2d;

fn render(ctx: &CanvasRenderingContext2d, w: f64, h: f64, trail: &[(f64, f64)]) {
    // Clear background
    ctx.set_fill_style_str("#0f0f23");
    ctx.fill_rect(0.0, 0.0, w, h);

    // Draw grid
    ctx.set_stroke_style_str("#1a1a2e");
    ctx.set_line_width(1.0);
    for i in 1..=10 {
        let x = w * (i as f64) / 10.0;
        ctx.begin_path();
        ctx.move_to(x, 0.0);
        ctx.line_to(x, h);
        ctx.stroke();
    }

    // Draw circle with glow
    ctx.set_fill_style_str("#ffd93d");
    ctx.begin_path();
    let _ = ctx.arc(w/2.0, h/2.0, 15.0, 0.0, std::f64::consts::PI * 2.0);
    ctx.fill();

    // Semi-transparent glow
    ctx.set_global_alpha(0.3);
    ctx.begin_path();
    let _ = ctx.arc(w/2.0, h/2.0, 25.0, 0.0, std::f64::consts::PI * 2.0);
    ctx.fill();
    ctx.set_global_alpha(1.0);

    // Draw trail with fading alpha
    ctx.begin_path();
    if let Some((x, y)) = trail.first() {
        ctx.move_to(*x, *y);
    }
    for (i, (x, y)) in trail.iter().enumerate().skip(1) {
        let alpha = (i as f64) / (trail.len() as f64) * 0.5;
        ctx.set_global_alpha(alpha);
        ctx.line_to(*x, *y);
    }
    ctx.set_global_alpha(1.0);
    ctx.stroke();

    // Text labels
    ctx.set_fill_style_str("#888888");
    ctx.set_font("12px monospace");
    let _ = ctx.fill_text("Label", 100.0, 100.0);
}
"##,
            )
            .with_related(vec!["wasm-zero-js", "wasm-event-handling"])
            .with_test_code(
                r#"#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    #[test]
    fn test_grid_spacing_calculation() {
        let width = 800.0_f64;
        let divisions = 10;
        let spacing = width / (divisions as f64);
        assert_eq!(spacing, 80.0);
    }

    #[test]
    fn test_trail_alpha_fade() {
        let trail_len = 10;
        let index = 5;
        let alpha = (index as f64) / (trail_len as f64) * 0.5;
        assert!((alpha - 0.25).abs() < 0.001);
    }

    #[test]
    fn test_trail_data_structure() {
        let mut trail: Vec<(f64, f64)> = Vec::new();
        trail.push((100.0, 200.0));
        trail.push((150.0, 250.0));
        assert_eq!(trail.len(), 2);
    }
}"#,
            ),
    );
}

// =========================================================================
// ML Recipes
// =========================================================================

fn register_ml_recipes(cookbook: &mut super::Cookbook) {
    // Random Forest Classification
    cookbook.add(
        Recipe::new("ml-random-forest", "Random Forest Classification")
            .with_problem("Train a random forest classifier and export for serving")
            .with_components(vec!["aprender", "realizar", "alimentar"])
            .with_tags(vec!["ml", "classification", "random-forest", "supervised"])
            .with_code(
                r#"use aprender::prelude::*;
use alimentar::CsvReader;

// Load data
let data = CsvReader::from_path("data.csv")?.load()?;
let (X, y) = data.split_target("label")?;

// Train-test split
let (X_train, X_test, y_train, y_test) = train_test_split(&X, &y, 0.2, Some(42))?;

// Train model
let model = RandomForestClassifier::new()
    .n_estimators(100)
    .max_depth(Some(10))
    .min_samples_split(5)
    .fit(&X_train, &y_train)?;

// Evaluate
let predictions = model.predict(&X_test)?;
let accuracy = accuracy_score(&y_test, &predictions);
println!("Accuracy: {:.2}%", accuracy * 100.0);

// Export for serving
model.save_apr("model.apr")?;

// Load in realizar for inference
// realizar serve --model model.apr --port 8080
"#,
            )
            .with_related(vec!["ml-serving", "ml-preprocessing"])
            .with_test_code(
                r#"#[cfg(test)]
mod tests {
    #[test]
    fn test_random_forest_builder_config() {
        let n_estimators = 100;
        let max_depth = Some(10);
        assert_eq!(n_estimators, 100);
        assert!(max_depth.expect("unexpected failure") > 0);
    }

    #[test]
    fn test_predictions_collection() {
        let predictions = vec![0, 1, 1, 0, 1];
        assert_eq!(predictions.len(), 5);
    }

    #[test]
    fn test_accuracy_in_range() {
        let correct = 85;
        let total = 100;
        let accuracy = correct as f64 / total as f64;
        assert!(accuracy >= 0.0 && accuracy <= 1.0);
    }
}"#,
            ),
    );

    // Model Serving
    cookbook.add(
        Recipe::new("ml-serving", "Model Serving with Realizar")
            .with_problem("Deploy trained models as HTTP API or Lambda function")
            .with_components(vec!["realizar", "aprender"])
            .with_tags(vec!["ml", "serving", "inference", "api", "lambda"])
            .with_code(
                r#"// Command line serving
// realizar serve --model model.apr --port 8080

// Programmatic serving
use realizar::prelude::*;

let model = Model::load("model.apr")?;
let server = Server::new(model)
    .port(8080)
    .batch_size(32)
    .workers(4);

server.run()?;

// Lambda deployment
// realizar package --model model.apr --output lambda.zip
// aws lambda create-function --function-name my-model \
//     --runtime provided.al2 --handler bootstrap \
//     --zip-file fileb://lambda.zip

// Request format
// POST /predict
// Content-Type: application/json
// {"features": [1.0, 2.0, 3.0, 4.0]}
"#,
            )
            .with_related(vec!["ml-random-forest", "distributed-inference"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_server_config_port() {
        let port = 8080_u16;
        assert!(port >= 1024);
    }

    #[test]
    fn test_server_config_batch_and_workers() {
        let batch_size = 32;
        let workers = 4;
        assert_eq!(batch_size, 32);
        assert_eq!(workers, 4);
    }

    #[test]
    fn test_feature_vector_format() {
        let features: Vec<f32> = vec![1.0, 2.5, 3.7, 4.2];
        assert_eq!(features.len(), 4);
    }
}",
            ),
    );
}

// =========================================================================
// Transpilation Recipes
// =========================================================================

fn register_transpilation_recipes(cookbook: &mut super::Cookbook) {
    // Python to Rust
    cookbook.add(
        Recipe::new("transpile-python", "Python to Rust Migration")
            .with_problem("Convert Python ML code to Rust using depyler")
            .with_components(vec!["depyler", "aprender", "trueno", "batuta"])
            .with_tags(vec!["transpilation", "python", "migration"])
            .with_code(
                r"# Original Python code (sklearn_model.py)
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import numpy as np

X = np.random.rand(100, 4)
y = np.random.randint(0, 2, 100)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)

# Transpile with depyler
# batuta transpile sklearn_model.py --output src/model.rs

// Generated Rust code (src/model.rs)
use aprender::prelude::*;
use trueno::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let X = Tensor::rand(&[100, 4]);
    let y = Tensor::randint(0, 2, &[100]);

    let (X_train, X_test, y_train, y_test) = train_test_split(&X, &y, 0.2, None)?;
    let model = RandomForestClassifier::new()
        .n_estimators(100)
        .fit(&X_train, &y_train)?;

    Ok(())
}
",
            )
            .with_related(vec!["transpile-numpy", "quality-golden-trace"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_tensor_shape() {
        let rows = 100;
        let cols = 4;
        let total_elements = rows * cols;
        assert_eq!(total_elements, 400);
    }

    #[test]
    fn test_train_test_split_ratio() {
        let total = 100;
        let train_ratio = 0.8;
        let train_size = (total as f64 * train_ratio) as usize;
        assert_eq!(train_size, 80);
    }

    #[test]
    fn test_label_values_binary() {
        let labels = vec![0, 1, 1, 0, 1];
        assert!(labels.iter().all(|&l| l == 0 || l == 1));
    }
}",
            ),
    );

    // NumPy to Trueno
    cookbook.add(
        Recipe::new("transpile-numpy", "NumPy to Trueno Conversion")
            .with_problem("Convert NumPy operations to SIMD-accelerated Trueno")
            .with_components(vec!["depyler", "trueno"])
            .with_tags(vec!["transpilation", "numpy", "simd", "tensors"])
            .with_code(
                r"# Python NumPy
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
dot = np.dot(a, b)
matmul = np.matmul(X, W)

// Rust Trueno (SIMD-accelerated)
use trueno::prelude::*;

let a = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
let b = Tensor::from_vec(vec![5.0, 6.0, 7.0, 8.0]);
let dot = a.dot(&b);  // SIMD auto-vectorized
let matmul = X.matmul(&W);  // GPU if available
",
            )
            .with_related(vec!["transpile-python"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_vector_creation() {
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![5.0, 6.0, 7.0, 8.0];
        assert_eq!(a.len(), b.len());
    }

    #[test]
    fn test_dot_product_computation() {
        let a = vec![1.0, 2.0, 3.0, 4.0];
        let b = vec![5.0, 6.0, 7.0, 8.0];
        let dot: f64 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        assert_eq!(dot, 70.0);
    }

    #[test]
    fn test_simd_element_type() {
        let vec: Vec<f64> = vec![1.0, 2.0, 3.0];
        assert!(vec.iter().all(|x| x.is_finite()));
    }
}",
            ),
    );
}

// =========================================================================
// Distributed Recipes
// =========================================================================

fn register_distributed_recipes(cookbook: &mut super::Cookbook) {
    // Work-Stealing Distribution
    cookbook.add(
        Recipe::new("distributed-work-stealing", "Work-Stealing Task Distribution")
            .with_problem("Distribute computation across CPU cores with work-stealing")
            .with_components(vec!["repartir", "trueno"])
            .with_tags(vec!["distributed", "parallel", "work-stealing", "cpu"])
            .with_code(
                r"use repartir::prelude::*;

// Create pool with work-stealing scheduler
let pool = Pool::builder()
    .num_workers(num_cpus::get())
    .build()?;

// Submit tasks
let results: Vec<f64> = pool.map(data.chunks(1000), |chunk| {
    // Each chunk processed by a worker
    chunk.iter().map(|x| x * x).sum()
})?;

// Reduce results
let total: f64 = results.iter().sum();
",
            )
            .with_related(vec!["distributed-gpu", "distributed-remote"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_pool_worker_count() {
        let workers = 8;
        assert!(workers > 0);
    }

    #[test]
    fn test_chunk_processing() {
        let data = vec![1, 2, 3, 4, 5];
        let chunks: Vec<_> = data.chunks(2).collect();
        assert_eq!(chunks.len(), 3);
    }

    #[test]
    fn test_result_reduction() {
        let results = vec![10.0, 20.0, 30.0, 40.0];
        let total: f64 = results.iter().sum();
        assert_eq!(total, 100.0);
    }
}",
            ),
    );

    // GPU Distribution
    cookbook.add(
        Recipe::new("distributed-gpu", "GPU Compute Distribution")
            .with_problem("Offload computation to GPU using wgpu")
            .with_components(vec!["repartir", "trueno"])
            .with_tags(vec!["distributed", "gpu", "wgpu", "compute"])
            .with_code(
                r"use repartir::prelude::*;
use trueno::prelude::*;

// Create GPU executor
let executor = GpuExecutor::new()?;

// GPU-accelerated matrix multiplication
let result = executor.submit(|gpu| {
    let a = gpu.tensor(&matrix_a)?;
    let b = gpu.tensor(&matrix_b)?;
    a.matmul(&b)
})?;

// Hybrid CPU/GPU
let pool = Pool::builder()
    .add_gpu_executor(executor)
    .add_cpu_executor(CpuExecutor::new())
    .build()?;
",
            )
            .with_related(vec!["distributed-work-stealing"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_matrix_dimensions_valid() {
        let m = 1024;
        let k = 512;
        let n = 2048;
        assert!(m > 0 && k > 0 && n > 0);
    }

    #[test]
    fn test_hybrid_pool_worker_total() {
        let cpu_workers = 4;
        let gpu_workers = 2;
        let total = cpu_workers + gpu_workers;
        assert_eq!(total, 6);
    }

    #[test]
    fn test_matmul_output_shape() {
        let rows_a = 128;
        let cols_b = 256;
        let output_elements = rows_a * cols_b;
        assert_eq!(output_elements, 32768);
    }
}",
            ),
    );
}

// =========================================================================
// Quality Recipes
// =========================================================================

fn register_quality_recipes(cookbook: &mut super::Cookbook) {
    // EDD Testing Pattern
    cookbook.add(
        Recipe::new("quality-edd", "Equation-Driven Development")
            .with_problem("Implement simulations with verifiable governing equations")
            .with_components(vec!["simular", "probar", "certeza"])
            .with_tags(vec!["quality", "testing", "edd", "simulation", "falsification"])
            .with_code(
                r"use simular::prelude::*;

/// EDD Demo following the complete cycle:
/// 1. Equation - Define governing equation
/// 2. Failing Test - Test fails without implementation
/// 3. Implementation - Implement the simulation
/// 4. Verification - Test passes, equation verified
/// 5. Falsification - Demonstrate conditions that break

pub trait DemoEngine {
    /// Load from YAML configuration (YAML-first)
    fn from_yaml(yaml: &str) -> Result<Self, DemoError> where Self: Sized;

    /// Advance simulation by one timestep
    fn step(&mut self, dt: f64) -> StepResult;

    /// Verify governing equation invariants
    fn verify_invariants(&self) -> bool;

    /// Get falsification status
    fn falsification_status(&self) -> FalsificationStatus;
}

// Example: Harmonic Oscillator
// Governing equation: E = (1/2)kx² + (1/2)mv²
// Falsification: Energy drift > tolerance indicates integrator failure

impl DemoEngine for HarmonicOscillator {
    fn verify_invariants(&self) -> bool {
        let energy = self.kinetic_energy() + self.potential_energy();
        let error = (energy - self.initial_energy).abs() / self.initial_energy;
        error < 1e-9  // Energy conservation within tolerance
    }
}
",
            )
            .with_related(vec!["quality-probar", "quality-golden-trace"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_energy_conservation_invariant() {
        let kinetic = 60.0;
        let potential = 40.0;
        let initial_energy = 100.0;
        assert!((kinetic + potential - initial_energy).abs() < 1e-6);
    }

    #[test]
    fn test_timestep_positivity() {
        let dt = 0.01_f64;
        assert!(dt > 0.0);
    }

    #[test]
    fn test_falsification_detection() {
        let tolerance = 1e-9_f64;
        let error = 1e-5;
        assert!(error > tolerance);
    }
}",
            ),
    );

    // Probar Testing
    cookbook.add(
        Recipe::new("quality-probar", "Probar Property-Based Testing")
            .with_problem("Validate WASM demos with property-based and GUI coverage testing")
            .with_components(vec!["probar", "simular", "certeza"])
            .with_tags(vec!["quality", "testing", "probar", "property-testing", "gui-coverage"])
            .with_code(
                r#"use probar::prelude::*;

// Property-based tests
#[probar::property]
fn prop_tour_length_positive(cities: Vec<City>) -> bool {
    let tour = solve_tsp(&cities);
    tour.length() > 0.0
}

#[probar::property]
fn prop_energy_conserved(dt: f64) -> bool {
    let mut sim = OrbitSimulation::new();
    let e0 = sim.total_energy();
    sim.step(dt);
    let e1 = sim.total_energy();
    (e1 - e0).abs() / e0.abs() < 1e-9
}

// Metamorphic relations
#[probar::metamorphic]
fn mr_scale_invariance(scale: f64, cities: Vec<City>) {
    let tour1 = solve_tsp(&cities);
    let scaled = cities.iter().map(|c| c * scale).collect();
    let tour2 = solve_tsp(&scaled);
    assert!((tour2.length() / tour1.length() - scale).abs() < 1e-6);
}

// GUI coverage
#[probar::gui_coverage]
fn test_canvas_coverage(app: &mut App) {
    app.click_button("start");
    app.wait_frames(100);
    let coverage = app.pixel_coverage();
    assert!(coverage > 0.8, "Must render to >80% of canvas");
}
"#,
            )
            .with_related(vec!["quality-edd", "quality-certeza"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_tour_length_positivity() {
        let distances = vec![10.5, 20.3, 15.7, 8.2];
        let tour_length: f64 = distances.iter().sum();
        assert!(tour_length > 0.0);
    }

    #[test]
    fn test_energy_conservation_property() {
        let initial = 100.0_f64;
        let final_energy = 100.0_f64;
        assert!((initial - final_energy).abs() < 1e-10);
    }

    #[test]
    fn test_coverage_threshold() {
        let covered = 950;
        let total = 1000;
        let coverage = covered as f64 / total as f64;
        assert!(coverage >= 0.95);
    }
}",
            ),
    );

    // Golden Trace Comparison
    cookbook.add(
        Recipe::new("quality-golden-trace", "Golden Trace Validation")
            .with_problem("Validate transpiled code produces identical behavior to original")
            .with_components(vec!["renacer", "certeza"])
            .with_tags(vec!["quality", "validation", "trace", "transpilation"])
            .with_code(
                r#"use renacer::prelude::*;

// Capture golden trace from Python
// renacer trace python sklearn_model.py --output golden.trace

// Capture Rust trace
// renacer trace ./target/release/model --output rust.trace

// Compare traces
let comparison = renacer::compare("golden.trace", "rust.trace")?;

assert!(comparison.semantically_equivalent(),
    "Transpiled code must produce identical results");
assert!(comparison.syscall_compatible(),
    "System calls must match (file I/O, network, etc.)");

// Performance comparison
println!("Python: {:.2}ms", comparison.baseline_time_ms());
println!("Rust: {:.2}ms", comparison.target_time_ms());
println!("Speedup: {:.1}x", comparison.speedup());
"#,
            )
            .with_related(vec!["transpile-python", "quality-edd"])
            .with_test_code(
                r#"#[cfg(test)]
mod tests {
    #[test]
    fn test_semantic_equivalence_check() {
        let baseline = vec![1.0, 2.0, 3.0];
        let candidate = vec![1.0, 2.0, 3.0];
        assert_eq!(baseline, candidate);
    }

    #[test]
    fn test_speedup_calculation() {
        let baseline_ms = 1000.0_f64;
        let optimized_ms = 250.0_f64;
        let speedup = baseline_ms / optimized_ms;
        assert_eq!(speedup, 4.0);
    }

    #[test]
    fn test_trace_comparison_length() {
        let golden = vec!["step1", "step2", "step3"];
        let actual = vec!["step1", "step2", "step3"];
        assert_eq!(golden.len(), actual.len());
    }
}"#,
            ),
    );
}

// =========================================================================
// Speech Recognition Recipes
// =========================================================================

fn register_speech_recipes(cookbook: &mut super::Cookbook) {
    // Whisper ASR
    cookbook.add(
        Recipe::new("speech-whisper", "Whisper Speech Recognition")
            .with_problem("Transcribe audio to text using pure-Rust Whisper implementation")
            .with_components(vec!["whisper-apr", "aprender", "trueno"])
            .with_tags(vec!["speech", "asr", "whisper", "transcription", "wasm"])
            .with_code(
                r#"use whisper_apr::prelude::*;

// Load model (downloads from HuggingFace on first run)
let model = WhisperModel::load("tiny.en")?;

// Transcribe audio file
let audio = Audio::load("recording.wav")?;
let result = model.transcribe(&audio)?;

println!("Transcription: {}", result.text);

// With timestamps
for segment in result.segments {
    println!("[{:.2}s - {:.2}s] {}",
        segment.start, segment.end, segment.text);
}

// Streaming transcription (real-time)
let mut stream = model.stream()?;
for chunk in audio_chunks {
    if let Some(text) = stream.process(&chunk)? {
        print!("{}", text);
    }
}

// WASM deployment
// wasm-pack build --target web --features wasm
// <script type="module">
//   import init, { transcribe } from './pkg/whisper_apr.js';
//   await init();
//   const text = await transcribe(audioBuffer);
// </script>
"#,
            )
            .with_related(vec!["speech-streaming", "ml-serving"])
            .with_test_code(
                r#"#[cfg(test)]
mod tests {
    #[test]
    fn test_model_name_validation() {
        let model = "tiny.en";
        let valid = vec!["tiny", "tiny.en", "base", "base.en"];
        assert!(valid.contains(&model));
    }

    #[test]
    fn test_transcription_result_non_empty() {
        let text = "Hello world";
        assert!(!text.is_empty());
    }

    #[test]
    fn test_segment_timestamp_ordering() {
        let start = 0.5_f64;
        let end = 2.0_f64;
        assert!(start < end);
    }
}"#,
            ),
    );

    // Streaming Speech
    cookbook.add(
        Recipe::new("speech-streaming", "Real-time Speech Streaming")
            .with_problem("Process audio in real-time with low latency")
            .with_components(vec!["whisper-apr", "trueno"])
            .with_tags(vec!["speech", "streaming", "real-time", "low-latency"])
            .with_code(
                r#"use whisper_apr::streaming::*;

// Configure streaming decoder
let config = StreamConfig {
    chunk_size_ms: 500,      // Process every 500ms
    overlap_ms: 100,         // Overlap for continuity
    vad_enabled: true,       // Voice activity detection
    language: Some("en"),
};

let mut decoder = StreamingDecoder::new(model, config)?;

// Process microphone input
let mut mic = Microphone::open()?;
loop {
    let samples = mic.read_chunk()?;

    // VAD filters silence
    if let Some(result) = decoder.process(&samples)? {
        // Partial results for UI feedback
        if result.is_partial {
            print!("\r{}", result.text);
        } else {
            println!("\n[Final] {}", result.text);
        }
    }
}
"#,
            )
            .with_related(vec!["speech-whisper"])
            .with_test_code(
                r"#[cfg(test)]
mod tests {
    #[test]
    fn test_stream_config_chunk_size_positive() {
        let chunk_size_ms = 500_u32;
        assert!(chunk_size_ms > 0);
    }

    #[test]
    fn test_overlap_less_than_chunk_size() {
        let chunk_size_ms = 500;
        let overlap_ms = 100;
        assert!(overlap_ms < chunk_size_ms);
    }

    #[test]
    fn test_vad_enabled_flag() {
        let vad_enabled = true;
        assert!(vad_enabled);
    }
}",
            ),
    );
}

// =========================================================================
// Training Recipes
// =========================================================================