Skip to main content

cortex_reflect/
error.rs

1//! Error types for reflection parsing and quarantine handoff.
2
3use thiserror::Error;
4
5/// Reflection parser failures.
6///
7/// Every variant keeps the original input bytes as UTF-8 text so callers can
8/// write a quarantine record without reconstructing or normalizing the model
9/// response.
10#[derive(Debug, Error, Clone, PartialEq, Eq)]
11pub enum ReflectError {
12    /// The model output was not valid JSON.
13    #[error("reflection JSON parse failed: {message}")]
14    InvalidJson {
15        /// Original model output.
16        raw: String,
17        /// Parser-facing error detail.
18        message: String,
19    },
20
21    /// The JSON was syntactically valid but did not match the reflection type.
22    #[error("reflection schema validation failed: {message}")]
23    InvalidSchema {
24        /// Original model output.
25        raw: String,
26        /// Structural validation detail.
27        message: String,
28    },
29
30    /// The JSON parsed structurally but failed the memory admission gate.
31    #[error("reflection admission failed: {message}")]
32    AdmissionRejected {
33        /// Original model output.
34        raw: String,
35        /// Admission gate detail.
36        message: String,
37    },
38
39    /// The JSON parsed structurally but failed a source-authority gate.
40    #[error("reflection authority failed: {message}")]
41    AuthorityRejected {
42        /// Original model output.
43        raw: String,
44        /// Authority gate detail.
45        message: String,
46    },
47}
48
49impl ReflectError {
50    /// Original input suitable for a quarantine record.
51    #[must_use]
52    pub fn quarantine_payload(&self) -> &str {
53        match self {
54            Self::InvalidJson { raw, .. }
55            | Self::InvalidSchema { raw, .. }
56            | Self::AdmissionRejected { raw, .. }
57            | Self::AuthorityRejected { raw, .. } => raw,
58        }
59    }
60
61    /// Stable reason code suitable for audit metadata.
62    #[must_use]
63    pub const fn quarantine_reason(&self) -> &'static str {
64        match self {
65            Self::InvalidJson { .. } => "invalid_json",
66            Self::InvalidSchema { .. } => "invalid_schema",
67            Self::AdmissionRejected { .. } => "admission_rejected",
68            Self::AuthorityRejected { .. } => "authority_rejected",
69        }
70    }
71
72    /// Operator-facing detail without the original payload.
73    #[must_use]
74    pub fn detail(&self) -> &str {
75        match self {
76            Self::InvalidJson { message, .. }
77            | Self::InvalidSchema { message, .. }
78            | Self::AdmissionRejected { message, .. }
79            | Self::AuthorityRejected { message, .. } => message,
80        }
81    }
82}