open-eeg-codec-standard 0.9.0

OpenECS — a universal, vendor-neutral Open EEG Codec Standard: grade any EEG codec (any language, via a file-based contract) against a hash-pinned corpus into one comparable verdict.
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
//! `openecs` — CLI for the OpenECS universal EEG-compression benchmark
//! (OpenECS v1.0; see `SPEC/OpenECS-v1.0.md`).
//!
//! Subcommands (run `openecs <cmd> --help` for each):
//!
//! - `grade` — grade ONE codec over a corpus / EDF / synthetic fixture.
//! - `bench` — grade a codec **and baselines**, ranked, with confidence
//!   intervals + significance, a progress bar, and optional HTML report.
//! - `verify-corpus` — check a corpus manifest's SHA-256 + shape pins.
//! - `emit-corpus-manifest` — hash a directory of EDFs into a pinned manifest.
//!
//! Legacy form (preserved): `openecs <store|gzip|quantize> [FILE.edf]` grades
//! the built-in fixture or an EDF and prints a single report.
//!
//! Exit codes: 0 pass · 1 below-floor · 2 unknown codec · 3 EDF read error ·
//! 4 corpus integrity/shape failure · 5 manifest load/parse error.

use std::path::{Path, PathBuf};
use std::process::ExitCode;

use clap::{Args, Parser, Subcommand};
use indicatif::{ProgressBar, ProgressStyle};

use open_eeg_codec_standard::adapter::{deserialize, serialize, Codec, Gzip, Store};
use open_eeg_codec_standard::corpus::{self, sha256_hex, CorpusManifest};
use open_eeg_codec_standard::report::{CodecIdentity, CorpusIdentity, EcsReport, EcsSubmission};
use open_eeg_codec_standard::subprocess::write_edf_bytes;
use open_eeg_codec_standard::{charts, edf, harness, manifest, report_html, stats, term};

// ─────────────────────────────── demo codec ────────────────────────────────

/// A deliberately-lossy demo codec (÷STEP / ×STEP). Not bit-exact — exercises
/// the lossy battery. Ships in the CLI as a demonstration.
struct Quantize {
    step: i64,
}

impl Codec for Quantize {
    fn name(&self) -> &str {
        "quantize"
    }
    fn declared_lossless(&self) -> bool {
        false
    }
    fn encode(&self, signal: &[Vec<i64>], _fs: f64) -> Vec<u8> {
        let q: Vec<Vec<i64>> = signal
            .iter()
            .map(|chan| chan.iter().map(|&s| s / self.step).collect())
            .collect();
        serialize(&q)
    }
    fn decode(&self, blob: &[u8]) -> Vec<Vec<i64>> {
        deserialize(blob)
            .into_iter()
            .map(|chan| chan.into_iter().map(|s| s * self.step).collect())
            .collect()
    }
}

/// A deterministic synthetic multichannel EEG-like signal.
fn synthetic_signal(n_chan: usize, n: usize, fs: f64) -> Vec<Vec<i64>> {
    use std::f64::consts::PI;
    (0..n_chan)
        .map(|c| {
            let amp = 1.0 + 0.3 * c as f64;
            (0..n)
                .map(|i| {
                    let t = i as f64 / fs;
                    let v = amp
                        * (40.0
                            + 120.0 * (2.0 * PI * 2.0 * t).sin()
                            + 80.0 * (2.0 * PI * 6.0 * t).sin()
                            + 60.0 * (2.0 * PI * 10.0 * t).sin()
                            + 30.0 * (2.0 * PI * 20.0 * t).sin()
                            + 15.0 * (2.0 * PI * 40.0 * t).sin());
                    v.round() as i64
                })
                .collect()
        })
        .collect()
}

// ──────────────────────────────── clap model ───────────────────────────────

#[derive(Parser)]
#[command(
    name = "openecs",
    version,
    about = "OpenECS — the universal, vendor-neutral EEG-compression benchmark",
    long_about = "Grade any EEG codec (any language, via the file-based CLI contract) over a \
                  hash-pinned corpus and get a single comparable verdict. See SPEC/OpenECS-v1.0.md.\n\n\
                  Legacy form (preserved): openecs <store|gzip|quantize> [FILE.edf]"
)]
struct Cli {
    #[command(subcommand)]
    cmd: Cmd,
}

