lean-ctx-protocol 0.4.0

Open wire contracts for lean-ctx: events, observations, assignments, evidence gaps.
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
//! Canonical signed local evidence receipt.
//!
//! `ReceiptDocumentV1` is the immutable, digest-addressed record for one
//! Task -> Plan -> Invocation -> Receipt -> Outcome lineage.  It deliberately
//! contains metadata and references only: prompts, completions, source files,
//! credentials, headers, and other private payloads belong in separately
//! persisted evidence artifacts.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{self, Write as _};

use serde::de::{MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};

use crate::{
    AcceptanceState, CapabilityId, OutcomeId, PlanId, ProtocolReference, SemanticVersion,
    Sha256Digest, SignatureStatus, TaskId, UtcTimestamp, ValidationError,
    deserialize_schema_version, validate_bounded_opaque_identifier, validate_schema_version,
};

/// Maximum array length in the receipt wire contract.
pub const MAX_RECEIPT_ITEMS: usize = 64;

/// Maximum integer that round-trips exactly through every v1 JSON consumer.
pub const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;

const MAX_ITEMS: usize = MAX_RECEIPT_ITEMS;

/// Terminal state of the recorded execution.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReceiptTerminalStatusV1 {
    Succeeded,
    Failed,
    Rejected,
    Cancelled,
    TimedOut,
}

/// Provenance class of one integer observation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReceiptValueClassificationV1 {
    Measured,
    Estimated,
    Calculated,
    Reconciled,
    Unavailable,
}

/// Kind of payload referenced by a receipt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReceiptEvidenceKindV1 {
    Measurement,
    Assumption,
    Formula,
    PriceTable,
    Invoice,
    Outcome,
    Runtime,
    Methodology,
}

/// Strict digest-only evidence reference; payload bytes are never embedded.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptEvidenceRefV1 {
    pub kind: ReceiptEvidenceKindV1,
    pub uri: ProtocolReference,
    pub digest: Sha256Digest,
    pub media_type: String,
    pub signature_status: SignatureStatus,
}

/// One named integer fact with enough provenance to interpret its claim class.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptValueV1 {
    pub name: String,
    pub unit: String,
    pub classification: ReceiptValueClassificationV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<u64>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub evidence_digests: Vec<Sha256Digest>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub formula_digest: Option<Sha256Digest>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub price_table_digest: Option<Sha256Digest>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reconciliation_digest: Option<Sha256Digest>,
}

