candle-graph 0.3.0

Static structure and dataflow analysis for candle-rs models
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
//! Bounded runtime evidence and gradient-audit protocol.
//!
//! Transport/import layer for target-side probe emissions. Schema:
//! `candle-graph/runtime/1`. No Candle dependency.

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::fmt;
use std::io::Write;

use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};

/// Schema identifier written into every document and expected on parse.
pub const SCHEMA: &str = "candle-graph/runtime/1";
pub const SCHEMA_V2: &str = "candle-graph/runtime/2";
pub const SCHEMA_V3: &str = "candle-graph/runtime/3";

/// Build / process metadata for a single probe run.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RunMetadata {
    /// Analyzed or probed entrypoint, e.g. `train` or `Model::forward`.
    pub entrypoint: String,
    /// Cargo profile, e.g. `debug` or `release`.
    pub profile: String,
    /// Enabled Cargo features at probe time.
    #[serde(default)]
    pub cargo_features: Vec<String>,
    /// Relevant `cfg` flags observed by the probe.
    #[serde(default)]
    pub cfg: Vec<String>,
    /// Optional analysis identity this trace was captured against (e.g. model IR `analysis_id`).
    /// Omitted for backward compatibility; when present, importers must check it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub analysis_id: Option<String>,
    /// Optional build identity (package/features/profile fingerprint) for the probed binary.
    /// Omitted for backward compatibility; when present, importers must check it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub build_id: Option<String>,
    /// Train vs inference phase for this trace (`train` / `infer`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub phase: Option<String>,
}

/// Expected analysis/build identity supplied by an importer when correlating a trace.
///
/// Only fields that are `Some` on **both** this value and the trace metadata are compared.
/// Omitted optional identity on either side is compatible (backward compatible).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExpectedIdentity {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub analysis_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub build_id: Option<String>,
}

/// Which optional identity field disagreed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum IdentityField {
    AnalysisId,
    BuildId,
}

impl fmt::Display for IdentityField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AnalysisId => write!(f, "analysis_id"),
            Self::BuildId => write!(f, "build_id"),
        }
    }
}

/// Mismatch between trace metadata and an importer-supplied expected identity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdentityMismatch {
    pub field: IdentityField,
    pub expected: String,
    pub observed: String,
}

/// Confidence for a refined runtime observation.
///
/// Importers must not treat [`ObservationConfidence::Unknown`] conflicts as proven facts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ObservationConfidence {
    /// Observations for this identity agree.
    Proven,
    /// Observations disagree, identity does not match, or evidence is otherwise insufficient.
    Unknown,
}

/// One tensor snapshot emitted by the probe.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TensorObservation {
    /// Stable event identity within a trace; used for duplicate detection.
    pub event_id: String,
    /// Optional stable static identity shared across related observations.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub static_id: Option<String>,
    /// Optional source location or label from the probe.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// Optional training step index when the trace is a time series.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step: Option<u64>,
    pub shape: Vec<usize>,
    pub dtype: String,
    pub device: String,
    /// Whether storage is contiguous (layout fact).
    pub contiguous: bool,
    pub requires_grad: bool,
    /// Optional opaque storage identity from the runtime.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub storage_id: Option<String>,
}

/// One operation observation emitted by the probe.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OperationObservation {
    pub event_id: String,
    /// Operation name / kind, e.g. `matmul`, `add`.
    pub op: String,
    /// Optional stable static identity for correlating with static analysis.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub static_id: Option<String>,
    /// Optional source location or label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// Input tensor event or storage ids observed at the call.
    #[serde(default)]
    pub inputs: Vec<String>,
    /// Output tensor event or storage id, when known.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output: Option<String>,
    /// Optional training step index when the trace is a time series.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step: Option<u64>,
    /// Wall time spent in this operation (nanoseconds), when profiled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_ns: Option<u64>,
}