#[derive(Subcommand)]
enum Cmd {
    /// Grade ONE codec over a corpus (or a single EDF / the synthetic fixture).
    Grade(GradeArgs),
    /// Grade a codec AND baselines — ranked, with CIs, significance, a progress bar.
    Bench(BenchArgs),
    /// Verify a corpus manifest's SHA-256 + shape pins (no grading).
    VerifyCorpus(CorpusArg),
    /// Walk a directory of *.edf files and print a pinned corpus manifest.
    EmitCorpusManifest(EmitArgs),
}

#[derive(Args)]
struct GradeArgs {
    /// Codec manifest (TOML) for an external codec (any language).
    #[arg(long, value_name = "C.toml")]
    codec_manifest: Option<PathBuf>,
    /// Built-in codec when no manifest is given.
    #[arg(long, default_value = "store", value_name = "store|gzip|quantize")]
    codec: String,
    /// Corpus manifest (TOML) to grade over (hash-pinned).
    #[arg(long, value_name = "K.toml")]
    corpus_manifest: Option<PathBuf>,
    /// Grade a single EDF file instead of a corpus.
    #[arg(long, value_name = "FILE.edf")]
    edf: Option<PathBuf>,
    /// Write the results submission JSON here.
    #[arg(long, value_name = "sub.json")]
    out: Option<PathBuf>,
    /// Write a self-contained HTML report here.
    #[arg(long, value_name = "report.html")]
    report: Option<PathBuf>,
    /// Dump paired orig_/recon_ EDFs here (for the task-concordance tool).
    #[arg(long, value_name = "DIR")]
    dump_recon: Option<PathBuf>,
    /// Show ASCII charts in the terminal.
    #[arg(long)]
    charts: bool,
    /// Disable colored output.
    #[arg(long)]
    no_color: bool,
    /// Timed round trips per file for the median-throughput measurement.
    #[arg(long, default_value_t = 1, value_name = "N")]
    repeat: usize,
}

#[derive(Args)]
struct BenchArgs {
    /// Codec manifest (TOML) for the codec under test (any language).
    #[arg(long, value_name = "C.toml")]
    codec_manifest: Option<PathBuf>,
    /// Built-in codec under test when no manifest is given.
    #[arg(long, value_name = "store|gzip|quantize")]
    codec: Option<String>,
    /// Corpus manifest (TOML) to benchmark over (hash-pinned). Required.
    #[arg(long, value_name = "K.toml")]
    corpus_manifest: PathBuf,
    /// Comma-separated built-in baselines to grade alongside the codec.
    #[arg(long, value_delimiter = ',', default_value = "store,gzip", value_name = "a,b")]
    baselines: Vec<String>,
    /// Timed round trips per file for the median-throughput measurement.
    #[arg(long, default_value_t = 3, value_name = "N")]
    repeat: usize,
    /// Write the codec-under-test submission JSON here.
    #[arg(long, value_name = "sub.json")]
    out: Option<PathBuf>,
    /// Write a self-contained HTML report (all codecs) here.
    #[arg(long, value_name = "report.html")]
    report: Option<PathBuf>,
    /// Show ASCII charts in the terminal.
    #[arg(long)]
    charts: bool,
    /// Disable colored output.
    #[arg(long)]
    no_color: bool,
}

#[derive(Args)]
struct CorpusArg {
    /// Corpus manifest (TOML) to verify.
    #[arg(long, value_name = "K.toml")]
    corpus_manifest: PathBuf,
}

#[derive(Args)]
struct EmitArgs {
    /// Root directory to walk for *.edf files.
    #[arg(long, value_name = "DIR")]
    root: PathBuf,
    /// Corpus name to stamp.
    #[arg(long, default_value = "corpus")]
    name: String,
    /// Corpus version to stamp.
    #[arg(long, default_value = "1.0.0")]
    version: String,
}

// ─────────────────────────────── dispatch ──────────────────────────────────