impl ReceiptValueV1 {
    fn validate(
        &self,
        evidence: &BTreeMap<&str, ReceiptEvidenceKindV1>,
    ) -> Result<(), ValidationError> {
        validate_name(&self.name, "ReceiptValueV1 name")?;
        validate_name(&self.unit, "ReceiptValueV1 unit")?;
        validate_unique_digests(&self.evidence_digests, "ReceiptValueV1 evidence_digests")?;
        if self
            .evidence_digests
            .iter()
            .any(|digest| !evidence.contains_key(digest.as_str()))
        {
            return Err(ValidationError::new(
                "ReceiptValueV1 references evidence absent from the receipt",
            ));
        }

        let direct = self
            .evidence_digests
            .iter()
            .map(|digest| digest.as_str())
            .collect::<BTreeSet<_>>();
        let mut derived = BTreeSet::new();
        for (field, digest) in [
            ("formula_digest", self.formula_digest.as_ref()),
            ("price_table_digest", self.price_table_digest.as_ref()),
            ("reconciliation_digest", self.reconciliation_digest.as_ref()),
        ] {
            if let Some(digest) = digest {
                if direct.contains(digest.as_str()) || !derived.insert(digest.as_str()) {
                    return Err(ValidationError::new(format!(
                        "ReceiptValueV1 {field} must occur exactly once"
                    )));
                }
                if !evidence.contains_key(digest.as_str()) {
                    return Err(ValidationError::new(format!(
                        "ReceiptValueV1 {field} references evidence absent from the receipt"
                    )));
                }
            }
        }
        if self.value.is_some_and(|value| value > MAX_SAFE_INTEGER) {
            return Err(ValidationError::new(
                "ReceiptValueV1 value exceeds the cross-language safe integer ceiling",
            ));
        }

        let has_value = self.value.is_some();
        let has_sources = !self.evidence_digests.is_empty();
        match self.classification {
            ReceiptValueClassificationV1::Unavailable => {
                if has_value
                    || has_sources
                    || self.formula_digest.is_some()
                    || self.price_table_digest.is_some()
                    || self.reconciliation_digest.is_some()
                {
                    return Err(ValidationError::new(
                        "unavailable values must omit value and provenance",
                    ));
                }
            }
            ReceiptValueClassificationV1::Measured | ReceiptValueClassificationV1::Estimated => {
                if !has_value || !has_sources {
                    return Err(ValidationError::new(
                        "measured and estimated values require a value and evidence",
                    ));
                }
                let expected_kind = match self.classification {
                    ReceiptValueClassificationV1::Measured => ReceiptEvidenceKindV1::Measurement,
                    ReceiptValueClassificationV1::Estimated => ReceiptEvidenceKindV1::Assumption,
                    _ => unreachable!("classification branch is exhaustive"),
                };
                if !self
                    .evidence_digests
                    .iter()
                    .any(|digest| evidence.get(digest.as_str()) == Some(&expected_kind))
                {
                    return Err(ValidationError::new(
                        "measured and estimated values require matching evidence kind",
                    ));
                }
                if self.formula_digest.is_some()
                    || self.price_table_digest.is_some()
                    || self.reconciliation_digest.is_some()
                {
                    return Err(ValidationError::new(
                        "measured and estimated values cannot claim calculation or reconciliation",
                    ));
                }
            }
            ReceiptValueClassificationV1::Calculated => {
                if !has_value
                    || !has_sources
                    || self.formula_digest.is_none()
                    || self.price_table_digest.is_none()
                    || self.reconciliation_digest.is_some()
                {
                    return Err(ValidationError::new(
                        "calculated values require value, evidence, formula and price-table digests",
                    ));
                }
                if !self.evidence_digests.iter().all(|digest| {
                    matches!(
                        evidence.get(digest.as_str()),
                        Some(
                            ReceiptEvidenceKindV1::Measurement | ReceiptEvidenceKindV1::Assumption
                        )
                    )
                }) || !matches!(
                    self.formula_digest
                        .as_ref()
                        .and_then(|digest| evidence.get(digest.as_str())),
                    Some(ReceiptEvidenceKindV1::Formula)
                ) || !matches!(
                    self.price_table_digest
                        .as_ref()
                        .and_then(|digest| evidence.get(digest.as_str())),
                    Some(ReceiptEvidenceKindV1::PriceTable)
                ) {
                    return Err(ValidationError::new(
                        "calculated values require measurement/assumption, formula and price-table evidence",
                    ));
                }
            }
            ReceiptValueClassificationV1::Reconciled => {
                let Some(reconciliation) = self.reconciliation_digest.as_ref() else {
                    return Err(ValidationError::new(
                        "reconciled values require invoice evidence",
                    ));
                };
                if !has_value
                    || !has_sources
                    || self.formula_digest.is_none()
                    || self.price_table_digest.is_none()
                {
                    return Err(ValidationError::new(
                        "reconciled values require calculated provenance and listed invoice evidence",
                    ));
                }
                if !self.evidence_digests.iter().all(|digest| {
                    matches!(
                        evidence.get(digest.as_str()),
                        Some(
                            ReceiptEvidenceKindV1::Measurement | ReceiptEvidenceKindV1::Assumption
                        )
                    )
                }) || !matches!(
                    self.formula_digest
                        .as_ref()
                        .and_then(|digest| evidence.get(digest.as_str())),
                    Some(ReceiptEvidenceKindV1::Formula)
                ) || !matches!(
                    self.price_table_digest
                        .as_ref()
                        .and_then(|digest| evidence.get(digest.as_str())),
                    Some(ReceiptEvidenceKindV1::PriceTable)
                ) || evidence.get(reconciliation.as_str())
                    != Some(&ReceiptEvidenceKindV1::Invoice)
                {
                    return Err(ValidationError::new(
                        "reconciled values require invoice, formula and price-table evidence",
                    ));
                }
            }
        }
        Ok(())
    }
}

/// Version-pinned capability that participated in the invocation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptCapabilityLinkV1 {
    pub capability_id: CapabilityId,
    pub capability_version: SemanticVersion,
    pub invocation_ref: Sha256Digest,
}

/// Immutable identity map for the execution lineage.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptLineageV1 {
    pub task_id: TaskId,
    pub task_ref: Sha256Digest,
    pub plan_id: PlanId,
    pub plan_ref: Sha256Digest,
    pub invocation_id: String,
    pub invocation_ref: Sha256Digest,
    pub identity_ref: Sha256Digest,
    pub policy_refs: Vec<Sha256Digest>,
    pub capabilities: Vec<ReceiptCapabilityLinkV1>,
}

