cobre-io 0.15.0

Case directory loading and validation for the Cobre power systems ecosystem
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
//! JSON metadata writers for the output pipeline:
//!
//! - [`write_training_metadata`] — `training/metadata.json` (run context,
//!   configuration, convergence, row-pool statistics).
//! - [`write_simulation_metadata`] — `simulation/metadata.json` (run context,
//!   scenario completion counts).
//!
//! Each output directory gets a single merged metadata file; the `_SUCCESS`
//! marker signals completion.

use std::path::Path;

use serde::{Deserialize, Serialize};

use super::atomic::write_bytes_atomic;
use super::error::OutputError;

// ── OutputContext ─────────────────────────────────────────────────────────────

/// Runtime context for metadata output files.
///
/// Captures environment information not available from the solver output
/// or configuration alone: hostname, execution distribution, and wall-clock
/// timestamps. Built by the CLI or Python entry point and passed to the
/// output writers.
pub struct OutputContext {
    /// Hostname of the machine that produced this output.
    pub hostname: String,
    /// LP solver backend name (e.g. `"highs"` or `"clp"`).
    pub solver: String,
    /// LP solver version string (e.g. `"1.8.0"`), if known.
    pub solver_version: Option<String>,
    /// ISO 8601 timestamp when the phase started.
    pub started_at: String,
    /// ISO 8601 timestamp when the phase completed.
    pub completed_at: String,
    /// Execution distribution and environment information.
    pub distribution: DistributionInfo,
    /// Per-phase setup wall time, collected on the rank that writes metadata.
    /// `None` when the producer did not collect setup timings (e.g. the
    /// simulation context, or a producer that has not wired collection); the
    /// metadata `setup` section is then omitted.
    pub setup: Option<SetupTimings>,
    /// Run-level rollup of the per-entity production-model fit deviation.
    /// `None` when the producer fitted no model whose deviation is measured
    /// (e.g. the simulation context, or a run with no computed production
    /// model); the metadata `production_fit_deviation` section is then omitted.
    pub production_fit_deviation: Option<DeviationSummary>,
}

/// Read the system hostname via the `gethostname` syscall.
///
/// Falls back to `"unknown"` when the syscall yields an empty name.
#[must_use]
pub fn get_hostname() -> String {
    let name = gethostname::gethostname().to_string_lossy().into_owned();
    if name.is_empty() {
        "unknown".to_string()
    } else {
        name
    }
}

/// Return the current UTC time as an ISO 8601 string (e.g. `"2026-04-05T14:30:00Z"`).
#[must_use]
pub fn now_iso8601() -> String {
    chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}

// ── Shared nested structs ────────────────────────────────────────────────────

/// Per-host rank assignment for a single physical host.
///
/// Captures which global ranks were placed on a given host, enabling
/// reconstruction of the multi-host process layout from persisted metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HostLayout {
    /// Hostname as reported by the backend.
    pub hostname: String,
    /// Sorted global ranks assigned to this host.
    pub ranks: Vec<u32>,
}

/// Execution distribution information embedded in metadata files.
///
/// Captures the communication backend, process topology, and optional
/// MPI/scheduler metadata for reproducibility.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributionInfo {
    /// Communication backend: `"mpi"` or `"local"`.
    pub backend: String,
    /// Total number of processes in the communicator.
    pub world_size: u32,
    /// Number of processes that actually participated in computation.
    pub ranks_participated: u32,
    /// Number of distinct physical hosts.
    pub num_hosts: u32,
    /// Rayon threads per process.
    pub threads_per_rank: u32,
    /// MPI implementation version, e.g. `"Open MPI v4.1.6"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mpi_library: Option<String>,
    /// MPI standard version, e.g. `"MPI 4.0"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mpi_standard: Option<String>,
    /// Negotiated MPI thread safety level, e.g. `"Funneled"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_level: Option<String>,
    /// SLURM job ID, if running under SLURM.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slurm_job_id: Option<String>,
    /// Per-host rank assignment for multi-node runs. Empty for single-host or
    /// local runs.
    #[serde(default)]
    pub hosts: Vec<HostLayout>,
}

/// Selected training configuration fields captured for reproducibility.
///
/// This is an informational snapshot, not a normative schema. The canonical
/// configuration schema lives in `config.json` (see `cobre_io::config`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataConfiguration {
    /// Random seed used for scenario generation.
    pub seed: Option<i64>,
    /// Maximum iterations from the iteration-limit stopping rule.
    pub max_iterations: Option<u32>,
    /// Number of forward-pass scenario trajectories per iteration.
    pub forward_passes: Option<u32>,
    /// How multiple stopping rules combine: `"any"` or `"all"`.
    pub stopping_mode: String,
    /// Policy warm-start mode (e.g. `"fresh"`, `"resume"`).
    pub policy_mode: String,
}

/// Problem dimensionality embedded in [`TrainingMetadata`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataProblemDimensions {
    /// Number of stages in the planning horizon.
    pub num_stages: u32,
    /// Total number of hydro plants.
    pub num_hydros: u32,
    /// Total number of thermal plants.
    pub num_thermals: u32,
    /// Total number of buses.
    pub num_buses: u32,
    /// Total number of transmission lines.
    pub num_lines: u32,
}

/// Iteration counts embedded in [`TrainingMetadata`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataIterations {
    /// Number of iterations actually completed.
    pub completed: u32,
    /// Iteration at which convergence was achieved (`null` if not converged).
    pub converged_at: Option<u32>,
}

