prodigy 0.4.4

Turn ad-hoc Claude sessions into reproducible development pipelines with parallel AI agents
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
//! Semigroup implementation for aggregate results
//!
//! This module provides the `AggregateResult` type and its `Semigroup` implementation,
//! enabling consistent combination semantics across all aggregate types.
//! The `combine` operation is associative, allowing safe parallel aggregation.
//!
//! # Validation Pattern
//!
//! Following Stillwater's "pure core, imperative shell" philosophy, this module uses
//! homogeneous validation to prevent type mismatches in aggregation. The `Semigroup`
//! implementation is kept pure (only handles matching types), while validation
//! happens at boundaries using `combine_homogeneous`.

use rayon::prelude::*;
use serde_json::Value;
use std::collections::{HashMap, HashSet};
use stillwater::validation::homogeneous::{
    combine_homogeneous, DiscriminantName, TypeMismatchError,
};
use stillwater::{Semigroup, Validation};

/// Internal representation of aggregate results that can be combined
///
/// This enum represents intermediate aggregate states that can be combined
/// using the Semigroup trait. Some aggregations (like Average, Median, etc.)
/// track state that gets finalized into a final value later.
#[derive(Debug, Clone, PartialEq)]
pub enum AggregateResult {
    /// Count of items
    Count(usize),

    /// Sum of numeric values
    Sum(f64),

    /// Minimum value
    Min(Value),

    /// Maximum value
    Max(Value),

    /// Collection of all values
    Collect(Vec<Value>),

    /// Average state (sum, count) - finalized to average later
    Average(f64, usize),

    /// Median state - collects all values, computes median on finalize
    Median(Vec<f64>),

    /// Standard deviation state - collects all values
    StdDev(Vec<f64>),

    /// Variance state - collects all values
    Variance(Vec<f64>),

    /// Unique values set
    Unique(HashSet<String>),

    /// Concatenated strings
    Concat(String),

    /// Merged object/map
    Merge(HashMap<String, Value>),

    /// Flattened nested arrays
    Flatten(Vec<Value>),

    /// Sorted values (collected, sorted on finalize)
    Sort(Vec<Value>, bool), // values, descending

    /// Grouped values by key
    GroupBy(HashMap<String, Vec<Value>>),
}

impl DiscriminantName for AggregateResult {
    fn discriminant_name(&self) -> &'static str {
        match self {
            AggregateResult::Count(_) => "Count",
            AggregateResult::Sum(_) => "Sum",
            AggregateResult::Min(_) => "Min",
            AggregateResult::Max(_) => "Max",
            AggregateResult::Collect(_) => "Collect",
            AggregateResult::Average(_, _) => "Average",
            AggregateResult::Median(_) => "Median",
            AggregateResult::StdDev(_) => "StdDev",
            AggregateResult::Variance(_) => "Variance",
            AggregateResult::Unique(_) => "Unique",
            AggregateResult::Concat(_) => "Concat",
            AggregateResult::Merge(_) => "Merge",
            AggregateResult::Flatten(_) => "Flatten",
            AggregateResult::Sort(_, _) => "Sort",
            AggregateResult::GroupBy(_) => "GroupBy",
        }
    }
}

impl Semigroup for AggregateResult {
    fn combine(self, other: Self) -> Self {
        use AggregateResult::*;

        match (self, other) {
            // Numeric aggregations (use saturating arithmetic to prevent overflow)
            (Count(a), Count(b)) => Count(a.saturating_add(b)),
            (Sum(a), Sum(b)) => Sum(a + b),

            // Min/Max - compare and keep appropriate value
            (Min(a), Min(b)) => {
                if compare_values(&a, &b) == std::cmp::Ordering::Less {
                    Min(a)
                } else {
                    Min(b)
                }
            }
            (Max(a), Max(b)) => {
                if compare_values(&a, &b) == std::cmp::Ordering::Greater {
                    Max(a)
                } else {
                    Max(b)
                }
            }

            // Collection aggregations
            (Collect(mut a), Collect(b)) => {
                a.extend(b);
                Collect(a)
            }

            // Stateful aggregations (combine state, finalize later)
            (Average(sum_a, count_a), Average(sum_b, count_b)) => {
                Average(sum_a + sum_b, count_a + count_b)
            }

            (Median(mut a), Median(b)) => {
                a.extend(b);
                Median(a)
            }

            (StdDev(mut a), StdDev(b)) => {
                a.extend(b);
                StdDev(a)
            }

            (Variance(mut a), Variance(b)) => {
                a.extend(b);
                Variance(a)
            }

            // String aggregations
            (Concat(mut a), Concat(b)) => {
                a.push_str(&b);
                Concat(a)
            }

            (Unique(mut a), Unique(b)) => {
                a.extend(b);
                Unique(a)
            }

            // Map aggregations
            (Merge(mut a), Merge(b)) => {
                for (k, v) in b {
                    a.entry(k).or_insert(v);
                }
                Merge(a)
            }

            // Nested collections
            (Flatten(mut a), Flatten(b)) => {
                a.extend(b);
                Flatten(a)
            }

            // Sort - combine collections, preserve descending flag
            (Sort(mut a, desc_a), Sort(b, desc_b)) => {
                a.extend(b);
                // If flags differ, default to first one (could also panic)
                Sort(a, desc_a && desc_b)
            }

            // GroupBy - merge groups
            (GroupBy(mut a), GroupBy(b)) => {
                for (key, mut values) in b {
                    a.entry(key).or_insert_with(Vec::new).append(&mut values);
                }
                GroupBy(a)
            }

            // Incompatible types - should be validated before combining
            _ => unreachable!(
                "Type mismatch in aggregation. Use `aggregate_map_results` or \
                 `combine_homogeneous` to validate types before combining."
            ),
        }
    }
}

