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
//! Prompt learning job orchestration.
//!
//! This module provides the high-level `PromptLearningJob` class for
//! submitting and tracking GEPA/MIPRO optimization jobs.

use std::collections::HashMap;
use std::time::Duration;

use reqwest::header::{HeaderMap, HeaderValue};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::api::types::PolicyJobStatus;
use crate::api::SynthClient;
use crate::auth;
use crate::errors::CoreError;

use super::events::{ParsedEvent, TerminalStatus};
use super::progress::ProgressTracker;
use crate::sse::stream_sse_events;
use futures_util::StreamExt;

/// Result from a prompt learning job.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptLearningResult {
    /// Job ID
    pub job_id: String,
    /// Current status
    pub status: PolicyJobStatus,
    /// Best reward achieved
    #[serde(default, alias = "best_score")]
    pub best_reward: Option<f64>,
    /// Best candidate configuration
    #[serde(default)]
    pub best_candidate: Option<Value>,
    /// Lever summary emitted by optimizer runtimes.
    #[serde(default)]
    pub lever_summary: Option<Value>,
    /// Sensor frame summaries emitted by optimizer runtimes.
    #[serde(default)]
    pub sensor_frames: Vec<Value>,
    /// Lever versions for the selected/best candidate.
    #[serde(default)]
    pub lever_versions: HashMap<String, i64>,
    /// Highest lever version represented in `lever_versions`.
    #[serde(default)]
    pub best_lever_version: Option<i64>,
    /// Baseline reward
    #[serde(default, alias = "baseline_score")]
    pub baseline_reward: Option<f64>,
    /// Number of candidates evaluated
    #[serde(default)]
    pub candidates_evaluated: i32,
    /// Number of generations completed
    #[serde(default)]
    pub generations_completed: i32,
    /// Error message if failed
    #[serde(default)]
    pub error: Option<String>,
    /// Raw response data
    #[serde(default)]
    pub raw: Value,
}

impl PromptLearningResult {
    /// Check if the job succeeded.
    pub fn succeeded(&self) -> bool {
        self.status == PolicyJobStatus::Succeeded
    }

    /// Check if the job failed.
    pub fn failed(&self) -> bool {
        self.status == PolicyJobStatus::Failed
    }

    /// Check if the job is in a terminal state.
    pub fn is_terminal(&self) -> bool {
        self.status.is_terminal()
    }

    /// Get the system prompt from best_candidate if available.
    pub fn get_system_prompt(&self) -> Option<String> {
        self.best_candidate.as_ref().and_then(|p| {
            // Try various paths where system prompt might be
            p.get("system_prompt")
                .and_then(|v| v.as_str())
                .or_else(|| p.get("instruction").and_then(|v| v.as_str()))
                .or_else(|| {
                    p.get("stages")
                        .and_then(|s| s.get("main"))
                        .and_then(|m| m.get("instruction"))
                        .and_then(|v| v.as_str())
                })
                .map(|s| s.to_string())
        })
    }
}

/// Ranked prompt from results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RankedPrompt {
    /// Rank (1 = best)
    pub rank: i32,
    /// Candidate ID
    pub candidate_id: String,
    /// Training accuracy
    #[serde(default)]
    pub train_accuracy: Option<f64>,
    /// Validation accuracy
    #[serde(default)]
    pub val_accuracy: Option<f64>,
    /// Prompt text or configuration
    #[serde(default)]
    pub prompt: Option<Value>,
}

/// Extracted prompt results.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptResults {
    /// Best candidate text
    #[serde(default)]
    pub best_candidate: Option<String>,
    /// Best reward
    #[serde(default, alias = "best_score")]
    pub best_reward: Option<f64>,
    /// Top prompts ranked by score
    #[serde(default)]
    pub top_prompts: Vec<RankedPrompt>,
    /// Lever summary emitted by optimizer runtimes.
    #[serde(default)]
    pub lever_summary: Option<Value>,
    /// Sensor frame summaries emitted by optimizer runtimes.
    #[serde(default)]
    pub sensor_frames: Vec<Value>,
    /// Lever versions for the selected/best candidate.
    #[serde(default)]
    pub lever_versions: HashMap<String, i64>,
    /// Highest lever version represented in `lever_versions`.
    #[serde(default)]
    pub best_lever_version: Option<i64>,
}