/// Convergence summary embedded in [`TrainingMetadata`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataConvergence {
    /// Whether a convergence-oriented stopping rule triggered termination.
    pub achieved: bool,
    /// Final optimality gap in percent (`null` when upper bound evaluation is disabled).
    pub final_gap_percent: Option<f64>,
    /// Human-readable description of the rule that terminated the run.
    pub termination_reason: String,
}

/// Row-pool summary embedded in [`TrainingMetadata`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataRowPool {
    /// Total rows generated over the entire run.
    pub total_generated: u64,
    /// Rows still active in the pool at termination.
    pub total_active: u64,
    /// Highest number of simultaneously active rows observed.
    pub peak_active: u64,
    /// Rows currently active in the LP at termination.
    #[serde(default)]
    pub cuts_active: u64,
    /// Sum of resident rows-in-LP over every lazy-selection solve in the run
    /// (reduced across ranks). With `rows_in_lp_solve_count`, the mean per solve.
    /// Zero when no lazy selection ran. `serde(default)` for old-metadata reads.
    #[serde(default)]
    pub rows_in_lp_total: u64,
    /// Number of lazy-selection solves in the run (reduced across ranks).
    #[serde(default)]
    pub rows_in_lp_solve_count: u64,
    /// Largest resident rows-in-LP over any single lazy-selection solve
    /// (reduced across ranks). Zero when no lazy selection ran.
    #[serde(default)]
    pub rows_in_lp_max: u64,
    /// Rows loaded from a boundary policy rather than generated by this run;
    /// a subset of `total_generated`. Zero when no boundary policy loaded.
    #[serde(default)]
    pub total_loaded: u64,
}

/// Final objective bounds embedded in [`TrainingMetadata`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataBounds {
    /// Final lower bound on the objective at termination.
    pub final_lower_bound: f64,
    /// Final upper bound estimate (`null` when upper-bound evaluation is disabled).
    pub final_upper_bound: Option<f64>,
    /// Standard deviation of the final upper-bound estimate (`null` when
    /// unavailable or the bound is exact).
    pub final_upper_bound_std: Option<f64>,
    /// Upper-bound regime: `"statistical"` (sampled forward) or `"exact"`
    /// (enumerated forward). `#[serde(default)]` reads pre-exact metadata as
    /// `"statistical"`.
    #[serde(default = "default_upper_bound_kind")]
    pub final_upper_bound_kind: String,
}

/// The pre-exact-regime default for [`MetadataBounds::final_upper_bound_kind`].
#[must_use]
pub fn default_upper_bound_kind() -> String {
    "statistical".to_string()
}

/// Default bounds (`final_lower_bound` `0.0`, absent upper bounds) used when
/// metadata omits the `bounds` field.
#[must_use]
pub fn default_bounds() -> MetadataBounds {
    MetadataBounds {
        final_lower_bound: 0.0,
        final_upper_bound: None,
        final_upper_bound_std: None,
        final_upper_bound_kind: default_upper_bound_kind(),
    }
}

/// Training solve statistics embedded in [`TrainingMetadata`].
///
/// Each field is optional so that absence is represented faithfully when the
/// producing run did not record the corresponding statistic.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MetadataTrainingSolveStats {
    /// Total number of LP solves performed during training.
    pub total_lp_solves: Option<u64>,
    /// Number of LP solves that succeeded on the first attempt.
    pub first_try: Option<u64>,
    /// Number of LP solves that succeeded after one or more retries.
    pub retried: Option<u64>,
    /// Number of LP solves that failed terminally.
    pub failed: Option<u64>,
    /// Cumulative wall-clock seconds spent in forward-phase LP solves.
    pub forward_solve_seconds: Option<f64>,
    /// Cumulative wall-clock seconds spent in backward-phase LP solves.
    pub backward_solve_seconds: Option<f64>,
    /// Degree of parallelism (e.g. worker count) used during training.
    pub parallelism: Option<u32>,
}

/// Per-phase setup wall time (wall-clock seconds) embedded in
/// [`TrainingMetadata`].
///
/// **Non-deterministic** — informational, never hashed, excluded from any parity
/// computation. All fields are `#[serde(default)]`, so pre-section metadata reads
/// back as zeros.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SetupTimings {
    /// Wall-clock seconds spent loading the input case.
    #[serde(default)]
    pub load_seconds: f64,
    /// Wall-clock seconds spent fitting the stochastic process.
    #[serde(default)]
    pub stochastic_fit_seconds: f64,
    /// Wall-clock seconds spent fitting the production model.
    #[serde(default)]
    pub production_fit_seconds: f64,
    /// Wall-clock seconds spent fitting the evaporation model.
    #[serde(default)]
    pub evaporation_fit_seconds: f64,
    /// Wall-clock seconds spent broadcasting setup data across ranks.
    #[serde(default)]
    pub broadcast_seconds: f64,
}

/// Run-level rollup of a per-entity model-fit deviation, embedded in
/// [`TrainingMetadata`].
///
/// Field names are deliberately generic — the producer maps its records into
/// this shape — so the metadata struct stays free of algorithm vocabulary. All
/// fields are `#[serde(default)]`, so pre-section metadata reads back as zeros /
/// `None`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeviationSummary {
    /// Number of per-entity entries the rollup summarizes (`>= 1` whenever the
    /// summary is present; the producer omits the section for an empty set).
    #[serde(default)]
    pub n_entries: u32,
    /// Arithmetic mean of the per-entry mean absolute deviation magnitudes.
    #[serde(default)]
    pub mean_abs: f64,
    /// Maximum of the per-entry max absolute deviation magnitudes.
    #[serde(default)]
    pub max_abs: f64,
    /// Largest per-entry relative (dimensionless) deviation across all entries.
    #[serde(default)]
    pub worst_relative: f64,
    /// The entry with the largest relative deviation, when any entry exists.
    #[serde(default)]
    pub worst_entry: Option<DeviationWorstEntry>,
}