/// Observed data-flow edge timing between two static ids (profiler output).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EdgeTimingObservation {
    pub event_id: String,
    pub from_static_id: String,
    pub to_static_id: String,
    pub duration_ns: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step: Option<u64>,
}

/// Builder-root + parameter-key identity for gradient facts.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ParamIdentity {
    /// Builder root namespace (e.g. `vb`), matching static analysis roots.
    pub root: String,
    /// Parameter key under that root.
    pub key: String,
}

/// Observed gradient presence / quality for one parameter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GradientState {
    Present,
    Missing,
    Zero,
    NonFinite,
}

impl fmt::Display for GradientState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Present => write!(f, "present"),
            Self::Missing => write!(f, "missing"),
            Self::Zero => write!(f, "zero"),
            Self::NonFinite => write!(f, "non_finite"),
        }
    }
}

/// Per-parameter gradient fact keyed by builder root + parameter key.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GradientFact {
    pub event_id: String,
    pub root: String,
    pub key: String,
    pub state: GradientState,
    /// Optional training step index when the trace is a time series.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step: Option<u64>,
    /// Optional L2 (or probe-defined) norm; must be consistent with [`GradientState`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub norm: Option<f64>,
}

impl GradientFact {
    pub fn identity(&self) -> ParamIdentity {
        ParamIdentity {
            root: self.root.clone(),
            key: self.key.clone(),
        }
    }

    /// Returns an error when state and optional norm contradict each other.
    pub fn validate(&self) -> Result<()> {
        match self.state {
            GradientState::Missing => {
                if self.norm.is_some() {
                    bail!(
                        "invalid gradient fact {}:{}: missing state must not carry a norm",
                        self.root,
                        self.key
                    );
                }
            }
            GradientState::Zero => {
                if let Some(n) = self.norm {
                    if n != 0.0 || !n.is_finite() {
                        bail!(
                            "invalid gradient fact {}:{}: zero state requires norm 0.0 when set, got {n}",
                            self.root,
                            self.key
                        );
                    }
                }
            }
            GradientState::Present => {
                if let Some(n) = self.norm {
                    if !n.is_finite() {
                        bail!(
                            "invalid gradient fact {}:{}: present state cannot have non-finite norm",
                            self.root,
                            self.key
                        );
                    }
                    if n == 0.0 {
                        bail!(
                            "invalid gradient fact {}:{}: present state cannot have zero norm (use zero)",
                            self.root,
                            self.key
                        );
                    }
                }
            }
            GradientState::NonFinite => {
                if let Some(n) = self.norm {
                    if n.is_finite() {
                        bail!(
                            "invalid gradient fact {}:{}: non_finite state cannot have finite norm {n}",
                            self.root,
                            self.key
                        );
                    }
                }
            }
        }
        Ok(())
    }
}

/// Forward value-range observation for numeric-domain runtime audits.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ValueObservation {
    pub event_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub static_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step: Option<u64>,
    pub min: f64,
    pub max: f64,
    pub abs_max: f64,
    #[serde(default)]
    pub nonfinite_count: u64,
    #[serde(default)]
    pub saturated_count: u64,
}

/// Full runtime trace document (`candle-graph/runtime/1` or `/2`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RuntimeTrace {
    pub schema: String,
    pub run: RunMetadata,
    #[serde(default)]
    pub tensors: Vec<TensorObservation>,
    #[serde(default)]
    pub operations: Vec<OperationObservation>,
    #[serde(default)]
    pub gradients: Vec<GradientFact>,
    #[serde(default)]
    pub values: Vec<ValueObservation>,
    #[serde(default)]
    pub edge_timings: Vec<EdgeTimingObservation>,
}

/// JSONL / streaming event records that assemble into a [`RuntimeTrace`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum RuntimeEvent {
    /// Declares schema + run metadata (at most one per stream).
    Meta {
        schema: String,
        #[serde(flatten)]
        run: RunMetadata,
    },
    Tensor(TensorObservation),
    Operation(OperationObservation),
    Gradient(GradientFact),
    Value(ValueObservation),
    EdgeTiming(EdgeTimingObservation),
}

