stateknot-core 0.1.0-alpha.1

Core domain types for the StateKnot agent runtime
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
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Protocol-neutral failures with explicit retry and reconciliation semantics.

use std::{borrow::Borrow, error::Error as StdError, fmt, str::FromStr, sync::Arc};

use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use serde_json::Value;
use thiserror::Error;

use crate::{
    BoundedJson, BoundedJsonError, DurationMillis, EventId, FailureId, JsonLimits, JsonLimitsError,
    SchemaReference,
};

const FAILURE_IDENTIFIER_PATTERN: &str = "^[a-z][a-z0-9_-]*(\\.[a-z][a-z0-9_-]*)*$";

/// Validation failure shared by [`FailureCode`] and [`FailureOrigin`].
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum FailureIdentifierError {
    /// The identifier contained no bytes.
    #[error("stable failure identifier must not be empty")]
    Empty,

    /// The identifier exceeded the 128-byte wire ceiling.
    #[error("stable failure identifier is {actual} bytes; maximum is {max}")]
    TooLong {
        /// Maximum accepted byte length.
        max: usize,
        /// Observed byte length.
        actual: usize,
    },

    /// The first byte, or the first byte after `.`, was not lowercase ASCII.
    #[error("failure identifier segment must start with lowercase ASCII at offset {index}")]
    InvalidSegmentStart {
        /// Zero-based byte offset of the invalid segment start.
        index: usize,
    },

    /// A non-separator byte did not belong to the stable ASCII grammar.
    #[error("stable failure identifier contains an invalid byte at offset {index}")]
    InvalidByte {
        /// Zero-based byte offset of the invalid byte.
        index: usize,
    },
}

fn validate_failure_identifier(value: &str) -> Result<(), FailureIdentifierError> {
    const MAX_LEN: usize = 128;

    if value.is_empty() {
        return Err(FailureIdentifierError::Empty);
    }
    if value.len() > MAX_LEN {
        return Err(FailureIdentifierError::TooLong {
            max: MAX_LEN,
            actual: value.len(),
        });
    }

    let mut at_segment_start = true;
    for (index, byte) in value.bytes().enumerate() {
        if at_segment_start {
            if !byte.is_ascii_lowercase() {
                return Err(FailureIdentifierError::InvalidSegmentStart { index });
            }
            at_segment_start = false;
        } else if byte == b'.' {
            at_segment_start = true;
        } else if !byte.is_ascii_lowercase()
            && !byte.is_ascii_digit()
            && !matches!(byte, b'_' | b'-')
        {
            return Err(FailureIdentifierError::InvalidByte { index });
        }
    }

    if at_segment_start {
        return Err(FailureIdentifierError::InvalidSegmentStart { index: value.len() });
    }

    Ok(())
}

macro_rules! define_failure_identifier {
    ($name:ident, $visitor:ident, $documentation:literal) => {
        #[doc = $documentation]
        #[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
        pub struct $name(Box<str>);

        impl $name {
            /// Maximum encoded length in bytes.
            pub const MAX_LEN: usize = 128;

            /// Validates and constructs the stable identifier.
            ///
            /// # Errors
            ///
            /// Returns [`FailureIdentifierError`] when the value is empty,
            /// oversized, or outside the lowercase segmented ASCII grammar.
            pub fn new(value: impl Into<String>) -> Result<Self, FailureIdentifierError> {
                let value = value.into();
                validate_failure_identifier(&value)?;
                Ok(Self(value.into_boxed_str()))
            }

            /// Returns the canonical identifier text.
            #[must_use]
            pub fn as_str(&self) -> &str {
                &self.0
            }
        }

        impl AsRef<str> for $name {
            fn as_ref(&self) -> &str {
                self.as_str()
            }
        }

        impl Borrow<str> for $name {
            fn borrow(&self) -> &str {
                self.as_str()
            }
        }

        impl fmt::Debug for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter
                    .debug_tuple(stringify!($name))
                    .field(&self.as_str())
                    .finish()
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str(self.as_str())
            }
        }

        impl FromStr for $name {
            type Err = FailureIdentifierError;

            fn from_str(value: &str) -> Result<Self, Self::Err> {
                Self::new(value)
            }
        }

        impl TryFrom<String> for $name {
            type Error = FailureIdentifierError;

            fn try_from(value: String) -> Result<Self, Self::Error> {
                Self::new(value)
            }
        }

        impl TryFrom<&str> for $name {
            type Error = FailureIdentifierError;

            fn try_from(value: &str) -> Result<Self, Self::Error> {
                Self::new(value)
            }
        }

        impl From<$name> for String {
            fn from(value: $name) -> Self {
                value.0.into()
            }
        }

        impl Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                serializer.serialize_str(self.as_str())
            }
        }

        impl<'de> Deserialize<'de> for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: Deserializer<'de>,
            {
                deserializer.deserialize_string($visitor)
            }
        }

        struct $visitor;

        impl de::Visitor<'_> for $visitor {
            type Value = $name;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("a lowercase segmented stable failure identifier")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                $name::try_from(value).map_err(E::custom)
            }

            fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                $name::try_from(value).map_err(E::custom)
            }
        }

        impl JsonSchema for $name {
            fn schema_name() -> std::borrow::Cow<'static, str> {
                stringify!($name).into()
            }

            fn schema_id() -> std::borrow::Cow<'static, str> {
                concat!(module_path!(), "::", stringify!($name)).into()
            }

            fn json_schema(_: &mut SchemaGenerator) -> Schema {
                json_schema!({
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 128,
                    "pattern": FAILURE_IDENTIFIER_PATTERN
                })
            }

            fn inline_schema() -> bool {
                true
            }
        }
    };
}

