zeph-bench 0.19.1

Benchmark harness for evaluating Zeph agent performance on standardized datasets
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Benchmark result types and writer.
//!
//! [`BenchRun`] is the top-level result record written to `results.json`.
//! [`ResultWriter`] handles serialization to JSON and a human-readable Markdown summary,
//! including partial flushing on SIGINT and resume support.

use std::collections::HashSet;
use std::fmt::Write as _;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::error::BenchError;

/// Status of a benchmark run serialized into `results.json`.
///
/// The `Running` variant is used in-memory during an active run and should never
/// appear in a persisted file.
///
/// # Examples
///
/// ```
/// use zeph_bench::RunStatus;
///
/// assert_ne!(RunStatus::Completed, RunStatus::Interrupted);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RunStatus {
    /// All scenarios finished successfully.
    Completed,
    /// The run was cancelled (e.g. SIGINT) before all scenarios finished.
    Interrupted,
    /// The run is currently in progress; should not appear in a persisted file.
    Running,
}

/// Per-scenario result record persisted inside [`BenchRun::results`].
///
/// # Examples
///
/// ```
/// use zeph_bench::ScenarioResult;
///
/// let r = ScenarioResult {
///     scenario_id: "gaia_t1".into(),
///     score: 1.0,
///     response_excerpt: "1945".into(),
///     error: None,
///     elapsed_ms: 820,
/// };
/// assert!(r.error.is_none());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScenarioResult {
    /// Unique identifier for the scenario (matches [`crate::Scenario::id`]).
    pub scenario_id: String,
    /// Numeric score in `[0.0, 1.0]` produced by the evaluator.
    pub score: f64,
    /// First 200 characters of the agent response for quick review.
    pub response_excerpt: String,
    /// Error message if the scenario could not be completed, otherwise `None`.
    pub error: Option<String>,
    /// Wall-clock time in milliseconds for this scenario.
    pub elapsed_ms: u64,
}

/// Aggregate statistics computed from all [`ScenarioResult`]s in a [`BenchRun`].
///
/// Recomputed after every scenario via [`BenchRun::recompute_aggregate`] and persisted
/// into `results.json` so partial runs still contain meaningful statistics.
///
/// # Examples
///
/// ```
/// use zeph_bench::Aggregate;
///
/// let agg = Aggregate {
///     total: 100,
///     mean_score: 0.72,
///     exact_match: 55,
///     total_elapsed_ms: 240_000,
/// };
/// assert_eq!(agg.total, 100);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Aggregate {
    /// Number of scenarios included in the statistics.
    pub total: usize,
    /// Arithmetic mean of all per-scenario scores.
    pub mean_score: f64,
    /// Count of scenarios where `score >= 1.0` (exact match).
    pub exact_match: usize,
    /// Sum of [`ScenarioResult::elapsed_ms`] across all scenarios.
    pub total_elapsed_ms: u64,
}

/// Top-level benchmark run record written to `results.json`.
///
/// The schema is a superset of the `LongMemEval` leaderboard submission format (NFR-008),
/// making it directly usable for leaderboard submission after a `longmemeval` run.
///
/// Create a default instance, then populate [`BenchRun::results`] incrementally and
/// call [`BenchRun::recompute_aggregate`] before persisting with [`ResultWriter`].
///
/// # Examples
///
/// ```
/// use zeph_bench::{BenchRun, RunStatus, Aggregate};
///
/// let run = BenchRun {
///     dataset: "gaia".into(),
///     model: "openai/gpt-4o".into(),
///     run_id: "a1b2c3".into(),
///     started_at: "2026-04-09T10:00:00Z".into(),
///     finished_at: String::new(),
///     status: RunStatus::Running,
///     results: vec![],
///     aggregate: Aggregate::default(),
/// };
/// assert_eq!(run.dataset, "gaia");
/// assert!(run.results.is_empty());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchRun {
    /// Dataset name (e.g. `"longmemeval"`).
    pub dataset: String,
    /// Provider/model identifier (e.g. `"openai/gpt-4o"`).
    pub model: String,
    /// UUID v4 uniquely identifying this run.
    pub run_id: String,
    /// RFC 3339 timestamp when the run started.
    pub started_at: String,
    /// RFC 3339 timestamp when the run ended (empty string if interrupted).
    pub finished_at: String,
    /// Run status.
    pub status: RunStatus,
    /// Per-scenario results.
    pub results: Vec<ScenarioResult>,
    /// Aggregate statistics.
    pub aggregate: Aggregate,
}