/// High-level prompt learning job orchestration.
pub struct PromptLearningJob {
    /// Synth API client
    client: SynthClient,
    /// Job ID (set after submit)
    job_id: Option<String>,
    /// Job configuration
    config: Value,
    /// Optional SynthTunnel worker token
    container_worker_token: Option<String>,
    /// Progress tracker
    tracker: ProgressTracker,
}

impl PromptLearningJob {
    /// Create a job from a configuration dict.
    ///
    /// # Arguments
    ///
    /// * `config` - Job configuration (algorithm, container_url, policy, etc.)
    /// * `api_key` - Optional API key (uses env if not provided)
    /// * `base_url` - Optional base URL (uses default if not provided)
    ///
    /// # Example
    ///
    /// ```ignore
    /// let job = PromptLearningJob::from_dict(
    ///     serde_json::json!({
    ///         "algorithm": "gepa",
    ///         "container_url": "http://localhost:8000",
    ///         "env_name": "default",
    ///         "policy": { "model": "gpt-4o-mini", "provider": "openai" },
    ///         "gepa": { "rollout_budget": 100 }
    ///     }),
    ///     None,
    ///     None,
    ///     None,
    /// )?;
    /// ```
    pub fn from_dict(
        config: Value,
        api_key: Option<&str>,
        base_url: Option<&str>,
        container_worker_token: Option<String>,
    ) -> Result<Self, CoreError> {
        let api_key = match api_key {
            Some(k) => k.to_string(),
            None => auth::get_api_key(None)
                .ok_or_else(|| CoreError::Authentication("SYNTH_API_KEY not found".to_string()))?,
        };

        let client = SynthClient::new(&api_key, base_url)?;

        Ok(Self {
            client,
            job_id: None,
            config,
            container_worker_token,
            tracker: ProgressTracker::new(),
        })
    }

    /// Reconnect to an existing job by ID.
    ///
    /// # Arguments
    ///
    /// * `job_id` - Existing job ID
    /// * `api_key` - Optional API key
    /// * `base_url` - Optional base URL
    pub fn from_job_id(
        job_id: &str,
        api_key: Option<&str>,
        base_url: Option<&str>,
    ) -> Result<Self, CoreError> {
        let api_key = match api_key {
            Some(k) => k.to_string(),
            None => auth::get_api_key(None)
                .ok_or_else(|| CoreError::Authentication("SYNTH_API_KEY not found".to_string()))?,
        };

        let client = SynthClient::new(&api_key, base_url)?;

        Ok(Self {
            client,
            job_id: Some(job_id.to_string()),
            config: Value::Null,
            container_worker_token: None,
            tracker: ProgressTracker::new(),
        })
    }

    /// Get the job ID (if submitted).
    pub fn job_id(&self) -> Option<&str> {
        self.job_id.as_deref()
    }

    /// Get the progress tracker.
    pub fn tracker(&self) -> &ProgressTracker {
        &self.tracker
    }

    /// Submit the job to the backend.
    ///
    /// Returns the job ID on success.
    pub async fn submit(&mut self) -> Result<String, CoreError> {
        if self.job_id.is_some() {
            return Err(CoreError::Validation("job already submitted".to_string()));
        }

        if self.config.is_null() {
            return Err(CoreError::Validation(
                "no configuration provided".to_string(),
            ));
        }

        // Submit via jobs API
        let job_id = self
            .client
            .jobs()
            .submit_raw_with_worker_token(self.config.clone(), self.container_worker_token.clone())
            .await?;
        self.job_id = Some(job_id.clone());

        Ok(job_id)
    }

    /// Get the current job status.
    pub async fn get_status(&self) -> Result<PromptLearningResult, CoreError> {
        let job_id = self
            .job_id
            .as_ref()
            .ok_or_else(|| CoreError::Validation("job not submitted yet".to_string()))?;

        let result = self.client.jobs().get_status(job_id).await?;

        Ok(PromptLearningResult {
            job_id: result.job_id,
            status: result.status,
            best_reward: result.best_reward,
            best_candidate: result.best_candidate,
            lever_summary: result.lever_summary,
            sensor_frames: result.sensor_frames,
            lever_versions: result.lever_versions,
            best_lever_version: result.best_lever_version,
            baseline_reward: None,
            candidates_evaluated: result.candidates_evaluated.unwrap_or(0),
            generations_completed: result.generations_completed.unwrap_or(0),
            error: result.error,
            raw: Value::Null,
        })
    }