define_failure_identifier!(
    FailureCode,
    FailureCodeVisitor,
    "A stable, machine-readable failure code owned by an application or adapter."
);
define_failure_identifier!(
    FailureOrigin,
    FailureOriginVisitor,
    "A stable component or dependency namespace that originated a failure."
);

/// A bounded message explicitly approved for public error surfaces.
///
/// Construction validates shape, not confidentiality. Callers must supply a
/// message that contains no secrets, prompts, private resource existence,
/// stack traces, provider payloads, or internal implementation details.
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct FailureMessage(Box<str>);

impl FailureMessage {
    /// Maximum encoded UTF-8 length in bytes.
    pub const MAX_BYTES: usize = 1024;

    /// Validates and constructs a public-safe, single-line message.
    ///
    /// # Errors
    ///
    /// Returns [`FailureMessageError`] when the value is empty, oversized,
    /// whitespace-padded, or contains a control, bidirectional formatting
    /// control, line separator, or Unicode noncharacter.
    pub fn new(value: impl Into<String>) -> Result<Self, FailureMessageError> {
        let value = value.into();
        validate_failure_message(&value)?;
        Ok(Self(value.into_boxed_str()))
    }

    /// Returns the public-safe message text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Returns the encoded UTF-8 length without revealing message contents.
    #[must_use]
    pub fn len_bytes(&self) -> usize {
        self.0.len()
    }
}

impl AsRef<str> for FailureMessage {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for FailureMessage {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Debug for FailureMessage {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FailureMessage")
            .field("utf8_bytes", &self.len_bytes())
            .finish_non_exhaustive()
    }
}

impl fmt::Display for FailureMessage {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl FromStr for FailureMessage {
    type Err = FailureMessageError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

impl TryFrom<String> for FailureMessage {
    type Error = FailureMessageError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl TryFrom<&str> for FailureMessage {
    type Error = FailureMessageError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<FailureMessage> for String {
    fn from(value: FailureMessage) -> Self {
        value.0.into()
    }
}

impl Serialize for FailureMessage {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for FailureMessage {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_string(FailureMessageVisitor)
    }
}

struct FailureMessageVisitor;

impl de::Visitor<'_> for FailureMessageVisitor {
    type Value = FailureMessage;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a bounded public-safe single-line failure message")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        FailureMessage::try_from(value).map_err(E::custom)
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        FailureMessage::try_from(value).map_err(E::custom)
    }
}

impl JsonSchema for FailureMessage {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "FailureMessage".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::FailureMessage").into()
    }

    fn json_schema(_: &mut SchemaGenerator) -> Schema {
        json_schema!({
            "type": "string",
            "minLength": 1,
            "maxLength": 1024,
            "description": "A public-safe single-line message bounded to 1024 UTF-8 bytes. StateKnot additionally rejects leading/trailing whitespace, controls, bidirectional formatting controls, Unicode line separators, and Unicode noncharacters at runtime. Shape validation cannot prove that the caller omitted secrets."
        })
    }

    fn inline_schema() -> bool {
        true
    }
}

/// Invalid public failure message text.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum FailureMessageError {
    /// The message contained no bytes.
    #[error("failure message must not be empty")]
    Empty,

    /// The message exceeded [`FailureMessage::MAX_BYTES`].
    #[error("failure message is {actual} bytes; maximum is {max}")]
    TooLong {
        /// Maximum accepted UTF-8 byte length.
        max: usize,
        /// Observed UTF-8 byte length.
        actual: usize,
    },

    /// The message began or ended with Unicode whitespace.
    #[error("failure message must not contain leading or trailing whitespace")]
    SurroundingWhitespace,

