synth-ai-core 0.3.0

Rust core for Synth AI SDKs - API client, streaming, tracing, and tunnels
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
//! Event types and parsing for optimization job events.
//!
//! This module provides event parsing and categorization for SSE events
//! from optimization jobs.

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Event category for classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EventCategory {
    /// Baseline evaluation event
    Baseline,
    /// Candidate evaluation event
    Candidate,
    /// Pareto frontier update
    Frontier,
    /// Progress update (rollouts, trials)
    Progress,
    /// Generation complete
    Generation,
    /// Throughput metrics
    Throughput,
    /// Early termination triggered
    Termination,
    /// Job complete
    Complete,
    /// Validation phase event
    Validation,
    /// Usage/billing event
    Usage,
    /// Unknown event type
    Unknown,
}

/// Terminal status derived from terminal events.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TerminalStatus {
    Succeeded,
    Failed,
    Cancelled,
    Paused,
}

/// Parsed event path segments for logging/debugging.
#[derive(Debug, Clone, Default)]
pub struct EventPath {
    pub entity: Option<String>,
    pub action: Option<String>,
    pub algorithm: Option<String>,
    pub detail: Option<String>,
}

impl EventCategory {
    /// Check if this is a terminal event category.
    pub fn is_terminal(&self) -> bool {
        matches!(self, EventCategory::Complete | EventCategory::Termination)
    }

    /// Get the category as a string.
    pub fn as_str(&self) -> &'static str {
        match self {
            EventCategory::Baseline => "baseline",
            EventCategory::Candidate => "candidate",
            EventCategory::Frontier => "frontier",
            EventCategory::Progress => "progress",
            EventCategory::Generation => "generation",
            EventCategory::Throughput => "throughput",
            EventCategory::Termination => "termination",
            EventCategory::Complete => "complete",
            EventCategory::Validation => "validation",
            EventCategory::Usage => "usage",
            EventCategory::Unknown => "unknown",
        }
    }
}

/// Parsed event with category and typed data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsedEvent {
    /// Original event type string
    pub event_type: String,
    /// Classified category
    pub category: EventCategory,
    /// Event data payload
    pub data: Value,
    /// Sequence number for ordering
    #[serde(default)]
    pub seq: Option<i64>,
    /// Timestamp in milliseconds
    #[serde(default)]
    pub timestamp_ms: Option<i64>,
}

/// Baseline evaluation event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BaselineEvent {
    /// Baseline reward
    #[serde(default, alias = "accuracy")]
    pub reward: Option<f64>,
    /// Objective scores
    #[serde(default)]
    pub objectives: Option<HashMap<String, f64>>,
    /// Per-instance rewards
    #[serde(default, alias = "instance_scores")]
    pub instance_rewards: Option<Vec<f64>>,
    /// Per-instance objectives
    #[serde(default)]
    pub instance_objectives: Option<Vec<HashMap<String, f64>>>,
    /// Prompt configuration
    #[serde(default)]
    pub prompt: Option<Value>,
}

/// Candidate evaluation event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CandidateEvent {
    /// Candidate ID
    #[serde(default)]
    pub candidate_id: String,
    /// Candidate reward
    #[serde(default, alias = "accuracy")]
    pub reward: Option<f64>,
    /// Objective scores
    #[serde(default)]
    pub objectives: Option<HashMap<String, f64>>,
    /// Whether candidate was accepted
    #[serde(default)]
    pub accepted: bool,
    /// Generation number
    #[serde(default)]
    pub generation: Option<i32>,
    /// Parent candidate ID
    #[serde(default)]
    pub parent_id: Option<String>,
    /// Whether on Pareto frontier
    #[serde(default)]
    pub is_pareto: bool,
    /// Mutation type used
    #[serde(default)]
    pub mutation_type: Option<String>,
    /// Per-instance rewards
    #[serde(default, alias = "instance_scores")]
    pub instance_rewards: Option<Vec<f64>>,
    /// Per-instance objectives
    #[serde(default)]
    pub instance_objectives: Option<Vec<HashMap<String, f64>>>,
}