/// Streaming JSONL emitter for instrumented CPU or synthetic rollouts.
///
/// The writer emits metadata immediately, rejects duplicate event ids before writing an event,
/// and uses the same validation rules as the importer. Tensor callers should populate
/// [`TensorObservation::static_id`] with an id copied from the model IR or a `tensor` query.
pub struct RuntimeTraceWriter<W: Write> {
    writer: W,
    seen_event_ids: HashSet<String>,
}

impl<W: Write> RuntimeTraceWriter<W> {
    /// Start a JSONL stream by writing its required metadata event (schema v1).
    pub fn new(writer: W, run: RunMetadata) -> Result<Self> {
        Self::new_with_schema(writer, SCHEMA, run)
    }

    /// Start a JSONL stream with an explicit schema (`/1`, `/2`, or `/3`).
    pub fn new_with_schema(writer: W, schema: &str, run: RunMetadata) -> Result<Self> {
        let mut output = Self {
            writer,
            seen_event_ids: HashSet::new(),
        };
        output.write_event(&RuntimeEvent::Meta {
            schema: schema.to_string(),
            run,
        })?;
        Ok(output)
    }

    /// Emit one tensor observation. `static_id` is the static/runtime correlation key.
    pub fn tensor(&mut self, observation: TensorObservation) -> Result<()> {
        self.reserve_event_id(&observation.event_id)?;
        self.write_event(&RuntimeEvent::Tensor(observation))
    }

    /// Emit one operation observation.
    pub fn operation(&mut self, observation: OperationObservation) -> Result<()> {
        self.reserve_event_id(&observation.event_id)?;
        self.write_event(&RuntimeEvent::Operation(observation))
    }

    /// Emit one parameter-gradient fact after checking state/norm consistency.
    pub fn gradient(&mut self, fact: GradientFact) -> Result<()> {
        fact.validate()?;
        self.reserve_event_id(&fact.event_id)?;
        self.write_event(&RuntimeEvent::Gradient(fact))
    }

    /// Emit one forward value-range observation.
    pub fn value(&mut self, observation: ValueObservation) -> Result<()> {
        self.reserve_event_id(&observation.event_id)?;
        self.write_event(&RuntimeEvent::Value(observation))
    }

    /// Emit one data-flow edge timing observation (runtime v3).
    pub fn edge_timing(&mut self, observation: EdgeTimingObservation) -> Result<()> {
        self.reserve_event_id(&observation.event_id)?;
        self.write_event(&RuntimeEvent::EdgeTiming(observation))
    }

    pub fn flush(&mut self) -> Result<()> {
        self.writer.flush().context("flushing runtime JSONL trace")
    }

    /// Flush the stream and return the underlying writer.
    pub fn finish(mut self) -> Result<W> {
        self.writer
            .flush()
            .context("flushing runtime JSONL trace")?;
        Ok(self.writer)
    }

    fn reserve_event_id(&mut self, event_id: &str) -> Result<()> {
        if event_id.is_empty() {
            bail!("empty event_id is not allowed");
        }
        if !self.seen_event_ids.insert(event_id.to_string()) {
            bail!("duplicate event_id `{event_id}`");
        }
        Ok(())
    }

    fn write_event(&mut self, event: &RuntimeEvent) -> Result<()> {
        let mut line = serde_json::to_vec(event).context("serializing runtime JSONL event")?;
        line.push(b'\n');
        self.writer
            .write_all(&line)
            .context("writing runtime JSONL event")
    }
}

/// Which tensor attribute disagreed across observations sharing a `static_id`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TensorConflictKind {
    Dtype,
    Device,
    Shape,
    Layout,
    RequiresGrad,
}

fn unknown_confidence() -> ObservationConfidence {
    ObservationConfidence::Unknown
}