/// The single worst per-entity entry of a [`DeviationSummary`].
///
/// Field names are deliberately generic (no producer-specific vocabulary); all
/// fields are `#[serde(default)]`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeviationWorstEntry {
    /// Integer identifier of the entity owning the worst entry.
    #[serde(default)]
    pub entity_id: i32,
    /// Integer identifier of the stage owning the worst entry.
    #[serde(default)]
    pub stage_id: i32,
    /// Relative (dimensionless) deviation of the worst entry.
    #[serde(default)]
    pub relative: f64,
    /// Mean absolute deviation magnitude of the worst entry.
    #[serde(default)]
    pub mean_abs: f64,
    /// Max absolute deviation magnitude of the worst entry.
    #[serde(default)]
    pub max_abs: f64,
}

/// Scenario counts embedded in [`SimulationMetadata`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataScenarios {
    /// Total number of scenarios dispatched for simulation.
    pub total: u32,
    /// Number of scenarios that completed without error.
    pub completed: u32,
    /// Number of scenarios that encountered a terminal error.
    pub failed: u32,
}

/// Aggregate cost statistics embedded in [`SimulationMetadata`].
///
/// Captures the expected total cost across the simulated scenarios together
/// with its dispersion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataCost {
    /// Mean total cost across simulated scenarios.
    pub mean_cost: f64,
    /// Standard deviation of the total cost across simulated scenarios.
    pub std_cost: f64,
}

/// Simulation solve statistics embedded in [`SimulationMetadata`].
///
/// Each field is optional so that absence is represented faithfully when the
/// producing run did not record the corresponding statistic.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MetadataSimulationSolveStats {
    /// Total number of LP solves performed during simulation.
    pub total_lp_solves: Option<u64>,
    /// Number of LP solves that succeeded on the first attempt.
    pub first_try: Option<u64>,
    /// Number of LP solves that succeeded after one or more retries.
    pub retried: Option<u64>,
    /// Number of LP solves that failed terminally.
    pub failed: Option<u64>,
    /// Cumulative wall-clock seconds spent in simulation LP solves.
    pub solve_seconds: Option<f64>,
    /// Degree of parallelism (e.g. worker count) used during simulation.
    pub parallelism: Option<u32>,
}

// ── TrainingMetadata ─────────────────────────────────────────────────────────

/// Merged metadata for the training output directory (`training/metadata.json`).
///
/// A single file containing all run information: convergence, cuts,
/// configuration, and environment.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingMetadata {
    /// Version of the cobre crate that produced this output.
    pub cobre_version: String,
    /// Hostname of the machine that ran training.
    pub hostname: String,
    /// LP solver backend name (e.g. `"highs"` or `"clp"`).
    pub solver: String,
    /// LP solver version string (e.g. `"1.8.0"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub solver_version: Option<String>,
    /// ISO 8601 timestamp when training started.
    pub started_at: String,
    /// ISO 8601 timestamp when training completed.
    pub completed_at: String,
    /// Total training wall-clock duration in seconds.
    pub duration_seconds: f64,
    /// Run status: `"complete"` or `"partial"`.
    pub status: String,
    /// Snapshot of key configuration fields.
    pub configuration: MetadataConfiguration,
    /// Problem size dimensions.
    pub problem_dimensions: MetadataProblemDimensions,
    /// Iteration completion counts.
    pub iterations: MetadataIterations,
    /// Convergence outcome.
    pub convergence: MetadataConvergence,
    /// Row-pool summary.
    pub row_pool: MetadataRowPool,
    /// Final objective bounds at termination.
    #[serde(default = "default_bounds")]
    pub bounds: MetadataBounds,
    /// Training solve statistics.
    #[serde(default)]
    pub solve_stats: MetadataTrainingSolveStats,
    /// Per-phase setup wall time (informational, non-deterministic). Absent
    /// when a run did not collect setup timings and from any legacy metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub setup: Option<SetupTimings>,
    /// Run-level rollup of the per-entity production-model fit deviation
    /// (informational, never hashed). Absent when a run fitted no model whose
    /// deviation is measured and from any legacy metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub production_fit_deviation: Option<DeviationSummary>,
    /// Execution distribution and environment information.
    pub distribution: DistributionInfo,
}

// ── SimulationMetadata ───────────────────────────────────────────────────────

/// Metadata for the simulation output directory (`simulation/metadata.json`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SimulationMetadata {
    /// Version of the cobre crate that produced this output.
    pub cobre_version: String,
    /// Hostname of the machine that ran simulation.
    pub hostname: String,
    /// LP solver backend name (e.g. `"highs"` or `"clp"`).
    pub solver: String,
    /// LP solver version string (e.g. `"1.8.0"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub solver_version: Option<String>,
    /// ISO 8601 timestamp when simulation started.
    pub started_at: String,
    /// ISO 8601 timestamp when simulation completed.
    pub completed_at: String,
    /// Total simulation wall-clock duration in seconds.
    pub duration_seconds: f64,
    /// Run status: `"complete"` or `"partial"`.
    pub status: String,
    /// Scenario completion counts.
    pub scenarios: MetadataScenarios,
    /// Aggregate cost statistics (`null` when cost was not persisted).
    #[serde(default)]
    pub cost: Option<MetadataCost>,
    /// Simulation solve statistics.
    #[serde(default)]
    pub solve_stats: MetadataSimulationSolveStats,
    /// Execution distribution and environment information.
    pub distribution: DistributionInfo,
}