impl ReceiptLineageV1 {
    fn validate(&self) -> Result<(), ValidationError> {
        validate_bounded_opaque_identifier(&self.invocation_id, "ReceiptLineageV1 invocation_id")?;
        validate_unique_digests(&self.policy_refs, "ReceiptLineageV1 policy_refs")?;
        if self.policy_refs.is_empty() {
            return Err(ValidationError::new(
                "ReceiptLineageV1 requires at least one policy reference",
            ));
        }
        if self.capabilities.is_empty() || self.capabilities.len() > MAX_ITEMS {
            return Err(ValidationError::new(
                "ReceiptLineageV1 capabilities must contain 1..=64 entries",
            ));
        }
        let mut unique = BTreeSet::new();
        for capability in &self.capabilities {
            if capability.invocation_ref != self.invocation_ref {
                return Err(ValidationError::new(
                    "ReceiptLineageV1 capability invocation_ref must bind the invocation",
                ));
            }
            if !unique.insert((
                capability.capability_id.as_str(),
                capability.capability_version.as_str(),
            )) {
                return Err(ValidationError::new(
                    "ReceiptLineageV1 capabilities must be unique",
                ));
            }
        }
        Ok(())
    }
}

/// Link to the separately persisted outcome observation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptOutcomeLinkV1 {
    pub state: AcceptanceState,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub outcome_id: Option<OutcomeId>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub outcome_ref: Option<Sha256Digest>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub acceptance_evidence_digest: Option<Sha256Digest>,
}

impl ReceiptOutcomeLinkV1 {
    fn validate(
        &self,
        evidence: &BTreeMap<&str, ReceiptEvidenceKindV1>,
    ) -> Result<(), ValidationError> {
        match self.state {
            AcceptanceState::Unknown => {
                if self.outcome_id.is_some()
                    || self.outcome_ref.is_some()
                    || self.acceptance_evidence_digest.is_some()
                {
                    return Err(ValidationError::new(
                        "unknown outcome must not claim an outcome or acceptance observation",
                    ));
                }
            }
            AcceptanceState::Rejected => {
                let Some(outcome_ref) = self.outcome_ref.as_ref() else {
                    return Err(ValidationError::new(
                        "rejected outcome requires outcome_id and outcome_ref",
                    ));
                };
                if self.outcome_id.is_none()
                    || evidence.get(outcome_ref.as_str()) != Some(&ReceiptEvidenceKindV1::Outcome)
                    || self.acceptance_evidence_digest.is_some()
                {
                    return Err(ValidationError::new(
                        "rejected outcome requires listed outcome evidence and no acceptance claim",
                    ));
                }
            }
            AcceptanceState::Accepted => {
                let Some(outcome_ref) = self.outcome_ref.as_ref() else {
                    return Err(ValidationError::new(
                        "accepted outcome requires outcome_id and outcome_ref",
                    ));
                };
                let Some(digest) = self.acceptance_evidence_digest.as_ref() else {
                    return Err(ValidationError::new(
                        "accepted outcome requires identified acceptance evidence",
                    ));
                };
                if self.outcome_id.is_none()
                    || evidence.get(outcome_ref.as_str()) != Some(&ReceiptEvidenceKindV1::Outcome)
                    || !evidence.contains_key(digest.as_str())
                    || outcome_ref == digest
                {
                    return Err(ValidationError::new(
                        "accepted outcome requires distinct listed outcome and acceptance evidence",
                    ));
                }
            }
        }
        Ok(())
    }
}

/// Chain predecessor covered by this receipt signature.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptChainLinkV1 {
    pub chain_id: String,
    pub sequence_number: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub previous_receipt_id: Option<Sha256Digest>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub previous_signature_digest: Option<Sha256Digest>,
}

/// Admission mode for signer keys.  No key bytes are embedded in a receipt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReceiptKeyAdmissionV1 {
    ExternalTrustStore,
}

/// Explicit signer identity without an embedded trust root.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptSignerV1 {
    pub algorithm: String,
    pub key_id: String,
    pub key_admission: ReceiptKeyAdmissionV1,
}

/// Canonical signed receipt document.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReceiptDocumentV1 {
    #[serde(deserialize_with = "deserialize_schema_version")]
    pub schema_version: u32,
    pub receipt_id: Sha256Digest,
    pub lineage: ReceiptLineageV1,
    pub chain: ReceiptChainLinkV1,
    pub status: ReceiptTerminalStatusV1,
    pub values: Vec<ReceiptValueV1>,
    pub outcome: ReceiptOutcomeLinkV1,
    pub evidence_refs: Vec<ReceiptEvidenceRefV1>,
    pub issued_at: UtcTimestamp,
    pub signer: ReceiptSignerV1,
    pub signature: String,
}

impl ReceiptDocumentV1 {
    /// Canonical bytes for the complete document, including signature.
    pub fn canonical_bytes(&self) -> Result<Vec<u8>, ValidationError> {
        canonical_without(self, &[])
    }

    /// Return canonical JSON as UTF-8 text without changing its bytes.
    pub fn canonical_json(&self) -> Result<String, ValidationError> {
        String::from_utf8(self.canonical_bytes()?)
            .map_err(|error| ValidationError::new(format!("canonical receipt UTF-8: {error}")))
    }

    /// Canonical bytes used to derive the receipt ID.
    pub fn identity_bytes(&self) -> Result<Vec<u8>, ValidationError> {
        canonical_without(self, &["receipt_id", "signature"])
    }

