Skip to main content

aecr_sync/
error.rs

1//! Error types for ISO 19650 CDE workflows, workset borrow leases, and delta synchronization.
2
3use aecr_pig::{ActorId, ContentHash, Diagnostic, NodeId, PigError};
4use thiserror::Error;
5
6use crate::iso19650::{CdeState, SuitabilityCode};
7
8/// Errors returned by ISO 19650 container validation, promotion gates, worksets, and delta sync.
9#[derive(Debug, Error)]
10pub enum SyncError {
11    /// ISO 19650-2 container identifier failed token format validation.
12    #[error("invalid ISO 19650 container id: {0}")]
13    InvalidContainerId(String),
14
15    /// Requested CDE state transition or suitability code violates the ISO 19650 lifecycle matrix.
16    #[error("invalid CDE transition from {from} to {to} with suitability {suitability}")]
17    InvalidTransition {
18        /// Current container lifecycle state.
19        from: CdeState,
20        /// Requested target lifecycle state.
21        to: CdeState,
22        /// Suitability code supplied for the transition.
23        suitability: SuitabilityCode,
24    },
25
26    /// Mutation or non-archival transition rejected because the container is immutable.
27    #[error("container in state {0} is immutable")]
28    ContainerLocked(CdeState),
29
30    /// WIP-to-Shared promotion rejected by [`aecr_pig::VerificationPipeline`].
31    #[error("promotion to Shared blocked: {0:?}")]
32    PromotionBlocked(Vec<Diagnostic>),
33
34    /// Node cannot be edited or acquired because another actor holds an exclusive borrow lease.
35    #[error("element {node} is borrowed by actor '{owner}'")]
36    ElementBorrowed {
37        /// Contended PIG node identifier.
38        node: NodeId,
39        /// Actor currently holding the exclusive borrow lease.
40        owner: ActorId,
41    },
42
43    /// Delta base root hash does not match any active revision in local Command Bus history.
44    #[error("unknown base root hash {0}")]
45    UnknownBaseHash(ContentHash),
46
47    /// Merkle root hash chain mismatch detected while validating or applying a delta envelope.
48    #[error("root hash mismatch: expected {expected}, actual {actual}")]
49    HashMismatch {
50        /// Expected SHA-256 Merkle root hash.
51        expected: ContentHash,
52        /// Observed SHA-256 Merkle root hash.
53        actual: ContentHash,
54    },
55
56    /// Underlying Parametric Intent Graph or Command Bus error.
57    #[error("PIG error: {0}")]
58    Pig(#[from] PigError),
59}
60
61#[cfg(test)]
62mod tests {
63    use aecr_pig::{ActorId, ContentHash, Diagnostic, DiagnosticSeverity, NodeId, PigError};
64
65    use super::SyncError;
66    use crate::iso19650::{CdeState, SuitabilityCode};
67
68    #[test]
69    fn sync_error_variants_format_cleanly() {
70        let bad_id = SyncError::InvalidContainerId("bad-id".to_owned());
71        assert!(bad_id.to_string().contains("bad-id"));
72
73        let bad_tx = SyncError::InvalidTransition {
74            from: CdeState::WorkInProgress,
75            to: CdeState::Published,
76            suitability: SuitabilityCode::A1,
77        };
78        assert!(bad_tx.to_string().contains("WorkInProgress"));
79        assert!(bad_tx.to_string().contains("Published"));
80        assert!(bad_tx.to_string().contains("A1"));
81
82        let locked = SyncError::ContainerLocked(CdeState::Archived);
83        assert!(locked.to_string().contains("Archived"));
84
85        let blocked = SyncError::PromotionBlocked(vec![Diagnostic {
86            node: Some(NodeId(4)),
87            code: "CHECK_FAILED".to_owned(),
88            message: "clash detected".to_owned(),
89            severity: DiagnosticSeverity::Error,
90        }]);
91        assert!(blocked.to_string().contains("CHECK_FAILED"));
92
93        let borrowed = SyncError::ElementBorrowed {
94            node: NodeId(7),
95            owner: ActorId::new("architect-1"),
96        };
97        assert!(borrowed.to_string().contains("7"));
98        assert!(borrowed.to_string().contains("architect-1"));
99
100        let unknown = SyncError::UnknownBaseHash(ContentHash::ZERO);
101        assert!(unknown.to_string().contains(&ContentHash::ZERO.to_hex()));
102
103        let mismatch = SyncError::HashMismatch {
104            expected: ContentHash::ZERO,
105            actual: ContentHash::from_bytes([1_u8; 32]),
106        };
107        assert!(mismatch.to_string().contains("mismatch"));
108
109        let pig_err = SyncError::from(PigError::NodeNotFound(NodeId(9)));
110        assert!(matches!(pig_err, SyncError::Pig(_)));
111        assert!(pig_err.to_string().contains("9"));
112    }
113}