    /// A control, bidi formatting control, line separator, or noncharacter was present.
    #[error("failure message contains a disallowed scalar at byte offset {index}")]
    DisallowedScalar {
        /// Zero-based UTF-8 byte offset of the rejected scalar.
        index: usize,
    },
}

fn validate_failure_message(value: &str) -> Result<(), FailureMessageError> {
    if value.is_empty() {
        return Err(FailureMessageError::Empty);
    }
    if value.len() > FailureMessage::MAX_BYTES {
        return Err(FailureMessageError::TooLong {
            max: FailureMessage::MAX_BYTES,
            actual: value.len(),
        });
    }
    if value.chars().next().is_some_and(char::is_whitespace)
        || value.chars().next_back().is_some_and(char::is_whitespace)
    {
        return Err(FailureMessageError::SurroundingWhitespace);
    }
    if let Some((index, _)) = value.char_indices().find(|(_, scalar)| {
        scalar.is_control()
            || is_bidi_formatting_control(*scalar)
            || matches!(scalar, '\u{2028}' | '\u{2029}')
            || is_unicode_noncharacter(*scalar)
    }) {
        return Err(FailureMessageError::DisallowedScalar { index });
    }
    Ok(())
}

const fn is_unicode_noncharacter(value: char) -> bool {
    let code_point = value as u32;
    (code_point >= 0xfdd0 && code_point <= 0xfdef) || code_point & 0xfffe == 0xfffe
}

const fn is_bidi_formatting_control(value: char) -> bool {
    matches!(
        value,
        '\u{061c}'
            | '\u{200e}'
            | '\u{200f}'
            | '\u{202a}'..='\u{202e}'
            | '\u{2066}'..='\u{2069}'
    )
}

/// Stable semantic category shared by component-specific errors.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum FailureCategory {
    /// A caller-supplied value failed validation.
    InvalidInput,
    /// Authentication was absent or invalid.
    Unauthenticated,
    /// The authenticated principal lacked a required permission.
    PermissionDenied,
    /// A policy explicitly denied the operation.
    PolicyDenied,
    /// A visible resource was not found.
    NotFound,
    /// Current state conflicted with the requested transition.
    Conflict,
    /// The requested operation or capability is unsupported.
    Unsupported,
    /// A rate or quota limit prevented execution.
    RateLimited,
    /// The operation exceeded its deadline.
    DeadlineExceeded,
    /// Cancellation was requested or observed.
    Cancelled,
    /// A required dependency was temporarily unavailable.
    DependencyUnavailable,
    /// Durable or transported data failed integrity validation.
    DataCorruption,
    /// An external side effect may have occurred but cannot yet be proven.
    AmbiguousExternalOutcome,
    /// An internal invariant or implementation failed.
    Internal,
}

/// Explicit recovery advice supplied by the originating component.
///
/// Retryability is never inferred from [`FailureCategory`]. A scheduler must
/// still intersect this advice with idempotency, deadline, budget, attempt,
/// circuit-breaker, and policy constraints.
#[derive(Clone, Copy, Debug, Eq, Hash, JsonSchema, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum RetryAdvice {
    /// Do not automatically retry this operation.
    Never,
    /// A new attempt is semantically safe no earlier than the stated delay.
    SafeAfter {
        /// Minimum delay before a scheduler may begin a new attempt.
        delay: DurationMillis,
    },
    /// Determine the external outcome before choosing retry or compensation.
    ReconcileFirst,
}

impl RetryAdvice {
    /// Returns the explicit minimum delay for a safe retry, if present.
    #[must_use]
    pub const fn safe_after_delay(self) -> Option<DurationMillis> {
        match self {
            Self::SafeAfter { delay } => Some(delay),
            Self::Never | Self::ReconcileFirst => None,
        }
    }

    /// Returns whether recovery must reconcile an external outcome first.
    #[must_use]
    pub const fn requires_reconciliation(self) -> bool {
        matches!(self, Self::ReconcileFirst)
    }
}

impl<'de> Deserialize<'de> for RetryAdvice {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(RetryAdviceVisitor)
    }
}

struct RetryAdviceVisitor;

impl<'de> de::Visitor<'de> for RetryAdviceVisitor {
    type Value = RetryAdvice;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a closed retry-advice object")
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: de::MapAccess<'de>,
    {
        let mut kind = None;
        let mut delay = None;

        while let Some(field) = map.next_key::<RetryAdviceField>()? {
            match field {
                RetryAdviceField::Kind => {
                    if kind.is_some() {
                        return Err(de::Error::duplicate_field("kind"));
                    }
                    kind = Some(map.next_value::<RetryAdviceKind>()?);
                }
                RetryAdviceField::Delay => {
                    if delay.is_some() {
                        return Err(de::Error::duplicate_field("delay"));
                    }
                    delay = Some(map.next_value::<DurationMillis>()?);
                }
            }
        }

        match (kind.ok_or_else(|| de::Error::missing_field("kind"))?, delay) {
            (RetryAdviceKind::Never, None) => Ok(RetryAdvice::Never),
            (RetryAdviceKind::SafeAfter, Some(delay)) => Ok(RetryAdvice::SafeAfter { delay }),
            (RetryAdviceKind::ReconcileFirst, None) => Ok(RetryAdvice::ReconcileFirst),
            (RetryAdviceKind::SafeAfter, None) => Err(de::Error::missing_field("delay")),
            (RetryAdviceKind::Never | RetryAdviceKind::ReconcileFirst, Some(_)) => {
                Err(de::Error::unknown_field("delay", &["kind"]))
            }
        }
    }
}

enum RetryAdviceField {
    Kind,
    Delay,
}

impl<'de> Deserialize<'de> for RetryAdviceField {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_identifier(RetryAdviceFieldVisitor)
    }
}

struct RetryAdviceFieldVisitor;

impl de::Visitor<'_> for RetryAdviceFieldVisitor {
    type Value = RetryAdviceField;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("`kind` or `delay`")
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        match value {
            "kind" => Ok(RetryAdviceField::Kind),
            "delay" => Ok(RetryAdviceField::Delay),
            _ => Err(E::unknown_field(value, &["kind", "delay"])),
        }
    }
}