// ── Writers ──────────────────────────────────────────────────────────────────

/// Write training metadata to `path` atomically.
///
/// # Errors
///
/// - [`OutputError::ManifestError`] if JSON serialization fails.
/// - [`OutputError::IoError`] if the file write or atomic rename fails.
pub fn write_training_metadata(
    path: &Path,
    metadata: &TrainingMetadata,
) -> Result<(), OutputError> {
    write_json_atomic(path, metadata, "training_metadata")
}

/// Write simulation metadata to `path` atomically.
///
/// # Errors
///
/// - [`OutputError::ManifestError`] if JSON serialization fails.
/// - [`OutputError::IoError`] if the file write or atomic rename fails.
pub fn write_simulation_metadata(
    path: &Path,
    metadata: &SimulationMetadata,
) -> Result<(), OutputError> {
    write_json_atomic(path, metadata, "simulation_metadata")
}

/// Read training metadata from `path`.
///
/// # Errors
///
/// - [`OutputError::IoError`] if the file cannot be read.
/// - [`OutputError::ManifestError`] if the file contains malformed JSON.
pub fn read_training_metadata(path: &Path) -> Result<TrainingMetadata, OutputError> {
    read_json(path, "training_metadata")
}

/// Read simulation metadata from `path`.
///
/// # Errors
///
/// - [`OutputError::IoError`] if the file cannot be read.
/// - [`OutputError::ManifestError`] if the file contains malformed JSON.
pub fn read_simulation_metadata(path: &Path) -> Result<SimulationMetadata, OutputError> {
    read_json(path, "simulation_metadata")
}

// ── Internal helpers ─────────────────────────────────────────────────────────

/// Read and deserialize a JSON file into `T`.
fn read_json<T>(path: &Path, manifest_type: &str) -> Result<T, OutputError>
where
    T: serde::de::DeserializeOwned,
{
    let content = std::fs::read_to_string(path).map_err(|e| OutputError::io(path, e))?;
    serde_json::from_str(&content).map_err(|e| OutputError::ManifestError {
        manifest_type: manifest_type.to_string(),
        message: e.to_string(),
    })
}