impl BenchRun {
    /// Recompute [`BenchRun::aggregate`] from the current [`BenchRun::results`] list.
    ///
    /// Call this after appending one or more [`ScenarioResult`]s to keep the
    /// aggregate statistics in sync before writing to disk.
    ///
    /// # Examples
    ///
    /// ```
    /// use zeph_bench::{BenchRun, RunStatus, ScenarioResult, Aggregate};
    ///
    /// let mut run = BenchRun {
    ///     dataset: "frames".into(),
    ///     model: "openai/gpt-4o-mini".into(),
    ///     run_id: "r1".into(),
    ///     started_at: "2026-01-01T00:00:00Z".into(),
    ///     finished_at: String::new(),
    ///     status: RunStatus::Running,
    ///     results: vec![
    ///         ScenarioResult {
    ///             scenario_id: "frames_0".into(),
    ///             score: 1.0,
    ///             response_excerpt: "Paris".into(),
    ///             error: None,
    ///             elapsed_ms: 500,
    ///         },
    ///     ],
    ///     aggregate: Aggregate::default(),
    /// };
    ///
    /// run.recompute_aggregate();
    /// assert_eq!(run.aggregate.total, 1);
    /// assert!((run.aggregate.mean_score - 1.0).abs() < f64::EPSILON);
    /// assert_eq!(run.aggregate.exact_match, 1);
    /// ```
    pub fn recompute_aggregate(&mut self) {
        let total = self.results.len();
        #[allow(clippy::cast_precision_loss)]
        let mean_score = if total == 0 {
            0.0
        } else {
            self.results.iter().map(|r| r.score).sum::<f64>() / total as f64
        };
        let exact_match = self.results.iter().filter(|r| r.score >= 1.0).count();
        let total_elapsed_ms = self.results.iter().map(|r| r.elapsed_ms).sum();
        self.aggregate = Aggregate {
            total,
            mean_score,
            exact_match,
            total_elapsed_ms,
        };
    }

    /// Return the set of scenario IDs already present in [`BenchRun::results`].
    ///
    /// Used by the `--resume` logic to determine which scenarios can be skipped.
    ///
    /// # Examples
    ///
    /// ```
    /// use zeph_bench::{BenchRun, RunStatus, ScenarioResult, Aggregate};
    ///
    /// let run = BenchRun {
    ///     dataset: "gaia".into(),
    ///     model: "openai/gpt-4o".into(),
    ///     run_id: "r2".into(),
    ///     started_at: "2026-01-01T00:00:00Z".into(),
    ///     finished_at: String::new(),
    ///     status: RunStatus::Interrupted,
    ///     results: vec![
    ///         ScenarioResult {
    ///             scenario_id: "t1".into(),
    ///             score: 1.0,
    ///             response_excerpt: "1945".into(),
    ///             error: None,
    ///             elapsed_ms: 300,
    ///         },
    ///     ],
    ///     aggregate: Aggregate::default(),
    /// };
    ///
    /// let done = run.completed_ids();
    /// assert!(done.contains("t1"));
    /// assert!(!done.contains("t2"));
    /// ```
    #[must_use]
    pub fn completed_ids(&self) -> HashSet<String> {
        self.results.iter().map(|r| r.scenario_id.clone()).collect()
    }
}

/// Writes `results.json` and `summary.md` to an output directory.
///
/// Files are written atomically by flushing to a `.tmp` sibling file and then
/// renaming, so a concurrent SIGINT cannot leave a half-written JSON file.
///
/// # Examples
///
/// ```no_run
/// use zeph_bench::{ResultWriter, BenchRun, RunStatus, Aggregate};
///
/// let writer = ResultWriter::new("/tmp/my-bench-run").unwrap();
/// println!("results at {}", writer.results_path().display());
/// ```
pub struct ResultWriter {
    output_dir: PathBuf,
}