/// Conflict among repeated tensor observations for one static id.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TensorConflict {
    pub static_id: String,
    pub kind: TensorConflictKind,
    /// Distinct observed values, sorted.
    pub values: Vec<String>,
    /// Always [`ObservationConfidence::Unknown`]: do not refine as proven.
    #[serde(default = "unknown_confidence")]
    pub confidence: ObservationConfidence,
}

/// Conflict among gradient facts for one parameter identity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GradientConflict {
    pub identity: ParamIdentity,
    /// Distinct observed states, sorted.
    pub states: Vec<String>,
    /// Event ids that participated, sorted.
    pub event_ids: Vec<String>,
    /// Always [`ObservationConfidence::Unknown`]: do not pick a winning state.
    #[serde(default = "unknown_confidence")]
    pub confidence: ObservationConfidence,
}

/// Compact audit summary over a normalized trace.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RuntimeAudit {
    pub missing_gradients: Vec<ParamIdentity>,
    pub non_finite_gradients: Vec<ParamIdentity>,
    pub zero_gradients: Vec<ParamIdentity>,
    #[serde(default)]
    pub tensor_conflicts: Vec<TensorConflict>,
    #[serde(default)]
    pub gradient_conflicts: Vec<GradientConflict>,
    #[serde(default)]
    pub identity_mismatches: Vec<IdentityMismatch>,
    /// First step at which any parameter gradient was `non_finite`, when steps are present.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub first_non_finite_step: Option<u64>,
    /// Parameter identities whose observed forward `abs_max` crossed a saturation threshold.
    #[serde(default)]
    pub saturating_activations: Vec<ParamIdentity>,
}

impl RuntimeAudit {
    pub fn is_clean(&self) -> bool {
        self.missing_gradients.is_empty()
            && self.non_finite_gradients.is_empty()
            && self.zero_gradients.is_empty()
            && self.tensor_conflicts.is_empty()
            && self.gradient_conflicts.is_empty()
            && self.identity_mismatches.is_empty()
    }

    /// True when any conflict or identity mismatch requires Unknown (not Proven) treatment.
    pub fn has_unknown_evidence(&self) -> bool {
        !self.tensor_conflicts.is_empty()
            || !self.gradient_conflicts.is_empty()
            || !self.identity_mismatches.is_empty()
    }
}

impl RuntimeTrace {
    /// Sort collections deterministically for stable diffs and audits.
    pub fn normalize(&mut self) {
        self.run.cargo_features.sort();
        self.run.cargo_features.dedup();
        self.run.cfg.sort();
        self.run.cfg.dedup();

        self.tensors.sort_by(|a, b| {
            a.event_id
                .cmp(&b.event_id)
                .then_with(|| a.static_id.cmp(&b.static_id))
                .then_with(|| a.source.cmp(&b.source))
        });
        self.operations.sort_by(|a, b| {
            a.event_id
                .cmp(&b.event_id)
                .then_with(|| a.op.cmp(&b.op))
                .then_with(|| a.static_id.cmp(&b.static_id))
        });
        self.gradients.sort_by(|a, b| {
            a.root
                .cmp(&b.root)
                .then_with(|| a.key.cmp(&b.key))
                .then_with(|| a.step.cmp(&b.step))
                .then_with(|| a.event_id.cmp(&b.event_id))
        });
        self.values.sort_by(|a, b| {
            a.event_id
                .cmp(&b.event_id)
                .then_with(|| a.step.cmp(&b.step))
                .then_with(|| a.source.cmp(&b.source))
        });
        self.edge_timings.sort_by(|a, b| {
            a.from_static_id
                .cmp(&b.from_static_id)
                .then_with(|| a.to_static_id.cmp(&b.to_static_id))
                .then_with(|| a.step.cmp(&b.step))
                .then_with(|| a.event_id.cmp(&b.event_id))
        });
    }