#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum RetryAdviceKind {
    Never,
    SafeAfter,
    ReconcileFirst,
}

/// Schema-bound, resource-limited structured information safe to expose.
///
/// The schema reference gives adapters an explicit mapping contract. The value
/// is revalidated under limits tighter than general [`BoundedJson`] values.
/// Enclosing transports must also cap the raw error body because generic Serde
/// cannot account for insignificant whitespace outside this value.
#[derive(Clone, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct FailureDetails {
    schema: SchemaReference,
    value: BoundedJson,
}

impl FailureDetails {
    /// Maximum compact JSON size for public details.
    pub const MAX_JSON_BYTES: usize = 16 * 1024;
    /// Maximum object/array nesting depth for public details.
    pub const MAX_JSON_DEPTH: usize = 8;
    /// Maximum entries in any one public-details container.
    pub const MAX_JSON_CONTAINER_ENTRIES: usize = 64;
    /// Maximum total value nodes in public details.
    pub const MAX_JSON_NODES: usize = 512;
    /// Maximum decoded UTF-8 bytes in one public-details string.
    pub const MAX_JSON_STRING_BYTES: usize = 4 * 1024;
    /// Maximum decoded UTF-8 bytes in one public-details object key.
    pub const MAX_JSON_OBJECT_KEY_BYTES: usize = 128;

    /// Constructs schema-bound details after enforcing the tighter limits.
    ///
    /// # Errors
    ///
    /// Returns [`FailureDetailsError`] if the value exceeds any details limit
    /// or the library's static limit configuration is internally invalid.
    pub fn try_new(
        schema: SchemaReference,
        value: BoundedJson,
    ) -> Result<Self, FailureDetailsError> {
        let limits = failure_details_json_limits()?;
        let value = BoundedJson::try_from_value_with_limits(value.into_value(), limits)
            .map_err(FailureDetailsError::json)?;
        Ok(Self { schema, value })
    }

    /// Constructs schema-bound details from a trusted in-process JSON value.
    ///
    /// Wire input must first use an enclosing raw-body limit and ordinary
    /// deserialization so duplicate object member names remain detectable.
    ///
    /// # Errors
    ///
    /// Returns [`FailureDetailsError`] if the value exceeds any details limit
    /// or the library's static limit configuration is internally invalid.
    pub fn try_from_value(
        schema: SchemaReference,
        value: Value,
    ) -> Result<Self, FailureDetailsError> {
        let limits = failure_details_json_limits()?;
        let value = BoundedJson::try_from_value_with_limits(value, limits)
            .map_err(FailureDetailsError::json)?;
        Ok(Self { schema, value })
    }

    /// Returns the immutable schema identity for the details value.
    #[must_use]
    pub const fn schema(&self) -> &SchemaReference {
        &self.schema
    }

    /// Returns the validated structured details without permitting mutation.
    #[must_use]
    pub const fn value(&self) -> &BoundedJson {
        &self.value
    }

    /// Consumes the details and returns its schema and validated value.
    #[must_use]
    pub fn into_parts(self) -> (SchemaReference, BoundedJson) {
        (self.schema, self.value)
    }
}

impl fmt::Debug for FailureDetails {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FailureDetails")
            .field("schema", &self.schema)
            .field("value", &self.value)
            .finish_non_exhaustive()
    }
}

impl<'de> Deserialize<'de> for FailureDetails {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct Wire {
            schema: SchemaReference,
            value: BoundedJson,
        }

        let wire = Wire::deserialize(deserializer)?;
        Self::try_new(wire.schema, wire.value).map_err(de::Error::custom)
    }
}

fn failure_details_json_limits() -> Result<JsonLimits, FailureDetailsError> {
    JsonLimits::try_new(
        FailureDetails::MAX_JSON_BYTES,
        FailureDetails::MAX_JSON_DEPTH,
        FailureDetails::MAX_JSON_CONTAINER_ENTRIES,
        FailureDetails::MAX_JSON_NODES,
        FailureDetails::MAX_JSON_STRING_BYTES,
        FailureDetails::MAX_JSON_OBJECT_KEY_BYTES,
    )
    .map_err(FailureDetailsError::limits_configuration)
}

/// Invalid schema-bound public failure details.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum FailureDetailsError {
    /// The static details configuration violated the global JSON hard limits.
    #[error("failure details JSON limits are internally invalid: {source}")]
    LimitsConfiguration {
        /// Underlying invalid limit configuration.
        #[source]
        source: JsonLimitsError,
    },

    /// The details value exceeded a resource-safety or JSON interoperability limit.
    #[error("failure details violate JSON safety limits: {source}")]
    Json {
        /// Underlying bounded JSON violation.
        #[source]
        source: BoundedJsonError,
    },
}

impl FailureDetailsError {
    const fn limits_configuration(source: JsonLimitsError) -> Self {
        Self::LimitsConfiguration { source }
    }

    const fn json(source: BoundedJsonError) -> Self {
        Self::Json { source }
    }
}