fn main() -> ExitCode {
    let args: Vec<String> = std::env::args().collect();
    const SUBS: &[&str] = &[
        "grade",
        "bench",
        "verify-corpus",
        "emit-corpus-manifest",
        "help",
        "-h",
        "--help",
        "-V",
        "--version",
    ];
    let is_clap = args.get(1).map(|s| SUBS.contains(&s.as_str())).unwrap_or(false);
    if !is_clap {
        // Legacy `openecs <codec> [file.edf]` (or no args -> synthetic).
        return cmd_legacy(&args[1..]);
    }
    match Cli::parse().cmd {
        Cmd::Grade(a) => cmd_grade(a),
        Cmd::Bench(a) => cmd_bench(a),
        Cmd::VerifyCorpus(a) => cmd_verify_corpus(&a.corpus_manifest),
        Cmd::EmitCorpusManifest(a) => cmd_emit_manifest(&a),
    }
}

/// True iff stdout is an interactive terminal (progress bars / spinners on).
fn interactive() -> bool {
    use std::io::IsTerminal;
    std::io::stdout().is_terminal()
}

// ─────────────────────────────── codec build ───────────────────────────────

/// Build a built-in codec by name (Sync, for the parallel grader).
fn build_builtin(name: &str) -> Option<(Box<dyn Codec + Sync>, CodecIdentity)> {
    let codec: Box<dyn Codec + Sync> = match name {
        "store" => Box::new(Store),
        "gzip" => Box::new(Gzip),
        "quantize" => Box::new(Quantize { step: 8 }),
        _ => return None,
    };
    Some((codec, CodecIdentity { name: name.to_string(), manifest_sha256: None }))
}

/// Resolve the codec under test: a manifest-defined external codec takes
/// precedence over a built-in name.
fn resolve_target(
    codec_manifest: Option<&Path>,
    builtin: &str,
) -> Result<(Box<dyn Codec + Sync>, CodecIdentity), ExitCode> {
    if let Some(path) = codec_manifest {
        let m = match manifest::load_codec_manifest(path) {
            Ok(m) => m,
            Err(e) => {
                eprintln!("error: {e}");
                return Err(ExitCode::from(5));
            }
        };
        let codec = match m.into_adapter() {
            Some(c) => c,
            None => {
                eprintln!(
                    "error: codec '{}' command could not be resolved (set $ECS_CODEC_<NAME>_BIN or fix `cmd`)",
                    m.codec.name
                );
                return Err(ExitCode::from(5));
            }
        };
        let sha = std::fs::read(path).map(|b| sha256_hex(&b)).ok();
        let id = CodecIdentity { name: codec.name().to_string(), manifest_sha256: sha };
        return Ok((Box::new(codec), id));
    }
    build_builtin(builtin).ok_or_else(|| {
        eprintln!("unknown codec '{builtin}'; valid: store | gzip | quantize, or --codec-manifest");
        ExitCode::from(2)
    })
}

// ─────────────────────────────── corpus grade ──────────────────────────────

/// Grade a codec over a corpus manifest with a labelled progress bar.
fn grade_corpus_with_progress(
    codec: &(dyn Codec + Sync),
    manifest: &CorpusManifest,
    base: &Path,
    repeats: usize,
    label: &str,
    show: bool,
) -> Result<(Vec<EcsReport>, harness::CorpusSummary), corpus::CorpusError> {
    let pb = if show {
        let pb = ProgressBar::new(manifest.file.len() as u64);
        pb.set_style(
            ProgressStyle::with_template("{prefix:>12} [{bar:28.cyan/blue}] {pos:>4}/{len} {eta:>5}")
                .unwrap()
                .progress_chars("█▉ "),
        );
        pb.set_prefix(label.to_string());
        pb
    } else {
        ProgressBar::hidden()
    };
    let res = corpus::grade_manifest_parallel(manifest, base, codec, repeats, || pb.inc(1));
    pb.finish_and_clear();
    res
}

fn manifest_base(path: &Path) -> &Path {
    path.parent().unwrap_or_else(|| Path::new("."))
}

// ─────────────────────────────────── grade ─────────────────────────────────