impl AggregateResult {
    /// Finalize aggregate result into a JSON value
    ///
    /// This converts the internal aggregate state into the final computed value.
    /// For simple aggregates (Count, Sum), this is straightforward.
    /// For stateful aggregates (Average, Median, StdDev, Variance), this performs
    /// the final computation.
    pub fn finalize(self) -> Value {
        match self {
            AggregateResult::Count(n) => Value::Number(serde_json::Number::from(n)),

            AggregateResult::Sum(s) => serde_json::Number::from_f64(s)
                .map(Value::Number)
                .unwrap_or(Value::Null),

            AggregateResult::Min(v) | AggregateResult::Max(v) => v,

            AggregateResult::Collect(values) => Value::Array(values),

            AggregateResult::Average(sum, count) => {
                if count == 0 {
                    Value::Null
                } else {
                    let avg = sum / count as f64;
                    serde_json::Number::from_f64(avg)
                        .map(Value::Number)
                        .unwrap_or(Value::Null)
                }
            }

            AggregateResult::Median(mut values) => {
                if values.is_empty() {
                    Value::Null
                } else {
                    values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                    let mid = values.len() / 2;
                    serde_json::Number::from_f64(values[mid])
                        .map(Value::Number)
                        .unwrap_or(Value::Null)
                }
            }

            AggregateResult::StdDev(values) => {
                if values.is_empty() {
                    Value::Null
                } else {
                    let mean = values.iter().sum::<f64>() / values.len() as f64;
                    let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>()
                        / values.len() as f64;
                    serde_json::Number::from_f64(variance.sqrt())
                        .map(Value::Number)
                        .unwrap_or(Value::Null)
                }
            }

            AggregateResult::Variance(values) => {
                if values.is_empty() {
                    Value::Null
                } else {
                    let mean = values.iter().sum::<f64>() / values.len() as f64;
                    let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f64>()
                        / values.len() as f64;
                    serde_json::Number::from_f64(variance)
                        .map(Value::Number)
                        .unwrap_or(Value::Null)
                }
            }

            AggregateResult::Unique(set) => {
                let values: Vec<Value> = set.into_iter().map(Value::String).collect();
                Value::Array(values)
            }

            AggregateResult::Concat(s) => Value::String(s),

            AggregateResult::Merge(map) => Value::Object(map.into_iter().collect()),

            AggregateResult::Flatten(values) => Value::Array(values),

            AggregateResult::Sort(mut values, descending) => {
                values.sort_by(|a, b| {
                    let cmp = compare_values(a, b);
                    if descending {
                        cmp.reverse()
                    } else {
                        cmp
                    }
                });
                Value::Array(values)
            }

            AggregateResult::GroupBy(groups) => {
                let obj: serde_json::Map<String, Value> = groups
                    .into_iter()
                    .map(|(k, v)| (k, Value::Array(v)))
                    .collect();
                Value::Object(obj)
            }
        }
    }
}

/// Compare two JSON values (numeric if possible, otherwise string)
fn compare_values(a: &Value, b: &Value) -> std::cmp::Ordering {
    if let (Some(a_num), Some(b_num)) = (a.as_f64(), b.as_f64()) {
        a_num
            .partial_cmp(&b_num)
            .unwrap_or(std::cmp::Ordering::Equal)
    } else {
        a.to_string().cmp(&b.to_string())
    }
}