    /// Canonical bytes covered by the Ed25519 signature.
    pub fn signing_bytes(&self) -> Result<Vec<u8>, ValidationError> {
        canonical_without(self, &["signature"])
    }

    /// Alias used by verifiers when requesting signature coverage bytes.
    pub fn signature_bytes(&self) -> Result<Vec<u8>, ValidationError> {
        self.signing_bytes()
    }

    /// Derive the canonical SHA-256 receipt identity.
    pub fn derived_receipt_id(&self) -> Result<Sha256Digest, ValidationError> {
        digest_bytes(&self.identity_bytes()?)
    }

    /// Decode exactly canonical UTF-8 JSON and validate all protocol joins.
    pub fn from_canonical_bytes(bytes: &[u8]) -> Result<Self, ValidationError> {
        let value = strict_json_value(bytes)?;
        let document = serde_json::from_value::<Self>(value)
            .map_err(|error| ValidationError::new(format!("decode receipt document: {error}")))?;
        if document.canonical_bytes()? != bytes {
            return Err(ValidationError::new(
                "receipt document JSON is not canonical UTF-8, compactness or key order",
            ));
        }
        document.validate()?;
        Ok(document)
    }

    /// Compatibility alias for callers that name the wire input `from_bytes`.
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, ValidationError> {
        Self::from_canonical_bytes(bytes)
    }

    /// Compatibility alias for callers that name the wire input JSON.
    pub fn from_canonical_json(bytes: &[u8]) -> Result<Self, ValidationError> {
        Self::from_canonical_bytes(bytes)
    }

    /// Validate the full signed lineage and its content identity.
    pub fn validate(&self) -> Result<(), ValidationError> {
        validate_schema_version(self.schema_version)?;
        self.lineage.validate()?;
        validate_bounded_opaque_identifier(&self.chain.chain_id, "ReceiptChainLinkV1 chain_id")?;
        validate_key_id(&self.signer.key_id)?;
        if self.signer.algorithm != "ed25519" {
            return Err(ValidationError::new(
                "ReceiptSignerV1 algorithm must be ed25519",
            ));
        }
        if !valid_base64_signature(&self.signature) {
            return Err(ValidationError::new(
                "ReceiptDocumentV1 signature must be canonical base64 Ed25519 bytes",
            ));
        }
        if self.chain.sequence_number == 0 || self.chain.sequence_number > MAX_SAFE_INTEGER {
            return Err(ValidationError::new(
                "ReceiptChainLinkV1 sequence_number must be 1..=9007199254740991",
            ));
        }
        let predecessor_pair = (
            self.chain.previous_receipt_id.is_some(),
            self.chain.previous_signature_digest.is_some(),
        );
        if (self.chain.sequence_number == 1 && predecessor_pair != (false, false))
            || (self.chain.sequence_number > 1 && predecessor_pair != (true, true))
        {
            return Err(ValidationError::new(
                "ReceiptChainLinkV1 predecessor fields must be absent only at genesis",
            ));
        }
        if self.chain.previous_receipt_id.as_ref() == Some(&self.receipt_id) {
            return Err(ValidationError::new(
                "ReceiptChainLinkV1 cannot reference the current receipt",
            ));
        }
        if self.evidence_refs.len() > MAX_ITEMS || self.values.len() > MAX_ITEMS {
            return Err(ValidationError::new(
                "ReceiptDocumentV1 collection limit exceeded",
            ));
        }

        let mut evidence = BTreeMap::new();
        for reference in &self.evidence_refs {
            validate_media_type(&reference.media_type)?;
            validate_safe_evidence_uri(reference.uri.as_str())?;
            if evidence
                .insert(reference.digest.as_str(), reference.kind)
                .is_some()
            {
                return Err(ValidationError::new(
                    "ReceiptDocumentV1 evidence digests must be unique",
                ));
            }
        }
        let mut value_names = BTreeSet::new();
        for value in &self.values {
            if !value_names.insert(value.name.as_str()) {
                return Err(ValidationError::new(
                    "ReceiptDocumentV1 value names must be unique",
                ));
            }
            value.validate(&evidence)?;
        }
        self.outcome.validate(&evidence)?;
        match (self.status, self.outcome.state) {
            (ReceiptTerminalStatusV1::Rejected, AcceptanceState::Rejected)
            | (ReceiptTerminalStatusV1::Succeeded, AcceptanceState::Accepted) => {}
            (ReceiptTerminalStatusV1::Rejected, _) => {
                return Err(ValidationError::new(
                    "rejected terminal status requires a rejected outcome",
                ));
            }
            (_, AcceptanceState::Unknown) => {}
            (_, AcceptanceState::Rejected) => {
                return Err(ValidationError::new(
                    "rejected outcome requires a rejected terminal status",
                ));
            }
            (_, AcceptanceState::Accepted) => {
                return Err(ValidationError::new(
                    "accepted outcome requires a succeeded terminal status",
                ));
            }
        }
        if self.derived_receipt_id()? != self.receipt_id {
            return Err(ValidationError::new(
                "ReceiptDocumentV1 receipt_id does not match canonical content",
            ));
        }
        Ok(())
    }
}