fn cmd_grade(a: GradeArgs) -> ExitCode {
    let color = term::colors_on() && !a.no_color;
    let (codec, codec_id) = match resolve_target(a.codec_manifest.as_deref(), &a.codec) {
        Ok(v) => v,
        Err(code) => return code,
    };

    // Corpus path.
    if let Some(corpus_path) = &a.corpus_manifest {
        let manifest = match corpus::load_corpus_manifest(corpus_path) {
            Ok(m) => m,
            Err(e) => {
                eprintln!("error: {e}");
                return ExitCode::from(5);
            }
        };
        let base = manifest_base(corpus_path);
        let (reports, summary) = match grade_corpus_with_progress(
            codec.as_ref(),
            &manifest,
            base,
            a.repeat,
            &codec_id.name,
            interactive(),
        ) {
            Ok(v) => v,
            Err(e) => {
                eprintln!("error: corpus integrity/shape: {e}");
                return ExitCode::from(4);
            }
        };
        println!("{}", term::render_corpus_summary(&summary, color));
        let ranked: Vec<&EcsReport> = reports.iter().collect();
        println!("\n{}", term::render_leaderboard(&ranked, color));
        if a.charts {
            let pts: Vec<(f32, f32)> =
                reports.iter().map(|r| (r.cr as f32, r.prd as f32)).collect();
            println!("{}", charts::ascii_rd_scatter(&pts));
        }
        if let Some(dir) = &a.dump_recon {
            // Reload signals (verify) just for the recon dump.
            if let Ok(files) = corpus::verify_and_load(&manifest, base) {
                if let Err(e) = dump_recon(codec.as_ref(), &files, &manifest.name, dir) {
                    eprintln!("warning: --dump-recon: {e}");
                } else {
                    println!("dumped originals + reconstructions to {}", dir.display());
                }
            }
        }
        let submission = EcsSubmission::new(
            codec_id,
            CorpusIdentity { name: manifest.name.clone(), version: manifest.version.clone() },
            reports,
            summary.clone(),
        );
        write_outputs(&[submission], a.out.as_deref(), a.report.as_deref());
        return exit_for_grade(summary.worst_grade);
    }

    // Single EDF or synthetic fixture.
    let (signal, fs, dataset) = match &a.edf {
        Some(path) => match edf::read_edf(path) {
            Ok(e) => (e.channels, e.fs, path.display().to_string()),
            Err(err) => {
                eprintln!("error: failed to read EDF '{}': {err}", path.display());
                return ExitCode::from(3);
            }
        },
        None => (synthetic_signal(4, 512, 256.0), 256.0, "(synthetic)".to_string()),
    };
    let mut report = harness::run_measured(codec.as_ref(), &signal, fs, a.repeat);
    report.dataset = dataset.clone();
    println!("{}", term::render_report(&report, color));
    if a.charts {
        println!("\n{}", charts::ascii_per_band(&report, 24));
    }
    if a.out.is_some() || a.report.is_some() {
        let (reports, summary) = harness::run_corpus(codec.as_ref(), &[(signal, fs)]);
        let mut reports = reports;
        reports.iter_mut().for_each(|r| r.dataset = dataset.clone());
        let submission = EcsSubmission::new(
            codec_id,
            CorpusIdentity { name: dataset, version: "n/a".to_string() },
            reports,
            summary,
        );
        write_outputs(&[submission], a.out.as_deref(), a.report.as_deref());
    }
    exit_for_grade(report.grade)
}

// ─────────────────────────────────── bench ─────────────────────────────────