/// Serialize `value` to pretty-printed JSON and atomically write it to `path`.
fn write_json_atomic<T: Serialize>(
    path: &Path,
    value: &T,
    manifest_type: &str,
) -> Result<(), OutputError> {
    // Serialize here, not via the shared JSON helper, to keep the `ManifestError`
    // failure variant callers expect (bytes are identical); the shared helper
    // then owns the crash-safe write.
    let json = serde_json::to_string_pretty(value).map_err(|e| OutputError::ManifestError {
        manifest_type: manifest_type.to_string(),
        message: e.to_string(),
    })?;

    write_bytes_atomic(path, json.as_bytes())
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::float_cmp,
    clippy::cast_possible_truncation
)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    // ── Helpers ───────────────────────────────────────────────────────────────

    fn make_distribution_info() -> DistributionInfo {
        DistributionInfo {
            backend: "local".to_string(),
            world_size: 1,
            ranks_participated: 1,
            num_hosts: 1,
            threads_per_rank: 1,
            mpi_library: None,
            mpi_standard: None,
            thread_level: None,
            slurm_job_id: None,
            hosts: Vec::new(),
        }
    }

    fn make_training_metadata() -> TrainingMetadata {
        TrainingMetadata {
            cobre_version: env!("CARGO_PKG_VERSION").to_string(),
            hostname: "test-host".to_string(),
            solver: "highs".to_string(),
            solver_version: Some("1.8.0".to_string()),
            started_at: "2026-01-17T08:00:00Z".to_string(),
            completed_at: "2026-01-17T12:30:00Z".to_string(),
            duration_seconds: 16_200.0,
            status: "complete".to_string(),
            configuration: MetadataConfiguration {
                seed: Some(42),
                max_iterations: Some(100),
                forward_passes: Some(192),
                stopping_mode: "any".to_string(),
                policy_mode: "fresh".to_string(),
            },
            problem_dimensions: MetadataProblemDimensions {
                num_stages: 12,
                num_hydros: 160,
                num_thermals: 200,
                num_buses: 5,
                num_lines: 8,
            },
            iterations: MetadataIterations {
                completed: 100,
                converged_at: Some(95),
            },
            convergence: MetadataConvergence {
                achieved: true,
                final_gap_percent: Some(0.45),
                termination_reason: "bound_stalling".to_string(),
            },
            row_pool: MetadataRowPool {
                total_generated: 1_250_000,
                total_active: 980_000,
                peak_active: 1_100_000,
                cuts_active: 980_000,
                rows_in_lp_total: 0,
                rows_in_lp_solve_count: 0,
                rows_in_lp_max: 0,
                total_loaded: 0,
            },
            bounds: MetadataBounds {
                final_lower_bound: 48_500.0,
                final_upper_bound: Some(49_000.0),
                final_upper_bound_std: Some(250.0),
                final_upper_bound_kind: "statistical".to_string(),
            },
            solve_stats: MetadataTrainingSolveStats {
                total_lp_solves: Some(84_000),
                first_try: Some(80_000),
                retried: Some(3_800),
                failed: Some(200),
                forward_solve_seconds: Some(123.5),
                backward_solve_seconds: Some(456.75),
                parallelism: Some(8),
            },
            setup: None,
            production_fit_deviation: None,
            distribution: make_distribution_info(),
        }
    }

    fn make_simulation_metadata() -> SimulationMetadata {
        SimulationMetadata {
            cobre_version: env!("CARGO_PKG_VERSION").to_string(),
            hostname: "test-host".to_string(),
            solver: "highs".to_string(),
            solver_version: Some("1.8.0".to_string()),
            started_at: "2026-01-17T13:00:00Z".to_string(),
            completed_at: "2026-01-17T13:15:00Z".to_string(),
            duration_seconds: 900.0,
            status: "complete".to_string(),
            scenarios: MetadataScenarios {
                total: 100,
                completed: 100,
                failed: 0,
            },
            cost: Some(MetadataCost {
                mean_cost: 12_345.6,
                std_cost: 200.0,
            }),
            solve_stats: MetadataSimulationSolveStats {
                total_lp_solves: Some(50_000),
                first_try: Some(48_000),
                retried: Some(1_900),
                failed: Some(100),
                solve_seconds: Some(321.0),
                parallelism: Some(8),
            },
            distribution: make_distribution_info(),
        }
    }

    // ── Roundtrip tests ──────────────────────────────────────────────────────

    #[test]
    fn training_metadata_roundtrip() {
        let original = make_training_metadata();
        let json = serde_json::to_string_pretty(&original).unwrap();
        let decoded: TrainingMetadata = serde_json::from_str(&json).unwrap();

        assert_eq!(decoded.cobre_version, original.cobre_version);
        assert_eq!(decoded.hostname, original.hostname);
        assert_eq!(decoded.solver, original.solver);
        assert_eq!(decoded.started_at, original.started_at);
        assert_eq!(decoded.completed_at, original.completed_at);
        assert_eq!(decoded.duration_seconds, original.duration_seconds);
        assert_eq!(decoded.status, original.status);
        assert_eq!(decoded.iterations.completed, original.iterations.completed);
        assert_eq!(
            decoded.iterations.converged_at,
            original.iterations.converged_at
        );
        assert_eq!(decoded.convergence.achieved, original.convergence.achieved);
        assert_eq!(
            decoded.convergence.final_gap_percent,
            original.convergence.final_gap_percent
        );
        assert_eq!(
            decoded.row_pool.total_generated,
            original.row_pool.total_generated
        );
        assert_eq!(
            decoded.row_pool.total_active,
            original.row_pool.total_active
        );
        assert_eq!(decoded.row_pool.peak_active, original.row_pool.peak_active);
        assert_eq!(
            decoded.distribution.world_size,
            original.distribution.world_size
        );
    }

    #[test]
    fn simulation_metadata_roundtrip() {
        let original = make_simulation_metadata();
        let json = serde_json::to_string_pretty(&original).unwrap();
        let decoded: SimulationMetadata = serde_json::from_str(&json).unwrap();

        assert_eq!(decoded.cobre_version, original.cobre_version);
        assert_eq!(decoded.status, original.status);
        assert_eq!(decoded.scenarios.total, original.scenarios.total);
        assert_eq!(decoded.scenarios.completed, original.scenarios.completed);
        assert_eq!(decoded.scenarios.failed, original.scenarios.failed);
        assert_eq!(
            decoded.distribution.world_size,
            original.distribution.world_size
        );
    }

    #[test]
    fn simulation_metadata_cost_round_trip() {
        let original = SimulationMetadata {
            cost: Some(MetadataCost {
                mean_cost: 12_345.6,
                std_cost: 200.0,
            }),
            ..make_simulation_metadata()
        };

        let json = serde_json::to_string(&original).unwrap();
        assert!(
            json.contains(r#""mean_cost":12345.6"#),
            "serialized JSON must contain the mean cost, got: {json}"
        );
        assert!(
            json.contains(r#""std_cost":200.0"#),
            "serialized JSON must contain the std cost, got: {json}"
        );

        let decoded: SimulationMetadata = serde_json::from_str(&json).unwrap();
        let cost = decoded.cost.expect("cost must be present after round-trip");
        assert_eq!(cost.mean_cost, 12_345.6);
        assert_eq!(cost.std_cost, 200.0);
    }

    #[test]
    fn simulation_metadata_solve_stats_round_trip() {
        let original = SimulationMetadata {
            solve_stats: MetadataSimulationSolveStats {
                total_lp_solves: Some(50_000),
                first_try: Some(48_000),
                retried: Some(1_900),
                failed: Some(100),
                solve_seconds: Some(321.0),
                parallelism: Some(8),
            },
            ..make_simulation_metadata()
        };

        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        write_simulation_metadata(&path, &original).expect("write must succeed");
        let decoded = read_simulation_metadata(&path).expect("read must succeed");

        assert_eq!(decoded.solve_stats.total_lp_solves, Some(50_000));
        assert_eq!(decoded.solve_stats.first_try, Some(48_000));
        assert_eq!(decoded.solve_stats.retried, Some(1_900));
        assert_eq!(decoded.solve_stats.failed, Some(100));
        assert_eq!(decoded.solve_stats.solve_seconds, Some(321.0));
        assert_eq!(decoded.solve_stats.parallelism, Some(8));
    }

    #[test]
    fn simulation_metadata_back_compat_without_cost_or_solve_stats() {
        let legacy = r#"{
            "cobre_version": "0.0.0",
            "hostname": "legacy-host",
            "solver": "highs",
            "started_at": "2026-01-17T13:00:00Z",
            "completed_at": "2026-01-17T13:15:00Z",
            "duration_seconds": 900.0,
            "status": "complete",
            "scenarios": {
                "total": 100,
                "completed": 100,
                "failed": 0
            },
            "distribution": {
                "backend": "local",
                "world_size": 1,
                "ranks_participated": 1,
                "num_hosts": 1,
                "threads_per_rank": 1
            }
        }"#;

        let decoded: SimulationMetadata = serde_json::from_str(legacy).unwrap();
        assert!(decoded.cost.is_none());
        assert_eq!(decoded.solve_stats.total_lp_solves, None);
        assert_eq!(decoded.solve_stats.parallelism, None);
    }

    #[test]
    fn distribution_info_hosts_round_trip() {
        let original = DistributionInfo {
            hosts: vec![HostLayout {
                hostname: "node01".to_string(),
                ranks: vec![0, 1, 2, 3],
            }],
            ..make_distribution_info()
        };

        let json = serde_json::to_string(&original).unwrap();
        assert!(
            json.contains(r#""hosts":[{"hostname":"node01","ranks":[0,1,2,3]}]"#),
            "serialized JSON must contain the hosts array, got: {json}"
        );

        let decoded: DistributionInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.hosts.len(), 1);
        assert_eq!(decoded.hosts[0].hostname, "node01");
        assert_eq!(decoded.hosts[0].ranks, vec![0, 1, 2, 3]);
    }

    #[test]
    fn distribution_info_empty_hosts_serialize_as_array() {
        let info = make_distribution_info();
        let json = serde_json::to_string(&info).unwrap();
        assert!(
            json.contains(r#""hosts":[]"#),
            "empty hosts must serialize as [], got: {json}"
        );
    }

    #[test]
    fn distribution_info_back_compat_without_hosts() {
        let legacy = r#"{
            "backend": "local",
            "world_size": 1,
            "ranks_participated": 1,
            "num_hosts": 1,
            "threads_per_rank": 1
        }"#;

        let decoded: DistributionInfo = serde_json::from_str(legacy).unwrap();
        assert!(
            decoded.hosts.is_empty(),
            "missing hosts key must deserialize to an empty vector"
        );
    }

    #[test]
    fn training_metadata_bounds_round_trip() {
        let original = TrainingMetadata {
            bounds: MetadataBounds {
                final_lower_bound: 48_500.0,
                final_upper_bound: Some(49_000.0),
                final_upper_bound_std: Some(250.0),
                final_upper_bound_kind: "statistical".to_string(),
            },
            ..make_training_metadata()
        };

        let json = serde_json::to_string(&original).unwrap();
        assert!(
            json.contains(r#""final_lower_bound":48500.0"#),
            "serialized JSON must contain the lower bound, got: {json}"
        );
        assert!(
            json.contains(r#""final_upper_bound":49000.0"#),
            "serialized JSON must contain the upper bound, got: {json}"
        );

        let decoded: TrainingMetadata = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.bounds.final_lower_bound, 48_500.0);
        assert_eq!(decoded.bounds.final_upper_bound, Some(49_000.0));
        assert_eq!(decoded.bounds.final_upper_bound_std, Some(250.0));
    }

    #[test]
    fn training_metadata_solve_stats_round_trip() {
        let original = TrainingMetadata {
            solve_stats: MetadataTrainingSolveStats {
                total_lp_solves: Some(84_000),
                first_try: Some(80_000),
                retried: Some(3_800),
                failed: Some(200),
                forward_solve_seconds: Some(123.5),
                backward_solve_seconds: Some(456.75),
                parallelism: Some(8),
            },
            ..make_training_metadata()
        };

        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        write_training_metadata(&path, &original).expect("write must succeed");
        let decoded = read_training_metadata(&path).expect("read must succeed");

        assert_eq!(decoded.solve_stats.total_lp_solves, Some(84_000));
        assert_eq!(decoded.solve_stats.first_try, Some(80_000));
        assert_eq!(decoded.solve_stats.retried, Some(3_800));
        assert_eq!(decoded.solve_stats.failed, Some(200));
        assert_eq!(decoded.solve_stats.forward_solve_seconds, Some(123.5));
        assert_eq!(decoded.solve_stats.backward_solve_seconds, Some(456.75));
        assert_eq!(decoded.solve_stats.parallelism, Some(8));
    }

    #[test]
    fn training_metadata_back_compat_without_bounds_or_solve_stats() {
        let legacy = r#"{
            "cobre_version": "0.0.0",
            "hostname": "legacy-host",
            "solver": "highs",
            "started_at": "2026-01-17T08:00:00Z",
            "completed_at": "2026-01-17T12:30:00Z",
            "duration_seconds": 16200.0,
            "status": "complete",
            "configuration": {
                "seed": 42,
                "max_iterations": 100,
                "forward_passes": 192,
                "stopping_mode": "any",
                "policy_mode": "fresh"
            },
            "problem_dimensions": {
                "num_stages": 12,
                "num_hydros": 160,
                "num_thermals": 200,
                "num_buses": 5,
                "num_lines": 8
            },
            "iterations": {
                "completed": 100,
                "converged_at": 95
            },
            "convergence": {
                "achieved": true,
                "final_gap_percent": 0.45,
                "termination_reason": "bound_stalling"
            },
            "row_pool": {
                "total_generated": 1250000,
                "total_active": 980000,
                "peak_active": 1100000
            },
            "distribution": {
                "backend": "local",
                "world_size": 1,
                "ranks_participated": 1,
                "num_hosts": 1,
                "threads_per_rank": 1
            }
        }"#;

        let decoded: TrainingMetadata = serde_json::from_str(legacy).unwrap();
        assert_eq!(decoded.bounds.final_lower_bound, 0.0);
        assert_eq!(decoded.bounds.final_upper_bound, None);
        assert_eq!(decoded.bounds.final_upper_bound_std, None);
        assert_eq!(decoded.solve_stats.total_lp_solves, None);
        assert_eq!(decoded.solve_stats.parallelism, None);
    }

    #[test]
    fn setup_timings_round_trips() {
        let original = TrainingMetadata {
            setup: Some(SetupTimings {
                load_seconds: 1.5,
                stochastic_fit_seconds: 2.25,
                production_fit_seconds: 3.75,
                evaporation_fit_seconds: 0.5,
                broadcast_seconds: 0.125,
            }),
            ..make_training_metadata()
        };

        let json = serde_json::to_string(&original).unwrap();
        let decoded: TrainingMetadata = serde_json::from_str(&json).unwrap();

        let setup = decoded
            .setup
            .expect("setup must be present after round-trip");
        assert_eq!(setup.load_seconds, 1.5);
        assert_eq!(setup.stochastic_fit_seconds, 2.25);
        assert_eq!(setup.production_fit_seconds, 3.75);
        assert_eq!(setup.evaporation_fit_seconds, 0.5);
        assert_eq!(setup.broadcast_seconds, 0.125);
    }

    #[test]
    fn training_metadata_without_setup_reads_as_none() {
        let without_setup = r#"{
            "cobre_version": "0.0.0",
            "hostname": "legacy-host",
            "solver": "highs",
            "started_at": "2026-01-17T08:00:00Z",
            "completed_at": "2026-01-17T12:30:00Z",
            "duration_seconds": 16200.0,
            "status": "complete",
            "configuration": {
                "seed": 42,
                "max_iterations": 100,
                "forward_passes": 192,
                "stopping_mode": "any",
                "policy_mode": "fresh"
            },
            "problem_dimensions": {
                "num_stages": 12,
                "num_hydros": 160,
                "num_thermals": 200,
                "num_buses": 5,
                "num_lines": 8
            },
            "iterations": {
                "completed": 100,
                "converged_at": 95
            },
            "convergence": {
                "achieved": true,
                "final_gap_percent": 0.45,
                "termination_reason": "bound_stalling"
            },
            "row_pool": {
                "total_generated": 1250000,
                "total_active": 980000,
                "peak_active": 1100000
            },
            "distribution": {
                "backend": "local",
                "world_size": 1,
                "ranks_participated": 1,
                "num_hosts": 1,
                "threads_per_rank": 1
            }
        }"#;

        let decoded: TrainingMetadata = serde_json::from_str(without_setup).unwrap();
        assert!(decoded.setup.is_none());
    }

    // ── DeviationSummary ───────────────────────────────────────────────────────

    #[test]
    fn deviation_summary_with_worst_entry_round_trips() {
        let original = TrainingMetadata {
            production_fit_deviation: Some(DeviationSummary {
                n_entries: 3,
                mean_abs: 4.1,
                max_abs: 31.7,
                worst_relative: 0.062,
                worst_entry: Some(DeviationWorstEntry {
                    entity_id: 12,
                    stage_id: 4,
                    relative: 0.062,
                    mean_abs: 8.0,
                    max_abs: 31.7,
                }),
            }),
            ..make_training_metadata()
        };

        let json = serde_json::to_string(&original).unwrap();
        assert!(
            json.contains(r#""production_fit_deviation""#),
            "serialized JSON must contain the deviation section, got: {json}"
        );

        let decoded: TrainingMetadata = serde_json::from_str(&json).unwrap();
        let summary = decoded
            .production_fit_deviation
            .expect("deviation summary must survive round-trip");
        assert_eq!(summary.n_entries, 3);
        assert_eq!(summary.mean_abs, 4.1);
        assert_eq!(summary.max_abs, 31.7);
        assert_eq!(summary.worst_relative, 0.062);
        let worst = summary.worst_entry.expect("worst entry must be present");
        assert_eq!(worst.entity_id, 12);
        assert_eq!(worst.stage_id, 4);
        assert_eq!(worst.relative, 0.062);
        assert_eq!(worst.mean_abs, 8.0);
        assert_eq!(worst.max_abs, 31.7);
    }

    #[test]
    fn training_metadata_skips_deviation_when_none() {
        let metadata = TrainingMetadata {
            production_fit_deviation: None,
            ..make_training_metadata()
        };

        let json = serde_json::to_string(&metadata).unwrap();
        assert!(
            !json.contains("production_fit_deviation"),
            "the key must be omitted when None, got: {json}"
        );
    }

    #[test]
    fn training_metadata_without_deviation_reads_as_none() {
        let without_deviation = r#"{
            "cobre_version": "0.0.0",
            "hostname": "legacy-host",
            "solver": "highs",
            "started_at": "2026-01-17T08:00:00Z",
            "completed_at": "2026-01-17T12:30:00Z",
            "duration_seconds": 16200.0,
            "status": "complete",
            "configuration": {
                "seed": 42,
                "max_iterations": 100,
                "forward_passes": 192,
                "stopping_mode": "any",
                "policy_mode": "fresh"
            },
            "problem_dimensions": {
                "num_stages": 12,
                "num_hydros": 160,
                "num_thermals": 200,
                "num_buses": 5,
                "num_lines": 8
            },
            "iterations": {
                "completed": 100,
                "converged_at": 95
            },
            "convergence": {
                "achieved": true,
                "final_gap_percent": 0.45,
                "termination_reason": "bound_stalling"
            },
            "row_pool": {
                "total_generated": 1250000,
                "total_active": 980000,
                "peak_active": 1100000
            },
            "distribution": {
                "backend": "local",
                "world_size": 1,
                "ranks_participated": 1,
                "num_hosts": 1,
                "threads_per_rank": 1
            }
        }"#;

        let decoded: TrainingMetadata = serde_json::from_str(without_deviation).unwrap();
        assert!(decoded.production_fit_deviation.is_none());
    }

    // ── Writer tests ─────────────────────────────────────────────────────────

    #[test]
    fn write_training_metadata_creates_file() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        let metadata = make_training_metadata();

        write_training_metadata(&path, &metadata).expect("write must succeed");

        assert!(path.exists(), "metadata file must exist after write");
        let content = std::fs::read_to_string(&path).unwrap();
        let _parsed: serde_json::Value =
            serde_json::from_str(&content).expect("file must contain valid JSON");
    }

    #[test]
    fn write_simulation_metadata_creates_file() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        let metadata = make_simulation_metadata();

        write_simulation_metadata(&path, &metadata).expect("write must succeed");

        assert!(path.exists(), "metadata file must exist after write");
        let content = std::fs::read_to_string(&path).unwrap();
        let _parsed: serde_json::Value =
            serde_json::from_str(&content).expect("file must contain valid JSON");
    }

    #[test]
    fn write_training_metadata_fields_survive_write_read_cycle() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        let original = make_training_metadata();

        write_training_metadata(&path, &original).expect("write must succeed");
        let decoded = read_training_metadata(&path).expect("read must succeed");

        assert_eq!(decoded.iterations.completed, 100);
        assert!(decoded.convergence.achieved);
        assert_eq!(decoded.row_pool.total_generated, 1_250_000);
    }

    #[test]
    fn write_simulation_metadata_fields_survive_write_read_cycle() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        let original = make_simulation_metadata();

        write_simulation_metadata(&path, &original).expect("write must succeed");
        let decoded = read_simulation_metadata(&path).expect("read must succeed");

        assert_eq!(decoded.scenarios.total, 100);
        assert_eq!(decoded.scenarios.completed, 100);
    }

    // ── Error handling ───────────────────────────────────────────────────────

    #[test]
    fn write_training_metadata_missing_parent_returns_io_error() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("nonexistent_subdir").join("metadata.json");
        let metadata = make_training_metadata();

        let result = write_training_metadata(&path, &metadata);

        assert!(
            matches!(result, Err(OutputError::IoError { .. })),
            "error must be IoError when parent directory is missing, got: {result:?}"
        );
    }

    #[test]
    fn write_simulation_metadata_missing_parent_returns_io_error() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("nonexistent_subdir").join("metadata.json");
        let metadata = make_simulation_metadata();

        let result = write_simulation_metadata(&path, &metadata);

        assert!(
            matches!(result, Err(OutputError::IoError { .. })),
            "error must be IoError when parent directory is missing"
        );
    }

    #[test]
    fn read_training_metadata_missing_file() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("nonexistent.json");

        let result = read_training_metadata(&path);

        assert!(
            matches!(result, Err(OutputError::IoError { .. })),
            "missing file must return OutputError::IoError, got: {result:?}"
        );
    }

    #[test]
    fn read_training_metadata_malformed_json() {
        use std::io::Write;
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        let mut file = std::fs::File::create(&path).unwrap();
        writeln!(file, "{{not valid json at all").unwrap();

        let result = read_training_metadata(&path);

        assert!(
            matches!(result, Err(OutputError::ManifestError { .. })),
            "malformed JSON must return OutputError::ManifestError, got: {result:?}"
        );
    }

    // ── Atomic write ─────────────────────────────────────────────────────────

    #[test]
    fn write_metadata_atomic_no_tmp_remains() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        let metadata = make_training_metadata();

        write_training_metadata(&path, &metadata).expect("write must succeed");

        let tmp = path.with_extension("json.tmp");
        assert!(
            !tmp.exists(),
            "no .tmp file must remain after a successful write"
        );
        assert!(path.exists(), "the target file must exist");
    }

    // ── cobre_version ────────────────────────────────────────────────────────

    #[test]
    fn training_metadata_cobre_version_matches_cargo_pkg_version() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.json");
        let metadata = make_training_metadata();

        write_training_metadata(&path, &metadata).expect("write must succeed");

        let content = std::fs::read_to_string(&path).unwrap();
        let value: serde_json::Value = serde_json::from_str(&content).unwrap();

        let version = value["cobre_version"]
            .as_str()
            .expect("cobre_version must be a string");
        assert_eq!(version, env!("CARGO_PKG_VERSION"));
    }

    // ── Helpers ──────────────────────────────────────────────────────────────

    #[test]
    fn now_iso8601_returns_valid_format() {
        let ts = now_iso8601();
        assert!(ts.ends_with('Z'), "timestamp must end with Z: {ts}");
        assert!(ts.contains('T'), "timestamp must contain T separator: {ts}");
    }
}