    /// Poll until the job reaches a terminal state.
    ///
    /// # Arguments
    ///
    /// * `timeout_secs` - Maximum time to wait
    /// * `interval_secs` - Polling interval
    pub async fn poll_until_complete(
        &self,
        timeout_secs: f64,
        interval_secs: f64,
    ) -> Result<PromptLearningResult, CoreError> {
        let job_id = self
            .job_id
            .as_ref()
            .ok_or_else(|| CoreError::Validation("job not submitted yet".to_string()))?;

        let result = self
            .client
            .jobs()
            .poll_until_complete(job_id, timeout_secs, interval_secs)
            .await?;

        Ok(PromptLearningResult {
            job_id: result.job_id,
            status: result.status,
            best_reward: result.best_reward,
            best_candidate: result.best_candidate,
            lever_summary: result.lever_summary,
            sensor_frames: result.sensor_frames,
            lever_versions: result.lever_versions,
            best_lever_version: result.best_lever_version,
            baseline_reward: None,
            candidates_evaluated: result.candidates_evaluated.unwrap_or(0),
            generations_completed: result.generations_completed.unwrap_or(0),
            error: result.error,
            raw: Value::Null,
        })
    }

    /// Stream events until completion with callback.
    ///
    /// # Arguments
    ///
    /// * `timeout_secs` - Maximum time to wait
    /// * `on_event` - Optional callback for each event
    pub async fn stream_until_complete<F>(
        &mut self,
        timeout_secs: f64,
        mut on_event: Option<F>,
    ) -> Result<PromptLearningResult, CoreError>
    where
        F: FnMut(&ParsedEvent),
    {
        use std::cell::Cell;

        let job_id = self
            .job_id
            .as_ref()
            .ok_or_else(|| CoreError::Validation("job not submitted yet".to_string()))?;

        eprintln!(
            "[PL] stream_until_complete: job={} timeout={:.0}s",
            job_id, timeout_secs
        );

        let timeout = Duration::from_secs_f64(timeout_secs);
        let base_url = self.client.base_url().trim_end_matches('/').to_string();
        let events_url = format!(
            "{}/api/prompt-learning/online/jobs/{}/events/stream",
            base_url, job_id
        );
        let api_key = self.client.http().api_key().to_string();
        let mut headers = HeaderMap::new();
        headers.insert("Accept", HeaderValue::from_static("text/event-stream"));
        headers.insert(
            "Authorization",
            HeaderValue::from_str(&format!("Bearer {}", api_key))
                .map_err(|_| CoreError::Validation("invalid api key".to_string()))?,
        );
        headers.insert(
            "X-API-Key",
            HeaderValue::from_str(&api_key)
                .map_err(|_| CoreError::Validation("invalid api key".to_string()))?,
        );

        // Use Cell for interior mutability to satisfy borrow checker
        let terminal_reached = Cell::new(false);
        let event_count = Cell::new(0u64);
        let terminal_status = Cell::new(None);

        {
            let tracker = &mut self.tracker;

            let mut stream =
                stream_sse_events(&events_url, "GET", headers, None, Some(timeout)).await?;

            while let Some(item) = stream.next().await {
                let event = item?;
                if event.data.trim() == "[DONE]" {
                    break;
                }

                let payload: Value = serde_json::from_str(&event.data).unwrap_or(Value::Null);
                let parsed = super::events::EventParser::parse(&payload);
                let count = event_count.get() + 1;
                event_count.set(count);

                // Update tracker
                tracker.update(&parsed);

                // Log progress periodically
                if count % 5 == 0 || parsed.category.is_terminal() {
                    eprintln!(
                        "[PL] Event #{}: type={} category={:?} | tracker: best={:.3} baseline={:?} candidates={} gens={}",
                        count,
                        parsed.event_type,
                        parsed.category,
                        tracker.best_reward(),
                        tracker.baseline_reward(),
                        tracker.progress.candidates_evaluated,
                        tracker.progress.generations_completed,
                    );
                }

                // Call user callback
                if let Some(ref mut cb) = on_event {
                    cb(&parsed);
                }

                // Check for terminal
                if parsed.category.is_terminal() {
                    eprintln!(
                        "[PL] Terminal event received: {} (category={:?})",
                        parsed.event_type, parsed.category
                    );
                    terminal_status.set(super::events::EventParser::terminal_status(
                        &parsed.event_type,
                    ));
                    terminal_reached.set(true);
                    break;
                }
            }
        }

        eprintln!(
            "[PL] stream_until_complete: streaming finished, processed {} events",
            event_count.get()
        );

        if !terminal_reached.get() {
            return Err(CoreError::Timeout(
                "stream ended without terminal event".to_string(),
            ));
        }

        // Get final status (tracker borrow is dropped now)
        eprintln!("[PL] Fetching final job status...");
        let status_result = match self.get_status().await {
            Ok(result) => Some(result),
            Err(err) => {
                eprintln!("[PL] Warning: failed to fetch final job status: {}", err);
                None
            }
        };

        let mut final_status = status_result
            .as_ref()
            .map(|result| result.status)
            .unwrap_or(crate::api::types::PolicyJobStatus::Succeeded);
        if !final_status.is_terminal() {
            if let Some(status) = terminal_status.get() {
                final_status = match status {
                    TerminalStatus::Succeeded => crate::api::types::PolicyJobStatus::Succeeded,
                    TerminalStatus::Failed => crate::api::types::PolicyJobStatus::Failed,
                    TerminalStatus::Cancelled => crate::api::types::PolicyJobStatus::Cancelled,
                    TerminalStatus::Paused => crate::api::types::PolicyJobStatus::Paused,
                };
                eprintln!(
                    "[PL] Final status override from terminal event: {:?}",
                    final_status
                );
            }
        }
        eprintln!(
            "[PL] Final status: status={:?} best_reward={:?} error={:?}",
            final_status,
            status_result.as_ref().and_then(|result| result.best_reward),
            status_result
                .as_ref()
                .and_then(|result| result.error.clone())
        );

        // Merge tracker data with status (fall back to tracker if status fetch failed)
        let result = PromptLearningResult {
            job_id: status_result
                .as_ref()
                .map(|result| result.job_id.clone())
                .unwrap_or_else(|| job_id.to_string()),
            status: final_status,
            best_reward: status_result
                .as_ref()
                .and_then(|result| result.best_reward)
                .or(Some(self.tracker.best_reward())),
            best_candidate: status_result
                .as_ref()
                .and_then(|result| result.best_candidate.clone()),
            lever_summary: status_result
                .as_ref()
                .and_then(|result| result.lever_summary.clone()),
            sensor_frames: status_result
                .as_ref()
                .map(|result| result.sensor_frames.clone())
                .unwrap_or_default(),
            lever_versions: status_result
                .as_ref()
                .map(|result| result.lever_versions.clone())
                .unwrap_or_default(),
            best_lever_version: status_result
                .as_ref()
                .and_then(|result| result.best_lever_version),
            baseline_reward: self.tracker.baseline_reward(),
            candidates_evaluated: self.tracker.progress.candidates_evaluated,
            generations_completed: self.tracker.progress.generations_completed,
            error: status_result.and_then(|result| result.error),
            raw: Value::Null,
        };

        eprintln!(
            "[PL] RESULT: status={:?} best={:?} baseline={:?} candidates={} gens={}",
            result.status,
            result.best_reward,
            result.baseline_reward,
            result.candidates_evaluated,
            result.generations_completed
        );

        Ok(result)
    }