/// Invalid relationship between failure category and recovery advice.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum FailureBuildError {
    /// An ambiguous external outcome did not require reconciliation.
    #[error("ambiguous external outcomes must use reconcile-first advice")]
    AmbiguousOutcomeRequiresReconciliation,

    /// Reconciliation advice was attached to a non-ambiguous category.
    #[error("reconcile-first advice is reserved for ambiguous external outcomes")]
    ReconciliationRequiresAmbiguousOutcome,
}

type PrivateSource = dyn StdError + Send + Sync + 'static;

/// One protocol-neutral failure occurrence.
///
/// Serialized and schema-visible fields are safe for an authorized public
/// error surface. The optional in-process source chain is deliberately absent
/// from Serde, JSON Schema, and `Debug`; it may contain private provider,
/// database, transport, or implementation diagnostics.
#[derive(Clone)]
pub struct Failure {
    id: FailureId,
    category: FailureCategory,
    code: FailureCode,
    origin: FailureOrigin,
    message: FailureMessage,
    retry_advice: RetryAdvice,
    details: Option<FailureDetails>,
    caused_by_event_id: Option<EventId>,
    private_source: Option<Arc<PrivateSource>>,
}

impl Failure {
    /// Constructs a failure from validated public components.
    ///
    /// Optional details, durable causation, and a private source chain can be
    /// attached with the consuming `with_*` methods before publication.
    ///
    /// # Errors
    ///
    /// Returns [`FailureBuildError`] if ambiguous external outcome semantics
    /// and reconciliation advice do not correspond exactly.
    pub fn new(
        id: FailureId,
        category: FailureCategory,
        code: FailureCode,
        origin: FailureOrigin,
        message: FailureMessage,
        retry_advice: RetryAdvice,
    ) -> Result<Self, FailureBuildError> {
        validate_failure_recovery(category, retry_advice)?;
        Ok(Self {
            id,
            category,
            code,
            origin,
            message,
            retry_advice,
            details: None,
            caused_by_event_id: None,
            private_source: None,
        })
    }

    /// Attaches already validated public structured details.
    #[must_use]
    pub fn with_details(mut self, details: FailureDetails) -> Self {
        self.details = Some(details);
        self
    }

    /// Attaches the durable event that directly caused this failure.
    #[must_use]
    pub fn with_caused_by_event(mut self, event_id: EventId) -> Self {
        self.caused_by_event_id = Some(event_id);
        self
    }

    /// Attaches private in-process diagnostics that never cross a wire boundary.
    #[must_use]
    pub fn with_private_source<E>(mut self, source: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        self.private_source = Some(Arc::new(source));
        self
    }

    /// Attaches an already shared private in-process diagnostic chain.
    #[must_use]
    pub fn with_shared_private_source(mut self, source: Arc<PrivateSource>) -> Self {
        self.private_source = Some(source);
        self
    }

    /// Returns the stable identifier of this occurrence.
    #[must_use]
    pub const fn id(&self) -> FailureId {
        self.id
    }

    /// Returns the protocol-neutral semantic category.
    #[must_use]
    pub const fn category(&self) -> FailureCategory {
        self.category
    }

    /// Returns the stable application- or adapter-owned code.
    #[must_use]
    pub const fn code(&self) -> &FailureCode {
        &self.code
    }

    /// Returns the component or dependency namespace that originated the failure.
    #[must_use]
    pub const fn origin(&self) -> &FailureOrigin {
        &self.origin
    }

    /// Returns the message approved for authorized public error surfaces.
    #[must_use]
    pub const fn message(&self) -> &FailureMessage {
        &self.message
    }

    /// Returns explicit recovery advice without inferring it from category.
    #[must_use]
    pub const fn retry_advice(&self) -> RetryAdvice {
        self.retry_advice
    }

    /// Returns optional schema-bound public details.
    #[must_use]
    pub const fn details(&self) -> Option<&FailureDetails> {
        self.details.as_ref()
    }

    /// Returns the durable causal event identifier, when known.
    #[must_use]
    pub const fn caused_by_event_id(&self) -> Option<EventId> {
        self.caused_by_event_id
    }

    /// Returns whether private in-process diagnostics are attached.
    #[must_use]
    pub const fn has_private_source(&self) -> bool {
        self.private_source.is_some()
    }

    /// Returns private diagnostics to trusted in-process callers only.
    #[must_use]
    pub fn private_source(&self) -> Option<&PrivateSource> {
        self.private_source.as_deref()
    }
}

fn validate_failure_recovery(
    category: FailureCategory,
    retry_advice: RetryAdvice,
) -> Result<(), FailureBuildError> {
    match (category, retry_advice) {
        (FailureCategory::AmbiguousExternalOutcome, RetryAdvice::ReconcileFirst) => Ok(()),
        (FailureCategory::AmbiguousExternalOutcome, _) => {
            Err(FailureBuildError::AmbiguousOutcomeRequiresReconciliation)
        }
        (_, RetryAdvice::ReconcileFirst) => {
            Err(FailureBuildError::ReconciliationRequiresAmbiguousOutcome)
        }
        _ => Ok(()),
    }
}