    /// Reject duplicate event ids and invalid gradient facts.
    ///
    /// Contradictory tensor/gradient *observations* are not rejected here; they are reported by
    /// [`Self::audit`] as Unknown conflicts so importers never silently overwrite a winner.
    pub fn validate(&self) -> Result<()> {
        if self.schema != SCHEMA && self.schema != SCHEMA_V2 && self.schema != SCHEMA_V3 {
            bail!(
                "unsupported runtime schema {:?}; expected {:?}, {:?}, or {:?}",
                self.schema,
                SCHEMA,
                SCHEMA_V2,
                SCHEMA_V3
            );
        }

        let mut seen = HashSet::new();
        let mut push_id = |id: &str| -> Result<()> {
            if id.is_empty() {
                bail!("empty event_id is not allowed");
            }
            if !seen.insert(id.to_string()) {
                bail!("duplicate event_id `{id}`");
            }
            Ok(())
        };

        for t in &self.tensors {
            push_id(&t.event_id)?;
        }
        for op in &self.operations {
            push_id(&op.event_id)?;
        }
        for g in &self.gradients {
            push_id(&g.event_id)?;
            g.validate()?;
        }
        for v in &self.values {
            push_id(&v.event_id)?;
        }
        for edge in &self.edge_timings {
            push_id(&edge.event_id)?;
        }
        Ok(())
    }

    /// Normalize then validate; returns a ready-to-query trace.
    pub fn finalize(mut self) -> Result<Self> {
        self.normalize();
        self.validate()?;
        Ok(self)
    }

    /// Compare optional trace identity fields against an importer-supplied expectation.
    ///
    /// Fields omitted on either side are skipped (backward compatible). When both sides supply a
    /// field and they differ, a mismatch is returned — callers must reject or report it as Unknown.
    pub fn check_identity(&self, expected: &ExpectedIdentity) -> Vec<IdentityMismatch> {
        let mut out = Vec::new();
        if let (Some(expected_id), Some(observed)) = (
            expected.analysis_id.as_deref(),
            self.run.analysis_id.as_deref(),
        ) {
            if expected_id != observed {
                out.push(IdentityMismatch {
                    field: IdentityField::AnalysisId,
                    expected: expected_id.to_string(),
                    observed: observed.to_string(),
                });
            }
        }
        if let (Some(expected_id), Some(observed)) =
            (expected.build_id.as_deref(), self.run.build_id.as_deref())
        {
            if expected_id != observed {
                out.push(IdentityMismatch {
                    field: IdentityField::BuildId,
                    expected: expected_id.to_string(),
                    observed: observed.to_string(),
                });
            }
        }
        out
    }

    /// Reject when supplied identity disagrees with `expected`.
    ///
    /// Compatible when either side omits a field.
    pub fn require_identity(&self, expected: &ExpectedIdentity) -> Result<()> {
        let mismatches = self.check_identity(expected);
        if let Some(first) = mismatches.first() {
            bail!(
                "runtime {} mismatch: expected {:?}, observed {:?}",
                first.field,
                first.expected,
                first.observed
            );
        }
        Ok(())
    }

    /// All tensor observations carrying `static_id`.
    pub fn tensors_by_static_id(&self, static_id: &str) -> Vec<&TensorObservation> {
        self.tensors
            .iter()
            .filter(|t| t.static_id.as_deref() == Some(static_id))
            .collect()
    }

    /// Agreed tensor observation for `static_id`, if any.
    ///
    /// Returns `None` when there are no observations or when shape/dtype/device conflict — never
    /// silently picks a contradictory winner.
    pub fn agreed_tensor(&self, static_id: &str) -> Option<&TensorObservation> {
        let obs = self.tensors_by_static_id(static_id);
        if obs.is_empty() {
            return None;
        }
        let first = obs[0];
        for other in &obs[1..] {
            if other.shape != first.shape
                || other.dtype != first.dtype
                || other.device != first.device
                || other.contiguous != first.contiguous
                || other.requires_grad != first.requires_grad
            {
                return None;
            }
        }
        Some(first)
    }