fn cmd_bench(a: BenchArgs) -> ExitCode {
    let color = term::colors_on() && !a.no_color;
    let manifest = match corpus::load_corpus_manifest(&a.corpus_manifest) {
        Ok(m) => m,
        Err(e) => {
            eprintln!("error: {e}");
            return ExitCode::from(5);
        }
    };
    let base = manifest_base(&a.corpus_manifest);

    let (target, target_id) =
        match resolve_target(a.codec_manifest.as_deref(), a.codec.as_deref().unwrap_or("store")) {
            Ok(v) => v,
            Err(code) => return code,
        };

    // Codec set: the target first, then the requested baselines (de-duped).
    let mut codecs: Vec<(Box<dyn Codec + Sync>, CodecIdentity)> = vec![(target, target_id.clone())];
    for name in &a.baselines {
        if *name == target_id.name {
            continue;
        }
        if let Some(c) = build_builtin(name) {
            codecs.push(c);
        } else {
            eprintln!("warning: skipping unknown baseline '{name}'");
        }
    }

    println!(
        "OpenECS bench — {} codecs over {} v{} ({} files, repeat={})\n",
        codecs.len(),
        manifest.name,
        manifest.version,
        manifest.file.len(),
        a.repeat
    );

    let mut subs: Vec<EcsSubmission> = Vec::new();
    for (codec, id) in &codecs {
        let (reports, summary) = match grade_corpus_with_progress(
            codec.as_ref(),
            &manifest,
            base,
            a.repeat,
            &id.name,
            interactive(),
        ) {
            Ok(v) => v,
            Err(e) => {
                eprintln!("error: corpus integrity/shape ({}): {e}", id.name);
                return ExitCode::from(4);
            }
        };
        subs.push(EcsSubmission::new(
            id.clone(),
            CorpusIdentity { name: manifest.name.clone(), version: manifest.version.clone() },
            reports,
            summary,
        ));
    }

    print!("{}", render_bench_table(&subs, &target_id.name, color));

    if a.charts {
        let series: Vec<(String, Vec<(f64, f64)>)> = subs
            .iter()
            .map(|s| (s.codec.name.clone(), s.reports.iter().map(|r| (r.cr, r.prd)).collect()))
            .collect();
        let pts: Vec<(f32, f32)> =
            series.iter().flat_map(|(_, p)| p.iter().map(|(x, y)| (*x as f32, *y as f32))).collect();
        println!("\n{}", charts::ascii_rd_scatter(&pts));
    }

    // Submission JSON = the codec under test (subs[0]); HTML = all codecs.
    let target_sub = subs.first().cloned().into_iter().collect::<Vec<_>>();
    write_outputs(&target_sub, a.out.as_deref(), None);
    if let Some(report_path) = &a.report {
        let html = report_html::render(&subs);
        if let Err(e) = std::fs::write(report_path, html) {
            eprintln!("error: writing report '{}': {e}", report_path.display());
        } else {
            println!("wrote HTML report: {}", report_path.display());
        }
    }

    exit_for_grade(subs.first().map(|s| s.summary.worst_grade).unwrap_or('\0'))
}

/// Render the bench comparison table: one row per codec, ranked worst-grade
/// first then pooled CR, with a 95% CI on mean R and a sign-test p-value for
/// the codec under test versus the strongest baseline.
fn render_bench_table(subs: &[EcsSubmission], target: &str, color: bool) -> String {
    // Rank order.
    let mut order: Vec<usize> = (0..subs.len()).collect();
    order.sort_by(|&a, &b| {
        let (sa, sb) = (&subs[a].summary, &subs[b].summary);
        grade_rank(sa.worst_grade)
            .cmp(&grade_rank(sb.worst_grade))
            .then(sb.mean_cr.partial_cmp(&sa.mean_cr).unwrap_or(std::cmp::Ordering::Equal))
    });

    // Per-file R series per codec (for CI + significance).
    let r_series: Vec<Vec<f64>> =
        subs.iter().map(|s| s.reports.iter().map(|r| r.r).collect()).collect();
    // Strongest baseline = best-ranked non-target codec.
    let best_baseline = order.iter().copied().find(|&i| subs[i].codec.name != target);

    let mut s = String::new();
    let bold = |t: &str| if color { console::style(t).bold().force_styling(true).to_string() } else { t.to_string() };
    s.push_str(&bold(
        "  rank  codec                  grade   pooled CR   mean R (95% CI)             PRD%   p vs base\n",
    ));
    s.push_str("  ────────────────────────────────────────────────────────────────────────────────────────\n");
    for (rank, &i) in order.iter().enumerate() {
        let sub = &subs[i];
        let sm = &sub.summary;
        let ci = stats::bootstrap_ci(&r_series[i], 0.95, 2000, 0x1c5_5eed);
        let pval = match best_baseline {
            Some(b) if i != b => format!("{:.4}", stats::sign_test(&r_series[i], &r_series[b])),
            _ => "".to_string(),
        };
        let name_cell = if sub.codec.name == target {
            format!("{}*", sub.codec.name)
        } else {
            sub.codec.name.clone()
        };
        // ANSI-aware padding for the colored grade cell (plain cells use
        // format-width specifiers).
        let grade_str = term::grade_short(sm.worst_grade, color);
        let grade_cell = console::pad_str(&grade_str, 6, console::Alignment::Center, None);
        s.push_str(&format!(
            "  {:>3}   {:<22} {}  {:>8.2}:1   {:.4} [{:.4},{:.4}]   {:>5.2}   {:>8}\n",
            rank + 1,
            truncate(&name_cell, 22),
            grade_cell,
            sm.mean_cr,
            ci.mean,
            ci.lo,
            ci.hi,
            sm.mean_prd,
            pval,
        ));
    }
    s.push_str("\n  * codec under test · p = paired sign-test on per-file R vs the strongest baseline\n");
    s
}