/// Frontier update event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FrontierEvent {
    /// Current frontier candidate IDs
    #[serde(default)]
    pub frontier: Vec<String>,
    /// Candidates added to frontier
    #[serde(default)]
    pub added: Vec<String>,
    /// Candidates removed from frontier
    #[serde(default)]
    pub removed: Vec<String>,
    /// Frontier size
    #[serde(default)]
    pub frontier_size: i32,
    /// Best reward on frontier
    #[serde(default, alias = "best_score")]
    pub best_reward: Option<f64>,
    /// Rewards by candidate ID
    #[serde(default, alias = "frontier_scores")]
    pub frontier_rewards: Option<HashMap<String, f64>>,
    /// Objective scores by candidate (if provided)
    #[serde(default)]
    pub frontier_objectives: Option<Vec<HashMap<String, f64>>>,
}

/// Progress event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ProgressEvent {
    /// Rollouts completed
    #[serde(default)]
    pub rollouts_completed: i32,
    /// Total rollouts planned
    #[serde(default)]
    pub rollouts_total: Option<i32>,
    /// Trials completed (MIPRO)
    #[serde(default)]
    pub trials_completed: i32,
    /// Current best reward
    #[serde(default, alias = "best_score")]
    pub best_reward: Option<f64>,
    /// Baseline reward for comparison
    #[serde(default, alias = "baseline_score")]
    pub baseline_reward: Option<f64>,
}

/// Generation complete event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GenerationEvent {
    /// Generation number
    #[serde(default)]
    pub generation: i32,
    /// Best reward in this generation
    #[serde(default, alias = "best_accuracy")]
    pub best_reward: f64,
    /// Candidates proposed
    #[serde(default)]
    pub candidates_proposed: i32,
    /// Candidates accepted
    #[serde(default)]
    pub candidates_accepted: i32,
}

/// Job complete event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CompleteEvent {
    /// Final best reward
    #[serde(default, alias = "best_score")]
    pub best_reward: Option<f64>,
    /// Baseline reward
    #[serde(default, alias = "baseline_score")]
    pub baseline_reward: Option<f64>,
    /// Finish reason
    #[serde(default)]
    pub finish_reason: Option<String>,
    /// Total candidates evaluated
    #[serde(default)]
    pub total_candidates: i32,
}

/// Termination event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TerminationEvent {
    /// Termination reason
    #[serde(default)]
    pub reason: String,
}

/// Usage event data.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct UsageEvent {
    /// Total cost in USD
    #[serde(default)]
    pub total_usd: f64,
    /// Token cost in USD
    #[serde(default)]
    pub tokens_usd: f64,
    /// Sandbox cost in USD
    #[serde(default)]
    pub sandbox_usd: f64,
}

/// Event parser for categorizing and parsing job events.
pub struct EventParser;