    /// Confidence for refining a static tensor from this trace.
    pub fn tensor_confidence(&self, static_id: &str) -> ObservationConfidence {
        match self.agreed_tensor(static_id) {
            Some(_) => ObservationConfidence::Proven,
            None => ObservationConfidence::Unknown,
        }
    }

    /// Gradient facts for a builder-root + parameter-key identity.
    pub fn gradients_for(&self, root: &str, key: &str) -> Vec<&GradientFact> {
        self.gradients
            .iter()
            .filter(|g| g.root == root && g.key == key)
            .collect()
    }

    /// Agreed gradient fact for the identity, if any.
    ///
    /// Returns `None` when facts are missing or contradictory (state/norm disagree). Does not
    /// silently overwrite by returning the first observation.
    pub fn gradient(&self, root: &str, key: &str) -> Option<&GradientFact> {
        let facts = self.gradients_for(root, key);
        agreed_gradient(&facts)
    }

    /// Confidence for refining a parameter gradient from this trace.
    pub fn gradient_confidence(&self, root: &str, key: &str) -> ObservationConfidence {
        match self.gradient(root, key) {
            Some(_) => ObservationConfidence::Proven,
            None => ObservationConfidence::Unknown,
        }
    }

    /// Compact audit: gradient quality, tensor/gradient conflicts, optional identity mismatches.
    pub fn audit(&self) -> RuntimeAudit {
        self.audit_with_identity(None)
    }

    /// Like [`Self::audit`], also reporting identity mismatches when `expected` is supplied.
    pub fn audit_with_identity(&self, expected: Option<&ExpectedIdentity>) -> RuntimeAudit {
        let gradient_conflicts = self.collect_gradient_conflicts();
        let conflicted: HashSet<ParamIdentity> = gradient_conflicts
            .iter()
            .map(|c| c.identity.clone())
            .collect();

        let mut missing = BTreeSet::new();
        let mut non_finite = BTreeSet::new();
        let mut zero = BTreeSet::new();

        // Only classify agreed (non-conflicted) gradient identities.
        let mut by_param: BTreeMap<ParamIdentity, Vec<&GradientFact>> = BTreeMap::new();
        for g in &self.gradients {
            by_param.entry(g.identity()).or_default().push(g);
        }
        for (id, facts) in by_param {
            if conflicted.contains(&id) {
                continue;
            }
            let Some(g) = latest_agreed_gradient(&facts).or_else(|| agreed_gradient(&facts)) else {
                continue;
            };
            match g.state {
                GradientState::Missing => {
                    missing.insert(id);
                }
                GradientState::NonFinite => {
                    non_finite.insert(id);
                }
                GradientState::Zero => {
                    zero.insert(id);
                }
                GradientState::Present => {}
            }
        }

        let mut by_static: BTreeMap<&str, Vec<&TensorObservation>> = BTreeMap::new();
        for t in &self.tensors {
            if let Some(sid) = t.static_id.as_deref() {
                by_static.entry(sid).or_default().push(t);
            }
        }

        let mut conflicts = Vec::new();
        for (static_id, obs) in by_static {
            if obs.len() < 2 {
                continue;
            }
            push_conflict(
                &mut conflicts,
                static_id,
                TensorConflictKind::Dtype,
                |t| t.dtype.clone(),
                &obs,
            );
            push_conflict(
                &mut conflicts,
                static_id,
                TensorConflictKind::Device,
                |t| t.device.clone(),
                &obs,
            );
            push_conflict(
                &mut conflicts,
                static_id,
                TensorConflictKind::Shape,
                |t| format_shape(&t.shape),
                &obs,
            );
            push_conflict(
                &mut conflicts,
                static_id,
                TensorConflictKind::Layout,
                |t| t.contiguous.to_string(),
                &obs,
            );
            push_conflict(
                &mut conflicts,
                static_id,
                TensorConflictKind::RequiresGrad,
                |t| t.requires_grad.to_string(),
                &obs,
            );
        }
        conflicts.sort_by(|a, b| {
            a.static_id
                .cmp(&b.static_id)
                .then_with(|| a.kind.cmp(&b.kind))
        });

        let identity_mismatches = expected
            .map(|expected| self.check_identity(expected))
            .unwrap_or_default();

        let first_non_finite_step = self
            .gradients
            .iter()
            .filter(|g| matches!(g.state, GradientState::NonFinite))
            .filter_map(|g| g.step)
            .min();

        let mut saturating = BTreeSet::new();
        for value in &self.values {
            if value.saturated_count == 0 {
                continue;
            }
            if let Some(source) = &value.source {
                saturating.insert(ParamIdentity {
                    root: "value".into(),
                    key: source.clone(),
                });
            }
        }

        RuntimeAudit {
            missing_gradients: missing.into_iter().collect(),
            non_finite_gradients: non_finite.into_iter().collect(),
            zero_gradients: zero.into_iter().collect(),
            tensor_conflicts: conflicts,
            gradient_conflicts,
            identity_mismatches,
            first_non_finite_step,
            saturating_activations: saturating.into_iter().collect(),
        }
    }