// ──────────────────────────── verify / emit ────────────────────────────────

fn cmd_verify_corpus(path: &Path) -> ExitCode {
    let manifest = match corpus::load_corpus_manifest(path) {
        Ok(m) => m,
        Err(e) => {
            eprintln!("error: {e}");
            return ExitCode::from(5);
        }
    };
    let base = manifest_base(path);
    println!("verifying corpus {} v{} ({} files)", manifest.name, manifest.version, manifest.file.len());
    match corpus::verify_and_load(&manifest, base) {
        Ok(files) => {
            for (entry, (sig, _)) in manifest.file.iter().zip(&files) {
                println!(
                    "  PASS  {} ({} ch x {} samp)",
                    entry.path,
                    sig.len(),
                    sig.first().map(|c| c.len()).unwrap_or(0)
                );
            }
            println!("\nOK: all {} files verified (sha256 + shape).", files.len());
            ExitCode::SUCCESS
        }
        Err(e) => {
            eprintln!("  FAIL  {e}");
            ExitCode::from(4)
        }
    }
}

fn cmd_emit_manifest(a: &EmitArgs) -> ExitCode {
    let mut edfs = Vec::new();
    if let Err(e) = collect_edfs(&a.root, &mut edfs) {
        eprintln!("error: walking '{}': {e}", a.root.display());
        return ExitCode::from(3);
    }
    edfs.sort();

    println!("spec_version = \"{}\"", open_eeg_codec_standard::SPEC_VERSION);
    println!("name = \"{}\"", a.name);
    println!("version = \"{}\"", a.version);
    println!();

    let mut emitted = 0usize;
    for path in &edfs {
        let bytes = match std::fs::read(path) {
            Ok(b) => b,
            Err(e) => {
                eprintln!("warning: skip {} ({e})", path.display());
                continue;
            }
        };
        let sig = match edf::read_edf(path) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("warning: skip {} (not readable EDF: {e})", path.display());
                continue;
            }
        };
        let n_chan = sig.channels.len();
        let n_samples = sig.channels.first().map(|c| c.len()).unwrap_or(0);
        if n_chan == 0 || !sig.channels.iter().all(|c| c.len() == n_samples) {
            eprintln!("warning: skip {} (empty or ragged channels)", path.display());
            continue;
        }
        let rel = path.strip_prefix(&a.root).unwrap_or(path);
        println!("[[file]]");
        println!("path = \"{}\"", rel.display());
        println!("sha256 = \"{}\"", sha256_hex(&bytes));
        // `{:?}` renders a whole number with a decimal (256.0, not 256).
        println!("fs = {:?}", sig.fs);
        println!("n_chan = {n_chan}");
        println!("n_samples = {n_samples}");
        println!();
        emitted += 1;
    }
    eprintln!("emitted {emitted} file entries from {}", a.root.display());
    if emitted == 0 {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    }
}

// ──────────────────────────────── legacy ───────────────────────────────────