fn canonical_without<T: Serialize>(
    value: &T,
    omitted: &[&str],
) -> Result<Vec<u8>, ValidationError> {
    let mut value = serde_json::to_value(value)
        .map_err(|error| ValidationError::new(format!("serialize receipt document: {error}")))?;
    let object = value
        .as_object_mut()
        .ok_or_else(|| ValidationError::new("receipt document must serialize as an object"))?;
    for field in omitted {
        object.remove(*field);
    }
    serde_json::to_vec(&sort_json(value))
        .map_err(|error| ValidationError::new(format!("canonicalize receipt document: {error}")))
}

pub(crate) fn sort_json(value: Value) -> Value {
    match value {
        Value::Object(object) => Value::Object(
            object
                .into_iter()
                .map(|(key, value)| (key, sort_json(value)))
                .collect::<BTreeMap<_, _>>()
                .into_iter()
                .collect(),
        ),
        Value::Array(values) => Value::Array(values.into_iter().map(sort_json).collect()),
        scalar => scalar,
    }
}

/// A serde JSON visitor that rejects duplicate object keys at every depth.
struct StrictJsonValue(Value);

impl<'de> Deserialize<'de> for StrictJsonValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(StrictJsonVisitor)
    }
}

struct StrictJsonVisitor;

impl<'de> Visitor<'de> for StrictJsonVisitor {
    type Value = StrictJsonValue;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a JSON value with unique object keys")
    }

    fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(StrictJsonValue(Value::Bool(value)))
    }

    fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(StrictJsonValue(Value::Number(value.into())))
    }

    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(StrictJsonValue(Value::Number(value.into())))
    }

    fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        let _ = value;
        Err(E::custom(
            "floating-point JSON numbers are not canonical receipt values",
        ))
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(StrictJsonValue(Value::String(value.to_owned())))
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(StrictJsonValue(Value::String(value)))
    }

    fn visit_none<E>(self) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(StrictJsonValue(Value::Null))
    }

    fn visit_unit<E>(self) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        Ok(StrictJsonValue(Value::Null))
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut values = Vec::new();
        while let Some(value) = seq.next_element::<StrictJsonValue>()? {
            values.push(value.0);
        }
        Ok(StrictJsonValue(Value::Array(values)))
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let mut object = Map::new();
        while let Some(key) = map.next_key::<String>()? {
            if object.contains_key(&key) {
                return Err(serde::de::Error::custom(format!(
                    "duplicate JSON object key {key:?}"
                )));
            }
            let value = map.next_value::<StrictJsonValue>()?;
            object.insert(key, value.0);
        }
        Ok(StrictJsonValue(Value::Object(object)))
    }
}

fn strict_json_value(bytes: &[u8]) -> Result<Value, ValidationError> {
    let mut deserializer = serde_json::Deserializer::from_slice(bytes);
    let value = StrictJsonValue::deserialize(&mut deserializer)
        .map_err(|error| ValidationError::new(format!("decode canonical JSON: {error}")))?
        .0;
    deserializer
        .end()
        .map_err(|error| ValidationError::new(format!("trailing canonical JSON: {error}")))?;
    Ok(value)
}

pub(crate) fn digest_bytes(bytes: &[u8]) -> Result<Sha256Digest, ValidationError> {
    let mut value = String::with_capacity(71);
    value.push_str("sha256:");
    for byte in Sha256::digest(bytes) {
        write!(&mut value, "{byte:02x}")
            .map_err(|_| ValidationError::new("digest encoding failed"))?;
    }
    Sha256Digest::new(value)
}

fn validate_name(value: &str, field: &str) -> Result<(), ValidationError> {
    if value.is_empty()
        || value.len() > 64
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
    {
        return Err(ValidationError::new(format!(
            "{field} must be 1..=64 lowercase ASCII letters, digits or underscores"
        )));
    }
    Ok(())
}

fn validate_media_type(value: &str) -> Result<(), ValidationError> {
    let mut parts = value.split('/');
    let Some(major) = parts.next() else {
        return Err(ValidationError::new("media type must contain one slash"));
    };
    let Some(subtype) = parts.next() else {
        return Err(ValidationError::new("media type must contain one slash"));
    };
    if parts.next().is_some()
        || value.is_empty()
        || value.len() > 128
        || major.is_empty()
        || subtype.is_empty()
        || !major.bytes().all(is_media_token)
        || !subtype.bytes().all(is_media_token)
    {
        return Err(ValidationError::new(
            "ReceiptEvidenceRefV1 media_type must be a bounded RFC token type/subtype",
        ));
    }
    Ok(())
}