    /// Cancel a running job.
    ///
    /// # Arguments
    ///
    /// * `reason` - Optional cancellation reason
    pub async fn cancel(&self, reason: Option<&str>) -> Result<(), CoreError> {
        let job_id = self
            .job_id
            .as_ref()
            .ok_or_else(|| CoreError::Validation("job not submitted yet".to_string()))?;

        self.client.jobs().cancel(job_id, reason).await
    }

    /// Pause a running job.
    ///
    /// # Arguments
    ///
    /// * `reason` - Optional pause reason
    pub async fn pause(&self, reason: Option<&str>) -> Result<(), CoreError> {
        let job_id = self
            .job_id
            .as_ref()
            .ok_or_else(|| CoreError::Validation("job not submitted yet".to_string()))?;

        self.client.jobs().pause(job_id, reason).await
    }

    /// Resume a paused job.
    ///
    /// # Arguments
    ///
    /// * `reason` - Optional resume reason
    pub async fn resume(&self, reason: Option<&str>) -> Result<(), CoreError> {
        let job_id = self
            .job_id
            .as_ref()
            .ok_or_else(|| CoreError::Validation("job not submitted yet".to_string()))?;

        self.client.jobs().resume(job_id, reason).await
    }

    /// Get detailed results including prompt extraction.
    ///
    /// This fetches events to extract the best prompts.
    pub async fn get_results(&self) -> Result<PromptResults, CoreError> {
        // Get final status for best_candidate
        let status = self.get_status().await?;

        let best_candidate = status.get_system_prompt();
        let best_reward = status.best_reward.or(Some(self.tracker.best_reward()));

        // Build ranked prompts from tracker candidates
        let mut top_prompts: Vec<RankedPrompt> = self
            .tracker
            .candidates
            .iter()
            .filter(|c| c.accepted || c.is_pareto)
            .map(|c| RankedPrompt {
                rank: 0,
                candidate_id: c.candidate_id.clone(),
                train_accuracy: c.reward,
                val_accuracy: c.val_reward,
                prompt: None,
            })
            .collect();

        // Sort by accuracy descending
        top_prompts.sort_by(|a, b| {
            let a_score = a.val_accuracy.or(a.train_accuracy).unwrap_or(0.0);
            let b_score = b.val_accuracy.or(b.train_accuracy).unwrap_or(0.0);
            b_score
                .partial_cmp(&a_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Assign ranks
        for (i, prompt) in top_prompts.iter_mut().enumerate() {
            prompt.rank = (i + 1) as i32;
        }

        Ok(PromptResults {
            best_candidate,
            best_reward,
            top_prompts,
            lever_summary: status.lever_summary,
            sensor_frames: status.sensor_frames,
            lever_versions: status.lever_versions,
            best_lever_version: status.best_lever_version,
        })
    }
}

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

    #[test]
    fn test_result_status() {
        let result = PromptLearningResult {
            job_id: "test".to_string(),
            status: PolicyJobStatus::Succeeded,
            best_reward: Some(0.85),
            best_candidate: None,
            lever_summary: None,
            sensor_frames: Vec::new(),
            lever_versions: HashMap::new(),
            best_lever_version: None,
            baseline_reward: None,
            candidates_evaluated: 10,
            generations_completed: 3,
            error: None,
            raw: Value::Null,
        };

        assert!(result.succeeded());
        assert!(!result.failed());
        assert!(result.is_terminal());
    }

    #[test]
    fn test_result_get_system_prompt() {
        let result = PromptLearningResult {
            job_id: "test".to_string(),
            status: PolicyJobStatus::Succeeded,
            best_reward: Some(0.85),
            best_candidate: Some(json!({
                "system_prompt": "You are a helpful assistant."
            })),
            lever_summary: None,
            sensor_frames: Vec::new(),
            lever_versions: HashMap::new(),
            best_lever_version: None,
            baseline_reward: None,
            candidates_evaluated: 10,
            generations_completed: 3,
            error: None,
            raw: Value::Null,
        };

        assert_eq!(
            result.get_system_prompt(),
            Some("You are a helpful assistant.".to_string())
        );
    }

    #[test]
    fn test_ranked_prompt_sorting() {
        let mut prompts = vec![
            RankedPrompt {
                rank: 0,
                candidate_id: "a".to_string(),
                train_accuracy: Some(0.7),
                val_accuracy: None,
                prompt: None,
            },
            RankedPrompt {
                rank: 0,
                candidate_id: "b".to_string(),
                train_accuracy: Some(0.9),
                val_accuracy: None,
                prompt: None,
            },
            RankedPrompt {
                rank: 0,
                candidate_id: "c".to_string(),
                train_accuracy: Some(0.8),
                val_accuracy: Some(0.85),
                prompt: None,
            },
        ];

        // Sort by accuracy descending (val_accuracy takes precedence)
        prompts.sort_by(|a, b| {
            let a_score = a.val_accuracy.or(a.train_accuracy).unwrap_or(0.0);
            let b_score = b.val_accuracy.or(b.train_accuracy).unwrap_or(0.0);
            b_score
                .partial_cmp(&a_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        assert_eq!(prompts[0].candidate_id, "b"); // 0.9
        assert_eq!(prompts[1].candidate_id, "c"); // 0.85 (val)
        assert_eq!(prompts[2].candidate_id, "a"); // 0.7
    }
}