impl fmt::Debug for Failure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Failure")
            .field("id", &self.id)
            .field("category", &self.category)
            .field("code", &self.code)
            .field("origin", &self.origin)
            .field("message_utf8_bytes", &self.message.len_bytes())
            .field("retry_advice", &self.retry_advice)
            .field("has_details", &self.details.is_some())
            .field("caused_by_event_id", &self.caused_by_event_id)
            .field("has_private_source", &self.private_source.is_some())
            .finish_non_exhaustive()
    }
}

impl fmt::Display for Failure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.message, formatter)
    }
}

impl StdError for Failure {}

#[derive(Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
struct FailureWire {
    id: FailureId,
    category: FailureCategory,
    code: FailureCode,
    origin: FailureOrigin,
    message: FailureMessage,
    retry_advice: RetryAdvice,
    #[serde(default)]
    details: Option<FailureDetails>,
    #[serde(default)]
    caused_by_event_id: Option<EventId>,
}

#[derive(Serialize)]
struct FailureWireRef<'a> {
    id: FailureId,
    category: FailureCategory,
    code: &'a FailureCode,
    origin: &'a FailureOrigin,
    message: &'a FailureMessage,
    retry_advice: RetryAdvice,
    #[serde(skip_serializing_if = "Option::is_none")]
    details: Option<&'a FailureDetails>,
    #[serde(skip_serializing_if = "Option::is_none")]
    caused_by_event_id: Option<EventId>,
}

impl Serialize for Failure {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        FailureWireRef {
            id: self.id,
            category: self.category,
            code: &self.code,
            origin: &self.origin,
            message: &self.message,
            retry_advice: self.retry_advice,
            details: self.details.as_ref(),
            caused_by_event_id: self.caused_by_event_id,
        }
        .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Failure {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let wire = FailureWire::deserialize(deserializer)?;
        let mut failure = Self::new(
            wire.id,
            wire.category,
            wire.code,
            wire.origin,
            wire.message,
            wire.retry_advice,
        )
        .map_err(de::Error::custom)?;
        failure.details = wire.details;
        failure.caused_by_event_id = wire.caused_by_event_id;
        Ok(failure)
    }
}