/// Aggregate multiple results with homogeneous validation.
///
/// This function validates that all results have the same variant type before
/// combining them using the Semigroup trait. If any result has a different type,
/// ALL mismatches are accumulated and returned as errors.
///
/// # Example
///
/// ```
/// use prodigy::cook::execution::variables::semigroup::{AggregateResult, aggregate_map_results};
/// use stillwater::Validation;
///
/// let results = vec![
///     AggregateResult::Count(5),
///     AggregateResult::Count(3),
/// ];
///
/// match aggregate_map_results(results) {
///     Validation::Success(combined) => {
///         // All types matched, aggregation succeeded
///         assert_eq!(combined, AggregateResult::Count(8));
///     }
///     Validation::Failure(errors) => {
///         // Type mismatches found - ALL errors reported
///         for error in errors {
///             eprintln!("Aggregation error: {}", error);
///         }
///     }
/// }
/// ```
pub fn aggregate_map_results(
    results: Vec<AggregateResult>,
) -> Validation<AggregateResult, Vec<TypeMismatchError>> {
    if results.is_empty() {
        // Return a default Count(0) for empty results
        return Validation::success(AggregateResult::Count(0));
    }

    combine_homogeneous(results, std::mem::discriminant, TypeMismatchError::new)
}

/// Aggregate results with an initial value, using homogeneous validation.
///
/// This is useful for checkpoint-based aggregation where you have an existing
/// aggregate state and want to combine it with new results.
///
/// # Example
///
/// ```
/// use prodigy::cook::execution::variables::semigroup::{AggregateResult, aggregate_with_initial};
/// use stillwater::Validation;
///
/// let initial = AggregateResult::Count(10);
/// let new_results = vec![
///     AggregateResult::Count(5),
///     AggregateResult::Count(3),
/// ];
///
/// match aggregate_with_initial(initial, new_results) {
///     Validation::Success(combined) => {
///         assert_eq!(combined, AggregateResult::Count(18));
///     }
///     Validation::Failure(errors) => {
///         for error in errors {
///             eprintln!("Aggregation error: {}", error);
///         }
///     }
/// }
/// ```
pub fn aggregate_with_initial(
    initial: AggregateResult,
    results: Vec<AggregateResult>,
) -> Validation<AggregateResult, Vec<TypeMismatchError>> {
    let mut all_results = vec![initial];
    all_results.extend(results);
    aggregate_map_results(all_results)
}