fn is_media_token(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || b"!#$&^_.+-".contains(&byte)
}

fn validate_safe_evidence_uri(value: &str) -> Result<(), ValidationError> {
    if value.is_empty()
        || value.len() > 1_024
        || !value.is_ascii()
        || value
            .bytes()
            .any(|byte| byte.is_ascii_whitespace() || byte.is_ascii_control())
        || value.contains(['?', '#', '@'])
    {
        return Err(ValidationError::new(
            "evidence URI must be an ASCII privacy-safe locator without query, fragment or userinfo",
        ));
    }
    let allowed = [
        "artifact://",
        "evidence://",
        "bundle://",
        "source://",
        "urn:",
    ];
    let Some(prefix) = allowed.iter().find(|prefix| value.starts_with(*prefix)) else {
        return Err(ValidationError::new(
            "evidence URI must use artifact, evidence, bundle, source or urn scheme",
        ));
    };
    if value.len() == prefix.len() || value[prefix.len()..].starts_with('/') {
        return Err(ValidationError::new(
            "evidence URI must include a non-path locator",
        ));
    }
    if value.contains("..") || value.starts_with("file:") {
        return Err(ValidationError::new(
            "evidence URI must not contain path traversal or local file paths",
        ));
    }
    Ok(())
}

fn validate_key_id(value: &str) -> Result<(), ValidationError> {
    if value.is_empty()
        || value.len() > 128
        || !value.is_ascii()
        || !value.as_bytes()[0].is_ascii_alphanumeric()
        || !value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || b"._:/-".contains(&byte))
        || value.starts_with("base64:")
        || value.starts_with("hex:")
    {
        return Err(ValidationError::new(
            "ReceiptSignerV1 key_id must be a bounded external key identifier, not key material",
        ));
    }
    Ok(())
}

fn validate_unique_digests(values: &[Sha256Digest], field: &str) -> Result<(), ValidationError> {
    if values.len() > MAX_ITEMS {
        return Err(ValidationError::new(format!("{field} exceeds 64 entries")));
    }
    let mut unique = BTreeSet::new();
    if values.iter().any(|value| !unique.insert(value.as_str())) {
        return Err(ValidationError::new(format!("{field} must be unique")));
    }
    Ok(())
}

fn valid_base64_signature(value: &str) -> bool {
    if value.len() != 88 || !value.ends_with("==") {
        return false;
    }
    let bytes = value.as_bytes();
    if !bytes[..86]
        .iter()
        .all(|byte| byte.is_ascii_alphanumeric() || *byte == b'+' || *byte == b'/')
    {
        return false;
    }
    // 64 bytes encode as 86 meaningful base64 characters plus `==`.  The
    // low four bits of the final sextet are padding and must be zero; without
    // this check multiple encodings would verify the same signature bytes.
    base64_value(bytes[85]).is_some_and(|last| last & 0x0f == 0)
}