fn cmd_legacy(args: &[String]) -> ExitCode {
    let color = term::colors_on();
    let codec_name = args.first().cloned().unwrap_or_else(|| "store".to_string());
    let file_arg = args.get(1);

    let (signal, fs, dataset) = match file_arg {
        Some(path) => match edf::read_edf(path) {
            Ok(e) => {
                let ds = path.clone();
                (e.channels, e.fs, ds)
            }
            Err(err) => {
                eprintln!("error: failed to read EDF file '{path}': {err}");
                return ExitCode::from(3);
            }
        },
        None => (synthetic_signal(4, 512, 256.0), 256.0, "(synthetic)".to_string()),
    };

    let codec: Box<dyn Codec> = match codec_name.as_str() {
        "store" => Box::new(Store),
        "gzip" => Box::new(Gzip),
        "quantize" => Box::new(Quantize { step: 8 }),
        other => {
            eprintln!("unknown codec '{other}'; valid: store | gzip | quantize");
            eprintln!("(for the full CLI run `openecs --help`)");
            return ExitCode::from(2);
        }
    };

    let mut report = harness::run(codec.as_ref(), &signal, fs);
    report.dataset = dataset;
    println!("{}", term::render_report(&report, color));
    exit_for_grade(report.grade)
}

// ──────────────────────────────── helpers ──────────────────────────────────

fn exit_for_grade(grade: char) -> ExitCode {
    if grade != '\0' {
        ExitCode::SUCCESS
    } else {
        ExitCode::FAILURE
    }
}

fn grade_rank(g: char) -> u8 {
    match g {
        'L' => 0,
        'N' => 1,
        'C' => 2,
        'M' => 3,
        'A' => 4,
        _ => 5,
    }
}

fn truncate(s: &str, n: usize) -> String {
    if s.chars().count() <= n {
        s.to_string()
    } else {
        let mut t: String = s.chars().take(n.saturating_sub(1)).collect();
        t.push('');
        t
    }
}

/// Write a submission JSON (`out`) and/or an HTML report (`report`).
fn write_outputs(subs: &[EcsSubmission], out: Option<&Path>, report: Option<&Path>) {
    if let (Some(path), Some(sub)) = (out, subs.first()) {
        match std::fs::write(path, sub.to_json()) {
            Ok(()) => println!("wrote submission: {}", path.display()),
            Err(e) => eprintln!("error: writing submission '{}': {e}", path.display()),
        }
    }
    if let Some(path) = report {
        match std::fs::write(path, report_html::render(subs)) {
            Ok(()) => println!("wrote HTML report: {}", path.display()),
            Err(e) => eprintln!("error: writing report '{}': {e}", path.display()),
        }
    }
}

/// Write paired `orig_{i}.edf` / `recon_{i}.edf` for each corpus file.
fn dump_recon(
    codec: &dyn Codec,
    files: &[(Vec<Vec<i64>>, f64)],
    dataset: &str,
    dir: &Path,
) -> std::io::Result<()> {
    std::fs::create_dir_all(dir)?;
    for (i, (signal, fs)) in files.iter().enumerate() {
        let blob = codec.encode(signal, *fs);
        let recon = codec.decode(&blob);
        match (write_edf_bytes(signal, *fs), write_edf_bytes(&recon, *fs)) {
            (Some(o), Some(r)) => {
                std::fs::write(dir.join(format!("orig_{i}.edf")), o)?;
                std::fs::write(dir.join(format!("recon_{i}.edf")), r)?;
            }
            _ => eprintln!(
                "  skip recon dump for {dataset} file {i}: not EDF-expressible (ragged/out-of-range)"
            ),
        }
    }
    Ok(())
}

/// Recursively collect `*.edf` files under `dir`.
fn collect_edfs(dir: &Path, out: &mut Vec<PathBuf>) -> std::io::Result<()> {
    for entry in std::fs::read_dir(dir)? {
        let path = entry?.path();
        if path.is_dir() {
            collect_edfs(&path, out)?;
        } else if path
            .extension()
            .and_then(|e| e.to_str())
            .map(|e| e.eq_ignore_ascii_case("edf"))
            .unwrap_or(false)
        {
            out.push(path);
        }
    }
    Ok(())
}