impl JsonSchema for Failure {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "Failure".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::Failure").into()
    }

    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
        FailureWire::json_schema(generator)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Digest, SchemaId, Version};
    use serde_json::{from_value, json, to_value};

    fn schema_reference() -> SchemaReference {
        SchemaReference::new(
            "https://stateknot.github.io/schema/failure-details/1.0.0"
                .parse::<SchemaId>()
                .unwrap(),
            Version::new(1, 0, 0),
            Digest::sha256(b"failure details schema"),
        )
    }

    fn failure(category: FailureCategory, retry_advice: RetryAdvice) -> Failure {
        Failure::new(
            FailureId::generate(),
            category,
            "dependency.rate_limited".parse().unwrap(),
            "provider.openai".parse().unwrap(),
            "The dependency is temporarily unavailable."
                .parse()
                .unwrap(),
            retry_advice,
        )
        .unwrap()
    }

    #[test]
    fn stable_failure_identifiers_enforce_the_segmented_grammar() {
        for value in [
            "a",
            "rate_limited",
            "provider.openai",
            "stateknot.tool-runtime_v2",
        ] {
            assert_eq!(value.parse::<FailureCode>().unwrap().as_str(), value);
            assert_eq!(value.parse::<FailureOrigin>().unwrap().as_str(), value);
        }

        for value in [
            "",
            "A",
            "2provider",
            ".provider",
            "provider.",
            "provider..openai",
            "provider/OpenAI",
            "provider:openai",
            "provider.é",
        ] {
            assert!(value.parse::<FailureCode>().is_err(), "accepted {value:?}");
            assert!(
                value.parse::<FailureOrigin>().is_err(),
                "accepted {value:?}"
            );
        }

        let too_long = "a".repeat(FailureCode::MAX_LEN + 1);
        assert_eq!(
            too_long.parse::<FailureCode>(),
            Err(FailureIdentifierError::TooLong {
                max: FailureCode::MAX_LEN,
                actual: FailureCode::MAX_LEN + 1,
            })
        );
    }

    #[test]
    fn public_messages_are_bounded_single_line_values_with_redacted_debug() {
        let message = FailureMessage::new("请求暂时无法完成。").unwrap();
        assert_eq!(message.as_str(), "请求暂时无法完成。");
        assert_eq!(message.to_string(), "请求暂时无法完成。");
        assert!(!format!("{message:?}").contains("请求"));

        for value in [
            "",
            " padded",
            "padded ",
            "two\nlines",
            "tab\tinside",
            "line\u{2028}separator",
            "bidi\u{202e}override",
            "noncharacter\u{fdd0}",
        ] {
            assert!(FailureMessage::new(value).is_err(), "accepted {value:?}");
        }

        assert_eq!(
            FailureMessage::new("a".repeat(FailureMessage::MAX_BYTES + 1)),
            Err(FailureMessageError::TooLong {
                max: FailureMessage::MAX_BYTES,
                actual: FailureMessage::MAX_BYTES + 1,
            })
        );
    }

    #[test]
    fn categories_and_retry_advice_have_closed_canonical_wire_forms() {
        let categories = [
            (FailureCategory::InvalidInput, "invalid_input"),
            (FailureCategory::Unauthenticated, "unauthenticated"),
            (FailureCategory::PermissionDenied, "permission_denied"),
            (FailureCategory::PolicyDenied, "policy_denied"),
            (FailureCategory::NotFound, "not_found"),
            (FailureCategory::Conflict, "conflict"),
            (FailureCategory::Unsupported, "unsupported"),
            (FailureCategory::RateLimited, "rate_limited"),
            (FailureCategory::DeadlineExceeded, "deadline_exceeded"),
            (FailureCategory::Cancelled, "cancelled"),
            (
                FailureCategory::DependencyUnavailable,
                "dependency_unavailable",
            ),
            (FailureCategory::DataCorruption, "data_corruption"),
            (
                FailureCategory::AmbiguousExternalOutcome,
                "ambiguous_external_outcome",
            ),
            (FailureCategory::Internal, "internal"),
        ];
        for (category, text) in categories {
            assert_eq!(to_value(category).unwrap(), json!(text));
            assert_eq!(
                from_value::<FailureCategory>(json!(text)).unwrap(),
                category
            );
        }
        assert!(from_value::<FailureCategory>(json!("unknown")).is_err());

        let retry_forms = [
            (RetryAdvice::Never, json!({ "kind": "never" })),
            (
                RetryAdvice::SafeAfter {
                    delay: DurationMillis::new(250).unwrap(),
                },
                json!({ "kind": "safe_after", "delay": "250" }),
            ),
            (
                RetryAdvice::ReconcileFirst,
                json!({ "kind": "reconcile_first" }),
            ),
        ];
        for (advice, value) in retry_forms {
            assert_eq!(to_value(advice).unwrap(), value);
            assert_eq!(from_value::<RetryAdvice>(value).unwrap(), advice);
        }
        assert!(from_value::<RetryAdvice>(json!({ "kind": "never", "delay": "1" })).is_err());
        assert!(from_value::<RetryAdvice>(json!({ "kind": "retry" })).is_err());
    }

    #[test]
    fn failure_details_revalidate_every_tighter_json_dimension() {
        let valid = FailureDetails::try_from_value(
            schema_reference(),
            json!({ "field": "region", "reason": "unsupported" }),
        )
        .unwrap();
        assert_eq!(valid.value().as_value()["field"], "region");
        assert!(!format!("{valid:?}").contains("unsupported"));

        let long_string = Value::String("a".repeat(FailureDetails::MAX_JSON_STRING_BYTES + 1));
        assert!(matches!(
            FailureDetails::try_from_value(schema_reference(), long_string),
            Err(FailureDetailsError::Json {
                source: BoundedJsonError::StringTooLong { .. }
            })
        ));

        let many_entries = Value::Array(
            (0..=FailureDetails::MAX_JSON_CONTAINER_ENTRIES)
                .map(|value| json!(value))
                .collect(),
        );
        assert!(matches!(
            FailureDetails::try_from_value(schema_reference(), many_entries),
            Err(FailureDetailsError::Json {
                source: BoundedJsonError::TooManyContainerEntries { .. }
            })
        ));

        let mut nested = Value::Null;
        for _ in 0..=FailureDetails::MAX_JSON_DEPTH {
            nested = Value::Array(vec![nested]);
        }
        assert!(matches!(
            FailureDetails::try_from_value(schema_reference(), nested),
            Err(FailureDetailsError::Json {
                source: BoundedJsonError::NestingTooDeep { .. }
            })
        ));

        let oversized_compact = Value::Array(
            (0..5)
                .map(|_| Value::String("a".repeat(FailureDetails::MAX_JSON_STRING_BYTES)))
                .collect(),
        );
        assert!(matches!(
            FailureDetails::try_from_value(schema_reference(), oversized_compact),
            Err(FailureDetailsError::Json {
                source: BoundedJsonError::CompactRepresentationTooLarge { .. }
            })
        ));

        let too_many_nodes = Value::Array(
            (0..FailureDetails::MAX_JSON_CONTAINER_ENTRIES)
                .map(|_| Value::Array((0..8).map(|_| Value::Null).collect()))
                .collect(),
        );
        assert!(matches!(
            FailureDetails::try_from_value(schema_reference(), too_many_nodes),
            Err(FailureDetailsError::Json {
                source: BoundedJsonError::TooManyNodes { .. }
            })
        ));

        let mut oversized_key = serde_json::Map::new();
        oversized_key.insert(
            "k".repeat(FailureDetails::MAX_JSON_OBJECT_KEY_BYTES + 1),
            Value::Null,
        );
        assert!(matches!(
            FailureDetails::try_from_value(schema_reference(), Value::Object(oversized_key)),
            Err(FailureDetailsError::Json {
                source: BoundedJsonError::ObjectKeyTooLong { .. }
            })
        ));

        let limits = failure_details_json_limits().unwrap();
        assert_eq!(limits.max_bytes(), FailureDetails::MAX_JSON_BYTES);
        assert_eq!(limits.max_depth(), FailureDetails::MAX_JSON_DEPTH);
        assert_eq!(
            limits.max_container_entries(),
            FailureDetails::MAX_JSON_CONTAINER_ENTRIES
        );
        assert_eq!(limits.max_nodes(), FailureDetails::MAX_JSON_NODES);
        assert_eq!(
            limits.max_string_bytes(),
            FailureDetails::MAX_JSON_STRING_BYTES
        );
        assert_eq!(
            limits.max_object_key_bytes(),
            FailureDetails::MAX_JSON_OBJECT_KEY_BYTES
        );
    }

    #[test]
    fn ambiguous_outcomes_and_reconciliation_are_an_exact_pair() {
        assert!(
            Failure::new(
                FailureId::generate(),
                FailureCategory::AmbiguousExternalOutcome,
                "tool.outcome_unknown".parse().unwrap(),
                "tool.payments".parse().unwrap(),
                "The payment outcome must be reconciled.".parse().unwrap(),
                RetryAdvice::ReconcileFirst,
            )
            .is_ok()
        );

        assert_eq!(
            Failure::new(
                FailureId::generate(),
                FailureCategory::AmbiguousExternalOutcome,
                "tool.outcome_unknown".parse().unwrap(),
                "tool.payments".parse().unwrap(),
                "The payment outcome must be reconciled.".parse().unwrap(),
                RetryAdvice::Never,
            )
            .unwrap_err(),
            FailureBuildError::AmbiguousOutcomeRequiresReconciliation
        );
        assert_eq!(
            Failure::new(
                FailureId::generate(),
                FailureCategory::Internal,
                "runtime.invariant".parse().unwrap(),
                "stateknot.runtime".parse().unwrap(),
                "The operation could not be completed.".parse().unwrap(),
                RetryAdvice::ReconcileFirst,
            )
            .unwrap_err(),
            FailureBuildError::ReconciliationRequiresAmbiguousOutcome
        );
    }

    #[derive(Debug, Error)]
    #[error("private database diagnostics: password=secret")]
    struct PrivateDiagnostic;

    #[test]
    fn wire_debug_and_public_display_never_expose_private_diagnostics() {
        let details =
            FailureDetails::try_from_value(schema_reference(), json!({ "safe": "public detail" }))
                .unwrap();
        let event_id = EventId::generate();
        let failure = failure(FailureCategory::DependencyUnavailable, RetryAdvice::Never)
            .with_details(details)
            .with_caused_by_event(event_id)
            .with_private_source(PrivateDiagnostic);

        let encoded = to_value(&failure).unwrap();
        assert_eq!(encoded["caused_by_event_id"], event_id.to_string());
        assert!(encoded.get("private_source").is_none());
        assert!(!encoded.to_string().contains("password"));

        let debug = format!("{failure:?}");
        assert!(!debug.contains("password"));
        assert!(!debug.contains("public detail"));
        assert_eq!(
            failure.to_string(),
            "The dependency is temporarily unavailable."
        );
        assert!(StdError::source(&failure).is_none());
        assert!(failure.private_source().is_some());

        let decoded = from_value::<Failure>(encoded).unwrap();
        assert!(!decoded.has_private_source());
        assert_eq!(decoded.caused_by_event_id(), Some(event_id));
        assert_eq!(decoded.category(), failure.category());
    }

    #[test]
    fn deserialization_rejects_private_or_unknown_fields_and_bad_invariants() {
        let base = to_value(failure(FailureCategory::Internal, RetryAdvice::Never)).unwrap();
        let mut with_source = base.clone();
        with_source["private_source"] = json!("secret");
        assert!(from_value::<Failure>(with_source).is_err());

        let mut unknown = base.clone();
        unknown["extension"] = json!(true);
        assert!(from_value::<Failure>(unknown).is_err());

        let mut invalid = base;
        invalid["category"] = json!("ambiguous_external_outcome");
        assert!(from_value::<Failure>(invalid).is_err());
    }

    #[test]
    fn schemas_close_failure_objects_and_exclude_private_sources() {
        let schema = to_value(schemars::schema_for!(Failure)).unwrap();
        assert_eq!(schema["type"], "object");
        assert_eq!(schema["additionalProperties"], false);
        assert!(schema["properties"].get("private_source").is_none());
        assert!(schema["properties"].get("details").is_some());

        let details_schema = to_value(schemars::schema_for!(FailureDetails)).unwrap();
        assert_eq!(details_schema["type"], "object");
        assert_eq!(details_schema["additionalProperties"], false);

        let retry_schema = to_value(schemars::schema_for!(RetryAdvice)).unwrap();
        let variants = retry_schema["oneOf"].as_array().unwrap();
        assert_eq!(variants.len(), 3);
        assert!(
            variants
                .iter()
                .all(|variant| variant["additionalProperties"] == false),
            "retry schema was not closed: {retry_schema}"
        );

        let code_schema = to_value(schemars::schema_for!(FailureCode)).unwrap();
        assert_eq!(code_schema["pattern"], FAILURE_IDENTIFIER_PATTERN);
        assert_eq!(code_schema["maxLength"], FailureCode::MAX_LEN);

        let message_schema = to_value(schemars::schema_for!(FailureMessage)).unwrap();
        assert_eq!(message_schema["maxLength"], FailureMessage::MAX_BYTES);
    }
}