    fn collect_gradient_conflicts(&self) -> Vec<GradientConflict> {
        let mut by_param: BTreeMap<ParamIdentity, Vec<&GradientFact>> = BTreeMap::new();
        for g in &self.gradients {
            by_param.entry(g.identity()).or_default().push(g);
        }
        let mut out = Vec::new();
        for (identity, facts) in by_param {
            if facts.len() < 2 {
                continue;
            }
            // Distinct steps with one observation each are a time series, not a conflict.
            if facts.iter().all(|g| g.step.is_some()) {
                let steps: BTreeSet<_> = facts.iter().filter_map(|g| g.step).collect();
                if steps.len() == facts.len() {
                    continue;
                }
            }
            if latest_agreed_gradient(&facts).is_some() || agreed_gradient(&facts).is_some() {
                continue;
            }
            let mut states: BTreeSet<String> = BTreeSet::new();
            let mut event_ids: BTreeSet<String> = BTreeSet::new();
            for g in &facts {
                states.insert(g.state.to_string());
                event_ids.insert(g.event_id.clone());
            }
            // Distinct norms with same state also conflict (e.g. two present norms).
            let mut norms: BTreeSet<String> = BTreeSet::new();
            for g in &facts {
                norms.insert(match g.norm {
                    Some(n) => format!("{n}"),
                    None => "none".to_string(),
                });
            }
            if states.len() <= 1 && norms.len() <= 1 {
                continue;
            }
            out.push(GradientConflict {
                identity,
                states: states.into_iter().collect(),
                event_ids: event_ids.into_iter().collect(),
                confidence: ObservationConfidence::Unknown,
            });
        }
        out.sort_by(|a, b| a.identity.cmp(&b.identity));
        out
    }
}

/// Returns the sole agreed fact when all entries share state and norm; otherwise `None`.
fn agreed_gradient<'a>(facts: &[&'a GradientFact]) -> Option<&'a GradientFact> {
    let first = facts.first().copied()?;
    for other in facts.iter().skip(1) {
        if other.state != first.state {
            return None;
        }
        match (first.norm, other.norm) {
            (None, None) => {}
            (Some(a), Some(b)) if float_eq(a, b) => {}
            (None, Some(_)) | (Some(_), None) => return None,
            (Some(_), Some(_)) => return None,
        }
    }
    Some(first)
}