fn base64_value(byte: u8) -> Option<u8> {
    match byte {
        b'A'..=b'Z' => Some(byte - b'A'),
        b'a'..=b'z' => Some(byte - b'a' + 26),
        b'0'..=b'9' => Some(byte - b'0' + 52),
        b'+' => Some(62),
        b'/' => Some(63),
        _ => None,
    }
}

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

    const A: &str = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    const B: &str = "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
    const C: &str = "sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
    const D: &str = "sha256:dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd";

    fn digest(value: &str) -> Sha256Digest {
        Sha256Digest::new(value).expect("valid digest")
    }

    fn evidence(
        kind: ReceiptEvidenceKindV1,
        digest_value: &str,
        uri: &str,
    ) -> ReceiptEvidenceRefV1 {
        ReceiptEvidenceRefV1 {
            kind,
            uri: ProtocolReference::new(uri).unwrap(),
            digest: digest(digest_value),
            media_type: "application/json".to_owned(),
            signature_status: SignatureStatus::Verified,
        }
    }

    fn document() -> ReceiptDocumentV1 {
        let mut document = ReceiptDocumentV1 {
            schema_version: 1,
            receipt_id: digest(B),
            lineage: ReceiptLineageV1 {
                task_id: TaskId::new("task-1").unwrap(),
                task_ref: digest(A),
                plan_id: PlanId::new("plan-1").unwrap(),
                plan_ref: digest(B),
                invocation_id: "invocation-1".to_owned(),
                invocation_ref: digest(C),
                identity_ref: digest(A),
                policy_refs: vec![digest(B)],
                capabilities: vec![ReceiptCapabilityLinkV1 {
                    capability_id: CapabilityId::new("capability://leanctx/context").unwrap(),
                    capability_version: SemanticVersion::new("1.0.0").unwrap(),
                    invocation_ref: digest(C),
                }],
            },
            chain: ReceiptChainLinkV1 {
                chain_id: "chain-1".to_owned(),
                sequence_number: 1,
                previous_receipt_id: None,
                previous_signature_digest: None,
            },
            status: ReceiptTerminalStatusV1::Succeeded,
            values: vec![ReceiptValueV1 {
                name: "input_tokens".to_owned(),
                unit: "token".to_owned(),
                classification: ReceiptValueClassificationV1::Measured,
                value: Some(42),
                evidence_digests: vec![digest(A)],
                formula_digest: None,
                price_table_digest: None,
                reconciliation_digest: None,
            }],
            outcome: ReceiptOutcomeLinkV1 {
                state: AcceptanceState::Unknown,
                outcome_id: None,
                outcome_ref: None,
                acceptance_evidence_digest: None,
            },
            evidence_refs: vec![evidence(
                ReceiptEvidenceKindV1::Measurement,
                A,
                "artifact://usage",
            )],
            issued_at: UtcTimestamp::new("2026-08-23T12:00:00Z").unwrap(),
            signer: ReceiptSignerV1 {
                algorithm: "ed25519".to_owned(),
                key_id: "test-key".to_owned(),
                key_admission: ReceiptKeyAdmissionV1::ExternalTrustStore,
            },
            signature: "A".repeat(86) + "==",
        };
        document.receipt_id = document.derived_receipt_id().unwrap();
        document
    }

    #[test]
    fn canonical_identity_and_signature_bytes_are_deterministic() {
        let document = document();
        document.validate().unwrap();
        assert_eq!(
            document.identity_bytes().unwrap(),
            document.identity_bytes().unwrap()
        );
        assert_ne!(
            document.identity_bytes().unwrap(),
            document.signing_bytes().unwrap()
        );
        assert_ne!(
            document.signing_bytes().unwrap(),
            document.canonical_bytes().unwrap()
        );
    }

    #[test]
    fn canonical_decoder_rejects_duplicates_order_whitespace_and_invalid_utf8() {
        let document = document();
        let canonical = document.canonical_bytes().unwrap();
        let mut duplicate = canonical.clone();
        duplicate.pop();
        duplicate.extend_from_slice(b",\"schema_version\":1}");
        assert!(ReceiptDocumentV1::from_canonical_bytes(&duplicate).is_err());

        let mut whitespace = Vec::from(b" \n".as_slice());
        whitespace.extend_from_slice(&canonical);
        assert!(ReceiptDocumentV1::from_canonical_bytes(&whitespace).is_err());
        assert!(ReceiptDocumentV1::from_canonical_bytes(&[0xff]).is_err());
        assert!(strict_json_value(br#"{"value":1.0}"#).is_err());
    }

    #[test]
    fn canonical_decoder_round_trips_golden_value() {
        let document = document();
        let bytes = document.canonical_bytes().unwrap();
        let decoded = ReceiptDocumentV1::from_canonical_bytes(&bytes).unwrap();
        assert_eq!(decoded, document);
        let golden =
            include_bytes!("../../../../docs/contracts/receipt-document/v1/valid-structure.json");
        let golden = golden.strip_suffix(b"\n").unwrap_or(golden);
        assert_eq!(
            ReceiptDocumentV1::from_canonical_bytes(golden).unwrap(),
            document
        );
        let identity = include_bytes!(
            "../../../../docs/contracts/receipt-document/v1/canonical-identity.json"
        );
        let identity = identity.strip_suffix(b"\n").unwrap_or(identity);
        assert_eq!(document.identity_bytes().unwrap(), identity);
        let signing =
            include_bytes!("../../../../docs/contracts/receipt-document/v1/canonical-signing.json");
        let signing = signing.strip_suffix(b"\n").unwrap_or(signing);
        assert_eq!(document.signing_bytes().unwrap(), signing);
        let invalid = include_bytes!(
            "../../../../docs/contracts/receipt-document/v1/invalid-unknown-field.json"
        );
        let invalid = invalid.strip_suffix(b"\n").unwrap_or(invalid);
        assert!(ReceiptDocumentV1::from_canonical_bytes(invalid).is_err());
        let unicode =
            include_bytes!("../../../../docs/contracts/receipt-document/v1/canonical-unicode.json");
        let unicode = unicode.strip_suffix(b"\n").unwrap_or(unicode);
        let unicode_document = ReceiptDocumentV1::from_canonical_bytes(unicode).unwrap();
        assert_eq!(unicode_document.lineage.task_id.as_str(), "tâsk-✓");
        assert_eq!(unicode_document.canonical_bytes().unwrap(), unicode);
        let escaped = include_bytes!(
            "../../../../docs/contracts/receipt-document/v1/invalid-unicode-escape.json"
        );
        let escaped = escaped.strip_suffix(b"\n").unwrap_or(escaped);
        assert!(ReceiptDocumentV1::from_canonical_bytes(escaped).is_err());
    }

    #[test]
    fn integer_ceiling_and_timestamp_are_cross_language_strict() {
        let mut document = document();
        document.values[0].value = Some(MAX_SAFE_INTEGER + 1);
        document.receipt_id = document.derived_receipt_id().unwrap();
        assert!(document.validate().is_err());
        assert!(UtcTimestamp::new("2026-08-23T12:00:00.000Z").is_err());
        assert!(UtcTimestamp::new("2026-08-23T14:00:00+02:00").is_err());
    }

    #[test]
    fn bounded_opaque_ids_enforce_utf8_byte_ceiling() {
        assert!(TaskId::new("é".repeat(128)).is_ok());
        assert!(TaskId::new("é".repeat(129)).is_err());
    }

    #[test]
    fn base64_and_key_id_are_strict_without_embedded_trust() {
        assert!(valid_base64_signature(&("A".repeat(86) + "==")));
        assert!(!valid_base64_signature(&("A".repeat(85) + "B==")));
        let mut alias = document();
        alias.signature = "A".repeat(85) + "B==";
        assert!(alias.validate().is_err());
        assert!(validate_media_type("/json").is_err());
        assert!(validate_media_type("application/json").is_ok());
        assert!(validate_safe_evidence_uri("artifact://").is_err());
        assert!(validate_safe_evidence_uri("artifact:///tmp/private").is_err());
        assert!(validate_key_id("key://runtime/ed25519").is_ok());
        assert!(validate_key_id("base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=").is_err());
        assert!(validate_key_id("bad key").is_err());
    }

    #[test]
    fn calculated_and_reconciled_values_require_typed_evidence() {
        let mut calculated = document();
        calculated.evidence_refs.extend([
            evidence(ReceiptEvidenceKindV1::Formula, B, "artifact://formula"),
            evidence(ReceiptEvidenceKindV1::PriceTable, C, "artifact://price"),
        ]);
        calculated.values[0].classification = ReceiptValueClassificationV1::Calculated;
        calculated.values[0].formula_digest = Some(digest(B));
        calculated.values[0].price_table_digest = Some(digest(C));
        calculated.receipt_id = calculated.derived_receipt_id().unwrap();
        calculated.validate().unwrap();
        calculated.values[0].price_table_digest = Some(digest(B));
        calculated.receipt_id = calculated.derived_receipt_id().unwrap();
        assert!(calculated.validate().is_err());
        calculated.values[0].price_table_digest = Some(digest(C));

        let mut reconciled = calculated;
        reconciled.evidence_refs.push(evidence(
            ReceiptEvidenceKindV1::Invoice,
            D,
            "artifact://invoice",
        ));
        reconciled.values[0].classification = ReceiptValueClassificationV1::Reconciled;
        reconciled.values[0].reconciliation_digest = Some(digest(D));
        reconciled.values[0].evidence_digests.push(digest(D));
        reconciled.receipt_id = reconciled.derived_receipt_id().unwrap();
        assert!(reconciled.validate().is_err());
        reconciled.values[0].evidence_digests.pop();
        reconciled.receipt_id = reconciled.derived_receipt_id().unwrap();
        reconciled.validate().unwrap();
    }

    #[test]
    fn rejected_outcome_requires_outcome_evidence_and_terminal_status() {
        let mut document = document();
        document.status = ReceiptTerminalStatusV1::Rejected;
        document.receipt_id = document.derived_receipt_id().unwrap();
        assert!(document.validate().is_err());

        document.evidence_refs.push(evidence(
            ReceiptEvidenceKindV1::Outcome,
            D,
            "artifact://outcome",
        ));
        document.outcome = ReceiptOutcomeLinkV1 {
            state: AcceptanceState::Rejected,
            outcome_id: Some(OutcomeId::new("outcome-1").unwrap()),
            outcome_ref: Some(digest(D)),
            acceptance_evidence_digest: None,
        };
        document.receipt_id = document.derived_receipt_id().unwrap();
        document.validate().unwrap();
        document.outcome.acceptance_evidence_digest = Some(digest(A));
        document.receipt_id = document.derived_receipt_id().unwrap();
        assert!(document.validate().is_err());
    }

    #[test]
    fn duplicate_provenance_is_rejected() {
        let mut document = document();
        document.evidence_refs.push(evidence(
            ReceiptEvidenceKindV1::Runtime,
            A,
            "artifact://runtime",
        ));
        document.receipt_id = document.derived_receipt_id().unwrap();
        assert!(document.validate().is_err());
    }

    #[test]
    fn strict_decoder_rejects_unknown_fields() {
        let mut value = serde_json::to_value(document()).unwrap();
        value["unknown"] = Value::Bool(true);
        assert!(serde_json::from_value::<ReceiptDocumentV1>(value).is_err());
    }
}