impl ResultWriter {
    /// Create a writer targeting `output_dir`.
    ///
    /// The directory is created automatically (single level) if it does not exist.
    ///
    /// # Errors
    ///
    /// Returns [`BenchError::Io`] if the directory cannot be created.
    pub fn new(output_dir: impl Into<PathBuf>) -> Result<Self, BenchError> {
        let output_dir = output_dir.into();
        if !output_dir.exists() {
            std::fs::create_dir(&output_dir)?;
        }
        Ok(Self { output_dir })
    }

    /// Absolute path of `results.json` inside the output directory.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::path::Path;
    /// use zeph_bench::ResultWriter;
    ///
    /// let dir = tempfile::tempdir().unwrap();
    /// let writer = ResultWriter::new(dir.path()).unwrap();
    /// assert!(writer.results_path().ends_with("results.json"));
    /// ```
    #[must_use]
    pub fn results_path(&self) -> PathBuf {
        self.output_dir.join("results.json")
    }

    /// Absolute path of `summary.md` inside the output directory.
    ///
    /// # Examples
    ///
    /// ```
    /// use zeph_bench::ResultWriter;
    ///
    /// let dir = tempfile::tempdir().unwrap();
    /// let writer = ResultWriter::new(dir.path()).unwrap();
    /// assert!(writer.summary_path().ends_with("summary.md"));
    /// ```
    #[must_use]
    pub fn summary_path(&self) -> PathBuf {
        self.output_dir.join("summary.md")
    }

    /// Load an existing `results.json` for resume.
    ///
    /// Returns `None` when the file does not exist (treat as fresh run).
    ///
    /// # Errors
    ///
    /// Returns [`BenchError::Io`] on read failure, or [`BenchError::InvalidFormat`] if
    /// the file exists but cannot be deserialized.
    pub fn load_existing(&self) -> Result<Option<BenchRun>, BenchError> {
        let path = self.results_path();
        if !path.exists() {
            return Ok(None);
        }
        let data = std::fs::read_to_string(&path)?;
        let run: BenchRun =
            serde_json::from_str(&data).map_err(|e| BenchError::InvalidFormat(e.to_string()))?;
        Ok(Some(run))
    }

    /// Write `run` to `results.json` and `summary.md` atomically (best-effort).
    ///
    /// # Errors
    ///
    /// Returns [`BenchError`] on serialization or I/O failure.
    pub fn write(&self, run: &BenchRun) -> Result<(), BenchError> {
        self.write_json(run)?;
        self.write_markdown(run)?;
        Ok(())
    }

    fn write_json(&self, run: &BenchRun) -> Result<(), BenchError> {
        let json = serde_json::to_string_pretty(run)
            .map_err(|e| BenchError::InvalidFormat(e.to_string()))?;
        write_atomic(&self.results_path(), json.as_bytes())?;
        Ok(())
    }

    fn write_markdown(&self, run: &BenchRun) -> Result<(), BenchError> {
        let mut md = String::new();
        let _ = writeln!(md, "# Benchmark Results: {}\n", run.dataset);
        let _ = writeln!(md, "- **Model**: {}", run.model);
        let _ = writeln!(md, "- **Run ID**: {}", run.run_id);
        let _ = writeln!(md, "- **Status**: {:?}", run.status);
        let _ = writeln!(md, "- **Started**: {}", run.started_at);
        if !run.finished_at.is_empty() {
            let _ = writeln!(md, "- **Finished**: {}", run.finished_at);
        }
        let _ = writeln!(
            md,
            "- **Mean score**: {:.4} ({}/{} exact)\n",
            run.aggregate.mean_score, run.aggregate.exact_match, run.aggregate.total
        );

        md.push_str("| scenario_id | score | response_excerpt | error |\n");
        md.push_str("|-------------|-------|------------------|-------|\n");
        for r in &run.results {
            let excerpt = r.response_excerpt.replace('|', "\\|");
            let error = r.error.as_deref().unwrap_or("").replace('|', "\\|");
            let _ = writeln!(
                md,
                "| {} | {:.4} | {} | {} |",
                r.scenario_id, r.score, excerpt, error
            );
        }

        write_atomic(&self.summary_path(), md.as_bytes())?;
        Ok(())
    }
}