/// When every fact carries a step, return the latest-step observation (time-series rollup).
fn latest_agreed_gradient<'a>(facts: &[&'a GradientFact]) -> Option<&'a GradientFact> {
    if facts.is_empty() || !facts.iter().all(|g| g.step.is_some()) {
        return None;
    }
    facts.iter().copied().max_by_key(|g| g.step.unwrap_or(0))
}

fn float_eq(a: f64, b: f64) -> bool {
    if a.is_nan() && b.is_nan() {
        return true;
    }
    a == b
}

fn format_shape(shape: &[usize]) -> String {
    format!(
        "[{}]",
        shape
            .iter()
            .map(|d| d.to_string())
            .collect::<Vec<_>>()
            .join(", ")
    )
}

fn push_conflict(
    out: &mut Vec<TensorConflict>,
    static_id: &str,
    kind: TensorConflictKind,
    project: impl Fn(&TensorObservation) -> String,
    obs: &[&TensorObservation],
) {
    let mut values: BTreeSet<String> = BTreeSet::new();
    for t in obs {
        values.insert(project(t));
    }
    if values.len() > 1 {
        out.push(TensorConflict {
            static_id: static_id.to_string(),
            kind,
            values: values.into_iter().collect(),
            confidence: ObservationConfidence::Unknown,
        });
    }
}

/// Parse a single JSON document into a validated, normalized trace.
pub fn parse_json(input: &str) -> Result<RuntimeTrace> {
    let trace: RuntimeTrace =
        serde_json::from_str(input).context("failed to parse runtime JSON document")?;
    trace.finalize()
}

/// Parse JSONL event records into a validated, normalized trace.
pub fn parse_jsonl(input: &str) -> Result<RuntimeTrace> {
    let mut schema: Option<String> = None;
    let mut run: Option<RunMetadata> = None;
    let mut tensors = Vec::new();
    let mut operations = Vec::new();
    let mut gradients = Vec::new();
    let mut values = Vec::new();
    let mut edge_timings = Vec::new();

    for (line_no, line) in input.lines().enumerate() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let event: RuntimeEvent = serde_json::from_str(line)
            .with_context(|| format!("failed to parse runtime JSONL line {}", line_no + 1))?;
        match event {
            RuntimeEvent::Meta {
                schema: s,
                run: meta,
            } => {
                if schema.is_some() || run.is_some() {
                    bail!(
                        "duplicate meta event on JSONL line {}; only one meta record is allowed",
                        line_no + 1
                    );
                }
                schema = Some(s);
                run = Some(meta);
            }
            RuntimeEvent::Tensor(t) => tensors.push(t),
            RuntimeEvent::Operation(op) => operations.push(op),
            RuntimeEvent::Gradient(g) => gradients.push(g),
            RuntimeEvent::Value(v) => values.push(v),
            RuntimeEvent::EdgeTiming(edge) => edge_timings.push(edge),
        }
    }

    let schema = schema.unwrap_or_else(|| SCHEMA.to_string());
    let run = run.context("JSONL stream is missing a meta event with run metadata")?;

    RuntimeTrace {
        schema,
        run,
        tensors,
        operations,
        gradients,
        values,
        edge_timings,
    }
    .finalize()
}

/// Auto-detect a single JSON object versus JSONL event records.
pub fn parse(input: &str) -> Result<RuntimeTrace> {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        bail!("empty runtime evidence input");
    }
    // A document starts with `{` and is a single JSON value; JSONL is line-oriented events.
    if trimmed.starts_with('{') && !trimmed.contains('\n') {
        return parse_json(trimmed);
    }
    if trimmed.starts_with('{') {
        // Multi-line JSON document vs JSONL: try document first when the whole buffer is one value.
        if let Ok(value) = serde_json::from_str::<serde_json::Value>(trimmed) {
            if value.is_object() && value.get("schema").is_some() && value.get("run").is_some() {
                let trace: RuntimeTrace = serde_json::from_value(value)
                    .context("failed to parse runtime JSON document")?;
                return trace.finalize();
            }
        }
    }
    parse_jsonl(input)
}