/// Aggregate multiple results in parallel using rayon
///
/// This function leverages rayon's parallel iterator to combine aggregate results
/// concurrently. The semigroup combine operation's associativity guarantees that
/// parallel aggregation produces the same result as sequential aggregation.
///
/// # Arguments
/// * `results` - Vector of aggregate results to combine in parallel
///
/// # Returns
/// * `Some(AggregateResult)` if the vector is non-empty
/// * `None` if the vector is empty
///
/// # Performance
/// Parallel aggregation is beneficial for large datasets (typically >1000 items).
/// For smaller datasets, the overhead of parallelization may outweigh the benefits.
/// Use `aggregate_results` for small datasets.
///
/// # Example
/// ```
/// use prodigy::cook::execution::variables::semigroup::{AggregateResult, parallel_aggregate};
///
/// let results: Vec<_> = (0..10000)
///     .map(|_| AggregateResult::Count(1))
///     .collect();
///
/// let combined = parallel_aggregate(results).unwrap();
/// assert_eq!(combined, AggregateResult::Count(10000));
/// ```
pub fn parallel_aggregate(results: Vec<AggregateResult>) -> Option<AggregateResult> {
    results.into_par_iter().reduce_with(|a, b| a.combine(b))
}

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

    #[test]
    fn test_count_combine() {
        let a = AggregateResult::Count(5);
        let b = AggregateResult::Count(3);
        assert_eq!(a.combine(b), AggregateResult::Count(8));
    }

    #[test]
    fn test_sum_combine() {
        let a = AggregateResult::Sum(10.5);
        let b = AggregateResult::Sum(5.5);
        assert_eq!(a.combine(b), AggregateResult::Sum(16.0));
    }

    #[test]
    fn test_collect_combine() {
        let a = AggregateResult::Collect(vec![Value::Number(1.into())]);
        let b = AggregateResult::Collect(vec![Value::Number(2.into())]);
        let result = a.combine(b);

        match result {
            AggregateResult::Collect(values) => {
                assert_eq!(values.len(), 2);
                assert_eq!(values[0], Value::Number(1.into()));
                assert_eq!(values[1], Value::Number(2.into()));
            }
            _ => panic!("Expected Collect"),
        }
    }

    #[test]
    fn test_average_combine_and_finalize() {
        let a = AggregateResult::Average(10.0, 2); // avg: 5.0
        let b = AggregateResult::Average(20.0, 3); // avg: 6.67
        let combined = a.combine(b); // sum: 30.0, count: 5

        match combined {
            AggregateResult::Average(sum, count) => {
                assert_eq!(sum, 30.0);
                assert_eq!(count, 5);

                let final_value = AggregateResult::Average(sum, count).finalize();
                assert_eq!(
                    final_value,
                    Value::Number(serde_json::Number::from_f64(6.0).unwrap())
                );
            }
            _ => panic!("Expected Average"),
        }
    }

    #[test]
    fn test_min_combine() {
        let a = AggregateResult::Min(Value::Number(5.into()));
        let b = AggregateResult::Min(Value::Number(3.into()));
        let result = a.combine(b);

        match result {
            AggregateResult::Min(v) => assert_eq!(v, Value::Number(3.into())),
            _ => panic!("Expected Min"),
        }
    }

    #[test]
    fn test_max_combine() {
        let a = AggregateResult::Max(Value::Number(5.into()));
        let b = AggregateResult::Max(Value::Number(8.into()));
        let result = a.combine(b);

        match result {
            AggregateResult::Max(v) => assert_eq!(v, Value::Number(8.into())),
            _ => panic!("Expected Max"),
        }
    }

    #[test]
    fn test_concat_combine() {
        let a = AggregateResult::Concat("Hello, ".to_string());
        let b = AggregateResult::Concat("World!".to_string());
        let result = a.combine(b);

        match result {
            AggregateResult::Concat(s) => assert_eq!(s, "Hello, World!"),
            _ => panic!("Expected Concat"),
        }
    }

    #[test]
    fn test_unique_combine() {
        let mut set_a = HashSet::new();
        set_a.insert("a".to_string());
        set_a.insert("b".to_string());

        let mut set_b = HashSet::new();
        set_b.insert("b".to_string());
        set_b.insert("c".to_string());

        let a = AggregateResult::Unique(set_a);
        let b = AggregateResult::Unique(set_b);
        let result = a.combine(b);

        match result {
            AggregateResult::Unique(set) => {
                assert_eq!(set.len(), 3);
                assert!(set.contains("a"));
                assert!(set.contains("b"));
                assert!(set.contains("c"));
            }
            _ => panic!("Expected Unique"),
        }
    }

    #[test]
    fn test_merge_combine() {
        let mut map_a = HashMap::new();
        map_a.insert("a".to_string(), Value::Number(1.into()));
        map_a.insert("b".to_string(), Value::Number(2.into()));

        let mut map_b = HashMap::new();
        map_b.insert("b".to_string(), Value::Number(999.into())); // Should not override
        map_b.insert("c".to_string(), Value::Number(3.into()));

        let a = AggregateResult::Merge(map_a);
        let b = AggregateResult::Merge(map_b);
        let result = a.combine(b);

        match result {
            AggregateResult::Merge(map) => {
                assert_eq!(map.len(), 3);
                assert_eq!(map.get("a"), Some(&Value::Number(1.into())));
                assert_eq!(map.get("b"), Some(&Value::Number(2.into()))); // First value wins
                assert_eq!(map.get("c"), Some(&Value::Number(3.into())));
            }
            _ => panic!("Expected Merge"),
        }
    }

    #[test]
    fn test_flatten_combine() {
        let a = AggregateResult::Flatten(vec![Value::Number(1.into()), Value::Number(2.into())]);
        let b = AggregateResult::Flatten(vec![Value::Number(3.into())]);
        let result = a.combine(b);

        match result {
            AggregateResult::Flatten(values) => {
                assert_eq!(values.len(), 3);
            }
            _ => panic!("Expected Flatten"),
        }
    }

    #[test]
    fn test_median_combine_and_finalize() {
        let a = AggregateResult::Median(vec![1.0, 3.0, 5.0]);
        let b = AggregateResult::Median(vec![2.0, 4.0]);
        let combined = a.combine(b);

        let finalized = combined.finalize();
        // Median of [1, 2, 3, 4, 5] = 3
        assert_eq!(
            finalized,
            Value::Number(serde_json::Number::from_f64(3.0).unwrap())
        );
    }

    #[test]
    fn test_variance_combine_and_finalize() {
        let a = AggregateResult::Variance(vec![1.0, 2.0, 3.0]);
        let b = AggregateResult::Variance(vec![4.0, 5.0]);
        let combined = a.combine(b);

        let finalized = combined.finalize();
        // Variance of [1, 2, 3, 4, 5]: mean = 3, variance = 2
        match finalized {
            Value::Number(n) => {
                let variance = n.as_f64().unwrap();
                assert!((variance - 2.0).abs() < 0.01);
            }
            _ => panic!("Expected number"),
        }
    }

    #[test]
    fn test_group_by_combine() {
        let mut groups_a = HashMap::new();
        groups_a.insert("group1".to_string(), vec![Value::Number(1.into())]);

        let mut groups_b = HashMap::new();
        groups_b.insert("group1".to_string(), vec![Value::Number(2.into())]);
        groups_b.insert("group2".to_string(), vec![Value::Number(3.into())]);

        let a = AggregateResult::GroupBy(groups_a);
        let b = AggregateResult::GroupBy(groups_b);
        let result = a.combine(b);

        match result {
            AggregateResult::GroupBy(groups) => {
                assert_eq!(groups.len(), 2);
                assert_eq!(groups.get("group1").unwrap().len(), 2);
                assert_eq!(groups.get("group2").unwrap().len(), 1);
            }
            _ => panic!("Expected GroupBy"),
        }
    }

    #[test]
    fn test_multiple_combines() {
        let results = vec![
            AggregateResult::Count(1),
            AggregateResult::Count(2),
            AggregateResult::Count(3),
            AggregateResult::Count(4),
        ];

        let combined = results.into_iter().reduce(|a, b| a.combine(b)).unwrap();
        assert_eq!(combined, AggregateResult::Count(10));
    }

    #[test]
    fn test_finalize_count() {
        let result = AggregateResult::Count(42);
        let finalized = result.finalize();
        assert_eq!(finalized, Value::Number(42.into()));
    }

    #[test]
    fn test_finalize_sum() {
        let result = AggregateResult::Sum(123.45);
        let finalized = result.finalize();
        match finalized {
            Value::Number(n) => assert_eq!(n.as_f64().unwrap(), 123.45),
            _ => panic!("Expected number"),
        }
    }

    #[test]
    fn test_finalize_concat() {
        let result = AggregateResult::Concat("test string".to_string());
        let finalized = result.finalize();
        assert_eq!(finalized, Value::String("test string".to_string()));
    }

    #[test]
    fn test_homogeneous_aggregation_succeeds() {
        let results = vec![
            AggregateResult::Count(5),
            AggregateResult::Count(3),
            AggregateResult::Count(2),
        ];

        let result = aggregate_map_results(results);

        match result {
            Validation::Success(combined) => {
                assert_eq!(combined, AggregateResult::Count(10));
            }
            Validation::Failure(_) => panic!("Expected success"),
        }
    }

    #[test]
    fn test_heterogeneous_aggregation_returns_all_errors() {
        let results = vec![
            AggregateResult::Count(5),
            AggregateResult::Sum(10.0),       // Error at index 1
            AggregateResult::Average(6.0, 2), // Error at index 2
            AggregateResult::Count(3),
        ];

        let result = aggregate_map_results(results);

        match result {
            Validation::Failure(errors) => {
                assert_eq!(errors.len(), 2); // Both errors reported
                assert!(errors.iter().any(|e| e.index == 1 && e.got == "Sum"));
                assert!(errors.iter().any(|e| e.index == 2 && e.got == "Average"));
            }
            _ => panic!("Expected validation failure"),
        }
    }

    #[test]
    fn test_aggregate_with_initial() {
        let initial = AggregateResult::Count(10);
        let results = vec![AggregateResult::Count(5), AggregateResult::Count(3)];

        let result = aggregate_with_initial(initial, results);

        match result {
            Validation::Success(combined) => {
                assert_eq!(combined, AggregateResult::Count(18));
            }
            _ => panic!("Expected success"),
        }
    }

    #[test]
    fn test_empty_results() {
        let results: Vec<AggregateResult> = vec![];
        let result = aggregate_map_results(results);

        match result {
            Validation::Success(combined) => {
                assert_eq!(combined, AggregateResult::Count(0));
            }
            _ => panic!("Expected success with default Count(0)"),
        }
    }
}