/// Write `data` to `path` using a temp file + rename for atomicity.
fn write_atomic(path: &Path, data: &[u8]) -> Result<(), std::io::Error> {
    let tmp = path.with_extension("tmp");
    std::fs::write(&tmp, data)?;
    std::fs::rename(&tmp, path)?;
    Ok(())
}

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

    fn make_run() -> BenchRun {
        BenchRun {
            dataset: "longmemeval".into(),
            model: "openai/gpt-4o".into(),
            run_id: "test-run-001".into(),
            started_at: "2026-01-01T00:00:00Z".into(),
            finished_at: "2026-01-01T00:01:00Z".into(),
            status: RunStatus::Completed,
            results: vec![
                ScenarioResult {
                    scenario_id: "s1".into(),
                    score: 1.0,
                    response_excerpt: "The answer is 42.".into(),
                    error: None,
                    elapsed_ms: 1000,
                },
                ScenarioResult {
                    scenario_id: "s2".into(),
                    score: 0.0,
                    response_excerpt: String::new(),
                    error: Some("timeout".into()),
                    elapsed_ms: 5000,
                },
            ],
            aggregate: Aggregate::default(),
        }
    }

    #[test]
    fn recompute_aggregate_correct() {
        let mut run = make_run();
        run.recompute_aggregate();
        assert_eq!(run.aggregate.total, 2);
        assert!((run.aggregate.mean_score - 0.5).abs() < f64::EPSILON);
        assert_eq!(run.aggregate.exact_match, 1);
        assert_eq!(run.aggregate.total_elapsed_ms, 6000);
    }

    #[test]
    fn completed_ids_returns_all_scenario_ids() {
        let run = make_run();
        let ids = run.completed_ids();
        assert!(ids.contains("s1"));
        assert!(ids.contains("s2"));
        assert_eq!(ids.len(), 2);
    }

    #[test]
    fn json_round_trip() {
        let mut run = make_run();
        run.recompute_aggregate();
        let json = serde_json::to_string_pretty(&run).unwrap();
        let decoded: BenchRun = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.dataset, run.dataset);
        assert_eq!(decoded.run_id, run.run_id);
        assert_eq!(decoded.results.len(), 2);
        assert_eq!(decoded.status, RunStatus::Completed);
        assert_eq!(decoded.aggregate.exact_match, run.aggregate.exact_match);
    }

    #[test]
    fn interrupted_status_serializes_correctly() {
        let mut run = make_run();
        run.status = RunStatus::Interrupted;
        let json = serde_json::to_string(&run).unwrap();
        assert!(json.contains("\"interrupted\""));
    }

    #[test]
    fn write_and_load_round_trip() {
        let dir = tempfile::tempdir().unwrap();
        let writer = ResultWriter::new(dir.path()).unwrap();

        assert!(writer.load_existing().unwrap().is_none());

        let mut run = make_run();
        run.recompute_aggregate();
        writer.write(&run).unwrap();

        let loaded = writer.load_existing().unwrap().unwrap();
        assert_eq!(loaded.run_id, run.run_id);
        assert_eq!(loaded.results.len(), 2);
        assert_eq!(loaded.aggregate.exact_match, 1);
    }

    #[test]
    fn summary_md_contains_table_header() {
        let dir = tempfile::tempdir().unwrap();
        let writer = ResultWriter::new(dir.path()).unwrap();
        let mut run = make_run();
        run.recompute_aggregate();
        writer.write(&run).unwrap();

        let md = std::fs::read_to_string(writer.summary_path()).unwrap();
        assert!(md.contains("| scenario_id | score |"));
        assert!(md.contains("s1"));
        assert!(md.contains("s2"));
    }

    #[test]
    fn write_creates_output_dir_if_absent() {
        let tmp = tempfile::tempdir().unwrap();
        let new_dir = tmp.path().join("new_subdir");
        assert!(!new_dir.exists());
        ResultWriter::new(&new_dir).unwrap();
        assert!(new_dir.exists());
    }

    #[test]
    fn resume_skips_completed_scenarios() {
        let dir = tempfile::tempdir().unwrap();
        let writer = ResultWriter::new(dir.path()).unwrap();

        // Write partial results (only s1 done).
        let mut partial = make_run();
        partial.results.retain(|r| r.scenario_id == "s1");
        partial.status = RunStatus::Interrupted;
        partial.recompute_aggregate();
        writer.write(&partial).unwrap();

        let loaded = writer.load_existing().unwrap().unwrap();
        let done = loaded.completed_ids();
        assert!(done.contains("s1"));
        assert!(!done.contains("s2"));
    }
}