impl EventParser {
    /// Patterns for baseline events
    const BASELINE_PATTERNS: &'static [&'static str] = &[".baseline"];

    /// Patterns for candidate events
    const CANDIDATE_PATTERNS: &'static [&'static str] = &[
        ".candidate.evaluated",
        ".candidate.new_best",
        ".proposal.scored",
        ".optimized.scored",
        ".candidate_scored",
    ];

    /// Patterns for frontier events
    const FRONTIER_PATTERNS: &'static [&'static str] = &[".frontier_updated", ".frontier.updated"];

    /// Patterns for progress events
    const PROGRESS_PATTERNS: &'static [&'static str] = &[
        ".progress",
        ".rollouts_limit_progress",
        ".rollouts.progress",
        ".rollout.failures",
        ".proposer.invoked",
        ".job.started",
        ".trial.started",
        ".trial.completed",
        ".iteration.started",
        ".iteration.completed",
    ];

    /// Patterns for generation events
    const GENERATION_PATTERNS: &'static [&'static str] = &[
        ".generation.complete",
        ".generation.completed",
        ".generation.started",
    ];

    /// Patterns for throughput events
    const THROUGHPUT_PATTERNS: &'static [&'static str] = &[
        ".throughput",
        ".rollout.concurrency",
        ".rollout_concurrency",
    ];

    /// Patterns for termination events
    const TERMINATION_PATTERNS: &'static [&'static str] =
        &[".termination.triggered", ".termination", ".job.paused"];

    /// Patterns for complete events
    const COMPLETE_PATTERNS: &'static [&'static str] = &[
        ".complete",
        ".completed",
        ".job.completed",
        ".job.cancelled",
        ".job.canceled",
    ];

    /// Patterns for validation events
    const VALIDATION_PATTERNS: &'static [&'static str] =
        &[".validation.scored", ".validation.completed"];

    /// Patterns for usage events
    const USAGE_PATTERNS: &'static [&'static str] =
        &[".usage.recorded", ".billing.sandboxes", ".billing.updated"];

    /// Normalize event type by replacing [MASKED] with "gepa".
    pub fn normalize_type(event_type: &str) -> String {
        event_type.replace("[MASKED]", "gepa")
    }

    /// Parse an event type into path segments for logging.
    pub fn parse_path(event_type: &str) -> EventPath {
        let normalized = Self::normalize_type(event_type);
        let parts: Vec<&str> = normalized.split('.').collect();
        let entity = parts.get(0).map(|s| s.to_string());
        let action = parts.get(1).map(|s| s.to_string());
        let algorithm = parts.get(2).map(|s| s.to_string());
        let detail = if parts.len() > 3 {
            Some(parts[3..].join("."))
        } else {
            None
        };
        EventPath {
            entity,
            action,
            algorithm,
            detail,
        }
    }

    /// Map a terminal event type to a terminal status.
    pub fn terminal_status(event_type: &str) -> Option<TerminalStatus> {
        let normalized = Self::normalize_type(event_type).to_lowercase();
        if normalized.contains("cancel") {
            return Some(TerminalStatus::Cancelled);
        }
        if normalized.contains("pause") {
            return Some(TerminalStatus::Paused);
        }
        if normalized.contains("fail") || normalized.contains("error") {
            return Some(TerminalStatus::Failed);
        }
        if normalized.contains("complete") || normalized.contains("succeed") {
            return Some(TerminalStatus::Succeeded);
        }
        None
    }

    fn coerce_f64(value: Option<&Value>) -> Option<f64> {
        match value {
            Some(Value::Number(num)) => num.as_f64(),
            Some(Value::String(s)) => s.parse::<f64>().ok(),
            _ => None,
        }
    }

    fn coerce_i32(value: Option<&Value>) -> Option<i32> {
        match value {
            Some(Value::Number(num)) => num.as_i64().and_then(|v| i32::try_from(v).ok()),
            Some(Value::String(s)) => s.parse::<i32>().ok(),
            _ => None,
        }
    }

    fn coerce_bool(value: Option<&Value>) -> Option<bool> {
        match value {
            Some(Value::Bool(b)) => Some(*b),
            Some(Value::String(s)) => s.parse::<bool>().ok(),
            _ => None,
        }
    }

    fn coerce_string(value: Option<&Value>) -> Option<String> {
        match value {
            Some(Value::String(s)) => Some(s.clone()),
            Some(Value::Number(num)) => Some(num.to_string()),
            _ => None,
        }
    }

    fn parse_f64_map(value: Option<&Value>) -> Option<HashMap<String, f64>> {
        let obj = value?.as_object()?;
        if obj.is_empty() {
            return None;
        }
        let mut map = HashMap::new();
        for (k, v) in obj {
            if let Some(val) = Self::coerce_f64(Some(v)) {
                map.insert(k.clone(), val);
            } else {
                return None;
            }
        }
        Some(map)
    }

    fn parse_vec_f64_map(value: Option<&Value>) -> Option<Vec<HashMap<String, f64>>> {
        let arr = value?.as_array()?;
        if arr.is_empty() {
            return None;
        }
        let mut out = Vec::with_capacity(arr.len());
        for item in arr {
            let map = Self::parse_f64_map(Some(item))?;
            out.push(map);
        }
        Some(out)
    }

    fn parse_vec_string(value: Option<&Value>) -> Option<Vec<String>> {
        let arr = value?.as_array()?;
        if arr.is_empty() {
            return None;
        }
        let mut out = Vec::with_capacity(arr.len());
        for item in arr {
            let s = match item {
                Value::String(s) => s.clone(),
                Value::Number(n) => n.to_string(),
                _ => return None,
            };
            out.push(s);
        }
        Some(out)
    }

    fn extract_reward_from_value(value: Option<&Value>) -> Option<f64> {
        let value = value?;
        match value {
            Value::Number(num) => num.as_f64(),
            Value::String(s) => s.parse::<f64>().ok(),
            Value::Object(obj) => obj
                .get("reward")
                .and_then(|v| Self::coerce_f64(Some(v)))
                .or_else(|| {
                    obj.get("mean_reward")
                        .and_then(|v| Self::coerce_f64(Some(v)))
                })
                .or_else(|| {
                    obj.get("outcome_reward")
                        .and_then(|v| Self::coerce_f64(Some(v)))
                })
                .or_else(|| obj.get("accuracy").and_then(|v| Self::coerce_f64(Some(v))))
                .or_else(|| obj.get("score").and_then(|v| Self::coerce_f64(Some(v)))),
            _ => None,
        }
    }

    fn extract_instance_rewards(data: &Value) -> Option<Vec<f64>> {
        let instance_objectives = data.get("instance_objectives")?.as_array()?;
        if instance_objectives.is_empty() {
            return None;
        }
        let mut values = Vec::with_capacity(instance_objectives.len());
        for item in instance_objectives {
            let reward_val = if let Some(obj) = item.as_object() {
                if let Some(objectives) = obj.get("objectives").and_then(|v| v.as_object()) {
                    Self::coerce_f64(objectives.get("reward"))
                } else {
                    Self::coerce_f64(obj.get("reward"))
                }
            } else {
                None
            };
            let reward_val = reward_val?;
            values.push(reward_val);
        }
        if values.is_empty() {
            None
        } else {
            Some(values)
        }
    }

    /// Get the category for an event type string.
    pub fn get_category(event_type: &str) -> EventCategory {
        let normalized = Self::normalize_type(event_type);
        let lower = normalized.to_lowercase();

        // Generation.complete should be matched before generic .complete
        for pattern in Self::GENERATION_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Generation;
            }
        }

        for pattern in Self::BASELINE_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Baseline;
            }
        }

        for pattern in Self::CANDIDATE_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Candidate;
            }
        }

        for pattern in Self::FRONTIER_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Frontier;
            }
        }

        for pattern in Self::PROGRESS_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Progress;
            }
        }

        for pattern in Self::THROUGHPUT_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Throughput;
            }
        }

        for pattern in Self::TERMINATION_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Termination;
            }
        }

        for pattern in Self::VALIDATION_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Validation;
            }
        }

        for pattern in Self::COMPLETE_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Complete;
            }
        }

        for pattern in Self::USAGE_PATTERNS {
            if lower.contains(pattern) {
                return EventCategory::Usage;
            }
        }

        EventCategory::Unknown
    }

    /// Parse a raw event JSON into a ParsedEvent.
    pub fn parse(event: &Value) -> ParsedEvent {
        let raw_type = event.get("type").and_then(|v| v.as_str()).unwrap_or("");
        let event_type = Self::normalize_type(raw_type);
        let category = Self::get_category(&event_type);

        let seq = event.get("seq").and_then(|v| v.as_i64());

        let timestamp_ms = event
            .get("timestamp_ms")
            .and_then(|v| v.as_i64())
            .or_else(|| {
                event.get("ts").and_then(|v| v.as_str()).and_then(|ts| {
                    chrono::DateTime::parse_from_rfc3339(ts)
                        .ok()
                        .map(|dt| dt.timestamp_millis())
                })
            });

        let data = event
            .get("data")
            .and_then(|v| v.as_object())
            .cloned()
            .unwrap_or_default();

        ParsedEvent {
            event_type,
            category,
            data: Value::Object(data),
            seq,
            timestamp_ms,
        }
    }

    /// Parse baseline event data from a ParsedEvent.
    pub fn parse_baseline(event: &ParsedEvent) -> BaselineEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();
        let data_value = Value::Object(data.clone());

        let objectives = Self::parse_f64_map(data.get("objectives"));
        let reward_value = objectives.as_ref().and_then(|m| m.get("reward").copied());
        let accuracy = reward_value
            .or_else(|| Self::coerce_f64(data.get("accuracy")))
            .or_else(|| Self::coerce_f64(data.get("baseline_score")))
            .or_else(|| Self::coerce_f64(data.get("baseline_accuracy")))
            .or_else(|| {
                data.get("outcome_objectives")
                    .and_then(|v| Self::extract_reward_from_value(Some(v)))
            })
            .or_else(|| {
                data.get("outcome_reward")
                    .and_then(|v| Self::coerce_f64(Some(v)))
            })
            .or_else(|| {
                data.get("score")
                    .and_then(|v| Self::extract_reward_from_value(Some(v)))
            });

        let instance_objectives = Self::parse_vec_f64_map(data.get("instance_objectives"));
        let instance_rewards = Self::extract_instance_rewards(&data_value);
        let prompt = data.get("prompt").cloned();

        BaselineEvent {
            reward: accuracy,
            objectives,
            instance_rewards,
            instance_objectives,
            prompt,
        }
    }

    /// Parse candidate event data from a ParsedEvent.
    pub fn parse_candidate(event: &ParsedEvent) -> CandidateEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();
        let candidate_data = data
            .get("program_candidate")
            .and_then(|v| v.as_object())
            .cloned();
        let candidate_view = candidate_data.as_ref().unwrap_or(&data);
        let candidate_value = Value::Object(candidate_view.clone());

        // Extract objectives: try top-level, then score.objectives
        let objectives = Self::parse_f64_map(candidate_view.get("objectives")).or_else(|| {
            candidate_view
                .get("score")
                .and_then(|v| v.as_object())
                .and_then(|score| Self::parse_f64_map(score.get("objectives")))
        });

        let reward_value = objectives.as_ref().and_then(|m| m.get("reward").copied());

        // Extract accuracy/reward: try objectives.reward, then direct fields
        // Backend emits `reward` (top-level), `score.mean_reward`, `score.objectives.reward`
        let accuracy = reward_value
            .or_else(|| Self::coerce_f64(candidate_view.get("reward")))
            .or_else(|| Self::coerce_f64(candidate_view.get("accuracy")))
            .or_else(|| Self::coerce_f64(candidate_view.get("score")))
            .or_else(|| Self::extract_reward_from_value(candidate_view.get("score")))
            .or_else(|| {
                candidate_view
                    .get("outcome_objectives")
                    .and_then(|v| Self::extract_reward_from_value(Some(v)))
            })
            .or_else(|| {
                candidate_view
                    .get("outcome_reward")
                    .and_then(|v| Self::coerce_f64(Some(v)))
            });

        // If we found a reward but no objectives dict, construct one
        let objectives = objectives.or_else(|| {
            accuracy.map(|r| {
                let mut m = std::collections::HashMap::new();
                m.insert("reward".to_string(), r);
                m
            })
        });

        let instance_objectives =
            Self::parse_vec_f64_map(candidate_view.get("instance_objectives"));
        let instance_rewards = Self::extract_instance_rewards(&candidate_value);

        CandidateEvent {
            candidate_id: Self::coerce_string(data.get("version_id"))
                .or_else(|| Self::coerce_string(data.get("candidate_id")))
                .unwrap_or_default(),
            reward: accuracy,
            objectives,
            accepted: Self::coerce_bool(data.get("accepted")).unwrap_or(false),
            generation: Self::coerce_i32(data.get("generation")),
            parent_id: Self::coerce_string(data.get("parent_id")),
            is_pareto: Self::coerce_bool(data.get("is_pareto")).unwrap_or(false),
            mutation_type: Self::coerce_string(data.get("mutation_type"))
                .or_else(|| Self::coerce_string(data.get("operator"))),
            instance_rewards,
            instance_objectives,
        }
    }

    /// Parse frontier event data from a ParsedEvent.
    pub fn parse_frontier(event: &ParsedEvent) -> FrontierEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();
        let frontier = Self::parse_vec_string(data.get("frontier"));

        FrontierEvent {
            frontier: frontier.clone().unwrap_or_default(),
            added: Self::parse_vec_string(data.get("added")).unwrap_or_default(),
            removed: Self::parse_vec_string(data.get("removed")).unwrap_or_default(),
            frontier_size: Self::coerce_i32(data.get("frontier_size"))
                .unwrap_or_else(|| frontier.as_ref().map(|v| v.len() as i32).unwrap_or(0)),
            best_reward: Self::coerce_f64(data.get("best_score")),
            frontier_rewards: Self::parse_f64_map(data.get("frontier_scores")),
            frontier_objectives: Self::parse_vec_f64_map(data.get("frontier_objectives")),
        }
    }

    /// Parse progress event data from a ParsedEvent.
    pub fn parse_progress(event: &ParsedEvent) -> ProgressEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();

        ProgressEvent {
            rollouts_completed: Self::coerce_i32(data.get("rollouts_completed"))
                .or_else(|| Self::coerce_i32(data.get("rollouts_executed")))
                .unwrap_or(0),
            rollouts_total: Self::coerce_i32(data.get("rollouts_total"))
                .or_else(|| Self::coerce_i32(data.get("total_rollouts"))),
            trials_completed: Self::coerce_i32(data.get("trials_completed")).unwrap_or(0),
            best_reward: Self::coerce_f64(data.get("best_score")),
            baseline_reward: Self::coerce_f64(data.get("baseline_score")),
        }
    }

    /// Parse generation event data from a ParsedEvent.
    pub fn parse_generation(event: &ParsedEvent) -> GenerationEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();

        GenerationEvent {
            generation: Self::coerce_i32(data.get("generation")).unwrap_or(0),
            best_reward: Self::coerce_f64(data.get("best_accuracy")).unwrap_or(0.0),
            candidates_proposed: Self::coerce_i32(data.get("candidates_proposed")).unwrap_or(0),
            candidates_accepted: Self::coerce_i32(data.get("candidates_accepted")).unwrap_or(0),
        }
    }

    /// Parse complete event data from a ParsedEvent.
    pub fn parse_complete(event: &ParsedEvent) -> CompleteEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();

        CompleteEvent {
            best_reward: Self::coerce_f64(data.get("best_score")),
            baseline_reward: Self::coerce_f64(data.get("baseline_score")),
            finish_reason: Self::coerce_string(data.get("finish_reason"))
                .or_else(|| Self::coerce_string(data.get("reason_terminated"))),
            total_candidates: Self::coerce_i32(data.get("total_candidates")).unwrap_or(0),
        }
    }

    /// Parse termination event data from a ParsedEvent.
    pub fn parse_termination(event: &ParsedEvent) -> TerminationEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();
        TerminationEvent {
            reason: Self::coerce_string(data.get("reason"))
                .unwrap_or_else(|| "unknown".to_string()),
        }
    }

    /// Parse usage event data from a ParsedEvent.
    pub fn parse_usage(event: &ParsedEvent) -> UsageEvent {
        let data = event.data.as_object().cloned().unwrap_or_default();

        UsageEvent {
            total_usd: Self::coerce_f64(data.get("total_usd")).unwrap_or(0.0),
            tokens_usd: Self::coerce_f64(data.get("usd_tokens")).unwrap_or(0.0),
            sandbox_usd: Self::coerce_f64(data.get("sandbox_usd")).unwrap_or(0.0),
        }
    }
}

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

    #[test]
    fn test_event_category_terminal() {
        assert!(EventCategory::Complete.is_terminal());
        assert!(EventCategory::Termination.is_terminal());
        assert!(!EventCategory::Progress.is_terminal());
        assert!(!EventCategory::Candidate.is_terminal());
    }

    #[test]
    fn test_normalize_type() {
        assert_eq!(
            EventParser::normalize_type("learning.policy.[MASKED].candidate.evaluated"),
            "learning.policy.gepa.candidate.evaluated"
        );
    }

    #[test]
    fn test_get_category() {
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.baseline"),
            EventCategory::Baseline
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.candidate.evaluated"),
            EventCategory::Candidate
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.frontier_updated"),
            EventCategory::Frontier
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.progress"),
            EventCategory::Progress
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.job.completed"),
            EventCategory::Complete
        );
        assert_eq!(
            EventParser::get_category("unknown.event.type"),
            EventCategory::Unknown
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.rollout.concurrency"),
            EventCategory::Throughput
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.candidate.new_best"),
            EventCategory::Candidate
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.job.started"),
            EventCategory::Progress
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.rollout.failures"),
            EventCategory::Progress
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.proposer.invoked"),
            EventCategory::Progress
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.validation.completed"),
            EventCategory::Validation
        );
        assert_eq!(
            EventParser::get_category("learning.policy.gepa.generation.started"),
            EventCategory::Generation
        );
    }

    #[test]
    fn test_parse_event() {
        let raw = json!({
            "type": "learning.policy.gepa.candidate.evaluated",
            "seq": 42,
            "data": {
                "candidate_id": "cand_123",
                "accuracy": 0.85,
                "accepted": true,
                "generation": 2
            }
        });

        let parsed = EventParser::parse(&raw);
        assert_eq!(parsed.category, EventCategory::Candidate);
        assert_eq!(parsed.seq, Some(42));

        let candidate = EventParser::parse_candidate(&parsed);
        assert_eq!(candidate.candidate_id, "cand_123");
        assert_eq!(candidate.reward, Some(0.85));
        assert!(candidate.accepted);
        assert_eq!(candidate.generation, Some(2));
    }

    #[test]
    fn test_parse_baseline() {
        let raw = json!({
            "type": "learning.policy.gepa.baseline",
            "data": {
                "accuracy": 0.72,
                "objectives": {"score": 0.72, "cost": 0.01}
            }
        });

        let parsed = EventParser::parse(&raw);
        let baseline = EventParser::parse_baseline(&parsed);
        assert_eq!(baseline.reward, Some(0.72));
        assert!(baseline.objectives.is_some());
    }

    #[test]
    fn test_parse_frontier() {
        let raw = json!({
            "type": "learning.policy.gepa.frontier_updated",
            "data": {
                "frontier": ["cand_1", "cand_2"],
                "added": ["cand_2"],
                "removed": [],
                "best_score": 0.88
            }
        });

        let parsed = EventParser::parse(&raw);
        let frontier = EventParser::parse_frontier(&parsed);
        assert_eq!(frontier.frontier.len(), 2);
        assert_eq!(frontier.best_reward, Some(0.88));
    }
}